-
-
Notifications
You must be signed in to change notification settings - Fork 136
/
mormot.core.os.pas
10808 lines (9777 loc) · 385 KB
/
mormot.core.os.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/// Framework Core Low-Level Wrappers to the Operating-System API
// - this unit is a part of the Open Source Synopse mORMot framework 2,
// licensed under a MPL/GPL/LGPL three license - see LICENSE.md
unit mormot.core.os;
{
*****************************************************************************
Cross-platform functions shared by all framework units
- Some Cross-System Type and Constant Definitions
- Gather Operating System Information
- Operating System Specific Types (e.g. TWinRegistry)
- Unicode, Time, File, Console, Library process
- Per Class Properties O(1) Lookup via vmtAutoTable Slot (e.g. for RTTI cache)
- TSynLocker/TSynLocked and Low-Level Threading Features
- Unix Daemon and Windows Service Support
Aim of this unit is to centralize most used OS-specific API calls, like a
SysUtils unit on steroids, to avoid $ifdef/$endif in "uses" clauses.
See mormot.core.os.mac and mormot.core.os.security units for completion.
In practice, no "Windows", nor "Linux/Posix" reference should be needed in
regular units, once mormot.core.os is included. :)
This unit only refers to mormot.core.base so can be used almost stand-alone.
*****************************************************************************
}
interface
{$I ..\mormot.defines.inc}
uses
{$ifdef OSWINDOWS}
Windows, // needed here e.g. for redefinition/redirection of standard types
Messages,
{$endif OSWINDOWS}
classes,
contnrs,
types,
sysutils,
mormot.core.base;
{ ****************** Some Cross-System Type and Constant Definitions }
type
/// allow to customize the possible line feeds
TLineFeed = (
lfSystem,
lfCR,
lfCRLF);
const
{$ifdef OSWINDOWS}
/// operating-system dependent Line Feed characters (#13#10 or #10)
CRLF = #13#10;
/// operating-system dependent wildchar to match all files in a folder
FILES_ALL = '*.*';
/// operating-system dependent "inverted" delimiter for NormalizeFileName()
InvertedPathDelim = '/';
/// operating-system dependent boolean if paths are case-insensitive
PathCaseInsensitive = true;
{$else}
/// operating-system dependent Line Feed characters
CRLF = #10;
/// operating-system dependent wildchar to match all files in a folder
FILES_ALL = '*';
/// operating-system dependent "inverted" delimiter for NormalizeFileName()
InvertedPathDelim = '\';
/// operating-system dependent boolean if paths are case-insensitive
PathCaseInsensitive = false;
{$endif OSWINDOWS}
/// convert a TLineFeed value into its UTF-8 text representation
LINE_FEED: array[TLineFeed] of string[3] = (CRLF, #10, #13#10);
/// human-friendly alias to open a file for exclusive writing
fmShareRead = fmShareDenyWrite;
/// human-friendly alias to open a file for exclusive reading
fmShareWrite = fmShareDenyRead;
/// human-friendly alias to open a file with no read/write exclusion
fmShareReadWrite = fmShareDenyNone;
/// a convenient constant to open a file for reading without exclusion
fmOpenReadShared = fmOpenRead or fmShareReadWrite;
/// a convenient constant to open a file for writing without exclusion
fmOpenWriteShared = fmOpenReadWrite or fmShareReadWrite;
/// a convenient constant to create a file without exclusion
fmCreateShared = fmCreate or fmShareReadWrite;
/// a convenient array constant to open a file for writing without exclusion
fmCreateOrRewrite: array[{rewrite=}boolean] of cardinal = (
fmCreateShared,
fmOpenWriteShared);
type
/// the available HTTP methods transmitted between client and server
// - remote ORM supports non-standard mLOCK/mUNLOCK/mABORT/mSTATE verbs
// - not all IANA verbs are available, because our TRestRouter will only
// support mGET .. mOPTIONS verbs anyway
// - for basic CRUD operations, we consider Create=mPOST, Read=mGET,
// Update=mPUT and Delete=mDELETE - even if it is not fully RESTful
TUriMethod = (
mNone,
mGET,
mPOST,
mPUT,
mDELETE,
mHEAD,
mBEGIN,
mEND,
mABORT,
mLOCK,
mUNLOCK,
mSTATE,
mPATCH,
mOPTIONS);
/// set of available HTTP methods transmitted between client and server
TUriMethods = set of TUriMethod;
/// convert a string HTTP verb into its TUriMethod enumerate
// - conversion is case-insensitive
function ToMethod(const method: RawUtf8): TUriMethod;
/// convert a TUriMethod enumerate to its #0 terminated uppercase text
function ToText(m: TUriMethod): PUtf8Char; overload;
const
/// void HTTP Status Code (not a standard value, for internal use only)
HTTP_NONE = 0;
/// HTTP Status Code for "Continue"
HTTP_CONTINUE = 100;
/// HTTP Status Code for "Switching Protocols"
HTTP_SWITCHINGPROTOCOLS = 101;
/// HTTP Status Code for "Success"
HTTP_SUCCESS = 200;
/// HTTP Status Code for "Created"
HTTP_CREATED = 201;
/// HTTP Status Code for "Accepted"
HTTP_ACCEPTED = 202;
/// HTTP Status Code for "Non-Authoritative Information"
HTTP_NONAUTHORIZEDINFO = 203;
/// HTTP Status Code for "No Content"
HTTP_NOCONTENT = 204;
/// HTTP Status Code for "Reset Content"
HTTP_RESETCONTENT = 205;
/// HTTP Status Code for "Partial Content"
HTTP_PARTIALCONTENT = 206;
/// HTTP Status Code for "Multiple Choices"
HTTP_MULTIPLECHOICES = 300;
/// HTTP Status Code for "Moved Permanently"
HTTP_MOVEDPERMANENTLY = 301;
/// HTTP Status Code for "Found"
HTTP_FOUND = 302;
/// HTTP Status Code for "See Other"
HTTP_SEEOTHER = 303;
/// HTTP Status Code for "Not Modified"
HTTP_NOTMODIFIED = 304;
/// HTTP Status Code for "Use Proxy"
HTTP_USEPROXY = 305;
/// HTTP Status Code for "Temporary Redirect"
HTTP_TEMPORARYREDIRECT = 307;
/// HTTP Status Code for "Permanent Redirect"
HTTP_PERMANENTREDIRECT = 308;
/// HTTP Status Code for "Bad Request"
HTTP_BADREQUEST = 400;
/// HTTP Status Code for "Unauthorized"
HTTP_UNAUTHORIZED = 401;
/// HTTP Status Code for "Forbidden"
HTTP_FORBIDDEN = 403;
/// HTTP Status Code for "Not Found"
HTTP_NOTFOUND = 404;
// HTTP Status Code for "Method Not Allowed"
HTTP_NOTALLOWED = 405;
// HTTP Status Code for "Not Acceptable"
HTTP_NOTACCEPTABLE = 406;
// HTTP Status Code for "Proxy Authentication Required"
HTTP_PROXYAUTHREQUIRED = 407;
/// HTTP Status Code for "Request Time-out"
HTTP_TIMEOUT = 408;
/// HTTP Status Code for "Conflict"
HTTP_CONFLICT = 409;
/// HTTP Status Code for "Payload Too Large"
HTTP_PAYLOADTOOLARGE = 413;
/// HTTP Status Code for "Range Not Satisfiable"
HTTP_RANGENOTSATISFIABLE = 416;
/// HTTP Status Code for "I'm a teapot"
HTTP_TEAPOT = 418;
/// HTTP Status Code for "Internal Server Error"
HTTP_SERVERERROR = 500;
/// HTTP Status Code for "Not Implemented"
HTTP_NOTIMPLEMENTED = 501;
/// HTTP Status Code for "Bad Gateway"
HTTP_BADGATEWAY = 502;
/// HTTP Status Code for "Service Unavailable"
HTTP_UNAVAILABLE = 503;
/// HTTP Status Code for "Gateway Timeout"
HTTP_GATEWAYTIMEOUT = 504;
/// HTTP Status Code for "HTTP Version Not Supported"
HTTP_HTTPVERSIONNONSUPPORTED = 505;
/// a fake response code, generated for client side panic failure/exception
// - for it is the number of a man
HTTP_CLIENTERROR = 666;
/// a fake response code, usedfor internal THttpAsyncServer asynchronous process
HTTP_ASYNCRESPONSE = 777;
/// the successful HTTP response codes after a GET request
HTTP_GET_OK = [HTTP_SUCCESS, HTTP_NOCONTENT, HTTP_PARTIALCONTENT];
/// retrieve the HTTP reason text from its integer code as PRawUtf8
// - e.g. StatusCodeToText(200)^='OK'
// - as defined in http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
// - returns the generic 'Invalid Request' for any unknown Code
function StatusCodeToText(Code: cardinal): PRawUtf8;
/// retrieve the HTTP reason text from its integer code
// - as defined in http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
procedure StatusCodeToReason(Code: cardinal; var Reason: RawUtf8);
/// convert any HTTP_* constant to an integer status code and its English text
// - returns e.g. '200 OK' or '404 Not Found', calling StatusCodeToText()
function StatusCodeToShort(Code: cardinal): TShort47;
/// returns true for successful HTTP status codes, i.e. in 200..399 range
// - will map mainly SUCCESS (200), CREATED (201), NOCONTENT (204),
// PARTIALCONTENT (206), NOTMODIFIED (304) or TEMPORARYREDIRECT (307) codes
// - any HTTP status not part of this range will be identified as erronous
// request e.g. in the web server statistics
function StatusCodeIsSuccess(Code: integer): boolean;
{$ifdef HASINLINE}inline;{$endif}
/// check the supplied HTTP header to not contain more than one EOL
// - to avoid unexpected HTTP body injection, e.g. from unsafe business code
function IsInvalidHttpHeader(head: PUtf8Char; headlen: PtrInt): boolean;
const
/// HTTP header name for the content type, as defined in the corresponding RFC
HEADER_CONTENT_TYPE = 'Content-Type: ';
/// HTTP header name for the content type, in upper case
// - as defined in the corresponding RFC
// - could be used e.g. with IdemPChar() to retrieve the Content-Type value
HEADER_CONTENT_TYPE_UPPER = 'CONTENT-TYPE: ';
/// HTTP header name for the client IP, in upper case
// - as defined in our HTTP server classes
// - could be used e.g. with IdemPChar() to retrieve the remote IP address
HEADER_REMOTEIP_UPPER = 'REMOTEIP: ';
/// HTTP header name for the authorization token, in upper case
// - could be used e.g. with IdemPChar() to retrieve a JWT value
// - will detect header computed e.g. by motmot.net.http's
// AuthorizationBearer()
HEADER_BEARER_UPPER = 'AUTHORIZATION: BEARER ';
/// MIME content type used for JSON communication (as used by the Microsoft
// WCF framework and the YUI framework)
// - no 'charset=UTF-8' encoding is necessary, as by specified by RFC 7159
JSON_CONTENT_TYPE = 'application/json';
/// HTTP header for MIME content type used for plain JSON
// - i.e. 'Content-Type: application/json'
JSON_CONTENT_TYPE_HEADER = HEADER_CONTENT_TYPE + JSON_CONTENT_TYPE;
/// MIME content type used for plain JSON, in upper case
// - could be used e.g. with IdemPChar() to retrieve the Content-Type value
JSON_CONTENT_TYPE_UPPER = 'APPLICATION/JSON';
/// HTTP header for MIME content type used for plain JSON, in upper case
// - could be used e.g. with IdemPChar() to retrieve the Content-Type value
JSON_CONTENT_TYPE_HEADER_UPPER =
HEADER_CONTENT_TYPE_UPPER + JSON_CONTENT_TYPE_UPPER;
/// MIME content type used for plain UTF-8 text
TEXT_CONTENT_TYPE = 'text/plain; charset=UTF-8';
/// HTTP header for MIME content type used for plain UTF-8 text
TEXT_CONTENT_TYPE_HEADER = HEADER_CONTENT_TYPE + TEXT_CONTENT_TYPE;
/// MIME content type used for UTF-8 encoded HTML
HTML_CONTENT_TYPE = 'text/html; charset=UTF-8';
/// HTTP header for MIME content type used for UTF-8 encoded HTML
HTML_CONTENT_TYPE_HEADER = HEADER_CONTENT_TYPE + HTML_CONTENT_TYPE;
/// MIME content type used for UTF-8 encoded XML
XML_CONTENT_TYPE = 'text/xml';
/// HTTP header for MIME content type used for UTF-8 encoded XML
XML_CONTENT_TYPE_HEADER = HEADER_CONTENT_TYPE + XML_CONTENT_TYPE;
/// MIME content type used for raw binary data
BINARY_CONTENT_TYPE = 'application/octet-stream';
/// MIME content type used for raw binary data, in upper case
BINARY_CONTENT_TYPE_UPPER = 'APPLICATION/OCTET-STREAM';
/// HTTP header for MIME content type used for raw binary data
BINARY_CONTENT_TYPE_HEADER = HEADER_CONTENT_TYPE + BINARY_CONTENT_TYPE;
/// MIME content type used for a JPEG picture
JPEG_CONTENT_TYPE = 'image/jpeg';
/// a IdemPPChar() compatible array of textual MIME content types
// - as used e.g. by IsHtmlContentTypeTextual()
CONTENT_TYPE_TEXTUAL: array[0..7] of PAnsiChar = (
JSON_CONTENT_TYPE_UPPER,
'TEXT/',
'APPLICATION/XML',
'APPLICATION/JSON',
'APPLICATION/JAVASCRIPT',
'APPLICATION/X-JAVASCRIPT',
'IMAGE/SVG+XML',
nil);
/// internal HTTP content-type for efficient static file sending
// - detected e.g. by http.sys' THttpApiServer.Request or via the NGINX
// X-Accel-Redirect header's THttpServer.Process (see
// THttpServer.NginxSendFileFrom) for direct sending with no local bufferring
// - the OutCustomHeader should contain the proper 'Content-type: ....'
// corresponding to the file (e.g. by calling GetMimeContentType() function)
STATICFILE_CONTENT_TYPE = '!STATICFILE';
/// internal HTTP content-type Header for efficient static file sending
STATICFILE_CONTENT_TYPE_HEADER =
HEADER_CONTENT_TYPE + STATICFILE_CONTENT_TYPE;
/// uppercase version of HTTP header for static file content serving
STATICFILE_CONTENT_TYPE_HEADER_UPPPER =
HEADER_CONTENT_TYPE_UPPER + STATICFILE_CONTENT_TYPE;
/// used to notify e.g. the THttpServerRequest not to wait for any response
// from the client
// - is not to be used in normal HTTP process, but may be used e.g. by
// TWebSocketProtocolRest.ProcessFrame() to avoid to wait for an incoming
// response from the other endpoint
NORESPONSE_CONTENT_TYPE = '!NORESPONSE';
/// HTTP body following RFC 2324 standard e.g. for banned IP
HTTP_BANIP_RESPONSE: string[201] =
'HTTP/1.0 418 I''m a teapot'#13#10 +
'Content-Length: 125'#13#10 +
'Content-Type: text/plain'#13#10#13#10 +
'Server refuses to brew coffee because it is currently a teapot.'#13#10 +
'Do not mess with it and retry from this IP in a few seconds.';
/// JSON compatible representation of a boolean value, i.e. 'false' and 'true'
// - can be used e.g. in logs, or anything accepting a ShortString
BOOL_STR: array[boolean] of string[7] = (
'false', 'true');
/// the JavaScript-like values of non-number IEEE constants
// - as recognized by FloatToShortNan, and used by TTextWriter.Add()
// when serializing such single/double/extended floating-point values
JSON_NAN: array[TFloatNan] of string[11] = (
'0', '"NaN"', '"Infinity"', '"-Infinity"');
var
/// MIME content type used for JSON communication
// - i.e. 'application/json' as stated by datatracker.ietf.org/doc/html/rfc7159
// - this global will be initialized with JSON_CONTENT_TYPE constant, to
// avoid a memory allocation each time it is assigned to a variable
JSON_CONTENT_TYPE_VAR: RawUtf8;
/// HTTP header for MIME content type used for plain JSON
// - this global will be initialized with JSON_CONTENT_TYPE_HEADER constant,
// to avoid a memory allocation each time it is assigned to a variable
JSON_CONTENT_TYPE_HEADER_VAR: RawUtf8;
/// can be used to avoid a memory allocation for res := 'null'
// - this global will be initialized with 'null' constant, to
// avoid a memory allocation each time it is assigned to a variable
NULL_STR_VAR: RawUtf8;
/// JSON compatible representation of a boolean value, i.e. 'false' and 'true'
// - can be used when a RawUtf8 string is expected
// - this global will be initialized with 'false' and 'true' constants, to
// avoid a memory allocation each time it is assigned to a variable
BOOL_UTF8: array[boolean] of RawUtf8;
const // some time conversion constants with Milli/Micro/NanoSec resolution
SecsPerHour = SecsPerMin * MinsPerHour; // missing in oldest Delphi
SecsPerDay = SecsPerMin * MinsPerDay;
SecsPerWeek = 7 * SecsPerDay;
SecsPerMonth = 2629746; // rough approximation of SecsPerDay * 365.2425 / 12
SecsPerYear = 12 * SecsPerMonth;
MilliSecsPerSec = 1000;
MilliSecsPerSecShl = 10; // 1 shl 10 = 1024 = rough approximation of 1000
MilliSecsPerMin = MilliSecsPerSec * SecsPerMin;
MilliSecsPerHour = MilliSecsPerMin * MinsPerHour;
MilliSecsPerDay = MilliSecsPerHour * HoursPerDay;
MicroSecsPerMilliSec = 1000;
MicroSecsPerSec = MicroSecsPerMilliSec * MilliSecsPerSec;
NanoSecsPerMicroSec = 1000;
NanoSecsPerMilliSec = NanoSecsPerMicroSec * MicroSecsPerMilliSec;
NanoSecsPerSec = NanoSecsPerMilliSec * MilliSecsPerSec;
{ ****************** Gather Operating System Information }
type
/// Exception types raised by this mormot.core.os unit
EOSException = class(ExceptionWithProps);
/// the known operating systems
// - it will also recognize most Linux distributions
TOperatingSystem = (
osUnknown,
osWindows,
osLinux,
osOSX,
osBSD,
osPOSIX,
osArch,
osAurox,
osDebian,
osFedora,
osGentoo,
osKnoppix,
osMint,
osMandrake,
osMandriva,
osNovell,
osUbuntu,
osSlackware,
osSolaris,
osSuse,
osSynology,
osTrustix,
osClear,
osUnited,
osRedHat,
osLFS,
osOracle,
osMageia,
osCentOS,
osCloud,
osXen,
osAmazon,
osCoreOS,
osAlpine,
osAndroid);
/// the recognized Windows versions
// - defined even outside OSWINDOWS to access e.g. from monitoring tools
TWindowsVersion = (
wUnknown,
w2000,
wXP,
wXP_64,
wServer2003,
wServer2003_R2,
wVista,
wVista_64,
wServer2008,
wServer2008_64,
wSeven,
wSeven_64,
wServer2008_R2,
wServer2008_R2_64,
wEight,
wEight_64,
wServer2012,
wServer2012_64,
wEightOne,
wEightOne_64,
wServer2012R2,
wServer2012R2_64,
wTen,
wTen_64,
wServer2016,
wServer2016_64,
wEleven,
wEleven_64,
wServer2019_64,
wServer2022_64,
wServer2025_64);
/// the running Operating System, encoded as a 32-bit integer
TOperatingSystemVersion = packed record
case os: TOperatingSystem of
osUnknown: (
b: array[0..2] of byte);
osWindows: (
win: TWindowsVersion;
winbuild: word);
osLinux: (
utsrelease: array[0..2] of byte);
end;
const
/// the recognized MacOS versions, as plain text
// - indexed from OSVersion32.utsrelease[2] kernel revision
MACOS_NAME: array[8 .. 25] of RawUtf8 = (
'10.4 Tiger',
'10.5 Leopard',
'10.6 Snow Leopard',
'10.7 Lion',
'10.8 Mountain Lion',
'10.9 Mavericks',
'10.10 Yosemite',
'10.11 El Capitan',
'10.12 Sierra',
'10.13 High Sierra',
'10.14 Mojave',
'10.15 Catalina',
'11 Big Sur',
'12 Monterey',
'13 Ventura',
'14 Sonoma',
'15 Sequoia',
'16 Next');
/// the recognized Windows versions, as plain text
// - defined even outside OSWINDOWS to allow process e.g. from monitoring tools
WINDOWS_NAME: array[TWindowsVersion] of RawUtf8 = (
'',
'2000',
'XP',
'XP 64bit',
'Server 2003',
'Server 2003 R2',
'Vista',
'Vista 64bit',
'Server 2008',
'Server 2008 64bit',
'7',
'7 64bit',
'Server 2008 R2',
'Server 2008 R2 64bit',
'8',
'8 64bit',
'Server 2012',
'Server 2012 64bit',
'8.1',
'8.1 64bit',
'Server 2012 R2',
'Server 2012 R2 64bit',
'10',
'10 64bit',
'Server 2016',
'Server 2016 64bit',
'11',
'11 64bit',
'Server 2019 64bit',
'Server 2022 64bit',
'Server 2025 64bit');
/// the recognized Windows versions which are 32-bit
WINDOWS_32 = [
w2000,
wXP,
wServer2003,
wServer2003_R2,
wVista,
wServer2008,
wSeven,
wServer2008_R2,
wEight,
wServer2012,
wEightOne,
wServer2012R2,
wTen,
wServer2016,
wEleven];
/// translate one operating system (and distribution) into a its common name
OS_NAME: array[TOperatingSystem] of RawUtf8 = (
'Unknown',
'Windows',
'Linux',
'OSX',
'BSD',
'POSIX',
'Arch',
'Aurox',
'Debian',
'Fedora',
'Gentoo',
'Knoppix',
'Mint',
'Mandrake',
'Mandriva',
'Novell',
'Ubuntu',
'Slackware',
'Solaris',
'Suse',
'Synology',
'Trustix',
'Clear',
'United',
'RedHat',
'LFS',
'Oracle',
'Mageia',
'CentOS',
'Cloud',
'Xen',
'Amazon',
'CoreOS',
'Alpine',
'Android');
/// translate one operating system (and distribution) into a single character
// - may be used internally e.g. for a HTTP User-Agent header, as with
// TFileVersion.UserAgent and UserAgentParse()
OS_INITIAL: array[TOperatingSystem] of AnsiChar = (
'?', // Unknown
'W', // Windows
'L', // Linux
'X', // OSX
'B', // BSD
'P', // POSIX
'A', // Arch
'a', // Aurox
'D', // Debian
'F', // Fedora
'G', // Gentoo
'K', // Knoppix
'M', // Mint
'm', // Mandrake
'n', // Mandriva
'N', // Novell
'U', // Ubuntu
'S', // Slackware
's', // Solaris
'u', // Suse
'Y', // Synology
'T', // Trustix
'C', // Clear
't', // United
'R', // RedHat
'l', // LFS
'O', // Oracle
'G', // Mageia
'c', // CentOS
'd', // Cloud
'x', // Xen
'Z', // Amazon
'r', // CoreOS
'p', // Alpine
'J' // Android (J=JVM)
);
/// the operating systems items which actually have a Linux kernel
OS_LINUX = [
osLinux,
osArch .. osAndroid];
/// the compiler family used
COMP_TEXT = {$ifdef FPC}'Fpc'{$else}'Delphi'{$endif};
/// the target Operating System used for compilation, as short text
OS_TEXT =
{$ifdef OSWINDOWS}
'Win';
{$else} {$ifdef OSDARWIN}
'OSX';
{$else}{$ifdef OSBSD}
'BSD';
{$else} {$ifdef OSANDROID}
'Android';
{$else} {$ifdef OSLINUX}
'Linux';
{$else}
'Posix';
{$endif OSLINUX}
{$endif OSANDROID}
{$endif OSBSD}
{$endif OSDARWIN}
{$endif OSWINDOWS}
/// the CPU architecture used for compilation
CPU_ARCH_TEXT =
{$ifdef CPUX86}
'x86'
{$else} {$ifdef CPUX64}
'x64'
{$else} {$ifdef CPUARM}
'arm' +
{$else} {$ifdef CPUAARCH64}
'aarch' +
{$ifdef CPUPOWERPC}
'ppc' +
{$else} {$ifdef CPUSPARC}
'sparc' +
{$endif CPUSPARC}
{$endif CPUPOWERPC}
{$endif CPUARM}
{$endif CPUAARCH64}
{$ifdef CPU32}
'32'
{$else}
'64'
{$endif CPU32}
{$endif CPUX64}
{$endif CPUX86};
var
/// the target Operating System used for compilation, as TOperatingSystem
// - a specific Linux distribution may be detected instead of plain osLinux
OS_KIND: TOperatingSystem =
{$ifdef OSWINDOWS}
osWindows
{$else} {$ifdef OSDARWIN}
osOSX
{$else} {$ifdef OSBSD}
osBSD
{$else} {$ifdef OSANDROID}
osAndroid
{$else} {$ifdef OSLINUX}
osLinux
{$else}
osPOSIX
{$endif OSLINUX}
{$endif OSANDROID}
{$endif OSBSD}
{$endif OSDARWIN}
{$endif OSWINDOWS};
/// the current Operating System version, as retrieved for the current process
// - contains e.g. 'Windows Seven 64 SP1 (6.1.7601)' or 'Windows XP SP3 (5.1.2600)' or
// 'Windows 10 64bit 22H2 (10.0.19045.4046)' or 'macOS 13 Ventura (Darwin 22.3.0)' or
// 'Ubuntu 16.04.5 LTS - Linux 3.13.0 110 generic#157 Ubuntu SMP Mon Feb 20 11:55:25 UTC 2017'
OSVersionText: RawUtf8;
/// some addition system information as text, e.g. 'Wine 1.1.5'
// - also always appended to OSVersionText high-level description
// - use if PosEx('Wine', OSVersionInfoEx) > 0 then to check for Wine presence
OSVersionInfoEx: RawUtf8;
/// the current Operating System version, as retrieved for the current process
// and computed by ToTextOS(OSVersionInt32)
// - contains e.g. 'Windows Vista' or 'Ubuntu Linux 5.4.0' or
// 'macOS 13 Ventura 22.3.0'
OSVersionShort: RawUtf8;
{$ifdef OSWINDOWS}
/// on Windows, the Update Build Revision as shown with the "ver/winver" command
// - to track the current update state of the system
WindowsUbr: integer;
/// on Windows, the ready-to-be-displayed text version of the system
// - e.g. 'Windows 10 Entreprise N'
WindowsProductName: RawUtf8;
/// on Windows, the ready-to-be-displayed text version of the system
// - e.g. '22H2'
WindowsDisplayVersion: RawUtf8;
{$endif OSWINDOWS}
/// some textual information about the current CPU and its known cache
// - contains e.g. '4 x Intel(R) Core(TM) i5-7300U CPU @ 2.60GHz [3MB]'
CpuInfoText: RawUtf8;
/// the on-chip cache size, in bytes, as returned by the OS
// - retrieved from /proc/cpuinfo "cache size" entry (L3 cache) on Linux or
// CpuCache[3/4].Size (from GetLogicalProcessorInformation) on Windows
CpuCacheSize: cardinal;
/// the available cache information as returned by the OS
// - e.g. 'L1=2*32KB L2=256KB L3=3MB' on Windows or '3072 KB' on Linux
CpuCacheText: RawUtf8;
/// some textual information about the current computer hardware, from BIOS
// - contains e.g. 'LENOVO 20HES23B0U ThinkPad T470'
BiosInfoText: RawUtf8;
/// how many hardware CPU sockets are defined on this system
// - i.e. the number of physical CPU slots, not the number of logical CPU
// cores as returned by SystemInfo.dwNumberOfProcessors
// - as used e.g. by SetThreadAffinity()
CpuSockets: integer;
/// Level 1 to 4 CPU caches as returned by GetLogicalProcessorInformation
// - yes, Intel introduced a Level 4 cache (eDRAM) with some Haswell/Iris CPUs
// - this information is not retrieved on all Linux / POSIX systems yet
// - only Unified or Data caches are include (not Instruction or Trace)
// - note: some CPU - like the Apple M1 - have 128 bytes of LineSize
CpuCache: array[1..4] of record
Count, Size, LineSize: cardinal;
end;
{$ifdef OSLINUXANDROID}
/// contains the Flags: or Features: value of Linux /proc/cpuinfo
CpuInfoFeatures: RawUtf8;
{$endif OSLINUXANDROID}
/// the running Operating System
OSVersion32: TOperatingSystemVersion;
/// the running Operating System, encoded as a 32-bit integer
OSVersionInt32: integer absolute OSVersion32;
/// convert an Operating System type into its text representation
// - returns e.g. 'Windows Vista' or 'Ubuntu' or 'macOS 13 Ventura'
function ToText(const osv: TOperatingSystemVersion): RawUtf8; overload;
/// convert an Operating System type into its one-word text representation
// - returns e.g. 'Vista' or 'Ubuntu' or 'OSX'
function ToTextShort(const osv: TOperatingSystemVersion): RawUtf8;
/// convert a 32-bit Operating System type into its full text representation
// - including the kernel revision (not the distribution version) on POSIX systems
// - returns e.g. 'Windows Vista', 'Windows 11 64-bit 22000' or 'Ubuntu Linux 5.4.0'
function ToTextOS(osint32: integer): RawUtf8;
/// check if the current OS (i.e. OS_KIND value) match a description
// - will handle osPosix and osLinux as generic detection of those systems
// - osUnknown will always return true
function MatchOS(os: TOperatingSystem): boolean;
type
/// the recognized ARM/AARCH64 CPU types
// - https://github.com/karelzak/util-linux/blob/master/sys-utils/lscpu-arm.c
// - is defined on all platforms for cross-system use
TArmCpuType = (
actUnknown,
actARM810,
actARM920,
actARM922,
actARM926,
actARM940,
actARM946,
actARM966,
actARM1020,
actARM1022,
actARM1026,
actARM11MPCore,
actARM1136,
actARM1156,
actARM1176,
actCortexA5,
actCortexA7,
actCortexA8,
actCortexA9,
actCortexA12,
actCortexA15,
actCortexA17,
actCortexR4,
actCortexR5,
actCortexR7,
actCortexR8,
actCortexM0,
actCortexM1,
actCortexM3,
actCortexM4,
actCortexM7,
actCortexM0P,
actCortexA32,
actCortexA53,
actCortexA35,
actCortexA55,
actCortexA65,
actCortexA57,
actCortexA72,
actCortexA73,
actCortexA75,
actCortexA76,
actNeoverseN1,
actCortexA77,
actCortexA76AE,
actCortexR52,
actCortexM23,
actCortexM33,
actNeoverseV1,
actCortexA78,
actCortexA78AE,
actCortexX1,
actCortex510,
actCortex710,
actCortexX2,
actNeoverseN2,
actNeoverseE1,
actCortexA78C,
actCortexX1C,
actCortexA715,
actCortexX3,
actNeoverseV2,
actCortexA520,
actCortexA720,
actCortexX4,
actNeoverseV3,
actCortextX925,
actCortextA725,
actNeoverseN3);
/// a set of recognized ARM/AARCH64 CPU types
TArmCpuTypes = set of TArmCpuType;
/// the recognized ARM/AARCH64 CPU hardware implementers
// - https://github.com/karelzak/util-linux/blob/master/sys-utils/lscpu-arm.c
TArmCpuImplementer = (
aciUnknown,
aciARM,
aciBroadcom,
aciCavium,
aciDEC,
aciFUJITSU,
aciHiSilicon,
aciInfineon,
aciMotorola,
aciNVIDIA,
aciAPM,
aciQualcomm,
aciSamsung,
aciMarvell,
aciApple,
aciFaraday,
aciIntel,
aciMicrosoft,
aciPhytium,
aciAmpere);
/// a set of recognized ARM/AARCH64 CPU hardware implementers
TArmCpuImplementers = set of TArmCpuImplementer;
/// recognize a given ARM/AARCH64 CPU from its 12-bit hardware ID
function ArmCpuType(id: word): TArmCpuType;
/// recognize a given ARM/AARCH64 CPU type name from its 12-bit hardware ID
function ArmCpuTypeName(act: TArmCpuType; id: word): RawUtf8;
/// recognize a given ARM/AARCH64 CPU implementer from its 8-bit hardware ID
function ArmCpuImplementer(id: byte): TArmCpuImplementer;
/// recognize a given ARM/AARCH64 CPU implementer name from its 8-bit hardware ID
function ArmCpuImplementerName(aci: TArmCpuImplementer; id: word): RawUtf8;
const
/// contains the Delphi/FPC Compiler Version as text
// - e.g. 'Delphi 10.3 Rio', 'Delphi 2010' or 'Free Pascal 3.3.1'
COMPILER_VERSION: RawUtf8 =
{$ifdef FPC}
'Free Pascal ' + {$I %FPCVERSION%} // FPC makes it simple
{$else}
'Delphi'
{$if defined(VER140)} + ' 6'
{$elseif defined(VER150)} + ' 7'
{$elseif defined(VER160)} + ' 8'
{$elseif defined(VER170)} + ' 2005'
{$elseif defined(VER185)} + ' 2007'
{$elseif defined(VER180)} + ' 2006'
{$elseif defined(VER200)} + ' 2009'
{$elseif defined(VER210)} + ' 2010'
{$elseif defined(VER220)} + ' XE'
{$elseif defined(VER230)} + ' XE2'
{$elseif defined(VER240)} + ' XE3'
{$elseif defined(VER250)} + ' XE4'
{$elseif defined(VER260)} + ' XE5'
{$elseif defined(VER265)} + ' AppMethod 1'
{$elseif defined(VER270)} + ' XE6'
{$elseif defined(VER280)} + ' XE7'
{$elseif defined(VER290)} + ' XE8'
{$elseif defined(VER300)} + ' 10 Seattle'
{$elseif defined(VER310)} + ' 10.1 Berlin'
{$elseif defined(VER320)} + ' 10.2 Tokyo'
{$elseif defined(VER330)} + ' 10.3 Rio'
{$elseif defined(VER340)} + ' 10.4 Sydney'
{$elseif defined(VER350)} + ' 11'
{$if declared(RTLVersion113)} + '.3' {$else}
{$if declared(RTLVersion112)} + '.2' {$else}
{$if declared(RTLVersion111)} + '.1' {$ifend} {$ifend} {$ifend}
+ ' Alexandria'
{$elseif defined(VER360)} + ' 12'
{$if declared(RTLVersion123)} + '.3' {$else}
{$if declared(RTLVersion122)} + '.2' {$else}
{$if declared(RTLVersion121)} + '.1' {$ifend} {$ifend} {$ifend}
+ ' Athens'
{$elseif defined(VER370)} + ' 13 Next'
{$ifend}
{$endif FPC}
{$ifdef CPU64} + ' 64 bit' {$else} + ' 32 bit' {$endif};
{$ifndef PUREMORMOT2}
const
HTTP_RESP_STATICFILE = STATICFILE_CONTENT_TYPE;
/// deprecated function: use COMPILER_VERSION constant instead
function GetDelphiCompilerVersion: RawUtf8; deprecated;
{$endif PUREMORMOT2}
{$ifdef OSWINDOWS}
{$ifdef UNICODE}
const
/// a global constant to be appended for Windows Ansi or Wide API names
// - match the Wide API on Delphi, since String=UnicodeString
// - you should not use this suffix, but the 'W' API everywhere, with proper
// conversion into RawUtf8 or TFileName/string
_AW = 'W';
{$else}
const
/// a global constant to be appended for Windows Ansi or Wide API names
// - match the Ansi API oldest Delphi, where String=AnsiString
// - but won't always match the Ansi API on FPC, because Lazarus forces
// CP_UTF8, so you should NOT use this suffix, but the '*W' API everywhere,
// with proper conversion into RawUtf8 or TFileName/string
_AW = 'A';
type