1 /* 2 +----------------------------------------------------------------------+ 3 | PHP Version 7 | 4 +----------------------------------------------------------------------+ 5 | Copyright (c) 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. Therefore 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 char * php_gcvt(double value, int ndigit, char dec_point, char exponent, char *buf); 86 PHPAPI char * php_0cvt(double value, int ndigit, char dec_point, char exponent, char *buf); 87 PHPAPI char * php_conv_fp(char format, double num, 88 boolean_e add_dp, int precision, char dec_point, bool_int * is_negative, char *buf, size_t *len); 89 90 END_EXTERN_C() 91 92 #ifdef slprintf 93 #undef slprintf 94 #endif 95 #define slprintf ap_php_slprintf 96 97 #ifdef vslprintf 98 #undef vslprintf 99 #endif 100 #define vslprintf ap_php_vslprintf 101 102 #ifdef snprintf 103 #undef snprintf 104 #endif 105 #define snprintf ap_php_snprintf 106 107 #ifdef vsnprintf 108 #undef vsnprintf 109 #endif 110 #define vsnprintf ap_php_vsnprintf 111 112 #ifndef HAVE_VASPRINTF 113 #define vasprintf ap_php_vasprintf 114 #endif 115 116 #ifndef HAVE_ASPRINTF 117 #define asprintf ap_php_asprintf 118 #endif 119 120 typedef enum { 121 LM_STD = 0, 122 #if SIZEOF_INTMAX_T 123 LM_INTMAX_T, 124 #endif 125 #if SIZEOF_PTRDIFF_T 126 LM_PTRDIFF_T, 127 #endif 128 #if SIZEOF_LONG_LONG 129 LM_LONG_LONG, 130 #endif 131 LM_SIZE_T, 132 LM_LONG, 133 LM_LONG_DOUBLE, 134 LM_PHP_INT_T 135 } length_modifier_e; 136 137 #ifdef PHP_WIN32 138 # define WIDE_INT __int64 139 #elif SIZEOF_LONG_LONG 140 # define WIDE_INT long long 141 #else 142 # define WIDE_INT long 143 #endif 144 typedef WIDE_INT wide_int; 145 typedef unsigned WIDE_INT u_wide_int; 146 147 PHPAPI char * ap_php_conv_10(wide_int num, bool_int is_unsigned, 148 bool_int * is_negative, char *buf_end, size_t *len); 149 150 PHPAPI char * ap_php_conv_p2(u_wide_int num, int nbits, 151 char format, char *buf_end, size_t *len); 152 153 /* The maximum precision that's allowed for float conversion. Does not include 154 * decimal separator, exponent, sign, terminator. Currently does not affect 155 * the modes e/f, only g/k/H, as those have a different limit enforced at 156 * another level (see NDIG in php_conv_fp()). 157 * Applies to the formatting functions of both spprintf.c and snprintf.c, which 158 * use equally sized buffers of MAX_BUF_SIZE = 512 to hold the result of the 159 * call to php_gcvt(). 160 * This should be reasonably smaller than MAX_BUF_SIZE (I think MAX_BUF_SIZE - 9 161 * should be enough, but let's give some more space) */ 162 #define FORMAT_CONV_MAX_PRECISION 500 163 164 #endif /* SNPRINTF_H */ 165