snprintf.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2018 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Author: Stig Sæther Bakken <ssb@php.net> |
  16. | Marcus Boerger <helly@php.net> |
  17. +----------------------------------------------------------------------+
  18. */
  19. /*
  20. Comparing: sprintf, snprintf, slprintf, spprintf
  21. sprintf offers the ability to make a lot of failures since it does not know
  22. the size of the buffer it uses. Therefore usage of sprintf often
  23. results in possible entries for buffer overrun attacks. So please
  24. use this version only if you are sure the call is safe. sprintf
  25. always terminstes the buffer it writes to.
  26. snprintf knows the buffers size and will not write behind it. But you will
  27. have to use either a static buffer or allocate a dynamic buffer
  28. before being able to call the function. In other words you must
  29. be sure that you really know the maximum size of the buffer required.
  30. A bad thing is having a big maximum while in most cases you would
  31. only need a small buffer. If the size of the resulting string is
  32. longer or equal to the buffer size than the buffer is not terminated.
  33. The function also returns the number of chars not including the
  34. terminating \0 that were needed to fully comply to the print request.
  35. slprintf same as snprintf with the difference that it actually returns the
  36. length printed not including the terminating \0.
  37. spprintf is the dynamical version of snprintf. It allocates the buffer in size
  38. as needed and allows a maximum setting as snprintf (turn this feature
  39. off by setting max_len to 0). spprintf is a little bit slower than
  40. snprintf and offers possible memory leakes if you miss freeing the
  41. buffer allocated by the function. Therfore this function should be
  42. used where either no maximum is known or the maximum is much bigger
  43. than normal size required. spprintf always terminates the buffer.
  44. Example:
  45. #define MAX 1024 | #define MAX 1024 | #define MAX 1024
  46. char buffer[MAX] | char buffer[MAX] | char *buffer;
  47. | |
  48. | | // No need to initialize buffer:
  49. | | // spprintf ignores value of buffer
  50. sprintf(buffer, "test"); | snprintf(buffer, MAX, "test"); | spprintf(&buffer, MAX, "text");
  51. | | if (!buffer)
  52. | | return OUT_OF_MEMORY
  53. // sprintf always terminates | // manual termination of | // spprintf allays terminates buffer
  54. // buffer | // buffer *IS* required |
  55. | buffer[MAX-1] = 0; |
  56. action_with_buffer(buffer); | action_with_buffer(buffer); | action_with_buffer(buffer);
  57. | | efree(buffer);
  58. */
  59. #ifndef SNPRINTF_H
  60. #define SNPRINTF_H
  61. typedef int bool_int;
  62. typedef enum {
  63. NO = 0, YES = 1
  64. } boolean_e;
  65. BEGIN_EXTERN_C()
  66. PHPAPI int ap_php_slprintf(char *buf, size_t len, const char *format,...) ZEND_ATTRIBUTE_FORMAT(printf, 3, 4);
  67. PHPAPI int ap_php_vslprintf(char *buf, size_t len, const char *format, va_list ap);
  68. PHPAPI int ap_php_snprintf(char *, size_t, const char *, ...) ZEND_ATTRIBUTE_FORMAT(printf, 3, 4);
  69. PHPAPI int ap_php_vsnprintf(char *, size_t, const char *, va_list ap);
  70. PHPAPI int ap_php_vasprintf(char **buf, const char *format, va_list ap);
  71. PHPAPI int ap_php_asprintf(char **buf, const char *format, ...) ZEND_ATTRIBUTE_FORMAT(printf, 2, 3);
  72. PHPAPI int php_sprintf (char* s, const char* format, ...) PHP_ATTRIBUTE_FORMAT(printf, 2, 3);
  73. PHPAPI char * php_gcvt(double value, int ndigit, char dec_point, char exponent, char *buf);
  74. PHPAPI char * php_0cvt(double value, int ndigit, char dec_point, char exponent, char *buf);
  75. PHPAPI char * php_conv_fp(char format, double num,
  76. boolean_e add_dp, int precision, char dec_point, bool_int * is_negative, char *buf, size_t *len);
  77. END_EXTERN_C()
  78. #ifdef slprintf
  79. #undef slprintf
  80. #endif
  81. #define slprintf ap_php_slprintf
  82. #ifdef vslprintf
  83. #undef vslprintf
  84. #endif
  85. #define vslprintf ap_php_vslprintf
  86. #ifdef snprintf
  87. #undef snprintf
  88. #endif
  89. #define snprintf ap_php_snprintf
  90. #ifdef vsnprintf
  91. #undef vsnprintf
  92. #endif
  93. #define vsnprintf ap_php_vsnprintf
  94. #ifndef HAVE_VASPRINTF
  95. #define vasprintf ap_php_vasprintf
  96. #endif
  97. #ifndef HAVE_ASPRINTF
  98. #define asprintf ap_php_asprintf
  99. #endif
  100. #ifdef sprintf
  101. #undef sprintf
  102. #endif
  103. #define sprintf php_sprintf
  104. typedef enum {
  105. LM_STD = 0,
  106. #if SIZEOF_INTMAX_T
  107. LM_INTMAX_T,
  108. #endif
  109. #if SIZEOF_PTRDIFF_T
  110. LM_PTRDIFF_T,
  111. #endif
  112. #if SIZEOF_LONG_LONG
  113. LM_LONG_LONG,
  114. #endif
  115. LM_SIZE_T,
  116. LM_LONG,
  117. LM_LONG_DOUBLE,
  118. LM_PHP_INT_T
  119. } length_modifier_e;
  120. #ifdef PHP_WIN32
  121. # define WIDE_INT __int64
  122. #elif SIZEOF_LONG_LONG_INT
  123. # define WIDE_INT long long int
  124. #elif SIZEOF_LONG_LONG
  125. # define WIDE_INT long long
  126. #else
  127. # define WIDE_INT long
  128. #endif
  129. typedef WIDE_INT wide_int;
  130. typedef unsigned WIDE_INT u_wide_int;
  131. PHPAPI char * ap_php_conv_10(wide_int num, bool_int is_unsigned,
  132. bool_int * is_negative, char *buf_end, size_t *len);
  133. PHPAPI char * ap_php_conv_p2(u_wide_int num, int nbits,
  134. char format, char *buf_end, size_t *len);
  135. /* The maximum precision that's allowed for float conversion. Does not include
  136. * decimal separator, exponent, sign, terminator. Currently does not affect
  137. * the modes e/f, only g/k/H, as those have a different limit enforced at
  138. * another level (see NDIG in php_conv_fp()).
  139. * Applies to the formatting functions of both spprintf.c and snprintf.c, which
  140. * use equally sized buffers of MAX_BUF_SIZE = 512 to hold the result of the
  141. * call to php_gcvt().
  142. * This should be reasonably smaller than MAX_BUF_SIZE (I think MAX_BUF_SIZE - 9
  143. * should be enough, but let's give some more space) */
  144. #define FORMAT_CONV_MAX_PRECISION 500
  145. #endif /* SNPRINTF_H */
  146. /*
  147. * Local variables:
  148. * tab-width: 4
  149. * c-basic-offset: 4
  150. * End:
  151. * vim600: sw=4 ts=4 fdm=marker
  152. * vim<600: sw=4 ts=4
  153. */