aboutsummaryrefslogtreecommitdiff
path: root/Foundation/GTMStringEncoding.m
blob: 1cd63526941cfae1e40ddd0aa8d8fc4c770a216e (plain)
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
//
//  GTMStringEncoding.m
//
//  Copyright 2009 Google Inc.
//
//  Licensed 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 "GTMStringEncoding.h"

enum {
  kUnknownChar = -1,
  kPaddingChar = -2,
  kIgnoreChar = -3
};

@implementation GTMStringEncoding

+ (id)binaryStringEncoding {
  return [self stringEncodingWithString:@"01"];
}

+ (id)hexStringEncoding {
  GTMStringEncoding *ret = [self stringEncodingWithString:
      @"0123456789ABCDEF"];
  [ret addDecodeSynonyms:@"AaBbCcDdEeFf"];
  return ret;
}

+ (id)rfc4648Base32StringEncoding {
  GTMStringEncoding *ret = [self stringEncodingWithString:
      @"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"];
  [ret setPaddingChar:'='];
  [ret setDoPad:YES];
  return ret;
}

+ (id)rfc4648Base32HexStringEncoding {
  GTMStringEncoding *ret = [self stringEncodingWithString:
      @"0123456789ABCDEFGHIJKLMNOPQRSTUV"];
  [ret setPaddingChar:'='];
  [ret setDoPad:YES];
  return ret;
}

+ (id)crockfordBase32StringEncoding {
  GTMStringEncoding *ret = [self stringEncodingWithString:
      @"0123456789ABCDEFGHJKMNPQRSTVWXYZ"];
  [ret addDecodeSynonyms:
      @"0oO1iIlLAaBbCcDdEeFfGgHhJjKkMmNnPpQqRrSsTtVvWwXxYyZz"];
  return ret;
}

