1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2023 Google LLC. All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7
8 #include "upb/text/encode.h"
9
10 #include <inttypes.h>
11 #include <stdarg.h>
12 #include <stddef.h>
13 #include <stdint.h>
14 #include <string.h>
15
16 #include "upb/base/descriptor_constants.h"
17 #include "upb/base/string_view.h"
18 #include "upb/lex/round_trip.h"
19 #include "upb/message/array.h"
20 #include "upb/message/internal/map_entry.h"
21 #include "upb/message/internal/map_sorter.h"
22 #include "upb/message/map.h"
23 #include "upb/message/message.h"
24 #include "upb/message/value.h"
25 #include "upb/reflection/def.h"
26 #include "upb/reflection/message.h"
27 #include "upb/text/internal/encode.h"
28 #include "upb/wire/eps_copy_input_stream.h"
29
30 // Must be last.
31 #include "upb/port/def.inc"
32
33 static void _upb_TextEncode_Msg(txtenc* e, const upb_Message* msg,
34 const upb_MessageDef* m);
35
_upb_TextEncode_Enum(int32_t val,const upb_FieldDef * f,txtenc * e)36 static void _upb_TextEncode_Enum(int32_t val, const upb_FieldDef* f,
37 txtenc* e) {
38 const upb_EnumDef* e_def = upb_FieldDef_EnumSubDef(f);
39 const upb_EnumValueDef* ev = upb_EnumDef_FindValueByNumber(e_def, val);
40
41 if (ev) {
42 UPB_PRIVATE(_upb_TextEncode_Printf)(e, "%s", upb_EnumValueDef_Name(ev));
43 } else {
44 UPB_PRIVATE(_upb_TextEncode_Printf)(e, "%" PRId32, val);
45 }
46 }
47
_upb_TextEncode_Field(txtenc * e,upb_MessageValue val,const upb_FieldDef * f)48 static void _upb_TextEncode_Field(txtenc* e, upb_MessageValue val,
49 const upb_FieldDef* f) {
50 UPB_PRIVATE(_upb_TextEncode_Indent)(e);
51 const upb_CType ctype = upb_FieldDef_CType(f);
52 const bool is_ext = upb_FieldDef_IsExtension(f);
53 const char* full = upb_FieldDef_FullName(f);
54 const char* name = upb_FieldDef_Name(f);
55
56 if (ctype == kUpb_CType_Message) {
57 if (is_ext) {
58 UPB_PRIVATE(_upb_TextEncode_Printf)(e, "[%s] {", full);
59 } else {
60 UPB_PRIVATE(_upb_TextEncode_Printf)(e, "%s {", name);
61 }
62 UPB_PRIVATE(_upb_TextEncode_EndField)(e);
63 e->indent_depth++;
64 _upb_TextEncode_Msg(e, val.msg_val, upb_FieldDef_MessageSubDef(f));
65 e->indent_depth--;
66 UPB_PRIVATE(_upb_TextEncode_Indent)(e);
67 UPB_PRIVATE(_upb_TextEncode_PutStr)(e, "}");
68 UPB_PRIVATE(_upb_TextEncode_EndField)(e);
69 return;
70 }
71
72 if (is_ext) {
73 UPB_PRIVATE(_upb_TextEncode_Printf)(e, "[%s]: ", full);
74 } else {
75 UPB_PRIVATE(_upb_TextEncode_Printf)(e, "%s: ", name);
76 }
77
78 if (ctype == kUpb_CType_Enum) {
79 _upb_TextEncode_Enum(val.int32_val, f, e);
80 } else {
81 UPB_PRIVATE(_upb_TextEncode_Scalar)(e, val, ctype);
82 }
83
84 UPB_PRIVATE(_upb_TextEncode_EndField)(e);
85 }
86
87 /*
88 * Arrays print as simple repeated elements, eg.
89 *
90 * foo_field: 1
91 * foo_field: 2
92 * foo_field: 3
93 */
_upb_TextEncode_Array(txtenc * e,const upb_Array * arr,const upb_FieldDef * f)94 static void _upb_TextEncode_Array(txtenc* e, const upb_Array* arr,
95 const upb_FieldDef* f) {
96 size_t i;
97 size_t size = upb_Array_Size(arr);
98
99 for (i = 0; i < size; i++) {
100 _upb_TextEncode_Field(e, upb_Array_Get(arr, i), f);
101 }
102 }
103
_upb_TextEncode_MapEntry(txtenc * e,upb_MessageValue key,upb_MessageValue val,const upb_FieldDef * f)104 static void _upb_TextEncode_MapEntry(txtenc* e, upb_MessageValue key,
105 upb_MessageValue val,
106 const upb_FieldDef* f) {
107 const upb_MessageDef* entry = upb_FieldDef_MessageSubDef(f);
108 const upb_FieldDef* key_f = upb_MessageDef_Field(entry, 0);
109 const upb_FieldDef* val_f = upb_MessageDef_Field(entry, 1);
110 UPB_PRIVATE(_upb_TextEncode_Indent)(e);
111 UPB_PRIVATE(_upb_TextEncode_Printf)(e, "%s {", upb_FieldDef_Name(f));
112 UPB_PRIVATE(_upb_TextEncode_EndField)(e);
113 e->indent_depth++;
114
115 _upb_TextEncode_Field(e, key, key_f);
116 _upb_TextEncode_Field(e, val, val_f);
117
118 e->indent_depth--;
119 UPB_PRIVATE(_upb_TextEncode_Indent)(e);
120 UPB_PRIVATE(_upb_TextEncode_PutStr)(e, "}");
121 UPB_PRIVATE(_upb_TextEncode_EndField)(e);
122 }
123
124 /*
125 * Maps print as messages of key/value, etc.
126 *
127 * foo_map: {
128 * key: "abc"
129 * value: 123
130 * }
131 * foo_map: {
132 * key: "def"
133 * value: 456
134 * }
135 */
_upb_TextEncode_Map(txtenc * e,const upb_Map * map,const upb_FieldDef * f)136 static void _upb_TextEncode_Map(txtenc* e, const upb_Map* map,
137 const upb_FieldDef* f) {
138 if (e->options & UPB_TXTENC_NOSORT) {
139 size_t iter = kUpb_Map_Begin;
140 upb_MessageValue key, val;
141 while (upb_Map_Next(map, &key, &val, &iter)) {
142 _upb_TextEncode_MapEntry(e, key, val, f);
143 }
144 } else {
145 if (upb_Map_Size(map) == 0) return;
146
147 const upb_MessageDef* entry = upb_FieldDef_MessageSubDef(f);
148 const upb_FieldDef* key_f = upb_MessageDef_Field(entry, 0);
149 _upb_sortedmap sorted;
150 upb_MapEntry ent;
151
152 _upb_mapsorter_pushmap(&e->sorter, upb_FieldDef_Type(key_f), map, &sorted);
153 while (_upb_sortedmap_next(&e->sorter, map, &sorted, &ent)) {
154 upb_MessageValue key, val;
155 memcpy(&key, &ent.k, sizeof(key));
156 memcpy(&val, &ent.v, sizeof(val));
157 _upb_TextEncode_MapEntry(e, key, val, f);
158 }
159 _upb_mapsorter_popmap(&e->sorter, &sorted);
160 }
161 }
162
_upb_TextEncode_Msg(txtenc * e,const upb_Message * msg,const upb_MessageDef * m)163 static void _upb_TextEncode_Msg(txtenc* e, const upb_Message* msg,
164 const upb_MessageDef* m) {
165 size_t iter = kUpb_Message_Begin;
166 const upb_FieldDef* f;
167 upb_MessageValue val;
168
169 while (upb_Message_Next(msg, m, e->ext_pool, &f, &val, &iter)) {
170 if (upb_FieldDef_IsMap(f)) {
171 _upb_TextEncode_Map(e, val.map_val, f);
172 } else if (upb_FieldDef_IsRepeated(f)) {
173 _upb_TextEncode_Array(e, val.array_val, f);
174 } else {
175 _upb_TextEncode_Field(e, val, f);
176 }
177 }
178
179 if ((e->options & UPB_TXTENC_SKIPUNKNOWN) == 0) {
180 size_t size;
181 const char* ptr = upb_Message_GetUnknown(msg, &size);
182 if (size != 0) {
183 char* start = e->ptr;
184 upb_EpsCopyInputStream stream;
185 upb_EpsCopyInputStream_Init(&stream, &ptr, size, true);
186 if (!UPB_PRIVATE(_upb_TextEncode_Unknown)(e, ptr, &stream, -1)) {
187 /* Unknown failed to parse, back up and don't print it at all. */
188 e->ptr = start;
189 }
190 }
191 }
192 }
193
upb_TextEncode(const upb_Message * msg,const upb_MessageDef * m,const upb_DefPool * ext_pool,int options,char * buf,size_t size)194 size_t upb_TextEncode(const upb_Message* msg, const upb_MessageDef* m,
195 const upb_DefPool* ext_pool, int options, char* buf,
196 size_t size) {
197 txtenc e;
198
199 e.buf = buf;
200 e.ptr = buf;
201 e.end = UPB_PTRADD(buf, size);
202 e.overflow = 0;
203 e.indent_depth = 0;
204 e.options = options;
205 e.ext_pool = ext_pool;
206 _upb_mapsorter_init(&e.sorter);
207
208 _upb_TextEncode_Msg(&e, msg, m);
209 _upb_mapsorter_destroy(&e.sorter);
210 return UPB_PRIVATE(_upb_TextEncode_Nullz)(&e, size);
211 }
212