1 //-----------------------------------------------------------------------------
2 // Borrowed initially from subr_prf.c 8.3 (Berkeley) 1/21/94
3 // Copyright (c) 1986, 1988, 1991, 1993
4 // The Regents of the University of California. All rights reserved.
5 // (c) UNIX System Laboratories, Inc.
6 // All or some portions of this file are derived from material licensed
7 // to the University of California by American Telephone and Telegraph
8 // Co. or Unix System Laboratories, Inc. and are reproduced herein with
9 // the permission of UNIX System Laboratories, Inc.
10 // Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
12 // This program is free software: you can redistribute it and/or modify
13 // it under the terms of the GNU General Public License as published by
14 // the Free Software Foundation, either version 3 of the License, or
15 // (at your option) any later version.
17 // This program is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU General Public License for more details.
22 // See LICENSE.txt for the text of the license.
23 //-----------------------------------------------------------------------------
27 typedef unsigned char u_char
;
28 typedef unsigned int u_int
;
29 typedef unsigned long long u_quad_t
;
30 typedef long long quad_t
;
31 typedef unsigned long u_long
;
32 typedef unsigned short u_short
;
35 #define NBBY 8 /* number of bits in a byte */
37 static char const hex2ascii_data
[] = "0123456789abcdefghijklmnopqrstuvwxyz";
38 #define hex2ascii(hex) (hex2ascii_data[hex])
39 #define toupper(c) ((c) - 0x20 * (((c) >= 'a') && ((c) <= 'z')))
41 /* Max number conversion buffer length: a u_quad_t in base 2, plus NUL byte. */
42 #define MAXNBUF (sizeof(intmax_t) * NBBY + 1)
45 * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse
46 * order; return an optional length and a pointer to the last character
47 * written in the buffer (i.e., the first character of the string).
48 * The buffer pointed to by `nbuf' must have length >= MAXNBUF.
51 ksprintn(char *nbuf
, uintmax_t num
, int base
, int *lenp
, int upper
) {
55 char c
= hex2ascii(num
% base
);
56 *++p
= upper
? toupper(c
) : c
;
57 } while (num
/= base
);
64 * Scaled down version of printf(3).
66 * Two additional formats:
68 * The format %b is supported to decode error registers.
71 * printf("reg=%b\n", regval, "*");
73 * where is the output base expressed as a control character, e.g.
74 * \10 gives octal; \20 gives hex. Each arg is a sequence of characters,
75 * the first of which gives the bit number to be inspected (origin 1), and
76 * the next characters (up to a control character, i.e. a character <= 32),
77 * give the name of the register. Thus:
79 * kvprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
81 * would produce output:
85 * XXX: %D -- Hexdump, takes pointer and separator string:
86 * ("%6D", ptr, ":") -> XX:XX:XX:XX:XX:XX
87 * ("%*D", len, ptr, " " -> XX XX XX XX ...
90 kvsprintf(char const *fmt
, void *arg
, int radix
, va_list ap
) {
91 #define PCHAR(c) {int cc=(c); *d++ = cc; retval++; }
94 const char *p
, *percent
, *q
;
98 int base
, lflag
, qflag
, tmp
, width
, ladjust
, sharpflag
, neg
, sign
, dot
;
99 int cflag
, hflag
, jflag
, tflag
, zflag
;
102 int stop
= 0, retval
= 0;
108 fmt
= "(fmt null)\n";
110 if (radix
< 2 || radix
> 36)
117 while ((ch
= (u_char
) * fmt
++) != '%' || stop
) {
140 switch (ch
= (u_char
) * fmt
++) {
158 width
= va_arg(ap
, int);
164 dwidth
= va_arg(ap
, int);
181 for (n
= 0;; ++fmt
) {
182 n
= n
* 10 + ch
- '0';
184 if (ch
< '0' || ch
> '9') {
195 num
= (u_int
)va_arg(ap
, int);
196 p
= va_arg(ap
, char *);
197 for (q
= ksprintn(nbuf
, num
, *p
++, NULL
, 0); *q
;) {
207 if (num
& (1 << (n
- 1))) {
208 PCHAR(tmp
? ',' : '<');
209 for (; (n
= *p
) > ' '; ++p
) {
214 for (; *p
> ' '; ++p
) {};
223 PCHAR(va_arg(ap
, int));
226 up
= va_arg(ap
, u_char
*);
227 p
= va_arg(ap
, char *);
233 PCHAR(hex2ascii(*up
>> 4));
234 PCHAR(hex2ascii(*up
& 0x0f));
237 for (q
= p
; *q
; q
++) {
269 *(va_arg(ap
, intmax_t *)) = retval
;
271 *(va_arg(ap
, quad_t
*)) = retval
;
273 *(va_arg(ap
, long *)) = retval
;
275 *(va_arg(ap
, size_t *)) = retval
;
277 *(va_arg(ap
, short *)) = retval
;
279 *(va_arg(ap
, char *)) = retval
;
281 *(va_arg(ap
, int *)) = retval
;
288 sharpflag
= (width
== 0);
290 num
= (uintptr_t)va_arg(ap
, void *);
302 p
= va_arg(ap
, char *);
310 for (n
= 0; n
< dwidth
&& p
[n
]; n
++) {};
315 if (!ladjust
&& width
> 0) {
324 if (ladjust
&& width
> 0) {
351 num
= va_arg(ap
, uintmax_t);
353 num
= va_arg(ap
, u_quad_t
);
355 num
= va_arg(ap
, ptrdiff_t);
357 num
= va_arg(ap
, u_long
);
359 num
= va_arg(ap
, size_t);
361 num
= (u_short
)va_arg(ap
, int);
363 num
= (u_char
)va_arg(ap
, int);
365 num
= va_arg(ap
, u_int
);
369 num
= va_arg(ap
, intmax_t);
371 num
= va_arg(ap
, quad_t
);
373 num
= va_arg(ap
, ptrdiff_t);
375 num
= va_arg(ap
, long);
377 num
= va_arg(ap
, ssize_t
);
379 num
= (short)va_arg(ap
, int);
381 num
= (char)va_arg(ap
, int);
383 num
= va_arg(ap
, int);
385 if (sign
&& (intmax_t)num
< 0) {
387 num
= -(intmax_t)num
;
390 p
= ksprintn(nbuf
, num
, base
, &tmp
, upper
);
392 if (sharpflag
&& num
!= 0) {
395 } else if (base
== 16) {
404 if (!ladjust
&& padc
!= '0' && width
&& (width
-= tmp
) > 0) {
414 if (sharpflag
&& num
!= 0) {
417 } else if (base
== 16) {
423 if (!ladjust
&& width
&& (width
-= tmp
) > 0) {
433 if (ladjust
&& width
&& (width
-= tmp
) > 0) {
441 while (percent
< fmt
) {
445 * Since we ignore an formatting argument it is no
446 * longer safe to obey the remaining formatting
447 * arguments as the arguments will no longer match
459 int vsprintf(char *dest
, const char *fmt
, va_list ap
) {
460 return kvsprintf(fmt
, dest
, 10, ap
);
464 sprintf(char *dest
, const char *fmt
, ...) {
465 /* http://www.pagetable.com/?p=298 */
469 retval
= kvsprintf(fmt
, dest
, 10, ap
);