text_format.cc 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  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
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // Author: jschorr@google.com (Joseph Schorr)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. #include <algorithm>
  34. #include <float.h>
  35. #include <math.h>
  36. #include <stdio.h>
  37. #include <stack>
  38. #include <limits>
  39. #include <vector>
  40. #include <google/protobuf/text_format.h>
  41. #include <google/protobuf/stubs/stringprintf.h>
  42. #include <google/protobuf/any.h>
  43. #include <google/protobuf/io/strtod.h>
  44. #include <google/protobuf/io/coded_stream.h>
  45. #include <google/protobuf/io/tokenizer.h>
  46. #include <google/protobuf/io/zero_copy_stream.h>
  47. #include <google/protobuf/io/zero_copy_stream_impl.h>
  48. #include <google/protobuf/descriptor.pb.h>
  49. #include <google/protobuf/descriptor.h>
  50. #include <google/protobuf/dynamic_message.h>
  51. #include <google/protobuf/repeated_field.h>
  52. #include <google/protobuf/unknown_field_set.h>
  53. #include <google/protobuf/wire_format_lite.h>
  54. #include <google/protobuf/stubs/strutil.h>
  55. #include <google/protobuf/stubs/map_util.h>
  56. #include <google/protobuf/stubs/stl_util.h>
  57. namespace google {
  58. namespace protobuf {
  59. namespace {
  60. inline bool IsHexNumber(const string& str) {
  61. return (str.length() >= 2 && str[0] == '0' &&
  62. (str[1] == 'x' || str[1] == 'X'));
  63. }
  64. inline bool IsOctNumber(const string& str) {
  65. return (str.length() >= 2 && str[0] == '0' &&
  66. (str[1] >= '0' && str[1] < '8'));
  67. }
  68. } // namespace
  69. string Message::DebugString() const {
  70. string debug_string;
  71. TextFormat::Printer printer;
  72. printer.SetExpandAny(true);
  73. printer.PrintToString(*this, &debug_string);
  74. return debug_string;
  75. }
  76. string Message::ShortDebugString() const {
  77. string debug_string;
  78. TextFormat::Printer printer;
  79. printer.SetSingleLineMode(true);
  80. printer.SetExpandAny(true);
  81. printer.PrintToString(*this, &debug_string);
  82. // Single line mode currently might have an extra space at the end.
  83. if (debug_string.size() > 0 &&
  84. debug_string[debug_string.size() - 1] == ' ') {
  85. debug_string.resize(debug_string.size() - 1);
  86. }
  87. return debug_string;
  88. }
  89. string Message::Utf8DebugString() const {
  90. string debug_string;
  91. TextFormat::Printer printer;
  92. printer.SetUseUtf8StringEscaping(true);
  93. printer.SetExpandAny(true);
  94. printer.PrintToString(*this, &debug_string);
  95. return debug_string;
  96. }
  97. void Message::PrintDebugString() const {
  98. printf("%s", DebugString().c_str());
  99. }
  100. // ===========================================================================
  101. // Implementation of the parse information tree class.
  102. TextFormat::ParseInfoTree::ParseInfoTree() { }
  103. TextFormat::ParseInfoTree::~ParseInfoTree() {
  104. // Remove any nested information trees, as they are owned by this tree.
  105. for (NestedMap::iterator it = nested_.begin(); it != nested_.end(); ++it) {
  106. STLDeleteElements(&(it->second));
  107. }
  108. }
  109. void TextFormat::ParseInfoTree::RecordLocation(
  110. const FieldDescriptor* field,
  111. TextFormat::ParseLocation location) {
  112. locations_[field].push_back(location);
  113. }
  114. TextFormat::ParseInfoTree* TextFormat::ParseInfoTree::CreateNested(
  115. const FieldDescriptor* field) {
  116. // Owned by us in the map.
  117. TextFormat::ParseInfoTree* instance = new TextFormat::ParseInfoTree();
  118. std::vector<TextFormat::ParseInfoTree*>* trees = &nested_[field];
  119. GOOGLE_CHECK(trees);
  120. trees->push_back(instance);
  121. return instance;
  122. }
  123. void CheckFieldIndex(const FieldDescriptor* field, int index) {
  124. if (field == NULL) { return; }
  125. if (field->is_repeated() && index == -1) {
  126. GOOGLE_LOG(DFATAL) << "Index must be in range of repeated field values. "
  127. << "Field: " << field->name();
  128. } else if (!field->is_repeated() && index != -1) {
  129. GOOGLE_LOG(DFATAL) << "Index must be -1 for singular fields."
  130. << "Field: " << field->name();
  131. }
  132. }
  133. TextFormat::ParseLocation TextFormat::ParseInfoTree::GetLocation(
  134. const FieldDescriptor* field, int index) const {
  135. CheckFieldIndex(field, index);
  136. if (index == -1) { index = 0; }
  137. const std::vector<TextFormat::ParseLocation>* locations =
  138. FindOrNull(locations_, field);
  139. if (locations == NULL || index >= locations->size()) {
  140. return TextFormat::ParseLocation();
  141. }
  142. return (*locations)[index];
  143. }
  144. TextFormat::ParseInfoTree* TextFormat::ParseInfoTree::GetTreeForNested(
  145. const FieldDescriptor* field, int index) const {
  146. CheckFieldIndex(field, index);
  147. if (index == -1) { index = 0; }
  148. const std::vector<TextFormat::ParseInfoTree*>* trees =
  149. FindOrNull(nested_, field);
  150. if (trees == NULL || index >= trees->size()) {
  151. return NULL;
  152. }
  153. return (*trees)[index];
  154. }
  155. namespace {
  156. // These functions implement the behavior of the "default" TextFormat::Finder,
  157. // they are defined as standalone to be called when finder_ is NULL.
  158. const FieldDescriptor* DefaultFinderFindExtension(Message* message,
  159. const string& name) {
  160. return message->GetReflection()->FindKnownExtensionByName(name);
  161. }
  162. const Descriptor* DefaultFinderFindAnyType(const Message& message,
  163. const string& prefix,
  164. const string& name) {
  165. if (prefix != internal::kTypeGoogleApisComPrefix &&
  166. prefix != internal::kTypeGoogleProdComPrefix) {
  167. return NULL;
  168. }
  169. return message.GetDescriptor()->file()->pool()->FindMessageTypeByName(name);
  170. }
  171. } // namespace
  172. // ===========================================================================
  173. // Internal class for parsing an ASCII representation of a Protocol Message.
  174. // This class makes use of the Protocol Message compiler's tokenizer found
  175. // in //google/protobuf/io/tokenizer.h. Note that class's Parse
  176. // method is *not* thread-safe and should only be used in a single thread at
  177. // a time.
  178. // Makes code slightly more readable. The meaning of "DO(foo)" is
  179. // "Execute foo and fail if it fails.", where failure is indicated by
  180. // returning false. Borrowed from parser.cc (Thanks Kenton!).
  181. #define DO(STATEMENT) if (STATEMENT) {} else return false
  182. class TextFormat::Parser::ParserImpl {
  183. public:
  184. // Determines if repeated values for non-repeated fields and
  185. // oneofs are permitted, e.g., the string "foo: 1 foo: 2" for a
  186. // required/optional field named "foo", or "baz: 1 qux: 2"
  187. // where "baz" and "qux" are members of the same oneof.
  188. enum SingularOverwritePolicy {
  189. ALLOW_SINGULAR_OVERWRITES = 0, // the last value is retained
  190. FORBID_SINGULAR_OVERWRITES = 1, // an error is issued
  191. };
  192. ParserImpl(const Descriptor* root_message_type,
  193. io::ZeroCopyInputStream* input_stream,
  194. io::ErrorCollector* error_collector,
  195. const TextFormat::Finder* finder,
  196. ParseInfoTree* parse_info_tree,
  197. SingularOverwritePolicy singular_overwrite_policy,
  198. bool allow_case_insensitive_field,
  199. bool allow_unknown_field,
  200. bool allow_unknown_enum,
  201. bool allow_field_number,
  202. bool allow_relaxed_whitespace,
  203. bool allow_partial)
  204. : error_collector_(error_collector),
  205. finder_(finder),
  206. parse_info_tree_(parse_info_tree),
  207. tokenizer_error_collector_(this),
  208. tokenizer_(input_stream, &tokenizer_error_collector_),
  209. root_message_type_(root_message_type),
  210. singular_overwrite_policy_(singular_overwrite_policy),
  211. allow_case_insensitive_field_(allow_case_insensitive_field),
  212. allow_unknown_field_(allow_unknown_field),
  213. allow_unknown_enum_(allow_unknown_enum),
  214. allow_field_number_(allow_field_number),
  215. allow_partial_(allow_partial),
  216. had_errors_(false) {
  217. // For backwards-compatibility with proto1, we need to allow the 'f' suffix
  218. // for floats.
  219. tokenizer_.set_allow_f_after_float(true);
  220. // '#' starts a comment.
  221. tokenizer_.set_comment_style(io::Tokenizer::SH_COMMENT_STYLE);
  222. if (allow_relaxed_whitespace) {
  223. tokenizer_.set_require_space_after_number(false);
  224. tokenizer_.set_allow_multiline_strings(true);
  225. }
  226. // Consume the starting token.
  227. tokenizer_.Next();
  228. }
  229. ~ParserImpl() { }
  230. // Parses the ASCII representation specified in input and saves the
  231. // information into the output pointer (a Message). Returns
  232. // false if an error occurs (an error will also be logged to
  233. // GOOGLE_LOG(ERROR)).
  234. bool Parse(Message* output) {
  235. // Consume fields until we cannot do so anymore.
  236. while (true) {
  237. if (LookingAtType(io::Tokenizer::TYPE_END)) {
  238. return !had_errors_;
  239. }
  240. DO(ConsumeField(output));
  241. }
  242. }
  243. bool ParseField(const FieldDescriptor* field, Message* output) {
  244. bool suc;
  245. if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
  246. suc = ConsumeFieldMessage(output, output->GetReflection(), field);
  247. } else {
  248. suc = ConsumeFieldValue(output, output->GetReflection(), field);
  249. }
  250. return suc && LookingAtType(io::Tokenizer::TYPE_END);
  251. }
  252. void ReportError(int line, int col, const string& message) {
  253. had_errors_ = true;
  254. if (error_collector_ == NULL) {
  255. if (line >= 0) {
  256. GOOGLE_LOG(ERROR) << "Error parsing text-format "
  257. << root_message_type_->full_name()
  258. << ": " << (line + 1) << ":"
  259. << (col + 1) << ": " << message;
  260. } else {
  261. GOOGLE_LOG(ERROR) << "Error parsing text-format "
  262. << root_message_type_->full_name()
  263. << ": " << message;
  264. }
  265. } else {
  266. error_collector_->AddError(line, col, message);
  267. }
  268. }
  269. void ReportWarning(int line, int col, const string& message) {
  270. if (error_collector_ == NULL) {
  271. if (line >= 0) {
  272. GOOGLE_LOG(WARNING) << "Warning parsing text-format "
  273. << root_message_type_->full_name()
  274. << ": " << (line + 1) << ":"
  275. << (col + 1) << ": " << message;
  276. } else {
  277. GOOGLE_LOG(WARNING) << "Warning parsing text-format "
  278. << root_message_type_->full_name()
  279. << ": " << message;
  280. }
  281. } else {
  282. error_collector_->AddWarning(line, col, message);
  283. }
  284. }
  285. private:
  286. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ParserImpl);
  287. // Reports an error with the given message with information indicating
  288. // the position (as derived from the current token).
  289. void ReportError(const string& message) {
  290. ReportError(tokenizer_.current().line, tokenizer_.current().column,
  291. message);
  292. }
  293. // Reports a warning with the given message with information indicating
  294. // the position (as derived from the current token).
  295. void ReportWarning(const string& message) {
  296. ReportWarning(tokenizer_.current().line, tokenizer_.current().column,
  297. message);
  298. }
  299. // Consumes the specified message with the given starting delimiter.
  300. // This method checks to see that the end delimiter at the conclusion of
  301. // the consumption matches the starting delimiter passed in here.
  302. bool ConsumeMessage(Message* message, const string delimiter) {
  303. while (!LookingAt(">") && !LookingAt("}")) {
  304. DO(ConsumeField(message));
  305. }
  306. // Confirm that we have a valid ending delimiter.
  307. DO(Consume(delimiter));
  308. return true;
  309. }
  310. // Consume either "<" or "{".
  311. bool ConsumeMessageDelimiter(string* delimiter) {
  312. if (TryConsume("<")) {
  313. *delimiter = ">";
  314. } else {
  315. DO(Consume("{"));
  316. *delimiter = "}";
  317. }
  318. return true;
  319. }
  320. // Consumes the current field (as returned by the tokenizer) on the
  321. // passed in message.
  322. bool ConsumeField(Message* message) {
  323. const Reflection* reflection = message->GetReflection();
  324. const Descriptor* descriptor = message->GetDescriptor();
  325. string field_name;
  326. bool reserved_field = false;
  327. const FieldDescriptor* field = NULL;
  328. int start_line = tokenizer_.current().line;
  329. int start_column = tokenizer_.current().column;
  330. const FieldDescriptor* any_type_url_field;
  331. const FieldDescriptor* any_value_field;
  332. if (internal::GetAnyFieldDescriptors(*message, &any_type_url_field,
  333. &any_value_field) &&
  334. TryConsume("[")) {
  335. string full_type_name, prefix;
  336. DO(ConsumeAnyTypeUrl(&full_type_name, &prefix));
  337. DO(Consume("]"));
  338. TryConsume(":"); // ':' is optional between message labels and values.
  339. string serialized_value;
  340. const Descriptor* value_descriptor =
  341. finder_ ? finder_->FindAnyType(*message, prefix, full_type_name)
  342. : DefaultFinderFindAnyType(*message, prefix, full_type_name);
  343. if (value_descriptor == NULL) {
  344. ReportError("Could not find type \"" + prefix + full_type_name +
  345. "\" stored in google.protobuf.Any.");
  346. return false;
  347. }
  348. DO(ConsumeAnyValue(value_descriptor, &serialized_value));
  349. if (singular_overwrite_policy_ == FORBID_SINGULAR_OVERWRITES) {
  350. // Fail if any_type_url_field has already been specified.
  351. if ((!any_type_url_field->is_repeated() &&
  352. reflection->HasField(*message, any_type_url_field)) ||
  353. (!any_value_field->is_repeated() &&
  354. reflection->HasField(*message, any_value_field))) {
  355. ReportError("Non-repeated Any specified multiple times.");
  356. return false;
  357. }
  358. }
  359. reflection->SetString(
  360. message, any_type_url_field,
  361. string(prefix + full_type_name));
  362. reflection->SetString(message, any_value_field, serialized_value);
  363. return true;
  364. }
  365. if (TryConsume("[")) {
  366. // Extension.
  367. DO(ConsumeFullTypeName(&field_name));
  368. DO(Consume("]"));
  369. field = finder_ ? finder_->FindExtension(message, field_name)
  370. : DefaultFinderFindExtension(message, field_name);
  371. if (field == NULL) {
  372. if (!allow_unknown_field_) {
  373. ReportError("Extension \"" + field_name + "\" is not defined or "
  374. "is not an extension of \"" +
  375. descriptor->full_name() + "\".");
  376. return false;
  377. } else {
  378. ReportWarning("Extension \"" + field_name + "\" is not defined or "
  379. "is not an extension of \"" +
  380. descriptor->full_name() + "\".");
  381. }
  382. }
  383. } else {
  384. DO(ConsumeIdentifier(&field_name));
  385. int32 field_number;
  386. if (allow_field_number_ && safe_strto32(field_name, &field_number)) {
  387. if (descriptor->IsExtensionNumber(field_number)) {
  388. field = reflection->FindKnownExtensionByNumber(field_number);
  389. } else if (descriptor->IsReservedNumber(field_number)) {
  390. reserved_field = true;
  391. } else {
  392. field = descriptor->FindFieldByNumber(field_number);
  393. }
  394. } else {
  395. field = descriptor->FindFieldByName(field_name);
  396. // Group names are expected to be capitalized as they appear in the
  397. // .proto file, which actually matches their type names, not their
  398. // field names.
  399. if (field == NULL) {
  400. string lower_field_name = field_name;
  401. LowerString(&lower_field_name);
  402. field = descriptor->FindFieldByName(lower_field_name);
  403. // If the case-insensitive match worked but the field is NOT a group,
  404. if (field != NULL && field->type() != FieldDescriptor::TYPE_GROUP) {
  405. field = NULL;
  406. }
  407. }
  408. // Again, special-case group names as described above.
  409. if (field != NULL && field->type() == FieldDescriptor::TYPE_GROUP
  410. && field->message_type()->name() != field_name) {
  411. field = NULL;
  412. }
  413. if (field == NULL && allow_case_insensitive_field_) {
  414. string lower_field_name = field_name;
  415. LowerString(&lower_field_name);
  416. field = descriptor->FindFieldByLowercaseName(lower_field_name);
  417. }
  418. if (field == NULL) {
  419. reserved_field = descriptor->IsReservedName(field_name);
  420. }
  421. }
  422. if (field == NULL && !reserved_field) {
  423. if (!allow_unknown_field_) {
  424. ReportError("Message type \"" + descriptor->full_name() +
  425. "\" has no field named \"" + field_name + "\".");
  426. return false;
  427. } else {
  428. ReportWarning("Message type \"" + descriptor->full_name() +
  429. "\" has no field named \"" + field_name + "\".");
  430. }
  431. }
  432. }
  433. // Skips unknown or reserved fields.
  434. if (field == NULL) {
  435. GOOGLE_CHECK(allow_unknown_field_ || reserved_field);
  436. // Try to guess the type of this field.
  437. // If this field is not a message, there should be a ":" between the
  438. // field name and the field value and also the field value should not
  439. // start with "{" or "<" which indicates the beginning of a message body.
  440. // If there is no ":" or there is a "{" or "<" after ":", this field has
  441. // to be a message or the input is ill-formed.
  442. if (TryConsume(":") && !LookingAt("{") && !LookingAt("<")) {
  443. return SkipFieldValue();
  444. } else {
  445. return SkipFieldMessage();
  446. }
  447. }
  448. if (singular_overwrite_policy_ == FORBID_SINGULAR_OVERWRITES) {
  449. // Fail if the field is not repeated and it has already been specified.
  450. if (!field->is_repeated() && reflection->HasField(*message, field)) {
  451. ReportError("Non-repeated field \"" + field_name +
  452. "\" is specified multiple times.");
  453. return false;
  454. }
  455. // Fail if the field is a member of a oneof and another member has already
  456. // been specified.
  457. const OneofDescriptor* oneof = field->containing_oneof();
  458. if (oneof != NULL && reflection->HasOneof(*message, oneof)) {
  459. const FieldDescriptor* other_field =
  460. reflection->GetOneofFieldDescriptor(*message, oneof);
  461. ReportError("Field \"" + field_name + "\" is specified along with "
  462. "field \"" + other_field->name() + "\", another member "
  463. "of oneof \"" + oneof->name() + "\".");
  464. return false;
  465. }
  466. }
  467. // Perform special handling for embedded message types.
  468. if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
  469. // ':' is optional here.
  470. bool consumed_semicolon = TryConsume(":");
  471. if (consumed_semicolon && field->options().weak() && LookingAtType(io::Tokenizer::TYPE_STRING)) {
  472. // we are getting a bytes string for a weak field.
  473. string tmp;
  474. DO(ConsumeString(&tmp));
  475. reflection->MutableMessage(message, field)->ParseFromString(tmp);
  476. goto label_skip_parsing;
  477. }
  478. } else {
  479. // ':' is required here.
  480. DO(Consume(":"));
  481. }
  482. if (field->is_repeated() && TryConsume("[")) {
  483. // Short repeated format, e.g. "foo: [1, 2, 3]".
  484. if (!TryConsume("]")) {
  485. // "foo: []" is treated as empty.
  486. while (true) {
  487. if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
  488. // Perform special handling for embedded message types.
  489. DO(ConsumeFieldMessage(message, reflection, field));
  490. } else {
  491. DO(ConsumeFieldValue(message, reflection, field));
  492. }
  493. if (TryConsume("]")) {
  494. break;
  495. }
  496. DO(Consume(","));
  497. }
  498. }
  499. } else if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
  500. DO(ConsumeFieldMessage(message, reflection, field));
  501. } else {
  502. DO(ConsumeFieldValue(message, reflection, field));
  503. }
  504. label_skip_parsing:
  505. // For historical reasons, fields may optionally be separated by commas or
  506. // semicolons.
  507. TryConsume(";") || TryConsume(",");
  508. if (field->options().deprecated()) {
  509. ReportWarning("text format contains deprecated field \""
  510. + field_name + "\"");
  511. }
  512. // If a parse info tree exists, add the location for the parsed
  513. // field.
  514. if (parse_info_tree_ != NULL) {
  515. RecordLocation(parse_info_tree_, field,
  516. ParseLocation(start_line, start_column));
  517. }
  518. return true;
  519. }
  520. // Skips the next field including the field's name and value.
  521. bool SkipField() {
  522. if (TryConsume("[")) {
  523. // Extension name or type URL.
  524. DO(ConsumeTypeUrlOrFullTypeName());
  525. DO(Consume("]"));
  526. } else {
  527. string field_name;
  528. DO(ConsumeIdentifier(&field_name));
  529. }
  530. // Try to guess the type of this field.
  531. // If this field is not a message, there should be a ":" between the
  532. // field name and the field value and also the field value should not
  533. // start with "{" or "<" which indicates the beginning of a message body.
  534. // If there is no ":" or there is a "{" or "<" after ":", this field has
  535. // to be a message or the input is ill-formed.
  536. if (TryConsume(":") && !LookingAt("{") && !LookingAt("<")) {
  537. DO(SkipFieldValue());
  538. } else {
  539. DO(SkipFieldMessage());
  540. }
  541. // For historical reasons, fields may optionally be separated by commas or
  542. // semicolons.
  543. TryConsume(";") || TryConsume(",");
  544. return true;
  545. }
  546. bool ConsumeFieldMessage(Message* message,
  547. const Reflection* reflection,
  548. const FieldDescriptor* field) {
  549. // If the parse information tree is not NULL, create a nested one
  550. // for the nested message.
  551. ParseInfoTree* parent = parse_info_tree_;
  552. if (parent != NULL) {
  553. parse_info_tree_ = CreateNested(parent, field);
  554. }
  555. string delimiter;
  556. DO(ConsumeMessageDelimiter(&delimiter));
  557. if (field->is_repeated()) {
  558. DO(ConsumeMessage(reflection->AddMessage(message, field), delimiter));
  559. } else {
  560. DO(ConsumeMessage(reflection->MutableMessage(message, field),
  561. delimiter));
  562. }
  563. // Reset the parse information tree.
  564. parse_info_tree_ = parent;
  565. return true;
  566. }
  567. // Skips the whole body of a message including the beginning delimiter and
  568. // the ending delimiter.
  569. bool SkipFieldMessage() {
  570. string delimiter;
  571. DO(ConsumeMessageDelimiter(&delimiter));
  572. while (!LookingAt(">") && !LookingAt("}")) {
  573. DO(SkipField());
  574. }
  575. DO(Consume(delimiter));
  576. return true;
  577. }
  578. bool ConsumeFieldValue(Message* message,
  579. const Reflection* reflection,
  580. const FieldDescriptor* field) {
  581. // Define an easy to use macro for setting fields. This macro checks
  582. // to see if the field is repeated (in which case we need to use the Add
  583. // methods or not (in which case we need to use the Set methods).
  584. #define SET_FIELD(CPPTYPE, VALUE) \
  585. if (field->is_repeated()) { \
  586. reflection->Add##CPPTYPE(message, field, VALUE); \
  587. } else { \
  588. reflection->Set##CPPTYPE(message, field, VALUE); \
  589. } \
  590. switch(field->cpp_type()) {
  591. case FieldDescriptor::CPPTYPE_INT32: {
  592. int64 value;
  593. DO(ConsumeSignedInteger(&value, kint32max));
  594. SET_FIELD(Int32, static_cast<int32>(value));
  595. break;
  596. }
  597. case FieldDescriptor::CPPTYPE_UINT32: {
  598. uint64 value;
  599. DO(ConsumeUnsignedInteger(&value, kuint32max));
  600. SET_FIELD(UInt32, static_cast<uint32>(value));
  601. break;
  602. }
  603. case FieldDescriptor::CPPTYPE_INT64: {
  604. int64 value;
  605. DO(ConsumeSignedInteger(&value, kint64max));
  606. SET_FIELD(Int64, value);
  607. break;
  608. }
  609. case FieldDescriptor::CPPTYPE_UINT64: {
  610. uint64 value;
  611. DO(ConsumeUnsignedInteger(&value, kuint64max));
  612. SET_FIELD(UInt64, value);
  613. break;
  614. }
  615. case FieldDescriptor::CPPTYPE_FLOAT: {
  616. double value;
  617. DO(ConsumeDouble(&value));
  618. SET_FIELD(Float, io::SafeDoubleToFloat(value));
  619. break;
  620. }
  621. case FieldDescriptor::CPPTYPE_DOUBLE: {
  622. double value;
  623. DO(ConsumeDouble(&value));
  624. SET_FIELD(Double, value);
  625. break;
  626. }
  627. case FieldDescriptor::CPPTYPE_STRING: {
  628. string value;
  629. DO(ConsumeString(&value));
  630. SET_FIELD(String, value);
  631. break;
  632. }
  633. case FieldDescriptor::CPPTYPE_BOOL: {
  634. if (LookingAtType(io::Tokenizer::TYPE_INTEGER)) {
  635. uint64 value;
  636. DO(ConsumeUnsignedInteger(&value, 1));
  637. SET_FIELD(Bool, value);
  638. } else {
  639. string value;
  640. DO(ConsumeIdentifier(&value));
  641. if (value == "true" || value == "True" || value == "t") {
  642. SET_FIELD(Bool, true);
  643. } else if (value == "false" || value == "False" || value == "f") {
  644. SET_FIELD(Bool, false);
  645. } else {
  646. ReportError("Invalid value for boolean field \"" + field->name()
  647. + "\". Value: \"" + value + "\".");
  648. return false;
  649. }
  650. }
  651. break;
  652. }
  653. case FieldDescriptor::CPPTYPE_ENUM: {
  654. string value;
  655. const EnumDescriptor* enum_type = field->enum_type();
  656. const EnumValueDescriptor* enum_value = NULL;
  657. if (LookingAtType(io::Tokenizer::TYPE_IDENTIFIER)) {
  658. DO(ConsumeIdentifier(&value));
  659. // Find the enumeration value.
  660. enum_value = enum_type->FindValueByName(value);
  661. } else if (LookingAt("-") ||
  662. LookingAtType(io::Tokenizer::TYPE_INTEGER)) {
  663. int64 int_value;
  664. DO(ConsumeSignedInteger(&int_value, kint32max));
  665. value = SimpleItoa(int_value); // for error reporting
  666. enum_value = enum_type->FindValueByNumber(int_value);
  667. } else {
  668. ReportError("Expected integer or identifier, got: " +
  669. tokenizer_.current().text);
  670. return false;
  671. }
  672. if (enum_value == NULL) {
  673. if (!allow_unknown_enum_) {
  674. ReportError("Unknown enumeration value of \"" + value + "\" for "
  675. "field \"" + field->name() + "\".");
  676. return false;
  677. } else {
  678. ReportWarning("Unknown enumeration value of \"" + value + "\" for "
  679. "field \"" + field->name() + "\".");
  680. return true;
  681. }
  682. }
  683. SET_FIELD(Enum, enum_value);
  684. break;
  685. }
  686. case FieldDescriptor::CPPTYPE_MESSAGE: {
  687. // We should never get here. Put here instead of a default
  688. // so that if new types are added, we get a nice compiler warning.
  689. GOOGLE_LOG(FATAL) << "Reached an unintended state: CPPTYPE_MESSAGE";
  690. break;
  691. }
  692. }
  693. #undef SET_FIELD
  694. return true;
  695. }
  696. bool SkipFieldValue() {
  697. if (LookingAtType(io::Tokenizer::TYPE_STRING)) {
  698. while (LookingAtType(io::Tokenizer::TYPE_STRING)) {
  699. tokenizer_.Next();
  700. }
  701. return true;
  702. }
  703. if (TryConsume("[")) {
  704. while (true) {
  705. if (!LookingAt("{") && !LookingAt("<")) {
  706. DO(SkipFieldValue());
  707. } else {
  708. DO(SkipFieldMessage());
  709. }
  710. if (TryConsume("]")) {
  711. break;
  712. }
  713. DO(Consume(","));
  714. }
  715. return true;
  716. }
  717. // Possible field values other than string:
  718. // 12345 => TYPE_INTEGER
  719. // -12345 => TYPE_SYMBOL + TYPE_INTEGER
  720. // 1.2345 => TYPE_FLOAT
  721. // -1.2345 => TYPE_SYMBOL + TYPE_FLOAT
  722. // inf => TYPE_IDENTIFIER
  723. // -inf => TYPE_SYMBOL + TYPE_IDENTIFIER
  724. // TYPE_INTEGER => TYPE_IDENTIFIER
  725. // Divides them into two group, one with TYPE_SYMBOL
  726. // and the other without:
  727. // Group one:
  728. // 12345 => TYPE_INTEGER
  729. // 1.2345 => TYPE_FLOAT
  730. // inf => TYPE_IDENTIFIER
  731. // TYPE_INTEGER => TYPE_IDENTIFIER
  732. // Group two:
  733. // -12345 => TYPE_SYMBOL + TYPE_INTEGER
  734. // -1.2345 => TYPE_SYMBOL + TYPE_FLOAT
  735. // -inf => TYPE_SYMBOL + TYPE_IDENTIFIER
  736. // As we can see, the field value consists of an optional '-' and one of
  737. // TYPE_INTEGER, TYPE_FLOAT and TYPE_IDENTIFIER.
  738. bool has_minus = TryConsume("-");
  739. if (!LookingAtType(io::Tokenizer::TYPE_INTEGER) &&
  740. !LookingAtType(io::Tokenizer::TYPE_FLOAT) &&
  741. !LookingAtType(io::Tokenizer::TYPE_IDENTIFIER)) {
  742. return false;
  743. }
  744. // Combination of '-' and TYPE_IDENTIFIER may result in an invalid field
  745. // value while other combinations all generate valid values.
  746. // We check if the value of this combination is valid here.
  747. // TYPE_IDENTIFIER after a '-' should be one of the float values listed
  748. // below:
  749. // inf, inff, infinity, nan
  750. if (has_minus && LookingAtType(io::Tokenizer::TYPE_IDENTIFIER)) {
  751. string text = tokenizer_.current().text;
  752. LowerString(&text);
  753. if (text != "inf" &&
  754. text != "infinity" &&
  755. text != "nan") {
  756. ReportError("Invalid float number: " + text);
  757. return false;
  758. }
  759. }
  760. tokenizer_.Next();
  761. return true;
  762. }
  763. // Returns true if the current token's text is equal to that specified.
  764. bool LookingAt(const string& text) {
  765. return tokenizer_.current().text == text;
  766. }
  767. // Returns true if the current token's type is equal to that specified.
  768. bool LookingAtType(io::Tokenizer::TokenType token_type) {
  769. return tokenizer_.current().type == token_type;
  770. }
  771. // Consumes an identifier and saves its value in the identifier parameter.
  772. // Returns false if the token is not of type IDENTFIER.
  773. bool ConsumeIdentifier(string* identifier) {
  774. if (LookingAtType(io::Tokenizer::TYPE_IDENTIFIER)) {
  775. *identifier = tokenizer_.current().text;
  776. tokenizer_.Next();
  777. return true;
  778. }
  779. // If allow_field_numer_ or allow_unknown_field_ is true, we should able
  780. // to parse integer identifiers.
  781. if ((allow_field_number_ || allow_unknown_field_)
  782. && LookingAtType(io::Tokenizer::TYPE_INTEGER)) {
  783. *identifier = tokenizer_.current().text;
  784. tokenizer_.Next();
  785. return true;
  786. }
  787. ReportError("Expected identifier, got: " + tokenizer_.current().text);
  788. return false;
  789. }
  790. // Consume a string of form "<id1>.<id2>....<idN>".
  791. bool ConsumeFullTypeName(string* name) {
  792. DO(ConsumeIdentifier(name));
  793. while (TryConsume(".")) {
  794. string part;
  795. DO(ConsumeIdentifier(&part));
  796. *name += ".";
  797. *name += part;
  798. }
  799. return true;
  800. }
  801. bool ConsumeTypeUrlOrFullTypeName() {
  802. string discarded;
  803. DO(ConsumeIdentifier(&discarded));
  804. while (TryConsume(".") || TryConsume("/")) {
  805. DO(ConsumeIdentifier(&discarded));
  806. }
  807. return true;
  808. }
  809. // Consumes a string and saves its value in the text parameter.
  810. // Returns false if the token is not of type STRING.
  811. bool ConsumeString(string* text) {
  812. if (!LookingAtType(io::Tokenizer::TYPE_STRING)) {
  813. ReportError("Expected string, got: " + tokenizer_.current().text);
  814. return false;
  815. }
  816. text->clear();
  817. while (LookingAtType(io::Tokenizer::TYPE_STRING)) {
  818. io::Tokenizer::ParseStringAppend(tokenizer_.current().text, text);
  819. tokenizer_.Next();
  820. }
  821. return true;
  822. }
  823. // Consumes a uint64 and saves its value in the value parameter.
  824. // Returns false if the token is not of type INTEGER.
  825. bool ConsumeUnsignedInteger(uint64* value, uint64 max_value) {
  826. if (!LookingAtType(io::Tokenizer::TYPE_INTEGER)) {
  827. ReportError("Expected integer, got: " + tokenizer_.current().text);
  828. return false;
  829. }
  830. if (!io::Tokenizer::ParseInteger(tokenizer_.current().text,
  831. max_value, value)) {
  832. ReportError("Integer out of range (" + tokenizer_.current().text + ")");
  833. return false;
  834. }
  835. tokenizer_.Next();
  836. return true;
  837. }
  838. // Consumes an int64 and saves its value in the value parameter.
  839. // Note that since the tokenizer does not support negative numbers,
  840. // we actually may consume an additional token (for the minus sign) in this
  841. // method. Returns false if the token is not an integer
  842. // (signed or otherwise).
  843. bool ConsumeSignedInteger(int64* value, uint64 max_value) {
  844. bool negative = false;
  845. if (TryConsume("-")) {
  846. negative = true;
  847. // Two's complement always allows one more negative integer than
  848. // positive.
  849. ++max_value;
  850. }
  851. uint64 unsigned_value;
  852. DO(ConsumeUnsignedInteger(&unsigned_value, max_value));
  853. if (negative) {
  854. if ((static_cast<uint64>(kint64max) + 1) == unsigned_value) {
  855. *value = kint64min;
  856. } else {
  857. *value = -static_cast<int64>(unsigned_value);
  858. }
  859. } else {
  860. *value = static_cast<int64>(unsigned_value);
  861. }
  862. return true;
  863. }
  864. // Consumes a uint64 and saves its value in the value parameter.
  865. // Accepts decimal numbers only, rejects hex or oct numbers.
  866. bool ConsumeUnsignedDecimalInteger(uint64* value, uint64 max_value) {
  867. if (!LookingAtType(io::Tokenizer::TYPE_INTEGER)) {
  868. ReportError("Expected integer, got: " + tokenizer_.current().text);
  869. return false;
  870. }
  871. const string& text = tokenizer_.current().text;
  872. if (IsHexNumber(text) || IsOctNumber(text)) {
  873. ReportError("Expect a decimal number, got: " + text);
  874. return false;
  875. }
  876. if (!io::Tokenizer::ParseInteger(text, max_value, value)) {
  877. ReportError("Integer out of range (" + text + ")");
  878. return false;
  879. }
  880. tokenizer_.Next();
  881. return true;
  882. }
  883. // Consumes a double and saves its value in the value parameter.
  884. // Note that since the tokenizer does not support negative numbers,
  885. // we actually may consume an additional token (for the minus sign) in this
  886. // method. Returns false if the token is not a double
  887. // (signed or otherwise).
  888. bool ConsumeDouble(double* value) {
  889. bool negative = false;
  890. if (TryConsume("-")) {
  891. negative = true;
  892. }
  893. // A double can actually be an integer, according to the tokenizer.
  894. // Therefore, we must check both cases here.
  895. if (LookingAtType(io::Tokenizer::TYPE_INTEGER)) {
  896. // We have found an integer value for the double.
  897. uint64 integer_value;
  898. DO(ConsumeUnsignedDecimalInteger(&integer_value, kuint64max));
  899. *value = static_cast<double>(integer_value);
  900. } else if (LookingAtType(io::Tokenizer::TYPE_FLOAT)) {
  901. // We have found a float value for the double.
  902. *value = io::Tokenizer::ParseFloat(tokenizer_.current().text);
  903. // Mark the current token as consumed.
  904. tokenizer_.Next();
  905. } else if (LookingAtType(io::Tokenizer::TYPE_IDENTIFIER)) {
  906. string text = tokenizer_.current().text;
  907. LowerString(&text);
  908. if (text == "inf" ||
  909. text == "infinity") {
  910. *value = std::numeric_limits<double>::infinity();
  911. tokenizer_.Next();
  912. } else if (text == "nan") {
  913. *value = std::numeric_limits<double>::quiet_NaN();
  914. tokenizer_.Next();
  915. } else {
  916. ReportError("Expected double, got: " + text);
  917. return false;
  918. }
  919. } else {
  920. ReportError("Expected double, got: " + tokenizer_.current().text);
  921. return false;
  922. }
  923. if (negative) {
  924. *value = -*value;
  925. }
  926. return true;
  927. }
  928. // Consumes Any::type_url value, of form "type.googleapis.com/full.type.Name"
  929. // or "type.googleprod.com/full.type.Name"
  930. bool ConsumeAnyTypeUrl(string* full_type_name, string* prefix) {
  931. // TODO(saito) Extend Consume() to consume multiple tokens at once, so that
  932. // this code can be written as just DO(Consume(kGoogleApisTypePrefix)).
  933. DO(ConsumeIdentifier(prefix));
  934. while (TryConsume(".")) {
  935. string url;
  936. DO(ConsumeIdentifier(&url));
  937. *prefix += "." + url;
  938. }
  939. DO(Consume("/"));
  940. *prefix += "/";
  941. DO(ConsumeFullTypeName(full_type_name));
  942. return true;
  943. }
  944. // A helper function for reconstructing Any::value. Consumes a text of
  945. // full_type_name, then serializes it into serialized_value.
  946. bool ConsumeAnyValue(const Descriptor* value_descriptor,
  947. string* serialized_value) {
  948. DynamicMessageFactory factory;
  949. const Message* value_prototype = factory.GetPrototype(value_descriptor);
  950. if (value_prototype == NULL) {
  951. return false;
  952. }
  953. std::unique_ptr<Message> value(value_prototype->New());
  954. string sub_delimiter;
  955. DO(ConsumeMessageDelimiter(&sub_delimiter));
  956. DO(ConsumeMessage(value.get(), sub_delimiter));
  957. if (allow_partial_) {
  958. value->AppendPartialToString(serialized_value);
  959. } else {
  960. if (!value->IsInitialized()) {
  961. ReportError(
  962. "Value of type \"" + value_descriptor->full_name() +
  963. "\" stored in google.protobuf.Any has missing required fields");
  964. return false;
  965. }
  966. value->AppendToString(serialized_value);
  967. }
  968. return true;
  969. }
  970. // Consumes a token and confirms that it matches that specified in the
  971. // value parameter. Returns false if the token found does not match that
  972. // which was specified.
  973. bool Consume(const string& value) {
  974. const string& current_value = tokenizer_.current().text;
  975. if (current_value != value) {
  976. ReportError("Expected \"" + value + "\", found \"" + current_value
  977. + "\".");
  978. return false;
  979. }
  980. tokenizer_.Next();
  981. return true;
  982. }
  983. // Attempts to consume the supplied value. Returns false if a the
  984. // token found does not match the value specified.
  985. bool TryConsume(const string& value) {
  986. if (tokenizer_.current().text == value) {
  987. tokenizer_.Next();
  988. return true;
  989. } else {
  990. return false;
  991. }
  992. }
  993. // An internal instance of the Tokenizer's error collector, used to
  994. // collect any base-level parse errors and feed them to the ParserImpl.
  995. class ParserErrorCollector : public io::ErrorCollector {
  996. public:
  997. explicit ParserErrorCollector(TextFormat::Parser::ParserImpl* parser) :
  998. parser_(parser) { }
  999. virtual ~ParserErrorCollector() { }
  1000. virtual void AddError(int line, int column, const string& message) {
  1001. parser_->ReportError(line, column, message);
  1002. }
  1003. virtual void AddWarning(int line, int column, const string& message) {
  1004. parser_->ReportWarning(line, column, message);
  1005. }
  1006. private:
  1007. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ParserErrorCollector);
  1008. TextFormat::Parser::ParserImpl* parser_;
  1009. };
  1010. io::ErrorCollector* error_collector_;
  1011. const TextFormat::Finder* finder_;
  1012. ParseInfoTree* parse_info_tree_;
  1013. ParserErrorCollector tokenizer_error_collector_;
  1014. io::Tokenizer tokenizer_;
  1015. const Descriptor* root_message_type_;
  1016. SingularOverwritePolicy singular_overwrite_policy_;
  1017. const bool allow_case_insensitive_field_;
  1018. const bool allow_unknown_field_;
  1019. const bool allow_unknown_enum_;
  1020. const bool allow_field_number_;
  1021. const bool allow_partial_;
  1022. bool had_errors_;
  1023. };
  1024. #undef DO
  1025. // ===========================================================================
  1026. // Internal class for writing text to the io::ZeroCopyOutputStream. Adapted
  1027. // from the Printer found in //google/protobuf/io/printer.h
  1028. class TextFormat::Printer::TextGenerator
  1029. : public TextFormat::BaseTextGenerator {
  1030. public:
  1031. explicit TextGenerator(io::ZeroCopyOutputStream* output,
  1032. int initial_indent_level)
  1033. : output_(output),
  1034. buffer_(NULL),
  1035. buffer_size_(0),
  1036. at_start_of_line_(true),
  1037. failed_(false),
  1038. indent_level_(initial_indent_level),
  1039. initial_indent_level_(initial_indent_level) {
  1040. }
  1041. ~TextGenerator() {
  1042. // Only BackUp() if we're sure we've successfully called Next() at least
  1043. // once.
  1044. if (!failed_ && buffer_size_ > 0) {
  1045. output_->BackUp(buffer_size_);
  1046. }
  1047. }
  1048. // Indent text by two spaces. After calling Indent(), two spaces will be
  1049. // inserted at the beginning of each line of text. Indent() may be called
  1050. // multiple times to produce deeper indents.
  1051. void Indent() override { ++indent_level_; }
  1052. // Reduces the current indent level by two spaces, or crashes if the indent
  1053. // level is zero.
  1054. void Outdent() override {
  1055. if (indent_level_ == 0 ||
  1056. indent_level_ < initial_indent_level_) {
  1057. GOOGLE_LOG(DFATAL) << " Outdent() without matching Indent().";
  1058. return;
  1059. }
  1060. --indent_level_;
  1061. }
  1062. // Print text to the output stream.
  1063. void Print(const char* text, size_t size) override {
  1064. if (indent_level_ > 0) {
  1065. size_t pos = 0; // The number of bytes we've written so far.
  1066. for (size_t i = 0; i < size; i++) {
  1067. if (text[i] == '\n') {
  1068. // Saw newline. If there is more text, we may need to insert an
  1069. // indent here. So, write what we have so far, including the '\n'.
  1070. Write(text + pos, i - pos + 1);
  1071. pos = i + 1;
  1072. // Setting this true will cause the next Write() to insert an indent
  1073. // first.
  1074. at_start_of_line_ = true;
  1075. }
  1076. }
  1077. // Write the rest.
  1078. Write(text + pos, size - pos);
  1079. } else {
  1080. Write(text, size);
  1081. if (size > 0 && text[size - 1] == '\n') {
  1082. at_start_of_line_ = true;
  1083. }
  1084. }
  1085. }
  1086. // True if any write to the underlying stream failed. (We don't just
  1087. // crash in this case because this is an I/O failure, not a programming
  1088. // error.)
  1089. bool failed() const { return failed_; }
  1090. private:
  1091. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TextGenerator);
  1092. void Write(const char* data, size_t size) {
  1093. if (failed_) return;
  1094. if (size == 0) return;
  1095. if (at_start_of_line_) {
  1096. // Insert an indent.
  1097. at_start_of_line_ = false;
  1098. WriteIndent();
  1099. if (failed_) return;
  1100. }
  1101. while (size > buffer_size_) {
  1102. // Data exceeds space in the buffer. Copy what we can and request a
  1103. // new buffer.
  1104. if (buffer_size_ > 0) {
  1105. memcpy(buffer_, data, buffer_size_);
  1106. data += buffer_size_;
  1107. size -= buffer_size_;
  1108. }
  1109. void* void_buffer = NULL;
  1110. failed_ = !output_->Next(&void_buffer, &buffer_size_);
  1111. if (failed_) return;
  1112. buffer_ = reinterpret_cast<char*>(void_buffer);
  1113. }
  1114. // Buffer is big enough to receive the data; copy it.
  1115. memcpy(buffer_, data, size);
  1116. buffer_ += size;
  1117. buffer_size_ -= size;
  1118. }
  1119. void WriteIndent() {
  1120. if (indent_level_ == 0) { return; }
  1121. GOOGLE_DCHECK(!failed_);
  1122. int size = 2 * indent_level_;
  1123. while (size > buffer_size_) {
  1124. // Data exceeds space in the buffer. Write what we can and request a new
  1125. // buffer.
  1126. memset(buffer_, ' ', buffer_size_);
  1127. size -= buffer_size_;
  1128. void* void_buffer;
  1129. failed_ = !output_->Next(&void_buffer, &buffer_size_);
  1130. if (failed_) return;
  1131. buffer_ = reinterpret_cast<char*>(void_buffer);
  1132. }
  1133. // Buffer is big enough to receive the data; copy it.
  1134. memset(buffer_, ' ', size);
  1135. buffer_ += size;
  1136. buffer_size_ -= size;
  1137. }
  1138. io::ZeroCopyOutputStream* const output_;
  1139. char* buffer_;
  1140. int buffer_size_;
  1141. bool at_start_of_line_;
  1142. bool failed_;
  1143. int indent_level_;
  1144. int initial_indent_level_;
  1145. };
  1146. // ===========================================================================
  1147. // Implementation of the default Finder for extensions.
  1148. TextFormat::Finder::~Finder() {
  1149. }
  1150. const FieldDescriptor* TextFormat::Finder::FindExtension(
  1151. Message* message, const string& name) const {
  1152. return DefaultFinderFindExtension(message, name);
  1153. }
  1154. const Descriptor* TextFormat::Finder::FindAnyType(const Message& message,
  1155. const string& prefix,
  1156. const string& name) const {
  1157. return DefaultFinderFindAnyType(message, prefix, name);
  1158. }
  1159. // ===========================================================================
  1160. TextFormat::Parser::Parser()
  1161. : error_collector_(NULL),
  1162. finder_(NULL),
  1163. parse_info_tree_(NULL),
  1164. allow_partial_(false),
  1165. allow_case_insensitive_field_(false),
  1166. allow_unknown_field_(false),
  1167. allow_unknown_enum_(false),
  1168. allow_field_number_(false),
  1169. allow_relaxed_whitespace_(false),
  1170. allow_singular_overwrites_(false) {
  1171. }
  1172. TextFormat::Parser::~Parser() {}
  1173. bool TextFormat::Parser::Parse(io::ZeroCopyInputStream* input,
  1174. Message* output) {
  1175. output->Clear();
  1176. ParserImpl::SingularOverwritePolicy overwrites_policy =
  1177. allow_singular_overwrites_
  1178. ? ParserImpl::ALLOW_SINGULAR_OVERWRITES
  1179. : ParserImpl::FORBID_SINGULAR_OVERWRITES;
  1180. ParserImpl parser(output->GetDescriptor(), input, error_collector_,
  1181. finder_, parse_info_tree_,
  1182. overwrites_policy,
  1183. allow_case_insensitive_field_, allow_unknown_field_,
  1184. allow_unknown_enum_, allow_field_number_,
  1185. allow_relaxed_whitespace_, allow_partial_);
  1186. return MergeUsingImpl(input, output, &parser);
  1187. }
  1188. bool TextFormat::Parser::ParseFromString(const string& input,
  1189. Message* output) {
  1190. io::ArrayInputStream input_stream(input.data(), input.size());
  1191. return Parse(&input_stream, output);
  1192. }
  1193. bool TextFormat::Parser::Merge(io::ZeroCopyInputStream* input,
  1194. Message* output) {
  1195. ParserImpl parser(output->GetDescriptor(), input, error_collector_,
  1196. finder_, parse_info_tree_,
  1197. ParserImpl::ALLOW_SINGULAR_OVERWRITES,
  1198. allow_case_insensitive_field_, allow_unknown_field_,
  1199. allow_unknown_enum_, allow_field_number_,
  1200. allow_relaxed_whitespace_, allow_partial_);
  1201. return MergeUsingImpl(input, output, &parser);
  1202. }
  1203. bool TextFormat::Parser::MergeFromString(const string& input,
  1204. Message* output) {
  1205. io::ArrayInputStream input_stream(input.data(), input.size());
  1206. return Merge(&input_stream, output);
  1207. }
  1208. bool TextFormat::Parser::MergeUsingImpl(io::ZeroCopyInputStream* /* input */,
  1209. Message* output,
  1210. ParserImpl* parser_impl) {
  1211. if (!parser_impl->Parse(output)) return false;
  1212. if (!allow_partial_ && !output->IsInitialized()) {
  1213. std::vector<string> missing_fields;
  1214. output->FindInitializationErrors(&missing_fields);
  1215. parser_impl->ReportError(-1, 0,
  1216. "Message missing required fields: " +
  1217. Join(missing_fields, ", "));
  1218. return false;
  1219. }
  1220. return true;
  1221. }
  1222. bool TextFormat::Parser::ParseFieldValueFromString(
  1223. const string& input,
  1224. const FieldDescriptor* field,
  1225. Message* output) {
  1226. io::ArrayInputStream input_stream(input.data(), input.size());
  1227. ParserImpl parser(output->GetDescriptor(), &input_stream, error_collector_,
  1228. finder_, parse_info_tree_,
  1229. ParserImpl::ALLOW_SINGULAR_OVERWRITES,
  1230. allow_case_insensitive_field_, allow_unknown_field_,
  1231. allow_unknown_enum_, allow_field_number_,
  1232. allow_relaxed_whitespace_, allow_partial_);
  1233. return parser.ParseField(field, output);
  1234. }
  1235. /* static */ bool TextFormat::Parse(io::ZeroCopyInputStream* input,
  1236. Message* output) {
  1237. return Parser().Parse(input, output);
  1238. }
  1239. /* static */ bool TextFormat::Merge(io::ZeroCopyInputStream* input,
  1240. Message* output) {
  1241. return Parser().Merge(input, output);
  1242. }
  1243. /* static */ bool TextFormat::ParseFromString(const string& input,
  1244. Message* output) {
  1245. return Parser().ParseFromString(input, output);
  1246. }
  1247. /* static */ bool TextFormat::MergeFromString(const string& input,
  1248. Message* output) {
  1249. return Parser().MergeFromString(input, output);
  1250. }
  1251. // ===========================================================================
  1252. TextFormat::BaseTextGenerator::~BaseTextGenerator() {}
  1253. namespace {
  1254. // A BaseTextGenerator that writes to a string.
  1255. class StringBaseTextGenerator : public TextFormat::BaseTextGenerator {
  1256. public:
  1257. void Print(const char* text, size_t size) { output_.append(text, size); }
  1258. // Some compilers do not support ref-qualifiers even in C++11 mode.
  1259. // Disable the optimization for now and revisit it later.
  1260. #if 0 // LANG_CXX11
  1261. string Consume() && { return std::move(output_); }
  1262. #else // !LANG_CXX11
  1263. const string& Get() { return output_; }
  1264. #endif // LANG_CXX11
  1265. private:
  1266. string output_;
  1267. };
  1268. } // namespace
  1269. // The default implementation for FieldValuePrinter. We just delegate the
  1270. // implementation to the default FastFieldValuePrinter to avoid duplicating the
  1271. // logic.
  1272. TextFormat::FieldValuePrinter::FieldValuePrinter() {}
  1273. TextFormat::FieldValuePrinter::~FieldValuePrinter() {}
  1274. #if 0 // LANG_CXX11
  1275. #define FORWARD_IMPL(fn, ...) \
  1276. StringBaseTextGenerator generator; \
  1277. delegate_.fn(__VA_ARGS__, &generator); \
  1278. return std::move(generator).Consume()
  1279. #else // !LANG_CXX11
  1280. #define FORWARD_IMPL(fn, ...) \
  1281. StringBaseTextGenerator generator; \
  1282. delegate_.fn(__VA_ARGS__, &generator); \
  1283. return generator.Get()
  1284. #endif // LANG_CXX11
  1285. string TextFormat::FieldValuePrinter::PrintBool(bool val) const {
  1286. FORWARD_IMPL(PrintBool, val);
  1287. }
  1288. string TextFormat::FieldValuePrinter::PrintInt32(int32 val) const {
  1289. FORWARD_IMPL(PrintInt32, val);
  1290. }
  1291. string TextFormat::FieldValuePrinter::PrintUInt32(uint32 val) const {
  1292. FORWARD_IMPL(PrintUInt32, val);
  1293. }
  1294. string TextFormat::FieldValuePrinter::PrintInt64(int64 val) const {
  1295. FORWARD_IMPL(PrintInt64, val);
  1296. }
  1297. string TextFormat::FieldValuePrinter::PrintUInt64(uint64 val) const {
  1298. FORWARD_IMPL(PrintUInt64, val);
  1299. }
  1300. string TextFormat::FieldValuePrinter::PrintFloat(float val) const {
  1301. FORWARD_IMPL(PrintFloat, val);
  1302. }
  1303. string TextFormat::FieldValuePrinter::PrintDouble(double val) const {
  1304. FORWARD_IMPL(PrintDouble, val);
  1305. }
  1306. string TextFormat::FieldValuePrinter::PrintString(const string& val) const {
  1307. FORWARD_IMPL(PrintString, val);
  1308. }
  1309. string TextFormat::FieldValuePrinter::PrintBytes(const string& val) const {
  1310. return PrintString(val);
  1311. }
  1312. string TextFormat::FieldValuePrinter::PrintEnum(int32 val,
  1313. const string& name) const {
  1314. FORWARD_IMPL(PrintEnum, val, name);
  1315. }
  1316. string TextFormat::FieldValuePrinter::PrintFieldName(
  1317. const Message& message,
  1318. const Reflection* reflection,
  1319. const FieldDescriptor* field) const {
  1320. FORWARD_IMPL(PrintFieldName, message, reflection, field);
  1321. }
  1322. string TextFormat::FieldValuePrinter::PrintMessageStart(
  1323. const Message& message,
  1324. int field_index,
  1325. int field_count,
  1326. bool single_line_mode) const {
  1327. FORWARD_IMPL(PrintMessageStart, message, field_index, field_count,
  1328. single_line_mode);
  1329. }
  1330. string TextFormat::FieldValuePrinter::PrintMessageEnd(
  1331. const Message& message,
  1332. int field_index,
  1333. int field_count,
  1334. bool single_line_mode) const {
  1335. FORWARD_IMPL(PrintMessageEnd, message, field_index, field_count,
  1336. single_line_mode);
  1337. }
  1338. #undef FORWARD_IMPL
  1339. TextFormat::FastFieldValuePrinter::FastFieldValuePrinter() {}
  1340. TextFormat::FastFieldValuePrinter::~FastFieldValuePrinter() {}
  1341. void TextFormat::FastFieldValuePrinter::PrintBool(
  1342. bool val, BaseTextGenerator* generator) const {
  1343. if (val) {
  1344. generator->PrintLiteral("true");
  1345. } else {
  1346. generator->PrintLiteral("false");
  1347. }
  1348. }
  1349. void TextFormat::FastFieldValuePrinter::PrintInt32(
  1350. int32 val, BaseTextGenerator* generator) const {
  1351. generator->PrintString(SimpleItoa(val));
  1352. }
  1353. void TextFormat::FastFieldValuePrinter::PrintUInt32(
  1354. uint32 val, BaseTextGenerator* generator) const {
  1355. generator->PrintString(SimpleItoa(val));
  1356. }
  1357. void TextFormat::FastFieldValuePrinter::PrintInt64(
  1358. int64 val, BaseTextGenerator* generator) const {
  1359. generator->PrintString(SimpleItoa(val));
  1360. }
  1361. void TextFormat::FastFieldValuePrinter::PrintUInt64(
  1362. uint64 val, BaseTextGenerator* generator) const {
  1363. generator->PrintString(SimpleItoa(val));
  1364. }
  1365. void TextFormat::FastFieldValuePrinter::PrintFloat(
  1366. float val, BaseTextGenerator* generator) const {
  1367. generator->PrintString(SimpleFtoa(val));
  1368. }
  1369. void TextFormat::FastFieldValuePrinter::PrintDouble(
  1370. double val, BaseTextGenerator* generator) const {
  1371. generator->PrintString(SimpleDtoa(val));
  1372. }
  1373. void TextFormat::FastFieldValuePrinter::PrintEnum(
  1374. int32 val, const string& name, BaseTextGenerator* generator) const {
  1375. generator->PrintString(name);
  1376. }
  1377. void TextFormat::FastFieldValuePrinter::PrintString(
  1378. const string& val, BaseTextGenerator* generator) const {
  1379. generator->PrintLiteral("\"");
  1380. generator->PrintString(CEscape(val));
  1381. generator->PrintLiteral("\"");
  1382. }
  1383. void TextFormat::FastFieldValuePrinter::PrintBytes(
  1384. const string& val, BaseTextGenerator* generator) const {
  1385. PrintString(val, generator);
  1386. }
  1387. void TextFormat::FastFieldValuePrinter::PrintFieldName(
  1388. const Message& message, int field_index, int field_count,
  1389. const Reflection* reflection, const FieldDescriptor* field,
  1390. BaseTextGenerator* generator) const {
  1391. PrintFieldName(message, reflection, field, generator);
  1392. }
  1393. void TextFormat::FastFieldValuePrinter::PrintFieldName(
  1394. const Message& message, const Reflection* reflection,
  1395. const FieldDescriptor* field, BaseTextGenerator* generator) const {
  1396. if (field->is_extension()) {
  1397. generator->PrintLiteral("[");
  1398. // We special-case MessageSet elements for compatibility with proto1.
  1399. if (field->containing_type()->options().message_set_wire_format() &&
  1400. field->type() == FieldDescriptor::TYPE_MESSAGE &&
  1401. field->is_optional() &&
  1402. field->extension_scope() == field->message_type()) {
  1403. generator->PrintString(field->message_type()->full_name());
  1404. } else {
  1405. generator->PrintString(field->full_name());
  1406. }
  1407. generator->PrintLiteral("]");
  1408. } else if (field->type() == FieldDescriptor::TYPE_GROUP) {
  1409. // Groups must be serialized with their original capitalization.
  1410. generator->PrintString(field->message_type()->name());
  1411. } else {
  1412. generator->PrintString(field->name());
  1413. }
  1414. }
  1415. void TextFormat::FastFieldValuePrinter::PrintMessageStart(
  1416. const Message& message, int field_index, int field_count,
  1417. bool single_line_mode, BaseTextGenerator* generator) const {
  1418. if (single_line_mode) {
  1419. generator->PrintLiteral(" { ");
  1420. } else {
  1421. generator->PrintLiteral(" {\n");
  1422. }
  1423. }
  1424. void TextFormat::FastFieldValuePrinter::PrintMessageEnd(
  1425. const Message& message, int field_index, int field_count,
  1426. bool single_line_mode, BaseTextGenerator* generator) const {
  1427. if (single_line_mode) {
  1428. generator->PrintLiteral("} ");
  1429. } else {
  1430. generator->PrintLiteral("}\n");
  1431. }
  1432. }
  1433. namespace {
  1434. // A legacy compatibility wrapper. Takes ownership of the delegate.
  1435. class FieldValuePrinterWrapper : public TextFormat::FastFieldValuePrinter {
  1436. public:
  1437. explicit FieldValuePrinterWrapper(
  1438. const TextFormat::FieldValuePrinter* delegate)
  1439. : delegate_(delegate) {}
  1440. void SetDelegate(const TextFormat::FieldValuePrinter* delegate) {
  1441. delegate_.reset(delegate);
  1442. }
  1443. void PrintBool(bool val, TextFormat::BaseTextGenerator* generator) const {
  1444. generator->PrintString(delegate_->PrintBool(val));
  1445. }
  1446. void PrintInt32(int32 val, TextFormat::BaseTextGenerator* generator) const {
  1447. generator->PrintString(delegate_->PrintInt32(val));
  1448. }
  1449. void PrintUInt32(uint32 val, TextFormat::BaseTextGenerator* generator) const {
  1450. generator->PrintString(delegate_->PrintUInt32(val));
  1451. }
  1452. void PrintInt64(int64 val, TextFormat::BaseTextGenerator* generator) const {
  1453. generator->PrintString(delegate_->PrintInt64(val));
  1454. }
  1455. void PrintUInt64(uint64 val, TextFormat::BaseTextGenerator* generator) const {
  1456. generator->PrintString(delegate_->PrintUInt64(val));
  1457. }
  1458. void PrintFloat(float val, TextFormat::BaseTextGenerator* generator) const {
  1459. generator->PrintString(delegate_->PrintFloat(val));
  1460. }
  1461. void PrintDouble(double val, TextFormat::BaseTextGenerator* generator) const {
  1462. generator->PrintString(delegate_->PrintDouble(val));
  1463. }
  1464. void PrintString(const string& val,
  1465. TextFormat::BaseTextGenerator* generator) const {
  1466. generator->PrintString(delegate_->PrintString(val));
  1467. }
  1468. void PrintBytes(const string& val,
  1469. TextFormat::BaseTextGenerator* generator) const {
  1470. generator->PrintString(delegate_->PrintBytes(val));
  1471. }
  1472. void PrintEnum(int32 val, const string& name,
  1473. TextFormat::BaseTextGenerator* generator) const {
  1474. generator->PrintString(delegate_->PrintEnum(val, name));
  1475. }
  1476. void PrintFieldName(const Message& message, int field_index, int field_count,
  1477. const Reflection* reflection,
  1478. const FieldDescriptor* field,
  1479. TextFormat::BaseTextGenerator* generator) const {
  1480. generator->PrintString(delegate_->PrintFieldName(
  1481. message, reflection, field));
  1482. }
  1483. void PrintFieldName(const Message& message, const Reflection* reflection,
  1484. const FieldDescriptor* field,
  1485. TextFormat::BaseTextGenerator* generator) const {
  1486. generator->PrintString(
  1487. delegate_->PrintFieldName(message, reflection, field));
  1488. }
  1489. void PrintMessageStart(const Message& message, int field_index,
  1490. int field_count, bool single_line_mode,
  1491. TextFormat::BaseTextGenerator* generator) const {
  1492. generator->PrintString(delegate_->PrintMessageStart(
  1493. message, field_index, field_count, single_line_mode));
  1494. }
  1495. void PrintMessageEnd(const Message& message, int field_index, int field_count,
  1496. bool single_line_mode,
  1497. TextFormat::BaseTextGenerator* generator) const {
  1498. generator->PrintString(delegate_->PrintMessageEnd(
  1499. message, field_index, field_count, single_line_mode));
  1500. }
  1501. private:
  1502. std::unique_ptr<const TextFormat::FieldValuePrinter> delegate_;
  1503. };
  1504. // Our own specialization: for UTF8 escaped strings.
  1505. class FastFieldValuePrinterUtf8Escaping
  1506. : public TextFormat::FastFieldValuePrinter {
  1507. public:
  1508. void PrintString(const string& val,
  1509. TextFormat::BaseTextGenerator* generator) const {
  1510. generator->PrintLiteral("\"");
  1511. generator->PrintString(strings::Utf8SafeCEscape(val));
  1512. generator->PrintLiteral("\"");
  1513. }
  1514. void PrintBytes(const string& val,
  1515. TextFormat::BaseTextGenerator* generator) const {
  1516. return FastFieldValuePrinter::PrintString(val, generator);
  1517. }
  1518. };
  1519. } // namespace
  1520. TextFormat::Printer::Printer()
  1521. : initial_indent_level_(0),
  1522. single_line_mode_(false),
  1523. use_field_number_(false),
  1524. use_short_repeated_primitives_(false),
  1525. hide_unknown_fields_(false),
  1526. print_message_fields_in_index_order_(false),
  1527. expand_any_(false),
  1528. truncate_string_field_longer_than_(0LL),
  1529. finder_(NULL) {
  1530. SetUseUtf8StringEscaping(false);
  1531. }
  1532. TextFormat::Printer::~Printer() {
  1533. STLDeleteValues(&custom_printers_);
  1534. STLDeleteValues(&custom_message_printers_);
  1535. }
  1536. void TextFormat::Printer::SetUseUtf8StringEscaping(bool as_utf8) {
  1537. SetDefaultFieldValuePrinter(as_utf8 ? new FastFieldValuePrinterUtf8Escaping()
  1538. : new FastFieldValuePrinter());
  1539. }
  1540. void TextFormat::Printer::SetDefaultFieldValuePrinter(
  1541. const FieldValuePrinter* printer) {
  1542. default_field_value_printer_.reset(new FieldValuePrinterWrapper(printer));
  1543. }
  1544. void TextFormat::Printer::SetDefaultFieldValuePrinter(
  1545. const FastFieldValuePrinter* printer) {
  1546. default_field_value_printer_.reset(printer);
  1547. }
  1548. bool TextFormat::Printer::RegisterFieldValuePrinter(
  1549. const FieldDescriptor* field,
  1550. const FieldValuePrinter* printer) {
  1551. if (field == NULL || printer == NULL) {
  1552. return false;
  1553. }
  1554. FieldValuePrinterWrapper* const wrapper =
  1555. new FieldValuePrinterWrapper(nullptr);
  1556. if (custom_printers_.insert(std::make_pair(field, wrapper)).second) {
  1557. wrapper->SetDelegate(printer);
  1558. return true;
  1559. } else {
  1560. delete wrapper;
  1561. return false;
  1562. }
  1563. }
  1564. bool TextFormat::Printer::RegisterFieldValuePrinter(
  1565. const FieldDescriptor* field, const FastFieldValuePrinter* printer) {
  1566. return field != NULL && printer != NULL &&
  1567. custom_printers_.insert(std::make_pair(field, printer)).second;
  1568. }
  1569. bool TextFormat::Printer::RegisterMessagePrinter(
  1570. const Descriptor* descriptor, const MessagePrinter* printer) {
  1571. return descriptor != nullptr && printer != nullptr &&
  1572. custom_message_printers_.insert(std::make_pair(descriptor, printer))
  1573. .second;
  1574. }
  1575. bool TextFormat::Printer::PrintToString(const Message& message,
  1576. string* output) const {
  1577. GOOGLE_DCHECK(output) << "output specified is NULL";
  1578. output->clear();
  1579. io::StringOutputStream output_stream(output);
  1580. return Print(message, &output_stream);
  1581. }
  1582. bool TextFormat::Printer::PrintUnknownFieldsToString(
  1583. const UnknownFieldSet& unknown_fields,
  1584. string* output) const {
  1585. GOOGLE_DCHECK(output) << "output specified is NULL";
  1586. output->clear();
  1587. io::StringOutputStream output_stream(output);
  1588. return PrintUnknownFields(unknown_fields, &output_stream);
  1589. }
  1590. bool TextFormat::Printer::Print(const Message& message,
  1591. io::ZeroCopyOutputStream* output) const {
  1592. TextGenerator generator(output, initial_indent_level_);
  1593. Print(message, &generator);
  1594. // Output false if the generator failed internally.
  1595. return !generator.failed();
  1596. }
  1597. bool TextFormat::Printer::PrintUnknownFields(
  1598. const UnknownFieldSet& unknown_fields,
  1599. io::ZeroCopyOutputStream* output) const {
  1600. TextGenerator generator(output, initial_indent_level_);
  1601. PrintUnknownFields(unknown_fields, &generator);
  1602. // Output false if the generator failed internally.
  1603. return !generator.failed();
  1604. }
  1605. namespace {
  1606. // Comparison functor for sorting FieldDescriptors by field index.
  1607. // Normal fields have higher precedence than extensions.
  1608. struct FieldIndexSorter {
  1609. bool operator()(const FieldDescriptor* left,
  1610. const FieldDescriptor* right) const {
  1611. if (left->is_extension() && right->is_extension()) {
  1612. return left->number() < right->number();
  1613. } else if (left->is_extension()) {
  1614. return false;
  1615. } else if (right->is_extension()) {
  1616. return true;
  1617. } else {
  1618. return left->index() < right->index();
  1619. }
  1620. }
  1621. };
  1622. } // namespace
  1623. bool TextFormat::Printer::PrintAny(const Message& message,
  1624. TextGenerator* generator) const {
  1625. const FieldDescriptor* type_url_field;
  1626. const FieldDescriptor* value_field;
  1627. if (!internal::GetAnyFieldDescriptors(message, &type_url_field,
  1628. &value_field)) {
  1629. return false;
  1630. }
  1631. const Reflection* reflection = message.GetReflection();
  1632. // Extract the full type name from the type_url field.
  1633. const string& type_url = reflection->GetString(message, type_url_field);
  1634. string url_prefix;
  1635. string full_type_name;
  1636. if (!internal::ParseAnyTypeUrl(type_url, &url_prefix, &full_type_name)) {
  1637. return false;
  1638. }
  1639. // Print the "value" in text.
  1640. const google::protobuf::Descriptor* value_descriptor =
  1641. finder_ ? finder_->FindAnyType(message, url_prefix, full_type_name)
  1642. : DefaultFinderFindAnyType(message, url_prefix, full_type_name);
  1643. if (value_descriptor == NULL) {
  1644. GOOGLE_LOG(WARNING) << "Proto type " << type_url << " not found";
  1645. return false;
  1646. }
  1647. DynamicMessageFactory factory;
  1648. std::unique_ptr<google::protobuf::Message> value_message(
  1649. factory.GetPrototype(value_descriptor)->New());
  1650. string serialized_value = reflection->GetString(message, value_field);
  1651. if (!value_message->ParseFromString(serialized_value)) {
  1652. GOOGLE_LOG(WARNING) << type_url << ": failed to parse contents";
  1653. return false;
  1654. }
  1655. generator->PrintLiteral("[");
  1656. generator->PrintString(type_url);
  1657. generator->PrintLiteral("]");
  1658. const FastFieldValuePrinter* printer = FindWithDefault(
  1659. custom_printers_, value_field, default_field_value_printer_.get());
  1660. printer->PrintMessageStart(message, -1, 0, single_line_mode_, generator);
  1661. generator->Indent();
  1662. Print(*value_message, generator);
  1663. generator->Outdent();
  1664. printer->PrintMessageEnd(message, -1, 0, single_line_mode_, generator);
  1665. return true;
  1666. }
  1667. void TextFormat::Printer::Print(const Message& message,
  1668. TextGenerator* generator) const {
  1669. const Descriptor* descriptor = message.GetDescriptor();
  1670. auto itr = custom_message_printers_.find(descriptor);
  1671. if (itr != custom_message_printers_.end()) {
  1672. itr->second->Print(message, single_line_mode_, generator);
  1673. return;
  1674. }
  1675. const Reflection* reflection = message.GetReflection();
  1676. if (descriptor->full_name() == internal::kAnyFullTypeName && expand_any_ &&
  1677. PrintAny(message, generator)) {
  1678. return;
  1679. }
  1680. std::vector<const FieldDescriptor*> fields;
  1681. reflection->ListFields(message, &fields);
  1682. if (print_message_fields_in_index_order_) {
  1683. std::sort(fields.begin(), fields.end(), FieldIndexSorter());
  1684. }
  1685. for (int i = 0; i < fields.size(); i++) {
  1686. PrintField(message, reflection, fields[i], generator);
  1687. }
  1688. if (!hide_unknown_fields_) {
  1689. PrintUnknownFields(reflection->GetUnknownFields(message), generator);
  1690. }
  1691. }
  1692. void TextFormat::Printer::PrintFieldValueToString(
  1693. const Message& message,
  1694. const FieldDescriptor* field,
  1695. int index,
  1696. string* output) const {
  1697. GOOGLE_DCHECK(output) << "output specified is NULL";
  1698. output->clear();
  1699. io::StringOutputStream output_stream(output);
  1700. TextGenerator generator(&output_stream, initial_indent_level_);
  1701. PrintFieldValue(message, message.GetReflection(), field, index, &generator);
  1702. }
  1703. void TextFormat::Printer::PrintField(const Message& message,
  1704. const Reflection* reflection,
  1705. const FieldDescriptor* field,
  1706. TextGenerator* generator) const {
  1707. if (use_short_repeated_primitives_ &&
  1708. field->is_repeated() &&
  1709. field->cpp_type() != FieldDescriptor::CPPTYPE_STRING &&
  1710. field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) {
  1711. PrintShortRepeatedField(message, reflection, field, generator);
  1712. return;
  1713. }
  1714. int count = 0;
  1715. if (field->is_repeated()) {
  1716. count = reflection->FieldSize(message, field);
  1717. } else if (reflection->HasField(message, field)) {
  1718. count = 1;
  1719. }
  1720. std::vector<const Message*> map_entries;
  1721. const bool is_map = field->is_map();
  1722. if (is_map) {
  1723. map_entries = DynamicMapSorter::Sort(message, count, reflection, field);
  1724. }
  1725. for (int j = 0; j < count; ++j) {
  1726. const int field_index = field->is_repeated() ? j : -1;
  1727. PrintFieldName(message, field_index, count, reflection, field, generator);
  1728. if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
  1729. const FastFieldValuePrinter* printer = FindWithDefault(
  1730. custom_printers_, field, default_field_value_printer_.get());
  1731. const Message& sub_message =
  1732. field->is_repeated()
  1733. ? (is_map ? *map_entries[j]
  1734. : reflection->GetRepeatedMessage(message, field, j))
  1735. : reflection->GetMessage(message, field);
  1736. printer->PrintMessageStart(sub_message, field_index, count,
  1737. single_line_mode_, generator);
  1738. generator->Indent();
  1739. Print(sub_message, generator);
  1740. generator->Outdent();
  1741. printer->PrintMessageEnd(sub_message, field_index, count,
  1742. single_line_mode_, generator);
  1743. } else {
  1744. generator->PrintLiteral(": ");
  1745. // Write the field value.
  1746. PrintFieldValue(message, reflection, field, field_index, generator);
  1747. if (single_line_mode_) {
  1748. generator->PrintLiteral(" ");
  1749. } else {
  1750. generator->PrintLiteral("\n");
  1751. }
  1752. }
  1753. }
  1754. }
  1755. void TextFormat::Printer::PrintShortRepeatedField(
  1756. const Message& message, const Reflection* reflection,
  1757. const FieldDescriptor* field, TextGenerator* generator) const {
  1758. // Print primitive repeated field in short form.
  1759. int size = reflection->FieldSize(message, field);
  1760. PrintFieldName(message, /*field_index=*/-1, /*field_count=*/size, reflection,
  1761. field, generator);
  1762. generator->PrintLiteral(": [");
  1763. for (int i = 0; i < size; i++) {
  1764. if (i > 0) generator->PrintLiteral(", ");
  1765. PrintFieldValue(message, reflection, field, i, generator);
  1766. }
  1767. if (single_line_mode_) {
  1768. generator->PrintLiteral("] ");
  1769. } else {
  1770. generator->PrintLiteral("]\n");
  1771. }
  1772. }
  1773. void TextFormat::Printer::PrintFieldName(const Message& message,
  1774. int field_index, int field_count,
  1775. const Reflection* reflection,
  1776. const FieldDescriptor* field,
  1777. TextGenerator* generator) const {
  1778. // if use_field_number_ is true, prints field number instead
  1779. // of field name.
  1780. if (use_field_number_) {
  1781. generator->PrintString(SimpleItoa(field->number()));
  1782. return;
  1783. }
  1784. const FastFieldValuePrinter* printer = FindWithDefault(
  1785. custom_printers_, field, default_field_value_printer_.get());
  1786. printer->PrintFieldName(message, field_index, field_count, reflection, field,
  1787. generator);
  1788. }
  1789. void TextFormat::Printer::PrintFieldValue(const Message& message,
  1790. const Reflection* reflection,
  1791. const FieldDescriptor* field,
  1792. int index,
  1793. TextGenerator* generator) const {
  1794. GOOGLE_DCHECK(field->is_repeated() || (index == -1))
  1795. << "Index must be -1 for non-repeated fields";
  1796. const FastFieldValuePrinter* printer = FindWithDefault(
  1797. custom_printers_, field, default_field_value_printer_.get());
  1798. switch (field->cpp_type()) {
  1799. #define OUTPUT_FIELD(CPPTYPE, METHOD) \
  1800. case FieldDescriptor::CPPTYPE_##CPPTYPE: \
  1801. printer->Print##METHOD( \
  1802. field->is_repeated() \
  1803. ? reflection->GetRepeated##METHOD(message, field, index) \
  1804. : reflection->Get##METHOD(message, field), \
  1805. generator); \
  1806. break
  1807. OUTPUT_FIELD( INT32, Int32);
  1808. OUTPUT_FIELD( INT64, Int64);
  1809. OUTPUT_FIELD(UINT32, UInt32);
  1810. OUTPUT_FIELD(UINT64, UInt64);
  1811. OUTPUT_FIELD( FLOAT, Float);
  1812. OUTPUT_FIELD(DOUBLE, Double);
  1813. OUTPUT_FIELD( BOOL, Bool);
  1814. #undef OUTPUT_FIELD
  1815. case FieldDescriptor::CPPTYPE_STRING: {
  1816. string scratch;
  1817. const string& value = field->is_repeated()
  1818. ? reflection->GetRepeatedStringReference(
  1819. message, field, index, &scratch)
  1820. : reflection->GetStringReference(message, field, &scratch);
  1821. const string* value_to_print = &value;
  1822. string truncated_value;
  1823. if (truncate_string_field_longer_than_ > 0 &&
  1824. truncate_string_field_longer_than_ < value.size()) {
  1825. truncated_value = value.substr(0, truncate_string_field_longer_than_) +
  1826. "...<truncated>...";
  1827. value_to_print = &truncated_value;
  1828. }
  1829. if (field->type() == FieldDescriptor::TYPE_STRING) {
  1830. printer->PrintString(*value_to_print, generator);
  1831. } else {
  1832. GOOGLE_DCHECK_EQ(field->type(), FieldDescriptor::TYPE_BYTES);
  1833. printer->PrintBytes(*value_to_print, generator);
  1834. }
  1835. break;
  1836. }
  1837. case FieldDescriptor::CPPTYPE_ENUM: {
  1838. int enum_value = field->is_repeated()
  1839. ? reflection->GetRepeatedEnumValue(message, field, index)
  1840. : reflection->GetEnumValue(message, field);
  1841. const EnumValueDescriptor* enum_desc =
  1842. field->enum_type()->FindValueByNumber(enum_value);
  1843. if (enum_desc != NULL) {
  1844. printer->PrintEnum(enum_value, enum_desc->name(), generator);
  1845. } else {
  1846. // Ordinarily, enum_desc should not be null, because proto2 has the
  1847. // invariant that set enum field values must be in-range, but with the
  1848. // new integer-based API for enums (or the RepeatedField<int> loophole),
  1849. // it is possible for the user to force an unknown integer value. So we
  1850. // simply use the integer value itself as the enum value name in this
  1851. // case.
  1852. printer->PrintEnum(enum_value, StringPrintf("%d", enum_value),
  1853. generator);
  1854. }
  1855. break;
  1856. }
  1857. case FieldDescriptor::CPPTYPE_MESSAGE:
  1858. Print(field->is_repeated()
  1859. ? reflection->GetRepeatedMessage(message, field, index)
  1860. : reflection->GetMessage(message, field),
  1861. generator);
  1862. break;
  1863. }
  1864. }
  1865. /* static */ bool TextFormat::Print(const Message& message,
  1866. io::ZeroCopyOutputStream* output) {
  1867. return Printer().Print(message, output);
  1868. }
  1869. /* static */ bool TextFormat::PrintUnknownFields(
  1870. const UnknownFieldSet& unknown_fields,
  1871. io::ZeroCopyOutputStream* output) {
  1872. return Printer().PrintUnknownFields(unknown_fields, output);
  1873. }
  1874. /* static */ bool TextFormat::PrintToString(
  1875. const Message& message, string* output) {
  1876. return Printer().PrintToString(message, output);
  1877. }
  1878. /* static */ bool TextFormat::PrintUnknownFieldsToString(
  1879. const UnknownFieldSet& unknown_fields, string* output) {
  1880. return Printer().PrintUnknownFieldsToString(unknown_fields, output);
  1881. }
  1882. /* static */ void TextFormat::PrintFieldValueToString(
  1883. const Message& message,
  1884. const FieldDescriptor* field,
  1885. int index,
  1886. string* output) {
  1887. return Printer().PrintFieldValueToString(message, field, index, output);
  1888. }
  1889. /* static */ bool TextFormat::ParseFieldValueFromString(
  1890. const string& input,
  1891. const FieldDescriptor* field,
  1892. Message* message) {
  1893. return Parser().ParseFieldValueFromString(input, field, message);
  1894. }
  1895. // Prints an integer as hex with a fixed number of digits dependent on the
  1896. // integer type.
  1897. template<typename IntType>
  1898. static string PaddedHex(IntType value) {
  1899. string result;
  1900. result.reserve(sizeof(value) * 2);
  1901. for (int i = sizeof(value) * 2 - 1; i >= 0; i--) {
  1902. result.push_back(int_to_hex_digit(value >> (i*4) & 0x0F));
  1903. }
  1904. return result;
  1905. }
  1906. void TextFormat::Printer::PrintUnknownFields(
  1907. const UnknownFieldSet& unknown_fields, TextGenerator* generator) const {
  1908. for (int i = 0; i < unknown_fields.field_count(); i++) {
  1909. const UnknownField& field = unknown_fields.field(i);
  1910. string field_number = SimpleItoa(field.number());
  1911. switch (field.type()) {
  1912. case UnknownField::TYPE_VARINT:
  1913. generator->PrintString(field_number);
  1914. generator->PrintLiteral(": ");
  1915. generator->PrintString(SimpleItoa(field.varint()));
  1916. if (single_line_mode_) {
  1917. generator->PrintLiteral(" ");
  1918. } else {
  1919. generator->PrintLiteral("\n");
  1920. }
  1921. break;
  1922. case UnknownField::TYPE_FIXED32: {
  1923. generator->PrintString(field_number);
  1924. generator->PrintLiteral(": 0x");
  1925. generator->PrintString(
  1926. StrCat(strings::Hex(field.fixed32(), strings::ZERO_PAD_8)));
  1927. if (single_line_mode_) {
  1928. generator->PrintLiteral(" ");
  1929. } else {
  1930. generator->PrintLiteral("\n");
  1931. }
  1932. break;
  1933. }
  1934. case UnknownField::TYPE_FIXED64: {
  1935. generator->PrintString(field_number);
  1936. generator->PrintLiteral(": 0x");
  1937. generator->PrintString(
  1938. StrCat(strings::Hex(field.fixed64(), strings::ZERO_PAD_16)));
  1939. if (single_line_mode_) {
  1940. generator->PrintLiteral(" ");
  1941. } else {
  1942. generator->PrintLiteral("\n");
  1943. }
  1944. break;
  1945. }
  1946. case UnknownField::TYPE_LENGTH_DELIMITED: {
  1947. generator->PrintString(field_number);
  1948. const string& value = field.length_delimited();
  1949. UnknownFieldSet embedded_unknown_fields;
  1950. if (!value.empty() && embedded_unknown_fields.ParseFromString(value)) {
  1951. // This field is parseable as a Message.
  1952. // So it is probably an embedded message.
  1953. if (single_line_mode_) {
  1954. generator->PrintLiteral(" { ");
  1955. } else {
  1956. generator->PrintLiteral(" {\n");
  1957. generator->Indent();
  1958. }
  1959. PrintUnknownFields(embedded_unknown_fields, generator);
  1960. if (single_line_mode_) {
  1961. generator->PrintLiteral("} ");
  1962. } else {
  1963. generator->Outdent();
  1964. generator->PrintLiteral("}\n");
  1965. }
  1966. } else {
  1967. // This field is not parseable as a Message.
  1968. // So it is probably just a plain string.
  1969. generator->PrintLiteral(": \"");
  1970. generator->PrintString(CEscape(value));
  1971. if (single_line_mode_) {
  1972. generator->PrintLiteral("\" ");
  1973. } else {
  1974. generator->PrintLiteral("\"\n");
  1975. }
  1976. }
  1977. break;
  1978. }
  1979. case UnknownField::TYPE_GROUP:
  1980. generator->PrintString(field_number);
  1981. if (single_line_mode_) {
  1982. generator->PrintLiteral(" { ");
  1983. } else {
  1984. generator->PrintLiteral(" {\n");
  1985. generator->Indent();
  1986. }
  1987. PrintUnknownFields(field.group(), generator);
  1988. if (single_line_mode_) {
  1989. generator->PrintLiteral("} ");
  1990. } else {
  1991. generator->Outdent();
  1992. generator->PrintLiteral("}\n");
  1993. }
  1994. break;
  1995. }
  1996. }
  1997. }
  1998. } // namespace protobuf
  1999. } // namespace google