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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
|
/** \file
* Routines to generate text format protobufs.
*
* This file contains the internal support functions as well as the
* exported functions which are used to generate text format protobufs
* from C protobuf data types.
*
* \author Kevin Lyda <kevin@ie.suberic.net>
* \date March 2014
*/
#include "../stdafx.h"
#include <sys/types.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "protobuf-c.h"
#include "protobuf-c-util.h"
/** Append a string to the ReturnString.
*
* Append the string built from \c format and its args to the \c rs
* string. Note that \c malloc_err is checked and if it's true,
* this function won't do anything.
*
* \param[in,out] rs The string to append to.
* \param[in] guess A guess at the number of chars being added.
* \param[in] allocator allocator functions.
* \param[in] format Printf-style format string.
* \param[in] ... Variable number of args for \c format.
*/
/** \defgroup generate Functions to generate text format proto bufs
* \ingroup internal
* @{
*/
/** Escape string.
*
* Add escape characters to strings for problematic characters.
*
* \param[in] src The unescaped string to process.
* \param[in] len Length of \c src. Note that \c src might have ASCII
* \c NULs so strlen() isn't good enough here.
* \param[in] allocator allocator functions.
* \return The fully escaped string, or \c NULL if there has been an
* allocation error.
*/
static char *
esc_str(const char *src, size_t len)
{
size_t escapes = 0, dst_len = 0;
for (size_t i = 0; i < len; i++) {
if (!isprint(src[i])) {
escapes++;
}
}
char *dst = (char *)malloc((escapes * 2) + ((len - escapes) * 2) + 1);
if (!dst) {
return NULL;
}
for (size_t i = 0; i < len; i++) {
switch (src[i]) {
/* Special cases. */
case '\'':
dst[dst_len++] = '\\';
dst[dst_len++] = '\'';
break;
case '\"':
dst[dst_len++] = '\\';
dst[dst_len++] = '\"';
break;
case '\\':
dst[dst_len++] = '\\';
dst[dst_len++] = '\\';
break;
case '\n':
dst[dst_len++] = '\\';
dst[dst_len++] = 'n';
break;
case '\r':
dst[dst_len++] = '\\';
dst[dst_len++] = 'r';
break;
case '\t':
dst[dst_len++] = '\\';
dst[dst_len++] = 't';
break;
/* Escape with octal if !isprint. */
default:
dst[dst_len++] = src[i];
break;
}
}
dst[dst_len] = '\0';
return dst;
}
/** Internal function to back API function.
*
* Has a few extra params to better enable recursion. This function gets
* called for each nested message as the \c ProtobufCMessage struct is
* traversed.
*
* \param[in,out] rs The string being built up for the text format protobuf.
* \param[in] level Indent level - increments in 2's.
* \param[in] m The \c ProtobufCMessage being serialised.
* \param[in] d The descriptor for the \c ProtobufCMessage.
* \param[in] allocator allocator functions.
*/
static void protobuf_c_text_to_string_internal(
CMStringA &str,
int level,
const ProtobufCMessage *m,
const ProtobufCMessageDescriptor *d)
{
size_t j, quantifier_offset;
double float_var;
const ProtobufCFieldDescriptor *f;
ProtobufCEnumDescriptor *enumd;
const ProtobufCEnumValue *enumv;
f = d->fields;
for (unsigned i = 0; i < d->n_fields; i++) {
/* Decide if something needs to be done for this field. */
switch (f[i].label) {
case PROTOBUF_C_LABEL_OPTIONAL:
if (f[i].type == PROTOBUF_C_TYPE_STRING) {
if (!STRUCT_MEMBER(char *, m, f[i].offset)
|| (STRUCT_MEMBER(char *, m, f[i].offset)
== (char *)f[i].default_value)) {
continue;
}
}
else if (f[i].type == PROTOBUF_C_TYPE_MESSAGE) {
if (!STRUCT_MEMBER(char *, m, f[i].offset)) {
continue;
}
}
else {
if (!STRUCT_MEMBER(protobuf_c_boolean, m, f[i].quantifier_offset)) {
continue;
}
}
break;
case PROTOBUF_C_LABEL_REPEATED:
if (!STRUCT_MEMBER(size_t, m, f[i].quantifier_offset)) {
continue;
}
break;
}
quantifier_offset = STRUCT_MEMBER(size_t, m, f[i].quantifier_offset);
/* Field exists and has data, dump it. */
switch (f[i].type) {
case PROTOBUF_C_TYPE_INT32:
case PROTOBUF_C_TYPE_UINT32:
case PROTOBUF_C_TYPE_FIXED32:
if (f[i].label == PROTOBUF_C_LABEL_REPEATED) {
for (j = 0; j < quantifier_offset; j++) {
str.AppendFormat(
"%*s%s: %u\n",
level, "", f[i].name,
STRUCT_MEMBER(uint32_t *, m, f[i].offset)[j]);
}
}
else {
str.AppendFormat(
"%*s%s: %u\n",
level, "", f[i].name,
STRUCT_MEMBER(uint32_t, m, f[i].offset));
}
break;
case PROTOBUF_C_TYPE_SINT32:
case PROTOBUF_C_TYPE_SFIXED32:
if (f[i].label == PROTOBUF_C_LABEL_REPEATED) {
for (j = 0; j < quantifier_offset; j++) {
str.AppendFormat(
"%*s%s: %d\n",
level, "", f[i].name,
STRUCT_MEMBER(int32_t *, m, f[i].offset)[j]);
}
}
else {
str.AppendFormat(
"%*s%s: %d\n",
level, "", f[i].name,
STRUCT_MEMBER(int32_t, m, f[i].offset));
}
break;
case PROTOBUF_C_TYPE_INT64:
case PROTOBUF_C_TYPE_UINT64:
case PROTOBUF_C_TYPE_FIXED64:
if (f[i].label == PROTOBUF_C_LABEL_REPEATED) {
for (j = 0; j < quantifier_offset; j++) {
str.AppendFormat(
"%*s%s: %lld\n",
level, "", f[i].name,
STRUCT_MEMBER(uint64_t *, m, f[i].offset)[j]);
}
}
else {
str.AppendFormat(
"%*s%s: %lld\n",
level, "", f[i].name,
STRUCT_MEMBER(uint64_t, m, f[i].offset));
}
break;
case PROTOBUF_C_TYPE_SINT64:
case PROTOBUF_C_TYPE_SFIXED64:
if (f[i].label == PROTOBUF_C_LABEL_REPEATED) {
for (j = 0; j < quantifier_offset; j++) {
str.AppendFormat(
"%*s%s: %lld\n",
level, "", f[i].name,
STRUCT_MEMBER(int64_t *, m, f[i].offset)[j]);
}
}
else {
str.AppendFormat(
"%*s%s: %lld\n",
level, "", f[i].name,
STRUCT_MEMBER(int64_t, m, f[i].offset));
}
break;
case PROTOBUF_C_TYPE_FLOAT:
if (f[i].label == PROTOBUF_C_LABEL_REPEATED) {
for (j = 0; j < quantifier_offset; j++) {
float_var = STRUCT_MEMBER(float *, m, f[i].offset)[j];
str.AppendFormat(
"%*s%s: %g\n",
level, "", f[i].name,
float_var);
}
}
else {
float_var = STRUCT_MEMBER(float, m, f[i].offset);
str.AppendFormat(
"%*s%s: %g\n",
level, "", f[i].name,
float_var);
}
break;
case PROTOBUF_C_TYPE_DOUBLE:
if (f[i].label == PROTOBUF_C_LABEL_REPEATED) {
for (j = 0; j < quantifier_offset; j++) {
str.AppendFormat(
"%*s%s: %g\n",
level, "", f[i].name,
STRUCT_MEMBER(double *, m, f[i].offset)[j]);
}
}
else {
str.AppendFormat(
"%*s%s: %g\n",
level, "", f[i].name,
STRUCT_MEMBER(double, m, f[i].offset));
}
break;
case PROTOBUF_C_TYPE_BOOL:
if (f[i].label == PROTOBUF_C_LABEL_REPEATED) {
for (j = 0; j < quantifier_offset; j++) {
str.AppendFormat(
"%*s%s: %s\n",
level, "", f[i].name,
STRUCT_MEMBER(protobuf_c_boolean *, m, f[i].offset)[j] ?
"true" : "false");
}
}
else {
str.AppendFormat(
"%*s%s: %s\n",
level, "", f[i].name,
STRUCT_MEMBER(protobuf_c_boolean, m, f[i].offset) ?
"true" : "false");
}
break;
case PROTOBUF_C_TYPE_ENUM:
enumd = (ProtobufCEnumDescriptor *)f[i].descriptor;
if (f[i].label == PROTOBUF_C_LABEL_REPEATED) {
for (j = 0; j < quantifier_offset; j++) {
enumv = protobuf_c_enum_descriptor_get_value(
enumd, STRUCT_MEMBER(int *, m, f[i].offset)[j]);
str.AppendFormat(
"%*s%s: %s\n",
level, "", f[i].name,
enumv ? enumv->name : "unknown");
}
}
else {
enumv = protobuf_c_enum_descriptor_get_value(
enumd, STRUCT_MEMBER(int, m, f[i].offset));
str.AppendFormat(
"%*s%s: %s\n",
level, "", f[i].name,
enumv ? enumv->name : "unknown");
}
break;
case PROTOBUF_C_TYPE_STRING:
if (f[i].label == PROTOBUF_C_LABEL_REPEATED) {
for (j = 0; j < quantifier_offset; j++) {
char *escaped = esc_str(
STRUCT_MEMBER(char **, m, f[i].offset)[j],
strlen(STRUCT_MEMBER(char **, m, f[i].offset)[j]));
str.AppendFormat("%*s%s: \"%s\"\n", level, "", f[i].name, escaped);
free(escaped);
}
}
else {
char *escaped = esc_str(STRUCT_MEMBER(char *, m, f[i].offset),
strlen(STRUCT_MEMBER(char *, m, f[i].offset)));
str.AppendFormat("%*s%s: \"%s\"\n", level, "", f[i].name, escaped);
free(escaped);
}
break;
case PROTOBUF_C_TYPE_BYTES:
if (f[i].label == PROTOBUF_C_LABEL_REPEATED) {
for (j = 0; j < quantifier_offset; j++) {
auto *member = STRUCT_MEMBER(ProtobufCBinaryData *, m, f[i].offset);
if (member->len && member->data) {
char *p = (char *)malloc(member->len * 2 + 1);
bin2hex(member->data, member->len, p);
str.AppendFormat("%*s%s: \"%s\"\n", level, "", f[i].name, p);
free(p);
}
}
}
else {
auto member = STRUCT_MEMBER(ProtobufCBinaryData, m, f[i].offset);
if (member.len && member.data) {
char *p = (char *)malloc(member.len * 2 + 1);
bin2hex(member.data, member.len, p);
str.AppendFormat("%*s%s: \"%s\"\n", level, "", f[i].name, p);
free(p);
}
}
break;
case PROTOBUF_C_TYPE_MESSAGE:
if (f[i].label == PROTOBUF_C_LABEL_REPEATED) {
for (j = 0;
j < STRUCT_MEMBER(size_t, m, f[i].quantifier_offset);
j++) {
str.AppendFormat("%*s%s {\n", level, "", f[i].name);
protobuf_c_text_to_string_internal(str, level + 2,
STRUCT_MEMBER(ProtobufCMessage **, m, f[i].offset)[j],
(ProtobufCMessageDescriptor *)f[i].descriptor);
str.AppendFormat("%*s}\n", level, "");
}
}
else {
str.AppendFormat("%*s%s {\n", level, "", f[i].name);
protobuf_c_text_to_string_internal(str, level + 2,
STRUCT_MEMBER(ProtobufCMessage *, m, f[i].offset),
(ProtobufCMessageDescriptor *)f[i].descriptor);
str.AppendFormat("%*s}\n", level, "");
}
break;
default:
return;
}
}
}
/** @} */ /* End of generate group. */
/* See .h file for API docs. */
CMStringA protobuf_c_text_to_string(const ProtobufCMessage &msg)
{
CMStringA ret;
ret.AppendFormat("%s {\n", msg.descriptor->c_name);
protobuf_c_text_to_string_internal(ret, 1, &msg, msg.descriptor);
ret.Append("}\n");
return ret;
}
|