76 #ifndef NO_SYS_TYPES_H
77 #include <sys/types.h>
82 #if defined(BN_LLONG) || defined(SIXTY_FOUR_BIT)
83 # ifndef HAVE_LONG_LONG
84 # define HAVE_LONG_LONG 1
112 #ifdef HAVE_LONG_DOUBLE
113 #define LDOUBLE long double
115 #define LDOUBLE double
118 #ifdef HAVE_LONG_LONG
119 # if defined(_WIN32) && !defined(__GNUC__)
120 # define LLONG __int64
122 # define LLONG long long
128 static void fmtstr (
char **,
char **,
size_t *,
size_t *,
129 const char *,
int,
int,
int);
130 static void fmtint (
char **,
char **,
size_t *,
size_t *,
131 LLONG,
int,
int,
int,
int);
132 static void fmtfp (
char **,
char **,
size_t *,
size_t *,
134 static void doapr_outch (
char **,
char **,
size_t *,
size_t *,
int);
135 static void _dopr(
char **sbuffer,
char **buffer,
136 size_t *maxlen,
size_t *retlen,
int *truncated,
137 const char *
format, va_list args);
140 #define DP_S_DEFAULT 0
150 #define DP_F_MINUS (1 << 0)
151 #define DP_F_PLUS (1 << 1)
152 #define DP_F_SPACE (1 << 2)
153 #define DP_F_NUM (1 << 3)
154 #define DP_F_ZERO (1 << 4)
155 #define DP_F_UP (1 << 5)
156 #define DP_F_UNSIGNED (1 << 6)
161 #define DP_C_LDOUBLE 3
165 #define char_to_int(p) (p - '0')
166 #define OSSL_MAX(p,q) ((p >= q) ? p : q)
190 flags = currlen = cflags = min = 0;
195 if (ch ==
'\0' || (buffer == NULL && currlen >= *maxlen))
203 doapr_outch(sbuffer,buffer, &currlen, maxlen, ch);
234 if (isdigit((
unsigned char)ch)) {
237 }
else if (ch ==
'*') {
238 min = va_arg(args,
int);
252 if (isdigit((
unsigned char)ch)) {
257 }
else if (ch ==
'*') {
258 max = va_arg(args,
int);
271 if (*format ==
'l') {
297 value = (
short int)va_arg(args,
int);
300 value = va_arg(args,
long int);
303 value = va_arg(args,
LLONG);
306 value = va_arg(args,
int);
309 fmtint(sbuffer, buffer, &currlen, maxlen,
310 value, 10, min, max, flags);
321 value = (
unsigned short int)va_arg(args,
unsigned int);
324 value = (
LLONG) va_arg(args,
328 value = va_arg(args,
unsigned LLONG);
331 value = (
LLONG) va_arg(args,
335 fmtint(sbuffer, buffer, &currlen, maxlen, value,
336 ch ==
'o' ? 8 : (ch ==
'u' ? 10 : 16),
341 fvalue = va_arg(args,
LDOUBLE);
343 fvalue = va_arg(args,
double);
344 fmtfp(sbuffer, buffer, &currlen, maxlen,
345 fvalue, min, max, flags);
351 fvalue = va_arg(args,
LDOUBLE);
353 fvalue = va_arg(args,
double);
359 fvalue = va_arg(args,
LDOUBLE);
361 fvalue = va_arg(args,
double);
364 doapr_outch(sbuffer, buffer, &currlen, maxlen,
368 strvalue = va_arg(args,
char *);
375 fmtstr(sbuffer, buffer, &currlen, maxlen, strvalue,
379 value = (long)va_arg(args,
void *);
380 fmtint(sbuffer, buffer, &currlen, maxlen,
381 value, 16, min, max, flags|
DP_F_NUM);
386 num = va_arg(args,
short int *);
390 num = va_arg(args,
long int *);
391 *num = (
long int) currlen;
394 num = va_arg(args,
LLONG *);
395 *num = (
LLONG) currlen;
398 num = va_arg(args,
int *);
403 doapr_outch(sbuffer, buffer, &currlen, maxlen, ch);
415 flags = cflags = min = 0;
424 *truncated = (currlen > *maxlen - 1);
426 currlen = *maxlen - 1;
427 doapr_outch(sbuffer, buffer, &currlen, maxlen,
'\0');
428 *retlen = currlen - 1;
448 for (strln = 0; value[strln]; ++strln)
450 padlen = min - strln;
456 while ((padlen > 0) && (cnt < max)) {
457 doapr_outch(sbuffer, buffer, currlen, maxlen,
' ');
461 while (*value && (cnt < max)) {
462 doapr_outch(sbuffer, buffer, currlen, maxlen, *value++);
465 while ((padlen < 0) && (cnt < max)) {
466 doapr_outch(sbuffer, buffer, currlen, maxlen,
' ');
485 const char *prefix =
"";
486 unsigned LLONG uvalue;
506 if (base == 8) prefix =
"0";
507 if (base == 16) prefix =
"0x";
513 (caps ?
"0123456789ABCDEF" :
"0123456789abcdef")
514 [uvalue % (
unsigned) base];
515 uvalue = (uvalue / (unsigned) base);
516 }
while (uvalue && (place < (
int)
sizeof(convert)));
517 if (place ==
sizeof(convert))
521 zpadlen = max - place;
522 spadlen = min -
OSSL_MAX(max, place) - (signvalue ? 1 : 0) - strlen(prefix);
528 zpadlen =
OSSL_MAX(zpadlen, spadlen);
531 if (flags & DP_F_MINUS)
535 while (spadlen > 0) {
536 doapr_outch(sbuffer, buffer, currlen, maxlen,
' ');
542 doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue);
546 doapr_outch(sbuffer, buffer, currlen, maxlen, *prefix);
552 while (zpadlen > 0) {
553 doapr_outch(sbuffer, buffer, currlen, maxlen,
'0');
559 doapr_outch(sbuffer, buffer, currlen, maxlen, convert[--place]);
562 while (spadlen < 0) {
563 doapr_outch(sbuffer, buffer, currlen, maxlen,
' ');
593 intpart = (long) value;
594 value = value - intpart;
626 ufvalue = abs_val(fvalue);
629 else if (flags & DP_F_PLUS)
634 intpart = (long)ufvalue;
643 max10 = roundv(pow_10(max));
644 fracpart = roundv(pow_10(max) * (ufvalue - intpart));
646 if (fracpart >= max10) {
654 (caps ?
"0123456789ABCDEF"
655 :
"0123456789abcdef")[intpart % 10];
656 intpart = (intpart / 10);
657 }
while (intpart && (iplace < (
int)
sizeof(iconvert)));
658 if (iplace ==
sizeof iconvert)
660 iconvert[iplace] = 0;
665 (caps ?
"0123456789ABCDEF"
666 :
"0123456789abcdef")[fracpart % 10];
667 fracpart = (fracpart / 10);
668 }
while (fplace < max);
669 if (fplace ==
sizeof fconvert)
671 fconvert[fplace] = 0;
674 padlen = min - iplace - max - 1 - ((signvalue) ? 1 : 0);
675 zpadlen = max - fplace;
680 if (flags & DP_F_MINUS)
683 if ((flags & DP_F_ZERO) && (padlen > 0)) {
685 doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue);
690 doapr_outch(sbuffer, buffer, currlen, maxlen,
'0');
695 doapr_outch(sbuffer, buffer, currlen, maxlen,
' ');
699 doapr_outch(sbuffer, buffer, currlen, maxlen, signvalue);
702 doapr_outch(sbuffer, buffer, currlen, maxlen, iconvert[--iplace]);
708 if (max > 0 || (flags & DP_F_NUM)) {
709 doapr_outch(sbuffer, buffer, currlen, maxlen,
'.');
712 doapr_outch(sbuffer, buffer, currlen, maxlen, fconvert[--fplace]);
714 while (zpadlen > 0) {
715 doapr_outch(sbuffer, buffer, currlen, maxlen,
'0');
720 doapr_outch(sbuffer, buffer, currlen, maxlen,
' ');
734 assert(*sbuffer != NULL || buffer != NULL);
737 while (*currlen >= *maxlen) {
738 if (*buffer == NULL) {
743 assert(*sbuffer != NULL);
744 memcpy(*buffer, *sbuffer, *currlen);
753 assert(*sbuffer != NULL || *buffer != NULL);
756 if (*currlen < *maxlen) {
758 (*sbuffer)[(*currlen)++] = (char)c;
760 (*buffer)[(*currlen)++] = (char)c;
773 va_start(args, format);
785 char hugebuf[1024*2];
788 char *hugebufp = hugebuf;
789 size_t hugebufsize =
sizeof(hugebuf);
795 _dopr(&hugebufp, &dynbuf, &hugebufsize,
796 &retlen, &ignored, format, args);
804 ret=
BIO_write(bio, hugebuf, (
int)retlen);
819 va_start(args, format);
832 _dopr(&buf, NULL, &n, &retlen, &truncated, format, args);
841 return (retlen <= INT_MAX) ? (int)retlen : -1;