OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2009, Google Inc. |
| 3 * All rights reserved. |
| 4 * |
| 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions are |
| 7 * met: |
| 8 * |
| 9 * * Redistributions of source code must retain the above copyright |
| 10 * notice, this list of conditions and the following disclaimer. |
| 11 * * Redistributions in binary form must reproduce the above |
| 12 * copyright notice, this list of conditions and the following disclaimer |
| 13 * in the documentation and/or other materials provided with the |
| 14 * distribution. |
| 15 * * Neither the name of Google Inc. nor the names of its |
| 16 * contributors may be used to endorse or promote products derived from |
| 17 * this software without specific prior written permission. |
| 18 * |
| 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 */ |
| 31 |
| 32 // This file contains the declaration of functions for dealing with base64 |
| 33 // encoding and decoding |
| 34 |
| 35 #include "utils/cross/base64.h" |
| 36 #include "core/cross/types.h" |
| 37 |
| 38 namespace o3d { |
| 39 namespace base64 { |
| 40 |
| 41 size_t GetEncodeLength(size_t length) { |
| 42 return (length + 2) / 3 * 4; |
| 43 } |
| 44 |
| 45 void Encode(const void* src_ptr, size_t length, void* dst_ptr) { |
| 46 const int kEncodePad = 64; |
| 47 |
| 48 static const char kEncode[] = |
| 49 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 50 "abcdefghijklmnopqrstuvwxyz" |
| 51 "0123456789+/="; |
| 52 |
| 53 const uint8* src = reinterpret_cast<const uint8*>(src_ptr); |
| 54 uint8* dst = reinterpret_cast<uint8*>(dst_ptr); |
| 55 if (dst) { |
| 56 size_t remainder = length % 3; |
| 57 const uint8* end = &src[length - remainder]; |
| 58 while (src < end) { |
| 59 unsigned a = *src++; |
| 60 unsigned b = *src++; |
| 61 unsigned c = *src++; |
| 62 unsigned d = c & 0x3F; |
| 63 c = (c >> 6 | b << 2) & 0x3F; |
| 64 b = (b >> 4 | a << 4) & 0x3F; |
| 65 a = a >> 2; |
| 66 *dst++ = kEncode[a]; |
| 67 *dst++ = kEncode[b]; |
| 68 *dst++ = kEncode[c]; |
| 69 *dst++ = kEncode[d]; |
| 70 } |
| 71 if (remainder > 0) { |
| 72 unsigned k1 = 0; |
| 73 unsigned k2 = kEncodePad; |
| 74 unsigned a = *src++; |
| 75 if (remainder == 2) { |
| 76 int b = *src++; |
| 77 k1 = b >> 4; |
| 78 k2 = (b << 2) & 0x3F; |
| 79 } |
| 80 *dst++ = kEncode[a >> 2]; |
| 81 *dst++ = kEncode[(k1 | a << 4) & 0x3F]; |
| 82 *dst++ = kEncode[k2]; |
| 83 *dst++ = kEncode[kEncodePad]; |
| 84 } |
| 85 } |
| 86 } |
| 87 |
| 88 } // namespace base64 |
| 89 } // namespace o3d |
| 90 |
OLD | NEW |