OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-present eyeo GmbH |
| 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 14 * You should have received a copy of the GNU General Public License |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ |
| 17 |
| 18 #include "Base64.h" |
| 19 |
| 20 static const char base64_alphabet[] = { |
| 21 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', |
| 22 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', |
| 23 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', |
| 24 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', |
| 25 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', |
| 26 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', |
| 27 'w', 'x', 'y', 'z', '0', '1', '2', '3', |
| 28 '4', '5', '6', '7', '8', '9', '+', '/' |
| 29 }; |
| 30 |
| 31 // Note: this base64 encoding fill with 0 instead of using padding. |
| 32 // ie it is not conformant. |
| 33 OwnedString ToBase64(const uint8_t* buffer, size_t len) |
| 34 { |
| 35 String::size_type length = (len / 3 * 4) + (1 + len % 3); |
| 36 OwnedString output(length); |
| 37 String::size_type outputIndex = 0; |
| 38 uint8_t input[3]; |
| 39 for (size_t i = 0; i < len; i += 3) |
| 40 { |
| 41 size_t left = len - i; |
| 42 if (left > 3) |
| 43 left = 3; |
| 44 for (size_t j = 0; j < left; j++) |
| 45 input[j] = *buffer++; |
| 46 switch (left) { |
| 47 case 1: |
| 48 input[1] = 0; |
| 49 case 2: |
| 50 input[2] = 0; |
| 51 } |
| 52 |
| 53 output[outputIndex++] = base64_alphabet[input[0] >> 2]; |
| 54 output[outputIndex++] = base64_alphabet[((input[0] & 0x03) << 4) + (input[1]
>> 4)]; |
| 55 if (left > 1) |
| 56 output[outputIndex++] = base64_alphabet[((input[1] & 0x0f) << 2) + (input[
2] >> 6)]; |
| 57 if (left > 2) |
| 58 output[outputIndex++] = base64_alphabet[input[2] & 0x3f]; |
| 59 } |
| 60 |
| 61 return output; |
| 62 } |
OLD | NEW |