diff options
Diffstat (limited to 'libxsde/xsde/cxx/parser/non-validating/base64-binary.cxx')
-rw-r--r-- | libxsde/xsde/cxx/parser/non-validating/base64-binary.cxx | 97 |
1 files changed, 50 insertions, 47 deletions
diff --git a/libxsde/xsde/cxx/parser/non-validating/base64-binary.cxx b/libxsde/xsde/cxx/parser/non-validating/base64-binary.cxx index ac5de47..be48cb0 100644 --- a/libxsde/xsde/cxx/parser/non-validating/base64-binary.cxx +++ b/libxsde/xsde/cxx/parser/non-validating/base64-binary.cxx @@ -180,10 +180,10 @@ namespace xsde str_.truncate (size); } - // Our length should be a multiple of four. + // Our length should be a multiple of four but it can also be 0. // size_type quad_count = size / 4; - size_type capacity = quad_count * 3 + 1; + size_type capacity = quad_count != 0 ? quad_count * 3 + 1 : 0; #ifdef XSDE_EXCEPTIONS buf_->size (capacity); @@ -194,71 +194,74 @@ namespace xsde return 0; } #endif - char* dst = buf_->data (); + if (quad_count != 0) + { + char* dst = buf_->data (); - // Source and destination indexes. - // - size_type si = 0; - size_type di = 0; + // Source and destination indexes. + // + size_type si = 0; + size_type di = 0; - // Process all quads except the last one. - // - unsigned char b1, b2, b3, b4; + // Process all quads except the last one. + // + unsigned char b1, b2, b3, b4; - for (size_type q = 0; q < quad_count - 1; ++q) - { - b1 = base64_decode (src[si++]); - b2 = base64_decode (src[si++]); - b3 = base64_decode (src[si++]); - b4 = base64_decode (src[si++]); + for (size_type q = 0; q < quad_count - 1; ++q) + { + b1 = base64_decode (src[si++]); + b2 = base64_decode (src[si++]); + b3 = base64_decode (src[si++]); + b4 = base64_decode (src[si++]); - dst[di++] = (b1 << 2) | (b2 >> 4); - dst[di++] = (b2 << 4) | (b3 >> 2); - dst[di++] = (b3 << 6) | b4; - } + dst[di++] = (b1 << 2) | (b2 >> 4); + dst[di++] = (b2 << 4) | (b3 >> 2); + dst[di++] = (b3 << 6) | b4; + } - // Process the last quad. The first two octets are always there. - // - b1 = base64_decode (src[si++]); - b2 = base64_decode (src[si++]); + // Process the last quad. The first two octets are always there. + // + b1 = base64_decode (src[si++]); + b2 = base64_decode (src[si++]); - char e3 = src[si++]; - char e4 = src[si++]; + char e3 = src[si++]; + char e4 = src[si++]; - if (e4 == '=') - { - if (e3 == '=') + if (e4 == '=') { - // Two pads. Last 4 bits in b2 should be zero. - // - dst[di++] = (b1 << 2) | (b2 >> 4); + if (e3 == '=') + { + // Two pads. Last 4 bits in b2 should be zero. + // + dst[di++] = (b1 << 2) | (b2 >> 4); + } + else + { + // One pad. Last 2 bits in b3 should be zero. + // + b3 = base64_decode (e3); + + dst[di++] = (b1 << 2) | (b2 >> 4); + dst[di++] = (b2 << 4) | (b3 >> 2); + } } else { - // One pad. Last 2 bits in b3 should be zero. + // No pads. // b3 = base64_decode (e3); + b4 = base64_decode (e4); dst[di++] = (b1 << 2) | (b2 >> 4); dst[di++] = (b2 << 4) | (b3 >> 2); + dst[di++] = (b3 << 6) | b4; } - } - else - { - // No pads. - // - b3 = base64_decode (e3); - b4 = base64_decode (e4); - dst[di++] = (b1 << 2) | (b2 >> 4); - dst[di++] = (b2 << 4) | (b3 >> 2); - dst[di++] = (b3 << 6) | b4; + // Set the real size. + // + buf_->size (di); } - // Set the real size. - // - buf_->size (di); - buffer* r = buf_; buf_ = 0; return r; |