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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
22 namespace Thrift.Protocol
24 internal static class TBase64Utils
26 internal const string ENCODE_TABLE =
27 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
29 internal static void encode(byte[] src, int srcOff, int len, byte[] dst,
32 dst[dstOff] = (byte)ENCODE_TABLE[(src[srcOff] >> 2) & 0x3F];
37 ((src[srcOff] << 4) & 0x30) | ((src[srcOff + 1] >> 4) & 0x0F)];
40 ((src[srcOff + 1] << 2) & 0x3C) | ((src[srcOff + 2] >> 6) & 0x03)];
42 (byte)ENCODE_TABLE[src[srcOff + 2] & 0x3F];
48 ((src[srcOff] << 4) & 0x30) | ((src[srcOff + 1] >> 4) & 0x0F)];
50 (byte)ENCODE_TABLE[(src[srcOff + 1] << 2) & 0x3C];
56 (byte)ENCODE_TABLE[(src[srcOff] << 4) & 0x30];
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,
79 internal static void decode(byte[] src, int srcOff, int len, byte[] dst,
83 ((DECODE_TABLE[src[srcOff] & 0x0FF] << 2) |
84 (DECODE_TABLE[src[srcOff + 1] & 0x0FF] >> 4));
87 dst[dstOff + 1] = (byte)
88 (((DECODE_TABLE[src[srcOff + 1] & 0x0FF] << 4) & 0xF0) |
89 (DECODE_TABLE[src[srcOff + 2] & 0x0FF] >> 2));
92 dst[dstOff + 2] = (byte)
93 (((DECODE_TABLE[src[srcOff + 2] & 0x0FF] << 6) & 0xC0) |
94 DECODE_TABLE[src[srcOff + 3] & 0x0FF]);