QN_GTM_Base64.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //
  2. // GTMBase64.h
  3. //
  4. // Copyright 2006-2008 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. // GTMBase64
  20. //
  21. /// Helper for handling Base64 and WebSafeBase64 encodings
  22. //
  23. /// The webSafe methods use different character set and also the results aren't
  24. /// always padded to a multiple of 4 characters. This is done so the resulting
  25. /// data can be used in urls and url query arguments without needing any
  26. /// encoding. You must use the webSafe* methods together, the data does not
  27. /// interop with the RFC methods.
  28. //
  29. @interface QN_GTM_Base64 : NSObject
  30. //
  31. // Standard Base64 (RFC) handling
  32. //
  33. // encodeData:
  34. //
  35. /// Base64 encodes contents of the NSData object.
  36. //
  37. /// Returns:
  38. /// A new autoreleased NSData with the encoded payload. nil for any error.
  39. //
  40. + (NSData *)encodeData:(NSData *)data;
  41. // decodeData:
  42. //
  43. /// Base64 decodes contents of the NSData object.
  44. //
  45. /// Returns:
  46. /// A new autoreleased NSData with the decoded payload. nil for any error.
  47. //
  48. + (NSData *)decodeData:(NSData *)data;
  49. // encodeBytes:length:
  50. //
  51. /// Base64 encodes the data pointed at by |bytes|.
  52. //
  53. /// Returns:
  54. /// A new autoreleased NSData with the encoded payload. nil for any error.
  55. //
  56. + (NSData *)encodeBytes:(const void *)bytes length:(NSUInteger)length;
  57. // decodeBytes:length:
  58. //
  59. /// Base64 decodes the data pointed at by |bytes|.
  60. //
  61. /// Returns:
  62. /// A new autoreleased NSData with the encoded payload. nil for any error.
  63. //
  64. + (NSData *)decodeBytes:(const void *)bytes length:(NSUInteger)length;
  65. // stringByEncodingData:
  66. //
  67. /// Base64 encodes contents of the NSData object.
  68. //
  69. /// Returns:
  70. /// A new autoreleased NSString with the encoded payload. nil for any error.
  71. //
  72. + (NSString *)stringByEncodingData:(NSData *)data;
  73. // stringByEncodingBytes:length:
  74. //
  75. /// Base64 encodes the data pointed at by |bytes|.
  76. //
  77. /// Returns:
  78. /// A new autoreleased NSString with the encoded payload. nil for any error.
  79. //
  80. + (NSString *)stringByEncodingBytes:(const void *)bytes length:(NSUInteger)length;
  81. // decodeString:
  82. //
  83. /// Base64 decodes contents of the NSString.
  84. //
  85. /// Returns:
  86. /// A new autoreleased NSData with the decoded payload. nil for any error.
  87. //
  88. + (NSData *)decodeString:(NSString *)string;
  89. //
  90. // Modified Base64 encoding so the results can go onto urls.
  91. //
  92. // The changes are in the characters generated and also allows the result to
  93. // not be padded to a multiple of 4.
  94. // Must use the matching call to encode/decode, won't interop with the
  95. // RFC versions.
  96. //
  97. // webSafeEncodeData:padded:
  98. //
  99. /// WebSafe Base64 encodes contents of the NSData object. If |padded| is YES
  100. /// then padding characters are added so the result length is a multiple of 4.
  101. //
  102. /// Returns:
  103. /// A new autoreleased NSData with the encoded payload. nil for any error.
  104. //
  105. + (NSData *)webSafeEncodeData:(NSData *)data
  106. padded:(BOOL)padded;
  107. // webSafeDecodeData:
  108. //
  109. /// WebSafe Base64 decodes contents of the NSData object.
  110. //
  111. /// Returns:
  112. /// A new autoreleased NSData with the decoded payload. nil for any error.
  113. //
  114. + (NSData *)webSafeDecodeData:(NSData *)data;
  115. // webSafeEncodeBytes:length:padded:
  116. //
  117. /// WebSafe Base64 encodes the data pointed at by |bytes|. If |padded| is YES
  118. /// then padding characters are added so the result length is a multiple of 4.
  119. //
  120. /// Returns:
  121. /// A new autoreleased NSData with the encoded payload. nil for any error.
  122. //
  123. + (NSData *)webSafeEncodeBytes:(const void *)bytes
  124. length:(NSUInteger)length
  125. padded:(BOOL)padded;
  126. // webSafeDecodeBytes:length:
  127. //
  128. /// WebSafe Base64 decodes the data pointed at by |bytes|.
  129. //
  130. /// Returns:
  131. /// A new autoreleased NSData with the encoded payload. nil for any error.
  132. //
  133. + (NSData *)webSafeDecodeBytes:(const void *)bytes length:(NSUInteger)length;
  134. // stringByWebSafeEncodingData:padded:
  135. //
  136. /// WebSafe Base64 encodes contents of the NSData object. If |padded| is YES
  137. /// then padding characters are added so the result length is a multiple of 4.
  138. //
  139. /// Returns:
  140. /// A new autoreleased NSString with the encoded payload. nil for any error.
  141. //
  142. + (NSString *)stringByWebSafeEncodingData:(NSData *)data
  143. padded:(BOOL)padded;
  144. // stringByWebSafeEncodingBytes:length:padded:
  145. //
  146. /// WebSafe Base64 encodes the data pointed at by |bytes|. If |padded| is YES
  147. /// then padding characters are added so the result length is a multiple of 4.
  148. //
  149. /// Returns:
  150. /// A new autoreleased NSString with the encoded payload. nil for any error.
  151. //
  152. + (NSString *)stringByWebSafeEncodingBytes:(const void *)bytes
  153. length:(NSUInteger)length
  154. padded:(BOOL)padded;
  155. // webSafeDecodeString:
  156. //
  157. /// WebSafe Base64 decodes contents of the NSString.
  158. //
  159. /// Returns:
  160. /// A new autoreleased NSData with the decoded payload. nil for any error.
  161. //
  162. + (NSData *)webSafeDecodeString:(NSString *)string;
  163. @end