Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //https://ru.wikipedia.org/wiki/Base64
- #include <iostream>
- #include <memory>
- #include <array>
- struct release_alloc { void operator()(void* p) const { free(p); } };
- std::unique_ptr<char, release_alloc> toB64(const void* mem, size_t len);
- std::unique_ptr<char, release_alloc> fromB64(const char* b64);
- int main()
- {
- std::string str = "Test string encoded to base64";
- auto str64 = toB64(str.data(), str.length());
- std::cout << str64.get() << std::endl << std::endl;
- std::cout << fromB64(str64.get()).get() << std::endl << std::endl;
- system("pause");
- return 0;
- }
- std::array<char, 64> _B64 = { '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','+','/' };
- std::unique_ptr<char, release_alloc> toB64(const void* mem, size_t len)
- {
- std::unique_ptr<char, release_alloc> result((char*)malloc(len / 3 * 4 + (len % 3 ? 5 : 1)));
- const char* source = (char*)mem;
- char* dest = result.get();
- char buf[3];
- char index = 0;
- for (; len > 0; --len)
- {
- buf[index++] = *(source++);
- if (index == 3)
- {
- *(dest++) = _B64[(buf[0] & 0xfc) >> 2];
- *(dest++) = _B64[((buf[0] & 0x03) << 4) + ((buf[1] & 0xf0) >> 4)];
- *(dest++) = _B64[((buf[1] & 0x0f) << 2) + ((buf[2] & 0xc0) >> 6)];
- *(dest++) = _B64[buf[2] & 0x3f];
- index = 0;
- }
- }
- if (index)
- {
- *(dest++) = _B64[(buf[0] & 0xfc) >> 2];
- if (index == 1)
- {
- *(dest++) = _B64[((buf[0] & 0x03) << 4)];
- *(dest++) = '='; *(dest++) = '=';
- }
- else
- {
- *(dest++) = _B64[((buf[0] & 0x03) << 4) + ((buf[1] & 0xf0) >> 4)];
- *(dest++) = _B64[((buf[1] & 0x0f) << 2)];
- *(dest++) = '=';
- }
- }
- *(dest++) = 0;
- return result;
- }
- std::array<unsigned char, 123> _R64 =
- { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,62,0,0,0,63,52,53,54,55,56,57,58,59,60,61,0,0,0,0,0,0,0,0,
- 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,0,0,0,
- 0,0,0,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51 };
- std::unique_ptr<char, release_alloc> fromB64(const char* b64)
- {
- size_t len = strlen(b64);
- std::unique_ptr<char, release_alloc> result((char*)malloc(len / 4 * 3));
- unsigned char* dest = (unsigned char*)result.get();
- char buf[4];
- char index = 0;
- while (*b64 && *b64 != '=')
- {
- buf[index++] = *(b64++);
- if (index == 4)
- {
- int pos1 = _R64[buf[0]], pos2 = _R64[buf[1]];
- *(dest++) = (_R64[buf[0]] << 2) | (_R64[buf[1]] >> 4);
- *(dest++) = (_R64[buf[1]] << 4) | (_R64[buf[2]] >> 2);
- *(dest++) = (_R64[buf[2]] << 6) | (_R64[buf[3]]);
- index = 0;
- }
- }
- if (index)
- {
- while (index != 4)
- buf[index++] = 0;
- *(dest++) = (_R64[buf[0]] << 2) | (_R64[buf[1]] >> 4);
- *(dest++) = (_R64[buf[1]] << 4) | (_R64[buf[2]] >> 2);
- *(dest++) = (_R64[buf[2]] << 6) | (_R64[buf[3]]);
- }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment