Changeset 1264
- Timestamp:
- Jan 7, 2009 9:31:14 AM (14 years ago)
- Location:
- trunk
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/lang/tcl/src/RpEncodeTclInterface.cc
r1042 r1264 68 68 Tcl_AppendResult(interp, 69 69 "wrong # args: should be \"", 70 Tcl_GetString(objv[0])," binary <string>\"",70 Tcl_GetString(objv[0])," binary|encoded <string>\"", 71 71 (char*)NULL); 72 72 return TCL_ERROR; … … 78 78 const char *buf = (const char*) Tcl_GetByteArrayFromObj(objv[2],&bufLen); 79 79 80 if ( strcmp(type,"binary") == 0) {80 if (('b' == *type) && (strcmp(type,"binary") == 0)) { 81 81 if (Rappture::encoding::isbinary(buf,bufLen) != 0) { 82 82 // non-ascii character found, return yes … … 86 86 } 87 87 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; 88 96 } 89 97 Tcl_AppendResult(interp, "bad option \"", type, 90 "\": should be binary",98 "\": should be one of binary, encoded", 91 99 (char*)NULL); 92 100 return TCL_ERROR; -
trunk/lang/tcl/tests/encode.test
r1042 r1264 29 29 test is-1.0.1 {Rappture::encoding::is, 0 arguments} { 30 30 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>"}} 32 32 33 33 test is-1.1.1 {Rappture::encoding::is, 1 incomplete subcommand} { 34 34 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>"}} 36 36 37 37 test is-1.1.2 {Rappture::encoding::is, 1 subcommand w/ string} { … … 47 47 test is-1.2.0 {Rappture::encoding::is, 1 invalid subcommand} { 48 48 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}} 50 50 51 51 test is-1.2.1 {Rappture::encoding::is, too many arguments} { 52 52 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>"}} 54 54 55 55 test is-1.2.2 {Rappture::encoding::is, with an embedded null} { 56 56 list [catch {Rappture::encoding::is binary "x\000x"} msg] $msg 57 } {0 yes} 58 59 test 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 63 test 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 68 test 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 73 test 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 78 test 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 83 test 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 88 test 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 93 test 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 98 test 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 103 test 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 108 test 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 57 110 } {0 yes} 58 111 -
trunk/src/core/RpEncode.cc
r1051 r1264 34 34 isbinary(const char* buf, int size) 35 35 { 36 if (buf == NULL) { 37 return 0; 38 } 39 36 40 if (size < 0) { 37 41 size = strlen(buf); … … 51 55 } 52 56 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 71 size_t 72 isencoded(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 } 56 122 57 123 /**********************************************************************/ … … 66 132 67 133 Rappture::Outcome 68 encode (Rappture::Buffer& buf, int flags)134 encode (Rappture::Buffer& buf, size_t flags) 69 135 { 70 136 … … 125 191 126 192 Rappture::Outcome 127 decode (Rappture::Buffer& buf, int flags)193 decode (Rappture::Buffer& buf, size_t flags) 128 194 { 129 195 -
trunk/src/core/RpEncode.h
r1030 r1264 29 29 30 30 int isbinary(const char* buf, int size); 31 Rappture::Outcome encode (Rappture::Buffer& buf, int flags); 32 Rappture::Outcome decode (Rappture::Buffer& buf, int flags); 31 size_t isencoded(const char* buf, int size); 32 Rappture::Outcome encode (Rappture::Buffer& buf, size_t flags); 33 Rappture::Outcome decode (Rappture::Buffer& buf, size_t flags); 33 34 34 35 #ifdef __cplusplus -
trunk/src/core/RpLibrary.cc
r1041 r1264 1457 1457 int myChildCount = 0; 1458 1458 1459 1459 parentNode = NULL; 1460 1460 if (path.empty()) { 1461 1461 // an empty path uses the current RpLibrary as parent … … 1539 1539 1540 1540 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"); 1561 1549 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 } 1562 1566 } 1563 1567 … … 1957 1961 Rappture::Buffer inData; 1958 1962 unsigned int bytesWritten = 0; 1959 int flags = 0;1963 size_t flags = 0; 1960 1964 1961 1965 if (!this->root) {
Note: See TracChangeset
for help on using the changeset viewer.