1 /** 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 20 using System; 21 22 namespace Thrift.Protocol 23 { 24 internal static class TBase64Utils 25 { 26 internal const string ENCODE_TABLE = 27 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 28 encode(byte[] src, int srcOff, int len, byte[] dst, int dstOff)29 internal static void encode(byte[] src, int srcOff, int len, byte[] dst, 30 int dstOff) 31 { 32 dst[dstOff] = (byte)ENCODE_TABLE[(src[srcOff] >> 2) & 0x3F]; 33 if (len == 3) 34 { 35 dst[dstOff + 1] = 36 (byte)ENCODE_TABLE[ 37 ((src[srcOff] << 4) & 0x30) | ((src[srcOff + 1] >> 4) & 0x0F)]; 38 dst[dstOff + 2] = 39 (byte)ENCODE_TABLE[ 40 ((src[srcOff + 1] << 2) & 0x3C) | ((src[srcOff + 2] >> 6) & 0x03)]; 41 dst[dstOff + 3] = 42 (byte)ENCODE_TABLE[src[srcOff + 2] & 0x3F]; 43 } 44 else if (len == 2) 45 { 46 dst[dstOff + 1] = 47 (byte)ENCODE_TABLE[ 48 ((src[srcOff] << 4) & 0x30) | ((src[srcOff + 1] >> 4) & 0x0F)]; 49 dst[dstOff + 2] = 50 (byte)ENCODE_TABLE[(src[srcOff + 1] << 2) & 0x3C]; 51 52 } 53 else 54 { // len == 1) { 55 dst[dstOff + 1] = 56 (byte)ENCODE_TABLE[(src[srcOff] << 4) & 0x30]; 57 } 58 } 59 60 private static int[] DECODE_TABLE = { 61 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 62 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 63 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, 64 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, 65 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, 66 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, 67 -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, 68 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, 69 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 70 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 71 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 72 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 73 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 74 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 75 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 76 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, 77 }; 78 decode(byte[] src, int srcOff, int len, byte[] dst, int dstOff)79 internal static void decode(byte[] src, int srcOff, int len, byte[] dst, 80 int dstOff) 81 { 82 dst[dstOff] = (byte) 83 ((DECODE_TABLE[src[srcOff] & 0x0FF] << 2) | 84 (DECODE_TABLE[src[srcOff + 1] & 0x0FF] >> 4)); 85 if (len > 2) 86 { 87 dst[dstOff + 1] = (byte) 88 (((DECODE_TABLE[src[srcOff + 1] & 0x0FF] << 4) & 0xF0) | 89 (DECODE_TABLE[src[srcOff + 2] & 0x0FF] >> 2)); 90 if (len > 3) 91 { 92 dst[dstOff + 2] = (byte) 93 (((DECODE_TABLE[src[srcOff + 2] & 0x0FF] << 6) & 0xC0) | 94 DECODE_TABLE[src[srcOff + 3] & 0x0FF]); 95 } 96 } 97 } 98 99 } 100 } 101