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 | |
163 - (void)dealloc { | 147 - (void)dealloc { |
164 [self flush]; | 148 [self flush]; |
165 [state_.output close]; | 149 [state_.output close]; |
166 [state_.output release]; | 150 [state_.output release]; |
167 [buffer_ release]; | 151 [buffer_ release]; |
168 | 152 |
169 [super dealloc]; | 153 [super dealloc]; |
170 } | 154 } |
171 | 155 |
172 - (instancetype)initWithOutputStream:(NSOutputStream *)output { | 156 - (instancetype)initWithOutputStream:(NSOutputStream *)output { |
(...skipping 23 matching lines...) Expand all Loading... |
196 + (instancetype)streamWithOutputStream:(NSOutputStream *)output { | 180 + (instancetype)streamWithOutputStream:(NSOutputStream *)output { |
197 NSMutableData *data = [NSMutableData dataWithLength:PAGE_SIZE]; | 181 NSMutableData *data = [NSMutableData dataWithLength:PAGE_SIZE]; |
198 return [[[self alloc] initWithOutputStream:output | 182 return [[[self alloc] initWithOutputStream:output |
199 data:data] autorelease]; | 183 data:data] autorelease]; |
200 } | 184 } |
201 | 185 |
202 + (instancetype)streamWithData:(NSMutableData *)data { | 186 + (instancetype)streamWithData:(NSMutableData *)data { |
203 return [[[self alloc] initWithData:data] autorelease]; | 187 return [[[self alloc] initWithData:data] autorelease]; |
204 } | 188 } |
205 | 189 |
| 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 |
206 - (void)writeDoubleNoTag:(double)value { | 196 - (void)writeDoubleNoTag:(double)value { |
207 GPBWriteRawLittleEndian64(&state_, GPBConvertDoubleToInt64(value)); | 197 GPBWriteRawLittleEndian64(&state_, GPBConvertDoubleToInt64(value)); |
208 } | 198 } |
209 | 199 |
210 - (void)writeDouble:(int32_t)fieldNumber value:(double)value { | 200 - (void)writeDouble:(int32_t)fieldNumber value:(double)value { |
211 GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatFixed64); | 201 GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatFixed64); |
212 GPBWriteRawLittleEndian64(&state_, GPBConvertDoubleToInt64(value)); | 202 GPBWriteRawLittleEndian64(&state_, GPBConvertDoubleToInt64(value)); |
213 } | 203 } |
214 | 204 |
215 - (void)writeFloatNoTag:(float)value { | 205 - (void)writeFloatNoTag:(float)value { |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 - (void)writeBoolNoTag:(BOOL)value { | 259 - (void)writeBoolNoTag:(BOOL)value { |
270 GPBWriteRawByte(&state_, (value ? 1 : 0)); | 260 GPBWriteRawByte(&state_, (value ? 1 : 0)); |
271 } | 261 } |
272 | 262 |
273 - (void)writeBool:(int32_t)fieldNumber value:(BOOL)value { | 263 - (void)writeBool:(int32_t)fieldNumber value:(BOOL)value { |
274 GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatVarint); | 264 GPBWriteTagWithFormat(&state_, fieldNumber, GPBWireFormatVarint); |
275 GPBWriteRawByte(&state_, (value ? 1 : 0)); | 265 GPBWriteRawByte(&state_, (value ? 1 : 0)); |
276 } | 266 } |
277 | 267 |
278 - (void)writeStringNoTag:(const NSString *)value { | 268 - (void)writeStringNoTag:(const NSString *)value { |
279 // If you are concerned about embedded NULLs see the test in | 269 size_t length = [value lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; |
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]; | |
286 GPBWriteRawVarint32(&state_, (int32_t)length); | 270 GPBWriteRawVarint32(&state_, (int32_t)length); |
287 | |
288 if (length == 0) { | 271 if (length == 0) { |
289 return; | 272 return; |
290 } | 273 } |
291 | 274 |
| 275 const char *quickString = |
| 276 CFStringGetCStringPtr((CFStringRef)value, kCFStringEncodingUTF8); |
| 277 |
292 // Fast path: Most strings are short, if the buffer already has space, | 278 // Fast path: Most strings are short, if the buffer already has space, |
293 // add to it directly. | 279 // add to it directly. |
294 NSUInteger bufferBytesLeft = state_.size - state_.position; | 280 NSUInteger bufferBytesLeft = state_.size - state_.position; |
295 if (bufferBytesLeft >= length) { | 281 if (bufferBytesLeft >= length) { |
296 NSUInteger usedBufferLength = 0; | 282 NSUInteger usedBufferLength = 0; |
297 BOOL result; | 283 BOOL result; |
298 if (quickString != NULL) { | 284 if (quickString != NULL) { |
299 memcpy(state_.bytes + state_.position, quickString, length); | 285 memcpy(state_.bytes + state_.position, quickString, length); |
300 usedBufferLength = length; | 286 usedBufferLength = length; |
301 result = YES; | 287 result = YES; |
302 } else { | 288 } else { |
303 result = [value getBytes:state_.bytes + state_.position | 289 result = [value getBytes:state_.bytes + state_.position |
304 maxLength:bufferBytesLeft | 290 maxLength:bufferBytesLeft |
305 usedLength:&usedBufferLength | 291 usedLength:&usedBufferLength |
306 encoding:NSUTF8StringEncoding | 292 encoding:NSUTF8StringEncoding |
307 options:0 | 293 options:(NSStringEncodingConversionOptions)0 |
308 range:NSMakeRange(0, [value length]) | 294 range:NSMakeRange(0, [value length]) |
309 remainingRange:NULL]; | 295 remainingRange:NULL]; |
310 } | 296 } |
311 if (result) { | 297 if (result) { |
312 NSAssert2((usedBufferLength == length), | 298 NSAssert2((usedBufferLength == length), |
313 @"Our UTF8 calc was wrong? %tu vs %zd", usedBufferLength, | 299 @"Our UTF8 calc was wrong? %tu vs %zd", usedBufferLength, |
314 length); | 300 length); |
315 state_.position += usedBufferLength; | 301 state_.position += usedBufferLength; |
316 return; | 302 return; |
317 } | 303 } |
(...skipping 656 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
974 } | 960 } |
975 | 961 |
976 - (void)writeRawLittleEndian32:(int32_t)value { | 962 - (void)writeRawLittleEndian32:(int32_t)value { |
977 GPBWriteRawLittleEndian32(&state_, value); | 963 GPBWriteRawLittleEndian32(&state_, value); |
978 } | 964 } |
979 | 965 |
980 - (void)writeRawLittleEndian64:(int64_t)value { | 966 - (void)writeRawLittleEndian64:(int64_t)value { |
981 GPBWriteRawLittleEndian64(&state_, value); | 967 GPBWriteRawLittleEndian64(&state_, value); |
982 } | 968 } |
983 | 969 |
| 970 #pragma clang diagnostic pop |
| 971 |
984 @end | 972 @end |
985 | 973 |
986 size_t GPBComputeDoubleSizeNoTag(Float64 value) { | 974 size_t GPBComputeDoubleSizeNoTag(Float64 value) { |
987 #pragma unused(value) | 975 #pragma unused(value) |
988 return LITTLE_ENDIAN_64_SIZE; | 976 return LITTLE_ENDIAN_64_SIZE; |
989 } | 977 } |
990 | 978 |
991 size_t GPBComputeFloatSizeNoTag(Float32 value) { | 979 size_t GPBComputeFloatSizeNoTag(Float32 value) { |
992 #pragma unused(value) | 980 #pragma unused(value) |
993 return LITTLE_ENDIAN_32_SIZE; | 981 return LITTLE_ENDIAN_32_SIZE; |
(...skipping 29 matching lines...) Expand all Loading... |
1023 #pragma unused(value) | 1011 #pragma unused(value) |
1024 return LITTLE_ENDIAN_32_SIZE; | 1012 return LITTLE_ENDIAN_32_SIZE; |
1025 } | 1013 } |
1026 | 1014 |
1027 size_t GPBComputeBoolSizeNoTag(BOOL value) { | 1015 size_t GPBComputeBoolSizeNoTag(BOOL value) { |
1028 #pragma unused(value) | 1016 #pragma unused(value) |
1029 return 1; | 1017 return 1; |
1030 } | 1018 } |
1031 | 1019 |
1032 size_t GPBComputeStringSizeNoTag(NSString *value) { | 1020 size_t GPBComputeStringSizeNoTag(NSString *value) { |
1033 // If you are concerned about embedded NULLs see the test in | 1021 NSUInteger length = [value lengthOfBytesUsingEncoding:NSUTF8StringEncoding]; |
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]; | |
1041 return GPBComputeRawVarint32SizeForInteger(length) + length; | 1022 return GPBComputeRawVarint32SizeForInteger(length) + length; |
1042 } | 1023 } |
1043 | 1024 |
1044 size_t GPBComputeGroupSizeNoTag(GPBMessage *value) { | 1025 size_t GPBComputeGroupSizeNoTag(GPBMessage *value) { |
1045 return [value serializedSize]; | 1026 return [value serializedSize]; |
1046 } | 1027 } |
1047 | 1028 |
1048 size_t GPBComputeUnknownGroupSizeNoTag(GPBUnknownFieldSet *value) { | 1029 size_t GPBComputeUnknownGroupSizeNoTag(GPBUnknownFieldSet *value) { |
1049 return value.serializedSize; | 1030 return value.serializedSize; |
1050 } | 1031 } |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1212 if ((value & (0xffffffffffffffffL << 14)) == 0) return 2; | 1193 if ((value & (0xffffffffffffffffL << 14)) == 0) return 2; |
1213 if ((value & (0xffffffffffffffffL << 21)) == 0) return 3; | 1194 if ((value & (0xffffffffffffffffL << 21)) == 0) return 3; |
1214 if ((value & (0xffffffffffffffffL << 28)) == 0) return 4; | 1195 if ((value & (0xffffffffffffffffL << 28)) == 0) return 4; |
1215 if ((value & (0xffffffffffffffffL << 35)) == 0) return 5; | 1196 if ((value & (0xffffffffffffffffL << 35)) == 0) return 5; |
1216 if ((value & (0xffffffffffffffffL << 42)) == 0) return 6; | 1197 if ((value & (0xffffffffffffffffL << 42)) == 0) return 6; |
1217 if ((value & (0xffffffffffffffffL << 49)) == 0) return 7; | 1198 if ((value & (0xffffffffffffffffL << 49)) == 0) return 7; |
1218 if ((value & (0xffffffffffffffffL << 56)) == 0) return 8; | 1199 if ((value & (0xffffffffffffffffL << 56)) == 0) return 8; |
1219 if ((value & (0xffffffffffffffffL << 63)) == 0) return 9; | 1200 if ((value & (0xffffffffffffffffL << 63)) == 0) return 9; |
1220 return 10; | 1201 return 10; |
1221 } | 1202 } |
OLD | NEW |