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