the game where you go into mines and start crafting! but for consoles (forked directly from smartcmd's github)
at main 156 lines 4.0 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::wstring base64_chars = 34 L"ABCDEFGHIJKLMNOPQRSTUVWXYZ" 35 L"abcdefghijklmnopqrstuvwxyz" 36 L"0123456789+/"; 37 38 39static inline bool is_base64(wchar_t c) 40{ 41 return (isalnum(c) || (c == L'+') || (c == L'/')); 42} 43 44// 4J changed to use Platform::String 45Platform::String^ base64_encode(unsigned char* chars_to_encode, unsigned int in_len) 46{ 47 std::wstring ret; 48 int i = 0; 49 int j = 0; 50 unsigned char char_array_3[3]; 51 unsigned char char_array_4[4]; 52 53 while (in_len--) 54 { 55 char_array_3[i++] = *(chars_to_encode++); 56 if (i == 3) 57 { 58 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; 59 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); 60 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); 61 char_array_4[3] = char_array_3[2] & 0x3f; 62 63 for(int ii = 0; (ii <4) ; ii++) 64 { 65 ret += base64_chars[char_array_4[ii]]; 66 } 67 i = 0; 68 } 69 } 70 71 if (i) 72 { 73 for(j = i; j < 3; j++) 74 { 75 char_array_3[j] = '\0'; 76 } 77 78 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2; 79 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4); 80 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6); 81 char_array_4[3] = char_array_3[2] & 0x3f; 82 83 for (j = 0; (j < i + 1); j++) 84 { 85 ret += base64_chars[char_array_4[j]]; 86 } 87 88 while((i++ < 3)) 89 { 90 ret += L'='; 91 } 92 93 } 94 95 return ref new Platform::String(ret.c_str()); 96 97} 98 99void base64_decode(Platform::String ^encoded_string, unsigned char *output, unsigned int out_len) 100{ 101 int in_len = encoded_string->Length(); 102 int i = 0; 103 int j = 0; 104 int in_ = 0; 105 unsigned char char_array_4[4]; 106 unsigned char char_array_3[3]; 107 108 unsigned char *pucOut = output; 109 110 while (in_len-- && ( encoded_string->Data()[in_] != L'=') && is_base64(encoded_string->Data()[in_])) 111 { 112 char_array_4[i++] = (unsigned char)(encoded_string->Data()[in_]); 113 in_++; 114 if (i ==4) 115 { 116 for (i = 0; i <4; i++) 117 { 118 char_array_4[i] = (unsigned char )base64_chars.find(char_array_4[i]); 119 } 120 121 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); 122 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); 123 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; 124 125 for (i = 0; (i < 3); i++) 126 { 127 *pucOut++ = char_array_3[i]; 128 if( ( pucOut - output ) >= out_len ) return; 129 } 130 i = 0; 131 } 132 } 133 134 if(i) 135 { 136 for (j = i; j <4; j++) 137 { 138 char_array_4[j] = 0; 139 } 140 141 for (j = 0; j <4; j++) 142 { 143 char_array_4[j] = (unsigned char )base64_chars.find(char_array_4[j]); 144 } 145 146 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); 147 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); 148 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; 149 150 for (j = 0; (j < i - 1); j++) 151 { 152 *pucOut++ = char_array_3[j]; 153 if( ( pucOut - output ) >= out_len ) return; 154 } 155 } 156}