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; |
Jens Geyer | 589ee5b | 2021-03-29 21:40:55 +0200 | [diff] [blame] | 83 | |
| 84 | class function IsHtmlDoctype( const fourBytes : Integer) : Boolean; static; |
Jens Geyer | 7107043 | 2016-01-29 10:08:39 +0100 | [diff] [blame] | 85 | end; |
| 86 | |
Jens Geyer | 62445c1 | 2022-06-29 00:00:00 +0200 | [diff] [blame] | 87 | |
| 88 | IntegerUtils = class sealed |
| 89 | strict private |
| 90 | class procedure SwapBytes( var one, two : Byte); static; inline; |
| 91 | class procedure Swap2( const pValue : Pointer); static; |
| 92 | class procedure Swap4( const pValue : Pointer); static; |
| 93 | class procedure Swap8( const pValue : Pointer); static; |
| 94 | public |
| 95 | class procedure SwapByteOrder( const pValue : Pointer; const size : Integer); overload; static; |
| 96 | end; |
| 97 | |
| 98 | |
Jens Geyer | f8f6278 | 2022-09-10 00:55:02 +0200 | [diff] [blame] | 99 | // problem: inheritance possible for class helpers ONLY but not with record helpers |
| 100 | // workaround: use static class method instead of record helper :-( |
| 101 | GuidUtils = class sealed |
Jens Geyer | 62445c1 | 2022-06-29 00:00:00 +0200 | [diff] [blame] | 102 | public |
Jens Geyer | f8f6278 | 2022-09-10 00:55:02 +0200 | [diff] [blame] | 103 | // new stuff |
| 104 | class function SwapByteOrder( const aGuid : TGuid) : TGuid; static; |
Jens Geyer | 62445c1 | 2022-06-29 00:00:00 +0200 | [diff] [blame] | 105 | |
| 106 | {$IFDEF Debug} |
| 107 | class procedure SelfTest; static; |
| 108 | {$ENDIF} |
| 109 | end; |
| 110 | |
| 111 | |
Jens Geyer | 8f7487e | 2019-05-09 22:21:32 +0200 | [diff] [blame] | 112 | EnumUtils<T> = class sealed |
| 113 | public |
| 114 | class function ToString(const value : Integer) : string; reintroduce; static; inline; |
| 115 | end; |
| 116 | |
Jens Geyer | 62445c1 | 2022-06-29 00:00:00 +0200 | [diff] [blame] | 117 | |
Jens Geyer | 8f7487e | 2019-05-09 22:21:32 +0200 | [diff] [blame] | 118 | StringUtils<T> = class sealed |
| 119 | public |
| 120 | class function ToString(const value : T) : string; reintroduce; static; inline; |
| 121 | end; |
| 122 | |
Jens Geyer | 7107043 | 2016-01-29 10:08:39 +0100 | [diff] [blame] | 123 | |
Jens Geyer | 83ff753 | 2019-06-06 22:46:03 +0200 | [diff] [blame] | 124 | const |
| 125 | THRIFT_MIMETYPE = 'application/x-thrift'; |
| 126 | |
Jens Geyer | f790445 | 2017-07-26 15:02:12 +0200 | [diff] [blame] | 127 | {$IFDEF Win64} |
Jens Geyer | 6e2a698 | 2019-12-12 23:07:49 +0100 | [diff] [blame] | 128 | function InterlockedExchangeAdd64( var Addend : Int64; Value : Int64) : Int64; |
Jens Geyer | f790445 | 2017-07-26 15:02:12 +0200 | [diff] [blame] | 129 | {$ENDIF} |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 130 | |
Jens Geyer | 7107043 | 2016-01-29 10:08:39 +0100 | [diff] [blame] | 131 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 132 | implementation |
| 133 | |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 134 | { TOverlappedHelperImpl } |
| 135 | |
| 136 | constructor TOverlappedHelperImpl.Create; |
| 137 | begin |
| 138 | inherited Create; |
| 139 | FillChar( FOverlapped, SizeOf(FOverlapped), 0); |
| 140 | FEvent := TEvent.Create( nil, TRUE, FALSE, ''); // always ManualReset, see MSDN |
| 141 | FOverlapped.hEvent := FEvent.Handle; |
| 142 | end; |
| 143 | |
| 144 | |
| 145 | |
| 146 | destructor TOverlappedHelperImpl.Destroy; |
| 147 | begin |
| 148 | try |
| 149 | FOverlapped.hEvent := 0; |
| 150 | FreeAndNil( FEvent); |
| 151 | |
| 152 | finally |
| 153 | inherited Destroy; |
| 154 | end; |
| 155 | |
| 156 | end; |
| 157 | |
| 158 | |
| 159 | function TOverlappedHelperImpl.Overlapped : TOverlapped; |
| 160 | begin |
| 161 | result := FOverlapped; |
| 162 | end; |
| 163 | |
| 164 | |
| 165 | function TOverlappedHelperImpl.OverlappedPtr : POverlapped; |
| 166 | begin |
| 167 | result := @FOverlapped; |
| 168 | end; |
| 169 | |
| 170 | |
| 171 | function TOverlappedHelperImpl.WaitHandle : THandle; |
| 172 | begin |
| 173 | result := FOverlapped.hEvent; |
| 174 | end; |
| 175 | |
| 176 | |
| 177 | function TOverlappedHelperImpl.WaitFor( dwTimeout : DWORD) : DWORD; |
| 178 | begin |
| 179 | result := WaitForSingleObject( FOverlapped.hEvent, dwTimeout); |
| 180 | end; |
| 181 | |
| 182 | |
Jens Geyer | d8bddbc | 2014-12-14 00:41:33 +0100 | [diff] [blame] | 183 | { Base64Utils } |
| 184 | |
| 185 | class function Base64Utils.Encode( const src : TBytes; srcOff, len : Integer; dst : TBytes; dstOff : Integer) : Integer; |
| 186 | const ENCODE_TABLE : PAnsiChar = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; |
| 187 | begin |
| 188 | ASSERT( len in [1..3]); |
| 189 | dst[dstOff] := Byte( ENCODE_TABLE[ (src[srcOff] shr 2) and $3F]); |
| 190 | case len of |
| 191 | 3 : begin |
| 192 | Inc(dstOff); |
| 193 | dst[dstOff] := Byte( ENCODE_TABLE[ ((src[srcOff] shl 4) and $30) or ((src[srcOff + 1] shr 4) and $0F)]); |
| 194 | Inc(dstOff); |
| 195 | dst[dstOff] := Byte( ENCODE_TABLE[ ((src[srcOff + 1] shl 2) and $3C) or ((src[srcOff + 2] shr 6) and $03)]); |
| 196 | Inc(dstOff); |
| 197 | dst[dstOff] := Byte( ENCODE_TABLE[ src[srcOff + 2] and $3F]); |
| 198 | result := 4; |
| 199 | end; |
| 200 | |
| 201 | 2 : begin |
| 202 | Inc(dstOff); |
| 203 | dst[dstOff] := Byte( ENCODE_TABLE[ ((src[srcOff] shl 4) and $30) or ((src[srcOff + 1] shr 4) and $0F)]); |
| 204 | Inc(dstOff); |
| 205 | dst[dstOff] := Byte( ENCODE_TABLE[ (src[srcOff + 1] shl 2) and $3C]); |
| 206 | result := 3; |
| 207 | end; |
| 208 | |
| 209 | 1 : begin |
| 210 | Inc(dstOff); |
| 211 | dst[dstOff] := Byte( ENCODE_TABLE[ (src[srcOff] shl 4) and $30]); |
| 212 | result := 2; |
| 213 | end; |
| 214 | |
| 215 | else |
| 216 | ASSERT( FALSE); |
Jens Geyer | 9f9535c | 2014-12-14 04:16:05 +0100 | [diff] [blame] | 217 | result := 0; // because invalid call |
Jens Geyer | d8bddbc | 2014-12-14 00:41:33 +0100 | [diff] [blame] | 218 | end; |
| 219 | end; |
| 220 | |
| 221 | |
| 222 | class function Base64Utils.Decode( const src : TBytes; srcOff, len : Integer; dst : TBytes; dstOff : Integer) : Integer; |
| 223 | const DECODE_TABLE : array[0..$FF] of Integer |
| 224 | = ( -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 225 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 226 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63, |
| 227 | 52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1, |
| 228 | -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14, |
| 229 | 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1, |
| 230 | -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, |
| 231 | 41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1, |
| 232 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 233 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 234 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 235 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 236 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 237 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 238 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, |
| 239 | -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 ); |
| 240 | begin |
| 241 | ASSERT( len in [1..4]); |
| 242 | result := 1; |
| 243 | dst[dstOff] := ((DECODE_TABLE[src[srcOff] and $0FF] shl 2) |
| 244 | or (DECODE_TABLE[src[srcOff + 1] and $0FF] shr 4)); |
| 245 | |
| 246 | if (len > 2) then begin |
| 247 | Inc( result); |
| 248 | Inc( dstOff); |
| 249 | dst[dstOff] := (((DECODE_TABLE[src[srcOff + 1] and $0FF] shl 4) and $F0) |
| 250 | or (DECODE_TABLE[src[srcOff + 2] and $0FF] shr 2)); |
| 251 | |
| 252 | if (len > 3) then begin |
| 253 | Inc( result); |
| 254 | Inc( dstOff); |
| 255 | dst[dstOff] := (((DECODE_TABLE[src[srcOff + 2] and $0FF] shl 6) and $C0) |
| 256 | or DECODE_TABLE[src[srcOff + 3] and $0FF]); |
| 257 | end; |
| 258 | end; |
| 259 | end; |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 260 | |
| 261 | |
Jens Geyer | 7107043 | 2016-01-29 10:08:39 +0100 | [diff] [blame] | 262 | class function CharUtils.IsHighSurrogate( const c : Char) : Boolean; |
| 263 | begin |
Jens Geyer | 36c0b34 | 2018-01-19 19:17:33 +0100 | [diff] [blame] | 264 | {$IF CompilerVersion < 25.0} |
| 265 | {$IFDEF OLD_UNIT_NAMES} |
| 266 | result := Character.IsHighSurrogate(c); |
| 267 | {$ELSE} |
| 268 | result := System.Character.IsHighSurrogate(c); |
| 269 | {$ENDIF} |
Jens Geyer | 7107043 | 2016-01-29 10:08:39 +0100 | [diff] [blame] | 270 | {$ELSE} |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 271 | result := c.IsHighSurrogate(); |
Jens Geyer | 7107043 | 2016-01-29 10:08:39 +0100 | [diff] [blame] | 272 | {$IFEND} |
| 273 | end; |
| 274 | |
| 275 | |
| 276 | class function CharUtils.IsLowSurrogate( const c : Char) : Boolean; |
| 277 | begin |
Jens Geyer | 36c0b34 | 2018-01-19 19:17:33 +0100 | [diff] [blame] | 278 | {$IF CompilerVersion < 25.0} |
| 279 | {$IFDEF OLD_UNIT_NAMES} |
| 280 | result := Character.IsLowSurrogate(c); |
| 281 | {$ELSE} |
| 282 | result := System.Character.IsLowSurrogate(c); |
| 283 | {$ENDIF} |
Jens Geyer | 7107043 | 2016-01-29 10:08:39 +0100 | [diff] [blame] | 284 | {$ELSE} |
Jens Geyer | 36c0b34 | 2018-01-19 19:17:33 +0100 | [diff] [blame] | 285 | result := c.IsLowSurrogate(); |
Jens Geyer | 7107043 | 2016-01-29 10:08:39 +0100 | [diff] [blame] | 286 | {$IFEND} |
| 287 | end; |
| 288 | |
| 289 | |
Jens Geyer | 589ee5b | 2021-03-29 21:40:55 +0200 | [diff] [blame] | 290 | class function CharUtils.IsHtmlDoctype( const fourBytes : Integer) : Boolean; |
| 291 | var pc : PAnsiChar; |
| 292 | const HTML_BEGIN : PAnsiChar = 'OD!<'; // first 4 bytes of '<!DOCTYPE ' in LE byte order |
| 293 | begin |
| 294 | pc := @fourBytes; |
| 295 | |
| 296 | if UpCase(pc^) <> HTML_BEGIN[0] |
| 297 | then Exit(FALSE); |
| 298 | |
| 299 | Inc( pc); |
| 300 | if UpCase(pc^) <> HTML_BEGIN[1] |
| 301 | then Exit(FALSE); |
| 302 | |
| 303 | |
| 304 | Inc( pc); |
| 305 | if UpCase(pc^) <> HTML_BEGIN[2] |
| 306 | then Exit(FALSE); |
| 307 | |
| 308 | Inc( pc); |
| 309 | result := (UpCase(pc^) = HTML_BEGIN[3]); |
| 310 | end; |
| 311 | |
Jens Geyer | 62445c1 | 2022-06-29 00:00:00 +0200 | [diff] [blame] | 312 | { IntegerUtils } |
| 313 | |
| 314 | |
| 315 | class procedure IntegerUtils.SwapBytes( var one, two : Byte); |
| 316 | var tmp : Byte; |
| 317 | begin |
| 318 | tmp := one; |
| 319 | one := two; |
| 320 | two := tmp; |
| 321 | end; |
| 322 | |
| 323 | |
| 324 | class procedure IntegerUtils.Swap2( const pValue : Pointer); |
| 325 | var pData : PByteArray absolute pValue; |
| 326 | begin |
| 327 | SwapBytes( pData^[0], pData^[1]); |
| 328 | end; |
| 329 | |
| 330 | |
| 331 | class procedure IntegerUtils.Swap4( const pValue : Pointer); |
| 332 | var pData : PByteArray absolute pValue; |
| 333 | begin |
| 334 | SwapBytes( pData^[0], pData^[3]); |
| 335 | SwapBytes( pData^[1], pData^[2]); |
| 336 | end; |
| 337 | |
| 338 | |
| 339 | class procedure IntegerUtils.Swap8( const pValue : Pointer); |
| 340 | var pData : PByteArray absolute pValue; |
| 341 | begin |
| 342 | SwapBytes( pData^[0], pData^[7]); |
| 343 | SwapBytes( pData^[1], pData^[6]); |
| 344 | SwapBytes( pData^[2], pData^[5]); |
| 345 | SwapBytes( pData^[3], pData^[4]); |
| 346 | end; |
| 347 | |
| 348 | |
| 349 | class procedure IntegerUtils.SwapByteOrder( const pValue : Pointer; const size : Integer); |
| 350 | begin |
| 351 | case size of |
| 352 | 2 : Swap2( pValue); |
| 353 | 4 : Swap4( pValue); |
| 354 | 8 : Swap8( pValue); |
| 355 | else |
| 356 | raise EArgumentException.Create('Unexpected size'); |
| 357 | end; |
| 358 | end; |
| 359 | |
| 360 | |
Jens Geyer | f8f6278 | 2022-09-10 00:55:02 +0200 | [diff] [blame] | 361 | { GuidUtils } |
Jens Geyer | 62445c1 | 2022-06-29 00:00:00 +0200 | [diff] [blame] | 362 | |
| 363 | |
Jens Geyer | f8f6278 | 2022-09-10 00:55:02 +0200 | [diff] [blame] | 364 | class function GuidUtils.SwapByteOrder( const aGuid : TGuid) : TGuid; |
Jens Geyer | 62445c1 | 2022-06-29 00:00:00 +0200 | [diff] [blame] | 365 | // convert to/from network byte order |
| 366 | // - https://www.ietf.org/rfc/rfc4122.txt |
| 367 | // - https://stackoverflow.com/questions/10850075/guid-uuid-compatibility-issue-between-net-and-linux |
| 368 | // - https://lists.gnu.org/archive/html/bug-parted/2002-01/msg00099.html |
| 369 | begin |
Jens Geyer | f8f6278 | 2022-09-10 00:55:02 +0200 | [diff] [blame] | 370 | result := aGuid; |
Jens Geyer | 62445c1 | 2022-06-29 00:00:00 +0200 | [diff] [blame] | 371 | |
| 372 | IntegerUtils.SwapByteOrder( @result.D1, SizeOf(result.D1)); |
| 373 | IntegerUtils.SwapByteOrder( @result.D2, SizeOf(result.D2)); |
| 374 | IntegerUtils.SwapByteOrder( @result.D3, SizeOf(result.D3)); |
| 375 | //result.D4 = array of byte -> implicitly correct |
| 376 | end; |
| 377 | |
| 378 | |
| 379 | {$IFDEF Debug} |
Jens Geyer | f8f6278 | 2022-09-10 00:55:02 +0200 | [diff] [blame] | 380 | class procedure GuidUtils.SelfTest; |
Jens Geyer | 62445c1 | 2022-06-29 00:00:00 +0200 | [diff] [blame] | 381 | var guid : TGuid; |
| 382 | pBytes : PByteArray; |
| 383 | i, expected : Integer; |
| 384 | const TEST_GUID : TGuid = '{00112233-4455-6677-8899-aabbccddeeff}'; |
| 385 | begin |
| 386 | // host to network |
| 387 | guid := TEST_GUID; |
Jens Geyer | f8f6278 | 2022-09-10 00:55:02 +0200 | [diff] [blame] | 388 | guid := GuidUtils.SwapByteOrder(guid); |
Jens Geyer | 62445c1 | 2022-06-29 00:00:00 +0200 | [diff] [blame] | 389 | |
| 390 | // validate network order |
| 391 | pBytes := @guid; |
| 392 | for i := 0 to $F do begin |
| 393 | expected := i * $11; |
| 394 | ASSERT( pBytes^[i] = expected); |
| 395 | end; |
| 396 | |
| 397 | // network to host and final validation |
Jens Geyer | f8f6278 | 2022-09-10 00:55:02 +0200 | [diff] [blame] | 398 | guid := GuidUtils.SwapByteOrder(guid); |
Jens Geyer | 62445c1 | 2022-06-29 00:00:00 +0200 | [diff] [blame] | 399 | ASSERT( IsEqualGuid( guid, TEST_GUID)); |
Jens Geyer | f8f6278 | 2022-09-10 00:55:02 +0200 | [diff] [blame] | 400 | |
| 401 | // prevent collisions with SysUtils.TGuidHelper |
| 402 | guid := TGuid.NewGuid; |
Jens Geyer | 62445c1 | 2022-06-29 00:00:00 +0200 | [diff] [blame] | 403 | end; |
| 404 | {$ENDIF} |
| 405 | |
Jens Geyer | 589ee5b | 2021-03-29 21:40:55 +0200 | [diff] [blame] | 406 | |
| 407 | |
Jens Geyer | f790445 | 2017-07-26 15:02:12 +0200 | [diff] [blame] | 408 | {$IFDEF Win64} |
| 409 | |
| 410 | function InterlockedCompareExchange64( var Target : Int64; Exchange, Comparand : Int64) : Int64; inline; |
| 411 | begin |
| 412 | {$IFDEF OLD_UNIT_NAMES} |
| 413 | result := Windows.InterlockedCompareExchange64( Target, Exchange, Comparand); |
| 414 | {$ELSE} |
| 415 | result := WinApi.Windows.InterlockedCompareExchange64( Target, Exchange, Comparand); |
| 416 | {$ENDIF} |
| 417 | end; |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 418 | |
| 419 | |
Jens Geyer | f790445 | 2017-07-26 15:02:12 +0200 | [diff] [blame] | 420 | function InterlockedExchangeAdd64( var Addend : Int64; Value : Int64) : Int64; |
Jens Geyer | 9f7f11e | 2016-04-14 21:37:11 +0200 | [diff] [blame] | 421 | var old : Int64; |
| 422 | begin |
| 423 | repeat |
| 424 | Old := Addend; |
| 425 | until (InterlockedCompareExchange64( Addend, Old + Value, Old) = Old); |
| 426 | result := Old; |
| 427 | end; |
| 428 | |
Jens Geyer | f790445 | 2017-07-26 15:02:12 +0200 | [diff] [blame] | 429 | {$ENDIF} |
Jens Geyer | 7107043 | 2016-01-29 10:08:39 +0100 | [diff] [blame] | 430 | |
| 431 | |
Jens Geyer | 8f7487e | 2019-05-09 22:21:32 +0200 | [diff] [blame] | 432 | { EnumUtils<T> } |
| 433 | |
| 434 | class function EnumUtils<T>.ToString(const value : Integer) : string; |
| 435 | var pType : PTypeInfo; |
| 436 | begin |
| 437 | pType := PTypeInfo(TypeInfo(T)); |
Jens Geyer | 6e2a698 | 2019-12-12 23:07:49 +0100 | [diff] [blame] | 438 | if Assigned(pType) |
| 439 | and (pType^.Kind = tkEnumeration) |
| 440 | {$IF CompilerVersion >= 23.0} // TODO: Range correct? What we know is that XE does not offer it, but Rio has it |
| 441 | and (pType^.TypeData^.MaxValue >= value) |
| 442 | and (pType^.TypeData^.MinValue <= value) |
| 443 | {$ELSE} |
| 444 | and FALSE // THRIFT-5048: pType^.TypeData^ member not supported -> prevent GetEnumName() from reading outside the legal range |
| 445 | {$IFEND} |
| 446 | then result := GetEnumName( PTypeInfo(pType), value) |
Jens Geyer | 8f7487e | 2019-05-09 22:21:32 +0200 | [diff] [blame] | 447 | else result := IntToStr(Ord(value)); |
| 448 | end; |
| 449 | |
| 450 | |
| 451 | { StringUtils<T> } |
| 452 | |
| 453 | class function StringUtils<T>.ToString(const value : T) : string; |
Jens Geyer | 85431d9 | 2019-05-10 21:17:00 +0200 | [diff] [blame] | 454 | type PInterface = ^IInterface; |
Jens Geyer | 8f7487e | 2019-05-09 22:21:32 +0200 | [diff] [blame] | 455 | var pType : PTypeInfo; |
Jens Geyer | 85431d9 | 2019-05-10 21:17:00 +0200 | [diff] [blame] | 456 | stos : ISupportsToString; |
| 457 | pIntf : PInterface; // Workaround: Rio does not allow the direct typecast |
Jens Geyer | 8f7487e | 2019-05-09 22:21:32 +0200 | [diff] [blame] | 458 | begin |
| 459 | pType := PTypeInfo(TypeInfo(T)); |
| 460 | if Assigned(pType) then begin |
| 461 | case pType^.Kind of |
Jens Geyer | ec57271 | 2020-07-13 21:15:31 +0200 | [diff] [blame] | 462 | |
Jens Geyer | 8f7487e | 2019-05-09 22:21:32 +0200 | [diff] [blame] | 463 | tkInterface : begin |
Jens Geyer | 85431d9 | 2019-05-10 21:17:00 +0200 | [diff] [blame] | 464 | pIntf := PInterface(@value); |
| 465 | if Supports( pIntf^, ISupportsToString, stos) then begin |
| 466 | result := stos.toString; |
Jens Geyer | 8f7487e | 2019-05-09 22:21:32 +0200 | [diff] [blame] | 467 | Exit; |
| 468 | end; |
| 469 | end; |
Jens Geyer | ec57271 | 2020-07-13 21:15:31 +0200 | [diff] [blame] | 470 | |
| 471 | tkEnumeration : begin |
| 472 | case SizeOf(value) of |
| 473 | 1 : begin result := EnumUtils<T>.ToString( PShortInt(@value)^); Exit; end; |
| 474 | 2 : begin result := EnumUtils<T>.ToString( PSmallInt(@value)^); Exit; end; |
| 475 | 4 : begin result := EnumUtils<T>.ToString( PLongInt(@value)^); Exit; end; |
| 476 | else |
| 477 | ASSERT(FALSE); // in theory, this should not happen |
| 478 | end; |
| 479 | end; |
| 480 | |
Jens Geyer | 8f7487e | 2019-05-09 22:21:32 +0200 | [diff] [blame] | 481 | end; |
| 482 | end; |
| 483 | |
| 484 | result := TValue.From<T>(value).ToString; |
| 485 | end; |
| 486 | |
| 487 | |
| 488 | { TThriftStringBuilder } |
| 489 | |
| 490 | function TThriftStringBuilder.Append(const Value: TBytes): TStringBuilder; |
| 491 | begin |
| 492 | Result := Append( string( RawByteString(Value)) ); |
| 493 | end; |
| 494 | |
| 495 | function TThriftStringBuilder.Append( const Value: ISupportsToString): TStringBuilder; |
| 496 | begin |
| 497 | Result := Append( Value.ToString ); |
| 498 | end; |
| 499 | |
| 500 | |
Jens Geyer | 62445c1 | 2022-06-29 00:00:00 +0200 | [diff] [blame] | 501 | begin |
| 502 | {$IFDEF Debug} |
Jens Geyer | f8f6278 | 2022-09-10 00:55:02 +0200 | [diff] [blame] | 503 | GuidUtils.SelfTest; |
Jens Geyer | 62445c1 | 2022-06-29 00:00:00 +0200 | [diff] [blame] | 504 | {$ENDIF} |
Jens Geyer | d5436f5 | 2014-10-03 19:50:38 +0200 | [diff] [blame] | 505 | end. |