From 2cf4ef05671a4ad1016d476fe1cff629c0818b00 Mon Sep 17 00:00:00 2001 From: Ming Date: Fri, 16 Jul 2010 07:07:38 +0000 Subject: [PATCH] issue #11: add base64.cpp and base64.h --- cocos2dx/support/base64.cpp | 108 ++++++++++++++++++++++++++++++++++++ cocos2dx/support/base64.h | 53 ++++++++++++++++++ 2 files changed, 161 insertions(+) create mode 100644 cocos2dx/support/base64.cpp create mode 100644 cocos2dx/support/base64.h diff --git a/cocos2dx/support/base64.cpp b/cocos2dx/support/base64.cpp new file mode 100644 index 0000000000..6cdef60cd4 --- /dev/null +++ b/cocos2dx/support/base64.cpp @@ -0,0 +1,108 @@ +/**************************************************************************** +Copyright (c) 2010 cocos2d-x.org + +http://www.cocos2d-x.org + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +****************************************************************************/ + +#include +#include +#include "base64.h" + +unsigned char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + +INT32 _base64Decode( unsigned char *input, UINT32 input_len, unsigned char *output, UINT32 *output_len ) +{ + static char inalphabet[256], decoder[256]; + INT32 i, bits, c, char_count, errors = 0; + UINT32 input_idx = 0; + UINT32 output_idx = 0; + + for (i = (sizeof alphabet) - 1; i >= 0 ; i--) { + inalphabet[alphabet[i]] = 1; + decoder[alphabet[i]] = i; + } + + char_count = 0; + bits = 0; + for( input_idx=0; input_idx < input_len ; input_idx++ ) { + c = input[ input_idx ]; + if (c == '=') + break; + if (c > 255 || ! inalphabet[c]) + continue; + bits += decoder[c]; + char_count++; + if (char_count == 4) { + output[ output_idx++ ] = (bits >> 16); + output[ output_idx++ ] = ((bits >> 8) & 0xff); + output[ output_idx++ ] = ( bits & 0xff); + bits = 0; + char_count = 0; + } else { + bits <<= 6; + } + } + + if( c == '=' ) { + switch (char_count) { + case 1: + fprintf(stderr, "base64Decode: encoding incomplete: at least 2 bits missing"); + errors++; + break; + case 2: + output[ output_idx++ ] = ( bits >> 10 ); + break; + case 3: + output[ output_idx++ ] = ( bits >> 16 ); + output[ output_idx++ ] = (( bits >> 8 ) & 0xff); + break; + } + } else if ( input_idx < input_len ) { + if (char_count) { + fprintf(stderr, "base64 encoding incomplete: at least %d bits truncated", + ((4 - char_count) * 6)); + errors++; + } + } + + *output_len = output_idx; + return errors; +} + +INT32 base64Decode(unsigned char *in, UINT32 inLength, unsigned char **out) +{ + UINT32 outLength = 0; + + //should be enough to store 6-bit buffers in 8-bit buffers + *out = (unsigned char *)malloc( (size_t)(inLength * 3.0f / 4.0f + 1)); + if( *out ) { + INT32 ret = _base64Decode(in, inLength, *out, &outLength); + + if (ret > 0 ) + { + printf("Base64Utils: error decoding"); + free(*out); + *out = NULL; + outLength = 0; + } + } + return outLength; +} diff --git a/cocos2dx/support/base64.h b/cocos2dx/support/base64.h new file mode 100644 index 0000000000..ce5df5e58c --- /dev/null +++ b/cocos2dx/support/base64.h @@ -0,0 +1,53 @@ +/**************************************************************************** +Copyright (c) 2010 cocos2d-x.org + +http://www.cocos2d-x.org + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +****************************************************************************/ + +#ifndef __SUPPORT_BASE64_H__ +#define __SUPPORT_BASE64_H__ + +#include "platform/platform.h" + +#ifdef __cplusplus +extern "C" { +#endif + + +/** @file + base64 helper functions + */ + +/** + * Decodes a 64base encoded memory. The decoded memory is + * expected to be freed by the caller. + * + * @returns the length of the out buffer + * + @since v0.8.1 + */ +INT32 base64Decode(unsigned char *in, unsigned int inLength, unsigned char **out); + +#ifdef __cplusplus +} +#endif + +#endif // __SUPPORT_BASE64_H__