| 55 | } |
| 56 | |
| 57 | void encode_base64(const char* input, size_t n, std::string& output) |
| 58 | { |
| 59 | const size_t size = 4 * int(std::ceil(n / 3.0)); // Estimate encoded string size |
| 60 | output.resize(size); |
| 61 | |
| 62 | auto outIt = output.begin(); |
| 63 | auto outEnd = output.end(); |
| 64 | uint8_t next = 0; |
| 65 | size_t i = 0; |
| 66 | size_t j = 0; |
| 67 | for (; i < n; ++i, ++input) { |
| 68 | auto inputValue = *input; |
| 69 | switch (j) { |
| 70 | case 0: |
| 71 | *outIt = base64Char((inputValue & 0b11111100) >> 2); |
| 72 | ++outIt; |
| 73 | next |= (inputValue & 0b00000011) << 4; |
| 74 | ++j; |
| 75 | break; |
| 76 | case 1: |
| 77 | *outIt = base64Char(((inputValue & 0b11110000) >> 4) | next); |
| 78 | ++outIt; |
| 79 | next = (inputValue & 0b00001111) << 2; |
| 80 | ++j; |
| 81 | break; |
| 82 | case 2: |
| 83 | *outIt = base64Char(((inputValue & 0b11000000) >> 6) | next); |
| 84 | ++outIt; |
| 85 | *outIt = base64Char(inputValue & 0b00111111); |
| 86 | ++outIt; |
| 87 | next = 0; |
| 88 | j = 0; |
| 89 | break; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | if (outIt != outEnd) { |
| 94 | *outIt = base64Char(next); |
| 95 | ++outIt; |
| 96 | for (; outIt != outEnd; ++outIt) |
| 97 | *outIt = '='; // Padding |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | void decode_base64(const char* input, size_t n, buffer& output) |
| 102 | { |
| 103 | size_t size = 3 * int(std::ceil(n / 4.0)); // Estimate decoded buffer size |
| 104 | output.resize(size); |
nothing calls this directly
no test coverage detected