Index: experimental/fiddle/base64.h |
diff --git a/experimental/fiddle/base64.h b/experimental/fiddle/base64.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..b007c66164c9ad3d2a2c183f000f39140566275f |
--- /dev/null |
+++ b/experimental/fiddle/base64.h |
@@ -0,0 +1,44 @@ |
+/* |
+ * Copyright 2015 Google Inc. |
+ * |
+ * Use of this source code is governed by a BSD-style license that can be |
+ * found in the LICENSE file. |
+ */ |
+#ifndef base64_DEFINED |
+#define base64_DEFINED |
+ |
+#include <stdio.h> |
+ |
+inline void EncodeToBase64(const void* data, size_t size, FILE* out) { |
mtklein
2015/09/17 13:58:22
Ditto. Why is this not in fiddle_main?
|
+ const uint8_t* input = reinterpret_cast<const uint8_t*>(data); |
+ const uint8_t* end = &input[size]; |
+ static const char codes[] = |
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
+ "abcdefghijklmnopqrstuvwxyz0123456789+/"; |
+ while (input != end) { |
+ uint8_t b = (*input & 0xFC) >> 2; |
+ fputc(codes[b], out); |
+ b = (*input & 0x03) << 4; |
+ ++input; |
+ if (input == end) { |
+ fputc(codes[b], out); |
+ fputs("==", out); |
+ return; |
+ } |
+ b |= (*input & 0xF0) >> 4; |
+ fputc(codes[b], out); |
+ b = (*input & 0x0F) << 2; |
+ ++input; |
+ if (input == end) { |
+ fputc(codes[b], out); |
+ fputc('=', out); |
+ return; |
+ } |
+ b |= (*input & 0xC0) >> 6; |
+ fputc(codes[b], out); |
+ b = *input & 0x3F; |
+ fputc(codes[b], out); |
+ ++input; |
+ } |
+} |
+#endif // base64_DEFINED |