blob: a0bf144973bd411bce4c8ca809a1cf58de2c395d [file] [log] [blame]
Jens Geyerd5436f52014-10-03 19:50:38 +02001(*
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
20unit Thrift.Utils;
21
22interface
23
Jens Geyer9f7f11e2016-04-14 21:37:11 +020024{$I Thrift.Defines.inc}
Nick4f5229e2016-04-14 16:43:22 +030025
Jens Geyerd5436f52014-10-03 19:50:38 +020026uses
Jens Geyer9f7f11e2016-04-14 21:37:11 +020027 {$IFDEF OLD_UNIT_NAMES}
Jens Geyer71070432016-01-29 10:08:39 +010028 Classes, Windows, SysUtils, Character, SyncObjs;
Jens Geyer9f7f11e2016-04-14 21:37:11 +020029 {$ELSE}
Nick4f5229e2016-04-14 16:43:22 +030030 System.Classes, Winapi.Windows, System.SysUtils, System.Character, System.SyncObjs;
Jens Geyer9f7f11e2016-04-14 21:37:11 +020031 {$ENDIF}
Jens Geyerd5436f52014-10-03 19:50:38 +020032
33type
34 IOverlappedHelper = interface
35 ['{A1832EFA-2E02-4884-8F09-F0A0277157FA}']
36 function Overlapped : TOverlapped;
37 function OverlappedPtr : POverlapped;
38 function WaitHandle : THandle;
39 function WaitFor(dwTimeout: DWORD) : DWORD;
40 end;
41
42 TOverlappedHelperImpl = class( TInterfacedObject, IOverlappedHelper)
43 strict protected
44 FOverlapped : TOverlapped;
45 FEvent : TEvent;
46
47 // IOverlappedHelper
48 function Overlapped : TOverlapped;
49 function OverlappedPtr : POverlapped;
50 function WaitHandle : THandle;
51 function WaitFor(dwTimeout: DWORD) : DWORD;
52 public
53 constructor Create;
54 destructor Destroy; override;
55 end;
56
57
Jens Geyerd8bddbc2014-12-14 00:41:33 +010058 Base64Utils = class sealed
59 public
60 class function Encode( const src : TBytes; srcOff, len : Integer; dst : TBytes; dstOff : Integer) : Integer; static;
61 class function Decode( const src : TBytes; srcOff, len : Integer; dst : TBytes; dstOff : Integer) : Integer; static;
62 end;
63
64
Jens Geyer71070432016-01-29 10:08:39 +010065 CharUtils = class sealed
66 public
67 class function IsHighSurrogate( const c : Char) : Boolean; static; inline;
68 class function IsLowSurrogate( const c : Char) : Boolean; static; inline;
69 end;
70
71
Jens Geyer9f7f11e2016-04-14 21:37:11 +020072function InterlockedCompareExchange64( var Target : Int64; Exchange, Comparand : Int64) : Int64; stdcall;
73function InterlockedExchangeAdd64( var Addend : Int64; Value : Int64) : Int64; stdcall;
74
Jens Geyer71070432016-01-29 10:08:39 +010075
Jens Geyerd5436f52014-10-03 19:50:38 +020076implementation
77
Jens Geyerd5436f52014-10-03 19:50:38 +020078{ TOverlappedHelperImpl }
79
80constructor TOverlappedHelperImpl.Create;
81begin
82 inherited Create;
83 FillChar( FOverlapped, SizeOf(FOverlapped), 0);
84 FEvent := TEvent.Create( nil, TRUE, FALSE, ''); // always ManualReset, see MSDN
85 FOverlapped.hEvent := FEvent.Handle;
86end;
87
88
89
90destructor TOverlappedHelperImpl.Destroy;
91begin
92 try
93 FOverlapped.hEvent := 0;
94 FreeAndNil( FEvent);
95
96 finally
97 inherited Destroy;
98 end;
99
100end;
101
102
103function TOverlappedHelperImpl.Overlapped : TOverlapped;
104begin
105 result := FOverlapped;
106end;
107
108
109function TOverlappedHelperImpl.OverlappedPtr : POverlapped;
110begin
111 result := @FOverlapped;
112end;
113
114
115function TOverlappedHelperImpl.WaitHandle : THandle;
116begin
117 result := FOverlapped.hEvent;
118end;
119
120
121function TOverlappedHelperImpl.WaitFor( dwTimeout : DWORD) : DWORD;
122begin
123 result := WaitForSingleObject( FOverlapped.hEvent, dwTimeout);
124end;
125
126
Jens Geyerd8bddbc2014-12-14 00:41:33 +0100127{ Base64Utils }
128
129class function Base64Utils.Encode( const src : TBytes; srcOff, len : Integer; dst : TBytes; dstOff : Integer) : Integer;
130const ENCODE_TABLE : PAnsiChar = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
131begin
132 ASSERT( len in [1..3]);
133 dst[dstOff] := Byte( ENCODE_TABLE[ (src[srcOff] shr 2) and $3F]);
134 case len of
135 3 : begin
136 Inc(dstOff);
137 dst[dstOff] := Byte( ENCODE_TABLE[ ((src[srcOff] shl 4) and $30) or ((src[srcOff + 1] shr 4) and $0F)]);
138 Inc(dstOff);
139 dst[dstOff] := Byte( ENCODE_TABLE[ ((src[srcOff + 1] shl 2) and $3C) or ((src[srcOff + 2] shr 6) and $03)]);
140 Inc(dstOff);
141 dst[dstOff] := Byte( ENCODE_TABLE[ src[srcOff + 2] and $3F]);
142 result := 4;
143 end;
144
145 2 : begin
146 Inc(dstOff);
147 dst[dstOff] := Byte( ENCODE_TABLE[ ((src[srcOff] shl 4) and $30) or ((src[srcOff + 1] shr 4) and $0F)]);
148 Inc(dstOff);
149 dst[dstOff] := Byte( ENCODE_TABLE[ (src[srcOff + 1] shl 2) and $3C]);
150 result := 3;
151 end;
152
153 1 : begin
154 Inc(dstOff);
155 dst[dstOff] := Byte( ENCODE_TABLE[ (src[srcOff] shl 4) and $30]);
156 result := 2;
157 end;
158
159 else
160 ASSERT( FALSE);
Jens Geyer9f9535c2014-12-14 04:16:05 +0100161 result := 0; // because invalid call
Jens Geyerd8bddbc2014-12-14 00:41:33 +0100162 end;
163end;
164
165
166class function Base64Utils.Decode( const src : TBytes; srcOff, len : Integer; dst : TBytes; dstOff : Integer) : Integer;
167const DECODE_TABLE : array[0..$FF] of Integer
168 = ( -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
169 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
170 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63,
171 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1,
172 -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,
173 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,
174 -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,
175 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1,
176 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
177 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
178 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
179 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
180 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
181 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
182 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
183 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 );
184begin
185 ASSERT( len in [1..4]);
186 result := 1;
187 dst[dstOff] := ((DECODE_TABLE[src[srcOff] and $0FF] shl 2)
188 or (DECODE_TABLE[src[srcOff + 1] and $0FF] shr 4));
189
190 if (len > 2) then begin
191 Inc( result);
192 Inc( dstOff);
193 dst[dstOff] := (((DECODE_TABLE[src[srcOff + 1] and $0FF] shl 4) and $F0)
194 or (DECODE_TABLE[src[srcOff + 2] and $0FF] shr 2));
195
196 if (len > 3) then begin
197 Inc( result);
198 Inc( dstOff);
199 dst[dstOff] := (((DECODE_TABLE[src[srcOff + 2] and $0FF] shl 6) and $C0)
200 or DECODE_TABLE[src[srcOff + 3] and $0FF]);
201 end;
202 end;
203end;
Jens Geyerd5436f52014-10-03 19:50:38 +0200204
205
Jens Geyer71070432016-01-29 10:08:39 +0100206class function CharUtils.IsHighSurrogate( const c : Char) : Boolean;
207begin
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200208 {$IF CompilerVersion < 23.0}
209 result := Character.IsHighSurrogate( c);
Jens Geyer71070432016-01-29 10:08:39 +0100210 {$ELSE}
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200211 result := c.IsHighSurrogate();
Jens Geyer71070432016-01-29 10:08:39 +0100212 {$IFEND}
213end;
214
215
216class function CharUtils.IsLowSurrogate( const c : Char) : Boolean;
217begin
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200218 {$IF CompilerVersion < 23.0}
219 result := Character.IsLowSurrogate( c);
Jens Geyer71070432016-01-29 10:08:39 +0100220 {$ELSE}
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200221 result := c.IsLowSurrogate;
Jens Geyer71070432016-01-29 10:08:39 +0100222 {$IFEND}
223end;
224
225
Jens Geyer9f7f11e2016-04-14 21:37:11 +0200226// natively available since stone age
227function InterlockedCompareExchange64;
228external KERNEL32 name 'InterlockedCompareExchange64';
229
230
231// natively available >= Vista
232// implemented this way since there are still some people running Windows XP :-(
233function InterlockedExchangeAdd64( var Addend : Int64; Value : Int64) : Int64; stdcall;
234var old : Int64;
235begin
236 repeat
237 Old := Addend;
238 until (InterlockedCompareExchange64( Addend, Old + Value, Old) = Old);
239 result := Old;
240end;
241
Jens Geyer71070432016-01-29 10:08:39 +0100242
243
Jens Geyerd5436f52014-10-03 19:50:38 +0200244end.