2 * Copyright (c) 2011,2013 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
25 #ifndef COMMON_BASE_XX_H
26 #define COMMON_BASE_XX_H
28 #if !defined(COMMON_NUMERICS_H)
29 #include <CommonNumerics/CommonNumerics.h>
38 @abstract Encodings available through CommonBaseXX().
40 @constant kCNEncodingBase64 Base64 Encoding.
41 @constant kCNEncodingBase32 Base32 Encoding.
42 @constant kCNEncodingBase32HEX Base32 Encoding -
43 @constant kCNEncodingBase32Recovery Base32 Simplified Encoding.
46 kCNEncodingBase64
= 0x0001,
47 kCNEncodingBase32
= 0x0002,
48 kCNEncodingBase32Recovery
= 0x0003,
49 kCNEncodingBase32HEX
= 0x0004,
50 kCNEncodingBase16
= 0x0005,
51 kCNEncodingCustom
= 0xcafe
53 typedef uint32_t CNEncodings
;
56 @enum kCNEncodingDirection
57 @abstract Determine whether the CNEncoderRef is to be used
60 @constant kCNEncode Encode (base256 to baseXX)
61 @constant kCNDecode Decode (baseXX to base256)
68 typedef uint32_t CNEncodingDirection
;
72 @abstract Opaque reference to a CNEncoder object.
75 typedef struct _CNEncoder
*CNEncoderRef
;
79 @abstract One-Shot baseXX encode or decode.
80 @param encoding selects one of the base encodings above.
81 @param direction Designate the direction (encode or decode)
82 @param in The bytes to be processed.
83 @param inLen The number of bytes to be processed.
84 @param out The destination of the processed data.
85 @param outLen The length of the processed data.
86 @result kCCSuccess or one of kCCParamErr, kCCMemoryFailure.
91 CNEncode(CNEncodings encoding
,
92 CNEncodingDirection direction
,
93 const void *in
, const size_t inLen
,
94 void *out
, size_t *outLen
)
95 __OSX_AVAILABLE_STARTING(__MAC_10_9
, __IPHONE_5_0
);
101 @function CCEncoderCreate
102 @abstract Create a base encoder context.
103 @param encoding selects one of the base encodings above.
104 @param direction Designate the direction (encode or decode) for this
106 @param coderRef A (required) pointer to the returned CNEncoderRef.
110 CNEncoderCreate(CNEncodings encoding
,
111 CNEncodingDirection direction
,
112 CNEncoderRef
*encoderRef
) /* RETURNED */
113 __OSX_AVAILABLE_STARTING(__MAC_10_9
, __IPHONE_5_0
);
116 @function CCEncoderCreateCustom
117 @abstract Create a custom base encoder context.
118 @param name A name for this encoder (optional)
119 @param baseNum The base of the encoding (16, 32, 64)
120 @param charMap A string containing the characters to map an encoded
122 @param padding The character to use for padding (usually '=')
123 @param direction Designate the direction (encode or decode) for this
125 @param coderRef A (required) pointer to the returned CNEncoderRef.
129 CNEncoderCreateCustom(
131 const uint8_t baseNum
,
134 CNEncodingDirection direction
,
135 CNEncoderRef
*coderRef
) /* RETURNED */
136 __OSX_AVAILABLE_STARTING(__MAC_10_9
, __IPHONE_5_0
);
139 @function CNEncoderRelease
140 @abstract Release a CNEncoderRef and associated objects.
143 CNEncoderRelease(CNEncoderRef
*coderRef
)
144 __OSX_AVAILABLE_STARTING(__MAC_10_9
, __IPHONE_5_0
);
148 @function CNEncoderGetOutputLength
149 @abstract Determine the size required to hold the result of processing the
152 @param coderRef A CNEncoderRef obtained through CNEncoderCreate()
153 or CNEncoderCreateCustom().
155 @param inLen The number of bytes to be processed.
157 @result The length required for the encoding. Zero (0) will be returned in
158 the result of a NULL input pointer for the "in" parameter.
162 CNEncoderGetOutputLength(CNEncoderRef coderRef
, const size_t inLen
)
163 __OSX_AVAILABLE_STARTING(__MAC_10_9
, __IPHONE_6_0
);
166 @function CNEncoderGetOutputLengthFromEncoding
167 @abstract Determine the size required to hold the result of processing the
168 input given an encoding constant and direction and length.
170 @param encoding selects one of the base encodings above.
171 @param direction Designate the direction (encode or decode) for this
174 @param inLen The number of bytes to be processed.
176 @result The length required for the encoding. Zero (0) will be returned in
177 the result of a NULL input pointer for the "in" parameter.
181 CNEncoderGetOutputLengthFromEncoding(CNEncodings encoding
,
182 CNEncodingDirection direction
,
184 __OSX_AVAILABLE_STARTING(__MAC_10_9
, __IPHONE_6_0
);
187 @function CNEncoderUpdate
188 @abstract Encode or decode the input string to or from the designated
191 @param coderRef A CNEncoderRef obtained through CNEncoderCreate()
192 or CNEncoderCreateCustom().
194 @param in The bytes to be processed.
196 @param inLen The number of bytes to be processed.
198 @param out The destination of the processed data.
200 @param outLen The length of the processed data.
202 @result kCCSuccess or one of kCCParamErr, kCCMemoryFailure.
206 CNEncoderUpdate(CNEncoderRef coderRef
, const void *in
, const size_t inLen
, void *out
,
208 __OSX_AVAILABLE_STARTING(__MAC_10_9
, __IPHONE_6_0
);
212 @function CNEncoderFinal
213 @abstract Complete coding for all available inputs, padding where necessary.
215 @param coderRef A CNEncoderRef obtained through CNEncoderCreate()
216 or CNEncoderCreateCustom().
218 @param out The destination of the processed data.
220 @param outLen The length of the processed data.
222 @result kCCSuccess or one of kCCParamErr, kCCMemoryFailure.
226 CNEncoderFinal(CNEncoderRef coderRef
, void *out
, size_t *outLen
)
227 __OSX_AVAILABLE_STARTING(__MAC_10_9
, __IPHONE_6_0
);
231 @function CNEncoderBlocksize
232 @abstract Get the number of bytes per block of input (base256) to output (base of encoder).
234 @param encoding The encoding format.
236 @param inputSize The number of raw bytes upon which an encoding operation should be performed
237 if padding should not be added. If CNEncode is called with this many bytes
238 the resulting coded bytes will not need padding.
239 @param outputSize The output block size in bytes for this encoding.
241 @result kCCSuccess or kCCParamErr.
247 CNEncoderBlocksize(CNEncodings encoding
, size_t *inputSize
, size_t *outputSize
)
248 __OSX_AVAILABLE_STARTING(__MAC_10_9
, __IPHONE_6_0
);
251 @function CNEncoderBlocksizeFromRef
252 @abstract Get the number of bytes per block of input (base256) to output (base of encoder).
254 @param encoderRef A CNEncoderRef gotten from CNEncoderCreate or CNEncoderCreateCustom.
256 @param inputSize The number of raw bytes upon which an encoding operation should be performed
257 if padding should not be added. If CNEncode is called with this many bytes
258 the resulting coded bytes will not need padding.
259 @param outputSize The output block size in bytes for this encoding.
261 @result kCCSuccess or kCCParamErr.
265 CNEncoderBlocksizeFromRef(CNEncoderRef encoderRef
, size_t *inputSize
, size_t *outputSize
)
266 __OSX_AVAILABLE_STARTING(__MAC_10_9
, __IPHONE_6_0
);
273 #endif /* COMMON_BASE_XX_H */