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