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 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
/*
FALCON - The Falcon Programming Language.
FILE: webhelp.cpp
Helpers for web applications
-------------------------------------------------------------------
Author: Giancarlo Niccolai
Begin: Sun, 20 Jun 2010 17:12:56 +0200
-------------------------------------------------------------------
(C) Copyright 2010: the FALCON developers (see list in AUTHORS file)
See LICENSE file for licensing details.
*/
/*#
@beginmodule core
*/
#include "core_module.h"
#include <falcon/base64.h>
namespace Falcon {
namespace core {
/*#
@class Base64
@brief Collection of methods to handle rfc3548 Base64 encoding.
*/
/*#
@method encode Base64
@brief Transforms an input string or MemBuf into a Base64 encoded string.
@param data The data to be encoded.
@return The encoded string.
This @b static method encodes the contents of the incoming data as a
@b base64 encoded string.
The rfc3548 doesn't define the encoding of the input data, as base64 is a
method to encode generic binary data and send them across the Internet.
However, it's common practice to encode textual contents as utf-8 strings
and then apply the base64 encoding.
This method automatically uses utf-8
encoding to transform strings with international characters. If this is
not desired, provide a MemBuf as the parameter.
*/
FALCON_FUNC Base64_encode( VMachine* vm )
{
Item* i_data = vm->param(0);
if( i_data == 0 || ! (i_data->isMemBuf() || i_data->isString()) )
{
throw new ParamError( ErrorParam( e_inv_params ).
extra("S|M") );
}
CoreString* cs = new CoreString;
if( i_data->isString() )
{
Base64::encode( *i_data->asString(), *cs );
}
else
{
Base64::encode( i_data->asMemBuf()->data(), i_data->asMemBuf()->size(), *cs );
}
vm->retval(cs);
}
/*#
@method decode Base64
@brief Decodes a previously encoded text data.
@param data The data to be decoded.
@raise ParseError if the incoming data is not a correct base64 string.
@return The original string (as an international text).
This @b static method decodes the contents of the incoming data as a
@b base64 encoded string into a Falcon text-oriented String.
The rfc3548 doesn't define the encoding of the input data, as base64 is a
method to encode generic binary data and send them across the Internet.
However, it's common practice to encode textual contents as utf-8 strings
and then apply the base64 encoding.
So, this method supposes that the data, to be transformed in a string,
is actually an utf-8 representation of a text. If this is not desired,
use the Base64.decmb method.
*/
FALCON_FUNC Base64_decode( VMachine* vm )
{
Item* i_data = vm->param(0);
if( i_data == 0 || ! i_data->isString() )
{
throw new ParamError( ErrorParam( e_inv_params ).
extra("S") );
}
CoreString* cs = new CoreString;
if( ! Base64::decode( *i_data->asString(), *cs ) )
{
cs->mark(1);
throw new ParseError( ErrorParam( e_parse_format, __LINE__ ) );
}
vm->retval(cs);
}
/*#
@method decmb Base64
@brief Decodes a previously encoded binary data.
@param data The data to be decoded.
@raise ParseError if the incoming data is not a correct base64 string.
@return The origianl data, as a binary sequence of bytes.
This @b static method decodes the contents of the incoming data as a
@b base64 encoded string into a binary buffer.
*/
FALCON_FUNC Base64_decmb( VMachine* vm )
{
Item* i_data = vm->param(0);
if( i_data == 0 || ! i_data->isString() )
{
throw new ParamError( ErrorParam( e_inv_params ).
extra("S") );
}
const String& s = *i_data->asString();
uint32 tgtsize = s.length() / 4*3+3;
byte* tgt = (byte*) memAlloc( tgtsize );
if ( ! Base64::decode( *i_data->asString(), tgt, tgtsize ) )
{
memFree( tgt );
throw new ParseError( ErrorParam( e_parse_format, __LINE__ ) );
}
MemBuf_1* mb = new MemBuf_1( tgt, tgtsize, memFree );
vm->retval(mb);
}
}
}
/* end of webhelp.cpp */
|