Changeset 1379 for trunk


Ignore:
Timestamp:
Apr 4, 2009, 3:49:35 PM (15 years ago)
Author:
gah
Message:

Check for empty strings before encoding/decoding

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/lang/tcl/src/RpEncodeTclInterface.cc

    r1368 r1379  
    1414#include <tcl.h>
    1515#include "RpEncode.h"
    16 
    17 #define TRUE    1
    18 #define FALSE   0
    1916
    2017extern "C" Tcl_AppInitProc RpEncoding_Init;
     
    7673    }
    7774
    78     const char *type = Tcl_GetString(objv[1]);
    7975
    8076    int bufLen;
    8177    const char *buf;
     78    const char *string;
    8279    buf = (const char*) Tcl_GetByteArrayFromObj(objv[2], &bufLen);
     80    const char *type = Tcl_GetString(objv[1]);
    8381    if (('b' == *type) && (strcmp(type,"binary") == 0)) {
    84         if (Rappture::encoding::isbinary(buf,bufLen) != 0) {
    85             // non-ascii character found, return yes
    86             Tcl_SetResult(interp, (char *)"yes", TCL_STATIC);
    87         } else {
    88             Tcl_SetResult(interp, (char *)"no", TCL_STATIC);
    89         }
    90         return TCL_OK;
     82        bool isBinary;
     83
     84        isBinary = (Rappture::encoding::isbinary(buf,bufLen) != 0);
     85        string = (isBinary) ? "yes" : "no" ;
    9186    } else if (('e' == *type) && (strcmp(type,"encoded") == 0)) {
    92         if (Rappture::encoding::isencoded(buf, bufLen) != 0) {
    93             // valid "@@RP-ENC:" header found, return yes
    94             Tcl_SetResult(interp, (char *)"yes", TCL_STATIC);
    95         } else {
    96             Tcl_SetResult(interp, (char *)"no", TCL_STATIC);
    97         }
    98         return TCL_OK;
     87        bool isEncoded;
     88
     89        isEncoded = (Rappture::encoding::isencoded(buf, bufLen) != 0);
     90        string = (isEncoded) ? "yes" : "no" ;
    9991    } else {
    10092        Tcl_AppendResult(interp, "bad option \"", type,
    10193                "\": should be binary or encoded", (char*)NULL);
    102     }
    103     return TCL_ERROR;
     94        return TCL_ERROR;
     95    }
     96    Tcl_SetResult(interp, (char *)string, TCL_STATIC);
     97    return TCL_OK;
    10498}
    10599
     
    120114
    121115static int
    122 RpTclEncodingEncode (   ClientData cdata,
     116RpTclEncodingEncode (ClientData cdata, Tcl_Interp *interp, int objc,
     117                     Tcl_Obj *const *objv)
     118{
     119    const char* encodeType    = NULL; // name of the units provided by user
     120    const char* cmdName       = NULL;
     121    Rappture::Outcome err;
     122
     123    int typeLen               = 0;
     124    int nextarg               = 0; // start parsing using the '2'th argument
     125
     126    bool compress, base64, addHeader;
     127
     128    compress = base64 = addHeader = true;
     129
     130    Tcl_Obj *result           = NULL;
     131
     132    Tcl_ResetResult(interp);
     133
     134    cmdName = Tcl_GetString(objv[nextarg++]);
     135
     136    // parse through command line options
     137    if (objc < 1) {
     138        Tcl_AppendResult(interp,
     139                "wrong # args: should be \"", cmdName,
     140                " ?-as z|b64|zb64? ?-no-header? ?--? <string>\"", (char*)NULL);
     141        return TCL_ERROR;
     142    }
     143
     144    while ((objc - nextarg) > 0) {
     145        const char *option;
     146        int optionLen;
     147
     148        option = Tcl_GetStringFromObj(objv[nextarg], &optionLen);
     149        if (*option == '-') {
     150            if (strncmp(option,"-as", optionLen) == 0 ) {
     151                nextarg++;
     152                typeLen = 0;
     153                if (nextarg < objc) {
     154                    encodeType = Tcl_GetStringFromObj(objv[nextarg],&typeLen);
     155                    nextarg++;
     156                }
     157                if ((typeLen == 1) && (strncmp(encodeType,"z",typeLen) == 0) ) {
     158                    compress = true;
     159                    base64 = false;
     160                } else if ((typeLen == 3) &&
     161                         (strncmp(encodeType,"b64",typeLen) == 0) ) {
     162                    compress = false;
     163                    base64 = true;
     164                } else if ((typeLen == 4) &&
     165                         (strncmp(encodeType,"zb64",typeLen) == 0) ) {
     166                    compress = true;
     167                    base64 = true;
     168                } else {
     169                    // user did not specify recognized wishes for this option,
     170                    Tcl_AppendResult(interp, "bad value \"",(char*)NULL);
     171                    if (encodeType != NULL) {
     172                        Tcl_AppendResult(interp, encodeType,(char*)NULL);
     173                    }
     174                    Tcl_AppendResult(interp,
     175                            "\": should be one of z, b64, zb64",
     176                            (char*)NULL);
     177                    return TCL_ERROR;
     178                }
     179            } else if ( strncmp(option,"-no-header", optionLen) == 0 ) {
     180                nextarg++;
     181                addHeader = false;
     182            } else if ( strcmp(option,"--") == 0 ) {
     183                nextarg++;
     184                break;
     185            } else {
     186                Tcl_AppendResult(interp,
     187                    "bad option \"", option,
     188                    "\": should be -as, -no-header, --", (char*)NULL);
     189                return TCL_ERROR;
     190            }
     191        } else {
     192            break;
     193        }
     194    }
     195
     196    if ((objc - nextarg) != 1) {
     197        Tcl_AppendResult(interp,
     198                "wrong # args: should be \"", cmdName,
     199                " ?-as z|b64|zb64? ?-no-header? ?--? <string>\"", (char*)NULL);
     200        return TCL_ERROR;
     201    }
     202
     203    int nBytes;
     204    const char* string;
     205    string = (const char*)Tcl_GetByteArrayFromObj(objv[nextarg++], &nBytes);
     206    if (nBytes <= 0) {
     207        return TCL_OK;          // Nothing to encode.
     208    }
     209    Rappture::Buffer buf(string, nBytes);
     210    unsigned int flags = 0;
     211    if (compress) {
     212        flags |= RPENC_Z;
     213    }
     214    if (base64) {
     215        flags |= RPENC_B64;
     216    }
     217    if (addHeader) {
     218        flags |= RPENC_HDR;
     219    }
     220    if (!Rappture::encoding::encode(err, buf, flags)) {
     221        Tcl_AppendResult(interp, err.remark(), "\n", err.context(), NULL);
     222        return TCL_ERROR;
     223    }
     224    result = Tcl_NewByteArrayObj((const unsigned char*)buf.bytes(), buf.size());
     225    Tcl_SetObjResult(interp, result);
     226    return TCL_OK;
     227}
     228
     229/**********************************************************************/
     230// FUNCTION: RpTclEncodingDecode()
     231/// Rappture::encoding::decode function in Tcl, decodes provided string
     232/**
     233 * Decode a string by uncompressing it with zlib and base64 decoding it.
     234 * If binary data is provided, the data is base64 decoded and uncompressed.
     235 * RpTclEncodingIs is used to qualify binary data.
     236 *
     237 * Full function call:
     238 * ::Rappture::encoding::decode ?-as z|b64|zb64? <string>
     239 */
     240
     241static int
     242RpTclEncodingDecode (   ClientData cdata,
    123243                        Tcl_Interp *interp,
    124244                        int objc,
     
    133253    int nextarg               = 0; // start parsing using the '2'th argument
    134254
    135     bool compress, base64, addHeader;
    136 
    137     compress = base64 = addHeader = true;
    138 
    139     Tcl_Obj *result           = NULL;
    140 
    141     Tcl_ResetResult(interp);
     255    bool decompress, base64;
     256    decompress = base64 = false;
    142257
    143258    cmdName = Tcl_GetString(objv[nextarg++]);
     
    147262        Tcl_AppendResult(interp,
    148263                "wrong # args: should be \"", cmdName,
    149                 " ?-as z|b64|zb64? ?-no-header? ?--? <string>\"", (char*)NULL);
     264                " ?-as z|b64|zb64? ?--? <string>\"", (char*)NULL);
    150265        return TCL_ERROR;
    151266    }
     
    166281                if (        (typeLen == 1) &&
    167282                            (strncmp(encodeType,"z",typeLen) == 0) ) {
    168                     compress = TRUE;
    169                     base64 = FALSE;
     283                    decompress = true;
     284                    base64 = false;
    170285                }
    171286                else if (   (typeLen == 3) &&
    172287                            (strncmp(encodeType,"b64",typeLen) == 0) ) {
    173                     compress = FALSE;
    174                     base64 = TRUE;
     288                    decompress = false;
     289                    base64 = true;
    175290                }
    176291                else if (   (typeLen == 4) &&
    177292                            (strncmp(encodeType,"zb64",typeLen) == 0) ) {
    178                     compress = TRUE;
    179                     base64 = TRUE;
     293                    decompress = true;
     294                    base64 = true;
    180295                }
    181296                else {
     
    190305                    return TCL_ERROR;
    191306                }
    192             }
    193             else if ( strncmp(option,"-no-header",optionLen) == 0 ) {
    194                 nextarg++;
    195                 addHeader = FALSE;
    196             }
    197             else if ( strcmp(option,"--") == 0 ) {
    198                 nextarg++;
    199                 break;
    200             }
    201             else {
    202                 Tcl_AppendResult(interp,
    203                     "bad option \"", option,
    204                     "\": should be -as, -no-header, --", (char*)NULL);
    205                 return TCL_ERROR;
    206             }
    207         }
    208         else {
    209             break;
    210         }
    211     }
    212 
    213     if ((objc - nextarg) != 1) {
    214         Tcl_AppendResult(interp,
    215                 "wrong # args: should be \"", cmdName,
    216                 " ?-as z|b64|zb64? ?-no-header? ?--? <string>\"", (char*)NULL);
    217         return TCL_ERROR;
    218     }
    219 
    220     int optionLen;
    221     const char* option;
    222     option = (const char*) Tcl_GetByteArrayFromObj(objv[nextarg++], &optionLen);
    223     Rappture::Buffer buf(option,optionLen);
    224 
    225     unsigned int flags = 0;
    226     if (compress) {
    227         flags |= RPENC_Z;
    228     }
    229     if (base64) {
    230         flags |= RPENC_B64;
    231     }
    232     if (addHeader) {
    233         flags |= RPENC_HDR;
    234     }
    235     if (!Rappture::encoding::encode(err, buf, flags)) {
    236         Tcl_AppendResult(interp, err.remark(), "\n", err.context(), NULL);
    237         return TCL_ERROR;
    238     }
    239     result = Tcl_NewByteArrayObj((const unsigned char*)buf.bytes(), buf.size());
    240     Tcl_SetObjResult(interp, result);
    241     return TCL_OK;
    242 }
    243 
    244 /**********************************************************************/
    245 // FUNCTION: RpTclEncodingDecode()
    246 /// Rappture::encoding::decode function in Tcl, decodes provided string
    247 /**
    248  * Decode a string by uncompressing it with zlib and base64 decoding it.
    249  * If binary data is provided, the data is base64 decoded and uncompressed.
    250  * RpTclEncodingIs is used to qualify binary data.
    251  *
    252  * Full function call:
    253  * ::Rappture::encoding::decode ?-as z|b64|zb64? <string>
    254  */
    255 
    256 static int
    257 RpTclEncodingDecode (   ClientData cdata,
    258                         Tcl_Interp *interp,
    259                         int objc,
    260                         Tcl_Obj *const objv[]  )
    261 {
    262 
    263     const char* encodeType    = NULL; // name of the units provided by user
    264     const char* cmdName       = NULL;
    265     Rappture::Outcome err;
    266 
    267     int typeLen               = 0;
    268     int nextarg               = 0; // start parsing using the '2'th argument
    269 
    270     bool decompress, base64;
    271     decompress = base64 = false;
    272 
    273     cmdName = Tcl_GetString(objv[nextarg++]);
    274 
    275     // parse through command line options
    276     if (objc < 1) {
    277         Tcl_AppendResult(interp,
    278                 "wrong # args: should be \"", cmdName,
    279                 " ?-as z|b64|zb64? ?--? <string>\"", (char*)NULL);
    280         return TCL_ERROR;
    281     }
    282 
    283     while ((objc - nextarg) > 0) {
    284         const char *option;
    285         int optionLen;
    286 
    287         option = Tcl_GetStringFromObj(objv[nextarg], &optionLen);
    288         if (*option == '-') {
    289             if ( strncmp(option,"-as",optionLen) == 0 ) {
    290                 nextarg++;
    291                 typeLen = 0;
    292                 if (nextarg < objc) {
    293                     encodeType = Tcl_GetStringFromObj(objv[nextarg],&typeLen);
    294                     nextarg++;
    295                 }
    296                 if (        (typeLen == 1) &&
    297                             (strncmp(encodeType,"z",typeLen) == 0) ) {
    298                     decompress = true;
    299                     base64 = false;
    300                 }
    301                 else if (   (typeLen == 3) &&
    302                             (strncmp(encodeType,"b64",typeLen) == 0) ) {
    303                     decompress = false;
    304                     base64 = true;
    305                 }
    306                 else if (   (typeLen == 4) &&
    307                             (strncmp(encodeType,"zb64",typeLen) == 0) ) {
    308                     decompress = true;
    309                     base64 = true;
    310                 }
    311                 else {
    312                     // user did not specify recognized wishes for this option,
    313                     Tcl_AppendResult(interp, "bad value \"",(char*)NULL);
    314                     if (encodeType != NULL) {
    315                         Tcl_AppendResult(interp, encodeType,(char*)NULL);
    316                     }
    317                     Tcl_AppendResult(interp,
    318                             "\": should be one of z, b64, zb64",
    319                             (char*)NULL);
    320                     return TCL_ERROR;
    321                 }
    322307            } else if ( strcmp(option,"--") == 0 ) {
    323308                nextarg++;
Note: See TracChangeset for help on using the changeset viewer.