-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.c
186 lines (154 loc) · 4.75 KB
/
example.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include <string.h>
#include "example.h"
#include "parse_printf.h"
struct callback_info {
char *buffer_in;
uint32_t buffer_used;
uint32_t buffer_size;
};
uint32_t
format_to_buffer(char *const buffer_in,
const uint32_t buffer_len,
const char *const format,
...)
{
va_list list;
va_start(list, format);
const uint32_t result =
vformat_to_buffer(buffer_in, buffer_len, format, list);
va_end(list);
return result;
}
static uint32_t
format_to_buffer_write_ch_callback(
struct printf_spec_info *const spec_info,
void *const info,
const char ch,
uint32_t amount,
bool *const should_continue_out)
{
(void)spec_info;
struct callback_info *const cb_info = (struct callback_info *)info;
const uint32_t old_used = cb_info->buffer_used;
uint32_t new_used = old_used;
if (__builtin_add_overflow(new_used, amount, &new_used)) {
*should_continue_out = false;
return 0;
}
const uint32_t buffer_size = cb_info->buffer_size;
if (new_used >= buffer_size) {
/*
* Truncate amount to just the space left if we don't have enough space
* in the buffer to copy the whole string.
*/
amount = buffer_size - old_used;
*should_continue_out = false;
cb_info->buffer_used = old_used + amount;
} else {
cb_info->buffer_used = new_used;
}
memset(cb_info->buffer_in + old_used, ch, amount);
return amount;
}
static uint32_t
format_to_buffer_write_string_callback(
struct printf_spec_info *const spec_info,
void *const info,
const char *const string,
const uint32_t length,
bool *const should_continue_out)
{
(void)spec_info;
struct callback_info *const cb_info = (struct callback_info *)info;
const uint32_t old_used = cb_info->buffer_used;
uint32_t new_used = old_used;
if (__builtin_add_overflow(new_used, length, &new_used)) {
*should_continue_out = false;
return 0;
}
const uint32_t buffer_size = cb_info->buffer_size;
uint32_t amount = length;
if (new_used >= buffer_size) {
/*
* Truncate amount to just the space left if we don't have enough space
* in the buffer to copy the whole string.
*/
amount = buffer_size - old_used;
*should_continue_out = false;
cb_info->buffer_used = old_used + amount;
} else {
cb_info->buffer_used = new_used;
}
memcpy(cb_info->buffer_in + old_used, string, amount);
return amount;
}
uint32_t
vformat_to_buffer(char *const buffer_in,
const uint32_t buffer_len,
const char *const format,
va_list list)
{
if (buffer_len == 0) {
return 0;
}
// Subtract one so that buffer can contain a null-terminator.
struct callback_info cb_info = {
.buffer_in = buffer_in,
.buffer_used = 0,
.buffer_size = buffer_len - 1
};
const uint32_t length =
parse_printf_format(format_to_buffer_write_ch_callback,
&cb_info,
format_to_buffer_write_string_callback,
&cb_info,
format,
list);
cb_info.buffer_in[cb_info.buffer_used] = '\0';
return length;
}
static uint32_t
get_length_ch_callback(struct printf_spec_info *const spec_info,
void *const cb_info,
const char ch,
const uint32_t times,
bool *const should_continue_out)
{
(void)spec_info;
(void)cb_info;
(void)ch;
(void)times;
(void)should_continue_out;
return times;
}
static uint32_t
get_length_string_callback(struct printf_spec_info *const spec_info,
void *const cb_info,
const char *const string,
const uint32_t length,
bool *const should_continue_out)
{
(void)spec_info;
(void)cb_info;
(void)string;
(void)length;
(void)should_continue_out;
return length;
}
uint32_t get_length_of_printf_format(const char *const fmt, ...) {
va_list list;
va_start(list, fmt);
const uint32_t result = get_length_of_printf_vformat(fmt, list);
va_end(list);
return result;
}
uint32_t get_length_of_printf_vformat(const char *const fmt, va_list list) {
const uint32_t length =
parse_printf_format(get_length_ch_callback,
/*char_cb_info=*/NULL,
get_length_string_callback,
/*string_cb_info=*/NULL,
fmt,
list);
return length;
}