From: <yx...@us...> - 2010-12-14 17:07:46
|
Revision: 227 http://simspark.svn.sourceforge.net/simspark/?rev=227&view=rev Author: yxu Date: 2010-12-14 17:07:37 +0000 (Tue, 14 Dec 2010) Log Message: ----------- add libb64 Modified Paths: -------------- trunk/spark/utility/CMakeLists.txt Added Paths: ----------- trunk/spark/utility/libb64/ trunk/spark/utility/libb64/CMakeLists.txt trunk/spark/utility/libb64/README trunk/spark/utility/libb64/base64.cc trunk/spark/utility/libb64/cdecode.c trunk/spark/utility/libb64/cdecode.h trunk/spark/utility/libb64/cencode.c trunk/spark/utility/libb64/cencode.h trunk/spark/utility/libb64/decode.h trunk/spark/utility/libb64/encode.h Modified: trunk/spark/utility/CMakeLists.txt =================================================================== --- trunk/spark/utility/CMakeLists.txt 2010-12-14 17:07:14 UTC (rev 226) +++ trunk/spark/utility/CMakeLists.txt 2010-12-14 17:07:37 UTC (rev 227) @@ -4,3 +4,4 @@ add_subdirectory(rcssnet) add_subdirectory(sfsexp) add_subdirectory(tinyxml) +add_subdirectory(libb64) Added: trunk/spark/utility/libb64/CMakeLists.txt =================================================================== --- trunk/spark/utility/libb64/CMakeLists.txt (rev 0) +++ trunk/spark/utility/libb64/CMakeLists.txt 2010-12-14 17:07:37 UTC (rev 227) @@ -0,0 +1,19 @@ + +set(b64_SRCS + cdecode.c + cdecode.h + cencode.c + cencode.h + ) + +set(base64_SRCS + base64.cc + ) + +add_library(b64 STATIC ${b64_SRCS}) +if(UNIX) + set_target_properties( b64 PROPERTIES COMPILE_FLAGS -fPIC) +endif(UNIX) + +add_executable(base64 ${base64_SRCS}) +target_link_libraries(base64 b64) Added: trunk/spark/utility/libb64/README =================================================================== --- trunk/spark/utility/libb64/README (rev 0) +++ trunk/spark/utility/libb64/README 2010-12-14 17:07:37 UTC (rev 227) @@ -0,0 +1,33 @@ +libb64: Base64 Encoding/Decoding Routines +Modified for SimSpark (http://simspark.sourceforge.net) +====================================== + +Overview: +-------- +libb64 is a library of ANSI C routines for fast encoding/decoding data into and +from a base64-encoded format. +Please visit http://libb64.sourceforge.net/ for more information. + +Why modify? +---------------- +We use base64 to en/decode image data inside the program, +so we use the library without stream. + +License: +------- +This work is released under into the Public Domain, +and you can take it and do whatever you want with it. + +An example of this "license" is the Creative Commons Public Domain License, a +copy of which can be found in the LICENSE file, and also online at +http://creativecommons.org/licenses/publicdomain/ + +Authors: +-------- +This modified version: Xu Yuan xuy...@gm... http://airobot.org +Original Version: Chris Venter chr...@gm... http://rocketpod.blogspot.com + +References: +---------- +* Wikipedia article: + http://en.wikipedia.org/wiki/Base64 Added: trunk/spark/utility/libb64/base64.cc =================================================================== --- trunk/spark/utility/libb64/base64.cc (rev 0) +++ trunk/spark/utility/libb64/base64.cc 2010-12-14 17:07:37 UTC (rev 227) @@ -0,0 +1,94 @@ +/* +base64.cc - c++ source to a base64 reference encoder and decoder + +This is part of the libb64 project, and has been placed in the public domain. +For details, see http://sourceforge.net/projects/libb64 +*/ + +#include "encode.h" +#include "decode.h" + +#include <iostream> +#include <fstream> +#include <string> + +#include <stdlib.h> + +// Function which prints the usage of this executable +void usage() +{ + std::cerr<< \ + "base64: Encodes and Decodes files using base64\n" \ + "Usage: base64 [-e|-d] [input] [output]\n" \ + " Where [-e] will encode the input file into the output file,\n" \ + " [-d] will decode the input file into the output file, and\n" \ + " [input] and [output] are the input and output files, respectively.\n"; +} +// Function which prints the usage of this executable, plus a short message +void usage(const std::string& message) +{ + usage(); + std::cerr<<"Incorrect invocation of base64:\n"; + std::cerr<<message<<std::endl; +} + +int main(int argc, char** argv) +{ + // Quick check for valid arguments + if (argc == 1) + { + usage(); + exit(-1); + } + if (argc != 4) + { + usage("Wrong number of arguments!"); + exit(-1); + } + + // So far so good; try to open the input file + std::string input = argv[2]; + // Note that we have to open the input in binary mode. + // This is due to some operating systems not using binary mode by default. + // Since we will most likely be dealing with binary files when encoding, we + // have to be able to deal with zeros (and other invalid chars) in the input stream. + std::ifstream instream(input.c_str(), std::ios_base::in | std::ios_base::binary); + if (!instream.is_open()) + { + usage("Could not open input file!"); + exit(-1); + } + + // Now try to open the output file + std::string output = argv[3]; + // Again, note that we have to open the ouput in binary mode. + // Similiarly, we will most likely need to deal with zeros in the output stream when we + // are decoding, and the output stream has to be able to use these invalid text chars. + std::ofstream outstream(output.c_str(), std::ios_base::out | std::ios_base::binary); + if (!outstream.is_open()) + { + usage("Could not open output file!"); + exit(-1); + } + + // determine whether we need to encode or decode: + std::string choice = argv[1]; + if (choice == "-d") + { + base64::Decoder D; + D.decode(instream, outstream); + } + else if (choice == "-e") + { + base64::Encoder E; + E.encode(instream, outstream); + } + else + { + std::cout<<"["<<choice<<"]"<<std::endl; + usage("Please specify -d or -e as first argument!"); + } + + return 0; +} + Added: trunk/spark/utility/libb64/cdecode.c =================================================================== --- trunk/spark/utility/libb64/cdecode.c (rev 0) +++ trunk/spark/utility/libb64/cdecode.c 2010-12-14 17:07:37 UTC (rev 227) @@ -0,0 +1,88 @@ +/* +cdecoder.c - c source to a base64 decoding algorithm implementation + +This is part of the libb64 project, and has been placed in the public domain. +For details, see http://sourceforge.net/projects/libb64 +*/ + +#include "cdecode.h" + +int base64_decode_value(char value_in) +{ + static const char decoding[] = {62,-1,-1,-1,63,52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-2,-1,-1,-1,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,-1,-1,-1,-1,-1,-1,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}; + static const char decoding_size = sizeof(decoding); + value_in -= 43; + if (value_in < 0 || value_in > decoding_size) return -1; + return decoding[(int)value_in]; +} + +void base64_init_decodestate(base64_decodestate* state_in) +{ + state_in->step = step_a; + state_in->plainchar = 0; +} + +int base64_decode_block(const char* code_in, const int length_in, char* plaintext_out, base64_decodestate* state_in) +{ + const char* codechar = code_in; + char* plainchar = plaintext_out; + char fragment; + + *plainchar = state_in->plainchar; + + switch (state_in->step) + { + while (1) + { + case step_a: + do { + if (codechar == code_in+length_in) + { + state_in->step = step_a; + state_in->plainchar = *plainchar; + return plainchar - plaintext_out; + } + fragment = (char)base64_decode_value(*codechar++); + } while (fragment < 0); + *plainchar = (fragment & 0x03f) << 2; + case step_b: + do { + if (codechar == code_in+length_in) + { + state_in->step = step_b; + state_in->plainchar = *plainchar; + return plainchar - plaintext_out; + } + fragment = (char)base64_decode_value(*codechar++); + } while (fragment < 0); + *plainchar++ |= (fragment & 0x030) >> 4; + *plainchar = (fragment & 0x00f) << 4; + case step_c: + do { + if (codechar == code_in+length_in) + { + state_in->step = step_c; + state_in->plainchar = *plainchar; + return plainchar - plaintext_out; + } + fragment = (char)base64_decode_value(*codechar++); + } while (fragment < 0); + *plainchar++ |= (fragment & 0x03c) >> 2; + *plainchar = (fragment & 0x003) << 6; + case step_d: + do { + if (codechar == code_in+length_in) + { + state_in->step = step_d; + state_in->plainchar = *plainchar; + return plainchar - plaintext_out; + } + fragment = (char)base64_decode_value(*codechar++); + } while (fragment < 0); + *plainchar++ |= (fragment & 0x03f); + } + } + /* control should not reach here */ + return plainchar - plaintext_out; +} + Added: trunk/spark/utility/libb64/cdecode.h =================================================================== --- trunk/spark/utility/libb64/cdecode.h (rev 0) +++ trunk/spark/utility/libb64/cdecode.h 2010-12-14 17:07:37 UTC (rev 227) @@ -0,0 +1,28 @@ +/* +cdecode.h - c header for a base64 decoding algorithm + +This is part of the libb64 project, and has been placed in the public domain. +For details, see http://sourceforge.net/projects/libb64 +*/ + +#ifndef BASE64_CDECODE_H +#define BASE64_CDECODE_H + +typedef enum +{ + step_a, step_b, step_c, step_d +} base64_decodestep; + +typedef struct +{ + base64_decodestep step; + char plainchar; +} base64_decodestate; + +void base64_init_decodestate(base64_decodestate* state_in); + +int base64_decode_value(char value_in); + +int base64_decode_block(const char* code_in, const int length_in, char* plaintext_out, base64_decodestate* state_in); + +#endif /* BASE64_CDECODE_H */ Added: trunk/spark/utility/libb64/cencode.c =================================================================== --- trunk/spark/utility/libb64/cencode.c (rev 0) +++ trunk/spark/utility/libb64/cencode.c 2010-12-14 17:07:37 UTC (rev 227) @@ -0,0 +1,103 @@ +/* +cencoder.c - c source to a base64 encoding algorithm implementation + +This is part of the libb64 project, and has been placed in the public domain. +For details, see http://sourceforge.net/projects/libb64 + +Modified for SimSpark (http://simspark.sourceforge.net) +*/ + +#include "cencode.h" + +void base64_init_encodestate(base64_encodestate* state_in) +{ + state_in->step = step_A; + state_in->result = 0; + state_in->stepcount = 0; +} + +char base64_encode_value(char value_in) +{ + static const char* encoding = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + if (value_in > 63) return '='; + return encoding[(int)value_in]; +} + +int base64_encode_block(const char* plaintext_in, int length_in, char* code_out, base64_encodestate* state_in) +{ + const char* plainchar = plaintext_in; + const char* const plaintextend = plaintext_in + length_in; + char* codechar = code_out; + char result; + char fragment; + + result = state_in->result; + + switch (state_in->step) + { + while (1) + { + case step_A: + if (plainchar == plaintextend) + { + state_in->result = result; + state_in->step = step_A; + return codechar - code_out; + } + fragment = *plainchar++; + result = (fragment & 0x0fc) >> 2; + *codechar++ = base64_encode_value(result); + result = (fragment & 0x003) << 4; + case step_B: + if (plainchar == plaintextend) + { + state_in->result = result; + state_in->step = step_B; + return codechar - code_out; + } + fragment = *plainchar++; + result |= (fragment & 0x0f0) >> 4; + *codechar++ = base64_encode_value(result); + result = (fragment & 0x00f) << 2; + case step_C: + if (plainchar == plaintextend) + { + state_in->result = result; + state_in->step = step_C; + return codechar - code_out; + } + fragment = *plainchar++; + result |= (fragment & 0x0c0) >> 6; + *codechar++ = base64_encode_value(result); + result = (fragment & 0x03f) >> 0; + *codechar++ = base64_encode_value(result); + + ++(state_in->stepcount); + } + } + /* control should not reach here */ + return codechar - code_out; +} + +int base64_encode_blockend(char* code_out, base64_encodestate* state_in) +{ + char* codechar = code_out; + + switch (state_in->step) + { + case step_B: + *codechar++ = base64_encode_value(state_in->result); + *codechar++ = '='; + *codechar++ = '='; + break; + case step_C: + *codechar++ = base64_encode_value(state_in->result); + *codechar++ = '='; + break; + case step_A: + break; + } + + return codechar - code_out; +} + Added: trunk/spark/utility/libb64/cencode.h =================================================================== --- trunk/spark/utility/libb64/cencode.h (rev 0) +++ trunk/spark/utility/libb64/cencode.h 2010-12-14 17:07:37 UTC (rev 227) @@ -0,0 +1,33 @@ +/* +cencode.h - c header for a base64 encoding algorithm + +This is part of the libb64 project, and has been placed in the public domain. +For details, see http://sourceforge.net/projects/libb64 +*/ + +#ifndef BASE64_CENCODE_H +#define BASE64_CENCODE_H + +#define BUFFERSIZE 64 + +typedef enum +{ + step_A, step_B, step_C +} base64_encodestep; + +typedef struct +{ + base64_encodestep step; + char result; + int stepcount; +} base64_encodestate; + +void base64_init_encodestate(base64_encodestate* state_in); + +char base64_encode_value(char value_in); + +int base64_encode_block(const char* plaintext_in, int length_in, char* code_out, base64_encodestate* state_in); + +int base64_encode_blockend(char* code_out, base64_encodestate* state_in); + +#endif /* BASE64_CENCODE_H */ Added: trunk/spark/utility/libb64/decode.h =================================================================== --- trunk/spark/utility/libb64/decode.h (rev 0) +++ trunk/spark/utility/libb64/decode.h 2010-12-14 17:07:37 UTC (rev 227) @@ -0,0 +1,96 @@ +// :mode=c++: +/* +decode.h - c++ wrapper for a base64 decoding algorithm + +This is part of the libb64 project, and has been placed in the public domain. +For details, see http://sourceforge.net/projects/libb64 + +Modified for SimSpark (http://simspark.sourceforge.net) + */ +#ifndef BASE64_DECODE_H +#define BASE64_DECODE_H + +#include <iostream> + +namespace base64 { +extern "C" { +#include "cdecode.h" +} + +class Decoder +{ +public: + + void decode(std::istream& istream_in, std::ostream& ostream_in) + { + std::string code; + istream_in >> code; + + int len = code.size(); + char* text = new char[len]; // enough space + + int textLen = decode(code, text, len); + + ostream_in.write((const char*) text, textLen); + } + + /* Please make sure the out has enough memory (but not bigger than length of in ) + * @return the lenght of decode result + */ + int decode(const std::string& in, char* out, int out_len) + { + base64_init_decodestate(&_state); + + int codelength; + int plainlength; + int totalLen = 0; + + const char* code = in.c_str(); + int len = in.size(); + + while ( len>0 && out_len>0 ) + { + if (len > out_len) + { + codelength = out_len; + } + else + { + codelength = len; + } + + plainlength = decode(code, codelength, out+totalLen); + + code += codelength; + len -= codelength; + + out_len -= plainlength; + totalLen += plainlength; + } + + return totalLen; + } + +protected: + int decode(char value_in) + { + return base64_decode_value(value_in); + } + + int decode(const char* code_in, const int length_in, char* plaintext_out) + { + return base64_decode_block(code_in, length_in, plaintext_out, &_state); + } + +private: + base64_decodestate _state; + int _buffersize; +}; + + +} // namespace base64 + + + +#endif // BASE64_DECODE_H + Added: trunk/spark/utility/libb64/encode.h =================================================================== --- trunk/spark/utility/libb64/encode.h (rev 0) +++ trunk/spark/utility/libb64/encode.h 2010-12-14 17:07:37 UTC (rev 227) @@ -0,0 +1,108 @@ +// :mode=c++: +/* +encode.h - c++ wrapper for a base64 encoding algorithm + +This is part of the libb64 project, and has been placed in the public domain. +For details, see http://sourceforge.net/projects/libb64 + + Modified for SimSpark (http://simspark.sourceforge.net) + */ +#ifndef BASE64_ENCODE_H +#define BASE64_ENCODE_H + +#include <iostream> +#include <sstream> + +namespace base64 { +extern "C" { +#include "cencode.h" +} + +class Encoder +{ +public: + + Encoder(int buffersize_in = BUFFERSIZE) + : _buffersize(buffersize_in) + { + _code = new char[2 * _buffersize]; + } + + ~Encoder() + { + delete [] _code; + } + + void encode(std::istream& istream_in, std::ostream& ostream_in) + { + // get length + istream_in.seekg(0, std::ios::end); + int length = istream_in.tellg(); + istream_in.seekg(0, std::ios::beg); + + // allocate memory: + char* buffer = new char [length]; + + // read data as a block: + istream_in.read(buffer, length); + + std::string code = encode(buffer, length); + ostream_in << code; + } + + std::string encode(const char* in, int len) + { + base64_init_encodestate(&_state); + + int plainlength; + int codelength; + std::stringstream ss; + + while (len > 0) { + if (len > _buffersize) { + plainlength = _buffersize; + } else { + plainlength = len; + } + // + codelength = encode(in, plainlength, _code); + + ss.write(_code, codelength); + + in += plainlength; + len -= plainlength; + } + + codelength = encode_end(_code); + ss.write(_code, codelength); + + return ss.str(); + } + +protected: + + int encode(char value_in) + { + return base64_encode_value(value_in); + } + + int encode(const char* code_in, const int length_in, char* plaintext_out) + { + return base64_encode_block(code_in, length_in, plaintext_out, &_state); + } + + int encode_end(char* plaintext_out) + { + return base64_encode_blockend(plaintext_out, &_state); + } + +private: + base64_encodestate _state; + int _buffersize; + char* _code; +}; + +} // namespace base64 + +#endif // BASE64_ENCODE_H + This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |