2 * Copyright 2011, Haiku, Inc. All rights reserved.
3 * Copyright 2001-2003 Dr. Zoidberg Enterprises. All rights reserved.
10 #include <mail_encoding.h>
11 #include <SupportDefs.h>
13 static const char kBase64Alphabet
[64] = {
14 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
15 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
16 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
17 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
18 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
23 static const char kHexAlphabet
[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
24 '8','9','A','B','C','D','E','F'};
28 encode_base64(char *out
, const char *in
, off_t length
, bool packed
)
35 concat
= ((in
[i
] & 0xff) << 16);
38 concat
|= ((in
[i
+1] & 0xff) << 8);
40 concat
|= (in
[i
+2] & 0xff);
44 out
[k
++] = kBase64Alphabet
[(concat
>> 18) & 63];
45 out
[k
++] = kBase64Alphabet
[(concat
>> 12) & 63];
47 out
[k
++] = kBase64Alphabet
[(concat
>> 6) & 63];
51 out
[k
++] = kBase64Alphabet
[concat
& 63];
61 decode_base64(char *out
, const char *in
, off_t length
)
68 for (i
= 0; i
< length
; i
+= 4) {
71 for (j
= 0; j
< 4 && (i
+ j
) < length
; j
++) {
74 if (value
== '\n' || value
== '\r') {
75 // jump over line breaks
76 lastOutLine
= outIndex
;
82 if ((value
>= 'A') && (value
<= 'Z'))
84 else if ((value
>= 'a') && (value
<= 'z'))
85 value
= value
- 'a' + 26;
86 else if ((value
>= '0') && (value
<= '9'))
87 value
= value
- '0' + 52;
88 else if (value
== '+')
90 else if (value
== '/')
92 else if (value
== '=')
95 // there is an invalid character in this line - we will
96 // ignore the whole line and go to the next
97 outIndex
= lastOutLine
;
98 while (i
< length
&& in
[i
] != '\n' && in
[i
] != '\r')
103 value
= value
<< ((3-j
)*6);
109 out
[outIndex
++] = (concat
& 0x00ff0000) >> 16;
111 out
[outIndex
++] = (concat
& 0x0000ff00) >> 8;
113 out
[outIndex
++] = (concat
& 0x000000ff);