1 /**
2 * Copyright (C) 2008 Happy Fish / YuQing
3 *
4 * FastDFS may be copied only under the terms of the GNU General
5 * Public License V3, which may be found in the FastDFS source kit.
6 * Please visit the FastDFS Home Page http://www.fastken.com/ for more detail.
7 **/
8 
9 //base64.h
10 
11 #ifndef _BASE64_H
12 #define _BASE64_H
13 
14 #include "common_define.h"
15 
16 struct base64_context
17 {
18 	char line_separator[16];
19 	int line_sep_len;
20 
21 	/**
22 	 * max chars per line, excluding line_separator.  A multiple of 4.
23 	 */
24 	int line_length;
25 
26 	/**
27 	 * letter of the alphabet used to encode binary values 0..63
28 	 */
29 	unsigned char valueToChar[64];
30 
31 	/**
32 	 * binary value encoded by a given letter of the alphabet 0..63
33 	 */
34 	int charToValue[256];
35 	int pad_ch;
36 };
37 
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
42 /** use stardand base64 charset
43  */
44 #define base64_init(context, nLineLength) \
45         base64_init_ex(context, nLineLength, '+', '/', '=')
46 
47 
48 /** stardand base64 encode
49  */
50 #define base64_encode(context, src, nSrcLen, dest, dest_len) \
51         base64_encode_ex(context, src, nSrcLen, dest, dest_len, true)
52 
53 /** base64 init function, before use base64 function, you should call
54  *  this function
55  *  parameters:
56  *           context:     the base64 context
57  *           nLineLength: length of a line, 0 for never add line seperator
58  *           chPlus:      specify a char for base64 char plus (+)
59  *           chSplash:    specify a char for base64 char splash (/)
60  *           chPad:       specify a char for base64 padding char =
61  *  return: none
62  */
63 void base64_init_ex(struct base64_context *context, const int nLineLength, \
64 		const unsigned char chPlus, const unsigned char chSplash, \
65 		const unsigned char chPad);
66 
67 /** calculate the encoded length of the source buffer
68  *  parameters:
69  *           context:     the base64 context
70  *           nSrcLen:     the source buffer length
71  *  return: the encoded length of the source buffer
72  */
73 int base64_get_encode_length(struct base64_context *context, const int nSrcLen);
74 
75 /** base64 encode buffer
76  *  parameters:
77  *           context:   the base64 context
78  *           src:       the source buffer
79  *           nSrcLen:   the source buffer length
80  *           dest:      the dest buffer
81  *           dest_len:  return dest buffer length
82  *           bPad:      if padding
83  *  return: the encoded buffer
84  */
85 char *base64_encode_ex(struct base64_context *context, const char *src, \
86 		const int nSrcLen, char *dest, int *dest_len, const bool bPad);
87 
88 /** base64 decode buffer, work only with padding source string
89  *  parameters:
90  *           context:   the base64 context
91  *           src:       the source buffer with padding
92  *           nSrcLen:   the source buffer length
93  *           dest:      the dest buffer
94  *           dest_len:  return dest buffer length
95  *  return: the decoded buffer
96  */
97 char *base64_decode(struct base64_context *context, const char *src, \
98 		const int nSrcLen, char *dest, int *dest_len);
99 
100 /** base64 decode buffer, can work with no padding source string
101  *  parameters:
102  *           context:   the base64 context
103  *           src:       the source buffer with padding or no padding
104  *           nSrcLen:   the source buffer length
105  *           dest:      the dest buffer
106  *           dest_len:  return dest buffer length
107  *  return: the decoded buffer
108  */
109 char *base64_decode_auto(struct base64_context *context, const char *src, \
110 		const int nSrcLen, char *dest, int *dest_len);
111 
112 /** set line separator string, such as \n or \r\n
113  *  parameters:
114  *           context:   the base64 context
115  *           pLineSeparator: the line separator string
116  *  return: none
117  */
118 void base64_set_line_separator(struct base64_context *context, \
119 		const char *pLineSeparator);
120 
121 /** set line length, 0 for never add line separators
122  *  parameters:
123  *           context:   the base64 context
124  *           length:    the line length
125  *  return: none
126  */
127 void base64_set_line_length(struct base64_context *context, const int length);
128 
129 #ifdef __cplusplus
130 }
131 #endif
132 
133 #endif
134 
135