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 | |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 24 | {$I Thrift.Defines.inc} |
Nick | 4f5229e | 2016-04-14 16:43:22 +0300 | [diff] [blame] | 25 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 26 | uses |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 27 | {$IFDEF OLD_UNIT_NAMES} |
Jens Geyer | 8f7487e | 2019-05-09 22:21:32 +0200 | [diff] [blame] | 28 | Classes, Windows, SysUtils, Character, SyncObjs, TypInfo, Rtti; |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 29 | {$ELSE} |
Jens Geyer | 8f7487e | 2019-05-09 22:21:32 +0200 | [diff] [blame] | 30 | System.Classes, Winapi.Windows, System.SysUtils, System.Character, |
| 31 | System.SyncObjs, System.TypInfo, System.Rtti; |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 32 | {$ENDIF} |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 33 | |
| 34 | type |
Jens Geyer | 8f7487e | 2019-05-09 22:21:32 +0200 | [diff] [blame] | 35 | ISupportsToString = interface |
| 36 | ['{AF71C350-E0CD-4E94-B77C-0310DC8227FF}'] |
| 37 | function ToString : string; |
| 38 | end; |
| 39 | |
| 40 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 41 | IOverlappedHelper = interface |
| 42 | ['{A1832EFA-2E02-4884-8F09-F0A0277157FA}'] |
| 43 | function Overlapped : TOverlapped; |
| 44 | function OverlappedPtr : POverlapped; |
| 45 | function WaitHandle : THandle; |
| 46 | function WaitFor(dwTimeout: DWORD) : DWORD; |
| 47 | end; |
| 48 | |
| 49 | TOverlappedHelperImpl = class( TInterfacedObject, IOverlappedHelper) |
| 50 | strict protected |
| 51 | FOverlapped : TOverlapped; |
| 52 | FEvent : TEvent; |
| 53 | |
| 54 | // IOverlappedHelper |
| 55 | function Overlapped : TOverlapped; |
| 56 | function OverlappedPtr : POverlapped; |
| 57 | function WaitHandle : THandle; |
| 58 | function WaitFor(dwTimeout: DWORD) : DWORD; |
| 59 | public |
| 60 | constructor Create; |
| 61 | destructor Destroy; override; |
| 62 | end; |
| 63 | |
| 64 | |
Jens Geyer | 8f7487e | 2019-05-09 22:21:32 +0200 | [diff] [blame] | 65 | TThriftStringBuilder = class( TStringBuilder) |
| 66 | public |
| 67 | function Append(const Value: TBytes): TStringBuilder; overload; |
| 68 | function Append(const Value: ISupportsToString): TStringBuilder; overload; |
| 69 | end; |
| 70 | |
| 71 | |
Jens Geyer | d8bddbc | 2014-12-14 00:41:33 +0100 | [diff] [blame] | 72 | Base64Utils = class sealed |
| 73 | public |
| 74 | class function Encode( const src : TBytes; srcOff, len : Integer; dst : TBytes; dstOff : Integer) : Integer; static; |
| 75 | class function Decode( const src : TBytes; srcOff, len : Integer; dst : TBytes; dstOff : Integer) : Integer; static; |
| 76 | end; |
| 77 | |
| 78 | |
Jens Geyer | 7107043 | 2016-01-29 10:08:39 +0100 | [diff] [blame] | 79 | CharUtils = class sealed |
| 80 | public |
| 81 | class function IsHighSurrogate( const c : Char) : Boolean; static; inline; |
| 82 | class function IsLowSurrogate( const c : Char) : Boolean; static; inline; |
| 83 | end; |
| 84 | |
Jens Geyer | 8f7487e | 2019-05-09 22:21:32 +0200 | [diff] [blame] | 85 | EnumUtils<T> = class sealed |
| 86 | public |
| 87 | class function ToString(const value : Integer) : string; reintroduce; static; inline; |
| 88 | end; |
| 89 | |
| 90 | StringUtils<T> = class sealed |
| 91 | public |
| 92 | class function ToString(const value : T) : string; reintroduce; static; inline; |
| 93 | end; |
| 94 | |
Jens Geyer | 7107043 | 2016-01-29 10:08:39 +0100 | [diff] [blame] | 95 | |
Jens Geyer | f790445 | 2017-07-26 15:02:12 +0200 | [diff] [blame] | 96 | {$IFDEF Win64} |
| 97 | function InterlockedExchangeAdd64( var Addend : Int64; Value : Int64) : Int64; |
| 98 | {$ENDIF} |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 99 | |
Jens Geyer | 7107043 | 2016-01-29 10:08:39 +0100 | [diff] [blame] | 100 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 101 | implementation |
| 102 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 103 | { TOverlappedHelperImpl } |
| 104 | |
| 105 | constructor TOverlappedHelperImpl.Create; |
| 106 | begin |
| 107 | inherited Create; |
| 108 | FillChar( FOverlapped, SizeOf(FOverlapped), 0); |
| 109 | FEvent := TEvent.Create( nil, TRUE, FALSE, ''); // always ManualReset, see MSDN |
| 110 | FOverlapped.hEvent := FEvent.Handle; |
| 111 | end; |
| 112 | |
| 113 | |
| 114 | |
| 115 | destructor TOverlappedHelperImpl.Destroy; |
| 116 | begin |
| 117 | try |
| 118 | FOverlapped.hEvent := 0; |
| 119 | FreeAndNil( FEvent); |
| 120 | |
| 121 | finally |
| 122 | inherited Destroy; |
| 123 | end; |
| 124 | |
| 125 | end; |
| 126 | |
| 127 | |
| 128 | function TOverlappedHelperImpl.Overlapped : TOverlapped; |
| 129 | begin |
| 130 | result := FOverlapped; |
| 131 | end; |
| 132 | |
| 133 | |
| 134 | function TOverlappedHelperImpl.OverlappedPtr : POverlapped; |
| 135 | begin |
| 136 | result := @FOverlapped; |
| 137 | end; |
| 138 | |
| 139 | |
| 140 | function TOverlappedHelperImpl.WaitHandle : THandle; |
| 141 | begin |
| 142 | result := FOverlapped.hEvent; |
| 143 | end; |
| 144 | |
| 145 | |
| 146 | function TOverlappedHelperImpl.WaitFor( dwTimeout : DWORD) : DWORD; |
| 147 | begin |
| 148 | result := WaitForSingleObject( FOverlapped.hEvent, dwTimeout); |
| 149 | end; |
| 150 | |
| 151 | |
Jens Geyer | d8bddbc | 2014-12-14 00:41:33 +0100 | [diff] [blame] | 152 | { Base64Utils } |
| 153 | |
| 154 | class function Base64Utils.Encode( const src : TBytes; srcOff, len : Integer; dst : TBytes; dstOff : Integer) : Integer; |
| 155 | const ENCODE_TABLE : PAnsiChar = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; |
| 156 | begin |
| 157 | ASSERT( len in [1..3]); |
| 158 | dst[dstOff] := Byte( ENCODE_TABLE[ (src[srcOff] shr 2) and $3F]); |
| 159 | case len of |
| 160 | 3 : begin |
| 161 | Inc(dstOff); |
| 162 | dst[dstOff] := Byte( ENCODE_TABLE[ ((src[srcOff] shl 4) and $30) or ((src[srcOff + 1] shr 4) and $0F)]); |
| 163 | Inc(dstOff); |
| 164 | dst[dstOff] := Byte( ENCODE_TABLE[ ((src[srcOff + 1] shl 2) and $3C) or ((src[srcOff + 2] shr 6) and $03)]); |
| 165 | Inc(dstOff); |
| 166 | dst[dstOff] := Byte( ENCODE_TABLE[ src[srcOff + 2] and $3F]); |
| 167 | result := 4; |
| 168 | end; |
| 169 | |
| 170 | 2 : begin |
| 171 | Inc(dstOff); |
| 172 | dst[dstOff] := Byte( ENCODE_TABLE[ ((src[srcOff] shl 4) and $30) or ((src[srcOff + 1] shr 4) and $0F)]); |
| 173 | Inc(dstOff); |
| 174 | dst[dstOff] := Byte( ENCODE_TABLE[ (src[srcOff + 1] shl 2) and $3C]); |
| 175 | result := 3; |
| 176 | end; |
| 177 | |
| 178 | 1 : begin |
| 179 | Inc(dstOff); |
| 180 | dst[dstOff] := Byte( ENCODE_TABLE[ (src[srcOff] shl 4) and $30]); |
| 181 | result := 2; |
| 182 | end; |
| 183 | |
| 184 | else |
| 185 | ASSERT( FALSE); |
Jens Geyer | 9f9535c | 2014-12-14 04:16:05 +0100 | [diff] [blame] | 186 | result := 0; // because invalid call |
Jens Geyer | d8bddbc | 2014-12-14 00:41:33 +0100 | [diff] [blame] | 187 | end; |
| 188 | end; |
| 189 | |
| 190 | |
| 191 | class function Base64Utils.Decode( const src : TBytes; srcOff, len : Integer; dst : TBytes; dstOff : Integer) : Integer; |
| 192 | const DECODE_TABLE : array[0..$FF] of Integer |
| 193 | = ( -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 194 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 195 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, |
| 196 | 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, |
| 197 | -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, |
| 198 | 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, |
| 199 | -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, |
| 200 | 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, |
| 201 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 202 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 203 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 204 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 205 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 206 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 207 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 208 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 ); |
| 209 | begin |
| 210 | ASSERT( len in [1..4]); |
| 211 | result := 1; |
| 212 | dst[dstOff] := ((DECODE_TABLE[src[srcOff] and $0FF] shl 2) |
| 213 | or (DECODE_TABLE[src[srcOff + 1] and $0FF] shr 4)); |
| 214 | |
| 215 | if (len > 2) then begin |
| 216 | Inc( result); |
| 217 | Inc( dstOff); |
| 218 | dst[dstOff] := (((DECODE_TABLE[src[srcOff + 1] and $0FF] shl 4) and $F0) |
| 219 | or (DECODE_TABLE[src[srcOff + 2] and $0FF] shr 2)); |
| 220 | |
| 221 | if (len > 3) then begin |
| 222 | Inc( result); |
| 223 | Inc( dstOff); |
| 224 | dst[dstOff] := (((DECODE_TABLE[src[srcOff + 2] and $0FF] shl 6) and $C0) |
| 225 | or DECODE_TABLE[src[srcOff + 3] and $0FF]); |
| 226 | end; |
| 227 | end; |
| 228 | end; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 229 | |
| 230 | |
Jens Geyer | 7107043 | 2016-01-29 10:08:39 +0100 | [diff] [blame] | 231 | class function CharUtils.IsHighSurrogate( const c : Char) : Boolean; |
| 232 | begin |
Jens Geyer | 36c0b34 | 2018-01-19 19:17:33 +0100 | [diff] [blame] | 233 | {$IF CompilerVersion < 25.0} |
| 234 | {$IFDEF OLD_UNIT_NAMES} |
| 235 | result := Character.IsHighSurrogate(c); |
| 236 | {$ELSE} |
| 237 | result := System.Character.IsHighSurrogate(c); |
| 238 | {$ENDIF} |
Jens Geyer | 7107043 | 2016-01-29 10:08:39 +0100 | [diff] [blame] | 239 | {$ELSE} |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 240 | result := c.IsHighSurrogate(); |
Jens Geyer | 7107043 | 2016-01-29 10:08:39 +0100 | [diff] [blame] | 241 | {$IFEND} |
| 242 | end; |
| 243 | |
| 244 | |
| 245 | class function CharUtils.IsLowSurrogate( const c : Char) : Boolean; |
| 246 | begin |
Jens Geyer | 36c0b34 | 2018-01-19 19:17:33 +0100 | [diff] [blame] | 247 | {$IF CompilerVersion < 25.0} |
| 248 | {$IFDEF OLD_UNIT_NAMES} |
| 249 | result := Character.IsLowSurrogate(c); |
| 250 | {$ELSE} |
| 251 | result := System.Character.IsLowSurrogate(c); |
| 252 | {$ENDIF} |
Jens Geyer | 7107043 | 2016-01-29 10:08:39 +0100 | [diff] [blame] | 253 | {$ELSE} |
Jens Geyer | 36c0b34 | 2018-01-19 19:17:33 +0100 | [diff] [blame] | 254 | result := c.IsLowSurrogate(); |
Jens Geyer | 7107043 | 2016-01-29 10:08:39 +0100 | [diff] [blame] | 255 | {$IFEND} |
| 256 | end; |
| 257 | |
| 258 | |
Jens Geyer | f790445 | 2017-07-26 15:02:12 +0200 | [diff] [blame] | 259 | {$IFDEF Win64} |
| 260 | |
| 261 | function InterlockedCompareExchange64( var Target : Int64; Exchange, Comparand : Int64) : Int64; inline; |
| 262 | begin |
| 263 | {$IFDEF OLD_UNIT_NAMES} |
| 264 | result := Windows.InterlockedCompareExchange64( Target, Exchange, Comparand); |
| 265 | {$ELSE} |
| 266 | result := WinApi.Windows.InterlockedCompareExchange64( Target, Exchange, Comparand); |
| 267 | {$ENDIF} |
| 268 | end; |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 269 | |
| 270 | |
Jens Geyer | f790445 | 2017-07-26 15:02:12 +0200 | [diff] [blame] | 271 | function InterlockedExchangeAdd64( var Addend : Int64; Value : Int64) : Int64; |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 272 | var old : Int64; |
| 273 | begin |
| 274 | repeat |
| 275 | Old := Addend; |
| 276 | until (InterlockedCompareExchange64( Addend, Old + Value, Old) = Old); |
| 277 | result := Old; |
| 278 | end; |
| 279 | |
Jens Geyer | f790445 | 2017-07-26 15:02:12 +0200 | [diff] [blame] | 280 | {$ENDIF} |
Jens Geyer | 7107043 | 2016-01-29 10:08:39 +0100 | [diff] [blame] | 281 | |
| 282 | |
Jens Geyer | 8f7487e | 2019-05-09 22:21:32 +0200 | [diff] [blame] | 283 | { EnumUtils<T> } |
| 284 | |
| 285 | class function EnumUtils<T>.ToString(const value : Integer) : string; |
| 286 | var pType : PTypeInfo; |
| 287 | begin |
| 288 | pType := PTypeInfo(TypeInfo(T)); |
| 289 | if Assigned(pType) and (pType^.Kind = tkEnumeration) |
| 290 | then result := GetEnumName(pType,value) |
| 291 | else result := IntToStr(Ord(value)); |
| 292 | end; |
| 293 | |
| 294 | |
| 295 | { StringUtils<T> } |
| 296 | |
| 297 | class function StringUtils<T>.ToString(const value : T) : string; |
| 298 | var pType : PTypeInfo; |
| 299 | base : ISupportsToString; |
| 300 | begin |
| 301 | pType := PTypeInfo(TypeInfo(T)); |
| 302 | if Assigned(pType) then begin |
| 303 | case pType^.Kind of |
| 304 | tkInterface : begin |
| 305 | if Supports(IInterface(value), ISupportsToString, base) then begin |
| 306 | result := base.toString; |
| 307 | Exit; |
| 308 | end; |
| 309 | end; |
| 310 | end; |
| 311 | end; |
| 312 | |
| 313 | result := TValue.From<T>(value).ToString; |
| 314 | end; |
| 315 | |
| 316 | |
| 317 | { TThriftStringBuilder } |
| 318 | |
| 319 | function TThriftStringBuilder.Append(const Value: TBytes): TStringBuilder; |
| 320 | begin |
| 321 | Result := Append( string( RawByteString(Value)) ); |
| 322 | end; |
| 323 | |
| 324 | function TThriftStringBuilder.Append( const Value: ISupportsToString): TStringBuilder; |
| 325 | begin |
| 326 | Result := Append( Value.ToString ); |
| 327 | end; |
| 328 | |
| 329 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 330 | end. |