Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 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 | unit Thrift.Utils; |
| 21 | |
| 22 | interface |
| 23 | |
| 24 | uses |
| 25 | Classes, Windows, SysUtils, SyncObjs; |
| 26 | |
| 27 | type |
| 28 | IOverlappedHelper = interface |
| 29 | ['{A1832EFA-2E02-4884-8F09-F0A0277157FA}'] |
| 30 | function Overlapped : TOverlapped; |
| 31 | function OverlappedPtr : POverlapped; |
| 32 | function WaitHandle : THandle; |
| 33 | function WaitFor(dwTimeout: DWORD) : DWORD; |
| 34 | end; |
| 35 | |
| 36 | TOverlappedHelperImpl = class( TInterfacedObject, IOverlappedHelper) |
| 37 | strict protected |
| 38 | FOverlapped : TOverlapped; |
| 39 | FEvent : TEvent; |
| 40 | |
| 41 | // IOverlappedHelper |
| 42 | function Overlapped : TOverlapped; |
| 43 | function OverlappedPtr : POverlapped; |
| 44 | function WaitHandle : THandle; |
| 45 | function WaitFor(dwTimeout: DWORD) : DWORD; |
| 46 | public |
| 47 | constructor Create; |
| 48 | destructor Destroy; override; |
| 49 | end; |
| 50 | |
| 51 | |
Jens Geyer | d8bddbc | 2014-12-14 00:41:33 +0100 | [diff] [blame] | 52 | Base64Utils = class sealed |
| 53 | public |
| 54 | class function Encode( const src : TBytes; srcOff, len : Integer; dst : TBytes; dstOff : Integer) : Integer; static; |
| 55 | class function Decode( const src : TBytes; srcOff, len : Integer; dst : TBytes; dstOff : Integer) : Integer; static; |
| 56 | end; |
| 57 | |
| 58 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 59 | implementation |
| 60 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 61 | { TOverlappedHelperImpl } |
| 62 | |
| 63 | constructor TOverlappedHelperImpl.Create; |
| 64 | begin |
| 65 | inherited Create; |
| 66 | FillChar( FOverlapped, SizeOf(FOverlapped), 0); |
| 67 | FEvent := TEvent.Create( nil, TRUE, FALSE, ''); // always ManualReset, see MSDN |
| 68 | FOverlapped.hEvent := FEvent.Handle; |
| 69 | end; |
| 70 | |
| 71 | |
| 72 | |
| 73 | destructor TOverlappedHelperImpl.Destroy; |
| 74 | begin |
| 75 | try |
| 76 | FOverlapped.hEvent := 0; |
| 77 | FreeAndNil( FEvent); |
| 78 | |
| 79 | finally |
| 80 | inherited Destroy; |
| 81 | end; |
| 82 | |
| 83 | end; |
| 84 | |
| 85 | |
| 86 | function TOverlappedHelperImpl.Overlapped : TOverlapped; |
| 87 | begin |
| 88 | result := FOverlapped; |
| 89 | end; |
| 90 | |
| 91 | |
| 92 | function TOverlappedHelperImpl.OverlappedPtr : POverlapped; |
| 93 | begin |
| 94 | result := @FOverlapped; |
| 95 | end; |
| 96 | |
| 97 | |
| 98 | function TOverlappedHelperImpl.WaitHandle : THandle; |
| 99 | begin |
| 100 | result := FOverlapped.hEvent; |
| 101 | end; |
| 102 | |
| 103 | |
| 104 | function TOverlappedHelperImpl.WaitFor( dwTimeout : DWORD) : DWORD; |
| 105 | begin |
| 106 | result := WaitForSingleObject( FOverlapped.hEvent, dwTimeout); |
| 107 | end; |
| 108 | |
| 109 | |
Jens Geyer | d8bddbc | 2014-12-14 00:41:33 +0100 | [diff] [blame] | 110 | { Base64Utils } |
| 111 | |
| 112 | class function Base64Utils.Encode( const src : TBytes; srcOff, len : Integer; dst : TBytes; dstOff : Integer) : Integer; |
| 113 | const ENCODE_TABLE : PAnsiChar = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; |
| 114 | begin |
| 115 | ASSERT( len in [1..3]); |
| 116 | dst[dstOff] := Byte( ENCODE_TABLE[ (src[srcOff] shr 2) and $3F]); |
| 117 | case len of |
| 118 | 3 : begin |
| 119 | Inc(dstOff); |
| 120 | dst[dstOff] := Byte( ENCODE_TABLE[ ((src[srcOff] shl 4) and $30) or ((src[srcOff + 1] shr 4) and $0F)]); |
| 121 | Inc(dstOff); |
| 122 | dst[dstOff] := Byte( ENCODE_TABLE[ ((src[srcOff + 1] shl 2) and $3C) or ((src[srcOff + 2] shr 6) and $03)]); |
| 123 | Inc(dstOff); |
| 124 | dst[dstOff] := Byte( ENCODE_TABLE[ src[srcOff + 2] and $3F]); |
| 125 | result := 4; |
| 126 | end; |
| 127 | |
| 128 | 2 : begin |
| 129 | Inc(dstOff); |
| 130 | dst[dstOff] := Byte( ENCODE_TABLE[ ((src[srcOff] shl 4) and $30) or ((src[srcOff + 1] shr 4) and $0F)]); |
| 131 | Inc(dstOff); |
| 132 | dst[dstOff] := Byte( ENCODE_TABLE[ (src[srcOff + 1] shl 2) and $3C]); |
| 133 | result := 3; |
| 134 | end; |
| 135 | |
| 136 | 1 : begin |
| 137 | Inc(dstOff); |
| 138 | dst[dstOff] := Byte( ENCODE_TABLE[ (src[srcOff] shl 4) and $30]); |
| 139 | result := 2; |
| 140 | end; |
| 141 | |
| 142 | else |
| 143 | ASSERT( FALSE); |
Jens Geyer | 9f9535c | 2014-12-14 04:16:05 +0100 | [diff] [blame^] | 144 | result := 0; // because invalid call |
Jens Geyer | d8bddbc | 2014-12-14 00:41:33 +0100 | [diff] [blame] | 145 | end; |
| 146 | end; |
| 147 | |
| 148 | |
| 149 | class function Base64Utils.Decode( const src : TBytes; srcOff, len : Integer; dst : TBytes; dstOff : Integer) : Integer; |
| 150 | const DECODE_TABLE : array[0..$FF] of Integer |
| 151 | = ( -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 152 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 153 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, |
| 154 | 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, |
| 155 | -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, |
| 156 | 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, |
| 157 | -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, |
| 158 | 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, |
| 159 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 160 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 161 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 162 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 163 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 164 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 165 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 166 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 ); |
| 167 | begin |
| 168 | ASSERT( len in [1..4]); |
| 169 | result := 1; |
| 170 | dst[dstOff] := ((DECODE_TABLE[src[srcOff] and $0FF] shl 2) |
| 171 | or (DECODE_TABLE[src[srcOff + 1] and $0FF] shr 4)); |
| 172 | |
| 173 | if (len > 2) then begin |
| 174 | Inc( result); |
| 175 | Inc( dstOff); |
| 176 | dst[dstOff] := (((DECODE_TABLE[src[srcOff + 1] and $0FF] shl 4) and $F0) |
| 177 | or (DECODE_TABLE[src[srcOff + 2] and $0FF] shr 2)); |
| 178 | |
| 179 | if (len > 3) then begin |
| 180 | Inc( result); |
| 181 | Inc( dstOff); |
| 182 | dst[dstOff] := (((DECODE_TABLE[src[srcOff + 2] and $0FF] shl 6) and $C0) |
| 183 | or DECODE_TABLE[src[srcOff + 3] and $0FF]); |
| 184 | end; |
| 185 | end; |
| 186 | end; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 187 | |
| 188 | |
| 189 | end. |