OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 GPBWriteRawByte(state, (int32_t)(value)&0xFF); | 137 GPBWriteRawByte(state, (int32_t)(value)&0xFF); |
138 GPBWriteRawByte(state, (int32_t)(value >> 8) & 0xFF); | 138 GPBWriteRawByte(state, (int32_t)(value >> 8) & 0xFF); |
139 GPBWriteRawByte(state, (int32_t)(value >> 16) & 0xFF); | 139 GPBWriteRawByte(state, (int32_t)(value >> 16) & 0xFF); |
140 GPBWriteRawByte(state, (int32_t)(value >> 24) & 0xFF); | 140 GPBWriteRawByte(state, (int32_t)(value >> 24) & 0xFF); |
141 GPBWriteRawByte(state, (int32_t)(value >> 32) & 0xFF); | 141 GPBWriteRawByte(state, (int32_t)(value >> 32) & 0xFF); |
142 GPBWriteRawByte(state, (int32_t)(value >> 40) & 0xFF); | 142 GPBWriteRawByte(state, (int32_t)(value >> 40) & 0xFF); |
143 GPBWriteRawByte(state, (int32_t)(value >> 48) & 0xFF); | 143 GPBWriteRawByte(state, (int32_t)(value >> 48) & 0xFF); |
144 GPBWriteRawByte(state, (int32_t)(value >> 56) & 0xFF); | 144 GPBWriteRawByte(state, (int32_t)(value >> 56) & 0xFF); |
145 } | 145 } |
146 | 146 |
| 147 #if DEBUG && !defined(NS_BLOCK_ASSERTIONS) |
| 148 + (void)load { |
| 149 // This test exists to verify that CFStrings with embedded NULLs will work |
| 150 // for us. If this Assert fails, all code below that depends on |
| 151 // CFStringGetCStringPtr will NOT work properly on strings that contain |
| 152 // embedded NULLs, and we do get that in some protobufs. |
| 153 // Note that this will not be compiled in release. |
| 154 // We didn't feel that just keeping it in a unit test was sufficient because |
| 155 // the Protobuf unit tests are only run when somebody is actually working |
| 156 // on protobufs. |
| 157 CFStringRef zeroTest = CFSTR("Test\0String"); |
| 158 const char *cString = CFStringGetCStringPtr(zeroTest, kCFStringEncodingUTF8); |
| 159 NSAssert(cString == NULL, @"Serious Error"); |
| 160 } |
| 161 #endif // DEBUG && !defined(NS_BLOCK_ASSERTIONS) |
| 162 |
147 - (void)dealloc { | 163 - (void)dealloc { |
148 [self flush]; | 164 [self flush]; |
149 [state_.output close]; | 165 [state_.output close]; |
150 [state_.output release]; | 166 [state_.output release]; |
151 [buffer_ release]; | 167 [buffer_ release]; |
152 | 168 |
153 [super dealloc]; | 169 [super dealloc]; |
154 } | 170 } |
155 | 171 |
156 - (instancetype)initWithOutputStream:(NSOutputStream *)output { | 172 - (instancetype)initWithOutputStream:(NSOutputStream *)output { |
(...skipping 23 matching lines...) Expand all Loading... |
180 + (instancetype)streamWithOutputStream:(NSOutputStream *)output { | 196 + (instancetype)streamWithOutputStream:(NSOutputStream *)output { |
181 NSMutableData *data = [NSMutableData dataWithLength:PAGE_SIZE]; | 197 NSMutableData *data = [NSMutableData dataWithLength:PAGE_SIZE]; |
182 return [[[self alloc] initWithOutputStream:output | 198 return [[[self alloc] initWithOutputStream:output |
183 data:data] autorelease]; | 199 data:data] autorelease]; |
184 } | 200 } |
185 | 201 |
186 + (instancetype)streamWithData:(NSMutableData *)data { | 202 + (instancetype)streamWithData:(NSMutableData *)data { |
187 return [[[self alloc] initWithData:data] autorelease]; | 203 return [[[self alloc] initWithData:data] autorelease]; |
188 } | 204 } |
189 | 205 |
190 // Direct access is use for speed, to avoid even internally declaring things | |
191 // read/write, etc. The warning is enabled in the project to ensure code calling | |
192 // protos can turn on -Wdirect-ivar-access without issues. | |
193 #pragma clang diagnostic push | |
194 #pragma clang diagnostic ignored "-Wdirect-ivar-access" | |
195 | |
196 - (void)writeDoubleNoTag:(double)value { | 206 - (void)writeDoubleNoTag:(double)value { |
197 GPBWriteRawLittleEndian64(&state_, GPBConvertDoubleToInt64(value)); | 207 GPBWriteRawLittleEndian64(&state_, GPBConvertDoubleToInt64(value)); |
198 } | 208 } |
199 | 209 |
200 - (void)writeDouble:(int32_t)fieldNumber value:(double)value { | 210 - (void)writeDouble:(int32_t)fieldNumber value:(double)value { |
201 GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatFixed64); | 211 GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatFixed64); |
202 GPBWriteRawLittleEndian64(&state_, GPBConvertDoubleToInt64(value)); | 212 GPBWriteRawLittleEndian64(&state_, GPBConvertDoubleToInt64(value)); |
203 } | 213 } |
204 | 214 |
205 - (void)writeFloatNoTag:(float)value { | 215 - (void)writeFloatNoTag:(float)value { |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
259 - (void)writeBoolNoTag:(BOOL)value { | 269 - (void)writeBoolNoTag:(BOOL)value { |
260 GPBWriteRawByte(&state_, (value ? 1 : 0)); | 270 GPBWriteRawByte(&state_, (value ? 1 : 0)); |
261 } | 271 } |
262 | 272 |
263 - (void)writeBool:(int32_t)fieldNumber value:(BOOL)value { | 273 - (void)writeBool:(int32_t)fieldNumber value:(BOOL)value { |
264 GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatVarint); | 274 GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatVarint); |
265 GPBWriteRawByte(&state_, (value ? 1 : 0)); | 275 GPBWriteRawByte(&state_, (value ? 1 : 0)); |
266 } | 276 } |
267 | 277 |
268 - (void)writeStringNoTag:(const NSString *)value { | 278 - (void)writeStringNoTag:(const NSString *)value { |
269 size_t length = [value lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; | 279 // If you are concerned about embedded NULLs see the test in |
| 280 // +load above. |
| 281 const char *quickString = |
| 282 CFStringGetCStringPtr((CFStringRef)value, kCFStringEncodingUTF8); |
| 283 size_t length = (quickString != NULL) |
| 284 ? strlen(quickString) |
| 285 : [value lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; |
270 GPBWriteRawVarint32(&state_, (int32_t)length); | 286 GPBWriteRawVarint32(&state_, (int32_t)length); |
| 287 |
271 if (length == 0) { | 288 if (length == 0) { |
272 return; | 289 return; |
273 } | 290 } |
274 | 291 |
275 const char *quickString = | |
276 CFStringGetCStringPtr((CFStringRef)value, kCFStringEncodingUTF8); | |
277 | |
278 // Fast path: Most strings are short, if the buffer already has space, | 292 // Fast path: Most strings are short, if the buffer already has space, |
279 // add to it directly. | 293 // add to it directly. |
280 NSUInteger bufferBytesLeft = state_.size - state_.position; | 294 NSUInteger bufferBytesLeft = state_.size - state_.position; |
281 if (bufferBytesLeft >= length) { | 295 if (bufferBytesLeft >= length) { |
282 NSUInteger usedBufferLength = 0; | 296 NSUInteger usedBufferLength = 0; |
283 BOOL result; | 297 BOOL result; |
284 if (quickString != NULL) { | 298 if (quickString != NULL) { |
285 memcpy(state_.bytes + state_.position, quickString, length); | 299 memcpy(state_.bytes + state_.position, quickString, length); |
286 usedBufferLength = length; | 300 usedBufferLength = length; |
287 result = YES; | 301 result = YES; |
288 } else { | 302 } else { |
289 result = [value getBytes:state_.bytes + state_.position | 303 result = [value getBytes:state_.bytes + state_.position |
290 maxLength:bufferBytesLeft | 304 maxLength:bufferBytesLeft |
291 usedLength:&usedBufferLength | 305 usedLength:&usedBufferLength |
292 encoding:NSUTF8StringEncoding | 306 encoding:NSUTF8StringEncoding |
293 options:(NSStringEncodingConversionOptions)0 | 307 options:0 |
294 range:NSMakeRange(0, [value length]) | 308 range:NSMakeRange(0, [value length]) |
295 remainingRange:NULL]; | 309 remainingRange:NULL]; |
296 } | 310 } |
297 if (result) { | 311 if (result) { |
298 NSAssert2((usedBufferLength == length), | 312 NSAssert2((usedBufferLength == length), |
299 @"Our UTF8 calc was wrong? %tu vs %zd", usedBufferLength, | 313 @"Our UTF8 calc was wrong? %tu vs %zd", usedBufferLength, |
300 length); | 314 length); |
301 state_.position += usedBufferLength; | 315 state_.position += usedBufferLength; |
302 return; | 316 return; |
303 } | 317 } |
(...skipping 656 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
960 } | 974 } |
961 | 975 |
962 - (void)writeRawLittleEndian32:(int32_t)value { | 976 - (void)writeRawLittleEndian32:(int32_t)value { |
963 GPBWriteRawLittleEndian32(&state_, value); | 977 GPBWriteRawLittleEndian32(&state_, value); |
964 } | 978 } |
965 | 979 |
966 - (void)writeRawLittleEndian64:(int64_t)value { | 980 - (void)writeRawLittleEndian64:(int64_t)value { |
967 GPBWriteRawLittleEndian64(&state_, value); | 981 GPBWriteRawLittleEndian64(&state_, value); |
968 } | 982 } |
969 | 983 |
970 #pragma clang diagnostic pop | |
971 | |
972 @end | 984 @end |
973 | 985 |
974 size_t GPBComputeDoubleSizeNoTag(Float64 value) { | 986 size_t GPBComputeDoubleSizeNoTag(Float64 value) { |
975 #pragma unused(value) | 987 #pragma unused(value) |
976 return LITTLE_ENDIAN_64_SIZE; | 988 return LITTLE_ENDIAN_64_SIZE; |
977 } | 989 } |
978 | 990 |
979 size_t GPBComputeFloatSizeNoTag(Float32 value) { | 991 size_t GPBComputeFloatSizeNoTag(Float32 value) { |
980 #pragma unused(value) | 992 #pragma unused(value) |
981 return LITTLE_ENDIAN_32_SIZE; | 993 return LITTLE_ENDIAN_32_SIZE; |
(...skipping 29 matching lines...) Expand all Loading... |
1011 #pragma unused(value) | 1023 #pragma unused(value) |
1012 return LITTLE_ENDIAN_32_SIZE; | 1024 return LITTLE_ENDIAN_32_SIZE; |
1013 } | 1025 } |
1014 | 1026 |
1015 size_t GPBComputeBoolSizeNoTag(BOOL value) { | 1027 size_t GPBComputeBoolSizeNoTag(BOOL value) { |
1016 #pragma unused(value) | 1028 #pragma unused(value) |
1017 return 1; | 1029 return 1; |
1018 } | 1030 } |
1019 | 1031 |
1020 size_t GPBComputeStringSizeNoTag(NSString *value) { | 1032 size_t GPBComputeStringSizeNoTag(NSString *value) { |
1021 NSUInteger length = [value lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; | 1033 // If you are concerned about embedded NULLs see the test in |
| 1034 // +load above. |
| 1035 const char *quickString = |
| 1036 CFStringGetCStringPtr((CFStringRef)value, kCFStringEncodingUTF8); |
| 1037 NSUInteger length = |
| 1038 (quickString != NULL) |
| 1039 ? strlen(quickString) |
| 1040 : [value lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; |
1022 return GPBComputeRawVarint32SizeForInteger(length) + length; | 1041 return GPBComputeRawVarint32SizeForInteger(length) + length; |
1023 } | 1042 } |
1024 | 1043 |
1025 size_t GPBComputeGroupSizeNoTag(GPBMessage *value) { | 1044 size_t GPBComputeGroupSizeNoTag(GPBMessage *value) { |
1026 return [value serializedSize]; | 1045 return [value serializedSize]; |
1027 } | 1046 } |
1028 | 1047 |
1029 size_t GPBComputeUnknownGroupSizeNoTag(GPBUnknownFieldSet *value) { | 1048 size_t GPBComputeUnknownGroupSizeNoTag(GPBUnknownFieldSet *value) { |
1030 return value.serializedSize; | 1049 return value.serializedSize; |
1031 } | 1050 } |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1193 if ((value & (0xffffffffffffffffL << 14)) == 0) return 2; | 1212 if ((value & (0xffffffffffffffffL << 14)) == 0) return 2; |
1194 if ((value & (0xffffffffffffffffL << 21)) == 0) return 3; | 1213 if ((value & (0xffffffffffffffffL << 21)) == 0) return 3; |
1195 if ((value & (0xffffffffffffffffL << 28)) == 0) return 4; | 1214 if ((value & (0xffffffffffffffffL << 28)) == 0) return 4; |
1196 if ((value & (0xffffffffffffffffL << 35)) == 0) return 5; | 1215 if ((value & (0xffffffffffffffffL << 35)) == 0) return 5; |
1197 if ((value & (0xffffffffffffffffL << 42)) == 0) return 6; | 1216 if ((value & (0xffffffffffffffffL << 42)) == 0) return 6; |
1198 if ((value & (0xffffffffffffffffL << 49)) == 0) return 7; | 1217 if ((value & (0xffffffffffffffffL << 49)) == 0) return 7; |
1199 if ((value & (0xffffffffffffffffL << 56)) == 0) return 8; | 1218 if ((value & (0xffffffffffffffffL << 56)) == 0) return 8; |
1200 if ((value & (0xffffffffffffffffL << 63)) == 0) return 9; | 1219 if ((value & (0xffffffffffffffffL << 63)) == 0) return 9; |
1201 return 10; | 1220 return 10; |
1202 } | 1221 } |
OLD | NEW |