1 //
2 // Copyright 2020 Serge Martin
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a
5 // copy of this software and associated documentation files (the "Software"),
6 // to deal in the Software without restriction, including without limitation
7 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 // and/or sell copies of the Software, and to permit persons to whom the
9 // Software is furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 // OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 // OTHER DEALINGS IN THE SOFTWARE.
21 //
22 // Extract from Serge's printf clover code by airlied.
23
24 #include <assert.h>
25 #include <stdarg.h>
26 #include <stdbool.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include "macros.h"
31 #include "strndup.h"
32 #include "u_math.h"
33 #include "u_printf.h"
34
35 /* Some versions of MinGW are missing _vscprintf's declaration, although they
36 * still provide the symbol in the import library. */
37 #ifdef __MINGW32__
38 _CRTIMP int _vscprintf(const char *format, va_list argptr);
39 #endif
40
41 static const char*
util_printf_prev_tok(const char * str)42 util_printf_prev_tok(const char *str)
43 {
44 while (*str != '%')
45 str--;
46 return str;
47 }
48
util_printf_next_spec_pos(const char * str,size_t pos)49 size_t util_printf_next_spec_pos(const char *str, size_t pos)
50 {
51 if (str == NULL)
52 return -1;
53
54 const char *str_found = str + pos;
55 do {
56 str_found = strchr(str_found, '%');
57 if (str_found == NULL)
58 return -1;
59
60 ++str_found;
61 if (*str_found == '%') {
62 ++str_found;
63 continue;
64 }
65
66 char *spec_pos = strpbrk(str_found, "cdieEfFgGaAosuxXp%");
67 if (spec_pos == NULL) {
68 return -1;
69 } else if (*spec_pos == '%') {
70 str_found = spec_pos;
71 } else {
72 return spec_pos - str;
73 }
74 } while (1);
75 }
76
u_printf_length(const char * fmt,va_list untouched_args)77 size_t u_printf_length(const char *fmt, va_list untouched_args)
78 {
79 int size;
80 char junk;
81
82 /* Make a copy of the va_list so the original caller can still use it */
83 va_list args;
84 va_copy(args, untouched_args);
85
86 #ifdef _WIN32
87 /* We need to use _vcsprintf to calculate the size as vsnprintf returns -1
88 * if the number of characters to write is greater than count.
89 */
90 size = _vscprintf(fmt, args);
91 (void)junk;
92 #else
93 size = vsnprintf(&junk, 1, fmt, args);
94 #endif
95 assert(size >= 0);
96
97 va_end(args);
98
99 return size;
100 }
101
u_printf(FILE * out,const char * buffer,size_t buffer_size,const u_printf_info * info,unsigned info_size)102 void u_printf(FILE *out, const char *buffer, size_t buffer_size,
103 const u_printf_info *info, unsigned info_size)
104 {
105 for (size_t buf_pos = 0; buf_pos < buffer_size;) {
106 uint32_t fmt_idx = *(uint32_t*)&buffer[buf_pos];
107
108 /* the idx is 1 based */
109 assert(fmt_idx > 0);
110 fmt_idx -= 1;
111
112 /* The API allows more arguments than the format uses */
113 if (fmt_idx >= info_size)
114 return;
115
116 const u_printf_info *fmt = &info[fmt_idx];
117 const char *format = fmt->strings;
118 buf_pos += sizeof(fmt_idx);
119
120 if (!fmt->num_args) {
121 fprintf(out, "%s", format);
122 continue;
123 }
124
125 for (int i = 0; i < fmt->num_args; i++) {
126 int arg_size = fmt->arg_sizes[i];
127 size_t spec_pos = util_printf_next_spec_pos(format, 0);
128
129 if (spec_pos == -1) {
130 fprintf(out, "%s", format);
131 continue;
132 }
133
134 const char *token = util_printf_prev_tok(&format[spec_pos]);
135 const char *next_format = &format[spec_pos + 1];
136
137 /* print the part before the format token */
138 if (token != format)
139 fwrite(format, token - format, 1, out);
140
141 char *print_str = strndup(token, next_format - token);
142 /* rebase spec_pos so we can use it with print_str */
143 spec_pos += format - token;
144
145 /* print the formated part */
146 if (print_str[spec_pos] == 's') {
147 uint64_t idx;
148 memcpy(&idx, &buffer[buf_pos], 8);
149 fprintf(out, print_str, &fmt->strings[idx]);
150
151 /* Never pass a 'n' spec to the host printf */
152 } else if (print_str[spec_pos] != 'n') {
153 char *vec_pos = strchr(print_str, 'v');
154 char *mod_pos = strpbrk(print_str, "hl");
155
156 int component_count = 1;
157 if (vec_pos != NULL) {
158 /* non vector part of the format */
159 size_t base = mod_pos ? mod_pos - print_str : spec_pos;
160 size_t l = base - (vec_pos - print_str) - 1;
161 char *vec = strndup(&vec_pos[1], l);
162 component_count = atoi(vec);
163 free(vec);
164
165 /* remove the vector and precision stuff */
166 memmove(&print_str[vec_pos - print_str], &print_str[spec_pos], 2);
167 }
168
169 /* in fact vec3 are vec4 */
170 int men_components = component_count == 3 ? 4 : component_count;
171 size_t elmt_size = arg_size / men_components;
172 bool is_float = strpbrk(print_str, "fFeEgGaA") != NULL;
173
174 for (int i = 0; i < component_count; i++) {
175 size_t elmt_buf_pos = buf_pos + i * elmt_size;
176 switch (elmt_size) {
177 case 1: {
178 uint8_t v;
179 memcpy(&v, &buffer[elmt_buf_pos], elmt_size);
180 fprintf(out, print_str, v);
181 break;
182 }
183 case 2: {
184 uint16_t v;
185 memcpy(&v, &buffer[elmt_buf_pos], elmt_size);
186 fprintf(out, print_str, v);
187 break;
188 }
189 case 4: {
190 if (is_float) {
191 float v;
192 memcpy(&v, &buffer[elmt_buf_pos], elmt_size);
193 fprintf(out, print_str, v);
194 } else {
195 uint32_t v;
196 memcpy(&v, &buffer[elmt_buf_pos], elmt_size);
197 fprintf(out, print_str, v);
198 }
199 break;
200 }
201 case 8: {
202 if (is_float) {
203 double v;
204 memcpy(&v, &buffer[elmt_buf_pos], elmt_size);
205 fprintf(out, print_str, v);
206 } else {
207 uint64_t v;
208 memcpy(&v, &buffer[elmt_buf_pos], elmt_size);
209 fprintf(out, print_str, v);
210 }
211 break;
212 }
213 default:
214 assert(false);
215 break;
216 }
217
218 if (i < component_count - 1)
219 fprintf(out, ",");
220 }
221 }
222
223 /* rebase format */
224 format = next_format;
225 free(print_str);
226
227 buf_pos += arg_size;
228 buf_pos = ALIGN(buf_pos, 4);
229 }
230
231 /* print remaining */
232 fprintf(out, "%s", format);
233 }
234 }
235