Changeset 1366 for trunk/src/core/RpEncode.cc
- Timestamp:
- Mar 27, 2009 4:54:09 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/core/RpEncode.cc
r1264 r1366 15 15 #include <cstring> 16 16 17 #ifdef __cplusplus18 extern "C" {19 #endif // ifdef __cplusplus20 21 using namespace Rappture::encoding;22 23 17 /**********************************************************************/ 24 18 // FUNCTION: Rappture::encoding::isbinary() … … 32 26 33 27 int 34 isbinary(const char* buf, int size)28 Rappture::encoding::isbinary(const char* buf, int size) 35 29 { 36 30 if (buf == NULL) { … … 70 64 71 65 size_t 72 isencoded(const char* buf, int size)66 Rappture::encoding::isencoded(const char* buf, int size) 73 67 { 74 68 size_t flags = 0; … … 117 111 } 118 112 } 119 120 113 return flags; 121 114 } … … 131 124 */ 132 125 133 Rappture::Outcome 134 encode (Rappture::Buffer& buf, size_t flags) 135 { 136 137 int compress = 0; 138 int base64 = 0; 139 int addHeader = 0; 140 Rappture::Outcome err; 126 bool 127 Rappture::encoding::encode(Rappture::Outcome &err, Rappture::Buffer& buf, 128 size_t flags) 129 { 141 130 Rappture::Buffer outData; 142 131 143 if ((flags & RPENC_Z) == RPENC_Z ) { 144 compress = 1; 145 } 146 if ((flags & RPENC_B64) == RPENC_B64 ) { 147 base64 = 1; 148 } 149 if ((flags & RPENC_HDR) == RPENC_HDR ) { 150 addHeader = 1; 151 } 152 153 outData.append(buf.bytes(),buf.size()); 154 err = outData.encode(compress,base64); 155 if (!err) { 132 bool compress, base64, addHeader; 133 compress = (flags & RPENC_Z); 134 base64 = (flags & RPENC_B64); 135 addHeader = (flags & RPENC_HDR); 136 137 if (outData.append(buf.bytes(), buf.size()) != (int)buf.size()) { 138 err.addError("can't append %d bytes", buf.size()); 139 return false; 140 } 141 if (outData.encode(err, compress, base64)) { 156 142 buf.clear(); 157 if (addHeader == 1) { 158 if ((compress == 1) && (base64 == 0)) { 159 buf.append("@@RP-ENC:z\n",11); 160 } 161 else if ((compress == 0) && (base64 == 1)) { 162 buf.append("@@RP-ENC:b64\n",13); 163 } 164 else if ((compress == 1) && (base64 == 1)) { 165 buf.append("@@RP-ENC:zb64\n",14); 166 } 167 else { 143 if (addHeader) { 144 if ((compress) && (!base64)) { 145 buf.append("@@RP-ENC:z\n", 11); 146 } else if ((!compress) && (base64)) { 147 buf.append("@@RP-ENC:b64\n", 13); 148 } else if ((compress) && (base64)) { 149 buf.append("@@RP-ENC:zb64\n", 14); 150 } else { 168 151 // do nothing 169 152 } 170 } 171 else { 153 } else { 172 154 // do nothing 173 155 } 174 buf.append(outData.bytes(),outData.size()); 175 } 176 177 return err; 156 if (buf.append(outData.bytes(),outData.size()) != (int)outData.size()) { 157 err.addError("can't append %d bytes", outData.size()); 158 return false; 159 } 160 } 161 return true; 178 162 } 179 163 … … 190 174 */ 191 175 192 Rappture::Outcome 193 decode (Rappture::Buffer& buf, size_t flags) 194 { 195 196 int decompress = 0; 197 int base64 = 0; 198 int checkHDR = 0; 199 Rappture::Outcome err; 176 bool 177 Rappture::encoding::decode(Rappture::Outcome &err, Rappture::Buffer& buf, 178 size_t flags) 179 { 200 180 Rappture::Buffer outData; 201 181 202 if ((flags & RPENC_Z) == RPENC_Z ) { 203 decompress = 1; 204 } 205 if ((flags & RPENC_B64) == RPENC_B64 ) { 206 base64 = 1; 207 } 208 if ((flags & RPENC_HDR) == RPENC_HDR ) { 209 checkHDR = 1; 210 } 211 212 if ((buf.size() > 11) && (strncmp(buf.bytes(),"@@RP-ENC:z\n",11) == 0)) { 213 outData.append(buf.bytes()+11,buf.size()-11); 214 if ( (checkHDR == 1) || ( (decompress == 0) && (base64 == 0) ) ) { 215 decompress = 1; 216 base64 = 0; 217 } 218 } 219 else if ((buf.size() > 13) && (strncmp(buf.bytes(),"@@RP-ENC:b64\n",13) == 0)) { 220 outData.append(buf.bytes()+13,buf.size()-13); 221 if ( (checkHDR == 1) || ( (decompress == 0) && (base64 == 0) ) ) { 222 decompress = 0; 223 base64 = 1; 224 } 225 } 226 else if ((buf.size() > 14) && (strncmp(buf.bytes(),"@@RP-ENC:zb64\n",14) == 0)) { 227 outData.append(buf.bytes()+14,buf.size()-14); 228 if ( (checkHDR == 1) || ( (decompress == 0) && (base64 == 0) ) ) { 229 decompress = 1; 230 base64 = 1; 231 } 232 } 233 else { 234 // no special recognized tags 235 outData.append(buf.bytes(),buf.size()); 236 } 237 238 err = outData.decode(decompress,base64); 239 if (!err) { 240 buf.move(outData); 241 } 242 243 return err; 244 } 245 246 #ifdef __cplusplus 247 } 248 #endif // ifdef __cplusplus 182 bool decompress, base64, checkHDR; 183 decompress = (flags & RPENC_Z); 184 base64 = (flags & RPENC_B64); 185 checkHDR = (flags & RPENC_HDR); 186 187 off_t offset; 188 if ((buf.size() > 11) && (strncmp(buf.bytes(),"@@RP-ENC:z\n", 11) == 0)) { 189 offset = 11; 190 if ((checkHDR) || ((!decompress) && (!base64))) { 191 decompress = true; 192 base64 = false; 193 } 194 } else if ((buf.size() > 13) && 195 (strncmp(buf.bytes(),"@@RP-ENC:b64\n",13) == 0)) { 196 offset = 13; 197 if ((checkHDR) || ((!decompress) && (!base64) )) { 198 decompress = false; 199 base64 = true; 200 } 201 } else if ((buf.size() > 14) && 202 (strncmp(buf.bytes(), "@@RP-ENC:zb64\n",14) == 0)) { 203 offset = 14; 204 if ((checkHDR) || ((!decompress) && (!base64))) { 205 decompress = true; 206 base64 = true; 207 } 208 } else { 209 offset = 0; 210 } 211 int nBytes = buf.size() - offset; 212 if (outData.append(buf.bytes() + offset, nBytes) != nBytes) { 213 err.addError("can't append %d bytes to buffer", nBytes); 214 return false; 215 } 216 if (!outData.decode(err, decompress, base64)) { 217 return false; 218 } 219 buf.move(outData); 220 return true; 221 } 222
Note: See TracChangeset
for help on using the changeset viewer.