the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
at main 131 lines 3.9 kB view raw
1/* 2 base64.cpp and base64.h 3 4 Copyright (C) 2004-2008 Ren� Nyffenegger 5 6 This source code is provided 'as-is', without any express or implied 7 warranty. In no event will the author be held liable for any damages 8 arising from the use of this software. 9 10 Permission is granted to anyone to use this software for any purpose, 11 including commercial applications, and to alter it and redistribute it 12 freely, subject to the following restrictions: 13 14 1. The origin of this source code must not be misrepresented; you must not 15 claim that you wrote the original source code. If you use this source code 16 in a product, an acknowledgment in the product documentation would be 17 appreciated but is not required. 18 19 2. Altered source versions must be plainly marked as such, and must not be 20 misrepresented as being the original source code. 21 22 3. This notice may not be removed or altered from any source distribution. 23 24 Ren� Nyffenegger rene.nyffenegger@adp-gmbh.ch 25 26*/ 27 28#include "stdafx.h" 29 30#include "base64.h" 31#include <iostream> 32 33static const std::string base64_chars = 34 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 35 "abcdefghijklmnopqrstuvwxyz" 36 "0123456789+/"; 37 38 39static inline bool is_base64(unsigned char c) { 40 return (isalnum(c) || (c == '+') || (c == '/')); 41} 42 43// 4J ADDED, 44std::string base64_encode(std::string str) 45{ 46 return base64_encode( reinterpret_cast<const unsigned char*>(str.c_str()), str.length() ); 47} 48 49std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) { 50 std::string ret; 51 int i = 0; 52 int j = 0; 53 unsigned char char_array_3[3]; 54 unsigned char char_array_4[4]; 55 56 while (in_len--) { 57 char_array_3[i++] = *(bytes_to_encode++); 58 if (i == 3) { 59 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; 60 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); 61 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); 62 char_array_4[3] = char_array_3[2] & 0x3f; 63 64 for(int ii = 0; (ii <4) ; ii++) 65 ret += base64_chars[char_array_4[ii]]; 66 i = 0; 67 } 68 } 69 70 if (i) 71 { 72 for(j = i; j < 3; j++) 73 char_array_3[j] = '\0'; 74 75 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; 76 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); 77 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); 78 char_array_4[3] = char_array_3[2] & 0x3f; 79 80 for (j = 0; (j < i + 1); j++) 81 ret += base64_chars[char_array_4[j]]; 82 83 while((i++ < 3)) 84 ret += '='; 85 86 } 87 88 return ret; 89 90} 91 92std::string base64_decode(std::string const& encoded_string) { 93 int in_len = encoded_string.size(); 94 int i = 0; 95 int j = 0; 96 int in_ = 0; 97 unsigned char char_array_4[4], char_array_3[3]; 98 std::string ret; 99 100 while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) { 101 char_array_4[i++] = encoded_string[in_]; in_++; 102 if (i ==4) { 103 for (i = 0; i <4; i++) 104 char_array_4[i] = base64_chars.find(char_array_4[i]); 105 106 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); 107 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); 108 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; 109 110 for (i = 0; (i < 3); i++) 111 ret += char_array_3[i]; 112 i = 0; 113 } 114 } 115 116 if (i) { 117 for (j = i; j <4; j++) 118 char_array_4[j] = 0; 119 120 for (j = 0; j <4; j++) 121 char_array_4[j] = base64_chars.find(char_array_4[j]); 122 123 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); 124 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); 125 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; 126 127 for (j = 0; (j < i - 1); j++) ret += char_array_3[j]; 128 } 129 130 return ret; 131}