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