Changeset 1264 for trunk


Ignore:
Timestamp:
Jan 7, 2009, 9:31:14 AM (16 years ago)
Author:
dkearney
Message:

fixing rp library's get() function for retrieving encoded data. if the data is encoded there is no need to do xml entity translation. adding a function rappture's encode class for checking to see if a string has a proper header encode header. adding corresponding tcl functions and tests. also adjusting some of the int declarations to size_t.

Location:
trunk
Files:
5 edited

Legend:

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

    r1042 r1264  
    6868        Tcl_AppendResult(interp,
    6969            "wrong # args: should be \"",
    70             Tcl_GetString(objv[0])," binary <string>\"",
     70            Tcl_GetString(objv[0])," binary|encoded <string>\"",
    7171            (char*)NULL);
    7272        return TCL_ERROR;
     
    7878    const char *buf = (const char*) Tcl_GetByteArrayFromObj(objv[2],&bufLen);
    7979
    80     if (strcmp(type,"binary") == 0) {
     80    if (('b' == *type) && (strcmp(type,"binary") == 0)) {
    8181        if (Rappture::encoding::isbinary(buf,bufLen) != 0) {
    8282            // non-ascii character found, return yes
     
    8686        }
    8787        return TCL_OK;
     88    } else if (('e' == *type) && (strcmp(type,"encoded") == 0)) {
     89        if (Rappture::encoding::isencoded(buf,bufLen) != 0) {
     90            // valid "@@RP-ENC:" header found, return yes
     91            Tcl_AppendResult(interp, "yes", (char*)NULL);
     92        } else {
     93            Tcl_AppendResult(interp, "no",(char*)NULL);
     94        }
     95        return TCL_OK;
    8896    }
    8997    Tcl_AppendResult(interp, "bad option \"", type,
    90             "\": should be binary",
     98            "\": should be one of binary, encoded",
    9199            (char*)NULL);
    92100    return TCL_ERROR;
  • trunk/lang/tcl/tests/encode.test

    r1042 r1264  
    2929test is-1.0.1 {Rappture::encoding::is, 0 arguments} {
    3030    list [catch {Rappture::encoding::is} msg] $msg
    31 } {1 {wrong # args: should be "Rappture::encoding::is binary <string>"}}
     31} {1 {wrong # args: should be "Rappture::encoding::is binary|encoded <string>"}}
    3232
    3333test is-1.1.1 {Rappture::encoding::is, 1 incomplete subcommand} {
    3434    list [catch {Rappture::encoding::is binary} msg] $msg
    35 } {1 {wrong # args: should be "Rappture::encoding::is binary <string>"}}
     35} {1 {wrong # args: should be "Rappture::encoding::is binary|encoded <string>"}}
    3636
    3737test is-1.1.2 {Rappture::encoding::is, 1 subcommand w/ string} {
     
    4747test is-1.2.0 {Rappture::encoding::is, 1 invalid subcommand} {
    4848    list [catch {Rappture::encoding::is binaryy "hi"} msg] $msg
    49 } {1 {bad option "binaryy": should be binary}}
     49} {1 {bad option "binaryy": should be one of binary, encoded}}
    5050
    5151test is-1.2.1 {Rappture::encoding::is, too many arguments} {
    5252    list [catch {Rappture::encoding::is binary binary "hi"} msg] $msg
    53 } {1 {wrong # args: should be "Rappture::encoding::is binary <string>"}}
     53} {1 {wrong # args: should be "Rappture::encoding::is binary|encoded <string>"}}
    5454
    5555test is-1.2.2 {Rappture::encoding::is, with an embedded null} {
    5656    list [catch {Rappture::encoding::is binary "x\000x"} msg] $msg
     57} {0 yes}
     58
     59test is-1.3.1 {Rappture::encoding::is, encoded subcommand w/ false string} {
     60    list [catch {Rappture::encoding::is encoded "hi"} msg] $msg
     61} {0 no}
     62
     63test is-1.3.2 {Rappture::encoding::is, encoded b64 flag and extra string} {
     64    set h "@@RP-ENC:b64\nH4sIAAAAAAAAA8vIBACsKpPYAgAAAA=="
     65    list [catch {Rappture::encoding::is encoded $h} msg] $msg
     66} {0 yes}
     67
     68test is-1.3.3 {Rappture::encoding::is, encoded zb64 flag and extra string} {
     69    set h "@@RP-ENC:zb64\nH4sIAAAAAAAAA8vIBACsKpPYAgAAAA=="
     70    list [catch {Rappture::encoding::is encoded $h} msg] $msg
     71} {0 yes}
     72
     73test is-1.3.4 {Rappture::encoding::is, encoded z flag and extra string} {
     74    set h "@@RP-ENC:z\nH4sIAAAAAAAAA8vIBACsKpPYAgAAAA=="
     75    list [catch {Rappture::encoding::is encoded $h} msg] $msg
     76} {0 yes}
     77
     78test is-1.3.5 {Rappture::encoding::is, encoded z flag newline no string} {
     79    set h "@@RP-ENC:z\n"
     80    list [catch {Rappture::encoding::is encoded $h} msg] $msg
     81} {0 yes}
     82
     83test is-1.3.6 {Rappture::encoding::is, encoded b64 flag newline no string} {
     84    set h "@@RP-ENC:b64\n"
     85    list [catch {Rappture::encoding::is encoded $h} msg] $msg
     86} {0 yes}
     87
     88test is-1.3.7 {Rappture::encoding::is, encoded zb64 flag newline no string} {
     89    set h "@@RP-ENC:zb64\n"
     90    list [catch {Rappture::encoding::is encoded $h} msg] $msg
     91} {0 yes}
     92
     93test is-1.3.7 {Rappture::encoding::is, encoded z flag no newline no string} {
     94    set h "@@RP-ENC:zb64"
     95    list [catch {Rappture::encoding::is encoded $h} msg] $msg
     96} {0 no}
     97
     98test is-1.3.8 {Rappture::encoding::is, encoded b64 flag no newline no string} {
     99    set h "@@RP-ENC:b64"
     100    list [catch {Rappture::encoding::is encoded $h} msg] $msg
     101} {0 no}
     102
     103test is-1.3.9 {Rappture::encoding::is, encoded zb64 flag no newline no string} {
     104    set h "@@RP-ENC:zb64"
     105    list [catch {Rappture::encoding::is encoded $h} msg] $msg
     106} {0 no}
     107
     108test is-1.3.10 {Rappture::encoding::is, encoded with an embedded null} {
     109    list [catch {Rappture::encoding::is encoded "@@RP-ENC:zb64\nx\000x"} msg] $msg
    57110} {0 yes}
    58111
  • trunk/src/core/RpEncode.cc

    r1051 r1264  
    3434isbinary(const char* buf, int size)
    3535{
     36    if (buf == NULL) {
     37        return 0;
     38    }
     39
    3640    if (size < 0) {
    3741        size = strlen(buf);
     
    5155}
    5256
    53 
    54 
    55 
     57/**********************************************************************/
     58// FUNCTION: Rappture::encoding::isencoded()
     59/// checks header of given string to determine if it was encoded by rappture.
     60/**
     61 * Checks to see if the string buf was encoded by rappture
     62 * and contains the proper "@@RP-ENC:" header.
     63 * rappture encoded strings start with the string "@@RP-ENC:X\n"
     64 * where X is one of z, b64, zb64
     65 * This function will not work for strings that do not have the header.
     66 * Full function call:
     67 * Rappture::encoding::isencoded(buf,size);
     68 *
     69 */
     70
     71size_t
     72isencoded(const char* buf, int size)
     73{
     74    size_t flags = 0;
     75    size_t len = 0;
     76
     77    if (buf == NULL) {
     78        return flags;
     79    }
     80
     81    if (size < 0) {
     82        len = strlen(buf);
     83    } else {
     84        len = size;
     85    }
     86
     87    // check the following for valid rappture encoded string:
     88    // all strings encoded by rappture are at least 11 characters
     89    // rappture encoded strings start with the '@' character
     90    // rappture encoded strings start with the string "@@RP-ENC:X\n"
     91    // where X is one of z, b64, zb64
     92    if (    (len >= 11)
     93        &&  ('@' == *buf)
     94        &&  (strncmp("@@RP-ENC:",buf,9) == 0) ) {
     95
     96        size_t idx = 9;
     97
     98        // check the string length and if the z flag was specified
     99        // add 1 for \n
     100        if (    (len >= (idx + 1))
     101            &&  (buf[idx] == 'z') ) {
     102            flags |= RPENC_Z;
     103            ++idx;
     104        }
     105        // check the string length and if the b64 flag was specified
     106        // add 1 for \n
     107        if (    (len >= (idx + 2 + 1))
     108            &&  (buf[idx]   == 'b')
     109            &&  (buf[idx+1] == '6')
     110            &&  (buf[idx+2] == '4') ) {
     111            flags |= RPENC_B64;
     112            idx += 3;
     113        }
     114        // check for the '\n' at the end of the header
     115        if (buf[idx] != '\n') {
     116            flags = 0;
     117        }
     118    }
     119
     120    return flags;
     121}
    56122
    57123/**********************************************************************/
     
    66132
    67133Rappture::Outcome
    68 encode (Rappture::Buffer& buf, int flags)
     134encode (Rappture::Buffer& buf, size_t flags)
    69135{
    70136
     
    125191
    126192Rappture::Outcome
    127 decode (Rappture::Buffer& buf, int flags)
     193decode (Rappture::Buffer& buf, size_t flags)
    128194{
    129195
  • trunk/src/core/RpEncode.h

    r1030 r1264  
    2929
    3030int isbinary(const char* buf, int size);
    31 Rappture::Outcome encode (Rappture::Buffer& buf, int flags);
    32 Rappture::Outcome decode (Rappture::Buffer& buf, int flags);
     31size_t isencoded(const char* buf, int size);
     32Rappture::Outcome encode (Rappture::Buffer& buf, size_t flags);
     33Rappture::Outcome decode (Rappture::Buffer& buf, size_t flags);
    3334
    3435#ifdef __cplusplus
  • trunk/src/core/RpLibrary.cc

    r1041 r1264  
    14571457        int myChildCount = 0;
    14581458
    1459         parentNode = NULL;
     1459        parentNode = NULL;
    14601460        if (path.empty()) {
    14611461            // an empty path uses the current RpLibrary as parent
     
    15391539
    15401540    inData = Rappture::Buffer(retCStr);
    1541     status &= Rappture::encoding::decode(inData,0);
    1542     status.addContext("RpLibrary::getSting");
    1543     // inData.append("\0",1);
    1544 
    1545     if (translateFlag == RPLIB_TRANSLATE) {
    1546         translatedContents = ERTranslator.decode(inData.bytes(),inData.size());
    1547         if (translatedContents == NULL) {
    1548             // translation failed
    1549             if (!status) {
    1550                 status.error("Error while translating entity references");
    1551                 status.addContext("RpLibrary::getSting");
    1552             }
    1553         }
    1554         else {
    1555             // subtract 1 from size because ERTranslator adds extra NULL
    1556             retStr = std::string(translatedContents,ERTranslator.size()-1);
    1557             translatedContents = NULL;
    1558         }
    1559     }
    1560     else {
     1541
     1542    if (Rappture::encoding::isencoded(inData.bytes(),inData.size()) != 0) {
     1543        // data is encoded,
     1544        // coming from an rplib, this means it was at least base64 encoded
     1545        // there is no reason to do entity translation
     1546        // because base64 character set does not include xml entity chars
     1547        status &= Rappture::encoding::decode(inData,0);
     1548        status.addContext("RpLibrary::getSting");
    15611549        retStr = std::string(inData.bytes(),inData.size());
     1550    } else {
     1551        // check translateFlag to see if we need to translate entity refs
     1552        if (translateFlag == RPLIB_TRANSLATE) {
     1553            translatedContents = ERTranslator.decode(inData.bytes(),inData.size());
     1554            if (translatedContents == NULL) {
     1555                // translation failed
     1556                if (!status) {
     1557                    status.error("Error while translating entity references");
     1558                    status.addContext("RpLibrary::getSting");
     1559                }
     1560            } else {
     1561                // subtract 1 from size because ERTranslator adds extra NULL
     1562                retStr = std::string(translatedContents,ERTranslator.size()-1);
     1563                translatedContents = NULL;
     1564            }
     1565        }
    15621566    }
    15631567
     
    19571961    Rappture::Buffer inData;
    19581962    unsigned int bytesWritten = 0;
    1959     int flags = 0;
     1963    size_t flags = 0;
    19601964
    19611965    if (!this->root) {
Note: See TracChangeset for help on using the changeset viewer.