• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 ///////////////////////////////////////////////////////////////////////////////
2 // \author (c) Marco Paland (info@paland.com)
3 //             2014-2019, PALANDesign Hannover, Germany
4 //
5 // \license The MIT License (MIT)
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a copy
8 // of this software and associated documentation files (the "Software"), to deal
9 // in the Software without restriction, including without limitation the rights
10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 // copies of the Software, and to permit persons to whom the Software is
12 // furnished to do so, subject to the following conditions:
13 //
14 // The above copyright notice and this permission notice shall be included in
15 // all copies or substantial portions of the Software.
16 //
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 // THE SOFTWARE.
24 //
25 // \brief Tiny printf, sprintf and (v)snprintf implementation, optimized for speed on
26 //        embedded systems with a very limited resources. These routines are thread
27 //        safe and reentrant!
28 //        Use this instead of the bloated standard/newlib printf cause these use
29 //        malloc for printf (and may not be thread safe).
30 //
31 ///////////////////////////////////////////////////////////////////////////////
32 #include <stdbool.h>
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <stddef.h>
36 #include "printf_uart.h"
37 
38 // 'ntoa' conversion buffer size, this must be big enough to hold one converted
39 // numeric number including padded zeros (dynamically created on stack)
40 // default: 32 byte
41 #ifndef PRINTF_NTOA_BUFFER_SIZE
42 #define PRINTF_NTOA_BUFFER_SIZE    32U
43 #endif
44 
45 // 'ftoa' conversion buffer size, this must be big enough to hold one converted
46 // float number including padded zeros (dynamically created on stack)
47 // default: 32 byte
48 #ifndef PRINTF_FTOA_BUFFER_SIZE
49 #define PRINTF_FTOA_BUFFER_SIZE    32U
50 #endif
51 
52 // support for the floating point type (%f)
53 // default: activated
54 #ifndef PRINTF_DISABLE_SUPPORT_FLOAT
55 #define PRINTF_SUPPORT_FLOAT
56 #endif
57 
58 // support for exponential floating point notation (%e/%g)
59 // default: activated
60 #ifndef PRINTF_DISABLE_SUPPORT_EXPONENTIAL
61 #define PRINTF_SUPPORT_EXPONENTIAL
62 #endif
63 
64 // define the default floating point precision
65 // default: 6 digits
66 #ifndef PRINTF_DEFAULT_FLOAT_PRECISION
67 #define PRINTF_DEFAULT_FLOAT_PRECISION  6U
68 #endif
69 
70 // define the largest float suitable to print with %f
71 // default: 1e9
72 #ifndef PRINTF_MAX_FLOAT
73 #define PRINTF_MAX_FLOAT  1e9
74 #endif
75 
76 // support for the long long types (%llu or %p)
77 // default: activated
78 #ifndef PRINTF_DISABLE_SUPPORT_LONG_LONG
79 #define PRINTF_SUPPORT_LONG_LONG
80 #endif
81 
82 // support for the ptrdiff_t type (%t)
83 // ptrdiff_t is normally defined in <stddef.h> as long or long long type
84 // default: activated
85 #ifndef PRINTF_DISABLE_SUPPORT_PTRDIFF_T
86 #define PRINTF_SUPPORT_PTRDIFF_T
87 #endif
88 
89 ///////////////////////////////////////////////////////////////////////////////
90 
91 // internal flag definitions
92 #define FLAGS_ZEROPAD   (1U <<  0U)
93 #define FLAGS_LEFT      (1U <<  1U)
94 #define FLAGS_PLUS      (1U <<  2U)
95 #define FLAGS_SPACE     (1U <<  3U)
96 #define FLAGS_HASH      (1U <<  4U)
97 #define FLAGS_UPPERCASE (1U <<  5U)
98 #define FLAGS_CHAR      (1U <<  6U)
99 #define FLAGS_SHORT     (1U <<  7U)
100 #define FLAGS_LONG      (1U <<  8U)
101 #define FLAGS_LONG_LONG (1U <<  9U)
102 #define FLAGS_PRECISION (1U << 10U)
103 #define FLAGS_ADAPT_EXP (1U << 11U)
104 
105 // import float.h for DBL_MAX
106 #if defined(PRINTF_SUPPORT_FLOAT)
107 #include <float.h>
108 #endif
109 
110 // output function type
111 typedef void (*out_fct_type)(char character, void *buffer, size_t idx, size_t maxlen);
112 
113 // wrapper (used as buffer) for output function type
114 typedef struct {
115     void  (*fct)(char character, void *arg);
116     void *arg;
117 } out_fct_wrap_type;
118 
119 // internal buffer output
_out_buffer(char character,void * buffer,size_t idx,size_t maxlen)120 static inline void _out_buffer(char character, void *buffer, size_t idx, size_t maxlen)
121 {
122     if (idx < maxlen) {
123         ((char *)buffer)[idx] = character;
124     }
125 }
126 
127 // internal null output
_out_null(char character,void * buffer,size_t idx,size_t maxlen)128 static inline void _out_null(char character, void *buffer, size_t idx, size_t maxlen)
129 {
130     (void)character;
131     (void)buffer;
132     (void)idx;
133     (void)maxlen;
134 }
135 
136 // internal _putchar wrapper
_out_char(char character,void * buffer,size_t idx,size_t maxlen)137 static inline void _out_char(char character, void *buffer, size_t idx, size_t maxlen)
138 {
139     (void)buffer;
140     (void)idx;
141     (void)maxlen;
142     if (character) {
143         uart_put_char(character);
144     }
145 }
146 
147 // internal output function wrapper
_out_fct(char character,void * buffer,size_t idx,size_t maxlen)148 static inline void _out_fct(char character, void *buffer, size_t idx, size_t maxlen)
149 {
150     (void)idx;
151     (void)maxlen;
152     if (character) {
153         // buffer is the output fct pointer
154         ((out_fct_wrap_type *)buffer)->fct(character, ((out_fct_wrap_type *)buffer)->arg);
155     }
156 }
157 
158 // internal secure strlen
159 // \return The length of the string (excluding the terminating 0) limited by 'maxsize'
_strnlen_s(const char * str,size_t maxsize)160 static inline unsigned int _strnlen_s(const char *str, size_t maxsize)
161 {
162     const char *s;
163     for (s = str; *s && maxsize--; ++s);
164     return (unsigned int)(s - str);
165 }
166 
167 // internal test if char is a digit (0-9)
168 // \return true if char is a digit
_is_digit(char ch)169 static inline bool _is_digit(char ch)
170 {
171     return (ch >= '0') && (ch <= '9');
172 }
173 
174 // internal ASCII string to unsigned int conversion
_atoi(const char ** str)175 static unsigned int _atoi(const char **str)
176 {
177     unsigned int i = 0U;
178     while (_is_digit(**str)) {
179         i = i * 10U + (unsigned int)(*((*str)++) - '0');
180     }
181     return i;
182 }
183 
184 // output the specified string in reverse, taking care of any zero-padding
_out_rev(out_fct_type out,char * buffer,size_t idx,size_t maxlen,const char * buf,size_t len,unsigned int width,unsigned int flags)185 static size_t _out_rev(out_fct_type out, char *buffer, size_t idx, size_t maxlen, const char *buf, size_t len,
186                        unsigned int width, unsigned int flags)
187 {
188     const size_t start_idx = idx;
189 
190     // pad spaces up to given width
191     if (!(flags & FLAGS_LEFT) && !(flags & FLAGS_ZEROPAD)) {
192         for (size_t i = len; i < width; i++) {
193             out(' ', buffer, idx++, maxlen);
194         }
195     }
196 
197     // reverse string
198     while (len) {
199         out(buf[--len], buffer, idx++, maxlen);
200     }
201 
202     // append pad spaces up to given width
203     if (flags & FLAGS_LEFT) {
204         while (idx - start_idx < width) {
205             out(' ', buffer, idx++, maxlen);
206         }
207     }
208 
209     return idx;
210 }
211 
212 // internal itoa format
_ntoa_format(out_fct_type out,char * buffer,size_t idx,size_t maxlen,char * buf,size_t len,bool negative,unsigned int base,unsigned int prec,unsigned int width,unsigned int flags)213 static size_t _ntoa_format(out_fct_type out, char *buffer, size_t idx, size_t maxlen, char *buf, size_t len,
214                            bool negative, unsigned int base, unsigned int prec, unsigned int width, unsigned int flags)
215 {
216     // pad leading zeros
217     if (!(flags & FLAGS_LEFT)) {
218         if (width && (flags & FLAGS_ZEROPAD) && (negative || (flags & (FLAGS_PLUS | FLAGS_SPACE)))) {
219             width--;
220         }
221         while ((len < prec) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
222             buf[len++] = '0';
223         }
224         while ((flags & FLAGS_ZEROPAD) && (len < width) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
225             buf[len++] = '0';
226         }
227     }
228 
229     // handle hash
230     if (flags & FLAGS_HASH) {
231         if (!(flags & FLAGS_PRECISION) && len && ((len == prec) || (len == width))) {
232             len--;
233             if (len && (base == 16U)) {
234                 len--;
235             }
236         }
237         if ((base == 16U) && !(flags & FLAGS_UPPERCASE) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
238             buf[len++] = 'x';
239         } else if ((base == 16U) && (flags & FLAGS_UPPERCASE) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
240             buf[len++] = 'X';
241         } else if ((base == 2U) && (len < PRINTF_NTOA_BUFFER_SIZE)) {
242             buf[len++] = 'b';
243         }
244         if (len < PRINTF_NTOA_BUFFER_SIZE) {
245             buf[len++] = '0';
246         }
247     }
248 
249     if (len < PRINTF_NTOA_BUFFER_SIZE) {
250         if (negative) {
251             buf[len++] = '-';
252         } else if (flags & FLAGS_PLUS) {
253             buf[len++] = '+';  // ignore the space if the '+' exists
254         } else if (flags & FLAGS_SPACE) {
255             buf[len++] = ' ';
256         }
257     }
258 
259     return _out_rev(out, buffer, idx, maxlen, buf, len, width, flags);
260 }
261 
262 // internal itoa for 'long' type
_ntoa_long(out_fct_type out,char * buffer,size_t idx,size_t maxlen,unsigned long value,bool negative,unsigned long base,unsigned int prec,unsigned int width,unsigned int flags)263 static size_t _ntoa_long(out_fct_type out, char *buffer, size_t idx, size_t maxlen, unsigned long value, bool negative,
264                          unsigned long base, unsigned int prec, unsigned int width, unsigned int flags)
265 {
266     char buf[PRINTF_NTOA_BUFFER_SIZE];
267     size_t len = 0U;
268 
269     // no hash for 0 values
270     if (!value) {
271         flags &= ~FLAGS_HASH;
272     }
273 
274     // write if precision != 0 and value is != 0
275     if (!(flags & FLAGS_PRECISION) || value) {
276         do {
277             const char digit = (char)(value % base);
278             buf[len++] = digit < 10 ? '0' + digit : (flags & FLAGS_UPPERCASE ? 'A' : 'a') + digit - 10;
279             value /= base;
280         } while (value && (len < PRINTF_NTOA_BUFFER_SIZE));
281     }
282 
283     return _ntoa_format(out, buffer, idx, maxlen, buf, len, negative, (unsigned int)base, prec, width, flags);
284 }
285 
286 // internal itoa for 'long long' type
287 #if defined(PRINTF_SUPPORT_LONG_LONG)
_ntoa_long_long(out_fct_type out,char * buffer,size_t idx,size_t maxlen,unsigned long long value,bool negative,unsigned long long base,unsigned int prec,unsigned int width,unsigned int flags)288 static size_t _ntoa_long_long(out_fct_type out, char *buffer, size_t idx, size_t maxlen, unsigned long long value,
289                               bool negative, unsigned long long base, unsigned int prec, unsigned int width, unsigned int flags)
290 {
291     char buf[PRINTF_NTOA_BUFFER_SIZE];
292     size_t len = 0U;
293 
294     // no hash for 0 values
295     if (!value) {
296         flags &= ~FLAGS_HASH;
297     }
298 
299     // write if precision != 0 and value is != 0
300     if (!(flags & FLAGS_PRECISION) || value) {
301         do {
302             const char digit = (char)(value % base);
303             buf[len++] = digit < 10 ? '0' + digit : (flags & FLAGS_UPPERCASE ? 'A' : 'a') + digit - 10;
304             value /= base;
305         } while (value && (len < PRINTF_NTOA_BUFFER_SIZE));
306     }
307 
308     return _ntoa_format(out, buffer, idx, maxlen, buf, len, negative, (unsigned int)base, prec, width, flags);
309 }
310 #endif  // PRINTF_SUPPORT_LONG_LONG
311 
312 #if defined(PRINTF_SUPPORT_FLOAT)
313 
314 #if defined(PRINTF_SUPPORT_EXPONENTIAL)
315 // forward declaration so that _ftoa can switch to exp notation for values > PRINTF_MAX_FLOAT
316 static size_t _etoa(out_fct_type out, char *buffer, size_t idx, size_t maxlen, double value, unsigned int prec,
317                     unsigned int width, unsigned int flags);
318 #endif
319 
320 // internal ftoa for fixed decimal floating point
_ftoa(out_fct_type out,char * buffer,size_t idx,size_t maxlen,double value,unsigned int prec,unsigned int width,unsigned int flags)321 static size_t _ftoa(out_fct_type out, char *buffer, size_t idx, size_t maxlen, double value, unsigned int prec,
322                     unsigned int width, unsigned int flags)
323 {
324     char buf[PRINTF_FTOA_BUFFER_SIZE];
325     size_t len  = 0U;
326     double diff = 0.0;
327 
328     // powers of 10
329     static const double pow10[] = { 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 };
330 
331     // test for special values
332     if (value != value) {
333         return _out_rev(out, buffer, idx, maxlen, "nan", 3, width, flags);
334     }
335     if (value < -DBL_MAX) {
336         return _out_rev(out, buffer, idx, maxlen, "fni-", 4, width, flags);
337     }
338     if (value > DBL_MAX) {
339         return _out_rev(out, buffer, idx, maxlen, (flags & FLAGS_PLUS) ? "fni+" : "fni", (flags & FLAGS_PLUS) ? 4U : 3U, width,
340                         flags);
341     }
342 
343     // test for very large values
344     // standard printf behavior is to print EVERY whole number digit -- which could be 100s of characters overflowing your buffers == bad
345     if ((value > PRINTF_MAX_FLOAT) || (value < -PRINTF_MAX_FLOAT)) {
346 #if defined(PRINTF_SUPPORT_EXPONENTIAL)
347         return _etoa(out, buffer, idx, maxlen, value, prec, width, flags);
348 #else
349         return 0U;
350 #endif
351     }
352 
353     // test for negative
354     bool negative = false;
355     if (value < 0) {
356         negative = true;
357         value = 0 - value;
358     }
359 
360     // set default precision, if not set explicitly
361     if (!(flags & FLAGS_PRECISION)) {
362         prec = PRINTF_DEFAULT_FLOAT_PRECISION;
363     }
364     // limit precision to 9, cause a prec >= 10 can lead to overflow errors
365     while ((len < PRINTF_FTOA_BUFFER_SIZE) && (prec > 9U)) {
366         buf[len++] = '0';
367         prec--;
368     }
369 
370     int whole = (int)value;
371     double tmp = (value - whole) * pow10[prec];
372     unsigned long frac = (unsigned long)tmp;
373     diff = tmp - frac;
374 
375     if (diff > 0.5) {
376         ++frac;
377         // handle rollover, e.g. case 0.99 with prec 1 is 1.0
378         if (frac >= pow10[prec]) {
379             frac = 0;
380             ++whole;
381         }
382     } else if (diff < 0.5) {
383     } else if ((frac == 0U) || (frac & 1U)) {
384         // if halfway, round up if odd OR if last digit is 0
385         ++frac;
386     }
387 
388     if (prec == 0U) {
389         diff = value - (double)whole;
390         if ((!(diff < 0.5) || (diff > 0.5)) && (whole & 1)) {
391             // exactly 0.5 and ODD, then round up
392             // 1.5 -> 2, but 2.5 -> 2
393             ++whole;
394         }
395     } else {
396         unsigned int count = prec;
397         // now do fractional part, as an unsigned number
398         while (len < PRINTF_FTOA_BUFFER_SIZE) {
399             --count;
400             buf[len++] = (char)(48U + (frac % 10U));
401             if (!(frac /= 10U)) {
402                 break;
403             }
404         }
405         // add extra 0s
406         while ((len < PRINTF_FTOA_BUFFER_SIZE) && (count-- > 0U)) {
407             buf[len++] = '0';
408         }
409         if (len < PRINTF_FTOA_BUFFER_SIZE) {
410             // add decimal
411             buf[len++] = '.';
412         }
413     }
414 
415     // do whole part, number is reversed
416     while (len < PRINTF_FTOA_BUFFER_SIZE) {
417         buf[len++] = (char)(48 + (whole % 10));
418         if (!(whole /= 10)) {
419             break;
420         }
421     }
422 
423     // pad leading zeros
424     if (!(flags & FLAGS_LEFT) && (flags & FLAGS_ZEROPAD)) {
425         if (width && (negative || (flags & (FLAGS_PLUS | FLAGS_SPACE)))) {
426             width--;
427         }
428         while ((len < width) && (len < PRINTF_FTOA_BUFFER_SIZE)) {
429             buf[len++] = '0';
430         }
431     }
432 
433     if (len < PRINTF_FTOA_BUFFER_SIZE) {
434         if (negative) {
435             buf[len++] = '-';
436         } else if (flags & FLAGS_PLUS) {
437             buf[len++] = '+';  // ignore the space if the '+' exists
438         } else if (flags & FLAGS_SPACE) {
439             buf[len++] = ' ';
440         }
441     }
442 
443     return _out_rev(out, buffer, idx, maxlen, buf, len, width, flags);
444 }
445 
446 #if defined(PRINTF_SUPPORT_EXPONENTIAL)
447 // internal ftoa variant for exponential floating-point type, contributed by Martijn Jasperse <m.jasperse@gmail.com>
_etoa(out_fct_type out,char * buffer,size_t idx,size_t maxlen,double value,unsigned int prec,unsigned int width,unsigned int flags)448 static size_t _etoa(out_fct_type out, char *buffer, size_t idx, size_t maxlen, double value, unsigned int prec,
449                     unsigned int width, unsigned int flags)
450 {
451     // check for NaN and special values
452     if ((value != value) || (value > DBL_MAX) || (value < -DBL_MAX)) {
453         return _ftoa(out, buffer, idx, maxlen, value, prec, width, flags);
454     }
455 
456     // determine the sign
457     const bool negative = value < 0;
458     if (negative) {
459         value = -value;
460     }
461 
462     // default precision
463     if (!(flags & FLAGS_PRECISION)) {
464         prec = PRINTF_DEFAULT_FLOAT_PRECISION;
465     }
466 
467     // determine the decimal exponent
468     // based on the algorithm by David Gay (https://www.ampl.com/netlib/fp/dtoa.c)
469     union {
470         uint64_t U;
471         double   F;
472     } conv;
473 
474     conv.F = value;
475     int exp2 = (int)((conv.U >> 52U) & 0x07FFU) - 1023;           // effectively log2
476     conv.U = (conv.U & ((1ULL << 52U) - 1U)) | (1023ULL << 52U);  // drop the exponent so conv.F is now in [1,2)
477     // now approximate log10 from the log2 integer part and an expansion of ln around 1.5
478     int expval = (int)(0.1760912590558 + exp2 * 0.301029995663981 + (conv.F - 1.5) * 0.289529654602168);
479     // now we want to compute 10^expval but we want to be sure it won't overflow
480     exp2 = (int)(expval * 3.321928094887362 + 0.5);
481     const double z  = expval * 2.302585092994046 - exp2 * 0.6931471805599453;
482     const double z2 = z * z;
483     conv.U = (uint64_t)(exp2 + 1023) << 52U;
484     // compute exp(z) using continued fractions, see https://en.wikipedia.org/wiki/Exponential_function#Continued_fractions_for_ex
485     conv.F *= 1 + 2 * z / (2 - z + (z2 / (6 + (z2 / (10 + z2 / 14)))));
486     // correct for rounding errors
487     if (value < conv.F) {
488         expval--;
489         conv.F /= 10;
490     }
491 
492     // the exponent format is "%+03d" and largest value is "307", so set aside 4-5 characters
493     unsigned int minwidth = ((expval < 100) && (expval > -100)) ? 4U : 5U;
494 
495     // in "%g" mode, "prec" is the number of *significant figures* not decimals
496     if (flags & FLAGS_ADAPT_EXP) {
497         // do we want to fall-back to "%f" mode?
498         if ((value >= 1e-4) && (value < 1e6)) {
499             if ((int)prec > expval) {
500                 prec = (unsigned)((int)prec - expval - 1);
501             } else {
502                 prec = 0;
503             }
504             flags |= FLAGS_PRECISION;   // make sure _ftoa respects precision
505             // no characters in exponent
506             minwidth = 0U;
507             expval   = 0;
508         } else {
509             // we use one sigfig for the whole part
510             if ((prec > 0) && (flags & FLAGS_PRECISION)) {
511                 --prec;
512             }
513         }
514     }
515 
516     // will everything fit?
517     unsigned int fwidth = width;
518     if (width > minwidth) {
519         // we didn't fall-back so subtract the characters required for the exponent
520         fwidth -= minwidth;
521     } else {
522         // not enough characters, so go back to default sizing
523         fwidth = 0U;
524     }
525     if ((flags & FLAGS_LEFT) && minwidth) {
526         // if we're padding on the right, DON'T pad the floating part
527         fwidth = 0U;
528     }
529 
530     // rescale the float value
531     if (expval) {
532         value /= conv.F;
533     }
534 
535     // output the floating part
536     const size_t start_idx = idx;
537     idx = _ftoa(out, buffer, idx, maxlen, negative ? -value : value, prec, fwidth, flags & ~FLAGS_ADAPT_EXP);
538 
539     // output the exponent part
540     if (minwidth) {
541         // output the exponential symbol
542         out((flags & FLAGS_UPPERCASE) ? 'E' : 'e', buffer, idx++, maxlen);
543         // output the exponent value
544         idx = _ntoa_long(out, buffer, idx, maxlen, (expval < 0) ? -expval : expval, expval < 0, 10, 0, minwidth - 1,
545                          FLAGS_ZEROPAD | FLAGS_PLUS);
546         // might need to right-pad spaces
547         if (flags & FLAGS_LEFT) {
548             while (idx - start_idx < width) {
549                 out(' ', buffer, idx++, maxlen);
550             }
551         }
552     }
553     return idx;
554 }
555 #endif  // PRINTF_SUPPORT_EXPONENTIAL
556 #endif  // PRINTF_SUPPORT_FLOAT
557 
558 // internal vsnprintf
_vsnprintf(out_fct_type out,char * buffer,const size_t maxlen,const char * format,va_list va)559 static int _vsnprintf(out_fct_type out, char *buffer, const size_t maxlen, const char *format, va_list va)
560 {
561     unsigned int flags, width, precision, n;
562     size_t idx = 0U;
563 
564     if (!buffer) {
565         // use null output function
566         out = _out_null;
567     }
568 
569     while (*format) {
570         // format specifier?  %[flags][width][.precision][length]
571         if (*format != '%') {
572             // no
573             out(*format, buffer, idx++, maxlen);
574             format++;
575             continue;
576         } else {
577             // yes, evaluate it
578             format++;
579         }
580 
581         // evaluate flags
582         flags = 0U;
583         do {
584             switch (*format) {
585                 case '0':
586                     flags |= FLAGS_ZEROPAD;
587                     format++;
588                     n = 1U;
589                     break;
590                 case '-':
591                     flags |= FLAGS_LEFT;
592                     format++;
593                     n = 1U;
594                     break;
595                 case '+':
596                     flags |= FLAGS_PLUS;
597                     format++;
598                     n = 1U;
599                     break;
600                 case ' ':
601                     flags |= FLAGS_SPACE;
602                     format++;
603                     n = 1U;
604                     break;
605                 case '#':
606                     flags |= FLAGS_HASH;
607                     format++;
608                     n = 1U;
609                     break;
610                 default :
611                     n = 0U;
612                     break;
613             }
614         } while (n);
615 
616         // evaluate width field
617         width = 0U;
618         if (_is_digit(*format)) {
619             width = _atoi(&format);
620         } else if (*format == '*') {
621             const int w = va_arg(va, int);
622             if (w < 0) {
623                 flags |= FLAGS_LEFT;    // reverse padding
624                 width = (unsigned int) - w;
625             } else {
626                 width = (unsigned int)w;
627             }
628             format++;
629         }
630 
631         // evaluate precision field
632         precision = 0U;
633         if (*format == '.') {
634             flags |= FLAGS_PRECISION;
635             format++;
636             if (_is_digit(*format)) {
637                 precision = _atoi(&format);
638             } else if (*format == '*') {
639                 const int prec = (int)va_arg(va, int);
640                 precision = prec > 0 ? (unsigned int)prec : 0U;
641                 format++;
642             }
643         }
644 
645         // evaluate length field
646         switch (*format) {
647             case 'l' :
648                 flags |= FLAGS_LONG;
649                 format++;
650                 if (*format == 'l') {
651                     flags |= FLAGS_LONG_LONG;
652                     format++;
653                 }
654                 break;
655             case 'h' :
656                 flags |= FLAGS_SHORT;
657                 format++;
658                 if (*format == 'h') {
659                     flags |= FLAGS_CHAR;
660                     format++;
661                 }
662                 break;
663 #if defined(PRINTF_SUPPORT_PTRDIFF_T)
664             case 't' :
665                 flags |= (sizeof(ptrdiff_t) == sizeof(long) ? FLAGS_LONG : FLAGS_LONG_LONG);
666                 format++;
667                 break;
668 #endif
669             case 'j' :
670                 flags |= (sizeof(intmax_t) == sizeof(long) ? FLAGS_LONG : FLAGS_LONG_LONG);
671                 format++;
672                 break;
673             case 'z' :
674                 flags |= (sizeof(size_t) == sizeof(long) ? FLAGS_LONG : FLAGS_LONG_LONG);
675                 format++;
676                 break;
677             default :
678                 break;
679         }
680 
681         // evaluate specifier
682         switch (*format) {
683             case 'd' :
684             case 'i' :
685             case 'u' :
686             case 'x' :
687             case 'X' :
688             case 'o' :
689             case 'b' : {
690                 // set the base
691                 unsigned int base;
692                 if (*format == 'x' || *format == 'X') {
693                     base = 16U;
694                 } else if (*format == 'o') {
695                     base =  8U;
696                 } else if (*format == 'b') {
697                     base =  2U;
698                 } else {
699                     base = 10U;
700                     flags &= ~FLAGS_HASH;   // no hash for dec format
701                 }
702                 // uppercase
703                 if (*format == 'X') {
704                     flags |= FLAGS_UPPERCASE;
705                 }
706 
707                 // no plus or space flag for u, x, X, o, b
708                 if ((*format != 'i') && (*format != 'd')) {
709                     flags &= ~(FLAGS_PLUS | FLAGS_SPACE);
710                 }
711 
712                 // ignore '0' flag when precision is given
713                 if (flags & FLAGS_PRECISION) {
714                     flags &= ~FLAGS_ZEROPAD;
715                 }
716 
717                 // convert the integer
718                 if ((*format == 'i') || (*format == 'd')) {
719                     // signed
720                     if (flags & FLAGS_LONG_LONG) {
721 #if defined(PRINTF_SUPPORT_LONG_LONG)
722                         const long long value = va_arg(va, long long);
723                         idx = _ntoa_long_long(out, buffer, idx, maxlen, (unsigned long long)(value > 0 ? value : 0 - value), value < 0, base,
724                                               precision, width, flags);
725 #endif
726                     } else if (flags & FLAGS_LONG) {
727                         const long value = va_arg(va, long);
728                         idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned long)(value > 0 ? value : 0 - value), value < 0, base, precision,
729                                          width, flags);
730                     } else {
731                         const int value = (flags & FLAGS_CHAR) ? (char)va_arg(va, int) : (flags & FLAGS_SHORT) ? (short int)va_arg(va,
732                                           int) : va_arg(va, int);
733                         idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned int)(value > 0 ? value : 0 - value), value < 0, base, precision,
734                                          width, flags);
735                     }
736                 } else {
737                     // unsigned
738                     if (flags & FLAGS_LONG_LONG) {
739 #if defined(PRINTF_SUPPORT_LONG_LONG)
740                         idx = _ntoa_long_long(out, buffer, idx, maxlen, va_arg(va, unsigned long long), false, base, precision, width, flags);
741 #endif
742                     } else if (flags & FLAGS_LONG) {
743                         idx = _ntoa_long(out, buffer, idx, maxlen, va_arg(va, unsigned long), false, base, precision, width, flags);
744                     } else {
745                         const unsigned int value = (flags & FLAGS_CHAR) ? (unsigned char)va_arg(va,
746                                                    unsigned int) : (flags & FLAGS_SHORT) ? (unsigned short int)va_arg(va, unsigned int) : va_arg(va, unsigned int);
747                         idx = _ntoa_long(out, buffer, idx, maxlen, value, false, base, precision, width, flags);
748                     }
749                 }
750                 format++;
751                 break;
752             }
753 #if defined(PRINTF_SUPPORT_FLOAT)
754             case 'f' :
755             case 'F' :
756                 if (*format == 'F') {
757                     flags |= FLAGS_UPPERCASE;
758                 }
759                 idx = _ftoa(out, buffer, idx, maxlen, va_arg(va, double), precision, width, flags);
760                 format++;
761                 break;
762 #if defined(PRINTF_SUPPORT_EXPONENTIAL)
763             case 'e':
764             case 'E':
765             case 'g':
766             case 'G':
767                 if ((*format == 'g') || (*format == 'G')) {
768                     flags |= FLAGS_ADAPT_EXP;
769                 }
770                 if ((*format == 'E') || (*format == 'G')) {
771                     flags |= FLAGS_UPPERCASE;
772                 }
773                 idx = _etoa(out, buffer, idx, maxlen, va_arg(va, double), precision, width, flags);
774                 format++;
775                 break;
776 #endif  // PRINTF_SUPPORT_EXPONENTIAL
777 #endif  // PRINTF_SUPPORT_FLOAT
778             case 'c' : {
779                 unsigned int l = 1U;
780                 // pre padding
781                 if (!(flags & FLAGS_LEFT)) {
782                     while (l++ < width) {
783                         out(' ', buffer, idx++, maxlen);
784                     }
785                 }
786                 // char output
787                 out((char)va_arg(va, int), buffer, idx++, maxlen);
788                 // post padding
789                 if (flags & FLAGS_LEFT) {
790                     while (l++ < width) {
791                         out(' ', buffer, idx++, maxlen);
792                     }
793                 }
794                 format++;
795                 break;
796             }
797 
798             case 's' : {
799                 const char *p = va_arg(va, char *);
800                 if (!p) {
801                     p = "(null)";
802                 }
803                 unsigned int l = _strnlen_s(p, precision ? precision : (size_t) -1);
804                 // pre padding
805                 if (flags & FLAGS_PRECISION) {
806                     l = (l < precision ? l : precision);
807                 }
808                 if (!(flags & FLAGS_LEFT)) {
809                     while (l++ < width) {
810                         out(' ', buffer, idx++, maxlen);
811                     }
812                 }
813                 // string output
814                 while ((*p != 0) && (!(flags & FLAGS_PRECISION) || precision--)) {
815                     out(*(p++), buffer, idx++, maxlen);
816                 }
817                 // post padding
818                 if (flags & FLAGS_LEFT) {
819                     while (l++ < width) {
820                         out(' ', buffer, idx++, maxlen);
821                     }
822                 }
823                 format++;
824                 break;
825             }
826 
827             case 'p' : {
828                 width = sizeof(void *) * 2U;
829                 flags |= FLAGS_ZEROPAD | FLAGS_UPPERCASE;
830 #if defined(PRINTF_SUPPORT_LONG_LONG)
831                 const bool is_ll = sizeof(uintptr_t) == sizeof(long long);
832                 if (is_ll) {
833                     idx = _ntoa_long_long(out, buffer, idx, maxlen, (uintptr_t)va_arg(va, void *), false, 16U, precision, width, flags);
834                 } else {
835 #endif
836                     idx = _ntoa_long(out, buffer, idx, maxlen, (unsigned long)((uintptr_t)va_arg(va, void *)), false, 16U, precision, width,
837                                      flags);
838 #if defined(PRINTF_SUPPORT_LONG_LONG)
839                 }
840 #endif
841                 format++;
842                 break;
843             }
844 
845             case '%' :
846                 out('%', buffer, idx++, maxlen);
847                 format++;
848                 break;
849 
850             default :
851                 out(*format, buffer, idx++, maxlen);
852                 format++;
853                 break;
854         }
855     }
856 
857     // termination
858     out((char)0, buffer, idx < maxlen ? idx : maxlen - 1U, maxlen);
859 
860     // return written chars without terminating \0
861     return (int)idx;
862 }
863 
__wrap_printf(const char * format,...)864 int __wrap_printf(const char *format, ...)
865 {
866     va_list va;
867     va_start(va, format);
868     char buffer[1];
869     const int ret = _vsnprintf(_out_char, buffer, (size_t) -1, format, va);
870     va_end(va);
871     return ret;
872 }
873 
__wrap_sprintf(char * buffer,const char * format,...)874 int __wrap_sprintf(char *buffer, const char *format, ...)
875 {
876     va_list va;
877     va_start(va, format);
878     const int ret = _vsnprintf(_out_buffer, buffer, (size_t) -1, format, va);
879     va_end(va);
880     return ret;
881 }
882 
__wrap_snprintf(char * buffer,size_t count,const char * format,...)883 int __wrap_snprintf(char *buffer, size_t count, const char *format, ...)
884 {
885     va_list va;
886     va_start(va, format);
887     const int ret = _vsnprintf(_out_buffer, buffer, count, format, va);
888     va_end(va);
889     return ret;
890 }
891 
__wrap_vprintf(const char * format,va_list va)892 int __wrap_vprintf(const char *format, va_list va)
893 {
894     char buffer[1];
895     return _vsnprintf(_out_char, buffer, (size_t) -1, format, va);
896 }
897 
__wrap_vsprintf(char * buffer,const char * format,va_list va)898 int __wrap_vsprintf(char *buffer, const char *format, va_list va)
899 {
900     return _vsnprintf(_out_buffer, buffer, (size_t) -1, format, va);
901 }
902 
__wrap_vsnprintf(char * buffer,size_t count,const char * format,va_list va)903 int __wrap_vsnprintf(char *buffer, size_t count, const char *format, va_list va)
904 {
905     return _vsnprintf(_out_buffer, buffer, count, format, va);
906 }
907 
908 #ifdef PRINTF2_SUPPORT
_out_char_2(char character,void * buffer,size_t idx,size_t maxlen)909 static inline void _out_char_2(char character, void *buffer, size_t idx, size_t maxlen)
910 {
911     (void)buffer;
912     (void)idx;
913     (void)maxlen;
914     if (character) {
915         uart_put_char_2(character);
916     }
917 }
918 
printf2(const char * format,...)919 int printf2(const char *format, ...)
920 {
921     va_list va;
922     va_start(va, format);
923     char buffer[1];
924     const int ret = _vsnprintf(_out_char_2, buffer, (size_t) -1, format, va);
925     va_end(va);
926     return ret;
927 }
928 #endif
929 
930 #if 0
931 // it's supported, but not used yet
932 int fctprintf(void (*out)(char character, void *arg), void *arg, const char *format, ...)
933 {
934     va_list va;
935     va_start(va, format);
936     const out_fct_wrap_type out_fct_wrap = { out, arg };
937     const int ret = _vsnprintf(_out_fct, (char *)(uintptr_t)&out_fct_wrap, (size_t) -1, format, va);
938     va_end(va);
939     return ret;
940 }
941 #endif
942