]> git.zerfleddert.de Git - proxmark3-svn/blame - armsrc/printf.c
More CRLF -> LF
[proxmark3-svn] / armsrc / printf.c
CommitLineData
6f5cb60c 1/*-
2 * Copyright (c) 1986, 1988, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 4. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 *
34 * @(#)subr_prf.c 8.3 (Berkeley) 1/21/94
35 */
36
37typedef unsigned long size_t;
38typedef long ssize_t;
39#ifdef __64BIT__
40typedef unsigned long long uintmax_t;
41typedef long long intmax_t;
42#else
43typedef unsigned int uintmax_t;
44typedef int intmax_t;
45#endif
46typedef unsigned char u_char;
47typedef unsigned int u_int;
48typedef unsigned long u_long;
49typedef unsigned short u_short;
50typedef unsigned long long u_quad_t;
51typedef long long quad_t;
52typedef unsigned long uintptr_t;
53typedef long ptrdiff_t;
54#define NULL ((void*)0)
55#define NBBY 8 /* number of bits in a byte */
56char const hex2ascii_data[] = "0123456789abcdefghijklmnopqrstuvwxyz";
57#define hex2ascii(hex) (hex2ascii_data[hex])
58#define va_list __builtin_va_list
59#define va_start __builtin_va_start
60#define va_arg __builtin_va_arg
61#define va_end __builtin_va_end
62#define toupper(c) ((c) - 0x20 * (((c) >= 'a') && ((c) <= 'z')))
63static size_t
64strlen(const char *s)
65{
66 size_t l = 0;
67 while (*s++)
68 l++;
69 return l;
70}
71
72/* Max number conversion buffer length: a u_quad_t in base 2, plus NUL byte. */
73#define MAXNBUF (sizeof(intmax_t) * NBBY + 1)
74
75/*
76 * Put a NUL-terminated ASCII number (base <= 36) in a buffer in reverse
77 * order; return an optional length and a pointer to the last character
78 * written in the buffer (i.e., the first character of the string).
79 * The buffer pointed to by `nbuf' must have length >= MAXNBUF.
80 */
81static char *
82ksprintn(char *nbuf, uintmax_t num, int base, int *lenp, int upper)
83{
84 char *p, c;
85
86 p = nbuf;
87 *p = '\0';
88 do {
89 c = hex2ascii(num % base);
90 *++p = upper ? toupper(c) : c;
91 } while (num /= base);
92 if (lenp)
93 *lenp = p - nbuf;
94 return (p);
95}
96
97/*
98 * Scaled down version of printf(3).
99 *
100 * Two additional formats:
101 *
102 * The format %b is supported to decode error registers.
103 * Its usage is:
104 *
105 * printf("reg=%b\n", regval, "*");
106 *
107 * where is the output base expressed as a control character, e.g.
108 * \10 gives octal; \20 gives hex. Each arg is a sequence of characters,
109 * the first of which gives the bit number to be inspected (origin 1), and
110 * the next characters (up to a control character, i.e. a character <= 32),
111 * give the name of the register. Thus:
112 *
113 * kvprintf("reg=%b\n", 3, "\10\2BITTWO\1BITONE\n");
114 *
115 * would produce output:
116 *
117 * reg=3
118 *
119 * XXX: %D -- Hexdump, takes pointer and separator string:
120 * ("%6D", ptr, ":") -> XX:XX:XX:XX:XX:XX
121 * ("%*D", len, ptr, " " -> XX XX XX XX ...
122 */
123int
124kvsprintf(char const *fmt, void *arg, int radix, va_list ap)
125{
126#define PCHAR(c) {int cc=(c); *d++ = cc; retval++; }
127 char nbuf[MAXNBUF];
128 char *d;
129 const char *p, *percent, *q;
130 u_char *up;
131 int ch, n;
132 uintmax_t num;
133 int base, lflag, qflag, tmp, width, ladjust, sharpflag, neg, sign, dot;
134 int cflag, hflag, jflag, tflag, zflag;
135 int dwidth, upper;
136 char padc;
137 int stop = 0, retval = 0;
138
139 num = 0;
140 d = (char *) arg;
141
142 if (fmt == NULL)
143 fmt = "(fmt null)\n";
144
145 if (radix < 2 || radix > 36)
146 radix = 10;
147
148 for (;;) {
149 padc = ' ';
150 width = 0;
151 while ((ch = (u_char)*fmt++) != '%' || stop) {
152 PCHAR(ch);
153 if (ch == '\0')
154 return (retval);
155 }
156 percent = fmt - 1;
157 qflag = 0; lflag = 0; ladjust = 0; sharpflag = 0; neg = 0;
158 sign = 0; dot = 0; dwidth = 0; upper = 0;
159 cflag = 0; hflag = 0; jflag = 0; tflag = 0; zflag = 0;
160reswitch: switch (ch = (u_char)*fmt++) {
161 case '.':
162 dot = 1;
163 goto reswitch;
164 case '#':
165 sharpflag = 1;
166 goto reswitch;
167 case '+':
168 sign = 1;
169 goto reswitch;
170 case '-':
171 ladjust = 1;
172 goto reswitch;
173 case '%':
174 PCHAR(ch);
175 break;
176 case '*':
177 if (!dot) {
178 width = va_arg(ap, int);
179 if (width < 0) {
180 ladjust = !ladjust;
181 width = -width;
182 }
183 } else {
184 dwidth = va_arg(ap, int);
185 }
186 goto reswitch;
187 case '0':
188 if (!dot) {
189 padc = '0';
190 goto reswitch;
191 }
192 case '1': case '2': case '3': case '4':
193 case '5': case '6': case '7': case '8': case '9':
194 for (n = 0;; ++fmt) {
195 n = n * 10 + ch - '0';
196 ch = *fmt;
197 if (ch < '0' || ch > '9')
198 break;
199 }
200 if (dot)
201 dwidth = n;
202 else
203 width = n;
204 goto reswitch;
205 case 'b':
206 num = (u_int)va_arg(ap, int);
207 p = va_arg(ap, char *);
208 for (q = ksprintn(nbuf, num, *p++, NULL, 0); *q;)
209 PCHAR(*q--);
210
211 if (num == 0)
212 break;
213
214 for (tmp = 0; *p;) {
215 n = *p++;
216 if (num & (1 << (n - 1))) {
217 PCHAR(tmp ? ',' : '<');
218 for (; (n = *p) > ' '; ++p)
219 PCHAR(n);
220 tmp = 1;
221 } else
222 for (; *p > ' '; ++p)
223 continue;
224 }
225 if (tmp)
226 PCHAR('>');
227 break;
228 case 'c':
229 PCHAR(va_arg(ap, int));
230 break;
231 case 'D':
232 up = va_arg(ap, u_char *);
233 p = va_arg(ap, char *);
234 if (!width)
235 width = 16;
236 while(width--) {
237 PCHAR(hex2ascii(*up >> 4));
238 PCHAR(hex2ascii(*up & 0x0f));
239 up++;
240 if (width)
241 for (q=p;*q;q++)
242 PCHAR(*q);
243 }
244 break;
245 case 'd':
246 case 'i':
247 base = 10;
248 sign = 1;
249 goto handle_sign;
250 case 'h':
251 if (hflag) {
252 hflag = 0;
253 cflag = 1;
254 } else
255 hflag = 1;
256 goto reswitch;
257 case 'j':
258 jflag = 1;
259 goto reswitch;
260 case 'l':
261 if (lflag) {
262 lflag = 0;
263 qflag = 1;
264 } else
265 lflag = 1;
266 goto reswitch;
267 case 'n':
268 if (jflag)
269 *(va_arg(ap, intmax_t *)) = retval;
270 else if (qflag)
271 *(va_arg(ap, quad_t *)) = retval;
272 else if (lflag)
273 *(va_arg(ap, long *)) = retval;
274 else if (zflag)
275 *(va_arg(ap, size_t *)) = retval;
276 else if (hflag)
277 *(va_arg(ap, short *)) = retval;
278 else if (cflag)
279 *(va_arg(ap, char *)) = retval;
280 else
281 *(va_arg(ap, int *)) = retval;
282 break;
283 case 'o':
284 base = 8;
285 goto handle_nosign;
286 case 'p':
287 base = 16;
288 sharpflag = (width == 0);
289 sign = 0;
290 num = (uintptr_t)va_arg(ap, void *);
291 goto number;
292 case 'q':
293 qflag = 1;
294 goto reswitch;
295 case 'r':
296 base = radix;
297 if (sign)
298 goto handle_sign;
299 goto handle_nosign;
300 case 's':
301 p = va_arg(ap, char *);
302 if (p == NULL)
303 p = "(null)";
304 if (!dot)
305 n = strlen (p);
306 else
307 for (n = 0; n < dwidth && p[n]; n++)
308 continue;
309
310 width -= n;
311
312 if (!ladjust && width > 0)
313 while (width--)
314 PCHAR(padc);
315 while (n--)
316 PCHAR(*p++);
317 if (ladjust && width > 0)
318 while (width--)
319 PCHAR(padc);
320 break;
321 case 't':
322 tflag = 1;
323 goto reswitch;
324 case 'u':
325 base = 10;
326 goto handle_nosign;
327 case 'X':
328 upper = 1;
329 case 'x':
330 base = 16;
331 goto handle_nosign;
332 case 'y':
333 base = 16;
334 sign = 1;
335 goto handle_sign;
336 case 'z':
337 zflag = 1;
338 goto reswitch;
339handle_nosign:
340 sign = 0;
341 if (jflag)
342 num = va_arg(ap, uintmax_t);
343 else if (qflag)
344 num = va_arg(ap, u_quad_t);
345 else if (tflag)
346 num = va_arg(ap, ptrdiff_t);
347 else if (lflag)
348 num = va_arg(ap, u_long);
349 else if (zflag)
350 num = va_arg(ap, size_t);
351 else if (hflag)
352 num = (u_short)va_arg(ap, int);
353 else if (cflag)
354 num = (u_char)va_arg(ap, int);
355 else
356 num = va_arg(ap, u_int);
357 goto number;
358handle_sign:
359 if (jflag)
360 num = va_arg(ap, intmax_t);
361 else if (qflag)
362 num = va_arg(ap, quad_t);
363 else if (tflag)
364 num = va_arg(ap, ptrdiff_t);
365 else if (lflag)
366 num = va_arg(ap, long);
367 else if (zflag)
368 num = va_arg(ap, ssize_t);
369 else if (hflag)
370 num = (short)va_arg(ap, int);
371 else if (cflag)
372 num = (char)va_arg(ap, int);
373 else
374 num = va_arg(ap, int);
375number:
376 if (sign && (intmax_t)num < 0) {
377 neg = 1;
378 num = -(intmax_t)num;
379 }
380 p = ksprintn(nbuf, num, base, &tmp, upper);
381 if (sharpflag && num != 0) {
382 if (base == 8)
383 tmp++;
384 else if (base == 16)
385 tmp += 2;
386 }
387 if (neg)
388 tmp++;
389
390 if (!ladjust && padc != '0' && width
391 && (width -= tmp) > 0)
392 while (width--)
393 PCHAR(padc);
394 if (neg)
395 PCHAR('-');
396 if (sharpflag && num != 0) {
397 if (base == 8) {
398 PCHAR('0');
399 } else if (base == 16) {
400 PCHAR('0');
401 PCHAR('x');
402 }
403 }
404 if (!ladjust && width && (width -= tmp) > 0)
405 while (width--)
406 PCHAR(padc);
407
408 while (*p)
409 PCHAR(*p--);
410
411 if (ladjust && width && (width -= tmp) > 0)
412 while (width--)
413 PCHAR(padc);
414
415 break;
416 default:
417 while (percent < fmt)
418 PCHAR(*percent++);
419 /*
420 * Since we ignore an formatting argument it is no
421 * longer safe to obey the remaining formatting
422 * arguments as the arguments will no longer match
423 * the format specs.
424 */
425 stop = 1;
426 break;
427 }
428 }
429 PCHAR(0);
430 return retval;
431#undef PCHAR
432}
433
434int
435sprintf(char *dest, const char *fmt, ...)
436{
437 /* http://www.pagetable.com/?p=298 */
438 int retval;
439 va_list ap;
440
441 va_start(ap, fmt);
442 retval = kvsprintf(fmt, dest, 10, ap);
443 va_end(ap);
444 return retval;
445}
Impressum, Datenschutz