• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
31 // Author: kenton@google.com (Kenton Varda)
32 //  Based on original Protocol Buffers design by
33 //  Sanjay Ghemawat, Jeff Dean, and others.
34 #include <google/protobuf/reflection_ops.h>
35 
36 #include <string>
37 #include <vector>
38 
39 #include <google/protobuf/stubs/logging.h>
40 #include <google/protobuf/stubs/common.h>
41 #include <google/protobuf/descriptor.pb.h>
42 #include <google/protobuf/descriptor.h>
43 #include <google/protobuf/map_field.h>
44 #include <google/protobuf/map_field_inl.h>
45 #include <google/protobuf/unknown_field_set.h>
46 #include <google/protobuf/stubs/strutil.h>
47 
48 #include <google/protobuf/port_def.inc>
49 
50 namespace google {
51 namespace protobuf {
52 namespace internal {
53 
GetReflectionOrDie(const Message & m)54 static const Reflection* GetReflectionOrDie(const Message& m) {
55   const Reflection* r = m.GetReflection();
56   if (r == nullptr) {
57     const Descriptor* d = m.GetDescriptor();
58     const std::string& mtype = d ? d->name() : "unknown";
59     // RawMessage is one known type for which GetReflection() returns nullptr.
60     GOOGLE_LOG(FATAL) << "Message does not support reflection (type " << mtype << ").";
61   }
62   return r;
63 }
64 
Copy(const Message & from,Message * to)65 void ReflectionOps::Copy(const Message& from, Message* to) {
66   if (&from == to) return;
67   Clear(to);
68   Merge(from, to);
69 }
70 
Merge(const Message & from,Message * to)71 void ReflectionOps::Merge(const Message& from, Message* to) {
72   GOOGLE_CHECK_NE(&from, to);
73 
74   const Descriptor* descriptor = from.GetDescriptor();
75   GOOGLE_CHECK_EQ(to->GetDescriptor(), descriptor)
76       << "Tried to merge messages of different types "
77       << "(merge " << descriptor->full_name() << " to "
78       << to->GetDescriptor()->full_name() << ")";
79 
80   const Reflection* from_reflection = GetReflectionOrDie(from);
81   const Reflection* to_reflection = GetReflectionOrDie(*to);
82   bool is_from_generated = (from_reflection->GetMessageFactory() ==
83                             google::protobuf::MessageFactory::generated_factory());
84   bool is_to_generated = (to_reflection->GetMessageFactory() ==
85                           google::protobuf::MessageFactory::generated_factory());
86 
87   std::vector<const FieldDescriptor*> fields;
88   from_reflection->ListFieldsOmitStripped(from, &fields);
89   for (int i = 0; i < fields.size(); i++) {
90     const FieldDescriptor* field = fields[i];
91 
92     if (field->is_repeated()) {
93       // Use map reflection if both are in map status and have the
94       // same map type to avoid sync with repeated field.
95       // Note: As from and to messages have the same descriptor, the
96       // map field types are the same if they are both generated
97       // messages or both dynamic messages.
98       if (is_from_generated == is_to_generated && field->is_map()) {
99         const MapFieldBase* from_field =
100             from_reflection->GetMapData(from, field);
101         MapFieldBase* to_field = to_reflection->MutableMapData(to, field);
102         if (to_field->IsMapValid() && from_field->IsMapValid()) {
103           to_field->MergeFrom(*from_field);
104           continue;
105         }
106       }
107       int count = from_reflection->FieldSize(from, field);
108       for (int j = 0; j < count; j++) {
109         switch (field->cpp_type()) {
110 #define HANDLE_TYPE(CPPTYPE, METHOD)                                      \
111   case FieldDescriptor::CPPTYPE_##CPPTYPE:                                \
112     to_reflection->Add##METHOD(                                           \
113         to, field, from_reflection->GetRepeated##METHOD(from, field, j)); \
114     break;
115 
116           HANDLE_TYPE(INT32, Int32);
117           HANDLE_TYPE(INT64, Int64);
118           HANDLE_TYPE(UINT32, UInt32);
119           HANDLE_TYPE(UINT64, UInt64);
120           HANDLE_TYPE(FLOAT, Float);
121           HANDLE_TYPE(DOUBLE, Double);
122           HANDLE_TYPE(BOOL, Bool);
123           HANDLE_TYPE(STRING, String);
124           HANDLE_TYPE(ENUM, Enum);
125 #undef HANDLE_TYPE
126 
127           case FieldDescriptor::CPPTYPE_MESSAGE:
128             const Message& from_child =
129                 from_reflection->GetRepeatedMessage(from, field, j);
130             if (from_reflection == to_reflection) {
131               to_reflection
132                   ->AddMessage(to, field,
133                                from_child.GetReflection()->GetMessageFactory())
134                   ->MergeFrom(from_child);
135             } else {
136               to_reflection->AddMessage(to, field)->MergeFrom(from_child);
137             }
138             break;
139         }
140       }
141     } else {
142       switch (field->cpp_type()) {
143 #define HANDLE_TYPE(CPPTYPE, METHOD)                                       \
144   case FieldDescriptor::CPPTYPE_##CPPTYPE:                                 \
145     to_reflection->Set##METHOD(to, field,                                  \
146                                from_reflection->Get##METHOD(from, field)); \
147     break;
148 
149         HANDLE_TYPE(INT32, Int32);
150         HANDLE_TYPE(INT64, Int64);
151         HANDLE_TYPE(UINT32, UInt32);
152         HANDLE_TYPE(UINT64, UInt64);
153         HANDLE_TYPE(FLOAT, Float);
154         HANDLE_TYPE(DOUBLE, Double);
155         HANDLE_TYPE(BOOL, Bool);
156         HANDLE_TYPE(STRING, String);
157         HANDLE_TYPE(ENUM, Enum);
158 #undef HANDLE_TYPE
159 
160         case FieldDescriptor::CPPTYPE_MESSAGE:
161           const Message& from_child = from_reflection->GetMessage(from, field);
162           if (from_reflection == to_reflection) {
163             to_reflection
164                 ->MutableMessage(
165                     to, field, from_child.GetReflection()->GetMessageFactory())
166                 ->MergeFrom(from_child);
167           } else {
168             to_reflection->MutableMessage(to, field)->MergeFrom(from_child);
169           }
170           break;
171       }
172     }
173   }
174 
175   to_reflection->MutableUnknownFields(to)->MergeFrom(
176       from_reflection->GetUnknownFields(from));
177 }
178 
Clear(Message * message)179 void ReflectionOps::Clear(Message* message) {
180   const Reflection* reflection = GetReflectionOrDie(*message);
181 
182   std::vector<const FieldDescriptor*> fields;
183   reflection->ListFieldsOmitStripped(*message, &fields);
184   for (int i = 0; i < fields.size(); i++) {
185     reflection->ClearField(message, fields[i]);
186   }
187 
188   reflection->MutableUnknownFields(message)->Clear();
189 }
190 
IsInitialized(const Message & message,bool check_fields,bool check_descendants)191 bool ReflectionOps::IsInitialized(const Message& message, bool check_fields,
192                                   bool check_descendants) {
193   const Descriptor* descriptor = message.GetDescriptor();
194   const Reflection* reflection = GetReflectionOrDie(message);
195   if (const int field_count = descriptor->field_count()) {
196     const FieldDescriptor* begin = descriptor->field(0);
197     const FieldDescriptor* end = begin + field_count;
198     GOOGLE_DCHECK_EQ(descriptor->field(field_count - 1), end - 1);
199 
200     if (check_fields) {
201       // Check required fields of this message.
202       for (const FieldDescriptor* field = begin; field != end; ++field) {
203         if (field->is_required() && !reflection->HasField(message, field)) {
204           return false;
205         }
206       }
207     }
208 
209     if (check_descendants) {
210       for (const FieldDescriptor* field = begin; field != end; ++field) {
211         if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
212           const Descriptor* message_type = field->message_type();
213           if (PROTOBUF_PREDICT_FALSE(message_type->options().map_entry())) {
214             if (message_type->field(1)->cpp_type() ==
215                 FieldDescriptor::CPPTYPE_MESSAGE) {
216               const MapFieldBase* map_field =
217                   reflection->GetMapData(message, field);
218               if (map_field->IsMapValid()) {
219                 MapIterator it(const_cast<Message*>(&message), field);
220                 MapIterator end(const_cast<Message*>(&message), field);
221                 for (map_field->MapBegin(&it), map_field->MapEnd(&end);
222                      it != end; ++it) {
223                   if (!it.GetValueRef().GetMessageValue().IsInitialized()) {
224                     return false;
225                   }
226                 }
227               }
228             }
229           } else if (field->is_repeated()) {
230             const int size = reflection->FieldSize(message, field);
231             for (int j = 0; j < size; j++) {
232               if (!reflection->GetRepeatedMessage(message, field, j)
233                        .IsInitialized()) {
234                 return false;
235               }
236             }
237           } else if (reflection->HasField(message, field)) {
238             if (!reflection->GetMessage(message, field).IsInitialized()) {
239               return false;
240             }
241           }
242         }
243       }
244     }
245   }
246   if (check_descendants && reflection->HasExtensionSet(message) &&
247       !reflection->GetExtensionSet(message).IsInitialized()) {
248     return false;
249   }
250   return true;
251 }
252 
IsInitialized(const Message & message)253 bool ReflectionOps::IsInitialized(const Message& message) {
254   const Descriptor* descriptor = message.GetDescriptor();
255   const Reflection* reflection = GetReflectionOrDie(message);
256 
257   // Check required fields of this message.
258   {
259     const int field_count = descriptor->field_count();
260     for (int i = 0; i < field_count; i++) {
261       if (descriptor->field(i)->is_required()) {
262         if (!reflection->HasField(message, descriptor->field(i))) {
263           return false;
264         }
265       }
266     }
267   }
268 
269   // Check that sub-messages are initialized.
270   std::vector<const FieldDescriptor*> fields;
271   // Should be safe to skip stripped fields because required fields are not
272   // stripped.
273   reflection->ListFieldsOmitStripped(message, &fields);
274   for (int i = 0; i < fields.size(); i++) {
275     const FieldDescriptor* field = fields[i];
276     if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
277 
278       if (field->is_map()) {
279         const FieldDescriptor* value_field = field->message_type()->field(1);
280         if (value_field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
281           const MapFieldBase* map_field =
282               reflection->GetMapData(message, field);
283           if (map_field->IsMapValid()) {
284             MapIterator iter(const_cast<Message*>(&message), field);
285             MapIterator end(const_cast<Message*>(&message), field);
286             for (map_field->MapBegin(&iter), map_field->MapEnd(&end);
287                  iter != end; ++iter) {
288               if (!iter.GetValueRef().GetMessageValue().IsInitialized()) {
289                 return false;
290               }
291             }
292             continue;
293           }
294         } else {
295           continue;
296         }
297       }
298 
299       if (field->is_repeated()) {
300         int size = reflection->FieldSize(message, field);
301 
302         for (int j = 0; j < size; j++) {
303           if (!reflection->GetRepeatedMessage(message, field, j)
304                    .IsInitialized()) {
305             return false;
306           }
307         }
308       } else {
309         if (!reflection->GetMessage(message, field).IsInitialized()) {
310           return false;
311         }
312       }
313     }
314   }
315 
316   return true;
317 }
318 
IsMapValueMessageTyped(const FieldDescriptor * map_field)319 static bool IsMapValueMessageTyped(const FieldDescriptor* map_field) {
320   return map_field->message_type()->field(1)->cpp_type() ==
321          FieldDescriptor::CPPTYPE_MESSAGE;
322 }
323 
DiscardUnknownFields(Message * message)324 void ReflectionOps::DiscardUnknownFields(Message* message) {
325   const Reflection* reflection = GetReflectionOrDie(*message);
326 
327   reflection->MutableUnknownFields(message)->Clear();
328 
329   // Walk through the fields of this message and DiscardUnknownFields on any
330   // messages present.
331   std::vector<const FieldDescriptor*> fields;
332   reflection->ListFields(*message, &fields);
333   for (int i = 0; i < fields.size(); i++) {
334     const FieldDescriptor* field = fields[i];
335     // Skip over non-message fields.
336     if (field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) {
337       continue;
338     }
339     // Discard the unknown fields in maps that contain message values.
340     if (field->is_map() && IsMapValueMessageTyped(field)) {
341       const MapFieldBase* map_field =
342           reflection->MutableMapData(message, field);
343       if (map_field->IsMapValid()) {
344         MapIterator iter(message, field);
345         MapIterator end(message, field);
346         for (map_field->MapBegin(&iter), map_field->MapEnd(&end); iter != end;
347              ++iter) {
348           iter.MutableValueRef()->MutableMessageValue()->DiscardUnknownFields();
349         }
350       }
351       // Discard every unknown field inside messages in a repeated field.
352     } else if (field->is_repeated()) {
353       int size = reflection->FieldSize(*message, field);
354       for (int j = 0; j < size; j++) {
355         reflection->MutableRepeatedMessage(message, field, j)
356             ->DiscardUnknownFields();
357       }
358       // Discard the unknown fields inside an optional message.
359     } else {
360       reflection->MutableMessage(message, field)->DiscardUnknownFields();
361     }
362   }
363 }
364 
SubMessagePrefix(const std::string & prefix,const FieldDescriptor * field,int index)365 static std::string SubMessagePrefix(const std::string& prefix,
366                                     const FieldDescriptor* field, int index) {
367   std::string result(prefix);
368   if (field->is_extension()) {
369     result.append("(");
370     result.append(field->full_name());
371     result.append(")");
372   } else {
373     result.append(field->name());
374   }
375   if (index != -1) {
376     result.append("[");
377     result.append(StrCat(index));
378     result.append("]");
379   }
380   result.append(".");
381   return result;
382 }
383 
FindInitializationErrors(const Message & message,const std::string & prefix,std::vector<std::string> * errors)384 void ReflectionOps::FindInitializationErrors(const Message& message,
385                                              const std::string& prefix,
386                                              std::vector<std::string>* errors) {
387   const Descriptor* descriptor = message.GetDescriptor();
388   const Reflection* reflection = GetReflectionOrDie(message);
389 
390   // Check required fields of this message.
391   {
392     const int field_count = descriptor->field_count();
393     for (int i = 0; i < field_count; i++) {
394       if (descriptor->field(i)->is_required()) {
395         if (!reflection->HasField(message, descriptor->field(i))) {
396           errors->push_back(prefix + descriptor->field(i)->name());
397         }
398       }
399     }
400   }
401 
402   // Check sub-messages.
403   std::vector<const FieldDescriptor*> fields;
404   reflection->ListFieldsOmitStripped(message, &fields);
405   for (int i = 0; i < fields.size(); i++) {
406     const FieldDescriptor* field = fields[i];
407     if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) {
408 
409       if (field->is_repeated()) {
410         int size = reflection->FieldSize(message, field);
411 
412         for (int j = 0; j < size; j++) {
413           const Message& sub_message =
414               reflection->GetRepeatedMessage(message, field, j);
415           FindInitializationErrors(sub_message,
416                                    SubMessagePrefix(prefix, field, j), errors);
417         }
418       } else {
419         const Message& sub_message = reflection->GetMessage(message, field);
420         FindInitializationErrors(sub_message,
421                                  SubMessagePrefix(prefix, field, -1), errors);
422       }
423     }
424   }
425 }
426 
GenericSwap(Message * m1,Message * m2)427 void GenericSwap(Message* m1, Message* m2) {
428   Arena* m2_arena = m2->GetArena();
429   GOOGLE_DCHECK(m1->GetArena() != m2_arena);
430 
431   // Copy semantics in this case. We try to improve efficiency by placing the
432   // temporary on |m2|'s arena so that messages are copied twice rather than
433   // three times.
434   Message* tmp = m2->New(m2_arena);
435   std::unique_ptr<Message> tmp_deleter(m2_arena == nullptr ? tmp : nullptr);
436   tmp->CheckTypeAndMergeFrom(*m1);
437   m1->Clear();
438   m1->CheckTypeAndMergeFrom(*m2);
439   m2->GetReflection()->Swap(tmp, m2);
440 }
441 
442 }  // namespace internal
443 }  // namespace protobuf
444 }  // namespace google
445