michael@0: michael@0: /* michael@0: * Copyright 2006 The Android Open Source Project michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: michael@0: #include "SkBase64.h" michael@0: michael@0: #define DecodePad -2 michael@0: #define EncodePad 64 michael@0: michael@0: static const char default_encode[] = michael@0: "ABCDEFGHIJKLMNOPQRSTUVWXYZ" michael@0: "abcdefghijklmnopqrstuvwxyz" michael@0: "0123456789+/="; michael@0: michael@0: static const signed char decodeData[] = { michael@0: 62, -1, -1, -1, 63, michael@0: 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, DecodePad, -1, -1, michael@0: -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, michael@0: 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, michael@0: -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, michael@0: 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51 michael@0: }; michael@0: michael@0: SkBase64::SkBase64() : fLength((size_t) -1), fData(NULL) { michael@0: } michael@0: michael@0: #if defined _WIN32 && _MSC_VER >= 1300 // disable 'two', etc. may be used without having been initialized michael@0: #pragma warning ( push ) michael@0: #pragma warning ( disable : 4701 ) michael@0: #endif michael@0: michael@0: SkBase64::Error SkBase64::decode(const void* srcPtr, size_t size, bool writeDestination) { michael@0: unsigned char* dst = (unsigned char*) fData; michael@0: const unsigned char* dstStart = (const unsigned char*) fData; michael@0: const unsigned char* src = (const unsigned char*) srcPtr; michael@0: bool padTwo = false; michael@0: bool padThree = false; michael@0: const unsigned char* end = src + size; michael@0: while (src < end) { michael@0: unsigned char bytes[4]; michael@0: int byte = 0; michael@0: do { michael@0: unsigned char srcByte = *src++; michael@0: if (srcByte == 0) michael@0: goto goHome; michael@0: if (srcByte <= ' ') michael@0: continue; // treat as white space michael@0: if (srcByte < '+' || srcByte > 'z') michael@0: return kBadCharError; michael@0: signed char decoded = decodeData[srcByte - '+']; michael@0: bytes[byte] = decoded; michael@0: if (decoded < 0) { michael@0: if (decoded == DecodePad) michael@0: goto handlePad; michael@0: return kBadCharError; michael@0: } else michael@0: byte++; michael@0: if (*src) michael@0: continue; michael@0: if (byte == 0) michael@0: goto goHome; michael@0: if (byte == 4) michael@0: break; michael@0: handlePad: michael@0: if (byte < 2) michael@0: return kPadError; michael@0: padThree = true; michael@0: if (byte == 2) michael@0: padTwo = true; michael@0: break; michael@0: } while (byte < 4); michael@0: int two = 0; michael@0: int three = 0; michael@0: if (writeDestination) { michael@0: int one = (uint8_t) (bytes[0] << 2); michael@0: two = bytes[1]; michael@0: one |= two >> 4; michael@0: two = (uint8_t) (two << 4); michael@0: three = bytes[2]; michael@0: two |= three >> 2; michael@0: three = (uint8_t) (three << 6); michael@0: three |= bytes[3]; michael@0: SkASSERT(one < 256 && two < 256 && three < 256); michael@0: *dst = (unsigned char) one; michael@0: } michael@0: dst++; michael@0: if (padTwo) michael@0: break; michael@0: if (writeDestination) michael@0: *dst = (unsigned char) two; michael@0: dst++; michael@0: if (padThree) michael@0: break; michael@0: if (writeDestination) michael@0: *dst = (unsigned char) three; michael@0: dst++; michael@0: } michael@0: goHome: michael@0: fLength = dst - dstStart; michael@0: return kNoError; michael@0: } michael@0: michael@0: #if defined _WIN32 && _MSC_VER >= 1300 michael@0: #pragma warning ( pop ) michael@0: #endif michael@0: michael@0: size_t SkBase64::Encode(const void* srcPtr, size_t length, void* dstPtr, const char* encodeMap) { michael@0: const char* encode; michael@0: if (NULL == encodeMap) { michael@0: encode = default_encode; michael@0: } else { michael@0: encode = encodeMap; michael@0: } michael@0: const unsigned char* src = (const unsigned char*) srcPtr; michael@0: unsigned char* dst = (unsigned char*) dstPtr; michael@0: if (dst) { michael@0: size_t remainder = length % 3; michael@0: const unsigned char* end = &src[length - remainder]; michael@0: while (src < end) { michael@0: unsigned a = *src++; michael@0: unsigned b = *src++; michael@0: unsigned c = *src++; michael@0: int d = c & 0x3F; michael@0: c = (c >> 6 | b << 2) & 0x3F; michael@0: b = (b >> 4 | a << 4) & 0x3F; michael@0: a = a >> 2; michael@0: *dst++ = encode[a]; michael@0: *dst++ = encode[b]; michael@0: *dst++ = encode[c]; michael@0: *dst++ = encode[d]; michael@0: } michael@0: if (remainder > 0) { michael@0: int k1 = 0; michael@0: int k2 = EncodePad; michael@0: int a = (uint8_t) *src++; michael@0: if (remainder == 2) michael@0: { michael@0: int b = *src++; michael@0: k1 = b >> 4; michael@0: k2 = (b << 2) & 0x3F; michael@0: } michael@0: *dst++ = encode[a >> 2]; michael@0: *dst++ = encode[(k1 | a << 4) & 0x3F]; michael@0: *dst++ = encode[k2]; michael@0: *dst++ = encode[EncodePad]; michael@0: } michael@0: } michael@0: return (length + 2) / 3 * 4; michael@0: } michael@0: michael@0: SkBase64::Error SkBase64::decode(const char* src, size_t len) { michael@0: Error err = decode(src, len, false); michael@0: SkASSERT(err == kNoError); michael@0: if (err != kNoError) michael@0: return err; michael@0: fData = new char[fLength]; // should use sk_malloc/sk_free michael@0: decode(src, len, true); michael@0: return kNoError; michael@0: }