GTMStringEncoding.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //
  2. // GTMStringEncoding.h
  3. //
  4. // Copyright 2010 Google Inc.
  5. //
  6. // Licensed under the Apache License, Version 2.0 (the "License"); you may not
  7. // use this file except in compliance with the License. You may obtain a copy
  8. // 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, software
  13. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  14. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  15. // License for the specific language governing permissions and limitations under
  16. // the License.
  17. //
  18. #import <Foundation/Foundation.h>
  19. #import "GTMDefines.h"
  20. // A generic class for arbitrary base-2 to 128 string encoding and decoding.
  21. @interface GTMStringEncoding : NSObject {
  22. @private
  23. NSData *charMapData_;
  24. char *charMap_;
  25. int reverseCharMap_[128];
  26. int shift_;
  27. int mask_;
  28. BOOL doPad_;
  29. char paddingChar_;
  30. int padLen_;
  31. }
  32. // Create a new, autoreleased GTMStringEncoding object with a standard encoding.
  33. + (id)binaryStringEncoding;
  34. + (id)hexStringEncoding;
  35. + (id)rfc4648Base32StringEncoding;
  36. + (id)rfc4648Base32HexStringEncoding;
  37. + (id)crockfordBase32StringEncoding;
  38. + (id)rfc4648Base64StringEncoding;
  39. + (id)rfc4648Base64WebsafeStringEncoding;
  40. // Create a new, autoreleased GTMStringEncoding object with the given string,
  41. // as described below.
  42. + (id)stringEncodingWithString:(NSString *)string;
  43. // Initialize a new GTMStringEncoding object with the string.
  44. //
  45. // The length of the string must be a power of 2, at least 2 and at most 128.
  46. // Only 7-bit ASCII characters are permitted in the string.
  47. //
  48. // These characters are the canonical set emitted during encoding.
  49. // If the characters have alternatives (e.g. case, easily transposed) then use
  50. // addDecodeSynonyms: to configure them.
  51. - (id)initWithString:(NSString *)string;
  52. // Add decoding synonyms as specified in the synonyms argument.
  53. //
  54. // It should be a sequence of one previously reverse mapped character,
  55. // followed by one or more non-reverse mapped character synonyms.
  56. // Only 7-bit ASCII characters are permitted in the string.
  57. //
  58. // e.g. If a GTMStringEncoder object has already been initialised with a set
  59. // of characters excluding I, L and O (to avoid confusion with digits) and you
  60. // want to accept them as digits you can call addDecodeSynonyms:@"0oO1iIlL".
  61. - (void)addDecodeSynonyms:(NSString *)synonyms;
  62. // A sequence of characters to ignore if they occur during encoding.
  63. // Only 7-bit ASCII characters are permitted in the string.
  64. - (void)ignoreCharacters:(NSString *)chars;
  65. // Indicates whether padding is performed during encoding.
  66. - (BOOL)doPad;
  67. - (void)setDoPad:(BOOL)doPad;
  68. // Sets the padding character to use during encoding.
  69. - (void)setPaddingChar:(char)c;
  70. // Encode a raw binary buffer to a 7-bit ASCII string.
  71. - (NSString *)encode:(NSData *)data __attribute__((deprecated("Use encode:error:")));
  72. - (NSString *)encodeString:(NSString *)string __attribute__((deprecated("Use encodeString:error:")));
  73. - (NSString *)encode:(NSData *)data error:(NSError **)error;
  74. - (NSString *)encodeString:(NSString *)string error:(NSError **)error;
  75. // Decode a 7-bit ASCII string to a raw binary buffer.
  76. - (NSData *)decode:(NSString *)string __attribute__((deprecated("Use decode:error:")));
  77. - (NSString *)stringByDecoding:(NSString *)string __attribute__((deprecated("Use stringByDecoding:error:")));
  78. - (NSData *)decode:(NSString *)string error:(NSError **)error;
  79. - (NSString *)stringByDecoding:(NSString *)string error:(NSError **)error;
  80. @end
  81. FOUNDATION_EXPORT NSString *const GTMStringEncodingErrorDomain;
  82. FOUNDATION_EXPORT NSString *const GTMStringEncodingBadCharacterIndexKey; // NSNumber
  83. typedef NS_ENUM(NSInteger, GTMStringEncodingError) {
  84. // Unable to convert a buffer to NSASCIIStringEncoding.
  85. GTMStringEncodingErrorUnableToConverToAscii = 1024,
  86. // Unable to convert a buffer to NSUTF8StringEncoding.
  87. GTMStringEncodingErrorUnableToConverToUTF8,
  88. // Encountered a bad character.
  89. // GTMStringEncodingBadCharacterIndexKey will have the index of the character.
  90. GTMStringEncodingErrorUnknownCharacter,
  91. // The data had a padding character in the middle of the data. Padding characters
  92. // can only be at the end.
  93. GTMStringEncodingErrorExpectedPadding,
  94. // There is unexpected data at the end of the data that could not be decoded.
  95. GTMStringEncodingErrorIncompleteTrailingData,
  96. };