Back to index

php5  5.3.10
snprintf.h
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | PHP Version 5                                                        |
00004    +----------------------------------------------------------------------+
00005    | Copyright (c) 1997-2012 The PHP Group                                |
00006    +----------------------------------------------------------------------+
00007    | This source file is subject to version 3.01 of the PHP license,      |
00008    | that is bundled with this package in the file LICENSE, and is        |
00009    | available through the world-wide-web at the following url:           |
00010    | http://www.php.net/license/3_01.txt                                  |
00011    | If you did not receive a copy of the PHP license and are unable to   |
00012    | obtain it through the world-wide-web, please send a note to          |
00013    | license@php.net so we can mail you a copy immediately.               |
00014    +----------------------------------------------------------------------+
00015    | Author: Stig Sæther Bakken <ssb@php.net>                             |
00016    |         Marcus Boerger <helly@php.net>                               |
00017    +----------------------------------------------------------------------+
00018 */
00019 
00020 /* $Id: snprintf.h 321634 2012-01-01 13:15:04Z felipe $ */
00021 
00022 /*
00023 
00024 Comparing: sprintf, snprintf, slprintf, spprintf
00025 
00026 sprintf  offers the ability to make a lot of failures since it does not know
00027          the size of the buffer it uses. Therefore usage of sprintf often
00028          results in possible entries for buffer overrun attacks. So please
00029          use this version only if you are sure the call is safe. sprintf
00030          allways terminstes the buffer it writes to.
00031 
00032 snprintf knows the buffers size and will not write behind it. But you will
00033          have to use either a static buffer or allocate a dynamic buffer
00034          before beeing able to call the function. In other words you must
00035          be sure that you really know the maximum size of the buffer required.
00036          A bad thing is having a big maximum while in most cases you would
00037          only need a small buffer. If the size of the resulting string is
00038          longer or equal to the buffer size than the buffer is not terminated.
00039          The function also returns the number of chars not including the
00040          terminating \0 that were needed to fully comply to the print request.
00041 
00042 slprintf same as snprintf with the difference that it actually returns the
00043          length printed not including the terminating \0.
00044 
00045 spprintf is the dynamical version of snprintf. It allocates the buffer in size
00046          as needed and allows a maximum setting as snprintf (turn this feature
00047          off by setting max_len to 0). spprintf is a little bit slower than
00048          snprintf and offers possible memory leakes if you miss freeing the
00049          buffer allocated by the function. Therfore this function should be
00050          used where either no maximum is known or the maximum is much bigger
00051          than normal size required. spprintf allways terminates the buffer.
00052 
00053 Example:
00054 
00055  #define MAX 1024              | #define MAX 1024               | #define MAX 1024
00056  char buffer[MAX]              | char buffer[MAX]               | char *buffer;
00057                                |                                |
00058                                |                                | // No need to initialize buffer:
00059                                |                                | // spprintf ignores value of buffer
00060  sprintf(buffer, "test");      | snprintf(buffer, MAX, "test"); | spprintf(&buffer, MAX, "text");
00061                                |                                | if (!buffer)
00062                                |                                |   return OUT_OF_MEMORY
00063  // sprintf allways terminates | // manual termination of       | // spprintf allays terminates buffer
00064  // buffer                     | // buffer *IS* required        |
00065                                | buffer[MAX-1] = 0;             |
00066  action_with_buffer(buffer);   | action_with_buffer(buffer);    | action_with_buffer(buffer);
00067                                |                                | efree(buffer);
00068 */
00069 
00070 #ifndef SNPRINTF_H
00071 #define SNPRINTF_H
00072 
00073 typedef int bool_int;
00074 
00075 typedef enum {
00076        NO = 0, YES = 1
00077 } boolean_e;
00078 
00079 
00080 BEGIN_EXTERN_C()
00081 PHPAPI int ap_php_slprintf(char *buf, size_t len, const char *format,...);
00082 PHPAPI int ap_php_vslprintf(char *buf, size_t len, const char *format, va_list ap);
00083 PHPAPI int ap_php_snprintf(char *, size_t, const char *, ...);
00084 PHPAPI int ap_php_vsnprintf(char *, size_t, const char *, va_list ap);
00085 PHPAPI int ap_php_vasprintf(char **buf, const char *format, va_list ap);
00086 PHPAPI int ap_php_asprintf(char **buf, const char *format, ...);
00087 PHPAPI int php_sprintf (char* s, const char* format, ...) PHP_ATTRIBUTE_FORMAT(printf, 2, 3);
00088 PHPAPI char * php_gcvt(double value, int ndigit, char dec_point, char exponent, char *buf);
00089 PHPAPI char * php_conv_fp(register char format, register double num,
00090                boolean_e add_dp, int precision, char dec_point, bool_int * is_negative, char *buf, int *len);
00091 
00092 END_EXTERN_C()
00093 
00094 #ifdef slprintf
00095 #undef slprintf
00096 #endif
00097 #define slprintf ap_php_slprintf
00098 
00099 #ifdef vslprintf
00100 #undef vslprintf
00101 #endif
00102 #define vslprintf ap_php_vslprintf
00103 
00104 #ifdef snprintf
00105 #undef snprintf
00106 #endif
00107 #define snprintf ap_php_snprintf
00108 
00109 #ifdef vsnprintf
00110 #undef vsnprintf
00111 #endif
00112 #define vsnprintf ap_php_vsnprintf
00113 
00114 #ifndef HAVE_VASPRINTF
00115 #define vasprintf ap_php_vasprintf
00116 #endif
00117 
00118 #ifndef HAVE_ASPRINTF
00119 #define asprintf ap_php_asprintf
00120 #endif
00121 
00122 #ifdef sprintf
00123 #undef sprintf
00124 #endif
00125 #define sprintf php_sprintf
00126 
00127 typedef enum {
00128        LM_STD = 0,
00129 #if SIZEOF_INTMAX_T
00130        LM_INTMAX_T,
00131 #endif
00132 #if SIZEOF_PTRDIFF_T
00133        LM_PTRDIFF_T,
00134 #endif
00135 #if SIZEOF_LONG_LONG
00136        LM_LONG_LONG,
00137 #endif
00138        LM_SIZE_T,
00139        LM_LONG,
00140        LM_LONG_DOUBLE
00141 } length_modifier_e;
00142 
00143 #ifdef PHP_WIN32
00144 # define WIDE_INT           __int64
00145 #elif SIZEOF_LONG_LONG_INT
00146 # define WIDE_INT           long long int
00147 #elif SIZEOF_LONG_LONG
00148 # define WIDE_INT           long long
00149 #else
00150 # define WIDE_INT           long
00151 #endif
00152 typedef WIDE_INT wide_int;
00153 typedef unsigned WIDE_INT u_wide_int;
00154 
00155 extern char * ap_php_conv_10(register wide_int num, register bool_int is_unsigned,
00156           register bool_int * is_negative, char *buf_end, register int *len);
00157 
00158 extern char * ap_php_conv_p2(register u_wide_int num, register int nbits,
00159                char format, char *buf_end, register int *len);
00160 
00161 /* The maximum precision that's allowed for float conversion. Does not include
00162  * decimal separator, exponent, sign, terminator. Currently does not affect
00163  * the modes e/f, only g/k/H, as those have a different limit enforced at
00164  * another level (see NDIG in php_conv_fp()).
00165  * Applies to the formatting functions of both spprintf.c and snprintf.c, which
00166  * use equally sized buffers of MAX_BUF_SIZE = 512 to hold the result of the
00167  * call to php_gcvt().
00168  * This should be reasonably smaller than MAX_BUF_SIZE (I think MAX_BUF_SIZE - 9
00169  * should be enough, but let's give some more space) */
00170 #define FORMAT_CONV_MAX_PRECISION 500
00171 
00172 #endif /* SNPRINTF_H */
00173 
00174 /*
00175  * Local variables:
00176  * tab-width: 4
00177  * c-basic-offset: 4
00178  * End:
00179  */