BWAPI
|
00001 // Protocol Buffers - Google's data interchange format 00002 // Copyright 2008 Google Inc. All rights reserved. 00003 // http://code.google.com/p/protobuf/ 00004 // 00005 // Redistribution and use in source and binary forms, with or without 00006 // modification, are permitted provided that the following conditions are 00007 // met: 00008 // 00009 // * Redistributions of source code must retain the above copyright 00010 // notice, this list of conditions and the following disclaimer. 00011 // * Redistributions in binary form must reproduce the above 00012 // copyright notice, this list of conditions and the following disclaimer 00013 // in the documentation and/or other materials provided with the 00014 // distribution. 00015 // * Neither the name of Google Inc. nor the names of its 00016 // contributors may be used to endorse or promote products derived from 00017 // this software without specific prior written permission. 00018 // 00019 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00020 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00021 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00022 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00023 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00024 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00025 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00026 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00027 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00028 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00029 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00030 00031 // Author: kenton@google.com (Kenton Varda) 00032 // atenasio@google.com (Chris Atenasio) (ZigZag transform) 00033 // Based on original Protocol Buffers design by 00034 // Sanjay Ghemawat, Jeff Dean, and others. 00035 // 00036 // This header is logically internal, but is made public because it is used 00037 // from protocol-compiler-generated code, which may reside in other components. 00038 00039 #ifndef GOOGLE_PROTOBUF_WIRE_FORMAT_H__ 00040 #define GOOGLE_PROTOBUF_WIRE_FORMAT_H__ 00041 00042 #include <string> 00043 #include <google/protobuf/descriptor.pb.h> 00044 #include <google/protobuf/descriptor.h> 00045 #include <google/protobuf/message.h> 00046 #include <google/protobuf/wire_format_lite.h> 00047 00048 // Do UTF-8 validation on string type in Debug build only 00049 #ifndef NDEBUG 00050 #define GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED 00051 #endif 00052 00053 namespace google { 00054 namespace protobuf { 00055 namespace io { 00056 class CodedInputStream; // coded_stream.h 00057 class CodedOutputStream; // coded_stream.h 00058 } 00059 class UnknownFieldSet; // unknown_field_set.h 00060 } 00061 00062 namespace protobuf { 00063 namespace internal { 00064 00065 // This class is for internal use by the protocol buffer library and by 00066 // protocol-complier-generated message classes. It must not be called 00067 // directly by clients. 00068 // 00069 // This class contains code for implementing the binary protocol buffer 00070 // wire format via reflection. The WireFormatLite class implements the 00071 // non-reflection based routines. 00072 // 00073 // This class is really a namespace that contains only static methods 00074 class LIBPROTOBUF_EXPORT WireFormat { 00075 public: 00076 00077 // Given a field return its WireType 00078 static inline WireFormatLite::WireType WireTypeForField( 00079 const FieldDescriptor* field); 00080 00081 // Given a FieldSescriptor::Type return its WireType 00082 static inline WireFormatLite::WireType WireTypeForFieldType( 00083 FieldDescriptor::Type type); 00084 00085 // Compute the byte size of a tag. For groups, this includes both the start 00086 // and end tags. 00087 static inline int TagSize(int field_number, FieldDescriptor::Type type); 00088 00089 // These procedures can be used to implement the methods of Message which 00090 // handle parsing and serialization of the protocol buffer wire format 00091 // using only the Reflection interface. When you ask the protocol 00092 // compiler to optimize for code size rather than speed, it will implement 00093 // those methods in terms of these procedures. Of course, these are much 00094 // slower than the specialized implementations which the protocol compiler 00095 // generates when told to optimize for speed. 00096 00097 // Read a message in protocol buffer wire format. 00098 // 00099 // This procedure reads either to the end of the input stream or through 00100 // a WIRETYPE_END_GROUP tag ending the message, whichever comes first. 00101 // It returns false if the input is invalid. 00102 // 00103 // Required fields are NOT checked by this method. You must call 00104 // IsInitialized() on the resulting message yourself. 00105 static bool ParseAndMergePartial(io::CodedInputStream* input, 00106 Message* message); 00107 00108 // Serialize a message in protocol buffer wire format. 00109 // 00110 // Any embedded messages within the message must have their correct sizes 00111 // cached. However, the top-level message need not; its size is passed as 00112 // a parameter to this procedure. 00113 // 00114 // These return false iff the underlying stream returns a write error. 00115 static void SerializeWithCachedSizes( 00116 const Message& message, 00117 int size, io::CodedOutputStream* output); 00118 00119 // Implements Message::ByteSize() via reflection. WARNING: The result 00120 // of this method is *not* cached anywhere. However, all embedded messages 00121 // will have their ByteSize() methods called, so their sizes will be cached. 00122 // Therefore, calling this method is sufficient to allow you to call 00123 // WireFormat::SerializeWithCachedSizes() on the same object. 00124 static int ByteSize(const Message& message); 00125 00126 // ----------------------------------------------------------------- 00127 // Helpers for dealing with unknown fields 00128 00129 // Skips a field value of the given WireType. The input should start 00130 // positioned immediately after the tag. If unknown_fields is non-NULL, 00131 // the contents of the field will be added to it. 00132 static bool SkipField(io::CodedInputStream* input, uint32 tag, 00133 UnknownFieldSet* unknown_fields); 00134 00135 // Reads and ignores a message from the input. If unknown_fields is non-NULL, 00136 // the contents will be added to it. 00137 static bool SkipMessage(io::CodedInputStream* input, 00138 UnknownFieldSet* unknown_fields); 00139 00140 // Write the contents of an UnknownFieldSet to the output. 00141 static void SerializeUnknownFields(const UnknownFieldSet& unknown_fields, 00142 io::CodedOutputStream* output); 00143 // Same as above, except writing directly to the provided buffer. 00144 // Requires that the buffer have sufficient capacity for 00145 // ComputeUnknownFieldsSize(unknown_fields). 00146 // 00147 // Returns a pointer past the last written byte. 00148 static uint8* SerializeUnknownFieldsToArray( 00149 const UnknownFieldSet& unknown_fields, 00150 uint8* target); 00151 00152 // Same thing except for messages that have the message_set_wire_format 00153 // option. 00154 static void SerializeUnknownMessageSetItems( 00155 const UnknownFieldSet& unknown_fields, 00156 io::CodedOutputStream* output); 00157 // Same as above, except writing directly to the provided buffer. 00158 // Requires that the buffer have sufficient capacity for 00159 // ComputeUnknownMessageSetItemsSize(unknown_fields). 00160 // 00161 // Returns a pointer past the last written byte. 00162 static uint8* SerializeUnknownMessageSetItemsToArray( 00163 const UnknownFieldSet& unknown_fields, 00164 uint8* target); 00165 00166 // Compute the size of the UnknownFieldSet on the wire. 00167 static int ComputeUnknownFieldsSize(const UnknownFieldSet& unknown_fields); 00168 00169 // Same thing except for messages that have the message_set_wire_format 00170 // option. 00171 static int ComputeUnknownMessageSetItemsSize( 00172 const UnknownFieldSet& unknown_fields); 00173 00174 00175 // Helper functions for encoding and decoding tags. (Inlined below and in 00176 // _inl.h) 00177 // 00178 // This is different from MakeTag(field->number(), field->type()) in the case 00179 // of packed repeated fields. 00180 static uint32 MakeTag(const FieldDescriptor* field); 00181 00182 // Parse a single field. The input should start out positioned immidately 00183 // after the tag. 00184 static bool ParseAndMergeField( 00185 uint32 tag, 00186 const FieldDescriptor* field, // May be NULL for unknown 00187 Message* message, 00188 io::CodedInputStream* input); 00189 00190 // Serialize a single field. 00191 static void SerializeFieldWithCachedSizes( 00192 const FieldDescriptor* field, // Cannot be NULL 00193 const Message& message, 00194 io::CodedOutputStream* output); 00195 00196 // Compute size of a single field. If the field is a message type, this 00197 // will call ByteSize() for the embedded message, insuring that it caches 00198 // its size. 00199 static int FieldByteSize( 00200 const FieldDescriptor* field, // Cannot be NULL 00201 const Message& message); 00202 00203 // Parse/serialize a MessageSet::Item group. Used with messages that use 00204 // opion message_set_wire_format = true. 00205 static bool ParseAndMergeMessageSetItem( 00206 io::CodedInputStream* input, 00207 Message* message); 00208 static void SerializeMessageSetItemWithCachedSizes( 00209 const FieldDescriptor* field, 00210 const Message& message, 00211 io::CodedOutputStream* output); 00212 static int MessageSetItemByteSize( 00213 const FieldDescriptor* field, 00214 const Message& message); 00215 00216 // Computes the byte size of a field, excluding tags. For packed fields, it 00217 // only includes the size of the raw data, and not the size of the total 00218 // length, but for other length-delimited types, the size of the length is 00219 // included. 00220 static int FieldDataOnlyByteSize( 00221 const FieldDescriptor* field, // Cannot be NULL 00222 const Message& message); 00223 00224 enum Operation { 00225 PARSE, 00226 SERIALIZE, 00227 }; 00228 00229 // Verifies that a string field is valid UTF8, logging an error if not. 00230 static void VerifyUTF8String(const char* data, int size, Operation op); 00231 00232 private: 00233 // Verifies that a string field is valid UTF8, logging an error if not. 00234 static void VerifyUTF8StringFallback( 00235 const char* data, 00236 int size, 00237 Operation op); 00238 00239 00240 00241 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(WireFormat); 00242 }; 00243 00244 // Subclass of FieldSkipper which saves skipped fields to an UnknownFieldSet. 00245 class LIBPROTOBUF_EXPORT UnknownFieldSetFieldSkipper : public FieldSkipper { 00246 public: 00247 UnknownFieldSetFieldSkipper(UnknownFieldSet* unknown_fields) 00248 : unknown_fields_(unknown_fields) {} 00249 virtual ~UnknownFieldSetFieldSkipper() {} 00250 00251 // implements FieldSkipper ----------------------------------------- 00252 virtual bool SkipField(io::CodedInputStream* input, uint32 tag); 00253 virtual bool SkipMessage(io::CodedInputStream* input); 00254 virtual void SkipUnknownEnum(int field_number, int value); 00255 00256 private: 00257 UnknownFieldSet* unknown_fields_; 00258 }; 00259 00260 // inline methods ==================================================== 00261 00262 inline WireFormatLite::WireType WireFormat::WireTypeForField( 00263 const FieldDescriptor* field) { 00264 if (field->options().packed()) { 00265 return WireFormatLite::WIRETYPE_LENGTH_DELIMITED; 00266 } else { 00267 return WireTypeForFieldType(field->type()); 00268 } 00269 } 00270 00271 inline WireFormatLite::WireType WireFormat::WireTypeForFieldType( 00272 FieldDescriptor::Type type) { 00273 // Some compilers don't like enum -> enum casts, so we implicit_cast to 00274 // int first. 00275 return WireFormatLite::WireTypeForFieldType( 00276 static_cast<WireFormatLite::FieldType>( 00277 implicit_cast<int>(type))); 00278 } 00279 00280 inline uint32 WireFormat::MakeTag(const FieldDescriptor* field) { 00281 return WireFormatLite::MakeTag(field->number(), WireTypeForField(field)); 00282 } 00283 00284 inline int WireFormat::TagSize(int field_number, FieldDescriptor::Type type) { 00285 // Some compilers don't like enum -> enum casts, so we implicit_cast to 00286 // int first. 00287 return WireFormatLite::TagSize(field_number, 00288 static_cast<WireFormatLite::FieldType>( 00289 implicit_cast<int>(type))); 00290 } 00291 00292 inline void WireFormat::VerifyUTF8String(const char* data, int size, 00293 WireFormat::Operation op) { 00294 #ifdef GOOGLE_PROTOBUF_UTF8_VALIDATION_ENABLED 00295 WireFormat::VerifyUTF8StringFallback(data, size, op); 00296 #endif 00297 } 00298 00299 00300 } // namespace internal 00301 } // namespace protobuf 00302 00303 } // namespace google 00304 #endif // GOOGLE_PROTOBUF_WIRE_FORMAT_H__