GTMBase64.h 5.5 KB

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