David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | /* |
| 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 Slee | 7e9eea4 | 2007-09-10 21:00:23 +0000 | [diff] [blame] | 20 | #import "TProtocolUtil.h" |
| 21 | |
| 22 | @implementation TProtocolUtil |
| 23 | |
Jens Geyer | 56e5b9b | 2015-10-09 22:01:55 +0200 | [diff] [blame] | 24 | +(BOOL) skipType:(int)type onProtocol:(id <TProtocol>)protocol error:(NSError **)error |
Mark Slee | 7e9eea4 | 2007-09-10 21:00:23 +0000 | [diff] [blame] | 25 | { |
| 26 | switch (type) { |
Jens Geyer | 56e5b9b | 2015-10-09 22:01:55 +0200 | [diff] [blame] | 27 | 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 Slee | 7e9eea4 | 2007-09-10 21:00:23 +0000 | [diff] [blame] | 87 | while (true) { |
Jens Geyer | 56e5b9b | 2015-10-09 22:01:55 +0200 | [diff] [blame] | 88 | SInt32 fieldType; |
| 89 | if (![protocol readFieldBeginReturningName:nil type:&fieldType fieldID:nil error:error]) { |
| 90 | return NO; |
| 91 | } |
| 92 | if (fieldType == TTypeSTOP) { |
Mark Slee | 7e9eea4 | 2007-09-10 21:00:23 +0000 | [diff] [blame] | 93 | break; |
| 94 | } |
Jens Geyer | 56e5b9b | 2015-10-09 22:01:55 +0200 | [diff] [blame] | 95 | if (![self skipType:fieldType onProtocol:protocol error:error]) { |
| 96 | return NO; |
| 97 | } |
| 98 | if (![protocol readFieldEnd:error]) { |
| 99 | return NO; |
| 100 | } |
Mark Slee | 7e9eea4 | 2007-09-10 21:00:23 +0000 | [diff] [blame] | 101 | } |
Jens Geyer | 56e5b9b | 2015-10-09 22:01:55 +0200 | [diff] [blame] | 102 | 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 Slee | 7e9eea4 | 2007-09-10 21:00:23 +0000 | [diff] [blame] | 115 | int i; |
| 116 | for (i = 0; i < size; i++) { |
Jens Geyer | 56e5b9b | 2015-10-09 22:01:55 +0200 | [diff] [blame] | 117 | if (![TProtocolUtil skipType:keyType onProtocol:protocol error:error]) { |
| 118 | return NO; |
Mark Slee | 7e9eea4 | 2007-09-10 21:00:23 +0000 | [diff] [blame] | 119 | } |
Jens Geyer | 56e5b9b | 2015-10-09 22:01:55 +0200 | [diff] [blame] | 120 | if (![TProtocolUtil skipType:valueType onProtocol:protocol error:error]) { |
| 121 | return NO; |
Mark Slee | 7e9eea4 | 2007-09-10 21:00:23 +0000 | [diff] [blame] | 122 | } |
Mark Slee | 7e9eea4 | 2007-09-10 21:00:23 +0000 | [diff] [blame] | 123 | } |
Jens Geyer | 56e5b9b | 2015-10-09 22:01:55 +0200 | [diff] [blame] | 124 | if (![protocol readMapEnd:error]) { |
| 125 | return NO; |
| 126 | } |
Mark Slee | 7e9eea4 | 2007-09-10 21:00:23 +0000 | [diff] [blame] | 127 | } |
Jens Geyer | 56e5b9b | 2015-10-09 22:01:55 +0200 | [diff] [blame] | 128 | 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 Slee | 7e9eea4 | 2007-09-10 21:00:23 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | @end |