diff options
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/Base85.cc | 129 | ||||
| -rw-r--r-- | lib/LuaHttp.cc | 45 | 
2 files changed, 174 insertions, 0 deletions
diff --git a/lib/Base85.cc b/lib/Base85.cc new file mode 100644 index 0000000..d8722fb --- /dev/null +++ b/lib/Base85.cc @@ -0,0 +1,129 @@ +/* + *  Baltisot + *  Copyright (C) 1999-2009 Nicolas "Pixel" Noble + * + *  This program is free software; you can redistribute it and/or modify + *  it under the terms of the GNU General Public License as published by + *  the Free Software Foundation; either version 2 of the License, or + *  (at your option) any later version. + * + *  This program is distributed in the hope that it will be useful, + *  but WITHOUT ANY WARRANTY; without even the implied warranty of + *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the + *  GNU General Public License for more details. + * + *  You should have received a copy of the GNU General Public License + *  along with this program; if not, write to the Free Software + *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA + */ + +#include <Base85.h> + +String Base85::encode_block(unsigned char in_tab[4], int len) { +    unsigned int tuple; +    char r[6]; +     +    tuple  = in_tab[0]; +    tuple |= in_tab[1] <<  8; +    tuple |= in_tab[2] << 16; +    tuple |= in_tab[3] << 24; +     +    r[0] = (tuple % 85) + '!'; +    tuple /= 85; +    r[1] = (tuple % 85) + '!'; +    tuple /= 85; +    r[2] = len > 1 ? (tuple % 85) + '!' : '~'; +    tuple /= 85; +    r[3] = len > 2 ? (tuple % 85) + '!' : '~'; +    tuple /= 85; +    r[4] = len > 3 ? (tuple % 85) + '!' : '~'; +    r[5] = 0; +     +    return r; +} + +String Base85::encode(const char * data, int stream_size) { +    String encoded = ""; +    unsigned char in_tab[4]; +    int len, i, s_pos; + +    s_pos = 0; + +    while (stream_size > 0) { +        in_tab[0] = 0; +        in_tab[1] = 0; +        in_tab[2] = 0; +        in_tab[3] = 0; + +        len = stream_size >= 4 ? 4 : stream_size; + +        for (i = 0; i < len; i++) { +            in_tab[i] = data[s_pos + i]; +        } + +        encoded += encode_block(in_tab, len); + +        s_pos += 4; +        stream_size -= 4; +    } + +    return encoded; +} + +int Base85::decode_block(char s1, char s2, char s3, char s4, char s5, unsigned char * out_tab) { +    int len, sb1, sb2, sb3, sb4, sb5; +    unsigned int tuple; + +    len = s3 == '~' ? 1 : s4 == '~' ? 2 : s5 == '~' ? 3 : 4; +    s3 = (s3 == '~') || (s3 == 0) ? '!' : s3; +    s4 = (s4 == '~') || (s4 == 0) ? '!' : s4; +    s5 = (s5 == '~') || (s5 == 0) ? '!' : s5; +     +    sb1 = s1 - '!'; +    sb2 = s2 - '!'; +    sb3 = s3 - '!'; +    sb4 = s4 - '!'; +    sb5 = s5 - '!'; +     +    tuple  = s5; tuple *= 85; +    tuple |= s4; tuple *= 85; +    tuple |= s3; tuple *= 85; +    tuple |= s2; tuple *= 85; +    tuple |= s1; +     +    out_tab[0] = tuple & 255; tuple >>= 8; +    out_tab[1] = tuple & 255; tuple >>= 8; +    out_tab[2] = tuple & 255; tuple >>= 8; +    out_tab[3] = tuple & 255; +     +    return len; +} + +unsigned char * Base85::decode(const String & str_in, int * len_out) { +    int s_len = str_in.strlen(), len = 0, i, j, t_len; +    char s1, s2, s3, s4, s5; +    unsigned char t_out[4]; +    unsigned char * out = (unsigned char *) malloc(s_len * 4 / 5 + 5); +    unsigned char * p = out; +     +    for (i = 0; i < s_len; i += 5) { +        s1 = str_in[i + 0]; +        s2 = str_in[i + 1]; +        s3 = str_in[i + 2]; +        s4 = str_in[i + 3]; +        s5 = str_in[i + 4]; +        t_len = decode_block(s1, s2, s3, s4, s5, t_out); +         +        for (j = 0; j < t_len; j++) { +            *(p++) = t_out[j]; +        } + +        len += t_len; +    } +     +    if (len_out) { +        *len_out = len; +    } + +    return out; +} diff --git a/lib/LuaHttp.cc b/lib/LuaHttp.cc index 2a78391..a88c77c 100644 --- a/lib/LuaHttp.cc +++ b/lib/LuaHttp.cc @@ -22,6 +22,7 @@  #include "LuaHandle.h"  #include "LuaTask.h"  #include "Base64.h" +#include "Base85.h"  #include "HashFunction.h"  #include "RandISAAC.h" @@ -120,6 +121,9 @@ enum HttpResponse_functions_t {      HTTPRESPONSE_BASE64_ENCODE,      HTTPRESPONSE_BASE64_DECODE,      HTTPRESPONSE_BASE64_DECODE_BIN, +    HTTPRESPONSE_BASE85_ENCODE, +    HTTPRESPONSE_BASE85_DECODE, +    HTTPRESPONSE_BASE85_DECODE_BIN,      HTTPRESPONSE_MD5,      HTTPRESPONSE_SHA1,      HTTPRESPONSE_SHA256, @@ -137,6 +141,9 @@ struct lua_functypes_t HttpResponse_functions[] = {      { HTTPRESPONSE_BASE64_ENCODE,     "Base64Encode",    1, 1, { BLUA_STRING | BLUA_OBJECT } },      { HTTPRESPONSE_BASE64_DECODE,     "Base64Decode",    1, 1, { BLUA_STRING } },      { HTTPRESPONSE_BASE64_DECODE_BIN, "Base64DecodeBin", 2, 2, { BLUA_STRING, BLUA_OBJECT } }, +    { HTTPRESPONSE_BASE85_ENCODE,     "Base85Encode",    1, 1, { BLUA_STRING | BLUA_OBJECT } }, +    { HTTPRESPONSE_BASE85_DECODE,     "Base85Decode",    1, 1, { BLUA_STRING } }, +    { HTTPRESPONSE_BASE85_DECODE_BIN, "Base85DecodeBin", 2, 2, { BLUA_STRING, BLUA_OBJECT } },      { HTTPRESPONSE_MD5,               "MD5",             1, 1, { BLUA_STRING | BLUA_OBJECT } },      { HTTPRESPONSE_SHA1,              "SHA1",            1, 1, { BLUA_STRING | BLUA_OBJECT } },      { HTTPRESPONSE_SHA256,            "SHA256",          1, 1, { BLUA_STRING | BLUA_OBJECT } }, @@ -153,6 +160,9 @@ class sLua_HttpResponse : public Base {      DECLARE_FUNCTION(HttpResponse, HTTPRESPONSE_BASE64_ENCODE);      DECLARE_FUNCTION(HttpResponse, HTTPRESPONSE_BASE64_DECODE);      DECLARE_FUNCTION(HttpResponse, HTTPRESPONSE_BASE64_DECODE_BIN); +    DECLARE_FUNCTION(HttpResponse, HTTPRESPONSE_BASE85_ENCODE); +    DECLARE_FUNCTION(HttpResponse, HTTPRESPONSE_BASE85_DECODE); +    DECLARE_FUNCTION(HttpResponse, HTTPRESPONSE_BASE85_DECODE_BIN);      DECLARE_FUNCTION(HttpResponse, HTTPRESPONSE_MD5);      DECLARE_FUNCTION(HttpResponse, HTTPRESPONSE_SHA1);      DECLARE_FUNCTION(HttpResponse, HTTPRESPONSE_SHA256); @@ -177,6 +187,9 @@ void LuaHttpResponse::pushstatics(Lua * L) throw (GeneralException) {      PUSH_FUNCTION(HttpResponse, HTTPRESPONSE_BASE64_ENCODE);      PUSH_FUNCTION(HttpResponse, HTTPRESPONSE_BASE64_DECODE);      PUSH_FUNCTION(HttpResponse, HTTPRESPONSE_BASE64_DECODE_BIN); +    PUSH_FUNCTION(HttpResponse, HTTPRESPONSE_BASE85_ENCODE); +    PUSH_FUNCTION(HttpResponse, HTTPRESPONSE_BASE85_DECODE); +    PUSH_FUNCTION(HttpResponse, HTTPRESPONSE_BASE85_DECODE_BIN);      PUSH_FUNCTION(HttpResponse, HTTPRESPONSE_MD5);      PUSH_FUNCTION(HttpResponse, HTTPRESPONSE_SHA1);      PUSH_FUNCTION(HttpResponse, HTTPRESPONSE_SHA256); @@ -331,6 +344,38 @@ int sLua_HttpResponse::HttpResponse_proceed_statics(Lua * L, int n, int caller)              free(enc_t);          }          break; +    case HTTPRESPONSE_BASE85_ENCODE: +        if (L->isstring()) { +            dec = L->tostring(); +            dec_t = dec.to_charp(); +            L->push(Base85::encode(dec_t, dec.strlen())); +        } else { +            Handle * hdata = L->recast<Handle>(1); +            int size = hdata->GetSize(); +            char * data = (char *) malloc(size); +            hdata->read(data, size); +            L->push(Base85::encode(data, size)); +            free(data); +        } +        r = 1; +        break; +    case HTTPRESPONSE_BASE85_DECODE: +        enc_t = (char *) Base85::decode(L->tostring(), &l); +        enc_t[l] = 0; +        L->push(enc_t, l); +        free(enc_t); +        r = 1; +        break; +    case HTTPRESPONSE_BASE85_DECODE_BIN: +        { +            Handle * out = lua_recast<Handle>(L, 2); +            if (!out) +                L->error("Need an output handle to Base85DecodeBin"); +            enc_t = (char *) Base85::decode(L->tostring(1), &l); +            out->write(enc_t, l); +            free(enc_t); +        } +        break;      case HTTPRESPONSE_MD5:          if (L->isstring()) {              dec = L->tostring();  | 
