Index: compiled/Base64.cpp |
=================================================================== |
new file mode 100644 |
--- /dev/null |
+++ b/compiled/Base64.cpp |
@@ -0,0 +1,60 @@ |
+/* |
+ * This file is part of Adblock Plus <https://adblockplus.org/>, |
+ * Copyright (C) 2006-present eyeo GmbH |
+ * |
+ * Adblock Plus is free software: you can redistribute it and/or modify |
+ * it under the terms of the GNU General Public License version 3 as |
+ * published by the Free Software Foundation. |
+ * |
+ * Adblock Plus is distributed in the hope that it will be useful, |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
+ * GNU General Public License for more details. |
+ * |
+ * You should have received a copy of the GNU General Public License |
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
+ */ |
+ |
+#include "Base64.h" |
+ |
+static const char base64_alphabet[] = { |
+ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', |
+ 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', |
+ 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', |
+ 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', |
+ 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', |
+ 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', |
+ 'w', 'x', 'y', 'z', '0', '1', '2', '3', |
+ '4', '5', '6', '7', '8', '9', '+', '/' |
+}; |
+ |
+// Note: this base64 encoding fill with 0 instead of using padding. |
+// ie it is not conformant. |
+OwnedString ToBase64(const uint8_t* buffer, size_t len) |
+{ |
+ String::size_type length = (len / 3 * 4) + (len % 3 ? 1 : 0); |
+ OwnedString output(length); |
+ String::size_type outputIndex = 0; |
+ uint8_t input[3]; |
+ for (size_t i = 0; i < len; i += 3) |
+ { |
+ size_t left = len - i; |
+ if (left > 3) |
+ left = 3; |
+ for (size_t j = 0; j < left; j++) |
+ input[j] = *buffer++; |
+ switch (left) { |
+ case 1: |
+ input[1] = 0; |
+ case 2: |
+ input[2] = 0; |
+ } |
+ |
+ output[outputIndex++] = base64_alphabet[input[0] >> 2]; |
+ output[outputIndex++] = base64_alphabet[((input[0] & 0x03) << 4) + (input[1] >> 4)]; |
+ output[outputIndex++] = base64_alphabet[((input[1] & 0x0f) << 2) + (input[2] >> 6)]; |
+ output[outputIndex++] = base64_alphabet[input[2] & 0x3f]; |
+ } |
+ |
+ return output; |
+} |