+ (id)rfc4648Base64StringEncoding {
  GTMStringEncoding *ret = [self stringEncodingWithString:
      @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"];
  [ret setPaddingChar:'='];
  [ret setDoPad:YES];
  return ret;
}

+ (id)rfc4648Base64WebsafeStringEncoding {
  GTMStringEncoding *ret = [self stringEncodingWithString:
      @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"];
  [ret setPaddingChar:'='];
  [ret setDoPad:YES];
  return ret;
}

GTM_INLINE int lcm(int a, int b) {
  for (int aa = a, bb = b;;) {
    if (aa == bb)
      return aa;
    else if (aa < bb)
      aa += a;
    else
      bb += b;
  }
}

+ (id)stringEncodingWithString:(NSString *)string {
  return [[[self alloc] initWithString:string] autorelease];
}

- (id)initWithString:(NSString *)string {
  if ((self = [super init])) {
    charMapData_ = [[string dataUsingEncoding:NSASCIIStringEncoding] retain];
    if (!charMapData_) {
      _GTMDevLog(@"Unable to convert string to ASCII");
      [self release];
      return nil;
    }
    charMap_ = (char *)[charMapData_ bytes];
    NSUInteger length = [charMapData_ length];
    if (length < 2 || length > 128 || length & (length - 1)) {
      _GTMDevLog(@"Length not a power of 2 between 2 and 128");
      [self release];
      return nil;
    }

    memset(reverseCharMap_, kUnknownChar, sizeof(reverseCharMap_));
    for (unsigned int i = 0; i < length; i++) {
      if (reverseCharMap_[(int)charMap_[i]] != kUnknownChar) {
        _GTMDevLog(@"Duplicate character at pos %d", i);
        [self release];
        return nil;
      }
      reverseCharMap_[(int)charMap_[i]] = i;
    }

    for (NSUInteger i = 1; i < length; i <<= 1)
      shift_++;
    mask_ = (1 << shift_) - 1;
    padLen_ = lcm(8, shift_) / shift_;
  }
  return self;
}

- (void)dealloc {
  [charMapData_ release];
  [super dealloc];
}

- (NSString *)description {
  // TODO(iwade) track synonyms
  return [NSString stringWithFormat:@"<Base%d StringEncoder: %@>",
          1 << shift_, charMapData_];
}

- (void)addDecodeSynonyms:(NSString *)synonyms {
  char *buf = (char *)[synonyms cStringUsingEncoding:NSASCIIStringEncoding];
  int val = kUnknownChar;
  while (*buf) {
    int c = *buf++;
    if (reverseCharMap_[c] == kUnknownChar) {
      reverseCharMap_[c] = val;
    } else {
      val = reverseCharMap_[c];
    }
  }
}

- (void)ignoreCharacters:(NSString *)chars {
  char *buf = (char *)[chars cStringUsingEncoding:NSASCIIStringEncoding];
  while (*buf) {
    int c = *buf++;
    _GTMDevAssert(reverseCharMap_[c] == kUnknownChar,
                  @"Character already mapped");
    reverseCharMap_[c] = kIgnoreChar;
  }
}

- (BOOL)doPad {
  return doPad_;
}

- (void)setDoPad:(BOOL)doPad {
  doPad_ = doPad;
}

- (void)setPaddingChar:(char)c {
  paddingChar_ = c;
  reverseCharMap_[(int)c] = kPaddingChar;
}

- (NSString *)encode:(NSData *)inData {
  NSUInteger inLen = [inData length];
  if (inLen <= 0) {
    _GTMDevLog(@"Empty input");
    return @"";
  }
  unsigned char *inBuf = (unsigned char *)[inData bytes];
  NSUInteger inPos = 0;

  NSUInteger outLen = (inLen * 8 + shift_ - 1) / shift_;
  if (doPad_) {
    outLen = ((outLen + padLen_ - 1) / padLen_) * padLen_;
  }
  NSMutableData *outData = [NSMutableData dataWithLength:outLen];
  unsigned char *outBuf = (unsigned char *)[outData mutableBytes];
  NSUInteger outPos = 0;

  int buffer = inBuf[inPos++];
  int bitsLeft = 8;
  while (bitsLeft > 0 || inPos < inLen) {
    if (bitsLeft < shift_) {
      if (inPos < inLen) {
        buffer <<= 8;
        buffer |= (inBuf[inPos++] & 0xff);
        bitsLeft += 8;
      } else {
        int pad = shift_ - bitsLeft;
        buffer <<= pad;
        bitsLeft += pad;
      }
    }
    int idx = (buffer >> (bitsLeft - shift_)) & mask_;
    bitsLeft -= shift_;
    outBuf[outPos++] = charMap_[idx];
  }

  if (doPad_) {
    while (outPos < outLen)
      outBuf[outPos++] = paddingChar_;
  }

  _GTMDevAssert(outPos == outLen, @"Underflowed output buffer");
  [outData setLength:outPos];

  return [[[NSString alloc] initWithData:outData
                                encoding:NSASCIIStringEncoding] autorelease];
}

- (NSString *)encodeString:(NSString *)inString {
  return [self encode:[inString dataUsingEncoding:NSUTF8StringEncoding]];
}

- (NSData *)decode:(NSString *)inString {
  char *inBuf = (char *)[inString cStringUsingEncoding:NSASCIIStringEncoding];
  if (!inBuf) {
    _GTMDevLog(@"unable to convert buffer to ASCII");
    return nil;
  }
  NSUInteger inLen = strlen(inBuf);

  NSUInteger outLen = inLen * shift_ / 8;
  NSMutableData *outData = [NSMutableData dataWithLength:outLen];
  unsigned char *outBuf = (unsigned char *)[outData mutableBytes];
  NSUInteger outPos = 0;

  int buffer = 0;
  int bitsLeft = 0;
  BOOL expectPad = NO;
  for (NSUInteger i = 0; i < inLen; i++) {
    int val = reverseCharMap_[(int)inBuf[i]];
    switch (val) {
      case kIgnoreChar:
        break;
      case kPaddingChar:
        expectPad = YES;
        break;
      case kUnknownChar:
        _GTMDevLog(@"Unexpected data in input pos %lu", (unsigned long)i);
        return nil;
      default:
        if (expectPad) {
          _GTMDevLog(@"Expected further padding characters");
          return nil;
        }
        buffer <<= shift_;
        buffer |= val & mask_;
        bitsLeft += shift_;
        if (bitsLeft >= 8) {
          outBuf[outPos++] = (unsigned char)(buffer >> (bitsLeft - 8));
          bitsLeft -= 8;
        }
        break;
    }
  }

  if (bitsLeft && buffer & ((1 << bitsLeft) - 1)) {
    _GTMDevLog(@"Incomplete trailing data");
    return nil;
  }

  // Shorten buffer if needed due to padding chars
  _GTMDevAssert(outPos <= outLen, @"Overflowed buffer");
  [outData setLength:outPos];

  return outData;
}

- (NSString *)stringByDecoding:(NSString *)inString {
  NSData *ret = [self decode:inString];
  return [[[NSString alloc] initWithData:ret
                                encoding:NSUTF8StringEncoding] autorelease];
}

@end