/src/mozilla-central/mozglue/misc/Printf.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
2 | | * vim: set ts=8 sts=4 et sw=4 tw=99: |
3 | | * This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | /* |
8 | | * Portable safe sprintf code. |
9 | | * |
10 | | * Author: Kipp E.B. Hickman |
11 | | */ |
12 | | |
13 | | #include "mozilla/AllocPolicy.h" |
14 | | #include "mozilla/Printf.h" |
15 | | #include "mozilla/Sprintf.h" |
16 | | #include "mozilla/UniquePtrExtensions.h" |
17 | | #include "mozilla/Vector.h" |
18 | | |
19 | | #include <stdarg.h> |
20 | | #include <stdio.h> |
21 | | #include <stdlib.h> |
22 | | #include <string.h> |
23 | | |
24 | | #if defined(XP_WIN) |
25 | | #include <windows.h> |
26 | | #endif |
27 | | |
28 | | /* |
29 | | * Note: on some platforms va_list is defined as an array, |
30 | | * and requires array notation. |
31 | | */ |
32 | | #ifdef HAVE_VA_COPY |
33 | 0 | #define VARARGS_ASSIGN(foo, bar) VA_COPY(foo, bar) |
34 | | #elif defined(HAVE_VA_LIST_AS_ARRAY) |
35 | | #define VARARGS_ASSIGN(foo, bar) foo[0] = bar[0] |
36 | | #else |
37 | | #define VARARGS_ASSIGN(foo, bar) (foo) = (bar) |
38 | | #endif |
39 | | |
40 | | /* |
41 | | * Numbered Argument State |
42 | | */ |
43 | | struct NumArgState |
44 | | { |
45 | | int type; // type of the current ap |
46 | | va_list ap; // point to the corresponding position on ap |
47 | | }; |
48 | | |
49 | | typedef mozilla::Vector<NumArgState, 20, mozilla::MallocAllocPolicy> NumArgStateVector; |
50 | | |
51 | | |
52 | 0 | #define TYPE_SHORT 0 |
53 | 0 | #define TYPE_USHORT 1 |
54 | 61.4k | #define TYPE_INTN 2 |
55 | 154 | #define TYPE_UINTN 3 |
56 | 4.34k | #define TYPE_LONG 4 |
57 | 208 | #define TYPE_ULONG 5 |
58 | 4.13k | #define TYPE_LONGLONG 6 |
59 | 4.13k | #define TYPE_ULONGLONG 7 |
60 | 0 | #define TYPE_STRING 8 |
61 | 0 | #define TYPE_DOUBLE 9 |
62 | 0 | #define TYPE_INTSTR 10 |
63 | 0 | #define TYPE_POINTER 11 |
64 | | #if defined(XP_WIN) |
65 | | #define TYPE_WSTRING 12 |
66 | | #endif |
67 | 0 | #define TYPE_UNKNOWN 20 |
68 | | |
69 | 53.9k | #define FLAG_LEFT 0x1 |
70 | 49.4k | #define FLAG_SIGNED 0x2 |
71 | 16.4k | #define FLAG_SPACED 0x4 |
72 | 23.0k | #define FLAG_ZEROS 0x8 |
73 | 16.4k | #define FLAG_NEG 0x10 |
74 | | |
75 | | // Fill into the buffer using the data in src |
76 | | bool |
77 | | mozilla::PrintfTarget::fill2(const char* src, int srclen, int width, int flags) |
78 | 12.0k | { |
79 | 12.0k | char space = ' '; |
80 | 12.0k | |
81 | 12.0k | width -= srclen; |
82 | 12.0k | if (width > 0 && (flags & FLAG_LEFT) == 0) { // Right adjusting |
83 | 0 | if (flags & FLAG_ZEROS) |
84 | 0 | space = '0'; |
85 | 0 | while (--width >= 0) { |
86 | 0 | if (!emit(&space, 1)) |
87 | 0 | return false; |
88 | 0 | } |
89 | 0 | } |
90 | 12.0k | |
91 | 12.0k | // Copy out the source data |
92 | 12.0k | if (!emit(src, srclen)) |
93 | 0 | return false; |
94 | 12.0k | |
95 | 12.0k | if (width > 0 && (flags & FLAG_LEFT) != 0) { // Left adjusting |
96 | 0 | while (--width >= 0) { |
97 | 0 | if (!emit(&space, 1)) |
98 | 0 | return false; |
99 | 0 | } |
100 | 0 | } |
101 | 12.0k | return true; |
102 | 12.0k | } |
103 | | |
104 | | /* |
105 | | * Fill a number. The order is: optional-sign zero-filling conversion-digits |
106 | | */ |
107 | | bool |
108 | | mozilla::PrintfTarget::fill_n(const char* src, int srclen, int width, int prec, int type, int flags) |
109 | 20.9k | { |
110 | 20.9k | int zerowidth = 0; |
111 | 20.9k | int precwidth = 0; |
112 | 20.9k | int signwidth = 0; |
113 | 20.9k | int leftspaces = 0; |
114 | 20.9k | int rightspaces = 0; |
115 | 20.9k | int cvtwidth; |
116 | 20.9k | char sign; |
117 | 20.9k | |
118 | 20.9k | if ((type & 1) == 0) { |
119 | 16.4k | if (flags & FLAG_NEG) { |
120 | 0 | sign = '-'; |
121 | 0 | signwidth = 1; |
122 | 16.4k | } else if (flags & FLAG_SIGNED) { |
123 | 0 | sign = '+'; |
124 | 0 | signwidth = 1; |
125 | 16.4k | } else if (flags & FLAG_SPACED) { |
126 | 0 | sign = ' '; |
127 | 0 | signwidth = 1; |
128 | 0 | } |
129 | 16.4k | } |
130 | 20.9k | cvtwidth = signwidth + srclen; |
131 | 20.9k | |
132 | 20.9k | if (prec > 0) { |
133 | 0 | if (prec > srclen) { |
134 | 0 | precwidth = prec - srclen; // Need zero filling |
135 | 0 | cvtwidth += precwidth; |
136 | 0 | } |
137 | 0 | } |
138 | 20.9k | |
139 | 20.9k | if ((flags & FLAG_ZEROS) && (prec < 0)) { |
140 | 2.06k | if (width > cvtwidth) { |
141 | 1.25k | zerowidth = width - cvtwidth; // Zero filling |
142 | 1.25k | cvtwidth += zerowidth; |
143 | 1.25k | } |
144 | 2.06k | } |
145 | 20.9k | |
146 | 20.9k | if (flags & FLAG_LEFT) { |
147 | 0 | if (width > cvtwidth) { |
148 | 0 | // Space filling on the right (i.e. left adjusting) |
149 | 0 | rightspaces = width - cvtwidth; |
150 | 0 | } |
151 | 20.9k | } else { |
152 | 20.9k | if (width > cvtwidth) { |
153 | 0 | // Space filling on the left (i.e. right adjusting) |
154 | 0 | leftspaces = width - cvtwidth; |
155 | 0 | } |
156 | 20.9k | } |
157 | 20.9k | while (--leftspaces >= 0) { |
158 | 0 | if (!emit(" ", 1)) |
159 | 0 | return false; |
160 | 0 | } |
161 | 20.9k | if (signwidth) { |
162 | 0 | if (!emit(&sign, 1)) |
163 | 0 | return false; |
164 | 20.9k | } |
165 | 20.9k | while (--precwidth >= 0) { |
166 | 0 | if (!emit("0", 1)) |
167 | 0 | return false; |
168 | 0 | } |
169 | 22.9k | while (--zerowidth >= 0) { |
170 | 2.00k | if (!emit("0", 1)) |
171 | 0 | return false; |
172 | 2.00k | } |
173 | 20.9k | if (!emit(src, uint32_t(srclen))) |
174 | 0 | return false; |
175 | 20.9k | while (--rightspaces >= 0) { |
176 | 0 | if (!emit(" ", 1)) |
177 | 0 | return false; |
178 | 0 | } |
179 | 20.9k | return true; |
180 | 20.9k | } |
181 | | |
182 | | /* Convert a long into its printable form. */ |
183 | | bool |
184 | | mozilla::PrintfTarget::cvt_l(long num, int width, int prec, int radix, |
185 | | int type, int flags, const char* hexp) |
186 | 16.8k | { |
187 | 16.8k | char cvtbuf[100]; |
188 | 16.8k | char* cvt; |
189 | 16.8k | int digits; |
190 | 16.8k | |
191 | 16.8k | // according to the man page this needs to happen |
192 | 16.8k | if ((prec == 0) && (num == 0)) |
193 | 0 | return true; |
194 | 16.8k | |
195 | 16.8k | // Converting decimal is a little tricky. In the unsigned case we |
196 | 16.8k | // need to stop when we hit 10 digits. In the signed case, we can |
197 | 16.8k | // stop when the number is zero. |
198 | 16.8k | cvt = cvtbuf + sizeof(cvtbuf); |
199 | 16.8k | digits = 0; |
200 | 25.0k | while (num) { |
201 | 8.24k | int digit = (((unsigned long)num) % radix) & 0xF; |
202 | 8.24k | *--cvt = hexp[digit]; |
203 | 8.24k | digits++; |
204 | 8.24k | num = (long)(((unsigned long)num) / radix); |
205 | 8.24k | } |
206 | 16.8k | if (digits == 0) { |
207 | 12.7k | *--cvt = '0'; |
208 | 12.7k | digits++; |
209 | 12.7k | } |
210 | 16.8k | |
211 | 16.8k | // Now that we have the number converted without its sign, deal with |
212 | 16.8k | // the sign and zero padding. |
213 | 16.8k | return fill_n(cvt, digits, width, prec, type, flags); |
214 | 16.8k | } |
215 | | |
216 | | /* Convert a 64-bit integer into its printable form. */ |
217 | | bool |
218 | | mozilla::PrintfTarget::cvt_ll(int64_t num, int width, int prec, int radix, |
219 | | int type, int flags, const char* hexp) |
220 | 4.13k | { |
221 | 4.13k | // According to the man page, this needs to happen. |
222 | 4.13k | if (prec == 0 && num == 0) |
223 | 0 | return true; |
224 | 4.13k | |
225 | 4.13k | // Converting decimal is a little tricky. In the unsigned case we |
226 | 4.13k | // need to stop when we hit 10 digits. In the signed case, we can |
227 | 4.13k | // stop when the number is zero. |
228 | 4.13k | int64_t rad = int64_t(radix); |
229 | 4.13k | char cvtbuf[100]; |
230 | 4.13k | char* cvt = cvtbuf + sizeof(cvtbuf); |
231 | 4.13k | int digits = 0; |
232 | 9.78k | while (num != 0) { |
233 | 5.65k | int64_t quot = uint64_t(num) / rad; |
234 | 5.65k | int64_t rem = uint64_t(num) % rad; |
235 | 5.65k | int32_t digit = int32_t(rem); |
236 | 5.65k | *--cvt = hexp[digit & 0xf]; |
237 | 5.65k | digits++; |
238 | 5.65k | num = quot; |
239 | 5.65k | } |
240 | 4.13k | if (digits == 0) { |
241 | 1.49k | *--cvt = '0'; |
242 | 1.49k | digits++; |
243 | 1.49k | } |
244 | 4.13k | |
245 | 4.13k | // Now that we have the number converted without its sign, deal with |
246 | 4.13k | // the sign and zero padding. |
247 | 4.13k | return fill_n(cvt, digits, width, prec, type, flags); |
248 | 4.13k | } |
249 | | |
250 | | /* |
251 | | * Convert a double precision floating point number into its printable |
252 | | * form. |
253 | | */ |
254 | | bool |
255 | | mozilla::PrintfTarget::cvt_f(double d, const char* fmt0, const char* fmt1) |
256 | 9 | { |
257 | 9 | char fin[20]; |
258 | 9 | // The size is chosen such that we can print DBL_MAX. See bug#1350097. |
259 | 9 | char fout[320]; |
260 | 9 | int amount = fmt1 - fmt0; |
261 | 9 | |
262 | 9 | MOZ_ASSERT((amount > 0) && (amount < (int)sizeof(fin))); |
263 | 9 | if (amount >= (int)sizeof(fin)) { |
264 | 0 | // Totally bogus % command to sprintf. Just ignore it |
265 | 0 | return true; |
266 | 0 | } |
267 | 9 | memcpy(fin, fmt0, (size_t)amount); |
268 | 9 | fin[amount] = 0; |
269 | 9 | |
270 | 9 | // Convert floating point using the native snprintf code |
271 | | #ifdef DEBUG |
272 | | { |
273 | | const char* p = fin; |
274 | | while (*p) { |
275 | | MOZ_ASSERT(*p != 'L'); |
276 | | p++; |
277 | | } |
278 | | } |
279 | | #endif |
280 | | size_t len = SprintfLiteral(fout, fin, d); |
281 | 9 | MOZ_RELEASE_ASSERT(len <= sizeof(fout)); |
282 | 9 | |
283 | 9 | return emit(fout, len); |
284 | 9 | } |
285 | | |
286 | | /* |
287 | | * Convert a string into its printable form. "width" is the output |
288 | | * width. "prec" is the maximum number of characters of "s" to output, |
289 | | * where -1 means until NUL. |
290 | | */ |
291 | | bool |
292 | | mozilla::PrintfTarget::cvt_s(const char* s, int width, int prec, int flags) |
293 | 12.0k | { |
294 | 12.0k | if (prec == 0) |
295 | 0 | return true; |
296 | 12.0k | if (!s) |
297 | 0 | s = "(null)"; |
298 | 12.0k | |
299 | 12.0k | // Limit string length by precision value |
300 | 12.0k | int slen = int(strlen(s)); |
301 | 12.0k | if (0 < prec && prec < slen) |
302 | 0 | slen = prec; |
303 | 12.0k | |
304 | 12.0k | // and away we go |
305 | 12.0k | return fill2(s, slen, width, flags); |
306 | 12.0k | } |
307 | | |
308 | | /* |
309 | | * BuildArgArray stands for Numbered Argument list Sprintf |
310 | | * for example, |
311 | | * fmp = "%4$i, %2$d, %3s, %1d"; |
312 | | * the number must start from 1, and no gap among them |
313 | | */ |
314 | | static bool |
315 | | BuildArgArray(const char* fmt, va_list ap, NumArgStateVector& nas) |
316 | 18.9k | { |
317 | 18.9k | size_t number = 0, cn = 0, i; |
318 | 18.9k | const char* p; |
319 | 18.9k | char c; |
320 | 18.9k | |
321 | 18.9k | |
322 | 18.9k | // First pass: |
323 | 18.9k | // Detemine how many legal % I have got, then allocate space. |
324 | 18.9k | |
325 | 18.9k | p = fmt; |
326 | 18.9k | i = 0; |
327 | 261k | while ((c = *p++) != 0) { |
328 | 242k | if (c != '%') |
329 | 209k | continue; |
330 | 32.9k | if ((c = *p++) == '%') // skip %% case |
331 | 0 | continue; |
332 | 32.9k | |
333 | 37.1k | while (c != 0) { |
334 | 37.1k | if (c > '9' || c < '0') { |
335 | 32.9k | if (c == '$') { // numbered argument case |
336 | 0 | if (i > 0) |
337 | 0 | MOZ_CRASH("Bad format string"); |
338 | 0 | number++; |
339 | 32.9k | } else { // non-numbered argument case |
340 | 32.9k | if (number > 0) |
341 | 32.9k | MOZ_CRASH("Bad format string"); |
342 | 32.9k | i = 1; |
343 | 32.9k | } |
344 | 32.9k | break; |
345 | 4.13k | } |
346 | 4.13k | |
347 | 4.13k | c = *p++; |
348 | 4.13k | } |
349 | 32.9k | } |
350 | 18.9k | |
351 | 18.9k | if (number == 0) |
352 | 18.9k | return true; |
353 | 0 | |
354 | 0 | // Only allow a limited number of arguments. |
355 | 0 | MOZ_RELEASE_ASSERT(number <= 20); |
356 | 0 |
|
357 | 0 | if (!nas.growByUninitialized(number)) |
358 | 0 | return false; |
359 | 0 | |
360 | 0 | for (i = 0; i < number; i++) |
361 | 0 | nas[i].type = TYPE_UNKNOWN; |
362 | 0 |
|
363 | 0 |
|
364 | 0 | // Second pass: |
365 | 0 | // Set nas[].type. |
366 | 0 |
|
367 | 0 | p = fmt; |
368 | 0 | while ((c = *p++) != 0) { |
369 | 0 | if (c != '%') |
370 | 0 | continue; |
371 | 0 | c = *p++; |
372 | 0 | if (c == '%') |
373 | 0 | continue; |
374 | 0 | |
375 | 0 | cn = 0; |
376 | 0 | while (c && c != '$') { // should improve error check later |
377 | 0 | cn = cn*10 + c - '0'; |
378 | 0 | c = *p++; |
379 | 0 | } |
380 | 0 |
|
381 | 0 | if (!c || cn < 1 || cn > number) |
382 | 0 | MOZ_CRASH("Bad format string"); |
383 | 0 |
|
384 | 0 | // nas[cn] starts from 0, and make sure nas[cn].type is not assigned. |
385 | 0 | cn--; |
386 | 0 | if (nas[cn].type != TYPE_UNKNOWN) |
387 | 0 | continue; |
388 | 0 | |
389 | 0 | c = *p++; |
390 | 0 |
|
391 | 0 | // flags |
392 | 0 | while ((c == '-') || (c == '+') || (c == ' ') || (c == '0')) { |
393 | 0 | c = *p++; |
394 | 0 | } |
395 | 0 |
|
396 | 0 | // width |
397 | 0 | if (c == '*') { |
398 | 0 | // not supported feature, for the argument is not numbered |
399 | 0 | MOZ_CRASH("Bad format string"); |
400 | 0 | } |
401 | 0 |
|
402 | 0 | while ((c >= '0') && (c <= '9')) { |
403 | 0 | c = *p++; |
404 | 0 | } |
405 | 0 |
|
406 | 0 | // precision |
407 | 0 | if (c == '.') { |
408 | 0 | c = *p++; |
409 | 0 | if (c == '*') { |
410 | 0 | // not supported feature, for the argument is not numbered |
411 | 0 | MOZ_CRASH("Bad format string"); |
412 | 0 | } |
413 | 0 |
|
414 | 0 | while ((c >= '0') && (c <= '9')) { |
415 | 0 | c = *p++; |
416 | 0 | } |
417 | 0 | } |
418 | 0 |
|
419 | 0 | // size |
420 | 0 | nas[cn].type = TYPE_INTN; |
421 | 0 | if (c == 'h') { |
422 | 0 | nas[cn].type = TYPE_SHORT; |
423 | 0 | c = *p++; |
424 | 0 | } else if (c == 'L') { |
425 | 0 | nas[cn].type = TYPE_LONGLONG; |
426 | 0 | c = *p++; |
427 | 0 | } else if (c == 'l') { |
428 | 0 | nas[cn].type = TYPE_LONG; |
429 | 0 | c = *p++; |
430 | 0 | if (c == 'l') { |
431 | 0 | nas[cn].type = TYPE_LONGLONG; |
432 | 0 | c = *p++; |
433 | 0 | } |
434 | 0 | } else if (c == 'z' || c == 'I') { |
435 | 0 | static_assert(sizeof(size_t) == sizeof(int) || sizeof(size_t) == sizeof(long) || |
436 | 0 | sizeof(size_t) == sizeof(long long), |
437 | 0 | "size_t is not one of the expected sizes"); |
438 | 0 | nas[cn].type = sizeof(size_t) == sizeof(int) ? TYPE_INTN : |
439 | 0 | sizeof(size_t) == sizeof(long) ? TYPE_LONG : TYPE_LONGLONG; |
440 | 0 | c = *p++; |
441 | 0 | } |
442 | 0 |
|
443 | 0 | // format |
444 | 0 | switch (c) { |
445 | 0 | case 'd': |
446 | 0 | case 'c': |
447 | 0 | case 'i': |
448 | 0 | break; |
449 | 0 |
|
450 | 0 | case 'o': |
451 | 0 | case 'u': |
452 | 0 | case 'x': |
453 | 0 | case 'X': |
454 | 0 | // Mark as unsigned type. |
455 | 0 | nas[cn].type |= 1; |
456 | 0 | break; |
457 | 0 |
|
458 | 0 | case 'e': |
459 | 0 | case 'f': |
460 | 0 | case 'g': |
461 | 0 | nas[cn].type = TYPE_DOUBLE; |
462 | 0 | break; |
463 | 0 |
|
464 | 0 | case 'p': |
465 | 0 | nas[cn].type = TYPE_POINTER; |
466 | 0 | break; |
467 | 0 |
|
468 | 0 | case 'S': |
469 | | #if defined(XP_WIN) |
470 | | nas[cn].type = TYPE_WSTRING; |
471 | | #else |
472 | 0 | MOZ_ASSERT(0); |
473 | 0 | nas[cn].type = TYPE_UNKNOWN; |
474 | 0 | #endif |
475 | 0 | break; |
476 | 0 |
|
477 | 0 | case 's': |
478 | | #if defined(XP_WIN) |
479 | | if (nas[cn].type == TYPE_LONG) { |
480 | | nas[cn].type = TYPE_WSTRING; |
481 | | break; |
482 | | } |
483 | | #endif |
484 | | // Other type sizes are not supported here. |
485 | 0 | MOZ_ASSERT (nas[cn].type == TYPE_INTN); |
486 | 0 | nas[cn].type = TYPE_STRING; |
487 | 0 | break; |
488 | 0 |
|
489 | 0 | case 'n': |
490 | 0 | nas[cn].type = TYPE_INTSTR; |
491 | 0 | break; |
492 | 0 |
|
493 | 0 | default: |
494 | 0 | MOZ_ASSERT(0); |
495 | 0 | nas[cn].type = TYPE_UNKNOWN; |
496 | 0 | break; |
497 | 0 | } |
498 | 0 |
|
499 | 0 | // get a legal para. |
500 | 0 | if (nas[cn].type == TYPE_UNKNOWN) |
501 | 0 | MOZ_CRASH("Bad format string"); |
502 | 0 | } |
503 | 0 |
|
504 | 0 |
|
505 | 0 | // Third pass: |
506 | 0 | // Fill nas[].ap. |
507 | 0 |
|
508 | 0 | cn = 0; |
509 | 0 | while (cn < number) { |
510 | 0 | // A TYPE_UNKNOWN here means that the format asked for a |
511 | 0 | // positional argument without specifying the meaning of some |
512 | 0 | // earlier argument. |
513 | 0 | MOZ_ASSERT (nas[cn].type != TYPE_UNKNOWN); |
514 | 0 |
|
515 | 0 | VARARGS_ASSIGN(nas[cn].ap, ap); |
516 | 0 |
|
517 | 0 | switch (nas[cn].type) { |
518 | 0 | case TYPE_SHORT: |
519 | 0 | case TYPE_USHORT: |
520 | 0 | case TYPE_INTN: |
521 | 0 | case TYPE_UINTN: (void) va_arg(ap, int); break; |
522 | 0 | case TYPE_LONG: (void) va_arg(ap, long); break; |
523 | 0 | case TYPE_ULONG: (void) va_arg(ap, unsigned long); break; |
524 | 0 | case TYPE_LONGLONG: (void) va_arg(ap, long long); break; |
525 | 0 | case TYPE_ULONGLONG: (void) va_arg(ap, unsigned long long); break; |
526 | 0 | case TYPE_STRING: (void) va_arg(ap, char*); break; |
527 | 0 | case TYPE_INTSTR: (void) va_arg(ap, int*); break; |
528 | 0 | case TYPE_DOUBLE: (void) va_arg(ap, double); break; |
529 | 0 | case TYPE_POINTER: (void) va_arg(ap, void*); break; |
530 | | #if defined(XP_WIN) |
531 | | case TYPE_WSTRING: (void) va_arg(ap, wchar_t*); break; |
532 | | #endif |
533 | |
|
534 | 0 | default: MOZ_CRASH(); |
535 | 0 | } |
536 | 0 |
|
537 | 0 | cn++; |
538 | 0 | } |
539 | 0 |
|
540 | 0 | return true; |
541 | 0 | } |
542 | | |
543 | | mozilla::PrintfTarget::PrintfTarget() |
544 | | : mEmitted(0) |
545 | 18.9k | { |
546 | 18.9k | } |
547 | | |
548 | | bool |
549 | | mozilla::PrintfTarget::vprint(const char* fmt, va_list ap) |
550 | 18.9k | { |
551 | 18.9k | char c; |
552 | 18.9k | int flags, width, prec, radix, type; |
553 | 18.9k | union { |
554 | 18.9k | char ch; |
555 | 18.9k | int i; |
556 | 18.9k | long l; |
557 | 18.9k | long long ll; |
558 | 18.9k | double d; |
559 | 18.9k | const char* s; |
560 | 18.9k | int* ip; |
561 | 18.9k | void* p; |
562 | | #if defined(XP_WIN) |
563 | | const wchar_t* ws; |
564 | | #endif |
565 | | } u; |
566 | 18.9k | const char* fmt0; |
567 | 18.9k | static const char hex[] = "0123456789abcdef"; |
568 | 18.9k | static const char HEX[] = "0123456789ABCDEF"; |
569 | 18.9k | const char* hexp; |
570 | 18.9k | int i; |
571 | 18.9k | char pattern[20]; |
572 | 18.9k | const char* dolPt = nullptr; // in "%4$.2f", dolPt will point to '.' |
573 | 18.9k | |
574 | 18.9k | // Build an argument array, IF the fmt is numbered argument |
575 | 18.9k | // list style, to contain the Numbered Argument list pointers. |
576 | 18.9k | |
577 | 18.9k | NumArgStateVector nas; |
578 | 18.9k | if (!BuildArgArray(fmt, ap, nas)) { |
579 | 0 | // the fmt contains error Numbered Argument format, jliu@netscape.com |
580 | 0 | MOZ_CRASH("Bad format string"); |
581 | 0 | } |
582 | 18.9k | |
583 | 253k | while ((c = *fmt++) != 0) { |
584 | 234k | if (c != '%') { |
585 | 201k | if (!emit(fmt - 1, 1)) |
586 | 0 | return false; |
587 | 201k | |
588 | 201k | continue; |
589 | 201k | } |
590 | 32.9k | fmt0 = fmt - 1; |
591 | 32.9k | |
592 | 32.9k | // Gobble up the % format string. Hopefully we have handled all |
593 | 32.9k | // of the strange cases! |
594 | 32.9k | flags = 0; |
595 | 32.9k | c = *fmt++; |
596 | 32.9k | if (c == '%') { |
597 | 0 | // quoting a % with %% |
598 | 0 | if (!emit(fmt - 1, 1)) |
599 | 0 | return false; |
600 | 0 | |
601 | 0 | continue; |
602 | 0 | } |
603 | 32.9k | |
604 | 32.9k | if (!nas.empty()) { |
605 | 0 | // the fmt contains the Numbered Arguments feature |
606 | 0 | i = 0; |
607 | 0 | while (c && c != '$') { // should improve error check later |
608 | 0 | i = (i * 10) + (c - '0'); |
609 | 0 | c = *fmt++; |
610 | 0 | } |
611 | 0 |
|
612 | 0 | if (nas[i - 1].type == TYPE_UNKNOWN) |
613 | 0 | MOZ_CRASH("Bad format string"); |
614 | 0 |
|
615 | 0 | ap = nas[i - 1].ap; |
616 | 0 | dolPt = fmt; |
617 | 0 | c = *fmt++; |
618 | 0 | } |
619 | 32.9k | |
620 | 32.9k | // Examine optional flags. Note that we do not implement the |
621 | 32.9k | // '#' flag of sprintf(). The ANSI C spec. of the '#' flag is |
622 | 32.9k | // somewhat ambiguous and not ideal, which is perhaps why |
623 | 32.9k | // the various sprintf() implementations are inconsistent |
624 | 32.9k | // on this feature. |
625 | 35.0k | while ((c == '-') || (c == '+') || (c == ' ') || (c == '0')) { |
626 | 2.06k | if (c == '-') flags |= FLAG_LEFT; |
627 | 2.06k | if (c == '+') flags |= FLAG_SIGNED; |
628 | 2.06k | if (c == ' ') flags |= FLAG_SPACED; |
629 | 2.06k | if (c == '0') flags |= FLAG_ZEROS; |
630 | 2.06k | c = *fmt++; |
631 | 2.06k | } |
632 | 32.9k | if (flags & FLAG_SIGNED) flags &= ~FLAG_SPACED; |
633 | 32.9k | if (flags & FLAG_LEFT) flags &= ~FLAG_ZEROS; |
634 | 32.9k | |
635 | 32.9k | // width |
636 | 32.9k | if (c == '*') { |
637 | 0 | c = *fmt++; |
638 | 0 | width = va_arg(ap, int); |
639 | 0 | if (width < 0) { |
640 | 0 | width = -width; |
641 | 0 | flags |= FLAG_LEFT; |
642 | 0 | flags &= ~FLAG_ZEROS; |
643 | 0 | } |
644 | 32.9k | } else { |
645 | 32.9k | width = 0; |
646 | 35.0k | while ((c >= '0') && (c <= '9')) { |
647 | 2.06k | width = (width * 10) + (c - '0'); |
648 | 2.06k | c = *fmt++; |
649 | 2.06k | } |
650 | 32.9k | } |
651 | 32.9k | |
652 | 32.9k | // precision |
653 | 32.9k | prec = -1; |
654 | 32.9k | if (c == '.') { |
655 | 0 | c = *fmt++; |
656 | 0 | if (c == '*') { |
657 | 0 | c = *fmt++; |
658 | 0 | prec = va_arg(ap, int); |
659 | 0 | } else { |
660 | 0 | prec = 0; |
661 | 0 | while ((c >= '0') && (c <= '9')) { |
662 | 0 | prec = (prec * 10) + (c - '0'); |
663 | 0 | c = *fmt++; |
664 | 0 | } |
665 | 0 | } |
666 | 0 | } |
667 | 32.9k | |
668 | 32.9k | // size |
669 | 32.9k | type = TYPE_INTN; |
670 | 32.9k | if (c == 'h') { |
671 | 0 | type = TYPE_SHORT; |
672 | 0 | c = *fmt++; |
673 | 32.9k | } else if (c == 'L') { |
674 | 0 | type = TYPE_LONGLONG; |
675 | 0 | c = *fmt++; |
676 | 32.9k | } else if (c == 'l') { |
677 | 4.34k | type = TYPE_LONG; |
678 | 4.34k | c = *fmt++; |
679 | 4.34k | if (c == 'l') { |
680 | 4.13k | type = TYPE_LONGLONG; |
681 | 4.13k | c = *fmt++; |
682 | 4.13k | } |
683 | 28.6k | } else if (c == 'z' || c == 'I') { |
684 | 0 | static_assert(sizeof(size_t) == sizeof(int) || sizeof(size_t) == sizeof(long) || |
685 | 0 | sizeof(size_t) == sizeof(long long), |
686 | 0 | "size_t is not one of the expected sizes"); |
687 | 0 | type = sizeof(size_t) == sizeof(int) ? TYPE_INTN : |
688 | 0 | sizeof(size_t) == sizeof(long) ? TYPE_LONG : TYPE_LONGLONG; |
689 | 0 | c = *fmt++; |
690 | 0 | } |
691 | 32.9k | |
692 | 32.9k | // format |
693 | 32.9k | hexp = hex; |
694 | 32.9k | switch (c) { |
695 | 32.9k | case 'd': case 'i': // decimal/integer |
696 | 16.4k | radix = 10; |
697 | 16.4k | goto fetch_and_convert; |
698 | 16.4k | |
699 | 16.4k | case 'o': // octal |
700 | 0 | radix = 8; |
701 | 0 | type |= 1; |
702 | 0 | goto fetch_and_convert; |
703 | 16.4k | |
704 | 16.4k | case 'u': // unsigned decimal |
705 | 4.49k | radix = 10; |
706 | 4.49k | type |= 1; |
707 | 4.49k | goto fetch_and_convert; |
708 | 16.4k | |
709 | 16.4k | case 'x': // unsigned hex |
710 | 0 | radix = 16; |
711 | 0 | type |= 1; |
712 | 0 | goto fetch_and_convert; |
713 | 16.4k | |
714 | 16.4k | case 'X': // unsigned HEX |
715 | 0 | radix = 16; |
716 | 0 | hexp = HEX; |
717 | 0 | type |= 1; |
718 | 0 | goto fetch_and_convert; |
719 | 20.9k | |
720 | 20.9k | fetch_and_convert: |
721 | 20.9k | switch (type) { |
722 | 20.9k | case TYPE_SHORT: |
723 | 0 | u.l = va_arg(ap, int); |
724 | 0 | if (u.l < 0) { |
725 | 0 | u.l = -u.l; |
726 | 0 | flags |= FLAG_NEG; |
727 | 0 | } |
728 | 0 | goto do_long; |
729 | 20.9k | case TYPE_USHORT: |
730 | 0 | u.l = (unsigned short) va_arg(ap, unsigned int); |
731 | 0 | goto do_long; |
732 | 20.9k | case TYPE_INTN: |
733 | 16.4k | u.l = va_arg(ap, int); |
734 | 16.4k | if (u.l < 0) { |
735 | 0 | u.l = -u.l; |
736 | 0 | flags |= FLAG_NEG; |
737 | 0 | } |
738 | 16.4k | goto do_long; |
739 | 20.9k | case TYPE_UINTN: |
740 | 154 | u.l = (long)va_arg(ap, unsigned int); |
741 | 154 | goto do_long; |
742 | 20.9k | |
743 | 20.9k | case TYPE_LONG: |
744 | 2 | u.l = va_arg(ap, long); |
745 | 2 | if (u.l < 0) { |
746 | 0 | u.l = -u.l; |
747 | 0 | flags |= FLAG_NEG; |
748 | 0 | } |
749 | 2 | goto do_long; |
750 | 16.8k | case TYPE_ULONG: |
751 | 16.8k | u.l = (long)va_arg(ap, unsigned long); |
752 | 16.8k | do_long: |
753 | 16.8k | if (!cvt_l(u.l, width, prec, radix, type, flags, hexp)) |
754 | 0 | return false; |
755 | 16.8k | |
756 | 16.8k | break; |
757 | 16.8k | |
758 | 16.8k | case TYPE_LONGLONG: |
759 | 0 | u.ll = va_arg(ap, long long); |
760 | 0 | if (u.ll < 0) { |
761 | 0 | u.ll = -u.ll; |
762 | 0 | flags |= FLAG_NEG; |
763 | 0 | } |
764 | 0 | goto do_longlong; |
765 | 16.8k | case TYPE_POINTER: |
766 | 0 | u.ll = (uintptr_t)va_arg(ap, void*); |
767 | 0 | goto do_longlong; |
768 | 4.13k | case TYPE_ULONGLONG: |
769 | 4.13k | u.ll = va_arg(ap, unsigned long long); |
770 | 4.13k | do_longlong: |
771 | 4.13k | if (!cvt_ll(u.ll, width, prec, radix, type, flags, hexp)) |
772 | 0 | return false; |
773 | 4.13k | |
774 | 4.13k | break; |
775 | 20.9k | } |
776 | 20.9k | break; |
777 | 20.9k | |
778 | 20.9k | case 'e': |
779 | 9 | case 'E': |
780 | 9 | case 'f': |
781 | 9 | case 'g': |
782 | 9 | u.d = va_arg(ap, double); |
783 | 9 | if (!nas.empty()) { |
784 | 0 | i = fmt - dolPt; |
785 | 0 | if (i < int(sizeof(pattern))) { |
786 | 0 | pattern[0] = '%'; |
787 | 0 | memcpy(&pattern[1], dolPt, size_t(i)); |
788 | 0 | if (!cvt_f(u.d, pattern, &pattern[i + 1])) |
789 | 0 | return false; |
790 | 9 | } |
791 | 9 | } else { |
792 | 9 | if (!cvt_f(u.d, fmt0, fmt)) |
793 | 0 | return false; |
794 | 9 | } |
795 | 9 | |
796 | 9 | break; |
797 | 9 | |
798 | 9 | case 'c': |
799 | 0 | if ((flags & FLAG_LEFT) == 0) { |
800 | 0 | while (width-- > 1) { |
801 | 0 | if (!emit(" ", 1)) |
802 | 0 | return false; |
803 | 0 | } |
804 | 0 | } |
805 | 0 | switch (type) { |
806 | 0 | case TYPE_SHORT: |
807 | 0 | case TYPE_INTN: |
808 | 0 | u.ch = va_arg(ap, int); |
809 | 0 | if (!emit(&u.ch, 1)) |
810 | 0 | return false; |
811 | 0 | break; |
812 | 0 | } |
813 | 0 | if (flags & FLAG_LEFT) { |
814 | 0 | while (width-- > 1) { |
815 | 0 | if (!emit(" ", 1)) |
816 | 0 | return false; |
817 | 0 | } |
818 | 0 | } |
819 | 0 | break; |
820 | 0 |
|
821 | 0 | case 'p': |
822 | 0 | type = TYPE_POINTER; |
823 | 0 | radix = 16; |
824 | 0 | goto fetch_and_convert; |
825 | 0 |
|
826 | 12.0k | case 's': |
827 | 12.0k | if (type == TYPE_INTN) { |
828 | 12.0k | u.s = va_arg(ap, const char*); |
829 | 12.0k | if (!cvt_s(u.s, width, prec, flags)) |
830 | 0 | return false; |
831 | 12.0k | break; |
832 | 12.0k | } |
833 | 0 | MOZ_ASSERT(type == TYPE_LONG); |
834 | 0 | MOZ_FALLTHROUGH; |
835 | 0 | case 'S': |
836 | | #if defined(XP_WIN) |
837 | | { |
838 | | u.ws = va_arg(ap, const wchar_t*); |
839 | | |
840 | | int rv = WideCharToMultiByte(CP_ACP, 0, u.ws, -1, NULL, 0, NULL, NULL); |
841 | | if (rv == 0 && GetLastError() == ERROR_NO_UNICODE_TRANSLATION) { |
842 | | if (!cvt_s("<unicode errors in string>", width, prec, flags)) { |
843 | | return false; |
844 | | } |
845 | | } else { |
846 | | if (rv == 0) { |
847 | | rv = 1; |
848 | | } |
849 | | UniqueFreePtr<char[]> buf((char*)malloc(rv)); |
850 | | WideCharToMultiByte(CP_ACP, 0, u.ws, -1, buf.get(), rv, NULL, NULL); |
851 | | buf[rv - 1] = '\0'; |
852 | | |
853 | | if (!cvt_s(buf.get(), width, prec, flags)) { |
854 | | return false; |
855 | | } |
856 | | } |
857 | | } |
858 | | #else |
859 | | // Not supported here. |
860 | 0 | MOZ_ASSERT(0); |
861 | 0 | #endif |
862 | 0 | break; |
863 | 0 |
|
864 | 0 | case 'n': |
865 | 0 | u.ip = va_arg(ap, int*); |
866 | 0 | if (u.ip) { |
867 | 0 | *u.ip = mEmitted; |
868 | 0 | } |
869 | 0 | break; |
870 | 0 |
|
871 | 0 | default: |
872 | 0 | // Not a % token after all... skip it |
873 | 0 | if (!emit("%", 1)) |
874 | 0 | return false; |
875 | 0 | if (!emit(fmt - 1, 1)) |
876 | 0 | return false; |
877 | 32.9k | } |
878 | 32.9k | } |
879 | 18.9k | |
880 | 18.9k | return true; |
881 | 18.9k | } |
882 | | |
883 | | /************************************************************************/ |
884 | | |
885 | | bool |
886 | | mozilla::PrintfTarget::print(const char* format, ...) |
887 | 0 | { |
888 | 0 | va_list ap; |
889 | 0 |
|
890 | 0 | va_start(ap, format); |
891 | 0 | bool result = vprint(format, ap); |
892 | 0 | va_end(ap); |
893 | 0 | return result; |
894 | 0 | } |
895 | | |
896 | | #undef TYPE_SHORT |
897 | | #undef TYPE_USHORT |
898 | | #undef TYPE_INTN |
899 | | #undef TYPE_UINTN |
900 | | #undef TYPE_LONG |
901 | | #undef TYPE_ULONG |
902 | | #undef TYPE_LONGLONG |
903 | | #undef TYPE_ULONGLONG |
904 | | #undef TYPE_STRING |
905 | | #undef TYPE_DOUBLE |
906 | | #undef TYPE_INTSTR |
907 | | #undef TYPE_POINTER |
908 | | #undef TYPE_WSTRING |
909 | | #undef TYPE_UNKNOWN |
910 | | |
911 | | #undef FLAG_LEFT |
912 | | #undef FLAG_SIGNED |
913 | | #undef FLAG_SPACED |
914 | | #undef FLAG_ZEROS |
915 | | #undef FLAG_NEG |