Move the HTML5 remote desktop client to src/tools.
[haiku.git] / src / servers / app / drawing / interface / html5 / base64.cpp
blob8081559bd7a176fe5df78ac2dbfd8f44be5eb4b9
1 /*
2 * Copyright 2011, Haiku, Inc. All rights reserved.
3 * Copyright 2001-2003 Dr. Zoidberg Enterprises. All rights reserved.
4 */
7 #include <ctype.h>
8 #include <string.h>
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',
19 '+',
20 '/'
23 static const char kHexAlphabet[16] = {'0', '1', '2', '3', '4', '5', '6', '7',
24 '8','9','A','B','C','D','E','F'};
27 ssize_t
28 encode_base64(char *out, const char *in, off_t length, bool packed)
30 uint32 concat;
31 int i = 0;
32 int k = 0;
34 while (i < length) {
35 concat = ((in[i] & 0xff) << 16);
37 if ((i+1) < length)
38 concat |= ((in[i+1] & 0xff) << 8);
39 if ((i+2) < length)
40 concat |= (in[i+2] & 0xff);
42 i += 3;
44 out[k++] = kBase64Alphabet[(concat >> 18) & 63];
45 out[k++] = kBase64Alphabet[(concat >> 12) & 63];
46 if ((i+1) < length)
47 out[k++] = kBase64Alphabet[(concat >> 6) & 63];
48 else if (!packed)
49 out[k++] = '=';
50 if ((i+2) < length)
51 out[k++] = kBase64Alphabet[concat & 63];
52 else if (!packed)
53 out[k++] = '=';
56 return k;
60 ssize_t
61 decode_base64(char *out, const char *in, off_t length)
63 uint32 concat, value;
64 int lastOutLine = 0;
65 int i, j;
66 int outIndex = 0;
68 for (i = 0; i < length; i += 4) {
69 concat = 0;
71 for (j = 0; j < 4 && (i + j) < length; j++) {
72 value = in[i + j];
74 if (value == '\n' || value == '\r') {
75 // jump over line breaks
76 lastOutLine = outIndex;
77 i++;
78 j--;
79 continue;
82 if ((value >= 'A') && (value <= 'Z'))
83 value -= 'A';
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 == '+')
89 value = 62;
90 else if (value == '/')
91 value = 63;
92 else if (value == '=')
93 break;
94 else {
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')
99 i++;
100 concat = 0;
103 value = value << ((3-j)*6);
105 concat |= value;
108 if (j > 1)
109 out[outIndex++] = (concat & 0x00ff0000) >> 16;
110 if (j > 2)
111 out[outIndex++] = (concat & 0x0000ff00) >> 8;
112 if (j > 3)
113 out[outIndex++] = (concat & 0x000000ff);
116 return outIndex;