text_format.cc 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261
  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