snprintf.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Author: Stig Sæther Bakken <ssb@php.net> |
  14. | Marcus Boerger <helly@php.net> |
  15. +----------------------------------------------------------------------+
  16. */
  17. /*
  18. Comparing: sprintf, snprintf, slprintf, spprintf
  19. sprintf offers the ability to make a lot of failures since it does not know
  20. the size of the buffer it uses. Therefore usage of sprintf often
  21. results in possible entries for buffer overrun attacks. So please
  22. use this version only if you are sure the call is safe. sprintf
  23. always terminates the buffer it writes to.
  24. snprintf knows the buffers size and will not write behind it. But you will
  25. have to use either a static buffer or allocate a dynamic buffer
  26. before being able to call the function. In other words you must
  27. be sure that you really know the maximum size of the buffer required.
  28. A bad thing is having a big maximum while in most cases you would
  29. only need a small buffer. If the size of the resulting string is
  30. longer or equal to the buffer size than the buffer is not terminated.
  31. The function also returns the number of chars not including the
  32. terminating \0 that were needed to fully comply to the print request.
  33. slprintf same as snprintf with the difference that it actually returns the
  34. length printed not including the terminating \0.
  35. spprintf is the dynamical version of snprintf. It allocates the buffer in size
  36. as needed and allows a maximum setting as snprintf (turn this feature
  37. off by setting max_len to 0). spprintf is a little bit slower than
  38. snprintf and offers possible memory leaks if you miss freeing the
  39. buffer allocated by the function. Therefore this function should be
  40. used where either no maximum is known or the maximum is much bigger
  41. than normal size required. spprintf always terminates the buffer.
  42. Example:
  43. #define MAX 1024 | #define MAX 1024 | #define MAX 1024
  44. char buffer[MAX] | char buffer[MAX] | char *buffer;
  45. | |
  46. | | // No need to initialize buffer:
  47. | | // spprintf ignores value of buffer
  48. sprintf(buffer, "test"); | snprintf(buffer, MAX, "test"); | spprintf(&buffer, MAX, "text");
  49. | | if (!buffer)
  50. | | return OUT_OF_MEMORY
  51. // sprintf always terminates | // manual termination of | // spprintf allays terminates buffer
  52. // buffer | // buffer *IS* required |
  53. | buffer[MAX-1] = 0; |
  54. action_with_buffer(buffer); | action_with_buffer(buffer); | action_with_buffer(buffer);
  55. | | efree(buffer);
  56. */
  57. #ifndef SNPRINTF_H
  58. #define SNPRINTF_H
  59. #include <stdbool.h>
  60. BEGIN_EXTERN_C()
  61. PHPAPI int ap_php_slprintf(char *buf, size_t len, const char *format,...) ZEND_ATTRIBUTE_FORMAT(printf, 3, 4);
  62. PHPAPI int ap_php_vslprintf(char *buf, size_t len, const char *format, va_list ap);
  63. PHPAPI int ap_php_snprintf(char *, size_t, const char *, ...) ZEND_ATTRIBUTE_FORMAT(printf, 3, 4);
  64. PHPAPI int ap_php_vsnprintf(char *, size_t, const char *, va_list ap);
  65. PHPAPI int ap_php_vasprintf(char **buf, const char *format, va_list ap);
  66. PHPAPI int ap_php_asprintf(char **buf, const char *format, ...) ZEND_ATTRIBUTE_FORMAT(printf, 2, 3);
  67. PHPAPI char * php_0cvt(double value, int ndigit, char dec_point, char exponent, char *buf);
  68. PHPAPI char * php_conv_fp(char format, double num,
  69. bool add_dp, int precision, char dec_point, bool * is_negative, char *buf, size_t *len);
  70. END_EXTERN_C()
  71. #define php_gcvt zend_gcvt
  72. #ifdef slprintf
  73. #undef slprintf
  74. #endif
  75. #define slprintf ap_php_slprintf
  76. #ifdef vslprintf
  77. #undef vslprintf
  78. #endif
  79. #define vslprintf ap_php_vslprintf
  80. #ifdef snprintf
  81. #undef snprintf
  82. #endif
  83. #define snprintf ap_php_snprintf
  84. #ifdef vsnprintf
  85. #undef vsnprintf
  86. #endif
  87. #define vsnprintf ap_php_vsnprintf
  88. #ifndef HAVE_VASPRINTF
  89. #define vasprintf ap_php_vasprintf
  90. #endif
  91. #ifndef HAVE_ASPRINTF
  92. #define asprintf ap_php_asprintf
  93. #endif
  94. typedef enum {
  95. LM_STD = 0,
  96. #if SIZEOF_INTMAX_T
  97. LM_INTMAX_T,
  98. #endif
  99. #if SIZEOF_PTRDIFF_T
  100. LM_PTRDIFF_T,
  101. #endif
  102. #if SIZEOF_LONG_LONG
  103. LM_LONG_LONG,
  104. #endif
  105. LM_SIZE_T,
  106. LM_LONG,
  107. LM_LONG_DOUBLE,
  108. } length_modifier_e;
  109. PHPAPI char * ap_php_conv_10(int64_t num, bool is_unsigned,
  110. bool * is_negative, char *buf_end, size_t *len);
  111. PHPAPI char * ap_php_conv_p2(uint64_t num, int nbits,
  112. char format, char *buf_end, size_t *len);
  113. #endif /* SNPRINTF_H */