1999-04-08 05:05:13 +08:00
|
|
|
/*
|
|
|
|
+----------------------------------------------------------------------+
|
2014-09-20 00:33:14 +08:00
|
|
|
| PHP Version 7 |
|
1999-04-08 05:05:13 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2015-01-15 23:27:30 +08:00
|
|
|
| Copyright (c) 1997-2015 The PHP Group |
|
1999-04-08 05:05:13 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2006-01-01 20:51:34 +08:00
|
|
|
| This source file is subject to version 3.01 of the PHP license, |
|
1999-07-16 21:13:16 +08:00
|
|
|
| that is bundled with this package in the file LICENSE, and is |
|
2003-06-11 04:04:29 +08:00
|
|
|
| available through the world-wide-web at the following url: |
|
2006-01-01 20:51:34 +08:00
|
|
|
| http://www.php.net/license/3_01.txt |
|
1999-07-16 21:13:16 +08:00
|
|
|
| If you did not receive a copy of the PHP license and are unable to |
|
|
|
|
| obtain it through the world-wide-web, please send a note to |
|
|
|
|
| license@php.net so we can mail you a copy immediately. |
|
1999-04-08 05:05:13 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2011-02-21 14:53:24 +08:00
|
|
|
| Author: Stig Sæther Bakken <ssb@php.net> |
|
2005-07-09 04:19:38 +08:00
|
|
|
| Marcus Boerger <helly@php.net> |
|
1999-04-08 05:05:13 +08:00
|
|
|
+----------------------------------------------------------------------+
|
2003-02-19 16:40:19 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* $Id$ */
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2002-06-24 06:16:35 +08:00
|
|
|
/*
|
|
|
|
|
2008-02-07 20:47:44 +08:00
|
|
|
Comparing: sprintf, snprintf, slprintf, spprintf
|
2002-06-24 06:16:35 +08:00
|
|
|
|
|
|
|
sprintf offers the ability to make a lot of failures since it does not know
|
|
|
|
the size of the buffer it uses. Therefore usage of sprintf often
|
|
|
|
results in possible entries for buffer overrun attacks. So please
|
|
|
|
use this version only if you are sure the call is safe. sprintf
|
2013-07-13 20:37:04 +08:00
|
|
|
always terminstes the buffer it writes to.
|
2002-06-24 06:16:35 +08:00
|
|
|
|
|
|
|
snprintf knows the buffers size and will not write behind it. But you will
|
|
|
|
have to use either a static buffer or allocate a dynamic buffer
|
2014-11-20 03:59:31 +08:00
|
|
|
before being able to call the function. In other words you must
|
2002-06-24 06:16:35 +08:00
|
|
|
be sure that you really know the maximum size of the buffer required.
|
|
|
|
A bad thing is having a big maximum while in most cases you would
|
2008-02-07 20:47:44 +08:00
|
|
|
only need a small buffer. If the size of the resulting string is
|
2002-06-24 06:16:35 +08:00
|
|
|
longer or equal to the buffer size than the buffer is not terminated.
|
2008-02-07 20:47:44 +08:00
|
|
|
The function also returns the number of chars not including the
|
2007-02-25 02:20:46 +08:00
|
|
|
terminating \0 that were needed to fully comply to the print request.
|
|
|
|
|
2008-02-07 20:47:44 +08:00
|
|
|
slprintf same as snprintf with the difference that it actually returns the
|
2007-02-25 02:20:46 +08:00
|
|
|
length printed not including the terminating \0.
|
2002-06-24 06:16:35 +08:00
|
|
|
|
|
|
|
spprintf is the dynamical version of snprintf. It allocates the buffer in size
|
|
|
|
as needed and allows a maximum setting as snprintf (turn this feature
|
|
|
|
off by setting max_len to 0). spprintf is a little bit slower than
|
2008-02-07 20:47:44 +08:00
|
|
|
snprintf and offers possible memory leakes if you miss freeing the
|
|
|
|
buffer allocated by the function. Therfore this function should be
|
2002-06-24 06:16:35 +08:00
|
|
|
used where either no maximum is known or the maximum is much bigger
|
2013-07-13 20:37:04 +08:00
|
|
|
than normal size required. spprintf always terminates the buffer.
|
2002-06-24 06:16:35 +08:00
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
#define MAX 1024 | #define MAX 1024 | #define MAX 1024
|
|
|
|
char buffer[MAX] | char buffer[MAX] | char *buffer;
|
|
|
|
| |
|
|
|
|
| | // No need to initialize buffer:
|
|
|
|
| | // spprintf ignores value of buffer
|
|
|
|
sprintf(buffer, "test"); | snprintf(buffer, MAX, "test"); | spprintf(&buffer, MAX, "text");
|
|
|
|
| | if (!buffer)
|
|
|
|
| | return OUT_OF_MEMORY
|
2013-07-13 20:37:04 +08:00
|
|
|
// sprintf always terminates | // manual termination of | // spprintf allays terminates buffer
|
2008-02-07 20:47:44 +08:00
|
|
|
// buffer | // buffer *IS* required |
|
|
|
|
| buffer[MAX-1] = 0; |
|
2002-06-24 06:16:35 +08:00
|
|
|
action_with_buffer(buffer); | action_with_buffer(buffer); | action_with_buffer(buffer);
|
|
|
|
| | efree(buffer);
|
|
|
|
*/
|
|
|
|
|
2000-07-03 07:46:51 +08:00
|
|
|
#ifndef SNPRINTF_H
|
|
|
|
#define SNPRINTF_H
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2006-12-19 19:54:38 +08:00
|
|
|
typedef int bool_int;
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
NO = 0, YES = 1
|
|
|
|
} boolean_e;
|
|
|
|
|
|
|
|
|
2005-03-31 15:37:39 +08:00
|
|
|
BEGIN_EXTERN_C()
|
2007-04-07 03:25:52 +08:00
|
|
|
PHPAPI int ap_php_slprintf(char *buf, size_t len, const char *format,...);
|
|
|
|
PHPAPI int ap_php_vslprintf(char *buf, size_t len, const char *format, va_list ap);
|
|
|
|
PHPAPI int ap_php_snprintf(char *, size_t, const char *, ...);
|
|
|
|
PHPAPI int ap_php_vsnprintf(char *, size_t, const char *, va_list ap);
|
2008-11-22 06:05:03 +08:00
|
|
|
PHPAPI int ap_php_vasprintf(char **buf, const char *format, va_list ap);
|
2008-11-28 03:45:27 +08:00
|
|
|
PHPAPI int ap_php_asprintf(char **buf, const char *format, ...);
|
2014-09-03 21:22:08 +08:00
|
|
|
PHPAPI int php_sprintf (char* s, const char* format, ...) PHP_ATTRIBUTE_FORMAT(printf, 2, 3);
|
2006-12-19 19:54:38 +08:00
|
|
|
PHPAPI char * php_gcvt(double value, int ndigit, char dec_point, char exponent, char *buf);
|
|
|
|
PHPAPI char * php_conv_fp(register char format, register double num,
|
2014-08-26 02:22:49 +08:00
|
|
|
boolean_e add_dp, int precision, char dec_point, bool_int * is_negative, char *buf, size_t *len);
|
2006-12-19 19:54:38 +08:00
|
|
|
|
2005-03-31 15:37:39 +08:00
|
|
|
END_EXTERN_C()
|
|
|
|
|
2007-02-25 02:20:46 +08:00
|
|
|
#ifdef slprintf
|
|
|
|
#undef slprintf
|
|
|
|
#endif
|
|
|
|
#define slprintf ap_php_slprintf
|
|
|
|
|
|
|
|
#ifdef vslprintf
|
|
|
|
#undef vslprintf
|
|
|
|
#endif
|
|
|
|
#define vslprintf ap_php_vslprintf
|
|
|
|
|
2004-11-16 05:04:09 +08:00
|
|
|
#ifdef snprintf
|
|
|
|
#undef snprintf
|
|
|
|
#endif
|
2000-02-11 03:41:21 +08:00
|
|
|
#define snprintf ap_php_snprintf
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2004-11-16 05:04:09 +08:00
|
|
|
#ifdef vsnprintf
|
|
|
|
#undef vsnprintf
|
|
|
|
#endif
|
2000-02-11 03:41:21 +08:00
|
|
|
#define vsnprintf ap_php_vsnprintf
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2008-11-28 03:45:27 +08:00
|
|
|
#ifndef HAVE_VASPRINTF
|
2008-11-22 06:05:03 +08:00
|
|
|
#define vasprintf ap_php_vasprintf
|
2008-11-28 03:45:27 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef HAVE_ASPRINTF
|
|
|
|
#define asprintf ap_php_asprintf
|
|
|
|
#endif
|
2008-11-22 06:05:03 +08:00
|
|
|
|
2004-11-16 05:04:09 +08:00
|
|
|
#ifdef sprintf
|
|
|
|
#undef sprintf
|
|
|
|
#endif
|
2000-03-13 01:18:21 +08:00
|
|
|
#define sprintf php_sprintf
|
1999-04-08 05:05:13 +08:00
|
|
|
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
typedef enum {
|
|
|
|
LM_STD = 0,
|
|
|
|
#if SIZEOF_INTMAX_T
|
|
|
|
LM_INTMAX_T,
|
|
|
|
#endif
|
|
|
|
#if SIZEOF_PTRDIFF_T
|
|
|
|
LM_PTRDIFF_T,
|
|
|
|
#endif
|
|
|
|
#if SIZEOF_LONG_LONG
|
|
|
|
LM_LONG_LONG,
|
|
|
|
#endif
|
|
|
|
LM_SIZE_T,
|
2003-09-14 17:50:36 +08:00
|
|
|
LM_LONG,
|
2014-08-16 17:16:11 +08:00
|
|
|
LM_LONG_DOUBLE,
|
|
|
|
LM_PHP_INT_T
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
} length_modifier_e;
|
|
|
|
|
2005-12-03 09:23:25 +08:00
|
|
|
#ifdef PHP_WIN32
|
2005-09-16 03:11:15 +08:00
|
|
|
# define WIDE_INT __int64
|
|
|
|
#elif SIZEOF_LONG_LONG_INT
|
- Add length modifiers(ll, j, t, h, hh)
# Still missing formats (%a, %A)
# Still missing modifier (l) in (%lc, %ls)
# Still missing modifier (L) in (%La, %LA, %Le, %LE, %Lf, %LF, %Lg, %LG)
# C99 requires any conversion to be able to produce at least 4095
# characters. Implementation only allows less then 512.
#
# Only inside ext/mbstring etc. we could use %lc and %ls. And none of the
# rest should affect us until we stay with double and avoid long double.
2003-09-14 17:12:54 +08:00
|
|
|
# define WIDE_INT long long int
|
|
|
|
#elif SIZEOF_LONG_LONG
|
|
|
|
# define WIDE_INT long long
|
|
|
|
#else
|
|
|
|
# define WIDE_INT long
|
|
|
|
#endif
|
2002-04-10 09:07:49 +08:00
|
|
|
typedef WIDE_INT wide_int;
|
|
|
|
typedef unsigned WIDE_INT u_wide_int;
|
|
|
|
|
2014-10-19 04:55:14 +08:00
|
|
|
PHPAPI char * ap_php_conv_10(register wide_int num, register bool_int is_unsigned,
|
2014-08-26 02:22:49 +08:00
|
|
|
register bool_int * is_negative, char *buf_end, register size_t *len);
|
2002-04-10 09:07:49 +08:00
|
|
|
|
2014-10-19 04:55:14 +08:00
|
|
|
PHPAPI char * ap_php_conv_p2(register u_wide_int num, register int nbits,
|
2014-08-26 02:22:49 +08:00
|
|
|
char format, char *buf_end, register size_t *len);
|
2002-04-10 09:07:49 +08:00
|
|
|
|
2011-02-21 14:53:24 +08:00
|
|
|
/* The maximum precision that's allowed for float conversion. Does not include
|
|
|
|
* decimal separator, exponent, sign, terminator. Currently does not affect
|
|
|
|
* the modes e/f, only g/k/H, as those have a different limit enforced at
|
|
|
|
* another level (see NDIG in php_conv_fp()).
|
|
|
|
* Applies to the formatting functions of both spprintf.c and snprintf.c, which
|
|
|
|
* use equally sized buffers of MAX_BUF_SIZE = 512 to hold the result of the
|
|
|
|
* call to php_gcvt().
|
|
|
|
* This should be reasonably smaller than MAX_BUF_SIZE (I think MAX_BUF_SIZE - 9
|
|
|
|
* should be enough, but let's give some more space) */
|
|
|
|
#define FORMAT_CONV_MAX_PRECISION 500
|
|
|
|
|
2000-07-03 07:46:51 +08:00
|
|
|
#endif /* SNPRINTF_H */
|
1999-04-08 05:05:13 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* tab-width: 4
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* End:
|
|
|
|
*/
|