blob: c0d65aceaf13c2c9bea1f9c8dc938b4eb8a89edf [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
Mark Slee7e9eea42007-09-10 21:00:23 +000020#import "TProtocolUtil.h"
21
22@implementation TProtocolUtil
23
Jens Geyer56e5b9b2015-10-09 22:01:55 +020024+(BOOL) skipType:(int)type onProtocol:(id <TProtocol>)protocol error:(NSError **)error
Mark Slee7e9eea42007-09-10 21:00:23 +000025{
26 switch (type) {
Jens Geyer56e5b9b2015-10-09 22:01:55 +020027 case TTypeBOOL: {
28 BOOL val;
29 if (![protocol readBool:&val error:error]) {
30 return NO;
31 }
32 }
33 break;
34
35 case TTypeBYTE: {
36 UInt8 val;
37 if (![protocol readByte:&val error:error]) {
38 return NO;
39 }
40 }
41 break;
42
43 case TTypeI16: {
44 SInt16 val;
45 if (![protocol readI16:&val error:error]) {
46 return NO;
47 }
48 }
49 break;
50
51 case TTypeI32: {
52 SInt32 val;
53 if (![protocol readI32:&val error:error]) {
54 return NO;
55 }
56 }
57 break;
58
59 case TTypeI64: {
60 SInt64 val;
61 if (![protocol readI64:&val error:error]) {
62 return NO;
63 }
64 }
65 break;
66
67 case TTypeDOUBLE: {
68 double val;
69 if (![protocol readDouble:&val error:error]) {
70 return NO;
71 }
72 }
73 break;
74
75 case TTypeSTRING: {
76 NSString *val;
77 if (![protocol readString:&val error:error]) {
78 return NO;
79 }
80 }
81 break;
82
83 case TTypeSTRUCT: {
84 if (![protocol readStructBeginReturningName:NULL error:error]) {
85 return NO;
86 }
Mark Slee7e9eea42007-09-10 21:00:23 +000087 while (true) {
Jens Geyer56e5b9b2015-10-09 22:01:55 +020088 SInt32 fieldType;
89 if (![protocol readFieldBeginReturningName:nil type:&fieldType fieldID:nil error:error]) {
90 return NO;
91 }
92 if (fieldType == TTypeSTOP) {
Mark Slee7e9eea42007-09-10 21:00:23 +000093 break;
94 }
Jens Geyer56e5b9b2015-10-09 22:01:55 +020095 if (![self skipType:fieldType onProtocol:protocol error:error]) {
96 return NO;
97 }
98 if (![protocol readFieldEnd:error]) {
99 return NO;
100 }
Mark Slee7e9eea42007-09-10 21:00:23 +0000101 }
Jens Geyer56e5b9b2015-10-09 22:01:55 +0200102 if (![protocol readStructEnd:error]) {
103 return NO;
104 }
105 }
106 break;
107
108 case TTypeMAP: {
109 SInt32 keyType;
110 SInt32 valueType;
111 SInt32 size;
112 if (![protocol readMapBeginReturningKeyType:&keyType valueType:&valueType size:&size error:error]) {
113 return NO;
114 }
Mark Slee7e9eea42007-09-10 21:00:23 +0000115 int i;
116 for (i = 0; i < size; i++) {
Jens Geyer56e5b9b2015-10-09 22:01:55 +0200117 if (![TProtocolUtil skipType:keyType onProtocol:protocol error:error]) {
118 return NO;
Mark Slee7e9eea42007-09-10 21:00:23 +0000119 }
Jens Geyer56e5b9b2015-10-09 22:01:55 +0200120 if (![TProtocolUtil skipType:valueType onProtocol:protocol error:error]) {
121 return NO;
Mark Slee7e9eea42007-09-10 21:00:23 +0000122 }
Mark Slee7e9eea42007-09-10 21:00:23 +0000123 }
Jens Geyer56e5b9b2015-10-09 22:01:55 +0200124 if (![protocol readMapEnd:error]) {
125 return NO;
126 }
Mark Slee7e9eea42007-09-10 21:00:23 +0000127 }
Jens Geyer56e5b9b2015-10-09 22:01:55 +0200128 break;
129
130 case TTypeSET: {
131 SInt32 elemType;
132 SInt32 size;
133 if (![protocol readSetBeginReturningElementType:&elemType size:&size error:error]) {
134 return NO;
135 }
136 int i;
137 for (i = 0; i < size; i++) {
138 if (![TProtocolUtil skipType:elemType onProtocol:protocol error:error]) {
139 return NO;
140 }
141 }
142 if (![protocol readSetEnd:error]) {
143 return NO;
144 }
145 }
146 break;
147
148 case TTypeLIST: {
149 SInt32 elemType;
150 SInt32 size;
151 if (![protocol readListBeginReturningElementType:&elemType size:&size error:error]) {
152 return NO;
153 }
154 int i;
155 for (i = 0; i < size; i++) {
156 if (![TProtocolUtil skipType:elemType onProtocol:protocol error:error]) {
157 return NO;
158 }
159 }
160 if (![protocol readListEnd:error]) {
161 return NO;
162 }
163 }
164 break;
165
166 }
167
168 return YES;
Mark Slee7e9eea42007-09-10 21:00:23 +0000169}
170
171@end