18 #include "apr_private.h" 29 #if APR_HAVE_NETINET_IN_H 30 #include <netinet/in.h> 32 #if APR_HAVE_SYS_SOCKET_H 33 #include <sys/socket.h> 35 #if APR_HAVE_ARPA_INET_H 36 #include <arpa/inet.h> 58 #define S_NULL ((char *)null_string) 61 #define FLOAT_DIGITS 6 62 #define EXPONENT_LENGTH 10 69 #define NUM_BUF_SIZE 512 86 static char *
apr_cvt(
double arg,
int ndigits,
int *decpt,
int *sign,
91 register char *
p, *p1;
93 if (ndigits >=
NDIG - 1)
102 arg = modf(arg, &fi);
108 while (p1 > &
buf[0] && fi != 0) {
109 fj = modf(fi / 10, &fi);
110 *--p1 = (
int) ((fj + .03) * 10) +
'0';
117 while ((fj = arg * 10) < 1) {
133 arg = modf(arg, &fj);
134 *
p++ = (
int) fj +
'0';
160 static char *
apr_ecvt(
double arg,
int ndigits,
int *decpt,
int *sign,
char *
buf)
162 return (
apr_cvt(arg, ndigits, decpt, sign, 1,
buf));
165 static char *
apr_fcvt(
double arg,
int ndigits,
int *decpt,
int *sign,
char *
buf)
167 return (
apr_cvt(arg, ndigits, decpt, sign, 0,
buf));
178 register char *p1, *p2;
182 p1 =
apr_ecvt(number, ndigit, &decpt, &sign, buf1);
186 for (i = ndigit - 1; i > 0 && p1[i] ==
'0'; i--)
188 if ((decpt >= 0 && decpt - ndigit > 4)
189 || (decpt < 0 && decpt < -3)) {
193 for (i = 1; i < ndigit; i++)
203 *p2++ = decpt / 100 +
'0';
205 *p2++ = (decpt % 100) / 10 +
'0';
206 *p2++ = decpt % 10 +
'0';
217 for (i = 1; i <= ndigit; i++) {
222 if (ndigit < decpt) {
223 while (ndigit++ < decpt)
228 if (p2[-1] ==
'.' && !altform)
244 #define INS_CHAR(c, sp, bep, cc) \ 248 vbuff->curpos = sp; \ 249 if (flush_func(vbuff)) \ 251 sp = vbuff->curpos; \ 252 bep = vbuff->endpos; \ 259 #define NUM(c) (c - '0') 261 #define STR_TO_DEC(str, num) \ 263 while (apr_isdigit(*str)) \ 266 num += NUM(*str++); \ 279 #define FIX_PRECISION(adjust, precision, s, s_len) \ 281 apr_size_t p = (precision + 1 < NUM_BUF_SIZE) \ 282 ? precision : NUM_BUF_SIZE - 1; \ 294 #define PAD(width, len, ch) \ 297 INS_CHAR(ch, sp, bep, cc); \ 307 #define PREFIX(str, length, ch) \ 329 static char *
conv_10(
register apr_int32_t
num,
register int is_unsigned,
330 register int *is_negative,
char *buf_end,
331 register apr_size_t *
len)
333 register char *
p = buf_end;
334 register apr_uint32_t magnitude =
num;
337 *is_negative =
FALSE;
340 *is_negative = (
num < 0);
352 apr_int32_t
t =
num + 1;
353 magnitude = ((apr_uint32_t) -
t) + 1;
361 register apr_uint32_t new_magnitude = magnitude / 10;
363 *--
p = (char) (magnitude - new_magnitude * 10 +
'0');
364 magnitude = new_magnitude;
373 register int *is_negative,
char *buf_end,
374 register apr_size_t *
len)
376 register char *
p = buf_end;
377 apr_uint64_t magnitude =
num;
384 if ((magnitude <= APR_UINT32_MAX && is_unsigned)
385 || (num <= APR_INT32_MAX && num >= APR_INT32_MIN && !is_unsigned))
386 return(
conv_10((apr_int32_t)
num, is_unsigned, is_negative, buf_end,
len));
389 *is_negative =
FALSE;
392 *is_negative = (
num < 0);
404 apr_int64_t
t =
num + 1;
405 magnitude = ((apr_uint64_t) -
t) + 1;
413 apr_uint64_t new_magnitude = magnitude / 10;
415 *--
p = (char) (magnitude - new_magnitude * 10 +
'0');
416 magnitude = new_magnitude;
463 sub_len = strlen(ipaddr_str);
466 !IN6_IS_ADDR_V4MAPPED(&
sa->
sa.sin6.sin6_addr)) {
470 memcpy(
p + 1, ipaddr_str, sub_len);
476 memcpy(
p, ipaddr_str, sub_len);
486 static char *conv_os_thread_t(apr_os_thread_t *tid,
char *buf_end, apr_size_t *
len)
496 switch(
sizeof(u.tid)) {
497 case sizeof(apr_int32_t):
499 case sizeof(apr_int64_t):
517 boolean_e add_dp,
int precision,
int *is_negative,
518 char *
buf, apr_size_t *
len)
520 register char *
s =
buf;
526 p =
apr_fcvt(
num, precision, &decimal_point, is_negative, buf1);
528 p =
apr_ecvt(
num, precision + 1, &decimal_point, is_negative, buf1);
536 *is_negative =
FALSE;
541 if (decimal_point <= 0) {
545 while (decimal_point++ < 0)
552 while (decimal_point-- > 0)
554 if (precision > 0 || add_dp)
560 if (precision > 0 || add_dp)
573 int exponent_is_negative;
577 if (decimal_point != 0) {
578 p =
conv_10((apr_int32_t) decimal_point,
FALSE, &exponent_is_negative,
580 *
s++ = exponent_is_negative ?
'-' :
'+';
615 static char *
conv_p2(
register apr_uint32_t
num,
register int nbits,
616 char format,
char *buf_end,
register apr_size_t *
len)
618 register int mask = (1 << nbits) - 1;
619 register char *
p = buf_end;
620 static const char low_digits[] =
"0123456789abcdef";
621 static const char upper_digits[] =
"0123456789ABCDEF";
622 register const char *digits = (
format ==
'X') ? upper_digits : low_digits;
625 *--
p = digits[
num & mask];
635 char format,
char *buf_end,
register apr_size_t *
len)
637 register int mask = (1 << nbits) - 1;
638 register char *
p = buf_end;
639 static const char low_digits[] =
"0123456789abcdef";
640 static const char upper_digits[] =
"0123456789ABCDEF";
641 register const char *digits = (
format ==
'X') ? upper_digits : low_digits;
643 if (
num <= APR_UINT32_MAX)
647 *--
p = digits[
num & mask];
657 static char *conv_os_thread_t_hex(apr_os_thread_t *tid,
char *buf_end, apr_size_t *
len)
667 switch(
sizeof(u.tid)) {
668 case sizeof(apr_int32_t):
670 case sizeof(apr_int64_t):
688 register apr_size_t i;
690 register char *
s = NULL;
692 apr_size_t s_len = 0;
694 register apr_size_t min_width = 0;
695 apr_size_t precision = 0;
703 apr_int64_t i_quad = 0;
704 apr_uint64_t ui_quad;
705 apr_int32_t i_num = 0;
706 apr_uint32_t ui_num = 0;
712 IS_QUAD, IS_LONG, IS_SHORT, IS_INT
714 enum var_type_enum var_type = IS_INT;
739 alternate_form = print_sign = print_blank =
NO;
755 else if (*
fmt ==
'+')
757 else if (*
fmt ==
'#')
758 alternate_form =
YES;
759 else if (*
fmt ==
' ')
761 else if (*
fmt ==
'0')
774 else if (*
fmt ==
'*') {
775 int v = va_arg(
ap,
int);
780 min_width = (apr_size_t)(-v);
783 min_width = (apr_size_t)v;
792 adjust_precision =
YES;
797 else if (*
fmt ==
'*') {
798 int v = va_arg(
ap,
int);
800 precision = (v < 0) ? 0 : (apr_size_t)v;
806 adjust_precision =
NO;
809 adjust_precision = adjust_width =
NO;
819 if ((
sizeof(APR_OFF_T_FMT) >
sizeof(APR_INT64_T_FMT)) &&
820 ((
sizeof(APR_OFF_T_FMT) == 4 &&
821 fmt[0] == APR_OFF_T_FMT[0] &&
822 fmt[1] == APR_OFF_T_FMT[1]) ||
823 (
sizeof(APR_OFF_T_FMT) == 3 &&
824 fmt[0] == APR_OFF_T_FMT[0]) ||
825 (
sizeof(APR_OFF_T_FMT) > 4 &&
826 strncmp(
fmt, APR_OFF_T_FMT,
827 sizeof(APR_OFF_T_FMT) - 2) == 0))) {
830 fmt += (
sizeof(APR_OFF_T_FMT) - 2);
832 else if ((
sizeof(APR_INT64_T_FMT) == 4 &&
833 fmt[0] == APR_INT64_T_FMT[0] &&
834 fmt[1] == APR_INT64_T_FMT[1]) ||
835 (
sizeof(APR_INT64_T_FMT) == 3 &&
836 fmt[0] == APR_INT64_T_FMT[0]) ||
837 (
sizeof(APR_INT64_T_FMT) > 4 &&
838 strncmp(
fmt, APR_INT64_T_FMT,
839 sizeof(APR_INT64_T_FMT) - 2) == 0)) {
842 fmt += (
sizeof(APR_INT64_T_FMT) - 2);
844 else if (*
fmt ==
'q') {
848 else if (*
fmt ==
'l') {
852 else if (*
fmt ==
'h') {
873 if (var_type == IS_QUAD) {
874 i_quad = va_arg(
ap, apr_uint64_t);
879 if (var_type == IS_LONG)
880 i_num = (apr_int32_t) va_arg(
ap, apr_uint32_t);
881 else if (var_type == IS_SHORT)
882 i_num = (apr_int32_t) (
unsigned short) va_arg(
ap,
unsigned int);
884 i_num = (apr_int32_t) va_arg(
ap,
unsigned int);
893 if (var_type == IS_QUAD) {
894 i_quad = va_arg(
ap, apr_int64_t);
899 if (var_type == IS_LONG)
900 i_num = va_arg(
ap, apr_int32_t);
901 else if (var_type == IS_SHORT)
902 i_num = (short) va_arg(
ap,
int);
904 i_num = va_arg(
ap,
int);
914 else if (print_blank)
920 if (var_type == IS_QUAD) {
921 ui_quad = va_arg(
ap, apr_uint64_t);
926 if (var_type == IS_LONG)
927 ui_num = va_arg(
ap, apr_uint32_t);
928 else if (var_type == IS_SHORT)
929 ui_num = (
unsigned short) va_arg(
ap,
unsigned int);
931 ui_num = va_arg(
ap,
unsigned int);
936 if (alternate_form && *
s !=
'0') {
945 if (var_type == IS_QUAD) {
946 ui_quad = va_arg(
ap, apr_uint64_t);
951 if (var_type == IS_LONG)
952 ui_num = va_arg(
ap, apr_uint32_t);
953 else if (var_type == IS_SHORT)
954 ui_num = (
unsigned short) va_arg(
ap,
unsigned int);
956 ui_num = va_arg(
ap,
unsigned int);
961 if (alternate_form && ui_num != 0) {
970 s = va_arg(
ap,
char *);
972 if (!adjust_precision) {
1000 for (walk =
s, s_len = 0;
1001 (s_len < precision) && (*walk !=
'\0');
1016 fp_num = va_arg(
ap,
double);
1022 if (isnan(fp_num)) {
1028 if (!
s && isinf(fp_num)) {
1036 &is_negative, &num_buf[1], &s_len);
1039 else if (print_sign)
1041 else if (print_blank)
1049 if (adjust_precision ==
NO)
1051 else if (precision == 0)
1056 s =
apr_gcvt(va_arg(
ap,
double), (
int) precision, &num_buf[1],
1060 else if (print_sign)
1062 else if (print_blank)
1067 if (alternate_form && (q = strchr(
s,
'.')) == NULL) {
1071 if (*
fmt ==
'G' && (q = strchr(
s,
'e')) != NULL)
1077 char_buf[0] = (char) (va_arg(
ap,
int));
1093 if (var_type == IS_QUAD)
1094 *(va_arg(
ap, apr_int64_t *)) = cc;
1095 else if (var_type == IS_LONG)
1096 *(va_arg(
ap,
long *)) = cc;
1097 else if (var_type == IS_SHORT)
1098 *(va_arg(
ap,
short *)) = cc;
1100 *(va_arg(
ap,
int *)) = cc;
1101 print_something =
NO;
1117 #if APR_SIZEOF_VOIDP == 8 1118 if (
sizeof(
void *) <=
sizeof(apr_uint64_t)) {
1119 ui_quad = (apr_uint64_t) va_arg(
ap,
void *);
1124 if (
sizeof(
void *) <=
sizeof(apr_uint32_t)) {
1125 ui_num = (apr_uint32_t) va_arg(
ap,
void *);
1146 if (adjust_precision && precision < s_len)
1165 if (adjust_precision && precision < s_len)
1197 apr_os_thread_t *tid;
1199 tid = va_arg(
ap, apr_os_thread_t *);
1202 if (adjust_precision && precision < s_len)
1222 apr_os_thread_t *tid;
1224 tid = va_arg(
ap, apr_os_thread_t *);
1226 s = conv_os_thread_t_hex(tid, &num_buf[
NUM_BUF_SIZE], &s_len);
1227 if (adjust_precision && precision < s_len)
1252 apr_uint32_t *arg = va_arg(
ap, apr_uint32_t *);
1253 size = (arg) ? *arg : 0;
1255 else if (*
fmt ==
'F') {
1256 apr_off_t *arg = va_arg(
ap, apr_off_t *);
1257 size = (arg) ? *arg : 0;
1260 apr_size_t *arg = va_arg(
ap, apr_size_t *);
1261 size = (arg) ? *arg : 0;
1278 (void)va_arg(
ap,
void *);
1310 if (prefix_char !=
NUL &&
s !=
S_NULL &&
s != char_buf) {
1315 if (adjust_width && adjust == RIGHT && min_width > s_len) {
1316 if (pad_char ==
'0' && prefix_char !=
NUL) {
1322 PAD(min_width, s_len, pad_char);
1328 if (print_something ==
YES) {
1329 for (i = s_len; i != 0; i--) {
1335 if (adjust_width && adjust == LEFT && min_width > s_len)
1336 PAD(min_width, s_len, pad_char);
1383 return (cc == -1) ? (
int)
len - 1 : cc;
1406 return (cc == -1) ? (
int)
len - 1 : cc;