blob: 53cfe9955eaca877a92f23eff1364135c6bcb18a [file] [log] [blame]
Sylvain Munaut26bc4652020-09-14 10:19:49 +02001/*
2 * The Minimal snprintf() implementation
3 *
4 * Copyright (c) 2013,2014 Michal Ludvig <michal@logix.cz>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * * Neither the name of the auhor nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * ----
30 *
31 * This is a minimal snprintf() implementation optimised
32 * for embedded systems with a very limited program memory.
33 * mini_snprintf() doesn't support _all_ the formatting
34 * the glibc does but on the other hand is a lot smaller.
35 * Here are some numbers from my STM32 project (.bin file size):
36 * no snprintf(): 10768 bytes
37 * mini snprintf(): 11420 bytes (+ 652 bytes)
38 * glibc snprintf(): 34860 bytes (+24092 bytes)
39 * Wasting nearly 24kB of memory just for snprintf() on
40 * a chip with 32kB flash is crazy. Use mini_snprintf() instead.
41 *
42 */
43
44#include "mini-printf.h"
45
46static unsigned int
47mini_strlen(const char *s)
48{
49 unsigned int len = 0;
50 while (s[len] != '\0') len++;
51 return len;
52}
53
54static unsigned int
55mini_itoa(int value, unsigned int radix, unsigned int uppercase, unsigned int unsig,
56 char *buffer, unsigned int zero_pad)
57{
58 char *pbuffer = buffer;
59 int negative = 0;
60 unsigned int i, len;
61
62 /* No support for unusual radixes. */
63 if (radix > 16)
64 return 0;
65
66 if (value < 0 && !unsig) {
67 negative = 1;
68 value = -value;
69 }
70
71 /* This builds the string back to front ... */
72 do {
73 int digit = value % radix;
74 *(pbuffer++) = (digit < 10 ? '0' + digit : (uppercase ? 'A' : 'a') + digit - 10);
75 value /= radix;
76 } while (value > 0);
77
78 for (i = (pbuffer - buffer); i < zero_pad; i++)
79 *(pbuffer++) = '0';
80
81 if (negative)
82 *(pbuffer++) = '-';
83
84 *(pbuffer) = '\0';
85
86 /* ... now we reverse it (could do it recursively but will
87 * conserve the stack space) */
88 len = (pbuffer - buffer);
89 for (i = 0; i < len / 2; i++) {
90 char j = buffer[i];
91 buffer[i] = buffer[len-i-1];
92 buffer[len-i-1] = j;
93 }
94
95 return len;
96}
97
98struct mini_buff {
99 char *buffer, *pbuffer;
100 unsigned int buffer_len;
101};
102
103static int
104_putc(int ch, struct mini_buff *b)
105{
106 if ((unsigned int)((b->pbuffer - b->buffer) + 1) >= b->buffer_len)
107 return 0;
108 *(b->pbuffer++) = ch;
109 *(b->pbuffer) = '\0';
110 return 1;
111}
112
113static int
114_puts(char *s, unsigned int len, struct mini_buff *b)
115{
116 unsigned int i;
117
118 if (b->buffer_len - (b->pbuffer - b->buffer) - 1 < len)
119 len = b->buffer_len - (b->pbuffer - b->buffer) - 1;
120
121 /* Copy to buffer */
122 for (i = 0; i < len; i++)
123 *(b->pbuffer++) = s[i];
124 *(b->pbuffer) = '\0';
125
126 return len;
127}
128
129int
130mini_vsnprintf(char *buffer, unsigned int buffer_len, const char *fmt, va_list va)
131{
132 struct mini_buff b;
133 char bf[24];
134 char ch;
135
136 b.buffer = buffer;
137 b.pbuffer = buffer;
138 b.buffer_len = buffer_len;
139
140 while ((ch=*(fmt++))) {
141 if ((unsigned int)((b.pbuffer - b.buffer) + 1) >= b.buffer_len)
142 break;
143 if (ch!='%')
144 _putc(ch, &b);
145 else {
146 char zero_pad = 0;
147 char *ptr;
148 unsigned int len;
149
150 ch=*(fmt++);
151
152 /* Zero padding requested */
153 if (ch=='0') {
154 ch=*(fmt++);
155 if (ch == '\0')
156 goto end;
157 if (ch >= '0' && ch <= '9')
158 zero_pad = ch - '0';
159 ch=*(fmt++);
160 }
161
162 switch (ch) {
163 case 0:
164 goto end;
165
166 case 'u':
167 case 'd':
168 len = mini_itoa(va_arg(va, unsigned int), 10, 0, (ch=='u'), bf, zero_pad);
169 _puts(bf, len, &b);
170 break;
171
172 case 'x':
173 case 'X':
174 len = mini_itoa(va_arg(va, unsigned int), 16, (ch=='X'), 1, bf, zero_pad);
175 _puts(bf, len, &b);
176 break;
177
178 case 'c' :
179 _putc((char)(va_arg(va, int)), &b);
180 break;
181
182 case 's' :
183 ptr = va_arg(va, char*);
184 _puts(ptr, mini_strlen(ptr), &b);
185 break;
186
187 default:
188 _putc(ch, &b);
189 break;
190 }
191 }
192 }
193end:
194 return b.pbuffer - b.buffer;
195}
196
197
198int
199mini_snprintf(char* buffer, unsigned int buffer_len, const char *fmt, ...)
200{
201 int ret;
202 va_list va;
203 va_start(va, fmt);
204 ret = mini_vsnprintf(buffer, buffer_len, fmt, va);
205 va_end(va);
206
207 return ret;
208}