/
TBinaryProtocol.m
477 lines (381 loc) · 10.6 KB
/
TBinaryProtocol.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#import "TBinaryProtocol.h"
#import "TProtocolException.h"
int32_t VERSION_1 = 0x80010000;
int32_t VERSION_MASK = 0xffff0000;
static TBinaryProtocolFactory * gSharedFactory = nil;
@implementation TBinaryProtocolFactory
+ (TBinaryProtocolFactory *) sharedFactory {
if (gSharedFactory == nil) {
gSharedFactory = [[TBinaryProtocolFactory alloc] init];
}
return gSharedFactory;
}
- (TBinaryProtocol *) newProtocolOnTransport: (id <TTransport>) transport {
return [[[TBinaryProtocol alloc] initWithTransport: transport] autorelease];
}
@end
@implementation TBinaryProtocol
- (id) initWithTransport: (id <TTransport>) transport
{
return [self initWithTransport: transport strictRead: NO strictWrite: NO];
}
- (id) initWithTransport: (id <TTransport>) transport
strictRead: (BOOL) strictRead
strictWrite: (BOOL) strictWrite
{
self = [super init];
mTransport = [transport retain];
mStrictRead = strictRead;
mStrictWrite = strictWrite;
return self;
}
- (int32_t) messageSizeLimit
{
return mMessageSizeLimit;
}
- (void) setMessageSizeLimit: (int32_t) sizeLimit
{
mMessageSizeLimit = sizeLimit;
}
- (void) dealloc
{
[mTransport release];
[super dealloc];
}
- (id <TTransport>) transport
{
return mTransport;
}
- (NSString *) readStringBody: (int) size
{
char * buffer = malloc(size+1);
if (!buffer) {
@throw [TProtocolException exceptionWithName: @"TProtocolException"
reason: [NSString stringWithFormat: @"Unable to allocate memory in %s, size: %i",
__PRETTY_FUNCTION__,
size]];;
}
[mTransport readAll: (uint8_t *) buffer offset: 0 length: size];
buffer[size] = 0;
NSString * result = [NSString stringWithUTF8String: buffer];
free(buffer);
return result;
}
- (void) readMessageBeginReturningName: (NSString **) name
type: (int *) type
sequenceID: (int *) sequenceID
{
int32_t size = [self readI32];
if (size < 0) {
int version = size & VERSION_MASK;
if (version != VERSION_1) {
@throw [TProtocolException exceptionWithName: @"TProtocolException"
reason: @"Bad version in readMessageBegin"];
}
if (type != NULL) {
*type = version & 0x00FF;
}
NSString * messageName = [self readString];
if (name != NULL) {
*name = messageName;
}
int seqID = [self readI32];
if (sequenceID != NULL) {
*sequenceID = seqID;
}
} else {
if (mStrictRead) {
@throw [TProtocolException exceptionWithName: @"TProtocolException"
reason: @"Missing version in readMessageBegin, old client?"];
}
if ([self messageSizeLimit] > 0 && size > [self messageSizeLimit]) {
@throw [TProtocolException exceptionWithName: @"TProtocolException"
reason: [NSString stringWithFormat: @"Message too big. Size limit is: %d Message size is: %d",
mMessageSizeLimit,
size]];
}
NSString * messageName = [self readStringBody: size];
if (name != NULL) {
*name = messageName;
}
int messageType = [self readByte];
if (type != NULL) {
*type = messageType;
}
int seqID = [self readI32];
if (sequenceID != NULL) {
*sequenceID = seqID;
}
}
}
- (void) readMessageEnd {}
- (void) readStructBeginReturningName: (NSString **) name
{
if (name != NULL) {
*name = nil;
}
}
- (void) readStructEnd {}
- (void) readFieldBeginReturningName: (NSString **) name
type: (int *) fieldType
fieldID: (int *) fieldID
{
if (name != NULL) {
*name = nil;
}
int ft = [self readByte];
if (fieldType != NULL) {
*fieldType = ft;
}
if (ft != TType_STOP) {
int fid = [self readI16];
if (fieldID != NULL) {
*fieldID = fid;
}
}
}
- (void) readFieldEnd {}
- (int32_t) readI32
{
uint8_t i32rd[4];
[mTransport readAll: i32rd offset: 0 length: 4];
return
((i32rd[0] & 0xff) << 24) |
((i32rd[1] & 0xff) << 16) |
((i32rd[2] & 0xff) << 8) |
((i32rd[3] & 0xff));
}
- (NSString *) readString
{
int size = [self readI32];
return [self readStringBody: size];
}
- (BOOL) readBool
{
return [self readByte] == 1;
}
- (uint8_t) readByte
{
uint8_t myByte;
[mTransport readAll: &myByte offset: 0 length: 1];
return myByte;
}
- (short) readI16
{
uint8_t buff[2];
[mTransport readAll: buff offset: 0 length: 2];
return (short)
(((buff[0] & 0xff) << 8) |
((buff[1] & 0xff)));
return 0;
}
- (int64_t) readI64;
{
uint8_t i64rd[8];
[mTransport readAll: i64rd offset: 0 length: 8];
return
((int64_t)(i64rd[0] & 0xff) << 56) |
((int64_t)(i64rd[1] & 0xff) << 48) |
((int64_t)(i64rd[2] & 0xff) << 40) |
((int64_t)(i64rd[3] & 0xff) << 32) |
((int64_t)(i64rd[4] & 0xff) << 24) |
((int64_t)(i64rd[5] & 0xff) << 16) |
((int64_t)(i64rd[6] & 0xff) << 8) |
((int64_t)(i64rd[7] & 0xff));
}
- (double) readDouble;
{
// FIXME - will this get us into trouble on PowerPC?
int64_t ieee754 = [self readI64];
return *((double *) &ieee754);
}
- (NSData *) readBinary
{
int32_t size = [self readI32];
uint8_t * buff = malloc(size);
if (buff == NULL) {
@throw [TProtocolException
exceptionWithName: @"TProtocolException"
reason: [NSString stringWithFormat: @"Out of memory. Unable to allocate %d bytes trying to read binary data.",
size]];
}
[mTransport readAll: buff offset: 0 length: size];
return [NSData dataWithBytesNoCopy: buff length: size];
}
- (void) readMapBeginReturningKeyType: (int *) keyType
valueType: (int *) valueType
size: (int *) size
{
int kt = [self readByte];
int vt = [self readByte];
int s = [self readI32];
if (keyType != NULL) {
*keyType = kt;
}
if (valueType != NULL) {
*valueType = vt;
}
if (size != NULL) {
*size = s;
}
}
- (void) readMapEnd {}
- (void) readSetBeginReturningElementType: (int *) elementType
size: (int *) size
{
int et = [self readByte];
int s = [self readI32];
if (elementType != NULL) {
*elementType = et;
}
if (size != NULL) {
*size = s;
}
}
- (void) readSetEnd {}
- (void) readListBeginReturningElementType: (int *) elementType
size: (int *) size
{
int et = [self readByte];
int s = [self readI32];
if (elementType != NULL) {
*elementType = et;
}
if (size != NULL) {
*size = s;
}
}
- (void) readListEnd {}
- (void) writeByte: (uint8_t) value
{
[mTransport write: &value offset: 0 length: 1];
}
- (void) writeMessageBeginWithName: (NSString *) name
type: (int) messageType
sequenceID: (int) sequenceID
{
if (mStrictWrite) {
int version = VERSION_1 | messageType;
[self writeI32: version];
[self writeString: name];
[self writeI32: sequenceID];
} else {
[self writeString: name];
[self writeByte: messageType];
[self writeI32: sequenceID];
}
}
- (void) writeMessageEnd {}
- (void) writeStructBeginWithName: (NSString *) name {}
- (void) writeStructEnd {}
- (void) writeFieldBeginWithName: (NSString *) name
type: (int) fieldType
fieldID: (int) fieldID
{
[self writeByte: fieldType];
[self writeI16: fieldID];
}
- (void) writeI32: (int32_t) value
{
uint8_t buff[4];
buff[0] = 0xFF & (value >> 24);
buff[1] = 0xFF & (value >> 16);
buff[2] = 0xFF & (value >> 8);
buff[3] = 0xFF & value;
[mTransport write: buff offset: 0 length: 4];
}
- (void) writeI16: (short) value
{
uint8_t buff[2];
buff[0] = 0xff & (value >> 8);
buff[1] = 0xff & value;
[mTransport write: buff offset: 0 length: 2];
}
- (void) writeI64: (int64_t) value
{
uint8_t buff[8];
buff[0] = 0xFF & (value >> 56);
buff[1] = 0xFF & (value >> 48);
buff[2] = 0xFF & (value >> 40);
buff[3] = 0xFF & (value >> 32);
buff[4] = 0xFF & (value >> 24);
buff[5] = 0xFF & (value >> 16);
buff[6] = 0xFF & (value >> 8);
buff[7] = 0xFF & value;
[mTransport write: buff offset: 0 length: 8];
}
- (void) writeDouble: (double) value
{
// spit out IEEE 754 bits - FIXME - will this get us in trouble on
// PowerPC?
[self writeI64: *((int64_t *) &value)];
}
- (void) writeString: (NSString *) value
{
if (value != nil) {
const char * utf8Bytes = [value UTF8String];
size_t length = strlen(utf8Bytes);
[self writeI32: length];
[mTransport write: (uint8_t *) utf8Bytes offset: 0 length: length];
} else {
// instead of crashing when we get null, let's write out a zero
// length string
[self writeI32: 0];
}
}
- (void) writeBinary: (NSData *) data
{
[self writeI32: [data length]];
[mTransport write: [data bytes] offset: 0 length: [data length]];
}
- (void) writeFieldStop
{
[self writeByte: TType_STOP];
}
- (void) writeFieldEnd {}
- (void) writeMapBeginWithKeyType: (int) keyType
valueType: (int) valueType
size: (int) size
{
[self writeByte: keyType];
[self writeByte: valueType];
[self writeI32: size];
}
- (void) writeMapEnd {}
- (void) writeSetBeginWithElementType: (int) elementType
size: (int) size
{
[self writeByte: elementType];
[self writeI32: size];
}
- (void) writeSetEnd {}
- (void) writeListBeginWithElementType: (int) elementType
size: (int) size
{
[self writeByte: elementType];
[self writeI32: size];
}
- (void) writeListEnd {}
- (void) writeBool: (BOOL) value
{
[self writeByte: (value ? 1 : 0)];
}
@end