David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 19 | |
Roger Meier | 4285ba2 | 2013-06-10 21:17:23 +0200 | [diff] [blame] | 20 | #include <thrift/protocol/TBase64Utils.h> |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 21 | |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 22 | using std::string; |
| 23 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 24 | namespace apache { |
| 25 | namespace thrift { |
| 26 | namespace protocol { |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 27 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 28 | static const uint8_t* kBase64EncodeTable |
| 29 | = (const uint8_t*)"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 30 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 31 | void base64_encode(const uint8_t* in, uint32_t len, uint8_t* buf) { |
Roger Meier | 4cb8e40 | 2012-05-27 18:05:16 +0000 | [diff] [blame] | 32 | buf[0] = kBase64EncodeTable[(in[0] >> 2) & 0x3f]; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 33 | if (len == 3) { |
Roger Meier | 4cb8e40 | 2012-05-27 18:05:16 +0000 | [diff] [blame] | 34 | buf[1] = kBase64EncodeTable[((in[0] << 4) & 0x30) | ((in[1] >> 4) & 0x0f)]; |
| 35 | buf[2] = kBase64EncodeTable[((in[1] << 2) & 0x3c) | ((in[2] >> 6) & 0x03)]; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 36 | buf[3] = kBase64EncodeTable[in[2] & 0x3f]; |
| 37 | } else if (len == 2) { |
Roger Meier | 4cb8e40 | 2012-05-27 18:05:16 +0000 | [diff] [blame] | 38 | buf[1] = kBase64EncodeTable[((in[0] << 4) & 0x30) | ((in[1] >> 4) & 0x0f)]; |
| 39 | buf[2] = kBase64EncodeTable[(in[1] << 2) & 0x3c]; |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 40 | } else { // len == 1 |
Roger Meier | 4cb8e40 | 2012-05-27 18:05:16 +0000 | [diff] [blame] | 41 | buf[1] = kBase64EncodeTable[(in[0] << 4) & 0x30]; |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 42 | } |
| 43 | } |
| 44 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 45 | static const uint8_t kBase64DecodeTable[256] = { |
| 46 | 0xff, |
| 47 | 0xff, |
| 48 | 0xff, |
| 49 | 0xff, |
| 50 | 0xff, |
| 51 | 0xff, |
| 52 | 0xff, |
| 53 | 0xff, |
| 54 | 0xff, |
| 55 | 0xff, |
| 56 | 0xff, |
| 57 | 0xff, |
| 58 | 0xff, |
| 59 | 0xff, |
| 60 | 0xff, |
| 61 | 0xff, |
| 62 | 0xff, |
| 63 | 0xff, |
| 64 | 0xff, |
| 65 | 0xff, |
| 66 | 0xff, |
| 67 | 0xff, |
| 68 | 0xff, |
| 69 | 0xff, |
| 70 | 0xff, |
| 71 | 0xff, |
| 72 | 0xff, |
| 73 | 0xff, |
| 74 | 0xff, |
| 75 | 0xff, |
| 76 | 0xff, |
| 77 | 0xff, |
| 78 | 0xff, |
| 79 | 0xff, |
| 80 | 0xff, |
| 81 | 0xff, |
| 82 | 0xff, |
| 83 | 0xff, |
| 84 | 0xff, |
| 85 | 0xff, |
| 86 | 0xff, |
| 87 | 0xff, |
| 88 | 0xff, |
| 89 | 0x3e, |
| 90 | 0xff, |
| 91 | 0xff, |
| 92 | 0xff, |
| 93 | 0x3f, |
| 94 | 0x34, |
| 95 | 0x35, |
| 96 | 0x36, |
| 97 | 0x37, |
| 98 | 0x38, |
| 99 | 0x39, |
| 100 | 0x3a, |
| 101 | 0x3b, |
| 102 | 0x3c, |
| 103 | 0x3d, |
| 104 | 0xff, |
| 105 | 0xff, |
| 106 | 0xff, |
| 107 | 0xff, |
| 108 | 0xff, |
| 109 | 0xff, |
| 110 | 0xff, |
| 111 | 0x00, |
| 112 | 0x01, |
| 113 | 0x02, |
| 114 | 0x03, |
| 115 | 0x04, |
| 116 | 0x05, |
| 117 | 0x06, |
| 118 | 0x07, |
| 119 | 0x08, |
| 120 | 0x09, |
| 121 | 0x0a, |
| 122 | 0x0b, |
| 123 | 0x0c, |
| 124 | 0x0d, |
| 125 | 0x0e, |
| 126 | 0x0f, |
| 127 | 0x10, |
| 128 | 0x11, |
| 129 | 0x12, |
| 130 | 0x13, |
| 131 | 0x14, |
| 132 | 0x15, |
| 133 | 0x16, |
| 134 | 0x17, |
| 135 | 0x18, |
| 136 | 0x19, |
| 137 | 0xff, |
| 138 | 0xff, |
| 139 | 0xff, |
| 140 | 0xff, |
| 141 | 0xff, |
| 142 | 0xff, |
| 143 | 0x1a, |
| 144 | 0x1b, |
| 145 | 0x1c, |
| 146 | 0x1d, |
| 147 | 0x1e, |
| 148 | 0x1f, |
| 149 | 0x20, |
| 150 | 0x21, |
| 151 | 0x22, |
| 152 | 0x23, |
| 153 | 0x24, |
| 154 | 0x25, |
| 155 | 0x26, |
| 156 | 0x27, |
| 157 | 0x28, |
| 158 | 0x29, |
| 159 | 0x2a, |
| 160 | 0x2b, |
| 161 | 0x2c, |
| 162 | 0x2d, |
| 163 | 0x2e, |
| 164 | 0x2f, |
| 165 | 0x30, |
| 166 | 0x31, |
| 167 | 0x32, |
| 168 | 0x33, |
| 169 | 0xff, |
| 170 | 0xff, |
| 171 | 0xff, |
| 172 | 0xff, |
| 173 | 0xff, |
| 174 | 0xff, |
| 175 | 0xff, |
| 176 | 0xff, |
| 177 | 0xff, |
| 178 | 0xff, |
| 179 | 0xff, |
| 180 | 0xff, |
| 181 | 0xff, |
| 182 | 0xff, |
| 183 | 0xff, |
| 184 | 0xff, |
| 185 | 0xff, |
| 186 | 0xff, |
| 187 | 0xff, |
| 188 | 0xff, |
| 189 | 0xff, |
| 190 | 0xff, |
| 191 | 0xff, |
| 192 | 0xff, |
| 193 | 0xff, |
| 194 | 0xff, |
| 195 | 0xff, |
| 196 | 0xff, |
| 197 | 0xff, |
| 198 | 0xff, |
| 199 | 0xff, |
| 200 | 0xff, |
| 201 | 0xff, |
| 202 | 0xff, |
| 203 | 0xff, |
| 204 | 0xff, |
| 205 | 0xff, |
| 206 | 0xff, |
| 207 | 0xff, |
| 208 | 0xff, |
| 209 | 0xff, |
| 210 | 0xff, |
| 211 | 0xff, |
| 212 | 0xff, |
| 213 | 0xff, |
| 214 | 0xff, |
| 215 | 0xff, |
| 216 | 0xff, |
| 217 | 0xff, |
| 218 | 0xff, |
| 219 | 0xff, |
| 220 | 0xff, |
| 221 | 0xff, |
| 222 | 0xff, |
| 223 | 0xff, |
| 224 | 0xff, |
| 225 | 0xff, |
| 226 | 0xff, |
| 227 | 0xff, |
| 228 | 0xff, |
| 229 | 0xff, |
| 230 | 0xff, |
| 231 | 0xff, |
| 232 | 0xff, |
| 233 | 0xff, |
| 234 | 0xff, |
| 235 | 0xff, |
| 236 | 0xff, |
| 237 | 0xff, |
| 238 | 0xff, |
| 239 | 0xff, |
| 240 | 0xff, |
| 241 | 0xff, |
| 242 | 0xff, |
| 243 | 0xff, |
| 244 | 0xff, |
| 245 | 0xff, |
| 246 | 0xff, |
| 247 | 0xff, |
| 248 | 0xff, |
| 249 | 0xff, |
| 250 | 0xff, |
| 251 | 0xff, |
| 252 | 0xff, |
| 253 | 0xff, |
| 254 | 0xff, |
| 255 | 0xff, |
| 256 | 0xff, |
| 257 | 0xff, |
| 258 | 0xff, |
| 259 | 0xff, |
| 260 | 0xff, |
| 261 | 0xff, |
| 262 | 0xff, |
| 263 | 0xff, |
| 264 | 0xff, |
| 265 | 0xff, |
| 266 | 0xff, |
| 267 | 0xff, |
| 268 | 0xff, |
| 269 | 0xff, |
| 270 | 0xff, |
| 271 | 0xff, |
| 272 | 0xff, |
| 273 | 0xff, |
| 274 | 0xff, |
| 275 | 0xff, |
| 276 | 0xff, |
| 277 | 0xff, |
| 278 | 0xff, |
| 279 | 0xff, |
| 280 | 0xff, |
| 281 | 0xff, |
| 282 | 0xff, |
| 283 | 0xff, |
| 284 | 0xff, |
| 285 | 0xff, |
| 286 | 0xff, |
| 287 | 0xff, |
| 288 | 0xff, |
| 289 | 0xff, |
| 290 | 0xff, |
| 291 | 0xff, |
| 292 | 0xff, |
| 293 | 0xff, |
| 294 | 0xff, |
| 295 | 0xff, |
| 296 | 0xff, |
| 297 | 0xff, |
| 298 | 0xff, |
| 299 | 0xff, |
| 300 | 0xff, |
| 301 | 0xff, |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 302 | }; |
| 303 | |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 304 | void base64_decode(uint8_t* buf, uint32_t len) { |
| 305 | buf[0] = (kBase64DecodeTable[buf[0]] << 2) | (kBase64DecodeTable[buf[1]] >> 4); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 306 | if (len > 2) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 307 | buf[1] = ((kBase64DecodeTable[buf[1]] << 4) & 0xf0) | (kBase64DecodeTable[buf[2]] >> 2); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 308 | if (len > 3) { |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 309 | buf[2] = ((kBase64DecodeTable[buf[2]] << 6) & 0xc0) | (kBase64DecodeTable[buf[3]]); |
David Reiss | db0ea15 | 2008-02-18 01:49:37 +0000 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | } |
Konrad Grochowski | 16a23a6 | 2014-11-13 15:33:38 +0100 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | } // apache::thrift::protocol |