Changeset 1414 for trunk/src


Ignore:
Timestamp:
Apr 21, 2009, 5:41:50 PM (15 years ago)
Author:
dkearney
Message:

reverting the !isBinary() function back to using if checks. we can possibly use isprint along with isspace in the future. isprint alone thinks spaces, tabs, and newlines are included in the test for binary data. also updaing the encode tests so we have a reference point as to how we think the function should work.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/core/RpEncode.cc

    r1413 r1414  
    3737    const char *cp, *endPtr;
    3838    for (cp = buf, endPtr = buf + size; cp < endPtr; cp++) {
    39         if (!isprint(*cp)) {
    40             return true;
    41         }
     39        if (((*cp >= '\000') && (*cp <= '\010')) ||
     40            ((*cp >= '\013') && (*cp <= '\014')) ||
     41            ((*cp >= '\016') && (*cp <= '\037')) ||
     42            ((*cp >= '\177') && (*cp <= '\377')) ) {
     43            // data is binary
     44            return true;
     45        }
    4246    }
    4347    return false;
     
    5559    const char *cp, *endPtr;
    5660    for (cp = buf, endPtr = buf + size; cp < endPtr; cp++) {
    57         if (((*cp < 'A') || (*cp > '/')) && (!isspace(*cp)) && (*cp != '=')) {
    58             return false;
    59         }
     61        if (((*cp < 'A') || (*cp > '/')) && (!isspace(*cp)) && (*cp != '=')) {
     62            return false;
     63        }
    6064    }
    6165    return true;
     
    136140
    137141bool
    138 Rappture::encoding::encode(Rappture::Outcome &status, Rappture::Buffer& buf, 
    139                            unsigned int flags)
     142Rappture::encoding::encode(Rappture::Outcome &status, Rappture::Buffer& buf,
     143                           unsigned int flags)
    140144{
    141145    Rappture::Buffer outData;
    142146
    143147    if (buf.size() <= 0) {
    144         return true;            // Nothing to encode.
     148        return true;                // Nothing to encode.
    145149    }
    146150    if ((flags & (RPENC_Z | RPENC_B64)) == 0) {
    147         // By default compress and encode the string.
    148         flags |= RPENC_Z | RPENC_B64;
     151        // By default compress and encode the string.
     152        flags |= RPENC_Z | RPENC_B64;
    149153    }
    150154    if (outData.append(buf.bytes(), buf.size()) != (int)buf.size()) {
    151         status.addError("can't append %lu bytes", buf.size());
    152         return false;
     155        status.addError("can't append %lu bytes", buf.size());
     156        return false;
    153157    }
    154158    if (!outData.encode(status, flags)) {
    155         return false;
     159        return false;
    156160    }
    157161    buf.clear();
    158162    if ((flags & RPENC_RAW) == 0) {
    159         switch (flags & (RPENC_Z | RPENC_B64)) {
    160         case RPENC_Z:
    161             buf.append("@@RP-ENC:z\n", 11);
    162             break;
    163         case RPENC_B64:
    164             buf.append("@@RP-ENC:b64\n", 13);
    165             break;
    166         case (RPENC_B64 | RPENC_Z):
    167             buf.append("@@RP-ENC:zb64\n", 14);
    168             break;
    169         default:
    170             break;
    171         }
     163        switch (flags & (RPENC_Z | RPENC_B64)) {
     164        case RPENC_Z:
     165            buf.append("@@RP-ENC:z\n", 11);
     166            break;
     167        case RPENC_B64:
     168            buf.append("@@RP-ENC:b64\n", 13);
     169            break;
     170        case (RPENC_B64 | RPENC_Z):
     171            buf.append("@@RP-ENC:zb64\n", 14);
     172            break;
     173        default:
     174            break;
     175        }
    172176    }
    173177    if (buf.append(outData.bytes(),outData.size()) != (int)outData.size()) {
    174         status.addError("can't append %d bytes", outData.size());
    175         return false;
     178        status.addError("can't append %d bytes", outData.size());
     179        return false;
    176180    }
    177181    return true;
     
    193197
    194198bool
    195 Rappture::encoding::decode(Rappture::Outcome &status, Rappture::Buffer& buf, 
    196                            unsigned int flags)
     199Rappture::encoding::decode(Rappture::Outcome &status, Rappture::Buffer& buf,
     200                           unsigned int flags)
    197201{
    198202    Rappture::Buffer outData;
     
    203207    size = buf.size();
    204208    if (size == 0) {
    205         return true;            // Nothing to decode.
     209        return true;                // Nothing to decode.
    206210    }
    207211    bytes = buf.bytes();
    208212    if ((flags & RPENC_RAW) == 0) {
    209         unsigned int headerFlags = 0;
    210         if ((size > 11) && (strncmp(bytes, "@@RP-ENC:z\n", 11) == 0)) {
    211             bytes += 11;
    212             size -= 11;
    213             headerFlags |= RPENC_Z;
    214         } else if ((size > 13) && (strncmp(bytes, "@@RP-ENC:b64\n", 13) == 0)){
    215             bytes += 13;
    216             size -= 13;
    217             headerFlags |= RPENC_B64;
    218         } else if ((size > 14) && (strncmp(bytes, "@@RP-ENC:zb64\n", 14) == 0)){
    219             bytes += 14;
    220             size -= 14;
    221             headerFlags |= (RPENC_B64 | RPENC_Z);
    222         }
    223         if (headerFlags != 0) {
    224             unsigned int reqFlags;
    225 
    226             reqFlags = flags & (RPENC_B64 | RPENC_Z);
    227             /*
    228              * If there's a header and the programmer also requested decoding
    229              * flags, verify that the two are the same.  We don't want to
    230              * penalize the programmer for over-specifying.  But we need to
    231              * catch cases when they don't match.  If you really want to
    232              * override the header, you should also specify the RPENC_RAW flag
    233              * (-noheader).
    234              */
    235             if ((reqFlags != 0) && (reqFlags != headerFlags)) {
    236                 status.addError("decode flags don't match the header");
    237                 return false;
    238             }
    239             flags |= headerFlags;
    240         }
     213        unsigned int headerFlags = 0;
     214        if ((size > 11) && (strncmp(bytes, "@@RP-ENC:z\n", 11) == 0)) {
     215            bytes += 11;
     216            size -= 11;
     217            headerFlags |= RPENC_Z;
     218        } else if ((size > 13) && (strncmp(bytes, "@@RP-ENC:b64\n", 13) == 0)){
     219            bytes += 13;
     220            size -= 13;
     221            headerFlags |= RPENC_B64;
     222        } else if ((size > 14) && (strncmp(bytes, "@@RP-ENC:zb64\n", 14) == 0)){
     223            bytes += 14;
     224            size -= 14;
     225            headerFlags |= (RPENC_B64 | RPENC_Z);
     226        }
     227         if (headerFlags != 0) {
     228            unsigned int reqFlags;
     229
     230            reqFlags = flags & (RPENC_B64 | RPENC_Z);
     231            /*
     232             * If there's a header and the programmer also requested decoding
     233             * flags, verify that the two are the same.  We don't want to
     234             * penalize the programmer for over-specifying.  But we need to
     235             * catch cases when they don't match.  If you really want to
     236             * override the header, you should also specify the RPENC_RAW flag
     237             * (-noheader).
     238             */
     239            if ((reqFlags != 0) && (reqFlags != headerFlags)) {
     240                status.addError("decode flags don't match the header");
     241                return false;
     242            }
     243            flags |= headerFlags;
     244        }
    241245    }
    242246    if ((flags & (RPENC_B64 | RPENC_Z)) == 0) {
    243         return true;            /* No decode or decompress flags present. */
     247        return true;                /* No decode or decompress flags present. */
    244248    }
    245249    if (outData.append(bytes, size) != (int)size) {
    246         status.addError("can't append %d bytes to buffer", size);
    247         return false;
     250        status.addError("can't append %d bytes to buffer", size);
     251        return false;
    248252    }
    249253    if (!outData.decode(status, flags)) {
    250         return false;
     254        return false;
    251255    }
    252256    buf.move(outData);
    253257    return true;
    254258}
     259
Note: See TracChangeset for help on using the changeset viewer.