diff options
Diffstat (limited to 'src/daemon/https/lgl/vasnprintf.c')
-rw-r--r-- | src/daemon/https/lgl/vasnprintf.c | 4825 |
1 files changed, 0 insertions, 4825 deletions
diff --git a/src/daemon/https/lgl/vasnprintf.c b/src/daemon/https/lgl/vasnprintf.c deleted file mode 100644 index 8c848e95..00000000 --- a/src/daemon/https/lgl/vasnprintf.c +++ /dev/null | |||
@@ -1,4825 +0,0 @@ | |||
1 | /* vsprintf with automatic memory allocation. | ||
2 | Copyright (C) 1999, 2002-2007 Free Software Foundation, Inc. | ||
3 | |||
4 | This program is free software; you can redistribute it and/or modify | ||
5 | it under the terms of the GNU Lesser General Public License as published by | ||
6 | the Free Software Foundation; either version 2.1, or (at your option) | ||
7 | any later version. | ||
8 | |||
9 | This program is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU Lesser General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU Lesser General Public License along | ||
15 | with this program; if not, write to the Free Software Foundation, | ||
16 | Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ | ||
17 | |||
18 | /* This file can be parametrized with the following macros: | ||
19 | VASNPRINTF The name of the function being defined. | ||
20 | FCHAR_T The element type of the format string. | ||
21 | DCHAR_T The element type of the destination (result) string. | ||
22 | FCHAR_T_ONLY_ASCII Set to 1 to enable verification that all characters | ||
23 | in the format string are ASCII. MUST be set if | ||
24 | FCHAR_T and DCHAR_T are not the same type. | ||
25 | DIRECTIVE Structure denoting a format directive. | ||
26 | Depends on FCHAR_T. | ||
27 | DIRECTIVES Structure denoting the set of format directives of a | ||
28 | format string. Depends on FCHAR_T. | ||
29 | PRINTF_PARSE Function that parses a format string. | ||
30 | Depends on FCHAR_T. | ||
31 | DCHAR_CPY memcpy like function for DCHAR_T[] arrays. | ||
32 | DCHAR_SET memset like function for DCHAR_T[] arrays. | ||
33 | DCHAR_MBSNLEN mbsnlen like function for DCHAR_T[] arrays. | ||
34 | SNPRINTF The system's snprintf (or similar) function. | ||
35 | This may be either snprintf or swprintf. | ||
36 | TCHAR_T The element type of the argument and result string | ||
37 | of the said SNPRINTF function. This may be either | ||
38 | char or wchar_t. The code exploits that | ||
39 | sizeof (TCHAR_T) | sizeof (DCHAR_T) and | ||
40 | alignof (TCHAR_T) <= alignof (DCHAR_T). | ||
41 | DCHAR_IS_TCHAR Set to 1 if DCHAR_T and TCHAR_T are the same type. | ||
42 | DCHAR_CONV_FROM_ENCODING A function to convert from char[] to DCHAR[]. | ||
43 | DCHAR_IS_UINT8_T Set to 1 if DCHAR_T is uint8_t. | ||
44 | DCHAR_IS_UINT16_T Set to 1 if DCHAR_T is uint16_t. | ||
45 | DCHAR_IS_UINT32_T Set to 1 if DCHAR_T is uint32_t. */ | ||
46 | |||
47 | /* Tell glibc's <stdio.h> to provide a prototype for snprintf(). | ||
48 | This must come before <config.h> because <config.h> may include | ||
49 | <features.h>, and once <features.h> has been included, it's too late. */ | ||
50 | #ifndef _GNU_SOURCE | ||
51 | # define _GNU_SOURCE 1 | ||
52 | #endif | ||
53 | |||
54 | #ifndef VASNPRINTF | ||
55 | #include "MHD_config.h" | ||
56 | #endif | ||
57 | #ifndef IN_LIBINTL | ||
58 | # include <alloca.h> | ||
59 | #endif | ||
60 | |||
61 | /* Specification. */ | ||
62 | #ifndef VASNPRINTF | ||
63 | # if WIDE_CHAR_VERSION | ||
64 | # include "vasnwprintf.h" | ||
65 | # else | ||
66 | # include "vasnprintf.h" | ||
67 | # endif | ||
68 | #endif | ||
69 | |||
70 | #include <locale.h> /* localeconv() */ | ||
71 | #include <stdio.h> /* snprintf(), sprintf() */ | ||
72 | #include <stdlib.h> /* abort(), malloc(), realloc(), free() */ | ||
73 | #include <string.h> /* memcpy(), strlen() */ | ||
74 | #include <errno.h> /* errno */ | ||
75 | #include <limits.h> /* CHAR_BIT */ | ||
76 | #include <float.h> /* DBL_MAX_EXP, LDBL_MAX_EXP */ | ||
77 | #if HAVE_NL_LANGINFO | ||
78 | # include <langinfo.h> | ||
79 | #endif | ||
80 | #ifndef VASNPRINTF | ||
81 | # if WIDE_CHAR_VERSION | ||
82 | # include "wprintf-parse.h" | ||
83 | # else | ||
84 | # include "printf-parse.h" | ||
85 | # endif | ||
86 | #endif | ||
87 | |||
88 | /* Checked size_t computations. */ | ||
89 | #include "xsize.h" | ||
90 | |||
91 | #if (NEED_PRINTF_DOUBLE || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL | ||
92 | # include <math.h> | ||
93 | # include "float+.h" | ||
94 | #endif | ||
95 | |||
96 | #if (NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && !defined IN_LIBINTL | ||
97 | # include <math.h> | ||
98 | # include "isnan.h" | ||
99 | #endif | ||
100 | |||
101 | #if (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE) && !defined IN_LIBINTL | ||
102 | # include <math.h> | ||
103 | # include "isnanl-nolibm.h" | ||
104 | # include "fpucw.h" | ||
105 | #endif | ||
106 | |||
107 | #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL | ||
108 | # include <math.h> | ||
109 | # include "isnan.h" | ||
110 | # include "printf-frexp.h" | ||
111 | #endif | ||
112 | |||
113 | #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL | ||
114 | # include <math.h> | ||
115 | # include "isnanl-nolibm.h" | ||
116 | # include "printf-frexpl.h" | ||
117 | # include "fpucw.h" | ||
118 | #endif | ||
119 | |||
120 | /* Some systems, like OSF/1 4.0 and Woe32, don't have EOVERFLOW. */ | ||
121 | #ifndef EOVERFLOW | ||
122 | # define EOVERFLOW E2BIG | ||
123 | #endif | ||
124 | |||
125 | #if HAVE_WCHAR_T | ||
126 | # if HAVE_WCSLEN | ||
127 | # define local_wcslen wcslen | ||
128 | # else | ||
129 | /* Solaris 2.5.1 has wcslen() in a separate library libw.so. To avoid | ||
130 | a dependency towards this library, here is a local substitute. | ||
131 | Define this substitute only once, even if this file is included | ||
132 | twice in the same compilation unit. */ | ||
133 | # ifndef local_wcslen_defined | ||
134 | # define local_wcslen_defined 1 | ||
135 | static size_t | ||
136 | local_wcslen (const wchar_t * s) | ||
137 | { | ||
138 | const wchar_t *ptr; | ||
139 | |||
140 | for (ptr = s; *ptr != (wchar_t) 0; ptr++) | ||
141 | ; | ||
142 | return ptr - s; | ||
143 | } | ||
144 | # endif | ||
145 | # endif | ||
146 | #endif | ||
147 | |||
148 | /* Default parameters. */ | ||
149 | #ifndef VASNPRINTF | ||
150 | # if WIDE_CHAR_VERSION | ||
151 | # define VASNPRINTF vasnwprintf | ||
152 | # define FCHAR_T wchar_t | ||
153 | # define DCHAR_T wchar_t | ||
154 | # define TCHAR_T wchar_t | ||
155 | # define DCHAR_IS_TCHAR 1 | ||
156 | # define DIRECTIVE wchar_t_directive | ||
157 | # define DIRECTIVES wchar_t_directives | ||
158 | # define PRINTF_PARSE wprintf_parse | ||
159 | # define DCHAR_CPY wmemcpy | ||
160 | # else | ||
161 | # define VASNPRINTF vasnprintf | ||
162 | # define FCHAR_T char | ||
163 | # define DCHAR_T char | ||
164 | # define TCHAR_T char | ||
165 | # define DCHAR_IS_TCHAR 1 | ||
166 | # define DIRECTIVE char_directive | ||
167 | # define DIRECTIVES char_directives | ||
168 | # define PRINTF_PARSE printf_parse | ||
169 | # define DCHAR_CPY memcpy | ||
170 | # endif | ||
171 | #endif | ||
172 | #if WIDE_CHAR_VERSION | ||
173 | /* TCHAR_T is wchar_t. */ | ||
174 | # define USE_SNPRINTF 1 | ||
175 | # if HAVE_DECL__SNWPRINTF | ||
176 | /* On Windows, the function swprintf() has a different signature than | ||
177 | on Unix; we use the _snwprintf() function instead. */ | ||
178 | # define SNPRINTF _snwprintf | ||
179 | # else | ||
180 | /* Unix. */ | ||
181 | # define SNPRINTF swprintf | ||
182 | # endif | ||
183 | #else | ||
184 | /* TCHAR_T is char. */ | ||
185 | # /* Use snprintf if it exists under the name 'snprintf' or '_snprintf'. | ||
186 | But don't use it on BeOS, since BeOS snprintf produces no output if the | ||
187 | size argument is >= 0x3000000. */ | ||
188 | # if (HAVE_DECL__SNPRINTF || HAVE_SNPRINTF) && !defined __BEOS__ | ||
189 | # define USE_SNPRINTF 1 | ||
190 | # else | ||
191 | # define USE_SNPRINTF 0 | ||
192 | # endif | ||
193 | # if HAVE_DECL__SNPRINTF | ||
194 | /* Windows. */ | ||
195 | # define SNPRINTF _snprintf | ||
196 | # else | ||
197 | /* Unix. */ | ||
198 | # define SNPRINTF snprintf | ||
199 | /* Here we need to call the native snprintf, not rpl_snprintf. */ | ||
200 | # undef snprintf | ||
201 | # endif | ||
202 | #endif | ||
203 | /* Here we need to call the native sprintf, not rpl_sprintf. */ | ||
204 | #undef sprintf | ||
205 | |||
206 | #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && !defined IN_LIBINTL | ||
207 | /* Determine the decimal-point character according to the current locale. */ | ||
208 | # ifndef decimal_point_char_defined | ||
209 | # define decimal_point_char_defined 1 | ||
210 | static char | ||
211 | decimal_point_char () | ||
212 | { | ||
213 | const char *point; | ||
214 | /* Determine it in a multithread-safe way. We know nl_langinfo is | ||
215 | multithread-safe on glibc systems, but is not required to be multithread- | ||
216 | safe by POSIX. sprintf(), however, is multithread-safe. localeconv() | ||
217 | is rarely multithread-safe. */ | ||
218 | # if HAVE_NL_LANGINFO && __GLIBC__ | ||
219 | point = nl_langinfo (RADIXCHAR); | ||
220 | # elif 1 | ||
221 | char pointbuf[5]; | ||
222 | sprintf (pointbuf, "%#.0f", 1.0); | ||
223 | point = &pointbuf[1]; | ||
224 | # else | ||
225 | point = localeconv ()->decimal_point; | ||
226 | # endif | ||
227 | /* The decimal point is always a single byte: either '.' or ','. */ | ||
228 | return (point[0] != '\0' ? point[0] : '.'); | ||
229 | } | ||
230 | # endif | ||
231 | #endif | ||
232 | |||
233 | #if NEED_PRINTF_INFINITE_DOUBLE && !NEED_PRINTF_DOUBLE && !defined IN_LIBINTL | ||
234 | |||
235 | /* Equivalent to !isfinite(x) || x == 0, but does not require libm. */ | ||
236 | static int | ||
237 | is_infinite_or_zero (double x) | ||
238 | { | ||
239 | return isnan (x) || x + x == x; | ||
240 | } | ||
241 | |||
242 | #endif | ||
243 | |||
244 | #if NEED_PRINTF_INFINITE_LONG_DOUBLE && !NEED_PRINTF_LONG_DOUBLE && !defined IN_LIBINTL | ||
245 | |||
246 | /* Equivalent to !isfinite(x), but does not require libm. */ | ||
247 | static int | ||
248 | is_infinitel (long double x) | ||
249 | { | ||
250 | return isnanl (x) || (x + x == x && x != 0.0L); | ||
251 | } | ||
252 | |||
253 | #endif | ||
254 | |||
255 | #if (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL | ||
256 | |||
257 | /* Converting 'long double' to decimal without rare rounding bugs requires | ||
258 | real bignums. We use the naming conventions of GNU gmp, but vastly simpler | ||
259 | (and slower) algorithms. */ | ||
260 | |||
261 | typedef unsigned int mp_limb_t; | ||
262 | # define GMP_LIMB_BITS 32 | ||
263 | typedef int mp_limb_verify[2 * | ||
264 | (sizeof (mp_limb_t) * CHAR_BIT == | ||
265 | GMP_LIMB_BITS) - 1]; | ||
266 | |||
267 | typedef unsigned long long mp_twolimb_t; | ||
268 | # define GMP_TWOLIMB_BITS 64 | ||
269 | typedef int mp_twolimb_verify[2 * | ||
270 | (sizeof (mp_twolimb_t) * CHAR_BIT == | ||
271 | GMP_TWOLIMB_BITS) - 1]; | ||
272 | |||
273 | /* Representation of a bignum >= 0. */ | ||
274 | typedef struct | ||
275 | { | ||
276 | size_t nlimbs; | ||
277 | mp_limb_t *limbs; /* Bits in little-endian order, allocated with malloc(). */ | ||
278 | } mpn_t; | ||
279 | |||
280 | /* Compute the product of two bignums >= 0. | ||
281 | Return the allocated memory in case of success, NULL in case of memory | ||
282 | allocation failure. */ | ||
283 | static void * | ||
284 | multiply (mpn_t src1, mpn_t src2, mpn_t * dest) | ||
285 | { | ||
286 | const mp_limb_t *p1; | ||
287 | const mp_limb_t *p2; | ||
288 | size_t len1; | ||
289 | size_t len2; | ||
290 | |||
291 | if (src1.nlimbs <= src2.nlimbs) | ||
292 | { | ||
293 | len1 = src1.nlimbs; | ||
294 | p1 = src1.limbs; | ||
295 | len2 = src2.nlimbs; | ||
296 | p2 = src2.limbs; | ||
297 | } | ||
298 | else | ||
299 | { | ||
300 | len1 = src2.nlimbs; | ||
301 | p1 = src2.limbs; | ||
302 | len2 = src1.nlimbs; | ||
303 | p2 = src1.limbs; | ||
304 | } | ||
305 | /* Now 0 <= len1 <= len2. */ | ||
306 | if (len1 == 0) | ||
307 | { | ||
308 | /* src1 or src2 is zero. */ | ||
309 | dest->nlimbs = 0; | ||
310 | dest->limbs = (mp_limb_t *) malloc (1); | ||
311 | } | ||
312 | else | ||
313 | { | ||
314 | /* Here 1 <= len1 <= len2. */ | ||
315 | size_t dlen; | ||
316 | mp_limb_t *dp; | ||
317 | size_t k, i, j; | ||
318 | |||
319 | dlen = len1 + len2; | ||
320 | dp = (mp_limb_t *) malloc (dlen * sizeof (mp_limb_t)); | ||
321 | if (dp == NULL) | ||
322 | return NULL; | ||
323 | for (k = len2; k > 0;) | ||
324 | dp[--k] = 0; | ||
325 | for (i = 0; i < len1; i++) | ||
326 | { | ||
327 | mp_limb_t digit1 = p1[i]; | ||
328 | mp_twolimb_t carry = 0; | ||
329 | for (j = 0; j < len2; j++) | ||
330 | { | ||
331 | mp_limb_t digit2 = p2[j]; | ||
332 | carry += (mp_twolimb_t) digit1 *(mp_twolimb_t) digit2; | ||
333 | carry += dp[i + j]; | ||
334 | dp[i + j] = (mp_limb_t) carry; | ||
335 | carry = carry >> GMP_LIMB_BITS; | ||
336 | } | ||
337 | dp[i + len2] = (mp_limb_t) carry; | ||
338 | } | ||
339 | /* Normalise. */ | ||
340 | while (dlen > 0 && dp[dlen - 1] == 0) | ||
341 | dlen--; | ||
342 | dest->nlimbs = dlen; | ||
343 | dest->limbs = dp; | ||
344 | } | ||
345 | return dest->limbs; | ||
346 | } | ||
347 | |||
348 | /* Compute the quotient of a bignum a >= 0 and a bignum b > 0. | ||
349 | a is written as a = q * b + r with 0 <= r < b. q is the quotient, r | ||
350 | the remainder. | ||
351 | Finally, round-to-even is performed: If r > b/2 or if r = b/2 and q is odd, | ||
352 | q is incremented. | ||
353 | Return the allocated memory in case of success, NULL in case of memory | ||
354 | allocation failure. */ | ||
355 | static void * | ||
356 | divide (mpn_t a, mpn_t b, mpn_t * q) | ||
357 | { | ||
358 | /* Algorithm: | ||
359 | First normalise a and b: a=[a[m-1],...,a[0]], b=[b[n-1],...,b[0]] | ||
360 | with m>=0 and n>0 (in base beta = 2^GMP_LIMB_BITS). | ||
361 | If m<n, then q:=0 and r:=a. | ||
362 | If m>=n=1, perform a single-precision division: | ||
363 | r:=0, j:=m, | ||
364 | while j>0 do | ||
365 | {Here (q[m-1]*beta^(m-1)+...+q[j]*beta^j) * b[0] + r*beta^j = | ||
366 | = a[m-1]*beta^(m-1)+...+a[j]*beta^j und 0<=r<b[0]<beta} | ||
367 | j:=j-1, r:=r*beta+a[j], q[j]:=floor(r/b[0]), r:=r-b[0]*q[j]. | ||
368 | Normalise [q[m-1],...,q[0]], yields q. | ||
369 | If m>=n>1, perform a multiple-precision division: | ||
370 | We have a/b < beta^(m-n+1). | ||
371 | s:=intDsize-1-(hightest bit in b[n-1]), 0<=s<intDsize. | ||
372 | Shift a and b left by s bits, copying them. r:=a. | ||
373 | r=[r[m],...,r[0]], b=[b[n-1],...,b[0]] with b[n-1]>=beta/2. | ||
374 | For j=m-n,...,0: {Here 0 <= r < b*beta^(j+1).} | ||
375 | Compute q* : | ||
376 | q* := floor((r[j+n]*beta+r[j+n-1])/b[n-1]). | ||
377 | In case of overflow (q* >= beta) set q* := beta-1. | ||
378 | Compute c2 := ((r[j+n]*beta+r[j+n-1]) - q* * b[n-1])*beta + r[j+n-2] | ||
379 | and c3 := b[n-2] * q*. | ||
380 | {We have 0 <= c2 < 2*beta^2, even 0 <= c2 < beta^2 if no overflow | ||
381 | occurred. Furthermore 0 <= c3 < beta^2. | ||
382 | If there was overflow and | ||
383 | r[j+n]*beta+r[j+n-1] - q* * b[n-1] >= beta, i.e. c2 >= beta^2, | ||
384 | the next test can be skipped.} | ||
385 | While c3 > c2, {Here 0 <= c2 < c3 < beta^2} | ||
386 | Put q* := q* - 1, c2 := c2 + b[n-1]*beta, c3 := c3 - b[n-2]. | ||
387 | If q* > 0: | ||
388 | Put r := r - b * q* * beta^j. In detail: | ||
389 | [r[n+j],...,r[j]] := [r[n+j],...,r[j]] - q* * [b[n-1],...,b[0]]. | ||
390 | hence: u:=0, for i:=0 to n-1 do | ||
391 | u := u + q* * b[i], | ||
392 | r[j+i]:=r[j+i]-(u mod beta) (+ beta, if carry), | ||
393 | u:=u div beta (+ 1, if carry in subtraction) | ||
394 | r[n+j]:=r[n+j]-u. | ||
395 | {Since always u = (q* * [b[i-1],...,b[0]] div beta^i) + 1 | ||
396 | < q* + 1 <= beta, | ||
397 | the carry u does not overflow.} | ||
398 | If a negative carry occurs, put q* := q* - 1 | ||
399 | and [r[n+j],...,r[j]] := [r[n+j],...,r[j]] + [0,b[n-1],...,b[0]]. | ||
400 | Set q[j] := q*. | ||
401 | Normalise [q[m-n],..,q[0]]; this yields the quotient q. | ||
402 | Shift [r[n-1],...,r[0]] right by s bits and normalise; this yields the | ||
403 | rest r. | ||
404 | The room for q[j] can be allocated at the memory location of r[n+j]. | ||
405 | Finally, round-to-even: | ||
406 | Shift r left by 1 bit. | ||
407 | If r > b or if r = b and q[0] is odd, q := q+1. | ||
408 | */ | ||
409 | const mp_limb_t *a_ptr = a.limbs; | ||
410 | size_t a_len = a.nlimbs; | ||
411 | const mp_limb_t *b_ptr = b.limbs; | ||
412 | size_t b_len = b.nlimbs; | ||
413 | mp_limb_t *roomptr; | ||
414 | mp_limb_t *tmp_roomptr = NULL; | ||
415 | mp_limb_t *q_ptr; | ||
416 | size_t q_len; | ||
417 | mp_limb_t *r_ptr; | ||
418 | size_t r_len; | ||
419 | |||
420 | /* Allocate room for a_len+2 digits. | ||
421 | (Need a_len+1 digits for the real division and 1 more digit for the | ||
422 | final rounding of q.) */ | ||
423 | roomptr = (mp_limb_t *) malloc ((a_len + 2) * sizeof (mp_limb_t)); | ||
424 | if (roomptr == NULL) | ||
425 | return NULL; | ||
426 | |||
427 | /* Normalise a. */ | ||
428 | while (a_len > 0 && a_ptr[a_len - 1] == 0) | ||
429 | a_len--; | ||
430 | |||
431 | /* Normalise b. */ | ||
432 | for (;;) | ||
433 | { | ||
434 | if (b_len == 0) | ||
435 | /* Division by zero. */ | ||
436 | abort (); | ||
437 | if (b_ptr[b_len - 1] == 0) | ||
438 | b_len--; | ||
439 | else | ||
440 | break; | ||
441 | } | ||
442 | |||
443 | /* Here m = a_len >= 0 and n = b_len > 0. */ | ||
444 | |||
445 | if (a_len < b_len) | ||
446 | { | ||
447 | /* m<n: trivial case. q=0, r := copy of a. */ | ||
448 | r_ptr = roomptr; | ||
449 | r_len = a_len; | ||
450 | memcpy (r_ptr, a_ptr, a_len * sizeof (mp_limb_t)); | ||
451 | q_ptr = roomptr + a_len; | ||
452 | q_len = 0; | ||
453 | } | ||
454 | else if (b_len == 1) | ||
455 | { | ||
456 | /* n=1: single precision division. | ||
457 | beta^(m-1) <= a < beta^m ==> beta^(m-2) <= a/b < beta^m */ | ||
458 | r_ptr = roomptr; | ||
459 | q_ptr = roomptr + 1; | ||
460 | { | ||
461 | mp_limb_t den = b_ptr[0]; | ||
462 | mp_limb_t remainder = 0; | ||
463 | const mp_limb_t *sourceptr = a_ptr + a_len; | ||
464 | mp_limb_t *destptr = q_ptr + a_len; | ||
465 | size_t count; | ||
466 | for (count = a_len; count > 0; count--) | ||
467 | { | ||
468 | mp_twolimb_t num = | ||
469 | ((mp_twolimb_t) remainder << GMP_LIMB_BITS) | *--sourceptr; | ||
470 | *--destptr = num / den; | ||
471 | remainder = num % den; | ||
472 | } | ||
473 | /* Normalise and store r. */ | ||
474 | if (remainder > 0) | ||
475 | { | ||
476 | r_ptr[0] = remainder; | ||
477 | r_len = 1; | ||
478 | } | ||
479 | else | ||
480 | r_len = 0; | ||
481 | /* Normalise q. */ | ||
482 | q_len = a_len; | ||
483 | if (q_ptr[q_len - 1] == 0) | ||
484 | q_len--; | ||
485 | } | ||
486 | } | ||
487 | else | ||
488 | { | ||
489 | /* n>1: multiple precision division. | ||
490 | beta^(m-1) <= a < beta^m, beta^(n-1) <= b < beta^n ==> | ||
491 | beta^(m-n-1) <= a/b < beta^(m-n+1). */ | ||
492 | /* Determine s. */ | ||
493 | size_t s; | ||
494 | { | ||
495 | mp_limb_t msd = b_ptr[b_len - 1]; /* = b[n-1], > 0 */ | ||
496 | s = 31; | ||
497 | if (msd >= 0x10000) | ||
498 | { | ||
499 | msd = msd >> 16; | ||
500 | s -= 16; | ||
501 | } | ||
502 | if (msd >= 0x100) | ||
503 | { | ||
504 | msd = msd >> 8; | ||
505 | s -= 8; | ||
506 | } | ||
507 | if (msd >= 0x10) | ||
508 | { | ||
509 | msd = msd >> 4; | ||
510 | s -= 4; | ||
511 | } | ||
512 | if (msd >= 0x4) | ||
513 | { | ||
514 | msd = msd >> 2; | ||
515 | s -= 2; | ||
516 | } | ||
517 | if (msd >= 0x2) | ||
518 | { | ||
519 | msd = msd >> 1; | ||
520 | s -= 1; | ||
521 | } | ||
522 | } | ||
523 | /* 0 <= s < GMP_LIMB_BITS. | ||
524 | Copy b, shifting it left by s bits. */ | ||
525 | if (s > 0) | ||
526 | { | ||
527 | tmp_roomptr = (mp_limb_t *) malloc (b_len * sizeof (mp_limb_t)); | ||
528 | if (tmp_roomptr == NULL) | ||
529 | { | ||
530 | free (roomptr); | ||
531 | return NULL; | ||
532 | } | ||
533 | { | ||
534 | const mp_limb_t *sourceptr = b_ptr; | ||
535 | mp_limb_t *destptr = tmp_roomptr; | ||
536 | mp_twolimb_t accu = 0; | ||
537 | size_t count; | ||
538 | for (count = b_len; count > 0; count--) | ||
539 | { | ||
540 | accu += (mp_twolimb_t) * sourceptr++ << s; | ||
541 | *destptr++ = (mp_limb_t) accu; | ||
542 | accu = accu >> GMP_LIMB_BITS; | ||
543 | } | ||
544 | /* accu must be zero, since that was how s was determined. */ | ||
545 | if (accu != 0) | ||
546 | abort (); | ||
547 | } | ||
548 | b_ptr = tmp_roomptr; | ||
549 | } | ||
550 | /* Copy a, shifting it left by s bits, yields r. | ||
551 | Memory layout: | ||
552 | At the beginning: r = roomptr[0..a_len], | ||
553 | at the end: r = roomptr[0..b_len-1], q = roomptr[b_len..a_len] */ | ||
554 | r_ptr = roomptr; | ||
555 | if (s == 0) | ||
556 | { | ||
557 | memcpy (r_ptr, a_ptr, a_len * sizeof (mp_limb_t)); | ||
558 | r_ptr[a_len] = 0; | ||
559 | } | ||
560 | else | ||
561 | { | ||
562 | const mp_limb_t *sourceptr = a_ptr; | ||
563 | mp_limb_t *destptr = r_ptr; | ||
564 | mp_twolimb_t accu = 0; | ||
565 | size_t count; | ||
566 | for (count = a_len; count > 0; count--) | ||
567 | { | ||
568 | accu += (mp_twolimb_t) * sourceptr++ << s; | ||
569 | *destptr++ = (mp_limb_t) accu; | ||
570 | accu = accu >> GMP_LIMB_BITS; | ||
571 | } | ||
572 | *destptr++ = (mp_limb_t) accu; | ||
573 | } | ||
574 | q_ptr = roomptr + b_len; | ||
575 | q_len = a_len - b_len + 1; /* q will have m-n+1 limbs */ | ||
576 | { | ||
577 | size_t j = a_len - b_len; /* m-n */ | ||
578 | mp_limb_t b_msd = b_ptr[b_len - 1]; /* b[n-1] */ | ||
579 | mp_limb_t b_2msd = b_ptr[b_len - 2]; /* b[n-2] */ | ||
580 | mp_twolimb_t b_msdd = /* b[n-1]*beta+b[n-2] */ | ||
581 | ((mp_twolimb_t) b_msd << GMP_LIMB_BITS) | b_2msd; | ||
582 | /* Division loop, traversed m-n+1 times. | ||
583 | j counts down, b is unchanged, beta/2 <= b[n-1] < beta. */ | ||
584 | for (;;) | ||
585 | { | ||
586 | mp_limb_t q_star; | ||
587 | mp_limb_t c1; | ||
588 | if (r_ptr[j + b_len] < b_msd) /* r[j+n] < b[n-1] ? */ | ||
589 | { | ||
590 | /* Divide r[j+n]*beta+r[j+n-1] by b[n-1], no overflow. */ | ||
591 | mp_twolimb_t num = | ||
592 | ((mp_twolimb_t) r_ptr[j + b_len] << GMP_LIMB_BITS) | ||
593 | | r_ptr[j + b_len - 1]; | ||
594 | q_star = num / b_msd; | ||
595 | c1 = num % b_msd; | ||
596 | } | ||
597 | else | ||
598 | { | ||
599 | /* Overflow, hence r[j+n]*beta+r[j+n-1] >= beta*b[n-1]. */ | ||
600 | q_star = (mp_limb_t) ~ (mp_limb_t) 0; /* q* = beta-1 */ | ||
601 | /* Test whether r[j+n]*beta+r[j+n-1] - (beta-1)*b[n-1] >= beta | ||
602 | <==> r[j+n]*beta+r[j+n-1] + b[n-1] >= beta*b[n-1]+beta | ||
603 | <==> b[n-1] < floor((r[j+n]*beta+r[j+n-1]+b[n-1])/beta) | ||
604 | {<= beta !}. | ||
605 | If yes, jump directly to the subtraction loop. | ||
606 | (Otherwise, r[j+n]*beta+r[j+n-1] - (beta-1)*b[n-1] < beta | ||
607 | <==> floor((r[j+n]*beta+r[j+n-1]+b[n-1])/beta) = b[n-1] ) */ | ||
608 | if (r_ptr[j + b_len] > b_msd | ||
609 | || (c1 = r_ptr[j + b_len - 1] + b_msd) < b_msd) | ||
610 | /* r[j+n] >= b[n-1]+1 or | ||
611 | r[j+n] = b[n-1] and the addition r[j+n-1]+b[n-1] gives a | ||
612 | carry. */ | ||
613 | goto subtract; | ||
614 | } | ||
615 | /* q_star = q*, | ||
616 | c1 = (r[j+n]*beta+r[j+n-1]) - q* * b[n-1] (>=0, <beta). */ | ||
617 | { | ||
618 | mp_twolimb_t c2 = /* c1*beta+r[j+n-2] */ | ||
619 | ((mp_twolimb_t) c1 << GMP_LIMB_BITS) | r_ptr[j + b_len - 2]; | ||
620 | mp_twolimb_t c3 = /* b[n-2] * q* */ | ||
621 | (mp_twolimb_t) b_2msd * (mp_twolimb_t) q_star; | ||
622 | /* While c2 < c3, increase c2 and decrease c3. | ||
623 | Consider c3-c2. While it is > 0, decrease it by | ||
624 | b[n-1]*beta+b[n-2]. Because of b[n-1]*beta+b[n-2] >= beta^2/2 | ||
625 | this can happen only twice. */ | ||
626 | if (c3 > c2) | ||
627 | { | ||
628 | q_star = q_star - 1; /* q* := q* - 1 */ | ||
629 | if (c3 - c2 > b_msdd) | ||
630 | q_star = q_star - 1; /* q* := q* - 1 */ | ||
631 | } | ||
632 | } | ||
633 | if (q_star > 0) | ||
634 | subtract: | ||
635 | { | ||
636 | /* Subtract r := r - b * q* * beta^j. */ | ||
637 | mp_limb_t cr; | ||
638 | { | ||
639 | const mp_limb_t *sourceptr = b_ptr; | ||
640 | mp_limb_t *destptr = r_ptr + j; | ||
641 | mp_twolimb_t carry = 0; | ||
642 | size_t count; | ||
643 | for (count = b_len; count > 0; count--) | ||
644 | { | ||
645 | /* Here 0 <= carry <= q*. */ | ||
646 | carry = | ||
647 | carry | ||
648 | + (mp_twolimb_t) q_star *(mp_twolimb_t) * sourceptr++ | ||
649 | + (mp_limb_t) ~ (*destptr); | ||
650 | /* Here 0 <= carry <= beta*q* + beta-1. */ | ||
651 | *destptr++ = ~(mp_limb_t) carry; | ||
652 | carry = carry >> GMP_LIMB_BITS; /* <= q* */ | ||
653 | } | ||
654 | cr = (mp_limb_t) carry; | ||
655 | } | ||
656 | /* Subtract cr from r_ptr[j + b_len], then forget about | ||
657 | r_ptr[j + b_len]. */ | ||
658 | if (cr > r_ptr[j + b_len]) | ||
659 | { | ||
660 | /* Subtraction gave a carry. */ | ||
661 | q_star = q_star - 1; /* q* := q* - 1 */ | ||
662 | /* Add b back. */ | ||
663 | { | ||
664 | const mp_limb_t *sourceptr = b_ptr; | ||
665 | mp_limb_t *destptr = r_ptr + j; | ||
666 | mp_limb_t carry = 0; | ||
667 | size_t count; | ||
668 | for (count = b_len; count > 0; count--) | ||
669 | { | ||
670 | mp_limb_t source1 = *sourceptr++; | ||
671 | mp_limb_t source2 = *destptr; | ||
672 | *destptr++ = source1 + source2 + carry; | ||
673 | carry = | ||
674 | (carry | ||
675 | ? source1 >= (mp_limb_t) ~ source2 | ||
676 | : source1 > (mp_limb_t) ~ source2); | ||
677 | } | ||
678 | } | ||
679 | /* Forget about the carry and about r[j+n]. */ | ||
680 | } | ||
681 | } | ||
682 | /* q* is determined. Store it as q[j]. */ | ||
683 | q_ptr[j] = q_star; | ||
684 | if (j == 0) | ||
685 | break; | ||
686 | j--; | ||
687 | } | ||
688 | } | ||
689 | r_len = b_len; | ||
690 | /* Normalise q. */ | ||
691 | if (q_ptr[q_len - 1] == 0) | ||
692 | q_len--; | ||
693 | # if 0 /* Not needed here, since we need r only to compare it with b/2, and | ||
694 | b is shifted left by s bits. */ | ||
695 | /* Shift r right by s bits. */ | ||
696 | if (s > 0) | ||
697 | { | ||
698 | mp_limb_t ptr = r_ptr + r_len; | ||
699 | mp_twolimb_t accu = 0; | ||
700 | size_t count; | ||
701 | for (count = r_len; count > 0; count--) | ||
702 | { | ||
703 | accu = (mp_twolimb_t) (mp_limb_t) accu << GMP_LIMB_BITS; | ||
704 | accu += (mp_twolimb_t) * --ptr << (GMP_LIMB_BITS - s); | ||
705 | *ptr = (mp_limb_t) (accu >> GMP_LIMB_BITS); | ||
706 | } | ||
707 | } | ||
708 | # endif | ||
709 | /* Normalise r. */ | ||
710 | while (r_len > 0 && r_ptr[r_len - 1] == 0) | ||
711 | r_len--; | ||
712 | } | ||
713 | /* Compare r << 1 with b. */ | ||
714 | if (r_len > b_len) | ||
715 | goto increment_q; | ||
716 | { | ||
717 | size_t i; | ||
718 | for (i = b_len;;) | ||
719 | { | ||
720 | mp_limb_t r_i = | ||
721 | (i <= r_len && i > 0 ? r_ptr[i - 1] >> (GMP_LIMB_BITS - 1) : 0) | ||
722 | | (i < r_len ? r_ptr[i] << 1 : 0); | ||
723 | mp_limb_t b_i = (i < b_len ? b_ptr[i] : 0); | ||
724 | if (r_i > b_i) | ||
725 | goto increment_q; | ||
726 | if (r_i < b_i) | ||
727 | goto keep_q; | ||
728 | if (i == 0) | ||
729 | break; | ||
730 | i--; | ||
731 | } | ||
732 | } | ||
733 | if (q_len > 0 && ((q_ptr[0] & 1) != 0)) | ||
734 | /* q is odd. */ | ||
735 | increment_q: | ||
736 | { | ||
737 | size_t i; | ||
738 | for (i = 0; i < q_len; i++) | ||
739 | if (++(q_ptr[i]) != 0) | ||
740 | goto keep_q; | ||
741 | q_ptr[q_len++] = 1; | ||
742 | } | ||
743 | keep_q: | ||
744 | if (tmp_roomptr != NULL) | ||
745 | free (tmp_roomptr); | ||
746 | q->limbs = q_ptr; | ||
747 | q->nlimbs = q_len; | ||
748 | return roomptr; | ||
749 | } | ||
750 | |||
751 | /* Convert a bignum a >= 0, multiplied with 10^extra_zeroes, to decimal | ||
752 | representation. | ||
753 | Destroys the contents of a. | ||
754 | Return the allocated memory - containing the decimal digits in low-to-high | ||
755 | order, terminated with a NUL character - in case of success, NULL in case | ||
756 | of memory allocation failure. */ | ||
757 | static char * | ||
758 | convert_to_decimal (mpn_t a, size_t extra_zeroes) | ||
759 | { | ||
760 | mp_limb_t *a_ptr = a.limbs; | ||
761 | size_t a_len = a.nlimbs; | ||
762 | /* 0.03345 is slightly larger than log(2)/(9*log(10)). */ | ||
763 | size_t c_len = 9 * ((size_t) (a_len * (GMP_LIMB_BITS * 0.03345f)) + 1); | ||
764 | char *c_ptr = (char *) malloc (xsum (c_len, extra_zeroes)); | ||
765 | if (c_ptr != NULL) | ||
766 | { | ||
767 | char *d_ptr = c_ptr; | ||
768 | for (; extra_zeroes > 0; extra_zeroes--) | ||
769 | *d_ptr++ = '0'; | ||
770 | while (a_len > 0) | ||
771 | { | ||
772 | /* Divide a by 10^9, in-place. */ | ||
773 | mp_limb_t remainder = 0; | ||
774 | mp_limb_t *ptr = a_ptr + a_len; | ||
775 | size_t count; | ||
776 | for (count = a_len; count > 0; count--) | ||
777 | { | ||
778 | mp_twolimb_t num = | ||
779 | ((mp_twolimb_t) remainder << GMP_LIMB_BITS) | *--ptr; | ||
780 | *ptr = num / 1000000000; | ||
781 | remainder = num % 1000000000; | ||
782 | } | ||
783 | /* Store the remainder as 9 decimal digits. */ | ||
784 | for (count = 9; count > 0; count--) | ||
785 | { | ||
786 | *d_ptr++ = '0' + (remainder % 10); | ||
787 | remainder = remainder / 10; | ||
788 | } | ||
789 | /* Normalize a. */ | ||
790 | if (a_ptr[a_len - 1] == 0) | ||
791 | a_len--; | ||
792 | } | ||
793 | /* Remove leading zeroes. */ | ||
794 | while (d_ptr > c_ptr && d_ptr[-1] == '0') | ||
795 | d_ptr--; | ||
796 | /* But keep at least one zero. */ | ||
797 | if (d_ptr == c_ptr) | ||
798 | *d_ptr++ = '0'; | ||
799 | /* Terminate the string. */ | ||
800 | *d_ptr = '\0'; | ||
801 | } | ||
802 | return c_ptr; | ||
803 | } | ||
804 | |||
805 | # if NEED_PRINTF_LONG_DOUBLE | ||
806 | |||
807 | /* Assuming x is finite and >= 0: | ||
808 | write x as x = 2^e * m, where m is a bignum. | ||
809 | Return the allocated memory in case of success, NULL in case of memory | ||
810 | allocation failure. */ | ||
811 | static void * | ||
812 | decode_long_double (long double x, int *ep, mpn_t * mp) | ||
813 | { | ||
814 | mpn_t m; | ||
815 | int exp; | ||
816 | long double y; | ||
817 | size_t i; | ||
818 | |||
819 | /* Allocate memory for result. */ | ||
820 | m.nlimbs = (LDBL_MANT_BIT + GMP_LIMB_BITS - 1) / GMP_LIMB_BITS; | ||
821 | m.limbs = (mp_limb_t *) malloc (m.nlimbs * sizeof (mp_limb_t)); | ||
822 | if (m.limbs == NULL) | ||
823 | return NULL; | ||
824 | /* Split into exponential part and mantissa. */ | ||
825 | y = frexpl (x, &exp); | ||
826 | if (!(y >= 0.0L && y < 1.0L)) | ||
827 | abort (); | ||
828 | /* x = 2^exp * y = 2^(exp - LDBL_MANT_BIT) * (y * LDBL_MANT_BIT), and the | ||
829 | latter is an integer. */ | ||
830 | /* Convert the mantissa (y * LDBL_MANT_BIT) to a sequence of limbs. | ||
831 | I'm not sure whether it's safe to cast a 'long double' value between | ||
832 | 2^31 and 2^32 to 'unsigned int', therefore play safe and cast only | ||
833 | 'long double' values between 0 and 2^16 (to 'unsigned int' or 'int', | ||
834 | doesn't matter). */ | ||
835 | # if (LDBL_MANT_BIT % GMP_LIMB_BITS) != 0 | ||
836 | # if (LDBL_MANT_BIT % GMP_LIMB_BITS) > GMP_LIMB_BITS / 2 | ||
837 | { | ||
838 | mp_limb_t hi, lo; | ||
839 | y *= (mp_limb_t) 1 << (LDBL_MANT_BIT % (GMP_LIMB_BITS / 2)); | ||
840 | hi = (int) y; | ||
841 | y -= hi; | ||
842 | if (!(y >= 0.0L && y < 1.0L)) | ||
843 | abort (); | ||
844 | y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2); | ||
845 | lo = (int) y; | ||
846 | y -= lo; | ||
847 | if (!(y >= 0.0L && y < 1.0L)) | ||
848 | abort (); | ||
849 | m.limbs[LDBL_MANT_BIT / GMP_LIMB_BITS] = (hi << (GMP_LIMB_BITS / 2)) | lo; | ||
850 | } | ||
851 | # else | ||
852 | { | ||
853 | mp_limb_t d; | ||
854 | y *= (mp_limb_t) 1 << (LDBL_MANT_BIT % GMP_LIMB_BITS); | ||
855 | d = (int) y; | ||
856 | y -= d; | ||
857 | if (!(y >= 0.0L && y < 1.0L)) | ||
858 | abort (); | ||
859 | m.limbs[LDBL_MANT_BIT / GMP_LIMB_BITS] = d; | ||
860 | } | ||
861 | # endif | ||
862 | # endif | ||
863 | for (i = LDBL_MANT_BIT / GMP_LIMB_BITS; i > 0;) | ||
864 | { | ||
865 | mp_limb_t hi, lo; | ||
866 | y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2); | ||
867 | hi = (int) y; | ||
868 | y -= hi; | ||
869 | if (!(y >= 0.0L && y < 1.0L)) | ||
870 | abort (); | ||
871 | y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2); | ||
872 | lo = (int) y; | ||
873 | y -= lo; | ||
874 | if (!(y >= 0.0L && y < 1.0L)) | ||
875 | abort (); | ||
876 | m.limbs[--i] = (hi << (GMP_LIMB_BITS / 2)) | lo; | ||
877 | } | ||
878 | #if 0 /* On FreeBSD 6.1/x86, 'long double' numbers sometimes have excess | ||
879 | precision. */ | ||
880 | if (!(y == 0.0L)) | ||
881 | abort (); | ||
882 | #endif | ||
883 | /* Normalise. */ | ||
884 | while (m.nlimbs > 0 && m.limbs[m.nlimbs - 1] == 0) | ||
885 | m.nlimbs--; | ||
886 | *mp = m; | ||
887 | *ep = exp - LDBL_MANT_BIT; | ||
888 | return m.limbs; | ||
889 | } | ||
890 | |||
891 | # endif | ||
892 | |||
893 | # if NEED_PRINTF_DOUBLE | ||
894 | |||
895 | /* Assuming x is finite and >= 0: | ||
896 | write x as x = 2^e * m, where m is a bignum. | ||
897 | Return the allocated memory in case of success, NULL in case of memory | ||
898 | allocation failure. */ | ||
899 | static void * | ||
900 | decode_double (double x, int *ep, mpn_t * mp) | ||
901 | { | ||
902 | mpn_t m; | ||
903 | int exp; | ||
904 | double y; | ||
905 | size_t i; | ||
906 | |||
907 | /* Allocate memory for result. */ | ||
908 | m.nlimbs = (DBL_MANT_BIT + GMP_LIMB_BITS - 1) / GMP_LIMB_BITS; | ||
909 | m.limbs = (mp_limb_t *) malloc (m.nlimbs * sizeof (mp_limb_t)); | ||
910 | if (m.limbs == NULL) | ||
911 | return NULL; | ||
912 | /* Split into exponential part and mantissa. */ | ||
913 | y = frexp (x, &exp); | ||
914 | if (!(y >= 0.0 && y < 1.0)) | ||
915 | abort (); | ||
916 | /* x = 2^exp * y = 2^(exp - DBL_MANT_BIT) * (y * DBL_MANT_BIT), and the | ||
917 | latter is an integer. */ | ||
918 | /* Convert the mantissa (y * DBL_MANT_BIT) to a sequence of limbs. | ||
919 | I'm not sure whether it's safe to cast a 'double' value between | ||
920 | 2^31 and 2^32 to 'unsigned int', therefore play safe and cast only | ||
921 | 'double' values between 0 and 2^16 (to 'unsigned int' or 'int', | ||
922 | doesn't matter). */ | ||
923 | # if (DBL_MANT_BIT % GMP_LIMB_BITS) != 0 | ||
924 | # if (DBL_MANT_BIT % GMP_LIMB_BITS) > GMP_LIMB_BITS / 2 | ||
925 | { | ||
926 | mp_limb_t hi, lo; | ||
927 | y *= (mp_limb_t) 1 << (DBL_MANT_BIT % (GMP_LIMB_BITS / 2)); | ||
928 | hi = (int) y; | ||
929 | y -= hi; | ||
930 | if (!(y >= 0.0 && y < 1.0)) | ||
931 | abort (); | ||
932 | y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2); | ||
933 | lo = (int) y; | ||
934 | y -= lo; | ||
935 | if (!(y >= 0.0 && y < 1.0)) | ||
936 | abort (); | ||
937 | m.limbs[DBL_MANT_BIT / GMP_LIMB_BITS] = (hi << (GMP_LIMB_BITS / 2)) | lo; | ||
938 | } | ||
939 | # else | ||
940 | { | ||
941 | mp_limb_t d; | ||
942 | y *= (mp_limb_t) 1 << (DBL_MANT_BIT % GMP_LIMB_BITS); | ||
943 | d = (int) y; | ||
944 | y -= d; | ||
945 | if (!(y >= 0.0 && y < 1.0)) | ||
946 | abort (); | ||
947 | m.limbs[DBL_MANT_BIT / GMP_LIMB_BITS] = d; | ||
948 | } | ||
949 | # endif | ||
950 | # endif | ||
951 | for (i = DBL_MANT_BIT / GMP_LIMB_BITS; i > 0;) | ||
952 | { | ||
953 | mp_limb_t hi, lo; | ||
954 | y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2); | ||
955 | hi = (int) y; | ||
956 | y -= hi; | ||
957 | if (!(y >= 0.0 && y < 1.0)) | ||
958 | abort (); | ||
959 | y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2); | ||
960 | lo = (int) y; | ||
961 | y -= lo; | ||
962 | if (!(y >= 0.0 && y < 1.0)) | ||
963 | abort (); | ||
964 | m.limbs[--i] = (hi << (GMP_LIMB_BITS / 2)) | lo; | ||
965 | } | ||
966 | if (!(y == 0.0)) | ||
967 | abort (); | ||
968 | /* Normalise. */ | ||
969 | while (m.nlimbs > 0 && m.limbs[m.nlimbs - 1] == 0) | ||
970 | m.nlimbs--; | ||
971 | *mp = m; | ||
972 | *ep = exp - DBL_MANT_BIT; | ||
973 | return m.limbs; | ||
974 | } | ||
975 | |||
976 | # endif | ||
977 | |||
978 | /* Assuming x = 2^e * m is finite and >= 0, and n is an integer: | ||
979 | Returns the decimal representation of round (x * 10^n). | ||
980 | Return the allocated memory - containing the decimal digits in low-to-high | ||
981 | order, terminated with a NUL character - in case of success, NULL in case | ||
982 | of memory allocation failure. */ | ||
983 | static char * | ||
984 | scale10_round_decimal_decoded (int e, mpn_t m, void *memory, int n) | ||
985 | { | ||
986 | int s; | ||
987 | size_t extra_zeroes; | ||
988 | unsigned int abs_n; | ||
989 | unsigned int abs_s; | ||
990 | mp_limb_t *pow5_ptr; | ||
991 | size_t pow5_len; | ||
992 | unsigned int s_limbs; | ||
993 | unsigned int s_bits; | ||
994 | mpn_t pow5; | ||
995 | mpn_t z; | ||
996 | void *z_memory; | ||
997 | char *digits; | ||
998 | |||
999 | if (memory == NULL) | ||
1000 | return NULL; | ||
1001 | /* x = 2^e * m, hence | ||
1002 | y = round (2^e * 10^n * m) = round (2^(e+n) * 5^n * m) | ||
1003 | = round (2^s * 5^n * m). */ | ||
1004 | s = e + n; | ||
1005 | extra_zeroes = 0; | ||
1006 | /* Factor out a common power of 10 if possible. */ | ||
1007 | if (s > 0 && n > 0) | ||
1008 | { | ||
1009 | extra_zeroes = (s < n ? s : n); | ||
1010 | s -= extra_zeroes; | ||
1011 | n -= extra_zeroes; | ||
1012 | } | ||
1013 | /* Here y = round (2^s * 5^n * m) * 10^extra_zeroes. | ||
1014 | Before converting to decimal, we need to compute | ||
1015 | z = round (2^s * 5^n * m). */ | ||
1016 | /* Compute 5^|n|, possibly shifted by |s| bits if n and s have the same | ||
1017 | sign. 2.322 is slightly larger than log(5)/log(2). */ | ||
1018 | abs_n = (n >= 0 ? n : -n); | ||
1019 | abs_s = (s >= 0 ? s : -s); | ||
1020 | pow5_ptr = | ||
1021 | (mp_limb_t *) | ||
1022 | malloc (((int) (abs_n * (2.322f / GMP_LIMB_BITS)) + 1 + | ||
1023 | abs_s / GMP_LIMB_BITS + 1) * sizeof (mp_limb_t)); | ||
1024 | if (pow5_ptr == NULL) | ||
1025 | { | ||
1026 | free (memory); | ||
1027 | return NULL; | ||
1028 | } | ||
1029 | /* Initialize with 1. */ | ||
1030 | pow5_ptr[0] = 1; | ||
1031 | pow5_len = 1; | ||
1032 | /* Multiply with 5^|n|. */ | ||
1033 | if (abs_n > 0) | ||
1034 | { | ||
1035 | static mp_limb_t const small_pow5[13 + 1] = { | ||
1036 | 1, 5, 25, 125, 625, 3125, 15625, 78125, 390625, 1953125, 9765625, | ||
1037 | 48828125, 244140625, 1220703125 | ||
1038 | }; | ||
1039 | unsigned int n13; | ||
1040 | for (n13 = 0; n13 <= abs_n; n13 += 13) | ||
1041 | { | ||
1042 | mp_limb_t digit1 = small_pow5[n13 + 13 <= abs_n ? 13 : abs_n - n13]; | ||
1043 | size_t j; | ||
1044 | mp_twolimb_t carry = 0; | ||
1045 | for (j = 0; j < pow5_len; j++) | ||
1046 | { | ||
1047 | mp_limb_t digit2 = pow5_ptr[j]; | ||
1048 | carry += (mp_twolimb_t) digit1 *(mp_twolimb_t) digit2; | ||
1049 | pow5_ptr[j] = (mp_limb_t) carry; | ||
1050 | carry = carry >> GMP_LIMB_BITS; | ||
1051 | } | ||
1052 | if (carry > 0) | ||
1053 | pow5_ptr[pow5_len++] = (mp_limb_t) carry; | ||
1054 | } | ||
1055 | } | ||
1056 | s_limbs = abs_s / GMP_LIMB_BITS; | ||
1057 | s_bits = abs_s % GMP_LIMB_BITS; | ||
1058 | if (n >= 0 ? s >= 0 : s <= 0) | ||
1059 | { | ||
1060 | /* Multiply with 2^|s|. */ | ||
1061 | if (s_bits > 0) | ||
1062 | { | ||
1063 | mp_limb_t *ptr = pow5_ptr; | ||
1064 | mp_twolimb_t accu = 0; | ||
1065 | size_t count; | ||
1066 | for (count = pow5_len; count > 0; count--) | ||
1067 | { | ||
1068 | accu += (mp_twolimb_t) * ptr << s_bits; | ||
1069 | *ptr++ = (mp_limb_t) accu; | ||
1070 | accu = accu >> GMP_LIMB_BITS; | ||
1071 | } | ||
1072 | if (accu > 0) | ||
1073 | { | ||
1074 | *ptr = (mp_limb_t) accu; | ||
1075 | pow5_len++; | ||
1076 | } | ||
1077 | } | ||
1078 | if (s_limbs > 0) | ||
1079 | { | ||
1080 | size_t count; | ||
1081 | for (count = pow5_len; count > 0;) | ||
1082 | { | ||
1083 | count--; | ||
1084 | pow5_ptr[s_limbs + count] = pow5_ptr[count]; | ||
1085 | } | ||
1086 | for (count = s_limbs; count > 0;) | ||
1087 | { | ||
1088 | count--; | ||
1089 | pow5_ptr[count] = 0; | ||
1090 | } | ||
1091 | pow5_len += s_limbs; | ||
1092 | } | ||
1093 | pow5.limbs = pow5_ptr; | ||
1094 | pow5.nlimbs = pow5_len; | ||
1095 | if (n >= 0) | ||
1096 | { | ||
1097 | /* Multiply m with pow5. No division needed. */ | ||
1098 | z_memory = multiply (m, pow5, &z); | ||
1099 | } | ||
1100 | else | ||
1101 | { | ||
1102 | /* Divide m by pow5 and round. */ | ||
1103 | z_memory = divide (m, pow5, &z); | ||
1104 | } | ||
1105 | } | ||
1106 | else | ||
1107 | { | ||
1108 | pow5.limbs = pow5_ptr; | ||
1109 | pow5.nlimbs = pow5_len; | ||
1110 | if (n >= 0) | ||
1111 | { | ||
1112 | /* n >= 0, s < 0. | ||
1113 | Multiply m with pow5, then divide by 2^|s|. */ | ||
1114 | mpn_t numerator; | ||
1115 | mpn_t denominator; | ||
1116 | void *tmp_memory; | ||
1117 | tmp_memory = multiply (m, pow5, &numerator); | ||
1118 | if (tmp_memory == NULL) | ||
1119 | { | ||
1120 | free (pow5_ptr); | ||
1121 | free (memory); | ||
1122 | return NULL; | ||
1123 | } | ||
1124 | /* Construct 2^|s|. */ | ||
1125 | { | ||
1126 | mp_limb_t *ptr = pow5_ptr + pow5_len; | ||
1127 | size_t i; | ||
1128 | for (i = 0; i < s_limbs; i++) | ||
1129 | ptr[i] = 0; | ||
1130 | ptr[s_limbs] = (mp_limb_t) 1 << s_bits; | ||
1131 | denominator.limbs = ptr; | ||
1132 | denominator.nlimbs = s_limbs + 1; | ||
1133 | } | ||
1134 | z_memory = divide (numerator, denominator, &z); | ||
1135 | free (tmp_memory); | ||
1136 | } | ||
1137 | else | ||
1138 | { | ||
1139 | /* n < 0, s > 0. | ||
1140 | Multiply m with 2^s, then divide by pow5. */ | ||
1141 | mpn_t numerator; | ||
1142 | mp_limb_t *num_ptr; | ||
1143 | num_ptr = (mp_limb_t *) malloc ((m.nlimbs + s_limbs + 1) | ||
1144 | * sizeof (mp_limb_t)); | ||
1145 | if (num_ptr == NULL) | ||
1146 | { | ||
1147 | free (pow5_ptr); | ||
1148 | free (memory); | ||
1149 | return NULL; | ||
1150 | } | ||
1151 | { | ||
1152 | mp_limb_t *destptr = num_ptr; | ||
1153 | { | ||
1154 | size_t i; | ||
1155 | for (i = 0; i < s_limbs; i++) | ||
1156 | *destptr++ = 0; | ||
1157 | } | ||
1158 | if (s_bits > 0) | ||
1159 | { | ||
1160 | const mp_limb_t *sourceptr = m.limbs; | ||
1161 | mp_twolimb_t accu = 0; | ||
1162 | size_t count; | ||
1163 | for (count = m.nlimbs; count > 0; count--) | ||
1164 | { | ||
1165 | accu += (mp_twolimb_t) * sourceptr++ << s_bits; | ||
1166 | *destptr++ = (mp_limb_t) accu; | ||
1167 | accu = accu >> GMP_LIMB_BITS; | ||
1168 | } | ||
1169 | if (accu > 0) | ||
1170 | *destptr++ = (mp_limb_t) accu; | ||
1171 | } | ||
1172 | else | ||
1173 | { | ||
1174 | const mp_limb_t *sourceptr = m.limbs; | ||
1175 | size_t count; | ||
1176 | for (count = m.nlimbs; count > 0; count--) | ||
1177 | *destptr++ = *sourceptr++; | ||
1178 | } | ||
1179 | numerator.limbs = num_ptr; | ||
1180 | numerator.nlimbs = destptr - num_ptr; | ||
1181 | } | ||
1182 | z_memory = divide (numerator, pow5, &z); | ||
1183 | free (num_ptr); | ||
1184 | } | ||
1185 | } | ||
1186 | free (pow5_ptr); | ||
1187 | free (memory); | ||
1188 | |||
1189 | /* Here y = round (x * 10^n) = z * 10^extra_zeroes. */ | ||
1190 | |||
1191 | if (z_memory == NULL) | ||
1192 | return NULL; | ||
1193 | digits = convert_to_decimal (z, extra_zeroes); | ||
1194 | free (z_memory); | ||
1195 | return digits; | ||
1196 | } | ||
1197 | |||
1198 | # if NEED_PRINTF_LONG_DOUBLE | ||
1199 | |||
1200 | /* Assuming x is finite and >= 0, and n is an integer: | ||
1201 | Returns the decimal representation of round (x * 10^n). | ||
1202 | Return the allocated memory - containing the decimal digits in low-to-high | ||
1203 | order, terminated with a NUL character - in case of success, NULL in case | ||
1204 | of memory allocation failure. */ | ||
1205 | static char * | ||
1206 | scale10_round_decimal_long_double (long double x, int n) | ||
1207 | { | ||
1208 | int e; | ||
1209 | mpn_t m; | ||
1210 | void *memory = decode_long_double (x, &e, &m); | ||
1211 | return scale10_round_decimal_decoded (e, m, memory, n); | ||
1212 | } | ||
1213 | |||
1214 | # endif | ||
1215 | |||
1216 | # if NEED_PRINTF_DOUBLE | ||
1217 | |||
1218 | /* Assuming x is finite and >= 0, and n is an integer: | ||
1219 | Returns the decimal representation of round (x * 10^n). | ||
1220 | Return the allocated memory - containing the decimal digits in low-to-high | ||
1221 | order, terminated with a NUL character - in case of success, NULL in case | ||
1222 | of memory allocation failure. */ | ||
1223 | static char * | ||
1224 | scale10_round_decimal_double (double x, int n) | ||
1225 | { | ||
1226 | int e; | ||
1227 | mpn_t m; | ||
1228 | void *memory = decode_double (x, &e, &m); | ||
1229 | return scale10_round_decimal_decoded (e, m, memory, n); | ||
1230 | } | ||
1231 | |||
1232 | # endif | ||
1233 | |||
1234 | # if NEED_PRINTF_LONG_DOUBLE | ||
1235 | |||
1236 | /* Assuming x is finite and > 0: | ||
1237 | Return an approximation for n with 10^n <= x < 10^(n+1). | ||
1238 | The approximation is usually the right n, but may be off by 1 sometimes. */ | ||
1239 | static int | ||
1240 | floorlog10l (long double x) | ||
1241 | { | ||
1242 | int exp; | ||
1243 | long double y; | ||
1244 | double z; | ||
1245 | double l; | ||
1246 | |||
1247 | /* Split into exponential part and mantissa. */ | ||
1248 | y = frexpl (x, &exp); | ||
1249 | if (!(y >= 0.0L && y < 1.0L)) | ||
1250 | abort (); | ||
1251 | if (y == 0.0L) | ||
1252 | return INT_MIN; | ||
1253 | if (y < 0.5L) | ||
1254 | { | ||
1255 | while (y < | ||
1256 | (1.0L / (1 << (GMP_LIMB_BITS / 2)) / (1 << (GMP_LIMB_BITS / 2)))) | ||
1257 | { | ||
1258 | y *= 1.0L * (1 << (GMP_LIMB_BITS / 2)) * (1 << (GMP_LIMB_BITS / 2)); | ||
1259 | exp -= GMP_LIMB_BITS; | ||
1260 | } | ||
1261 | if (y < (1.0L / (1 << 16))) | ||
1262 | { | ||
1263 | y *= 1.0L * (1 << 16); | ||
1264 | exp -= 16; | ||
1265 | } | ||
1266 | if (y < (1.0L / (1 << 8))) | ||
1267 | { | ||
1268 | y *= 1.0L * (1 << 8); | ||
1269 | exp -= 8; | ||
1270 | } | ||
1271 | if (y < (1.0L / (1 << 4))) | ||
1272 | { | ||
1273 | y *= 1.0L * (1 << 4); | ||
1274 | exp -= 4; | ||
1275 | } | ||
1276 | if (y < (1.0L / (1 << 2))) | ||
1277 | { | ||
1278 | y *= 1.0L * (1 << 2); | ||
1279 | exp -= 2; | ||
1280 | } | ||
1281 | if (y < (1.0L / (1 << 1))) | ||
1282 | { | ||
1283 | y *= 1.0L * (1 << 1); | ||
1284 | exp -= 1; | ||
1285 | } | ||
1286 | } | ||
1287 | if (!(y >= 0.5L && y < 1.0L)) | ||
1288 | abort (); | ||
1289 | /* Compute an approximation for l = log2(x) = exp + log2(y). */ | ||
1290 | l = exp; | ||
1291 | z = y; | ||
1292 | if (z < 0.70710678118654752444) | ||
1293 | { | ||
1294 | z *= 1.4142135623730950488; | ||
1295 | l -= 0.5; | ||
1296 | } | ||
1297 | if (z < 0.8408964152537145431) | ||
1298 | { | ||
1299 | z *= 1.1892071150027210667; | ||
1300 | l -= 0.25; | ||
1301 | } | ||
1302 | if (z < 0.91700404320467123175) | ||
1303 | { | ||
1304 | z *= 1.0905077326652576592; | ||
1305 | l -= 0.125; | ||
1306 | } | ||
1307 | if (z < 0.9576032806985736469) | ||
1308 | { | ||
1309 | z *= 1.0442737824274138403; | ||
1310 | l -= 0.0625; | ||
1311 | } | ||
1312 | /* Now 0.95 <= z <= 1.01. */ | ||
1313 | z = 1 - z; | ||
1314 | /* log(1-z) = - z - z^2/2 - z^3/3 - z^4/4 - ... | ||
1315 | Four terms are enough to get an approximation with error < 10^-7. */ | ||
1316 | l -= z * (1.0 + z * (0.5 + z * ((1.0 / 3) + z * 0.25))); | ||
1317 | /* Finally multiply with log(2)/log(10), yields an approximation for | ||
1318 | log10(x). */ | ||
1319 | l *= 0.30102999566398119523; | ||
1320 | /* Round down to the next integer. */ | ||
1321 | return (int) l + (l < 0 ? -1 : 0); | ||
1322 | } | ||
1323 | |||
1324 | # endif | ||
1325 | |||
1326 | # if NEED_PRINTF_DOUBLE | ||
1327 | |||
1328 | /* Assuming x is finite and > 0: | ||
1329 | Return an approximation for n with 10^n <= x < 10^(n+1). | ||
1330 | The approximation is usually the right n, but may be off by 1 sometimes. */ | ||
1331 | static int | ||
1332 | floorlog10 (double x) | ||
1333 | { | ||
1334 | int exp; | ||
1335 | double y; | ||
1336 | double z; | ||
1337 | double l; | ||
1338 | |||
1339 | /* Split into exponential part and mantissa. */ | ||
1340 | y = frexp (x, &exp); | ||
1341 | if (!(y >= 0.0 && y < 1.0)) | ||
1342 | abort (); | ||
1343 | if (y == 0.0) | ||
1344 | return INT_MIN; | ||
1345 | if (y < 0.5) | ||
1346 | { | ||
1347 | while (y < | ||
1348 | (1.0 / (1 << (GMP_LIMB_BITS / 2)) / (1 << (GMP_LIMB_BITS / 2)))) | ||
1349 | { | ||
1350 | y *= 1.0 * (1 << (GMP_LIMB_BITS / 2)) * (1 << (GMP_LIMB_BITS / 2)); | ||
1351 | exp -= GMP_LIMB_BITS; | ||
1352 | } | ||
1353 | if (y < (1.0 / (1 << 16))) | ||
1354 | { | ||
1355 | y *= 1.0 * (1 << 16); | ||
1356 | exp -= 16; | ||
1357 | } | ||
1358 | if (y < (1.0 / (1 << 8))) | ||
1359 | { | ||
1360 | y *= 1.0 * (1 << 8); | ||
1361 | exp -= 8; | ||
1362 | } | ||
1363 | if (y < (1.0 / (1 << 4))) | ||
1364 | { | ||
1365 | y *= 1.0 * (1 << 4); | ||
1366 | exp -= 4; | ||
1367 | } | ||
1368 | if (y < (1.0 / (1 << 2))) | ||
1369 | { | ||
1370 | y *= 1.0 * (1 << 2); | ||
1371 | exp -= 2; | ||
1372 | } | ||
1373 | if (y < (1.0 / (1 << 1))) | ||
1374 | { | ||
1375 | y *= 1.0 * (1 << 1); | ||
1376 | exp -= 1; | ||
1377 | } | ||
1378 | } | ||
1379 | if (!(y >= 0.5 && y < 1.0)) | ||
1380 | abort (); | ||
1381 | /* Compute an approximation for l = log2(x) = exp + log2(y). */ | ||
1382 | l = exp; | ||
1383 | z = y; | ||
1384 | if (z < 0.70710678118654752444) | ||
1385 | { | ||
1386 | z *= 1.4142135623730950488; | ||
1387 | l -= 0.5; | ||
1388 | } | ||
1389 | if (z < 0.8408964152537145431) | ||
1390 | { | ||
1391 | z *= 1.1892071150027210667; | ||
1392 | l -= 0.25; | ||
1393 | } | ||
1394 | if (z < 0.91700404320467123175) | ||
1395 | { | ||
1396 | z *= 1.0905077326652576592; | ||
1397 | l -= 0.125; | ||
1398 | } | ||
1399 | if (z < 0.9576032806985736469) | ||
1400 | { | ||
1401 | z *= 1.0442737824274138403; | ||
1402 | l -= 0.0625; | ||
1403 | } | ||
1404 | /* Now 0.95 <= z <= 1.01. */ | ||
1405 | z = 1 - z; | ||
1406 | /* log(1-z) = - z - z^2/2 - z^3/3 - z^4/4 - ... | ||
1407 | Four terms are enough to get an approximation with error < 10^-7. */ | ||
1408 | l -= z * (1.0 + z * (0.5 + z * ((1.0 / 3) + z * 0.25))); | ||
1409 | /* Finally multiply with log(2)/log(10), yields an approximation for | ||
1410 | log10(x). */ | ||
1411 | l *= 0.30102999566398119523; | ||
1412 | /* Round down to the next integer. */ | ||
1413 | return (int) l + (l < 0 ? -1 : 0); | ||
1414 | } | ||
1415 | |||
1416 | # endif | ||
1417 | |||
1418 | #endif | ||
1419 | |||
1420 | DCHAR_T * | ||
1421 | VASNPRINTF (DCHAR_T * resultbuf, size_t * lengthp, | ||
1422 | const FCHAR_T * format, va_list args) | ||
1423 | { | ||
1424 | DIRECTIVES d; | ||
1425 | arguments a; | ||
1426 | |||
1427 | if (PRINTF_PARSE (format, &d, &a) < 0) | ||
1428 | /* errno is already set. */ | ||
1429 | return NULL; | ||
1430 | |||
1431 | #define CLEANUP() \ | ||
1432 | free (d.dir); \ | ||
1433 | if (a.arg) \ | ||
1434 | free (a.arg); | ||
1435 | |||
1436 | if (PRINTF_FETCHARGS (args, &a) < 0) | ||
1437 | { | ||
1438 | CLEANUP (); | ||
1439 | errno = EINVAL; | ||
1440 | return NULL; | ||
1441 | } | ||
1442 | |||
1443 | { | ||
1444 | size_t buf_neededlength; | ||
1445 | TCHAR_T *buf; | ||
1446 | TCHAR_T *buf_malloced; | ||
1447 | const FCHAR_T *cp; | ||
1448 | size_t i; | ||
1449 | DIRECTIVE *dp; | ||
1450 | /* Output string accumulator. */ | ||
1451 | DCHAR_T *result; | ||
1452 | size_t allocated; | ||
1453 | size_t length; | ||
1454 | |||
1455 | /* Allocate a small buffer that will hold a directive passed to | ||
1456 | sprintf or snprintf. */ | ||
1457 | buf_neededlength = | ||
1458 | xsum4 (7, d.max_width_length, d.max_precision_length, 6); | ||
1459 | #if HAVE_ALLOCA | ||
1460 | if (buf_neededlength < 4000 / sizeof (TCHAR_T)) | ||
1461 | { | ||
1462 | buf = (TCHAR_T *) alloca (buf_neededlength * sizeof (TCHAR_T)); | ||
1463 | buf_malloced = NULL; | ||
1464 | } | ||
1465 | else | ||
1466 | #endif | ||
1467 | { | ||
1468 | size_t buf_memsize = xtimes (buf_neededlength, sizeof (TCHAR_T)); | ||
1469 | if (size_overflow_p (buf_memsize)) | ||
1470 | goto out_of_memory_1; | ||
1471 | buf = (TCHAR_T *) malloc (buf_memsize); | ||
1472 | if (buf == NULL) | ||
1473 | goto out_of_memory_1; | ||
1474 | buf_malloced = buf; | ||
1475 | } | ||
1476 | |||
1477 | if (resultbuf != NULL) | ||
1478 | { | ||
1479 | result = resultbuf; | ||
1480 | allocated = *lengthp; | ||
1481 | } | ||
1482 | else | ||
1483 | { | ||
1484 | result = NULL; | ||
1485 | allocated = 0; | ||
1486 | } | ||
1487 | length = 0; | ||
1488 | /* Invariants: | ||
1489 | result is either == resultbuf or == NULL or malloc-allocated. | ||
1490 | If length > 0, then result != NULL. */ | ||
1491 | |||
1492 | /* Ensures that allocated >= needed. Aborts through a jump to | ||
1493 | out_of_memory if needed is SIZE_MAX or otherwise too big. */ | ||
1494 | #define ENSURE_ALLOCATION(needed) \ | ||
1495 | if ((needed) > allocated) \ | ||
1496 | { \ | ||
1497 | size_t memory_size; \ | ||
1498 | DCHAR_T *memory; \ | ||
1499 | \ | ||
1500 | allocated = (allocated > 0 ? xtimes (allocated, 2) : 12); \ | ||
1501 | if ((needed) > allocated) \ | ||
1502 | allocated = (needed); \ | ||
1503 | memory_size = xtimes (allocated, sizeof (DCHAR_T)); \ | ||
1504 | if (size_overflow_p (memory_size)) \ | ||
1505 | goto out_of_memory; \ | ||
1506 | if (result == resultbuf || result == NULL) \ | ||
1507 | memory = (DCHAR_T *) malloc (memory_size); \ | ||
1508 | else \ | ||
1509 | memory = (DCHAR_T *) realloc (result, memory_size); \ | ||
1510 | if (memory == NULL) \ | ||
1511 | goto out_of_memory; \ | ||
1512 | if (result == resultbuf && length > 0) \ | ||
1513 | DCHAR_CPY (memory, result, length); \ | ||
1514 | result = memory; \ | ||
1515 | } | ||
1516 | |||
1517 | for (cp = format, i = 0, dp = &d.dir[0];; cp = dp->dir_end, i++, dp++) | ||
1518 | { | ||
1519 | if (cp != dp->dir_start) | ||
1520 | { | ||
1521 | size_t n = dp->dir_start - cp; | ||
1522 | size_t augmented_length = xsum (length, n); | ||
1523 | |||
1524 | ENSURE_ALLOCATION (augmented_length); | ||
1525 | /* This copies a piece of FCHAR_T[] into a DCHAR_T[]. Here we | ||
1526 | need that the format string contains only ASCII characters | ||
1527 | if FCHAR_T and DCHAR_T are not the same type. */ | ||
1528 | if (sizeof (FCHAR_T) == sizeof (DCHAR_T)) | ||
1529 | { | ||
1530 | DCHAR_CPY (result + length, (const DCHAR_T *) cp, n); | ||
1531 | length = augmented_length; | ||
1532 | } | ||
1533 | else | ||
1534 | { | ||
1535 | do | ||
1536 | result[length++] = (unsigned char) *cp++; | ||
1537 | while (--n > 0); | ||
1538 | } | ||
1539 | } | ||
1540 | if (i == d.count) | ||
1541 | break; | ||
1542 | |||
1543 | /* Execute a single directive. */ | ||
1544 | if (dp->conversion == '%') | ||
1545 | { | ||
1546 | size_t augmented_length; | ||
1547 | |||
1548 | if (!(dp->arg_index == ARG_NONE)) | ||
1549 | abort (); | ||
1550 | augmented_length = xsum (length, 1); | ||
1551 | ENSURE_ALLOCATION (augmented_length); | ||
1552 | result[length] = '%'; | ||
1553 | length = augmented_length; | ||
1554 | } | ||
1555 | else | ||
1556 | { | ||
1557 | if (!(dp->arg_index != ARG_NONE)) | ||
1558 | abort (); | ||
1559 | |||
1560 | if (dp->conversion == 'n') | ||
1561 | { | ||
1562 | switch (a.arg[dp->arg_index].type) | ||
1563 | { | ||
1564 | case TYPE_COUNT_SCHAR_POINTER: | ||
1565 | *a.arg[dp->arg_index].a.a_count_schar_pointer = length; | ||
1566 | break; | ||
1567 | case TYPE_COUNT_SHORT_POINTER: | ||
1568 | *a.arg[dp->arg_index].a.a_count_short_pointer = length; | ||
1569 | break; | ||
1570 | case TYPE_COUNT_INT_POINTER: | ||
1571 | *a.arg[dp->arg_index].a.a_count_int_pointer = length; | ||
1572 | break; | ||
1573 | case TYPE_COUNT_LONGINT_POINTER: | ||
1574 | *a.arg[dp->arg_index].a.a_count_longint_pointer = length; | ||
1575 | break; | ||
1576 | #if HAVE_LONG_LONG_INT | ||
1577 | case TYPE_COUNT_LONGLONGINT_POINTER: | ||
1578 | *a.arg[dp->arg_index].a.a_count_longlongint_pointer = | ||
1579 | length; | ||
1580 | break; | ||
1581 | #endif | ||
1582 | default: | ||
1583 | abort (); | ||
1584 | } | ||
1585 | } | ||
1586 | #if ENABLE_UNISTDIO | ||
1587 | /* The unistdio extensions. */ | ||
1588 | else if (dp->conversion == 'U') | ||
1589 | { | ||
1590 | arg_type type = a.arg[dp->arg_index].type; | ||
1591 | int flags = dp->flags; | ||
1592 | int has_width; | ||
1593 | size_t width; | ||
1594 | int has_precision; | ||
1595 | size_t precision; | ||
1596 | |||
1597 | has_width = 0; | ||
1598 | width = 0; | ||
1599 | if (dp->width_start != dp->width_end) | ||
1600 | { | ||
1601 | if (dp->width_arg_index != ARG_NONE) | ||
1602 | { | ||
1603 | int arg; | ||
1604 | |||
1605 | if (!(a.arg[dp->width_arg_index].type == TYPE_INT)) | ||
1606 | abort (); | ||
1607 | arg = a.arg[dp->width_arg_index].a.a_int; | ||
1608 | if (arg < 0) | ||
1609 | { | ||
1610 | /* "A negative field width is taken as a '-' flag | ||
1611 | followed by a positive field width." */ | ||
1612 | flags |= FLAG_LEFT; | ||
1613 | width = (unsigned int) (-arg); | ||
1614 | } | ||
1615 | else | ||
1616 | width = arg; | ||
1617 | } | ||
1618 | else | ||
1619 | { | ||
1620 | const FCHAR_T *digitp = dp->width_start; | ||
1621 | |||
1622 | do | ||
1623 | width = xsum (xtimes (width, 10), *digitp++ - '0'); | ||
1624 | while (digitp != dp->width_end); | ||
1625 | } | ||
1626 | has_width = 1; | ||
1627 | } | ||
1628 | |||
1629 | has_precision = 0; | ||
1630 | precision = 0; | ||
1631 | if (dp->precision_start != dp->precision_end) | ||
1632 | { | ||
1633 | if (dp->precision_arg_index != ARG_NONE) | ||
1634 | { | ||
1635 | int arg; | ||
1636 | |||
1637 | if (! | ||
1638 | (a.arg[dp->precision_arg_index].type == TYPE_INT)) | ||
1639 | abort (); | ||
1640 | arg = a.arg[dp->precision_arg_index].a.a_int; | ||
1641 | /* "A negative precision is taken as if the precision | ||
1642 | were omitted." */ | ||
1643 | if (arg >= 0) | ||
1644 | { | ||
1645 | precision = arg; | ||
1646 | has_precision = 1; | ||
1647 | } | ||
1648 | } | ||
1649 | else | ||
1650 | { | ||
1651 | const FCHAR_T *digitp = dp->precision_start + 1; | ||
1652 | |||
1653 | precision = 0; | ||
1654 | while (digitp != dp->precision_end) | ||
1655 | precision = | ||
1656 | xsum (xtimes (precision, 10), *digitp++ - '0'); | ||
1657 | has_precision = 1; | ||
1658 | } | ||
1659 | } | ||
1660 | |||
1661 | switch (type) | ||
1662 | { | ||
1663 | case TYPE_U8_STRING: | ||
1664 | { | ||
1665 | const uint8_t *arg = a.arg[dp->arg_index].a.a_u8_string; | ||
1666 | const uint8_t *arg_end; | ||
1667 | size_t characters; | ||
1668 | |||
1669 | if (has_precision) | ||
1670 | { | ||
1671 | /* Use only PRECISION characters, from the left. */ | ||
1672 | arg_end = arg; | ||
1673 | characters = 0; | ||
1674 | for (; precision > 0; precision--) | ||
1675 | { | ||
1676 | int count = u8_strmblen (arg_end); | ||
1677 | if (count == 0) | ||
1678 | break; | ||
1679 | if (count < 0) | ||
1680 | { | ||
1681 | if (! | ||
1682 | (result == resultbuf || result == NULL)) | ||
1683 | free (result); | ||
1684 | if (buf_malloced != NULL) | ||
1685 | free (buf_malloced); | ||
1686 | CLEANUP (); | ||
1687 | errno = EILSEQ; | ||
1688 | return NULL; | ||
1689 | } | ||
1690 | arg_end += count; | ||
1691 | characters++; | ||
1692 | } | ||
1693 | } | ||
1694 | else if (has_width) | ||
1695 | { | ||
1696 | /* Use the entire string, and count the number of | ||
1697 | characters. */ | ||
1698 | arg_end = arg; | ||
1699 | characters = 0; | ||
1700 | for (;;) | ||
1701 | { | ||
1702 | int count = u8_strmblen (arg_end); | ||
1703 | if (count == 0) | ||
1704 | break; | ||
1705 | if (count < 0) | ||
1706 | { | ||
1707 | if (! | ||
1708 | (result == resultbuf || result == NULL)) | ||
1709 | free (result); | ||
1710 | if (buf_malloced != NULL) | ||
1711 | free (buf_malloced); | ||
1712 | CLEANUP (); | ||
1713 | errno = EILSEQ; | ||
1714 | return NULL; | ||
1715 | } | ||
1716 | arg_end += count; | ||
1717 | characters++; | ||
1718 | } | ||
1719 | } | ||
1720 | else | ||
1721 | { | ||
1722 | /* Use the entire string. */ | ||
1723 | arg_end = arg + u8_strlen (arg); | ||
1724 | /* The number of characters doesn't matter. */ | ||
1725 | characters = 0; | ||
1726 | } | ||
1727 | |||
1728 | if (has_width && width > characters | ||
1729 | && !(dp->flags & FLAG_LEFT)) | ||
1730 | { | ||
1731 | size_t n = width - characters; | ||
1732 | ENSURE_ALLOCATION (xsum (length, n)); | ||
1733 | DCHAR_SET (result + length, ' ', n); | ||
1734 | length += n; | ||
1735 | } | ||
1736 | |||
1737 | # if DCHAR_IS_UINT8_T | ||
1738 | { | ||
1739 | size_t n = arg_end - arg; | ||
1740 | ENSURE_ALLOCATION (xsum (length, n)); | ||
1741 | DCHAR_CPY (result + length, arg, n); | ||
1742 | length += n; | ||
1743 | } | ||
1744 | # else | ||
1745 | { /* Convert. */ | ||
1746 | DCHAR_T *converted = result + length; | ||
1747 | size_t converted_len = allocated - length; | ||
1748 | # if DCHAR_IS_TCHAR | ||
1749 | /* Convert from UTF-8 to locale encoding. */ | ||
1750 | if (u8_conv_to_encoding (locale_charset (), | ||
1751 | iconveh_question_mark, | ||
1752 | arg, arg_end - arg, NULL, | ||
1753 | &converted, &converted_len) | ||
1754 | < 0) | ||
1755 | # else | ||
1756 | /* Convert from UTF-8 to UTF-16/UTF-32. */ | ||
1757 | converted = | ||
1758 | U8_TO_DCHAR (arg, arg_end - arg, | ||
1759 | converted, &converted_len); | ||
1760 | if (converted == NULL) | ||
1761 | # endif | ||
1762 | { | ||
1763 | int saved_errno = errno; | ||
1764 | if (!(result == resultbuf || result == NULL)) | ||
1765 | free (result); | ||
1766 | if (buf_malloced != NULL) | ||
1767 | free (buf_malloced); | ||
1768 | CLEANUP (); | ||
1769 | errno = saved_errno; | ||
1770 | return NULL; | ||
1771 | } | ||
1772 | if (converted != result + length) | ||
1773 | { | ||
1774 | ENSURE_ALLOCATION (xsum (length, converted_len)); | ||
1775 | DCHAR_CPY (result + length, converted, | ||
1776 | converted_len); | ||
1777 | free (converted); | ||
1778 | } | ||
1779 | length += converted_len; | ||
1780 | } | ||
1781 | # endif | ||
1782 | |||
1783 | if (has_width && width > characters | ||
1784 | && (dp->flags & FLAG_LEFT)) | ||
1785 | { | ||
1786 | size_t n = width - characters; | ||
1787 | ENSURE_ALLOCATION (xsum (length, n)); | ||
1788 | DCHAR_SET (result + length, ' ', n); | ||
1789 | length += n; | ||
1790 | } | ||
1791 | } | ||
1792 | break; | ||
1793 | |||
1794 | case TYPE_U16_STRING: | ||
1795 | { | ||
1796 | const uint16_t *arg = | ||
1797 | a.arg[dp->arg_index].a.a_u16_string; | ||
1798 | const uint16_t *arg_end; | ||
1799 | size_t characters; | ||
1800 | |||
1801 | if (has_precision) | ||
1802 | { | ||
1803 | /* Use only PRECISION characters, from the left. */ | ||
1804 | arg_end = arg; | ||
1805 | characters = 0; | ||
1806 | for (; precision > 0; precision--) | ||
1807 | { | ||
1808 | int count = u16_strmblen (arg_end); | ||
1809 | if (count == 0) | ||
1810 | break; | ||
1811 | if (count < 0) | ||
1812 | { | ||
1813 | if (! | ||
1814 | (result == resultbuf || result == NULL)) | ||
1815 | free (result); | ||
1816 | if (buf_malloced != NULL) | ||
1817 | free (buf_malloced); | ||
1818 | CLEANUP (); | ||
1819 | errno = EILSEQ; | ||
1820 | return NULL; | ||
1821 | } | ||
1822 | arg_end += count; | ||
1823 | characters++; | ||
1824 | } | ||
1825 | } | ||
1826 | else if (has_width) | ||
1827 | { | ||
1828 | /* Use the entire string, and count the number of | ||
1829 | characters. */ | ||
1830 | arg_end = arg; | ||
1831 | characters = 0; | ||
1832 | for (;;) | ||
1833 | { | ||
1834 | int count = u16_strmblen (arg_end); | ||
1835 | if (count == 0) | ||
1836 | break; | ||
1837 | if (count < 0) | ||
1838 | { | ||
1839 | if (! | ||
1840 | (result == resultbuf || result == NULL)) | ||
1841 | free (result); | ||
1842 | if (buf_malloced != NULL) | ||
1843 | free (buf_malloced); | ||
1844 | CLEANUP (); | ||
1845 | errno = EILSEQ; | ||
1846 | return NULL; | ||
1847 | } | ||
1848 | arg_end += count; | ||
1849 | characters++; | ||
1850 | } | ||
1851 | } | ||
1852 | else | ||
1853 | { | ||
1854 | /* Use the entire string. */ | ||
1855 | arg_end = arg + u16_strlen (arg); | ||
1856 | /* The number of characters doesn't matter. */ | ||
1857 | characters = 0; | ||
1858 | } | ||
1859 | |||
1860 | if (has_width && width > characters | ||
1861 | && !(dp->flags & FLAG_LEFT)) | ||
1862 | { | ||
1863 | size_t n = width - characters; | ||
1864 | ENSURE_ALLOCATION (xsum (length, n)); | ||
1865 | DCHAR_SET (result + length, ' ', n); | ||
1866 | length += n; | ||
1867 | } | ||
1868 | |||
1869 | # if DCHAR_IS_UINT16_T | ||
1870 | { | ||
1871 | size_t n = arg_end - arg; | ||
1872 | ENSURE_ALLOCATION (xsum (length, n)); | ||
1873 | DCHAR_CPY (result + length, arg, n); | ||
1874 | length += n; | ||
1875 | } | ||
1876 | # else | ||
1877 | { /* Convert. */ | ||
1878 | DCHAR_T *converted = result + length; | ||
1879 | size_t converted_len = allocated - length; | ||
1880 | # if DCHAR_IS_TCHAR | ||
1881 | /* Convert from UTF-16 to locale encoding. */ | ||
1882 | if (u16_conv_to_encoding (locale_charset (), | ||
1883 | iconveh_question_mark, | ||
1884 | arg, arg_end - arg, NULL, | ||
1885 | &converted, &converted_len) | ||
1886 | < 0) | ||
1887 | # else | ||
1888 | /* Convert from UTF-16 to UTF-8/UTF-32. */ | ||
1889 | converted = | ||
1890 | U16_TO_DCHAR (arg, arg_end - arg, | ||
1891 | converted, &converted_len); | ||
1892 | if (converted == NULL) | ||
1893 | # endif | ||
1894 | { | ||
1895 | int saved_errno = errno; | ||
1896 | if (!(result == resultbuf || result == NULL)) | ||
1897 | free (result); | ||
1898 | if (buf_malloced != NULL) | ||
1899 | free (buf_malloced); | ||
1900 | CLEANUP (); | ||
1901 | errno = saved_errno; | ||
1902 | return NULL; | ||
1903 | } | ||
1904 | if (converted != result + length) | ||
1905 | { | ||
1906 | ENSURE_ALLOCATION (xsum (length, converted_len)); | ||
1907 | DCHAR_CPY (result + length, converted, | ||
1908 | converted_len); | ||
1909 | free (converted); | ||
1910 | } | ||
1911 | length += converted_len; | ||
1912 | } | ||
1913 | # endif | ||
1914 | |||
1915 | if (has_width && width > characters | ||
1916 | && (dp->flags & FLAG_LEFT)) | ||
1917 | { | ||
1918 | size_t n = width - characters; | ||
1919 | ENSURE_ALLOCATION (xsum (length, n)); | ||
1920 | DCHAR_SET (result + length, ' ', n); | ||
1921 | length += n; | ||
1922 | } | ||
1923 | } | ||
1924 | break; | ||
1925 | |||
1926 | case TYPE_U32_STRING: | ||
1927 | { | ||
1928 | const uint32_t *arg = | ||
1929 | a.arg[dp->arg_index].a.a_u32_string; | ||
1930 | const uint32_t *arg_end; | ||
1931 | size_t characters; | ||
1932 | |||
1933 | if (has_precision) | ||
1934 | { | ||
1935 | /* Use only PRECISION characters, from the left. */ | ||
1936 | arg_end = arg; | ||
1937 | characters = 0; | ||
1938 | for (; precision > 0; precision--) | ||
1939 | { | ||
1940 | int count = u32_strmblen (arg_end); | ||
1941 | if (count == 0) | ||
1942 | break; | ||
1943 | if (count < 0) | ||
1944 | { | ||
1945 | if (! | ||
1946 | (result == resultbuf || result == NULL)) | ||
1947 | free (result); | ||
1948 | if (buf_malloced != NULL) | ||
1949 | free (buf_malloced); | ||
1950 | CLEANUP (); | ||
1951 | errno = EILSEQ; | ||
1952 | return NULL; | ||
1953 | } | ||
1954 | arg_end += count; | ||
1955 | characters++; | ||
1956 | } | ||
1957 | } | ||
1958 | else if (has_width) | ||
1959 | { | ||
1960 | /* Use the entire string, and count the number of | ||
1961 | characters. */ | ||
1962 | arg_end = arg; | ||
1963 | characters = 0; | ||
1964 | for (;;) | ||
1965 | { | ||
1966 | int count = u32_strmblen (arg_end); | ||
1967 | if (count == 0) | ||
1968 | break; | ||
1969 | if (count < 0) | ||
1970 | { | ||
1971 | if (! | ||
1972 | (result == resultbuf || result == NULL)) | ||
1973 | free (result); | ||
1974 | if (buf_malloced != NULL) | ||
1975 | free (buf_malloced); | ||
1976 | CLEANUP (); | ||
1977 | errno = EILSEQ; | ||
1978 | return NULL; | ||
1979 | } | ||
1980 | arg_end += count; | ||
1981 | characters++; | ||
1982 | } | ||
1983 | } | ||
1984 | else | ||
1985 | { | ||
1986 | /* Use the entire string. */ | ||
1987 | arg_end = arg + u32_strlen (arg); | ||
1988 | /* The number of characters doesn't matter. */ | ||
1989 | characters = 0; | ||
1990 | } | ||
1991 | |||
1992 | if (has_width && width > characters | ||
1993 | && !(dp->flags & FLAG_LEFT)) | ||
1994 | { | ||
1995 | size_t n = width - characters; | ||
1996 | ENSURE_ALLOCATION (xsum (length, n)); | ||
1997 | DCHAR_SET (result + length, ' ', n); | ||
1998 | length += n; | ||
1999 | } | ||
2000 | |||
2001 | # if DCHAR_IS_UINT32_T | ||
2002 | { | ||
2003 | size_t n = arg_end - arg; | ||
2004 | ENSURE_ALLOCATION (xsum (length, n)); | ||
2005 | DCHAR_CPY (result + length, arg, n); | ||
2006 | length += n; | ||
2007 | } | ||
2008 | # else | ||
2009 | { /* Convert. */ | ||
2010 | DCHAR_T *converted = result + length; | ||
2011 | size_t converted_len = allocated - length; | ||
2012 | # if DCHAR_IS_TCHAR | ||
2013 | /* Convert from UTF-32 to locale encoding. */ | ||
2014 | if (u32_conv_to_encoding (locale_charset (), | ||
2015 | iconveh_question_mark, | ||
2016 | arg, arg_end - arg, NULL, | ||
2017 | &converted, &converted_len) | ||
2018 | < 0) | ||
2019 | # else | ||
2020 | /* Convert from UTF-32 to UTF-8/UTF-16. */ | ||
2021 | converted = | ||
2022 | U32_TO_DCHAR (arg, arg_end - arg, | ||
2023 | converted, &converted_len); | ||
2024 | if (converted == NULL) | ||
2025 | # endif | ||
2026 | { | ||
2027 | int saved_errno = errno; | ||
2028 | if (!(result == resultbuf || result == NULL)) | ||
2029 | free (result); | ||
2030 | if (buf_malloced != NULL) | ||
2031 | free (buf_malloced); | ||
2032 | CLEANUP (); | ||
2033 | errno = saved_errno; | ||
2034 | return NULL; | ||
2035 | } | ||
2036 | if (converted != result + length) | ||
2037 | { | ||
2038 | ENSURE_ALLOCATION (xsum (length, converted_len)); | ||
2039 | DCHAR_CPY (result + length, converted, | ||
2040 | converted_len); | ||
2041 | free (converted); | ||
2042 | } | ||
2043 | length += converted_len; | ||
2044 | } | ||
2045 | # endif | ||
2046 | |||
2047 | if (has_width && width > characters | ||
2048 | && (dp->flags & FLAG_LEFT)) | ||
2049 | { | ||
2050 | size_t n = width - characters; | ||
2051 | ENSURE_ALLOCATION (xsum (length, n)); | ||
2052 | DCHAR_SET (result + length, ' ', n); | ||
2053 | length += n; | ||
2054 | } | ||
2055 | } | ||
2056 | break; | ||
2057 | |||
2058 | default: | ||
2059 | abort (); | ||
2060 | } | ||
2061 | } | ||
2062 | #endif | ||
2063 | #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL | ||
2064 | else if ((dp->conversion == 'a' || dp->conversion == 'A') | ||
2065 | # if !(NEED_PRINTF_DIRECTIVE_A || (NEED_PRINTF_LONG_DOUBLE && NEED_PRINTF_DOUBLE)) | ||
2066 | && (0 | ||
2067 | # if NEED_PRINTF_DOUBLE | ||
2068 | || a.arg[dp->arg_index].type == TYPE_DOUBLE | ||
2069 | # endif | ||
2070 | # if NEED_PRINTF_LONG_DOUBLE | ||
2071 | || a.arg[dp->arg_index].type == TYPE_LONGDOUBLE | ||
2072 | # endif | ||
2073 | ) | ||
2074 | # endif | ||
2075 | ) | ||
2076 | { | ||
2077 | arg_type type = a.arg[dp->arg_index].type; | ||
2078 | int flags = dp->flags; | ||
2079 | int has_width; | ||
2080 | size_t width; | ||
2081 | int has_precision; | ||
2082 | size_t precision; | ||
2083 | size_t tmp_length; | ||
2084 | DCHAR_T tmpbuf[700]; | ||
2085 | DCHAR_T *tmp; | ||
2086 | DCHAR_T *pad_ptr; | ||
2087 | DCHAR_T *p; | ||
2088 | |||
2089 | has_width = 0; | ||
2090 | width = 0; | ||
2091 | if (dp->width_start != dp->width_end) | ||
2092 | { | ||
2093 | if (dp->width_arg_index != ARG_NONE) | ||
2094 | { | ||
2095 | int arg; | ||
2096 | |||
2097 | if (!(a.arg[dp->width_arg_index].type == TYPE_INT)) | ||
2098 | abort (); | ||
2099 | arg = a.arg[dp->width_arg_index].a.a_int; | ||
2100 | if (arg < 0) | ||
2101 | { | ||
2102 | /* "A negative field width is taken as a '-' flag | ||
2103 | followed by a positive field width." */ | ||
2104 | flags |= FLAG_LEFT; | ||
2105 | width = (unsigned int) (-arg); | ||
2106 | } | ||
2107 | else | ||
2108 | width = arg; | ||
2109 | } | ||
2110 | else | ||
2111 | { | ||
2112 | const FCHAR_T *digitp = dp->width_start; | ||
2113 | |||
2114 | do | ||
2115 | width = xsum (xtimes (width, 10), *digitp++ - '0'); | ||
2116 | while (digitp != dp->width_end); | ||
2117 | } | ||
2118 | has_width = 1; | ||
2119 | } | ||
2120 | |||
2121 | has_precision = 0; | ||
2122 | precision = 0; | ||
2123 | if (dp->precision_start != dp->precision_end) | ||
2124 | { | ||
2125 | if (dp->precision_arg_index != ARG_NONE) | ||
2126 | { | ||
2127 | int arg; | ||
2128 | |||
2129 | if (! | ||
2130 | (a.arg[dp->precision_arg_index].type == TYPE_INT)) | ||
2131 | abort (); | ||
2132 | arg = a.arg[dp->precision_arg_index].a.a_int; | ||
2133 | /* "A negative precision is taken as if the precision | ||
2134 | were omitted." */ | ||
2135 | if (arg >= 0) | ||
2136 | { | ||
2137 | precision = arg; | ||
2138 | has_precision = 1; | ||
2139 | } | ||
2140 | } | ||
2141 | else | ||
2142 | { | ||
2143 | const FCHAR_T *digitp = dp->precision_start + 1; | ||
2144 | |||
2145 | precision = 0; | ||
2146 | while (digitp != dp->precision_end) | ||
2147 | precision = | ||
2148 | xsum (xtimes (precision, 10), *digitp++ - '0'); | ||
2149 | has_precision = 1; | ||
2150 | } | ||
2151 | } | ||
2152 | |||
2153 | /* Allocate a temporary buffer of sufficient size. */ | ||
2154 | if (type == TYPE_LONGDOUBLE) | ||
2155 | tmp_length = (unsigned int) ((LDBL_DIG + 1) * 0.831 /* decimal -> hexadecimal */ | ||
2156 | ) + 1; /* turn floor into ceil */ | ||
2157 | else | ||
2158 | tmp_length = (unsigned int) ((DBL_DIG + 1) * 0.831 /* decimal -> hexadecimal */ | ||
2159 | ) + 1; /* turn floor into ceil */ | ||
2160 | if (tmp_length < precision) | ||
2161 | tmp_length = precision; | ||
2162 | /* Account for sign, decimal point etc. */ | ||
2163 | tmp_length = xsum (tmp_length, 12); | ||
2164 | |||
2165 | if (tmp_length < width) | ||
2166 | tmp_length = width; | ||
2167 | |||
2168 | tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */ | ||
2169 | |||
2170 | if (tmp_length <= sizeof (tmpbuf) / sizeof (DCHAR_T)) | ||
2171 | tmp = tmpbuf; | ||
2172 | else | ||
2173 | { | ||
2174 | size_t tmp_memsize = | ||
2175 | xtimes (tmp_length, sizeof (DCHAR_T)); | ||
2176 | |||
2177 | if (size_overflow_p (tmp_memsize)) | ||
2178 | /* Overflow, would lead to out of memory. */ | ||
2179 | goto out_of_memory; | ||
2180 | tmp = (DCHAR_T *) malloc (tmp_memsize); | ||
2181 | if (tmp == NULL) | ||
2182 | /* Out of memory. */ | ||
2183 | goto out_of_memory; | ||
2184 | } | ||
2185 | |||
2186 | pad_ptr = NULL; | ||
2187 | p = tmp; | ||
2188 | if (type == TYPE_LONGDOUBLE) | ||
2189 | { | ||
2190 | # if NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE | ||
2191 | long double arg = a.arg[dp->arg_index].a.a_longdouble; | ||
2192 | |||
2193 | if (isnanl (arg)) | ||
2194 | { | ||
2195 | if (dp->conversion == 'A') | ||
2196 | { | ||
2197 | *p++ = 'N'; | ||
2198 | *p++ = 'A'; | ||
2199 | *p++ = 'N'; | ||
2200 | } | ||
2201 | else | ||
2202 | { | ||
2203 | *p++ = 'n'; | ||
2204 | *p++ = 'a'; | ||
2205 | *p++ = 'n'; | ||
2206 | } | ||
2207 | } | ||
2208 | else | ||
2209 | { | ||
2210 | int sign = 0; | ||
2211 | DECL_LONG_DOUBLE_ROUNDING | ||
2212 | BEGIN_LONG_DOUBLE_ROUNDING (); | ||
2213 | |||
2214 | if (signbit (arg)) /* arg < 0.0L or negative zero */ | ||
2215 | { | ||
2216 | sign = -1; | ||
2217 | arg = -arg; | ||
2218 | } | ||
2219 | |||
2220 | if (sign < 0) | ||
2221 | *p++ = '-'; | ||
2222 | else if (flags & FLAG_SHOWSIGN) | ||
2223 | *p++ = '+'; | ||
2224 | else if (flags & FLAG_SPACE) | ||
2225 | *p++ = ' '; | ||
2226 | |||
2227 | if (arg > 0.0L && arg + arg == arg) | ||
2228 | { | ||
2229 | if (dp->conversion == 'A') | ||
2230 | { | ||
2231 | *p++ = 'I'; | ||
2232 | *p++ = 'N'; | ||
2233 | *p++ = 'F'; | ||
2234 | } | ||
2235 | else | ||
2236 | { | ||
2237 | *p++ = 'i'; | ||
2238 | *p++ = 'n'; | ||
2239 | *p++ = 'f'; | ||
2240 | } | ||
2241 | } | ||
2242 | else | ||
2243 | { | ||
2244 | int exponent; | ||
2245 | long double mantissa; | ||
2246 | |||
2247 | if (arg > 0.0L) | ||
2248 | mantissa = printf_frexpl (arg, &exponent); | ||
2249 | else | ||
2250 | { | ||
2251 | exponent = 0; | ||
2252 | mantissa = 0.0L; | ||
2253 | } | ||
2254 | |||
2255 | if (has_precision | ||
2256 | && precision < | ||
2257 | (unsigned int) ((LDBL_DIG + 1) * 0.831) + 1) | ||
2258 | { | ||
2259 | /* Round the mantissa. */ | ||
2260 | long double tail = mantissa; | ||
2261 | size_t q; | ||
2262 | |||
2263 | for (q = precision;; q--) | ||
2264 | { | ||
2265 | int digit = (int) tail; | ||
2266 | tail -= digit; | ||
2267 | if (q == 0) | ||
2268 | { | ||
2269 | if (digit & 1 ? tail >= 0.5L : tail > | ||
2270 | 0.5L) | ||
2271 | tail = 1 - tail; | ||
2272 | else | ||
2273 | tail = -tail; | ||
2274 | break; | ||
2275 | } | ||
2276 | tail *= 16.0L; | ||
2277 | } | ||
2278 | if (tail != 0.0L) | ||
2279 | for (q = precision; q > 0; q--) | ||
2280 | tail *= 0.0625L; | ||
2281 | mantissa += tail; | ||
2282 | } | ||
2283 | |||
2284 | *p++ = '0'; | ||
2285 | *p++ = dp->conversion - 'A' + 'X'; | ||
2286 | pad_ptr = p; | ||
2287 | { | ||
2288 | int digit; | ||
2289 | |||
2290 | digit = (int) mantissa; | ||
2291 | mantissa -= digit; | ||
2292 | *p++ = '0' + digit; | ||
2293 | if ((flags & FLAG_ALT) | ||
2294 | || mantissa > 0.0L || precision > 0) | ||
2295 | { | ||
2296 | *p++ = decimal_point_char (); | ||
2297 | /* This loop terminates because we assume | ||
2298 | that FLT_RADIX is a power of 2. */ | ||
2299 | while (mantissa > 0.0L) | ||
2300 | { | ||
2301 | mantissa *= 16.0L; | ||
2302 | digit = (int) mantissa; | ||
2303 | mantissa -= digit; | ||
2304 | *p++ = digit | ||
2305 | + (digit < 10 | ||
2306 | ? '0' : dp->conversion - 10); | ||
2307 | if (precision > 0) | ||
2308 | precision--; | ||
2309 | } | ||
2310 | while (precision > 0) | ||
2311 | { | ||
2312 | *p++ = '0'; | ||
2313 | precision--; | ||
2314 | } | ||
2315 | } | ||
2316 | } | ||
2317 | *p++ = dp->conversion - 'A' + 'P'; | ||
2318 | # if WIDE_CHAR_VERSION | ||
2319 | { | ||
2320 | static const wchar_t decimal_format[] = | ||
2321 | { '%', '+', 'd', '\0' }; | ||
2322 | SNPRINTF (p, 6 + 1, decimal_format, exponent); | ||
2323 | } | ||
2324 | while (*p != '\0') | ||
2325 | p++; | ||
2326 | # else | ||
2327 | if (sizeof (DCHAR_T) == 1) | ||
2328 | { | ||
2329 | sprintf ((char *) p, "%+d", exponent); | ||
2330 | while (*p != '\0') | ||
2331 | p++; | ||
2332 | } | ||
2333 | else | ||
2334 | { | ||
2335 | char expbuf[6 + 1]; | ||
2336 | const char *ep; | ||
2337 | sprintf (expbuf, "%+d", exponent); | ||
2338 | for (ep = expbuf; (*p = *ep) != '\0'; ep++) | ||
2339 | p++; | ||
2340 | } | ||
2341 | # endif | ||
2342 | } | ||
2343 | |||
2344 | END_LONG_DOUBLE_ROUNDING (); | ||
2345 | } | ||
2346 | # else | ||
2347 | abort (); | ||
2348 | # endif | ||
2349 | } | ||
2350 | else | ||
2351 | { | ||
2352 | # if NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_DOUBLE | ||
2353 | double arg = a.arg[dp->arg_index].a.a_double; | ||
2354 | |||
2355 | if (isnan (arg)) | ||
2356 | { | ||
2357 | if (dp->conversion == 'A') | ||
2358 | { | ||
2359 | *p++ = 'N'; | ||
2360 | *p++ = 'A'; | ||
2361 | *p++ = 'N'; | ||
2362 | } | ||
2363 | else | ||
2364 | { | ||
2365 | *p++ = 'n'; | ||
2366 | *p++ = 'a'; | ||
2367 | *p++ = 'n'; | ||
2368 | } | ||
2369 | } | ||
2370 | else | ||
2371 | { | ||
2372 | int sign = 0; | ||
2373 | |||
2374 | if (signbit (arg)) /* arg < 0.0 or negative zero */ | ||
2375 | { | ||
2376 | sign = -1; | ||
2377 | arg = -arg; | ||
2378 | } | ||
2379 | |||
2380 | if (sign < 0) | ||
2381 | *p++ = '-'; | ||
2382 | else if (flags & FLAG_SHOWSIGN) | ||
2383 | *p++ = '+'; | ||
2384 | else if (flags & FLAG_SPACE) | ||
2385 | *p++ = ' '; | ||
2386 | |||
2387 | if (arg > 0.0 && arg + arg == arg) | ||
2388 | { | ||
2389 | if (dp->conversion == 'A') | ||
2390 | { | ||
2391 | *p++ = 'I'; | ||
2392 | *p++ = 'N'; | ||
2393 | *p++ = 'F'; | ||
2394 | } | ||
2395 | else | ||
2396 | { | ||
2397 | *p++ = 'i'; | ||
2398 | *p++ = 'n'; | ||
2399 | *p++ = 'f'; | ||
2400 | } | ||
2401 | } | ||
2402 | else | ||
2403 | { | ||
2404 | int exponent; | ||
2405 | double mantissa; | ||
2406 | |||
2407 | if (arg > 0.0) | ||
2408 | mantissa = printf_frexp (arg, &exponent); | ||
2409 | else | ||
2410 | { | ||
2411 | exponent = 0; | ||
2412 | mantissa = 0.0; | ||
2413 | } | ||
2414 | |||
2415 | if (has_precision | ||
2416 | && precision < | ||
2417 | (unsigned int) ((DBL_DIG + 1) * 0.831) + 1) | ||
2418 | { | ||
2419 | /* Round the mantissa. */ | ||
2420 | double tail = mantissa; | ||
2421 | size_t q; | ||
2422 | |||
2423 | for (q = precision;; q--) | ||
2424 | { | ||
2425 | int digit = (int) tail; | ||
2426 | tail -= digit; | ||
2427 | if (q == 0) | ||
2428 | { | ||
2429 | if (digit & 1 ? tail >= 0.5 : tail > | ||
2430 | 0.5) | ||
2431 | tail = 1 - tail; | ||
2432 | else | ||
2433 | tail = -tail; | ||
2434 | break; | ||
2435 | } | ||
2436 | tail *= 16.0; | ||
2437 | } | ||
2438 | if (tail != 0.0) | ||
2439 | for (q = precision; q > 0; q--) | ||
2440 | tail *= 0.0625; | ||
2441 | mantissa += tail; | ||
2442 | } | ||
2443 | |||
2444 | *p++ = '0'; | ||
2445 | *p++ = dp->conversion - 'A' + 'X'; | ||
2446 | pad_ptr = p; | ||
2447 | { | ||
2448 | int digit; | ||
2449 | |||
2450 | digit = (int) mantissa; | ||
2451 | mantissa -= digit; | ||
2452 | *p++ = '0' + digit; | ||
2453 | if ((flags & FLAG_ALT) | ||
2454 | || mantissa > 0.0 || precision > 0) | ||
2455 | { | ||
2456 | *p++ = decimal_point_char (); | ||
2457 | /* This loop terminates because we assume | ||
2458 | that FLT_RADIX is a power of 2. */ | ||
2459 | while (mantissa > 0.0) | ||
2460 | { | ||
2461 | mantissa *= 16.0; | ||
2462 | digit = (int) mantissa; | ||
2463 | mantissa -= digit; | ||
2464 | *p++ = digit | ||
2465 | + (digit < 10 | ||
2466 | ? '0' : dp->conversion - 10); | ||
2467 | if (precision > 0) | ||
2468 | precision--; | ||
2469 | } | ||
2470 | while (precision > 0) | ||
2471 | { | ||
2472 | *p++ = '0'; | ||
2473 | precision--; | ||
2474 | } | ||
2475 | } | ||
2476 | } | ||
2477 | *p++ = dp->conversion - 'A' + 'P'; | ||
2478 | # if WIDE_CHAR_VERSION | ||
2479 | { | ||
2480 | static const wchar_t decimal_format[] = | ||
2481 | { '%', '+', 'd', '\0' }; | ||
2482 | SNPRINTF (p, 6 + 1, decimal_format, exponent); | ||
2483 | } | ||
2484 | while (*p != '\0') | ||
2485 | p++; | ||
2486 | # else | ||
2487 | if (sizeof (DCHAR_T) == 1) | ||
2488 | { | ||
2489 | sprintf ((char *) p, "%+d", exponent); | ||
2490 | while (*p != '\0') | ||
2491 | p++; | ||
2492 | } | ||
2493 | else | ||
2494 | { | ||
2495 | char expbuf[6 + 1]; | ||
2496 | const char *ep; | ||
2497 | sprintf (expbuf, "%+d", exponent); | ||
2498 | for (ep = expbuf; (*p = *ep) != '\0'; ep++) | ||
2499 | p++; | ||
2500 | } | ||
2501 | # endif | ||
2502 | } | ||
2503 | } | ||
2504 | # else | ||
2505 | abort (); | ||
2506 | # endif | ||
2507 | } | ||
2508 | /* The generated string now extends from tmp to p, with the | ||
2509 | zero padding insertion point being at pad_ptr. */ | ||
2510 | if (has_width && p - tmp < width) | ||
2511 | { | ||
2512 | size_t pad = width - (p - tmp); | ||
2513 | DCHAR_T *end = p + pad; | ||
2514 | |||
2515 | if (flags & FLAG_LEFT) | ||
2516 | { | ||
2517 | /* Pad with spaces on the right. */ | ||
2518 | for (; pad > 0; pad--) | ||
2519 | *p++ = ' '; | ||
2520 | } | ||
2521 | else if ((flags & FLAG_ZERO) && pad_ptr != NULL) | ||
2522 | { | ||
2523 | /* Pad with zeroes. */ | ||
2524 | DCHAR_T *q = end; | ||
2525 | |||
2526 | while (p > pad_ptr) | ||
2527 | *--q = *--p; | ||
2528 | for (; pad > 0; pad--) | ||
2529 | *p++ = '0'; | ||
2530 | } | ||
2531 | else | ||
2532 | { | ||
2533 | /* Pad with spaces on the left. */ | ||
2534 | DCHAR_T *q = end; | ||
2535 | |||
2536 | while (p > tmp) | ||
2537 | *--q = *--p; | ||
2538 | for (; pad > 0; pad--) | ||
2539 | *p++ = ' '; | ||
2540 | } | ||
2541 | |||
2542 | p = end; | ||
2543 | } | ||
2544 | |||
2545 | { | ||
2546 | size_t count = p - tmp; | ||
2547 | |||
2548 | if (count >= tmp_length) | ||
2549 | /* tmp_length was incorrectly calculated - fix the | ||
2550 | code above! */ | ||
2551 | abort (); | ||
2552 | |||
2553 | /* Make room for the result. */ | ||
2554 | if (count >= allocated - length) | ||
2555 | { | ||
2556 | size_t n = xsum (length, count); | ||
2557 | |||
2558 | ENSURE_ALLOCATION (n); | ||
2559 | } | ||
2560 | |||
2561 | /* Append the result. */ | ||
2562 | memcpy (result + length, tmp, count * sizeof (DCHAR_T)); | ||
2563 | if (tmp != tmpbuf) | ||
2564 | free (tmp); | ||
2565 | length += count; | ||
2566 | } | ||
2567 | } | ||
2568 | #endif | ||
2569 | #if (NEED_PRINTF_INFINITE_DOUBLE || NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL | ||
2570 | else if ((dp->conversion == 'f' || dp->conversion == 'F' | ||
2571 | || dp->conversion == 'e' || dp->conversion == 'E' | ||
2572 | || dp->conversion == 'g' || dp->conversion == 'G' | ||
2573 | || dp->conversion == 'a' || dp->conversion == 'A') && (0 | ||
2574 | # if NEED_PRINTF_DOUBLE | ||
2575 | || | ||
2576 | a. | ||
2577 | arg | ||
2578 | [dp-> | ||
2579 | arg_index]. | ||
2580 | type | ||
2581 | == | ||
2582 | TYPE_DOUBLE | ||
2583 | # elif NEED_PRINTF_INFINITE_DOUBLE | ||
2584 | || | ||
2585 | (a. | ||
2586 | arg | ||
2587 | [dp-> | ||
2588 | arg_index]. | ||
2589 | type | ||
2590 | == | ||
2591 | TYPE_DOUBLE | ||
2592 | /* The systems (mingw) which produce wrong output | ||
2593 | for Inf, -Inf, and NaN also do so for -0.0. | ||
2594 | Therefore we treat this case here as well. */ | ||
2595 | && | ||
2596 | is_infinite_or_zero | ||
2597 | (a. | ||
2598 | arg | ||
2599 | [dp-> | ||
2600 | arg_index]. | ||
2601 | a. | ||
2602 | a_double)) | ||
2603 | # endif | ||
2604 | # if NEED_PRINTF_LONG_DOUBLE | ||
2605 | || | ||
2606 | a. | ||
2607 | arg | ||
2608 | [dp-> | ||
2609 | arg_index]. | ||
2610 | type | ||
2611 | == | ||
2612 | TYPE_LONGDOUBLE | ||
2613 | # elif NEED_PRINTF_INFINITE_LONG_DOUBLE | ||
2614 | || | ||
2615 | (a. | ||
2616 | arg | ||
2617 | [dp-> | ||
2618 | arg_index]. | ||
2619 | type | ||
2620 | == | ||
2621 | TYPE_LONGDOUBLE | ||
2622 | /* Some systems produce wrong output for Inf, | ||
2623 | -Inf, and NaN. */ | ||
2624 | && | ||
2625 | is_infinitel | ||
2626 | (a. | ||
2627 | arg | ||
2628 | [dp-> | ||
2629 | arg_index]. | ||
2630 | a. | ||
2631 | a_longdouble)) | ||
2632 | # endif | ||
2633 | )) | ||
2634 | { | ||
2635 | # if (NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE) | ||
2636 | arg_type type = a.arg[dp->arg_index].type; | ||
2637 | # endif | ||
2638 | int flags = dp->flags; | ||
2639 | int has_width; | ||
2640 | size_t width; | ||
2641 | int has_precision; | ||
2642 | size_t precision; | ||
2643 | size_t tmp_length; | ||
2644 | DCHAR_T tmpbuf[700]; | ||
2645 | DCHAR_T *tmp; | ||
2646 | DCHAR_T *pad_ptr; | ||
2647 | DCHAR_T *p; | ||
2648 | |||
2649 | has_width = 0; | ||
2650 | width = 0; | ||
2651 | if (dp->width_start != dp->width_end) | ||
2652 | { | ||
2653 | if (dp->width_arg_index != ARG_NONE) | ||
2654 | { | ||
2655 | int arg; | ||
2656 | |||
2657 | if (!(a.arg[dp->width_arg_index].type == TYPE_INT)) | ||
2658 | abort (); | ||
2659 | arg = a.arg[dp->width_arg_index].a.a_int; | ||
2660 | if (arg < 0) | ||
2661 | { | ||
2662 | /* "A negative field width is taken as a '-' flag | ||
2663 | followed by a positive field width." */ | ||
2664 | flags |= FLAG_LEFT; | ||
2665 | width = (unsigned int) (-arg); | ||
2666 | } | ||
2667 | else | ||
2668 | width = arg; | ||
2669 | } | ||
2670 | else | ||
2671 | { | ||
2672 | const FCHAR_T *digitp = dp->width_start; | ||
2673 | |||
2674 | do | ||
2675 | width = xsum (xtimes (width, 10), *digitp++ - '0'); | ||
2676 | while (digitp != dp->width_end); | ||
2677 | } | ||
2678 | has_width = 1; | ||
2679 | } | ||
2680 | |||
2681 | has_precision = 0; | ||
2682 | precision = 0; | ||
2683 | if (dp->precision_start != dp->precision_end) | ||
2684 | { | ||
2685 | if (dp->precision_arg_index != ARG_NONE) | ||
2686 | { | ||
2687 | int arg; | ||
2688 | |||
2689 | if (! | ||
2690 | (a.arg[dp->precision_arg_index].type == TYPE_INT)) | ||
2691 | abort (); | ||
2692 | arg = a.arg[dp->precision_arg_index].a.a_int; | ||
2693 | /* "A negative precision is taken as if the precision | ||
2694 | were omitted." */ | ||
2695 | if (arg >= 0) | ||
2696 | { | ||
2697 | precision = arg; | ||
2698 | has_precision = 1; | ||
2699 | } | ||
2700 | } | ||
2701 | else | ||
2702 | { | ||
2703 | const FCHAR_T *digitp = dp->precision_start + 1; | ||
2704 | |||
2705 | precision = 0; | ||
2706 | while (digitp != dp->precision_end) | ||
2707 | precision = | ||
2708 | xsum (xtimes (precision, 10), *digitp++ - '0'); | ||
2709 | has_precision = 1; | ||
2710 | } | ||
2711 | } | ||
2712 | |||
2713 | /* POSIX specifies the default precision to be 6 for %f, %F, | ||
2714 | %e, %E, but not for %g, %G. Implementations appear to use | ||
2715 | the same default precision also for %g, %G. */ | ||
2716 | if (!has_precision) | ||
2717 | precision = 6; | ||
2718 | |||
2719 | /* Allocate a temporary buffer of sufficient size. */ | ||
2720 | # if NEED_PRINTF_DOUBLE && NEED_PRINTF_LONG_DOUBLE | ||
2721 | tmp_length = | ||
2722 | (type == TYPE_LONGDOUBLE ? LDBL_DIG + 1 : DBL_DIG + 1); | ||
2723 | # elif NEED_PRINTF_INFINITE_DOUBLE && NEED_PRINTF_LONG_DOUBLE | ||
2724 | tmp_length = (type == TYPE_LONGDOUBLE ? LDBL_DIG + 1 : 0); | ||
2725 | # elif NEED_PRINTF_LONG_DOUBLE | ||
2726 | tmp_length = LDBL_DIG + 1; | ||
2727 | # elif NEED_PRINTF_DOUBLE | ||
2728 | tmp_length = DBL_DIG + 1; | ||
2729 | # else | ||
2730 | tmp_length = 0; | ||
2731 | # endif | ||
2732 | if (tmp_length < precision) | ||
2733 | tmp_length = precision; | ||
2734 | # if NEED_PRINTF_LONG_DOUBLE | ||
2735 | # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE | ||
2736 | if (type == TYPE_LONGDOUBLE) | ||
2737 | # endif | ||
2738 | if (dp->conversion == 'f' || dp->conversion == 'F') | ||
2739 | { | ||
2740 | long double arg = a.arg[dp->arg_index].a.a_longdouble; | ||
2741 | if (!(isnanl (arg) || arg + arg == arg)) | ||
2742 | { | ||
2743 | /* arg is finite and nonzero. */ | ||
2744 | int exponent = floorlog10l (arg < 0 ? -arg : arg); | ||
2745 | if (exponent >= 0 | ||
2746 | && tmp_length < exponent + precision) | ||
2747 | tmp_length = exponent + precision; | ||
2748 | } | ||
2749 | } | ||
2750 | # endif | ||
2751 | # if NEED_PRINTF_DOUBLE | ||
2752 | # if NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE | ||
2753 | if (type == TYPE_DOUBLE) | ||
2754 | # endif | ||
2755 | if (dp->conversion == 'f' || dp->conversion == 'F') | ||
2756 | { | ||
2757 | double arg = a.arg[dp->arg_index].a.a_double; | ||
2758 | if (!(isnan (arg) || arg + arg == arg)) | ||
2759 | { | ||
2760 | /* arg is finite and nonzero. */ | ||
2761 | int exponent = floorlog10 (arg < 0 ? -arg : arg); | ||
2762 | if (exponent >= 0 | ||
2763 | && tmp_length < exponent + precision) | ||
2764 | tmp_length = exponent + precision; | ||
2765 | } | ||
2766 | } | ||
2767 | # endif | ||
2768 | /* Account for sign, decimal point etc. */ | ||
2769 | tmp_length = xsum (tmp_length, 12); | ||
2770 | |||
2771 | if (tmp_length < width) | ||
2772 | tmp_length = width; | ||
2773 | |||
2774 | tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */ | ||
2775 | |||
2776 | if (tmp_length <= sizeof (tmpbuf) / sizeof (DCHAR_T)) | ||
2777 | tmp = tmpbuf; | ||
2778 | else | ||
2779 | { | ||
2780 | size_t tmp_memsize = | ||
2781 | xtimes (tmp_length, sizeof (DCHAR_T)); | ||
2782 | |||
2783 | if (size_overflow_p (tmp_memsize)) | ||
2784 | /* Overflow, would lead to out of memory. */ | ||
2785 | goto out_of_memory; | ||
2786 | tmp = (DCHAR_T *) malloc (tmp_memsize); | ||
2787 | if (tmp == NULL) | ||
2788 | /* Out of memory. */ | ||
2789 | goto out_of_memory; | ||
2790 | } | ||
2791 | |||
2792 | pad_ptr = NULL; | ||
2793 | p = tmp; | ||
2794 | |||
2795 | # if NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE | ||
2796 | # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE | ||
2797 | if (type == TYPE_LONGDOUBLE) | ||
2798 | # endif | ||
2799 | { | ||
2800 | long double arg = a.arg[dp->arg_index].a.a_longdouble; | ||
2801 | |||
2802 | if (isnanl (arg)) | ||
2803 | { | ||
2804 | if (dp->conversion >= 'A' && dp->conversion <= 'Z') | ||
2805 | { | ||
2806 | *p++ = 'N'; | ||
2807 | *p++ = 'A'; | ||
2808 | *p++ = 'N'; | ||
2809 | } | ||
2810 | else | ||
2811 | { | ||
2812 | *p++ = 'n'; | ||
2813 | *p++ = 'a'; | ||
2814 | *p++ = 'n'; | ||
2815 | } | ||
2816 | } | ||
2817 | else | ||
2818 | { | ||
2819 | int sign = 0; | ||
2820 | DECL_LONG_DOUBLE_ROUNDING | ||
2821 | BEGIN_LONG_DOUBLE_ROUNDING (); | ||
2822 | |||
2823 | if (signbit (arg)) /* arg < 0.0L or negative zero */ | ||
2824 | { | ||
2825 | sign = -1; | ||
2826 | arg = -arg; | ||
2827 | } | ||
2828 | |||
2829 | if (sign < 0) | ||
2830 | *p++ = '-'; | ||
2831 | else if (flags & FLAG_SHOWSIGN) | ||
2832 | *p++ = '+'; | ||
2833 | else if (flags & FLAG_SPACE) | ||
2834 | *p++ = ' '; | ||
2835 | |||
2836 | if (arg > 0.0L && arg + arg == arg) | ||
2837 | { | ||
2838 | if (dp->conversion >= 'A' | ||
2839 | && dp->conversion <= 'Z') | ||
2840 | { | ||
2841 | *p++ = 'I'; | ||
2842 | *p++ = 'N'; | ||
2843 | *p++ = 'F'; | ||
2844 | } | ||
2845 | else | ||
2846 | { | ||
2847 | *p++ = 'i'; | ||
2848 | *p++ = 'n'; | ||
2849 | *p++ = 'f'; | ||
2850 | } | ||
2851 | } | ||
2852 | else | ||
2853 | { | ||
2854 | # if NEED_PRINTF_LONG_DOUBLE | ||
2855 | pad_ptr = p; | ||
2856 | |||
2857 | if (dp->conversion == 'f' | ||
2858 | || dp->conversion == 'F') | ||
2859 | { | ||
2860 | char *digits; | ||
2861 | size_t ndigits; | ||
2862 | |||
2863 | digits = | ||
2864 | scale10_round_decimal_long_double (arg, | ||
2865 | precision); | ||
2866 | if (digits == NULL) | ||
2867 | { | ||
2868 | END_LONG_DOUBLE_ROUNDING (); | ||
2869 | goto out_of_memory; | ||
2870 | } | ||
2871 | ndigits = strlen (digits); | ||
2872 | |||
2873 | if (ndigits > precision) | ||
2874 | do | ||
2875 | { | ||
2876 | --ndigits; | ||
2877 | *p++ = digits[ndigits]; | ||
2878 | } | ||
2879 | while (ndigits > precision); | ||
2880 | else | ||
2881 | *p++ = '0'; | ||
2882 | /* Here ndigits <= precision. */ | ||
2883 | if ((flags & FLAG_ALT) || precision > 0) | ||
2884 | { | ||
2885 | *p++ = decimal_point_char (); | ||
2886 | for (; precision > ndigits; precision--) | ||
2887 | *p++ = '0'; | ||
2888 | while (ndigits > 0) | ||
2889 | { | ||
2890 | --ndigits; | ||
2891 | *p++ = digits[ndigits]; | ||
2892 | } | ||
2893 | } | ||
2894 | |||
2895 | free (digits); | ||
2896 | } | ||
2897 | else if (dp->conversion == 'e' | ||
2898 | || dp->conversion == 'E') | ||
2899 | { | ||
2900 | int exponent; | ||
2901 | |||
2902 | if (arg == 0.0L) | ||
2903 | { | ||
2904 | exponent = 0; | ||
2905 | *p++ = '0'; | ||
2906 | if ((flags & FLAG_ALT) || precision > 0) | ||
2907 | { | ||
2908 | *p++ = decimal_point_char (); | ||
2909 | for (; precision > 0; precision--) | ||
2910 | *p++ = '0'; | ||
2911 | } | ||
2912 | } | ||
2913 | else | ||
2914 | { | ||
2915 | /* arg > 0.0L. */ | ||
2916 | int adjusted; | ||
2917 | char *digits; | ||
2918 | size_t ndigits; | ||
2919 | |||
2920 | exponent = floorlog10l (arg); | ||
2921 | adjusted = 0; | ||
2922 | for (;;) | ||
2923 | { | ||
2924 | digits = | ||
2925 | scale10_round_decimal_long_double | ||
2926 | (arg, (int) precision - exponent); | ||
2927 | if (digits == NULL) | ||
2928 | { | ||
2929 | END_LONG_DOUBLE_ROUNDING (); | ||
2930 | goto out_of_memory; | ||
2931 | } | ||
2932 | ndigits = strlen (digits); | ||
2933 | |||
2934 | if (ndigits == precision + 1) | ||
2935 | break; | ||
2936 | if (ndigits < precision | ||
2937 | || ndigits > precision + 2) | ||
2938 | /* The exponent was not guessed | ||
2939 | precisely enough. */ | ||
2940 | abort (); | ||
2941 | if (adjusted) | ||
2942 | /* None of two values of exponent is | ||
2943 | the right one. Prevent an endless | ||
2944 | loop. */ | ||
2945 | abort (); | ||
2946 | free (digits); | ||
2947 | if (ndigits == precision) | ||
2948 | exponent -= 1; | ||
2949 | else | ||
2950 | exponent += 1; | ||
2951 | adjusted = 1; | ||
2952 | } | ||
2953 | |||
2954 | /* Here ndigits = precision+1. */ | ||
2955 | *p++ = digits[--ndigits]; | ||
2956 | if ((flags & FLAG_ALT) || precision > 0) | ||
2957 | { | ||
2958 | *p++ = decimal_point_char (); | ||
2959 | while (ndigits > 0) | ||
2960 | { | ||
2961 | --ndigits; | ||
2962 | *p++ = digits[ndigits]; | ||
2963 | } | ||
2964 | } | ||
2965 | |||
2966 | free (digits); | ||
2967 | } | ||
2968 | |||
2969 | *p++ = dp->conversion; /* 'e' or 'E' */ | ||
2970 | # if WIDE_CHAR_VERSION | ||
2971 | { | ||
2972 | static const wchar_t decimal_format[] = | ||
2973 | { '%', '+', '.', '2', 'd', '\0' }; | ||
2974 | SNPRINTF (p, 6 + 1, decimal_format, | ||
2975 | exponent); | ||
2976 | } | ||
2977 | while (*p != '\0') | ||
2978 | p++; | ||
2979 | # else | ||
2980 | if (sizeof (DCHAR_T) == 1) | ||
2981 | { | ||
2982 | sprintf ((char *) p, "%+.2d", exponent); | ||
2983 | while (*p != '\0') | ||
2984 | p++; | ||
2985 | } | ||
2986 | else | ||
2987 | { | ||
2988 | char expbuf[6 + 1]; | ||
2989 | const char *ep; | ||
2990 | sprintf (expbuf, "%+.2d", exponent); | ||
2991 | for (ep = expbuf; (*p = *ep) != '\0'; | ||
2992 | ep++) | ||
2993 | p++; | ||
2994 | } | ||
2995 | # endif | ||
2996 | } | ||
2997 | else if (dp->conversion == 'g' | ||
2998 | || dp->conversion == 'G') | ||
2999 | { | ||
3000 | if (precision == 0) | ||
3001 | precision = 1; | ||
3002 | /* precision >= 1. */ | ||
3003 | |||
3004 | if (arg == 0.0L) | ||
3005 | /* The exponent is 0, >= -4, < precision. | ||
3006 | Use fixed-point notation. */ | ||
3007 | { | ||
3008 | size_t ndigits = precision; | ||
3009 | /* Number of trailing zeroes that have to be | ||
3010 | dropped. */ | ||
3011 | size_t nzeroes = | ||
3012 | (flags & FLAG_ALT ? 0 : precision - 1); | ||
3013 | |||
3014 | --ndigits; | ||
3015 | *p++ = '0'; | ||
3016 | if ((flags & FLAG_ALT) | ||
3017 | || ndigits > nzeroes) | ||
3018 | { | ||
3019 | *p++ = decimal_point_char (); | ||
3020 | while (ndigits > nzeroes) | ||
3021 | { | ||
3022 | --ndigits; | ||
3023 | *p++ = '0'; | ||
3024 | } | ||
3025 | } | ||
3026 | } | ||
3027 | else | ||
3028 | { | ||
3029 | /* arg > 0.0L. */ | ||
3030 | int exponent; | ||
3031 | int adjusted; | ||
3032 | char *digits; | ||
3033 | size_t ndigits; | ||
3034 | size_t nzeroes; | ||
3035 | |||
3036 | exponent = floorlog10l (arg); | ||
3037 | adjusted = 0; | ||
3038 | for (;;) | ||
3039 | { | ||
3040 | digits = | ||
3041 | scale10_round_decimal_long_double | ||
3042 | (arg, | ||
3043 | (int) (precision - 1) - exponent); | ||
3044 | if (digits == NULL) | ||
3045 | { | ||
3046 | END_LONG_DOUBLE_ROUNDING (); | ||
3047 | goto out_of_memory; | ||
3048 | } | ||
3049 | ndigits = strlen (digits); | ||
3050 | |||
3051 | if (ndigits == precision) | ||
3052 | break; | ||
3053 | if (ndigits < precision - 1 | ||
3054 | || ndigits > precision + 1) | ||
3055 | /* The exponent was not guessed | ||
3056 | precisely enough. */ | ||
3057 | abort (); | ||
3058 | if (adjusted) | ||
3059 | /* None of two values of exponent is | ||
3060 | the right one. Prevent an endless | ||
3061 | loop. */ | ||
3062 | abort (); | ||
3063 | free (digits); | ||
3064 | if (ndigits < precision) | ||
3065 | exponent -= 1; | ||
3066 | else | ||
3067 | exponent += 1; | ||
3068 | adjusted = 1; | ||
3069 | } | ||
3070 | /* Here ndigits = precision. */ | ||
3071 | |||
3072 | /* Determine the number of trailing zeroes | ||
3073 | that have to be dropped. */ | ||
3074 | nzeroes = 0; | ||
3075 | if ((flags & FLAG_ALT) == 0) | ||
3076 | while (nzeroes < ndigits | ||
3077 | && digits[nzeroes] == '0') | ||
3078 | nzeroes++; | ||
3079 | |||
3080 | /* The exponent is now determined. */ | ||
3081 | if (exponent >= -4 | ||
3082 | && exponent < (long) precision) | ||
3083 | { | ||
3084 | /* Fixed-point notation: | ||
3085 | max(exponent,0)+1 digits, then the | ||
3086 | decimal point, then the remaining | ||
3087 | digits without trailing zeroes. */ | ||
3088 | if (exponent >= 0) | ||
3089 | { | ||
3090 | size_t count = exponent + 1; | ||
3091 | /* Note: count <= precision = ndigits. */ | ||
3092 | for (; count > 0; count--) | ||
3093 | *p++ = digits[--ndigits]; | ||
3094 | if ((flags & FLAG_ALT) | ||
3095 | || ndigits > nzeroes) | ||
3096 | { | ||
3097 | *p++ = decimal_point_char (); | ||
3098 | while (ndigits > nzeroes) | ||
3099 | { | ||
3100 | --ndigits; | ||
3101 | *p++ = digits[ndigits]; | ||
3102 | } | ||
3103 | } | ||
3104 | } | ||
3105 | else | ||
3106 | { | ||
3107 | size_t count = -exponent - 1; | ||
3108 | *p++ = '0'; | ||
3109 | *p++ = decimal_point_char (); | ||
3110 | for (; count > 0; count--) | ||
3111 | *p++ = '0'; | ||
3112 | while (ndigits > nzeroes) | ||
3113 | { | ||
3114 | --ndigits; | ||
3115 | *p++ = digits[ndigits]; | ||
3116 | } | ||
3117 | } | ||
3118 | } | ||
3119 | else | ||
3120 | { | ||
3121 | /* Exponential notation. */ | ||
3122 | *p++ = digits[--ndigits]; | ||
3123 | if ((flags & FLAG_ALT) | ||
3124 | || ndigits > nzeroes) | ||
3125 | { | ||
3126 | *p++ = decimal_point_char (); | ||
3127 | while (ndigits > nzeroes) | ||
3128 | { | ||
3129 | --ndigits; | ||
3130 | *p++ = digits[ndigits]; | ||
3131 | } | ||
3132 | } | ||
3133 | *p++ = dp->conversion - 'G' + 'E'; /* 'e' or 'E' */ | ||
3134 | # if WIDE_CHAR_VERSION | ||
3135 | { | ||
3136 | static const wchar_t | ||
3137 | decimal_format[] = | ||
3138 | { '%', '+', '.', '2', 'd', '\0' }; | ||
3139 | SNPRINTF (p, 6 + 1, decimal_format, | ||
3140 | exponent); | ||
3141 | } | ||
3142 | while (*p != '\0') | ||
3143 | p++; | ||
3144 | # else | ||
3145 | if (sizeof (DCHAR_T) == 1) | ||
3146 | { | ||
3147 | sprintf ((char *) p, "%+.2d", | ||
3148 | exponent); | ||
3149 | while (*p != '\0') | ||
3150 | p++; | ||
3151 | } | ||
3152 | else | ||
3153 | { | ||
3154 | char expbuf[6 + 1]; | ||
3155 | const char *ep; | ||
3156 | sprintf (expbuf, "%+.2d", | ||
3157 | exponent); | ||
3158 | for (ep = expbuf; | ||
3159 | (*p = *ep) != '\0'; ep++) | ||
3160 | p++; | ||
3161 | } | ||
3162 | # endif | ||
3163 | } | ||
3164 | |||
3165 | free (digits); | ||
3166 | } | ||
3167 | } | ||
3168 | else | ||
3169 | abort (); | ||
3170 | # else | ||
3171 | /* arg is finite. */ | ||
3172 | abort (); | ||
3173 | # endif | ||
3174 | } | ||
3175 | |||
3176 | END_LONG_DOUBLE_ROUNDING (); | ||
3177 | } | ||
3178 | } | ||
3179 | # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE | ||
3180 | else | ||
3181 | # endif | ||
3182 | # endif | ||
3183 | # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE | ||
3184 | { | ||
3185 | double arg = a.arg[dp->arg_index].a.a_double; | ||
3186 | |||
3187 | if (isnan (arg)) | ||
3188 | { | ||
3189 | if (dp->conversion >= 'A' && dp->conversion <= 'Z') | ||
3190 | { | ||
3191 | *p++ = 'N'; | ||
3192 | *p++ = 'A'; | ||
3193 | *p++ = 'N'; | ||
3194 | } | ||
3195 | else | ||
3196 | { | ||
3197 | *p++ = 'n'; | ||
3198 | *p++ = 'a'; | ||
3199 | *p++ = 'n'; | ||
3200 | } | ||
3201 | } | ||
3202 | else | ||
3203 | { | ||
3204 | int sign = 0; | ||
3205 | |||
3206 | if (signbit (arg)) /* arg < 0.0 or negative zero */ | ||
3207 | { | ||
3208 | sign = -1; | ||
3209 | arg = -arg; | ||
3210 | } | ||
3211 | |||
3212 | if (sign < 0) | ||
3213 | *p++ = '-'; | ||
3214 | else if (flags & FLAG_SHOWSIGN) | ||
3215 | *p++ = '+'; | ||
3216 | else if (flags & FLAG_SPACE) | ||
3217 | *p++ = ' '; | ||
3218 | |||
3219 | if (arg > 0.0 && arg + arg == arg) | ||
3220 | { | ||
3221 | if (dp->conversion >= 'A' | ||
3222 | && dp->conversion <= 'Z') | ||
3223 | { | ||
3224 | *p++ = 'I'; | ||
3225 | *p++ = 'N'; | ||
3226 | *p++ = 'F'; | ||
3227 | } | ||
3228 | else | ||
3229 | { | ||
3230 | *p++ = 'i'; | ||
3231 | *p++ = 'n'; | ||
3232 | *p++ = 'f'; | ||
3233 | } | ||
3234 | } | ||
3235 | else | ||
3236 | { | ||
3237 | # if NEED_PRINTF_DOUBLE | ||
3238 | pad_ptr = p; | ||
3239 | |||
3240 | if (dp->conversion == 'f' | ||
3241 | || dp->conversion == 'F') | ||
3242 | { | ||
3243 | char *digits; | ||
3244 | size_t ndigits; | ||
3245 | |||
3246 | digits = | ||
3247 | scale10_round_decimal_double (arg, | ||
3248 | precision); | ||
3249 | if (digits == NULL) | ||
3250 | goto out_of_memory; | ||
3251 | ndigits = strlen (digits); | ||
3252 | |||
3253 | if (ndigits > precision) | ||
3254 | do | ||
3255 | { | ||
3256 | --ndigits; | ||
3257 | *p++ = digits[ndigits]; | ||
3258 | } | ||
3259 | while (ndigits > precision); | ||
3260 | else | ||
3261 | *p++ = '0'; | ||
3262 | /* Here ndigits <= precision. */ | ||
3263 | if ((flags & FLAG_ALT) || precision > 0) | ||
3264 | { | ||
3265 | *p++ = decimal_point_char (); | ||
3266 | for (; precision > ndigits; precision--) | ||
3267 | *p++ = '0'; | ||
3268 | while (ndigits > 0) | ||
3269 | { | ||
3270 | --ndigits; | ||
3271 | *p++ = digits[ndigits]; | ||
3272 | } | ||
3273 | } | ||
3274 | |||
3275 | free (digits); | ||
3276 | } | ||
3277 | else if (dp->conversion == 'e' | ||
3278 | || dp->conversion == 'E') | ||
3279 | { | ||
3280 | int exponent; | ||
3281 | |||
3282 | if (arg == 0.0) | ||
3283 | { | ||
3284 | exponent = 0; | ||
3285 | *p++ = '0'; | ||
3286 | if ((flags & FLAG_ALT) || precision > 0) | ||
3287 | { | ||
3288 | *p++ = decimal_point_char (); | ||
3289 | for (; precision > 0; precision--) | ||
3290 | *p++ = '0'; | ||
3291 | } | ||
3292 | } | ||
3293 | else | ||
3294 | { | ||
3295 | /* arg > 0.0. */ | ||
3296 | int adjusted; | ||
3297 | char *digits; | ||
3298 | size_t ndigits; | ||
3299 | |||
3300 | exponent = floorlog10 (arg); | ||
3301 | adjusted = 0; | ||
3302 | for (;;) | ||
3303 | { | ||
3304 | digits = | ||
3305 | scale10_round_decimal_double (arg, | ||
3306 | (int) | ||
3307 | precision | ||
3308 | - | ||
3309 | exponent); | ||
3310 | if (digits == NULL) | ||
3311 | goto out_of_memory; | ||
3312 | ndigits = strlen (digits); | ||
3313 | |||
3314 | if (ndigits == precision + 1) | ||
3315 | break; | ||
3316 | if (ndigits < precision | ||
3317 | || ndigits > precision + 2) | ||
3318 | /* The exponent was not guessed | ||
3319 | precisely enough. */ | ||
3320 | abort (); | ||
3321 | if (adjusted) | ||
3322 | /* None of two values of exponent is | ||
3323 | the right one. Prevent an endless | ||
3324 | loop. */ | ||
3325 | abort (); | ||
3326 | free (digits); | ||
3327 | if (ndigits == precision) | ||
3328 | exponent -= 1; | ||
3329 | else | ||
3330 | exponent += 1; | ||
3331 | adjusted = 1; | ||
3332 | } | ||
3333 | |||
3334 | /* Here ndigits = precision+1. */ | ||
3335 | *p++ = digits[--ndigits]; | ||
3336 | if ((flags & FLAG_ALT) || precision > 0) | ||
3337 | { | ||
3338 | *p++ = decimal_point_char (); | ||
3339 | while (ndigits > 0) | ||
3340 | { | ||
3341 | --ndigits; | ||
3342 | *p++ = digits[ndigits]; | ||
3343 | } | ||
3344 | } | ||
3345 | |||
3346 | free (digits); | ||
3347 | } | ||
3348 | |||
3349 | *p++ = dp->conversion; /* 'e' or 'E' */ | ||
3350 | # if WIDE_CHAR_VERSION | ||
3351 | { | ||
3352 | static const wchar_t decimal_format[] = | ||
3353 | /* Produce the same number of exponent digits | ||
3354 | as the native printf implementation. */ | ||
3355 | # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ | ||
3356 | { '%', '+', '.', '3', 'd', '\0' }; | ||
3357 | # else | ||
3358 | { '%', '+', '.', '2', 'd', '\0' }; | ||
3359 | # endif | ||
3360 | SNPRINTF (p, 6 + 1, decimal_format, | ||
3361 | exponent); | ||
3362 | } | ||
3363 | while (*p != '\0') | ||
3364 | p++; | ||
3365 | # else | ||
3366 | { | ||
3367 | static const char decimal_format[] = | ||
3368 | /* Produce the same number of exponent digits | ||
3369 | as the native printf implementation. */ | ||
3370 | # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ | ||
3371 | "%+.3d"; | ||
3372 | # else | ||
3373 | "%+.2d"; | ||
3374 | # endif | ||
3375 | if (sizeof (DCHAR_T) == 1) | ||
3376 | { | ||
3377 | sprintf ((char *) p, decimal_format, | ||
3378 | exponent); | ||
3379 | while (*p != '\0') | ||
3380 | p++; | ||
3381 | } | ||
3382 | else | ||
3383 | { | ||
3384 | char expbuf[6 + 1]; | ||
3385 | const char *ep; | ||
3386 | sprintf (expbuf, decimal_format, | ||
3387 | exponent); | ||
3388 | for (ep = expbuf; (*p = *ep) != '\0'; | ||
3389 | ep++) | ||
3390 | p++; | ||
3391 | } | ||
3392 | } | ||
3393 | # endif | ||
3394 | } | ||
3395 | else if (dp->conversion == 'g' | ||
3396 | || dp->conversion == 'G') | ||
3397 | { | ||
3398 | if (precision == 0) | ||
3399 | precision = 1; | ||
3400 | /* precision >= 1. */ | ||
3401 | |||
3402 | if (arg == 0.0) | ||
3403 | /* The exponent is 0, >= -4, < precision. | ||
3404 | Use fixed-point notation. */ | ||
3405 | { | ||
3406 | size_t ndigits = precision; | ||
3407 | /* Number of trailing zeroes that have to be | ||
3408 | dropped. */ | ||
3409 | size_t nzeroes = | ||
3410 | (flags & FLAG_ALT ? 0 : precision - 1); | ||
3411 | |||
3412 | --ndigits; | ||
3413 | *p++ = '0'; | ||
3414 | if ((flags & FLAG_ALT) | ||
3415 | || ndigits > nzeroes) | ||
3416 | { | ||
3417 | *p++ = decimal_point_char (); | ||
3418 | while (ndigits > nzeroes) | ||
3419 | { | ||
3420 | --ndigits; | ||
3421 | *p++ = '0'; | ||
3422 | } | ||
3423 | } | ||
3424 | } | ||
3425 | else | ||
3426 | { | ||
3427 | /* arg > 0.0. */ | ||
3428 | int exponent; | ||
3429 | int adjusted; | ||
3430 | char *digits; | ||
3431 | size_t ndigits; | ||
3432 | size_t nzeroes; | ||
3433 | |||
3434 | exponent = floorlog10 (arg); | ||
3435 | adjusted = 0; | ||
3436 | for (;;) | ||
3437 | { | ||
3438 | digits = | ||
3439 | scale10_round_decimal_double (arg, | ||
3440 | (int) | ||
3441 | (precision | ||
3442 | - | ||
3443 | 1) - | ||
3444 | exponent); | ||
3445 | if (digits == NULL) | ||
3446 | goto out_of_memory; | ||
3447 | ndigits = strlen (digits); | ||
3448 | |||
3449 | if (ndigits == precision) | ||
3450 | break; | ||
3451 | if (ndigits < precision - 1 | ||
3452 | || ndigits > precision + 1) | ||
3453 | /* The exponent was not guessed | ||
3454 | precisely enough. */ | ||
3455 | abort (); | ||
3456 | if (adjusted) | ||
3457 | /* None of two values of exponent is | ||
3458 | the right one. Prevent an endless | ||
3459 | loop. */ | ||
3460 | abort (); | ||
3461 | free (digits); | ||
3462 | if (ndigits < precision) | ||
3463 | exponent -= 1; | ||
3464 | else | ||
3465 | exponent += 1; | ||
3466 | adjusted = 1; | ||
3467 | } | ||
3468 | /* Here ndigits = precision. */ | ||
3469 | |||
3470 | /* Determine the number of trailing zeroes | ||
3471 | that have to be dropped. */ | ||
3472 | nzeroes = 0; | ||
3473 | if ((flags & FLAG_ALT) == 0) | ||
3474 | while (nzeroes < ndigits | ||
3475 | && digits[nzeroes] == '0') | ||
3476 | nzeroes++; | ||
3477 | |||
3478 | /* The exponent is now determined. */ | ||
3479 | if (exponent >= -4 | ||
3480 | && exponent < (long) precision) | ||
3481 | { | ||
3482 | /* Fixed-point notation: | ||
3483 | max(exponent,0)+1 digits, then the | ||
3484 | decimal point, then the remaining | ||
3485 | digits without trailing zeroes. */ | ||
3486 | if (exponent >= 0) | ||
3487 | { | ||
3488 | size_t count = exponent + 1; | ||
3489 | /* Note: count <= precision = ndigits. */ | ||
3490 | for (; count > 0; count--) | ||
3491 | *p++ = digits[--ndigits]; | ||
3492 | if ((flags & FLAG_ALT) | ||
3493 | || ndigits > nzeroes) | ||
3494 | { | ||
3495 | *p++ = decimal_point_char (); | ||
3496 | while (ndigits > nzeroes) | ||
3497 | { | ||
3498 | --ndigits; | ||
3499 | *p++ = digits[ndigits]; | ||
3500 | } | ||
3501 | } | ||
3502 | } | ||
3503 | else | ||
3504 | { | ||
3505 | size_t count = -exponent - 1; | ||
3506 | *p++ = '0'; | ||
3507 | *p++ = decimal_point_char (); | ||
3508 | for (; count > 0; count--) | ||
3509 | *p++ = '0'; | ||
3510 | while (ndigits > nzeroes) | ||
3511 | { | ||
3512 | --ndigits; | ||
3513 | *p++ = digits[ndigits]; | ||
3514 | } | ||
3515 | } | ||
3516 | } | ||
3517 | else | ||
3518 | { | ||
3519 | /* Exponential notation. */ | ||
3520 | *p++ = digits[--ndigits]; | ||
3521 | if ((flags & FLAG_ALT) | ||
3522 | || ndigits > nzeroes) | ||
3523 | { | ||
3524 | *p++ = decimal_point_char (); | ||
3525 | while (ndigits > nzeroes) | ||
3526 | { | ||
3527 | --ndigits; | ||
3528 | *p++ = digits[ndigits]; | ||
3529 | } | ||
3530 | } | ||
3531 | *p++ = dp->conversion - 'G' + 'E'; /* 'e' or 'E' */ | ||
3532 | # if WIDE_CHAR_VERSION | ||
3533 | { | ||
3534 | static const wchar_t | ||
3535 | decimal_format[] = | ||
3536 | /* Produce the same number of exponent digits | ||
3537 | as the native printf implementation. */ | ||
3538 | # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ | ||
3539 | { '%', '+', '.', '3', 'd', '\0' }; | ||
3540 | # else | ||
3541 | { '%', '+', '.', '2', 'd', '\0' }; | ||
3542 | # endif | ||
3543 | SNPRINTF (p, 6 + 1, decimal_format, | ||
3544 | exponent); | ||
3545 | } | ||
3546 | while (*p != '\0') | ||
3547 | p++; | ||
3548 | # else | ||
3549 | { | ||
3550 | static const char decimal_format[] = | ||
3551 | /* Produce the same number of exponent digits | ||
3552 | as the native printf implementation. */ | ||
3553 | # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ | ||
3554 | "%+.3d"; | ||
3555 | # else | ||
3556 | "%+.2d"; | ||
3557 | # endif | ||
3558 | if (sizeof (DCHAR_T) == 1) | ||
3559 | { | ||
3560 | sprintf ((char *) p, | ||
3561 | decimal_format, | ||
3562 | exponent); | ||
3563 | while (*p != '\0') | ||
3564 | p++; | ||
3565 | } | ||
3566 | else | ||
3567 | { | ||
3568 | char expbuf[6 + 1]; | ||
3569 | const char *ep; | ||
3570 | sprintf (expbuf, decimal_format, | ||
3571 | exponent); | ||
3572 | for (ep = expbuf; | ||
3573 | (*p = *ep) != '\0'; ep++) | ||
3574 | p++; | ||
3575 | } | ||
3576 | } | ||
3577 | # endif | ||
3578 | } | ||
3579 | |||
3580 | free (digits); | ||
3581 | } | ||
3582 | } | ||
3583 | else | ||
3584 | abort (); | ||
3585 | # else | ||
3586 | /* arg is finite. */ | ||
3587 | if (!(arg == 0.0)) | ||
3588 | abort (); | ||
3589 | |||
3590 | pad_ptr = p; | ||
3591 | |||
3592 | if (dp->conversion == 'f' | ||
3593 | || dp->conversion == 'F') | ||
3594 | { | ||
3595 | *p++ = '0'; | ||
3596 | if ((flags & FLAG_ALT) || precision > 0) | ||
3597 | { | ||
3598 | *p++ = decimal_point_char (); | ||
3599 | for (; precision > 0; precision--) | ||
3600 | *p++ = '0'; | ||
3601 | } | ||
3602 | } | ||
3603 | else if (dp->conversion == 'e' | ||
3604 | || dp->conversion == 'E') | ||
3605 | { | ||
3606 | *p++ = '0'; | ||
3607 | if ((flags & FLAG_ALT) || precision > 0) | ||
3608 | { | ||
3609 | *p++ = decimal_point_char (); | ||
3610 | for (; precision > 0; precision--) | ||
3611 | *p++ = '0'; | ||
3612 | } | ||
3613 | *p++ = dp->conversion; /* 'e' or 'E' */ | ||
3614 | *p++ = '+'; | ||
3615 | /* Produce the same number of exponent digits as | ||
3616 | the native printf implementation. */ | ||
3617 | # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ | ||
3618 | *p++ = '0'; | ||
3619 | # endif | ||
3620 | *p++ = '0'; | ||
3621 | *p++ = '0'; | ||
3622 | } | ||
3623 | else if (dp->conversion == 'g' | ||
3624 | || dp->conversion == 'G') | ||
3625 | { | ||
3626 | *p++ = '0'; | ||
3627 | if (flags & FLAG_ALT) | ||
3628 | { | ||
3629 | size_t ndigits = | ||
3630 | (precision > 0 ? precision - 1 : 0); | ||
3631 | *p++ = decimal_point_char (); | ||
3632 | for (; ndigits > 0; --ndigits) | ||
3633 | *p++ = '0'; | ||
3634 | } | ||
3635 | } | ||
3636 | else | ||
3637 | abort (); | ||
3638 | # endif | ||
3639 | } | ||
3640 | } | ||
3641 | } | ||
3642 | # endif | ||
3643 | |||
3644 | /* The generated string now extends from tmp to p, with the | ||
3645 | zero padding insertion point being at pad_ptr. */ | ||
3646 | if (has_width && p - tmp < width) | ||
3647 | { | ||
3648 | size_t pad = width - (p - tmp); | ||
3649 | DCHAR_T *end = p + pad; | ||
3650 | |||
3651 | if (flags & FLAG_LEFT) | ||
3652 | { | ||
3653 | /* Pad with spaces on the right. */ | ||
3654 | for (; pad > 0; pad--) | ||
3655 | *p++ = ' '; | ||
3656 | } | ||
3657 | else if ((flags & FLAG_ZERO) && pad_ptr != NULL) | ||
3658 | { | ||
3659 | /* Pad with zeroes. */ | ||
3660 | DCHAR_T *q = end; | ||
3661 | |||
3662 | while (p > pad_ptr) | ||
3663 | *--q = *--p; | ||
3664 | for (; pad > 0; pad--) | ||
3665 | *p++ = '0'; | ||
3666 | } | ||
3667 | else | ||
3668 | { | ||
3669 | /* Pad with spaces on the left. */ | ||
3670 | DCHAR_T *q = end; | ||
3671 | |||
3672 | while (p > tmp) | ||
3673 | *--q = *--p; | ||
3674 | for (; pad > 0; pad--) | ||
3675 | *p++ = ' '; | ||
3676 | } | ||
3677 | |||
3678 | p = end; | ||
3679 | } | ||
3680 | |||
3681 | { | ||
3682 | size_t count = p - tmp; | ||
3683 | |||
3684 | if (count >= tmp_length) | ||
3685 | /* tmp_length was incorrectly calculated - fix the | ||
3686 | code above! */ | ||
3687 | abort (); | ||
3688 | |||
3689 | /* Make room for the result. */ | ||
3690 | if (count >= allocated - length) | ||
3691 | { | ||
3692 | size_t n = xsum (length, count); | ||
3693 | |||
3694 | ENSURE_ALLOCATION (n); | ||
3695 | } | ||
3696 | |||
3697 | /* Append the result. */ | ||
3698 | memcpy (result + length, tmp, count * sizeof (DCHAR_T)); | ||
3699 | if (tmp != tmpbuf) | ||
3700 | free (tmp); | ||
3701 | length += count; | ||
3702 | } | ||
3703 | } | ||
3704 | #endif | ||
3705 | else | ||
3706 | { | ||
3707 | arg_type type = a.arg[dp->arg_index].type; | ||
3708 | int flags = dp->flags; | ||
3709 | #if !USE_SNPRINTF || !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION | ||
3710 | int has_width; | ||
3711 | size_t width; | ||
3712 | #endif | ||
3713 | #if !USE_SNPRINTF || NEED_PRINTF_UNBOUNDED_PRECISION | ||
3714 | int has_precision; | ||
3715 | size_t precision; | ||
3716 | #endif | ||
3717 | #if NEED_PRINTF_UNBOUNDED_PRECISION | ||
3718 | int prec_ourselves; | ||
3719 | #else | ||
3720 | # define prec_ourselves 0 | ||
3721 | #endif | ||
3722 | #if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION | ||
3723 | int pad_ourselves; | ||
3724 | #else | ||
3725 | # define pad_ourselves 0 | ||
3726 | #endif | ||
3727 | TCHAR_T *fbp; | ||
3728 | unsigned int prefix_count; | ||
3729 | int prefixes[2]; | ||
3730 | #if !USE_SNPRINTF | ||
3731 | size_t tmp_length; | ||
3732 | TCHAR_T tmpbuf[700]; | ||
3733 | TCHAR_T *tmp; | ||
3734 | #endif | ||
3735 | |||
3736 | #if !USE_SNPRINTF || !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION | ||
3737 | has_width = 0; | ||
3738 | width = 0; | ||
3739 | if (dp->width_start != dp->width_end) | ||
3740 | { | ||
3741 | if (dp->width_arg_index != ARG_NONE) | ||
3742 | { | ||
3743 | int arg; | ||
3744 | |||
3745 | if (!(a.arg[dp->width_arg_index].type == TYPE_INT)) | ||
3746 | abort (); | ||
3747 | arg = a.arg[dp->width_arg_index].a.a_int; | ||
3748 | if (arg < 0) | ||
3749 | { | ||
3750 | /* "A negative field width is taken as a '-' flag | ||
3751 | followed by a positive field width." */ | ||
3752 | flags |= FLAG_LEFT; | ||
3753 | width = (unsigned int) (-arg); | ||
3754 | } | ||
3755 | else | ||
3756 | width = arg; | ||
3757 | } | ||
3758 | else | ||
3759 | { | ||
3760 | const FCHAR_T *digitp = dp->width_start; | ||
3761 | |||
3762 | do | ||
3763 | width = xsum (xtimes (width, 10), *digitp++ - '0'); | ||
3764 | while (digitp != dp->width_end); | ||
3765 | } | ||
3766 | has_width = 1; | ||
3767 | } | ||
3768 | #endif | ||
3769 | |||
3770 | #if !USE_SNPRINTF || NEED_PRINTF_UNBOUNDED_PRECISION | ||
3771 | has_precision = 0; | ||
3772 | precision = 6; | ||
3773 | if (dp->precision_start != dp->precision_end) | ||
3774 | { | ||
3775 | if (dp->precision_arg_index != ARG_NONE) | ||
3776 | { | ||
3777 | int arg; | ||
3778 | |||
3779 | if (! | ||
3780 | (a.arg[dp->precision_arg_index].type == TYPE_INT)) | ||
3781 | abort (); | ||
3782 | arg = a.arg[dp->precision_arg_index].a.a_int; | ||
3783 | /* "A negative precision is taken as if the precision | ||
3784 | were omitted." */ | ||
3785 | if (arg >= 0) | ||
3786 | { | ||
3787 | precision = arg; | ||
3788 | has_precision = 1; | ||
3789 | } | ||
3790 | } | ||
3791 | else | ||
3792 | { | ||
3793 | const FCHAR_T *digitp = dp->precision_start + 1; | ||
3794 | |||
3795 | precision = 0; | ||
3796 | while (digitp != dp->precision_end) | ||
3797 | precision = | ||
3798 | xsum (xtimes (precision, 10), *digitp++ - '0'); | ||
3799 | has_precision = 1; | ||
3800 | } | ||
3801 | } | ||
3802 | #endif | ||
3803 | |||
3804 | #if !USE_SNPRINTF | ||
3805 | /* Allocate a temporary buffer of sufficient size for calling | ||
3806 | sprintf. */ | ||
3807 | { | ||
3808 | switch (dp->conversion) | ||
3809 | { | ||
3810 | |||
3811 | case 'd': | ||
3812 | case 'i': | ||
3813 | case 'u': | ||
3814 | # if HAVE_LONG_LONG_INT | ||
3815 | if (type == TYPE_LONGLONGINT | ||
3816 | || type == TYPE_ULONGLONGINT) | ||
3817 | tmp_length = (unsigned int) (sizeof (unsigned long long) * CHAR_BIT * 0.30103 /* binary -> decimal */ | ||
3818 | ) + 1; /* turn floor into ceil */ | ||
3819 | else | ||
3820 | # endif | ||
3821 | if (type == TYPE_LONGINT || type == TYPE_ULONGINT) | ||
3822 | tmp_length = (unsigned int) (sizeof (unsigned long) * CHAR_BIT * 0.30103 /* binary -> decimal */ | ||
3823 | ) + 1; /* turn floor into ceil */ | ||
3824 | else | ||
3825 | tmp_length = (unsigned int) (sizeof (unsigned int) * CHAR_BIT * 0.30103 /* binary -> decimal */ | ||
3826 | ) + 1; /* turn floor into ceil */ | ||
3827 | if (tmp_length < precision) | ||
3828 | tmp_length = precision; | ||
3829 | /* Multiply by 2, as an estimate for FLAG_GROUP. */ | ||
3830 | tmp_length = xsum (tmp_length, tmp_length); | ||
3831 | /* Add 1, to account for a leading sign. */ | ||
3832 | tmp_length = xsum (tmp_length, 1); | ||
3833 | break; | ||
3834 | |||
3835 | case 'o': | ||
3836 | # if HAVE_LONG_LONG_INT | ||
3837 | if (type == TYPE_LONGLONGINT | ||
3838 | || type == TYPE_ULONGLONGINT) | ||
3839 | tmp_length = (unsigned int) (sizeof (unsigned long long) * CHAR_BIT * 0.333334 /* binary -> octal */ | ||
3840 | ) + 1; /* turn floor into ceil */ | ||
3841 | else | ||
3842 | # endif | ||
3843 | if (type == TYPE_LONGINT || type == TYPE_ULONGINT) | ||
3844 | tmp_length = (unsigned int) (sizeof (unsigned long) * CHAR_BIT * 0.333334 /* binary -> octal */ | ||
3845 | ) + 1; /* turn floor into ceil */ | ||
3846 | else | ||
3847 | tmp_length = (unsigned int) (sizeof (unsigned int) * CHAR_BIT * 0.333334 /* binary -> octal */ | ||
3848 | ) + 1; /* turn floor into ceil */ | ||
3849 | if (tmp_length < precision) | ||
3850 | tmp_length = precision; | ||
3851 | /* Add 1, to account for a leading sign. */ | ||
3852 | tmp_length = xsum (tmp_length, 1); | ||
3853 | break; | ||
3854 | |||
3855 | case 'x': | ||
3856 | case 'X': | ||
3857 | # if HAVE_LONG_LONG_INT | ||
3858 | if (type == TYPE_LONGLONGINT | ||
3859 | || type == TYPE_ULONGLONGINT) | ||
3860 | tmp_length = (unsigned int) (sizeof (unsigned long long) * CHAR_BIT * 0.25 /* binary -> hexadecimal */ | ||
3861 | ) + 1; /* turn floor into ceil */ | ||
3862 | else | ||
3863 | # endif | ||
3864 | if (type == TYPE_LONGINT || type == TYPE_ULONGINT) | ||
3865 | tmp_length = (unsigned int) (sizeof (unsigned long) * CHAR_BIT * 0.25 /* binary -> hexadecimal */ | ||
3866 | ) + 1; /* turn floor into ceil */ | ||
3867 | else | ||
3868 | tmp_length = (unsigned int) (sizeof (unsigned int) * CHAR_BIT * 0.25 /* binary -> hexadecimal */ | ||
3869 | ) + 1; /* turn floor into ceil */ | ||
3870 | if (tmp_length < precision) | ||
3871 | tmp_length = precision; | ||
3872 | /* Add 2, to account for a leading sign or alternate form. */ | ||
3873 | tmp_length = xsum (tmp_length, 2); | ||
3874 | break; | ||
3875 | |||
3876 | case 'f': | ||
3877 | case 'F': | ||
3878 | if (type == TYPE_LONGDOUBLE) | ||
3879 | tmp_length = (unsigned int) (LDBL_MAX_EXP * 0.30103 /* binary -> decimal */ | ||
3880 | * 2 /* estimate for FLAG_GROUP */ | ||
3881 | ) + 1 /* turn floor into ceil */ | ||
3882 | + 10; /* sign, decimal point etc. */ | ||
3883 | else | ||
3884 | tmp_length = (unsigned int) (DBL_MAX_EXP * 0.30103 /* binary -> decimal */ | ||
3885 | * 2 /* estimate for FLAG_GROUP */ | ||
3886 | ) + 1 /* turn floor into ceil */ | ||
3887 | + 10; /* sign, decimal point etc. */ | ||
3888 | tmp_length = xsum (tmp_length, precision); | ||
3889 | break; | ||
3890 | |||
3891 | case 'e': | ||
3892 | case 'E': | ||
3893 | case 'g': | ||
3894 | case 'G': | ||
3895 | tmp_length = 12; /* sign, decimal point, exponent etc. */ | ||
3896 | tmp_length = xsum (tmp_length, precision); | ||
3897 | break; | ||
3898 | |||
3899 | case 'a': | ||
3900 | case 'A': | ||
3901 | if (type == TYPE_LONGDOUBLE) | ||
3902 | tmp_length = (unsigned int) (LDBL_DIG * 0.831 /* decimal -> hexadecimal */ | ||
3903 | ) + 1; /* turn floor into ceil */ | ||
3904 | else | ||
3905 | tmp_length = (unsigned int) (DBL_DIG * 0.831 /* decimal -> hexadecimal */ | ||
3906 | ) + 1; /* turn floor into ceil */ | ||
3907 | if (tmp_length < precision) | ||
3908 | tmp_length = precision; | ||
3909 | /* Account for sign, decimal point etc. */ | ||
3910 | tmp_length = xsum (tmp_length, 12); | ||
3911 | break; | ||
3912 | |||
3913 | case 'c': | ||
3914 | # if HAVE_WINT_T && !WIDE_CHAR_VERSION | ||
3915 | if (type == TYPE_WIDE_CHAR) | ||
3916 | tmp_length = MB_CUR_MAX; | ||
3917 | else | ||
3918 | # endif | ||
3919 | tmp_length = 1; | ||
3920 | break; | ||
3921 | |||
3922 | case 's': | ||
3923 | # if HAVE_WCHAR_T | ||
3924 | if (type == TYPE_WIDE_STRING) | ||
3925 | { | ||
3926 | tmp_length = | ||
3927 | local_wcslen (a.arg[dp->arg_index].a. | ||
3928 | a_wide_string); | ||
3929 | |||
3930 | # if !WIDE_CHAR_VERSION | ||
3931 | tmp_length = xtimes (tmp_length, MB_CUR_MAX); | ||
3932 | # endif | ||
3933 | } | ||
3934 | else | ||
3935 | # endif | ||
3936 | tmp_length = strlen (a.arg[dp->arg_index].a.a_string); | ||
3937 | break; | ||
3938 | |||
3939 | case 'p': | ||
3940 | tmp_length = (unsigned int) (sizeof (void *) * CHAR_BIT * 0.25 /* binary -> hexadecimal */ | ||
3941 | ) + 1 /* turn floor into ceil */ | ||
3942 | + 2; /* account for leading 0x */ | ||
3943 | break; | ||
3944 | |||
3945 | default: | ||
3946 | abort (); | ||
3947 | } | ||
3948 | |||
3949 | # if ENABLE_UNISTDIO | ||
3950 | /* Padding considers the number of characters, therefore the | ||
3951 | number of elements after padding may be | ||
3952 | > max (tmp_length, width) | ||
3953 | but is certainly | ||
3954 | <= tmp_length + width. */ | ||
3955 | tmp_length = xsum (tmp_length, width); | ||
3956 | # else | ||
3957 | /* Padding considers the number of elements, says POSIX. */ | ||
3958 | if (tmp_length < width) | ||
3959 | tmp_length = width; | ||
3960 | # endif | ||
3961 | |||
3962 | tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */ | ||
3963 | } | ||
3964 | |||
3965 | if (tmp_length <= sizeof (tmpbuf) / sizeof (TCHAR_T)) | ||
3966 | tmp = tmpbuf; | ||
3967 | else | ||
3968 | { | ||
3969 | size_t tmp_memsize = | ||
3970 | xtimes (tmp_length, sizeof (TCHAR_T)); | ||
3971 | |||
3972 | if (size_overflow_p (tmp_memsize)) | ||
3973 | /* Overflow, would lead to out of memory. */ | ||
3974 | goto out_of_memory; | ||
3975 | tmp = (TCHAR_T *) malloc (tmp_memsize); | ||
3976 | if (tmp == NULL) | ||
3977 | /* Out of memory. */ | ||
3978 | goto out_of_memory; | ||
3979 | } | ||
3980 | #endif | ||
3981 | |||
3982 | /* Decide whether to handle the precision ourselves. */ | ||
3983 | #if NEED_PRINTF_UNBOUNDED_PRECISION | ||
3984 | switch (dp->conversion) | ||
3985 | { | ||
3986 | case 'd': | ||
3987 | case 'i': | ||
3988 | case 'u': | ||
3989 | case 'o': | ||
3990 | case 'x': | ||
3991 | case 'X': | ||
3992 | case 'p': | ||
3993 | prec_ourselves = has_precision && (precision > 0); | ||
3994 | break; | ||
3995 | default: | ||
3996 | prec_ourselves = 0; | ||
3997 | break; | ||
3998 | } | ||
3999 | #endif | ||
4000 | |||
4001 | /* Decide whether to perform the padding ourselves. */ | ||
4002 | #if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION | ||
4003 | switch (dp->conversion) | ||
4004 | { | ||
4005 | # if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO | ||
4006 | /* If we need conversion from TCHAR_T[] to DCHAR_T[], we need | ||
4007 | to perform the padding after this conversion. Functions | ||
4008 | with unistdio extensions perform the padding based on | ||
4009 | character count rather than element count. */ | ||
4010 | case 'c': | ||
4011 | case 's': | ||
4012 | # endif | ||
4013 | # if NEED_PRINTF_FLAG_ZERO | ||
4014 | case 'f': | ||
4015 | case 'F': | ||
4016 | case 'e': | ||
4017 | case 'E': | ||
4018 | case 'g': | ||
4019 | case 'G': | ||
4020 | case 'a': | ||
4021 | case 'A': | ||
4022 | # endif | ||
4023 | pad_ourselves = 1; | ||
4024 | break; | ||
4025 | default: | ||
4026 | pad_ourselves = prec_ourselves; | ||
4027 | break; | ||
4028 | } | ||
4029 | #endif | ||
4030 | |||
4031 | /* Construct the format string for calling snprintf or | ||
4032 | sprintf. */ | ||
4033 | fbp = buf; | ||
4034 | *fbp++ = '%'; | ||
4035 | #if NEED_PRINTF_FLAG_GROUPING | ||
4036 | /* The underlying implementation doesn't support the ' flag. | ||
4037 | Produce no grouping characters in this case; this is | ||
4038 | acceptable because the grouping is locale dependent. */ | ||
4039 | #else | ||
4040 | if (flags & FLAG_GROUP) | ||
4041 | *fbp++ = '\''; | ||
4042 | #endif | ||
4043 | if (flags & FLAG_LEFT) | ||
4044 | *fbp++ = '-'; | ||
4045 | if (flags & FLAG_SHOWSIGN) | ||
4046 | *fbp++ = '+'; | ||
4047 | if (flags & FLAG_SPACE) | ||
4048 | *fbp++ = ' '; | ||
4049 | if (flags & FLAG_ALT) | ||
4050 | *fbp++ = '#'; | ||
4051 | if (!pad_ourselves) | ||
4052 | { | ||
4053 | if (flags & FLAG_ZERO) | ||
4054 | *fbp++ = '0'; | ||
4055 | if (dp->width_start != dp->width_end) | ||
4056 | { | ||
4057 | size_t n = dp->width_end - dp->width_start; | ||
4058 | /* The width specification is known to consist only | ||
4059 | of standard ASCII characters. */ | ||
4060 | if (sizeof (FCHAR_T) == sizeof (TCHAR_T)) | ||
4061 | { | ||
4062 | memcpy (fbp, dp->width_start, | ||
4063 | n * sizeof (TCHAR_T)); | ||
4064 | fbp += n; | ||
4065 | } | ||
4066 | else | ||
4067 | { | ||
4068 | const FCHAR_T *mp = dp->width_start; | ||
4069 | do | ||
4070 | *fbp++ = (unsigned char) *mp++; | ||
4071 | while (--n > 0); | ||
4072 | } | ||
4073 | } | ||
4074 | } | ||
4075 | if (!prec_ourselves) | ||
4076 | { | ||
4077 | if (dp->precision_start != dp->precision_end) | ||
4078 | { | ||
4079 | size_t n = dp->precision_end - dp->precision_start; | ||
4080 | /* The precision specification is known to consist only | ||
4081 | of standard ASCII characters. */ | ||
4082 | if (sizeof (FCHAR_T) == sizeof (TCHAR_T)) | ||
4083 | { | ||
4084 | memcpy (fbp, dp->precision_start, | ||
4085 | n * sizeof (TCHAR_T)); | ||
4086 | fbp += n; | ||
4087 | } | ||
4088 | else | ||
4089 | { | ||
4090 | const FCHAR_T *mp = dp->precision_start; | ||
4091 | do | ||
4092 | *fbp++ = (unsigned char) *mp++; | ||
4093 | while (--n > 0); | ||
4094 | } | ||
4095 | } | ||
4096 | } | ||
4097 | |||
4098 | switch (type) | ||
4099 | { | ||
4100 | #if HAVE_LONG_LONG_INT | ||
4101 | case TYPE_LONGLONGINT: | ||
4102 | case TYPE_ULONGLONGINT: | ||
4103 | # if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__ | ||
4104 | *fbp++ = 'I'; | ||
4105 | *fbp++ = '6'; | ||
4106 | *fbp++ = '4'; | ||
4107 | break; | ||
4108 | # else | ||
4109 | *fbp++ = 'l'; | ||
4110 | /*FALLTHROUGH*/ | ||
4111 | # endif | ||
4112 | #endif | ||
4113 | case TYPE_LONGINT: | ||
4114 | case TYPE_ULONGINT: | ||
4115 | #if HAVE_WINT_T | ||
4116 | case TYPE_WIDE_CHAR: | ||
4117 | #endif | ||
4118 | #if HAVE_WCHAR_T | ||
4119 | case TYPE_WIDE_STRING: | ||
4120 | #endif | ||
4121 | *fbp++ = 'l'; | ||
4122 | break; | ||
4123 | case TYPE_LONGDOUBLE: | ||
4124 | *fbp++ = 'L'; | ||
4125 | break; | ||
4126 | default: | ||
4127 | break; | ||
4128 | } | ||
4129 | #if NEED_PRINTF_DIRECTIVE_F | ||
4130 | if (dp->conversion == 'F') | ||
4131 | *fbp = 'f'; | ||
4132 | else | ||
4133 | #endif | ||
4134 | *fbp = dp->conversion; | ||
4135 | #if USE_SNPRINTF | ||
4136 | # if !(__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)) | ||
4137 | fbp[1] = '%'; | ||
4138 | fbp[2] = 'n'; | ||
4139 | fbp[3] = '\0'; | ||
4140 | # else | ||
4141 | /* On glibc2 systems from glibc >= 2.3 - probably also older | ||
4142 | ones - we know that snprintf's returns value conforms to | ||
4143 | ISO C 99: the gl_SNPRINTF_DIRECTIVE_N test passes. | ||
4144 | Therefore we can avoid using %n in this situation. | ||
4145 | On glibc2 systems from 2004-10-18 or newer, the use of %n | ||
4146 | in format strings in writable memory may crash the program | ||
4147 | (if compiled with _FORTIFY_SOURCE=2), so we should avoid it | ||
4148 | in this situation. */ | ||
4149 | fbp[1] = '\0'; | ||
4150 | # endif | ||
4151 | #else | ||
4152 | fbp[1] = '\0'; | ||
4153 | #endif | ||
4154 | |||
4155 | /* Construct the arguments for calling snprintf or sprintf. */ | ||
4156 | prefix_count = 0; | ||
4157 | if (!pad_ourselves && dp->width_arg_index != ARG_NONE) | ||
4158 | { | ||
4159 | if (!(a.arg[dp->width_arg_index].type == TYPE_INT)) | ||
4160 | abort (); | ||
4161 | prefixes[prefix_count++] = | ||
4162 | a.arg[dp->width_arg_index].a.a_int; | ||
4163 | } | ||
4164 | if (dp->precision_arg_index != ARG_NONE) | ||
4165 | { | ||
4166 | if (!(a.arg[dp->precision_arg_index].type == TYPE_INT)) | ||
4167 | abort (); | ||
4168 | prefixes[prefix_count++] = | ||
4169 | a.arg[dp->precision_arg_index].a.a_int; | ||
4170 | } | ||
4171 | |||
4172 | #if USE_SNPRINTF | ||
4173 | /* The SNPRINTF result is appended after result[0..length]. | ||
4174 | The latter is an array of DCHAR_T; SNPRINTF appends an | ||
4175 | array of TCHAR_T to it. This is possible because | ||
4176 | sizeof (TCHAR_T) divides sizeof (DCHAR_T) and | ||
4177 | alignof (TCHAR_T) <= alignof (DCHAR_T). */ | ||
4178 | # define TCHARS_PER_DCHAR (sizeof (DCHAR_T) / sizeof (TCHAR_T)) | ||
4179 | /* Ensure that maxlen below will be >= 2. Needed on BeOS, | ||
4180 | where an snprintf() with maxlen==1 acts like sprintf(). */ | ||
4181 | ENSURE_ALLOCATION (xsum (length, | ||
4182 | (2 + TCHARS_PER_DCHAR - 1) | ||
4183 | / TCHARS_PER_DCHAR)); | ||
4184 | /* Prepare checking whether snprintf returns the count | ||
4185 | via %n. */ | ||
4186 | *(TCHAR_T *) (result + length) = '\0'; | ||
4187 | #endif | ||
4188 | |||
4189 | for (;;) | ||
4190 | { | ||
4191 | int count = -1; | ||
4192 | |||
4193 | #if USE_SNPRINTF | ||
4194 | int retcount = 0; | ||
4195 | size_t maxlen = allocated - length; | ||
4196 | /* SNPRINTF can fail if its second argument is | ||
4197 | > INT_MAX. */ | ||
4198 | if (maxlen > INT_MAX / TCHARS_PER_DCHAR) | ||
4199 | maxlen = INT_MAX / TCHARS_PER_DCHAR; | ||
4200 | maxlen = maxlen * TCHARS_PER_DCHAR; | ||
4201 | # define SNPRINTF_BUF(arg) \ | ||
4202 | switch (prefix_count) \ | ||
4203 | { \ | ||
4204 | case 0: \ | ||
4205 | retcount = SNPRINTF ((TCHAR_T *) (result + length), \ | ||
4206 | maxlen, buf, \ | ||
4207 | arg, &count); \ | ||
4208 | break; \ | ||
4209 | case 1: \ | ||
4210 | retcount = SNPRINTF ((TCHAR_T *) (result + length), \ | ||
4211 | maxlen, buf, \ | ||
4212 | prefixes[0], arg, &count); \ | ||
4213 | break; \ | ||
4214 | case 2: \ | ||
4215 | retcount = SNPRINTF ((TCHAR_T *) (result + length), \ | ||
4216 | maxlen, buf, \ | ||
4217 | prefixes[0], prefixes[1], arg, \ | ||
4218 | &count); \ | ||
4219 | break; \ | ||
4220 | default: \ | ||
4221 | abort (); \ | ||
4222 | } | ||
4223 | #else | ||
4224 | # define SNPRINTF_BUF(arg) \ | ||
4225 | switch (prefix_count) \ | ||
4226 | { \ | ||
4227 | case 0: \ | ||
4228 | count = sprintf (tmp, buf, arg); \ | ||
4229 | break; \ | ||
4230 | case 1: \ | ||
4231 | count = sprintf (tmp, buf, prefixes[0], arg); \ | ||
4232 | break; \ | ||
4233 | case 2: \ | ||
4234 | count = sprintf (tmp, buf, prefixes[0], prefixes[1],\ | ||
4235 | arg); \ | ||
4236 | break; \ | ||
4237 | default: \ | ||
4238 | abort (); \ | ||
4239 | } | ||
4240 | #endif | ||
4241 | |||
4242 | switch (type) | ||
4243 | { | ||
4244 | case TYPE_SCHAR: | ||
4245 | { | ||
4246 | int arg = a.arg[dp->arg_index].a.a_schar; | ||
4247 | SNPRINTF_BUF (arg); | ||
4248 | } | ||
4249 | break; | ||
4250 | case TYPE_UCHAR: | ||
4251 | { | ||
4252 | unsigned int arg = a.arg[dp->arg_index].a.a_uchar; | ||
4253 | SNPRINTF_BUF (arg); | ||
4254 | } | ||
4255 | break; | ||
4256 | case TYPE_SHORT: | ||
4257 | { | ||
4258 | int arg = a.arg[dp->arg_index].a.a_short; | ||
4259 | SNPRINTF_BUF (arg); | ||
4260 | } | ||
4261 | break; | ||
4262 | case TYPE_USHORT: | ||
4263 | { | ||
4264 | unsigned int arg = a.arg[dp->arg_index].a.a_ushort; | ||
4265 | SNPRINTF_BUF (arg); | ||
4266 | } | ||
4267 | break; | ||
4268 | case TYPE_INT: | ||
4269 | { | ||
4270 | int arg = a.arg[dp->arg_index].a.a_int; | ||
4271 | SNPRINTF_BUF (arg); | ||
4272 | } | ||
4273 | break; | ||
4274 | case TYPE_UINT: | ||
4275 | { | ||
4276 | unsigned int arg = a.arg[dp->arg_index].a.a_uint; | ||
4277 | SNPRINTF_BUF (arg); | ||
4278 | } | ||
4279 | break; | ||
4280 | case TYPE_LONGINT: | ||
4281 | { | ||
4282 | long int arg = a.arg[dp->arg_index].a.a_longint; | ||
4283 | SNPRINTF_BUF (arg); | ||
4284 | } | ||
4285 | break; | ||
4286 | case TYPE_ULONGINT: | ||
4287 | { | ||
4288 | unsigned long int arg = | ||
4289 | a.arg[dp->arg_index].a.a_ulongint; | ||
4290 | SNPRINTF_BUF (arg); | ||
4291 | } | ||
4292 | break; | ||
4293 | #if HAVE_LONG_LONG_INT | ||
4294 | case TYPE_LONGLONGINT: | ||
4295 | { | ||
4296 | long long int arg = | ||
4297 | a.arg[dp->arg_index].a.a_longlongint; | ||
4298 | SNPRINTF_BUF (arg); | ||
4299 | } | ||
4300 | break; | ||
4301 | case TYPE_ULONGLONGINT: | ||
4302 | { | ||
4303 | unsigned long long int arg = | ||
4304 | a.arg[dp->arg_index].a.a_ulonglongint; | ||
4305 | SNPRINTF_BUF (arg); | ||
4306 | } | ||
4307 | break; | ||
4308 | #endif | ||
4309 | case TYPE_DOUBLE: | ||
4310 | { | ||
4311 | double arg = a.arg[dp->arg_index].a.a_double; | ||
4312 | SNPRINTF_BUF (arg); | ||
4313 | } | ||
4314 | break; | ||
4315 | case TYPE_LONGDOUBLE: | ||
4316 | { | ||
4317 | long double arg = | ||
4318 | a.arg[dp->arg_index].a.a_longdouble; | ||
4319 | SNPRINTF_BUF (arg); | ||
4320 | } | ||
4321 | break; | ||
4322 | case TYPE_CHAR: | ||
4323 | { | ||
4324 | int arg = a.arg[dp->arg_index].a.a_char; | ||
4325 | SNPRINTF_BUF (arg); | ||
4326 | } | ||
4327 | break; | ||
4328 | #if HAVE_WINT_T | ||
4329 | case TYPE_WIDE_CHAR: | ||
4330 | { | ||
4331 | wint_t arg = a.arg[dp->arg_index].a.a_wide_char; | ||
4332 | SNPRINTF_BUF (arg); | ||
4333 | } | ||
4334 | break; | ||
4335 | #endif | ||
4336 | case TYPE_STRING: | ||
4337 | { | ||
4338 | const char *arg = a.arg[dp->arg_index].a.a_string; | ||
4339 | SNPRINTF_BUF (arg); | ||
4340 | } | ||
4341 | break; | ||
4342 | #if HAVE_WCHAR_T | ||
4343 | case TYPE_WIDE_STRING: | ||
4344 | { | ||
4345 | const wchar_t *arg = | ||
4346 | a.arg[dp->arg_index].a.a_wide_string; | ||
4347 | SNPRINTF_BUF (arg); | ||
4348 | } | ||
4349 | break; | ||
4350 | #endif | ||
4351 | case TYPE_POINTER: | ||
4352 | { | ||
4353 | void *arg = a.arg[dp->arg_index].a.a_pointer; | ||
4354 | SNPRINTF_BUF (arg); | ||
4355 | } | ||
4356 | break; | ||
4357 | default: | ||
4358 | abort (); | ||
4359 | } | ||
4360 | |||
4361 | #if USE_SNPRINTF | ||
4362 | /* Portability: Not all implementations of snprintf() | ||
4363 | are ISO C 99 compliant. Determine the number of | ||
4364 | bytes that snprintf() has produced or would have | ||
4365 | produced. */ | ||
4366 | if (count >= 0) | ||
4367 | { | ||
4368 | /* Verify that snprintf() has NUL-terminated its | ||
4369 | result. */ | ||
4370 | if (count < maxlen | ||
4371 | && ((TCHAR_T *) (result + length))[count] != '\0') | ||
4372 | abort (); | ||
4373 | /* Portability hack. */ | ||
4374 | if (retcount > count) | ||
4375 | count = retcount; | ||
4376 | } | ||
4377 | else | ||
4378 | { | ||
4379 | /* snprintf() doesn't understand the '%n' | ||
4380 | directive. */ | ||
4381 | if (fbp[1] != '\0') | ||
4382 | { | ||
4383 | /* Don't use the '%n' directive; instead, look | ||
4384 | at the snprintf() return value. */ | ||
4385 | fbp[1] = '\0'; | ||
4386 | continue; | ||
4387 | } | ||
4388 | else | ||
4389 | { | ||
4390 | /* Look at the snprintf() return value. */ | ||
4391 | if (retcount < 0) | ||
4392 | { | ||
4393 | /* HP-UX 10.20 snprintf() is doubly deficient: | ||
4394 | It doesn't understand the '%n' directive, | ||
4395 | *and* it returns -1 (rather than the length | ||
4396 | that would have been required) when the | ||
4397 | buffer is too small. */ | ||
4398 | size_t bigger_need = | ||
4399 | xsum (xtimes (allocated, 2), 12); | ||
4400 | ENSURE_ALLOCATION (bigger_need); | ||
4401 | continue; | ||
4402 | } | ||
4403 | else | ||
4404 | count = retcount; | ||
4405 | } | ||
4406 | } | ||
4407 | #endif | ||
4408 | |||
4409 | /* Attempt to handle failure. */ | ||
4410 | if (count < 0) | ||
4411 | { | ||
4412 | if (!(result == resultbuf || result == NULL)) | ||
4413 | free (result); | ||
4414 | if (buf_malloced != NULL) | ||
4415 | free (buf_malloced); | ||
4416 | CLEANUP (); | ||
4417 | errno = EINVAL; | ||
4418 | return NULL; | ||
4419 | } | ||
4420 | |||
4421 | #if USE_SNPRINTF | ||
4422 | /* Handle overflow of the allocated buffer. | ||
4423 | If such an overflow occurs, a C99 compliant snprintf() | ||
4424 | returns a count >= maxlen. However, a non-compliant | ||
4425 | snprintf() function returns only count = maxlen - 1. To | ||
4426 | cover both cases, test whether count >= maxlen - 1. */ | ||
4427 | if ((unsigned int) count + 1 >= maxlen) | ||
4428 | { | ||
4429 | /* If maxlen already has attained its allowed maximum, | ||
4430 | allocating more memory will not increase maxlen. | ||
4431 | Instead of looping, bail out. */ | ||
4432 | if (maxlen == INT_MAX / TCHARS_PER_DCHAR) | ||
4433 | goto overflow; | ||
4434 | else | ||
4435 | { | ||
4436 | /* Need at least (count + 1) * sizeof (TCHAR_T) | ||
4437 | bytes. (The +1 is for the trailing NUL.) | ||
4438 | But ask for (count + 2) * sizeof (TCHAR_T) | ||
4439 | bytes, so that in the next round, we likely get | ||
4440 | maxlen > (unsigned int) count + 1 | ||
4441 | and so we don't get here again. | ||
4442 | And allocate proportionally, to avoid looping | ||
4443 | eternally if snprintf() reports a too small | ||
4444 | count. */ | ||
4445 | size_t n = xmax (xsum (length, | ||
4446 | ((unsigned int) count + 2 | ||
4447 | + TCHARS_PER_DCHAR - 1) | ||
4448 | / TCHARS_PER_DCHAR), | ||
4449 | xtimes (allocated, 2)); | ||
4450 | |||
4451 | ENSURE_ALLOCATION (n); | ||
4452 | continue; | ||
4453 | } | ||
4454 | } | ||
4455 | #endif | ||
4456 | |||
4457 | #if NEED_PRINTF_UNBOUNDED_PRECISION | ||
4458 | if (prec_ourselves) | ||
4459 | { | ||
4460 | /* Handle the precision. */ | ||
4461 | TCHAR_T *prec_ptr = | ||
4462 | # if USE_SNPRINTF | ||
4463 | (TCHAR_T *) (result + length); | ||
4464 | # else | ||
4465 | tmp; | ||
4466 | # endif | ||
4467 | size_t prefix_count; | ||
4468 | size_t move; | ||
4469 | |||
4470 | prefix_count = 0; | ||
4471 | /* Put the additional zeroes after the sign. */ | ||
4472 | if (count >= 1 | ||
4473 | && (*prec_ptr == '-' || *prec_ptr == '+' | ||
4474 | || *prec_ptr == ' ')) | ||
4475 | prefix_count = 1; | ||
4476 | /* Put the additional zeroes after the 0x prefix if | ||
4477 | (flags & FLAG_ALT) || (dp->conversion == 'p'). */ | ||
4478 | else if (count >= 2 | ||
4479 | && prec_ptr[0] == '0' | ||
4480 | && (prec_ptr[1] == 'x' | ||
4481 | || prec_ptr[1] == 'X')) | ||
4482 | prefix_count = 2; | ||
4483 | |||
4484 | move = count - prefix_count; | ||
4485 | if (precision > move) | ||
4486 | { | ||
4487 | /* Insert zeroes. */ | ||
4488 | size_t insert = precision - move; | ||
4489 | TCHAR_T *prec_end; | ||
4490 | |||
4491 | # if USE_SNPRINTF | ||
4492 | size_t n = xsum (length, | ||
4493 | (count + insert + | ||
4494 | TCHARS_PER_DCHAR - | ||
4495 | 1) / TCHARS_PER_DCHAR); | ||
4496 | length += | ||
4497 | (count + TCHARS_PER_DCHAR - | ||
4498 | 1) / TCHARS_PER_DCHAR; | ||
4499 | ENSURE_ALLOCATION (n); | ||
4500 | length -= | ||
4501 | (count + TCHARS_PER_DCHAR - | ||
4502 | 1) / TCHARS_PER_DCHAR; | ||
4503 | prec_ptr = (TCHAR_T *) (result + length); | ||
4504 | # endif | ||
4505 | |||
4506 | prec_end = prec_ptr + count; | ||
4507 | prec_ptr += prefix_count; | ||
4508 | |||
4509 | while (prec_end > prec_ptr) | ||
4510 | { | ||
4511 | prec_end--; | ||
4512 | prec_end[insert] = prec_end[0]; | ||
4513 | } | ||
4514 | |||
4515 | prec_end += insert; | ||
4516 | do | ||
4517 | *--prec_end = '0'; | ||
4518 | while (prec_end > prec_ptr); | ||
4519 | |||
4520 | count += insert; | ||
4521 | } | ||
4522 | } | ||
4523 | #endif | ||
4524 | |||
4525 | #if !DCHAR_IS_TCHAR | ||
4526 | # if !USE_SNPRINTF | ||
4527 | if (count >= tmp_length) | ||
4528 | /* tmp_length was incorrectly calculated - fix the | ||
4529 | code above! */ | ||
4530 | abort (); | ||
4531 | # endif | ||
4532 | |||
4533 | /* Convert from TCHAR_T[] to DCHAR_T[]. */ | ||
4534 | if (dp->conversion == 'c' || dp->conversion == 's') | ||
4535 | { | ||
4536 | /* type = TYPE_CHAR or TYPE_WIDE_CHAR or TYPE_STRING | ||
4537 | TYPE_WIDE_STRING. | ||
4538 | The result string is not certainly ASCII. */ | ||
4539 | const TCHAR_T *tmpsrc; | ||
4540 | DCHAR_T *tmpdst; | ||
4541 | size_t tmpdst_len; | ||
4542 | /* This code assumes that TCHAR_T is 'char'. */ | ||
4543 | typedef int TCHAR_T_verify | ||
4544 | [2 * (sizeof (TCHAR_T) == 1) - 1]; | ||
4545 | # if USE_SNPRINTF | ||
4546 | tmpsrc = (TCHAR_T *) (result + length); | ||
4547 | # else | ||
4548 | tmpsrc = tmp; | ||
4549 | # endif | ||
4550 | tmpdst = NULL; | ||
4551 | tmpdst_len = 0; | ||
4552 | if (DCHAR_CONV_FROM_ENCODING (locale_charset (), | ||
4553 | iconveh_question_mark, | ||
4554 | tmpsrc, count, | ||
4555 | NULL, | ||
4556 | &tmpdst, &tmpdst_len) | ||
4557 | < 0) | ||
4558 | { | ||
4559 | int saved_errno = errno; | ||
4560 | if (!(result == resultbuf || result == NULL)) | ||
4561 | free (result); | ||
4562 | if (buf_malloced != NULL) | ||
4563 | free (buf_malloced); | ||
4564 | CLEANUP (); | ||
4565 | errno = saved_errno; | ||
4566 | return NULL; | ||
4567 | } | ||
4568 | ENSURE_ALLOCATION (xsum (length, tmpdst_len)); | ||
4569 | DCHAR_CPY (result + length, tmpdst, tmpdst_len); | ||
4570 | free (tmpdst); | ||
4571 | count = tmpdst_len; | ||
4572 | } | ||
4573 | else | ||
4574 | { | ||
4575 | /* The result string is ASCII. | ||
4576 | Simple 1:1 conversion. */ | ||
4577 | # if USE_SNPRINTF | ||
4578 | /* If sizeof (DCHAR_T) == sizeof (TCHAR_T), it's a | ||
4579 | no-op conversion, in-place on the array starting | ||
4580 | at (result + length). */ | ||
4581 | if (sizeof (DCHAR_T) != sizeof (TCHAR_T)) | ||
4582 | # endif | ||
4583 | { | ||
4584 | const TCHAR_T *tmpsrc; | ||
4585 | DCHAR_T *tmpdst; | ||
4586 | size_t n; | ||
4587 | |||
4588 | # if USE_SNPRINTF | ||
4589 | if (result == resultbuf) | ||
4590 | { | ||
4591 | tmpsrc = (TCHAR_T *) (result + length); | ||
4592 | /* ENSURE_ALLOCATION will not move tmpsrc | ||
4593 | (because it's part of resultbuf). */ | ||
4594 | ENSURE_ALLOCATION (xsum (length, count)); | ||
4595 | } | ||
4596 | else | ||
4597 | { | ||
4598 | /* ENSURE_ALLOCATION will move the array | ||
4599 | (because it uses realloc(). */ | ||
4600 | ENSURE_ALLOCATION (xsum (length, count)); | ||
4601 | tmpsrc = (TCHAR_T *) (result + length); | ||
4602 | } | ||
4603 | # else | ||
4604 | tmpsrc = tmp; | ||
4605 | ENSURE_ALLOCATION (xsum (length, count)); | ||
4606 | # endif | ||
4607 | tmpdst = result + length; | ||
4608 | /* Copy backwards, because of overlapping. */ | ||
4609 | tmpsrc += count; | ||
4610 | tmpdst += count; | ||
4611 | for (n = count; n > 0; n--) | ||
4612 | *--tmpdst = (unsigned char) *--tmpsrc; | ||
4613 | } | ||
4614 | } | ||
4615 | #endif | ||
4616 | |||
4617 | #if DCHAR_IS_TCHAR && !USE_SNPRINTF | ||
4618 | /* Make room for the result. */ | ||
4619 | if (count > allocated - length) | ||
4620 | { | ||
4621 | /* Need at least count elements. But allocate | ||
4622 | proportionally. */ | ||
4623 | size_t n = | ||
4624 | xmax (xsum (length, count), xtimes (allocated, 2)); | ||
4625 | |||
4626 | ENSURE_ALLOCATION (n); | ||
4627 | } | ||
4628 | #endif | ||
4629 | |||
4630 | /* Here count <= allocated - length. */ | ||
4631 | |||
4632 | /* Perform padding. */ | ||
4633 | #if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION | ||
4634 | if (pad_ourselves && has_width) | ||
4635 | { | ||
4636 | size_t w; | ||
4637 | # if ENABLE_UNISTDIO | ||
4638 | /* Outside POSIX, it's preferrable to compare the width | ||
4639 | against the number of _characters_ of the converted | ||
4640 | value. */ | ||
4641 | w = DCHAR_MBSNLEN (result + length, count); | ||
4642 | # else | ||
4643 | /* The width is compared against the number of _bytes_ | ||
4644 | of the converted value, says POSIX. */ | ||
4645 | w = count; | ||
4646 | # endif | ||
4647 | if (w < width) | ||
4648 | { | ||
4649 | size_t pad = width - w; | ||
4650 | # if USE_SNPRINTF | ||
4651 | /* Make room for the result. */ | ||
4652 | if (xsum (count, pad) > allocated - length) | ||
4653 | { | ||
4654 | /* Need at least count + pad elements. But | ||
4655 | allocate proportionally. */ | ||
4656 | size_t n = xmax (xsum3 (length, count, pad), | ||
4657 | xtimes (allocated, 2)); | ||
4658 | |||
4659 | length += count; | ||
4660 | ENSURE_ALLOCATION (n); | ||
4661 | length -= count; | ||
4662 | } | ||
4663 | /* Here count + pad <= allocated - length. */ | ||
4664 | # endif | ||
4665 | { | ||
4666 | # if !DCHAR_IS_TCHAR || USE_SNPRINTF | ||
4667 | DCHAR_T *const rp = result + length; | ||
4668 | # else | ||
4669 | DCHAR_T *const rp = tmp; | ||
4670 | # endif | ||
4671 | DCHAR_T *p = rp + count; | ||
4672 | DCHAR_T *end = p + pad; | ||
4673 | # if NEED_PRINTF_FLAG_ZERO | ||
4674 | DCHAR_T *pad_ptr; | ||
4675 | # if !DCHAR_IS_TCHAR | ||
4676 | if (dp->conversion == 'c' | ||
4677 | || dp->conversion == 's') | ||
4678 | /* No zero-padding for string directives. */ | ||
4679 | pad_ptr = NULL; | ||
4680 | else | ||
4681 | # endif | ||
4682 | { | ||
4683 | pad_ptr = (*rp == '-' ? rp + 1 : rp); | ||
4684 | /* No zero-padding of "inf" and "nan". */ | ||
4685 | if ((*pad_ptr >= 'A' && *pad_ptr <= 'Z') | ||
4686 | || (*pad_ptr >= 'a' && *pad_ptr <= 'z')) | ||
4687 | pad_ptr = NULL; | ||
4688 | } | ||
4689 | # endif | ||
4690 | /* The generated string now extends from rp to p, | ||
4691 | with the zero padding insertion point being at | ||
4692 | pad_ptr. */ | ||
4693 | |||
4694 | count = count + pad; /* = end - rp */ | ||
4695 | |||
4696 | if (flags & FLAG_LEFT) | ||
4697 | { | ||
4698 | /* Pad with spaces on the right. */ | ||
4699 | for (; pad > 0; pad--) | ||
4700 | *p++ = ' '; | ||
4701 | } | ||
4702 | # if NEED_PRINTF_FLAG_ZERO | ||
4703 | else if ((flags & FLAG_ZERO) && pad_ptr != NULL) | ||
4704 | { | ||
4705 | /* Pad with zeroes. */ | ||
4706 | DCHAR_T *q = end; | ||
4707 | |||
4708 | while (p > pad_ptr) | ||
4709 | *--q = *--p; | ||
4710 | for (; pad > 0; pad--) | ||
4711 | *p++ = '0'; | ||
4712 | } | ||
4713 | # endif | ||
4714 | else | ||
4715 | { | ||
4716 | /* Pad with spaces on the left. */ | ||
4717 | DCHAR_T *q = end; | ||
4718 | |||
4719 | while (p > rp) | ||
4720 | *--q = *--p; | ||
4721 | for (; pad > 0; pad--) | ||
4722 | *p++ = ' '; | ||
4723 | } | ||
4724 | } | ||
4725 | } | ||
4726 | } | ||
4727 | #endif | ||
4728 | |||
4729 | #if DCHAR_IS_TCHAR && !USE_SNPRINTF | ||
4730 | if (count >= tmp_length) | ||
4731 | /* tmp_length was incorrectly calculated - fix the | ||
4732 | code above! */ | ||
4733 | abort (); | ||
4734 | #endif | ||
4735 | |||
4736 | /* Here still count <= allocated - length. */ | ||
4737 | |||
4738 | #if !DCHAR_IS_TCHAR || USE_SNPRINTF | ||
4739 | /* The snprintf() result did fit. */ | ||
4740 | #else | ||
4741 | /* Append the sprintf() result. */ | ||
4742 | memcpy (result + length, tmp, count * sizeof (DCHAR_T)); | ||
4743 | #endif | ||
4744 | #if !USE_SNPRINTF | ||
4745 | if (tmp != tmpbuf) | ||
4746 | free (tmp); | ||
4747 | #endif | ||
4748 | |||
4749 | #if NEED_PRINTF_DIRECTIVE_F | ||
4750 | if (dp->conversion == 'F') | ||
4751 | { | ||
4752 | /* Convert the %f result to upper case for %F. */ | ||
4753 | DCHAR_T *rp = result + length; | ||
4754 | size_t rc; | ||
4755 | for (rc = count; rc > 0; rc--, rp++) | ||
4756 | if (*rp >= 'a' && *rp <= 'z') | ||
4757 | *rp = *rp - 'a' + 'A'; | ||
4758 | } | ||
4759 | #endif | ||
4760 | |||
4761 | length += count; | ||
4762 | break; | ||
4763 | } | ||
4764 | } | ||
4765 | } | ||
4766 | } | ||
4767 | |||
4768 | /* Add the final NUL. */ | ||
4769 | ENSURE_ALLOCATION (xsum (length, 1)); | ||
4770 | result[length] = '\0'; | ||
4771 | |||
4772 | if (result != resultbuf && length + 1 < allocated) | ||
4773 | { | ||
4774 | /* Shrink the allocated memory if possible. */ | ||
4775 | DCHAR_T *memory; | ||
4776 | |||
4777 | memory = | ||
4778 | (DCHAR_T *) realloc (result, (length + 1) * sizeof (DCHAR_T)); | ||
4779 | if (memory != NULL) | ||
4780 | result = memory; | ||
4781 | } | ||
4782 | |||
4783 | if (buf_malloced != NULL) | ||
4784 | free (buf_malloced); | ||
4785 | CLEANUP (); | ||
4786 | *lengthp = length; | ||
4787 | /* Note that we can produce a big string of a length > INT_MAX. POSIX | ||
4788 | says that snprintf() fails with errno = EOVERFLOW in this case, but | ||
4789 | that's only because snprintf() returns an 'int'. This function does | ||
4790 | not have this limitation. */ | ||
4791 | return result; | ||
4792 | |||
4793 | overflow: | ||
4794 | if (!(result == resultbuf || result == NULL)) | ||
4795 | free (result); | ||
4796 | if (buf_malloced != NULL) | ||
4797 | free (buf_malloced); | ||
4798 | CLEANUP (); | ||
4799 | errno = EOVERFLOW; | ||
4800 | return NULL; | ||
4801 | |||
4802 | out_of_memory: | ||
4803 | if (!(result == resultbuf || result == NULL)) | ||
4804 | free (result); | ||
4805 | if (buf_malloced != NULL) | ||
4806 | free (buf_malloced); | ||
4807 | out_of_memory_1: | ||
4808 | CLEANUP (); | ||
4809 | errno = ENOMEM; | ||
4810 | return NULL; | ||
4811 | } | ||
4812 | } | ||
4813 | |||
4814 | #undef TCHARS_PER_DCHAR | ||
4815 | #undef SNPRINTF | ||
4816 | #undef USE_SNPRINTF | ||
4817 | #undef DCHAR_CPY | ||
4818 | #undef PRINTF_PARSE | ||
4819 | #undef DIRECTIVES | ||
4820 | #undef DIRECTIVE | ||
4821 | #undef DCHAR_IS_TCHAR | ||
4822 | #undef TCHAR_T | ||
4823 | #undef DCHAR_T | ||
4824 | #undef FCHAR_T | ||
4825 | #undef VASNPRINTF | ||