1/* Copyright 2015 OpenMarket Ltd
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15#include <cassert>
16
17#include "olm/base64.h"
18#include "olm/base64.hh"
19
20namespace {
21
22static const std::uint8_t ENCODE_BASE64[64] = {
23 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
24 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F, 0x50,
25 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
26 0x59, 0x5A, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
27 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E,
28 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76,
29 0x77, 0x78, 0x79, 0x7A, 0x30, 0x31, 0x32, 0x33,
30 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x2B, 0x2F,
31};
32
33static const std::uint8_t E = -1;
34
35static const std::uint8_t DECODE_BASE64[128] = {
36/* 0x0 0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xA 0xB 0xC 0xD 0xE 0xF */
37 E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E,
38 E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E,
39 E, E, E, E, E, E, E, E, E, E, E, 62, E, E, E, 63,
40 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, E, E, E, E, E, E,
41 E, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
42 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, E, E, E, E, E,
43 E, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
44 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, E, E, E, E, E,
45};
46
47} // namespace
48
49
50std::size_t olm::encode_base64_length(
51 std::size_t input_length
52) {
53 return 4 * ((input_length + 2) / 3) + (input_length + 2) % 3 - 2;
54}
55
56std::uint8_t * olm::encode_base64(
57 std::uint8_t const * input, std::size_t input_length,
58 std::uint8_t * output
59) {
60 std::uint8_t const * end = input + (input_length / 3) * 3;
61 std::uint8_t const * pos = input;
62 while (pos != end) {
63 unsigned value = pos[0];
64 value <<= 8; value |= pos[1];
65 value <<= 8; value |= pos[2];
66 pos += 3;
67 output[3] = ENCODE_BASE64[value & 0x3F];
68 value >>= 6; output[2] = ENCODE_BASE64[value & 0x3F];
69 value >>= 6; output[1] = ENCODE_BASE64[value & 0x3F];
70 value >>= 6; output[0] = ENCODE_BASE64[value];
71 output += 4;
72 }
73 unsigned remainder = input + input_length - pos;
74 std::uint8_t * result = output;
75 if (remainder) {
76 unsigned value = pos[0];
77 if (remainder == 2) {
78 value <<= 8; value |= pos[1];
79 value <<= 2;
80 output[2] = ENCODE_BASE64[value & 0x3F];
81 value >>= 6;
82 result += 3;
83 } else {
84 value <<= 4;
85 result += 2;
86 }
87 output[1] = ENCODE_BASE64[value & 0x3F];
88 value >>= 6;
89 output[0] = ENCODE_BASE64[value];
90 }
91 return result;
92}
93
94
95std::size_t olm::decode_base64_length(
96 std::size_t input_length
97) {
98 if (input_length % 4 == 1) {
99 return std::size_t(-1);
100 } else {
101 return 3 * ((input_length + 2) / 4) + (input_length + 2) % 4 - 2;
102 }
103}
104
105
106std::size_t olm::decode_base64(
107 std::uint8_t const * input, std::size_t input_length,
108 std::uint8_t * output
109) {
110 size_t raw_length = olm::decode_base64_length(input_length);
111
112 if (raw_length == std::size_t(-1)) {
113 return std::size_t(-1);
114 }
115
116 std::uint8_t const * end = input + (input_length / 4) * 4;
117 std::uint8_t const * pos = input;
118
119 while (pos != end) {
120 unsigned value = DECODE_BASE64[pos[0] & 0x7F];
121 value <<= 6; value |= DECODE_BASE64[pos[1] & 0x7F];
122 value <<= 6; value |= DECODE_BASE64[pos[2] & 0x7F];
123 value <<= 6; value |= DECODE_BASE64[pos[3] & 0x7F];
124 pos += 4;
125 output[2] = value;
126 value >>= 8; output[1] = value;
127 value >>= 8; output[0] = value;
128 output += 3;
129 }
130
131 unsigned remainder = input + input_length - pos;
132 if (remainder) {
133 /* A base64 payload with a single byte remainder cannot occur because
134 * a single base64 character only encodes 6 bits, which is less than
135 * a full byte. Therefore, a minimum of two base64 characters are
136 * required to construct a single output byte and payloads with
137 * a remainder of 1 are illegal.
138 *
139 * Should never be the case due to length check above.
140 */
141 assert(remainder != 1);
142
143 unsigned value = DECODE_BASE64[pos[0] & 0x7F];
144 value <<= 6; value |= DECODE_BASE64[pos[1] & 0x7F];
145 if (remainder == 3) {
146 value <<= 6; value |= DECODE_BASE64[pos[2] & 0x7F];
147 value >>= 2;
148 output[1] = value;
149 value >>= 8;
150 } else {
151 value >>= 4;
152 }
153 output[0] = value;
154 }
155
156 return raw_length;
157}
158
159
160// implementations of base64.h
161
162size_t _olm_encode_base64_length(
163 size_t input_length
164) {
165 return olm::encode_base64_length(input_length);
166}
167
168size_t _olm_encode_base64(
169 uint8_t const * input, size_t input_length,
170 uint8_t * output
171) {
172 uint8_t * r = olm::encode_base64(input, input_length, output);
173 return r - output;
174}
175
176size_t _olm_decode_base64_length(
177 size_t input_length
178) {
179 return olm::decode_base64_length(input_length);
180}
181
182size_t _olm_decode_base64(
183 uint8_t const * input, size_t input_length,
184 uint8_t * output
185) {
186 return olm::decode_base64(input, input_length, output);
187}
188