Kitxuuu commited on
Commit
f0b5d86
·
verified ·
1 Parent(s): 6ca7e7f

Add files using upload-large-folder tool

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. .gitattributes +2 -0
  2. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/codec/test/TestFreeRDPCodecNCrush.c +122 -0
  3. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/crypto/CMakeLists.txt +58 -0
  4. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/crypto/base64.c +523 -0
  5. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/crypto/ber.c +733 -0
  6. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/crypto/cert_common.c +242 -0
  7. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/crypto/cert_common.h +53 -0
  8. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/crypto/certificate.c +1926 -0
  9. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/crypto/certificate_data.c +276 -0
  10. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/crypto/crypto.c +261 -0
  11. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/crypto/crypto.h +55 -0
  12. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/crypto/der.c +106 -0
  13. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/crypto/er.c +448 -0
  14. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/crypto/opensslcompat.c +45 -0
  15. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/crypto/opensslcompat.h +114 -0
  16. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/crypto/privatekey.c +552 -0
  17. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/crypto/tls.c +2106 -0
  18. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/crypto/tls.h +131 -0
  19. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/crypto/x509_utils.c +919 -0
  20. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/crypto/x509_utils.h +65 -0
  21. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/gdi/test/TestGdiClip.c +345 -0
  22. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/gdi/test/TestGdiEllipse.c +169 -0
  23. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/gdi/test/TestGdiLine.c +724 -0
  24. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/gdi/test/TestGdiRect.c +175 -0
  25. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/gdi/test/TestGdiRegion.c +256 -0
  26. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/gdi/test/TestGdiRop3.c +219 -0
  27. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/gdi/test/helpers.h +37 -0
  28. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/locale/CMakeLists.txt +86 -0
  29. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/locale/keyboard.c +508 -0
  30. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/locale/keyboard_apple.c +241 -0
  31. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/locale/keyboard_apple.h +28 -0
  32. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/locale/keyboard_layout.c +1662 -0
  33. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/locale/keyboard_sun.c +284 -0
  34. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/locale/keyboard_sun.h +27 -0
  35. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/locale/keyboard_x11.c +138 -0
  36. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/locale/keyboard_x11.h +27 -0
  37. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/locale/keyboard_xkbfile.c +510 -0
  38. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/locale/keyboard_xkbfile.h +30 -0
  39. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/locale/liblocale.h +39 -0
  40. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/locale/locale.c +888 -0
  41. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/locale/xkb_layout_ids.c +871 -0
  42. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/locale/xkb_layout_ids.h +28 -0
  43. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/primitives/CMakeLists.txt +100 -0
  44. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/primitives/prim_YCoCg.c +80 -0
  45. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/primitives/prim_YUV.c +2355 -0
  46. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/primitives/prim_YUV.h +31 -0
  47. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/primitives/prim_add.c +82 -0
  48. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/primitives/prim_add.h +30 -0
  49. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/primitives/prim_alphaComp.c +96 -0
  50. local-test-freerdp-delta-01/afc-freerdp/libfreerdp/primitives/prim_alphaComp.h +30 -0
.gitattributes CHANGED
@@ -115,3 +115,5 @@ local-test-sqlite3-delta-03/afc-sqlite3/test/fuzzdata8.db filter=lfs diff=lfs me
115
  local-test-sqlite3-delta-03/afc-sqlite3/test/fuzzdata5.db filter=lfs diff=lfs merge=lfs -text
116
  local-test-libexif-delta-01-exif-003/fuzz-tooling/infra/cifuzz/test_data/timeout_fuzzer filter=lfs diff=lfs merge=lfs -text
117
  local-test-libpostal-full-01/afc-libpostal/src/transliteration_data.c filter=lfs diff=lfs merge=lfs -text
 
 
 
115
  local-test-sqlite3-delta-03/afc-sqlite3/test/fuzzdata5.db filter=lfs diff=lfs merge=lfs -text
116
  local-test-libexif-delta-01-exif-003/fuzz-tooling/infra/cifuzz/test_data/timeout_fuzzer filter=lfs diff=lfs merge=lfs -text
117
  local-test-libpostal-full-01/afc-libpostal/src/transliteration_data.c filter=lfs diff=lfs merge=lfs -text
118
+ local-test-libxml2-delta-01/fuzz-tooling/infra/cifuzz/test_data/timeout_fuzzer filter=lfs diff=lfs merge=lfs -text
119
+ local-test-freerdp-full-01/fuzz-tooling/infra/cifuzz/test_data/timeout_fuzzer filter=lfs diff=lfs merge=lfs -text
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/codec/test/TestFreeRDPCodecNCrush.c ADDED
@@ -0,0 +1,122 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <winpr/crt.h>
2
+ #include <winpr/print.h>
3
+
4
+ #include "../ncrush.h"
5
+
6
+ static const BYTE TEST_BELLS_DATA[] = "for.whom.the.bell.tolls,.the.bell.tolls.for.thee!";
7
+
8
+ static const BYTE TEST_BELLS_NCRUSH[] =
9
+ "\xfb\x1d\x7e\xe4\xda\xc7\x1d\x70\xf8\xa1\x6b\x1f\x7d\xc0\xbe\x6b"
10
+ "\xef\xb5\xef\x21\x87\xd0\xc5\xe1\x85\x71\xd4\x10\x16\xe7\xda\xfb"
11
+ "\x1d\x7e\xe4\xda\x47\x1f\xb0\xef\xbe\xbd\xff\x2f";
12
+
13
+ static BOOL test_NCrushCompressBells(void)
14
+ {
15
+ BOOL rc = FALSE;
16
+ int status = 0;
17
+ UINT32 Flags = 0;
18
+ const BYTE* pDstData = NULL;
19
+ BYTE OutputBuffer[65536] = { 0 };
20
+ const UINT32 SrcSize = sizeof(TEST_BELLS_DATA) - 1;
21
+ const BYTE* pSrcData = TEST_BELLS_DATA;
22
+ const UINT32 expectedSize = sizeof(TEST_BELLS_NCRUSH) - 1;
23
+ UINT32 DstSize = sizeof(OutputBuffer);
24
+ NCRUSH_CONTEXT* ncrush = ncrush_context_new(TRUE);
25
+
26
+ if (!ncrush)
27
+ return rc;
28
+
29
+ status = ncrush_compress(ncrush, pSrcData, SrcSize, OutputBuffer, &pDstData, &DstSize, &Flags);
30
+
31
+ if (status < 0)
32
+ goto fail;
33
+
34
+ printf("status: %d Flags: 0x%08" PRIX32 " DstSize: %" PRIu32 "\n", status, Flags, DstSize);
35
+
36
+ if (DstSize != expectedSize)
37
+ {
38
+ printf("NCrushCompressBells: output size mismatch: Actual: %" PRIu32 ", Expected: %" PRIu32
39
+ "\n",
40
+ DstSize, expectedSize);
41
+ printf("Actual\n");
42
+ BitDump(__func__, WLOG_INFO, pDstData, DstSize * 8, 0);
43
+ printf("Expected\n");
44
+ BitDump(__func__, WLOG_INFO, TEST_BELLS_NCRUSH, expectedSize * 8, 0);
45
+ goto fail;
46
+ }
47
+
48
+ if (memcmp(pDstData, TEST_BELLS_NCRUSH, DstSize) != 0)
49
+ {
50
+ printf("NCrushCompressBells: output mismatch\n");
51
+ printf("Actual\n");
52
+ BitDump(__func__, WLOG_INFO, pDstData, DstSize * 8, 0);
53
+ printf("Expected\n");
54
+ BitDump(__func__, WLOG_INFO, TEST_BELLS_NCRUSH, expectedSize * 8, 0);
55
+ goto fail;
56
+ }
57
+
58
+ rc = TRUE;
59
+ fail:
60
+ ncrush_context_free(ncrush);
61
+ return rc;
62
+ }
63
+
64
+ static BOOL test_NCrushDecompressBells(void)
65
+ {
66
+ BOOL rc = FALSE;
67
+ int status = 0;
68
+ UINT32 Flags = 0;
69
+ const BYTE* pSrcData = NULL;
70
+ UINT32 SrcSize = 0;
71
+ UINT32 DstSize = 0;
72
+ UINT32 expectedSize = 0;
73
+ const BYTE* pDstData = NULL;
74
+ NCRUSH_CONTEXT* ncrush = ncrush_context_new(FALSE);
75
+
76
+ if (!ncrush)
77
+ return rc;
78
+
79
+ SrcSize = sizeof(TEST_BELLS_NCRUSH) - 1;
80
+ pSrcData = (const BYTE*)TEST_BELLS_NCRUSH;
81
+ Flags = PACKET_COMPRESSED | 2;
82
+ expectedSize = sizeof(TEST_BELLS_DATA) - 1;
83
+ status = ncrush_decompress(ncrush, pSrcData, SrcSize, &pDstData, &DstSize, Flags);
84
+
85
+ if (status < 0)
86
+ goto fail;
87
+
88
+ printf("Flags: 0x%08" PRIX32 " DstSize: %" PRIu32 "\n", Flags, DstSize);
89
+
90
+ if (DstSize != expectedSize)
91
+ {
92
+ printf("NCrushDecompressBells: output size mismatch: Actual: %" PRIu32
93
+ ", Expected: %" PRIu32 "\n",
94
+ DstSize, expectedSize);
95
+ goto fail;
96
+ }
97
+
98
+ if (memcmp(pDstData, TEST_BELLS_DATA, DstSize) != 0)
99
+ {
100
+ printf("NCrushDecompressBells: output mismatch\n");
101
+ goto fail;
102
+ }
103
+
104
+ rc = TRUE;
105
+ fail:
106
+ ncrush_context_free(ncrush);
107
+ return rc;
108
+ }
109
+
110
+ int TestFreeRDPCodecNCrush(int argc, char* argv[])
111
+ {
112
+ WINPR_UNUSED(argc);
113
+ WINPR_UNUSED(argv);
114
+
115
+ if (!test_NCrushCompressBells())
116
+ return -1;
117
+
118
+ if (!test_NCrushDecompressBells())
119
+ return -1;
120
+
121
+ return 0;
122
+ }
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/crypto/CMakeLists.txt ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # FreeRDP: A Remote Desktop Protocol Implementation
2
+ # libfreerdp-crypto cmake build script
3
+ #
4
+ # Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # 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, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+
18
+ set(MODULE_NAME "freerdp-crypto")
19
+ set(MODULE_PREFIX "FREERDP_CRYPTO")
20
+
21
+ freerdp_module_add(
22
+ er.c
23
+ der.c
24
+ ber.c
25
+ per.c
26
+ base64.c
27
+ x509_utils.c
28
+ x509_utils.h
29
+ cert_common.h
30
+ cert_common.c
31
+ privatekey.c
32
+ privatekey.h
33
+ certificate.c
34
+ certificate.h
35
+ certificate_data.c
36
+ certificate_store.c
37
+ crypto.c
38
+ tls.c
39
+ tls.h
40
+ opensslcompat.c
41
+ )
42
+
43
+ freerdp_include_directory_add(${OPENSSL_INCLUDE_DIR})
44
+
45
+ freerdp_library_add(${OPENSSL_LIBRARIES})
46
+
47
+ if(MBEDTLS_FOUND)
48
+ freerdp_include_directory_add(${MBEDTLS_INCLUDE_DIR})
49
+ freerdp_library_add(${MBEDTLS_LIBRARIES})
50
+ endif()
51
+
52
+ if(WIN32)
53
+ freerdp_library_add(ws2_32)
54
+ endif()
55
+
56
+ if(BUILD_TESTING_INTERNAL OR BUILD_TESTING)
57
+ add_subdirectory(test)
58
+ endif()
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/crypto/base64.c ADDED
@@ -0,0 +1,523 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * Base64 Encoding & Decoding
4
+ *
5
+ * Copyright 2011-2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+
20
+ #include <freerdp/config.h>
21
+
22
+ #include <winpr/crt.h>
23
+
24
+ #include <freerdp/crypto/crypto.h>
25
+
26
+ static const BYTE enc_base64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
27
+ static const BYTE enc_base64url[] =
28
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
29
+
30
+ static const signed char dec_base64url[] = {
31
+ -1, /* 0 000 00 00000000 NUL &#00; Null character */
32
+ -1, /* 1 001 01 00000001 SOH &#01; Start of Heading */
33
+ -1, /* 2 002 02 00000010 STX &#02; Start of Text */
34
+ -1, /* 3 003 03 00000011 ETX &#03; End of Text */
35
+ -1, /* 4 004 04 00000100 EOT &#04; End of Transmission */
36
+ -1, /* 5 005 05 00000101 ENQ &#05; Enquiry */
37
+ -1, /* 6 006 06 00000110 ACK &#06; Acknowledge */
38
+ -1, /* 7 007 07 00000111 BEL &#07; Bell, Alert */
39
+ -1, /* 8 010 08 00001000 BS &#08; Backspace */
40
+ -1, /* 9 011 09 00001001 HT &#09; Horizontal Tab */
41
+ -1, /* 10 012 0A 00001010 LF &#10; Line Feed */
42
+ -1, /* 11 013 0B 00001011 VT &#11; Vertical Tabulation */
43
+ -1, /* 12 014 0C 00001100 FF &#12; Form Feed */
44
+ -1, /* 13 015 0D 00001101 CR &#13; Carriage Return */
45
+ -1, /* 14 016 0E 00001110 SO &#14; Shift Out */
46
+ -1, /* 15 017 0F 00001111 SI &#15; Shift In */
47
+ -1, /* 16 020 10 00010000 DLE &#16; Data Link Escape */
48
+ -1, /* 17 021 11 00010001 DC1 &#17; Device Control One (XON) */
49
+ -1, /* 18 022 12 00010010 DC2 &#18; Device Control Two */
50
+ -1, /* 19 023 13 00010011 DC3 &#19; Device Control Three (XOFF) */
51
+ -1, /* 20 024 14 00010100 DC4 &#20; Device Control Four */
52
+ -1, /* 21 025 15 00010101 NAK &#21; Negative Acknowledge */
53
+ -1, /* 22 026 16 00010110 SYN &#22; Synchronous Idle */
54
+ -1, /* 23 027 17 00010111 ETB &#23; End of Transmission Block */
55
+ -1, /* 24 030 18 00011000 CAN &#24; Cancel */
56
+ -1, /* 25 031 19 00011001 EM &#25; End of medium */
57
+ -1, /* 26 032 1A 00011010 SUB &#26; Substitute */
58
+ -1, /* 27 033 1B 00011011 ESC &#27; Escape */
59
+ -1, /* 28 034 1C 00011100 FS &#28; File Separator */
60
+ -1, /* 29 035 1D 00011101 GS &#29; Group Separator */
61
+ -1, /* 30 036 1E 00011110 RS &#30; Record Separator */
62
+ -1, /* 31 037 1F 00011111 US &#31; Unit Separator */
63
+ -1, /* 32 040 20 00100000 SP &#32; Space */
64
+ -1, /* 33 041 21 00100001 ! &#33; &excl; Exclamation mark */
65
+ -1, /* 34 042 22 00100010 " &#34; &quot; Double quotes (or speech marks) */
66
+ -1, /* 35 043 23 00100011 # &#35; &num; Number sign */
67
+ -1, /* 36 044 24 00100100 $ &#36; &dollar; Dollar */
68
+ -1, /* 37 045 25 00100101 % &#37; &percnt; Per cent sign */
69
+ -1, /* 38 046 26 00100110 & &#38; &amp; Ampersand */
70
+ -1, /* 39 047 27 00100111 ' &#39; &apos; Single quote */
71
+ -1, /* 40 050 28 00101000 ( &#40; &lparen; Open parenthesis (or open
72
+ * bracket)
73
+ */
74
+ -1, /* 41 051 29 00101001 ) &#41; &rparen; Close parenthesis (or close
75
+ * bracket)
76
+ */
77
+ -1, /* 42 052 2A 00101010 * &#42; &ast; Asterisk */
78
+ -1, /* 43 053 2B 00101011 + &#43; &plus; Plus */
79
+ -1, /* 44 054 2C 00101100 , &#44; &comma; Comma */
80
+ 62, /* 45 055 2D 00101101 - &#45; Hyphen-minus */
81
+ -1, /* 46 056 2E 00101110 . &#46; &period; Period, dot or full stop */
82
+ -1, /* 47 057 2F 00101111 / &#47; &sol; Slash or divide */
83
+ 52, /* 48 060 30 00110000 0 &#48; Zero */
84
+ 53, /* 49 061 31 00110001 1 &#49; One */
85
+ 54, /* 50 062 32 00110010 2 &#50; Two */
86
+ 55, /* 51 063 33 00110011 3 &#51; Three */
87
+ 56, /* 52 064 34 00110100 4 &#52; Four */
88
+ 57, /* 53 065 35 00110101 5 &#53; Five */
89
+ 58, /* 54 066 36 00110110 6 &#54; Six */
90
+ 59, /* 55 067 37 00110111 7 &#55; Seven */
91
+ 60, /* 56 070 38 00111000 8 &#56; Eight */
92
+ 61, /* 57 071 39 00111001 9 &#57; Nine */
93
+ -1, /* 58 072 3A 00111010 : &#58; &colon; Colon */
94
+ -1, /* 59 073 3B 00111011 ; &#59; &semi; Semicolon */
95
+ -1, /* 60 074 3C 00111100 < &#60; &lt; Less than (or open angled bracket)
96
+ */
97
+ -1, /* 61 075 3D 00111101 = &#61; &equals; Equals */
98
+ -1, /* 62 076 3E 00111110 > &#62; &gt; Greater than (or close angled
99
+ * bracket)
100
+ */
101
+ -1, /* 63 077 3F 00111111 ? &#63; &quest; Question mark */
102
+ -1, /* 64 100 40 01000000 @ &#64; &commat; At sign */
103
+ 0, /* 65 101 41 01000001 A &#65; Uppercase A */
104
+ 1, /* 66 102 42 01000010 B &#66; Uppercase B */
105
+ 2, /* 67 103 43 01000011 C &#67; Uppercase C */
106
+ 3, /* 68 104 44 01000100 D &#68; Uppercase D */
107
+ 4, /* 69 105 45 01000101 E &#69; Uppercase E */
108
+ 5, /* 70 106 46 01000110 F &#70; Uppercase F */
109
+ 6, /* 71 107 47 01000111 G &#71; Uppercase G */
110
+ 7, /* 72 110 48 01001000 H &#72; Uppercase H */
111
+ 8, /* 73 111 49 01001001 I &#73; Uppercase I */
112
+ 9, /* 74 112 4A 01001010 J &#74; Uppercase J */
113
+ 10, /* 75 113 4B 01001011 K &#75; Uppercase K */
114
+ 11, /* 76 114 4C 01001100 L &#76; Uppercase L */
115
+ 12, /* 77 115 4D 01001101 M &#77; Uppercase M */
116
+ 13, /* 78 116 4E 01001110 N &#78; Uppercase N */
117
+ 14, /* 79 117 4F 01001111 O &#79; Uppercase O */
118
+ 15, /* 80 120 50 01010000 P &#80; Uppercase P */
119
+ 16, /* 81 121 51 01010001 Q &#81; Uppercase Q */
120
+ 17, /* 82 122 52 01010010 R &#82; Uppercase R */
121
+ 18, /* 83 123 53 01010011 S &#83; Uppercase S */
122
+ 19, /* 84 124 54 01010100 T &#84; Uppercase T */
123
+ 20, /* 85 125 55 01010101 U &#85; Uppercase U */
124
+ 21, /* 86 126 56 01010110 V &#86; Uppercase V */
125
+ 22, /* 87 127 57 01010111 W &#87; Uppercase W */
126
+ 23, /* 88 130 58 01011000 X &#88; Uppercase X */
127
+ 24, /* 89 131 59 01011001 Y &#89; Uppercase Y */
128
+ 25, /* 90 132 5A 01011010 Z &#90; Uppercase Z */
129
+ -1, /* 91 133 5B 01011011 [ &#91; &lsqb; Opening bracket */
130
+ -1, /* 92 134 5C 01011100 \ &#92; &bsol; Backslash */
131
+ -1, /* 93 135 5D 01011101 ] &#93; &rsqb; Closing bracket */
132
+ -1, /* 94 136 5E 01011110 ^ &#94; &Hat; Caret - circumflex */
133
+ 63, /* 95 137 5F 01011111 _ &#95; &lowbar; Underscore */
134
+ -1, /* 96 140 60 01100000 ` &#96; &grave; Grave accent */
135
+ 26, /* 97 141 61 01100001 a &#97; Lowercase a */
136
+ 27, /* 98 142 62 01100010 b &#98; Lowercase b */
137
+ 28, /* 99 143 63 01100011 c &#99; Lowercase c */
138
+ 29, /* 100 144 64 01100100 d &#100; Lowercase d */
139
+ 30, /* 101 145 65 01100101 e &#101; Lowercase e */
140
+ 31, /* 102 146 66 01100110 f &#102; Lowercase f */
141
+ 32, /* 103 147 67 01100111 g &#103; Lowercase g */
142
+ 33, /* 104 150 68 01101000 h &#104; Lowercase h */
143
+ 34, /* 105 151 69 01101001 i &#105; Lowercase i */
144
+ 35, /* 106 152 6A 01101010 j &#106; Lowercase j */
145
+ 36, /* 107 153 6B 01101011 k &#107; Lowercase k */
146
+ 37, /* 108 154 6C 01101100 l &#108; Lowercase l */
147
+ 38, /* 109 155 6D 01101101 m &#109; Lowercase m */
148
+ 39, /* 110 156 6E 01101110 n &#110; Lowercase n */
149
+ 40, /* 111 157 6F 01101111 o &#111; Lowercase o */
150
+ 41, /* 112 160 70 01110000 p &#112; Lowercase p */
151
+ 42, /* 113 161 71 01110001 q &#113; Lowercase q */
152
+ 43, /* 114 162 72 01110010 r &#114; Lowercase r */
153
+ 44, /* 115 163 73 01110011 s &#115; Lowercase s */
154
+ 45, /* 116 164 74 01110100 t &#116; Lowercase t */
155
+ 46, /* 117 165 75 01110101 u &#117; Lowercase u */
156
+ 47, /* 118 166 76 01110110 v &#118; Lowercase v */
157
+ 48, /* 119 167 77 01110111 w &#119; Lowercase w */
158
+ 49, /* 120 170 78 01111000 x &#120; Lowercase x */
159
+ 50, /* 121 171 79 01111001 y &#121; Lowercase y */
160
+ 51, /* 122 172 7A 01111010 z &#122; Lowercase z */
161
+ -1, /* 123 173 7B 01111011 { &#123; &lcub; Opening brace */
162
+ -1, /* 124 174 7C 01111100 | &#124; &verbar; Vertical bar */
163
+ -1, /* 125 175 7D 01111101 } &#125; &rcub; Closing brace */
164
+ -1, /* 126 176 7E 01111110 ~ &#126; &tilde; Equivalency sign - tilde */
165
+ -1, /* 127 177 7F 01111111 DEL &#127; Delete */
166
+ };
167
+ static const signed char dec_base64[] = {
168
+ -1, /* 0 000 00 00000000 NUL &#00; Null character */
169
+ -1, /* 1 001 01 00000001 SOH &#01; Start of Heading */
170
+ -1, /* 2 002 02 00000010 STX &#02; Start of Text */
171
+ -1, /* 3 003 03 00000011 ETX &#03; End of Text */
172
+ -1, /* 4 004 04 00000100 EOT &#04; End of Transmission */
173
+ -1, /* 5 005 05 00000101 ENQ &#05; Enquiry */
174
+ -1, /* 6 006 06 00000110 ACK &#06; Acknowledge */
175
+ -1, /* 7 007 07 00000111 BEL &#07; Bell, Alert */
176
+ -1, /* 8 010 08 00001000 BS &#08; Backspace */
177
+ -1, /* 9 011 09 00001001 HT &#09; Horizontal Tab */
178
+ -1, /* 10 012 0A 00001010 LF &#10; Line Feed */
179
+ -1, /* 11 013 0B 00001011 VT &#11; Vertical Tabulation */
180
+ -1, /* 12 014 0C 00001100 FF &#12; Form Feed */
181
+ -1, /* 13 015 0D 00001101 CR &#13; Carriage Return */
182
+ -1, /* 14 016 0E 00001110 SO &#14; Shift Out */
183
+ -1, /* 15 017 0F 00001111 SI &#15; Shift In */
184
+ -1, /* 16 020 10 00010000 DLE &#16; Data Link Escape */
185
+ -1, /* 17 021 11 00010001 DC1 &#17; Device Control One (XON) */
186
+ -1, /* 18 022 12 00010010 DC2 &#18; Device Control Two */
187
+ -1, /* 19 023 13 00010011 DC3 &#19; Device Control Three (XOFF) */
188
+ -1, /* 20 024 14 00010100 DC4 &#20; Device Control Four */
189
+ -1, /* 21 025 15 00010101 NAK &#21; Negative Acknowledge */
190
+ -1, /* 22 026 16 00010110 SYN &#22; Synchronous Idle */
191
+ -1, /* 23 027 17 00010111 ETB &#23; End of Transmission Block */
192
+ -1, /* 24 030 18 00011000 CAN &#24; Cancel */
193
+ -1, /* 25 031 19 00011001 EM &#25; End of medium */
194
+ -1, /* 26 032 1A 00011010 SUB &#26; Substitute */
195
+ -1, /* 27 033 1B 00011011 ESC &#27; Escape */
196
+ -1, /* 28 034 1C 00011100 FS &#28; File Separator */
197
+ -1, /* 29 035 1D 00011101 GS &#29; Group Separator */
198
+ -1, /* 30 036 1E 00011110 RS &#30; Record Separator */
199
+ -1, /* 31 037 1F 00011111 US &#31; Unit Separator */
200
+ -1, /* 32 040 20 00100000 SP &#32; Space */
201
+ -1, /* 33 041 21 00100001 ! &#33; &excl; Exclamation mark */
202
+ -1, /* 34 042 22 00100010 " &#34; &quot; Double quotes (or speech marks) */
203
+ -1, /* 35 043 23 00100011 # &#35; &num; Number sign */
204
+ -1, /* 36 044 24 00100100 $ &#36; &dollar; Dollar */
205
+ -1, /* 37 045 25 00100101 % &#37; &percnt; Per cent sign */
206
+ -1, /* 38 046 26 00100110 & &#38; &amp; Ampersand */
207
+ -1, /* 39 047 27 00100111 ' &#39; &apos; Single quote */
208
+ -1, /* 40 050 28 00101000 ( &#40; &lparen; Open parenthesis (or open
209
+ * bracket)
210
+ */
211
+ -1, /* 41 051 29 00101001 ) &#41; &rparen; Close parenthesis (or close
212
+ * bracket)
213
+ */
214
+ -1, /* 42 052 2A 00101010 * &#42; &ast; Asterisk */
215
+ 62, /* 43 053 2B 00101011 + &#43; &plus; Plus */
216
+ -1, /* 44 054 2C 00101100 , &#44; &comma; Comma */
217
+ -1, /* 45 055 2D 00101101 - &#45; Hyphen-minus */
218
+ -1, /* 46 056 2E 00101110 . &#46; &period; Period, dot or full stop */
219
+ 63, /* 47 057 2F 00101111 / &#47; &sol; Slash or divide */
220
+ 52, /* 48 060 30 00110000 0 &#48; Zero */
221
+ 53, /* 49 061 31 00110001 1 &#49; One */
222
+ 54, /* 50 062 32 00110010 2 &#50; Two */
223
+ 55, /* 51 063 33 00110011 3 &#51; Three */
224
+ 56, /* 52 064 34 00110100 4 &#52; Four */
225
+ 57, /* 53 065 35 00110101 5 &#53; Five */
226
+ 58, /* 54 066 36 00110110 6 &#54; Six */
227
+ 59, /* 55 067 37 00110111 7 &#55; Seven */
228
+ 60, /* 56 070 38 00111000 8 &#56; Eight */
229
+ 61, /* 57 071 39 00111001 9 &#57; Nine */
230
+ -1, /* 58 072 3A 00111010 : &#58; &colon; Colon */
231
+ -1, /* 59 073 3B 00111011 ; &#59; &semi; Semicolon */
232
+ -1, /* 60 074 3C 00111100 < &#60; &lt; Less than (or open angled bracket)
233
+ */
234
+ -1, /* 61 075 3D 00111101 = &#61; &equals; Equals */
235
+ -1, /* 62 076 3E 00111110 > &#62; &gt; Greater than (or close angled
236
+ * bracket)
237
+ */
238
+ -1, /* 63 077 3F 00111111 ? &#63; &quest; Question mark */
239
+ -1, /* 64 100 40 01000000 @ &#64; &commat; At sign */
240
+ 0, /* 65 101 41 01000001 A &#65; Uppercase A */
241
+ 1, /* 66 102 42 01000010 B &#66; Uppercase B */
242
+ 2, /* 67 103 43 01000011 C &#67; Uppercase C */
243
+ 3, /* 68 104 44 01000100 D &#68; Uppercase D */
244
+ 4, /* 69 105 45 01000101 E &#69; Uppercase E */
245
+ 5, /* 70 106 46 01000110 F &#70; Uppercase F */
246
+ 6, /* 71 107 47 01000111 G &#71; Uppercase G */
247
+ 7, /* 72 110 48 01001000 H &#72; Uppercase H */
248
+ 8, /* 73 111 49 01001001 I &#73; Uppercase I */
249
+ 9, /* 74 112 4A 01001010 J &#74; Uppercase J */
250
+ 10, /* 75 113 4B 01001011 K &#75; Uppercase K */
251
+ 11, /* 76 114 4C 01001100 L &#76; Uppercase L */
252
+ 12, /* 77 115 4D 01001101 M &#77; Uppercase M */
253
+ 13, /* 78 116 4E 01001110 N &#78; Uppercase N */
254
+ 14, /* 79 117 4F 01001111 O &#79; Uppercase O */
255
+ 15, /* 80 120 50 01010000 P &#80; Uppercase P */
256
+ 16, /* 81 121 51 01010001 Q &#81; Uppercase Q */
257
+ 17, /* 82 122 52 01010010 R &#82; Uppercase R */
258
+ 18, /* 83 123 53 01010011 S &#83; Uppercase S */
259
+ 19, /* 84 124 54 01010100 T &#84; Uppercase T */
260
+ 20, /* 85 125 55 01010101 U &#85; Uppercase U */
261
+ 21, /* 86 126 56 01010110 V &#86; Uppercase V */
262
+ 22, /* 87 127 57 01010111 W &#87; Uppercase W */
263
+ 23, /* 88 130 58 01011000 X &#88; Uppercase X */
264
+ 24, /* 89 131 59 01011001 Y &#89; Uppercase Y */
265
+ 25, /* 90 132 5A 01011010 Z &#90; Uppercase Z */
266
+ -1, /* 91 133 5B 01011011 [ &#91; &lsqb; Opening bracket */
267
+ -1, /* 92 134 5C 01011100 \ &#92; &bsol; Backslash */
268
+ -1, /* 93 135 5D 01011101 ] &#93; &rsqb; Closing bracket */
269
+ -1, /* 94 136 5E 01011110 ^ &#94; &Hat; Caret - circumflex */
270
+ -1, /* 95 137 5F 01011111 _ &#95; &lowbar; Underscore */
271
+ -1, /* 96 140 60 01100000 ` &#96; &grave; Grave accent */
272
+ 26, /* 97 141 61 01100001 a &#97; Lowercase a */
273
+ 27, /* 98 142 62 01100010 b &#98; Lowercase b */
274
+ 28, /* 99 143 63 01100011 c &#99; Lowercase c */
275
+ 29, /* 100 144 64 01100100 d &#100; Lowercase d */
276
+ 30, /* 101 145 65 01100101 e &#101; Lowercase e */
277
+ 31, /* 102 146 66 01100110 f &#102; Lowercase f */
278
+ 32, /* 103 147 67 01100111 g &#103; Lowercase g */
279
+ 33, /* 104 150 68 01101000 h &#104; Lowercase h */
280
+ 34, /* 105 151 69 01101001 i &#105; Lowercase i */
281
+ 35, /* 106 152 6A 01101010 j &#106; Lowercase j */
282
+ 36, /* 107 153 6B 01101011 k &#107; Lowercase k */
283
+ 37, /* 108 154 6C 01101100 l &#108; Lowercase l */
284
+ 38, /* 109 155 6D 01101101 m &#109; Lowercase m */
285
+ 39, /* 110 156 6E 01101110 n &#110; Lowercase n */
286
+ 40, /* 111 157 6F 01101111 o &#111; Lowercase o */
287
+ 41, /* 112 160 70 01110000 p &#112; Lowercase p */
288
+ 42, /* 113 161 71 01110001 q &#113; Lowercase q */
289
+ 43, /* 114 162 72 01110010 r &#114; Lowercase r */
290
+ 44, /* 115 163 73 01110011 s &#115; Lowercase s */
291
+ 45, /* 116 164 74 01110100 t &#116; Lowercase t */
292
+ 46, /* 117 165 75 01110101 u &#117; Lowercase u */
293
+ 47, /* 118 166 76 01110110 v &#118; Lowercase v */
294
+ 48, /* 119 167 77 01110111 w &#119; Lowercase w */
295
+ 49, /* 120 170 78 01111000 x &#120; Lowercase x */
296
+ 50, /* 121 171 79 01111001 y &#121; Lowercase y */
297
+ 51, /* 122 172 7A 01111010 z &#122; Lowercase z */
298
+ -1, /* 123 173 7B 01111011 { &#123; &lcub; Opening brace */
299
+ -1, /* 124 174 7C 01111100 | &#124; &verbar; Vertical bar */
300
+ -1, /* 125 175 7D 01111101 } &#125; &rcub; Closing brace */
301
+ -1, /* 126 176 7E 01111110 ~ &#126; &tilde; Equivalency sign - tilde */
302
+ -1, /* 127 177 7F 01111111 DEL &#127; Delete */
303
+ };
304
+
305
+ static INLINE char* base64_encode_ex(const BYTE* WINPR_RESTRICT alphabet,
306
+ const BYTE* WINPR_RESTRICT data, size_t length, BOOL pad,
307
+ BOOL crLf, size_t lineSize)
308
+ {
309
+ int c = 0;
310
+ size_t blocks = 0;
311
+ size_t outLen = (length + 3) * 4 / 3;
312
+ size_t extra = 0;
313
+ if (crLf)
314
+ {
315
+ size_t nCrLf = (outLen + lineSize - 1) / lineSize;
316
+ extra = nCrLf * 2;
317
+ }
318
+ size_t outCounter = 0;
319
+
320
+ const BYTE* q = data;
321
+ BYTE* p = malloc(outLen + extra + 1ull);
322
+ if (!p)
323
+ return NULL;
324
+
325
+ char* ret = (char*)p;
326
+
327
+ /* b1, b2, b3 are input bytes
328
+ *
329
+ * 0 1 2
330
+ * 012345678901234567890123
331
+ * | b1 | b2 | b3 |
332
+ *
333
+ * [ c1 ] [ c3 ]
334
+ * [ c2 ] [ c4 ]
335
+ *
336
+ * c1, c2, c3, c4 are output chars in base64
337
+ */
338
+
339
+ /* first treat complete blocks */
340
+ blocks = length - (length % 3);
341
+ for (size_t i = 0; i < blocks; i += 3, q += 3)
342
+ {
343
+ c = (q[0] << 16) + (q[1] << 8) + q[2];
344
+
345
+ *p++ = alphabet[(c & 0x00FC0000) >> 18];
346
+ *p++ = alphabet[(c & 0x0003F000) >> 12];
347
+ *p++ = alphabet[(c & 0x00000FC0) >> 6];
348
+ *p++ = alphabet[c & 0x0000003F];
349
+
350
+ outCounter += 4;
351
+ if (crLf && (outCounter % lineSize == 0))
352
+ {
353
+ *p++ = '\r';
354
+ *p++ = '\n';
355
+ }
356
+ }
357
+
358
+ /* then remainder */
359
+ switch (length % 3)
360
+ {
361
+ case 0:
362
+ break;
363
+ case 1:
364
+ c = (q[0] << 16);
365
+ *p++ = alphabet[(c & 0x00FC0000) >> 18];
366
+ *p++ = alphabet[(c & 0x0003F000) >> 12];
367
+ if (pad)
368
+ {
369
+ *p++ = '=';
370
+ *p++ = '=';
371
+ }
372
+ break;
373
+ case 2:
374
+ c = (q[0] << 16) + (q[1] << 8);
375
+ *p++ = alphabet[(c & 0x00FC0000) >> 18];
376
+ *p++ = alphabet[(c & 0x0003F000) >> 12];
377
+ *p++ = alphabet[(c & 0x00000FC0) >> 6];
378
+ if (pad)
379
+ *p++ = '=';
380
+ break;
381
+ default:
382
+ break;
383
+ }
384
+
385
+ if (crLf && length % 3)
386
+ {
387
+ *p++ = '\r';
388
+ *p++ = '\n';
389
+ }
390
+ *p = 0;
391
+
392
+ return ret;
393
+ }
394
+
395
+ static INLINE char* base64_encode(const BYTE* WINPR_RESTRICT alphabet,
396
+ const BYTE* WINPR_RESTRICT data, size_t length, BOOL pad)
397
+ {
398
+ return base64_encode_ex(alphabet, data, length, pad, FALSE, 64);
399
+ }
400
+
401
+ static INLINE int base64_decode_char(const signed char* WINPR_RESTRICT alphabet, char c)
402
+ {
403
+ if (c <= '\0')
404
+ return -1;
405
+
406
+ return alphabet[(size_t)c];
407
+ }
408
+
409
+ static INLINE void* base64_decode(const signed char* WINPR_RESTRICT alphabet,
410
+ const char* WINPR_RESTRICT s, size_t length,
411
+ size_t* WINPR_RESTRICT data_len, BOOL pad)
412
+ {
413
+ int n[4] = { 0 };
414
+ BYTE* data = NULL;
415
+ const size_t remainder = length % 4;
416
+
417
+ if ((pad && remainder > 0) || (remainder == 1))
418
+ return NULL;
419
+
420
+ if (!pad && remainder)
421
+ length += 4 - remainder;
422
+
423
+ BYTE* q = data = (BYTE*)malloc(length / 4 * 3 + 1);
424
+ if (!q)
425
+ return NULL;
426
+
427
+ /* first treat complete blocks */
428
+ const size_t nBlocks = (length / 4);
429
+ size_t outputLen = 0;
430
+
431
+ if (nBlocks < 1)
432
+ {
433
+ free(data);
434
+ return NULL;
435
+ }
436
+
437
+ for (size_t i = 0; i < nBlocks - 1; i++, q += 3)
438
+ {
439
+ n[0] = base64_decode_char(alphabet, *s++);
440
+ n[1] = base64_decode_char(alphabet, *s++);
441
+ n[2] = base64_decode_char(alphabet, *s++);
442
+ n[3] = base64_decode_char(alphabet, *s++);
443
+
444
+ if ((n[0] == -1) || (n[1] == -1) || (n[2] == -1) || (n[3] == -1))
445
+ goto out_free;
446
+
447
+ q[0] = (BYTE)((n[0] << 2) + (n[1] >> 4));
448
+ q[1] = (BYTE)(((n[1] & 15) << 4) + (n[2] >> 2));
449
+ q[2] = (BYTE)(((n[2] & 3) << 6) + n[3]);
450
+ outputLen += 3;
451
+ }
452
+
453
+ /* treat last block */
454
+ n[0] = base64_decode_char(alphabet, *s++);
455
+ n[1] = base64_decode_char(alphabet, *s++);
456
+ if ((n[0] == -1) || (n[1] == -1))
457
+ goto out_free;
458
+
459
+ n[2] = remainder == 2 ? -1 : base64_decode_char(alphabet, *s++);
460
+ n[3] = remainder >= 2 ? -1 : base64_decode_char(alphabet, *s++);
461
+
462
+ q[0] = (BYTE)((n[0] << 2) + (n[1] >> 4));
463
+ if (n[2] == -1)
464
+ {
465
+ /* XX== */
466
+ outputLen += 1;
467
+ if (n[3] != -1)
468
+ goto out_free;
469
+
470
+ q[1] = (BYTE)((n[1] & 15) << 4);
471
+ }
472
+ else if (n[3] == -1)
473
+ {
474
+ /* yyy= */
475
+ outputLen += 2;
476
+ q[1] = (BYTE)(((n[1] & 15) << 4) + (n[2] >> 2));
477
+ q[2] = (BYTE)((n[2] & 3) << 6);
478
+ }
479
+ else
480
+ {
481
+ /* XXXX */
482
+ outputLen += 3;
483
+ q[0] = (BYTE)((n[0] << 2) + (n[1] >> 4));
484
+ q[1] = (BYTE)(((n[1] & 15) << 4) + (n[2] >> 2));
485
+ q[2] = (BYTE)(((n[2] & 3) << 6) + n[3]);
486
+ }
487
+
488
+ if (data_len)
489
+ *data_len = outputLen;
490
+ data[outputLen] = '\0';
491
+
492
+ return data;
493
+ out_free:
494
+ free(data);
495
+ return NULL;
496
+ }
497
+
498
+ char* crypto_base64_encode_ex(const BYTE* WINPR_RESTRICT data, size_t length, BOOL withCrLf)
499
+ {
500
+ return base64_encode_ex(enc_base64, data, length, TRUE, withCrLf, 64);
501
+ }
502
+
503
+ char* crypto_base64_encode(const BYTE* WINPR_RESTRICT data, size_t length)
504
+ {
505
+ return base64_encode(enc_base64, data, length, TRUE);
506
+ }
507
+
508
+ void crypto_base64_decode(const char* WINPR_RESTRICT enc_data, size_t length,
509
+ BYTE** WINPR_RESTRICT dec_data, size_t* WINPR_RESTRICT res_length)
510
+ {
511
+ *dec_data = base64_decode(dec_base64, enc_data, length, res_length, TRUE);
512
+ }
513
+
514
+ char* crypto_base64url_encode(const BYTE* WINPR_RESTRICT data, size_t length)
515
+ {
516
+ return base64_encode(enc_base64url, data, length, FALSE);
517
+ }
518
+
519
+ void crypto_base64url_decode(const char* WINPR_RESTRICT enc_data, size_t length,
520
+ BYTE** WINPR_RESTRICT dec_data, size_t* WINPR_RESTRICT res_length)
521
+ {
522
+ *dec_data = base64_decode(dec_base64url, enc_data, length, res_length, FALSE);
523
+ }
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/crypto/ber.c ADDED
@@ -0,0 +1,733 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * ASN.1 Basic Encoding Rules (BER)
4
+ *
5
+ * Copyright 2011-2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+
20
+ #include <freerdp/config.h>
21
+
22
+ #include <stdio.h>
23
+ #include <winpr/assert.h>
24
+ #include <winpr/cast.h>
25
+ #include <winpr/crt.h>
26
+ #include <winpr/string.h>
27
+
28
+ #include <freerdp/log.h>
29
+ #include <freerdp/crypto/ber.h>
30
+
31
+ #define TAG FREERDP_TAG("crypto")
32
+
33
+ BOOL ber_read_length(wStream* s, size_t* length)
34
+ {
35
+ BYTE byte = 0;
36
+
37
+ WINPR_ASSERT(s);
38
+ WINPR_ASSERT(length);
39
+
40
+ if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
41
+ return FALSE;
42
+
43
+ Stream_Read_UINT8(s, byte);
44
+
45
+ if (byte & 0x80)
46
+ {
47
+ byte &= ~(0x80);
48
+
49
+ if (!Stream_CheckAndLogRequiredLength(TAG, s, byte))
50
+ return FALSE;
51
+
52
+ if (byte == 1)
53
+ Stream_Read_UINT8(s, *length);
54
+ else if (byte == 2)
55
+ Stream_Read_UINT16_BE(s, *length);
56
+ else
57
+ {
58
+ WLog_ERR(TAG, "ber: unexpected byte 0x%02" PRIx8 ", expected [1,2]", byte);
59
+ return FALSE;
60
+ }
61
+ }
62
+ else
63
+ {
64
+ *length = byte;
65
+ }
66
+
67
+ return TRUE;
68
+ }
69
+
70
+ /**
71
+ * Write BER length.
72
+ * @param s stream
73
+ * @param length length
74
+ */
75
+
76
+ size_t ber_write_length(wStream* s, size_t length)
77
+ {
78
+ WINPR_ASSERT(s);
79
+
80
+ if (length > 0xFF)
81
+ {
82
+ WINPR_ASSERT(length <= UINT16_MAX);
83
+ WINPR_ASSERT(Stream_GetRemainingCapacity(s) >= 3);
84
+ Stream_Write_UINT8(s, 0x80 ^ 2);
85
+ Stream_Write_UINT16_BE(s, (UINT16)length);
86
+ return 3;
87
+ }
88
+
89
+ WINPR_ASSERT(length <= UINT8_MAX);
90
+ if (length > 0x7F)
91
+ {
92
+ WINPR_ASSERT(Stream_GetRemainingCapacity(s) >= 2);
93
+ Stream_Write_UINT8(s, 0x80 ^ 1);
94
+ Stream_Write_UINT8(s, (UINT8)length);
95
+ return 2;
96
+ }
97
+
98
+ WINPR_ASSERT(Stream_GetRemainingCapacity(s) >= 1);
99
+ Stream_Write_UINT8(s, (UINT8)length);
100
+ return 1;
101
+ }
102
+
103
+ size_t _ber_sizeof_length(size_t length)
104
+ {
105
+ if (length > 0xFF)
106
+ return 3;
107
+
108
+ if (length > 0x7F)
109
+ return 2;
110
+
111
+ return 1;
112
+ }
113
+
114
+ /**
115
+ * Read BER Universal tag.
116
+ *
117
+ * @param s The stream to read from
118
+ * @param tag BER universally-defined tag
119
+ *
120
+ * @return \b TRUE for success, \b FALSE otherwise
121
+ */
122
+
123
+ BOOL ber_read_universal_tag(wStream* s, BYTE tag, BOOL pc)
124
+ {
125
+ BYTE byte = 0;
126
+ const BYTE expect = (BER_CLASS_UNIV | BER_PC(pc) | (BER_TAG_MASK & tag));
127
+
128
+ WINPR_ASSERT(s);
129
+
130
+ if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
131
+ return FALSE;
132
+
133
+ Stream_Read_UINT8(s, byte);
134
+
135
+ if (byte != expect)
136
+ {
137
+ WLog_WARN(TAG, "invalid tag, got 0x%02" PRIx8 ", expected 0x%02" PRIx8, byte, expect);
138
+ return FALSE;
139
+ }
140
+
141
+ return TRUE;
142
+ }
143
+
144
+ /**
145
+ * Write BER Universal tag.
146
+ * @param s stream
147
+ * @param tag BER universally-defined tag
148
+ * @param pc primitive (FALSE) or constructed (TRUE)
149
+ */
150
+
151
+ size_t ber_write_universal_tag(wStream* s, BYTE tag, BOOL pc)
152
+ {
153
+ WINPR_ASSERT(s);
154
+ Stream_Write_UINT8(s, (BER_CLASS_UNIV | BER_PC(pc)) | (BER_TAG_MASK & tag));
155
+ return 1;
156
+ }
157
+
158
+ /**
159
+ * Read BER Application tag.
160
+ * @param s stream
161
+ * @param tag BER application-defined tag
162
+ * @param length length
163
+ */
164
+
165
+ BOOL ber_read_application_tag(wStream* s, BYTE tag, size_t* length)
166
+ {
167
+ BYTE byte = 0;
168
+
169
+ WINPR_ASSERT(s);
170
+ WINPR_ASSERT(length);
171
+
172
+ if (tag > 30)
173
+ {
174
+ const BYTE expect = ((BER_CLASS_APPL | BER_CONSTRUCT) | BER_TAG_MASK);
175
+
176
+ if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
177
+ return FALSE;
178
+
179
+ Stream_Read_UINT8(s, byte);
180
+
181
+ if (byte != expect)
182
+ {
183
+ WLog_WARN(TAG, "invalid tag, got 0x%02" PRIx8 ", expected 0x%02" PRIx8, byte, expect);
184
+ return FALSE;
185
+ }
186
+
187
+ Stream_Read_UINT8(s, byte);
188
+
189
+ if (byte != tag)
190
+ {
191
+ WLog_WARN(TAG, "invalid tag, got 0x%02" PRIx8 ", expected 0x%02" PRIx8, byte, tag);
192
+ return FALSE;
193
+ }
194
+
195
+ return ber_read_length(s, length);
196
+ }
197
+ else
198
+ {
199
+ const BYTE expect = ((BER_CLASS_APPL | BER_CONSTRUCT) | (BER_TAG_MASK & tag));
200
+
201
+ if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
202
+ return FALSE;
203
+
204
+ Stream_Read_UINT8(s, byte);
205
+
206
+ if (byte != expect)
207
+ {
208
+ WLog_WARN(TAG, "invalid tag, got 0x%02" PRIx8 ", expected 0x%02" PRIx8, byte, expect);
209
+ return FALSE;
210
+ }
211
+
212
+ return ber_read_length(s, length);
213
+ }
214
+
215
+ return TRUE;
216
+ }
217
+
218
+ /**
219
+ * Write BER Application tag.
220
+ * @param s stream
221
+ * @param tag BER application-defined tag
222
+ * @param length length
223
+ */
224
+
225
+ void ber_write_application_tag(wStream* s, BYTE tag, size_t length)
226
+ {
227
+ WINPR_ASSERT(s);
228
+
229
+ if (tag > 30)
230
+ {
231
+ WINPR_ASSERT(Stream_GetRemainingCapacity(s) >= 2);
232
+ Stream_Write_UINT8(s, (BER_CLASS_APPL | BER_CONSTRUCT) | BER_TAG_MASK);
233
+ Stream_Write_UINT8(s, tag);
234
+ ber_write_length(s, length);
235
+ }
236
+ else
237
+ {
238
+ WINPR_ASSERT(Stream_GetRemainingCapacity(s) >= 1);
239
+ Stream_Write_UINT8(s, (BER_CLASS_APPL | BER_CONSTRUCT) | (BER_TAG_MASK & tag));
240
+ ber_write_length(s, length);
241
+ }
242
+ }
243
+
244
+ BOOL ber_read_contextual_tag(wStream* s, BYTE tag, size_t* length, BOOL pc)
245
+ {
246
+ const BYTE expect = ((BER_CLASS_CTXT | BER_PC(pc)) | (BER_TAG_MASK & tag));
247
+ BYTE byte = 0;
248
+
249
+ WINPR_ASSERT(s);
250
+ WINPR_ASSERT(length);
251
+
252
+ if (Stream_GetRemainingLength(s) < 1)
253
+ {
254
+ WLog_VRB(TAG, "short data, got %" PRIuz ", expected %" PRIuz, Stream_GetRemainingLength(s),
255
+ 1);
256
+ return FALSE;
257
+ }
258
+
259
+ Stream_Read_UINT8(s, byte);
260
+
261
+ if (byte != expect)
262
+ {
263
+ WLog_VRB(TAG, "invalid tag, got 0x%02" PRIx8 ", expected 0x%02" PRIx8, byte, expect);
264
+ Stream_Rewind(s, 1);
265
+ return FALSE;
266
+ }
267
+
268
+ return ber_read_length(s, length);
269
+ }
270
+
271
+ size_t ber_write_contextual_tag(wStream* s, BYTE tag, size_t length, BOOL pc)
272
+ {
273
+ WINPR_ASSERT(s);
274
+ WINPR_ASSERT(Stream_GetRemainingCapacity(s) >= 1);
275
+ Stream_Write_UINT8(s, (BER_CLASS_CTXT | BER_PC(pc)) | (BER_TAG_MASK & tag));
276
+ return 1 + ber_write_length(s, length);
277
+ }
278
+
279
+ size_t ber_sizeof_contextual_tag(size_t length)
280
+ {
281
+ return 1 + _ber_sizeof_length(length);
282
+ }
283
+
284
+ BOOL ber_read_sequence_tag(wStream* s, size_t* length)
285
+ {
286
+ const BYTE expect = ((BER_CLASS_UNIV | BER_CONSTRUCT) | (BER_TAG_SEQUENCE_OF));
287
+ BYTE byte = 0;
288
+
289
+ if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
290
+ return FALSE;
291
+
292
+ Stream_Read_UINT8(s, byte);
293
+
294
+ if (byte != expect)
295
+ {
296
+ WLog_WARN(TAG, "invalid tag, got 0x%02" PRIx8 ", expected 0x%02" PRIx8, byte, expect);
297
+ return FALSE;
298
+ }
299
+
300
+ return ber_read_length(s, length);
301
+ }
302
+
303
+ /**
304
+ * Write BER SEQUENCE tag.
305
+ * @param s stream
306
+ * @param length length
307
+ */
308
+
309
+ size_t ber_write_sequence_tag(wStream* s, size_t length)
310
+ {
311
+ Stream_Write_UINT8(s, (BER_CLASS_UNIV | BER_CONSTRUCT) | (BER_TAG_MASK & BER_TAG_SEQUENCE));
312
+ return 1 + ber_write_length(s, length);
313
+ }
314
+
315
+ size_t ber_sizeof_sequence(size_t length)
316
+ {
317
+ return 1 + _ber_sizeof_length(length) + length;
318
+ }
319
+
320
+ size_t ber_sizeof_sequence_tag(size_t length)
321
+ {
322
+ return 1 + _ber_sizeof_length(length);
323
+ }
324
+
325
+ BOOL ber_read_enumerated(wStream* s, BYTE* enumerated, BYTE count)
326
+ {
327
+ size_t length = 0;
328
+
329
+ WINPR_ASSERT(enumerated);
330
+
331
+ if (!ber_read_universal_tag(s, BER_TAG_ENUMERATED, FALSE) || !ber_read_length(s, &length))
332
+ return FALSE;
333
+
334
+ if (length != 1)
335
+ {
336
+ WLog_WARN(TAG, "short data, got %" PRIuz ", expected %" PRIuz, length, 1);
337
+ return FALSE;
338
+ }
339
+ if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
340
+ return FALSE;
341
+
342
+ Stream_Read_UINT8(s, *enumerated);
343
+
344
+ /* check that enumerated value falls within expected range */
345
+ if (*enumerated + 1 > count)
346
+ {
347
+ WLog_WARN(TAG, "invalid data, expected %" PRIu8 " < %" PRIu8, *enumerated, count);
348
+ return FALSE;
349
+ }
350
+
351
+ return TRUE;
352
+ }
353
+
354
+ void ber_write_enumerated(wStream* s, BYTE enumerated, BYTE count)
355
+ {
356
+ ber_write_universal_tag(s, BER_TAG_ENUMERATED, FALSE);
357
+ ber_write_length(s, 1);
358
+ Stream_Write_UINT8(s, enumerated);
359
+ }
360
+
361
+ BOOL ber_read_bit_string(wStream* s, size_t* length, BYTE* padding)
362
+ {
363
+ if (!ber_read_universal_tag(s, BER_TAG_BIT_STRING, FALSE) || !ber_read_length(s, length))
364
+ return FALSE;
365
+
366
+ if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
367
+ return FALSE;
368
+
369
+ Stream_Read_UINT8(s, *padding);
370
+ return TRUE;
371
+ }
372
+
373
+ /**
374
+ * Write a BER OCTET_STRING
375
+ * @param s stream
376
+ * @param oct_str octet string
377
+ * @param length string length
378
+ */
379
+
380
+ size_t ber_write_octet_string(wStream* s, const BYTE* oct_str, size_t length)
381
+ {
382
+ size_t size = 0;
383
+
384
+ WINPR_ASSERT(oct_str || (length == 0));
385
+ size += ber_write_universal_tag(s, BER_TAG_OCTET_STRING, FALSE);
386
+ size += ber_write_length(s, length);
387
+ Stream_Write(s, oct_str, length);
388
+ size += length;
389
+ return size;
390
+ }
391
+
392
+ size_t ber_write_contextual_octet_string(wStream* s, BYTE tag, const BYTE* oct_str, size_t length)
393
+ {
394
+ size_t inner = ber_sizeof_octet_string(length);
395
+ size_t ret = 0;
396
+ size_t r = 0;
397
+
398
+ ret = ber_write_contextual_tag(s, tag, inner, TRUE);
399
+ if (!ret)
400
+ return 0;
401
+
402
+ r = ber_write_octet_string(s, oct_str, length);
403
+ if (!r)
404
+ return 0;
405
+ return ret + r;
406
+ }
407
+
408
+ size_t ber_write_char_to_unicode_octet_string(wStream* s, const char* str)
409
+ {
410
+ WINPR_ASSERT(str);
411
+ size_t size = 0;
412
+ size_t length = strlen(str) + 1;
413
+ size += ber_write_universal_tag(s, BER_TAG_OCTET_STRING, FALSE);
414
+ size += ber_write_length(s, length * sizeof(WCHAR));
415
+
416
+ if (Stream_Write_UTF16_String_From_UTF8(s, length, str, length, TRUE) < 0)
417
+ return 0;
418
+ return size + length * sizeof(WCHAR);
419
+ }
420
+
421
+ size_t ber_write_contextual_unicode_octet_string(wStream* s, BYTE tag, LPWSTR str)
422
+ {
423
+ WINPR_ASSERT(str);
424
+ size_t len = _wcslen(str) * sizeof(WCHAR);
425
+ size_t inner_len = ber_sizeof_octet_string(len);
426
+ size_t ret = 0;
427
+
428
+ ret = ber_write_contextual_tag(s, tag, inner_len, TRUE);
429
+ return ret + ber_write_octet_string(s, (const BYTE*)str, len);
430
+ }
431
+
432
+ size_t ber_write_contextual_char_to_unicode_octet_string(wStream* s, BYTE tag, const char* str)
433
+ {
434
+ size_t ret = 0;
435
+ size_t len = strlen(str);
436
+ size_t inner_len = ber_sizeof_octet_string(len * 2);
437
+
438
+ WINPR_ASSERT(Stream_GetRemainingCapacity(s) < ber_sizeof_contextual_tag(inner_len) + inner_len);
439
+
440
+ ret = ber_write_contextual_tag(s, tag, inner_len, TRUE);
441
+ ret += ber_write_universal_tag(s, BER_TAG_OCTET_STRING, FALSE);
442
+ ret += ber_write_length(s, len * sizeof(WCHAR));
443
+
444
+ if (Stream_Write_UTF16_String_From_UTF8(s, len, str, len, TRUE) < 0)
445
+ return 0;
446
+
447
+ return ret + len;
448
+ }
449
+
450
+ BOOL ber_read_unicode_octet_string(wStream* s, LPWSTR* str)
451
+ {
452
+ LPWSTR ret = NULL;
453
+ size_t length = 0;
454
+
455
+ if (!ber_read_octet_string_tag(s, &length))
456
+ return FALSE;
457
+
458
+ if (!Stream_CheckAndLogRequiredLength(TAG, s, length))
459
+ return FALSE;
460
+
461
+ ret = calloc(1, length + 2);
462
+ if (!ret)
463
+ return FALSE;
464
+
465
+ memcpy(ret, Stream_ConstPointer(s), length);
466
+ ret[length / 2] = 0;
467
+ Stream_Seek(s, length);
468
+ *str = ret;
469
+ return TRUE;
470
+ }
471
+
472
+ BOOL ber_read_char_from_unicode_octet_string(wStream* s, char** str)
473
+ {
474
+ size_t length = 0;
475
+ char* ptr = NULL;
476
+
477
+ *str = NULL;
478
+ if (!ber_read_octet_string_tag(s, &length))
479
+ return FALSE;
480
+
481
+ ptr = Stream_Read_UTF16_String_As_UTF8(s, length / sizeof(WCHAR), NULL);
482
+ if (!ptr)
483
+ return FALSE;
484
+ *str = ptr;
485
+ return TRUE;
486
+ }
487
+
488
+ BOOL ber_read_octet_string_tag(wStream* s, size_t* length)
489
+ {
490
+ return ber_read_universal_tag(s, BER_TAG_OCTET_STRING, FALSE) && ber_read_length(s, length);
491
+ }
492
+
493
+ BOOL ber_read_octet_string(wStream* s, BYTE** content, size_t* length)
494
+ {
495
+ BYTE* ret = NULL;
496
+
497
+ WINPR_ASSERT(s);
498
+ WINPR_ASSERT(content);
499
+ WINPR_ASSERT(length);
500
+
501
+ if (!ber_read_octet_string_tag(s, length))
502
+ return FALSE;
503
+ if (!Stream_CheckAndLogRequiredLength(TAG, s, *length))
504
+ return FALSE;
505
+
506
+ ret = malloc(*length);
507
+ if (!ret)
508
+ return FALSE;
509
+
510
+ Stream_Read(s, ret, *length);
511
+ *content = ret;
512
+ return TRUE;
513
+ }
514
+
515
+ size_t ber_write_octet_string_tag(wStream* s, size_t length)
516
+ {
517
+ ber_write_universal_tag(s, BER_TAG_OCTET_STRING, FALSE);
518
+ ber_write_length(s, length);
519
+ return 1 + _ber_sizeof_length(length);
520
+ }
521
+
522
+ size_t ber_sizeof_octet_string(size_t length)
523
+ {
524
+ return 1 + _ber_sizeof_length(length) + length;
525
+ }
526
+
527
+ size_t ber_sizeof_contextual_octet_string(size_t length)
528
+ {
529
+ size_t ret = ber_sizeof_octet_string(length);
530
+ return ber_sizeof_contextual_tag(ret) + ret;
531
+ }
532
+
533
+ /** \brief Read a BER BOOLEAN
534
+ *
535
+ * @param s The stream to read from.
536
+ * @param value A pointer to the value read, must not be NULL
537
+ *
538
+ * \return \b TRUE for success, \b FALSE for any failure
539
+ */
540
+
541
+ BOOL ber_read_BOOL(wStream* s, BOOL* value)
542
+ {
543
+ size_t length = 0;
544
+ BYTE v = 0;
545
+
546
+ WINPR_ASSERT(value);
547
+ if (!ber_read_universal_tag(s, BER_TAG_BOOLEAN, FALSE) || !ber_read_length(s, &length))
548
+ return FALSE;
549
+
550
+ if (length != 1)
551
+ {
552
+ WLog_WARN(TAG, "short data, got %" PRIuz ", expected %" PRIuz, length, 1);
553
+ return FALSE;
554
+ }
555
+ if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
556
+ return FALSE;
557
+
558
+ Stream_Read_UINT8(s, v);
559
+ *value = (v ? TRUE : FALSE);
560
+ return TRUE;
561
+ }
562
+
563
+ /**
564
+ * Write a BER BOOLEAN
565
+ *
566
+ * @param s A pointer to the stream to write to
567
+ * @param value The value to write
568
+ */
569
+
570
+ void ber_write_BOOL(wStream* s, BOOL value)
571
+ {
572
+ ber_write_universal_tag(s, BER_TAG_BOOLEAN, FALSE);
573
+ ber_write_length(s, 1);
574
+ Stream_Write_UINT8(s, (value == TRUE) ? 0xFF : 0);
575
+ }
576
+
577
+ BOOL ber_read_integer(wStream* s, UINT32* value)
578
+ {
579
+ size_t length = 0;
580
+
581
+ WINPR_ASSERT(s);
582
+
583
+ if (!ber_read_universal_tag(s, BER_TAG_INTEGER, FALSE))
584
+ return FALSE;
585
+ if (!ber_read_length(s, &length))
586
+ return FALSE;
587
+ if (!Stream_CheckAndLogRequiredLength(TAG, s, length))
588
+ return FALSE;
589
+
590
+ if (value == NULL)
591
+ {
592
+ // even if we don't care the integer value, check the announced size
593
+ return Stream_SafeSeek(s, length);
594
+ }
595
+
596
+ if (length == 1)
597
+ {
598
+ Stream_Read_UINT8(s, *value);
599
+ }
600
+ else if (length == 2)
601
+ {
602
+ Stream_Read_UINT16_BE(s, *value);
603
+ }
604
+ else if (length == 3)
605
+ {
606
+ BYTE byte = 0;
607
+ Stream_Read_UINT8(s, byte);
608
+ Stream_Read_UINT16_BE(s, *value);
609
+ *value += (byte << 16) & 0xFF0000;
610
+ }
611
+ else if (length == 4)
612
+ {
613
+ Stream_Read_UINT32_BE(s, *value);
614
+ }
615
+ else if (length == 8)
616
+ {
617
+ WLog_ERR(TAG, "should implement reading an 8 bytes integer");
618
+ return FALSE;
619
+ }
620
+ else
621
+ {
622
+ WLog_ERR(TAG, "should implement reading an integer with length=%" PRIuz, length);
623
+ return FALSE;
624
+ }
625
+
626
+ return TRUE;
627
+ }
628
+
629
+ /**
630
+ * Write a BER INTEGER
631
+ *
632
+ * @param s A pointer to the stream to write to
633
+ * @param value The value to write
634
+ *
635
+ * @return The size in bytes that were written
636
+ */
637
+
638
+ size_t ber_write_integer(wStream* s, UINT32 value)
639
+ {
640
+ WINPR_ASSERT(s);
641
+
642
+ if (value < 0x80)
643
+ {
644
+ ber_write_universal_tag(s, BER_TAG_INTEGER, FALSE);
645
+ ber_write_length(s, 1);
646
+
647
+ Stream_Write_UINT8(s, WINPR_ASSERTING_INT_CAST(UINT8, value));
648
+ return 3;
649
+ }
650
+ else if (value < 0x8000)
651
+ {
652
+ ber_write_universal_tag(s, BER_TAG_INTEGER, FALSE);
653
+ ber_write_length(s, 2);
654
+
655
+ Stream_Write_UINT16_BE(s, WINPR_ASSERTING_INT_CAST(UINT16, value));
656
+ return 4;
657
+ }
658
+ else if (value < 0x800000)
659
+ {
660
+ ber_write_universal_tag(s, BER_TAG_INTEGER, FALSE);
661
+ ber_write_length(s, 3);
662
+
663
+ Stream_Write_UINT8(s, WINPR_ASSERTING_INT_CAST(UINT8, value >> 16));
664
+ Stream_Write_UINT16_BE(s, WINPR_ASSERTING_INT_CAST(UINT16, value));
665
+ return 5;
666
+ }
667
+ else if (value < 0x80000000)
668
+ {
669
+ ber_write_universal_tag(s, BER_TAG_INTEGER, FALSE);
670
+ ber_write_length(s, 4);
671
+
672
+ Stream_Write_UINT32_BE(s, value);
673
+ return 6;
674
+ }
675
+ else
676
+ {
677
+ /* treat as signed integer i.e. NT/HRESULT error codes */
678
+ ber_write_universal_tag(s, BER_TAG_INTEGER, FALSE);
679
+ ber_write_length(s, 4);
680
+
681
+ Stream_Write_UINT32_BE(s, value);
682
+ return 6;
683
+ }
684
+ }
685
+
686
+ size_t ber_write_contextual_integer(wStream* s, BYTE tag, UINT32 value)
687
+ {
688
+ size_t len = ber_sizeof_integer(value);
689
+
690
+ WINPR_ASSERT(s);
691
+
692
+ WINPR_ASSERT(Stream_EnsureRemainingCapacity(s, len + 5));
693
+
694
+ len += ber_write_contextual_tag(s, tag, len, TRUE);
695
+ ber_write_integer(s, value);
696
+ return len;
697
+ }
698
+
699
+ size_t ber_sizeof_integer(UINT32 value)
700
+ {
701
+ if (value < 0x80)
702
+ {
703
+ return 3;
704
+ }
705
+ else if (value < 0x8000)
706
+ {
707
+ return 4;
708
+ }
709
+ else if (value < 0x800000)
710
+ {
711
+ return 5;
712
+ }
713
+ else if (value < 0x80000000)
714
+ {
715
+ return 6;
716
+ }
717
+ else
718
+ {
719
+ /* treat as signed integer i.e. NT/HRESULT error codes */
720
+ return 6;
721
+ }
722
+ }
723
+
724
+ size_t ber_sizeof_contextual_integer(UINT32 value)
725
+ {
726
+ size_t intSize = ber_sizeof_integer(value);
727
+ return ber_sizeof_contextual_tag(intSize) + intSize;
728
+ }
729
+
730
+ BOOL ber_read_integer_length(wStream* s, size_t* length)
731
+ {
732
+ return ber_read_universal_tag(s, BER_TAG_INTEGER, FALSE) && ber_read_length(s, length);
733
+ }
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/crypto/cert_common.c ADDED
@@ -0,0 +1,242 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * Certificate Handling
4
+ *
5
+ * Copyright 2011 Jiten Pathy
6
+ * Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
7
+ * Copyright 2015 Thincast Technologies GmbH
8
+ * Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
9
+ * Copyright 2023 Armin Novak <anovak@thincast.com>
10
+ * Copyright 2023 Thincast Technologies GmbH
11
+ *
12
+ * Licensed under the Apache License, Version 2.0 (the "License");
13
+ * you may not use this file except in compliance with the License.
14
+ * You may obtain a copy of the License at
15
+ *
16
+ * http://www.apache.org/licenses/LICENSE-2.0
17
+ *
18
+ * Unless required by applicable law or agreed to in writing, software
19
+ * distributed under the License is distributed on an "AS IS" BASIS,
20
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21
+ * See the License for the specific language governing permissions and
22
+ * limitations under the License.
23
+ */
24
+
25
+ #include <freerdp/config.h>
26
+
27
+ #include <errno.h>
28
+ #include <stdio.h>
29
+ #include <string.h>
30
+
31
+ #include <winpr/assert.h>
32
+ #include <winpr/wtypes.h>
33
+ #include <winpr/crt.h>
34
+ #include <winpr/file.h>
35
+ #include <winpr/crypto.h>
36
+
37
+ #include <openssl/pem.h>
38
+ #include <openssl/rsa.h>
39
+ #include <openssl/bn.h>
40
+
41
+ #include "cert_common.h"
42
+ #include "crypto.h"
43
+ #include "opensslcompat.h"
44
+
45
+ #define TAG FREERDP_TAG("core")
46
+
47
+ static BOOL cert_info_allocate(rdpCertInfo* info, size_t size);
48
+
49
+ BOOL read_bignum(BYTE** dst, UINT32* length, const BIGNUM* num, BOOL alloc)
50
+ {
51
+ WINPR_ASSERT(dst);
52
+ WINPR_ASSERT(length);
53
+ WINPR_ASSERT(num);
54
+
55
+ if (alloc)
56
+ {
57
+ free(*dst);
58
+ *dst = NULL;
59
+ *length = 0;
60
+ }
61
+
62
+ const int len = BN_num_bytes(num);
63
+ if (len < 0)
64
+ return FALSE;
65
+
66
+ if (!alloc)
67
+ {
68
+ if (*length < (UINT32)len)
69
+ return FALSE;
70
+ }
71
+
72
+ if (len > 0)
73
+ {
74
+ if (alloc)
75
+ {
76
+ *dst = malloc((size_t)len);
77
+ if (!*dst)
78
+ return FALSE;
79
+ }
80
+ BN_bn2bin(num, *dst);
81
+ crypto_reverse(*dst, (size_t)len);
82
+ *length = (UINT32)len;
83
+ }
84
+
85
+ return TRUE;
86
+ }
87
+
88
+ BOOL cert_info_create(rdpCertInfo* dst, const BIGNUM* rsa, const BIGNUM* rsa_e)
89
+ {
90
+ const rdpCertInfo empty = { 0 };
91
+
92
+ WINPR_ASSERT(dst);
93
+ WINPR_ASSERT(rsa);
94
+
95
+ *dst = empty;
96
+
97
+ if (!read_bignum(&dst->Modulus, &dst->ModulusLength, rsa, TRUE))
98
+ goto fail;
99
+
100
+ UINT32 len = sizeof(dst->exponent);
101
+ BYTE* ptr = &dst->exponent[0];
102
+ if (!read_bignum(&ptr, &len, rsa_e, FALSE))
103
+ goto fail;
104
+ return TRUE;
105
+
106
+ fail:
107
+ cert_info_free(dst);
108
+ return FALSE;
109
+ }
110
+
111
+ BOOL cert_info_clone(rdpCertInfo* dst, const rdpCertInfo* src)
112
+ {
113
+ WINPR_ASSERT(dst);
114
+ WINPR_ASSERT(src);
115
+
116
+ *dst = *src;
117
+
118
+ dst->Modulus = NULL;
119
+ dst->ModulusLength = 0;
120
+ if (src->ModulusLength > 0)
121
+ {
122
+ dst->Modulus = malloc(src->ModulusLength);
123
+ if (!dst->Modulus)
124
+ return FALSE;
125
+ memcpy(dst->Modulus, src->Modulus, src->ModulusLength);
126
+ dst->ModulusLength = src->ModulusLength;
127
+ }
128
+ return TRUE;
129
+ }
130
+
131
+ void cert_info_free(rdpCertInfo* info)
132
+ {
133
+ WINPR_ASSERT(info);
134
+ free(info->Modulus);
135
+ info->ModulusLength = 0;
136
+ info->Modulus = NULL;
137
+ }
138
+
139
+ BOOL cert_info_allocate(rdpCertInfo* info, size_t size)
140
+ {
141
+ WINPR_ASSERT(info);
142
+ cert_info_free(info);
143
+
144
+ info->Modulus = (BYTE*)malloc(size);
145
+
146
+ if (!info->Modulus && (size > 0))
147
+ {
148
+ WLog_ERR(TAG, "Failed to allocate info->Modulus of size %" PRIuz, size);
149
+ return FALSE;
150
+ }
151
+ info->ModulusLength = (UINT32)size;
152
+ return TRUE;
153
+ }
154
+
155
+ BOOL cert_info_read_modulus(rdpCertInfo* info, size_t size, wStream* s)
156
+ {
157
+ if (!Stream_CheckAndLogRequiredLength(TAG, s, size))
158
+ return FALSE;
159
+ if (size > UINT32_MAX)
160
+ {
161
+ WLog_ERR(TAG, "modulus size %" PRIuz " exceeds limit of %" PRIu32, size, UINT32_MAX);
162
+ return FALSE;
163
+ }
164
+ if (!cert_info_allocate(info, size))
165
+ return FALSE;
166
+ Stream_Read(s, info->Modulus, info->ModulusLength);
167
+ return TRUE;
168
+ }
169
+
170
+ BOOL cert_info_read_exponent(rdpCertInfo* info, size_t size, wStream* s)
171
+ {
172
+ if (!Stream_CheckAndLogRequiredLength(TAG, s, size))
173
+ return FALSE;
174
+ if (size > 4)
175
+ {
176
+ WLog_ERR(TAG, "exponent size %" PRIuz " exceeds limit of %" PRIu32, size, 4);
177
+ return FALSE;
178
+ }
179
+ if (!info->Modulus || (info->ModulusLength == 0))
180
+ {
181
+ WLog_ERR(TAG, "invalid modulus=%p [%" PRIu32 "]", info->Modulus, info->ModulusLength);
182
+ return FALSE;
183
+ }
184
+ Stream_Read(s, &info->exponent[4 - size], size);
185
+ crypto_reverse(info->Modulus, info->ModulusLength);
186
+ crypto_reverse(info->exponent, 4);
187
+ return TRUE;
188
+ }
189
+
190
+ #if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
191
+ X509* x509_from_rsa(const RSA* rsa)
192
+ {
193
+ EVP_PKEY* pubkey = NULL;
194
+ X509* x509 = NULL;
195
+ BIO* bio = BIO_new(
196
+ #if defined(LIBRESSL_VERSION_NUMBER)
197
+ BIO_s_mem()
198
+ #else
199
+ BIO_s_secmem()
200
+ #endif
201
+ );
202
+ if (!bio)
203
+ {
204
+ WLog_ERR(TAG, "BIO_new() failed");
205
+ return NULL;
206
+ }
207
+
208
+ const int rc = PEM_write_bio_RSA_PUBKEY(bio, (RSA*)rsa);
209
+ if (rc != 1)
210
+ {
211
+ WLog_ERR(TAG, "PEM_write_bio_RSA_PUBKEY(bio, (RSA*)rsa) failed");
212
+ goto fail;
213
+ }
214
+
215
+ pubkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL);
216
+ if (!pubkey)
217
+ {
218
+ WLog_ERR(TAG, "PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL) failed");
219
+ goto fail;
220
+ }
221
+
222
+ x509 = X509_new();
223
+ if (!x509)
224
+ {
225
+ WLog_ERR(TAG, "X509_new() failed");
226
+ goto fail;
227
+ }
228
+
229
+ const int res = X509_set_pubkey(x509, pubkey);
230
+ if (res != 1)
231
+ {
232
+ WLog_ERR(TAG, "X509_set_pubkey(x509, pubkey) failed");
233
+ X509_free(x509);
234
+ x509 = NULL;
235
+ goto fail;
236
+ }
237
+ fail:
238
+ BIO_free_all(bio);
239
+ EVP_PKEY_free(pubkey);
240
+ return x509;
241
+ }
242
+ #endif
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/crypto/cert_common.h ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * Certificate and private key helpers
4
+ *
5
+ * Copyright 2023 Armin Novak <anovak@thincast.com>
6
+ * Copyright 2023 Thincast Technologies GmbH
7
+ *
8
+ * Licensed under the Apache License, Version 2.0 (the "License");
9
+ * you may not use this file except in compliance with the License.
10
+ * You may obtain a copy of the License at
11
+ *
12
+ * http://www.apache.org/licenses/LICENSE-2.0
13
+ *
14
+ * Unless required by applicable law or agreed to in writing, software
15
+ * distributed under the License is distributed on an "AS IS" BASIS,
16
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ * See the License for the specific language governing permissions and
18
+ * limitations under the License.
19
+ */
20
+
21
+ #ifndef FREERDP_LIB_CORE_CERT_COMMON_H
22
+ #define FREERDP_LIB_CORE_CERT_COMMON_H
23
+
24
+ #include <freerdp/crypto/ber.h>
25
+ #include <freerdp/crypto/crypto.h>
26
+
27
+ #include <freerdp/settings.h>
28
+ #include <freerdp/log.h>
29
+ #include <freerdp/api.h>
30
+
31
+ #ifdef __cplusplus
32
+ extern "C"
33
+ {
34
+ #endif
35
+
36
+ FREERDP_LOCAL BOOL cert_info_create(rdpCertInfo* dst, const BIGNUM* rsa, const BIGNUM* rsa_e);
37
+ FREERDP_LOCAL void cert_info_free(rdpCertInfo* info);
38
+
39
+ FREERDP_LOCAL BOOL cert_info_clone(rdpCertInfo* dst, const rdpCertInfo* src);
40
+ FREERDP_LOCAL BOOL cert_info_read_modulus(rdpCertInfo* info, size_t size, wStream* s);
41
+ FREERDP_LOCAL BOOL cert_info_read_exponent(rdpCertInfo* info, size_t size, wStream* s);
42
+
43
+ FREERDP_LOCAL BOOL read_bignum(BYTE** dst, UINT32* length, const BIGNUM* num, BOOL alloc);
44
+
45
+ #if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
46
+ FREERDP_LOCAL X509* x509_from_rsa(const RSA* rsa);
47
+ #endif
48
+
49
+ #ifdef __cplusplus
50
+ }
51
+ #endif
52
+
53
+ #endif /* FREERDP_LIB_CORE_CERT_COMMON_H */
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/crypto/certificate.c ADDED
@@ -0,0 +1,1926 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * Certificate Handling
4
+ *
5
+ * Copyright 2011 Jiten Pathy
6
+ * Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
7
+ * Copyright 2015 Thincast Technologies GmbH
8
+ * Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
9
+ * Copyright 2023 Armin Novak <anovak@thincast.com>
10
+ * Copyright 2023 Thincast Technologies GmbH
11
+ *
12
+ * Licensed under the Apache License, Version 2.0 (the "License");
13
+ * you may not use this file except in compliance with the License.
14
+ * You may obtain a copy of the License at
15
+ *
16
+ * http://www.apache.org/licenses/LICENSE-2.0
17
+ *
18
+ * Unless required by applicable law or agreed to in writing, software
19
+ * distributed under the License is distributed on an "AS IS" BASIS,
20
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21
+ * See the License for the specific language governing permissions and
22
+ * limitations under the License.
23
+ */
24
+
25
+ #include <freerdp/config.h>
26
+
27
+ #include <errno.h>
28
+ #include <stdio.h>
29
+ #include <string.h>
30
+
31
+ #include <winpr/assert.h>
32
+ #include <winpr/wtypes.h>
33
+ #include <winpr/crt.h>
34
+ #include <winpr/file.h>
35
+ #include <winpr/print.h>
36
+ #include <winpr/crypto.h>
37
+
38
+ #include <freerdp/crypto/certificate.h>
39
+
40
+ #include <openssl/err.h>
41
+ #include <openssl/pem.h>
42
+ #include <openssl/rsa.h>
43
+ #include <openssl/bn.h>
44
+ #include <openssl/x509v3.h>
45
+
46
+ #if defined(OPENSSL_VERSION_MAJOR) && (OPENSSL_VERSION_MAJOR >= 3)
47
+ #include <openssl/core_names.h>
48
+ #include <openssl/param_build.h>
49
+ #include <openssl/evp.h>
50
+ #include <openssl/x509.h>
51
+ #endif
52
+
53
+ #include "certificate.h"
54
+ #include "cert_common.h"
55
+ #include "crypto.h"
56
+
57
+ #include "x509_utils.h"
58
+ #include "privatekey.h"
59
+ #include "opensslcompat.h"
60
+
61
+ #define TAG FREERDP_TAG("core")
62
+
63
+ #ifdef WITH_DEBUG_CERTIFICATE
64
+ #define CERTIFICATE_TAG FREERDP_TAG("core.certificate")
65
+ #define DEBUG_CERTIFICATE(...) WLog_DBG(TAG, __VA_ARGS__)
66
+ #else
67
+ #define DEBUG_CERTIFICATE(...) \
68
+ do \
69
+ { \
70
+ } while (0)
71
+ #endif
72
+
73
+ #define TSSK_KEY_LENGTH 64
74
+
75
+ struct rdp_CertBlob
76
+ {
77
+ UINT32 length;
78
+ BYTE* data;
79
+ };
80
+ typedef struct rdp_CertBlob rdpCertBlob;
81
+
82
+ struct rdp_X509CertChain
83
+ {
84
+ UINT32 count;
85
+ rdpCertBlob* array;
86
+ };
87
+ typedef struct rdp_X509CertChain rdpX509CertChain;
88
+
89
+ struct rdp_certificate
90
+ {
91
+ X509* x509;
92
+ STACK_OF(X509) * chain;
93
+
94
+ rdpCertInfo cert_info;
95
+ rdpX509CertChain x509_cert_chain;
96
+ };
97
+
98
+ /**
99
+ *
100
+ * X.509 Certificate Structure
101
+ *
102
+ * Certificate ::= SEQUENCE
103
+ * {
104
+ * tbsCertificate TBSCertificate,
105
+ * signatureAlgorithm AlgorithmIdentifier,
106
+ * signatureValue BIT_STRING
107
+ * }
108
+ *
109
+ * TBSCertificate ::= SEQUENCE
110
+ * {
111
+ * version [0] EXPLICIT Version DEFAULT v1,
112
+ * serialNumber CertificateSerialNumber,
113
+ * signature AlgorithmIdentifier,
114
+ * issuer Name,
115
+ * validity Validity,
116
+ * subject Name,
117
+ * subjectPublicKeyInfo SubjectPublicKeyInfo,
118
+ * issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
119
+ * subjectUniqueId [2] IMPLICIT UniqueIdentifier OPTIONAL,
120
+ * extensions [3] EXPLICIT Extensions OPTIONAL
121
+ * }
122
+ *
123
+ * Version ::= INTEGER { v1(0), v2(1), v3(2) }
124
+ *
125
+ * CertificateSerialNumber ::= INTEGER
126
+ *
127
+ * AlgorithmIdentifier ::= SEQUENCE
128
+ * {
129
+ * algorithm OBJECT_IDENTIFIER,
130
+ * parameters ANY DEFINED BY algorithm OPTIONAL
131
+ * }
132
+ *
133
+ * Name ::= CHOICE { RDNSequence }
134
+ *
135
+ * RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
136
+ *
137
+ * RelativeDistinguishedName ::= SET OF AttributeTypeAndValue
138
+ *
139
+ * AttributeTypeAndValue ::= SEQUENCE
140
+ * {
141
+ * type AttributeType,
142
+ * value AttributeValue
143
+ * }
144
+ *
145
+ * AttributeType ::= OBJECT_IDENTIFIER
146
+ *
147
+ * AttributeValue ::= ANY DEFINED BY AttributeType
148
+ *
149
+ * Validity ::= SEQUENCE
150
+ * {
151
+ * notBefore Time,
152
+ * notAfter Time
153
+ * }
154
+ *
155
+ * Time ::= CHOICE
156
+ * {
157
+ * utcTime UTCTime,
158
+ * generalTime GeneralizedTime
159
+ * }
160
+ *
161
+ * UniqueIdentifier ::= BIT_STRING
162
+ *
163
+ * SubjectPublicKeyInfo ::= SEQUENCE
164
+ * {
165
+ * algorithm AlgorithmIdentifier,
166
+ * subjectPublicKey BIT_STRING
167
+ * }
168
+ *
169
+ * RSAPublicKey ::= SEQUENCE
170
+ * {
171
+ * modulus INTEGER
172
+ * publicExponent INTEGER
173
+ * }
174
+ *
175
+ * Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension
176
+ *
177
+ * Extension ::= SEQUENCE
178
+ * {
179
+ * extnID OBJECT_IDENTIFIER
180
+ * critical BOOLEAN DEFAULT FALSE,
181
+ * extnValue OCTET_STRING
182
+ * }
183
+ *
184
+ */
185
+
186
+ static const char rsa_magic[4] = "RSA1";
187
+
188
+ static const char* certificate_read_errors[] = { "Certificate tag",
189
+ "TBSCertificate",
190
+ "Explicit Contextual Tag [0]",
191
+ "version",
192
+ "CertificateSerialNumber",
193
+ "AlgorithmIdentifier",
194
+ "Issuer Name",
195
+ "Validity",
196
+ "Subject Name",
197
+ "SubjectPublicKeyInfo Tag",
198
+ "subjectPublicKeyInfo::AlgorithmIdentifier",
199
+ "subjectPublicKeyInfo::subjectPublicKey",
200
+ "RSAPublicKey Tag",
201
+ "modulusLength",
202
+ "zero padding",
203
+ "modulusLength",
204
+ "modulus",
205
+ "publicExponent length",
206
+ "publicExponent" };
207
+
208
+ static const BYTE initial_signature[] = {
209
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
210
+ 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
211
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
212
+ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01
213
+ };
214
+
215
+ #if defined(CERT_VALIDATE_RSA)
216
+ static const BYTE tssk_exponent[] = { 0x5b, 0x7b, 0x88, 0xc0 };
217
+ #endif
218
+
219
+ static void certificate_free_int(rdpCertificate* certificate);
220
+ static BOOL cert_clone_int(rdpCertificate* dst, const rdpCertificate* src);
221
+
222
+ /* [MS-RDPBCGR] 5.3.3.2 X.509 Certificate Chains:
223
+ *
224
+ * More detail[MS-RDPELE] section 2.2.1.4.2.
225
+ */
226
+ static BOOL cert_blob_copy(rdpCertBlob* dst, const rdpCertBlob* src);
227
+ static void cert_blob_free(rdpCertBlob* blob);
228
+ static BOOL cert_blob_write(const rdpCertBlob* blob, wStream* s);
229
+ static BOOL cert_blob_read(rdpCertBlob* blob, wStream* s);
230
+
231
+ BOOL cert_blob_read(rdpCertBlob* blob, wStream* s)
232
+ {
233
+ UINT32 certLength = 0;
234
+ WINPR_ASSERT(blob);
235
+ cert_blob_free(blob);
236
+
237
+ if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
238
+ goto fail;
239
+
240
+ Stream_Read_UINT32(s, certLength);
241
+
242
+ if (!Stream_CheckAndLogRequiredLength(TAG, s, certLength))
243
+ goto fail;
244
+
245
+ DEBUG_CERTIFICATE("X.509 Certificate length:%" PRIu32 "", certLength);
246
+ blob->data = (BYTE*)malloc(certLength);
247
+
248
+ if (!blob->data)
249
+ goto fail;
250
+
251
+ Stream_Read(s, blob->data, certLength);
252
+ blob->length = certLength;
253
+
254
+ return TRUE;
255
+
256
+ fail:
257
+ cert_blob_free(blob);
258
+ return FALSE;
259
+ }
260
+
261
+ BOOL cert_blob_write(const rdpCertBlob* blob, wStream* s)
262
+ {
263
+ WINPR_ASSERT(blob);
264
+
265
+ if (!Stream_EnsureRemainingCapacity(s, 4 + blob->length))
266
+ return FALSE;
267
+
268
+ Stream_Write_UINT32(s, blob->length);
269
+ Stream_Write(s, blob->data, blob->length);
270
+ return TRUE;
271
+ }
272
+
273
+ void cert_blob_free(rdpCertBlob* blob)
274
+ {
275
+ if (!blob)
276
+ return;
277
+ free(blob->data);
278
+ blob->data = NULL;
279
+ blob->length = 0;
280
+ }
281
+
282
+ /**
283
+ * Read X.509 Certificate
284
+ */
285
+
286
+ static BOOL is_rsa_key(const X509* x509)
287
+ {
288
+ EVP_PKEY* evp = X509_get0_pubkey(x509);
289
+ if (!evp)
290
+ return FALSE;
291
+
292
+ return (EVP_PKEY_id(evp) == EVP_PKEY_RSA);
293
+ }
294
+
295
+ static BOOL certificate_read_x509_certificate(const rdpCertBlob* cert, rdpCertInfo* info)
296
+ {
297
+ wStream sbuffer = { 0 };
298
+ wStream* s = NULL;
299
+ size_t length = 0;
300
+ BYTE padding = 0;
301
+ UINT32 version = 0;
302
+ size_t modulus_length = 0;
303
+ size_t exponent_length = 0;
304
+ int error = 0;
305
+
306
+ WINPR_ASSERT(cert);
307
+ WINPR_ASSERT(info);
308
+
309
+ cert_info_free(info);
310
+
311
+ s = Stream_StaticConstInit(&sbuffer, cert->data, cert->length);
312
+
313
+ if (!s)
314
+ return FALSE;
315
+
316
+ if (!ber_read_sequence_tag(s, &length)) /* Certificate (SEQUENCE) */
317
+ goto error;
318
+
319
+ error++;
320
+
321
+ if (!ber_read_sequence_tag(s, &length)) /* TBSCertificate (SEQUENCE) */
322
+ goto error;
323
+
324
+ error++;
325
+
326
+ if (!ber_read_contextual_tag(s, 0, &length, TRUE)) /* Explicit Contextual Tag [0] */
327
+ goto error;
328
+
329
+ error++;
330
+
331
+ if (!ber_read_integer(s, &version)) /* version (INTEGER) */
332
+ goto error;
333
+
334
+ error++;
335
+ version++;
336
+
337
+ /* serialNumber */
338
+ if (!ber_read_integer(s, NULL)) /* CertificateSerialNumber (INTEGER) */
339
+ goto error;
340
+
341
+ error++;
342
+
343
+ /* signature */
344
+ if (!ber_read_sequence_tag(s, &length) ||
345
+ !Stream_SafeSeek(s, length)) /* AlgorithmIdentifier (SEQUENCE) */
346
+ goto error;
347
+
348
+ error++;
349
+
350
+ /* issuer */
351
+ if (!ber_read_sequence_tag(s, &length) || !Stream_SafeSeek(s, length)) /* Name (SEQUENCE) */
352
+ goto error;
353
+
354
+ error++;
355
+
356
+ /* validity */
357
+ if (!ber_read_sequence_tag(s, &length) || !Stream_SafeSeek(s, length)) /* Validity (SEQUENCE) */
358
+ goto error;
359
+
360
+ error++;
361
+
362
+ /* subject */
363
+ if (!ber_read_sequence_tag(s, &length) || !Stream_SafeSeek(s, length)) /* Name (SEQUENCE) */
364
+ goto error;
365
+
366
+ error++;
367
+
368
+ /* subjectPublicKeyInfo */
369
+ if (!ber_read_sequence_tag(s, &length)) /* SubjectPublicKeyInfo (SEQUENCE) */
370
+ goto error;
371
+
372
+ error++;
373
+
374
+ /* subjectPublicKeyInfo::AlgorithmIdentifier */
375
+ if (!ber_read_sequence_tag(s, &length) ||
376
+ !Stream_SafeSeek(s, length)) /* AlgorithmIdentifier (SEQUENCE) */
377
+ goto error;
378
+
379
+ error++;
380
+
381
+ /* subjectPublicKeyInfo::subjectPublicKey */
382
+ if (!ber_read_bit_string(s, &length, &padding)) /* BIT_STRING */
383
+ goto error;
384
+
385
+ error++;
386
+
387
+ /* RSAPublicKey (SEQUENCE) */
388
+ if (!ber_read_sequence_tag(s, &length)) /* SEQUENCE */
389
+ goto error;
390
+
391
+ error++;
392
+
393
+ if (!ber_read_integer_length(s, &modulus_length)) /* modulus (INTEGER) */
394
+ goto error;
395
+
396
+ error++;
397
+
398
+ /* skip zero padding, if any */
399
+ do
400
+ {
401
+ if (!Stream_CheckAndLogRequiredLength(TAG, s, 1))
402
+ goto error;
403
+
404
+ Stream_Peek_UINT8(s, padding);
405
+
406
+ if (padding == 0)
407
+ {
408
+ if (!Stream_SafeSeek(s, 1))
409
+ goto error;
410
+
411
+ modulus_length--;
412
+ }
413
+ } while (padding == 0);
414
+
415
+ error++;
416
+
417
+ if (!cert_info_read_modulus(info, modulus_length, s))
418
+ goto error;
419
+
420
+ error++;
421
+
422
+ if (!ber_read_integer_length(s, &exponent_length)) /* publicExponent (INTEGER) */
423
+ goto error;
424
+
425
+ error++;
426
+
427
+ if (!cert_info_read_exponent(info, exponent_length, s))
428
+ goto error;
429
+ return TRUE;
430
+ error:
431
+ WLog_ERR(TAG, "error reading when reading certificate: part=%s error=%d",
432
+ certificate_read_errors[error], error);
433
+ cert_info_free(info);
434
+ return FALSE;
435
+ }
436
+
437
+ /**
438
+ * Instantiate new X.509 Certificate Chain.
439
+ * @param count certificate chain count
440
+ * @return new X.509 certificate chain
441
+ */
442
+
443
+ static rdpX509CertChain certificate_new_x509_certificate_chain(UINT32 count)
444
+ {
445
+ rdpX509CertChain x509_cert_chain = { 0 };
446
+
447
+ x509_cert_chain.array = (rdpCertBlob*)calloc(count, sizeof(rdpCertBlob));
448
+
449
+ if (x509_cert_chain.array)
450
+ x509_cert_chain.count = count;
451
+
452
+ return x509_cert_chain;
453
+ }
454
+
455
+ /**
456
+ * Free X.509 Certificate Chain.
457
+ * @param x509_cert_chain X.509 certificate chain to be freed
458
+ */
459
+
460
+ static void certificate_free_x509_certificate_chain(rdpX509CertChain* x509_cert_chain)
461
+ {
462
+ if (!x509_cert_chain)
463
+ return;
464
+
465
+ if (x509_cert_chain->array)
466
+ {
467
+ for (UINT32 i = 0; i < x509_cert_chain->count; i++)
468
+ {
469
+ rdpCertBlob* element = &x509_cert_chain->array[i];
470
+ cert_blob_free(element);
471
+ }
472
+ }
473
+
474
+ free(x509_cert_chain->array);
475
+ x509_cert_chain->array = NULL;
476
+ x509_cert_chain->count = 0;
477
+ }
478
+
479
+ #if defined(OPENSSL_VERSION_MAJOR) && (OPENSSL_VERSION_MAJOR >= 3)
480
+ static OSSL_PARAM* get_params(const BIGNUM* e, const BIGNUM* mod)
481
+ {
482
+ WINPR_ASSERT(e);
483
+ WINPR_ASSERT(mod);
484
+
485
+ OSSL_PARAM* parameters = NULL;
486
+ OSSL_PARAM_BLD* param = OSSL_PARAM_BLD_new();
487
+ if (!param)
488
+ {
489
+ WLog_ERR(TAG, "OSSL_PARAM_BLD_new() failed");
490
+ return NULL;
491
+ }
492
+
493
+ const int bits = BN_num_bits(e);
494
+ if ((bits < 0) || (bits > 32))
495
+ {
496
+ WLog_ERR(TAG, "BN_num_bits(e) out of range: 0 <= %d <= 32", bits);
497
+ goto fail;
498
+ }
499
+
500
+ UINT ie = 0;
501
+ const int ne = BN_bn2nativepad(e, (BYTE*)&ie, sizeof(ie));
502
+ if ((ne < 0) || (ne > 4))
503
+ {
504
+ WLog_ERR(TAG, "BN_bn2nativepad(e, (BYTE*)&ie, sizeof(ie)) out of range: 0<= %d <= 4", ne);
505
+ goto fail;
506
+ }
507
+ if (OSSL_PARAM_BLD_push_BN(param, OSSL_PKEY_PARAM_RSA_N, mod) != 1)
508
+ {
509
+ WLog_ERR(TAG, "OSSL_PARAM_BLD_push_BN(param, OSSL_PKEY_PARAM_RSA_N, mod) failed");
510
+ goto fail;
511
+ }
512
+ if (OSSL_PARAM_BLD_push_uint(param, OSSL_PKEY_PARAM_RSA_E, ie) != 1)
513
+ {
514
+ WLog_ERR(TAG, "OSSL_PARAM_BLD_push_uint(param, OSSL_PKEY_PARAM_RSA_E, ie) failed");
515
+ goto fail;
516
+ }
517
+
518
+ parameters = OSSL_PARAM_BLD_to_param(param);
519
+ if (!parameters)
520
+ WLog_ERR(TAG, "OSSL_PARAM_BLD_to_param(param) failed");
521
+ fail:
522
+ OSSL_PARAM_BLD_free(param);
523
+
524
+ return parameters;
525
+ }
526
+ #endif
527
+
528
+ static BOOL update_x509_from_info(rdpCertificate* cert)
529
+ {
530
+ BOOL rc = FALSE;
531
+
532
+ WINPR_ASSERT(cert);
533
+
534
+ X509_free(cert->x509);
535
+ cert->x509 = NULL;
536
+
537
+ rdpCertInfo* info = &cert->cert_info;
538
+
539
+ BIGNUM* e = BN_new();
540
+ BIGNUM* mod = BN_new();
541
+ #if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
542
+ RSA* rsa = RSA_new();
543
+ if (!rsa)
544
+ {
545
+ WLog_ERR(TAG, "RSA_new() failed");
546
+ goto fail;
547
+ }
548
+ #endif
549
+
550
+ if (!mod || !e)
551
+ {
552
+ WLog_ERR(TAG, "failure: mod=%p, e=%p", mod, e);
553
+ goto fail;
554
+ }
555
+
556
+ WINPR_ASSERT(info->ModulusLength <= INT_MAX);
557
+ if (!BN_bin2bn(info->Modulus, (int)info->ModulusLength, mod))
558
+ {
559
+ WLog_ERR(TAG, "BN_bin2bn(info->Modulus, (int)info->ModulusLength, mod) failed");
560
+ goto fail;
561
+ }
562
+
563
+ if (!BN_bin2bn(info->exponent, (int)sizeof(info->exponent), e))
564
+ {
565
+ WLog_ERR(TAG, "BN_bin2bn(info->exponent, (int)sizeof(info->exponent), e) failed");
566
+ goto fail;
567
+ }
568
+
569
+ #if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
570
+ const int rec = RSA_set0_key(rsa, mod, e, NULL);
571
+ if (rec != 1)
572
+ {
573
+ WLog_ERR(TAG, "RSA_set0_key(rsa, mod, e, NULL) failed");
574
+ goto fail;
575
+ }
576
+
577
+ cert->x509 = x509_from_rsa(rsa);
578
+ #else
579
+ EVP_PKEY* pkey = NULL;
580
+ EVP_PKEY_CTX* ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL);
581
+ if (!ctx)
582
+ {
583
+ WLog_ERR(TAG, "EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL) failed");
584
+ goto fail2;
585
+ }
586
+ const int xx = EVP_PKEY_fromdata_init(ctx);
587
+ if (xx != 1)
588
+ {
589
+ WLog_ERR(TAG, "EVP_PKEY_fromdata_init(ctx) failed");
590
+ goto fail2;
591
+ }
592
+
593
+ OSSL_PARAM* parameters = get_params(e, mod);
594
+ if (!parameters)
595
+ goto fail2;
596
+
597
+ const int rc2 = EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_PUBLIC_KEY, parameters);
598
+ OSSL_PARAM_free(parameters);
599
+ if (rc2 <= 0)
600
+ {
601
+ WLog_ERR(TAG, "EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_PUBLIC_KEY, parameters) failed");
602
+ goto fail2;
603
+ }
604
+
605
+ cert->x509 = X509_new();
606
+ if (!cert->x509)
607
+ {
608
+ WLog_ERR(TAG, "X509_new() failed");
609
+ goto fail2;
610
+ }
611
+
612
+ if (X509_set_pubkey(cert->x509, pkey) != 1)
613
+ {
614
+ WLog_ERR(TAG, "X509_set_pubkey(cert->x509, pkey) failed");
615
+ X509_free(cert->x509);
616
+ cert->x509 = NULL;
617
+ }
618
+ fail2:
619
+ EVP_PKEY_free(pkey);
620
+ EVP_PKEY_CTX_free(ctx);
621
+ #endif
622
+ if (!cert->x509)
623
+ goto fail;
624
+
625
+ rc = TRUE;
626
+
627
+ fail:
628
+ if (!rc)
629
+ WLog_ERR(TAG, "failed to update x509 from rdpCertInfo");
630
+
631
+ #if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
632
+ if (rsa)
633
+ RSA_free(rsa);
634
+ else
635
+ #endif
636
+ {
637
+ BN_free(mod);
638
+ BN_free(e);
639
+ }
640
+ return rc;
641
+ }
642
+
643
+ static BOOL certificate_process_server_public_key(rdpCertificate* cert, wStream* s, UINT32 length)
644
+ {
645
+ char magic[sizeof(rsa_magic)] = { 0 };
646
+ UINT32 keylen = 0;
647
+ UINT32 bitlen = 0;
648
+ UINT32 datalen = 0;
649
+
650
+ WINPR_ASSERT(cert);
651
+ WINPR_ASSERT(s);
652
+
653
+ if (!Stream_CheckAndLogRequiredLength(TAG, s, 20))
654
+ return FALSE;
655
+
656
+ Stream_Read(s, magic, sizeof(magic));
657
+
658
+ if (memcmp(magic, rsa_magic, sizeof(magic)) != 0)
659
+ {
660
+ WLog_ERR(TAG, "invalid RSA magic bytes");
661
+ return FALSE;
662
+ }
663
+
664
+ rdpCertInfo* info = &cert->cert_info;
665
+ cert_info_free(info);
666
+
667
+ Stream_Read_UINT32(s, keylen);
668
+ Stream_Read_UINT32(s, bitlen);
669
+ Stream_Read_UINT32(s, datalen);
670
+ Stream_Read(s, info->exponent, 4);
671
+
672
+ if (keylen <= 8)
673
+ {
674
+ WLog_ERR(TAG, "Invalid RSA keylen=%" PRIu32 " <= 8", keylen);
675
+ return FALSE;
676
+ }
677
+ if (!Stream_CheckAndLogRequiredLength(TAG, s, keylen))
678
+ return FALSE;
679
+ if (keylen != (bitlen / 8ull) + 8ull)
680
+ {
681
+ WLog_ERR(TAG, "Invalid RSA key bitlen %" PRIu32 ", expected %" PRIu32, bitlen,
682
+ (keylen - 8) * 8);
683
+ return FALSE;
684
+ }
685
+ if (datalen != (bitlen / 8ull) - 1ull)
686
+ {
687
+ WLog_ERR(TAG, "Invalid RSA key datalen %" PRIu32 ", expected %" PRIu32, datalen,
688
+ (bitlen / 8ull) - 1ull);
689
+ return FALSE;
690
+ }
691
+ info->ModulusLength = keylen - 8;
692
+ BYTE* tmp = realloc(info->Modulus, info->ModulusLength);
693
+
694
+ if (!tmp)
695
+ {
696
+ WLog_ERR(TAG, "Failed to reallocate modulus of length %" PRIu32, info->ModulusLength);
697
+ return FALSE;
698
+ }
699
+ info->Modulus = tmp;
700
+
701
+ Stream_Read(s, info->Modulus, info->ModulusLength);
702
+ Stream_Seek(s, 8); /* 8 bytes of zero padding */
703
+ return update_x509_from_info(cert);
704
+ }
705
+
706
+ static BOOL certificate_process_server_public_signature(rdpCertificate* certificate,
707
+ const BYTE* sigdata, size_t sigdatalen,
708
+ wStream* s, UINT32 siglen)
709
+ {
710
+ WINPR_ASSERT(certificate);
711
+ #if defined(CERT_VALIDATE_RSA)
712
+ BYTE sig[TSSK_KEY_LENGTH];
713
+ #endif
714
+ BYTE encsig[TSSK_KEY_LENGTH + 8];
715
+ #if defined(CERT_VALIDATE_MD5) && defined(CERT_VALIDATE_RSA)
716
+ BYTE md5hash[WINPR_MD5_DIGEST_LENGTH];
717
+ #endif
718
+ #if !defined(CERT_VALIDATE_MD5) || !defined(CERT_VALIDATE_RSA)
719
+ (void)sigdata;
720
+ (void)sigdatalen;
721
+ #endif
722
+ (void)certificate;
723
+ /* Do not bother with validation of server proprietary certificate. The use of MD5 here is not
724
+ * allowed under FIPS. Since the validation is not protecting against anything since the
725
+ * private/public keys are well known and documented in MS-RDPBCGR section 5.3.3.1, we are not
726
+ * gaining any security by using MD5 for signature comparison. Rather then use MD5
727
+ * here we just dont do the validation to avoid its use. Historically, freerdp has been ignoring
728
+ * a failed validation anyways. */
729
+ #if defined(CERT_VALIDATE_MD5)
730
+
731
+ if (!winpr_Digest(WINPR_MD_MD5, sigdata, sigdatalen, md5hash, sizeof(md5hash)))
732
+ return FALSE;
733
+
734
+ #endif
735
+ Stream_Read(s, encsig, siglen);
736
+
737
+ if (siglen < 8)
738
+ return FALSE;
739
+
740
+ /* Last 8 bytes shall be all zero. */
741
+ #if defined(CERT_VALIDATE_PADDING)
742
+ {
743
+ size_t sum = 0;
744
+ for (size_t i = sizeof(encsig) - 8; i < sizeof(encsig); i++)
745
+ sum += encsig[i];
746
+
747
+ if (sum != 0)
748
+ {
749
+ WLog_ERR(TAG, "invalid signature");
750
+ return FALSE;
751
+ }
752
+ }
753
+ #endif
754
+ #if defined(CERT_VALIDATE_RSA)
755
+
756
+ if (crypto_rsa_public_decrypt(encsig, siglen - 8, TSSK_KEY_LENGTH, tssk_modulus, tssk_exponent,
757
+ sig) <= 0)
758
+ {
759
+ WLog_ERR(TAG, "invalid RSA decrypt");
760
+ return FALSE;
761
+ }
762
+
763
+ /* Verify signature. */
764
+ /* Do not bother with validation of server proprietary certificate as described above. */
765
+ #if defined(CERT_VALIDATE_MD5)
766
+
767
+ if (memcmp(md5hash, sig, sizeof(md5hash)) != 0)
768
+ {
769
+ WLog_ERR(TAG, "invalid signature");
770
+ return FALSE;
771
+ }
772
+
773
+ #endif
774
+ /*
775
+ * Verify rest of decrypted data:
776
+ * The 17th byte is 0x00.
777
+ * The 18th through 62nd bytes are each 0xFF.
778
+ * The 63rd byte is 0x01.
779
+ */
780
+ {
781
+ size_t sum = 0;
782
+ for (size_t i = 17; i < 62; i++)
783
+ sum += sig[i];
784
+
785
+ if (sig[16] != 0x00 || sum != 0xFF * (62 - 17) || sig[62] != 0x01)
786
+ {
787
+ WLog_ERR(TAG, "invalid signature");
788
+ return FALSE;
789
+ }
790
+ }
791
+ #endif
792
+ return TRUE;
793
+ }
794
+
795
+ static BOOL certificate_read_server_proprietary_certificate(rdpCertificate* certificate, wStream* s)
796
+ {
797
+ UINT32 dwSigAlgId = 0;
798
+ UINT32 dwKeyAlgId = 0;
799
+ UINT16 wPublicKeyBlobType = 0;
800
+ UINT16 wPublicKeyBlobLen = 0;
801
+ UINT16 wSignatureBlobType = 0;
802
+ UINT16 wSignatureBlobLen = 0;
803
+ size_t sigdatalen = 0;
804
+
805
+ WINPR_ASSERT(certificate);
806
+ if (!Stream_CheckAndLogRequiredLength(TAG, s, 12))
807
+ return FALSE;
808
+
809
+ /* -4, because we need to include dwVersion */
810
+ const BYTE* sigdata = Stream_PointerAs(s, const BYTE) - 4;
811
+ Stream_Read_UINT32(s, dwSigAlgId);
812
+ Stream_Read_UINT32(s, dwKeyAlgId);
813
+
814
+ if (!((dwSigAlgId == SIGNATURE_ALG_RSA) && (dwKeyAlgId == KEY_EXCHANGE_ALG_RSA)))
815
+ {
816
+ WLog_ERR(TAG,
817
+ "unsupported signature or key algorithm, dwSigAlgId=%" PRIu32
818
+ " dwKeyAlgId=%" PRIu32 "",
819
+ dwSigAlgId, dwKeyAlgId);
820
+ return FALSE;
821
+ }
822
+
823
+ Stream_Read_UINT16(s, wPublicKeyBlobType);
824
+
825
+ if (wPublicKeyBlobType != BB_RSA_KEY_BLOB)
826
+ {
827
+ WLog_ERR(TAG, "unsupported public key blob type %" PRIu16 "", wPublicKeyBlobType);
828
+ return FALSE;
829
+ }
830
+
831
+ Stream_Read_UINT16(s, wPublicKeyBlobLen);
832
+
833
+ if (!Stream_CheckAndLogRequiredLength(TAG, s, wPublicKeyBlobLen))
834
+ return FALSE;
835
+
836
+ if (!certificate_process_server_public_key(certificate, s, wPublicKeyBlobLen))
837
+ return FALSE;
838
+
839
+ if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
840
+ return FALSE;
841
+
842
+ sigdatalen = WINPR_ASSERTING_INT_CAST(size_t, Stream_PointerAs(s, const BYTE) - sigdata);
843
+ Stream_Read_UINT16(s, wSignatureBlobType);
844
+
845
+ if (wSignatureBlobType != BB_RSA_SIGNATURE_BLOB)
846
+ {
847
+ WLog_ERR(TAG, "unsupported blob signature %" PRIu16 "", wSignatureBlobType);
848
+ return FALSE;
849
+ }
850
+
851
+ Stream_Read_UINT16(s, wSignatureBlobLen);
852
+
853
+ if (!Stream_CheckAndLogRequiredLength(TAG, s, wSignatureBlobLen))
854
+ return FALSE;
855
+
856
+ if (wSignatureBlobLen != 72)
857
+ {
858
+ WLog_ERR(TAG, "invalid signature length (got %" PRIu16 ", expected 72)", wSignatureBlobLen);
859
+ return FALSE;
860
+ }
861
+
862
+ if (!certificate_process_server_public_signature(certificate, sigdata, sigdatalen, s,
863
+ wSignatureBlobLen))
864
+ {
865
+ WLog_ERR(TAG, "unable to parse server public signature");
866
+ return FALSE;
867
+ }
868
+ return TRUE;
869
+ }
870
+
871
+ /* [MS-RDPBCGR] 2.2.1.4.3.1.1.1 RSA Public Key (RSA_PUBLIC_KEY) */
872
+ static BOOL cert_write_rsa_public_key(wStream* s, const rdpCertificate* cert)
873
+ {
874
+ WINPR_ASSERT(cert);
875
+ WINPR_ASSERT(freerdp_certificate_is_rsa(cert));
876
+
877
+ const rdpCertInfo* info = &cert->cert_info;
878
+
879
+ const UINT32 keyLen = info->ModulusLength + 8;
880
+ const UINT32 bitLen = info->ModulusLength * 8;
881
+ const UINT32 dataLen = (bitLen / 8) - 1;
882
+ const size_t pubExpLen = sizeof(info->exponent);
883
+ const BYTE* pubExp = info->exponent;
884
+ const BYTE* modulus = info->Modulus;
885
+
886
+ const size_t wPublicKeyBlobLen = 16 + pubExpLen + keyLen;
887
+ WINPR_ASSERT(wPublicKeyBlobLen <= UINT16_MAX);
888
+ if (!Stream_EnsureRemainingCapacity(s, 2 + wPublicKeyBlobLen))
889
+ return FALSE;
890
+ Stream_Write_UINT16(s, (UINT16)wPublicKeyBlobLen);
891
+ Stream_Write(s, rsa_magic, sizeof(rsa_magic));
892
+ Stream_Write_UINT32(s, keyLen);
893
+ Stream_Write_UINT32(s, bitLen);
894
+ Stream_Write_UINT32(s, dataLen);
895
+ Stream_Write(s, pubExp, pubExpLen);
896
+ Stream_Write(s, modulus, info->ModulusLength);
897
+ Stream_Zero(s, 8);
898
+ return TRUE;
899
+ }
900
+
901
+ static BOOL cert_write_rsa_signature(wStream* s, const void* sigData, size_t sigDataLen)
902
+ {
903
+ BYTE encryptedSignature[TSSK_KEY_LENGTH] = { 0 };
904
+ BYTE signature[sizeof(initial_signature)] = { 0 };
905
+
906
+ memcpy(signature, initial_signature, sizeof(initial_signature));
907
+ if (!winpr_Digest(WINPR_MD_MD5, sigData, sigDataLen, signature, sizeof(signature)))
908
+ return FALSE;
909
+
910
+ crypto_rsa_private_encrypt(signature, sizeof(signature), priv_key_tssk, encryptedSignature,
911
+ sizeof(encryptedSignature));
912
+
913
+ if (!Stream_EnsureRemainingCapacity(s, 2 * sizeof(UINT16) + sizeof(encryptedSignature) + 8))
914
+ return FALSE;
915
+ Stream_Write_UINT16(s, BB_RSA_SIGNATURE_BLOB);
916
+ Stream_Write_UINT16(s, sizeof(encryptedSignature) + 8); /* wSignatureBlobLen */
917
+ Stream_Write(s, encryptedSignature, sizeof(encryptedSignature));
918
+ Stream_Zero(s, 8);
919
+ return TRUE;
920
+ }
921
+
922
+ /* [MS-RDPBCGR] 2.2.1.4.3.1.1 Server Proprietary Certificate (PROPRIETARYSERVERCERTIFICATE) */
923
+ static BOOL cert_write_server_certificate_v1(wStream* s, const rdpCertificate* certificate)
924
+ {
925
+ const size_t start = Stream_GetPosition(s);
926
+ const BYTE* sigData = Stream_PointerAs(s, const BYTE) - sizeof(UINT32);
927
+
928
+ WINPR_ASSERT(start >= 4);
929
+ if (!Stream_EnsureRemainingCapacity(s, 10))
930
+ return FALSE;
931
+ Stream_Write_UINT32(s, SIGNATURE_ALG_RSA);
932
+ Stream_Write_UINT32(s, KEY_EXCHANGE_ALG_RSA);
933
+ Stream_Write_UINT16(s, BB_RSA_KEY_BLOB);
934
+ if (!cert_write_rsa_public_key(s, certificate))
935
+ return FALSE;
936
+
937
+ const size_t end = Stream_GetPosition(s);
938
+ return cert_write_rsa_signature(s, sigData, end - start + sizeof(UINT32));
939
+ }
940
+
941
+ static BOOL cert_write_server_certificate_v2(wStream* s, const rdpCertificate* certificate)
942
+ {
943
+ WINPR_ASSERT(certificate);
944
+
945
+ const rdpX509CertChain* chain = &certificate->x509_cert_chain;
946
+ const size_t padding = 8ull + 4ull * chain->count;
947
+
948
+ if (!Stream_EnsureRemainingCapacity(s, sizeof(UINT32)))
949
+ return FALSE;
950
+
951
+ Stream_Write_UINT32(s, chain->count);
952
+ for (UINT32 x = 0; x < chain->count; x++)
953
+ {
954
+ const rdpCertBlob* cert = &chain->array[x];
955
+ if (!cert_blob_write(cert, s))
956
+ return FALSE;
957
+ }
958
+
959
+ if (!Stream_EnsureRemainingCapacity(s, padding))
960
+ return FALSE;
961
+ Stream_Zero(s, padding);
962
+ return TRUE;
963
+ }
964
+
965
+ SSIZE_T freerdp_certificate_write_server_cert(const rdpCertificate* certificate, UINT32 dwVersion,
966
+ wStream* s)
967
+ {
968
+ if (!certificate)
969
+ return -1;
970
+
971
+ const size_t start = Stream_GetPosition(s);
972
+ if (!Stream_EnsureRemainingCapacity(s, 4))
973
+ return -1;
974
+ Stream_Write_UINT32(s, dwVersion);
975
+
976
+ switch (dwVersion & CERT_CHAIN_VERSION_MASK)
977
+ {
978
+ case CERT_CHAIN_VERSION_1:
979
+ if (!cert_write_server_certificate_v1(s, certificate))
980
+ return -1;
981
+ break;
982
+ case CERT_CHAIN_VERSION_2:
983
+ if (!cert_write_server_certificate_v2(s, certificate))
984
+ return -1;
985
+ break;
986
+ default:
987
+ WLog_ERR(TAG, "invalid certificate chain version:%" PRIu32 "",
988
+ dwVersion & CERT_CHAIN_VERSION_MASK);
989
+ return -1;
990
+ }
991
+
992
+ const size_t end = Stream_GetPosition(s);
993
+ if (start > end)
994
+ return -1;
995
+
996
+ const size_t diff = end - start;
997
+ WINPR_ASSERT(diff <= SSIZE_MAX);
998
+ return (SSIZE_T)diff;
999
+ }
1000
+
1001
+ /**
1002
+ * Read an X.509 Certificate Chain.
1003
+ * @param cert certificate module
1004
+ * @param s stream
1005
+ * @return \b TRUE for success, \b FALSE otherwise.
1006
+ */
1007
+
1008
+ static BOOL certificate_read_server_x509_certificate_chain(rdpCertificate* cert, wStream* s)
1009
+ {
1010
+ UINT32 numCertBlobs = 0;
1011
+ DEBUG_CERTIFICATE("Server X.509 Certificate Chain");
1012
+
1013
+ WINPR_ASSERT(cert);
1014
+ if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
1015
+ return FALSE;
1016
+
1017
+ Stream_Read_UINT32(s, numCertBlobs); /* numCertBlobs */
1018
+ certificate_free_x509_certificate_chain(&cert->x509_cert_chain);
1019
+ cert->x509_cert_chain = certificate_new_x509_certificate_chain(numCertBlobs);
1020
+
1021
+ for (UINT32 i = 0; i < cert->x509_cert_chain.count; i++)
1022
+ {
1023
+ rdpCertBlob* blob = &cert->x509_cert_chain.array[i];
1024
+ if (!cert_blob_read(blob, s))
1025
+ return FALSE;
1026
+
1027
+ if (numCertBlobs - i == 1)
1028
+ {
1029
+ DEBUG_CERTIFICATE("Terminal Server Certificate");
1030
+
1031
+ BOOL res = certificate_read_x509_certificate(blob, &cert->cert_info);
1032
+
1033
+ if (res)
1034
+ {
1035
+ if (!update_x509_from_info(cert))
1036
+ res = FALSE;
1037
+ }
1038
+
1039
+ if (!res)
1040
+ {
1041
+ WLog_ERR(TAG, "Failed to read x509 certificate");
1042
+ return FALSE;
1043
+ }
1044
+
1045
+ DEBUG_CERTIFICATE("modulus length:%" PRIu32 "", cert->cert_info.ModulusLength);
1046
+ }
1047
+ }
1048
+
1049
+ return update_x509_from_info(cert);
1050
+ }
1051
+
1052
+ static BOOL certificate_write_server_x509_certificate_chain(const rdpCertificate* certificate,
1053
+ wStream* s)
1054
+ {
1055
+ UINT32 numCertBlobs = 0;
1056
+
1057
+ WINPR_ASSERT(certificate);
1058
+ WINPR_ASSERT(s);
1059
+
1060
+ numCertBlobs = certificate->x509_cert_chain.count;
1061
+
1062
+ if (!Stream_EnsureRemainingCapacity(s, 4))
1063
+ return FALSE;
1064
+ Stream_Write_UINT32(s, numCertBlobs); /* numCertBlobs */
1065
+
1066
+ for (UINT32 i = 0; i < numCertBlobs; i++)
1067
+ {
1068
+ const rdpCertBlob* cert = &certificate->x509_cert_chain.array[i];
1069
+ if (!cert_blob_write(cert, s))
1070
+ return FALSE;
1071
+ }
1072
+
1073
+ return TRUE;
1074
+ }
1075
+
1076
+ /**
1077
+ * Read a Server Certificate.
1078
+ * @param certificate certificate module
1079
+ * @param server_cert server certificate
1080
+ * @param length certificate length
1081
+ */
1082
+
1083
+ BOOL freerdp_certificate_read_server_cert(rdpCertificate* certificate, const BYTE* server_cert,
1084
+ size_t length)
1085
+ {
1086
+ BOOL ret = FALSE;
1087
+ wStream* s = NULL;
1088
+ wStream sbuffer;
1089
+ UINT32 dwVersion = 0;
1090
+
1091
+ WINPR_ASSERT(certificate);
1092
+ if (length < 4) /* NULL certificate is not an error see #1795 */
1093
+ {
1094
+ WLog_DBG(TAG, "Received empty certificate, ignoring...");
1095
+ return TRUE;
1096
+ }
1097
+
1098
+ WINPR_ASSERT(server_cert);
1099
+ s = Stream_StaticConstInit(&sbuffer, server_cert, length);
1100
+
1101
+ if (!s)
1102
+ {
1103
+ WLog_ERR(TAG, "Stream_New failed!");
1104
+ return FALSE;
1105
+ }
1106
+
1107
+ Stream_Read_UINT32(s, dwVersion); /* dwVersion (4 bytes) */
1108
+
1109
+ switch (dwVersion & CERT_CHAIN_VERSION_MASK)
1110
+ {
1111
+ case CERT_CHAIN_VERSION_1:
1112
+ ret = certificate_read_server_proprietary_certificate(certificate, s);
1113
+ break;
1114
+
1115
+ case CERT_CHAIN_VERSION_2:
1116
+ ret = certificate_read_server_x509_certificate_chain(certificate, s);
1117
+ break;
1118
+
1119
+ default:
1120
+ WLog_ERR(TAG, "invalid certificate chain version:%" PRIu32 "",
1121
+ dwVersion & CERT_CHAIN_VERSION_MASK);
1122
+ ret = FALSE;
1123
+ break;
1124
+ }
1125
+
1126
+ return ret;
1127
+ }
1128
+
1129
+ static BOOL cert_blob_copy(rdpCertBlob* dst, const rdpCertBlob* src)
1130
+ {
1131
+ WINPR_ASSERT(dst);
1132
+ WINPR_ASSERT(src);
1133
+
1134
+ cert_blob_free(dst);
1135
+ if (src->length > 0)
1136
+ {
1137
+ dst->data = malloc(src->length);
1138
+ if (!dst->data)
1139
+ return FALSE;
1140
+ dst->length = src->length;
1141
+ memcpy(dst->data, src->data, src->length);
1142
+ }
1143
+
1144
+ return TRUE;
1145
+ }
1146
+
1147
+ static BOOL cert_x509_chain_copy(rdpX509CertChain* cert, const rdpX509CertChain* src)
1148
+ {
1149
+ WINPR_ASSERT(cert);
1150
+
1151
+ certificate_free_x509_certificate_chain(cert);
1152
+ if (!src)
1153
+ return TRUE;
1154
+
1155
+ if (src->count > 0)
1156
+ {
1157
+ cert->array = calloc(src->count, sizeof(rdpCertBlob));
1158
+ if (!cert->array)
1159
+ {
1160
+ return FALSE;
1161
+ }
1162
+ cert->count = src->count;
1163
+
1164
+ for (UINT32 x = 0; x < cert->count; x++)
1165
+ {
1166
+ const rdpCertBlob* srcblob = &src->array[x];
1167
+ rdpCertBlob* dstblob = &cert->array[x];
1168
+
1169
+ if (!cert_blob_copy(dstblob, srcblob))
1170
+ {
1171
+ certificate_free_x509_certificate_chain(cert);
1172
+ return FALSE;
1173
+ }
1174
+ }
1175
+ }
1176
+
1177
+ return TRUE;
1178
+ }
1179
+
1180
+ BOOL cert_clone_int(rdpCertificate* dst, const rdpCertificate* src)
1181
+ {
1182
+ WINPR_ASSERT(dst);
1183
+ WINPR_ASSERT(src);
1184
+
1185
+ if (src->x509)
1186
+ {
1187
+ dst->x509 = X509_dup(src->x509);
1188
+ if (!dst->x509)
1189
+ return FALSE;
1190
+ }
1191
+
1192
+ if (!cert_info_clone(&dst->cert_info, &src->cert_info))
1193
+ return FALSE;
1194
+ return cert_x509_chain_copy(&dst->x509_cert_chain, &src->x509_cert_chain);
1195
+ }
1196
+
1197
+ rdpCertificate* freerdp_certificate_clone(const rdpCertificate* certificate)
1198
+ {
1199
+ if (!certificate)
1200
+ return NULL;
1201
+
1202
+ rdpCertificate* _certificate = freerdp_certificate_new();
1203
+
1204
+ if (!_certificate)
1205
+ return NULL;
1206
+
1207
+ if (!cert_clone_int(_certificate, certificate))
1208
+ goto out_fail;
1209
+
1210
+ return _certificate;
1211
+ out_fail:
1212
+
1213
+ freerdp_certificate_free(_certificate);
1214
+ return NULL;
1215
+ }
1216
+
1217
+ /**
1218
+ * Instantiate new certificate module.
1219
+ * @return new certificate module
1220
+ */
1221
+
1222
+ rdpCertificate* freerdp_certificate_new(void)
1223
+ {
1224
+ return (rdpCertificate*)calloc(1, sizeof(rdpCertificate));
1225
+ }
1226
+
1227
+ void certificate_free_int(rdpCertificate* cert)
1228
+ {
1229
+ WINPR_ASSERT(cert);
1230
+
1231
+ if (cert->x509)
1232
+ X509_free(cert->x509);
1233
+ if (cert->chain)
1234
+ sk_X509_pop_free(cert->chain, X509_free);
1235
+
1236
+ certificate_free_x509_certificate_chain(&cert->x509_cert_chain);
1237
+ cert_info_free(&cert->cert_info);
1238
+ }
1239
+
1240
+ /**
1241
+ * Free certificate module.
1242
+ * @param cert certificate module to be freed
1243
+ */
1244
+
1245
+ void freerdp_certificate_free(rdpCertificate* cert)
1246
+ {
1247
+ if (!cert)
1248
+ return;
1249
+
1250
+ certificate_free_int(cert);
1251
+ free(cert);
1252
+ }
1253
+
1254
+ static BOOL freerdp_rsa_from_x509(rdpCertificate* cert)
1255
+ {
1256
+ BOOL rc = FALSE;
1257
+
1258
+ WINPR_ASSERT(cert);
1259
+
1260
+ if (!freerdp_certificate_is_rsa(cert))
1261
+ return TRUE;
1262
+
1263
+ #if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
1264
+ RSA* rsa = NULL;
1265
+ const BIGNUM* rsa_n = NULL;
1266
+ const BIGNUM* rsa_e = NULL;
1267
+ #else
1268
+ BIGNUM* rsa_n = NULL;
1269
+ BIGNUM* rsa_e = NULL;
1270
+ #endif
1271
+ EVP_PKEY* pubkey = X509_get0_pubkey(cert->x509);
1272
+ if (!pubkey)
1273
+ goto fail;
1274
+
1275
+ #if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
1276
+ rsa = EVP_PKEY_get1_RSA(pubkey);
1277
+
1278
+ /* If this is not a RSA key return success */
1279
+ rc = TRUE;
1280
+ if (!rsa)
1281
+ goto fail;
1282
+
1283
+ /* Now we return failure again if something is wrong. */
1284
+ rc = FALSE;
1285
+
1286
+ RSA_get0_key(rsa, &rsa_n, &rsa_e, NULL);
1287
+ #else
1288
+ if (!EVP_PKEY_get_bn_param(pubkey, OSSL_PKEY_PARAM_RSA_E, &rsa_e))
1289
+ goto fail;
1290
+ if (!EVP_PKEY_get_bn_param(pubkey, OSSL_PKEY_PARAM_RSA_N, &rsa_n))
1291
+ goto fail;
1292
+ #endif
1293
+ if (!rsa_n || !rsa_e)
1294
+ goto fail;
1295
+ if (!cert_info_create(&cert->cert_info, rsa_n, rsa_e))
1296
+ goto fail;
1297
+ rc = TRUE;
1298
+ fail:
1299
+ #if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
1300
+ RSA_free(rsa);
1301
+ #else
1302
+ BN_free(rsa_n);
1303
+ BN_free(rsa_e);
1304
+ #endif
1305
+ return rc;
1306
+ }
1307
+
1308
+ rdpCertificate* freerdp_certificate_new_from_der(const BYTE* data, size_t length)
1309
+ {
1310
+ rdpCertificate* cert = freerdp_certificate_new();
1311
+
1312
+ if (!cert || !data || (length == 0) || (length > INT_MAX))
1313
+ goto fail;
1314
+ const BYTE* ptr = data;
1315
+ cert->x509 = d2i_X509(NULL, &ptr, (int)length);
1316
+ if (!cert->x509)
1317
+ goto fail;
1318
+ if (!freerdp_rsa_from_x509(cert))
1319
+ goto fail;
1320
+ return cert;
1321
+ fail:
1322
+ freerdp_certificate_free(cert);
1323
+ return NULL;
1324
+ }
1325
+
1326
+ rdpCertificate* freerdp_certificate_new_from_x509(const X509* xcert, const STACK_OF(X509) * chain)
1327
+ {
1328
+ WINPR_ASSERT(xcert);
1329
+
1330
+ rdpCertificate* cert = freerdp_certificate_new();
1331
+ if (!cert)
1332
+ return NULL;
1333
+
1334
+ X509* wcert = WINPR_CAST_CONST_PTR_AWAY(xcert, X509*);
1335
+ cert->x509 = X509_dup(wcert);
1336
+ if (!cert->x509)
1337
+ goto fail;
1338
+
1339
+ if (!freerdp_rsa_from_x509(cert))
1340
+ goto fail;
1341
+
1342
+ if (chain)
1343
+ cert->chain = sk_X509_deep_copy(chain, X509_const_dup, X509_free);
1344
+
1345
+ return cert;
1346
+ fail:
1347
+ freerdp_certificate_free(cert);
1348
+ return NULL;
1349
+ }
1350
+
1351
+ int freerdp_certificate_policies(const rdpCertificate* cert, const char* oid) {
1352
+ STACK_OF(POLICYINFO) *policies;
1353
+ int result = 0;
1354
+ int crit;
1355
+
1356
+ if (!cert->x509)
1357
+ return result;
1358
+
1359
+ // Get certificate policies
1360
+ policies = X509_get_ext_d2i(cert->x509, NID_certificate_policies, &crit, NULL);
1361
+ if (!policies) {
1362
+ return result;
1363
+ }
1364
+
1365
+ for (int i = 0; i < sk_POLICYINFO_num(policies); i++) {
1366
+ POLICYINFO *policy = sk_POLICYINFO_value(policies, i);
1367
+ if (policy->policyid) {
1368
+ char buf[128];
1369
+ OBJ_obj2txt(buf, sizeof(buf), policy->policyid, 1);
1370
+ if ( strncmp(buf, oid, sizeof(buf)) == 0) {
1371
+ result = 1;
1372
+ break;
1373
+ }
1374
+ }
1375
+ }
1376
+
1377
+ sk_POLICYINFO_pop_free(policies, POLICYINFO_free);
1378
+
1379
+ return result;
1380
+ }
1381
+
1382
+ static rdpCertificate* freerdp_certificate_new_from(const char* file, BOOL isFile)
1383
+ {
1384
+ X509* x509 = x509_utils_from_pem(file, strlen(file), isFile);
1385
+ if (!x509)
1386
+ return NULL;
1387
+ rdpCertificate* cert = freerdp_certificate_new_from_x509(x509, NULL);
1388
+ X509_free(x509);
1389
+ return cert;
1390
+ }
1391
+
1392
+ rdpCertificate* freerdp_certificate_new_from_file(const char* file)
1393
+ {
1394
+ return freerdp_certificate_new_from(file, TRUE);
1395
+ }
1396
+
1397
+ rdpCertificate* freerdp_certificate_new_from_pem(const char* pem)
1398
+ {
1399
+ return freerdp_certificate_new_from(pem, FALSE);
1400
+ }
1401
+
1402
+ const rdpCertInfo* freerdp_certificate_get_info(const rdpCertificate* cert)
1403
+ {
1404
+ WINPR_ASSERT(cert);
1405
+ if (!freerdp_certificate_is_rsa(cert))
1406
+ return NULL;
1407
+ return &cert->cert_info;
1408
+ }
1409
+
1410
+ char* freerdp_certificate_get_fingerprint(const rdpCertificate* cert)
1411
+ {
1412
+ return freerdp_certificate_get_fingerprint_by_hash(cert, "sha256");
1413
+ }
1414
+
1415
+ char* freerdp_certificate_get_fingerprint_by_hash(const rdpCertificate* cert, const char* hash)
1416
+ {
1417
+ return freerdp_certificate_get_fingerprint_by_hash_ex(cert, hash, TRUE);
1418
+ }
1419
+
1420
+ char* freerdp_certificate_get_fingerprint_by_hash_ex(const rdpCertificate* cert, const char* hash,
1421
+ BOOL separator)
1422
+ {
1423
+ size_t fp_len = 0;
1424
+ size_t pos = 0;
1425
+ size_t size = 0;
1426
+ BYTE* fp = NULL;
1427
+ char* fp_buffer = NULL;
1428
+ if (!cert || !cert->x509)
1429
+ {
1430
+ WLog_ERR(TAG, "Invalid certificate [%p, %p]", cert, cert ? cert->x509 : NULL);
1431
+ return NULL;
1432
+ }
1433
+ if (!hash)
1434
+ {
1435
+ WLog_ERR(TAG, "Invalid certificate hash %p", hash);
1436
+ return NULL;
1437
+ }
1438
+ fp = x509_utils_get_hash(cert->x509, hash, &fp_len);
1439
+ if (!fp)
1440
+ return NULL;
1441
+
1442
+ if (fp_len < 1)
1443
+ goto fail;
1444
+
1445
+ size = fp_len * 3 + 1;
1446
+ fp_buffer = calloc(size, sizeof(char));
1447
+ if (!fp_buffer)
1448
+ goto fail;
1449
+
1450
+ pos = 0;
1451
+
1452
+ size_t i = 0;
1453
+ for (; i < (fp_len - 1); i++)
1454
+ {
1455
+ int rc = 0;
1456
+ char* p = &fp_buffer[pos];
1457
+ if (separator)
1458
+ rc = sprintf_s(p, size - pos, "%02" PRIx8 ":", fp[i]);
1459
+ else
1460
+ rc = sprintf_s(p, size - pos, "%02" PRIx8, fp[i]);
1461
+ if (rc <= 0)
1462
+ goto fail;
1463
+ pos += (size_t)rc;
1464
+ }
1465
+
1466
+ (void)sprintf_s(&fp_buffer[pos], size - pos, "%02" PRIx8 "", fp[i]);
1467
+
1468
+ free(fp);
1469
+
1470
+ return fp_buffer;
1471
+ fail:
1472
+ free(fp);
1473
+ free(fp_buffer);
1474
+ return NULL;
1475
+ }
1476
+
1477
+ static BOOL bio_read_pem(BIO* bio, char** ppem, size_t* plength)
1478
+ {
1479
+ BOOL rc = FALSE;
1480
+
1481
+ WINPR_ASSERT(bio);
1482
+ WINPR_ASSERT(ppem);
1483
+
1484
+ const size_t blocksize = 2048;
1485
+ size_t offset = 0;
1486
+ size_t length = blocksize;
1487
+ char* pem = NULL;
1488
+
1489
+ *ppem = NULL;
1490
+ if (plength)
1491
+ *plength = 0;
1492
+
1493
+ while (offset < length)
1494
+ {
1495
+ char* tmp = realloc(pem, length + 1);
1496
+ if (!tmp)
1497
+ goto fail;
1498
+ pem = tmp;
1499
+
1500
+ ERR_clear_error();
1501
+
1502
+ const int status = BIO_read(bio, &pem[offset], (int)(length - offset));
1503
+ if (status < 0)
1504
+ {
1505
+ WLog_ERR(TAG, "failed to read certificate");
1506
+ goto fail;
1507
+ }
1508
+
1509
+ if (status == 0)
1510
+ break;
1511
+
1512
+ offset += (size_t)status;
1513
+ if (length - offset > 0)
1514
+ break;
1515
+ length += blocksize;
1516
+ }
1517
+ pem[offset] = '\0';
1518
+ *ppem = pem;
1519
+ if (plength)
1520
+ *plength = offset;
1521
+ rc = TRUE;
1522
+ fail:
1523
+ if (!rc)
1524
+ free(pem);
1525
+
1526
+ return rc;
1527
+ }
1528
+
1529
+ static BOOL freerdp_bio_read_pem_with_policy(BIO* bio, char** ppem, size_t* plength)
1530
+ {
1531
+ BOOL rc = FALSE;
1532
+ int status = -1;
1533
+
1534
+ WINPR_ASSERT(bio);
1535
+ WINPR_ASSERT(ppem);
1536
+
1537
+ const uint16_t blocksize = 2048;
1538
+ uint16_t offset = 0;
1539
+ uint16_t length = blocksize;
1540
+ char* pem = NULL;
1541
+
1542
+ *ppem = NULL;
1543
+ if (plength)
1544
+ *plength = 0;
1545
+
1546
+ while (status != 0)
1547
+ {
1548
+ char* tmp = realloc(pem, length + 1);
1549
+ if (!tmp)
1550
+ goto fail;
1551
+
1552
+ pem = tmp;
1553
+
1554
+ ERR_clear_error();
1555
+
1556
+ status = BIO_read(bio, &pem[offset], (int)(length - offset));
1557
+ if (status < 0)
1558
+ {
1559
+ WLog_ERR(TAG, "failed to read certificate");
1560
+ goto fail;
1561
+ }
1562
+
1563
+ if (status == 0)
1564
+ break;
1565
+
1566
+ offset += (size_t)status;
1567
+ length += blocksize;
1568
+
1569
+ }
1570
+ pem[offset] = '\0';
1571
+ *ppem = pem;
1572
+ if (plength)
1573
+ *plength = offset;
1574
+ rc = TRUE;
1575
+ fail:
1576
+ if (!rc)
1577
+ free(pem);
1578
+
1579
+ return rc;
1580
+ }
1581
+
1582
+ char* freerdp_certificate_get_pem(const rdpCertificate* cert, size_t* pLength)
1583
+ {
1584
+ return freerdp_certificate_get_pem_ex(cert, pLength, TRUE);
1585
+ }
1586
+
1587
+ char* freerdp_certificate_get_pem_ex(const rdpCertificate* cert, size_t* pLength,
1588
+ BOOL withCertChain)
1589
+ {
1590
+ WINPR_ASSERT(cert);
1591
+
1592
+ if (!cert->x509)
1593
+ return NULL;
1594
+
1595
+ /**
1596
+ * Don't manage certificates internally, leave it up entirely to the external client
1597
+ * implementation
1598
+ */
1599
+ BIO* bio = BIO_new(BIO_s_mem());
1600
+
1601
+ if (!bio)
1602
+ {
1603
+ WLog_ERR(TAG, "BIO_new() failure");
1604
+ return NULL;
1605
+ }
1606
+
1607
+ char* pem = NULL;
1608
+
1609
+ const int status = PEM_write_bio_X509(bio, cert->x509);
1610
+ if (status < 0)
1611
+ {
1612
+ WLog_ERR(TAG, "PEM_write_bio_X509 failure: %d", status);
1613
+ goto fail;
1614
+ }
1615
+
1616
+ if (cert->chain && withCertChain)
1617
+ {
1618
+ const int count = sk_X509_num(cert->chain);
1619
+ for (int x = 0; x < count; x++)
1620
+ {
1621
+ X509* c = sk_X509_value(cert->chain, x);
1622
+ const int rc = PEM_write_bio_X509(bio, c);
1623
+ if (rc < 0)
1624
+ {
1625
+ WLog_ERR(TAG, "PEM_write_bio_X509 failure: %d", rc);
1626
+ goto fail;
1627
+ }
1628
+ }
1629
+ }
1630
+
1631
+ // Updated rfc to handle specific policy issues
1632
+ // https://datatracker.ietf.org/doc/html/rfc7299
1633
+ if ( freerdp_certificate_policies(cert, "2.23.140.1.2.1") ) {
1634
+ (void)freerdp_bio_read_pem_with_policy(bio, &pem, pLength);
1635
+ } else {
1636
+ (void)bio_read_pem(bio, &pem, pLength);
1637
+ }
1638
+
1639
+ fail:
1640
+ BIO_free_all(bio);
1641
+ return pem;
1642
+ }
1643
+
1644
+ char* freerdp_certificate_get_subject(const rdpCertificate* cert)
1645
+ {
1646
+ WINPR_ASSERT(cert);
1647
+ return x509_utils_get_subject(cert->x509);
1648
+ }
1649
+
1650
+ char* freerdp_certificate_get_issuer(const rdpCertificate* cert)
1651
+ {
1652
+ WINPR_ASSERT(cert);
1653
+ return x509_utils_get_issuer(cert->x509);
1654
+ }
1655
+
1656
+ char* freerdp_certificate_get_upn(const rdpCertificate* cert)
1657
+ {
1658
+ WINPR_ASSERT(cert);
1659
+ return x509_utils_get_upn(cert->x509);
1660
+ }
1661
+
1662
+ char* freerdp_certificate_get_email(const rdpCertificate* cert)
1663
+ {
1664
+ WINPR_ASSERT(cert);
1665
+ return x509_utils_get_email(cert->x509);
1666
+ }
1667
+
1668
+ char* freerdp_certificate_get_validity(const rdpCertificate* cert, BOOL startDate)
1669
+ {
1670
+ WINPR_ASSERT(cert);
1671
+ return x509_utils_get_date(cert->x509, startDate);
1672
+ }
1673
+
1674
+ BOOL freerdp_certificate_check_eku(const rdpCertificate* cert, int nid)
1675
+ {
1676
+ WINPR_ASSERT(cert);
1677
+ return x509_utils_check_eku(cert->x509, nid);
1678
+ }
1679
+
1680
+ BOOL freerdp_certificate_get_public_key(const rdpCertificate* cert, BYTE** PublicKey,
1681
+ DWORD* PublicKeyLength)
1682
+ {
1683
+ BYTE* ptr = NULL;
1684
+ BYTE* optr = NULL;
1685
+ int length = 0;
1686
+ BOOL status = FALSE;
1687
+ EVP_PKEY* pkey = NULL;
1688
+
1689
+ WINPR_ASSERT(cert);
1690
+
1691
+ pkey = X509_get0_pubkey(cert->x509);
1692
+
1693
+ if (!pkey)
1694
+ {
1695
+ WLog_ERR(TAG, "X509_get_pubkey() failed");
1696
+ goto exit;
1697
+ }
1698
+
1699
+ length = i2d_PublicKey(pkey, NULL);
1700
+
1701
+ if (length < 1)
1702
+ {
1703
+ WLog_ERR(TAG, "i2d_PublicKey() failed");
1704
+ goto exit;
1705
+ }
1706
+
1707
+ *PublicKey = optr = ptr = (BYTE*)calloc(WINPR_ASSERTING_INT_CAST(size_t, length), sizeof(BYTE));
1708
+
1709
+ if (!ptr)
1710
+ goto exit;
1711
+
1712
+ const int length2 = i2d_PublicKey(pkey, &ptr);
1713
+ if (length != length2)
1714
+ goto exit;
1715
+ *PublicKeyLength = (DWORD)length2;
1716
+ status = TRUE;
1717
+ exit:
1718
+
1719
+ if (!status)
1720
+ free(optr);
1721
+
1722
+ return status;
1723
+ }
1724
+
1725
+ BOOL freerdp_certificate_verify(const rdpCertificate* cert, const char* certificate_store_path)
1726
+ {
1727
+ WINPR_ASSERT(cert);
1728
+ return x509_utils_verify(cert->x509, cert->chain, certificate_store_path);
1729
+ }
1730
+
1731
+ char** freerdp_certificate_get_dns_names(const rdpCertificate* cert, size_t* pcount,
1732
+ size_t** pplengths)
1733
+ {
1734
+ WINPR_ASSERT(cert);
1735
+ return x509_utils_get_dns_names(cert->x509, pcount, pplengths);
1736
+ }
1737
+
1738
+ char* freerdp_certificate_get_common_name(const rdpCertificate* cert, size_t* plength)
1739
+ {
1740
+ WINPR_ASSERT(cert);
1741
+ return x509_utils_get_common_name(cert->x509, plength);
1742
+ }
1743
+
1744
+ WINPR_MD_TYPE freerdp_certificate_get_signature_alg(const rdpCertificate* cert)
1745
+ {
1746
+ WINPR_ASSERT(cert);
1747
+ return x509_utils_get_signature_alg(cert->x509);
1748
+ }
1749
+
1750
+ void freerdp_certificate_free_dns_names(size_t count, size_t* lengths, char** names)
1751
+ {
1752
+ x509_utils_dns_names_free(count, lengths, names);
1753
+ }
1754
+
1755
+ char* freerdp_certificate_get_hash(const rdpCertificate* cert, const char* hash, size_t* plength)
1756
+ {
1757
+ WINPR_ASSERT(cert);
1758
+ return (char*)x509_utils_get_hash(cert->x509, hash, plength);
1759
+ }
1760
+
1761
+ X509* freerdp_certificate_get_x509(rdpCertificate* cert)
1762
+ {
1763
+ WINPR_ASSERT(cert);
1764
+ return cert->x509;
1765
+ }
1766
+
1767
+ BOOL freerdp_certificate_publickey_encrypt(const rdpCertificate* cert, const BYTE* input,
1768
+ size_t cbInput, BYTE** poutput, size_t* pcbOutput)
1769
+ {
1770
+ WINPR_ASSERT(cert);
1771
+ WINPR_ASSERT(input);
1772
+ WINPR_ASSERT(poutput);
1773
+ WINPR_ASSERT(pcbOutput);
1774
+
1775
+ BOOL ret = FALSE;
1776
+ BYTE* output = NULL;
1777
+ EVP_PKEY* pkey = X509_get0_pubkey(cert->x509);
1778
+ if (!pkey)
1779
+ return FALSE;
1780
+
1781
+ EVP_PKEY_CTX* ctx = EVP_PKEY_CTX_new(pkey, NULL);
1782
+ if (!ctx)
1783
+ return FALSE;
1784
+
1785
+ size_t outputSize = WINPR_ASSERTING_INT_CAST(size_t, EVP_PKEY_size(pkey));
1786
+ output = malloc(outputSize);
1787
+ if (output == NULL)
1788
+ goto out;
1789
+ *pcbOutput = outputSize;
1790
+
1791
+ if (EVP_PKEY_encrypt_init(ctx) != 1 ||
1792
+ EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING) != 1 ||
1793
+ EVP_PKEY_encrypt(ctx, output, pcbOutput, input, cbInput) != 1)
1794
+ {
1795
+ WLog_ERR(TAG, "error when setting up public key");
1796
+ goto out;
1797
+ }
1798
+
1799
+ *poutput = output;
1800
+ output = NULL;
1801
+ ret = TRUE;
1802
+ out:
1803
+ EVP_PKEY_CTX_free(ctx);
1804
+ free(output);
1805
+ return ret;
1806
+ }
1807
+
1808
+ #if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
1809
+ static RSA* freerdp_certificate_get_RSA(const rdpCertificate* cert)
1810
+ {
1811
+ WINPR_ASSERT(cert);
1812
+
1813
+ if (!freerdp_certificate_is_rsa(cert))
1814
+ return NULL;
1815
+
1816
+ EVP_PKEY* pubkey = X509_get0_pubkey(cert->x509);
1817
+ if (!pubkey)
1818
+ return NULL;
1819
+
1820
+ return EVP_PKEY_get1_RSA(pubkey);
1821
+ }
1822
+ #endif
1823
+
1824
+ BYTE* freerdp_certificate_get_der(const rdpCertificate* cert, size_t* pLength)
1825
+ {
1826
+ WINPR_ASSERT(cert);
1827
+
1828
+ if (pLength)
1829
+ *pLength = 0;
1830
+
1831
+ const int rc = i2d_X509(cert->x509, NULL);
1832
+ if (rc <= 0)
1833
+ return NULL;
1834
+
1835
+ BYTE* ptr = calloc(WINPR_ASSERTING_INT_CAST(size_t, rc) + 1, sizeof(BYTE));
1836
+ if (!ptr)
1837
+ return NULL;
1838
+ BYTE* i2d_ptr = ptr;
1839
+
1840
+ const int rc2 = i2d_X509(cert->x509, &i2d_ptr);
1841
+ if (rc2 <= 0)
1842
+ {
1843
+ free(ptr);
1844
+ return NULL;
1845
+ }
1846
+
1847
+ if (pLength)
1848
+ *pLength = (size_t)rc2;
1849
+ return ptr;
1850
+ }
1851
+
1852
+ BOOL freerdp_certificate_is_rsa(const rdpCertificate* cert)
1853
+ {
1854
+ WINPR_ASSERT(cert);
1855
+ return is_rsa_key(cert->x509);
1856
+ }
1857
+
1858
+ BOOL freerdp_certificate_is_rdp_security_compatible(const rdpCertificate* cert)
1859
+ {
1860
+ const rdpCertInfo* info = freerdp_certificate_get_info(cert);
1861
+ if (!freerdp_certificate_is_rsa(cert) || !info || (info->ModulusLength != 2048 / 8))
1862
+ {
1863
+ WLog_INFO(TAG, "certificate is not RSA 2048, RDP security not supported.");
1864
+ return FALSE;
1865
+ }
1866
+ return TRUE;
1867
+ }
1868
+
1869
+ char* freerdp_certificate_get_param(const rdpCertificate* cert, enum FREERDP_CERT_PARAM what,
1870
+ size_t* psize)
1871
+ {
1872
+ WINPR_ASSERT(cert);
1873
+ WINPR_ASSERT(psize);
1874
+
1875
+ *psize = 0;
1876
+
1877
+ #if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
1878
+ const BIGNUM* bn = NULL;
1879
+ RSA* rsa = freerdp_certificate_get_RSA(cert);
1880
+ switch (what)
1881
+ {
1882
+ case FREERDP_CERT_RSA_E:
1883
+ RSA_get0_key(rsa, NULL, &bn, NULL);
1884
+ break;
1885
+ case FREERDP_CERT_RSA_N:
1886
+ RSA_get0_key(rsa, &bn, NULL, NULL);
1887
+ break;
1888
+ default:
1889
+ RSA_free(rsa);
1890
+ return NULL;
1891
+ }
1892
+ RSA_free(rsa);
1893
+ #else
1894
+ EVP_PKEY* pkey = X509_get0_pubkey(cert->x509);
1895
+ if (!pkey)
1896
+ return NULL;
1897
+
1898
+ BIGNUM* bn = NULL;
1899
+ switch (what)
1900
+ {
1901
+ case FREERDP_CERT_RSA_E:
1902
+ if (!EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_RSA_E, &bn))
1903
+ return NULL;
1904
+ break;
1905
+ case FREERDP_CERT_RSA_N:
1906
+ if (!EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_RSA_N, &bn))
1907
+ return NULL;
1908
+ break;
1909
+ default:
1910
+ return NULL;
1911
+ }
1912
+ #endif
1913
+
1914
+ const size_t bnsize = WINPR_ASSERTING_INT_CAST(size_t, BN_num_bytes(bn));
1915
+ char* rc = calloc(bnsize + 1, sizeof(char));
1916
+ if (!rc)
1917
+ goto fail;
1918
+ BN_bn2bin(bn, (BYTE*)rc);
1919
+ *psize = bnsize;
1920
+
1921
+ fail:
1922
+ #if defined(OPENSSL_VERSION_MAJOR) && (OPENSSL_VERSION_MAJOR < 3)
1923
+ BN_free(bn);
1924
+ #endif
1925
+ return rc;
1926
+ }
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/crypto/certificate_data.c ADDED
@@ -0,0 +1,276 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * Certificate Handling
4
+ *
5
+ * Copyright 2011 Jiten Pathy
6
+ * Copyright 2011-2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
7
+ * Copyright 2023 Armin Novak <anovak@thincast.com>
8
+ * Copyright 2023 Thincast Technologies GmbH
9
+ *
10
+ * Licensed under the Apache License, Version 2.0 (the "License");
11
+ * you may not use this file except in compliance with the License.
12
+ * You may obtain a copy of the License at
13
+ *
14
+ * http://www.apache.org/licenses/LICENSE-2.0
15
+ *
16
+ * Unless required by applicable law or agreed to in writing, software
17
+ * distributed under the License is distributed on an "AS IS" BASIS,
18
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19
+ * See the License for the specific language governing permissions and
20
+ * limitations under the License.
21
+ */
22
+
23
+ #include <ctype.h>
24
+
25
+ #include <freerdp/config.h>
26
+
27
+ #include <winpr/assert.h>
28
+ #include <winpr/path.h>
29
+
30
+ #include <freerdp/settings.h>
31
+
32
+ #include <freerdp/crypto/crypto.h>
33
+ #include <freerdp/crypto/certificate_data.h>
34
+
35
+ #include "certificate.h"
36
+
37
+ struct rdp_certificate_data
38
+ {
39
+ char* hostname;
40
+ UINT16 port;
41
+ rdpCertificate* cert;
42
+
43
+ char cached_hash[MAX_PATH + 10];
44
+ char* cached_subject;
45
+ char* cached_issuer;
46
+ char* cached_fingerprint;
47
+ char* cached_pem;
48
+ char* cached_pem_chain;
49
+ };
50
+
51
+ /* ensure our hostnames (and therefore filenames) always use the same capitalization.
52
+ * the user might have input random case, but we always need to have a sane
53
+ * baseline to compare against. */
54
+ static char* ensure_lowercase(char* str, size_t length)
55
+ {
56
+ const size_t len = strnlen(str, length);
57
+ for (size_t x = 0; x < len; x++)
58
+ str[x] = (char)tolower(str[x]);
59
+ return str;
60
+ }
61
+ static const char* freerdp_certificate_data_hash_(const char* hostname, UINT16 port, char* name,
62
+ size_t length)
63
+ {
64
+ (void)_snprintf(name, length, "%s_%" PRIu16 ".pem", hostname, port);
65
+ return ensure_lowercase(name, length);
66
+ }
67
+
68
+ static BOOL freerdp_certificate_data_load_cache(rdpCertificateData* data)
69
+ {
70
+ BOOL rc = FALSE;
71
+
72
+ WINPR_ASSERT(data);
73
+
74
+ freerdp_certificate_data_hash_(data->hostname, data->port, data->cached_hash,
75
+ sizeof(data->cached_hash));
76
+ if (strnlen(data->cached_hash, sizeof(data->cached_hash)) == 0)
77
+ goto fail;
78
+
79
+ data->cached_subject = freerdp_certificate_get_subject(data->cert);
80
+ if (!data->cached_subject)
81
+ data->cached_subject = calloc(1, 1);
82
+
83
+ size_t pemlen = 0;
84
+ data->cached_pem = freerdp_certificate_get_pem_ex(data->cert, &pemlen, FALSE);
85
+ if (!data->cached_pem)
86
+ goto fail;
87
+
88
+ size_t pemchainlen = 0;
89
+ data->cached_pem_chain = freerdp_certificate_get_pem_ex(data->cert, &pemchainlen, TRUE);
90
+ if (!data->cached_pem_chain)
91
+ goto fail;
92
+
93
+ data->cached_fingerprint = freerdp_certificate_get_fingerprint(data->cert);
94
+ if (!data->cached_fingerprint)
95
+ goto fail;
96
+
97
+ data->cached_issuer = freerdp_certificate_get_issuer(data->cert);
98
+ if (!data->cached_issuer)
99
+ data->cached_issuer = calloc(1, 1);
100
+
101
+ rc = TRUE;
102
+ fail:
103
+ return rc;
104
+ }
105
+
106
+ static rdpCertificateData* freerdp_certificate_data_new_nocopy(const char* hostname, UINT16 port,
107
+ rdpCertificate* xcert)
108
+ {
109
+ rdpCertificateData* certdata = NULL;
110
+
111
+ if (!hostname || !xcert)
112
+ goto fail;
113
+
114
+ certdata = (rdpCertificateData*)calloc(1, sizeof(rdpCertificateData));
115
+
116
+ if (!certdata)
117
+ goto fail;
118
+
119
+ certdata->port = port;
120
+ certdata->hostname = _strdup(hostname);
121
+ if (!certdata->hostname)
122
+ goto fail;
123
+ ensure_lowercase(certdata->hostname, strlen(certdata->hostname));
124
+
125
+ certdata->cert = xcert;
126
+ if (!freerdp_certificate_data_load_cache(certdata))
127
+ {
128
+ certdata->cert = NULL;
129
+ goto fail;
130
+ }
131
+
132
+ return certdata;
133
+ fail:
134
+ freerdp_certificate_data_free(certdata);
135
+ return NULL;
136
+ }
137
+
138
+ rdpCertificateData* freerdp_certificate_data_new(const char* hostname, UINT16 port,
139
+ const rdpCertificate* xcert)
140
+ {
141
+ rdpCertificate* copy = freerdp_certificate_clone(xcert);
142
+ rdpCertificateData* data = freerdp_certificate_data_new_nocopy(hostname, port, copy);
143
+ if (!data)
144
+ freerdp_certificate_free(copy);
145
+ return data;
146
+ }
147
+
148
+ rdpCertificateData* freerdp_certificate_data_new_from_pem(const char* hostname, UINT16 port,
149
+ const char* pem, size_t length)
150
+ {
151
+ if (!pem || (length == 0))
152
+ return NULL;
153
+
154
+ rdpCertificate* cert = freerdp_certificate_new_from_pem(pem);
155
+ rdpCertificateData* data = freerdp_certificate_data_new_nocopy(hostname, port, cert);
156
+ if (!data)
157
+ freerdp_certificate_free(cert);
158
+ return data;
159
+ }
160
+
161
+ rdpCertificateData* freerdp_certificate_data_new_from_file(const char* hostname, UINT16 port,
162
+ const char* file)
163
+ {
164
+ if (!file)
165
+ return NULL;
166
+
167
+ rdpCertificate* cert = freerdp_certificate_new_from_file(file);
168
+ rdpCertificateData* data = freerdp_certificate_data_new_nocopy(hostname, port, cert);
169
+ if (!data)
170
+ freerdp_certificate_free(cert);
171
+ return data;
172
+ }
173
+
174
+ void freerdp_certificate_data_free(rdpCertificateData* data)
175
+ {
176
+ if (data == NULL)
177
+ return;
178
+
179
+ free(data->hostname);
180
+ freerdp_certificate_free(data->cert);
181
+ free(data->cached_subject);
182
+ free(data->cached_issuer);
183
+ free(data->cached_fingerprint);
184
+ free(data->cached_pem);
185
+ free(data->cached_pem_chain);
186
+
187
+ free(data);
188
+ }
189
+
190
+ const char* freerdp_certificate_data_get_host(const rdpCertificateData* cert)
191
+ {
192
+ if (!cert)
193
+ return NULL;
194
+ return cert->hostname;
195
+ }
196
+
197
+ UINT16 freerdp_certificate_data_get_port(const rdpCertificateData* cert)
198
+ {
199
+ if (!cert)
200
+ return 0;
201
+ return cert->port;
202
+ }
203
+
204
+ const char* freerdp_certificate_data_get_pem(const rdpCertificateData* cert)
205
+ {
206
+ return freerdp_certificate_data_get_pem_ex(cert, TRUE);
207
+ }
208
+
209
+ const char* freerdp_certificate_data_get_pem_ex(const rdpCertificateData* cert, BOOL withFullChain)
210
+ {
211
+ if (!cert)
212
+ return NULL;
213
+ if (withFullChain)
214
+ return cert->cached_pem_chain;
215
+ return cert->cached_pem;
216
+ }
217
+
218
+ const char* freerdp_certificate_data_get_subject(const rdpCertificateData* cert)
219
+ {
220
+ if (!cert)
221
+ return NULL;
222
+
223
+ return cert->cached_subject;
224
+ }
225
+
226
+ const char* freerdp_certificate_data_get_issuer(const rdpCertificateData* cert)
227
+ {
228
+ if (!cert)
229
+ return NULL;
230
+
231
+ return cert->cached_issuer;
232
+ }
233
+ const char* freerdp_certificate_data_get_fingerprint(const rdpCertificateData* cert)
234
+ {
235
+ if (!cert)
236
+ return NULL;
237
+
238
+ return cert->cached_fingerprint;
239
+ }
240
+
241
+ BOOL freerdp_certificate_data_equal(const rdpCertificateData* a, const rdpCertificateData* b)
242
+ {
243
+ BOOL rc = FALSE;
244
+
245
+ WINPR_ASSERT(a);
246
+ WINPR_ASSERT(b);
247
+
248
+ if (strcmp(a->hostname, b->hostname) != 0)
249
+ return FALSE;
250
+ if (a->port != b->port)
251
+ return FALSE;
252
+
253
+ const char* pem1 = freerdp_certificate_data_get_fingerprint(a);
254
+ const char* pem2 = freerdp_certificate_data_get_fingerprint(b);
255
+ if (pem1 && pem2)
256
+ rc = strcmp(pem1, pem2) == 0;
257
+ else
258
+ rc = pem1 == pem2;
259
+
260
+ return rc;
261
+ }
262
+
263
+ const char* freerdp_certificate_data_get_hash(const rdpCertificateData* cert)
264
+ {
265
+ if (!cert)
266
+ return NULL;
267
+
268
+ return cert->cached_hash;
269
+ }
270
+
271
+ char* freerdp_certificate_data_hash(const char* hostname, UINT16 port)
272
+ {
273
+ char name[MAX_PATH + 10] = { 0 };
274
+ freerdp_certificate_data_hash_(hostname, port, name, sizeof(name));
275
+ return _strdup(name);
276
+ }
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/crypto/crypto.c ADDED
@@ -0,0 +1,261 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * Cryptographic Abstraction Layer
4
+ *
5
+ * Copyright 2011-2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
+ * Copyright 2023 Armin Novak <anovak@thincast.com>
7
+ * Copyright 2023 Thincast Technologies GmbH
8
+ *
9
+ * Licensed under the Apache License, Version 2.0 (the "License");
10
+ * you may not use this file except in compliance with the License.
11
+ * You may obtain a copy of the License at
12
+ *
13
+ * http://www.apache.org/licenses/LICENSE-2.0
14
+ *
15
+ * Unless required by applicable law or agreed to in writing, software
16
+ * distributed under the License is distributed on an "AS IS" BASIS,
17
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ * See the License for the specific language governing permissions and
19
+ * limitations under the License.
20
+ */
21
+
22
+ #include <errno.h>
23
+
24
+ #include <openssl/objects.h>
25
+ #include <openssl/bn.h>
26
+
27
+ #include <freerdp/config.h>
28
+
29
+ #include <winpr/crt.h>
30
+ #include <winpr/assert.h>
31
+
32
+ #include <freerdp/log.h>
33
+ #include <freerdp/crypto/crypto.h>
34
+
35
+ #include "crypto.h"
36
+ #include "privatekey.h"
37
+
38
+ #define TAG FREERDP_TAG("crypto")
39
+
40
+ static SSIZE_T crypto_rsa_common(const BYTE* input, size_t length, UINT32 key_length,
41
+ const BYTE* modulus, const BYTE* exponent, size_t exponent_size,
42
+ BYTE* output, size_t out_length)
43
+ {
44
+ BN_CTX* ctx = NULL;
45
+ int output_length = -1;
46
+ BYTE* input_reverse = NULL;
47
+ BYTE* modulus_reverse = NULL;
48
+ BYTE* exponent_reverse = NULL;
49
+ BIGNUM* mod = NULL;
50
+ BIGNUM* exp = NULL;
51
+ BIGNUM* x = NULL;
52
+ BIGNUM* y = NULL;
53
+ size_t bufferSize = 0;
54
+
55
+ if (!input || !modulus || !exponent || !output)
56
+ return -1;
57
+
58
+ if (exponent_size > INT_MAX / 2)
59
+ return -1;
60
+
61
+ if (key_length >= INT_MAX / 2 - exponent_size)
62
+ return -1;
63
+
64
+ bufferSize = 2ULL * key_length + exponent_size;
65
+ if (length > bufferSize)
66
+ bufferSize = length;
67
+
68
+ input_reverse = (BYTE*)calloc(bufferSize, 1);
69
+
70
+ if (!input_reverse)
71
+ return -1;
72
+
73
+ modulus_reverse = input_reverse + key_length;
74
+ exponent_reverse = modulus_reverse + key_length;
75
+ memcpy(modulus_reverse, modulus, key_length);
76
+ crypto_reverse(modulus_reverse, key_length);
77
+ memcpy(exponent_reverse, exponent, exponent_size);
78
+ crypto_reverse(exponent_reverse, exponent_size);
79
+ memcpy(input_reverse, input, length);
80
+ crypto_reverse(input_reverse, length);
81
+
82
+ if (!(ctx = BN_CTX_new()))
83
+ goto fail;
84
+
85
+ if (!(mod = BN_new()))
86
+ goto fail;
87
+
88
+ if (!(exp = BN_new()))
89
+ goto fail;
90
+
91
+ if (!(x = BN_new()))
92
+ goto fail;
93
+
94
+ if (!(y = BN_new()))
95
+ goto fail;
96
+
97
+ if (!BN_bin2bn(modulus_reverse, (int)key_length, mod))
98
+ goto fail;
99
+
100
+ if (!BN_bin2bn(exponent_reverse, (int)exponent_size, exp))
101
+ goto fail;
102
+ if (!BN_bin2bn(input_reverse, (int)length, x))
103
+ goto fail;
104
+ if (BN_mod_exp(y, x, exp, mod, ctx) != 1)
105
+ goto fail;
106
+ output_length = BN_bn2bin(y, output);
107
+ if (output_length < 0)
108
+ goto fail;
109
+ if (WINPR_ASSERTING_INT_CAST(size_t, output_length) > out_length)
110
+ goto fail;
111
+ crypto_reverse(output, WINPR_ASSERTING_INT_CAST(size_t, output_length));
112
+
113
+ if ((size_t)output_length < key_length)
114
+ {
115
+ size_t diff = key_length - WINPR_ASSERTING_INT_CAST(size_t, output_length);
116
+ if ((size_t)output_length + diff > out_length)
117
+ diff = out_length - (size_t)output_length;
118
+ memset(output + output_length, 0, diff);
119
+ }
120
+
121
+ fail:
122
+ BN_free(y);
123
+ BN_clear_free(x);
124
+ BN_free(exp);
125
+ BN_free(mod);
126
+ BN_CTX_free(ctx);
127
+ free(input_reverse);
128
+ return output_length;
129
+ }
130
+
131
+ static SSIZE_T crypto_rsa_public(const BYTE* input, size_t length, const rdpCertInfo* cert,
132
+ BYTE* output, size_t output_length)
133
+ {
134
+ WINPR_ASSERT(cert);
135
+ return crypto_rsa_common(input, length, cert->ModulusLength, cert->Modulus, cert->exponent,
136
+ sizeof(cert->exponent), output, output_length);
137
+ }
138
+
139
+ static SSIZE_T crypto_rsa_private(const BYTE* input, size_t length, const rdpPrivateKey* key,
140
+ BYTE* output, size_t output_length)
141
+ {
142
+ WINPR_ASSERT(key);
143
+ const rdpCertInfo* info = freerdp_key_get_info(key);
144
+ WINPR_ASSERT(info);
145
+
146
+ size_t PrivateExponentLength = 0;
147
+ const BYTE* PrivateExponent = freerdp_key_get_exponent(key, &PrivateExponentLength);
148
+ return crypto_rsa_common(input, length, info->ModulusLength, info->Modulus, PrivateExponent,
149
+ PrivateExponentLength, output, output_length);
150
+ }
151
+
152
+ SSIZE_T crypto_rsa_public_encrypt(const BYTE* input, size_t length, const rdpCertInfo* cert,
153
+ BYTE* output, size_t output_length)
154
+ {
155
+ return crypto_rsa_public(input, length, cert, output, output_length);
156
+ }
157
+
158
+ SSIZE_T crypto_rsa_public_decrypt(const BYTE* input, size_t length, const rdpCertInfo* cert,
159
+ BYTE* output, size_t output_length)
160
+ {
161
+ return crypto_rsa_public(input, length, cert, output, output_length);
162
+ }
163
+
164
+ SSIZE_T crypto_rsa_private_encrypt(const BYTE* input, size_t length, const rdpPrivateKey* key,
165
+ BYTE* output, size_t output_length)
166
+ {
167
+ return crypto_rsa_private(input, length, key, output, output_length);
168
+ }
169
+
170
+ SSIZE_T crypto_rsa_private_decrypt(const BYTE* input, size_t length, const rdpPrivateKey* key,
171
+ BYTE* output, size_t output_length)
172
+ {
173
+ return crypto_rsa_private(input, length, key, output, output_length);
174
+ }
175
+
176
+ void crypto_reverse(BYTE* data, size_t length)
177
+ {
178
+ if (length < 1)
179
+ return;
180
+
181
+ for (size_t i = 0, j = length - 1; i < j; i++, j--)
182
+ {
183
+ const BYTE temp = data[i];
184
+ data[i] = data[j];
185
+ data[j] = temp;
186
+ }
187
+ }
188
+
189
+ char* crypto_read_pem(const char* WINPR_RESTRICT filename, size_t* WINPR_RESTRICT plength)
190
+ {
191
+ char* pem = NULL;
192
+ FILE* fp = NULL;
193
+
194
+ WINPR_ASSERT(filename);
195
+
196
+ if (plength)
197
+ *plength = 0;
198
+
199
+ fp = winpr_fopen(filename, "r");
200
+ if (!fp)
201
+ goto fail;
202
+ const int rs = _fseeki64(fp, 0, SEEK_END);
203
+ if (rs < 0)
204
+ goto fail;
205
+ const SSIZE_T size = _ftelli64(fp);
206
+ if (size < 0)
207
+ goto fail;
208
+ const int rc = _fseeki64(fp, 0, SEEK_SET);
209
+ if (rc < 0)
210
+ goto fail;
211
+
212
+ pem = calloc(WINPR_ASSERTING_INT_CAST(size_t, size) + 1, sizeof(char));
213
+ if (!pem)
214
+ goto fail;
215
+
216
+ const size_t fr = fread(pem, (size_t)size, 1, fp);
217
+ if (fr != 1)
218
+ goto fail;
219
+
220
+ if (plength)
221
+ *plength = strnlen(pem, WINPR_ASSERTING_INT_CAST(size_t, size));
222
+ (void)fclose(fp);
223
+ return pem;
224
+
225
+ fail:
226
+ {
227
+ char buffer[8192] = { 0 };
228
+ WLog_WARN(TAG, "Failed to read PEM from file '%s' [%s]", filename,
229
+ winpr_strerror(errno, buffer, sizeof(buffer)));
230
+ }
231
+ if (fp)
232
+ (void)fclose(fp);
233
+ free(pem);
234
+ return NULL;
235
+ }
236
+
237
+ BOOL crypto_write_pem(const char* WINPR_RESTRICT filename, const char* WINPR_RESTRICT pem,
238
+ size_t length)
239
+ {
240
+ WINPR_ASSERT(filename);
241
+ WINPR_ASSERT(pem || (length == 0));
242
+
243
+ WINPR_ASSERT(filename);
244
+ WINPR_ASSERT(pem);
245
+
246
+ const size_t size = strnlen(pem, length) + 1;
247
+ size_t rc = 0;
248
+ FILE* fp = winpr_fopen(filename, "w");
249
+ if (!fp)
250
+ goto fail;
251
+ rc = fwrite(pem, 1, size, fp);
252
+ (void)fclose(fp);
253
+ fail:
254
+ if (rc == 0)
255
+ {
256
+ char buffer[8192] = { 0 };
257
+ WLog_WARN(TAG, "Failed to write PEM [%" PRIuz "] to file '%s' [%s]", length, filename,
258
+ winpr_strerror(errno, buffer, sizeof(buffer)));
259
+ }
260
+ return rc == size;
261
+ }
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/crypto/crypto.h ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * Cryptographic Abstraction Layer
4
+ *
5
+ * Copyright 2011-2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+
20
+ #ifndef FREERDP_LIB_CRYPTO_H
21
+ #define FREERDP_LIB_CRYPTO_H
22
+
23
+ /* OpenSSL includes windows.h */
24
+ #include <winpr/windows.h>
25
+ #include <winpr/custom-crypto.h>
26
+
27
+ #include <freerdp/api.h>
28
+ #include <freerdp/freerdp.h>
29
+ #include <freerdp/crypto/crypto.h>
30
+
31
+ #ifdef __cplusplus
32
+ extern "C"
33
+ {
34
+ #endif
35
+
36
+ FREERDP_LOCAL SSIZE_T crypto_rsa_public_encrypt(const BYTE* input, size_t length,
37
+ const rdpCertInfo* cert, BYTE* output,
38
+ size_t output_length);
39
+ FREERDP_LOCAL SSIZE_T crypto_rsa_public_decrypt(const BYTE* input, size_t length,
40
+ const rdpCertInfo* cert, BYTE* output,
41
+ size_t output_length);
42
+ FREERDP_LOCAL SSIZE_T crypto_rsa_private_encrypt(const BYTE* input, size_t length,
43
+ const rdpPrivateKey* key, BYTE* output,
44
+ size_t output_length);
45
+ FREERDP_LOCAL SSIZE_T crypto_rsa_private_decrypt(const BYTE* input, size_t length,
46
+ const rdpPrivateKey* key, BYTE* output,
47
+ size_t output_length);
48
+
49
+ FREERDP_LOCAL void crypto_reverse(BYTE* data, size_t length);
50
+
51
+ #ifdef __cplusplus
52
+ }
53
+ #endif
54
+
55
+ #endif /* FREERDP_LIB_CRYPTO_H */
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/crypto/der.c ADDED
@@ -0,0 +1,106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * ASN.1 Basic Encoding Rules (DER)
4
+ *
5
+ * Copyright 2011 Samsung, Author Jiten Pathy
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+
20
+ #include <freerdp/config.h>
21
+
22
+ #include <winpr/crt.h>
23
+ #include <winpr/assert.h>
24
+ #include <winpr/cast.h>
25
+
26
+ #include <freerdp/crypto/der.h>
27
+
28
+ int _der_skip_length(int length)
29
+ {
30
+ if (length > 0x7F && length <= 0xFF)
31
+ return 2;
32
+ else if (length > 0xFF)
33
+ return 3;
34
+ else
35
+ return 1;
36
+ }
37
+
38
+ int der_write_length(wStream* s, int length)
39
+ {
40
+ if (length > 0x7F && length <= 0xFF)
41
+ {
42
+ Stream_Write_UINT8(s, 0x81);
43
+ Stream_Write_UINT8(s, WINPR_ASSERTING_INT_CAST(UINT8, length));
44
+ return 2;
45
+ }
46
+ else if (length > 0xFF)
47
+ {
48
+ Stream_Write_UINT8(s, 0x82);
49
+ Stream_Write_UINT16_BE(s, WINPR_ASSERTING_INT_CAST(UINT16, length));
50
+ return 3;
51
+ }
52
+ else
53
+ {
54
+ Stream_Write_UINT8(s, WINPR_ASSERTING_INT_CAST(UINT8, length));
55
+ return 1;
56
+ }
57
+ }
58
+
59
+ int der_get_content_length(int length)
60
+ {
61
+ if (length > 0x81 && length <= 0x102)
62
+ return length - 3;
63
+ else if (length > 0x102)
64
+ return length - 4;
65
+ else
66
+ return length - 2;
67
+ }
68
+
69
+ int der_skip_contextual_tag(int length)
70
+ {
71
+ return _der_skip_length(length) + 1;
72
+ }
73
+
74
+ int der_write_contextual_tag(wStream* s, BYTE tag, int length, BOOL pc)
75
+ {
76
+ Stream_Write_UINT8(s, (ER_CLASS_CTXT | ER_PC(pc)) | (ER_TAG_MASK & tag));
77
+ return der_write_length(s, length) + 1;
78
+ }
79
+
80
+ static void der_write_universal_tag(wStream* s, BYTE tag, BOOL pc)
81
+ {
82
+ Stream_Write_UINT8(s, (ER_CLASS_UNIV | ER_PC(pc)) | (ER_TAG_MASK & tag));
83
+ }
84
+
85
+ int der_skip_octet_string(int length)
86
+ {
87
+ return 1 + _der_skip_length(length) + length;
88
+ }
89
+
90
+ void der_write_octet_string(wStream* s, BYTE* oct_str, int length)
91
+ {
92
+ der_write_universal_tag(s, ER_TAG_OCTET_STRING, FALSE);
93
+ der_write_length(s, length);
94
+ Stream_Write(s, oct_str, WINPR_ASSERTING_INT_CAST(size_t, length));
95
+ }
96
+
97
+ int der_skip_sequence_tag(int length)
98
+ {
99
+ return 1 + _der_skip_length(length);
100
+ }
101
+
102
+ int der_write_sequence_tag(wStream* s, int length)
103
+ {
104
+ Stream_Write_UINT8(s, (ER_CLASS_UNIV | ER_CONSTRUCT) | (ER_TAG_MASK & ER_TAG_SEQUENCE));
105
+ return der_write_length(s, length) + 1;
106
+ }
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/crypto/er.c ADDED
@@ -0,0 +1,448 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * ASN.1 Encoding Rules (BER/DER common functions)
4
+ *
5
+ * Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
+ * Modified by Jiten Pathy
7
+ *
8
+ * Licensed under the Apache License, Version 2.0 (the "License");
9
+ * you may not use this file except in compliance with the License.
10
+ * You may obtain a copy of the License at
11
+ *
12
+ * http://www.apache.org/licenses/LICENSE-2.0
13
+ *
14
+ * Unless required by applicable law or agreed to in writing, software
15
+ * distributed under the License is distributed on an "AS IS" BASIS,
16
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ * See the License for the specific language governing permissions and
18
+ * limitations under the License.
19
+ */
20
+
21
+ #include <freerdp/config.h>
22
+
23
+ #include <winpr/crt.h>
24
+ #include <winpr/assert.h>
25
+ #include <winpr/cast.h>
26
+
27
+ #include <freerdp/crypto/er.h>
28
+ #include <freerdp/crypto/ber.h>
29
+ #include <freerdp/crypto/der.h>
30
+
31
+ void er_read_length(wStream* s, int* length)
32
+ {
33
+ BYTE byte = 0;
34
+
35
+ Stream_Read_UINT8(s, byte);
36
+
37
+ if (!length)
38
+ return;
39
+
40
+ *length = 0;
41
+ if (!s)
42
+ return;
43
+
44
+ if (byte & 0x80)
45
+ {
46
+ byte &= ~(0x80);
47
+
48
+ if (byte == 1)
49
+ Stream_Read_UINT8(s, *length);
50
+ if (byte == 2)
51
+ Stream_Read_UINT16_BE(s, *length);
52
+ }
53
+ else
54
+ {
55
+ *length = byte;
56
+ }
57
+ }
58
+
59
+ /**
60
+ * Write er length.
61
+ * @param s stream
62
+ * @param length length
63
+ */
64
+
65
+ int er_write_length(wStream* s, int length, BOOL flag)
66
+ {
67
+ WINPR_ASSERT(length >= 0);
68
+ if (flag)
69
+ return der_write_length(s, length);
70
+ else
71
+ return (int)ber_write_length(s, (size_t)length);
72
+ }
73
+
74
+ int _er_skip_length(int length)
75
+ {
76
+ if (length > 0x7F)
77
+ return 3;
78
+ else
79
+ return 1;
80
+ }
81
+
82
+ int er_get_content_length(int length)
83
+ {
84
+ if (length - 1 > 0x7F)
85
+ return length - 4;
86
+ else
87
+ return length - 2;
88
+ }
89
+
90
+ /**
91
+ * Read er Universal tag.
92
+ * @param s A pointer to the stream to write to
93
+ * @param tag er universally-defined tag
94
+ * @return \b TRUE for success
95
+ */
96
+
97
+ BOOL er_read_universal_tag(wStream* s, BYTE tag, BOOL pc)
98
+ {
99
+ BYTE byte = 0;
100
+
101
+ Stream_Read_UINT8(s, byte);
102
+
103
+ if (byte != (ER_CLASS_UNIV | ER_PC(pc) | (ER_TAG_MASK & tag)))
104
+ return FALSE;
105
+
106
+ return TRUE;
107
+ }
108
+
109
+ /**
110
+ * Write er Universal tag.
111
+ * @param s stream
112
+ * @param tag er universally-defined tag
113
+ * @param pc primitive (FALSE) or constructed (TRUE)
114
+ */
115
+
116
+ void er_write_universal_tag(wStream* s, BYTE tag, BOOL pc)
117
+ {
118
+ Stream_Write_UINT8(s, (ER_CLASS_UNIV | ER_PC(pc)) | (ER_TAG_MASK & tag));
119
+ }
120
+
121
+ /**
122
+ * Read er Application tag.
123
+ * @param s stream
124
+ * @param tag er application-defined tag
125
+ * @param length length
126
+ */
127
+
128
+ BOOL er_read_application_tag(wStream* s, BYTE tag, int* length)
129
+ {
130
+ BYTE byte = 0;
131
+
132
+ if (tag > 30)
133
+ {
134
+ Stream_Read_UINT8(s, byte);
135
+
136
+ if (byte != ((ER_CLASS_APPL | ER_CONSTRUCT) | ER_TAG_MASK))
137
+ return FALSE;
138
+
139
+ Stream_Read_UINT8(s, byte);
140
+
141
+ if (byte != tag)
142
+ return FALSE;
143
+
144
+ er_read_length(s, length);
145
+ }
146
+ else
147
+ {
148
+ Stream_Read_UINT8(s, byte);
149
+
150
+ if (byte != ((ER_CLASS_APPL | ER_CONSTRUCT) | (ER_TAG_MASK & tag)))
151
+ return FALSE;
152
+
153
+ er_read_length(s, length);
154
+ }
155
+
156
+ return TRUE;
157
+ }
158
+
159
+ /**
160
+ * Write er Application tag.
161
+ * @param s stream
162
+ * @param tag er application-defined tag
163
+ * @param length length
164
+ */
165
+
166
+ void er_write_application_tag(wStream* s, BYTE tag, int length, BOOL flag)
167
+ {
168
+ if (tag > 30)
169
+ {
170
+ Stream_Write_UINT8(s, (ER_CLASS_APPL | ER_CONSTRUCT) | ER_TAG_MASK);
171
+ Stream_Write_UINT8(s, tag);
172
+ er_write_length(s, length, flag);
173
+ }
174
+ else
175
+ {
176
+ Stream_Write_UINT8(s, (ER_CLASS_APPL | ER_CONSTRUCT) | (ER_TAG_MASK & tag));
177
+ er_write_length(s, length, flag);
178
+ }
179
+ }
180
+
181
+ BOOL er_read_contextual_tag(wStream* s, BYTE tag, int* length, BOOL pc)
182
+ {
183
+ BYTE byte = 0;
184
+
185
+ Stream_Read_UINT8(s, byte);
186
+
187
+ if (byte != ((ER_CLASS_CTXT | ER_PC(pc)) | (ER_TAG_MASK & tag)))
188
+ {
189
+ Stream_Rewind(s, 1);
190
+ return FALSE;
191
+ }
192
+
193
+ er_read_length(s, length);
194
+
195
+ return TRUE;
196
+ }
197
+
198
+ int er_write_contextual_tag(wStream* s, BYTE tag, int length, BOOL pc, BOOL flag)
199
+ {
200
+ Stream_Write_UINT8(s, (ER_CLASS_CTXT | ER_PC(pc)) | (ER_TAG_MASK & tag));
201
+ return er_write_length(s, length, flag) + 1;
202
+ }
203
+
204
+ int er_skip_contextual_tag(int length)
205
+ {
206
+ return _er_skip_length(length) + 1;
207
+ }
208
+
209
+ BOOL er_read_sequence_tag(wStream* s, int* length)
210
+ {
211
+ BYTE byte = 0;
212
+
213
+ Stream_Read_UINT8(s, byte);
214
+
215
+ if (byte != ((ER_CLASS_UNIV | ER_CONSTRUCT) | (ER_TAG_SEQUENCE_OF)))
216
+ return FALSE;
217
+
218
+ er_read_length(s, length);
219
+
220
+ return TRUE;
221
+ }
222
+
223
+ /**
224
+ * Write er SEQUENCE tag.
225
+ * @param s stream
226
+ * @param length length
227
+ */
228
+
229
+ int er_write_sequence_tag(wStream* s, int length, BOOL flag)
230
+ {
231
+ Stream_Write_UINT8(s, (ER_CLASS_UNIV | ER_CONSTRUCT) | (ER_TAG_MASK & ER_TAG_SEQUENCE));
232
+ return er_write_length(s, length, flag) + 1;
233
+ }
234
+
235
+ int er_skip_sequence(int length)
236
+ {
237
+ return 1 + _er_skip_length(length) + length;
238
+ }
239
+
240
+ int er_skip_sequence_tag(int length)
241
+ {
242
+ return 1 + _er_skip_length(length);
243
+ }
244
+
245
+ BOOL er_read_enumerated(wStream* s, BYTE* enumerated, BYTE count)
246
+ {
247
+ int length = 0;
248
+
249
+ er_read_universal_tag(s, ER_TAG_ENUMERATED, FALSE);
250
+ er_read_length(s, &length);
251
+
252
+ if (length == 1)
253
+ Stream_Read_UINT8(s, *enumerated);
254
+ else
255
+ return FALSE;
256
+
257
+ /* check that enumerated value falls within expected range */
258
+ if (*enumerated + 1 > count)
259
+ return FALSE;
260
+
261
+ return TRUE;
262
+ }
263
+
264
+ void er_write_enumerated(wStream* s, BYTE enumerated, BYTE count, BOOL flag)
265
+ {
266
+ er_write_universal_tag(s, ER_TAG_ENUMERATED, FALSE);
267
+ er_write_length(s, 1, flag);
268
+ Stream_Write_UINT8(s, enumerated);
269
+ }
270
+
271
+ BOOL er_read_bit_string(wStream* s, int* length, BYTE* padding)
272
+ {
273
+ er_read_universal_tag(s, ER_TAG_BIT_STRING, FALSE);
274
+ er_read_length(s, length);
275
+ Stream_Read_UINT8(s, *padding);
276
+
277
+ return TRUE;
278
+ }
279
+
280
+ BOOL er_write_bit_string_tag(wStream* s, UINT32 length, BYTE padding, BOOL flag)
281
+ {
282
+ er_write_universal_tag(s, ER_TAG_BIT_STRING, FALSE);
283
+ er_write_length(s, WINPR_ASSERTING_INT_CAST(int, length), flag);
284
+ Stream_Write_UINT8(s, padding);
285
+ return TRUE;
286
+ }
287
+
288
+ BOOL er_read_octet_string(wStream* s, int* length)
289
+ {
290
+ if (!er_read_universal_tag(s, ER_TAG_OCTET_STRING, FALSE))
291
+ return FALSE;
292
+ er_read_length(s, length);
293
+
294
+ return TRUE;
295
+ }
296
+
297
+ /**
298
+ * Write a er OCTET_STRING
299
+ * @param s stream
300
+ * @param oct_str octet string
301
+ * @param length string length
302
+ */
303
+
304
+ void er_write_octet_string(wStream* s, BYTE* oct_str, int length, BOOL flag)
305
+ {
306
+ er_write_universal_tag(s, ER_TAG_OCTET_STRING, FALSE);
307
+ er_write_length(s, length, flag);
308
+ Stream_Write(s, oct_str, WINPR_ASSERTING_INT_CAST(size_t, length));
309
+ }
310
+
311
+ int er_write_octet_string_tag(wStream* s, int length, BOOL flag)
312
+ {
313
+ er_write_universal_tag(s, ER_TAG_OCTET_STRING, FALSE);
314
+ er_write_length(s, length, flag);
315
+ return 1 + _er_skip_length(length);
316
+ }
317
+
318
+ int er_skip_octet_string(int length)
319
+ {
320
+ return 1 + _er_skip_length(length) + length;
321
+ }
322
+
323
+ /**
324
+ * Read a er BOOLEAN
325
+ * @param s A pointer to the stream to read from
326
+ * @param value A pointer to read the data to
327
+ */
328
+
329
+ BOOL er_read_BOOL(wStream* s, BOOL* value)
330
+ {
331
+ int length = 0;
332
+ BYTE v = 0;
333
+
334
+ if (!er_read_universal_tag(s, ER_TAG_BOOLEAN, FALSE))
335
+ return FALSE;
336
+ er_read_length(s, &length);
337
+ if (length != 1)
338
+ return FALSE;
339
+ Stream_Read_UINT8(s, v);
340
+ *value = (v ? TRUE : FALSE);
341
+ return TRUE;
342
+ }
343
+
344
+ /**
345
+ * Write a er BOOLEAN
346
+ * @param s A pointer to the stream to write to
347
+ * @param value The value to write
348
+ */
349
+
350
+ void er_write_BOOL(wStream* s, BOOL value)
351
+ {
352
+ er_write_universal_tag(s, ER_TAG_BOOLEAN, FALSE);
353
+ er_write_length(s, 1, FALSE);
354
+ Stream_Write_UINT8(s, (value == TRUE) ? 0xFF : 0);
355
+ }
356
+
357
+ BOOL er_read_integer(wStream* s, UINT32* value)
358
+ {
359
+ int length = 0;
360
+
361
+ er_read_universal_tag(s, ER_TAG_INTEGER, FALSE);
362
+ er_read_length(s, &length);
363
+
364
+ if (value == NULL)
365
+ {
366
+ Stream_Seek(s, WINPR_ASSERTING_INT_CAST(size_t, length));
367
+ return TRUE;
368
+ }
369
+
370
+ if (length == 1)
371
+ {
372
+ Stream_Read_UINT8(s, *value);
373
+ }
374
+ else if (length == 2)
375
+ {
376
+ Stream_Read_UINT16_BE(s, *value);
377
+ }
378
+ else if (length == 3)
379
+ {
380
+ BYTE byte = 0;
381
+ Stream_Read_UINT8(s, byte);
382
+ Stream_Read_UINT16_BE(s, *value);
383
+ *value += (byte << 16) & 0xFF0000;
384
+ }
385
+ else if (length == 4)
386
+ {
387
+ Stream_Read_UINT32_BE(s, *value);
388
+ }
389
+ else
390
+ {
391
+ return FALSE;
392
+ }
393
+
394
+ return TRUE;
395
+ }
396
+
397
+ /**
398
+ * Write a er INTEGER
399
+ * @param s A pointer to the stream to write to
400
+ * @param value the value to write
401
+ */
402
+
403
+ int er_write_integer(wStream* s, INT32 value)
404
+ {
405
+ er_write_universal_tag(s, ER_TAG_INTEGER, FALSE);
406
+
407
+ if (value <= 127 && value >= -128)
408
+ {
409
+ er_write_length(s, 1, FALSE);
410
+ Stream_Write_INT8(s, WINPR_ASSERTING_INT_CAST(INT8, value));
411
+ return 2;
412
+ }
413
+ else if (value <= 32767 && value >= -32768)
414
+ {
415
+ er_write_length(s, 2, FALSE);
416
+ Stream_Write_INT16_BE(s, WINPR_ASSERTING_INT_CAST(INT16, value));
417
+ return 3;
418
+ }
419
+ else
420
+ {
421
+ er_write_length(s, 4, FALSE);
422
+ Stream_Write_INT32_BE(s, value);
423
+ return 5;
424
+ }
425
+ }
426
+
427
+ int er_skip_integer(INT32 value)
428
+ {
429
+ if (value <= 127 && value >= -128)
430
+ {
431
+ return _er_skip_length(1) + 2;
432
+ }
433
+ else if (value <= 32767 && value >= -32768)
434
+ {
435
+ return _er_skip_length(2) + 3;
436
+ }
437
+ else
438
+ {
439
+ return _er_skip_length(4) + 5;
440
+ }
441
+ }
442
+
443
+ BOOL er_read_integer_length(wStream* s, int* length)
444
+ {
445
+ er_read_universal_tag(s, ER_TAG_INTEGER, FALSE);
446
+ er_read_length(s, length);
447
+ return TRUE;
448
+ }
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/crypto/opensslcompat.c ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * OpenSSL Compatibility
4
+ *
5
+ * Copyright (C) 2016 Norbert Federa <norbert.federa@thincast.com>
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+
20
+ #include "opensslcompat.h"
21
+
22
+ #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
23
+ (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x2070000fL)
24
+
25
+ BIO_METHOD* BIO_meth_new(int type, const char* name)
26
+ {
27
+ BIO_METHOD* m;
28
+ if (!(m = calloc(1, sizeof(BIO_METHOD))))
29
+ return NULL;
30
+ m->type = type;
31
+ m->name = name;
32
+ return m;
33
+ }
34
+
35
+ void RSA_get0_key(const RSA* r, const BIGNUM** n, const BIGNUM** e, const BIGNUM** d)
36
+ {
37
+ if (n != NULL)
38
+ *n = r->n;
39
+ if (e != NULL)
40
+ *e = r->e;
41
+ if (d != NULL)
42
+ *d = r->d;
43
+ }
44
+
45
+ #endif /* OPENSSL < 1.1.0 || LIBRESSL */
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/crypto/opensslcompat.h ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * OpenSSL Compatibility
4
+ *
5
+ * Copyright (C) 2016 Norbert Federa <norbert.federa@thincast.com>
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+
20
+ #ifndef FREERDP_LIB_CRYPTO_OPENSSLCOMPAT_H
21
+ #define FREERDP_LIB_CRYPTO_OPENSSLCOMPAT_H
22
+
23
+ #include <winpr/winpr.h>
24
+ #include <winpr/assert.h>
25
+ #include <freerdp/config.h>
26
+
27
+ #include <freerdp/api.h>
28
+
29
+ #include <openssl/x509.h>
30
+
31
+ #ifdef WITH_OPENSSL
32
+
33
+ #include <openssl/opensslv.h>
34
+
35
+ #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
36
+ (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x2070000fL)
37
+
38
+ #include <openssl/bio.h>
39
+ #include <openssl/rsa.h>
40
+ #include <openssl/bn.h>
41
+
42
+ #define BIO_get_data(b) (b)->ptr
43
+ #define BIO_set_data(b, v) (b)->ptr = v
44
+ #define BIO_get_init(b) (b)->init
45
+ #define BIO_set_init(b, v) (b)->init = v
46
+ #define BIO_get_next(b, v) (b)->next_bio
47
+ #define BIO_set_next(b, v) (b)->next_bio = v
48
+ #define BIO_get_shutdown(b) (b)->shutdown
49
+ #define BIO_set_shutdown(b, v) (b)->shutdown = v
50
+ #define BIO_get_retry_reason(b) (b)->retry_reason
51
+ #define BIO_set_retry_reason(b, v) (b)->retry_reason = v
52
+
53
+ #define BIO_meth_set_write(b, f) (b)->bwrite = (f)
54
+ #define BIO_meth_set_read(b, f) (b)->bread = (f)
55
+ #define BIO_meth_set_puts(b, f) (b)->bputs = (f)
56
+ #define BIO_meth_set_gets(b, f) (b)->bgets = (f)
57
+ #define BIO_meth_set_ctrl(b, f) (b)->ctrl = (f)
58
+ #define BIO_meth_set_create(b, f) (b)->create = (f)
59
+ #define BIO_meth_set_destroy(b, f) (b)->destroy = (f)
60
+ #define BIO_meth_set_callback_ctrl(b, f) (b)->callback_ctrl = (f)
61
+
62
+ BIO_METHOD* BIO_meth_new(int type, const char* name);
63
+ void RSA_get0_key(const RSA* r, const BIGNUM** n, const BIGNUM** e, const BIGNUM** d);
64
+
65
+ #endif /* OPENSSL < 1.1.0 || LIBRESSL */
66
+ #endif /* WITH_OPENSSL */
67
+
68
+ /* Drop in replacement for older OpenSSL and LibRESSL */
69
+ #if defined(LIBRESSL_VERSION_NUMBER) || \
70
+ (defined(OPENSSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER < 0x1010000fL))
71
+
72
+ static INLINE STACK_OF(X509) * sk_X509_deep_copy(const STACK_OF(X509) * sk,
73
+ X509* (*copyfunc)(const X509*),
74
+ void (*freefunc)(X509*))
75
+ {
76
+ WINPR_ASSERT(copyfunc);
77
+ WINPR_ASSERT(freefunc);
78
+
79
+ STACK_OF(X509)* stack = sk_X509_new_null();
80
+ if (!stack)
81
+ return NULL;
82
+
83
+ if (sk)
84
+ {
85
+ for (int i = 0; i < sk_X509_num(sk); i++)
86
+ {
87
+ X509* cert = sk_X509_value(sk, i);
88
+ X509* copy = copyfunc(cert);
89
+ if (!copy)
90
+ goto fail;
91
+ const int rc = sk_X509_push(stack, copy);
92
+ if (rc <= 0)
93
+ goto fail;
94
+ }
95
+ }
96
+
97
+ return stack;
98
+
99
+ fail:
100
+ sk_X509_pop_free(stack, freefunc);
101
+ return NULL;
102
+ }
103
+ #endif
104
+
105
+ /* OpenSSL API is not const consistent.
106
+ * While the TYPE_dup function take non const arguments
107
+ * the TYPE_sk versions require the arguments to be const...
108
+ */
109
+ static INLINE X509* X509_const_dup(const X509* x509)
110
+ {
111
+ X509* ptr = WINPR_CAST_CONST_PTR_AWAY(x509, X509*);
112
+ return X509_dup(ptr);
113
+ }
114
+ #endif /* FREERDP_LIB_CRYPTO_OPENSSLCOMPAT_H */
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/crypto/privatekey.c ADDED
@@ -0,0 +1,552 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * Private key Handling
4
+ *
5
+ * Copyright 2011 Jiten Pathy
6
+ * Copyright 2011 Marc-Andre Moreau <marcandre.moreau@gmail.com>
7
+ * Copyright 2015 Thincast Technologies GmbH
8
+ * Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com>
9
+ * Copyright 2023 Armin Novak <anovak@thincast.com>
10
+ * Copyright 2023 Thincast Technologies GmbH
11
+ *
12
+ * Licensed under the Apache License, Version 2.0 (the "License");
13
+ * you may not use this file except in compliance with the License.
14
+ * You may obtain a copy of the License at
15
+ *
16
+ * http://www.apache.org/licenses/LICENSE-2.0
17
+ *
18
+ * Unless required by applicable law or agreed to in writing, software
19
+ * distributed under the License is distributed on an "AS IS" BASIS,
20
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21
+ * See the License for the specific language governing permissions and
22
+ * limitations under the License.
23
+ */
24
+
25
+ #include <freerdp/config.h>
26
+
27
+ #include <errno.h>
28
+ #include <stdio.h>
29
+ #include <string.h>
30
+
31
+ #include <winpr/assert.h>
32
+ #include <winpr/wtypes.h>
33
+ #include <winpr/crt.h>
34
+ #include <winpr/file.h>
35
+ #include <winpr/crypto.h>
36
+
37
+ #include <openssl/pem.h>
38
+ #include <openssl/rsa.h>
39
+ #include <openssl/bn.h>
40
+
41
+ #include "privatekey.h"
42
+ #include "cert_common.h"
43
+
44
+ #include <freerdp/crypto/privatekey.h>
45
+
46
+ #include <openssl/evp.h>
47
+
48
+ #if defined(OPENSSL_VERSION_MAJOR) && (OPENSSL_VERSION_MAJOR >= 3)
49
+ #include <openssl/core_names.h>
50
+ #endif
51
+
52
+ #include "x509_utils.h"
53
+ #include "crypto.h"
54
+ #include "opensslcompat.h"
55
+
56
+ #define TAG FREERDP_TAG("crypto")
57
+
58
+ struct rdp_private_key
59
+ {
60
+ EVP_PKEY* evp;
61
+
62
+ rdpCertInfo cert;
63
+ BYTE* PrivateExponent;
64
+ DWORD PrivateExponentLength;
65
+ };
66
+
67
+ /*
68
+ * Terminal Services Signing Keys.
69
+ * Yes, Terminal Services Private Key is publicly available.
70
+ */
71
+
72
+ static BYTE tssk_modulus[] = { 0x3d, 0x3a, 0x5e, 0xbd, 0x72, 0x43, 0x3e, 0xc9, 0x4d, 0xbb, 0xc1,
73
+ 0x1e, 0x4a, 0xba, 0x5f, 0xcb, 0x3e, 0x88, 0x20, 0x87, 0xef, 0xf5,
74
+ 0xc1, 0xe2, 0xd7, 0xb7, 0x6b, 0x9a, 0xf2, 0x52, 0x45, 0x95, 0xce,
75
+ 0x63, 0x65, 0x6b, 0x58, 0x3a, 0xfe, 0xef, 0x7c, 0xe7, 0xbf, 0xfe,
76
+ 0x3d, 0xf6, 0x5c, 0x7d, 0x6c, 0x5e, 0x06, 0x09, 0x1a, 0xf5, 0x61,
77
+ 0xbb, 0x20, 0x93, 0x09, 0x5f, 0x05, 0x6d, 0xea, 0x87 };
78
+
79
+ static BYTE tssk_privateExponent[] = {
80
+ 0x87, 0xa7, 0x19, 0x32, 0xda, 0x11, 0x87, 0x55, 0x58, 0x00, 0x16, 0x16, 0x25, 0x65, 0x68, 0xf8,
81
+ 0x24, 0x3e, 0xe6, 0xfa, 0xe9, 0x67, 0x49, 0x94, 0xcf, 0x92, 0xcc, 0x33, 0x99, 0xe8, 0x08, 0x60,
82
+ 0x17, 0x9a, 0x12, 0x9f, 0x24, 0xdd, 0xb1, 0x24, 0x99, 0xc7, 0x3a, 0xb8, 0x0a, 0x7b, 0x0d, 0xdd,
83
+ 0x35, 0x07, 0x79, 0x17, 0x0b, 0x51, 0x9b, 0xb3, 0xc7, 0x10, 0x01, 0x13, 0xe7, 0x3f, 0xf3, 0x5f
84
+ };
85
+
86
+ static const rdpPrivateKey tssk = { .PrivateExponent = tssk_privateExponent,
87
+ .PrivateExponentLength = sizeof(tssk_privateExponent),
88
+ .cert = { .Modulus = tssk_modulus,
89
+ .ModulusLength = sizeof(tssk_modulus) } };
90
+ const rdpPrivateKey* priv_key_tssk = &tssk;
91
+
92
+ #if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
93
+ static RSA* evp_pkey_to_rsa(const rdpPrivateKey* key)
94
+ {
95
+ if (!freerdp_key_is_rsa(key))
96
+ {
97
+ WLog_WARN(TAG, "Key is no RSA key");
98
+ return NULL;
99
+ }
100
+
101
+ RSA* rsa = NULL;
102
+ BIO* bio = BIO_new(
103
+ #if defined(LIBRESSL_VERSION_NUMBER)
104
+ BIO_s_mem()
105
+ #else
106
+ BIO_s_secmem()
107
+ #endif
108
+ );
109
+ if (!bio)
110
+ return NULL;
111
+ const int rc = PEM_write_bio_PrivateKey(bio, key->evp, NULL, NULL, 0, NULL, NULL);
112
+ if (rc != 1)
113
+ goto fail;
114
+ rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL);
115
+ fail:
116
+ BIO_free_all(bio);
117
+ return rsa;
118
+ }
119
+ #endif
120
+
121
+ static EVP_PKEY* evp_pkey_utils_from_pem(const char* data, size_t len, BOOL fromFile)
122
+ {
123
+ EVP_PKEY* evp = NULL;
124
+ BIO* bio = NULL;
125
+ if (fromFile)
126
+ bio = BIO_new_file(data, "rb");
127
+ else
128
+ {
129
+ if (len > INT_MAX)
130
+ return NULL;
131
+ bio = BIO_new_mem_buf(data, (int)len);
132
+ }
133
+
134
+ if (!bio)
135
+ {
136
+ WLog_ERR(TAG, "BIO_new failed for private key");
137
+ return NULL;
138
+ }
139
+
140
+ evp = PEM_read_bio_PrivateKey(bio, NULL, NULL, 0);
141
+ BIO_free_all(bio);
142
+ if (!evp)
143
+ WLog_ERR(TAG, "PEM_read_bio_PrivateKey returned NULL [input length %" PRIuz "]", len);
144
+
145
+ return evp;
146
+ }
147
+
148
+ static BOOL key_read_private(rdpPrivateKey* key)
149
+ {
150
+ BOOL rc = FALSE;
151
+
152
+ WINPR_ASSERT(key);
153
+ WINPR_ASSERT(key->evp);
154
+
155
+ /* The key is not an RSA key, that means we just return success. */
156
+ if (!freerdp_key_is_rsa(key))
157
+ return TRUE;
158
+
159
+ #if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
160
+ RSA* rsa = evp_pkey_to_rsa(key);
161
+ if (!rsa)
162
+ {
163
+ char ebuffer[256] = { 0 };
164
+ WLog_ERR(TAG, "unable to load RSA key: %s.",
165
+ winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
166
+ goto fail;
167
+ }
168
+
169
+ switch (RSA_check_key(rsa))
170
+ {
171
+ case 0:
172
+ WLog_ERR(TAG, "invalid RSA key");
173
+ goto fail;
174
+
175
+ case 1:
176
+ /* Valid key. */
177
+ break;
178
+
179
+ default:
180
+ {
181
+ char ebuffer[256] = { 0 };
182
+ WLog_ERR(TAG, "unexpected error when checking RSA key: %s.",
183
+ winpr_strerror(errno, ebuffer, sizeof(ebuffer)));
184
+ goto fail;
185
+ }
186
+ }
187
+
188
+ const BIGNUM* rsa_e = NULL;
189
+ const BIGNUM* rsa_n = NULL;
190
+ const BIGNUM* rsa_d = NULL;
191
+
192
+ RSA_get0_key(rsa, &rsa_n, &rsa_e, &rsa_d);
193
+ #else
194
+ BIGNUM* rsa_e = NULL;
195
+ BIGNUM* rsa_n = NULL;
196
+ BIGNUM* rsa_d = NULL;
197
+
198
+ if (!EVP_PKEY_get_bn_param(key->evp, OSSL_PKEY_PARAM_RSA_N, &rsa_n))
199
+ goto fail;
200
+ if (!EVP_PKEY_get_bn_param(key->evp, OSSL_PKEY_PARAM_RSA_E, &rsa_e))
201
+ goto fail;
202
+ if (!EVP_PKEY_get_bn_param(key->evp, OSSL_PKEY_PARAM_RSA_D, &rsa_d))
203
+ goto fail;
204
+ #endif
205
+ if (BN_num_bytes(rsa_e) > 4)
206
+ {
207
+ WLog_ERR(TAG, "RSA public exponent too large");
208
+ goto fail;
209
+ }
210
+
211
+ if (!read_bignum(&key->PrivateExponent, &key->PrivateExponentLength, rsa_d, TRUE))
212
+ goto fail;
213
+
214
+ if (!cert_info_create(&key->cert, rsa_n, rsa_e))
215
+ goto fail;
216
+ rc = TRUE;
217
+ fail:
218
+ #if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
219
+ RSA_free(rsa);
220
+ #else
221
+ BN_free(rsa_d);
222
+ BN_free(rsa_e);
223
+ BN_free(rsa_n);
224
+ #endif
225
+ return rc;
226
+ }
227
+
228
+ rdpPrivateKey* freerdp_key_new_from_pem(const char* pem)
229
+ {
230
+ rdpPrivateKey* key = freerdp_key_new();
231
+ if (!key || !pem)
232
+ goto fail;
233
+ key->evp = evp_pkey_utils_from_pem(pem, strlen(pem), FALSE);
234
+ if (!key->evp)
235
+ goto fail;
236
+ if (!key_read_private(key))
237
+ goto fail;
238
+ return key;
239
+ fail:
240
+ freerdp_key_free(key);
241
+ return NULL;
242
+ }
243
+
244
+ rdpPrivateKey* freerdp_key_new_from_file(const char* keyfile)
245
+ {
246
+
247
+ rdpPrivateKey* key = freerdp_key_new();
248
+ if (!key || !keyfile)
249
+ goto fail;
250
+
251
+ key->evp = evp_pkey_utils_from_pem(keyfile, strlen(keyfile), TRUE);
252
+ if (!key->evp)
253
+ goto fail;
254
+ if (!key_read_private(key))
255
+ goto fail;
256
+ return key;
257
+ fail:
258
+ freerdp_key_free(key);
259
+ return NULL;
260
+ }
261
+
262
+ rdpPrivateKey* freerdp_key_new(void)
263
+ {
264
+ return calloc(1, sizeof(rdpPrivateKey));
265
+ }
266
+
267
+ rdpPrivateKey* freerdp_key_clone(const rdpPrivateKey* key)
268
+ {
269
+ if (!key)
270
+ return NULL;
271
+
272
+ rdpPrivateKey* _key = (rdpPrivateKey*)calloc(1, sizeof(rdpPrivateKey));
273
+
274
+ if (!_key)
275
+ return NULL;
276
+
277
+ if (key->evp)
278
+ {
279
+ _key->evp = key->evp;
280
+ if (!_key->evp)
281
+ goto out_fail;
282
+ EVP_PKEY_up_ref(_key->evp);
283
+ }
284
+
285
+ if (key->PrivateExponent)
286
+ {
287
+ _key->PrivateExponent = (BYTE*)malloc(key->PrivateExponentLength);
288
+
289
+ if (!_key->PrivateExponent)
290
+ goto out_fail;
291
+
292
+ CopyMemory(_key->PrivateExponent, key->PrivateExponent, key->PrivateExponentLength);
293
+ _key->PrivateExponentLength = key->PrivateExponentLength;
294
+ }
295
+
296
+ if (!cert_info_clone(&_key->cert, &key->cert))
297
+ goto out_fail;
298
+
299
+ return _key;
300
+ out_fail:
301
+ WINPR_PRAGMA_DIAG_PUSH
302
+ WINPR_PRAGMA_DIAG_IGNORED_MISMATCHED_DEALLOC
303
+ freerdp_key_free(_key);
304
+ WINPR_PRAGMA_DIAG_POP
305
+ return NULL;
306
+ }
307
+
308
+ void freerdp_key_free(rdpPrivateKey* key)
309
+ {
310
+ if (!key)
311
+ return;
312
+
313
+ EVP_PKEY_free(key->evp);
314
+ if (key->PrivateExponent)
315
+ memset(key->PrivateExponent, 0, key->PrivateExponentLength);
316
+ free(key->PrivateExponent);
317
+ cert_info_free(&key->cert);
318
+ free(key);
319
+ }
320
+
321
+ const rdpCertInfo* freerdp_key_get_info(const rdpPrivateKey* key)
322
+ {
323
+ WINPR_ASSERT(key);
324
+ if (!freerdp_key_is_rsa(key))
325
+ return NULL;
326
+ return &key->cert;
327
+ }
328
+
329
+ const BYTE* freerdp_key_get_exponent(const rdpPrivateKey* key, size_t* plength)
330
+ {
331
+ WINPR_ASSERT(key);
332
+ if (!freerdp_key_is_rsa(key))
333
+ {
334
+ if (plength)
335
+ *plength = 0;
336
+ return NULL;
337
+ }
338
+
339
+ if (plength)
340
+ *plength = key->PrivateExponentLength;
341
+ return key->PrivateExponent;
342
+ }
343
+
344
+ EVP_PKEY* freerdp_key_get_evp_pkey(const rdpPrivateKey* key)
345
+ {
346
+ WINPR_ASSERT(key);
347
+
348
+ EVP_PKEY* evp = key->evp;
349
+ WINPR_ASSERT(evp);
350
+ EVP_PKEY_up_ref(evp);
351
+ return evp;
352
+ }
353
+
354
+ BOOL freerdp_key_is_rsa(const rdpPrivateKey* key)
355
+ {
356
+ WINPR_ASSERT(key);
357
+ if (key == priv_key_tssk)
358
+ return TRUE;
359
+
360
+ WINPR_ASSERT(key->evp);
361
+ return (EVP_PKEY_id(key->evp) == EVP_PKEY_RSA);
362
+ }
363
+
364
+ size_t freerdp_key_get_bits(const rdpPrivateKey* key)
365
+ {
366
+ int rc = -1;
367
+ #if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
368
+ RSA* rsa = evp_pkey_to_rsa(key);
369
+ if (rsa)
370
+ {
371
+ rc = RSA_bits(rsa);
372
+ RSA_free(rsa);
373
+ }
374
+ #else
375
+ rc = EVP_PKEY_get_bits(key->evp);
376
+ #endif
377
+
378
+ return WINPR_ASSERTING_INT_CAST(size_t, rc);
379
+ }
380
+
381
+ BOOL freerdp_key_generate(rdpPrivateKey* key, size_t key_length)
382
+ {
383
+ BOOL rc = FALSE;
384
+
385
+ #if !defined(OPENSSL_VERSION_MAJOR) || (OPENSSL_VERSION_MAJOR < 3)
386
+ RSA* rsa = NULL;
387
+ #if (OPENSSL_VERSION_NUMBER < 0x10100000L) || defined(LIBRESSL_VERSION_NUMBER)
388
+ rsa = RSA_generate_key(key_length, RSA_F4, NULL, NULL);
389
+ #else
390
+ {
391
+ BIGNUM* bn = BN_secure_new();
392
+
393
+ if (!bn)
394
+ return FALSE;
395
+
396
+ rsa = RSA_new();
397
+
398
+ if (!rsa)
399
+ {
400
+ BN_clear_free(bn);
401
+ return FALSE;
402
+ }
403
+
404
+ BN_set_word(bn, RSA_F4);
405
+ const int res = RSA_generate_key_ex(rsa, key_length, bn, NULL);
406
+ BN_clear_free(bn);
407
+
408
+ if (res != 1)
409
+ return FALSE;
410
+ }
411
+ #endif
412
+
413
+ EVP_PKEY_free(key->evp);
414
+ key->evp = EVP_PKEY_new();
415
+
416
+ if (!EVP_PKEY_assign_RSA(key->evp, rsa))
417
+ {
418
+ EVP_PKEY_free(key->evp);
419
+ key->evp = NULL;
420
+ RSA_free(rsa);
421
+ return FALSE;
422
+ }
423
+
424
+ rc = TRUE;
425
+ #else
426
+ EVP_PKEY_CTX* pctx = EVP_PKEY_CTX_new_from_name(NULL, "RSA", NULL);
427
+ if (!pctx)
428
+ return FALSE;
429
+
430
+ if (EVP_PKEY_keygen_init(pctx) != 1)
431
+ goto fail;
432
+
433
+ if (key_length > INT_MAX)
434
+ goto fail;
435
+
436
+ if (EVP_PKEY_CTX_set_rsa_keygen_bits(pctx, (int)key_length) != 1)
437
+ goto fail;
438
+
439
+ EVP_PKEY_free(key->evp);
440
+ key->evp = NULL;
441
+
442
+ if (EVP_PKEY_generate(pctx, &key->evp) != 1)
443
+ goto fail;
444
+
445
+ rc = TRUE;
446
+ fail:
447
+ EVP_PKEY_CTX_free(pctx);
448
+ #endif
449
+ return rc;
450
+ }
451
+
452
+ BYTE* freerdp_key_get_param(const rdpPrivateKey* key, enum FREERDP_KEY_PARAM param, size_t* plength)
453
+ {
454
+ BYTE* buf = NULL;
455
+
456
+ WINPR_ASSERT(key);
457
+ WINPR_ASSERT(plength);
458
+
459
+ *plength = 0;
460
+
461
+ BIGNUM* bn = NULL;
462
+ #if OPENSSL_VERSION_NUMBER >= 0x30000000L
463
+
464
+ const char* pk = NULL;
465
+ switch (param)
466
+ {
467
+ case FREERDP_KEY_PARAM_RSA_D:
468
+ pk = OSSL_PKEY_PARAM_RSA_D;
469
+ break;
470
+ case FREERDP_KEY_PARAM_RSA_E:
471
+ pk = OSSL_PKEY_PARAM_RSA_E;
472
+ break;
473
+ case FREERDP_KEY_PARAM_RSA_N:
474
+ pk = OSSL_PKEY_PARAM_RSA_N;
475
+ break;
476
+ default:
477
+ return NULL;
478
+ }
479
+
480
+ if (!EVP_PKEY_get_bn_param(key->evp, pk, &bn))
481
+ return NULL;
482
+ #else
483
+ {
484
+ const RSA* rsa = EVP_PKEY_get0_RSA(key->evp);
485
+ if (!rsa)
486
+ return NULL;
487
+
488
+ const BIGNUM* cbn = NULL;
489
+ switch (param)
490
+ {
491
+ case FREERDP_KEY_PARAM_RSA_D:
492
+ #if OPENSSL_VERSION_NUMBER >= 0x10101007L
493
+ cbn = RSA_get0_d(rsa);
494
+ #endif
495
+ break;
496
+ case FREERDP_KEY_PARAM_RSA_E:
497
+ #if OPENSSL_VERSION_NUMBER >= 0x10101007L
498
+ cbn = RSA_get0_e(rsa);
499
+ #endif
500
+ break;
501
+ case FREERDP_KEY_PARAM_RSA_N:
502
+ #if OPENSSL_VERSION_NUMBER >= 0x10101007L
503
+ cbn = RSA_get0_n(rsa);
504
+ #endif
505
+ break;
506
+ default:
507
+ return NULL;
508
+ }
509
+ if (!cbn)
510
+ return NULL;
511
+ bn = BN_dup(cbn);
512
+ if (!bn)
513
+ return NULL;
514
+ }
515
+ #endif
516
+
517
+ const int length = BN_num_bytes(bn);
518
+ if (length < 0)
519
+ goto fail;
520
+
521
+ const size_t alloc_size = (size_t)length + 1ull;
522
+ buf = calloc(alloc_size, sizeof(BYTE));
523
+ if (!buf)
524
+ goto fail;
525
+
526
+ const int bnlen = BN_bn2bin(bn, buf);
527
+ if (bnlen != length)
528
+ {
529
+ free(buf);
530
+ buf = NULL;
531
+ }
532
+ else
533
+ *plength = WINPR_ASSERTING_INT_CAST(size_t, length);
534
+
535
+ fail:
536
+ BN_free(bn);
537
+ return buf;
538
+ }
539
+
540
+ WINPR_DIGEST_CTX* freerdp_key_digest_sign(rdpPrivateKey* key, WINPR_MD_TYPE digest)
541
+ {
542
+ WINPR_DIGEST_CTX* md_ctx = winpr_Digest_New();
543
+ if (!md_ctx)
544
+ return NULL;
545
+
546
+ if (!winpr_DigestSign_Init(md_ctx, digest, key->evp))
547
+ {
548
+ winpr_Digest_Free(md_ctx);
549
+ return NULL;
550
+ }
551
+ return md_ctx;
552
+ }
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/crypto/tls.c ADDED
@@ -0,0 +1,2106 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * Transport Layer Security
4
+ *
5
+ * Copyright 2011-2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
+ * Copyright 2023 Armin Novak <anovak@thincast.com>
7
+ * Copyright 2023 Thincast Technologies GmbH
8
+ *
9
+ * Licensed under the Apache License, Version 2.0 (the "License");
10
+ * you may not use this file except in compliance with the License.
11
+ * You may obtain a copy of the License at
12
+ *
13
+ * http://www.apache.org/licenses/LICENSE-2.0
14
+ *
15
+ * Unless required by applicable law or agreed to in writing, software
16
+ * distributed under the License is distributed on an "AS IS" BASIS,
17
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ * See the License for the specific language governing permissions and
19
+ * limitations under the License.
20
+ */
21
+
22
+ #include <freerdp/config.h>
23
+
24
+ #include "../core/settings.h"
25
+
26
+ #include <winpr/assert.h>
27
+ #include <string.h>
28
+ #include <errno.h>
29
+
30
+ #include <winpr/crt.h>
31
+ #include <winpr/winpr.h>
32
+ #include <winpr/string.h>
33
+ #include <winpr/sspi.h>
34
+ #include <winpr/ssl.h>
35
+ #include <winpr/json.h>
36
+
37
+ #include <winpr/stream.h>
38
+ #include <freerdp/utils/ringbuffer.h>
39
+
40
+ #include <freerdp/crypto/certificate.h>
41
+ #include <freerdp/crypto/certificate_data.h>
42
+ #include <freerdp/utils/helpers.h>
43
+
44
+ #include <freerdp/log.h>
45
+ #include "../crypto/tls.h"
46
+ #include "../core/tcp.h"
47
+
48
+ #include "opensslcompat.h"
49
+ #include "certificate.h"
50
+ #include "privatekey.h"
51
+
52
+ #ifdef WINPR_HAVE_POLL_H
53
+ #include <poll.h>
54
+ #endif
55
+
56
+ #ifdef FREERDP_HAVE_VALGRIND_MEMCHECK_H
57
+ #include <valgrind/memcheck.h>
58
+ #endif
59
+
60
+ #define TAG FREERDP_TAG("crypto")
61
+
62
+ /**
63
+ * Earlier Microsoft iOS RDP clients have sent a null or even double null
64
+ * terminated hostname in the SNI TLS extension.
65
+ * If the length indicator does not equal the hostname strlen OpenSSL
66
+ * will abort (see openssl:ssl/t1_lib.c).
67
+ * Here is a tcpdump segment of Microsoft Remote Desktop Client Version
68
+ * 8.1.7 running on an iPhone 4 with iOS 7.1.2 showing the transmitted
69
+ * SNI hostname TLV blob when connection to server "abcd":
70
+ * 00 name_type 0x00 (host_name)
71
+ * 00 06 length_in_bytes 0x0006
72
+ * 61 62 63 64 00 00 host_name "abcd\0\0"
73
+ *
74
+ * Currently the only (runtime) workaround is setting an openssl tls
75
+ * extension debug callback that sets the SSL context's servername_done
76
+ * to 1 which effectively disables the parsing of that extension type.
77
+ *
78
+ * Nowadays this workaround is not required anymore but still can be
79
+ * activated by adding the following define:
80
+ *
81
+ * #define MICROSOFT_IOS_SNI_BUG
82
+ */
83
+
84
+ typedef struct
85
+ {
86
+ SSL* ssl;
87
+ CRITICAL_SECTION lock;
88
+ } BIO_RDP_TLS;
89
+
90
+ static int tls_verify_certificate(rdpTls* tls, const rdpCertificate* cert, const char* hostname,
91
+ UINT16 port);
92
+ static void tls_print_certificate_name_mismatch_error(const char* hostname, UINT16 port,
93
+ const char* common_name, char** alt_names,
94
+ size_t alt_names_count);
95
+ static void tls_print_new_certificate_warn(rdpCertificateStore* store, const char* hostname,
96
+ UINT16 port, const char* fingerprint);
97
+ static void tls_print_certificate_error(rdpCertificateStore* store, rdpCertificateData* stored_data,
98
+ const char* hostname, UINT16 port, const char* fingerprint);
99
+
100
+ static void free_tls_public_key(rdpTls* tls)
101
+ {
102
+ WINPR_ASSERT(tls);
103
+ free(tls->PublicKey);
104
+ tls->PublicKey = NULL;
105
+ tls->PublicKeyLength = 0;
106
+ }
107
+
108
+ static void free_tls_bindings(rdpTls* tls)
109
+ {
110
+ WINPR_ASSERT(tls);
111
+
112
+ if (tls->Bindings)
113
+ free(tls->Bindings->Bindings);
114
+
115
+ free(tls->Bindings);
116
+ tls->Bindings = NULL;
117
+ }
118
+
119
+ static int bio_rdp_tls_write(BIO* bio, const char* buf, int size)
120
+ {
121
+ int error = 0;
122
+ int status = 0;
123
+ BIO_RDP_TLS* tls = (BIO_RDP_TLS*)BIO_get_data(bio);
124
+
125
+ if (!buf || !tls)
126
+ return 0;
127
+
128
+ BIO_clear_flags(bio, BIO_FLAGS_WRITE | BIO_FLAGS_READ | BIO_FLAGS_IO_SPECIAL);
129
+ EnterCriticalSection(&tls->lock);
130
+ status = SSL_write(tls->ssl, buf, size);
131
+ error = SSL_get_error(tls->ssl, status);
132
+ LeaveCriticalSection(&tls->lock);
133
+
134
+ if (status <= 0)
135
+ {
136
+ switch (error)
137
+ {
138
+ case SSL_ERROR_NONE:
139
+ BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY);
140
+ break;
141
+
142
+ case SSL_ERROR_WANT_WRITE:
143
+ BIO_set_flags(bio, BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY);
144
+ break;
145
+
146
+ case SSL_ERROR_WANT_READ:
147
+ BIO_set_flags(bio, BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY);
148
+ break;
149
+
150
+ case SSL_ERROR_WANT_X509_LOOKUP:
151
+ BIO_set_flags(bio, BIO_FLAGS_IO_SPECIAL);
152
+ BIO_set_retry_reason(bio, BIO_RR_SSL_X509_LOOKUP);
153
+ break;
154
+
155
+ case SSL_ERROR_WANT_CONNECT:
156
+ BIO_set_flags(bio, BIO_FLAGS_IO_SPECIAL);
157
+ BIO_set_retry_reason(bio, BIO_RR_CONNECT);
158
+ break;
159
+
160
+ case SSL_ERROR_SYSCALL:
161
+ BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY);
162
+ break;
163
+
164
+ case SSL_ERROR_SSL:
165
+ BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY);
166
+ break;
167
+ default:
168
+ break;
169
+ }
170
+ }
171
+
172
+ return status;
173
+ }
174
+
175
+ static int bio_rdp_tls_read(BIO* bio, char* buf, int size)
176
+ {
177
+ int error = 0;
178
+ int status = 0;
179
+ BIO_RDP_TLS* tls = (BIO_RDP_TLS*)BIO_get_data(bio);
180
+
181
+ if (!buf || !tls)
182
+ return 0;
183
+
184
+ BIO_clear_flags(bio, BIO_FLAGS_WRITE | BIO_FLAGS_READ | BIO_FLAGS_IO_SPECIAL);
185
+ EnterCriticalSection(&tls->lock);
186
+ status = SSL_read(tls->ssl, buf, size);
187
+ error = SSL_get_error(tls->ssl, status);
188
+ LeaveCriticalSection(&tls->lock);
189
+
190
+ if (status <= 0)
191
+ {
192
+
193
+ switch (error)
194
+ {
195
+ case SSL_ERROR_NONE:
196
+ BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY);
197
+ break;
198
+
199
+ case SSL_ERROR_WANT_READ:
200
+ BIO_set_flags(bio, BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY);
201
+ break;
202
+
203
+ case SSL_ERROR_WANT_WRITE:
204
+ BIO_set_flags(bio, BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY);
205
+ break;
206
+
207
+ case SSL_ERROR_WANT_X509_LOOKUP:
208
+ BIO_set_flags(bio, BIO_FLAGS_IO_SPECIAL);
209
+ BIO_set_retry_reason(bio, BIO_RR_SSL_X509_LOOKUP);
210
+ break;
211
+
212
+ case SSL_ERROR_WANT_ACCEPT:
213
+ BIO_set_flags(bio, BIO_FLAGS_IO_SPECIAL);
214
+ BIO_set_retry_reason(bio, BIO_RR_ACCEPT);
215
+ break;
216
+
217
+ case SSL_ERROR_WANT_CONNECT:
218
+ BIO_set_flags(bio, BIO_FLAGS_IO_SPECIAL);
219
+ BIO_set_retry_reason(bio, BIO_RR_CONNECT);
220
+ break;
221
+
222
+ case SSL_ERROR_SSL:
223
+ BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY);
224
+ break;
225
+
226
+ case SSL_ERROR_ZERO_RETURN:
227
+ BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY);
228
+ break;
229
+
230
+ case SSL_ERROR_SYSCALL:
231
+ BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY);
232
+ break;
233
+ default:
234
+ break;
235
+ }
236
+ }
237
+
238
+ #ifdef FREERDP_HAVE_VALGRIND_MEMCHECK_H
239
+
240
+ if (status > 0)
241
+ {
242
+ VALGRIND_MAKE_MEM_DEFINED(buf, status);
243
+ }
244
+
245
+ #endif
246
+ return status;
247
+ }
248
+
249
+ static int bio_rdp_tls_puts(BIO* bio, const char* str)
250
+ {
251
+ if (!str)
252
+ return 0;
253
+
254
+ const size_t size = strnlen(str, INT_MAX + 1UL);
255
+ if (size > INT_MAX)
256
+ return -1;
257
+ ERR_clear_error();
258
+ return BIO_write(bio, str, (int)size);
259
+ }
260
+
261
+ static int bio_rdp_tls_gets(BIO* bio, char* str, int size)
262
+ {
263
+ return 1;
264
+ }
265
+
266
+ static long bio_rdp_tls_ctrl(BIO* bio, int cmd, long num, void* ptr)
267
+ {
268
+ BIO* ssl_rbio = NULL;
269
+ BIO* ssl_wbio = NULL;
270
+ BIO* next_bio = NULL;
271
+ long status = -1;
272
+ BIO_RDP_TLS* tls = (BIO_RDP_TLS*)BIO_get_data(bio);
273
+
274
+ if (!tls)
275
+ return 0;
276
+
277
+ if (!tls->ssl && (cmd != BIO_C_SET_SSL))
278
+ return 0;
279
+
280
+ next_bio = BIO_next(bio);
281
+ ssl_rbio = tls->ssl ? SSL_get_rbio(tls->ssl) : NULL;
282
+ ssl_wbio = tls->ssl ? SSL_get_wbio(tls->ssl) : NULL;
283
+
284
+ switch (cmd)
285
+ {
286
+ case BIO_CTRL_RESET:
287
+ SSL_shutdown(tls->ssl);
288
+
289
+ if (SSL_in_connect_init(tls->ssl))
290
+ SSL_set_connect_state(tls->ssl);
291
+ else if (SSL_in_accept_init(tls->ssl))
292
+ SSL_set_accept_state(tls->ssl);
293
+
294
+ SSL_clear(tls->ssl);
295
+
296
+ if (next_bio)
297
+ status = BIO_ctrl(next_bio, cmd, num, ptr);
298
+ else if (ssl_rbio)
299
+ status = BIO_ctrl(ssl_rbio, cmd, num, ptr);
300
+ else
301
+ status = 1;
302
+
303
+ break;
304
+
305
+ case BIO_C_GET_FD:
306
+ status = BIO_ctrl(ssl_rbio, cmd, num, ptr);
307
+ break;
308
+
309
+ case BIO_CTRL_INFO:
310
+ status = 0;
311
+ break;
312
+
313
+ case BIO_CTRL_SET_CALLBACK:
314
+ status = 0;
315
+ break;
316
+
317
+ case BIO_CTRL_GET_CALLBACK:
318
+ /* The OpenSSL API is horrible here:
319
+ * we get a function pointer returned and have to cast it to ULONG_PTR
320
+ * to return the value to the caller.
321
+ *
322
+ * This, of course, is something compilers warn about. So silence it by casting */
323
+ {
324
+ void* vptr = WINPR_FUNC_PTR_CAST(SSL_get_info_callback(tls->ssl), void*);
325
+ *((void**)ptr) = vptr;
326
+ status = 1;
327
+ }
328
+ break;
329
+
330
+ case BIO_C_SSL_MODE:
331
+ if (num)
332
+ SSL_set_connect_state(tls->ssl);
333
+ else
334
+ SSL_set_accept_state(tls->ssl);
335
+
336
+ status = 1;
337
+ break;
338
+
339
+ case BIO_CTRL_GET_CLOSE:
340
+ status = BIO_get_shutdown(bio);
341
+ break;
342
+
343
+ case BIO_CTRL_SET_CLOSE:
344
+ BIO_set_shutdown(bio, (int)num);
345
+ status = 1;
346
+ break;
347
+
348
+ case BIO_CTRL_WPENDING:
349
+ status = BIO_ctrl(ssl_wbio, cmd, num, ptr);
350
+ break;
351
+
352
+ case BIO_CTRL_PENDING:
353
+ status = SSL_pending(tls->ssl);
354
+
355
+ if (status == 0)
356
+ status = BIO_pending(ssl_rbio);
357
+
358
+ break;
359
+
360
+ case BIO_CTRL_FLUSH:
361
+ BIO_clear_retry_flags(bio);
362
+ status = BIO_ctrl(ssl_wbio, cmd, num, ptr);
363
+ if (status != 1)
364
+ WLog_DBG(TAG, "BIO_ctrl returned %d", status);
365
+ BIO_copy_next_retry(bio);
366
+ status = 1;
367
+ break;
368
+
369
+ case BIO_CTRL_PUSH:
370
+ if (next_bio && (next_bio != ssl_rbio))
371
+ {
372
+ #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
373
+ (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x2070000fL)
374
+ SSL_set_bio(tls->ssl, next_bio, next_bio);
375
+ CRYPTO_add(&(bio->next_bio->references), 1, CRYPTO_LOCK_BIO);
376
+ #else
377
+ /*
378
+ * We are going to pass ownership of next to the SSL object...but
379
+ * we don't own a reference to pass yet - so up ref
380
+ */
381
+ BIO_up_ref(next_bio);
382
+ SSL_set_bio(tls->ssl, next_bio, next_bio);
383
+ #endif
384
+ }
385
+
386
+ status = 1;
387
+ break;
388
+
389
+ case BIO_CTRL_POP:
390
+
391
+ /* Only detach if we are the BIO explicitly being popped */
392
+ if (bio == ptr)
393
+ {
394
+ if (ssl_rbio != ssl_wbio)
395
+ BIO_free_all(ssl_wbio);
396
+
397
+ #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
398
+ (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x2070000fL)
399
+
400
+ if (next_bio)
401
+ CRYPTO_add(&(bio->next_bio->references), -1, CRYPTO_LOCK_BIO);
402
+
403
+ tls->ssl->wbio = tls->ssl->rbio = NULL;
404
+ #else
405
+ /* OpenSSL 1.1: This will also clear the reference we obtained during push */
406
+ SSL_set_bio(tls->ssl, NULL, NULL);
407
+ #endif
408
+ }
409
+
410
+ status = 1;
411
+ break;
412
+
413
+ case BIO_C_GET_SSL:
414
+ if (ptr)
415
+ {
416
+ *((SSL**)ptr) = tls->ssl;
417
+ status = 1;
418
+ }
419
+
420
+ break;
421
+
422
+ case BIO_C_SET_SSL:
423
+ BIO_set_shutdown(bio, (int)num);
424
+
425
+ if (ptr)
426
+ {
427
+ tls->ssl = (SSL*)ptr;
428
+ ssl_rbio = SSL_get_rbio(tls->ssl);
429
+ }
430
+
431
+ if (ssl_rbio)
432
+ {
433
+ if (next_bio)
434
+ BIO_push(ssl_rbio, next_bio);
435
+
436
+ BIO_set_next(bio, ssl_rbio);
437
+ #if OPENSSL_VERSION_NUMBER < 0x10100000L || \
438
+ (defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x2070000fL)
439
+ CRYPTO_add(&(ssl_rbio->references), 1, CRYPTO_LOCK_BIO);
440
+ #else
441
+ BIO_up_ref(ssl_rbio);
442
+ #endif
443
+ }
444
+
445
+ BIO_set_init(bio, 1);
446
+ status = 1;
447
+ break;
448
+
449
+ case BIO_C_DO_STATE_MACHINE:
450
+ BIO_clear_flags(bio, BIO_FLAGS_READ | BIO_FLAGS_WRITE | BIO_FLAGS_IO_SPECIAL);
451
+ BIO_set_retry_reason(bio, 0);
452
+ status = SSL_do_handshake(tls->ssl);
453
+
454
+ if (status <= 0)
455
+ {
456
+ const int err = (status < INT32_MIN) ? INT32_MIN : (int)status;
457
+ switch (SSL_get_error(tls->ssl, err))
458
+ {
459
+ case SSL_ERROR_WANT_READ:
460
+ BIO_set_flags(bio, BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY);
461
+ break;
462
+
463
+ case SSL_ERROR_WANT_WRITE:
464
+ BIO_set_flags(bio, BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY);
465
+ break;
466
+
467
+ case SSL_ERROR_WANT_CONNECT:
468
+ BIO_set_flags(bio, BIO_FLAGS_IO_SPECIAL | BIO_FLAGS_SHOULD_RETRY);
469
+ BIO_set_retry_reason(bio, BIO_get_retry_reason(next_bio));
470
+ break;
471
+
472
+ default:
473
+ BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY);
474
+ break;
475
+ }
476
+ }
477
+
478
+ break;
479
+
480
+ default:
481
+ status = BIO_ctrl(ssl_rbio, cmd, num, ptr);
482
+ break;
483
+ }
484
+
485
+ return status;
486
+ }
487
+
488
+ static int bio_rdp_tls_new(BIO* bio)
489
+ {
490
+ BIO_RDP_TLS* tls = NULL;
491
+ BIO_set_flags(bio, BIO_FLAGS_SHOULD_RETRY);
492
+
493
+ if (!(tls = calloc(1, sizeof(BIO_RDP_TLS))))
494
+ return 0;
495
+
496
+ InitializeCriticalSectionAndSpinCount(&tls->lock, 4000);
497
+ BIO_set_data(bio, (void*)tls);
498
+ return 1;
499
+ }
500
+
501
+ static int bio_rdp_tls_free(BIO* bio)
502
+ {
503
+ BIO_RDP_TLS* tls = NULL;
504
+
505
+ if (!bio)
506
+ return 0;
507
+
508
+ tls = (BIO_RDP_TLS*)BIO_get_data(bio);
509
+
510
+ if (!tls)
511
+ return 0;
512
+
513
+ BIO_set_data(bio, NULL);
514
+ if (BIO_get_shutdown(bio))
515
+ {
516
+ if (BIO_get_init(bio) && tls->ssl)
517
+ {
518
+ SSL_shutdown(tls->ssl);
519
+ SSL_free(tls->ssl);
520
+ }
521
+
522
+ BIO_set_init(bio, 0);
523
+ BIO_set_flags(bio, 0);
524
+ }
525
+
526
+ DeleteCriticalSection(&tls->lock);
527
+ free(tls);
528
+
529
+ return 1;
530
+ }
531
+
532
+ static long bio_rdp_tls_callback_ctrl(BIO* bio, int cmd, bio_info_cb* fp)
533
+ {
534
+ long status = 0;
535
+
536
+ if (!bio)
537
+ return 0;
538
+
539
+ BIO_RDP_TLS* tls = (BIO_RDP_TLS*)BIO_get_data(bio);
540
+
541
+ if (!tls)
542
+ return 0;
543
+
544
+ switch (cmd)
545
+ {
546
+ case BIO_CTRL_SET_CALLBACK:
547
+ {
548
+ typedef void (*fkt_t)(const SSL*, int, int);
549
+
550
+ /* Documented since https://www.openssl.org/docs/man1.1.1/man3/BIO_set_callback.html
551
+ * the argument is not really of type bio_info_cb* and must be cast
552
+ * to the required type */
553
+
554
+ fkt_t fkt = WINPR_FUNC_PTR_CAST(fp, fkt_t);
555
+ SSL_set_info_callback(tls->ssl, fkt);
556
+ status = 1;
557
+ }
558
+ break;
559
+
560
+ default:
561
+ status = BIO_callback_ctrl(SSL_get_rbio(tls->ssl), cmd, fp);
562
+ break;
563
+ }
564
+
565
+ return status;
566
+ }
567
+
568
+ #define BIO_TYPE_RDP_TLS 68
569
+
570
+ static BIO_METHOD* BIO_s_rdp_tls(void)
571
+ {
572
+ static BIO_METHOD* bio_methods = NULL;
573
+
574
+ if (bio_methods == NULL)
575
+ {
576
+ if (!(bio_methods = BIO_meth_new(BIO_TYPE_RDP_TLS, "RdpTls")))
577
+ return NULL;
578
+
579
+ BIO_meth_set_write(bio_methods, bio_rdp_tls_write);
580
+ BIO_meth_set_read(bio_methods, bio_rdp_tls_read);
581
+ BIO_meth_set_puts(bio_methods, bio_rdp_tls_puts);
582
+ BIO_meth_set_gets(bio_methods, bio_rdp_tls_gets);
583
+ BIO_meth_set_ctrl(bio_methods, bio_rdp_tls_ctrl);
584
+ BIO_meth_set_create(bio_methods, bio_rdp_tls_new);
585
+ BIO_meth_set_destroy(bio_methods, bio_rdp_tls_free);
586
+ BIO_meth_set_callback_ctrl(bio_methods, bio_rdp_tls_callback_ctrl);
587
+ }
588
+
589
+ return bio_methods;
590
+ }
591
+
592
+ static BIO* BIO_new_rdp_tls(SSL_CTX* ctx, int client)
593
+ {
594
+ BIO* bio = NULL;
595
+ SSL* ssl = NULL;
596
+ bio = BIO_new(BIO_s_rdp_tls());
597
+
598
+ if (!bio)
599
+ return NULL;
600
+
601
+ ssl = SSL_new(ctx);
602
+
603
+ if (!ssl)
604
+ {
605
+ BIO_free_all(bio);
606
+ return NULL;
607
+ }
608
+
609
+ if (client)
610
+ SSL_set_connect_state(ssl);
611
+ else
612
+ SSL_set_accept_state(ssl);
613
+
614
+ BIO_set_ssl(bio, ssl, BIO_CLOSE);
615
+ return bio;
616
+ }
617
+
618
+ static rdpCertificate* tls_get_certificate(rdpTls* tls, BOOL peer)
619
+ {
620
+ X509* remote_cert = NULL;
621
+
622
+ if (peer)
623
+ remote_cert = SSL_get_peer_certificate(tls->ssl);
624
+ else
625
+ remote_cert = X509_dup(SSL_get_certificate(tls->ssl));
626
+
627
+ if (!remote_cert)
628
+ {
629
+ WLog_ERR(TAG, "failed to get the server TLS certificate");
630
+ return NULL;
631
+ }
632
+
633
+ /* Get the peer's chain. If it does not exist, we're setting NULL (clean data either way) */
634
+ STACK_OF(X509)* chain = SSL_get_peer_cert_chain(tls->ssl);
635
+ rdpCertificate* cert = freerdp_certificate_new_from_x509(remote_cert, chain);
636
+ X509_free(remote_cert);
637
+
638
+ return cert;
639
+ }
640
+
641
+ static const char* tls_get_server_name(rdpTls* tls)
642
+ {
643
+ return tls->serverName ? tls->serverName : tls->hostname;
644
+ }
645
+
646
+ #define TLS_SERVER_END_POINT "tls-server-end-point:"
647
+
648
+ static SecPkgContext_Bindings* tls_get_channel_bindings(const rdpCertificate* cert)
649
+ {
650
+ size_t CertificateHashLength = 0;
651
+ BYTE* ChannelBindingToken = NULL;
652
+ SEC_CHANNEL_BINDINGS* ChannelBindings = NULL;
653
+ const size_t PrefixLength = strnlen(TLS_SERVER_END_POINT, ARRAYSIZE(TLS_SERVER_END_POINT));
654
+
655
+ WINPR_ASSERT(cert);
656
+
657
+ /* See https://www.rfc-editor.org/rfc/rfc5929 for details about hashes */
658
+ WINPR_MD_TYPE alg = freerdp_certificate_get_signature_alg(cert);
659
+ const char* hash = NULL;
660
+ switch (alg)
661
+ {
662
+
663
+ case WINPR_MD_MD5:
664
+ case WINPR_MD_SHA1:
665
+ hash = winpr_md_type_to_string(WINPR_MD_SHA256);
666
+ break;
667
+ default:
668
+ hash = winpr_md_type_to_string(alg);
669
+ break;
670
+ }
671
+ if (!hash)
672
+ return NULL;
673
+
674
+ char* CertificateHash = freerdp_certificate_get_hash(cert, hash, &CertificateHashLength);
675
+ if (!CertificateHash)
676
+ return NULL;
677
+
678
+ const size_t ChannelBindingTokenLength = PrefixLength + CertificateHashLength;
679
+ SecPkgContext_Bindings* ContextBindings = calloc(1, sizeof(SecPkgContext_Bindings));
680
+
681
+ if (!ContextBindings)
682
+ goto out_free;
683
+
684
+ const size_t slen = sizeof(SEC_CHANNEL_BINDINGS) + ChannelBindingTokenLength;
685
+ if (slen > UINT32_MAX)
686
+ goto out_free;
687
+
688
+ ContextBindings->BindingsLength = (UINT32)slen;
689
+ ChannelBindings = (SEC_CHANNEL_BINDINGS*)calloc(1, ContextBindings->BindingsLength);
690
+
691
+ if (!ChannelBindings)
692
+ goto out_free;
693
+
694
+ ContextBindings->Bindings = ChannelBindings;
695
+ ChannelBindings->cbApplicationDataLength = (UINT32)ChannelBindingTokenLength;
696
+ ChannelBindings->dwApplicationDataOffset = sizeof(SEC_CHANNEL_BINDINGS);
697
+ ChannelBindingToken = &((BYTE*)ChannelBindings)[ChannelBindings->dwApplicationDataOffset];
698
+ memcpy(ChannelBindingToken, TLS_SERVER_END_POINT, PrefixLength);
699
+ memcpy(ChannelBindingToken + PrefixLength, CertificateHash, CertificateHashLength);
700
+ free(CertificateHash);
701
+ return ContextBindings;
702
+ out_free:
703
+ free(CertificateHash);
704
+ free(ContextBindings);
705
+ return NULL;
706
+ }
707
+
708
+ static INIT_ONCE secrets_file_idx_once = INIT_ONCE_STATIC_INIT;
709
+ static int secrets_file_idx = -1;
710
+
711
+ static BOOL CALLBACK secrets_file_init_cb(PINIT_ONCE once, PVOID param, PVOID* context)
712
+ {
713
+ secrets_file_idx = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL);
714
+
715
+ return (secrets_file_idx != -1);
716
+ }
717
+
718
+ static void SSLCTX_keylog_cb(const SSL* ssl, const char* line)
719
+ {
720
+ char* dfile = NULL;
721
+
722
+ if (secrets_file_idx == -1)
723
+ return;
724
+
725
+ dfile = SSL_get_ex_data(ssl, secrets_file_idx);
726
+ if (dfile)
727
+ {
728
+ FILE* f = winpr_fopen(dfile, "a+");
729
+ if (f)
730
+ {
731
+ (void)fwrite(line, strlen(line), 1, f);
732
+ (void)fwrite("\n", 1, 1, f);
733
+ (void)fclose(f);
734
+ }
735
+ }
736
+ }
737
+
738
+ static void tls_reset(rdpTls* tls)
739
+ {
740
+ WINPR_ASSERT(tls);
741
+
742
+ if (tls->ctx)
743
+ {
744
+ SSL_CTX_free(tls->ctx);
745
+ tls->ctx = NULL;
746
+ }
747
+
748
+ /* tls->underlying is a stacked BIO under tls->bio.
749
+ * BIO_free_all will free recursively. */
750
+ if (tls->bio)
751
+ BIO_free_all(tls->bio);
752
+ else if (tls->underlying)
753
+ BIO_free_all(tls->underlying);
754
+ tls->bio = NULL;
755
+ tls->underlying = NULL;
756
+
757
+ free_tls_public_key(tls);
758
+ free_tls_bindings(tls);
759
+ }
760
+
761
+ #if OPENSSL_VERSION_NUMBER >= 0x010000000L
762
+ static BOOL tls_prepare(rdpTls* tls, BIO* underlying, const SSL_METHOD* method, int options,
763
+ BOOL clientMode)
764
+ #else
765
+ static BOOL tls_prepare(rdpTls* tls, BIO* underlying, SSL_METHOD* method, int options,
766
+ BOOL clientMode)
767
+ #endif
768
+ {
769
+ WINPR_ASSERT(tls);
770
+
771
+ rdpSettings* settings = tls->context->settings;
772
+ WINPR_ASSERT(settings);
773
+
774
+ tls_reset(tls);
775
+ tls->ctx = SSL_CTX_new(method);
776
+
777
+ tls->underlying = underlying;
778
+
779
+ if (!tls->ctx)
780
+ {
781
+ WLog_ERR(TAG, "SSL_CTX_new failed");
782
+ return FALSE;
783
+ }
784
+
785
+ SSL_CTX_set_mode(tls->ctx, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | SSL_MODE_ENABLE_PARTIAL_WRITE);
786
+ SSL_CTX_set_options(tls->ctx, WINPR_ASSERTING_INT_CAST(uint64_t, options));
787
+ SSL_CTX_set_read_ahead(tls->ctx, 1);
788
+ #if OPENSSL_VERSION_NUMBER >= 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
789
+ UINT16 version = freerdp_settings_get_uint16(settings, FreeRDP_TLSMinVersion);
790
+ if (!SSL_CTX_set_min_proto_version(tls->ctx, version))
791
+ {
792
+ WLog_ERR(TAG, "SSL_CTX_set_min_proto_version %s failed", version);
793
+ return FALSE;
794
+ }
795
+ version = freerdp_settings_get_uint16(settings, FreeRDP_TLSMaxVersion);
796
+ if (!SSL_CTX_set_max_proto_version(tls->ctx, version))
797
+ {
798
+ WLog_ERR(TAG, "SSL_CTX_set_max_proto_version %s failed", version);
799
+ return FALSE;
800
+ }
801
+ #endif
802
+ #if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
803
+ SSL_CTX_set_security_level(tls->ctx, WINPR_ASSERTING_INT_CAST(int, settings->TlsSecLevel));
804
+ #endif
805
+
806
+ if (settings->AllowedTlsCiphers)
807
+ {
808
+ if (!SSL_CTX_set_cipher_list(tls->ctx, settings->AllowedTlsCiphers))
809
+ {
810
+ WLog_ERR(TAG, "SSL_CTX_set_cipher_list %s failed", settings->AllowedTlsCiphers);
811
+ return FALSE;
812
+ }
813
+ }
814
+
815
+ tls->bio = BIO_new_rdp_tls(tls->ctx, clientMode);
816
+
817
+ if (BIO_get_ssl(tls->bio, &tls->ssl) < 0)
818
+ {
819
+ WLog_ERR(TAG, "unable to retrieve the SSL of the connection");
820
+ return FALSE;
821
+ }
822
+
823
+ if (settings->TlsSecretsFile)
824
+ {
825
+ #if OPENSSL_VERSION_NUMBER >= 0x10101000L
826
+ InitOnceExecuteOnce(&secrets_file_idx_once, secrets_file_init_cb, NULL, NULL);
827
+
828
+ if (secrets_file_idx != -1)
829
+ {
830
+ SSL_set_ex_data(tls->ssl, secrets_file_idx, settings->TlsSecretsFile);
831
+ SSL_CTX_set_keylog_callback(tls->ctx, SSLCTX_keylog_cb);
832
+ }
833
+ #else
834
+ WLog_WARN(TAG, "Key-Logging not available - requires OpenSSL 1.1.1 or higher");
835
+ #endif
836
+ }
837
+
838
+ BIO_push(tls->bio, underlying);
839
+ return TRUE;
840
+ }
841
+
842
+ static void adjustSslOptions(int* options) // NOLINT(readability-non-const-parameter)
843
+ {
844
+ WINPR_ASSERT(options);
845
+ #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
846
+ *options |= SSL_OP_NO_SSLv2;
847
+ *options |= SSL_OP_NO_SSLv3;
848
+ #endif
849
+ }
850
+
851
+ const SSL_METHOD* freerdp_tls_get_ssl_method(BOOL isDtls, BOOL isClient)
852
+ {
853
+ if (isClient)
854
+ {
855
+ if (isDtls)
856
+ return DTLS_client_method();
857
+ #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
858
+ return SSLv23_client_method();
859
+ #else
860
+ return TLS_client_method();
861
+ #endif
862
+ }
863
+
864
+ if (isDtls)
865
+ return DTLS_server_method();
866
+
867
+ #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
868
+ return SSLv23_server_method();
869
+ #else
870
+ return TLS_server_method();
871
+ #endif
872
+ }
873
+
874
+ TlsHandshakeResult freerdp_tls_connect_ex(rdpTls* tls, BIO* underlying, const SSL_METHOD* methods)
875
+ {
876
+ WINPR_ASSERT(tls);
877
+
878
+ int options = 0;
879
+ /**
880
+ * SSL_OP_NO_COMPRESSION:
881
+ *
882
+ * The Microsoft RDP server does not advertise support
883
+ * for TLS compression, but alternative servers may support it.
884
+ * This was observed between early versions of the FreeRDP server
885
+ * and the FreeRDP client, and caused major performance issues,
886
+ * which is why we're disabling it.
887
+ */
888
+ #ifdef SSL_OP_NO_COMPRESSION
889
+ options |= SSL_OP_NO_COMPRESSION;
890
+ #endif
891
+ /**
892
+ * SSL_OP_TLS_BLOCK_PADDING_BUG:
893
+ *
894
+ * The Microsoft RDP server does *not* support TLS padding.
895
+ * It absolutely needs to be disabled otherwise it won't work.
896
+ */
897
+ options |= SSL_OP_TLS_BLOCK_PADDING_BUG;
898
+ /**
899
+ * SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS:
900
+ *
901
+ * Just like TLS padding, the Microsoft RDP server does not
902
+ * support empty fragments. This needs to be disabled.
903
+ */
904
+ options |= SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS;
905
+
906
+ tls->isClientMode = TRUE;
907
+ adjustSslOptions(&options);
908
+
909
+ if (!tls_prepare(tls, underlying, methods, options, TRUE))
910
+ return 0;
911
+
912
+ #if !defined(OPENSSL_NO_TLSEXT) && !defined(LIBRESSL_VERSION_NUMBER)
913
+ const char* str = tls_get_server_name(tls);
914
+ void* ptr = WINPR_CAST_CONST_PTR_AWAY(str, void*);
915
+ SSL_set_tlsext_host_name(tls->ssl, ptr);
916
+ #endif
917
+
918
+ return freerdp_tls_handshake(tls);
919
+ }
920
+
921
+ static int bio_err_print(const char* str, size_t len, void* u)
922
+ {
923
+ wLog* log = u;
924
+ WLog_Print(log, WLOG_ERROR, "[BIO_do_handshake] %s [%" PRIuz "]", str, len);
925
+ return 0;
926
+ }
927
+
928
+ TlsHandshakeResult freerdp_tls_handshake(rdpTls* tls)
929
+ {
930
+ TlsHandshakeResult ret = TLS_HANDSHAKE_ERROR;
931
+
932
+ WINPR_ASSERT(tls);
933
+ const long status = BIO_do_handshake(tls->bio);
934
+ if (status != 1)
935
+ {
936
+ if (!BIO_should_retry(tls->bio))
937
+ {
938
+ wLog* log = WLog_Get(TAG);
939
+ WLog_Print(log, WLOG_ERROR, "BIO_do_handshake failed");
940
+ ERR_print_errors_cb(bio_err_print, log);
941
+ return TLS_HANDSHAKE_ERROR;
942
+ }
943
+
944
+ return TLS_HANDSHAKE_CONTINUE;
945
+ }
946
+
947
+ int verify_status = 0;
948
+ rdpCertificate* cert = tls_get_certificate(tls, tls->isClientMode);
949
+
950
+ if (!cert)
951
+ {
952
+ WLog_ERR(TAG, "tls_get_certificate failed to return the server certificate.");
953
+ return TLS_HANDSHAKE_ERROR;
954
+ }
955
+
956
+ do
957
+ {
958
+ free_tls_bindings(tls);
959
+ tls->Bindings = tls_get_channel_bindings(cert);
960
+ if (!tls->Bindings)
961
+ {
962
+ WLog_ERR(TAG, "unable to retrieve bindings");
963
+ break;
964
+ }
965
+
966
+ free_tls_public_key(tls);
967
+ if (!freerdp_certificate_get_public_key(cert, &tls->PublicKey, &tls->PublicKeyLength))
968
+ {
969
+ WLog_ERR(TAG,
970
+ "freerdp_certificate_get_public_key failed to return the server public key.");
971
+ break;
972
+ }
973
+
974
+ /* server-side NLA needs public keys (keys from us, the server) but no certificate verify */
975
+ ret = TLS_HANDSHAKE_SUCCESS;
976
+
977
+ if (tls->isClientMode)
978
+ {
979
+ WINPR_ASSERT(tls->port <= UINT16_MAX);
980
+ verify_status =
981
+ tls_verify_certificate(tls, cert, tls_get_server_name(tls), (UINT16)tls->port);
982
+
983
+ if (verify_status < 1)
984
+ {
985
+ WLog_ERR(TAG, "certificate not trusted, aborting.");
986
+ freerdp_tls_send_alert(tls);
987
+ ret = TLS_HANDSHAKE_VERIFY_ERROR;
988
+ }
989
+ }
990
+ } while (0);
991
+
992
+ freerdp_certificate_free(cert);
993
+ return ret;
994
+ }
995
+
996
+ static int pollAndHandshake(rdpTls* tls)
997
+ {
998
+ WINPR_ASSERT(tls);
999
+
1000
+ do
1001
+ {
1002
+ HANDLE event = NULL;
1003
+ DWORD status = 0;
1004
+ if (BIO_get_event(tls->bio, &event) < 0)
1005
+ {
1006
+ WLog_ERR(TAG, "unable to retrieve BIO associated event");
1007
+ return -1;
1008
+ }
1009
+
1010
+ if (!event)
1011
+ {
1012
+ WLog_ERR(TAG, "unable to retrieve BIO event");
1013
+ return -1;
1014
+ }
1015
+
1016
+ status = WaitForSingleObjectEx(event, 50, TRUE);
1017
+ switch (status)
1018
+ {
1019
+ case WAIT_OBJECT_0:
1020
+ break;
1021
+ case WAIT_TIMEOUT:
1022
+ case WAIT_IO_COMPLETION:
1023
+ continue;
1024
+ default:
1025
+ WLog_ERR(TAG, "error during WaitForSingleObject(): 0x%08" PRIX32 "", status);
1026
+ return -1;
1027
+ }
1028
+
1029
+ TlsHandshakeResult result = freerdp_tls_handshake(tls);
1030
+ switch (result)
1031
+ {
1032
+ case TLS_HANDSHAKE_CONTINUE:
1033
+ break;
1034
+ case TLS_HANDSHAKE_SUCCESS:
1035
+ return 1;
1036
+ case TLS_HANDSHAKE_ERROR:
1037
+ case TLS_HANDSHAKE_VERIFY_ERROR:
1038
+ default:
1039
+ return -1;
1040
+ }
1041
+ } while (TRUE);
1042
+ }
1043
+
1044
+ int freerdp_tls_connect(rdpTls* tls, BIO* underlying)
1045
+ {
1046
+ const SSL_METHOD* method = freerdp_tls_get_ssl_method(FALSE, TRUE);
1047
+
1048
+ WINPR_ASSERT(tls);
1049
+ TlsHandshakeResult result = freerdp_tls_connect_ex(tls, underlying, method);
1050
+ switch (result)
1051
+ {
1052
+ case TLS_HANDSHAKE_SUCCESS:
1053
+ return 1;
1054
+ case TLS_HANDSHAKE_CONTINUE:
1055
+ break;
1056
+ case TLS_HANDSHAKE_ERROR:
1057
+ case TLS_HANDSHAKE_VERIFY_ERROR:
1058
+ return -1;
1059
+ default:
1060
+ return -1;
1061
+ }
1062
+
1063
+ return pollAndHandshake(tls);
1064
+ }
1065
+
1066
+ #if defined(MICROSOFT_IOS_SNI_BUG) && !defined(OPENSSL_NO_TLSEXT) && \
1067
+ !defined(LIBRESSL_VERSION_NUMBER)
1068
+ static void tls_openssl_tlsext_debug_callback(SSL* s, int client_server, int type,
1069
+ unsigned char* data, int len, void* arg)
1070
+ {
1071
+ if (type == TLSEXT_TYPE_server_name)
1072
+ {
1073
+ WLog_DBG(TAG, "Client uses SNI (extension disabled)");
1074
+ s->servername_done = 2;
1075
+ }
1076
+ }
1077
+ #endif
1078
+
1079
+ BOOL freerdp_tls_accept(rdpTls* tls, BIO* underlying, rdpSettings* settings)
1080
+ {
1081
+ WINPR_ASSERT(tls);
1082
+ TlsHandshakeResult res =
1083
+ freerdp_tls_accept_ex(tls, underlying, settings, freerdp_tls_get_ssl_method(FALSE, FALSE));
1084
+ switch (res)
1085
+ {
1086
+ case TLS_HANDSHAKE_SUCCESS:
1087
+ return TRUE;
1088
+ case TLS_HANDSHAKE_CONTINUE:
1089
+ break;
1090
+ case TLS_HANDSHAKE_ERROR:
1091
+ case TLS_HANDSHAKE_VERIFY_ERROR:
1092
+ default:
1093
+ return FALSE;
1094
+ }
1095
+
1096
+ return pollAndHandshake(tls) > 0;
1097
+ }
1098
+
1099
+ TlsHandshakeResult freerdp_tls_accept_ex(rdpTls* tls, BIO* underlying, rdpSettings* settings,
1100
+ const SSL_METHOD* methods)
1101
+ {
1102
+ WINPR_ASSERT(tls);
1103
+
1104
+ int options = 0;
1105
+ int status = 0;
1106
+
1107
+ /**
1108
+ * SSL_OP_NO_SSLv2:
1109
+ *
1110
+ * We only want SSLv3 and TLSv1, so disable SSLv2.
1111
+ * SSLv3 is used by, eg. Microsoft RDC for Mac OS X.
1112
+ */
1113
+ options |= SSL_OP_NO_SSLv2;
1114
+ /**
1115
+ * SSL_OP_NO_COMPRESSION:
1116
+ *
1117
+ * The Microsoft RDP server does not advertise support
1118
+ * for TLS compression, but alternative servers may support it.
1119
+ * This was observed between early versions of the FreeRDP server
1120
+ * and the FreeRDP client, and caused major performance issues,
1121
+ * which is why we're disabling it.
1122
+ */
1123
+ #ifdef SSL_OP_NO_COMPRESSION
1124
+ options |= SSL_OP_NO_COMPRESSION;
1125
+ #endif
1126
+ /**
1127
+ * SSL_OP_TLS_BLOCK_PADDING_BUG:
1128
+ *
1129
+ * The Microsoft RDP server does *not* support TLS padding.
1130
+ * It absolutely needs to be disabled otherwise it won't work.
1131
+ */
1132
+ options |= SSL_OP_TLS_BLOCK_PADDING_BUG;
1133
+ /**
1134
+ * SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS:
1135
+ *
1136
+ * Just like TLS padding, the Microsoft RDP server does not
1137
+ * support empty fragments. This needs to be disabled.
1138
+ */
1139
+ options |= SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS;
1140
+
1141
+ /**
1142
+ * SSL_OP_NO_RENEGOTIATION
1143
+ *
1144
+ * Disable SSL client site renegotiation.
1145
+ */
1146
+
1147
+ #if (OPENSSL_VERSION_NUMBER >= 0x10101000L) && (OPENSSL_VERSION_NUMBER < 0x30000000L) && \
1148
+ !defined(LIBRESSL_VERSION_NUMBER)
1149
+ options |= SSL_OP_NO_RENEGOTIATION;
1150
+ #endif
1151
+
1152
+ if (!tls_prepare(tls, underlying, methods, options, FALSE))
1153
+ return TLS_HANDSHAKE_ERROR;
1154
+
1155
+ const rdpPrivateKey* key = freerdp_settings_get_pointer(settings, FreeRDP_RdpServerRsaKey);
1156
+ if (!key)
1157
+ {
1158
+ WLog_ERR(TAG, "invalid private key");
1159
+ return TLS_HANDSHAKE_ERROR;
1160
+ }
1161
+
1162
+ EVP_PKEY* privkey = freerdp_key_get_evp_pkey(key);
1163
+ if (!privkey)
1164
+ {
1165
+ WLog_ERR(TAG, "invalid private key");
1166
+ return TLS_HANDSHAKE_ERROR;
1167
+ }
1168
+
1169
+ status = SSL_use_PrivateKey(tls->ssl, privkey);
1170
+ /* The local reference to the private key will anyway go out of
1171
+ * scope; so the reference count should be decremented weither
1172
+ * SSL_use_PrivateKey succeeds or fails.
1173
+ */
1174
+ EVP_PKEY_free(privkey);
1175
+
1176
+ if (status <= 0)
1177
+ {
1178
+ WLog_ERR(TAG, "SSL_CTX_use_PrivateKey_file failed");
1179
+ return TLS_HANDSHAKE_ERROR;
1180
+ }
1181
+
1182
+ rdpCertificate* cert =
1183
+ freerdp_settings_get_pointer_writable(settings, FreeRDP_RdpServerCertificate);
1184
+ if (!cert)
1185
+ {
1186
+ WLog_ERR(TAG, "invalid certificate");
1187
+ return TLS_HANDSHAKE_ERROR;
1188
+ }
1189
+
1190
+ status = SSL_use_certificate(tls->ssl, freerdp_certificate_get_x509(cert));
1191
+
1192
+ if (status <= 0)
1193
+ {
1194
+ WLog_ERR(TAG, "SSL_use_certificate_file failed");
1195
+ return TLS_HANDSHAKE_ERROR;
1196
+ }
1197
+
1198
+ #if defined(MICROSOFT_IOS_SNI_BUG) && !defined(OPENSSL_NO_TLSEXT) && \
1199
+ !defined(LIBRESSL_VERSION_NUMBER)
1200
+ SSL_set_tlsext_debug_callback(tls->ssl, tls_openssl_tlsext_debug_callback);
1201
+ #endif
1202
+
1203
+ return freerdp_tls_handshake(tls);
1204
+ }
1205
+
1206
+ BOOL freerdp_tls_send_alert(rdpTls* tls)
1207
+ {
1208
+ WINPR_ASSERT(tls);
1209
+
1210
+ if (!tls)
1211
+ return FALSE;
1212
+
1213
+ if (!tls->ssl)
1214
+ return TRUE;
1215
+
1216
+ /**
1217
+ * FIXME: The following code does not work on OpenSSL > 1.1.0 because the
1218
+ * SSL struct is opaqe now
1219
+ */
1220
+ #if (!defined(LIBRESSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER < 0x10100000L)) || \
1221
+ (defined(LIBRESSL_VERSION_NUMBER) && (LIBRESSL_VERSION_NUMBER <= 0x2080300fL))
1222
+
1223
+ if (tls->alertDescription != TLS_ALERT_DESCRIPTION_CLOSE_NOTIFY)
1224
+ {
1225
+ /**
1226
+ * OpenSSL doesn't really expose an API for sending a TLS alert manually.
1227
+ *
1228
+ * The following code disables the sending of the default "close notify"
1229
+ * and then proceeds to force sending a custom TLS alert before shutting down.
1230
+ *
1231
+ * Manually sending a TLS alert is necessary in certain cases,
1232
+ * like when server-side NLA results in an authentication failure.
1233
+ */
1234
+ SSL_SESSION* ssl_session = SSL_get_session(tls->ssl);
1235
+ SSL_CTX* ssl_ctx = SSL_get_SSL_CTX(tls->ssl);
1236
+ SSL_set_quiet_shutdown(tls->ssl, 1);
1237
+
1238
+ if ((tls->alertLevel == TLS_ALERT_LEVEL_FATAL) && (ssl_session))
1239
+ SSL_CTX_remove_session(ssl_ctx, ssl_session);
1240
+
1241
+ tls->ssl->s3->alert_dispatch = 1;
1242
+ tls->ssl->s3->send_alert[0] = tls->alertLevel;
1243
+ tls->ssl->s3->send_alert[1] = tls->alertDescription;
1244
+
1245
+ if (tls->ssl->s3->wbuf.left == 0)
1246
+ tls->ssl->method->ssl_dispatch_alert(tls->ssl);
1247
+ }
1248
+
1249
+ #endif
1250
+ return TRUE;
1251
+ }
1252
+
1253
+ int freerdp_tls_write_all(rdpTls* tls, const BYTE* data, size_t length)
1254
+ {
1255
+ WINPR_ASSERT(tls);
1256
+ size_t offset = 0;
1257
+ BIO* bio = tls->bio;
1258
+
1259
+ if (length > INT32_MAX)
1260
+ return -1;
1261
+
1262
+ while (offset < length)
1263
+ {
1264
+ ERR_clear_error();
1265
+ const int status = BIO_write(bio, &data[offset], (int)(length - offset));
1266
+
1267
+ if (status > 0)
1268
+ offset += (size_t)status;
1269
+ else
1270
+ {
1271
+ if (!BIO_should_retry(bio))
1272
+ return -1;
1273
+
1274
+ if (BIO_write_blocked(bio))
1275
+ {
1276
+ const long rc = BIO_wait_write(bio, 100);
1277
+ if (rc < 0)
1278
+ return -1;
1279
+ }
1280
+ else if (BIO_read_blocked(bio))
1281
+ return -2; /* Abort write, there is data that must be read */
1282
+ else
1283
+ USleep(100);
1284
+ }
1285
+ }
1286
+
1287
+ return (int)length;
1288
+ }
1289
+
1290
+ int freerdp_tls_set_alert_code(rdpTls* tls, int level, int description)
1291
+ {
1292
+ WINPR_ASSERT(tls);
1293
+ tls->alertLevel = level;
1294
+ tls->alertDescription = description;
1295
+ return 0;
1296
+ }
1297
+
1298
+ static BOOL tls_match_hostname(const char* pattern, const size_t pattern_length,
1299
+ const char* hostname)
1300
+ {
1301
+ if (strlen(hostname) == pattern_length)
1302
+ {
1303
+ if (_strnicmp(hostname, pattern, pattern_length) == 0)
1304
+ return TRUE;
1305
+ }
1306
+
1307
+ if ((pattern_length > 2) && (pattern[0] == '*') && (pattern[1] == '.') &&
1308
+ ((strlen(hostname)) >= pattern_length))
1309
+ {
1310
+ const char* check_hostname = &hostname[strlen(hostname) - pattern_length + 1];
1311
+
1312
+ if (_strnicmp(check_hostname, &pattern[1], pattern_length - 1) == 0)
1313
+ {
1314
+ return TRUE;
1315
+ }
1316
+ }
1317
+
1318
+ return FALSE;
1319
+ }
1320
+
1321
+ static BOOL is_redirected(rdpTls* tls)
1322
+ {
1323
+ rdpSettings* settings = tls->context->settings;
1324
+
1325
+ if (LB_NOREDIRECT & settings->RedirectionFlags)
1326
+ return FALSE;
1327
+
1328
+ return settings->RedirectionFlags != 0;
1329
+ }
1330
+
1331
+ static BOOL is_accepted(rdpTls* tls, const rdpCertificate* cert)
1332
+ {
1333
+ WINPR_ASSERT(tls);
1334
+ WINPR_ASSERT(tls->context);
1335
+ WINPR_ASSERT(cert);
1336
+ rdpSettings* settings = tls->context->settings;
1337
+ WINPR_ASSERT(settings);
1338
+
1339
+ FreeRDP_Settings_Keys_String keyAccepted = FreeRDP_AcceptedCert;
1340
+ FreeRDP_Settings_Keys_UInt32 keyLength = FreeRDP_AcceptedCertLength;
1341
+
1342
+ if (tls->isGatewayTransport)
1343
+ {
1344
+ keyAccepted = FreeRDP_GatewayAcceptedCert;
1345
+ keyLength = FreeRDP_GatewayAcceptedCertLength;
1346
+ }
1347
+ else if (is_redirected(tls))
1348
+ {
1349
+ keyAccepted = FreeRDP_RedirectionAcceptedCert;
1350
+ keyLength = FreeRDP_RedirectionAcceptedCertLength;
1351
+ }
1352
+
1353
+ const char* AcceptedKey = freerdp_settings_get_string(settings, keyAccepted);
1354
+ const UINT32 AcceptedKeyLength = freerdp_settings_get_uint32(settings, keyLength);
1355
+
1356
+ if ((AcceptedKeyLength > 0) && AcceptedKey)
1357
+ {
1358
+ BOOL accepted = FALSE;
1359
+ size_t pemLength = 0;
1360
+ char* pem = freerdp_certificate_get_pem_ex(cert, &pemLength, FALSE);
1361
+ if (pem && (AcceptedKeyLength == pemLength))
1362
+ {
1363
+ if (memcmp(AcceptedKey, pem, AcceptedKeyLength) == 0)
1364
+ accepted = TRUE;
1365
+ }
1366
+ free(pem);
1367
+ if (accepted)
1368
+ return TRUE;
1369
+ }
1370
+
1371
+ (void)freerdp_settings_set_string(settings, keyAccepted, NULL);
1372
+ (void)freerdp_settings_set_uint32(settings, keyLength, 0);
1373
+
1374
+ return FALSE;
1375
+ }
1376
+
1377
+ static BOOL compare_fingerprint(const char* fp, const char* hash, const rdpCertificate* cert,
1378
+ BOOL separator)
1379
+ {
1380
+ BOOL equal = 0;
1381
+ char* strhash = NULL;
1382
+
1383
+ WINPR_ASSERT(fp);
1384
+ WINPR_ASSERT(hash);
1385
+ WINPR_ASSERT(cert);
1386
+
1387
+ strhash = freerdp_certificate_get_fingerprint_by_hash_ex(cert, hash, separator);
1388
+ if (!strhash)
1389
+ return FALSE;
1390
+
1391
+ equal = (_stricmp(strhash, fp) == 0);
1392
+ free(strhash);
1393
+ return equal;
1394
+ }
1395
+
1396
+ static BOOL compare_fingerprint_all(const char* fp, const char* hash, const rdpCertificate* cert)
1397
+ {
1398
+ WINPR_ASSERT(fp);
1399
+ WINPR_ASSERT(hash);
1400
+ WINPR_ASSERT(cert);
1401
+ if (compare_fingerprint(fp, hash, cert, FALSE))
1402
+ return TRUE;
1403
+ if (compare_fingerprint(fp, hash, cert, TRUE))
1404
+ return TRUE;
1405
+ return FALSE;
1406
+ }
1407
+
1408
+ static BOOL is_accepted_fingerprint(const rdpCertificate* cert,
1409
+ const char* CertificateAcceptedFingerprints)
1410
+ {
1411
+ WINPR_ASSERT(cert);
1412
+
1413
+ BOOL rc = FALSE;
1414
+ if (CertificateAcceptedFingerprints)
1415
+ {
1416
+ char* context = NULL;
1417
+ char* copy = _strdup(CertificateAcceptedFingerprints);
1418
+ char* cur = strtok_s(copy, ",", &context);
1419
+ while (cur)
1420
+ {
1421
+ char* subcontext = NULL;
1422
+ const char* h = strtok_s(cur, ":", &subcontext);
1423
+
1424
+ if (!h)
1425
+ goto next;
1426
+
1427
+ const char* fp = h + strlen(h) + 1;
1428
+ if (compare_fingerprint_all(fp, h, cert))
1429
+ {
1430
+ rc = TRUE;
1431
+ break;
1432
+ }
1433
+ next:
1434
+ cur = strtok_s(NULL, ",", &context);
1435
+ }
1436
+ free(copy);
1437
+ }
1438
+
1439
+ return rc;
1440
+ }
1441
+
1442
+ static BOOL accept_cert(rdpTls* tls, const rdpCertificate* cert)
1443
+ {
1444
+ WINPR_ASSERT(tls);
1445
+ WINPR_ASSERT(tls->context);
1446
+ WINPR_ASSERT(cert);
1447
+
1448
+ FreeRDP_Settings_Keys_String id = FreeRDP_AcceptedCert;
1449
+ FreeRDP_Settings_Keys_UInt32 lid = FreeRDP_AcceptedCertLength;
1450
+
1451
+ rdpSettings* settings = tls->context->settings;
1452
+ WINPR_ASSERT(settings);
1453
+
1454
+ if (tls->isGatewayTransport)
1455
+ {
1456
+ id = FreeRDP_GatewayAcceptedCert;
1457
+ lid = FreeRDP_GatewayAcceptedCertLength;
1458
+ }
1459
+ else if (is_redirected(tls))
1460
+ {
1461
+ id = FreeRDP_RedirectionAcceptedCert;
1462
+ lid = FreeRDP_RedirectionAcceptedCertLength;
1463
+ }
1464
+
1465
+ size_t pemLength = 0;
1466
+ char* pem = freerdp_certificate_get_pem_ex(cert, &pemLength, FALSE);
1467
+ BOOL rc = FALSE;
1468
+ if (pemLength <= UINT32_MAX)
1469
+ {
1470
+ if (freerdp_settings_set_string_len(settings, id, pem, pemLength))
1471
+ rc = freerdp_settings_set_uint32(settings, lid, (UINT32)pemLength);
1472
+ }
1473
+ free(pem);
1474
+ return rc;
1475
+ }
1476
+
1477
+ static BOOL tls_extract_full_pem(const rdpCertificate* cert, BYTE** PublicKey,
1478
+ size_t* PublicKeyLength)
1479
+ {
1480
+ if (!cert || !PublicKey)
1481
+ return FALSE;
1482
+ *PublicKey = (BYTE*)freerdp_certificate_get_pem(cert, PublicKeyLength);
1483
+ return *PublicKey != NULL;
1484
+ }
1485
+
1486
+ static int tls_config_parse_bool(WINPR_JSON* json, const char* opt)
1487
+ {
1488
+ WINPR_JSON* val = WINPR_JSON_GetObjectItem(json, opt);
1489
+ if (!val || !WINPR_JSON_IsBool(val))
1490
+ return -1;
1491
+
1492
+ if (WINPR_JSON_IsTrue(val))
1493
+ return 1;
1494
+ return 0;
1495
+ }
1496
+
1497
+ static char* tls_config_read(const char* configfile)
1498
+ {
1499
+ char* data = NULL;
1500
+ FILE* fp = winpr_fopen(configfile, "r");
1501
+ if (!fp)
1502
+ return NULL;
1503
+
1504
+ const int rc = fseek(fp, 0, SEEK_END);
1505
+ if (rc != 0)
1506
+ goto fail;
1507
+
1508
+ const INT64 size = _ftelli64(fp);
1509
+ if (size <= 0)
1510
+ goto fail;
1511
+
1512
+ const int rc2 = fseek(fp, 0, SEEK_SET);
1513
+ if (rc2 != 0)
1514
+ goto fail;
1515
+
1516
+ data = calloc((size_t)size + 1, sizeof(char));
1517
+ if (!data)
1518
+ goto fail;
1519
+
1520
+ const size_t read = fread(data, 1, (size_t)size, fp);
1521
+ if (read != (size_t)size)
1522
+ {
1523
+ free(data);
1524
+ data = NULL;
1525
+ goto fail;
1526
+ }
1527
+
1528
+ fail:
1529
+ fclose(fp);
1530
+ return data;
1531
+ }
1532
+
1533
+ static int tls_config_check_allowed_hashed(const char* configfile, const rdpCertificate* cert,
1534
+ WINPR_JSON* json)
1535
+ {
1536
+ WINPR_ASSERT(configfile);
1537
+ WINPR_ASSERT(cert);
1538
+ WINPR_ASSERT(json);
1539
+
1540
+ WINPR_JSON* db = WINPR_JSON_GetObjectItem(json, "certificate-db");
1541
+ if (!db || !WINPR_JSON_IsArray(db))
1542
+ return 0;
1543
+
1544
+ for (size_t x = 0; x < WINPR_JSON_GetArraySize(db); x++)
1545
+ {
1546
+ WINPR_JSON* cur = WINPR_JSON_GetArrayItem(db, x);
1547
+ if (!cur || !WINPR_JSON_IsObject(cur))
1548
+ {
1549
+ WLog_WARN(TAG,
1550
+ "[%s] invalid certificate-db entry at position %" PRIuz ": not a JSON object",
1551
+ configfile, x);
1552
+ continue;
1553
+ }
1554
+
1555
+ WINPR_JSON* key = WINPR_JSON_GetObjectItem(cur, "type");
1556
+ if (!key || !WINPR_JSON_IsString(key))
1557
+ {
1558
+ WLog_WARN(TAG,
1559
+ "[%s] invalid certificate-db entry at position %" PRIuz
1560
+ ": invalid 'type' element, expected type string",
1561
+ configfile, x);
1562
+ continue;
1563
+ }
1564
+ WINPR_JSON* val = WINPR_JSON_GetObjectItem(cur, "hash");
1565
+ if (!val || !WINPR_JSON_IsString(val))
1566
+ {
1567
+ WLog_WARN(TAG,
1568
+ "[%s] invalid certificate-db entry at position %" PRIuz
1569
+ ": invalid 'hash' element, expected type string",
1570
+ configfile, x);
1571
+ continue;
1572
+ }
1573
+
1574
+ const char* skey = WINPR_JSON_GetStringValue(key);
1575
+ const char* sval = WINPR_JSON_GetStringValue(val);
1576
+
1577
+ char* hash = freerdp_certificate_get_fingerprint_by_hash_ex(cert, skey, FALSE);
1578
+ if (!hash)
1579
+ {
1580
+ WLog_WARN(TAG,
1581
+ "[%s] invalid certificate-db entry at position %" PRIuz
1582
+ ": hash type '%s' not supported by certificate",
1583
+ configfile, x, skey);
1584
+ continue;
1585
+ }
1586
+
1587
+ const int cmp = _stricmp(hash, sval);
1588
+ free(hash);
1589
+
1590
+ if (cmp == 0)
1591
+ return 1;
1592
+ }
1593
+
1594
+ return 0;
1595
+ }
1596
+
1597
+ static int tls_config_check_certificate(const rdpCertificate* cert, BOOL* pAllowUserconfig)
1598
+ {
1599
+ WINPR_ASSERT(cert);
1600
+ WINPR_ASSERT(pAllowUserconfig);
1601
+
1602
+ int rc = 0;
1603
+ char* configfile = freerdp_GetConfigFilePath(TRUE, "certificates.json");
1604
+ WINPR_JSON* json = NULL;
1605
+
1606
+ if (!configfile)
1607
+ {
1608
+ WLog_DBG(TAG, "No configuration file for certificate handling, asking user");
1609
+ goto fail;
1610
+ }
1611
+
1612
+ char* configdata = tls_config_read(configfile);
1613
+ if (!configdata)
1614
+ {
1615
+ WLog_DBG(TAG, "Configuration file for certificate handling, asking user");
1616
+ goto fail;
1617
+ }
1618
+ json = WINPR_JSON_Parse(configdata);
1619
+ if (!json)
1620
+ {
1621
+ WLog_DBG(TAG, "No valid configuration file '%s' for certificate handling, asking user",
1622
+ configfile);
1623
+ goto fail;
1624
+ }
1625
+
1626
+ if (tls_config_parse_bool(json, "deny") > 0)
1627
+ {
1628
+ WLog_WARN(TAG, "[%s] certificate denied by configuration", configfile);
1629
+ rc = -1;
1630
+ goto fail;
1631
+ }
1632
+
1633
+ if (tls_config_parse_bool(json, "ignore") > 0)
1634
+ {
1635
+ WLog_WARN(TAG, "[%s] certificate ignored by configuration", configfile);
1636
+ rc = 1;
1637
+ goto fail;
1638
+ }
1639
+
1640
+ if (tls_config_check_allowed_hashed(configfile, cert, json) > 0)
1641
+ {
1642
+ WLog_WARN(TAG, "[%s] certificate manually accepted by configuration", configfile);
1643
+ rc = 1;
1644
+ goto fail;
1645
+ }
1646
+
1647
+ if (tls_config_parse_bool(json, "deny-userconfig") > 0)
1648
+ {
1649
+ WLog_WARN(TAG, "[%s] configuration denies user to accept certificates", configfile);
1650
+ rc = -1;
1651
+ goto fail;
1652
+ }
1653
+
1654
+ fail:
1655
+
1656
+ *pAllowUserconfig = (rc == 0);
1657
+ WINPR_JSON_Delete(json);
1658
+ free(configfile);
1659
+ return rc;
1660
+ }
1661
+
1662
+ int tls_verify_certificate(rdpTls* tls, const rdpCertificate* cert, const char* hostname,
1663
+ UINT16 port)
1664
+ {
1665
+ int match = 0;
1666
+ size_t length = 0;
1667
+ BOOL certificate_status = 0;
1668
+ char* common_name = NULL;
1669
+ size_t common_name_length = 0;
1670
+ char** dns_names = 0;
1671
+ size_t dns_names_count = 0;
1672
+ size_t* dns_names_lengths = NULL;
1673
+ int verification_status = -1;
1674
+ BOOL hostname_match = FALSE;
1675
+ rdpCertificateData* certificate_data = NULL;
1676
+ BYTE* pemCert = NULL;
1677
+ DWORD flags = VERIFY_CERT_FLAG_NONE;
1678
+ freerdp* instance = NULL;
1679
+
1680
+ WINPR_ASSERT(tls);
1681
+ WINPR_ASSERT(tls->context->settings);
1682
+
1683
+ instance = (freerdp*)tls->context->settings->instance;
1684
+ WINPR_ASSERT(instance);
1685
+
1686
+ if (freerdp_shall_disconnect_context(instance->context))
1687
+ return -1;
1688
+
1689
+ if (!tls_extract_full_pem(cert, &pemCert, &length))
1690
+ goto end;
1691
+
1692
+ /* Check, if we already accepted this key. */
1693
+ if (is_accepted(tls, cert))
1694
+ {
1695
+ verification_status = 1;
1696
+ goto end;
1697
+ }
1698
+
1699
+ if (is_accepted_fingerprint(cert, tls->context->settings->CertificateAcceptedFingerprints))
1700
+ {
1701
+ verification_status = 1;
1702
+ goto end;
1703
+ }
1704
+
1705
+ if (tls->isGatewayTransport || is_redirected(tls))
1706
+ flags |= VERIFY_CERT_FLAG_LEGACY;
1707
+
1708
+ if (tls->isGatewayTransport)
1709
+ flags |= VERIFY_CERT_FLAG_GATEWAY;
1710
+
1711
+ if (is_redirected(tls))
1712
+ flags |= VERIFY_CERT_FLAG_REDIRECT;
1713
+
1714
+ /* Certificate management is done by the application */
1715
+ if (tls->context->settings->ExternalCertificateManagement)
1716
+ {
1717
+ if (instance->VerifyX509Certificate)
1718
+ verification_status =
1719
+ instance->VerifyX509Certificate(instance, pemCert, length, hostname, port, flags);
1720
+ else
1721
+ WLog_ERR(TAG, "No VerifyX509Certificate callback registered!");
1722
+
1723
+ if (verification_status > 0)
1724
+ accept_cert(tls, cert);
1725
+ else if (verification_status < 0)
1726
+ {
1727
+ WLog_ERR(TAG, "VerifyX509Certificate failed: (length = %" PRIuz ") status: [%d] %s",
1728
+ length, verification_status, pemCert);
1729
+ goto end;
1730
+ }
1731
+ }
1732
+ /* ignore certificate verification if user explicitly required it (discouraged) */
1733
+ else if (tls->context->settings->IgnoreCertificate)
1734
+ verification_status = 1; /* success! */
1735
+ else if (!tls->isGatewayTransport && (tls->context->settings->AuthenticationLevel == 0))
1736
+ verification_status = 1; /* success! */
1737
+ else
1738
+ {
1739
+ /* if user explicitly specified a certificate name, use it instead of the hostname */
1740
+ if (!tls->isGatewayTransport && tls->context->settings->CertificateName)
1741
+ hostname = tls->context->settings->CertificateName;
1742
+
1743
+ /* attempt verification using OpenSSL and the ~/.freerdp/certs certificate store */
1744
+ certificate_status = freerdp_certificate_verify(
1745
+ cert, freerdp_certificate_store_get_certs_path(tls->certificate_store));
1746
+ /* verify certificate name match */
1747
+ certificate_data = freerdp_certificate_data_new(hostname, port, cert);
1748
+ if (!certificate_data)
1749
+ goto end;
1750
+ /* extra common name and alternative names */
1751
+ common_name = freerdp_certificate_get_common_name(cert, &common_name_length);
1752
+ dns_names = freerdp_certificate_get_dns_names(cert, &dns_names_count, &dns_names_lengths);
1753
+
1754
+ /* compare against common name */
1755
+
1756
+ if (common_name)
1757
+ {
1758
+ if (tls_match_hostname(common_name, common_name_length, hostname))
1759
+ hostname_match = TRUE;
1760
+ }
1761
+
1762
+ /* compare against alternative names */
1763
+
1764
+ if (dns_names)
1765
+ {
1766
+ for (size_t index = 0; index < dns_names_count; index++)
1767
+ {
1768
+ if (tls_match_hostname(dns_names[index], dns_names_lengths[index], hostname))
1769
+ {
1770
+ hostname_match = TRUE;
1771
+ break;
1772
+ }
1773
+ }
1774
+ }
1775
+
1776
+ /* if the certificate is valid and the certificate name matches, verification succeeds
1777
+ */
1778
+ if (certificate_status && hostname_match)
1779
+ verification_status = 1; /* success! */
1780
+
1781
+ if (!hostname_match)
1782
+ flags |= VERIFY_CERT_FLAG_MISMATCH;
1783
+
1784
+ BOOL allowUserconfig = TRUE;
1785
+ if (!certificate_status || !hostname_match)
1786
+ verification_status = tls_config_check_certificate(cert, &allowUserconfig);
1787
+
1788
+ /* verification could not succeed with OpenSSL, use known_hosts file and prompt user for
1789
+ * manual verification */
1790
+ if (allowUserconfig && (!certificate_status || !hostname_match))
1791
+ {
1792
+ DWORD accept_certificate = 0;
1793
+ size_t pem_length = 0;
1794
+ char* issuer = freerdp_certificate_get_issuer(cert);
1795
+ char* subject = freerdp_certificate_get_subject(cert);
1796
+ char* pem = freerdp_certificate_get_pem(cert, &pem_length);
1797
+
1798
+ if (!pem)
1799
+ goto end;
1800
+
1801
+ /* search for matching entry in known_hosts file */
1802
+ match =
1803
+ freerdp_certificate_store_contains_data(tls->certificate_store, certificate_data);
1804
+
1805
+ if (match == 1)
1806
+ {
1807
+ /* no entry was found in known_hosts file, prompt user for manual verification
1808
+ */
1809
+ if (!hostname_match)
1810
+ tls_print_certificate_name_mismatch_error(hostname, port, common_name,
1811
+ dns_names, dns_names_count);
1812
+
1813
+ {
1814
+ char* efp = freerdp_certificate_get_fingerprint(cert);
1815
+ tls_print_new_certificate_warn(tls->certificate_store, hostname, port, efp);
1816
+ free(efp);
1817
+ }
1818
+
1819
+ /* Automatically accept certificate on first use */
1820
+ if (tls->context->settings->AutoAcceptCertificate)
1821
+ {
1822
+ WLog_INFO(TAG, "No certificate stored, automatically accepting.");
1823
+ accept_certificate = 1;
1824
+ }
1825
+ else if (tls->context->settings->AutoDenyCertificate)
1826
+ {
1827
+ WLog_INFO(TAG, "No certificate stored, automatically denying.");
1828
+ accept_certificate = 0;
1829
+ }
1830
+ else if (instance->VerifyX509Certificate)
1831
+ {
1832
+ int rc = instance->VerifyX509Certificate(instance, pemCert, pem_length,
1833
+ hostname, port, flags);
1834
+
1835
+ if (rc == 1)
1836
+ accept_certificate = 1;
1837
+ else if (rc > 1)
1838
+ accept_certificate = 2;
1839
+ else
1840
+ accept_certificate = 0;
1841
+ }
1842
+ else if (instance->VerifyCertificateEx)
1843
+ {
1844
+ const BOOL use_pem = freerdp_settings_get_bool(
1845
+ tls->context->settings, FreeRDP_CertificateCallbackPreferPEM);
1846
+ char* fp = NULL;
1847
+ DWORD cflags = flags;
1848
+ if (use_pem)
1849
+ {
1850
+ cflags |= VERIFY_CERT_FLAG_FP_IS_PEM;
1851
+ fp = pem;
1852
+ }
1853
+ else
1854
+ fp = freerdp_certificate_get_fingerprint(cert);
1855
+ accept_certificate = instance->VerifyCertificateEx(
1856
+ instance, hostname, port, common_name, subject, issuer, fp, cflags);
1857
+ if (!use_pem)
1858
+ free(fp);
1859
+ }
1860
+ #if defined(WITH_FREERDP_DEPRECATED)
1861
+ else if (instance->VerifyCertificate)
1862
+ {
1863
+ char* fp = freerdp_certificate_get_fingerprint(cert);
1864
+
1865
+ WLog_WARN(TAG, "The VerifyCertificate callback is deprecated, migrate your "
1866
+ "application to VerifyCertificateEx");
1867
+ accept_certificate = instance->VerifyCertificate(instance, common_name, subject,
1868
+ issuer, fp, !hostname_match);
1869
+ free(fp);
1870
+ }
1871
+ #endif
1872
+ }
1873
+ else if (match == -1)
1874
+ {
1875
+ rdpCertificateData* stored_data =
1876
+ freerdp_certificate_store_load_data(tls->certificate_store, hostname, port);
1877
+ /* entry was found in known_hosts file, but fingerprint does not match. ask user
1878
+ * to use it */
1879
+ {
1880
+ char* efp = freerdp_certificate_get_fingerprint(cert);
1881
+ tls_print_certificate_error(tls->certificate_store, stored_data, hostname, port,
1882
+ efp);
1883
+ free(efp);
1884
+ }
1885
+
1886
+ if (!stored_data)
1887
+ WLog_WARN(TAG, "Failed to get certificate entry for %s:%" PRIu16 "", hostname,
1888
+ port);
1889
+
1890
+ if (tls->context->settings->AutoDenyCertificate)
1891
+ {
1892
+ WLog_INFO(TAG, "No certificate stored, automatically denying.");
1893
+ accept_certificate = 0;
1894
+ }
1895
+ else if (instance->VerifyX509Certificate)
1896
+ {
1897
+ const int rc =
1898
+ instance->VerifyX509Certificate(instance, pemCert, pem_length, hostname,
1899
+ port, flags | VERIFY_CERT_FLAG_CHANGED);
1900
+
1901
+ if (rc == 1)
1902
+ accept_certificate = 1;
1903
+ else if (rc > 1)
1904
+ accept_certificate = 2;
1905
+ else
1906
+ accept_certificate = 0;
1907
+ }
1908
+ else if (instance->VerifyChangedCertificateEx)
1909
+ {
1910
+ DWORD cflags = flags | VERIFY_CERT_FLAG_CHANGED;
1911
+ const char* old_subject = freerdp_certificate_data_get_subject(stored_data);
1912
+ const char* old_issuer = freerdp_certificate_data_get_issuer(stored_data);
1913
+ const char* old_fp = freerdp_certificate_data_get_fingerprint(stored_data);
1914
+ const char* old_pem = freerdp_certificate_data_get_pem(stored_data);
1915
+ const BOOL fpIsAllocated =
1916
+ !old_pem ||
1917
+ !freerdp_settings_get_bool(tls->context->settings,
1918
+ FreeRDP_CertificateCallbackPreferPEM);
1919
+ char* fp = NULL;
1920
+ if (!fpIsAllocated)
1921
+ {
1922
+ cflags |= VERIFY_CERT_FLAG_FP_IS_PEM;
1923
+ fp = pem;
1924
+ old_fp = old_pem;
1925
+ }
1926
+ else
1927
+ {
1928
+ fp = freerdp_certificate_get_fingerprint(cert);
1929
+ }
1930
+ accept_certificate = instance->VerifyChangedCertificateEx(
1931
+ instance, hostname, port, common_name, subject, issuer, fp, old_subject,
1932
+ old_issuer, old_fp, cflags);
1933
+ if (fpIsAllocated)
1934
+ free(fp);
1935
+ }
1936
+ #if defined(WITH_FREERDP_DEPRECATED)
1937
+ else if (instance->VerifyChangedCertificate)
1938
+ {
1939
+ char* fp = freerdp_certificate_get_fingerprint(cert);
1940
+ const char* old_subject = freerdp_certificate_data_get_subject(stored_data);
1941
+ const char* old_issuer = freerdp_certificate_data_get_issuer(stored_data);
1942
+ const char* old_fingerprint =
1943
+ freerdp_certificate_data_get_fingerprint(stored_data);
1944
+
1945
+ WLog_WARN(TAG, "The VerifyChangedCertificate callback is deprecated, migrate "
1946
+ "your application to VerifyChangedCertificateEx");
1947
+ accept_certificate = instance->VerifyChangedCertificate(
1948
+ instance, common_name, subject, issuer, fp, old_subject, old_issuer,
1949
+ old_fingerprint);
1950
+ free(fp);
1951
+ }
1952
+ #endif
1953
+
1954
+ freerdp_certificate_data_free(stored_data);
1955
+ }
1956
+ else if (match == 0)
1957
+ accept_certificate = 2; /* success! */
1958
+
1959
+ /* Save certificate or do a simple accept / reject */
1960
+ switch (accept_certificate)
1961
+ {
1962
+ case 1:
1963
+
1964
+ /* user accepted certificate, add entry in known_hosts file */
1965
+ verification_status = freerdp_certificate_store_save_data(
1966
+ tls->certificate_store, certificate_data)
1967
+ ? 1
1968
+ : -1;
1969
+ break;
1970
+
1971
+ case 2:
1972
+ /* user did accept temporaty, do not add to known hosts file */
1973
+ verification_status = 1;
1974
+ break;
1975
+
1976
+ default:
1977
+ /* user did not accept, abort and do not add entry in known_hosts file */
1978
+ verification_status = -1; /* failure! */
1979
+ break;
1980
+ }
1981
+
1982
+ free(issuer);
1983
+ free(subject);
1984
+ free(pem);
1985
+ }
1986
+
1987
+ if (verification_status > 0)
1988
+ accept_cert(tls, cert);
1989
+ }
1990
+
1991
+ end:
1992
+ freerdp_certificate_data_free(certificate_data);
1993
+ free(common_name);
1994
+ freerdp_certificate_free_dns_names(dns_names_count, dns_names_lengths, dns_names);
1995
+ free(pemCert);
1996
+ return verification_status;
1997
+ }
1998
+
1999
+ void tls_print_new_certificate_warn(rdpCertificateStore* store, const char* hostname, UINT16 port,
2000
+ const char* fingerprint)
2001
+ {
2002
+ char* path = freerdp_certificate_store_get_cert_path(store, hostname, port);
2003
+
2004
+ WLog_ERR(TAG, "The host key for %s:%" PRIu16 " has changed", hostname, port);
2005
+ WLog_ERR(TAG, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
2006
+ WLog_ERR(TAG, "@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @");
2007
+ WLog_ERR(TAG, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
2008
+ WLog_ERR(TAG, "IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!");
2009
+ WLog_ERR(TAG, "Someone could be eavesdropping on you right now (man-in-the-middle attack)!");
2010
+ WLog_ERR(TAG, "It is also possible that a host key has just been changed.");
2011
+ WLog_ERR(TAG, "The fingerprint for the host key sent by the remote host is %s", fingerprint);
2012
+ WLog_ERR(TAG, "Please contact your system administrator.");
2013
+ WLog_ERR(TAG, "Add correct host key in %s to get rid of this message.", path);
2014
+ WLog_ERR(TAG, "Host key for %s has changed and you have requested strict checking.", hostname);
2015
+ WLog_ERR(TAG, "Host key verification failed.");
2016
+
2017
+ free(path);
2018
+ }
2019
+
2020
+ void tls_print_certificate_error(rdpCertificateStore* store, rdpCertificateData* stored_data,
2021
+ const char* hostname, UINT16 port, const char* fingerprint)
2022
+ {
2023
+ char* path = freerdp_certificate_store_get_cert_path(store, hostname, port);
2024
+
2025
+ WLog_ERR(TAG, "New host key for %s:%" PRIu16, hostname, port);
2026
+ WLog_ERR(TAG, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
2027
+ WLog_ERR(TAG, "@ WARNING: NEW HOST IDENTIFICATION! @");
2028
+ WLog_ERR(TAG, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
2029
+
2030
+ WLog_ERR(TAG, "The fingerprint for the host key sent by the remote host is %s", fingerprint);
2031
+ WLog_ERR(TAG, "Please contact your system administrator.");
2032
+ WLog_ERR(TAG, "Add correct host key in %s to get rid of this message.", path);
2033
+
2034
+ free(path);
2035
+ }
2036
+
2037
+ void tls_print_certificate_name_mismatch_error(const char* hostname, UINT16 port,
2038
+ const char* common_name, char** alt_names,
2039
+ size_t alt_names_count)
2040
+ {
2041
+ WINPR_ASSERT(NULL != hostname);
2042
+ WLog_ERR(TAG, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
2043
+ WLog_ERR(TAG, "@ WARNING: CERTIFICATE NAME MISMATCH! @");
2044
+ WLog_ERR(TAG, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
2045
+ WLog_ERR(TAG, "The hostname used for this connection (%s:%" PRIu16 ") ", hostname, port);
2046
+ WLog_ERR(TAG, "does not match %s given in the certificate:",
2047
+ alt_names_count < 1 ? "the name" : "any of the names");
2048
+ WLog_ERR(TAG, "Common Name (CN):");
2049
+ WLog_ERR(TAG, "\t%s", common_name ? common_name : "no CN found in certificate");
2050
+
2051
+ if (alt_names_count > 0)
2052
+ {
2053
+ WINPR_ASSERT(NULL != alt_names);
2054
+ WLog_ERR(TAG, "Alternative names:");
2055
+
2056
+ for (size_t index = 0; index < alt_names_count; index++)
2057
+ {
2058
+ WINPR_ASSERT(alt_names[index]);
2059
+ WLog_ERR(TAG, "\t %s", alt_names[index]);
2060
+ }
2061
+ }
2062
+
2063
+ WLog_ERR(TAG, "A valid certificate for the wrong name should NOT be trusted!");
2064
+ }
2065
+
2066
+ rdpTls* freerdp_tls_new(rdpContext* context)
2067
+ {
2068
+ rdpTls* tls = NULL;
2069
+ tls = (rdpTls*)calloc(1, sizeof(rdpTls));
2070
+
2071
+ if (!tls)
2072
+ return NULL;
2073
+
2074
+ tls->context = context;
2075
+
2076
+ if (!freerdp_settings_get_bool(tls->context->settings, FreeRDP_ServerMode))
2077
+ {
2078
+ tls->certificate_store = freerdp_certificate_store_new(tls->context->settings);
2079
+
2080
+ if (!tls->certificate_store)
2081
+ goto out_free;
2082
+ }
2083
+
2084
+ tls->alertLevel = TLS_ALERT_LEVEL_WARNING;
2085
+ tls->alertDescription = TLS_ALERT_DESCRIPTION_CLOSE_NOTIFY;
2086
+ return tls;
2087
+ out_free:
2088
+ free(tls);
2089
+ return NULL;
2090
+ }
2091
+
2092
+ void freerdp_tls_free(rdpTls* tls)
2093
+ {
2094
+ if (!tls)
2095
+ return;
2096
+
2097
+ tls_reset(tls);
2098
+
2099
+ if (tls->certificate_store)
2100
+ {
2101
+ freerdp_certificate_store_free(tls->certificate_store);
2102
+ tls->certificate_store = NULL;
2103
+ }
2104
+
2105
+ free(tls);
2106
+ }
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/crypto/tls.h ADDED
@@ -0,0 +1,131 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * Transport Layer Security
4
+ *
5
+ * Copyright 2011-2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+
20
+ #ifndef FREERDP_LIB_CRYPTO_TLS_H
21
+ #define FREERDP_LIB_CRYPTO_TLS_H
22
+
23
+ #include <winpr/crt.h>
24
+ #include <winpr/sspi.h>
25
+
26
+ #include <openssl/ssl.h>
27
+ #include <openssl/err.h>
28
+
29
+ #include <freerdp/api.h>
30
+ #include <freerdp/types.h>
31
+ #include <freerdp/crypto/certificate_store.h>
32
+
33
+ #include <winpr/stream.h>
34
+
35
+ #define TLS_ALERT_LEVEL_WARNING 1
36
+ #define TLS_ALERT_LEVEL_FATAL 2
37
+
38
+ #define TLS_ALERT_DESCRIPTION_CLOSE_NOTIFY 0
39
+ #define TLS_ALERT_DESCRIPTION_UNEXPECTED_MESSAGE 10
40
+ #define TLS_ALERT_DESCRIPTION_BAD_RECORD_MAC 20
41
+ #define TLS_ALERT_DESCRIPTION_DECRYPTION_FAILED 21
42
+ #define TLS_ALERT_DESCRIPTION_RECORD_OVERFLOW 22
43
+ #define TLS_ALERT_DESCRIPTION_DECOMPRESSION_FAILURE 30
44
+ #define TLS_ALERT_DESCRIPTION_HANSHAKE_FAILURE 40
45
+ #define TLS_ALERT_DESCRIPTION_NO_CERTIFICATE 41
46
+ #define TLS_ALERT_DESCRIPTION_BAD_CERTIFICATE 42
47
+ #define TLS_ALERT_DESCRIPTION_UNSUPPORTED_CERTIFICATE 43
48
+ #define TLS_ALERT_DESCRIPTION_CERTIFICATE_REVOKED 44
49
+ #define TLS_ALERT_DESCRIPTION_CERTIFICATE_EXPIRED 45
50
+ #define TLS_ALERT_DESCRIPTION_CERTIFICATE_UNKNOWN 46
51
+ #define TLS_ALERT_DESCRIPTION_ILLEGAL_PARAMETER 47
52
+ #define TLS_ALERT_DESCRIPTION_UNKNOWN_CA 48
53
+ #define TLS_ALERT_DESCRIPTION_ACCESS_DENIED 49
54
+ #define TLS_ALERT_DESCRIPTION_DECODE_ERROR 50
55
+ #define TLS_ALERT_DESCRIPTION_DECRYPT_ERROR 51
56
+ #define TLS_ALERT_DESCRIPTION_EXPORT_RESTRICTION 60
57
+ #define TLS_ALERT_DESCRIPTION_PROTOCOL_VERSION 70
58
+ #define TLS_ALERT_DESCRIPTION_INSUFFICIENT_SECURITY 71
59
+ #define TLS_ALERT_DESCRIPTION_INTERNAL_ERROR 80
60
+ #define TLS_ALERT_DESCRIPTION_USER_CANCELED 90
61
+ #define TLS_ALERT_DESCRIPTION_NO_RENEGOTIATION 100
62
+ #define TLS_ALERT_DESCRIPTION_UNSUPPORTED_EXTENSION 110
63
+
64
+ typedef struct rdp_tls rdpTls;
65
+
66
+ struct rdp_tls
67
+ {
68
+ SSL* ssl;
69
+ BIO* bio;
70
+ void* tsg;
71
+ SSL_CTX* ctx;
72
+ BYTE* PublicKey;
73
+ DWORD PublicKeyLength;
74
+ rdpContext* context;
75
+ SecPkgContext_Bindings* Bindings;
76
+ rdpCertificateStore* certificate_store;
77
+ BIO* underlying;
78
+ const char* hostname;
79
+ const char* serverName;
80
+ int port;
81
+ int alertLevel;
82
+ int alertDescription;
83
+ BOOL isGatewayTransport;
84
+ BOOL isClientMode;
85
+ };
86
+
87
+ /** @brief result of a handshake operation */
88
+ typedef enum
89
+ {
90
+ TLS_HANDSHAKE_SUCCESS, /*!< handshake was successful */
91
+ TLS_HANDSHAKE_CONTINUE, /*!< handshake is not completed */
92
+ TLS_HANDSHAKE_ERROR, /*!< an error (probably IO error) happened */
93
+ TLS_HANDSHAKE_VERIFY_ERROR /*!< Certificate verification failed (client mode) */
94
+ } TlsHandshakeResult;
95
+
96
+ #ifdef __cplusplus
97
+ extern "C"
98
+ {
99
+ #endif
100
+
101
+ FREERDP_LOCAL const SSL_METHOD* freerdp_tls_get_ssl_method(BOOL isDtls, BOOL isClient);
102
+
103
+ FREERDP_LOCAL int freerdp_tls_connect(rdpTls* tls, BIO* underlying);
104
+
105
+ FREERDP_LOCAL TlsHandshakeResult freerdp_tls_connect_ex(rdpTls* tls, BIO* underlying,
106
+ const SSL_METHOD* methods);
107
+
108
+ FREERDP_LOCAL BOOL freerdp_tls_accept(rdpTls* tls, BIO* underlying, rdpSettings* settings);
109
+
110
+ FREERDP_LOCAL TlsHandshakeResult freerdp_tls_accept_ex(rdpTls* tls, BIO* underlying,
111
+ rdpSettings* settings,
112
+ const SSL_METHOD* methods);
113
+
114
+ FREERDP_LOCAL TlsHandshakeResult freerdp_tls_handshake(rdpTls* tls);
115
+
116
+ FREERDP_LOCAL BOOL freerdp_tls_send_alert(rdpTls* tls);
117
+
118
+ FREERDP_LOCAL int freerdp_tls_write_all(rdpTls* tls, const BYTE* data, size_t length);
119
+
120
+ FREERDP_LOCAL int freerdp_tls_set_alert_code(rdpTls* tls, int level, int description);
121
+
122
+ FREERDP_LOCAL void freerdp_tls_free(rdpTls* tls);
123
+
124
+ WINPR_ATTR_MALLOC(freerdp_tls_free, 1)
125
+ FREERDP_LOCAL rdpTls* freerdp_tls_new(rdpContext* context);
126
+
127
+ #ifdef __cplusplus
128
+ }
129
+ #endif
130
+
131
+ #endif /* FREERDP_LIB_CRYPTO_TLS_H */
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/crypto/x509_utils.c ADDED
@@ -0,0 +1,919 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * Cryptographic Abstraction Layer
4
+ *
5
+ * Copyright 2011-2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
+ * Copyright 2023 Armin Novak <anovak@thincast.com>
7
+ * Copyright 2023 Thincast Technologies GmbH
8
+ *
9
+ * Licensed under the Apache License, Version 2.0 (the "License");
10
+ * you may not use this file except in compliance with the License.
11
+ * You may obtain a copy of the License at
12
+ *
13
+ * http://www.apache.org/licenses/LICENSE-2.0
14
+ *
15
+ * Unless required by applicable law or agreed to in writing, software
16
+ * distributed under the License is distributed on an "AS IS" BASIS,
17
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ * See the License for the specific language governing permissions and
19
+ * limitations under the License.
20
+ */
21
+
22
+ #include <openssl/objects.h>
23
+ #include <openssl/x509v3.h>
24
+ #include <openssl/pem.h>
25
+ #include <openssl/err.h>
26
+
27
+ #include <freerdp/config.h>
28
+
29
+ #include <winpr/crt.h>
30
+ #include <winpr/string.h>
31
+ #include <winpr/assert.h>
32
+
33
+ #include <freerdp/log.h>
34
+
35
+ #include "x509_utils.h"
36
+
37
+ #define TAG FREERDP_TAG("crypto")
38
+
39
+ BYTE* x509_utils_get_hash(const X509* xcert, const char* hash, size_t* length)
40
+ {
41
+ UINT32 fp_len = EVP_MAX_MD_SIZE;
42
+ BYTE* fp = NULL;
43
+ const EVP_MD* md = EVP_get_digestbyname(hash);
44
+ if (!md)
45
+ {
46
+ WLog_ERR(TAG, "System does not support %s hash!", hash);
47
+ return NULL;
48
+ }
49
+ if (!xcert || !length)
50
+ {
51
+ WLog_ERR(TAG, "Invalid arguments: xcert=%p, length=%p", xcert, length);
52
+ return NULL;
53
+ }
54
+
55
+ fp = calloc(fp_len + 1, sizeof(BYTE));
56
+ if (!fp)
57
+ {
58
+ WLog_ERR(TAG, "could not allocate %" PRIuz " bytes", fp_len);
59
+ return NULL;
60
+ }
61
+
62
+ if (X509_digest(xcert, md, fp, &fp_len) != 1)
63
+ {
64
+ free(fp);
65
+ WLog_ERR(TAG, "certificate does not have a %s hash!", hash);
66
+ return NULL;
67
+ }
68
+
69
+ *length = fp_len;
70
+ return fp;
71
+ }
72
+
73
+ static char* crypto_print_name(const X509_NAME* name)
74
+ {
75
+ char* buffer = NULL;
76
+ BIO* outBIO = BIO_new(BIO_s_mem());
77
+
78
+ if (X509_NAME_print_ex(outBIO, name, 0, XN_FLAG_ONELINE) > 0)
79
+ {
80
+ UINT64 size = BIO_number_written(outBIO);
81
+ if (size > INT_MAX)
82
+ goto fail;
83
+ buffer = calloc(1, (size_t)size + 1);
84
+
85
+ if (!buffer)
86
+ goto fail;
87
+
88
+ ERR_clear_error();
89
+ const int rc = BIO_read(outBIO, buffer, (int)size);
90
+ if (rc <= 0)
91
+ {
92
+ free(buffer);
93
+ buffer = NULL;
94
+ goto fail;
95
+ }
96
+ }
97
+
98
+ fail:
99
+ BIO_free_all(outBIO);
100
+ return buffer;
101
+ }
102
+
103
+ char* x509_utils_get_subject(const X509* xcert)
104
+ {
105
+ char* subject = NULL;
106
+ if (!xcert)
107
+ {
108
+ WLog_ERR(TAG, "Invalid certificate %p", xcert);
109
+ return NULL;
110
+ }
111
+ subject = crypto_print_name(X509_get_subject_name(xcert));
112
+ if (!subject)
113
+ WLog_WARN(TAG, "certificate does not have a subject!");
114
+ return subject;
115
+ }
116
+
117
+ /* GENERAL_NAME type labels */
118
+
119
+ static const char* general_name_type_labels[] = { "OTHERNAME", "EMAIL ", "DNS ",
120
+ "X400 ", "DIRNAME ", "EDIPARTY ",
121
+ "URI ", "IPADD ", "RID " };
122
+
123
+ static const char* general_name_type_label(int general_name_type)
124
+ {
125
+ if ((0 <= general_name_type) &&
126
+ ((size_t)general_name_type < ARRAYSIZE(general_name_type_labels)))
127
+ {
128
+ return general_name_type_labels[general_name_type];
129
+ }
130
+ else
131
+ {
132
+ static char buffer[80] = { 0 };
133
+ (void)snprintf(buffer, sizeof(buffer), "Unknown general name type (%d)", general_name_type);
134
+ return buffer;
135
+ }
136
+ }
137
+
138
+ /*
139
+
140
+ map_subject_alt_name(x509, general_name_type, mapper, data)
141
+
142
+ Call the function mapper with subjectAltNames found in the x509
143
+ certificate and data. if generate_name_type is GEN_ALL, the the
144
+ mapper is called for all the names, else it's called only for names
145
+ of the given type.
146
+
147
+
148
+ We implement two extractors:
149
+
150
+ - a string extractor that can be used to get the subjectAltNames of
151
+ the following types: GEN_URI, GEN_DNS, GEN_EMAIL
152
+
153
+ - a ASN1_OBJECT filter/extractor that can be used to get the
154
+ subjectAltNames of OTHERNAME type.
155
+
156
+ Note: usually, it's a string, but some type of otherNames can be
157
+ associated with different classes of objects. eg. a KPN may be a
158
+ sequence of realm and principal name, instead of a single string
159
+ object.
160
+
161
+ Not implemented yet: extractors for the types: GEN_X400, GEN_DIRNAME,
162
+ GEN_EDIPARTY, GEN_RID, GEN_IPADD (the later can contain nul-bytes).
163
+
164
+
165
+ mapper(name, data, index, count)
166
+
167
+ The mapper is passed:
168
+ - the GENERAL_NAME selected,
169
+ - the data,
170
+ - the index of the general name in the subjectAltNames,
171
+ - the total number of names in the subjectAltNames.
172
+
173
+ The last parameter let's the mapper allocate arrays to collect objects.
174
+ Note: if names are filtered, not all the indices from 0 to count-1 are
175
+ passed to mapper, only the indices selected.
176
+
177
+ When the mapper returns 0, map_subject_alt_name stops the iteration immediately.
178
+
179
+ */
180
+
181
+ #define GEN_ALL (-1)
182
+
183
+ typedef int (*general_name_mapper_pr)(GENERAL_NAME* name, void* data, int index, int count);
184
+
185
+ static void map_subject_alt_name(const X509* x509, int general_name_type,
186
+ general_name_mapper_pr mapper, void* data)
187
+ {
188
+ int num = 0;
189
+ STACK_OF(GENERAL_NAME)* gens = NULL;
190
+ gens = X509_get_ext_d2i(x509, NID_subject_alt_name, NULL, NULL);
191
+
192
+ if (!gens)
193
+ {
194
+ return;
195
+ }
196
+
197
+ num = sk_GENERAL_NAME_num(gens);
198
+
199
+ for (int i = 0; (i < num); i++)
200
+ {
201
+ GENERAL_NAME* name = sk_GENERAL_NAME_value(gens, i);
202
+
203
+ if (name)
204
+ {
205
+ if ((general_name_type == GEN_ALL) || (general_name_type == name->type))
206
+ {
207
+ if (!mapper(name, data, i, num))
208
+ {
209
+ break;
210
+ }
211
+ }
212
+ }
213
+ }
214
+
215
+ sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
216
+ }
217
+
218
+ /*
219
+ extract_string -- string extractor
220
+
221
+ - the strings array is allocated lazily, when we first have to store a
222
+ string.
223
+
224
+ - allocated contains the size of the strings array, or -1 if
225
+ allocation failed.
226
+
227
+ - count contains the actual count of strings in the strings array.
228
+
229
+ - maximum limits the number of strings we can store in the strings
230
+ array: beyond, the extractor returns 0 to short-cut the search.
231
+
232
+ extract_string stores in the string list OPENSSL strings,
233
+ that must be freed with OPENSSL_free.
234
+
235
+ */
236
+
237
+ typedef struct string_list
238
+ {
239
+ char** strings;
240
+ size_t allocated;
241
+ size_t count;
242
+ size_t maximum;
243
+ } string_list;
244
+
245
+ static void string_list_initialize(string_list* list)
246
+ {
247
+ list->strings = 0;
248
+ list->allocated = 0;
249
+ list->count = 0;
250
+ list->maximum = INT_MAX;
251
+ }
252
+
253
+ static void string_list_allocate(string_list* list, size_t allocate_count)
254
+ {
255
+ if (!list->strings && list->allocated == 0)
256
+ {
257
+ list->strings = (char**)calloc(allocate_count, sizeof(char*));
258
+ list->allocated = list->strings ? allocate_count : 0;
259
+ list->count = 0;
260
+ }
261
+ }
262
+
263
+ static void string_list_free(string_list* list)
264
+ {
265
+ /* Note: we don't free the contents of the strings array: this */
266
+ /* is handled by the caller, either by returning this */
267
+ /* content, or freeing it itself. */
268
+ free((void*)list->strings);
269
+ }
270
+
271
+ static int extract_string(GENERAL_NAME* name, void* data, int index, int count)
272
+ {
273
+ string_list* list = data;
274
+ unsigned char* cstring = 0;
275
+ ASN1_STRING* str = NULL;
276
+
277
+ WINPR_UNUSED(index);
278
+
279
+ switch (name->type)
280
+ {
281
+ case GEN_URI:
282
+ str = name->d.uniformResourceIdentifier;
283
+ break;
284
+
285
+ case GEN_DNS:
286
+ str = name->d.dNSName;
287
+ break;
288
+
289
+ case GEN_EMAIL:
290
+ str = name->d.rfc822Name;
291
+ break;
292
+
293
+ default:
294
+ return 1;
295
+ }
296
+
297
+ if ((ASN1_STRING_to_UTF8(&cstring, str)) < 0)
298
+ {
299
+ WLog_ERR(TAG, "ASN1_STRING_to_UTF8() failed for %s: %s",
300
+ general_name_type_label(name->type), ERR_error_string(ERR_get_error(), NULL));
301
+ return 1;
302
+ }
303
+
304
+ string_list_allocate(list, WINPR_ASSERTING_INT_CAST(WINPR_CIPHER_TYPE, count));
305
+
306
+ if (list->allocated <= 0)
307
+ {
308
+ OPENSSL_free(cstring);
309
+ return 0;
310
+ }
311
+
312
+ list->strings[list->count] = (char*)cstring;
313
+ list->count++;
314
+
315
+ if (list->count >= list->maximum)
316
+ {
317
+ return 0;
318
+ }
319
+
320
+ return 1;
321
+ }
322
+
323
+ /*
324
+ extract_othername_object -- object extractor.
325
+
326
+ - the objects array is allocated lazily, when we first have to store a
327
+ string.
328
+
329
+ - allocated contains the size of the objects array, or -1 if
330
+ allocation failed.
331
+
332
+ - count contains the actual count of objects in the objects array.
333
+
334
+ - maximum limits the number of objects we can store in the objects
335
+ array: beyond, the extractor returns 0 to short-cut the search.
336
+
337
+ extract_othername_objects stores in the objects array ASN1_TYPE *
338
+ pointers directly obtained from the GENERAL_NAME.
339
+ */
340
+
341
+ typedef struct object_list
342
+ {
343
+ ASN1_OBJECT* type_id;
344
+ char** strings;
345
+ size_t allocated;
346
+ size_t count;
347
+ size_t maximum;
348
+ } object_list;
349
+
350
+ static void object_list_initialize(object_list* list)
351
+ {
352
+ list->type_id = 0;
353
+ list->strings = 0;
354
+ list->allocated = 0;
355
+ list->count = 0;
356
+ list->maximum = INT_MAX;
357
+ }
358
+
359
+ static void object_list_allocate(object_list* list, size_t allocate_count)
360
+ {
361
+ if (!list->strings && (list->allocated == 0) && (allocate_count > 0))
362
+ {
363
+ list->strings = (char**)calloc(allocate_count, sizeof(list->strings[0]));
364
+ list->allocated = list->strings ? allocate_count : 0;
365
+ list->count = 0;
366
+ }
367
+ }
368
+
369
+ static char* object_string(ASN1_TYPE* object)
370
+ {
371
+ char* result = NULL;
372
+ unsigned char* utf8String = NULL;
373
+
374
+ /* TODO: check that object.type is a string type. */
375
+ const int length = ASN1_STRING_to_UTF8(&utf8String, object->value.asn1_string);
376
+
377
+ if (length < 0)
378
+ {
379
+ return 0;
380
+ }
381
+
382
+ result = strndup((char*)utf8String, WINPR_ASSERTING_INT_CAST(size_t, length));
383
+ OPENSSL_free(utf8String);
384
+ return result;
385
+ }
386
+
387
+ static void object_list_free(object_list* list)
388
+ {
389
+ WINPR_ASSERT(list);
390
+ free((void*)list->strings);
391
+ }
392
+
393
+ static int extract_othername_object_as_string(GENERAL_NAME* name, void* data, int index, int count)
394
+ {
395
+ object_list* list = data;
396
+ WINPR_UNUSED(index);
397
+
398
+ if (count < 0)
399
+ return -1;
400
+
401
+ if (name->type != GEN_OTHERNAME)
402
+ {
403
+ return 1;
404
+ }
405
+
406
+ if (0 != OBJ_cmp(name->d.otherName->type_id, list->type_id))
407
+ {
408
+ return 1;
409
+ }
410
+
411
+ object_list_allocate(list, WINPR_ASSERTING_INT_CAST(size_t, count));
412
+
413
+ if (list->allocated <= 0)
414
+ {
415
+ return 0;
416
+ }
417
+
418
+ list->strings[list->count] = object_string(name->d.otherName->value);
419
+
420
+ if (list->strings[list->count])
421
+ {
422
+ list->count++;
423
+ }
424
+
425
+ if (list->count >= list->maximum)
426
+ {
427
+ return 0;
428
+ }
429
+
430
+ return 1;
431
+ }
432
+
433
+ char* x509_utils_get_email(const X509* x509)
434
+ {
435
+ char* result = 0;
436
+ string_list list;
437
+ string_list_initialize(&list);
438
+ list.maximum = 1;
439
+ map_subject_alt_name(x509, GEN_EMAIL, extract_string, &list);
440
+
441
+ if (list.count == 0)
442
+ {
443
+ string_list_free(&list);
444
+ return 0;
445
+ }
446
+
447
+ result = _strdup(list.strings[0]);
448
+ OPENSSL_free(list.strings[0]);
449
+ string_list_free(&list);
450
+ return result;
451
+ }
452
+
453
+ char* x509_utils_get_upn(const X509* x509)
454
+ {
455
+ char* result = 0;
456
+ object_list list = { 0 };
457
+ object_list_initialize(&list);
458
+ list.type_id = OBJ_nid2obj(NID_ms_upn);
459
+ list.maximum = 1;
460
+ map_subject_alt_name(x509, GEN_OTHERNAME, extract_othername_object_as_string, &list);
461
+
462
+ if (list.count == 0)
463
+ {
464
+ object_list_free(&list);
465
+ return 0;
466
+ }
467
+
468
+ result = list.strings[0];
469
+ object_list_free(&list);
470
+ return result;
471
+ }
472
+
473
+ char* x509_utils_get_date(const X509* x509, BOOL startDate)
474
+ {
475
+ WINPR_ASSERT(x509);
476
+
477
+ const ASN1_TIME* date = startDate ? X509_get0_notBefore(x509) : X509_get0_notAfter(x509);
478
+ if (!date)
479
+ return NULL;
480
+
481
+ BIO* bmem = BIO_new(BIO_s_mem());
482
+ if (!bmem)
483
+ return NULL;
484
+
485
+ char* str = NULL;
486
+ if (ASN1_TIME_print(bmem, date))
487
+ {
488
+ BUF_MEM* bptr = NULL;
489
+
490
+ BIO_get_mem_ptr(bmem, &bptr);
491
+ str = strndup(bptr->data, bptr->length);
492
+ }
493
+ else
494
+ { // Log error
495
+ }
496
+ BIO_free_all(bmem);
497
+ return str;
498
+ }
499
+
500
+ void x509_utils_dns_names_free(size_t count, size_t* lengths, char** dns_names)
501
+ {
502
+ free(lengths);
503
+
504
+ if (dns_names)
505
+ {
506
+ for (size_t i = 0; i < count; i++)
507
+ {
508
+ if (dns_names[i])
509
+ {
510
+ OPENSSL_free(dns_names[i]);
511
+ }
512
+ }
513
+
514
+ free((void*)dns_names);
515
+ }
516
+ }
517
+
518
+ char** x509_utils_get_dns_names(const X509* x509, size_t* count, size_t** lengths)
519
+ {
520
+ char** result = 0;
521
+ string_list list = { 0 };
522
+ string_list_initialize(&list);
523
+ map_subject_alt_name(x509, GEN_DNS, extract_string, &list);
524
+ (*count) = list.count;
525
+
526
+ if (list.count <= 0)
527
+ {
528
+ string_list_free(&list);
529
+ return NULL;
530
+ }
531
+
532
+ /* lengths are not useful, since we converted the
533
+ strings to utf-8, there cannot be nul-bytes in them. */
534
+ result = (char**)calloc(list.count, sizeof(*result));
535
+ (*lengths) = calloc(list.count, sizeof(**lengths));
536
+
537
+ if (!result || !(*lengths))
538
+ {
539
+ string_list_free(&list);
540
+ free((void*)result);
541
+ free(*lengths);
542
+ (*lengths) = 0;
543
+ (*count) = 0;
544
+ return NULL;
545
+ }
546
+
547
+ for (size_t i = 0; i < list.count; i++)
548
+ {
549
+ result[i] = list.strings[i];
550
+ (*lengths)[i] = strlen(result[i]);
551
+ }
552
+
553
+ string_list_free(&list);
554
+ return result;
555
+ }
556
+
557
+ char* x509_utils_get_issuer(const X509* xcert)
558
+ {
559
+ char* issuer = NULL;
560
+ if (!xcert)
561
+ {
562
+ WLog_ERR(TAG, "Invalid certificate %p", xcert);
563
+ return NULL;
564
+ }
565
+ issuer = crypto_print_name(X509_get_issuer_name(xcert));
566
+ if (!issuer)
567
+ WLog_WARN(TAG, "certificate does not have an issuer!");
568
+ return issuer;
569
+ }
570
+
571
+ BOOL x509_utils_check_eku(const X509* xcert, int nid)
572
+ {
573
+ BOOL ret = FALSE;
574
+ STACK_OF(ASN1_OBJECT)* oid_stack = NULL;
575
+ ASN1_OBJECT* oid = NULL;
576
+
577
+ if (!xcert)
578
+ return FALSE;
579
+
580
+ oid = OBJ_nid2obj(nid);
581
+ if (!oid)
582
+ return FALSE;
583
+
584
+ oid_stack = X509_get_ext_d2i(xcert, NID_ext_key_usage, NULL, NULL);
585
+ if (!oid_stack)
586
+ return FALSE;
587
+
588
+ if (sk_ASN1_OBJECT_find(oid_stack, oid) >= 0)
589
+ ret = TRUE;
590
+
591
+ sk_ASN1_OBJECT_pop_free(oid_stack, ASN1_OBJECT_free);
592
+ return ret;
593
+ }
594
+
595
+ void x509_utils_print_info(const X509* xcert)
596
+ {
597
+ char* fp = NULL;
598
+ char* issuer = NULL;
599
+ char* subject = NULL;
600
+ subject = x509_utils_get_subject(xcert);
601
+ issuer = x509_utils_get_issuer(xcert);
602
+ fp = (char*)x509_utils_get_hash(xcert, "sha256", NULL);
603
+
604
+ if (!fp)
605
+ {
606
+ WLog_ERR(TAG, "error computing fingerprint");
607
+ goto out_free_issuer;
608
+ }
609
+
610
+ WLog_INFO(TAG, "Certificate details:");
611
+ WLog_INFO(TAG, "\tSubject: %s", subject);
612
+ WLog_INFO(TAG, "\tIssuer: %s", issuer);
613
+ WLog_INFO(TAG, "\tThumbprint: %s", fp);
614
+ WLog_INFO(TAG,
615
+ "The above X.509 certificate could not be verified, possibly because you do not have "
616
+ "the CA certificate in your certificate store, or the certificate has expired. "
617
+ "Please look at the OpenSSL documentation on how to add a private CA to the store.");
618
+ free(fp);
619
+ out_free_issuer:
620
+ free(issuer);
621
+ free(subject);
622
+ }
623
+
624
+ X509* x509_utils_from_pem(const char* data, size_t len, BOOL fromFile)
625
+ {
626
+ X509* x509 = NULL;
627
+ BIO* bio = NULL;
628
+ if (fromFile)
629
+ bio = BIO_new_file(data, "rb");
630
+ else
631
+ {
632
+ if (len > INT_MAX)
633
+ return NULL;
634
+
635
+ bio = BIO_new_mem_buf(data, (int)len);
636
+ }
637
+
638
+ if (!bio)
639
+ {
640
+ WLog_ERR(TAG, "BIO_new failed for certificate");
641
+ return NULL;
642
+ }
643
+
644
+ x509 = PEM_read_bio_X509(bio, NULL, NULL, 0);
645
+ BIO_free_all(bio);
646
+ if (!x509)
647
+ WLog_ERR(TAG, "PEM_read_bio_X509 returned NULL [input length %" PRIuz "]", len);
648
+
649
+ return x509;
650
+ }
651
+
652
+ static WINPR_MD_TYPE hash_nid_to_winpr(int hash_nid)
653
+ {
654
+ switch (hash_nid)
655
+ {
656
+ case NID_md2:
657
+ return WINPR_MD_MD2;
658
+ case NID_md4:
659
+ return WINPR_MD_MD4;
660
+ case NID_md5:
661
+ return WINPR_MD_MD5;
662
+ case NID_sha1:
663
+ return WINPR_MD_SHA1;
664
+ case NID_sha224:
665
+ return WINPR_MD_SHA224;
666
+ case NID_sha256:
667
+ return WINPR_MD_SHA256;
668
+ case NID_sha384:
669
+ return WINPR_MD_SHA384;
670
+ case NID_sha512:
671
+ return WINPR_MD_SHA512;
672
+ case NID_ripemd160:
673
+ return WINPR_MD_RIPEMD160;
674
+ #if (OPENSSL_VERSION_NUMBER >= 0x1010101fL) && !defined(LIBRESSL_VERSION_NUMBER)
675
+ case NID_sha3_224:
676
+ return WINPR_MD_SHA3_224;
677
+ case NID_sha3_256:
678
+ return WINPR_MD_SHA3_256;
679
+ case NID_sha3_384:
680
+ return WINPR_MD_SHA3_384;
681
+ case NID_sha3_512:
682
+ return WINPR_MD_SHA3_512;
683
+ case NID_shake128:
684
+ return WINPR_MD_SHAKE128;
685
+ case NID_shake256:
686
+ return WINPR_MD_SHAKE256;
687
+ #endif
688
+ case NID_undef:
689
+ default:
690
+ return WINPR_MD_NONE;
691
+ }
692
+ }
693
+
694
+ static WINPR_MD_TYPE get_rsa_pss_digest(const X509_ALGOR* alg)
695
+ {
696
+ WINPR_MD_TYPE ret = WINPR_MD_NONE;
697
+ WINPR_MD_TYPE message_digest = WINPR_MD_NONE;
698
+ WINPR_MD_TYPE mgf1_digest = WINPR_MD_NONE;
699
+ int param_type = 0;
700
+ const void* param_value = NULL;
701
+ const ASN1_STRING* sequence = NULL;
702
+ const unsigned char* inp = NULL;
703
+ RSA_PSS_PARAMS* params = NULL;
704
+ X509_ALGOR* mgf1_digest_alg = NULL;
705
+
706
+ /* The RSA-PSS digest is encoded in a complex structure, defined in
707
+ https://www.rfc-editor.org/rfc/rfc4055.html. */
708
+ X509_ALGOR_get0(NULL, &param_type, &param_value, alg);
709
+
710
+ /* param_type and param_value the parameter in ASN1_TYPE form, but split into two parameters. A
711
+ SEQUENCE is has type V_ASN1_SEQUENCE, and the value is an ASN1_STRING with the encoded
712
+ structure. */
713
+ if (param_type != V_ASN1_SEQUENCE)
714
+ goto end;
715
+ sequence = param_value;
716
+
717
+ /* Decode the structure. */
718
+ inp = ASN1_STRING_get0_data(sequence);
719
+ params = d2i_RSA_PSS_PARAMS(NULL, &inp, ASN1_STRING_length(sequence));
720
+ if (params == NULL)
721
+ goto end;
722
+
723
+ /* RSA-PSS uses two hash algorithms, a message digest and also an MGF function which is, itself,
724
+ parameterized by a hash function. Both fields default to SHA-1, so we must also check for the
725
+ value being NULL. */
726
+ message_digest = WINPR_MD_SHA1;
727
+ if (params->hashAlgorithm != NULL)
728
+ {
729
+ const ASN1_OBJECT* obj = NULL;
730
+ X509_ALGOR_get0(&obj, NULL, NULL, params->hashAlgorithm);
731
+ message_digest = hash_nid_to_winpr(OBJ_obj2nid(obj));
732
+ if (message_digest == WINPR_MD_NONE)
733
+ goto end;
734
+ }
735
+
736
+ mgf1_digest = WINPR_MD_SHA1;
737
+ if (params->maskGenAlgorithm != NULL)
738
+ {
739
+ const ASN1_OBJECT* obj = NULL;
740
+ int mgf_param_type = 0;
741
+ const void* mgf_param_value = NULL;
742
+ const ASN1_STRING* mgf_param_sequence = NULL;
743
+ /* First, check this is MGF-1, the only one ever defined. */
744
+ X509_ALGOR_get0(&obj, &mgf_param_type, &mgf_param_value, params->maskGenAlgorithm);
745
+ if (OBJ_obj2nid(obj) != NID_mgf1)
746
+ goto end;
747
+
748
+ /* MGF-1 is, itself, parameterized by a hash function, encoded as an AlgorithmIdentifier. */
749
+ if (mgf_param_type != V_ASN1_SEQUENCE)
750
+ goto end;
751
+ mgf_param_sequence = mgf_param_value;
752
+ inp = ASN1_STRING_get0_data(mgf_param_sequence);
753
+ mgf1_digest_alg = d2i_X509_ALGOR(NULL, &inp, ASN1_STRING_length(mgf_param_sequence));
754
+ if (mgf1_digest_alg == NULL)
755
+ goto end;
756
+
757
+ /* Finally, extract the digest. */
758
+ X509_ALGOR_get0(&obj, NULL, NULL, mgf1_digest_alg);
759
+ mgf1_digest = hash_nid_to_winpr(OBJ_obj2nid(obj));
760
+ if (mgf1_digest == WINPR_MD_NONE)
761
+ goto end;
762
+ }
763
+
764
+ /* If the two digests do not match, it is ambiguous which to return. tls-server-end-point leaves
765
+ it undefined, so return none.
766
+ https://www.rfc-editor.org/rfc/rfc5929.html#section-4.1 */
767
+ if (message_digest != mgf1_digest)
768
+ goto end;
769
+ ret = message_digest;
770
+
771
+ end:
772
+ RSA_PSS_PARAMS_free(params);
773
+ X509_ALGOR_free(mgf1_digest_alg);
774
+ return ret;
775
+ }
776
+
777
+ WINPR_MD_TYPE x509_utils_get_signature_alg(const X509* xcert)
778
+ {
779
+ WINPR_ASSERT(xcert);
780
+
781
+ const int nid = X509_get_signature_nid(xcert);
782
+
783
+ if (nid == NID_rsassaPss)
784
+ {
785
+ const X509_ALGOR* alg = NULL;
786
+ X509_get0_signature(NULL, &alg, xcert);
787
+ return get_rsa_pss_digest(alg);
788
+ }
789
+
790
+ int hash_nid = 0;
791
+ if (OBJ_find_sigid_algs(nid, &hash_nid, NULL) != 1)
792
+ return WINPR_MD_NONE;
793
+
794
+ return hash_nid_to_winpr(hash_nid);
795
+ }
796
+
797
+ char* x509_utils_get_common_name(const X509* xcert, size_t* plength)
798
+ {
799
+ X509_NAME* subject_name = X509_get_subject_name(xcert);
800
+ if (subject_name == NULL)
801
+ return NULL;
802
+
803
+ const int index = X509_NAME_get_index_by_NID(subject_name, NID_commonName, -1);
804
+ if (index < 0)
805
+ return NULL;
806
+
807
+ const X509_NAME_ENTRY* entry = X509_NAME_get_entry(subject_name, index);
808
+ if (entry == NULL)
809
+ return NULL;
810
+
811
+ const ASN1_STRING* entry_data = X509_NAME_ENTRY_get_data(entry);
812
+ if (entry_data == NULL)
813
+ return NULL;
814
+
815
+ BYTE* common_name_raw = NULL;
816
+ const int length = ASN1_STRING_to_UTF8(&common_name_raw, entry_data);
817
+ if (length < 0)
818
+ return NULL;
819
+
820
+ if (plength)
821
+ *plength = (size_t)length;
822
+
823
+ char* common_name = _strdup((char*)common_name_raw);
824
+ OPENSSL_free(common_name_raw);
825
+ return common_name;
826
+ }
827
+
828
+ static int verify_cb(int ok, X509_STORE_CTX* csc)
829
+ {
830
+ if (ok != 1)
831
+ {
832
+ WINPR_ASSERT(csc);
833
+ int err = X509_STORE_CTX_get_error(csc);
834
+ int derr = X509_STORE_CTX_get_error_depth(csc);
835
+ X509* where = X509_STORE_CTX_get_current_cert(csc);
836
+ const char* what = X509_verify_cert_error_string(err);
837
+ char* name = x509_utils_get_subject(where);
838
+
839
+ WLog_WARN(TAG, "Certificate verification failure '%s (%d)' at stack position %d", what, err,
840
+ derr);
841
+ WLog_WARN(TAG, "%s", name);
842
+
843
+ free(name);
844
+ }
845
+ return ok;
846
+ }
847
+
848
+ BOOL x509_utils_verify(X509* xcert, STACK_OF(X509) * chain, const char* certificate_store_path)
849
+ {
850
+ const int purposes[3] = { X509_PURPOSE_SSL_SERVER, X509_PURPOSE_SSL_CLIENT, X509_PURPOSE_ANY };
851
+ X509_STORE_CTX* csc = NULL;
852
+ BOOL status = FALSE;
853
+ X509_LOOKUP* lookup = NULL;
854
+
855
+ if (!xcert)
856
+ return FALSE;
857
+
858
+ X509_STORE* cert_ctx = X509_STORE_new();
859
+
860
+ if (cert_ctx == NULL)
861
+ goto end;
862
+
863
+ #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
864
+ OpenSSL_add_all_algorithms();
865
+ #else
866
+ OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS | OPENSSL_INIT_ADD_ALL_DIGESTS |
867
+ OPENSSL_INIT_LOAD_CONFIG,
868
+ NULL);
869
+ #endif
870
+
871
+ if (X509_STORE_set_default_paths(cert_ctx) != 1)
872
+ goto end;
873
+
874
+ lookup = X509_STORE_add_lookup(cert_ctx, X509_LOOKUP_hash_dir());
875
+
876
+ if (lookup == NULL)
877
+ goto end;
878
+
879
+ X509_LOOKUP_add_dir(lookup, NULL, X509_FILETYPE_DEFAULT);
880
+
881
+ if (certificate_store_path != NULL)
882
+ {
883
+ X509_LOOKUP_add_dir(lookup, certificate_store_path, X509_FILETYPE_PEM);
884
+ }
885
+
886
+ X509_STORE_set_flags(cert_ctx, 0);
887
+
888
+ for (size_t i = 0; i < ARRAYSIZE(purposes); i++)
889
+ {
890
+ int err = -1;
891
+ int rc = -1;
892
+ int purpose = purposes[i];
893
+ csc = X509_STORE_CTX_new();
894
+
895
+ if (csc == NULL)
896
+ goto skip;
897
+ if (!X509_STORE_CTX_init(csc, cert_ctx, xcert, chain))
898
+ goto skip;
899
+
900
+ X509_STORE_CTX_set_purpose(csc, purpose);
901
+ X509_STORE_CTX_set_verify_cb(csc, verify_cb);
902
+
903
+ rc = X509_verify_cert(csc);
904
+ err = X509_STORE_CTX_get_error(csc);
905
+ skip:
906
+ X509_STORE_CTX_free(csc);
907
+ if (rc == 1)
908
+ {
909
+ status = TRUE;
910
+ break;
911
+ }
912
+ else if (err != X509_V_ERR_INVALID_PURPOSE)
913
+ break;
914
+ }
915
+
916
+ X509_STORE_free(cert_ctx);
917
+ end:
918
+ return status;
919
+ }
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/crypto/x509_utils.h ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * Cryptographic Abstraction Layer
4
+ *
5
+ * Copyright 2011-2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
+ * Copyright 2023 Armin Novak <anovak@thincast.com>
7
+ * Copyright 2023 Thincast Technologies GmbH
8
+ *
9
+ * Licensed under the Apache License, Version 2.0 (the "License");
10
+ * you may not use this file except in compliance with the License.
11
+ * You may obtain a copy of the License at
12
+ *
13
+ * http://www.apache.org/licenses/LICENSE-2.0
14
+ *
15
+ * Unless required by applicable law or agreed to in writing, software
16
+ * distributed under the License is distributed on an "AS IS" BASIS,
17
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ * See the License for the specific language governing permissions and
19
+ * limitations under the License.
20
+ */
21
+
22
+ #ifndef FREERDP_LIB_X509_UTILS_H
23
+ #define FREERDP_LIB_X509_UTILS_H
24
+
25
+ #include <winpr/custom-crypto.h>
26
+
27
+ #include <openssl/x509.h>
28
+
29
+ #include <freerdp/api.h>
30
+
31
+ #ifdef __cplusplus
32
+ extern "C"
33
+ {
34
+ #endif
35
+
36
+ FREERDP_LOCAL WINPR_MD_TYPE x509_utils_get_signature_alg(const X509* xcert);
37
+ FREERDP_LOCAL BYTE* x509_utils_get_hash(const X509* xcert, const char* hash, size_t* length);
38
+
39
+ FREERDP_LOCAL BYTE* x509_utils_to_pem(const X509* xcert, const STACK_OF(X509) * chain,
40
+ size_t* length);
41
+ FREERDP_LOCAL X509* x509_utils_from_pem(const char* data, size_t length, BOOL fromFile);
42
+
43
+ FREERDP_LOCAL char* x509_utils_get_subject(const X509* xcert);
44
+ FREERDP_LOCAL char* x509_utils_get_issuer(const X509* xcert);
45
+ FREERDP_LOCAL char* x509_utils_get_email(const X509* x509);
46
+ FREERDP_LOCAL char* x509_utils_get_upn(const X509* x509);
47
+ FREERDP_LOCAL char* x509_utils_get_date(const X509* x509, BOOL startDate);
48
+
49
+ FREERDP_LOCAL char* x509_utils_get_common_name(const X509* xcert, size_t* plength);
50
+ FREERDP_LOCAL char** x509_utils_get_dns_names(const X509* xcert, size_t* count,
51
+ size_t** pplengths);
52
+
53
+ FREERDP_LOCAL void x509_utils_dns_names_free(size_t count, size_t* lengths, char** dns_names);
54
+
55
+ FREERDP_LOCAL BOOL x509_utils_check_eku(const X509* scert, int nid);
56
+ FREERDP_LOCAL void x509_utils_print_info(const X509* xcert);
57
+
58
+ FREERDP_LOCAL BOOL x509_utils_verify(X509* xcert, STACK_OF(X509) * chain,
59
+ const char* certificate_store_path);
60
+
61
+ #ifdef __cplusplus
62
+ }
63
+ #endif
64
+
65
+ #endif /* FREERDP_LIB_X509_UTILS_H */
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/gdi/test/TestGdiClip.c ADDED
@@ -0,0 +1,345 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ #include <freerdp/gdi/gdi.h>
3
+
4
+ #include <freerdp/gdi/dc.h>
5
+ #include <freerdp/gdi/pen.h>
6
+ #include <freerdp/gdi/region.h>
7
+ #include <freerdp/gdi/bitmap.h>
8
+
9
+ #include <winpr/crt.h>
10
+
11
+ #include "line.h"
12
+ #include "brush.h"
13
+ #include "clipping.h"
14
+
15
+ static int test_gdi_ClipCoords(void)
16
+ {
17
+ int rc = -1;
18
+ BOOL draw = 0;
19
+ HGDI_DC hdc = NULL;
20
+ HGDI_RGN rgn1 = NULL;
21
+ HGDI_RGN rgn2 = NULL;
22
+ HGDI_BITMAP bmp = NULL;
23
+ const UINT32 format = PIXEL_FORMAT_ARGB32;
24
+
25
+ if (!(hdc = gdi_GetDC()))
26
+ {
27
+ printf("failed to get gdi device context\n");
28
+ return -1;
29
+ }
30
+
31
+ hdc->format = format;
32
+ bmp = gdi_CreateBitmapEx(1024, 768, PIXEL_FORMAT_XRGB32, 0, NULL, NULL);
33
+ gdi_SelectObject(hdc, (HGDIOBJECT)bmp);
34
+ gdi_SetNullClipRgn(hdc);
35
+ rgn1 = gdi_CreateRectRgn(0, 0, 0, 0);
36
+ rgn2 = gdi_CreateRectRgn(0, 0, 0, 0);
37
+ rgn1->null = TRUE;
38
+ rgn2->null = TRUE;
39
+ /* null clipping region */
40
+ gdi_SetNullClipRgn(hdc);
41
+ gdi_SetRgn(rgn1, 20, 20, 100, 100);
42
+ gdi_SetRgn(rgn2, 20, 20, 100, 100);
43
+ gdi_ClipCoords(hdc, &(rgn1->x), &(rgn1->y), &(rgn1->w), &(rgn1->h), NULL, NULL);
44
+
45
+ if (!gdi_EqualRgn(rgn1, rgn2))
46
+ goto fail;
47
+
48
+ /* region all inside clipping region */
49
+ gdi_SetClipRgn(hdc, 0, 0, 1024, 768);
50
+ gdi_SetRgn(rgn1, 20, 20, 100, 100);
51
+ gdi_SetRgn(rgn2, 20, 20, 100, 100);
52
+ gdi_ClipCoords(hdc, &(rgn1->x), &(rgn1->y), &(rgn1->w), &(rgn1->h), NULL, NULL);
53
+
54
+ if (!gdi_EqualRgn(rgn1, rgn2))
55
+ goto fail;
56
+
57
+ /* region all outside clipping region, on the left */
58
+ gdi_SetClipRgn(hdc, 300, 300, 100, 100);
59
+ gdi_SetRgn(rgn1, 20, 20, 100, 100);
60
+ gdi_SetRgn(rgn2, 0, 0, 0, 0);
61
+ draw = gdi_ClipCoords(hdc, &(rgn1->x), &(rgn1->y), &(rgn1->w), &(rgn1->h), NULL, NULL);
62
+
63
+ if (draw)
64
+ goto fail;
65
+
66
+ /* region all outside clipping region, on the right */
67
+ gdi_SetClipRgn(hdc, 300, 300, 100, 100);
68
+ gdi_SetRgn(rgn1, 420, 420, 100, 100);
69
+ gdi_SetRgn(rgn2, 0, 0, 0, 0);
70
+ draw = gdi_ClipCoords(hdc, &(rgn1->x), &(rgn1->y), &(rgn1->w), &(rgn1->h), NULL, NULL);
71
+
72
+ if (draw)
73
+ goto fail;
74
+
75
+ /* region all outside clipping region, on top */
76
+ gdi_SetClipRgn(hdc, 300, 300, 100, 100);
77
+ gdi_SetRgn(rgn1, 300, 20, 100, 100);
78
+ gdi_SetRgn(rgn2, 0, 0, 0, 0);
79
+ draw = gdi_ClipCoords(hdc, &(rgn1->x), &(rgn1->y), &(rgn1->w), &(rgn1->h), NULL, NULL);
80
+
81
+ if (draw)
82
+ goto fail;
83
+
84
+ /* region all outside clipping region, at the bottom */
85
+ gdi_SetClipRgn(hdc, 300, 300, 100, 100);
86
+ gdi_SetRgn(rgn1, 300, 420, 100, 100);
87
+ gdi_SetRgn(rgn2, 0, 0, 0, 0);
88
+ draw = gdi_ClipCoords(hdc, &(rgn1->x), &(rgn1->y), &(rgn1->w), &(rgn1->h), NULL, NULL);
89
+
90
+ if (draw)
91
+ goto fail;
92
+
93
+ /* left outside, right = clip, top = clip, bottom = clip */
94
+ gdi_SetClipRgn(hdc, 300, 300, 100, 100);
95
+ gdi_SetRgn(rgn1, 100, 300, 300, 100);
96
+ gdi_SetRgn(rgn2, 300, 300, 100, 100);
97
+ gdi_ClipCoords(hdc, &(rgn1->x), &(rgn1->y), &(rgn1->w), &(rgn1->h), NULL, NULL);
98
+
99
+ if (!gdi_EqualRgn(rgn1, rgn2))
100
+ goto fail;
101
+
102
+ /* left outside, right inside, top = clip, bottom = clip */
103
+ gdi_SetClipRgn(hdc, 300, 300, 100, 100);
104
+ gdi_SetRgn(rgn1, 100, 300, 250, 100);
105
+ gdi_SetRgn(rgn2, 300, 300, 50, 100);
106
+ gdi_ClipCoords(hdc, &(rgn1->x), &(rgn1->y), &(rgn1->w), &(rgn1->h), NULL, NULL);
107
+
108
+ if (!gdi_EqualRgn(rgn1, rgn2))
109
+ goto fail;
110
+
111
+ /* left = clip, right outside, top = clip, bottom = clip */
112
+ gdi_SetClipRgn(hdc, 300, 300, 100, 100);
113
+ gdi_SetRgn(rgn1, 300, 300, 300, 100);
114
+ gdi_SetRgn(rgn2, 300, 300, 100, 100);
115
+ gdi_ClipCoords(hdc, &(rgn1->x), &(rgn1->y), &(rgn1->w), &(rgn1->h), NULL, NULL);
116
+
117
+ if (!gdi_EqualRgn(rgn1, rgn2))
118
+ goto fail;
119
+
120
+ /* left inside, right outside, top = clip, bottom = clip */
121
+ gdi_SetClipRgn(hdc, 300, 300, 100, 100);
122
+ gdi_SetRgn(rgn1, 350, 300, 200, 100);
123
+ gdi_SetRgn(rgn2, 350, 300, 50, 100);
124
+ gdi_ClipCoords(hdc, &(rgn1->x), &(rgn1->y), &(rgn1->w), &(rgn1->h), NULL, NULL);
125
+
126
+ if (!gdi_EqualRgn(rgn1, rgn2))
127
+ goto fail;
128
+
129
+ /* top outside, bottom = clip, left = clip, right = clip */
130
+ gdi_SetClipRgn(hdc, 300, 300, 100, 100);
131
+ gdi_SetRgn(rgn1, 300, 100, 300, 300);
132
+ gdi_SetRgn(rgn2, 300, 300, 100, 100);
133
+ gdi_ClipCoords(hdc, &(rgn1->x), &(rgn1->y), &(rgn1->w), &(rgn1->h), NULL, NULL);
134
+
135
+ if (!gdi_EqualRgn(rgn1, rgn2))
136
+ goto fail;
137
+
138
+ /* top = clip, bottom outside, left = clip, right = clip */
139
+ gdi_SetClipRgn(hdc, 300, 300, 100, 100);
140
+ gdi_SetRgn(rgn1, 300, 300, 100, 200);
141
+ gdi_SetRgn(rgn2, 300, 300, 100, 100);
142
+ gdi_ClipCoords(hdc, &(rgn1->x), &(rgn1->y), &(rgn1->w), &(rgn1->h), NULL, NULL);
143
+
144
+ if (!gdi_EqualRgn(rgn1, rgn2))
145
+ goto fail;
146
+
147
+ /* top = clip, bottom = clip, top = clip, bottom = clip */
148
+ gdi_SetClipRgn(hdc, 300, 300, 100, 100);
149
+ gdi_SetRgn(rgn1, 300, 300, 100, 100);
150
+ gdi_SetRgn(rgn2, 300, 300, 100, 100);
151
+ gdi_ClipCoords(hdc, &(rgn1->x), &(rgn1->y), &(rgn1->w), &(rgn1->h), NULL, NULL);
152
+
153
+ if (!gdi_EqualRgn(rgn1, rgn2))
154
+ goto fail;
155
+
156
+ rc = 0;
157
+ fail:
158
+ gdi_DeleteObject((HGDIOBJECT)rgn1);
159
+ gdi_DeleteObject((HGDIOBJECT)rgn2);
160
+ gdi_DeleteObject((HGDIOBJECT)bmp);
161
+ gdi_DeleteDC(hdc);
162
+ return rc;
163
+ }
164
+
165
+ static int test_gdi_InvalidateRegion(void)
166
+ {
167
+ int rc = -1;
168
+ HGDI_DC hdc = NULL;
169
+ HGDI_RGN rgn1 = NULL;
170
+ HGDI_RGN rgn2 = NULL;
171
+ HGDI_RGN invalid = NULL;
172
+ HGDI_BITMAP bmp = NULL;
173
+ const UINT32 format = PIXEL_FORMAT_XRGB32;
174
+
175
+ if (!(hdc = gdi_GetDC()))
176
+ {
177
+ printf("failed to get gdi device context\n");
178
+ return -1;
179
+ }
180
+
181
+ hdc->format = format;
182
+ bmp = gdi_CreateBitmapEx(1024, 768, PIXEL_FORMAT_XRGB32, 0, NULL, NULL);
183
+ gdi_SelectObject(hdc, (HGDIOBJECT)bmp);
184
+ gdi_SetNullClipRgn(hdc);
185
+ hdc->hwnd = (HGDI_WND)calloc(1, sizeof(GDI_WND));
186
+ hdc->hwnd->invalid = gdi_CreateRectRgn(0, 0, 0, 0);
187
+ hdc->hwnd->invalid->null = TRUE;
188
+ invalid = hdc->hwnd->invalid;
189
+ hdc->hwnd->count = 16;
190
+ hdc->hwnd->cinvalid = (HGDI_RGN)calloc(hdc->hwnd->count, sizeof(GDI_RGN));
191
+ rgn1 = gdi_CreateRectRgn(0, 0, 0, 0);
192
+ rgn2 = gdi_CreateRectRgn(0, 0, 0, 0);
193
+ rgn1->null = TRUE;
194
+ rgn2->null = TRUE;
195
+ /* no previous invalid region */
196
+ invalid->null = TRUE;
197
+ gdi_SetRgn(rgn1, 300, 300, 100, 100);
198
+ gdi_SetRgn(rgn2, 300, 300, 100, 100);
199
+ gdi_InvalidateRegion(hdc, rgn1->x, rgn1->y, rgn1->w, rgn1->h);
200
+
201
+ if (!gdi_EqualRgn(invalid, rgn2))
202
+ goto fail;
203
+
204
+ /* region same as invalid region */
205
+ gdi_SetRgn(invalid, 300, 300, 100, 100);
206
+ gdi_SetRgn(rgn1, 300, 300, 100, 100);
207
+ gdi_SetRgn(rgn2, 300, 300, 100, 100);
208
+ gdi_InvalidateRegion(hdc, rgn1->x, rgn1->y, rgn1->w, rgn1->h);
209
+
210
+ if (!gdi_EqualRgn(invalid, rgn2))
211
+ goto fail;
212
+
213
+ /* left outside */
214
+ gdi_SetRgn(invalid, 300, 300, 100, 100);
215
+ gdi_SetRgn(rgn1, 100, 300, 300, 100);
216
+ gdi_SetRgn(rgn2, 100, 300, 300, 100);
217
+ gdi_InvalidateRegion(hdc, rgn1->x, rgn1->y, rgn1->w, rgn1->h);
218
+
219
+ if (!gdi_EqualRgn(invalid, rgn2))
220
+ goto fail;
221
+
222
+ /* right outside */
223
+ gdi_SetRgn(invalid, 300, 300, 100, 100);
224
+ gdi_SetRgn(rgn1, 300, 300, 300, 100);
225
+ gdi_SetRgn(rgn2, 300, 300, 300, 100);
226
+ gdi_InvalidateRegion(hdc, rgn1->x, rgn1->y, rgn1->w, rgn1->h);
227
+
228
+ if (!gdi_EqualRgn(invalid, rgn2))
229
+ goto fail;
230
+
231
+ /* top outside */
232
+ gdi_SetRgn(invalid, 300, 300, 100, 100);
233
+ gdi_SetRgn(rgn1, 300, 100, 100, 300);
234
+ gdi_SetRgn(rgn2, 300, 100, 100, 300);
235
+ gdi_InvalidateRegion(hdc, rgn1->x, rgn1->y, rgn1->w, rgn1->h);
236
+
237
+ if (!gdi_EqualRgn(invalid, rgn2))
238
+ goto fail;
239
+
240
+ /* bottom outside */
241
+ gdi_SetRgn(invalid, 300, 300, 100, 100);
242
+ gdi_SetRgn(rgn1, 300, 300, 100, 300);
243
+ gdi_SetRgn(rgn2, 300, 300, 100, 300);
244
+ gdi_InvalidateRegion(hdc, rgn1->x, rgn1->y, rgn1->w, rgn1->h);
245
+
246
+ if (!gdi_EqualRgn(invalid, rgn2))
247
+ goto fail;
248
+
249
+ /* left outside, right outside */
250
+ gdi_SetRgn(invalid, 300, 300, 100, 100);
251
+ gdi_SetRgn(rgn1, 100, 300, 600, 300);
252
+ gdi_SetRgn(rgn2, 100, 300, 600, 300);
253
+ gdi_InvalidateRegion(hdc, rgn1->x, rgn1->y, rgn1->w, rgn1->h);
254
+
255
+ if (!gdi_EqualRgn(invalid, rgn2))
256
+ goto fail;
257
+
258
+ /* top outside, bottom outside */
259
+ gdi_SetRgn(invalid, 300, 300, 100, 100);
260
+ gdi_SetRgn(rgn1, 300, 100, 100, 500);
261
+ gdi_SetRgn(rgn2, 300, 100, 100, 500);
262
+ gdi_InvalidateRegion(hdc, rgn1->x, rgn1->y, rgn1->w, rgn1->h);
263
+
264
+ if (!gdi_EqualRgn(invalid, rgn2))
265
+ goto fail;
266
+
267
+ /* all outside, left */
268
+ gdi_SetRgn(invalid, 300, 300, 100, 100);
269
+ gdi_SetRgn(rgn1, 100, 300, 100, 100);
270
+ gdi_SetRgn(rgn2, 100, 300, 300, 100);
271
+ gdi_InvalidateRegion(hdc, rgn1->x, rgn1->y, rgn1->w, rgn1->h);
272
+
273
+ if (!gdi_EqualRgn(invalid, rgn2))
274
+ goto fail;
275
+
276
+ /* all outside, right */
277
+ gdi_SetRgn(invalid, 300, 300, 100, 100);
278
+ gdi_SetRgn(rgn1, 700, 300, 100, 100);
279
+ gdi_SetRgn(rgn2, 300, 300, 500, 100);
280
+ gdi_InvalidateRegion(hdc, rgn1->x, rgn1->y, rgn1->w, rgn1->h);
281
+
282
+ if (!gdi_EqualRgn(invalid, rgn2))
283
+ goto fail;
284
+
285
+ /* all outside, top */
286
+ gdi_SetRgn(invalid, 300, 300, 100, 100);
287
+ gdi_SetRgn(rgn1, 300, 100, 100, 100);
288
+ gdi_SetRgn(rgn2, 300, 100, 100, 300);
289
+ gdi_InvalidateRegion(hdc, rgn1->x, rgn1->y, rgn1->w, rgn1->h);
290
+
291
+ if (!gdi_EqualRgn(invalid, rgn2))
292
+ goto fail;
293
+
294
+ /* all outside, bottom */
295
+ gdi_SetRgn(invalid, 300, 300, 100, 100);
296
+ gdi_SetRgn(rgn1, 300, 500, 100, 100);
297
+ gdi_SetRgn(rgn2, 300, 300, 100, 300);
298
+ gdi_InvalidateRegion(hdc, rgn1->x, rgn1->y, rgn1->w, rgn1->h);
299
+
300
+ if (!gdi_EqualRgn(invalid, rgn2))
301
+ goto fail;
302
+
303
+ /* all outside */
304
+ gdi_SetRgn(invalid, 300, 300, 100, 100);
305
+ gdi_SetRgn(rgn1, 100, 100, 600, 600);
306
+ gdi_SetRgn(rgn2, 100, 100, 600, 600);
307
+ gdi_InvalidateRegion(hdc, rgn1->x, rgn1->y, rgn1->w, rgn1->h);
308
+
309
+ if (!gdi_EqualRgn(invalid, rgn2))
310
+ goto fail;
311
+
312
+ /* everything */
313
+ gdi_SetRgn(invalid, 300, 300, 100, 100);
314
+ gdi_SetRgn(rgn1, 0, 0, 1024, 768);
315
+ gdi_SetRgn(rgn2, 0, 0, 1024, 768);
316
+ gdi_InvalidateRegion(hdc, rgn1->x, rgn1->y, rgn1->w, rgn1->h);
317
+
318
+ if (!gdi_EqualRgn(invalid, rgn2))
319
+ goto fail;
320
+
321
+ rc = 0;
322
+ fail:
323
+ gdi_DeleteObject((HGDIOBJECT)rgn1);
324
+ gdi_DeleteObject((HGDIOBJECT)rgn2);
325
+ gdi_DeleteObject((HGDIOBJECT)bmp);
326
+ gdi_DeleteDC(hdc);
327
+ return rc;
328
+ }
329
+
330
+ int TestGdiClip(int argc, char* argv[])
331
+ {
332
+ WINPR_UNUSED(argc);
333
+ WINPR_UNUSED(argv);
334
+ (void)fprintf(stderr, "test_gdi_ClipCoords()\n");
335
+
336
+ if (test_gdi_ClipCoords() < 0)
337
+ return -1;
338
+
339
+ (void)fprintf(stderr, "test_gdi_InvalidateRegion()\n");
340
+
341
+ if (test_gdi_InvalidateRegion() < 0)
342
+ return -1;
343
+
344
+ return 0;
345
+ }
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/gdi/test/TestGdiEllipse.c ADDED
@@ -0,0 +1,169 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ #include <freerdp/gdi/gdi.h>
3
+
4
+ #include <freerdp/gdi/dc.h>
5
+ #include <freerdp/gdi/pen.h>
6
+ #include <freerdp/gdi/shape.h>
7
+ #include <freerdp/gdi/region.h>
8
+ #include <freerdp/gdi/bitmap.h>
9
+
10
+ #include <winpr/crt.h>
11
+ #include <winpr/print.h>
12
+
13
+ #include "line.h"
14
+ #include "brush.h"
15
+ #include "clipping.h"
16
+ #include "helpers.h"
17
+
18
+ /* Ellipse() Test Data */
19
+
20
+ static const BYTE ellipse_case_1[256] = {
21
+ "\xFF\xFF\xFF\xFF\xFF\x00\x00\x00\x00\x00\x00\xFF\xFF\xFF\xFF\xFF"
22
+ "\xFF\xFF\xFF\xFF\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF\xFF\xFF"
23
+ "\xFF\xFF\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF"
24
+ "\xFF\xFF\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF"
25
+ "\xFF\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF"
26
+ "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
27
+ "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
28
+ "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
29
+ "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
30
+ "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
31
+ "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
32
+ "\xFF\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF"
33
+ "\xFF\xFF\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF"
34
+ "\xFF\xFF\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF"
35
+ "\xFF\xFF\xFF\xFF\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF\xFF\xFF"
36
+ "\xFF\xFF\xFF\xFF\xFF\x00\x00\x00\x00\x00\x00\xFF\xFF\xFF\xFF\xFF"
37
+ };
38
+
39
+ static const BYTE ellipse_case_2[256] = {
40
+ "\xFF\xFF\xFF\xFF\xFF\xFF\x00\x00\x00\x00\xFF\xFF\xFF\xFF\xFF\xFF"
41
+ "\xFF\xFF\xFF\xFF\xFF\x00\x00\x00\x00\x00\x00\xFF\xFF\xFF\xFF\xFF"
42
+ "\xFF\xFF\xFF\xFF\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF\xFF\xFF"
43
+ "\xFF\xFF\xFF\xFF\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF\xFF\xFF"
44
+ "\xFF\xFF\xFF\xFF\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF\xFF\xFF"
45
+ "\xFF\xFF\xFF\xFF\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF\xFF\xFF"
46
+ "\xFF\xFF\xFF\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF\xFF"
47
+ "\xFF\xFF\xFF\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF\xFF"
48
+ "\xFF\xFF\xFF\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF\xFF"
49
+ "\xFF\xFF\xFF\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF\xFF"
50
+ "\xFF\xFF\xFF\xFF\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF\xFF\xFF"
51
+ "\xFF\xFF\xFF\xFF\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF\xFF\xFF"
52
+ "\xFF\xFF\xFF\xFF\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF\xFF\xFF"
53
+ "\xFF\xFF\xFF\xFF\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF\xFF\xFF"
54
+ "\xFF\xFF\xFF\xFF\xFF\x00\x00\x00\x00\x00\x00\xFF\xFF\xFF\xFF\xFF"
55
+ "\xFF\xFF\xFF\xFF\xFF\xFF\x00\x00\x00\x00\xFF\xFF\xFF\xFF\xFF\xFF"
56
+ };
57
+
58
+ static const BYTE ellipse_case_3[256] = {
59
+ "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"
60
+ "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"
61
+ "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"
62
+ "\xFF\xFF\xFF\xFF\xFF\xFF\x00\x00\x00\x00\xFF\xFF\xFF\xFF\xFF\xFF"
63
+ "\xFF\xFF\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF"
64
+ "\xFF\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF"
65
+ "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
66
+ "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
67
+ "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
68
+ "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
69
+ "\xFF\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF"
70
+ "\xFF\xFF\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF"
71
+ "\xFF\xFF\xFF\xFF\xFF\xFF\x00\x00\x00\x00\xFF\xFF\xFF\xFF\xFF\xFF"
72
+ "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"
73
+ "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"
74
+ "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"
75
+ };
76
+
77
+ int TestGdiEllipse(int argc, char* argv[])
78
+ {
79
+ int rc = -1;
80
+ const UINT32 RawFormat = PIXEL_FORMAT_RGB8;
81
+ const UINT32 colorFormats[] = { PIXEL_FORMAT_RGB15, PIXEL_FORMAT_ARGB15, PIXEL_FORMAT_RGB16,
82
+ PIXEL_FORMAT_RGB24, PIXEL_FORMAT_ARGB32, PIXEL_FORMAT_XRGB32,
83
+ PIXEL_FORMAT_RGBA32, PIXEL_FORMAT_RGBX32, PIXEL_FORMAT_BGR15,
84
+ PIXEL_FORMAT_ABGR15, PIXEL_FORMAT_BGR16, PIXEL_FORMAT_BGR24,
85
+ PIXEL_FORMAT_ABGR32, PIXEL_FORMAT_XBGR32, PIXEL_FORMAT_BGRA32,
86
+ PIXEL_FORMAT_BGRX32 };
87
+ const UINT32 number_formats = sizeof(colorFormats) / sizeof(colorFormats[0]);
88
+ gdiPalette g;
89
+ WINPR_UNUSED(argc);
90
+ WINPR_UNUSED(argv);
91
+
92
+ for (UINT32 i = 0; i < number_formats; i++)
93
+ {
94
+ HGDI_DC hdc = NULL;
95
+ HGDI_PEN pen = NULL;
96
+ HGDI_BITMAP hBmp = NULL;
97
+ HGDI_BITMAP hBmp_Ellipse_1 = NULL;
98
+ HGDI_BITMAP hBmp_Ellipse_2 = NULL;
99
+ HGDI_BITMAP hBmp_Ellipse_3 = NULL;
100
+ const UINT32 format = colorFormats[i];
101
+ gdiPalette* hPalette = &g;
102
+ g.format = format;
103
+
104
+ for (UINT32 j = 0; j < 256; j++)
105
+ g.palette[i] = FreeRDPGetColor(format, j, j, j, 0xFF);
106
+
107
+ rc = -1;
108
+
109
+ if (!(hdc = gdi_GetDC()))
110
+ {
111
+ printf("failed to get gdi device context\n");
112
+ goto fail;
113
+ }
114
+
115
+ hdc->format = format;
116
+ gdi_SetNullClipRgn(hdc);
117
+
118
+ if (!(pen = gdi_CreatePen(1, 1, 0, format, hPalette)))
119
+ {
120
+ printf("gdi_CreatePen failed\n");
121
+ goto fail;
122
+ }
123
+
124
+ gdi_SelectObject(hdc, (HGDIOBJECT)pen);
125
+ hBmp = gdi_CreateCompatibleBitmap(hdc, 16, 16);
126
+ gdi_SelectObject(hdc, (HGDIOBJECT)hBmp);
127
+ hBmp_Ellipse_1 = test_convert_to_bitmap(ellipse_case_1, RawFormat, 0, 0, 0, format, 0, 0, 0,
128
+ 16, 16, hPalette);
129
+
130
+ if (!hBmp_Ellipse_1)
131
+ goto fail;
132
+
133
+ hBmp_Ellipse_2 = test_convert_to_bitmap(ellipse_case_2, RawFormat, 0, 0, 0, format, 0, 0, 0,
134
+ 16, 16, hPalette);
135
+
136
+ if (!hBmp_Ellipse_2)
137
+ goto fail;
138
+
139
+ hBmp_Ellipse_3 = test_convert_to_bitmap(ellipse_case_3, RawFormat, 0, 0, 0, format, 0, 0, 0,
140
+ 16, 16, hPalette);
141
+
142
+ if (!hBmp_Ellipse_3)
143
+ goto fail;
144
+
145
+ /* Test Case 1: (0,0) -> (16, 16) */
146
+ if (!gdi_BitBlt(hdc, 0, 0, 16, 16, hdc, 0, 0, GDI_WHITENESS, hPalette))
147
+ {
148
+ printf("gdi_BitBlt failed (line #%u)\n", __LINE__);
149
+ goto fail;
150
+ }
151
+
152
+ if (!gdi_Ellipse(hdc, 0, 0, 15, 15))
153
+ goto fail;
154
+
155
+ rc = 0;
156
+ fail:
157
+ gdi_DeleteObject((HGDIOBJECT)hBmp_Ellipse_1);
158
+ gdi_DeleteObject((HGDIOBJECT)hBmp_Ellipse_2);
159
+ gdi_DeleteObject((HGDIOBJECT)hBmp_Ellipse_3);
160
+ gdi_DeleteObject((HGDIOBJECT)hBmp);
161
+ gdi_DeleteObject((HGDIOBJECT)pen);
162
+ gdi_DeleteDC(hdc);
163
+
164
+ if (rc != 0)
165
+ break;
166
+ }
167
+
168
+ return rc;
169
+ }
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/gdi/test/TestGdiLine.c ADDED
@@ -0,0 +1,724 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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erdp/gdi/gdi.h>
2
+
3
+ #include <freerdp/gdi/dc.h>
4
+ #include <freerdp/gdi/pen.h>
5
+ #include <freerdp/gdi/region.h>
6
+ #include <freerdp/gdi/bitmap.h>
7
+
8
+ #include <winpr/crt.h>
9
+ #include <winpr/print.h>
10
+
11
+ #include "line.h"
12
+ #include "brush.h"
13
+ #include "clipping.h"
14
+ #include "drawing.h"
15
+
16
+ #include "helpers.h"
17
+
18
+ /* LineTo() Test Data */
19
+ static const BYTE line_to_case_1[256] = {
20
+ "\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"
21
+ "\xFF\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"
22
+ "\xFF\xFF\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"
23
+ "\xFF\xFF\xFF\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"
24
+ "\xFF\xFF\xFF\xFF\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"
25
+ "\xFF\xFF\xFF\xFF\xFF\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"
26
+ "\xFF\xFF\xFF\xFF\xFF\xFF\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"
27
+ "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"
28
+ "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF"
29
+ "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x00\xFF\xFF\xFF\xFF\xFF\xFF"
30
+ "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x00\xFF\xFF\xFF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FF\xFF\xFF\xFF\xFF"
217
+ "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"
218
+ "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF"
219
+ "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x00\xFF\xFF\xFF\xFF\xFF\xFF"
220
+ "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x00\xFF\xFF\xFF\xFF\xFF"
221
+ "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x00\xFF\xFF\xFF\xFF"
222
+ "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x00\xFF\xFF\xFF"
223
+ "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x00\xFF\xFF"
224
+ "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x00\xFF"
225
+ "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x00"
226
+ };
227
+
228
+ static const BYTE* line_to_case[] = { line_to_case_1, line_to_case_2, line_to_case_3,
229
+ line_to_case_4, line_to_case_5, line_to_case_6,
230
+ line_to_case_7, line_to_case_8, line_to_case_9,
231
+ line_to_case_10, line_to_case_11 };
232
+
233
+ static const BYTE line_to_R2_BLACK[256] = {
234
+ "\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"
235
+ "\xFF\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"
236
+ "\xFF\xFF\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF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xFF\xFF"
535
+ };
536
+
537
+ #define LINTETO_NUMBER 11
538
+ struct ropMap
539
+ {
540
+ UINT32 rop;
541
+ HGDI_BITMAP bmp;
542
+ const BYTE* src;
543
+ };
544
+
545
+ static BOOL test_line(HGDI_DC hdc, const gdiPalette* hPalette, UINT32 mX, UINT32 mY, UINT32 lX,
546
+ UINT32 lY, HGDI_BITMAP hBmp, HGDI_BITMAP hOrgBmp, UINT32 cX, UINT32 cY,
547
+ UINT32 cW, UINT32 cH)
548
+ {
549
+ if (!gdi_BitBlt(hdc, 0, 0, 16, 16, hdc, 0, 0, GDI_WHITENESS, hPalette))
550
+ return FALSE;
551
+
552
+ if ((cX > 0) || (cY > 0) || (cW > 0) || (cH > 0))
553
+ gdi_SetClipRgn(hdc, cX, cY, cW, cH);
554
+
555
+ gdi_MoveToEx(hdc, mX, mY, NULL);
556
+ gdi_LineTo(hdc, lX, lY);
557
+
558
+ if (!test_assert_bitmaps_equal(hBmp, hOrgBmp, "Case 10", hPalette))
559
+ return FALSE;
560
+
561
+ return TRUE;
562
+ }
563
+
564
+ int TestGdiLine(int argc, char* argv[])
565
+ {
566
+ int rc = -1;
567
+
568
+ const UINT32 RawFormat = PIXEL_FORMAT_RGB8;
569
+ const UINT32 colorFormats[] = { PIXEL_FORMAT_RGB15, PIXEL_FORMAT_ARGB15, PIXEL_FORMAT_RGB16,
570
+ PIXEL_FORMAT_RGB24, PIXEL_FORMAT_ARGB32, PIXEL_FORMAT_XRGB32,
571
+ PIXEL_FORMAT_RGBA32, PIXEL_FORMAT_RGBX32, PIXEL_FORMAT_BGR15,
572
+ PIXEL_FORMAT_ABGR15, PIXEL_FORMAT_BGR16, PIXEL_FORMAT_BGR24,
573
+ PIXEL_FORMAT_ABGR32, PIXEL_FORMAT_XBGR32, PIXEL_FORMAT_BGRA32,
574
+ PIXEL_FORMAT_BGRX32 };
575
+ const size_t number_formats = sizeof(colorFormats) / sizeof(colorFormats[0]);
576
+ WINPR_UNUSED(argc);
577
+ WINPR_UNUSED(argv);
578
+
579
+ for (size_t ifmt = 0; ifmt < number_formats; ifmt++)
580
+ {
581
+ gdiPalette g = { 0 };
582
+ HGDI_DC hdc = NULL;
583
+ HGDI_PEN pen = NULL;
584
+ HGDI_BITMAP hBmp = NULL;
585
+ struct ropMap rop_map[] = { { GDI_R2_BLACK, NULL, line_to_R2_BLACK },
586
+ { GDI_R2_NOTMERGEPEN, NULL, line_to_R2_NOTMERGEPEN },
587
+ { GDI_R2_MASKNOTPEN, NULL, line_to_R2_MASKNOTPEN },
588
+ { GDI_R2_NOTCOPYPEN, NULL, line_to_R2_NOTCOPYPEN },
589
+ { GDI_R2_MASKPENNOT, NULL, line_to_R2_MASKPENNOT },
590
+ { GDI_R2_NOT, NULL, line_to_R2_NOT },
591
+ { GDI_R2_XORPEN, NULL, line_to_R2_XORPEN },
592
+ { GDI_R2_NOTMASKPEN, NULL, line_to_R2_NOTMASKPEN },
593
+ { GDI_R2_MASKPEN, NULL, line_to_R2_MASKPEN },
594
+ { GDI_R2_NOTXORPEN, NULL, line_to_R2_NOTXORPEN },
595
+ { GDI_R2_NOP, NULL, line_to_R2_NOP },
596
+ { GDI_R2_MERGENOTPEN, NULL, line_to_R2_MERGENOTPEN },
597
+ { GDI_R2_COPYPEN, NULL, line_to_R2_COPYPEN },
598
+ { GDI_R2_MERGEPENNOT, NULL, line_to_R2_MERGEPENNOT },
599
+ { GDI_R2_MERGEPEN, NULL, line_to_R2_MERGEPEN },
600
+ { GDI_R2_WHITE, NULL, line_to_R2_WHITE } };
601
+ const size_t map_size = sizeof(rop_map) / sizeof(rop_map[0]);
602
+ HGDI_BITMAP hBmp_LineTo[LINTETO_NUMBER] = { NULL };
603
+ gdiPalette* hPalette = &g;
604
+ UINT32 penColor = 0;
605
+ const UINT32 format = colorFormats[ifmt];
606
+ g.format = format;
607
+
608
+ for (unsigned x = 0; x < 256; x++)
609
+ g.palette[x] = FreeRDPGetColor(format, x, x, x, 0xFF);
610
+
611
+ rc = -1;
612
+
613
+ if (!(hdc = gdi_GetDC()))
614
+ {
615
+ printf("failed to get gdi device context\n");
616
+ goto fail;
617
+ }
618
+
619
+ hdc->format = format;
620
+ gdi_SetNullClipRgn(hdc);
621
+ penColor = FreeRDPGetColor(format, 0xFF, 0xFF, 0xFF, 0xFF);
622
+
623
+ if (!(pen = gdi_CreatePen(1, 1, penColor, format, hPalette)))
624
+ {
625
+ printf("gdi_CreatePen failed\n");
626
+ goto fail;
627
+ }
628
+
629
+ gdi_SelectObject(hdc, (HGDIOBJECT)pen);
630
+ hBmp = gdi_CreateCompatibleBitmap(hdc, 16, 16);
631
+ gdi_SelectObject(hdc, (HGDIOBJECT)hBmp);
632
+
633
+ for (UINT32 x = 0; x < LINTETO_NUMBER; x++)
634
+ {
635
+ hBmp_LineTo[x] = test_convert_to_bitmap(line_to_case[x], RawFormat, 0, 0, 0, format, 0,
636
+ 0, 0, 16, 16, hPalette);
637
+
638
+ if (!hBmp_LineTo[x])
639
+ goto fail;
640
+ }
641
+
642
+ for (UINT32 x = 0; x < map_size; x++)
643
+ {
644
+ rop_map[x].bmp = test_convert_to_bitmap(rop_map[x].src, RawFormat, 0, 0, 0, format, 0,
645
+ 0, 0, 16, 16, hPalette);
646
+
647
+ if (!rop_map[x].bmp)
648
+ goto fail;
649
+ }
650
+
651
+ if (!test_line(hdc, hPalette, 0, 0, 15, 15, hBmp, hBmp_LineTo[0], 0, 0, 16, 16))
652
+ goto fail;
653
+
654
+ if (!test_line(hdc, hPalette, 15, 15, 0, 0, hBmp, hBmp_LineTo[1], 0, 0, 16, 16))
655
+ goto fail;
656
+
657
+ if (!test_line(hdc, hPalette, 15, 0, 0, 15, hBmp, hBmp_LineTo[2], 0, 0, 16, 16))
658
+ goto fail;
659
+
660
+ if (!test_line(hdc, hPalette, 0, 15, 15, 0, hBmp, hBmp_LineTo[3], 0, 0, 16, 16))
661
+ goto fail;
662
+
663
+ if (!test_line(hdc, hPalette, 0, 8, 15, 8, hBmp, hBmp_LineTo[4], 0, 0, 16, 16))
664
+ goto fail;
665
+
666
+ if (!test_line(hdc, hPalette, 15, 8, 0, 8, hBmp, hBmp_LineTo[5], 0, 0, 16, 16))
667
+ goto fail;
668
+
669
+ if (!test_line(hdc, hPalette, 8, 0, 8, 15, hBmp, hBmp_LineTo[6], 0, 0, 16, 16))
670
+ goto fail;
671
+
672
+ if (!test_line(hdc, hPalette, 8, 15, 8, 0, hBmp, hBmp_LineTo[7], 0, 0, 16, 16))
673
+ goto fail;
674
+
675
+ if (!test_line(hdc, hPalette, 4, 4, 12, 12, hBmp, hBmp_LineTo[8], 0, 0, 16, 16))
676
+ goto fail;
677
+
678
+ if (!test_line(hdc, hPalette, 0, 0, 16, 16, hBmp, hBmp_LineTo[9], 5, 5, 8, 8))
679
+ goto fail;
680
+
681
+ if (!test_line(hdc, hPalette, 0, 0, 26, 26, hBmp, hBmp_LineTo[10], 0, 0, 16, 16))
682
+ goto fail;
683
+
684
+ for (UINT32 x = 0; x < map_size; x++)
685
+ {
686
+ char name[1024] = { 0 };
687
+ _snprintf(name, sizeof(name), "%s [%s]", gdi_rop_to_string(rop_map[x].rop),
688
+ FreeRDPGetColorFormatName(hdc->format));
689
+
690
+ /* Test Case 13: (0,0) -> (16,16), R2_NOTMERGEPEN */
691
+ if (!gdi_BitBlt(hdc, 0, 0, 16, 16, hdc, 0, 0, GDI_WHITENESS, hPalette))
692
+ {
693
+ printf("gdi_BitBlt failed (line #%u)\n", __LINE__);
694
+ goto fail;
695
+ }
696
+
697
+ gdi_SetClipRgn(hdc, 0, 0, 16, 16);
698
+ gdi_MoveToEx(hdc, 0, 0, NULL);
699
+ gdi_SetROP2(hdc, rop_map[x].rop);
700
+ gdi_LineTo(hdc, 16, 16);
701
+
702
+ if (!test_assert_bitmaps_equal(hBmp, rop_map[x].bmp, name, hPalette))
703
+ goto fail;
704
+ }
705
+
706
+ rc = 0;
707
+ fail:
708
+
709
+ for (UINT32 x = 0; x < LINTETO_NUMBER; x++)
710
+ gdi_DeleteObject((HGDIOBJECT)hBmp_LineTo[x]);
711
+
712
+ for (UINT32 x = 0; x < map_size; x++)
713
+ gdi_DeleteObject((HGDIOBJECT)rop_map[x].bmp);
714
+
715
+ gdi_DeleteObject((HGDIOBJECT)hBmp);
716
+ gdi_DeleteObject((HGDIOBJECT)pen);
717
+ gdi_DeleteDC(hdc);
718
+
719
+ if (rc != 0)
720
+ break;
721
+ }
722
+
723
+ return rc;
724
+ }
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/gdi/test/TestGdiRect.c ADDED
@@ -0,0 +1,175 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ #include <freerdp/gdi/gdi.h>
3
+
4
+ #include <freerdp/gdi/dc.h>
5
+ #include <freerdp/gdi/pen.h>
6
+ #include <freerdp/gdi/shape.h>
7
+ #include <freerdp/gdi/region.h>
8
+ #include <freerdp/gdi/bitmap.h>
9
+
10
+ #include <winpr/crt.h>
11
+ #include <winpr/print.h>
12
+
13
+ #include "line.h"
14
+ #include "brush.h"
15
+ #include "clipping.h"
16
+
17
+ static int test_gdi_PtInRect(void)
18
+ {
19
+ int rc = -1;
20
+ HGDI_RECT hRect = NULL;
21
+ UINT32 left = 20;
22
+ UINT32 top = 40;
23
+ UINT32 right = 60;
24
+ UINT32 bottom = 80;
25
+
26
+ if (!(hRect = gdi_CreateRect(
27
+ WINPR_ASSERTING_INT_CAST(int, left), WINPR_ASSERTING_INT_CAST(int, top),
28
+ WINPR_ASSERTING_INT_CAST(int, right), WINPR_ASSERTING_INT_CAST(int, bottom))))
29
+ {
30
+ printf("gdi_CreateRect failed\n");
31
+ return rc;
32
+ }
33
+
34
+ if (gdi_PtInRect(hRect, 0, 0))
35
+ goto fail;
36
+
37
+ if (gdi_PtInRect(hRect, 500, 500))
38
+ goto fail;
39
+
40
+ if (gdi_PtInRect(hRect, 40, 100))
41
+ goto fail;
42
+
43
+ if (gdi_PtInRect(hRect, 10, 40))
44
+ goto fail;
45
+
46
+ if (!gdi_PtInRect(hRect, 30, 50))
47
+ goto fail;
48
+
49
+ if (!gdi_PtInRect(hRect, WINPR_ASSERTING_INT_CAST(int, left),
50
+ WINPR_ASSERTING_INT_CAST(int, top)))
51
+ goto fail;
52
+
53
+ if (!gdi_PtInRect(hRect, WINPR_ASSERTING_INT_CAST(int, right),
54
+ WINPR_ASSERTING_INT_CAST(int, bottom)))
55
+ goto fail;
56
+
57
+ if (!gdi_PtInRect(hRect, WINPR_ASSERTING_INT_CAST(int, right), 60))
58
+ goto fail;
59
+
60
+ if (!gdi_PtInRect(hRect, 40, WINPR_ASSERTING_INT_CAST(int, bottom)))
61
+ goto fail;
62
+
63
+ rc = 0;
64
+ fail:
65
+ gdi_DeleteObject((HGDIOBJECT)hRect);
66
+ return rc;
67
+ }
68
+
69
+ static int test_gdi_FillRect(void)
70
+ {
71
+ int rc = -1;
72
+ HGDI_DC hdc = NULL;
73
+ HGDI_RECT hRect = NULL;
74
+ HGDI_BRUSH hBrush = NULL;
75
+ HGDI_BITMAP hBitmap = NULL;
76
+ UINT32 color = 0;
77
+ UINT32 pixel = 0;
78
+ UINT32 rawPixel = 0;
79
+ UINT32 badPixels = 0;
80
+ UINT32 goodPixels = 0;
81
+ UINT32 width = 200;
82
+ UINT32 height = 300;
83
+ UINT32 left = 20;
84
+ UINT32 top = 40;
85
+ UINT32 right = 60;
86
+ UINT32 bottom = 80;
87
+
88
+ if (!(hdc = gdi_GetDC()))
89
+ {
90
+ printf("failed to get gdi device context\n");
91
+ goto fail;
92
+ }
93
+
94
+ hdc->format = PIXEL_FORMAT_XRGB32;
95
+
96
+ if (!(hRect = gdi_CreateRect(
97
+ WINPR_ASSERTING_INT_CAST(int, left), WINPR_ASSERTING_INT_CAST(int, top),
98
+ WINPR_ASSERTING_INT_CAST(int, right), WINPR_ASSERTING_INT_CAST(int, bottom))))
99
+ {
100
+ printf("gdi_CreateRect failed\n");
101
+ goto fail;
102
+ }
103
+
104
+ hBitmap = gdi_CreateCompatibleBitmap(hdc, width, height);
105
+ ZeroMemory(hBitmap->data, 1ULL * width * height * FreeRDPGetBytesPerPixel(hdc->format));
106
+ gdi_SelectObject(hdc, (HGDIOBJECT)hBitmap);
107
+ color = FreeRDPGetColor(PIXEL_FORMAT_ARGB32, 0xAA, 0xBB, 0xCC, 0xFF);
108
+ hBrush = gdi_CreateSolidBrush(color);
109
+ gdi_FillRect(hdc, hRect, hBrush);
110
+ badPixels = 0;
111
+ goodPixels = 0;
112
+
113
+ for (UINT32 x = 0; x < width; x++)
114
+ {
115
+ for (UINT32 y = 0; y < height; y++)
116
+ {
117
+ rawPixel = gdi_GetPixel(hdc, x, y);
118
+ pixel = FreeRDPConvertColor(rawPixel, hdc->format, PIXEL_FORMAT_ARGB32, NULL);
119
+
120
+ if (gdi_PtInRect(hRect, WINPR_ASSERTING_INT_CAST(int, x),
121
+ WINPR_ASSERTING_INT_CAST(int, y)))
122
+ {
123
+ if (pixel == color)
124
+ {
125
+ goodPixels++;
126
+ }
127
+ else
128
+ {
129
+ printf("actual:%08" PRIX32 " expected:%08" PRIX32 "\n", gdi_GetPixel(hdc, x, y),
130
+ color);
131
+ badPixels++;
132
+ }
133
+ }
134
+ else
135
+ {
136
+ if (pixel == color)
137
+ {
138
+ badPixels++;
139
+ }
140
+ else
141
+ {
142
+ goodPixels++;
143
+ }
144
+ }
145
+ }
146
+ }
147
+
148
+ if (goodPixels != width * height)
149
+ goto fail;
150
+
151
+ if (badPixels != 0)
152
+ goto fail;
153
+
154
+ rc = 0;
155
+ fail:
156
+ gdi_DeleteObject((HGDIOBJECT)hBrush);
157
+ gdi_DeleteObject((HGDIOBJECT)hBitmap);
158
+ gdi_DeleteObject((HGDIOBJECT)hRect);
159
+ gdi_DeleteDC(hdc);
160
+ return rc;
161
+ }
162
+
163
+ int TestGdiRect(int argc, char* argv[])
164
+ {
165
+ WINPR_UNUSED(argc);
166
+ WINPR_UNUSED(argv);
167
+
168
+ if (test_gdi_PtInRect() < 0)
169
+ return -1;
170
+
171
+ if (test_gdi_FillRect() < 0)
172
+ return -1;
173
+
174
+ return 0;
175
+ }
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/gdi/test/TestGdiRegion.c ADDED
@@ -0,0 +1,256 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ #include <freerdp/gdi/gdi.h>
3
+
4
+ #include <freerdp/gdi/dc.h>
5
+ #include <freerdp/gdi/pen.h>
6
+ #include <freerdp/gdi/region.h>
7
+ #include <freerdp/gdi/bitmap.h>
8
+
9
+ #include <winpr/crt.h>
10
+ #include <winpr/print.h>
11
+
12
+ #include "helpers.h"
13
+
14
+ int TestGdiRegion(int argc, char* argv[])
15
+ {
16
+ int rc = -1;
17
+ INT32 x = 0;
18
+ INT32 y = 0;
19
+ INT32 w = 0;
20
+ INT32 h = 0;
21
+ INT32 l = 0;
22
+ INT32 r = 0;
23
+ INT32 t = 0;
24
+ INT32 b = 0;
25
+ HGDI_RGN rgn1 = NULL;
26
+ HGDI_RGN rgn2 = NULL;
27
+ HGDI_RECT rect1 = NULL;
28
+ HGDI_RECT rect2 = NULL;
29
+
30
+ WINPR_UNUSED(argc);
31
+ WINPR_UNUSED(argv);
32
+
33
+ rgn1 = gdi_CreateRectRgn(111, 2, 65, 77);
34
+ rect1 = gdi_CreateRect(2311, 11, 42, 17);
35
+ if (rgn1 || rect1)
36
+ goto fail;
37
+ rgn1 = gdi_CreateRectRgn(1, 2, 65, 77);
38
+ rgn2 = gdi_CreateRectRgn(11, 2, 65, 77);
39
+ rect1 = gdi_CreateRect(23, 11, 42, 17);
40
+ rect2 = gdi_CreateRect(23, 11, 42, 17);
41
+ if (!rgn1 || !rgn2 || !rect1 || !rect2)
42
+ goto fail;
43
+
44
+ if (!gdi_RectToRgn(rect1, rgn1))
45
+ goto fail;
46
+ if (rgn1->x != rect1->left)
47
+ goto fail;
48
+ if (rgn1->y != rect1->top)
49
+ goto fail;
50
+ if (rgn1->w != (rect1->right - rect1->left + 1))
51
+ goto fail;
52
+ if (rgn1->h != (rect1->bottom - rect1->top + 1))
53
+ goto fail;
54
+
55
+ if (gdi_CRectToRgn(1123, 111, 333, 444, rgn2))
56
+ goto fail;
57
+ if (gdi_CRectToRgn(123, 1111, 333, 444, rgn2))
58
+ goto fail;
59
+ if (!gdi_CRectToRgn(123, 111, 333, 444, rgn2))
60
+ goto fail;
61
+ if (rgn2->x != 123)
62
+ goto fail;
63
+ if (rgn2->y != 111)
64
+ goto fail;
65
+ if (rgn2->w != (333 - 123 + 1))
66
+ goto fail;
67
+ if (rgn2->h != (444 - 111 + 1))
68
+ goto fail;
69
+
70
+ if (!gdi_RectToCRgn(rect1, &x, &y, &w, &h))
71
+ goto fail;
72
+ if (rect1->left != x)
73
+ goto fail;
74
+ if (rect1->top != y)
75
+ goto fail;
76
+ if (rect1->right != (x + w - 1))
77
+ goto fail;
78
+ if (rect1->bottom != (y + h - 1))
79
+ goto fail;
80
+
81
+ w = 23;
82
+ h = 42;
83
+ if (gdi_CRectToCRgn(1, 2, 0, 4, &x, &y, &w, &h))
84
+ goto fail;
85
+ if ((w != 0) || (h != 0))
86
+ goto fail;
87
+ w = 23;
88
+ h = 42;
89
+ if (gdi_CRectToCRgn(1, 2, 3, 1, &x, &y, &w, &h))
90
+ goto fail;
91
+ if ((w != 0) || (h != 0))
92
+ goto fail;
93
+ w = 23;
94
+ h = 42;
95
+ if (!gdi_CRectToCRgn(1, 2, 3, 4, &x, &y, &w, &h))
96
+ goto fail;
97
+ if (x != 1)
98
+ goto fail;
99
+ if (y != 2)
100
+ goto fail;
101
+ if (w != (3 - 1 + 1))
102
+ goto fail;
103
+ if (h != (4 - 2 + 1))
104
+ goto fail;
105
+
106
+ if (!gdi_RgnToRect(rgn1, rect2))
107
+ goto fail;
108
+
109
+ if (rgn1->x != rect2->left)
110
+ goto fail;
111
+ if (rgn1->y != rect2->top)
112
+ goto fail;
113
+ if (rgn1->w != (rect2->right - rect2->left + 1))
114
+ goto fail;
115
+ if (rgn1->h != (rect2->bottom - rect2->top + 1))
116
+ goto fail;
117
+
118
+ if (gdi_CRgnToRect(1, 2, 0, 4, rect2))
119
+ goto fail;
120
+ if (gdi_CRgnToRect(1, 2, -1, 4, rect2))
121
+ goto fail;
122
+ if (gdi_CRgnToRect(1, 2, 3, 0, rect2))
123
+ goto fail;
124
+ if (gdi_CRgnToRect(1, 2, 3, -1, rect2))
125
+ goto fail;
126
+ if (!gdi_CRgnToRect(1, 2, 3, 4, rect2))
127
+ goto fail;
128
+ if (rect2->left != 1)
129
+ goto fail;
130
+ if (rect2->right != (1 + 3 - 1))
131
+ goto fail;
132
+ if (rect2->top != 2)
133
+ goto fail;
134
+ if (rect2->bottom != (2 + 4 - 1))
135
+ goto fail;
136
+
137
+ if (!gdi_RgnToCRect(rgn1, &l, &t, &r, &b))
138
+ goto fail;
139
+ if (rgn1->x != l)
140
+ goto fail;
141
+ if (rgn1->y != t)
142
+ goto fail;
143
+ if (rgn1->w != (r - l + 1))
144
+ goto fail;
145
+ if (rgn1->h != (b - t + 1))
146
+ goto fail;
147
+
148
+ if (gdi_CRgnToCRect(1, 2, -1, 4, &l, &t, &r, &b))
149
+ goto fail;
150
+ if (gdi_CRgnToCRect(1, 2, 0, 4, &l, &t, &r, &b))
151
+ goto fail;
152
+ if (gdi_CRgnToCRect(1, 2, 3, -1, &l, &t, &r, &b))
153
+ goto fail;
154
+ if (gdi_CRgnToCRect(1, 2, 3, -0, &l, &t, &r, &b))
155
+ goto fail;
156
+ if (!gdi_CRgnToCRect(1, 2, 3, 4, &l, &t, &r, &b))
157
+ goto fail;
158
+ if (l != 1)
159
+ goto fail;
160
+ if (t != 2)
161
+ goto fail;
162
+ if (r != (1 + 3 - 1))
163
+ goto fail;
164
+ if (b != 2 + 4 - 1)
165
+ goto fail;
166
+
167
+ if (gdi_CopyOverlap(1, 2, 5, 3, -5, 3))
168
+ goto fail;
169
+ if (gdi_CopyOverlap(1, 2, 5, 3, 3, -2))
170
+ goto fail;
171
+ if (!gdi_CopyOverlap(1, 2, 5, 3, 2, 3))
172
+ goto fail;
173
+
174
+ if (gdi_SetRect(rect2, -4, 500, 66, -5))
175
+ goto fail;
176
+ if (gdi_SetRect(rect2, -4, -500, -66, -5))
177
+ goto fail;
178
+ if (!gdi_SetRect(rect2, -4, 500, 66, 754))
179
+ goto fail;
180
+
181
+ if (gdi_SetRgn(NULL, -23, -42, 33, 99))
182
+ goto fail;
183
+ if (gdi_SetRgn(rgn2, -23, -42, -33, 99))
184
+ goto fail;
185
+ if (gdi_SetRgn(rgn2, -23, -42, 33, -99))
186
+ goto fail;
187
+ if (!gdi_SetRgn(rgn2, -23, -42, 33, 99))
188
+ goto fail;
189
+ if (rgn2->x != -23)
190
+ goto fail;
191
+ if (rgn2->y != -42)
192
+ goto fail;
193
+ if (rgn2->w != 33)
194
+ goto fail;
195
+ if (rgn2->h != 99)
196
+ goto fail;
197
+ if (rgn2->null)
198
+ goto fail;
199
+
200
+ if (gdi_SetRectRgn(NULL, 33, 22, 44, 33))
201
+ goto fail;
202
+ if (gdi_SetRectRgn(rgn1, 331, 22, 44, 33))
203
+ goto fail;
204
+ if (gdi_SetRectRgn(rgn1, 33, 122, 44, 33))
205
+ goto fail;
206
+ if (!gdi_SetRectRgn(rgn1, 33, 22, 44, 33))
207
+ goto fail;
208
+ if (rgn1->x != 33)
209
+ goto fail;
210
+ if (rgn1->y != 22)
211
+ goto fail;
212
+ if (rgn1->w != (44 - 33 + 1))
213
+ goto fail;
214
+ if (rgn1->h != (33 - 22 + 1))
215
+ goto fail;
216
+
217
+ if (gdi_EqualRgn(rgn1, rgn2))
218
+ goto fail;
219
+ if (!gdi_EqualRgn(rgn1, rgn1))
220
+ goto fail;
221
+
222
+ if (gdi_CopyRect(rect1, NULL))
223
+ goto fail;
224
+ if (gdi_CopyRect(NULL, rect1))
225
+ goto fail;
226
+ if (gdi_CopyRect(NULL, NULL))
227
+ goto fail;
228
+ if (!gdi_CopyRect(rect1, rect2))
229
+ goto fail;
230
+
231
+ if (rect1->left != rect2->left)
232
+ goto fail;
233
+ if (rect1->top != rect2->top)
234
+ goto fail;
235
+ if (rect1->right != rect2->right)
236
+ goto fail;
237
+ if (rect1->bottom != rect2->bottom)
238
+ goto fail;
239
+
240
+ if (gdi_PtInRect(rect1, -23, 550))
241
+ goto fail;
242
+ if (gdi_PtInRect(rect1, 2, 3))
243
+ goto fail;
244
+ if (!gdi_PtInRect(rect1, 2, 550))
245
+ goto fail;
246
+
247
+ // BOOL gdi_InvalidateRegion(HGDI_DC hdc, INT32 x, INT32 y, INT32 w, INT32 h);
248
+
249
+ rc = 0;
250
+ fail:
251
+ free(rgn1);
252
+ free(rgn2);
253
+ free(rect1);
254
+ free(rect2);
255
+ return rc;
256
+ }
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/gdi/test/TestGdiRop3.c ADDED
@@ -0,0 +1,219 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ #include <winpr/crt.h>
3
+ #include <winpr/winpr.h>
4
+ #include <winpr/collections.h>
5
+
6
+ /**
7
+ * Ternary Raster Operations:
8
+ * See "Windows Graphics Programming: Win32 GDI and DirectDraw", chapter 11. Advanced Bitmap
9
+ * Graphics
10
+ *
11
+ * Operators:
12
+ *
13
+ * AND & a
14
+ * OR | o
15
+ * NOT ~ n
16
+ * XOR ^ x
17
+ *
18
+ * Operands:
19
+ *
20
+ * Pen/Brush P
21
+ * Destination D
22
+ * Source S
23
+ *
24
+ * Example:
25
+ *
26
+ * Raster operation which returns P if S is 1 or D otherwise:
27
+ * (rop_S & rop_P) | (~rop_S & rop_D); -> 0xE2 (0x00E20746)
28
+ *
29
+ * Postfix notation: DSPDxax
30
+ * Infix notation: D^(S&(P^D))), (S&P)|(~S&D)
31
+ *
32
+ * DSPDxax using D^(S&(P^D)):
33
+ *
34
+ * mov eax, P // P
35
+ * xor eax, D // P^D
36
+ * and eax, S // S&(P^D)
37
+ * xor eax, D // D^(S&(P^D))
38
+ * mov D, eax // write result
39
+ *
40
+ * DSPDxax using (S&P)|(~S&D):
41
+ *
42
+ * mov eax, S // S
43
+ * and eax, P // S&P
44
+ * mov ebx, S // S
45
+ * not ebx // ~S
46
+ * and ebx, D // ~D&D
47
+ * or eax, ebx // (S&P)|(~S&D)
48
+ * mov D, eax // write result
49
+ *
50
+ * Raster operation lower word encoding:
51
+ *
52
+ * _______________________________________________________________________________
53
+ * | | | | | | | | | | | | | | | | |
54
+ * | Op5 | Op4 | Op3 | Op2 | Op1 | Not| Parse String | Offset |
55
+ * |____|____|____|____|____|____|____|____|____|____|____|____|____|____|____|____|
56
+ * 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
57
+ *
58
+ * Operator:
59
+ * 0: NOT
60
+ * 1: XOR
61
+ * 2: OR
62
+ * 3: AND
63
+ *
64
+ * Parse String:
65
+ * 0: SPDDDDDD
66
+ * 1: SPDSPDSP
67
+ * 2: SDPSDPSD
68
+ * 3: DDDDDDDD
69
+ * 4: DDDDDDDD
70
+ * 5: S+SP-DSS
71
+ * 6: S+SP-PDS
72
+ * 7: S+SD-PDS
73
+ *
74
+ * The lower word for 0x00E20746 is 0x0746 (00000111 01000110)
75
+ *
76
+ * 00 Op5 (NOT, n)
77
+ * 00 Op4 (NOT, n)
78
+ * 01 Op3 (XOR, x)
79
+ * 11 Op2 (AND, a)
80
+ * 01 Op1 (XOR, x)
81
+ * 0 Not (unused)
82
+ * 001 String (SPDSPDSP)
83
+ * 10 Offset (2)
84
+ *
85
+ * We shift SPDSPDSP to the left by 2: DSPDSPSP
86
+ *
87
+ * We have 5 operators: 3 binary operators and the last two are unary operators,
88
+ * so only four operands are needed. The parse string is truncated to reflect
89
+ * the number of operands we need: DSPD
90
+ *
91
+ * The operator string (from Op1 to Op5) is xaxnn, which can be simplified to xax
92
+ *
93
+ * The complete string representing the operation is DSPDxax
94
+ *
95
+ */
96
+
97
+ static char* gdi_convert_postfix_to_infix(const char* postfix)
98
+ {
99
+ BOOL unary = 0;
100
+ size_t al = 0;
101
+ size_t bl = 0;
102
+ wStack* stack = Stack_New(FALSE);
103
+ size_t length = strlen(postfix);
104
+
105
+ for (size_t i = 0; i < length; i++)
106
+ {
107
+ BOOL success = FALSE;
108
+ if ((postfix[i] == 'P') || (postfix[i] == 'D') || (postfix[i] == 'S'))
109
+ {
110
+ /* token is an operand, push on the stack */
111
+ char* a = malloc(2);
112
+ if (!a)
113
+ goto end;
114
+ a[0] = postfix[i];
115
+ a[1] = '\0';
116
+ // printf("Operand: %s\n", a);
117
+ Stack_Push(stack, a);
118
+ }
119
+ else
120
+ {
121
+ char* a = NULL;
122
+ char* b = NULL;
123
+
124
+ /* token is an operator */
125
+ unary = FALSE;
126
+ char* c = malloc(2);
127
+ if (!c)
128
+ goto fail;
129
+ c[0] = postfix[i];
130
+ c[1] = '\0';
131
+
132
+ if (c[0] == 'a')
133
+ {
134
+ c[0] = '&';
135
+ }
136
+ else if (c[0] == 'o')
137
+ {
138
+ c[0] = '|';
139
+ }
140
+ else if (c[0] == 'n')
141
+ {
142
+ c[0] = '~';
143
+ unary = TRUE;
144
+ }
145
+ else if (c[0] == 'x')
146
+ {
147
+ c[0] = '^';
148
+ }
149
+ else
150
+ {
151
+ printf("invalid operator: %c\n", c[0]);
152
+ }
153
+
154
+ // printf("Operator: %s\n", c);
155
+ a = (char*)Stack_Pop(stack);
156
+ if (!a)
157
+ goto fail;
158
+
159
+ if (!unary)
160
+ b = (char*)Stack_Pop(stack);
161
+
162
+ al = strlen(a);
163
+
164
+ if (b)
165
+ bl = strlen(b);
166
+
167
+ size_t cl = 1;
168
+ size_t dl = al + bl + cl + 3;
169
+ char* d = malloc(dl + 1);
170
+ if (!d)
171
+ goto fail;
172
+
173
+ (void)sprintf_s(d, dl, "(%s%s%s)", b ? b : "", c, a);
174
+ Stack_Push(stack, d);
175
+
176
+ success = TRUE;
177
+ fail:
178
+ free(a);
179
+ free(b);
180
+ free(c);
181
+ if (!success)
182
+ goto end;
183
+ }
184
+ }
185
+
186
+ char* d = (char*)Stack_Pop(stack);
187
+ Stack_Free(stack);
188
+ return d;
189
+
190
+ end:
191
+ Stack_Free(stack);
192
+ return NULL;
193
+ }
194
+
195
+ static const char* test_ROP3[] = { "DSPDxax", "PSDPxax", "SPna", "DSna", "DPa",
196
+ "PDxn", "DSxn", "PSDnox", "PDSona", "DSPDxox",
197
+ "DPSDonox", "SPDSxax", "DPon", "DPna", "Pn",
198
+ "PDna", "DPan", "DSan", "DSxn", "DPa",
199
+ "D", "DPno", "SDno", "PDno", "DPo" };
200
+
201
+ int TestGdiRop3(int argc, char* argv[])
202
+ {
203
+ WINPR_UNUSED(argc);
204
+ WINPR_UNUSED(argv);
205
+
206
+ for (size_t index = 0; index < sizeof(test_ROP3) / sizeof(test_ROP3[0]); index++)
207
+ {
208
+ const char* postfix = test_ROP3[index];
209
+ char* infix = gdi_convert_postfix_to_infix(postfix);
210
+
211
+ if (!infix)
212
+ return -1;
213
+
214
+ printf("%s\t\t%s\n", postfix, infix);
215
+ free(infix);
216
+ }
217
+
218
+ return 0;
219
+ }
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/gdi/test/helpers.h ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * GDI Library Tests
4
+ *
5
+ * Copyright 2016 Armin Novak <armin.novak@thincast.com>
6
+ * Copyright 2016 Thincast Technologies GmbH
7
+ *
8
+ * Licensed under the Apache License, Version 2.0 (the "License");
9
+ * you may not use this file except in compliance with the License.
10
+ * You may obtain a copy of the License at
11
+ *
12
+ * http://www.apache.org/licenses/LICENSE-2.0
13
+ *
14
+ * Unless required by applicable law or agreed to in writing, software
15
+ * distributed under the License is distributed on an "AS IS" BASIS,
16
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ * See the License for the specific language governing permissions and
18
+ * limitations under the License.
19
+ */
20
+
21
+ #ifndef GDI_TEST_HELPERS_H
22
+ #define GDI_TEST_HELPERS_H
23
+
24
+ #include <freerdp/codec/color.h>
25
+ #include <freerdp/gdi/bitmap.h>
26
+
27
+ HGDI_BITMAP test_convert_to_bitmap(const BYTE* src, UINT32 SrcFormat, UINT32 SrcStride, UINT32 xSrc,
28
+ UINT32 ySrc, UINT32 DstFormat, UINT32 DstStride, UINT32 xDst,
29
+ UINT32 yDst, UINT32 nWidth, UINT32 nHeight,
30
+
31
+ const gdiPalette* hPalette);
32
+
33
+ void test_dump_bitmap(HGDI_BITMAP hBmp, const char* name);
34
+ BOOL test_assert_bitmaps_equal(HGDI_BITMAP hBmpActual, HGDI_BITMAP hBmpExpected, const char* name,
35
+ const gdiPalette* palette);
36
+
37
+ #endif /* __GDI_CORE_H */
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/locale/CMakeLists.txt ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # FreeRDP: A Remote Desktop Protocol Implementation
2
+ # libfreerdp-locale cmake build script
3
+ #
4
+ # Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # 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, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+
18
+ set(MODULE_NAME "freerdp-locale")
19
+ set(MODULE_PREFIX "FREERDP_LOCALE")
20
+
21
+ set(SRCS keyboard_layout.c keyboard.c locale.c liblocale.h)
22
+
23
+ set(X11_SRCS keyboard_x11.c keyboard_x11.h xkb_layout_ids.c xkb_layout_ids.h)
24
+
25
+ set(XKBFILE_SRCS keyboard_xkbfile.c keyboard_xkbfile.h)
26
+
27
+ set(SUN_SRCS keyboard_sun.c keyboard_sun.h)
28
+
29
+ set(APPLE_SRCS keyboard_apple.c keyboard_apple.h)
30
+
31
+ include(CMakeDependentOption)
32
+ cmake_dependent_option(WITH_KEYBOARD_LAYOUT_FROM_FILE "Use keyboard definitions from JSON file" OFF WITH_WINPR_JSON OFF)
33
+ if(WITH_KEYBOARD_LAYOUT_FROM_FILE)
34
+ freerdp_definition_add(FREERDP_RESOURCE_ROOT="${FREERDP_RESOURCE_ROOT}")
35
+ freerdp_definition_add(WITH_KEYBOARD_LAYOUT_FROM_FILE)
36
+
37
+ install(FILES KeyboardLayoutMap.json DESTINATION ${FREERDP_RESOURCE_ROOT})
38
+ endif()
39
+
40
+ if(CMAKE_SYSTEM_NAME MATCHES Solaris)
41
+ set(WITH_SUN true)
42
+ endif()
43
+
44
+ if(APPLE AND (NOT IOS))
45
+ list(APPEND SRCS ${APPLE_SRCS})
46
+ find_library(CARBON Carbon)
47
+ freerdp_library_add(${CARBON})
48
+ endif()
49
+
50
+ if(APPLE)
51
+ find_library(CORE_FOUNDATION CoreFoundation REQUIRED)
52
+ freerdp_library_add(${CORE_FOUNDATION})
53
+ endif()
54
+
55
+ if(WITH_X11)
56
+ find_package(X11 REQUIRED)
57
+
58
+ freerdp_definition_add(WITH_X11)
59
+ freerdp_include_directory_add(${X11_INCLUDE_DIR})
60
+ list(APPEND SRCS ${X11_SRCS})
61
+ freerdp_library_add(${X11_LIBRARIES})
62
+
63
+ if(WITH_SUN)
64
+ freerdp_definition_add(WITH_SUN)
65
+ list(APPEND SRCS ${SUN_SRCS})
66
+ endif()
67
+
68
+ if(X11_Xkbfile_FOUND AND (NOT APPLE))
69
+ freerdp_definition_add(WITH_XKBFILE)
70
+ freerdp_include_directory_add(${X11_Xkbfile_INCLUDE_PATH})
71
+ list(APPEND SRCS ${XKBFILE_SRCS})
72
+ freerdp_library_add(${X11_Xkbfile_LIB})
73
+ else()
74
+ list(APPEND SRCS ${X11_KEYMAP_SRCS})
75
+ endif()
76
+ endif()
77
+
78
+ if(WITH_WAYLAND)
79
+ freerdp_definition_add(WITH_WAYLAND)
80
+ endif()
81
+
82
+ freerdp_module_add(${SRCS})
83
+
84
+ if(BUILD_TESTING_INTERNAL OR BUILD_TESTING)
85
+ add_subdirectory(test)
86
+ endif()
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/locale/keyboard.c ADDED
@@ -0,0 +1,508 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * Keyboard Localization
4
+ *
5
+ * Copyright 2009-2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+
20
+ #include <freerdp/config.h>
21
+
22
+ #include <stdio.h>
23
+ #include <string.h>
24
+ #include <stdint.h>
25
+
26
+ #include <winpr/assert.h>
27
+ #include <winpr/crt.h>
28
+
29
+ #include <freerdp/utils/string.h>
30
+ #include <freerdp/types.h>
31
+ #include <freerdp/locale/keyboard.h>
32
+ #include <freerdp/locale/locale.h>
33
+
34
+ #include <freerdp/log.h>
35
+
36
+ #include "liblocale.h"
37
+
38
+ #if defined(__MACOSX__)
39
+ #include "keyboard_apple.h"
40
+ #endif
41
+
42
+ #define TAG FREERDP_TAG("locale.keyboard")
43
+
44
+ #ifdef WITH_X11
45
+ #include "keyboard_x11.h"
46
+
47
+ #ifdef WITH_XKBFILE
48
+ #include "keyboard_xkbfile.h"
49
+ #endif
50
+
51
+ #endif
52
+
53
+ static WINPR_KEYCODE_TYPE maptype = WINPR_KEYCODE_TYPE_NONE;
54
+ static DWORD VIRTUAL_SCANCODE_TO_X11_KEYCODE[256][2] = { 0 };
55
+ static DWORD X11_KEYCODE_TO_VIRTUAL_SCANCODE[256] = { 0 };
56
+ static DWORD REMAPPING_TABLE[0x10000] = { 0 };
57
+
58
+ struct rdp_remap_table
59
+ {
60
+ DWORD table[0x10000];
61
+ };
62
+
63
+ struct scancode_map_entry
64
+ {
65
+ DWORD scancode;
66
+ const char* name;
67
+ };
68
+
69
+ static const struct scancode_map_entry RDP_SCANCODE_MAP[] = {
70
+ { RDP_SCANCODE_ESCAPE, "VK_ESCAPE" },
71
+ { RDP_SCANCODE_KEY_1, "VK_KEY_1" },
72
+ { RDP_SCANCODE_KEY_2, "VK_KEY_2" },
73
+ { RDP_SCANCODE_KEY_3, "VK_KEY_3" },
74
+ { RDP_SCANCODE_KEY_4, "VK_KEY_4" },
75
+ { RDP_SCANCODE_KEY_5, "VK_KEY_5" },
76
+ { RDP_SCANCODE_KEY_6, "VK_KEY_6" },
77
+ { RDP_SCANCODE_KEY_7, "VK_KEY_7" },
78
+ { RDP_SCANCODE_KEY_8, "VK_KEY_8" },
79
+ { RDP_SCANCODE_KEY_9, "VK_KEY_9" },
80
+ { RDP_SCANCODE_KEY_0, "VK_KEY_0" },
81
+ { RDP_SCANCODE_OEM_MINUS, "VK_OEM_MINUS" },
82
+ { RDP_SCANCODE_OEM_PLUS, "VK_OEM_PLUS" },
83
+ { RDP_SCANCODE_BACKSPACE, "VK_BACK Backspace" },
84
+ { RDP_SCANCODE_TAB, "VK_TAB" },
85
+ { RDP_SCANCODE_KEY_Q, "VK_KEY_Q" },
86
+ { RDP_SCANCODE_KEY_W, "VK_KEY_W" },
87
+ { RDP_SCANCODE_KEY_E, "VK_KEY_E" },
88
+ { RDP_SCANCODE_KEY_R, "VK_KEY_R" },
89
+ { RDP_SCANCODE_KEY_T, "VK_KEY_T" },
90
+ { RDP_SCANCODE_KEY_Y, "VK_KEY_Y" },
91
+ { RDP_SCANCODE_KEY_U, "VK_KEY_U" },
92
+ { RDP_SCANCODE_KEY_I, "VK_KEY_I" },
93
+ { RDP_SCANCODE_KEY_O, "VK_KEY_O" },
94
+ { RDP_SCANCODE_KEY_P, "VK_KEY_P" },
95
+ { RDP_SCANCODE_OEM_4, "VK_OEM_4 '[' on US" },
96
+ { RDP_SCANCODE_OEM_6, "VK_OEM_6 ']' on US" },
97
+ { RDP_SCANCODE_RETURN, "VK_RETURN Normal Enter" },
98
+ { RDP_SCANCODE_LCONTROL, "VK_LCONTROL" },
99
+ { RDP_SCANCODE_KEY_A, "VK_KEY_A" },
100
+ { RDP_SCANCODE_KEY_S, "VK_KEY_S" },
101
+ { RDP_SCANCODE_KEY_D, "VK_KEY_D" },
102
+ { RDP_SCANCODE_KEY_F, "VK_KEY_F" },
103
+ { RDP_SCANCODE_KEY_G, "VK_KEY_G" },
104
+ { RDP_SCANCODE_KEY_H, "VK_KEY_H" },
105
+ { RDP_SCANCODE_KEY_J, "VK_KEY_J" },
106
+ { RDP_SCANCODE_KEY_K, "VK_KEY_K" },
107
+ { RDP_SCANCODE_KEY_L, "VK_KEY_L" },
108
+ { RDP_SCANCODE_OEM_1, "VK_OEM_1 ';' on US" },
109
+ { RDP_SCANCODE_OEM_7, "VK_OEM_7 on US" },
110
+ { RDP_SCANCODE_OEM_3, "VK_OEM_3 Top left, '`' on US, JP DBE_SBCSCHAR" },
111
+ { RDP_SCANCODE_LSHIFT, "VK_LSHIFT" },
112
+ { RDP_SCANCODE_OEM_5, "VK_OEM_5 Next to Enter, '\' on US" },
113
+ { RDP_SCANCODE_KEY_Z, "VK_KEY_Z" },
114
+ { RDP_SCANCODE_KEY_X, "VK_KEY_X" },
115
+ { RDP_SCANCODE_KEY_C, "VK_KEY_C" },
116
+ { RDP_SCANCODE_KEY_V, "VK_KEY_V" },
117
+ { RDP_SCANCODE_KEY_B, "VK_KEY_B" },
118
+ { RDP_SCANCODE_KEY_N, "VK_KEY_N" },
119
+ { RDP_SCANCODE_KEY_M, "VK_KEY_M" },
120
+ { RDP_SCANCODE_OEM_COMMA, "VK_OEM_COMMA" },
121
+ { RDP_SCANCODE_OEM_PERIOD, "VK_OEM_PERIOD" },
122
+ { RDP_SCANCODE_OEM_2, "VK_OEM_2 '/' on US" },
123
+ { RDP_SCANCODE_RSHIFT, "VK_RSHIFT" },
124
+ { RDP_SCANCODE_MULTIPLY, "VK_MULTIPLY Numerical" },
125
+ { RDP_SCANCODE_LMENU, "VK_LMENU Left 'Alt' key" },
126
+ { RDP_SCANCODE_SPACE, "VK_SPACE" },
127
+ { RDP_SCANCODE_CAPSLOCK, "VK_CAPITAL 'Caps Lock', JP DBE_ALPHANUMERIC" },
128
+ { RDP_SCANCODE_F1, "VK_F1" },
129
+ { RDP_SCANCODE_F2, "VK_F2" },
130
+ { RDP_SCANCODE_F3, "VK_F3" },
131
+ { RDP_SCANCODE_F4, "VK_F4" },
132
+ { RDP_SCANCODE_F5, "VK_F5" },
133
+ { RDP_SCANCODE_F6, "VK_F6" },
134
+ { RDP_SCANCODE_F7, "VK_F7" },
135
+ { RDP_SCANCODE_F8, "VK_F8" },
136
+ { RDP_SCANCODE_F9, "VK_F9" },
137
+ { RDP_SCANCODE_F10, "VK_F10" },
138
+ { RDP_SCANCODE_NUMLOCK, "VK_NUMLOCK" },
139
+ { RDP_SCANCODE_SCROLLLOCK, "VK_SCROLL 'Scroll Lock', JP OEM_SCROLL" },
140
+ { RDP_SCANCODE_NUMPAD7, "VK_NUMPAD7" },
141
+ { RDP_SCANCODE_NUMPAD8, "VK_NUMPAD8" },
142
+ { RDP_SCANCODE_NUMPAD9, "VK_NUMPAD9" },
143
+ { RDP_SCANCODE_SUBTRACT, "VK_SUBTRACT" },
144
+ { RDP_SCANCODE_NUMPAD4, "VK_NUMPAD4" },
145
+ { RDP_SCANCODE_NUMPAD5, "VK_NUMPAD5" },
146
+ { RDP_SCANCODE_NUMPAD6, "VK_NUMPAD6" },
147
+ { RDP_SCANCODE_ADD, "VK_ADD" },
148
+ { RDP_SCANCODE_NUMPAD1, "VK_NUMPAD1" },
149
+ { RDP_SCANCODE_NUMPAD2, "VK_NUMPAD2" },
150
+ { RDP_SCANCODE_NUMPAD3, "VK_NUMPAD3" },
151
+ { RDP_SCANCODE_NUMPAD0, "VK_NUMPAD0" },
152
+ { RDP_SCANCODE_DECIMAL, "VK_DECIMAL Numerical, '.' on US" },
153
+ { RDP_SCANCODE_SYSREQ, "Sys Req" },
154
+ { RDP_SCANCODE_OEM_102, "VK_OEM_102 Lower left '\' on US" },
155
+ { RDP_SCANCODE_F11, "VK_F11" },
156
+ { RDP_SCANCODE_F12, "VK_F12" },
157
+ { RDP_SCANCODE_SLEEP, "VK_SLEEP OEM_8 on FR (undocumented?)" },
158
+ { RDP_SCANCODE_ZOOM, "VK_ZOOM (undocumented?)" },
159
+ { RDP_SCANCODE_HELP, "VK_HELP (undocumented?)" },
160
+ { RDP_SCANCODE_F13, "VK_F13" },
161
+ { RDP_SCANCODE_F14, "VK_F14" },
162
+ { RDP_SCANCODE_F15, "VK_F15" },
163
+ { RDP_SCANCODE_F16, "VK_F16" },
164
+ { RDP_SCANCODE_F17, "VK_F17" },
165
+ { RDP_SCANCODE_F18, "VK_F18" },
166
+ { RDP_SCANCODE_F19, "VK_F19" },
167
+ { RDP_SCANCODE_F20, "VK_F20" },
168
+ { RDP_SCANCODE_F21, "VK_F21" },
169
+ { RDP_SCANCODE_F22, "VK_F22" },
170
+ { RDP_SCANCODE_F23, "VK_F23" },
171
+ { RDP_SCANCODE_F24, "VK_F24" },
172
+ { RDP_SCANCODE_HIRAGANA, "JP DBE_HIRAGANA" },
173
+ { RDP_SCANCODE_HANJA_KANJI, "VK_HANJA / VK_KANJI (undocumented?)" },
174
+ { RDP_SCANCODE_KANA_HANGUL, "VK_KANA / VK_HANGUL (undocumented?)" },
175
+ { RDP_SCANCODE_ABNT_C1, "VK_ABNT_C1 JP OEM_102" },
176
+ { RDP_SCANCODE_F24_JP, "JP F24" },
177
+ { RDP_SCANCODE_CONVERT_JP, "JP VK_CONVERT" },
178
+ { RDP_SCANCODE_NONCONVERT_JP, "JP VK_NONCONVERT" },
179
+ { RDP_SCANCODE_TAB_JP, "JP TAB" },
180
+ { RDP_SCANCODE_BACKSLASH_JP, "JP OEM_5 ('\')" },
181
+ { RDP_SCANCODE_ABNT_C2, "VK_ABNT_C2, JP" },
182
+ { RDP_SCANCODE_HANJA, "KR VK_HANJA" },
183
+ { RDP_SCANCODE_HANGUL, "KR VK_HANGUL" },
184
+ { RDP_SCANCODE_RETURN_KP, "not RDP_SCANCODE_RETURN Numerical Enter" },
185
+ { RDP_SCANCODE_RCONTROL, "VK_RCONTROL" },
186
+ { RDP_SCANCODE_DIVIDE, "VK_DIVIDE Numerical" },
187
+ { RDP_SCANCODE_PRINTSCREEN, "VK_EXECUTE/VK_PRINT/VK_SNAPSHOT Print Screen" },
188
+ { RDP_SCANCODE_RMENU, "VK_RMENU Right 'Alt' / 'Alt Gr'" },
189
+ { RDP_SCANCODE_PAUSE, "VK_PAUSE Pause / Break (Slightly special handling)" },
190
+ { RDP_SCANCODE_HOME, "VK_HOME" },
191
+ { RDP_SCANCODE_UP, "VK_UP" },
192
+ { RDP_SCANCODE_PRIOR, "VK_PRIOR 'Page Up'" },
193
+ { RDP_SCANCODE_LEFT, "VK_LEFT" },
194
+ { RDP_SCANCODE_RIGHT, "VK_RIGHT" },
195
+ { RDP_SCANCODE_END, "VK_END" },
196
+ { RDP_SCANCODE_DOWN, "VK_DOWN" },
197
+ { RDP_SCANCODE_NEXT, "VK_NEXT 'Page Down'" },
198
+ { RDP_SCANCODE_INSERT, "VK_INSERT" },
199
+ { RDP_SCANCODE_DELETE, "VK_DELETE" },
200
+ { RDP_SCANCODE_NULL, "<00>" },
201
+ { RDP_SCANCODE_HELP2, "Help - documented, different from VK_HELP" },
202
+ { RDP_SCANCODE_LWIN, "VK_LWIN" },
203
+ { RDP_SCANCODE_RWIN, "VK_RWIN" },
204
+ { RDP_SCANCODE_APPS, "VK_APPS Application" },
205
+ { RDP_SCANCODE_POWER_JP, "JP POWER" },
206
+ { RDP_SCANCODE_SLEEP_JP, "JP SLEEP" },
207
+ { RDP_SCANCODE_NUMLOCK_EXTENDED, "should be RDP_SCANCODE_NUMLOCK" },
208
+ { RDP_SCANCODE_RSHIFT_EXTENDED, "should be RDP_SCANCODE_RSHIFT" },
209
+ { RDP_SCANCODE_VOLUME_MUTE, "VK_VOLUME_MUTE" },
210
+ { RDP_SCANCODE_VOLUME_DOWN, "VK_VOLUME_DOWN" },
211
+ { RDP_SCANCODE_VOLUME_UP, "VK_VOLUME_UP" },
212
+ { RDP_SCANCODE_MEDIA_NEXT_TRACK, "VK_MEDIA_NEXT_TRACK" },
213
+ { RDP_SCANCODE_MEDIA_PREV_TRACK, "VK_MEDIA_PREV_TRACK" },
214
+ { RDP_SCANCODE_MEDIA_STOP, "VK_MEDIA_MEDIA_STOP" },
215
+ { RDP_SCANCODE_MEDIA_PLAY_PAUSE, "VK_MEDIA_MEDIA_PLAY_PAUSE" },
216
+ { RDP_SCANCODE_BROWSER_BACK, "VK_BROWSER_BACK" },
217
+ { RDP_SCANCODE_BROWSER_FORWARD, "VK_BROWSER_FORWARD" },
218
+ { RDP_SCANCODE_BROWSER_REFRESH, "VK_BROWSER_REFRESH" },
219
+ { RDP_SCANCODE_BROWSER_STOP, "VK_BROWSER_STOP" },
220
+ { RDP_SCANCODE_BROWSER_SEARCH, "VK_BROWSER_SEARCH" },
221
+ { RDP_SCANCODE_BROWSER_FAVORITES, "VK_BROWSER_FAVORITES" },
222
+ { RDP_SCANCODE_BROWSER_HOME, "VK_BROWSER_HOME" },
223
+ { RDP_SCANCODE_LAUNCH_MAIL, "VK_LAUNCH_MAIL" },
224
+ { RDP_SCANCODE_LAUNCH_MEDIA_SELECT, "VK_LAUNCH_MEDIA_SELECT" },
225
+ { RDP_SCANCODE_LAUNCH_APP1, "VK_LAUNCH_APP1" },
226
+ { RDP_SCANCODE_LAUNCH_APP2, "VK_LAUNCH_APP2" },
227
+ };
228
+
229
+ static int freerdp_detect_keyboard(DWORD* keyboardLayoutId)
230
+ {
231
+ #if defined(_WIN32)
232
+ CHAR name[KL_NAMELENGTH + 1] = { 0 };
233
+ if (GetKeyboardLayoutNameA(name))
234
+ {
235
+ ULONG rc;
236
+
237
+ errno = 0;
238
+ rc = strtoul(name, NULL, 16);
239
+ if (errno == 0)
240
+ *keyboardLayoutId = rc;
241
+ }
242
+
243
+ if (*keyboardLayoutId == 0)
244
+ *keyboardLayoutId = ((DWORD)GetKeyboardLayout(0) >> 16) & 0x0000FFFF;
245
+ #endif
246
+
247
+ #if defined(__MACOSX__)
248
+ if (*keyboardLayoutId == 0)
249
+ freerdp_detect_keyboard_layout_from_cf(keyboardLayoutId);
250
+ #endif
251
+
252
+ #ifdef WITH_X11
253
+ if (*keyboardLayoutId == 0)
254
+ freerdp_detect_keyboard_layout_from_xkb(keyboardLayoutId);
255
+ #endif
256
+
257
+ if (*keyboardLayoutId == 0)
258
+ freerdp_detect_keyboard_layout_from_system_locale(keyboardLayoutId);
259
+
260
+ if (*keyboardLayoutId == 0)
261
+ *keyboardLayoutId = ENGLISH_UNITED_STATES;
262
+
263
+ return 0;
264
+ }
265
+
266
+ static int freerdp_keyboard_init_apple(const DWORD* keyboardLayoutId,
267
+ DWORD* x11_keycode_to_rdp_scancode, size_t count)
268
+ {
269
+ WINPR_ASSERT(x11_keycode_to_rdp_scancode);
270
+ WINPR_ASSERT(keyboardLayoutId);
271
+ WINPR_ASSERT(count <= UINT32_MAX);
272
+ for (size_t keycode = 8; keycode < count; keycode++)
273
+ {
274
+ const DWORD vkcode =
275
+ GetVirtualKeyCodeFromKeycode((UINT32)keycode - 8u, WINPR_KEYCODE_TYPE_APPLE);
276
+ x11_keycode_to_rdp_scancode[keycode] =
277
+ GetVirtualScanCodeFromVirtualKeyCode(vkcode, WINPR_KBD_TYPE_IBM_ENHANCED);
278
+ }
279
+
280
+ maptype = WINPR_KEYCODE_TYPE_APPLE;
281
+ return 0;
282
+ }
283
+
284
+ static int freerdp_keyboard_init_x11_evdev(const DWORD* keyboardLayoutId,
285
+ DWORD* x11_keycode_to_rdp_scancode, size_t count)
286
+ {
287
+ WINPR_ASSERT(keyboardLayoutId);
288
+ WINPR_ASSERT(x11_keycode_to_rdp_scancode);
289
+ WINPR_ASSERT(count <= UINT32_MAX);
290
+
291
+ for (size_t keycode = 0; keycode < count; keycode++)
292
+ {
293
+ const DWORD vkcode =
294
+ GetVirtualKeyCodeFromKeycode((UINT32)keycode, WINPR_KEYCODE_TYPE_EVDEV);
295
+ x11_keycode_to_rdp_scancode[keycode] =
296
+ GetVirtualScanCodeFromVirtualKeyCode(vkcode, WINPR_KBD_TYPE_IBM_ENHANCED);
297
+ }
298
+ maptype = WINPR_KEYCODE_TYPE_EVDEV;
299
+
300
+ return 0;
301
+ }
302
+
303
+ DWORD freerdp_keyboard_init(DWORD keyboardLayoutId)
304
+ {
305
+ int status = -1;
306
+
307
+ #if defined(__APPLE__)
308
+ if (status < 0)
309
+ status = freerdp_keyboard_init_apple(&keyboardLayoutId, X11_KEYCODE_TO_VIRTUAL_SCANCODE,
310
+ ARRAYSIZE(X11_KEYCODE_TO_VIRTUAL_SCANCODE));
311
+ #endif
312
+
313
+ #if defined(WITH_X11) || defined(WITH_WAYLAND)
314
+
315
+ #ifdef WITH_XKBFILE
316
+ if (status < 0)
317
+ {
318
+ status = freerdp_keyboard_init_xkbfile(&keyboardLayoutId, X11_KEYCODE_TO_VIRTUAL_SCANCODE,
319
+ ARRAYSIZE(X11_KEYCODE_TO_VIRTUAL_SCANCODE));
320
+ if (status >= 0)
321
+ maptype = WINPR_KEYCODE_TYPE_XKB;
322
+ }
323
+ #endif
324
+
325
+ if (status < 0)
326
+ status = freerdp_keyboard_init_x11_evdev(&keyboardLayoutId, X11_KEYCODE_TO_VIRTUAL_SCANCODE,
327
+ ARRAYSIZE(X11_KEYCODE_TO_VIRTUAL_SCANCODE));
328
+
329
+ #endif
330
+
331
+ if (status < 0)
332
+ WLog_DBG(TAG, "Platform keyboard detection failed, trying autodetection");
333
+
334
+ freerdp_detect_keyboard(&keyboardLayoutId);
335
+
336
+ ZeroMemory(VIRTUAL_SCANCODE_TO_X11_KEYCODE, sizeof(VIRTUAL_SCANCODE_TO_X11_KEYCODE));
337
+
338
+ WINPR_STATIC_ASSERT(ARRAYSIZE(VIRTUAL_SCANCODE_TO_X11_KEYCODE) <= UINT32_MAX);
339
+ for (size_t keycode = 0; keycode < ARRAYSIZE(VIRTUAL_SCANCODE_TO_X11_KEYCODE); keycode++)
340
+ {
341
+ const DWORD x11 = X11_KEYCODE_TO_VIRTUAL_SCANCODE[keycode];
342
+ const DWORD sc = RDP_SCANCODE_CODE(x11);
343
+ const BOOL ex = RDP_SCANCODE_EXTENDED(x11);
344
+ VIRTUAL_SCANCODE_TO_X11_KEYCODE[sc][ex ? 1 : 0] = (UINT32)keycode;
345
+ }
346
+
347
+ return keyboardLayoutId;
348
+ }
349
+
350
+ FREERDP_REMAP_TABLE* freerdp_keyboard_remap_string_to_list(const char* list)
351
+ {
352
+ const size_t remap_table_size = 0x10000;
353
+
354
+ FREERDP_REMAP_TABLE* remap_table = calloc(1, sizeof(FREERDP_REMAP_TABLE));
355
+ if (!remap_table)
356
+ return NULL;
357
+
358
+ for (size_t x = 0; x < ARRAYSIZE(remap_table->table); x++)
359
+ remap_table->table[x] = (UINT32)x;
360
+
361
+ if (!list)
362
+ return remap_table;
363
+
364
+ BOOL success = FALSE;
365
+ char* copy = _strdup(list);
366
+ if (!copy)
367
+ goto fail;
368
+
369
+ char* context = NULL;
370
+ char* token = strtok_s(copy, ",", &context);
371
+ while (token)
372
+ {
373
+ DWORD key = 0;
374
+ DWORD value = 0;
375
+ if (!freerdp_extract_key_value(token, &key, &value))
376
+ goto fail;
377
+ if (key >= remap_table_size)
378
+ goto fail;
379
+ remap_table->table[key] = value;
380
+ token = strtok_s(NULL, ",", &context);
381
+ }
382
+
383
+ success = TRUE;
384
+
385
+ fail:
386
+ free(copy);
387
+
388
+ if (!success)
389
+ {
390
+ free(remap_table);
391
+ return NULL;
392
+ }
393
+ return remap_table;
394
+ }
395
+
396
+ DWORD freerdp_keyboard_init_ex(DWORD keyboardLayoutId, const char* keyboardRemappingList)
397
+ {
398
+ DWORD res = freerdp_keyboard_init(keyboardLayoutId);
399
+
400
+ memset(REMAPPING_TABLE, 0, sizeof(REMAPPING_TABLE));
401
+ if (keyboardRemappingList)
402
+ {
403
+ char* copy = _strdup(keyboardRemappingList);
404
+ char* context = NULL;
405
+ char* token = NULL;
406
+ if (!copy)
407
+ goto fail;
408
+ token = strtok_s(copy, ",", &context);
409
+ while (token)
410
+ {
411
+ DWORD key = 0;
412
+ DWORD value = 0;
413
+ if (!freerdp_extract_key_value(token, &key, &value))
414
+ goto fail;
415
+ if (key >= ARRAYSIZE(REMAPPING_TABLE))
416
+ goto fail;
417
+ REMAPPING_TABLE[key] = value;
418
+ token = strtok_s(NULL, ",", &context);
419
+ }
420
+ fail:
421
+ free(copy);
422
+ }
423
+ return res;
424
+ }
425
+
426
+ DWORD freerdp_keyboard_get_rdp_scancode_from_x11_keycode(DWORD keycode)
427
+ {
428
+ if (keycode >= ARRAYSIZE(X11_KEYCODE_TO_VIRTUAL_SCANCODE))
429
+ {
430
+ WLog_ERR(TAG, "KeyCode %" PRIu32 " exceeds allowed value range [0,%" PRIuz "]", keycode,
431
+ ARRAYSIZE(X11_KEYCODE_TO_VIRTUAL_SCANCODE));
432
+ return 0;
433
+ }
434
+
435
+ const DWORD scancode = X11_KEYCODE_TO_VIRTUAL_SCANCODE[keycode];
436
+ if (scancode >= ARRAYSIZE(REMAPPING_TABLE))
437
+ {
438
+ WLog_ERR(TAG, "ScanCode %" PRIu32 " exceeds allowed value range [0,%" PRIuz "]", scancode,
439
+ ARRAYSIZE(REMAPPING_TABLE));
440
+ return 0;
441
+ }
442
+
443
+ const DWORD remapped = REMAPPING_TABLE[scancode];
444
+ #if defined(WITH_DEBUG_KBD)
445
+ const BOOL ex = RDP_SCANCODE_EXTENDED(scancode);
446
+ const DWORD sc = RDP_SCANCODE_CODE(scancode);
447
+ #endif
448
+
449
+ DEBUG_KBD("x11 keycode: %02" PRIX32 " -> rdp code: [%04" PRIx16 "] %02" PRIX8 "%s", keycode,
450
+ scancode, sc, ex ? " extended" : "");
451
+
452
+ if (remapped != 0)
453
+ {
454
+ #if defined(WITH_DEBUG_KBD)
455
+ const DWORD rsc = RDP_SCANCODE_CODE(remapped);
456
+ const BOOL rex = RDP_SCANCODE_EXTENDED(remapped);
457
+ #endif
458
+
459
+ DEBUG_KBD("remapped scancode: [%04" PRIx16 "] %02" PRIX8 "[%s] -> [%04" PRIx16 "] %02" PRIX8
460
+ "[%s]",
461
+ scancode, sc, ex ? " extended" : "", remapped, rsc, rex ? " extended" : "");
462
+ return remapped;
463
+ }
464
+ return scancode;
465
+ }
466
+
467
+ DWORD freerdp_keyboard_get_x11_keycode_from_rdp_scancode(DWORD scancode, BOOL extended)
468
+ {
469
+ if (scancode >= ARRAYSIZE(VIRTUAL_SCANCODE_TO_X11_KEYCODE))
470
+ {
471
+ WLog_ERR(TAG, "ScanCode %" PRIu32 " exceeds allowed value range [0,%" PRIuz "]", scancode,
472
+ ARRAYSIZE(VIRTUAL_SCANCODE_TO_X11_KEYCODE));
473
+ return 0;
474
+ }
475
+
476
+ const DWORD* x11 = VIRTUAL_SCANCODE_TO_X11_KEYCODE[scancode];
477
+ WINPR_ASSERT(x11);
478
+
479
+ if (extended)
480
+ return x11[1];
481
+ else
482
+ return x11[0];
483
+ }
484
+
485
+ const char* freerdp_keyboard_scancode_name(DWORD scancode)
486
+ {
487
+ for (size_t x = 0; x < ARRAYSIZE(RDP_SCANCODE_MAP); x++)
488
+ {
489
+ const struct scancode_map_entry* entry = &RDP_SCANCODE_MAP[x];
490
+ if (entry->scancode == scancode)
491
+ return entry->name;
492
+ }
493
+
494
+ return NULL;
495
+ }
496
+
497
+ DWORD freerdp_keyboard_remap_key(const FREERDP_REMAP_TABLE* remap_table, DWORD rdpScanCode)
498
+ {
499
+ if (!remap_table || (ARRAYSIZE(remap_table->table) <= rdpScanCode))
500
+ return 0;
501
+
502
+ return remap_table->table[rdpScanCode];
503
+ }
504
+
505
+ void freerdp_keyboard_remap_free(FREERDP_REMAP_TABLE* table)
506
+ {
507
+ free(table);
508
+ }
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/locale/keyboard_apple.c ADDED
@@ -0,0 +1,241 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * Apple Core Foundation Keyboard Mapping
4
+ *
5
+ * Copyright 2021 Thincast Technologies GmbH
6
+ * Copyright 2021 Martin Fleisz <martin.fleisz@thincast.com>
7
+ *
8
+ * Licensed under the Apache License, Version 2.0 (the "License");
9
+ * you may not use this file except in compliance with the License.
10
+ * You may obtain a copy of the License at
11
+ *
12
+ * http://www.apache.org/licenses/LICENSE-2.0
13
+ *
14
+ * Unless required by applicable law or agreed to in writing, software
15
+ * distributed under the License is distributed on an "AS IS" BASIS,
16
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ * See the License for the specific language governing permissions and
18
+ * limitations under the License.
19
+ */
20
+
21
+ #include <freerdp/config.h>
22
+
23
+ #include <Carbon/Carbon.h>
24
+ #include <string.h>
25
+
26
+ #include "liblocale.h"
27
+
28
+ #include <freerdp/locale/locale.h>
29
+ #include <freerdp/locale/keyboard.h>
30
+
31
+ #include "keyboard_apple.h"
32
+
33
+ struct KEYBOARD_LAYOUT_MAPPING_
34
+ {
35
+ const char* inputSourceId; /* Apple input source id (com.apple.keylayout or inputmethod) */
36
+ DWORD code; /* mapped rdp keyboard layout id */
37
+ };
38
+ typedef struct KEYBOARD_LAYOUT_MAPPING_ KEYBOARD_LAYOUT_MAPPING;
39
+
40
+ static const KEYBOARD_LAYOUT_MAPPING KEYBOARD_MAPPING_TABLE[] = {
41
+ { "com.apple.inputmethod.Kotoeri.Japanese", JAPANESE },
42
+ { "com.apple.inputmethod.Kotoeri.Japanese.FullWidthRoman", JAPANESE },
43
+ { "com.apple.inputmethod.Kotoeri.Japanese.HalfWidthKana", JAPANESE },
44
+ { "com.apple.inputmethod.Kotoeri.Japanese.Katakana", JAPANESE },
45
+ { "com.apple.inputmethod.Kotoeri.Katakana", JAPANESE },
46
+ { "com.apple.inputmethod.Kotoeri.Roman", JAPANESE },
47
+ { "com.apple.inputmethod.kotoeri.Ainu", JAPANESE },
48
+ { "com.apple.keylayout.2SetHangul", KOREAN },
49
+ { "com.apple.keylayout.390Hangul", KOREAN },
50
+ { "com.apple.keylayout.3SetHangul", KOREAN },
51
+ { "com.apple.keylayout.AfghanDari", KBD_PERSIAN },
52
+ { "com.apple.keylayout.AfghanPashto", PASHTO },
53
+ { "com.apple.keylayout.AfghanUzbek", UZBEK_LATIN },
54
+ { "com.apple.keylayout.Arabic", ARABIC_SAUDI_ARABIA },
55
+ { "com.apple.keylayout.Arabic-QWERTY", ARABIC_EGYPT },
56
+ { "com.apple.keylayout.ArabicPC", ARABIC_EGYPT },
57
+ { "com.apple.keylayout.Armenian-HMQWERTY", ARMENIAN },
58
+ { "com.apple.keylayout.Armenian-WesternQWERTY", ARMENIAN },
59
+ { "com.apple.keylayout.Australian", ENGLISH_AUSTRALIAN },
60
+ { "com.apple.keylayout.Austrian", GERMAN_STANDARD },
61
+ { "com.apple.keylayout.Azeri", AZERI_LATIN },
62
+ { "com.apple.keylayout.Bangla", KBD_BANGLA },
63
+ { "com.apple.keylayout.Bangla-QWERTY", KBD_BANGLA },
64
+ { "com.apple.keylayout.Belgian", DUTCH_BELGIAN },
65
+ { "com.apple.keylayout.Brazilian", PORTUGUESE_BRAZILIAN },
66
+ { "com.apple.keylayout.British", ENGLISH_UNITED_KINGDOM },
67
+ { "com.apple.keylayout.British-PC", ENGLISH_UNITED_KINGDOM },
68
+ { "com.apple.keylayout.Bulgarian", BULGARIAN },
69
+ { "com.apple.keylayout.Bulgarian-Phonetic", KBD_BULGARIAN_PHONETIC },
70
+ { "com.apple.keylayout.Byelorussian", BELARUSIAN },
71
+ { "com.apple.keylayout.Canadian", ENGLISH_UNITED_STATES },
72
+ { "com.apple.keylayout.Canadian-CSA", KBD_CANADIAN_MULTILINGUAL_STANDARD },
73
+ { "com.apple.keylayout.CangjieKeyboard", CHINESE_TAIWAN },
74
+ { "com.apple.keylayout.Cherokee-Nation", CHEROKEE },
75
+ { "com.apple.keylayout.Cherokee-QWERTY", ENGLISH_UNITED_STATES },
76
+ { "com.apple.keylayout.Colemak", ENGLISH_UNITED_STATES },
77
+ { "com.apple.keylayout.Croatian", CROATIAN },
78
+ { "com.apple.keylayout.Croatian-PC", CROATIAN },
79
+ { "com.apple.keylayout.Czech", CZECH },
80
+ { "com.apple.keylayout.Czech-QWERTY", KBD_CZECH_QWERTY },
81
+ { "com.apple.keylayout.DVORAK-QWERTYCMD", KBD_UNITED_STATES_DVORAK },
82
+ { "com.apple.keylayout.Danish", DANISH },
83
+ { "com.apple.keylayout.Devanagari", HINDI },
84
+ { "com.apple.keylayout.Devanagari-QWERTY", HINDI },
85
+ { "com.apple.keylayout.Dutch", KBD_UNITED_STATES_INTERNATIONAL },
86
+ { "com.apple.keylayout.Dvorak", KBD_UNITED_STATES_DVORAK },
87
+ { "com.apple.keylayout.Dvorak-Left", KBD_UNITED_STATES_DVORAK_FOR_LEFT_HAND },
88
+ { "com.apple.keylayout.Dvorak-Right", KBD_UNITED_STATES_DVORAK_FOR_RIGHT_HAND },
89
+ { "com.apple.keylayout.Estonian", ESTONIAN },
90
+ { "com.apple.keylayout.Faroese", FAEROESE },
91
+ { "com.apple.keylayout.Finnish", FINNISH },
92
+ { "com.apple.keylayout.FinnishExtended", KBD_SAMI_EXTENDED_FINLAND_SWEDEN },
93
+ { "com.apple.keylayout.FinnishSami-PC", KBD_FINNISH_WITH_SAMI },
94
+ { "com.apple.keylayout.French", KBD_BELGIAN_FRENCH },
95
+ { "com.apple.keylayout.French-PC", FRENCH_STANDARD },
96
+ { "com.apple.keylayout.French-numerical", KBD_BELGIAN_FRENCH },
97
+ { "com.apple.keylayout.GJCRomaja", ENGLISH_UNITED_STATES },
98
+ { "com.apple.keylayout.Georgian-QWERTY", KBD_GEORGIAN_QUERTY },
99
+ { "com.apple.keylayout.German", GERMAN_STANDARD },
100
+ { "com.apple.keylayout.Greek", GREEK },
101
+ { "com.apple.keylayout.GreekPolytonic", KBD_GREEK_POLYTONIC },
102
+ { "com.apple.keylayout.Gujarati", GUJARATI },
103
+ { "com.apple.keylayout.Gujarati-QWERTY", GUJARATI },
104
+ { "com.apple.keylayout.Gurmukhi", PUNJABI },
105
+ { "com.apple.keylayout.Gurmukhi-QWERTY", PUNJABI },
106
+ { "com.apple.keylayout.HNCRomaja", ENGLISH_UNITED_STATES },
107
+ { "com.apple.keylayout.Hawaiian", HAWAIIAN },
108
+ { "com.apple.keylayout.Hebrew", HEBREW },
109
+ { "com.apple.keylayout.Hebrew-PC", HEBREW },
110
+ { "com.apple.keylayout.Hebrew-QWERTY", HEBREW },
111
+ { "com.apple.keylayout.Hungarian", HUNGARIAN },
112
+ { "com.apple.keylayout.Hungarian-QWERTY", HUNGARIAN },
113
+ { "com.apple.keylayout.Icelandic", ICELANDIC },
114
+ { "com.apple.keylayout.Inuktitut-Nunavut", INUKTITUT },
115
+ { "com.apple.keylayout.Inuktitut-Nutaaq", INUKTITUT },
116
+ { "com.apple.keylayout.Inuktitut-QWERTY", INUKTITUT },
117
+ { "com.apple.keylayout.InuttitutNunavik", INUKTITUT },
118
+ { "com.apple.keylayout.Irish", ENGLISH_IRELAND },
119
+ { "com.apple.keylayout.IrishExtended", KBD_IRISH },
120
+ { "com.apple.keylayout.Italian", ITALIAN_STANDARD },
121
+ { "com.apple.keylayout.Italian-Pro", ITALIAN_STANDARD },
122
+ { "com.apple.keylayout.Jawi-QWERTY", ARABIC_SAUDI_ARABIA },
123
+ { "com.apple.keylayout.Kannada", KANNADA },
124
+ { "com.apple.keylayout.Kannada-QWERTY", KANNADA },
125
+ { "com.apple.keylayout.Kazakh", KAZAKH },
126
+ { "com.apple.keylayout.Khmer", KBD_KHMER },
127
+ { "com.apple.keylayout.Latvian", LATVIAN },
128
+ { "com.apple.keylayout.Lithuanian", LITHUANIAN },
129
+ { "com.apple.keylayout.Macedonian", MACEDONIAN },
130
+ { "com.apple.keylayout.Malayalam", MALAYALAM },
131
+ { "com.apple.keylayout.Malayalam-QWERTY", MALAYALAM },
132
+ { "com.apple.keylayout.Maltese", MALTESE },
133
+ { "com.apple.keylayout.Maori", MAORI },
134
+ { "com.apple.keylayout.Myanmar-QWERTY", MYANMAR },
135
+ { "com.apple.keylayout.Nepali", NEPALI },
136
+ { "com.apple.keylayout.NorthernSami", SAMI_NORTHERN_NORWAY },
137
+ { "com.apple.keylayout.Norwegian", NORWEGIAN_BOKMAL },
138
+ { "com.apple.keylayout.NorwegianExtended", NORWEGIAN_BOKMAL },
139
+ { "com.apple.keylayout.NorwegianSami-PC", NORWEGIAN_BOKMAL },
140
+ { "com.apple.keylayout.Oriya", ORIYA },
141
+ { "com.apple.keylayout.Persian", KBD_PERSIAN },
142
+ { "com.apple.keylayout.Persian-ISIRI2901", KBD_PERSIAN },
143
+ { "com.apple.keylayout.Polish", KBD_POLISH_214 },
144
+ { "com.apple.keylayout.PolishPro", KBD_POLISH_PROGRAMMERS },
145
+ { "com.apple.keylayout.Portuguese", PORTUGUESE_STANDARD },
146
+ { "com.apple.keylayout.Romanian", KBD_ROMANIAN },
147
+ { "com.apple.keylayout.Romanian-Standard", KBD_ROMANIAN_STANDARD },
148
+ { "com.apple.keylayout.Russian", RUSSIAN },
149
+ { "com.apple.keylayout.Russian-Phonetic", KBD_RUSSIAN_PHONETIC },
150
+ { "com.apple.keylayout.RussianWin", RUSSIAN },
151
+ { "com.apple.keylayout.Sami-PC", KBD_SAMI_EXTENDED_FINLAND_SWEDEN },
152
+ { "com.apple.keylayout.Serbian", KBD_SERBIAN_CYRILLIC },
153
+ { "com.apple.keylayout.Serbian-Latin", KBD_SERBIAN_LATIN },
154
+ { "com.apple.keylayout.Sinhala", SINHALA },
155
+ { "com.apple.keylayout.Sinhala-QWERTY", SINHALA },
156
+ { "com.apple.keylayout.Slovak", SLOVAK },
157
+ { "com.apple.keylayout.Slovak-QWERTY", KBD_SLOVAK_QWERTY },
158
+ { "com.apple.keylayout.Slovenian", SLOVENIAN },
159
+ { "com.apple.keylayout.Spanish", SPANISH_TRADITIONAL_SORT },
160
+ { "com.apple.keylayout.Spanish-ISO", KBD_SPANISH },
161
+ { "com.apple.keylayout.Swedish", SWEDISH },
162
+ { "com.apple.keylayout.Swedish-Pro", SWEDISH },
163
+ { "com.apple.keylayout.SwedishSami-PC", SWEDISH },
164
+ { "com.apple.keylayout.SwissFrench", FRENCH_SWISS },
165
+ { "com.apple.keylayout.SwissGerman", GERMAN_SWISS },
166
+ { "com.apple.keylayout.Telugu", TELUGU },
167
+ { "com.apple.keylayout.Telugu-QWERTY", TELUGU },
168
+ { "com.apple.keylayout.Thai", THAI },
169
+ { "com.apple.keylayout.Thai-PattaChote", KBD_THAI_PATTACHOTE },
170
+ { "com.apple.keylayout.Tibetan-QWERTY", TIBETAN_PRC },
171
+ { "com.apple.keylayout.Tibetan-Wylie", TIBETAN_PRC },
172
+ { "com.apple.keylayout.TibetanOtaniUS", TIBETAN_PRC },
173
+ { "com.apple.keylayout.Turkish", KBD_TURKISH_F },
174
+ { "com.apple.keylayout.Turkish-QWERTY", TURKISH },
175
+ { "com.apple.keylayout.Turkish-QWERTY-PC", TURKISH },
176
+ { "com.apple.keylayout.US", ENGLISH_UNITED_STATES },
177
+ { "com.apple.keylayout.USExtended", ENGLISH_UNITED_STATES },
178
+ { "com.apple.keylayout.USInternational-PC", ENGLISH_UNITED_STATES },
179
+ { "com.apple.keylayout.Ukrainian", UKRAINIAN },
180
+ { "com.apple.keylayout.Ukrainian-PC", UKRAINIAN },
181
+ { "com.apple.keylayout.UnicodeHexInput", ENGLISH_UNITED_STATES },
182
+ { "com.apple.keylayout.Urdu", URDU },
183
+ { "com.apple.keylayout.Uyghur", UIGHUR },
184
+ { "com.apple.keylayout.Vietnamese", VIETNAMESE },
185
+ { "com.apple.keylayout.Welsh", WELSH }
186
+ };
187
+
188
+ int freerdp_detect_keyboard_layout_from_cf(DWORD* keyboardLayoutId)
189
+ {
190
+ CFIndex length;
191
+ char* inputSourceId = NULL;
192
+ CFStringRef inputSourceIdRef;
193
+ TISInputSourceRef inputSrc = TISCopyCurrentKeyboardLayoutInputSource();
194
+ if (!inputSrc)
195
+ {
196
+ DEBUG_KBD("Failed to get current keyboard layout input source!");
197
+ return 0;
198
+ }
199
+
200
+ /* get current input source id */
201
+ inputSourceIdRef = (CFStringRef)TISGetInputSourceProperty(inputSrc, kTISPropertyInputSourceID);
202
+ if (!inputSourceIdRef)
203
+ {
204
+ DEBUG_KBD("Failed to get input source id!");
205
+ goto done;
206
+ }
207
+
208
+ /* convert it to a C-string */
209
+ length = CFStringGetLength(inputSourceIdRef);
210
+ length = CFStringGetMaximumSizeForEncoding(length, kCFStringEncodingUTF8) + 1;
211
+ inputSourceId = (char*)malloc(length);
212
+ if (!inputSourceId)
213
+ {
214
+ DEBUG_KBD("Failed to allocate string buffer!");
215
+ goto done;
216
+ }
217
+
218
+ if (!CFStringGetCString(inputSourceIdRef, inputSourceId, length, kCFStringEncodingUTF8))
219
+ {
220
+ DEBUG_KBD("Failed to convert CFString to C-string!");
221
+ goto done;
222
+ }
223
+
224
+ /* Search for the id in the mapping table */
225
+ for (size_t i = 0; i < ARRAYSIZE(KEYBOARD_MAPPING_TABLE); ++i)
226
+ {
227
+ if (strcmp(inputSourceId, KEYBOARD_MAPPING_TABLE[i].inputSourceId) == 0)
228
+ {
229
+ *keyboardLayoutId = KEYBOARD_MAPPING_TABLE[i].code;
230
+ break;
231
+ }
232
+ }
233
+
234
+ done:
235
+ free(inputSourceId);
236
+ CFRelease(inputSrc);
237
+ if (*keyboardLayoutId > 0)
238
+ return *keyboardLayoutId;
239
+
240
+ return 0;
241
+ }
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/locale/keyboard_apple.h ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * Apple Core Foundation Keyboard Mapping
4
+ *
5
+ * Copyright 2021 Thincast Technologies GmbH
6
+ * Copyright 2021 Martin Fleisz <martin.fleisz@thincast.com>
7
+ *
8
+ * Licensed under the Apache License, Version 2.0 (the "License");
9
+ * you may not use this file except in compliance with the License.
10
+ * You may obtain a copy of the License at
11
+ *
12
+ * http://www.apache.org/licenses/LICENSE-2.0
13
+ *
14
+ * Unless required by applicable law or agreed to in writing, software
15
+ * distributed under the License is distributed on an "AS IS" BASIS,
16
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ * See the License for the specific language governing permissions and
18
+ * limitations under the License.
19
+ */
20
+
21
+ #ifndef FREERDP_LOCALE_KEYBOARD_APPLE_H
22
+ #define FREERDP_LOCALE_KEYBOARD_APPLE_H
23
+
24
+ #include <freerdp/api.h>
25
+
26
+ FREERDP_LOCAL int freerdp_detect_keyboard_layout_from_cf(DWORD* keyboardLayoutId);
27
+
28
+ #endif /* FREERDP_LOCALE_KEYBOARD_APPLE_H */
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/locale/keyboard_layout.c ADDED
@@ -0,0 +1,1662 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * Keyboard Layouts
4
+ *
5
+ * Copyright 2009-2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+
20
+ #include <freerdp/config.h>
21
+
22
+ #include <stdio.h>
23
+ #include <stdlib.h>
24
+ #include <string.h>
25
+
26
+ #include <winpr/crt.h>
27
+ #include <winpr/path.h>
28
+ #include <winpr/json.h>
29
+
30
+ #include "liblocale.h"
31
+
32
+ #include <freerdp/types.h>
33
+ #include <freerdp/scancode.h>
34
+ #include <freerdp/locale/keyboard.h>
35
+
36
+ #include <freerdp/log.h>
37
+
38
+ #if defined(WITH_KEYBOARD_LAYOUT_FROM_FILE)
39
+ #define TAG FREERDP_TAG("locale.keyboard.layouts")
40
+ #endif
41
+
42
+ // #define DUMP_LAYOUTS_TO_JSON
43
+
44
+ struct LanguageIdentifier
45
+ {
46
+ /* LanguageIdentifier = (SublangaugeIdentifier<<2) | PrimaryLanguageIdentifier
47
+ * The table at
48
+ * https://docs.microsoft.com/en-us/windows/win32/intl/language-identifier-constants-and-strings
49
+ * is sometimes missing one or both of the entries.
50
+ */
51
+ const char* locale; /* en_US type strings for the locale */
52
+ UINT16 LanguageIdentifier;
53
+ const char* PrimaryLanguage;
54
+ UINT8 PrimaryLanguageIdentifier;
55
+ const char* PrimaryLanguageSymbol;
56
+ const char* Sublanguage;
57
+ UINT8 SublangaugeIdentifier;
58
+ const char* SublanguageSymbol;
59
+ };
60
+
61
+ typedef struct
62
+ {
63
+ INT64 code; /* Keyboard layout code */
64
+ DWORD id; /* Keyboard variant ID */
65
+ char* name; /* Keyboard layout variant name */
66
+ } RDP_KEYBOARD_LAYOUT_VARIANT;
67
+
68
+ typedef struct
69
+ {
70
+ INT64 code; /* Keyboard layout code */
71
+ char* file; /* IME file */
72
+ char* name; /* Keyboard layout name */
73
+ } RDP_KEYBOARD_IME;
74
+
75
+ static const struct LanguageIdentifier language_identifiers[] = {
76
+ /* [Language identifier] [Primary language] [Prim. lang. identifier] [Prim.
77
+ lang. symbol] [Sublanguage] [Sublang. identifier] [Sublang. symbol] */
78
+ { "", 0xc00, "Default custom locale language", 0x0, "LANG_NEUTRAL",
79
+ "Default custom sublanguage", 0x3, "SUBLANG_CUSTOM_DEFAULT" },
80
+ { "", 0x1400, "Default custom MUI locale language", 0x0, "LANG_NEUTRAL",
81
+ "Default custom MUI sublanguage", 0x5, "SUBLANG_UI_CUSTOM_DEFAULT" },
82
+ { "", 0x7f, "Invariant locale language", 0x7f, "LANG_INVARIANT", "Invariant sublanguage", 0x0,
83
+ "SUBLANG_NEUTRAL" },
84
+ { "", 0x0, "Neutral locale language", 0x0, "LANG_NEUTRAL", "Neutral sublanguage", 0x0,
85
+ "SUBLANG_NEUTRAL" },
86
+ { "", 0x800, "System default locale language", 0x2, "LANG_SYSTEM_DEFAULT",
87
+ "System default sublanguage", 0x2, "SUBLANG_SYS_DEFAULT" },
88
+ { "", 0x1000, "Unspecified custom locale language", 0x0, "LANG_NEUTRAL",
89
+ "Unspecified custom sublanguage", 0x4, "SUBLANG_CUSTOM_UNSPECIFIED" },
90
+ { "", 0x400, "User default locale language", 0x0, "LANG_USER_DEFAULT",
91
+ "User default sublanguage", 0x1, "SUBLANG_DEFAULT" },
92
+ { "af_ZA", 0x436, "Afrikaans (af)", 0x36, "LANG_AFRIKAANS", "South Africa (ZA)", 0x1,
93
+ "SUBLANG_AFRIKAANS_SOUTH_AFRICA" },
94
+ { "sq_AL", 0x41c, "Albanian (sq)", 0x1c, "LANG_ALBANIAN", "Albania (AL)", 0x1,
95
+ "SUBLANG_ALBANIAN_ALBANIA" },
96
+ { "gsw_FR", 0x484, "Alsatian (gsw)", 0x84, "LANG_ALSATIAN", "France (FR)", 0x1,
97
+ "SUBLANG_ALSATIAN_FRANCE" },
98
+ { "am_ET", 0x45e, "Amharic (am)", 0x5e, "LANG_AMHARIC", "Ethiopia (ET)", 0x1,
99
+ "SUBLANG_AMHARIC_ETHIOPIA" },
100
+ { "ar_DZ", 0x1401, "Arabic (ar)", 0x1, "LANG_ARABIC", "Algeria (DZ)", 0x5,
101
+ "SUBLANG_ARABIC_ALGERIA" },
102
+ { "ar_BH", 0x3c01, "Arabic (ar)", 0x01, "LANG_ARABIC", "Bahrain (BH)", 0xf,
103
+ "SUBLANG_ARABIC_BAHRAIN" },
104
+ { "ar_EG", 0xc01, "Arabic (ar)", 0x01, "LANG_ARABIC", "Egypt (EG)", 0x3,
105
+ "SUBLANG_ARABIC_EGYPT" },
106
+ { "ar_IQ", 0x801, "Arabic (ar)", 0x01, "LANG_ARABIC", "Iraq (IQ)", 0x2, "SUBLANG_ARABIC_IRAQ" },
107
+ { "ar_JO", 0x2c01, "Arabic (ar)", 0x01, "LANG_ARABIC", "Jordan (JO)", 0xb,
108
+ "SUBLANG_ARABIC_JORDAN" },
109
+ { "ar_KW", 0x3401, "Arabic (ar)", 0x01, "LANG_ARABIC", "Kuwait (KW)", 0xd,
110
+ "SUBLANG_ARABIC_KUWAIT" },
111
+ { "ar_LB", 0x3001, "Arabic (ar)", 0x01, "LANG_ARABIC", "Lebanon (LB)", 0xc,
112
+ "SUBLANG_ARABIC_LEBANON" },
113
+ { "ar_LY", 0x1001, "Arabic (ar)", 0x01, "LANG_ARABIC", "Libya (LY)", 0x4,
114
+ "SUBLANG_ARABIC_LIBYA" },
115
+ { "ar_MA", 0x1801, "Arabic (ar)", 0x01, "LANG_ARABIC", "Morocco (MA)", 0x6,
116
+ "SUBLANG_ARABIC_MOROCCO" },
117
+ { "ar_OM", 0x2001, "Arabic (ar)", 0x01, "LANG_ARABIC", "Oman (OM)", 0x8,
118
+ "SUBLANG_ARABIC_OMAN" },
119
+ { "ar_QA", 0x4001, "Arabic (ar)", 0x01, "LANG_ARABIC", "Qatar (QA)", 0x10,
120
+ "SUBLANG_ARABIC_QATAR" },
121
+ { "ar_SA", 0x401, "Arabic (ar)", 0x01, "LANG_ARABIC", "Saudi Arabia (SA)", 0x1,
122
+ "SUBLANG_ARABIC_SAUDI_ARABIA" },
123
+ { "ar_SY", 0x2801, "Arabic (ar)", 0x01, "LANG_ARABIC", "Syria (SY)", 0xa,
124
+ "SUBLANG_ARABIC_SYRIA" },
125
+ { "ar_TN", 0x1c01, "Arabic (ar)", 0x01, "LANG_ARABIC", "Tunisia (TN)", 0x7,
126
+ "SUBLANG_ARABIC_TUNISIA" },
127
+ { "ar_AE", 0x3801, "Arabic (ar)", 0x01, "LANG_ARABIC", "U.A.E. (AE)", 0xe,
128
+ "SUBLANG_ARABIC_UAE" },
129
+ { "ar_YE", 0x2401, "Arabic (ar)", 0x01, "LANG_ARABIC", "Yemen (YE)", 0x9,
130
+ "SUBLANG_ARABIC_YEMEN" },
131
+ { "hy_AM", 0x42b, "Armenian (hy)", 0x2b, "LANG_ARMENIAN", "Armenia (AM)", 0x1,
132
+ "SUBLANG_ARMENIAN_ARMENIA" },
133
+ { "as_IN", 0x44d, "Assamese (as)", 0x4d, "LANG_ASSAMESE", "India (IN)", 0x1,
134
+ "SUBLANG_ASSAMESE_INDIA" },
135
+ { "az_AZ", 0x82c, "Azerbaijani (az)", 0x2c, "LANG_AZERI", "Azerbaijan, Cyrillic (AZ)", 0x2,
136
+ "SUBLANG_AZERI_CYRILLIC" },
137
+ { "az_AZ", 0x42c, "Azerbaijani (az)", 0x2c, "LANG_AZERI", "Azerbaijan, Latin (AZ)", 0x1,
138
+ "SUBLANG_AZERI_LATIN" },
139
+ { "bn_BD", 0x445, "Bangla (bn)", 0x45, "LANG_BANGLA", "Bangladesh (BD)", 0x2,
140
+ "SUBLANG_BANGLA_BANGLADESH" },
141
+ { "bn_IN", 0x445, "Bangla (bn)", 0x45, "LANG_BANGLA", "India (IN)", 0x1,
142
+ "SUBLANG_BANGLA_INDIA" },
143
+ { "ba_RU", 0x46d, "Bashkir (ba)", 0x6d, "LANG_BASHKIR", "Russia (RU)", 0x1,
144
+ "SUBLANG_BASHKIR_RUSSIA" },
145
+ { "", 0x42d, "Basque (Basque)", 0x2d, "LANG_BASQUE", "Basque (Basque)", 0x1,
146
+ "SUBLANG_BASQUE_BASQUE" },
147
+ { "be_BY", 0x423, "Belarusian (be)", 0x23, "LANG_BELARUSIAN", "Belarus (BY)", 0x1,
148
+ "SUBLANG_BELARUSIAN_BELARUS" },
149
+ { "bs", 0x781a, "Bosnian (bs)", 0x1a, "LANG_BOSNIAN_NEUTRAL", "Neutral", 0x1E, "" },
150
+ { "bs_BA", 0x201a, "Bosnian (bs)", 0x1a, "LANG_BOSNIAN",
151
+ "Bosnia and Herzegovina, Cyrillic (BA)", 0x8, "SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_CYRILLIC" },
152
+ { "bs_BA", 0x141a, "Bosnian (bs)", 0x1a, "LANG_BOSNIAN", "Bosnia and Herzegovina, Latin (BA)",
153
+ 0x5, "SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_LATIN" },
154
+ { "br_FR", 0x47e, "Breton (br)", 0x7e, "LANG_BRETON", "France (FR)", 0x1,
155
+ "SUBLANG_BRETON_FRANCE" },
156
+ { "bg_BG", 0x402, "Bulgarian (bg)", 0x2, "LANG_BULGARIAN", "Bulgaria (BG)", 0x1,
157
+ "SUBLANG_BULGARIAN_BULGARIA" },
158
+ { "ku_IQ", 0x492, "Central Kurdish (ku)", 0x92, "LANG_CENTRAL_KURDISH", "Iraq (IQ)", 0x1,
159
+ "SUBLANG_CENTRAL_KURDISH_IRAQ" },
160
+ { "chr_US", 0x45c, "Cherokee (chr)", 0x5c, "LANG_CHEROKEE", "Cherokee (Cher)", 0x1,
161
+ "SUBLANG_CHEROKEE_CHEROKEE" },
162
+ { "ca_ES", 0x403, "Catalan (ca)", 0x3, "LANG_CATALAN", "Spain (ES)", 0x1,
163
+ "SUBLANG_CATALAN_CATALAN" },
164
+ { "zh_HK", 0xc04, "Chinese (zh)", 0x04, "LANG_CHINESE", "Hong Kong SAR, PRC (HK)", 0x3,
165
+ "SUBLANG_CHINESE_HONGKONG" },
166
+ { "zh_MO", 0x1404, "Chinese (zh)", 0x04, "LANG_CHINESE", "Macao SAR (MO)", 0x5,
167
+ "SUBLANG_CHINESE_MACAU" },
168
+ { "zh_SG", 0x1004, "Chinese (zh)", 0x04, "LANG_CHINESE", "Singapore (SG)", 0x4,
169
+ "SUBLANG_CHINESE_SINGAPORE" },
170
+ { "zh_CN", 0x4, "Chinese (zh)", 0x4, "LANG_CHINESE_SIMPLIFIED", "Simplified (Hans)", 0x2,
171
+ "SUBLANG_CHINESE_SIMPLIFIED" },
172
+ { "zh_CN", 0x7c04, "Chinese (zh)", 0x04, "LANG_CHINESE_TRADITIONAL", "Traditional (Hant)", 0x1,
173
+ "SUBLANG_CHINESE_TRADITIONAL" },
174
+ { "co_FR", 0x483, "Corsican (co)", 0x83, "LANG_CORSICAN", "France (FR)", 0x1,
175
+ "SUBLANG_CORSICAN_FRANCE" },
176
+ { "hr", 0x1a, "Croatian (hr)", 0x1a, "LANG_CROATIAN", "Neutral", 0x00, "" },
177
+ { "hr_BA", 0x101a, "Croatian (hr)", 0x1a, "LANG_CROATIAN", "Bosnia and Herzegovina, Latin (BA)",
178
+ 0x4, "SUBLANG_CROATIAN_BOSNIA_HERZEGOVINA_LATIN" },
179
+ { "hr_HR", 0x41a, "Croatian (hr)", 0x1a, "LANG_CROATIAN", "Croatia (HR)", 0x1,
180
+ "SUBLANG_CROATIAN_CROATIA" },
181
+ { "cs_CZ", 0x405, "Czech (cs)", 0x5, "LANG_CZECH", "Czech Republic (CZ)", 0x1,
182
+ "SUBLANG_CZECH_CZECH_REPUBLIC" },
183
+ { "da_DK", 0x406, "Danish (da)", 0x6, "LANG_DANISH", "Denmark (DK)", 0x1,
184
+ "SUBLANG_DANISH_DENMARK" },
185
+ { "prs_AF", 0x48c, "Dari (prs)", 0x8c, "LANG_DARI", "Afghanistan (AF)", 0x1,
186
+ "SUBLANG_DARI_AFGHANISTAN" },
187
+ { "dv_MV", 0x465, "Divehi (dv)", 0x65, "LANG_DIVEHI", "Maldives (MV)", 0x1,
188
+ "SUBLANG_DIVEHI_MALDIVES" },
189
+ { "nl_BE", 0x813, "Dutch (nl)", 0x13, "LANG_DUTCH", "Belgium (BE)", 0x2,
190
+ "SUBLANG_DUTCH_BELGIAN" },
191
+ { "nl_NL", 0x413, "Dutch (nl)", 0x13, "LANG_DUTCH", "Netherlands (NL)", 0x1, "SUBLANG_DUTCH" },
192
+ { "en_AU", 0xc09, "English (en)", 0x9, "LANG_ENGLISH", "Australia (AU)", 0x3,
193
+ "SUBLANG_ENGLISH_AUS" },
194
+ { "en_BZ", 0x2809, "English (en)", 0x09, "LANG_ENGLISH", "Belize (BZ)", 0xa,
195
+ "SUBLANG_ENGLISH_BELIZE" },
196
+ { "en_CA", 0x1009, "English (en)", 0x09, "LANG_ENGLISH", "Canada (CA)", 0x4,
197
+ "SUBLANG_ENGLISH_CAN" },
198
+ { "en_CB", 0x2409, "English (en)", 0x09, "LANG_ENGLISH", "Caribbean (029)", 0x9,
199
+ "SUBLANG_ENGLISH_CARIBBEAN" },
200
+ { "en_IN", 0x4009, "English (en)", 0x09, "LANG_ENGLISH", "India (IN)", 0x10,
201
+ "SUBLANG_ENGLISH_INDIA" },
202
+ { "en_IE", 0x1809, "English (en)", 0x09, "LANG_ENGLISH", "Ireland (IE)", 0x6,
203
+ "SUBLANG_ENGLISH_EIRE" },
204
+ { "en_IE", 0x1809, "English (en)", 0x09, "LANG_ENGLISH", "Ireland (IE)", 0x6,
205
+ "SUBLANG_ENGLISH_IRELAND" },
206
+ { "en_JM", 0x2009, "English (en)", 0x09, "LANG_ENGLISH", "Jamaica (JM)", 0x8,
207
+ "SUBLANG_ENGLISH_JAMAICA" },
208
+ { "en_MY", 0x4409, "English (en)", 0x09, "LANG_ENGLISH", "Malaysia (MY)", 0x11,
209
+ "SUBLANG_ENGLISH_MALAYSIA" },
210
+ { "en_NZ", 0x1409, "English (en)", 0x09, "LANG_ENGLISH", "New Zealand (NZ)", 0x5,
211
+ "SUBLANG_ENGLISH_NZ" },
212
+ { "en_PH", 0x3409, "English (en)", 0x09, "LANG_ENGLISH", "Philippines (PH)", 0xd,
213
+ "SUBLANG_ENGLISH_PHILIPPINES" },
214
+ { "en_SG", 0x4809, "English (en)", 0x09, "LANG_ENGLISH", "Singapore (SG)", 0x12,
215
+ "SUBLANG_ENGLISH_SINGAPORE" },
216
+ { "en_ZA", 0x1c09, "English (en)", 0x09, "LANG_ENGLISH", "South Africa (ZA)", 0x7,
217
+ "SUBLANG_ENGLISH_SOUTH_AFRICA" },
218
+ { "en_TT", 0x2c09, "English (en)", 0x09, "LANG_ENGLISH", "Trinidad and Tobago (TT)", 0xb,
219
+ "SUBLANG_ENGLISH_TRINIDAD" },
220
+ { "en_GB", 0x809, "English (en)", 0x09, "LANG_ENGLISH", "United Kingdom (GB)", 0x2,
221
+ "SUBLANG_ENGLISH_UK" },
222
+ { "en_US", 0x409, "English (en)", 0x09, "LANG_ENGLISH", "United States (US)", 0x1,
223
+ "SUBLANG_ENGLISH_US" },
224
+ { "en_ZW", 0x3009, "English (en)", 0x09, "LANG_ENGLISH", "Zimbabwe (ZW)", 0xc,
225
+ "SUBLANG_ENGLISH_ZIMBABWE" },
226
+ { "et_EE", 0x425, "Estonian (et)", 0x25, "LANG_ESTONIAN", "Estonia (EE)", 0x1,
227
+ "SUBLANG_ESTONIAN_ESTONIA" },
228
+ { "fo_FO", 0x438, "Faroese (fo)", 0x38, "LANG_FAEROESE", "Faroe Islands (FO)", 0x1,
229
+ "SUBLANG_FAEROESE_FAROE_ISLANDS" },
230
+ { "fil_PH", 0x464, "Filipino (fil)", 0x64, "LANG_FILIPINO", "Philippines (PH)", 0x1,
231
+ "SUBLANG_FILIPINO_PHILIPPINES" },
232
+ { "fi_FI", 0x40b, "Finnish (fi)", 0xb, "LANG_FINNISH", "Finland (FI)", 0x1,
233
+ "SUBLANG_FINNISH_FINLAND" },
234
+ { "fr_BE", 0x80c, "French (fr)", 0xc, "LANG_FRENCH", "Belgium (BE)", 0x2,
235
+ "SUBLANG_FRENCH_BELGIAN" },
236
+ { "fr_CA", 0xc0c, "French (fr)", 0x0c, "LANG_FRENCH", "Canada (CA)", 0x3,
237
+ "SUBLANG_FRENCH_CANADIAN" },
238
+ { "fr_FR", 0x40c, "French (fr)", 0x0c, "LANG_FRENCH", "France (FR)", 0x1, "SUBLANG_FRENCH" },
239
+ { "fr_LU", 0x140c, "French (fr)", 0x0c, "LANG_FRENCH", "Luxembourg (LU)", 0x5,
240
+ "SUBLANG_FRENCH_LUXEMBOURG" },
241
+ { "fr_MC", 0x180c, "French (fr)", 0x0c, "LANG_FRENCH", "Monaco (MC)", 0x6,
242
+ "SUBLANG_FRENCH_MONACO" },
243
+ { "fr_CH", 0x100c, "French (fr)", 0x0c, "LANG_FRENCH", "Switzerland (CH)", 0x4,
244
+ "SUBLANG_FRENCH_SWISS" },
245
+ { "fy_NL", 0x462, "Frisian (fy)", 0x62, "LANG_FRISIAN", "Netherlands (NL)", 0x1,
246
+ "SUBLANG_FRISIAN_NETHERLANDS" },
247
+ { "gl_ES", 0x456, "Galician (gl)", 0x56, "LANG_GALICIAN", "Spain (ES)", 0x1,
248
+ "SUBLANG_GALICIAN_GALICIAN" },
249
+ { "ka_GE", 0x437, "Georgian (ka)", 0x37, "LANG_GEORGIAN", "Georgia (GE)", 0x1,
250
+ "SUBLANG_GEORGIAN_GEORGIA" },
251
+ { "de_AT", 0xc07, "German (de)", 0x7, "LANG_GERMAN", "Austria (AT)", 0x3,
252
+ "SUBLANG_GERMAN_AUSTRIAN" },
253
+ { "de_DE", 0x407, "German (de)", 0x07, "LANG_GERMAN", "Germany (DE)", 0x1, "SUBLANG_GERMAN" },
254
+ { "de_LI", 0x1407, "German (de)", 0x07, "LANG_GERMAN", "Liechtenstein (LI)", 0x5,
255
+ "SUBLANG_GERMAN_LIECHTENSTEIN" },
256
+ { "de_LU", 0x1007, "German (de)", 0x07, "LANG_GERMAN", "Luxembourg (LU)", 0x4,
257
+ "SUBLANG_GERMAN_LUXEMBOURG" },
258
+ { "de_CH", 0x807, "German (de)", 0x07, "LANG_GERMAN", "Switzerland (CH)", 0x2,
259
+ "SUBLANG_GERMAN_SWISS" },
260
+ { "el_GR", 0x408, "Greek (el)", 0x8, "LANG_GREEK", "Greece (GR)", 0x1, "SUBLANG_GREEK_GREECE" },
261
+ { "kl_GL", 0x46f, "Greenlandic (kl)", 0x6f, "LANG_GREENLANDIC", "Greenland (GL)", 0x1,
262
+ "SUBLANG_GREENLANDIC_GREENLAND" },
263
+ { "gu_IN", 0x447, "Gujarati (gu)", 0x47, "LANG_GUJARATI", "India (IN)", 0x1,
264
+ "SUBLANG_GUJARATI_INDIA" },
265
+ { "ha_NG", 0x468, "Hausa (ha)", 0x68, "LANG_HAUSA", "Nigeria (NG)", 0x1,
266
+ "SUBLANG_HAUSA_NIGERIA_LATIN" },
267
+ { "haw_US", 0x475, "Hawiian (haw)", 0x75, "LANG_HAWAIIAN", "United States (US)", 0x1,
268
+ "SUBLANG_HAWAIIAN_US" },
269
+ { "he_IL", 0x40d, "Hebrew (he)", 0xd, "LANG_HEBREW", "Israel (IL)", 0x1,
270
+ "SUBLANG_HEBREW_ISRAEL" },
271
+ { "hi_IN", 0x439, "Hindi (hi)", 0x39, "LANG_HINDI", "India (IN)", 0x1, "SUBLANG_HINDI_INDIA" },
272
+ { "hu_HU", 0x40e, "Hungarian (hu)", 0xe, "LANG_HUNGARIAN", "Hungary (HU)", 0x1,
273
+ "SUBLANG_HUNGARIAN_HUNGARY" },
274
+ { "is_IS", 0x40f, "Icelandic (is)", 0xf, "LANG_ICELANDIC", "Iceland (IS)", 0x1,
275
+ "SUBLANG_ICELANDIC_ICELAND" },
276
+ { "ig_NG", 0x470, "Igbo (ig)", 0x70, "LANG_IGBO", "Nigeria (NG)", 0x1, "SUBLANG_IGBO_NIGERIA" },
277
+ { "id_ID", 0x421, "Indonesian (id)", 0x21, "LANG_INDONESIAN", "Indonesia (ID)", 0x1,
278
+ "SUBLANG_INDONESIAN_INDONESIA" },
279
+ { "iu_CA", 0x85d, "Inuktitut (iu)", 0x5d, "LANG_INUKTITUT", "Canada (CA), Latin", 0x2,
280
+ "SUBLANG_INUKTITUT_CANADA_LATIN" },
281
+ { "iu_CA", 0x45d, "Inuktitut (iu)", 0x5d, "LANG_INUKTITUT", "Canada (CA), Canadian Syllabics",
282
+ 0x1, "SUBLANG_INUKTITUT_CANADA" },
283
+ { "ga_IE", 0x83c, "Irish (ga)", 0x3c, "LANG_IRISH", "Ireland (IE)", 0x2,
284
+ "SUBLANG_IRISH_IRELAND" },
285
+ { "xh_ZA", 0x434, "isiXhosa (xh)", 0x34, "LANG_XHOSA", "South Africa (ZA)", 0x1,
286
+ "SUBLANG_XHOSA_SOUTH_AFRICA" },
287
+ { "zu_ZA", 0x435, "isiZulu (zu)", 0x35, "LANG_ZULU", "South Africa (ZA)", 0x1,
288
+ "SUBLANG_ZULU_SOUTH_AFRICA" },
289
+ { "it_IT", 0x410, "Italian (it)", 0x10, "LANG_ITALIAN", "Italy (IT)", 0x1, "SUBLANG_ITALIAN" },
290
+ { "it_CH", 0x810, "Italian (it)", 0x10, "LANG_ITALIAN", "Switzerland (CH)", 0x2,
291
+ "SUBLANG_ITALIAN_SWISS" },
292
+ { "ja_JP", 0x411, "Japanese (ja)", 0x11, "LANG_JAPANESE", "Japan (JP)", 0x1,
293
+ "SUBLANG_JAPANESE_JAPAN" },
294
+ { "kn_IN", 0x44b, "Kannada (kn)", 0x4b, "LANG_KANNADA", "India (IN)", 0x1,
295
+ "SUBLANG_KANNADA_INDIA" },
296
+ { "kk_KZ", 0x43f, "Kazakh (kk)", 0x3f, "LANG_KAZAK", "Kazakhstan (KZ)", 0x1,
297
+ "SUBLANG_KAZAK_KAZAKHSTAN" },
298
+ { "kh_KH", 0x453, "Khmer (kh)", 0x53, "LANG_KHMER", "Cambodia (KH)", 0x1,
299
+ "SUBLANG_KHMER_CAMBODIA" },
300
+ { "qut_GT", 0x486, "K'iche (qut)", 0x86, "LANG_KICHE", "Guatemala (GT)", 0x1,
301
+ "SUBLANG_KICHE_GUATEMALA" },
302
+ { "rw_RW", 0x487, "Kinyarwanda (rw)", 0x87, "LANG_KINYARWANDA", "Rwanda (RW)", 0x1,
303
+ "SUBLANG_KINYARWANDA_RWANDA" },
304
+ { "kok_IN", 0x457, "Konkani (kok)", 0x57, "LANG_KONKANI", "India (IN)", 0x1,
305
+ "SUBLANG_KONKANI_INDIA" },
306
+ { "ko_KR", 0x412, "Korean (ko)", 0x12, "LANG_KOREAN", "Korea (KR)", 0x1, "SUBLANG_KOREAN" },
307
+ { "ky_KG", 0x440, "Kyrgyz (ky)", 0x40, "LANG_KYRGYZ", "Kyrgyzstan (KG)", 0x1,
308
+ "SUBLANG_KYRGYZ_KYRGYZSTAN" },
309
+ { "lo_LA", 0x454, "Lao (lo)", 0x54, "LANG_LAO", "Lao PDR (LA)", 0x1, "SUBLANG_LAO_LAO" },
310
+ { "lv_LV", 0x426, "Latvian (lv)", 0x26, "LANG_LATVIAN", "Latvia (LV)", 0x1,
311
+ "SUBLANG_LATVIAN_LATVIA" },
312
+ { "lt_LT", 0x427, "Lithuanian (lt)", 0x27, "LANG_LITHUANIAN", "Lithuanian (LT)", 0x1,
313
+ "SUBLANG_LITHUANIAN_LITHUANIA" },
314
+ { "dsb_DE", 0x82e, "Lower Sorbian (dsb)", 0x2e, "LANG_LOWER_SORBIAN", "Germany (DE)", 0x2,
315
+ "SUBLANG_LOWER_SORBIAN_GERMANY" },
316
+ { "lb_LU", 0x46e, "Luxembourgish (lb)", 0x6e, "LANG_LUXEMBOURGISH", "Luxembourg (LU)", 0x1,
317
+ "SUBLANG_LUXEMBOURGISH_LUXEMBOURG" },
318
+ { "mk_MK", 0x42f, "Macedonian (mk)", 0x2f, "LANG_MACEDONIAN", "Macedonia (FYROM) (MK)", 0x1,
319
+ "SUBLANG_MACEDONIAN_MACEDONIA" },
320
+ { "ms_BN", 0x83e, "Malay (ms)", 0x3e, "LANG_MALAY", "Brunei Darassalam (BN)", 0x2,
321
+ "SUBLANG_MALAY_BRUNEI_DARUSSALAM" },
322
+ { "ms_MY", 0x43e, "Malay (ms)", 0x3e, "LANG_MALAY", "Malaysia (MY)", 0x1,
323
+ "SUBLANG_MALAY_MALAYSIA" },
324
+ { "ml_IN", 0x44c, "Malayalam (ml)", 0x4c, "LANG_MALAYALAM", "India (IN)", 0x1,
325
+ "SUBLANG_MALAYALAM_INDIA" },
326
+ { "mt_MT", 0x43a, "Maltese (mt)", 0x3a, "LANG_MALTESE", "Malta (MT)", 0x1,
327
+ "SUBLANG_MALTESE_MALTA" },
328
+ { "mi_NZ", 0x481, "Maori (mi)", 0x81, "LANG_MAORI", "New Zealand (NZ)", 0x1,
329
+ "SUBLANG_MAORI_NEW_ZEALAND" },
330
+ { "arn_CL", 0x47a, "Mapudungun (arn)", 0x7a, "LANG_MAPUDUNGUN", "Chile (CL)", 0x1,
331
+ "SUBLANG_MAPUDUNGUN_CHILE" },
332
+ { "mr_IN", 0x44e, "Marathi (mr)", 0x4e, "LANG_MARATHI", "India (IN)", 0x1,
333
+ "SUBLANG_MARATHI_INDIA" },
334
+ { "moh_CA", 0x47c, "Mohawk (moh)", 0x7c, "LANG_MOHAWK", "Canada (CA)", 0x1,
335
+ "SUBLANG_MOHAWK_MOHAWK" },
336
+ { "mn_MN", 0x450, "Mongolian (mn)", 0x50, "LANG_MONGOLIAN", "Mongolia, Cyrillic (MN)", 0x1,
337
+ "SUBLANG_MONGOLIAN_CYRILLIC_MONGOLIA" },
338
+ { "mn_MN", 0x850, "Mongolian (mn)", 0x50, "LANG_MONGOLIAN", "Mongolia, Mong (MN)", 0x2,
339
+ "SUBLANG_MONGOLIAN_PRC" },
340
+ { "ne_NP", 0x461, "Nepali (ne)", 0x61, "LANG_NEPALI", "Nepal (NP)", 0x1,
341
+ "SUBLANG_NEPALI_NEPAL" },
342
+ { "ne_IN", 0x861, "Nepali (ne)", 0x61, "LANG_NEPALI", "India (IN)", 0x2,
343
+ "SUBLANG_NEPALI_INDIA" },
344
+ { "no_NO", 0x414, "Norwegian (no)", 0x14, "LANG_NORWEGIAN", "Bokmål, Norway (NO)", 0x1,
345
+ "SUBLANG_NORWEGIAN_BOKMAL" },
346
+ { "no_NO", 0x814, "Norwegian (no)", 0x14, "LANG_NORWEGIAN", "Nynorsk, Norway (NO)", 0x2,
347
+ "SUBLANG_NORWEGIAN_NYNORSK" },
348
+ { "oc_FR", 0x482, "Occitan (oc)", 0x82, "LANG_OCCITAN", "France (FR)", 0x1,
349
+ "SUBLANG_OCCITAN_FRANCE" },
350
+ { "or_IN", 0x448, "Odia (or)", 0x48, "LANG_ORIYA", "India (IN)", 0x1, "SUBLANG_ORIYA_INDIA" },
351
+ { "ps_AF", 0x463, "Pashto (ps)", 0x63, "LANG_PASHTO", "Afghanistan (AF)", 0x1,
352
+ "SUBLANG_PASHTO_AFGHANISTAN" },
353
+ { "fa_IR", 0x429, "Persian (fa)", 0x29, "LANG_PERSIAN", "Iran (IR)", 0x1,
354
+ "SUBLANG_PERSIAN_IRAN" },
355
+ { "pl_PL", 0x415, "Polish (pl)", 0x15, "LANG_POLISH", "Poland (PL)", 0x1,
356
+ "SUBLANG_POLISH_POLAND" },
357
+ { "pt_BR", 0x416, "Portuguese (pt)", 0x16, "LANG_PORTUGUESE", "Brazil (BR)", 0x1,
358
+ "SUBLANG_PORTUGUESE_BRAZILIAN" },
359
+ { "pt_PT", 0x816, "Portuguese (pt)", 0x16, "LANG_PORTUGUESE", "Portugal (PT)", 0x2,
360
+ "SUBLANG_PORTUGUESE" },
361
+ { "ff_SN", 0x867, "Pular (ff)", 0x67, "LANG_PULAR", "Senegal (SN)", 0x2,
362
+ "SUBLANG_PULAR_SENEGAL" },
363
+ { "pa_IN", 0x446, "Punjabi (pa)", 0x46, "LANG_PUNJABI", "India, Gurmukhi script (IN)", 0x1,
364
+ "SUBLANG_PUNJABI_INDIA" },
365
+ { "pa_PK", 0x846, "Punjabi (pa)", 0x46, "LANG_PUNJABI", "Pakistan, Arabic script(PK)", 0x2,
366
+ "SUBLANG_PUNJABI_PAKISTAN" },
367
+ { "quz_BO", 0x46b, "Quechua (quz)", 0x6b, "LANG_QUECHUA", "Bolivia (BO)", 0x1,
368
+ "SUBLANG_QUECHUA_BOLIVIA" },
369
+ { "quz_EC", 0x86b, "Quechua (quz)", 0x6b, "LANG_QUECHUA", "Ecuador (EC)", 0x2,
370
+ "SUBLANG_QUECHUA_ECUADOR" },
371
+ { "quz_PE", 0xc6b, "Quechua (quz)", 0x6b, "LANG_QUECHUA", "Peru (PE)", 0x3,
372
+ "SUBLANG_QUECHUA_PERU" },
373
+ { "ro_RO", 0x418, "Romanian (ro)", 0x18, "LANG_ROMANIAN", "Romania (RO)", 0x1,
374
+ "SUBLANG_ROMANIAN_ROMANIA" },
375
+ { "rm_CH", 0x417, "Romansh (rm)", 0x17, "LANG_ROMANSH", "Switzerland (CH)", 0x1,
376
+ "SUBLANG_ROMANSH_SWITZERLAND" },
377
+ { "ru_RU", 0x419, "Russian (ru)", 0x19, "LANG_RUSSIAN", "Russia (RU)", 0x1,
378
+ "SUBLANG_RUSSIAN_RUSSIA" },
379
+ { "sah_RU", 0x485, "Sakha (sah)", 0x85, "LANG_SAKHA", "Russia (RU)", 0x1,
380
+ "SUBLANG_SAKHA_RUSSIA" },
381
+ { "smn_FI", 0x243b, "Sami (smn)", 0x3b, "LANG_SAMI", "Inari, Finland (FI)", 0x9,
382
+ "SUBLANG_SAMI_INARI_FINLAND" },
383
+ { "smj_NO", 0x103b, "Sami (smj)", 0x3b, "LANG_SAMI", "Lule, Norway (NO)", 0x4,
384
+ "SUBLANG_SAMI_LULE_NORWAY" },
385
+ { "smj_SE", 0x143b, "Sami (smj)", 0x3b, "LANG_SAMI", "Lule, Sweden (SE)", 0x5,
386
+ "SUBLANG_SAMI_LULE_SWEDEN" },
387
+ { "se_FI", 0xc3b, "Sami (se)", 0x3b, "LANG_SAMI", "Northern, Finland (FI)", 0x3,
388
+ "SUBLANG_SAMI_NORTHERN_FINLAND" },
389
+ { "se_NO", 0x43b, "Sami (se)", 0x3b, "LANG_SAMI", "Northern, Norway (NO)", 0x1,
390
+ "SUBLANG_SAMI_NORTHERN_NORWAY" },
391
+ { "se_SE", 0x83b, "Sami (se)", 0x3b, "LANG_SAMI", "Northern, Sweden (SE)", 0x2,
392
+ "SUBLANG_SAMI_NORTHERN_SWEDEN" },
393
+ { "sms_FI", 0x203b, "Sami (sms)", 0x3b, "LANG_SAMI", "Skolt, Finland (FI)", 0x8,
394
+ "SUBLANG_SAMI_SKOLT_FINLAND" },
395
+ { "sma_NO", 0x183b, "Sami (sma)", 0x3b, "LANG_SAMI", "Southern, Norway (NO)", 0x6,
396
+ "SUBLANG_SAMI_SOUTHERN_NORWAY" },
397
+ { "sma_SE", 0x1c3b, "Sami (sma)", 0x3b, "LANG_SAMI", "Southern, Sweden (SE)", 0x7,
398
+ "SUBLANG_SAMI_SOUTHERN_SWEDEN" },
399
+ { "sa_IN", 0x44f, "Sanskrit (sa)", 0x4f, "LANG_SANSKRIT", "India (IN)", 0x1,
400
+ "SUBLANG_SANSKRIT_INDIA" },
401
+ { "sr", 0x7c1a, "Serbian (sr)", 0x1a, "LANG_SERBIAN_NEUTRAL", "Neutral", 0x00, "" },
402
+ { "sr_BA", 0x1c1a, "Serbian (sr)", 0x1a, "LANG_SERBIAN",
403
+ "Bosnia and Herzegovina, Cyrillic (BA)", 0x7, "SUBLANG_SERBIAN_BOSNIA_HERZEGOVINA_CYRILLIC" },
404
+ { "sr_BA", 0x181a, "Serbian (sr)", 0x1a, "LANG_SERBIAN", "Bosnia and Herzegovina, Latin (BA)",
405
+ 0x6, "SUBLANG_SERBIAN_BOSNIA_HERZEGOVINA_LATIN" },
406
+ { "sr_HR", 0x41a, "Serbian (sr)", 0x1a, "LANG_SERBIAN", "Croatia (HR)", 0x1,
407
+ "SUBLANG_SERBIAN_CROATIA" },
408
+ { "sr_CS", 0xc1a, "Serbian (sr)", 0x1a, "LANG_SERBIAN",
409
+ "Serbia and Montenegro (former), Cyrillic (CS)", 0x3, "SUBLANG_SERBIAN_CYRILLIC" },
410
+ { "sr_CS", 0x81a, "Serbian (sr)", 0x1a, "LANG_SERBIAN",
411
+ "Serbia and Montenegro (former), Latin (CS)", 0x2, "SUBLANG_SERBIAN_LATIN" },
412
+ { "nso_ZA", 0x46c, "Sesotho sa Leboa (nso)", 0x6c, "LANG_SOTHO", "South Africa (ZA)", 0x1,
413
+ "SUBLANG_SOTHO_NORTHERN_SOUTH_AFRICA" },
414
+ { "tn_BW", 0x832, "Setswana / Tswana (tn)", 0x32, "LANG_TSWANA", "Botswana (BW)", 0x2,
415
+ "SUBLANG_TSWANA_BOTSWANA" },
416
+ { "tn_ZA", 0x432, "Setswana / Tswana (tn)", 0x32, "LANG_TSWANA", "South Africa (ZA)", 0x1,
417
+ "SUBLANG_TSWANA_SOUTH_AFRICA" },
418
+ { "", 0x859, "(reserved)", 0x59, "LANG_SINDHI", "(reserved)", 0x2,
419
+ "SUBLANG_SINDHI_AFGHANISTAN" },
420
+ { "", 0x459, "(reserved)", 0x59, "LANG_SINDHI", "(reserved)", 0x1, "SUBLANG_SINDHI_INDIA" },
421
+ { "sd_PK", 0x859, "Sindhi (sd)", 0x59, "LANG_SINDHI", "Pakistan (PK)", 0x2,
422
+ "SUBLANG_SINDHI_PAKISTAN" },
423
+ { "si_LK", 0x45b, "Sinhala (si)", 0x5b, "LANG_SINHALESE", "Sri Lanka (LK)", 0x1,
424
+ "SUBLANG_SINHALESE_SRI_LANKA" },
425
+ { "sk_SK", 0x41b, "Slovak (sk)", 0x1b, "LANG_SLOVAK", "Slovakia (SK)", 0x1,
426
+ "SUBLANG_SLOVAK_SLOVAKIA" },
427
+ { "sl_SI", 0x424, "Slovenian (sl)", 0x24, "LANG_SLOVENIAN", "Slovenia (SI)", 0x1,
428
+ "SUBLANG_SLOVENIAN_SLOVENIA" },
429
+ { "es_AR", 0x2c0a, "Spanish (es)", 0xa, "LANG_SPANISH", "Argentina (AR)", 0xb,
430
+ "SUBLANG_SPANISH_ARGENTINA" },
431
+ { "es_BO", 0x400a, "Spanish (es)", 0x0a, "LANG_SPANISH", "Bolivia (BO)", 0x10,
432
+ "SUBLANG_SPANISH_BOLIVIA" },
433
+ { "es_CL", 0x340a, "Spanish (es)", 0x0a, "LANG_SPANISH", "Chile (CL)", 0xd,
434
+ "SUBLANG_SPANISH_CHILE" },
435
+ { "es_CO", 0x240a, "Spanish (es)", 0x0a, "LANG_SPANISH", "Colombia (CO)", 0x9,
436
+ "SUBLANG_SPANISH_COLOMBIA" },
437
+ { "es_CR", 0x140a, "Spanish (es)", 0x0a, "LANG_SPANISH", "Costa Rica (CR)", 0x5,
438
+ "SUBLANG_SPANISH_COSTA_RICA" },
439
+ { "es_DO", 0x1c0a, "Spanish (es)", 0x0a, "LANG_SPANISH", "Dominican Republic (DO)", 0x7,
440
+ "SUBLANG_SPANISH_DOMINICAN_REPUBLIC" },
441
+ { "es_EC", 0x300a, "Spanish (es)", 0x0a, "LANG_SPANISH", "Ecuador (EC)", 0xc,
442
+ "SUBLANG_SPANISH_ECUADOR" },
443
+ { "es_SV", 0x440a, "Spanish (es)", 0x0a, "LANG_SPANISH", "El Salvador (SV)", 0x11,
444
+ "SUBLANG_SPANISH_EL_SALVADOR" },
445
+ { "es_GT", 0x100a, "Spanish (es)", 0x0a, "LANG_SPANISH", "Guatemala (GT)", 0x4,
446
+ "SUBLANG_SPANISH_GUATEMALA" },
447
+ { "es_HN", 0x480a, "Spanish (es)", 0x0a, "LANG_SPANISH", "Honduras (HN)", 0x12,
448
+ "SUBLANG_SPANISH_HONDURAS" },
449
+ { "es_MX", 0x80a, "Spanish (es)", 0x0a, "LANG_SPANISH", "Mexico (MX)", 0x2,
450
+ "SUBLANG_SPANISH_MEXICAN" },
451
+ { "es_NI", 0x4c0a, "Spanish (es)", 0x0a, "LANG_SPANISH", "Nicaragua (NI)", 0x13,
452
+ "SUBLANG_SPANISH_NICARAGUA" },
453
+ { "es_PA", 0x180a, "Spanish (es)", 0x0a, "LANG_SPANISH", "Panama (PA)", 0x6,
454
+ "SUBLANG_SPANISH_PANAMA" },
455
+ { "es_PY", 0x3c0a, "Spanish (es)", 0x0a, "LANG_SPANISH", "Paraguay (PY)", 0xf,
456
+ "SUBLANG_SPANISH_PARAGUAY" },
457
+ { "es_PE", 0x280a, "Spanish (es)", 0x0a, "LANG_SPANISH", "Peru (PE)", 0xa,
458
+ "SUBLANG_SPANISH_PERU" },
459
+ { "es_PR", 0x500a, "Spanish (es)", 0x0a, "LANG_SPANISH", "Puerto Rico (PR)", 0x14,
460
+ "SUBLANG_SPANISH_PUERTO_RICO" },
461
+ { "es_ES", 0xc0a, "Spanish (es)", 0x0a, "LANG_SPANISH", "Spain, Modern Sort (ES)", 0x3,
462
+ "SUBLANG_SPANISH_MODERN" },
463
+ { "es_ES", 0x40a, "Spanish (es)", 0x0a, "LANG_SPANISH", "Spain, Traditional Sort (ES)", 0x1,
464
+ "SUBLANG_SPANISH" },
465
+ { "es_US", 0x540a, "Spanish (es)", 0x0a, "LANG_SPANISH", "United States (US)", 0x15,
466
+ "SUBLANG_SPANISH_US" },
467
+ { "es_UY", 0x380a, "Spanish (es)", 0x0a, "LANG_SPANISH", "Uruguay (UY)", 0xe,
468
+ "SUBLANG_SPANISH_URUGUAY" },
469
+ { "es_VE", 0x200a, "Spanish (es)", 0x0a, "LANG_SPANISH", "Venezuela (VE)", 0x8,
470
+ "SUBLANG_SPANISH_VENEZUELA" },
471
+ { "sw_KE", 0x441, "Swahili (sw)", 0x41, "LANG_SWAHILI", "Kenya (KE)", 0x1, "SUBLANG_SWAHILI" },
472
+ { "sv_FI", 0x81d, "Swedish (sv)", 0x1d, "LANG_SWEDISH", "Finland (FI)", 0x2,
473
+ "SUBLANG_SWEDISH_FINLAND" },
474
+ { "sv_SE", 0x41d, "Swedish (sv)", 0x1d, "LANG_SWEDISH", "Sweden (SE);", 0x1,
475
+ "SUBLANG_SWEDISH" },
476
+ { "sv_SE", 0x41d, "Swedish (sv)", 0x1d, "LANG_SWEDISH", "Sweden (SE);", 0x1,
477
+ "SUBLANG_SWEDISH_SWEDEN" },
478
+ { "syr_SY", 0x45a, "Syriac (syr)", 0x5a, "LANG_SYRIAC", "Syria (SY)", 0x1, "SUBLANG_SYRIAC" },
479
+ { "tg_TJ", 0x428, "Tajik (tg)", 0x28, "LANG_TAJIK", "Tajikistan, Cyrillic (TJ)", 0x1,
480
+ "SUBLANG_TAJIK_TAJIKISTAN" },
481
+ { "tzm_DZ", 0x85f, "Tamazight (tzm)", 0x5f, "LANG_TAMAZIGHT", "Algeria, Latin (DZ)", 0x2,
482
+ "SUBLANG_TAMAZIGHT_ALGERIA_LATIN" },
483
+ { "ta_IN", 0x449, "Tamil (ta)", 0x49, "LANG_TAMIL", "India (IN)", 0x1, "SUBLANG_TAMIL_INDIA" },
484
+ { "ta_LK", 0x849, "Tamil (ta)", 0x49, "LANG_TAMIL", "Sri Lanka (LK)", 0x2,
485
+ "SUBLANG_TAMIL_SRI_LANKA" },
486
+ { "tt_RU", 0x444, "Tatar (tt)", 0x44, "LANG_TATAR", "Russia (RU)", 0x1,
487
+ "SUBLANG_TATAR_RUSSIA" },
488
+ { "te_IN", 0x44a, "Telugu (te)", 0x4a, "LANG_TELUGU", "India (IN)", 0x1,
489
+ "SUBLANG_TELUGU_INDIA" },
490
+ { "th_TH", 0x41e, "Thai (th)", 0x1e, "LANG_THAI", "Thailand (TH)", 0x1,
491
+ "SUBLANG_THAI_THAILAND" },
492
+ { "bo_CN", 0x451, "Tibetan (bo)", 0x51, "LANG_TIBETAN", "PRC (CN)", 0x1,
493
+ "SUBLANG_TIBETAN_PRC" },
494
+ { "ti_ER", 0x873, "Tigrinya (ti)", 0x73, "LANG_TIGRINYA", "Eritrea (ER)", 0x2,
495
+ "SUBLANG_TIGRINYA_ERITREA" },
496
+ { "ti_ET", 0x473, "Tigrinya (ti)", 0x73, "LANG_TIGRIGNA", "Ethiopia (ET)", 0x1,
497
+ "SUBLANG_TIGRINYA_ETHIOPIA" },
498
+ { "tr_TR", 0x41f, "Turkish (tr)", 0x1f, "LANG_TURKISH", "Turkey (TR)", 0x1,
499
+ "SUBLANG_TURKISH_TURKEY" },
500
+ { "tk_KM", 0x442, "Turkmen (tk)", 0x42, "LANG_TURKMEN", "Turkmenistan (TM)", 0x1,
501
+ "SUBLANG_TURKMEN_TURKMENISTAN" },
502
+ { "uk_UA", 0x422, "Ukrainian (uk)", 0x22, "LANG_UKRAINIAN", "Ukraine (UA)", 0x1,
503
+ "SUBLANG_UKRAINIAN_UKRAINE" },
504
+ { "hsb_DE", 0x42e, "Upper Sorbian (hsb)", 0x2e, "LANG_UPPER_SORBIAN", "Germany (DE)", 0x1,
505
+ "SUBLANG_UPPER_SORBIAN_GERMANY" },
506
+ { "ur", 0x820, "Urdu (ur)", 0x20, "LANG_URDU", "(reserved)", 0x2, "SUBLANG_URDU_INDIA" },
507
+ { "ur_PK", 0x420, "Urdu (ur)", 0x20, "LANG_URDU", "Pakistan (PK)", 0x1,
508
+ "SUBLANG_URDU_PAKISTAN" },
509
+ { "ug_CN", 0x480, "Uyghur (ug)", 0x80, "LANG_UIGHUR", "PRC (CN)", 0x1, "SUBLANG_UIGHUR_PRC" },
510
+ { "uz_UZ", 0x843, "Uzbek (uz)", 0x43, "LANG_UZBEK", "Uzbekistan, Cyrillic (UZ)", 0x2,
511
+ "SUBLANG_UZBEK_CYRILLIC" },
512
+ { "uz_UZ", 0x443, "Uzbek (uz)", 0x43, "LANG_UZBEK", "Uzbekistan, Latin (UZ)", 0x1,
513
+ "SUBLANG_UZBEK_LATIN" },
514
+ { "ca", 0x803, "Valencian (ca)", 0x3, "LANG_VALENCIAN", "Valencia (ES-Valencia)", 0x2,
515
+ "SUBLANG_VALENCIAN_VALENCIA" },
516
+ { "vi_VN", 0x42a, "Vietnamese (vi)", 0x2a, "LANG_VIETNAMESE", "Vietnam (VN)", 0x1,
517
+ "SUBLANG_VIETNAMESE_VIETNAM" },
518
+ { "cy_GB", 0x452, "Welsh (cy)", 0x52, "LANG_WELSH", "United Kingdom (GB)", 0x1,
519
+ "SUBLANG_WELSH_UNITED_KINGDOM" },
520
+ { "wo_SN", 0x488, "Wolof (wo)", 0x88, "LANG_WOLOF", "Senegal (SN)", 0x1,
521
+ "SUBLANG_WOLOF_SENEGAL" },
522
+ { "ii_CN", 0x478, "Yi (ii)", 0x78, "LANG_YI", "PRC (CN)", 0x1, "SUBLANG_YI_PRC" },
523
+ { "yu_NG", 0x46a, "Yoruba (yo)", 0x6a, "LANG_YORUBA", "Nigeria (NG)", 0x1,
524
+ "SUBLANG_YORUBA_NIGERIA" },
525
+ };
526
+
527
+ static BOOL reallocate_keyboard_layouts(RDP_KEYBOARD_LAYOUT** layouts, size_t* pcount,
528
+ size_t to_add)
529
+ {
530
+ WINPR_ASSERT(layouts);
531
+ WINPR_ASSERT(pcount);
532
+
533
+ RDP_KEYBOARD_LAYOUT* copy = (RDP_KEYBOARD_LAYOUT*)realloc(
534
+ *layouts, (*pcount + to_add + 1) * sizeof(RDP_KEYBOARD_LAYOUT));
535
+
536
+ if (!copy)
537
+ return FALSE;
538
+
539
+ memset(&copy[*pcount], 0, (to_add + 1) * sizeof(RDP_KEYBOARD_LAYOUT));
540
+ *layouts = copy;
541
+ *pcount += to_add;
542
+ return TRUE;
543
+ }
544
+
545
+ #if !defined(WITH_KEYBOARD_LAYOUT_FROM_FILE)
546
+ /*
547
+ * In Windows XP, this information is available in the system registry at
548
+ * HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet001/Control/Keyboard Layouts/
549
+ * https://docs.microsoft.com/en-us/windows-hardware/manufacture/desktop/windows-language-pack-default-values
550
+ */
551
+ static const RDP_KEYBOARD_LAYOUT sRDP_KEYBOARD_LAYOUT_TABLE[] = {
552
+ { 0x0000041c, "Albanian" },
553
+ { 0x00000401, "Arabic (101)" },
554
+ { 0x00010401, "Arabic (102)" },
555
+ { 0x00020401, "Arabic (102) AZERTY" },
556
+ { 0x0000042b, "Armenian Eastern" },
557
+ { 0x0002042b, "Armenian Phonetic" },
558
+ { 0x0003042b, "Armenian Typewriter" },
559
+ { 0x0001042b, "Armenian Western" },
560
+ { 0x0000044d, "Assamese - Inscript" },
561
+ { 0x0001042c, "Azerbaijani (Standard)" },
562
+ { 0x0000082c, "Azerbaijani Cyrillic" },
563
+ { 0x0000042c, "Azerbaijani Latin" },
564
+ { 0x0000046d, "Bashkir" },
565
+ { 0x00000423, "Belarusian" },
566
+ { 0x0001080c, "Belgian (Comma)" },
567
+ { 0x00000813, "Belgian (Period)" },
568
+ { 0x0000080c, "Belgian French" },
569
+ { 0x00000445, "Bangla (Bangladesh)" },
570
+ { 0x00020445, "Bangla (India)" },
571
+ { 0x00010445, "Bangla (India - Legacy)" },
572
+ { 0x0000201a, "Bosnian (Cyrillic)" },
573
+ { 0x000b0c00, "Buginese" },
574
+ { 0x00030402, "Bulgarian" },
575
+ { 0x00010402, "Bulgarian (Latin)" },
576
+ { 0x00020402, "Bulgarian (phonetic layout)" },
577
+ { 0x00040402, "Bulgarian (phonetic traditional)" },
578
+ { 0x00000402, "Bulgarian (Typewriter)" },
579
+ { 0x00001009, "Canadian French" },
580
+ { 0x00000c0c, "Canadian French (Legacy)" },
581
+ { 0x00011009, "Canadian Multilingual Standard" },
582
+ { 0x0000085f, "Central Atlas Tamazight" },
583
+ { 0x00000429, "Central Kurdish" },
584
+ { 0x0000045c, "Cherokee Nation" },
585
+ { 0x0001045c, "Cherokee Nation Phonetic" },
586
+ { 0x00000804, "Chinese (Simplified) - US Keyboard" },
587
+ { 0x00000404, "Chinese (Traditional) - US Keyboard" },
588
+ { 0x00000c04, "Chinese (Traditional, Hong Kong S.A.R.)" },
589
+ { 0x00001404, "Chinese (Traditional Macao S.A.R.) US Keyboard" },
590
+ { 0x00001004, "Chinese (Simplified, Singapore) - US keyboard" },
591
+ { 0x0000041a, "Croatian" },
592
+ { 0x00000405, "Czech" },
593
+ { 0x00010405, "Czech (QWERTY)" },
594
+ { 0x00020405, "Czech Programmers" },
595
+ { 0x00000406, "Danish" },
596
+ { 0x00000439, "Devanagari-INSCRIPT" },
597
+ { 0x00000465, "Divehi Phonetic" },
598
+ { 0x00010465, "Divehi Typewriter" },
599
+ { 0x00000413, "Dutch" },
600
+ { 0x00000c51, "Dzongkha" },
601
+ { 0x00000425, "Estonian" },
602
+ { 0x00000438, "Faeroese" },
603
+ { 0x0000040b, "Finnish" },
604
+ { 0x0001083b, "Finnish with Sami" },
605
+ { 0x0000040c, "French" },
606
+ { 0x00120c00, "Futhark" },
607
+ { 0x00000437, "Georgian" },
608
+ { 0x00020437, "Georgian (Ergonomic)" },
609
+ { 0x00010437, "Georgian (QWERTY)" },
610
+ { 0x00030437, "Georgian Ministry of Education and Science Schools" },
611
+ { 0x00040437, "Georgian (Old Alphabets)" },
612
+ { 0x00000407, "German" },
613
+ { 0x00010407, "German (IBM)" },
614
+ { 0x000c0c00, "Gothic" },
615
+ { 0x00000408, "Greek" },
616
+ { 0x00010408, "Greek (220)" },
617
+ { 0x00030408, "Greek (220) Latin" },
618
+ { 0x00020408, "Greek (319)" },
619
+ { 0x00040408, "Greek (319) Latin" },
620
+ { 0x00050408, "Greek Latin" },
621
+ { 0x00060408, "Greek Polytonic" },
622
+ { 0x0000046f, "Greenlandic" },
623
+ { 0x00000474, "Guarani" },
624
+ { 0x00000447, "Gujarati" },
625
+ { 0x00000468, "Hausa" },
626
+ { 0x0000040d, "Hebrew" },
627
+ { 0x00010439, "Hindi Traditional" },
628
+ { 0x0000040e, "Hungarian" },
629
+ { 0x0001040e, "Hungarian 101-key" },
630
+ { 0x0000040f, "Icelandic" },
631
+ { 0x00000470, "Igbo" },
632
+ { 0x00004009, "India" },
633
+ { 0x0000085d, "Inuktitut - Latin" },
634
+ { 0x0001045d, "Inuktitut - Naqittaut" },
635
+ { 0x00001809, "Irish" },
636
+ { 0x00000410, "Italian" },
637
+ { 0x00010410, "Italian (142)" },
638
+ { 0x00000411, "Japanese" },
639
+ { 0x00110c00, "Javanese" },
640
+ { 0x0000044b, "Kannada" },
641
+ { 0x0000043f, "Kazakh" },
642
+ { 0x00000453, "Khmer" },
643
+ { 0x00010453, "Khmer (NIDA)" },
644
+ { 0x00000412, "Korean" },
645
+ { 0x00000440, "Kyrgyz Cyrillic" },
646
+ { 0x00000454, "Lao" },
647
+ { 0x0000080a, "Latin American" },
648
+ { 0x00020426, "Latvian (Standard)" },
649
+ { 0x00010426, "Latvian (Legacy)" },
650
+ { 0x00070c00, "Lisu (Basic)" },
651
+ { 0x00080c00, "Lisu (Standard)" },
652
+ { 0x00010427, "Lithuanian" },
653
+ { 0x00000427, "Lithuanian IBM" },
654
+ { 0x00020427, "Lithuanian Standard" },
655
+ { 0x0000046e, "Luxembourgish" },
656
+ { 0x0000042f, "Macedonia (FYROM)" },
657
+ { 0x0001042f, "Macedonia (FYROM) - Standard" },
658
+ { 0x0000044c, "Malayalam" },
659
+ { 0x0000043a, "Maltese 47-Key" },
660
+ { 0x0001043a, "Maltese 48-key" },
661
+ { 0x00000481, "Maori" },
662
+ { 0x0000044e, "Marathi" },
663
+ { 0x00000850, "Mongolian (Mongolian Script - Legacy)" },
664
+ { 0x00020850, "Mongolian (Mongolian Script - Standard)" },
665
+ { 0x00000450, "Mongolian Cyrillic" },
666
+ { 0x00010c00, "Myanmar" },
667
+ { 0x00090c00, "N'ko" },
668
+ { 0x00000461, "Nepali" },
669
+ { 0x00020c00, "New Tai Lue" },
670
+ { 0x00000414, "Norwegian" },
671
+ { 0x0000043b, "Norwegian with Sami" },
672
+ { 0x00000448, "Odia" },
673
+ { 0x000d0c00, "Ol Chiki" },
674
+ { 0x000f0c00, "Old Italic" },
675
+ { 0x000e0c00, "Osmanya" },
676
+ { 0x00000463, "Pashto (Afghanistan)" },
677
+ { 0x00000429, "Persian" },
678
+ { 0x00050429, "Persian (Standard)" },
679
+ { 0x000a0c00, "Phags-pa" },
680
+ { 0x00010415, "Polish (214)" },
681
+ { 0x00000415, "Polish (Programmers)" },
682
+ { 0x00000816, "Portuguese" },
683
+ { 0x00000416, "Portuguese (Brazilian ABNT)" },
684
+ { 0x00010416, "Portuguese (Brazilian ABNT2)" },
685
+ { 0x00000446, "Punjabi" },
686
+ { 0x00000418, "Romanian (Legacy)" },
687
+ { 0x00020418, "Romanian (Programmers)" },
688
+ { 0x00010418, "Romanian (Standard)" },
689
+ { 0x00000419, "Russian" },
690
+ { 0x00020419, "Russian - Mnemonic" },
691
+ { 0x00010419, "Russian (Typewriter)" },
692
+ { 0x00000485, "Sakha" },
693
+ { 0x0002083b, "Sami Extended Finland-Sweden" },
694
+ { 0x0001043b, "Sami Extended Norway" },
695
+ { 0x00011809, "Scottish Gaelic" },
696
+ { 0x00000c1a, "Serbian (Cyrillic)" },
697
+ { 0x0000081a, "Serbian (Latin)" },
698
+ { 0x0000046c, "Sesotho sa Leboa" },
699
+ { 0x00000432, "Setswana" },
700
+ { 0x0000045b, "Sinhala" },
701
+ { 0x0001045b, "Sinhala - wij 9" },
702
+ { 0x0000041b, "Slovak" },
703
+ { 0x0001041b, "Slovak (QWERTY)" },
704
+ { 0x00000424, "Slovenian" },
705
+ { 0x00100c00, "Sora" },
706
+ { 0x0001042e, "Sorbian Extended" },
707
+ { 0x0002042e, "Sorbian Standard" },
708
+ { 0x0000042e, "Sorbian Standard (Legacy)" },
709
+ { 0x0000040a, "Spanish" },
710
+ { 0x0001040a, "Spanish Variation" },
711
+ { 0x0000041d, "Swedish" },
712
+ { 0x0000083b, "Swedish with Sami" },
713
+ { 0x0000100c, "Swiss French" },
714
+ { 0x00000807, "Swiss German" },
715
+ { 0x0000045a, "Syriac" },
716
+ { 0x0001045a, "Syriac Phonetic" },
717
+ { 0x00030c00, "Tai Le" },
718
+ { 0x00000428, "Tajik" },
719
+ { 0x00000449, "Tamil" },
720
+ { 0x00010444, "Tatar" },
721
+ { 0x00000444, "Tatar (Legacy)" },
722
+ { 0x0000044a, "Telugu" },
723
+ { 0x0000041e, "Thai Kedmanee" },
724
+ { 0x0002041e, "Thai Kedmanee (non-ShiftLock)" },
725
+ { 0x0001041e, "Thai Pattachote" },
726
+ { 0x0003041e, "Thai Pattachote (non-ShiftLock)" },
727
+ { 0x00010451, "Tibetan (PRC - Standard)" },
728
+ { 0x00000451, "Tibetan (PRC - Legacy)" },
729
+ { 0x00050c00, "Tifinagh (Basic)" },
730
+ { 0x00060c00, "Tifinagh (Full)" },
731
+ { 0x0001041f, "Turkish F" },
732
+ { 0x0000041f, "Turkish Q" },
733
+ { 0x00000442, "Turkmen" },
734
+ { 0x00010408, "Uyghur" },
735
+ { 0x00000480, "Uyghur (Legacy)" },
736
+ { 0x00000422, "Ukrainian" },
737
+ { 0x00020422, "Ukrainian (Enhanced)" },
738
+ { 0x00000809, "United Kingdom" },
739
+ { 0x00000452, "United Kingdom Extended" },
740
+ { 0x00010409, "United States - Dvorak" },
741
+ { 0x00020409, "United States - International" },
742
+ { 0x00030409, "United States-Dvorak for left hand" },
743
+ { 0x00040409, "United States-Dvorak for right hand" },
744
+ { 0x00000409, "United States - English" },
745
+ { 0x00000420, "Urdu" },
746
+ { 0x00010480, "Uyghur" },
747
+ { 0x00000843, "Uzbek Cyrillic" },
748
+ { 0x0000042a, "Vietnamese" },
749
+ { 0x00000488, "Wolof" },
750
+ { 0x00000485, "Yakut" },
751
+ { 0x0000046a, "Yoruba" },
752
+ };
753
+ static const size_t sRDP_KEYBOARD_LAYOUT_TABLE_len = ARRAYSIZE(sRDP_KEYBOARD_LAYOUT_TABLE);
754
+
755
+ static const RDP_KEYBOARD_LAYOUT_VARIANT sRDP_KEYBOARD_LAYOUT_VARIANT_TABLE[] = {
756
+ { KBD_ARABIC_102, 0x0028, "Arabic (102)" },
757
+ { KBD_BULGARIAN_LATIN, 0x0004, "Bulgarian (Latin)" },
758
+ { KBD_CZECH_QWERTY, 0x0005, "Czech (QWERTY)" },
759
+ { KBD_GERMAN_IBM, 0x0012, "German (IBM)" },
760
+ { KBD_GREEK_220, 0x0016, "Greek (220)" },
761
+ { KBD_UNITED_STATES_DVORAK, 0x0002, "United States-Dvorak" },
762
+ { KBD_SPANISH_VARIATION, 0x0086, "Spanish Variation" },
763
+ { KBD_HUNGARIAN_101_KEY, 0x0006, "Hungarian 101-key" },
764
+ { KBD_ITALIAN_142, 0x0003, "Italian (142)" },
765
+ { KBD_POLISH_214, 0x0007, "Polish (214)" },
766
+ { KBD_PORTUGUESE_BRAZILIAN_ABNT2, 0x001D, "Portuguese (Brazilian ABNT2)" },
767
+ { KBD_RUSSIAN_TYPEWRITER, 0x0008, "Russian (Typewriter)" },
768
+ { KBD_SLOVAK_QWERTY, 0x0013, "Slovak (QWERTY)" },
769
+ { KBD_THAI_PATTACHOTE, 0x0021, "Thai Pattachote" },
770
+ { KBD_TURKISH_F, 0x0014, "Turkish F" },
771
+ { KBD_LATVIAN_QWERTY, 0x0015, "Latvian (QWERTY)" },
772
+ { KBD_LITHUANIAN, 0x0027, "Lithuanian" },
773
+ { KBD_ARMENIAN_WESTERN, 0x0025, "Armenian Western" },
774
+ { KBD_HINDI_TRADITIONAL, 0x000C, "Hindi Traditional" },
775
+ { KBD_MALTESE_48_KEY, 0x002B, "Maltese 48-key" },
776
+ { KBD_SAMI_EXTENDED_NORWAY, 0x002C, "Sami Extended Norway" },
777
+ { KBD_BENGALI_INSCRIPT, 0x002A, "Bengali (Inscript)" },
778
+ { KBD_SYRIAC_PHONETIC, 0x000E, "Syriac Phonetic" },
779
+ { KBD_DIVEHI_TYPEWRITER, 0x000D, "Divehi Typewriter" },
780
+ { KBD_BELGIAN_COMMA, 0x001E, "Belgian (Comma)" },
781
+ { KBD_FINNISH_WITH_SAMI, 0x002D, "Finnish with Sami" },
782
+ { KBD_CANADIAN_MULTILINGUAL_STANDARD, 0x0020, "Canadian Multilingual Standard" },
783
+ { KBD_GAELIC, 0x0026, "Gaelic" },
784
+ { KBD_ARABIC_102_AZERTY, 0x0029, "Arabic (102) AZERTY" },
785
+ { KBD_CZECH_PROGRAMMERS, 0x000A, "Czech Programmers" },
786
+ { KBD_GREEK_319, 0x0018, "Greek (319)" },
787
+ { KBD_UNITED_STATES_INTERNATIONAL, 0x0001, "United States-International" },
788
+ { KBD_THAI_KEDMANEE_NON_SHIFTLOCK, 0x0022, "Thai Kedmanee (non-ShiftLock)" },
789
+ { KBD_SAMI_EXTENDED_FINLAND_SWEDEN, 0x002E, "Sami Extended Finland-Sweden" },
790
+ { KBD_GREEK_220_LATIN, 0x0017, "Greek (220) Latin" },
791
+ { KBD_UNITED_STATES_DVORAK_FOR_LEFT_HAND, 0x001A, "United States-Dvorak for left hand" },
792
+ { KBD_THAI_PATTACHOTE_NON_SHIFTLOCK, 0x0023, "Thai Pattachote (non-ShiftLock)" },
793
+ { KBD_GREEK_319_LATIN, 0x0011, "Greek (319) Latin" },
794
+ { KBD_UNITED_STATES_DVORAK_FOR_RIGHT_HAND, 0x001B, "United States-Dvorak for right hand" },
795
+ { KBD_UNITED_STATES_DVORAK_PROGRAMMER, 0x001C, "United States-Programmer Dvorak" },
796
+ { KBD_GREEK_LATIN, 0x0019, "Greek Latin" },
797
+ { KBD_US_ENGLISH_TABLE_FOR_IBM_ARABIC_238_L, 0x000B, "US English Table for IBM Arabic 238_L" },
798
+ { KBD_GREEK_POLYTONIC, 0x001F, "Greek Polytonic" },
799
+ { KBD_FRENCH_BEPO, 0x00C0, "French Bépo" },
800
+ { KBD_GERMAN_NEO, 0x00C0, "German Neo" }
801
+ };
802
+ static const size_t sRDP_KEYBOARD_LAYOUT_VARIANT_TABLE_len =
803
+ ARRAYSIZE(sRDP_KEYBOARD_LAYOUT_VARIANT_TABLE);
804
+
805
+ /* Input Method Editor (IME) */
806
+
807
+ /* Global Input Method Editors (IME) */
808
+
809
+ static const RDP_KEYBOARD_IME sRDP_KEYBOARD_IME_TABLE[] = {
810
+ { KBD_CHINESE_TRADITIONAL_PHONETIC, "phon.ime", "Chinese (Traditional) - Phonetic" },
811
+ { KBD_JAPANESE_INPUT_SYSTEM_MS_IME2002, "imjp81.ime", "Japanese Input System (MS-IME2002)" },
812
+ { KBD_KOREAN_INPUT_SYSTEM_IME_2000, "imekr61.ime", "Korean Input System (IME 2000)" },
813
+ { KBD_CHINESE_SIMPLIFIED_QUANPIN, "winpy.ime", "Chinese (Simplified) - QuanPin" },
814
+ { KBD_CHINESE_TRADITIONAL_CHANGJIE, "chajei.ime", "Chinese (Traditional) - ChangJie" },
815
+ { KBD_CHINESE_SIMPLIFIED_SHUANGPIN, "winsp.ime", "Chinese (Simplified) - ShuangPin" },
816
+ { KBD_CHINESE_TRADITIONAL_QUICK, "quick.ime", "Chinese (Traditional) - Quick" },
817
+ { KBD_CHINESE_SIMPLIFIED_ZHENGMA, "winzm.ime", "Chinese (Simplified) - ZhengMa" },
818
+ { KBD_CHINESE_TRADITIONAL_BIG5_CODE, "winime.ime", "Chinese (Traditional) - Big5 Code" },
819
+ { KBD_CHINESE_TRADITIONAL_ARRAY, "winar30.ime", "Chinese (Traditional) - Array" },
820
+ { KBD_CHINESE_SIMPLIFIED_NEIMA, "wingb.ime", "Chinese (Simplified) - NeiMa" },
821
+ { KBD_CHINESE_TRADITIONAL_DAYI, "dayi.ime", "Chinese (Traditional) - DaYi" },
822
+ { KBD_CHINESE_TRADITIONAL_UNICODE, "unicdime.ime", "Chinese (Traditional) - Unicode" },
823
+ { KBD_CHINESE_TRADITIONAL_NEW_PHONETIC, "TINTLGNT.IME",
824
+ "Chinese (Traditional) - New Phonetic" },
825
+ { KBD_CHINESE_TRADITIONAL_NEW_CHANGJIE, "CINTLGNT.IME",
826
+ "Chinese (Traditional) - New ChangJie" },
827
+ { KBD_CHINESE_TRADITIONAL_MICROSOFT_PINYIN_IME_3, "pintlgnt.ime",
828
+ "Chinese (Traditional) - Microsoft Pinyin IME 3.0" },
829
+ { KBD_CHINESE_TRADITIONAL_ALPHANUMERIC, "romanime.ime", "Chinese (Traditional) - Alphanumeric" }
830
+ };
831
+ static const size_t sRDP_KEYBOARD_IME_TABLE_len = ARRAYSIZE(sRDP_KEYBOARD_IME_TABLE);
832
+
833
+ #if defined(DUMP_LAYOUTS_TO_JSON)
834
+ static BOOL append_layout(WINPR_JSON* json)
835
+ {
836
+ WINPR_JSON* array = WINPR_JSON_AddArrayToObject(json, "KeyboardLayouts");
837
+ if (!array)
838
+ return FALSE;
839
+
840
+ for (size_t x = 0; x < sRDP_KEYBOARD_LAYOUT_TABLE_len; x++)
841
+ {
842
+ const RDP_KEYBOARD_LAYOUT* const ime = &sRDP_KEYBOARD_LAYOUT_TABLE[x];
843
+ WINPR_JSON* obj = WINPR_JSON_CreateObject();
844
+ if (!obj)
845
+ return FALSE;
846
+ WINPR_JSON_AddNumberToObject(obj, "code", ime->code);
847
+ WINPR_JSON_AddStringToObject(obj, "name", ime->name);
848
+
849
+ if (!WINPR_JSON_AddItemToArray(array, obj))
850
+ return FALSE;
851
+ }
852
+
853
+ return TRUE;
854
+ }
855
+
856
+ static BOOL append_variant(WINPR_JSON* json)
857
+ {
858
+ WINPR_JSON* array = WINPR_JSON_AddArrayToObject(json, "KeyboardVariants");
859
+ if (!array)
860
+ return FALSE;
861
+
862
+ for (size_t x = 0; x < sRDP_KEYBOARD_LAYOUT_VARIANT_TABLE_len; x++)
863
+ {
864
+ const RDP_KEYBOARD_LAYOUT_VARIANT* const ime = &sRDP_KEYBOARD_LAYOUT_VARIANT_TABLE[x];
865
+ WINPR_JSON* obj = WINPR_JSON_CreateObject();
866
+ if (!obj)
867
+ return FALSE;
868
+ WINPR_JSON_AddNumberToObject(obj, "code", ime->code);
869
+ WINPR_JSON_AddNumberToObject(obj, "id", ime->id);
870
+ WINPR_JSON_AddStringToObject(obj, "name", ime->name);
871
+
872
+ if (!WINPR_JSON_AddItemToArray(array, obj))
873
+ return FALSE;
874
+ }
875
+
876
+ return TRUE;
877
+ }
878
+
879
+ static BOOL append_ime(WINPR_JSON* json)
880
+ {
881
+ WINPR_JSON* array = WINPR_JSON_AddArrayToObject(json, "KeyboardIme");
882
+ if (!array)
883
+ return FALSE;
884
+
885
+ for (size_t x = 0; x < sRDP_KEYBOARD_IME_TABLE_len; x++)
886
+ {
887
+ const RDP_KEYBOARD_IME* const ime = &sRDP_KEYBOARD_IME_TABLE[x];
888
+ WINPR_JSON* obj = WINPR_JSON_CreateObject();
889
+ if (!obj)
890
+ return FALSE;
891
+ WINPR_JSON_AddNumberToObject(obj, "code", ime->code);
892
+ WINPR_JSON_AddStringToObject(obj, "file", ime->file);
893
+ WINPR_JSON_AddStringToObject(obj, "name", ime->name);
894
+
895
+ if (!WINPR_JSON_AddItemToArray(array, obj))
896
+ return FALSE;
897
+ }
898
+
899
+ return TRUE;
900
+ }
901
+ #endif
902
+
903
+ static BOOL load_layout_file(void)
904
+ {
905
+ #if defined(DUMP_LAYOUTS_TO_JSON)
906
+ /* Dump to file in /tmp */
907
+ char* str = NULL;
908
+ WINPR_JSON* json = WINPR_JSON_CreateObject();
909
+ if (!json)
910
+ goto end;
911
+
912
+ if (!append_layout(json))
913
+ goto end;
914
+ if (!append_variant(json))
915
+ goto end;
916
+ if (!append_ime(json))
917
+ goto end;
918
+
919
+ str = WINPR_JSON_Print(json);
920
+ if (!str)
921
+ goto end;
922
+ {
923
+ FILE* fp = winpr_fopen("/tmp/kbd.json", "w");
924
+ if (!fp)
925
+ goto end;
926
+ (void)fprintf(fp, "%s", str);
927
+ fclose(fp);
928
+ }
929
+ end:
930
+ free(str);
931
+ WINPR_JSON_Delete(json);
932
+ #endif
933
+ return TRUE;
934
+ }
935
+ #else
936
+ static RDP_KEYBOARD_LAYOUT* sRDP_KEYBOARD_LAYOUT_TABLE = NULL;
937
+ static size_t sRDP_KEYBOARD_LAYOUT_TABLE_len = 0;
938
+
939
+ static RDP_KEYBOARD_LAYOUT_VARIANT* sRDP_KEYBOARD_LAYOUT_VARIANT_TABLE = NULL;
940
+ static size_t sRDP_KEYBOARD_LAYOUT_VARIANT_TABLE_len = 0;
941
+
942
+ static RDP_KEYBOARD_IME* sRDP_KEYBOARD_IME_TABLE = NULL;
943
+ static size_t sRDP_KEYBOARD_IME_TABLE_len = 0;
944
+
945
+ static void clear_keyboard_layout(RDP_KEYBOARD_LAYOUT* layout)
946
+ {
947
+ if (!layout)
948
+ return;
949
+
950
+ free(layout->name);
951
+ const RDP_KEYBOARD_LAYOUT empty = { 0 };
952
+ *layout = empty;
953
+ }
954
+
955
+ static void clear_keyboard_variant(RDP_KEYBOARD_LAYOUT_VARIANT* layout)
956
+ {
957
+ if (!layout)
958
+ return;
959
+
960
+ free(layout->name);
961
+ const RDP_KEYBOARD_LAYOUT_VARIANT empty = { 0 };
962
+ *layout = empty;
963
+ }
964
+
965
+ static void clear_keyboard_ime(RDP_KEYBOARD_IME* layout)
966
+ {
967
+ if (!layout)
968
+ return;
969
+
970
+ free(layout->file);
971
+ free(layout->name);
972
+ const RDP_KEYBOARD_IME empty = { 0 };
973
+ *layout = empty;
974
+ }
975
+
976
+ static void clear_layout_tables(void)
977
+ {
978
+ for (size_t x = 0; x < sRDP_KEYBOARD_LAYOUT_TABLE_len; x++)
979
+ {
980
+ RDP_KEYBOARD_LAYOUT* layout = &sRDP_KEYBOARD_LAYOUT_TABLE[x];
981
+ clear_keyboard_layout(layout);
982
+ }
983
+
984
+ free(sRDP_KEYBOARD_LAYOUT_TABLE);
985
+ sRDP_KEYBOARD_LAYOUT_TABLE = NULL;
986
+ sRDP_KEYBOARD_LAYOUT_TABLE_len = 0;
987
+
988
+ for (size_t x = 0; x < sRDP_KEYBOARD_LAYOUT_VARIANT_TABLE_len; x++)
989
+ {
990
+ RDP_KEYBOARD_LAYOUT_VARIANT* variant = &sRDP_KEYBOARD_LAYOUT_VARIANT_TABLE[x];
991
+ clear_keyboard_variant(variant);
992
+ }
993
+ free(sRDP_KEYBOARD_LAYOUT_VARIANT_TABLE);
994
+ sRDP_KEYBOARD_LAYOUT_VARIANT_TABLE = NULL;
995
+ sRDP_KEYBOARD_LAYOUT_VARIANT_TABLE_len = 0;
996
+
997
+ for (size_t x = 0; x < sRDP_KEYBOARD_IME_TABLE_len; x++)
998
+ {
999
+ RDP_KEYBOARD_IME* ime = &sRDP_KEYBOARD_IME_TABLE[x];
1000
+ clear_keyboard_ime(ime);
1001
+ }
1002
+ free(sRDP_KEYBOARD_IME_TABLE);
1003
+ sRDP_KEYBOARD_IME_TABLE = NULL;
1004
+ sRDP_KEYBOARD_IME_TABLE_len = 0;
1005
+ }
1006
+
1007
+ static WINPR_JSON* load_layouts_from_file(const char* filename)
1008
+ {
1009
+ INT64 jstrlen = 0;
1010
+ char* jstr = NULL;
1011
+ WINPR_JSON* json = NULL;
1012
+ FILE* fp = winpr_fopen(filename, "r");
1013
+ if (!fp)
1014
+ {
1015
+ WLog_WARN(TAG, "resource file '%s' does not exist or is not readable", filename);
1016
+ return NULL;
1017
+ }
1018
+
1019
+ if (_fseeki64(fp, 0, SEEK_END) < 0)
1020
+ {
1021
+ WLog_WARN(TAG, "resource file '%s' seek failed", filename);
1022
+ goto end;
1023
+ }
1024
+ jstrlen = _ftelli64(fp);
1025
+ if (jstrlen < 0)
1026
+ {
1027
+ WLog_WARN(TAG, "resource file '%s' invalid length %" PRId64, filename, jstrlen);
1028
+ goto end;
1029
+ }
1030
+ if (_fseeki64(fp, 0, SEEK_SET) < 0)
1031
+ {
1032
+ WLog_WARN(TAG, "resource file '%s' seek failed", filename);
1033
+ goto end;
1034
+ }
1035
+
1036
+ jstr = calloc(jstrlen + 1, sizeof(char));
1037
+ if (!jstr)
1038
+ {
1039
+ WLog_WARN(TAG, "resource file '%s' failed to allocate buffer of size %" PRId64, filename,
1040
+ jstrlen);
1041
+ goto end;
1042
+ }
1043
+
1044
+ if (fread(jstr, jstrlen, sizeof(char), fp) != 1)
1045
+ {
1046
+ WLog_WARN(TAG, "resource file '%s' failed to read buffer of size %" PRId64, filename,
1047
+ jstrlen);
1048
+ goto end;
1049
+ }
1050
+
1051
+ json = WINPR_JSON_ParseWithLength(jstr, jstrlen);
1052
+ if (!json)
1053
+ WLog_WARN(TAG, "resource file '%s' is not a valid JSON file", filename);
1054
+ end:
1055
+ fclose(fp);
1056
+ free(jstr);
1057
+ return json;
1058
+ }
1059
+
1060
+ static char* get_object_str(WINPR_JSON* json, size_t pos, const char* name)
1061
+ {
1062
+ WINPR_ASSERT(json);
1063
+ if (!WINPR_JSON_IsObject(json) || !WINPR_JSON_HasObjectItem(json, name))
1064
+ {
1065
+ WLog_WARN(TAG, "Invalid JSON entry at entry %" PRIuz ", missing an Object named '%s'", pos,
1066
+ name);
1067
+ return NULL;
1068
+ }
1069
+ WINPR_JSON* obj = WINPR_JSON_GetObjectItem(json, name);
1070
+ WINPR_ASSERT(obj);
1071
+ if (!WINPR_JSON_IsString(obj))
1072
+ {
1073
+ WLog_WARN(TAG,
1074
+ "Invalid JSON entry at entry %" PRIuz ", Object named '%s': Not of type string",
1075
+ pos, name);
1076
+ return NULL;
1077
+ }
1078
+
1079
+ const char* str = WINPR_JSON_GetStringValue(obj);
1080
+ if (!str)
1081
+ {
1082
+ WLog_WARN(TAG, "Invalid JSON entry at entry %" PRIuz ", Object named '%s': NULL string",
1083
+ pos, name);
1084
+ return NULL;
1085
+ }
1086
+
1087
+ return _strdup(str);
1088
+ }
1089
+
1090
+ static UINT32 get_object_integer(WINPR_JSON* json, size_t pos, const char* name)
1091
+ {
1092
+ WINPR_ASSERT(json);
1093
+ if (!WINPR_JSON_IsObject(json) || !WINPR_JSON_HasObjectItem(json, name))
1094
+ {
1095
+ WLog_WARN(TAG, "Invalid JSON entry at entry %" PRIuz ", missing an Object named '%s'", pos,
1096
+ name);
1097
+ return 0;
1098
+ }
1099
+ WINPR_JSON* obj = WINPR_JSON_GetObjectItem(json, name);
1100
+ WINPR_ASSERT(obj);
1101
+ if (!WINPR_JSON_IsNumber(obj))
1102
+ {
1103
+ WLog_WARN(TAG,
1104
+ "Invalid JSON entry at entry %" PRIuz ", Object named '%s': Not of type string",
1105
+ pos, name);
1106
+ return 0;
1107
+ }
1108
+
1109
+ return WINPR_JSON_GetNumberValue(obj);
1110
+ }
1111
+
1112
+ static bool parse_json_layout_entry_id(WINPR_JSON* json, size_t pos, RDP_KEYBOARD_LAYOUT* entry)
1113
+ {
1114
+ WINPR_ASSERT(entry);
1115
+ const int64_t code = get_object_integer(json, pos, "code");
1116
+ if ((code < INT32_MIN) || (code > UINT32_MAX))
1117
+ {
1118
+ WLog_WARN(TAG,
1119
+ "Invalid JSON 'code' entry at entry %" PRIuz
1120
+ ", value out of range: %d <= %" PRId64 " <= %" PRIu32,
1121
+ pos, INT32_MIN, code, UINT32_MAX);
1122
+ return false;
1123
+ }
1124
+ entry->code = WINPR_CXX_COMPAT_CAST(uint32_t, code);
1125
+ entry->name = get_object_str(json, pos, "name");
1126
+ return entry->name != NULL;
1127
+ }
1128
+
1129
+ static BOOL parse_json_layout_entry(WINPR_JSON* json, size_t pos, RDP_KEYBOARD_LAYOUT* entry)
1130
+ {
1131
+ WINPR_ASSERT(entry);
1132
+ if (!json || !WINPR_JSON_IsObject(json))
1133
+ {
1134
+ WLog_WARN(TAG, "Invalid JSON entry at entry %" PRIuz ", expected an array", pos);
1135
+ return FALSE;
1136
+ }
1137
+
1138
+ if (!parse_json_layout_entry_id(json, pos, entry))
1139
+ {
1140
+ clear_keyboard_layout(entry);
1141
+ return FALSE;
1142
+ }
1143
+ return TRUE;
1144
+ }
1145
+
1146
+ static BOOL parse_layout_entries(WINPR_JSON* json, const char* filename)
1147
+ {
1148
+ if (!WINPR_JSON_IsArray(json))
1149
+ {
1150
+ WLog_WARN(TAG, "Invalid top level JSON type in file %s, expected an array", filename);
1151
+ return FALSE;
1152
+ }
1153
+
1154
+ sRDP_KEYBOARD_LAYOUT_TABLE_len = WINPR_JSON_GetArraySize(json);
1155
+ sRDP_KEYBOARD_LAYOUT_TABLE =
1156
+ calloc(sRDP_KEYBOARD_LAYOUT_TABLE_len, sizeof(RDP_KEYBOARD_LAYOUT));
1157
+ if (!sRDP_KEYBOARD_LAYOUT_TABLE)
1158
+ {
1159
+ clear_layout_tables();
1160
+ return FALSE;
1161
+ }
1162
+
1163
+ for (size_t x = 0; x < sRDP_KEYBOARD_LAYOUT_TABLE_len; x++)
1164
+ {
1165
+ WINPR_JSON* obj = WINPR_JSON_GetArrayItem(json, x);
1166
+
1167
+ if (!obj || !parse_json_layout_entry(obj, x, &sRDP_KEYBOARD_LAYOUT_TABLE[x]))
1168
+ {
1169
+ clear_layout_tables();
1170
+ return FALSE;
1171
+ }
1172
+ }
1173
+
1174
+ return TRUE;
1175
+ }
1176
+
1177
+ static BOOL parse_json_variant_entry(WINPR_JSON* json, size_t pos,
1178
+ RDP_KEYBOARD_LAYOUT_VARIANT* entry)
1179
+ {
1180
+ WINPR_ASSERT(entry);
1181
+ if (!json || !WINPR_JSON_IsObject(json))
1182
+ {
1183
+ WLog_WARN(TAG, "Invalid JSON entry at entry %" PRIuz ", expected an array", pos);
1184
+ return FALSE;
1185
+ }
1186
+
1187
+ if (!parse_json_layout_entry_id(json, pos, entry))
1188
+ {
1189
+ clear_keyboard_variant(entry);
1190
+ return FALSE;
1191
+ }
1192
+ entry->id = get_object_integer(json, pos, "id");
1193
+ return TRUE;
1194
+ }
1195
+
1196
+ static BOOL parse_variant_entries(WINPR_JSON* json, const char* filename)
1197
+ {
1198
+ if (!WINPR_JSON_IsArray(json))
1199
+ {
1200
+ WLog_WARN(TAG, "Invalid top level JSON type in file %s, expected an array", filename);
1201
+ return FALSE;
1202
+ }
1203
+ WINPR_ASSERT(!sRDP_KEYBOARD_LAYOUT_VARIANT_TABLE);
1204
+ WINPR_ASSERT(sRDP_KEYBOARD_LAYOUT_VARIANT_TABLE_len == 0);
1205
+
1206
+ const size_t count = WINPR_JSON_GetArraySize(json);
1207
+ sRDP_KEYBOARD_LAYOUT_VARIANT_TABLE = calloc(count, sizeof(RDP_KEYBOARD_LAYOUT_VARIANT));
1208
+ if (!sRDP_KEYBOARD_LAYOUT_VARIANT_TABLE)
1209
+ return FALSE;
1210
+ sRDP_KEYBOARD_LAYOUT_VARIANT_TABLE_len = count;
1211
+
1212
+ for (size_t x = 0; x < count; x++)
1213
+ {
1214
+ WINPR_JSON* entry = WINPR_JSON_GetArrayItem(json, x);
1215
+ if (!parse_json_variant_entry(entry, x, &sRDP_KEYBOARD_LAYOUT_VARIANT_TABLE[x]))
1216
+ return FALSE;
1217
+ }
1218
+ return TRUE;
1219
+ }
1220
+
1221
+ static BOOL parse_json_ime_entry(WINPR_JSON* json, size_t pos, RDP_KEYBOARD_IME* entry)
1222
+ {
1223
+ WINPR_ASSERT(entry);
1224
+ if (!json || !WINPR_JSON_IsObject(json))
1225
+ {
1226
+ WLog_WARN(TAG, "Invalid JSON entry at entry %" PRIuz ", expected an array", pos);
1227
+ return FALSE;
1228
+ }
1229
+
1230
+ entry->code = get_object_integer(json, pos, "code");
1231
+ entry->file = get_object_str(json, pos, "file");
1232
+ entry->name = get_object_str(json, pos, "name");
1233
+ if (!entry->file || !entry->name)
1234
+ {
1235
+ clear_keyboard_ime(entry);
1236
+ return FALSE;
1237
+ }
1238
+ return TRUE;
1239
+ }
1240
+
1241
+ static BOOL parse_ime_entries(WINPR_JSON* json, const char* filename)
1242
+ {
1243
+ if (!WINPR_JSON_IsArray(json))
1244
+ {
1245
+ WLog_WARN(TAG, "Invalid top level JSON type in file %s, expected an array", filename);
1246
+ return FALSE;
1247
+ }
1248
+ WINPR_ASSERT(!sRDP_KEYBOARD_IME_TABLE);
1249
+ WINPR_ASSERT(sRDP_KEYBOARD_IME_TABLE_len == 0);
1250
+
1251
+ const size_t count = WINPR_JSON_GetArraySize(json);
1252
+ sRDP_KEYBOARD_IME_TABLE = calloc(count, sizeof(RDP_KEYBOARD_IME));
1253
+ if (!sRDP_KEYBOARD_IME_TABLE)
1254
+ return FALSE;
1255
+ sRDP_KEYBOARD_IME_TABLE_len = count;
1256
+
1257
+ for (size_t x = 0; x < count; x++)
1258
+ {
1259
+ WINPR_JSON* entry = WINPR_JSON_GetArrayItem(json, x);
1260
+ if (!parse_json_ime_entry(entry, x, &sRDP_KEYBOARD_IME_TABLE[x]))
1261
+ return FALSE;
1262
+ }
1263
+ return TRUE;
1264
+ }
1265
+
1266
+ static BOOL CALLBACK load_layouts(PINIT_ONCE once, PVOID param, PVOID* context)
1267
+ {
1268
+ WINPR_UNUSED(once);
1269
+ WINPR_UNUSED(param);
1270
+ WINPR_UNUSED(context);
1271
+
1272
+ WINPR_JSON* json = NULL;
1273
+ char* filename = GetCombinedPath(FREERDP_RESOURCE_ROOT, "KeyboardLayoutMap.json");
1274
+ if (!filename)
1275
+ {
1276
+ WLog_WARN(TAG, "Could not create WinPR timezone resource filename");
1277
+ goto end;
1278
+ }
1279
+
1280
+ json = load_layouts_from_file(filename);
1281
+ if (!json)
1282
+ goto end;
1283
+
1284
+ if (!WINPR_JSON_IsObject(json))
1285
+ {
1286
+ WLog_WARN(TAG, "Invalid top level JSON type in file %s, expected an array", filename);
1287
+ goto end;
1288
+ }
1289
+
1290
+ clear_layout_tables();
1291
+ {
1292
+ WINPR_JSON* obj = WINPR_JSON_GetObjectItem(json, "KeyboardLayouts");
1293
+ if (!parse_layout_entries(obj, filename))
1294
+ goto end;
1295
+ }
1296
+ {
1297
+ WINPR_JSON* obj = WINPR_JSON_GetObjectItem(json, "KeyboardVariants");
1298
+ if (!parse_variant_entries(obj, filename))
1299
+ goto end;
1300
+ }
1301
+ {
1302
+ WINPR_JSON* obj = WINPR_JSON_GetObjectItem(json, "KeyboardIme");
1303
+ if (!parse_ime_entries(obj, filename))
1304
+ goto end;
1305
+ }
1306
+
1307
+ end:
1308
+ free(filename);
1309
+ WINPR_JSON_Delete(json);
1310
+ (void)atexit(clear_layout_tables);
1311
+ return TRUE;
1312
+ }
1313
+
1314
+ static BOOL load_layout_file(void)
1315
+ {
1316
+ static INIT_ONCE once = INIT_ONCE_STATIC_INIT;
1317
+ InitOnceExecuteOnce(&once, load_layouts, NULL, NULL);
1318
+ return TRUE;
1319
+ }
1320
+
1321
+ #endif
1322
+
1323
+ static UINT32 rdp_keyboard_layout_by_name(const char* name)
1324
+ {
1325
+ WINPR_ASSERT(name);
1326
+ load_layout_file();
1327
+
1328
+ for (size_t i = 0; i < sRDP_KEYBOARD_LAYOUT_TABLE_len; i++)
1329
+ {
1330
+ const RDP_KEYBOARD_LAYOUT* const layout = &sRDP_KEYBOARD_LAYOUT_TABLE[i];
1331
+ if (strcmp(layout->name, name) == 0)
1332
+ return layout->code;
1333
+ }
1334
+ return 0;
1335
+ }
1336
+
1337
+ static uint32_t internal2unsigned(int64_t code)
1338
+ {
1339
+ WINPR_ASSERT(code >= INT32_MIN);
1340
+ WINPR_ASSERT(code <= UINT32_MAX);
1341
+ return WINPR_CXX_COMPAT_CAST(uint32_t, code);
1342
+ }
1343
+
1344
+ static UINT32 rdp_keyboard_variant_by_name(const char* name)
1345
+ {
1346
+ WINPR_ASSERT(name);
1347
+ load_layout_file();
1348
+
1349
+ for (size_t i = 0; i < sRDP_KEYBOARD_LAYOUT_VARIANT_TABLE_len; i++)
1350
+ {
1351
+ const RDP_KEYBOARD_LAYOUT_VARIANT* const variant = &sRDP_KEYBOARD_LAYOUT_VARIANT_TABLE[i];
1352
+ if (strcmp(variant->name, name) == 0)
1353
+ return internal2unsigned(variant->code);
1354
+ }
1355
+ return 0;
1356
+ }
1357
+
1358
+ static UINT32 rdp_keyboard_ime_by_name(const char* name)
1359
+ {
1360
+ WINPR_ASSERT(name);
1361
+ load_layout_file();
1362
+
1363
+ for (size_t i = 0; i < sRDP_KEYBOARD_IME_TABLE_len; i++)
1364
+ {
1365
+ const RDP_KEYBOARD_IME* const ime = &sRDP_KEYBOARD_IME_TABLE[i];
1366
+ if (strcmp(ime->name, name) == 0)
1367
+ return internal2unsigned(ime->code);
1368
+ }
1369
+ return 0;
1370
+ }
1371
+
1372
+ static const char* rdp_keyboard_layout_by_id(UINT32 id)
1373
+ {
1374
+ load_layout_file();
1375
+
1376
+ for (size_t i = 0; i < sRDP_KEYBOARD_LAYOUT_TABLE_len; i++)
1377
+ {
1378
+ const RDP_KEYBOARD_LAYOUT* const layout = &sRDP_KEYBOARD_LAYOUT_TABLE[i];
1379
+ if (layout->code == id)
1380
+ return layout->name;
1381
+ }
1382
+
1383
+ return 0;
1384
+ }
1385
+
1386
+ static const char* rdp_keyboard_variant_by_id(UINT32 id)
1387
+ {
1388
+ load_layout_file();
1389
+
1390
+ for (size_t i = 0; i < sRDP_KEYBOARD_LAYOUT_VARIANT_TABLE_len; i++)
1391
+ {
1392
+ const RDP_KEYBOARD_LAYOUT_VARIANT* const variant = &sRDP_KEYBOARD_LAYOUT_VARIANT_TABLE[i];
1393
+ if (variant->code == id)
1394
+ return variant->name;
1395
+ }
1396
+ return 0;
1397
+ }
1398
+
1399
+ static const char* rdp_keyboard_ime_by_id(UINT32 id)
1400
+ {
1401
+ load_layout_file();
1402
+
1403
+ for (size_t i = 0; i < sRDP_KEYBOARD_IME_TABLE_len; i++)
1404
+ {
1405
+ const RDP_KEYBOARD_IME* const ime = &sRDP_KEYBOARD_IME_TABLE[i];
1406
+ if (ime->code == id)
1407
+ return ime->name;
1408
+ }
1409
+ return NULL;
1410
+ }
1411
+
1412
+ static BOOL rdp_keyboard_layout_clone_append(RDP_KEYBOARD_LAYOUT** layouts, size_t* pcount)
1413
+ {
1414
+ WINPR_ASSERT(layouts);
1415
+ WINPR_ASSERT(pcount);
1416
+
1417
+ load_layout_file();
1418
+
1419
+ const size_t length = sRDP_KEYBOARD_LAYOUT_TABLE_len;
1420
+ const size_t offset = *pcount;
1421
+ if (!reallocate_keyboard_layouts(layouts, pcount, length))
1422
+ return FALSE;
1423
+
1424
+ for (size_t i = 0; i < length; i++)
1425
+ {
1426
+ const RDP_KEYBOARD_LAYOUT* const ime = &sRDP_KEYBOARD_LAYOUT_TABLE[i];
1427
+ RDP_KEYBOARD_LAYOUT* layout = &(*layouts)[i + offset];
1428
+ layout->code = internal2unsigned(ime->code);
1429
+ if (ime->name)
1430
+ layout->name = _strdup(ime->name);
1431
+
1432
+ if (!layout->name)
1433
+ return FALSE;
1434
+ }
1435
+ return TRUE;
1436
+ }
1437
+
1438
+ static BOOL rdp_keyboard_variant_clone_append(RDP_KEYBOARD_LAYOUT** layouts, size_t* pcount)
1439
+ {
1440
+ WINPR_ASSERT(layouts);
1441
+ WINPR_ASSERT(pcount);
1442
+
1443
+ load_layout_file();
1444
+
1445
+ const size_t length = sRDP_KEYBOARD_LAYOUT_VARIANT_TABLE_len;
1446
+ const size_t offset = *pcount;
1447
+ if (!reallocate_keyboard_layouts(layouts, pcount, length))
1448
+ return FALSE;
1449
+
1450
+ for (size_t i = 0; i < length; i++)
1451
+ {
1452
+ const RDP_KEYBOARD_LAYOUT_VARIANT* const ime = &sRDP_KEYBOARD_LAYOUT_VARIANT_TABLE[i];
1453
+ RDP_KEYBOARD_LAYOUT* layout = &(*layouts)[i + offset];
1454
+ layout->code = internal2unsigned(ime->code);
1455
+ if (ime->name)
1456
+ layout->name = _strdup(ime->name);
1457
+
1458
+ if (!layout->name)
1459
+ return FALSE;
1460
+ }
1461
+ return TRUE;
1462
+ }
1463
+
1464
+ static BOOL rdp_keyboard_ime_clone_append(RDP_KEYBOARD_LAYOUT** layouts, size_t* pcount)
1465
+ {
1466
+ WINPR_ASSERT(layouts);
1467
+ WINPR_ASSERT(pcount);
1468
+
1469
+ load_layout_file();
1470
+
1471
+ const size_t length = sRDP_KEYBOARD_IME_TABLE_len;
1472
+ const size_t offset = *pcount;
1473
+ if (!reallocate_keyboard_layouts(layouts, pcount, length))
1474
+ return FALSE;
1475
+
1476
+ for (size_t i = 0; i < length; i++)
1477
+ {
1478
+ const RDP_KEYBOARD_IME* const ime = &sRDP_KEYBOARD_IME_TABLE[i];
1479
+ RDP_KEYBOARD_LAYOUT* layout = &(*layouts)[i + offset];
1480
+ layout->code = internal2unsigned(ime->code);
1481
+ if (ime->name)
1482
+ layout->name = _strdup(ime->name);
1483
+
1484
+ if (!layout->name)
1485
+ return FALSE;
1486
+ }
1487
+ return TRUE;
1488
+ }
1489
+
1490
+ void freerdp_keyboard_layouts_free(RDP_KEYBOARD_LAYOUT* layouts, size_t count)
1491
+ {
1492
+ if (!layouts)
1493
+ return;
1494
+
1495
+ for (size_t x = 0; x < count; x++)
1496
+ {
1497
+ RDP_KEYBOARD_LAYOUT* current = &layouts[x];
1498
+ free(current->name);
1499
+ current++;
1500
+ }
1501
+
1502
+ free(layouts);
1503
+ }
1504
+
1505
+ RDP_KEYBOARD_LAYOUT* freerdp_keyboard_get_layouts(DWORD types, size_t* count)
1506
+ {
1507
+ size_t num = 0;
1508
+ RDP_KEYBOARD_LAYOUT* layouts = NULL;
1509
+
1510
+ load_layout_file();
1511
+
1512
+ num = 0;
1513
+
1514
+ WINPR_ASSERT(count);
1515
+ *count = 0;
1516
+
1517
+ if ((types & RDP_KEYBOARD_LAYOUT_TYPE_STANDARD) != 0)
1518
+ {
1519
+ if (!rdp_keyboard_layout_clone_append(&layouts, &num))
1520
+ goto fail;
1521
+ }
1522
+
1523
+ if ((types & RDP_KEYBOARD_LAYOUT_TYPE_VARIANT) != 0)
1524
+ {
1525
+ if (!rdp_keyboard_variant_clone_append(&layouts, &num))
1526
+ goto fail;
1527
+ }
1528
+
1529
+ if ((types & RDP_KEYBOARD_LAYOUT_TYPE_IME) != 0)
1530
+ {
1531
+ if (!rdp_keyboard_ime_clone_append(&layouts, &num))
1532
+ goto fail;
1533
+ }
1534
+
1535
+ *count = num;
1536
+ return layouts;
1537
+ fail:
1538
+ freerdp_keyboard_layouts_free(layouts, num);
1539
+ return NULL;
1540
+ }
1541
+
1542
+ const char* freerdp_keyboard_get_layout_name_from_id(DWORD keyboardLayoutID)
1543
+ {
1544
+ const char* name = rdp_keyboard_layout_by_id(keyboardLayoutID);
1545
+ if (name)
1546
+ return name;
1547
+
1548
+ name = rdp_keyboard_variant_by_id(keyboardLayoutID);
1549
+ if (name)
1550
+ return name;
1551
+
1552
+ name = rdp_keyboard_ime_by_id(keyboardLayoutID);
1553
+ if (name)
1554
+ return name;
1555
+
1556
+ return "unknown";
1557
+ }
1558
+
1559
+ DWORD freerdp_keyboard_get_layout_id_from_name(const char* name)
1560
+ {
1561
+ DWORD rc = rdp_keyboard_layout_by_name(name);
1562
+ if (rc != 0)
1563
+ return rc;
1564
+
1565
+ rc = rdp_keyboard_variant_by_name(name);
1566
+ if (rc != 0)
1567
+ return rc;
1568
+
1569
+ return rdp_keyboard_ime_by_name(name);
1570
+ }
1571
+
1572
+ static void copy(const struct LanguageIdentifier* id, RDP_CODEPAGE* cp)
1573
+ {
1574
+ cp->id = id->LanguageIdentifier;
1575
+ cp->subId = id->SublangaugeIdentifier;
1576
+ cp->primaryId = id->PrimaryLanguageIdentifier;
1577
+ if (id->locale)
1578
+ strncpy(cp->locale, id->locale, ARRAYSIZE(cp->locale) - 1);
1579
+ if (id->PrimaryLanguage)
1580
+ strncpy(cp->primaryLanguage, id->PrimaryLanguage, ARRAYSIZE(cp->primaryLanguage) - 1);
1581
+ if (id->PrimaryLanguageSymbol)
1582
+ strncpy(cp->primaryLanguageSymbol, id->PrimaryLanguageSymbol,
1583
+ ARRAYSIZE(cp->primaryLanguageSymbol) - 1);
1584
+ if (id->Sublanguage)
1585
+ strncpy(cp->subLanguage, id->Sublanguage, ARRAYSIZE(cp->subLanguage) - 1);
1586
+ if (id->SublanguageSymbol)
1587
+ strncpy(cp->subLanguageSymbol, id->SublanguageSymbol, ARRAYSIZE(cp->subLanguageSymbol) - 1);
1588
+ }
1589
+
1590
+ static BOOL copyOnMatch(DWORD column, const char* filter, const struct LanguageIdentifier* cur,
1591
+ RDP_CODEPAGE* dst)
1592
+ {
1593
+ const char* what = NULL;
1594
+ switch (column)
1595
+ {
1596
+ case 0:
1597
+ what = cur->locale;
1598
+ break;
1599
+ case 1:
1600
+ what = cur->PrimaryLanguage;
1601
+ break;
1602
+ case 2:
1603
+ what = cur->PrimaryLanguageSymbol;
1604
+ break;
1605
+ case 3:
1606
+ what = cur->Sublanguage;
1607
+ break;
1608
+ case 4:
1609
+ what = cur->SublanguageSymbol;
1610
+ break;
1611
+ default:
1612
+ return FALSE;
1613
+ }
1614
+
1615
+ if (filter)
1616
+ {
1617
+ if (!strstr(what, filter))
1618
+ return FALSE;
1619
+ }
1620
+ copy(cur, dst);
1621
+ return TRUE;
1622
+ }
1623
+
1624
+ RDP_CODEPAGE* freerdp_keyboard_get_matching_codepages(DWORD column, const char* filter,
1625
+ size_t* count)
1626
+ {
1627
+ size_t cnt = 0;
1628
+ const size_t c = ARRAYSIZE(language_identifiers);
1629
+ RDP_CODEPAGE* pages = calloc(ARRAYSIZE(language_identifiers), sizeof(RDP_CODEPAGE));
1630
+
1631
+ if (!pages)
1632
+ return NULL;
1633
+
1634
+ if (count)
1635
+ *count = 0;
1636
+
1637
+ if (column > 4)
1638
+ goto fail;
1639
+
1640
+ for (size_t x = 0; x < c; x++)
1641
+ {
1642
+ const struct LanguageIdentifier* cur = &language_identifiers[x];
1643
+ if (copyOnMatch(column, filter, cur, &pages[cnt]))
1644
+ cnt++;
1645
+ }
1646
+
1647
+ if (cnt == 0)
1648
+ goto fail;
1649
+
1650
+ if (count)
1651
+ *count = cnt;
1652
+
1653
+ return pages;
1654
+ fail:
1655
+ freerdp_codepages_free(pages);
1656
+ return NULL;
1657
+ }
1658
+
1659
+ void freerdp_codepages_free(RDP_CODEPAGE* pages)
1660
+ {
1661
+ free(pages);
1662
+ }
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/locale/keyboard_sun.c ADDED
@@ -0,0 +1,284 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * Solaris Keyboard Mapping
4
+ *
5
+ * Copyright 2009-2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+
20
+ #include <freerdp/config.h>
21
+
22
+ #include <stdio.h>
23
+ #include <stdlib.h>
24
+ #include <string.h>
25
+ #include <errno.h>
26
+
27
+ #include <winpr/crt.h>
28
+
29
+ #include "liblocale.h"
30
+
31
+ #include <freerdp/locale/keyboard.h>
32
+
33
+ #include "keyboard_x11.h"
34
+
35
+ #include "keyboard_sun.h"
36
+
37
+ /* OpenSolaris 2008.11 and 2009.06 keyboard layouts
38
+ *
39
+ * While OpenSolaris comes with Xorg and XKB, it maintains a set of keyboard layout
40
+ * names that map directly to a particular keyboard layout in XKB. Fortunately for us,
41
+ * this way of doing things comes from Solaris, which is XKB unaware. The same keyboard
42
+ * layout naming system is used in Solaris, so we can use the same XKB configuration as
43
+ * we would on OpenSolaris and get an accurate keyboard layout detection :)
44
+ *
45
+ * We can check for the current keyboard layout using the "kbd -l" command:
46
+ *
47
+ * type=6
48
+ * layout=33 (0x21)
49
+ * delay(ms)=500
50
+ * rate(ms)=40
51
+ *
52
+ * We can check at runtime if the kbd utility is present, parse the output, and use the
53
+ * keyboard layout indicated by the index given (in this case, 33, or US-English).
54
+ */
55
+
56
+ typedef struct
57
+ {
58
+ UINT32 type; /* Solaris keyboard type */
59
+ UINT32 layout; /* Layout */
60
+ char* xkbType; /* XKB keyboard */
61
+ UINT32 keyboardLayoutId; /* XKB keyboard layout */
62
+ } SOLARIS_KEYBOARD;
63
+
64
+ static const SOLARIS_KEYBOARD SOLARIS_KEYBOARD_TABLE[] = {
65
+ { 4, 0, "sun(type4)", KBD_US }, /* US4 */
66
+ { 4, 1, "sun(type4)", KBD_US }, /* US4 */
67
+ { 4, 2, "sun(type4tuv)", KBD_FRENCH }, /* FranceBelg4 */
68
+ { 4, 3, "sun(type4_ca)", KBD_US }, /* Canada4 */
69
+ { 4, 4, "sun(type4tuv)", KBD_DANISH }, /* Denmark4 */
70
+ { 4, 5, "sun(type4tuv)", KBD_GERMAN }, /* Germany4 */
71
+ { 4, 6, "sun(type4tuv)", KBD_ITALIAN }, /* Italy4 */
72
+ { 4, 7, "sun(type4tuv)", KBD_DUTCH }, /* Netherland4 */
73
+ { 4, 8, "sun(type4tuv)", KBD_NORWEGIAN }, /* Norway4 */
74
+ { 4, 9, "sun(type4tuv)", KBD_PORTUGUESE }, /* Portugal4 */
75
+ { 4, 10, "sun(type4tuv)", KBD_SPANISH }, /* SpainLatAm4 */
76
+ { 4, 11, "sun(type4tuv)", KBD_SWEDISH }, /* SwedenFin4 */
77
+ { 4, 12, "sun(type4tuv)", KBD_SWISS_FRENCH }, /* Switzer_Fr4 */
78
+ { 4, 13, "sun(type4tuv)", KBD_SWISS_GERMAN }, /* Switzer_Ge4 */
79
+ { 4, 14, "sun(type4tuv)", KBD_UNITED_KINGDOM }, /* UK4 */
80
+ { 4, 16, "sun(type4)", KBD_KOREAN_INPUT_SYSTEM_IME_2000 }, /* Korea4 */
81
+ { 4, 17, "sun(type4)", KBD_CHINESE_TRADITIONAL_PHONETIC }, /* Taiwan4 */
82
+ { 4, 32, "sun(type4jp)", KBD_JAPANESE_INPUT_SYSTEM_MS_IME2002 }, /* Japan4 */
83
+ { 4, 19, "sun(type5)", KBD_US }, /* US101A_PC */
84
+ { 4, 33, "sun(type5)", KBD_US }, /* US5 */
85
+ { 4, 34, "sun(type5unix)", KBD_US }, /* US_UNIX5 */
86
+ { 4, 35, "sun(type5tuv)", KBD_FRENCH }, /* France5 */
87
+ { 4, 36, "sun(type5tuv)", KBD_DANISH }, /* Denmark5 */
88
+ { 4, 37, "sun(type5tuv)", KBD_GERMAN }, /* Germany5 */
89
+ { 4, 38, "sun(type5tuv)", KBD_ITALIAN }, /* Italy5 */
90
+ { 4, 39, "sun(type5tuv)", KBD_DUTCH }, /* Netherland5 */
91
+ { 4, 40, "sun(type5tuv)", KBD_NORWEGIAN }, /* Norway5 */
92
+ { 4, 41, "sun(type5tuv)", KBD_PORTUGUESE }, /* Portugal5 */
93
+ { 4, 42, "sun(type5tuv)", KBD_SPANISH }, /* Spain5 */
94
+ { 4, 43, "sun(type5tuv)", KBD_SWEDISH }, /* Sweden5 */
95
+ { 4, 44, "sun(type5tuv)", KBD_SWISS_FRENCH }, /* Switzer_Fr5 */
96
+ { 4, 45, "sun(type5tuv)", KBD_SWISS_GERMAN }, /* Switzer_Ge5 */
97
+ { 4, 46, "sun(type5tuv)", KBD_UNITED_KINGDOM }, /* UK5 */
98
+ { 4, 47, "sun(type5)", KBD_KOREAN_INPUT_SYSTEM_IME_2000 }, /* Korea5 */
99
+ { 4, 48, "sun(type5)", KBD_CHINESE_TRADITIONAL_PHONETIC }, /* Taiwan5 */
100
+ { 4, 49, "sun(type5jp)", KBD_JAPANESE_INPUT_SYSTEM_MS_IME2002 }, /* Japan5 */
101
+ { 4, 50, "sun(type5tuv)", KBD_CANADIAN_FRENCH }, /* Canada_Fr5 */
102
+ { 4, 51, "sun(type5tuv)", KBD_HUNGARIAN }, /* Hungary5 */
103
+ { 4, 52, "sun(type5tuv)", KBD_POLISH_214 }, /* Poland5 */
104
+ { 4, 53, "sun(type5tuv)", KBD_CZECH }, /* Czech5 */
105
+ { 4, 54, "sun(type5tuv)", KBD_RUSSIAN }, /* Russia5 */
106
+ { 4, 55, "sun(type5tuv)", KBD_LATVIAN }, /* Latvia5 */
107
+ { 4, 57, "sun(type5tuv)", KBD_GREEK }, /* Greece5 */
108
+ { 4, 59, "sun(type5tuv)", KBD_LITHUANIAN }, /* Lithuania5 */
109
+ { 4, 63, "sun(type5tuv)", KBD_CANADIAN_FRENCH }, /* Canada_Fr5_TBITS5 */
110
+ { 4, 56, "sun(type5tuv)", KBD_TURKISH_Q }, /* TurkeyQ5 */
111
+ { 4, 58, "sun(type5tuv)", KBD_ARABIC_101 }, /* Arabic5 */
112
+ { 4, 60, "sun(type5tuv)", KBD_BELGIAN_FRENCH }, /* Belgian5 */
113
+ { 4, 62, "sun(type5tuv)", KBD_TURKISH_F }, /* TurkeyF5 */
114
+ { 4, 80, "sun(type5hobo)", KBD_US }, /* US5_Hobo */
115
+ { 4, 81, "sun(type5hobo)", KBD_US }, /* US_UNIX5_Hobo */
116
+ { 4, 82, "sun(type5tuvhobo)", KBD_FRENCH }, /* France5_Hobo */
117
+ { 4, 83, "sun(type5tuvhobo)", KBD_DANISH }, /* Denmark5_Hobo */
118
+ { 4, 84, "sun(type5tuvhobo)", KBD_GERMAN }, /* Germany5_Hobo */
119
+ { 4, 85, "sun(type5tuvhobo)", KBD_ITALIAN }, /* Italy5_Hobo */
120
+ { 4, 86, "sun(type5tuvhobo)", KBD_DUTCH }, /* Netherland5_Hobo */
121
+ { 4, 87, "sun(type5tuvhobo)", KBD_NORWEGIAN }, /* Norway5_Hobo */
122
+ { 4, 88, "sun(type5tuvhobo)", KBD_PORTUGUESE }, /* Portugal5_Hobo */
123
+ { 4, 89, "sun(type5tuvhobo)", KBD_SPANISH }, /* Spain5_Hobo */
124
+ { 4, 90, "sun(type5tuvhobo)", KBD_SWEDISH }, /* Sweden5_Hobo */
125
+ { 4, 91, "sun(type5tuvhobo)", KBD_SWISS_FRENCH }, /* Switzer_Fr5_Hobo */
126
+ { 4, 92, "sun(type5tuvhobo)", KBD_SWISS_GERMAN }, /* Switzer_Ge5_Hobo */
127
+ { 4, 93, "sun(type5tuvhobo)", KBD_UNITED_KINGDOM }, /* UK5_Hobo */
128
+ { 4, 94, "sun(type5hobo)", KBD_KOREAN_INPUT_SYSTEM_IME_2000 }, /* Korea5_Hobo */
129
+ { 4, 95, "sun(type5hobo)", KBD_CHINESE_TRADITIONAL_PHONETIC }, /* Taiwan5_Hobo */
130
+ { 4, 96, "sun(type5jphobo)", KBD_JAPANESE_INPUT_SYSTEM_MS_IME2002 }, /* Japan5_Hobo */
131
+ { 4, 97, "sun(type5tuvhobo)", KBD_CANADIAN_FRENCH }, /* Canada_Fr5_Hobo */
132
+ { 101, 1, "digital_vndr/pc(pc104)", KBD_US }, /* US101A_x86 */
133
+ { 101, 34, "digital_vndr/pc(pc104)", KBD_US }, /* J3100_x86 */
134
+ { 101, 35, "digital_vndr/pc(pc104)", KBD_FRENCH }, /* France_x86 */
135
+ { 101, 36, "digital_vndr/pc(pc104)", KBD_DANISH }, /* Denmark_x86 */
136
+ { 101, 37, "digital_vndr/pc(pc104)", KBD_GERMAN }, /* Germany_x86 */
137
+ { 101, 38, "digital_vndr/pc(pc104)", KBD_ITALIAN }, /* Italy_x86 */
138
+ { 101, 39, "digital_vndr/pc(pc104)", KBD_DUTCH }, /* Netherland_x86 */
139
+ { 101, 40, "digital_vndr/pc(pc104)", KBD_NORWEGIAN }, /* Norway_x86 */
140
+ { 101, 41, "digital_vndr/pc(pc104)", KBD_PORTUGUESE }, /* Portugal_x86 */
141
+ { 101, 42, "digital_vndr/pc(pc104)", KBD_SPANISH }, /* Spain_x86 */
142
+ { 101, 43, "digital_vndr/pc(pc104)", KBD_SWEDISH }, /* Sweden_x86 */
143
+ { 101, 44, "digital_vndr/pc(pc104)", KBD_SWISS_FRENCH }, /* Switzer_Fr_x86 */
144
+ { 101, 45, "digital_vndr/pc(pc104)", KBD_SWISS_GERMAN }, /* Switzer_Ge_x86 */
145
+ { 101, 46, "digital_vndr/pc(pc104)", KBD_UNITED_KINGDOM }, /* UK_x86 */
146
+ { 101, 47, "digital_vndr/pc(pc104)", KBD_KOREAN_INPUT_SYSTEM_IME_2000 }, /* Korea_x86 */
147
+ { 101, 48, "digital_vndr/pc(pc104)", KBD_CHINESE_TRADITIONAL_PHONETIC }, /* Taiwan_x86 */
148
+ { 101, 49, "digital_vndr/pc(lk411jj)", KBD_JAPANESE_INPUT_SYSTEM_MS_IME2002 }, /* Japan_x86 */
149
+ { 101, 50, "digital_vndr/pc(pc104)", KBD_CANADIAN_FRENCH }, /* Canada_Fr2_x86 */
150
+ { 101, 51, "digital_vndr/pc(pc104)", KBD_HUNGARIAN }, /* Hungary_x86 */
151
+ { 101, 52, "digital_vndr/pc(pc104)", KBD_POLISH_214 }, /* Poland_x86 */
152
+ { 101, 53, "digital_vndr/pc(pc104)", KBD_CZECH }, /* Czech_x86 */
153
+ { 101, 54, "digital_vndr/pc(pc104)", KBD_RUSSIAN }, /* Russia_x86 */
154
+ { 101, 55, "digital_vndr/pc(pc104)", KBD_LATVIAN }, /* Latvia_x86 */
155
+ { 101, 56, "digital_vndr/pc(pc104)", KBD_TURKISH_Q }, /* Turkey_x86 */
156
+ { 101, 57, "digital_vndr/pc(pc104)", KBD_GREEK }, /* Greece_x86 */
157
+ { 101, 59, "digital_vndr/pc(pc104)", KBD_LITHUANIAN }, /* Lithuania_x86 */
158
+ { 101, 1001, "digital_vndr/pc(pc104)", KBD_US }, /* MS_US101A_x86 */
159
+ { 6, 6, "sun(type6tuv)", KBD_DANISH }, /* Denmark6_usb */
160
+ { 6, 7, "sun(type6tuv)", KBD_FINNISH }, /* Finnish6_usb */
161
+ { 6, 8, "sun(type6tuv)", KBD_FRENCH }, /* France6_usb */
162
+ { 6, 9, "sun(type6tuv)", KBD_GERMAN }, /* Germany6_usb */
163
+ { 6, 14, "sun(type6tuv)", KBD_ITALIAN }, /* Italy6_usb */
164
+ { 6, 15, "sun(type6jp)", KBD_JAPANESE_INPUT_SYSTEM_MS_IME2002 }, /* Japan7_usb */
165
+ { 6, 16, "sun(type6)", KBD_KOREAN_INPUT_SYSTEM_IME_2000 }, /* Korea6_usb */
166
+ { 6, 18, "sun(type6tuv)", KBD_DUTCH }, /* Netherland6_usb */
167
+ { 6, 19, "sun(type6tuv)", KBD_NORWEGIAN }, /* Norway6_usb */
168
+ { 6, 22, "sun(type6tuv)", KBD_PORTUGUESE }, /* Portugal6_usb */
169
+ { 6, 23, "sun(type6tuv)", KBD_RUSSIAN }, /* Russia6_usb */
170
+ { 6, 25, "sun(type6tuv)", KBD_SPANISH }, /* Spain6_usb */
171
+ { 6, 26, "sun(type6tuv)", KBD_SWEDISH }, /* Sweden6_usb */
172
+ { 6, 27, "sun(type6tuv)", KBD_SWISS_FRENCH }, /* Switzer_Fr6_usb */
173
+ { 6, 28, "sun(type6tuv)", KBD_SWISS_GERMAN }, /* Switzer_Ge6_usb */
174
+ { 6, 30, "sun(type6)", KBD_CHINESE_TRADITIONAL_PHONETIC }, /* Taiwan6_usb */
175
+ { 6, 32, "sun(type6tuv)", KBD_UNITED_KINGDOM }, /* UK6_usb */
176
+ { 6, 33, "sun(type6)", KBD_US }, /* US6_usb */
177
+ { 6, 1, "sun(type6tuv)", KBD_ARABIC_101 }, /* Arabic6_usb */
178
+ { 6, 2, "sun(type6tuv)", KBD_BELGIAN_FRENCH }, /* Belgian6_usb */
179
+ { 6, 31, "sun(type6tuv)", KBD_TURKISH_Q }, /* TurkeyQ6_usb */
180
+ { 6, 35, "sun(type6tuv)", KBD_TURKISH_F }, /* TurkeyF6_usb */
181
+ { 6, 271, "sun(type6jp)", KBD_JAPANESE_INPUT_SYSTEM_MS_IME2002 }, /* Japan6_usb */
182
+ { 6, 264, "sun(type6tuv)", KBD_ALBANIAN }, /* Albanian6_usb */
183
+ { 6, 261, "sun(type6tuv)", KBD_BELARUSIAN }, /* Belarusian6_usb */
184
+ { 6, 260, "sun(type6tuv)", KBD_BULGARIAN }, /* Bulgarian6_usb */
185
+ { 6, 259, "sun(type6tuv)", KBD_CROATIAN }, /* Croatian6_usb */
186
+ { 6, 5, "sun(type6tuv)", KBD_CZECH }, /* Czech6_usb */
187
+ { 6, 4, "sun(type6tuv)", KBD_CANADIAN_FRENCH }, /* French-Canadian6_usb */
188
+ { 6, 12, "sun(type6tuv)", KBD_HUNGARIAN }, /* Hungarian6_usb */
189
+ { 6, 10, "sun(type6tuv)", KBD_GREEK }, /* Greek6_usb */
190
+ { 6, 17, "sun(type6)", KBD_LATIN_AMERICAN }, /* Latin-American6_usb */
191
+ { 6, 265, "sun(type6tuv)", KBD_LITHUANIAN }, /* Lithuanian6_usb */
192
+ { 6, 266, "sun(type6tuv)", KBD_LATVIAN }, /* Latvian6_usb */
193
+ { 6, 267, "sun(type6tuv)", KBD_FYRO_MACEDONIAN }, /* Macedonian6_usb */
194
+ { 6, 263, "sun(type6tuv)", KBD_MALTESE_47_KEY }, /* Malta_UK6_usb */
195
+ { 6, 262, "sun(type6tuv)", KBD_MALTESE_48_KEY }, /* Malta_US6_usb */
196
+ { 6, 21, "sun(type6tuv)", KBD_POLISH_214 }, /* Polish6_usb */
197
+ { 6, 257, "sun(type6tuv)", KBD_SERBIAN_LATIN }, /* Serbia-And-Montenegro6_usb */
198
+ { 6, 256, "sun(type6tuv)", KBD_SLOVENIAN }, /* Slovenian6_usb */
199
+ { 6, 24, "sun(type6tuv)", KBD_SLOVAK }, /* Slovakian6_usb */
200
+ { 6, 3, "sun(type6)", KBD_CANADIAN_MULTILINGUAL_STANDARD }, /* Canada_Bi6_usb */
201
+ { 6, 272, "sun(type6)", KBD_PORTUGUESE_BRAZILIAN_ABNT } /* Brazil6_usb */
202
+ };
203
+
204
+ int freerdp_get_solaris_keyboard_layout_and_type(int* type, int* layout)
205
+ {
206
+ FILE* kbd;
207
+ char* pch;
208
+ char* beg;
209
+ char* end;
210
+ int rc = -1;
211
+ char buffer[1024];
212
+ /*
213
+ Sample output for "kbd -t -l" :
214
+
215
+ USB keyboard
216
+ type=6
217
+ layout=3 (0x03)
218
+ delay(ms)=500
219
+ rate(ms)=40
220
+ */
221
+ *type = 0;
222
+ *layout = 0;
223
+ kbd = popen("kbd -t -l", "r");
224
+
225
+ if (!kbd)
226
+ return -1;
227
+
228
+ while (fgets(buffer, sizeof(buffer), kbd) != NULL)
229
+ {
230
+ long val;
231
+
232
+ if ((pch = strstr(buffer, "type=")) != NULL)
233
+ {
234
+ beg = pch + sizeof("type=") - 1;
235
+ end = strchr(beg, '\n');
236
+ end[0] = '\0';
237
+ errno = 0;
238
+ val = strtol(beg, NULL, 0);
239
+
240
+ if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
241
+ goto fail;
242
+
243
+ *type = val;
244
+ }
245
+ else if ((pch = strstr(buffer, "layout=")) != NULL)
246
+ {
247
+ beg = pch + sizeof("layout=") - 1;
248
+ end = strchr(beg, ' ');
249
+ end[0] = '\0';
250
+ errno = 0;
251
+ val = strtol(beg, NULL, 0);
252
+
253
+ if ((errno != 0) || (val < INT32_MIN) || (val > INT32_MAX))
254
+ goto fail;
255
+
256
+ *layout = val;
257
+ }
258
+ }
259
+
260
+ rc = 0;
261
+ fail:
262
+ pclose(kbd);
263
+ return rc;
264
+ }
265
+
266
+ DWORD freerdp_detect_solaris_keyboard_layout()
267
+ {
268
+ int type;
269
+ int layout;
270
+
271
+ if (freerdp_get_solaris_keyboard_layout_and_type(&type, &layout) < 0)
272
+ return 0;
273
+
274
+ for (size_t i = 0; i < ARRAYSIZE(SOLARIS_KEYBOARD_TABLE); i++)
275
+ {
276
+ if (SOLARIS_KEYBOARD_TABLE[i].type == type)
277
+ {
278
+ if (SOLARIS_KEYBOARD_TABLE[i].layout == layout)
279
+ return SOLARIS_KEYBOARD_TABLE[i].keyboardLayoutId;
280
+ }
281
+ }
282
+
283
+ return 0;
284
+ }
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/locale/keyboard_sun.h ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * Solaris Keyboard Mapping
4
+ *
5
+ * Copyright 2009-2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+
20
+ #ifndef FREERDP_LOCALE_KEYBOARD_SUN_H
21
+ #define FREERDP_LOCALE_KEYBOARD_SUN_H
22
+
23
+ #include <freerdp/api.h>
24
+
25
+ FREERDP_LOCAL DWORD freerdp_detect_solaris_keyboard_layout();
26
+
27
+ #endif /* FREERDP_LOCALE_KEYBOARD_SUN_H */
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/locale/keyboard_x11.c ADDED
@@ -0,0 +1,138 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * X11 Keyboard Mapping
4
+ *
5
+ * Copyright 2009-2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
+ * Copyright 2023 Bernhard Miklautz <bernhard.miklautz@thincast.com>
7
+ *
8
+ * Licensed under the Apache License, Version 2.0 (the "License");
9
+ * you may not use this file except in compliance with the License.
10
+ * You may obtain a copy of the License at
11
+ *
12
+ * http://www.apache.org/licenses/LICENSE-2.0
13
+ *
14
+ * Unless required by applicable law or agreed to in writing, software
15
+ * distributed under the License is distributed on an "AS IS" BASIS,
16
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ * See the License for the specific language governing permissions and
18
+ * limitations under the License.
19
+ */
20
+
21
+ #include <string.h>
22
+
23
+ #include <X11/X.h>
24
+ #include <X11/Xatom.h>
25
+ #include <X11/Xlib.h>
26
+
27
+ #include "liblocale.h"
28
+ #include "keyboard_x11.h"
29
+ #include "xkb_layout_ids.h"
30
+
31
+ static BOOL parse_xkb_rule_names(char* xkb_rule, unsigned long num_bytes, char** layout,
32
+ char** variant)
33
+ {
34
+ /* Sample output for "Canadian Multilingual Standard"
35
+ *
36
+ * _XKB_RULES_NAMES_BACKUP(STRING) = "xorg", "pc105", "ca", "multi", "magic"
37
+ *
38
+ * Format: "rules", "model", "layout", "variant", "options"
39
+ *
40
+ * Where "xorg" is the set of rules
41
+ * "pc105" the keyboard model
42
+ * "ca" the keyboard layout(s) (can also be something like 'us,uk')
43
+ * "multi" the keyboard layout variant(s) (in the examples, “,winkeys” - which means first
44
+ * layout uses some “default” variant and second uses “winkeys” variant)
45
+ * "magic" - configuration option (in the examples,
46
+ * “eurosign:e,lv3:ralt_switch,grp:rctrl_toggle”
47
+ * - three options)
48
+ */
49
+ for (size_t i = 0, index = 0; i < num_bytes; i++, index++)
50
+ {
51
+ char* ptr = xkb_rule + i;
52
+
53
+ switch (index)
54
+ {
55
+ case 0: // rules
56
+ break;
57
+ case 1: // model
58
+ break;
59
+ case 2: // layout
60
+ {
61
+ /* If multiple languages are present we just take the first one */
62
+ char* delimiter = strchr(ptr, ',');
63
+ if (delimiter)
64
+ *delimiter = '\0';
65
+ *layout = ptr;
66
+ break;
67
+ }
68
+ case 3: // variant
69
+ *variant = ptr;
70
+ break;
71
+ case 4: // option
72
+ break;
73
+ default:
74
+ break;
75
+ }
76
+ i += strlen(ptr);
77
+ }
78
+ return TRUE;
79
+ }
80
+
81
+ static DWORD kbd_layout_id_from_x_property(Display* display, Window root, char* property_name)
82
+ {
83
+ char* layout = NULL;
84
+ char* variant = NULL;
85
+ char* rule = NULL;
86
+ Atom type = None;
87
+ int item_size = 0;
88
+ unsigned long items = 0;
89
+ unsigned long unread_items = 0;
90
+ DWORD layout_id = 0;
91
+
92
+ Atom property = XInternAtom(display, property_name, False);
93
+ if (property == None)
94
+ return 0;
95
+
96
+ if (XGetWindowProperty(display, root, property, 0, 1024, False, XA_STRING, &type, &item_size,
97
+ &items, &unread_items, (unsigned char**)&rule) != Success)
98
+ return 0;
99
+
100
+ if (type != XA_STRING || item_size != 8 || unread_items != 0)
101
+ {
102
+ XFree(rule);
103
+ return 0;
104
+ }
105
+
106
+ parse_xkb_rule_names(rule, items, &layout, &variant);
107
+
108
+ DEBUG_KBD("%s layout: %s, variant: %s", property_name, layout, variant);
109
+ layout_id = find_keyboard_layout_in_xorg_rules(layout, variant);
110
+
111
+ XFree(rule);
112
+
113
+ return layout_id;
114
+ }
115
+
116
+ int freerdp_detect_keyboard_layout_from_xkb(DWORD* keyboardLayoutId)
117
+ {
118
+ Display* display = XOpenDisplay(NULL);
119
+
120
+ if (!display)
121
+ return 0;
122
+
123
+ Window root = DefaultRootWindow(display);
124
+ if (!root)
125
+ return 0;
126
+
127
+ /* We start by looking for _XKB_RULES_NAMES_BACKUP which appears to be used by libxklavier */
128
+ DWORD id = kbd_layout_id_from_x_property(display, root, "_XKB_RULES_NAMES_BACKUP");
129
+
130
+ if (0 == id)
131
+ id = kbd_layout_id_from_x_property(display, root, "_XKB_RULES_NAMES");
132
+
133
+ if (0 != id)
134
+ *keyboardLayoutId = id;
135
+
136
+ XCloseDisplay(display);
137
+ return (int)id;
138
+ }
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/locale/keyboard_x11.h ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * X11 Keyboard Mapping
4
+ *
5
+ * Copyright 2009-2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+
20
+ #ifndef FREERDP_LOCALE_KEYBOARD_X11_H
21
+ #define FREERDP_LOCALE_KEYBOARD_X11_H
22
+
23
+ #include <freerdp/api.h>
24
+
25
+ FREERDP_LOCAL int freerdp_detect_keyboard_layout_from_xkb(DWORD* keyboardLayoutId);
26
+
27
+ #endif /* FREERDP_LOCALE_KEYBOARD_X11_H */
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/locale/keyboard_xkbfile.c ADDED
@@ -0,0 +1,510 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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+ * XKB Keyboard Mapping
4
+ *
5
+ * Copyright 2009-2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+
20
+ #include <freerdp/config.h>
21
+
22
+ #include "keyboard_xkbfile.h"
23
+
24
+ #include <stdio.h>
25
+ #include <stdlib.h>
26
+ #include <string.h>
27
+
28
+ #include <winpr/crt.h>
29
+ #include <winpr/input.h>
30
+
31
+ #include <freerdp/locale/keyboard.h>
32
+
33
+ #include "keyboard_x11.h"
34
+ #include "xkb_layout_ids.h"
35
+ #include "liblocale.h"
36
+
37
+ #include <X11/X.h>
38
+ #include <X11/Xlib.h>
39
+ #include <X11/XKBlib.h>
40
+ #include <X11/extensions/XKBfile.h>
41
+ #include <X11/extensions/XKBrules.h>
42
+
43
+ typedef struct
44
+ {
45
+ const char* xkb_keyname; /* XKB keyname */
46
+ DWORD rdp_scancode;
47
+ } XKB_KEY_NAME_SCANCODE;
48
+
49
+ static const XKB_KEY_NAME_SCANCODE XKB_KEY_NAME_SCANCODE_TABLE[] = {
50
+ { "", RDP_SCANCODE_UNKNOWN }, /* 008: [(null)] */
51
+ { "ESC", RDP_SCANCODE_ESCAPE }, /* 009: ESC [Escape] */
52
+ { "AE01", RDP_SCANCODE_KEY_1 }, /* 010: AE01 [1] */
53
+ { "AE02", RDP_SCANCODE_KEY_2 }, /* 011: AE02 [2] */
54
+ { "AE03", RDP_SCANCODE_KEY_3 }, /* 012: AE03 [3] */
55
+ { "AE04", RDP_SCANCODE_KEY_4 }, /* 013: AE04 [4] */
56
+ { "AE05", RDP_SCANCODE_KEY_5 }, /* 014: AE05 [5] */
57
+ { "AE06", RDP_SCANCODE_KEY_6 }, /* 015: AE06 [6] */
58
+ { "AE07", RDP_SCANCODE_KEY_7 }, /* 016: AE07 [7] */
59
+ { "AE08", RDP_SCANCODE_KEY_8 }, /* 017: AE08 [8] */
60
+ { "AE09", RDP_SCANCODE_KEY_9 }, /* 018: AE09 [9] */
61
+ { "AE10", RDP_SCANCODE_KEY_0 }, /* 019: AE10 [0] */
62
+ { "AE11", RDP_SCANCODE_OEM_MINUS }, /* 020: AE11 [minus] */
63
+ { "AE12", RDP_SCANCODE_OEM_PLUS }, /* 021: AE12 [equal] */
64
+ { "BKSP", RDP_SCANCODE_BACKSPACE }, /* 022: BKSP [BackSpace] */
65
+ { "TAB", RDP_SCANCODE_TAB }, /* 023: TAB [Tab] */
66
+ { "AD01", RDP_SCANCODE_KEY_Q }, /* 024: AD01 [q] */
67
+ { "AD02", RDP_SCANCODE_KEY_W }, /* 025: AD02 [w] */
68
+ { "AD03", RDP_SCANCODE_KEY_E }, /* 026: AD03 [e] */
69
+ { "AD04", RDP_SCANCODE_KEY_R }, /* 027: AD04 [r] */
70
+ { "AD05", RDP_SCANCODE_KEY_T }, /* 028: AD05 [t] */
71
+ { "AD06", RDP_SCANCODE_KEY_Y }, /* 029: AD06 [y] */
72
+ { "AD07", RDP_SCANCODE_KEY_U }, /* 030: AD07 [u] */
73
+ { "AD08", RDP_SCANCODE_KEY_I }, /* 031: AD08 [i] */
74
+ { "AD09", RDP_SCANCODE_KEY_O }, /* 032: AD09 [o] */
75
+ { "AD10", RDP_SCANCODE_KEY_P }, /* 033: AD10 [p] */
76
+ { "AD11", RDP_SCANCODE_OEM_4 }, /* 034: AD11 [bracketleft] */
77
+ { "AD12", RDP_SCANCODE_OEM_6 }, /* 035: AD12 [bracketright] */
78
+ { "RTRN", RDP_SCANCODE_RETURN }, /* 036: RTRN [Return] */
79
+ { "LCTL", RDP_SCANCODE_LCONTROL }, /* 037: LCTL [Control_L] */
80
+ { "AC01", RDP_SCANCODE_KEY_A }, /* 038: AC01 [a] */
81
+ { "AC02", RDP_SCANCODE_KEY_S }, /* 039: AC02 [s] */
82
+ { "AC03", RDP_SCANCODE_KEY_D }, /* 040: AC03 [d] */
83
+ { "AC04", RDP_SCANCODE_KEY_F }, /* 041: AC04 [f] */
84
+ { "AC05", RDP_SCANCODE_KEY_G }, /* 042: AC05 [g] */
85
+ { "AC06", RDP_SCANCODE_KEY_H }, /* 043: AC06 [h] */
86
+ { "AC07", RDP_SCANCODE_KEY_J }, /* 044: AC07 [j] */
87
+ { "AC08", RDP_SCANCODE_KEY_K }, /* 045: AC08 [k] */
88
+ { "AC09", RDP_SCANCODE_KEY_L }, /* 046: AC09 [l] */
89
+ { "AC10", RDP_SCANCODE_OEM_1 }, /* 047: AC10 [semicolon] */
90
+ { "AC11", RDP_SCANCODE_OEM_7 }, /* 048: AC11 [dead_acute] */
91
+ { "TLDE", RDP_SCANCODE_OEM_3 }, /* 049: TLDE [dead_grave] */
92
+ { "LFSH", RDP_SCANCODE_LSHIFT }, /* 050: LFSH [Shift_L] */
93
+ { "BKSL", RDP_SCANCODE_OEM_5 }, /* 051: BKSL [backslash] */
94
+ { "AB01", RDP_SCANCODE_KEY_Z }, /* 052: AB01 [z] */
95
+ { "AB02", RDP_SCANCODE_KEY_X }, /* 053: AB02 [x] */
96
+ { "AB03", RDP_SCANCODE_KEY_C }, /* 054: AB03 [c] */
97
+ { "AB04", RDP_SCANCODE_KEY_V }, /* 055: AB04 [v] */
98
+ { "AB05", RDP_SCANCODE_KEY_B }, /* 056: AB05 [b] */
99
+ { "AB06", RDP_SCANCODE_KEY_N }, /* 057: AB06 [n] */
100
+ { "AB07", RDP_SCANCODE_KEY_M }, /* 058: AB07 [m] */
101
+ { "AB08", RDP_SCANCODE_OEM_COMMA }, /* 059: AB08 [comma] */
102
+ { "AB09", RDP_SCANCODE_OEM_PERIOD }, /* 060: AB09 [period] */
103
+ { "AB10", RDP_SCANCODE_OEM_2 }, /* 061: AB10 [slash] */
104
+ { "RTSH", RDP_SCANCODE_RSHIFT }, /* 062: RTSH [Shift_R] */
105
+ { "KPMU", RDP_SCANCODE_MULTIPLY }, /* 063: KPMU [KP_Multiply] */
106
+ { "LALT", RDP_SCANCODE_LMENU }, /* 064: LALT [Alt_L] */
107
+ { "SPCE", RDP_SCANCODE_SPACE }, /* 065: SPCE [space] */
108
+ { "CAPS", RDP_SCANCODE_CAPSLOCK }, /* 066: CAPS [Caps_Lock] */
109
+ { "FK01", RDP_SCANCODE_F1 }, /* 067: FK01 [F1] */
110
+ { "FK02", RDP_SCANCODE_F2 }, /* 068: FK02 [F2] */
111
+ { "FK03", RDP_SCANCODE_F3 }, /* 069: FK03 [F3] */
112
+ { "FK04", RDP_SCANCODE_F4 }, /* 070: FK04 [F4] */
113
+ { "FK05", RDP_SCANCODE_F5 }, /* 071: FK05 [F5] */
114
+ { "FK06", RDP_SCANCODE_F6 }, /* 072: FK06 [F6] */
115
+ { "FK07", RDP_SCANCODE_F7 }, /* 073: FK07 [F7] */
116
+ { "FK08", RDP_SCANCODE_F8 }, /* 074: FK08 [F8] */
117
+ { "FK09", RDP_SCANCODE_F9 }, /* 075: FK09 [F9] */
118
+ { "FK10", RDP_SCANCODE_F10 }, /* 076: FK10 [F10] */
119
+ { "NMLK", RDP_SCANCODE_NUMLOCK }, /* 077: NMLK [Num_Lock] */
120
+ { "SCLK", RDP_SCANCODE_SCROLLLOCK }, /* 078: SCLK [Multi_key] */
121
+ { "KP7", RDP_SCANCODE_NUMPAD7 }, /* 079: KP7 [KP_Home] */
122
+ { "KP8", RDP_SCANCODE_NUMPAD8 }, /* 080: KP8 [KP_Up] */
123
+ { "KP9", RDP_SCANCODE_NUMPAD9 }, /* 081: KP9 [KP_Prior] */
124
+ { "KPSU", RDP_SCANCODE_SUBTRACT }, /* 082: KPSU [KP_Subtract] */
125
+ { "KP4", RDP_SCANCODE_NUMPAD4 }, /* 083: KP4 [KP_Left] */
126
+ { "KP5", RDP_SCANCODE_NUMPAD5 }, /* 084: KP5 [KP_Begin] */
127
+ { "KP6", RDP_SCANCODE_NUMPAD6 }, /* 085: KP6 [KP_Right] */
128
+ { "KPAD", RDP_SCANCODE_ADD }, /* 086: KPAD [KP_Add] */
129
+ { "KP1", RDP_SCANCODE_NUMPAD1 }, /* 087: KP1 [KP_End] */
130
+ { "KP2", RDP_SCANCODE_NUMPAD2 }, /* 088: KP2 [KP_Down] */
131
+ { "KP3", RDP_SCANCODE_NUMPAD3 }, /* 089: KP3 [KP_Next] */
132
+ { "KP0", RDP_SCANCODE_NUMPAD0 }, /* 090: KP0 [KP_Insert] */
133
+ { "KPDL", RDP_SCANCODE_DECIMAL }, /* 091: KPDL [KP_Delete] */
134
+ { "LVL3", RDP_SCANCODE_RMENU }, /* 092: LVL3 [ISO_Level3_Shift] */
135
+ { "", RDP_SCANCODE_UNKNOWN }, /* 093: [(null)] */
136
+ { "LSGT", RDP_SCANCODE_OEM_102 }, /* 094: LSGT [backslash] */
137
+ { "FK11", RDP_SCANCODE_F11 }, /* 095: FK11 [F11] */
138
+ { "FK12", RDP_SCANCODE_F12 }, /* 096: FK12 [F12] */
139
+ { "AB11", RDP_SCANCODE_ABNT_C1 }, /* 097: AB11 [(null)] */
140
+ { "KATA", RDP_SCANCODE_KANA_HANGUL }, /* 098: KATA [Katakana] */
141
+ { "HIRA", RDP_SCANCODE_HIRAGANA }, /* 099: HIRA [Hiragana] */
142
+ { "HENK", RDP_SCANCODE_CONVERT_JP }, /* 100: HENK [Henkan_Mode] */
143
+ { "HKTG", RDP_SCANCODE_HIRAGANA }, /* 101: HKTG [Hiragana_Katakana] */
144
+ { "MUHE", RDP_SCANCODE_NONCONVERT_JP }, /* 102: MUHE [Muhenkan] */
145
+ { "JPCM", RDP_SCANCODE_UNKNOWN }, /* 103: JPCM [(null)] */
146
+ { "KPEN", RDP_SCANCODE_RETURN_KP }, /* 104: KPEN [KP_Enter] */
147
+ { "RCTL", RDP_SCANCODE_RCONTROL }, /* 105: RCTL [Control_R] */
148
+ { "KPDV", RDP_SCANCODE_DIVIDE }, /* 106: KPDV [KP_Divide] */
149
+ { "PRSC", RDP_SCANCODE_PRINTSCREEN }, /* 107: PRSC [Print] */
150
+ { "RALT", RDP_SCANCODE_RMENU }, /* 108: RALT [ISO_Level3_Shift] */
151
+ { "LNFD", RDP_SCANCODE_UNKNOWN }, /* 109: LNFD [Linefeed] */
152
+ { "HOME", RDP_SCANCODE_HOME }, /* 110: HOME [Home] */
153
+ { "UP", RDP_SCANCODE_UP }, /* 111: UP [Up] */
154
+ { "PGUP", RDP_SCANCODE_PRIOR }, /* 112: PGUP [Prior] */
155
+ { "LEFT", RDP_SCANCODE_LEFT }, /* 113: LEFT [Left] */
156
+ { "RGHT", RDP_SCANCODE_RIGHT }, /* 114: RGHT [Right] */
157
+ { "END", RDP_SCANCODE_END }, /* 115: END [End] */
158
+ { "DOWN", RDP_SCANCODE_DOWN }, /* 116: DOWN [Down] */
159
+ { "PGDN", RDP_SCANCODE_NEXT }, /* 117: PGDN [Next] */
160
+ { "INS", RDP_SCANCODE_INSERT }, /* 118: INS [Insert] */
161
+ { "DELE", RDP_SCANCODE_DELETE }, /* 119: DELE [Delete] */
162
+ { "I120", RDP_SCANCODE_UNKNOWN }, /* 120: I120 [(null)] */
163
+ { "MUTE", RDP_SCANCODE_VOLUME_MUTE }, /* 121: MUTE [XF86AudioMute] */
164
+ { "VOL-", RDP_SCANCODE_VOLUME_DOWN }, /* 122: VOL- [XF86AudioLowerVolume] */
165
+ { "VOL+", RDP_SCANCODE_VOLUME_UP }, /* 123: VOL+ [XF86AudioRaiseVolume] */
166
+ { "POWR", RDP_SCANCODE_UNKNOWN }, /* 124: POWR [XF86PowerOff] */
167
+ { "KPEQ", RDP_SCANCODE_UNKNOWN }, /* 125: KPEQ [KP_Equal] */
168
+ { "I126", RDP_SCANCODE_UNKNOWN }, /* 126: I126 [plusminus] */
169
+ { "PAUS", RDP_SCANCODE_PAUSE }, /* 127: PAUS [Pause] */
170
+ { "I128", RDP_SCANCODE_LAUNCH_MEDIA_SELECT }, /* 128: I128 [XF86LaunchA] */
171
+ { "I129", RDP_SCANCODE_ABNT_C2 }, /* 129: I129 [KP_Decimal] */
172
+ { "HNGL", RDP_SCANCODE_HANGUL }, /* 130: HNGL [Hangul] */
173
+ { "HJCV", RDP_SCANCODE_HANJA }, /* 131: HJCV [Hangul_Hanja] */
174
+ { "AE13", RDP_SCANCODE_BACKSLASH_JP }, /* 132: AE13 [(null)] */
175
+ { "LWIN", RDP_SCANCODE_LWIN }, /* 133: LWIN [Super_L] */
176
+ { "RWIN", RDP_SCANCODE_RWIN }, /* 134: RWIN [Super_R] */
177
+ { "COMP", RDP_SCANCODE_APPS }, /* 135: COMP [Menu] */
178
+ { "STOP", RDP_SCANCODE_BROWSER_STOP }, /* 136: STOP [Cancel] */
179
+ { "AGAI", RDP_SCANCODE_UNKNOWN }, /* 137: AGAI [Redo] */
180
+ { "PROP", RDP_SCANCODE_UNKNOWN }, /* 138: PROP [SunProps] */
181
+ { "UNDO", RDP_SCANCODE_UNKNOWN }, /* 139: UNDO [Undo] */
182
+ { "FRNT", RDP_SCANCODE_UNKNOWN }, /* 140: FRNT [SunFront] */
183
+ { "COPY", RDP_SCANCODE_UNKNOWN }, /* 141: COPY [XF86Copy] */
184
+ { "OPEN", RDP_SCANCODE_UNKNOWN }, /* 142: OPEN [XF86Open] */
185
+ { "PAST", RDP_SCANCODE_UNKNOWN }, /* 143: PAST [XF86Paste] */
186
+ { "FIND", RDP_SCANCODE_UNKNOWN }, /* 144: FIND [Find] */
187
+ { "CUT", RDP_SCANCODE_UNKNOWN }, /* 145: CUT [XF86Cut] */
188
+ { "HELP", RDP_SCANCODE_HELP }, /* 146: HELP [Help] */
189
+ { "I147", RDP_SCANCODE_UNKNOWN }, /* 147: I147 [XF86MenuKB] */
190
+ { "I148", RDP_SCANCODE_UNKNOWN }, /* 148: I148 [XF86Calculator] */
191
+ { "I149", RDP_SCANCODE_UNKNOWN }, /* 149: I149 [(null)] */
192
+ { "I150", RDP_SCANCODE_SLEEP }, /* 150: I150 [XF86Sleep] */
193
+ { "I151", RDP_SCANCODE_UNKNOWN }, /* 151: I151 [XF86WakeUp] */
194
+ { "I152", RDP_SCANCODE_UNKNOWN }, /* 152: I152 [XF86Explorer] */
195
+ { "I153", RDP_SCANCODE_UNKNOWN }, /* 153: I153 [XF86Send] */
196
+ { "I154", RDP_SCANCODE_UNKNOWN }, /* 154: I154 [(null)] */
197
+ { "I155", RDP_SCANCODE_UNKNOWN }, /* 155: I155 [XF86Xfer] */
198
+ { "I156", RDP_SCANCODE_LAUNCH_APP1 }, /* 156: I156 [XF86Launch1] */
199
+ { "I157", RDP_SCANCODE_LAUNCH_APP2 }, /* 157: I157 [XF86Launch2] */
200
+ { "I158", RDP_SCANCODE_BROWSER_HOME }, /* 158: I158 [XF86WWW] */
201
+ { "I159", RDP_SCANCODE_UNKNOWN }, /* 159: I159 [XF86DOS] */
202
+ { "I160", RDP_SCANCODE_UNKNOWN }, /* 160: I160 [XF86ScreenSaver] */
203
+ { "I161", RDP_SCANCODE_UNKNOWN }, /* 161: I161 [XF86RotateWindows] */
204
+ { "I162", RDP_SCANCODE_UNKNOWN }, /* 162: I162 [XF86TaskPane] */
205
+ { "I163", RDP_SCANCODE_LAUNCH_MAIL }, /* 163: I163 [XF86Mail] */
206
+ { "I164", RDP_SCANCODE_BROWSER_FAVORITES }, /* 164: I164 [XF86Favorites] */
207
+ { "I165", RDP_SCANCODE_UNKNOWN }, /* 165: I165 [XF86MyComputer] */
208
+ { "I166", RDP_SCANCODE_BROWSER_BACK }, /* 166: I166 [XF86Back] */
209
+ { "I167", RDP_SCANCODE_BROWSER_FORWARD }, /* 167: I167 [XF86Forward] */
210
+ { "I168", RDP_SCANCODE_UNKNOWN }, /* 168: I168 [(null)] */
211
+ { "I169", RDP_SCANCODE_UNKNOWN }, /* 169: I169 [XF86Eject] */
212
+ { "I170", RDP_SCANCODE_UNKNOWN }, /* 170: I170 [XF86Eject] */
213
+ { "I171", RDP_SCANCODE_MEDIA_NEXT_TRACK }, /* 171: I171 [XF86AudioNext] */
214
+ { "I172", RDP_SCANCODE_MEDIA_PLAY_PAUSE }, /* 172: I172 [XF86AudioPlay] */
215
+ { "I173", RDP_SCANCODE_MEDIA_PREV_TRACK }, /* 173: I173 [XF86AudioPrev] */
216
+ { "I174", RDP_SCANCODE_MEDIA_STOP }, /* 174: I174 [XF86AudioStop] */
217
+ { "I175", RDP_SCANCODE_UNKNOWN }, /* 175: I175 [XF86AudioRecord] */
218
+ { "I176", RDP_SCANCODE_UNKNOWN }, /* 176: I176 [XF86AudioRewind] */
219
+ { "I177", RDP_SCANCODE_UNKNOWN }, /* 177: I177 [XF86Phone] */
220
+ { "I178", RDP_SCANCODE_UNKNOWN }, /* 178: I178 [(null)] */
221
+ { "I179", RDP_SCANCODE_UNKNOWN }, /* 179: I179 [XF86Tools] */
222
+ { "I180", RDP_SCANCODE_BROWSER_HOME }, /* 180: I180 [XF86HomePage] */
223
+ { "I181", RDP_SCANCODE_BROWSER_REFRESH }, /* 181: I181 [XF86Reload] */
224
+ { "I182", RDP_SCANCODE_UNKNOWN }, /* 182: I182 [XF86Close] */
225
+ { "I183", RDP_SCANCODE_UNKNOWN }, /* 183: I183 [(null)] */
226
+ { "I184", RDP_SCANCODE_UNKNOWN }, /* 184: I184 [(null)] */
227
+ { "I185", RDP_SCANCODE_UNKNOWN }, /* 185: I185 [XF86ScrollUp] */
228
+ { "I186", RDP_SCANCODE_UNKNOWN }, /* 186: I186 [XF86ScrollDown] */
229
+ { "I187", RDP_SCANCODE_UNKNOWN }, /* 187: I187 [parenleft] */
230
+ { "I188", RDP_SCANCODE_UNKNOWN }, /* 188: I188 [parenright] */
231
+ { "I189", RDP_SCANCODE_UNKNOWN }, /* 189: I189 [XF86New] */
232
+ { "I190", RDP_SCANCODE_UNKNOWN }, /* 190: I190 [Redo] */
233
+ { "FK13", RDP_SCANCODE_F13 }, /* 191: FK13 [XF86Tools] */
234
+ { "FK14", RDP_SCANCODE_F14 }, /* 192: FK14 [XF86Launch5] */
235
+ { "FK15", RDP_SCANCODE_F15 }, /* 193: FK15 [XF86Launch6] */
236
+ { "FK16", RDP_SCANCODE_F16 }, /* 194: FK16 [XF86Launch7] */
237
+ { "FK17", RDP_SCANCODE_F17 }, /* 195: FK17 [XF86Launch8] */
238
+ { "FK18", RDP_SCANCODE_F18 }, /* 196: FK18 [XF86Launch9] */
239
+ { "FK19", RDP_SCANCODE_F19 }, /* 197: FK19 [(null)] */
240
+ { "FK20", RDP_SCANCODE_F20 }, /* 198: FK20 [XF86AudioMicMute] */
241
+ { "FK21", RDP_SCANCODE_F21 }, /* 199: FK21 [XF86TouchpadToggle] */
242
+ { "FK22", RDP_SCANCODE_F22 }, /* 200: FK22 [XF86TouchpadOn] */
243
+ { "FK23", RDP_SCANCODE_F23 }, /* 201: FK23 [XF86TouchpadOff] */
244
+ { "FK24", RDP_SCANCODE_F24 }, /* 202: FK24 [(null)] */
245
+ { "LVL5", RDP_SCANCODE_UNKNOWN }, /* 203: LVL5 [ISO_Level5_Shift] */
246
+ { "ALT", RDP_SCANCODE_LMENU }, /* 204: ALT [(null)] */
247
+ { "META", RDP_SCANCODE_LMENU }, /* 205: META [(null)] */
248
+ { "SUPR", RDP_SCANCODE_LWIN }, /* 206: SUPR [(null)] */
249
+ { "HYPR", RDP_SCANCODE_LWIN }, /* 207: HYPR [(null)] */
250
+ { "I208", RDP_SCANCODE_MEDIA_PLAY_PAUSE }, /* 208: I208 [XF86AudioPlay] */
251
+ { "I209", RDP_SCANCODE_MEDIA_PLAY_PAUSE }, /* 209: I209 [XF86AudioPause] */
252
+ { "I210", RDP_SCANCODE_UNKNOWN }, /* 210: I210 [XF86Launch3] */
253
+ { "I211", RDP_SCANCODE_UNKNOWN }, /* 211: I211 [XF86Launch4] */
254
+ { "I212", RDP_SCANCODE_UNKNOWN }, /* 212: I212 [XF86LaunchB] */
255
+ { "I213", RDP_SCANCODE_UNKNOWN }, /* 213: I213 [XF86Suspend] */
256
+ { "I214", RDP_SCANCODE_UNKNOWN }, /* 214: I214 [XF86Close] */
257
+ { "I215", RDP_SCANCODE_MEDIA_PLAY_PAUSE }, /* 215: I215 [XF86AudioPlay] */
258
+ { "I216", RDP_SCANCODE_MEDIA_NEXT_TRACK }, /* 216: I216 [XF86AudioForward] */
259
+ { "I217", RDP_SCANCODE_UNKNOWN }, /* 217: I217 [(null)] */
260
+ { "I218", RDP_SCANCODE_UNKNOWN }, /* 218: I218 [Print] */
261
+ { "I219", RDP_SCANCODE_UNKNOWN }, /* 219: I219 [(null)] */
262
+ { "I220", RDP_SCANCODE_UNKNOWN }, /* 220: I220 [XF86WebCam] */
263
+ { "I221", RDP_SCANCODE_UNKNOWN }, /* 221: I221 [XF86AudioPreset] */
264
+ { "I222", RDP_SCANCODE_UNKNOWN }, /* 222: I222 [(null)] */
265
+ { "I223", RDP_SCANCODE_LAUNCH_MAIL }, /* 223: I223 [XF86Mail] */
266
+ { "I224", RDP_SCANCODE_UNKNOWN }, /* 224: I224 [XF86Messenger] */
267
+ { "I225", RDP_SCANCODE_BROWSER_SEARCH }, /* 225: I225 [XF86Search] */
268
+ { "I226", RDP_SCANCODE_UNKNOWN }, /* 226: I226 [XF86Go] */
269
+ { "I227", RDP_SCANCODE_UNKNOWN }, /* 227: I227 [XF86Finance] */
270
+ { "I228", RDP_SCANCODE_UNKNOWN }, /* 228: I228 [XF86Game] */
271
+ { "I229", RDP_SCANCODE_UNKNOWN }, /* 229: I229 [XF86Shop] */
272
+ { "I230", RDP_SCANCODE_UNKNOWN }, /* 230: I230 [(null)] */
273
+ { "I231", RDP_SCANCODE_UNKNOWN }, /* 231: I231 [Cancel] */
274
+ { "I232", RDP_SCANCODE_UNKNOWN }, /* 232: I232 [XF86MonBrightnessDown] */
275
+ { "I233", RDP_SCANCODE_UNKNOWN }, /* 233: I233 [XF86MonBrightnessUp] */
276
+ { "I234", RDP_SCANCODE_LAUNCH_MEDIA_SELECT }, /* 234: I234 [XF86AudioMedia] */
277
+ { "I235", RDP_SCANCODE_UNKNOWN }, /* 235: I235 [XF86Display] */
278
+ { "I236", RDP_SCANCODE_UNKNOWN }, /* 236: I236 [XF86KbdLightOnOff] */
279
+ { "I237", RDP_SCANCODE_UNKNOWN }, /* 237: I237 [XF86KbdBrightnessDown] */
280
+ { "I238", RDP_SCANCODE_UNKNOWN }, /* 238: I238 [XF86KbdBrightnessUp] */
281
+ { "I239", RDP_SCANCODE_UNKNOWN }, /* 239: I239 [XF86Send] */
282
+ { "I240", RDP_SCANCODE_UNKNOWN }, /* 240: I240 [XF86Reply] */
283
+ { "I241", RDP_SCANCODE_UNKNOWN }, /* 241: I241 [XF86MailForward] */
284
+ { "I242", RDP_SCANCODE_UNKNOWN }, /* 242: I242 [XF86Save] */
285
+ { "I243", RDP_SCANCODE_UNKNOWN }, /* 243: I243 [XF86Documents] */
286
+ { "I244", RDP_SCANCODE_UNKNOWN }, /* 244: I244 [XF86Battery] */
287
+ { "I245", RDP_SCANCODE_UNKNOWN }, /* 245: I245 [XF86Bluetooth] */
288
+ { "I246", RDP_SCANCODE_UNKNOWN }, /* 246: I246 [XF86WLAN] */
289
+ { "I247", RDP_SCANCODE_UNKNOWN }, /* 247: I247 [XF86UWB] */
290
+ { "I248", RDP_SCANCODE_UNKNOWN }, /* 248: I248 [(null)] */
291
+ { "I249", RDP_SCANCODE_UNKNOWN }, /* 249: I249 [XF86Next_VMode] */
292
+ { "I250", RDP_SCANCODE_UNKNOWN }, /* 250: I250 [XF86Prev_VMode] */
293
+ { "I251", RDP_SCANCODE_UNKNOWN }, /* 251: I251 [XF86MonBrightnessCycle] */
294
+ { "I252", RDP_SCANCODE_UNKNOWN }, /* 252: I252 [XF86BrightnessAuto] */
295
+ { "I253", RDP_SCANCODE_UNKNOWN }, /* 253: I253 [XF86DisplayOff] */
296
+ { "I254", RDP_SCANCODE_UNKNOWN }, /* 254: I254 [XF86WWAN] */
297
+ { "I255", RDP_SCANCODE_UNKNOWN } /* 255: I255 [XF86RFKill] */
298
+ };
299
+
300
+ static int detect_keyboard_layout_from_xkbfile(void* display, DWORD* keyboardLayoutId);
301
+ static int freerdp_keyboard_load_map_from_xkbfile(void* display, DWORD* x11_keycode_to_rdp_scancode,
302
+ size_t count);
303
+
304
+ static void* freerdp_keyboard_xkb_init(void)
305
+ {
306
+ int status = 0;
307
+
308
+ Display* display = XOpenDisplay(NULL);
309
+
310
+ if (!display)
311
+ return NULL;
312
+
313
+ status = XkbQueryExtension(display, NULL, NULL, NULL, NULL, NULL);
314
+
315
+ if (!status)
316
+ return NULL;
317
+
318
+ return (void*)display;
319
+ }
320
+
321
+ int freerdp_keyboard_init_xkbfile(DWORD* keyboardLayoutId, DWORD* x11_keycode_to_rdp_scancode,
322
+ size_t count)
323
+ {
324
+ WINPR_ASSERT(keyboardLayoutId);
325
+ WINPR_ASSERT(x11_keycode_to_rdp_scancode);
326
+ ZeroMemory(x11_keycode_to_rdp_scancode, sizeof(DWORD) * count);
327
+
328
+ void* display = freerdp_keyboard_xkb_init();
329
+
330
+ if (!display)
331
+ {
332
+ DEBUG_KBD("Error initializing xkb");
333
+ return -1;
334
+ }
335
+
336
+ if (*keyboardLayoutId == 0)
337
+ {
338
+ detect_keyboard_layout_from_xkbfile(display, keyboardLayoutId);
339
+ DEBUG_KBD("detect_keyboard_layout_from_xkb: %" PRIu32 " (0x%08" PRIX32 ")",
340
+ *keyboardLayoutId, *keyboardLayoutId);
341
+ }
342
+
343
+ const int rc =
344
+ freerdp_keyboard_load_map_from_xkbfile(display, x11_keycode_to_rdp_scancode, count);
345
+
346
+ XCloseDisplay(display);
347
+
348
+ return rc;
349
+ }
350
+
351
+ /* return substring starting after nth comma, ending at following comma */
352
+ static char* comma_substring(char* s, size_t n)
353
+ {
354
+ char* p = NULL;
355
+
356
+ if (!s)
357
+ return "";
358
+
359
+ while (n-- > 0)
360
+ {
361
+ if (!(p = strchr(s, ',')))
362
+ break;
363
+
364
+ s = p + 1;
365
+ }
366
+
367
+ if ((p = strchr(s, ',')))
368
+ *p = 0;
369
+
370
+ return s;
371
+ }
372
+
373
+ int detect_keyboard_layout_from_xkbfile(void* display, DWORD* keyboardLayoutId)
374
+ {
375
+ DEBUG_KBD("display: %p", display);
376
+ if (!display)
377
+ return -2;
378
+
379
+ char* rules = NULL;
380
+ XkbRF_VarDefsRec rules_names = { 0 };
381
+ const Bool rc = XkbRF_GetNamesProp(display, &rules, &rules_names);
382
+ if (!rc)
383
+ {
384
+ DEBUG_KBD("XkbRF_GetNamesProp == False");
385
+ }
386
+ else
387
+ {
388
+ DEBUG_KBD("rules: %s", rules ? rules : "");
389
+ DEBUG_KBD("model: %s", rules_names.model ? rules_names.model : "");
390
+ DEBUG_KBD("layouts: %s", rules_names.layout ? rules_names.layout : "");
391
+ DEBUG_KBD("variants: %s", rules_names.variant ? rules_names.variant : "");
392
+
393
+ DWORD group = 0;
394
+ XkbStateRec state = { 0 };
395
+ XKeyboardState coreKbdState = { 0 };
396
+ XGetKeyboardControl(display, &coreKbdState);
397
+
398
+ if (XkbGetState(display, XkbUseCoreKbd, &state) == Success)
399
+ group = state.group;
400
+
401
+ DEBUG_KBD("group: %u", state.group);
402
+
403
+ const char* layout = comma_substring(rules_names.layout, group);
404
+ const char* variant = comma_substring(rules_names.variant, group);
405
+
406
+ DEBUG_KBD("layout: %s", layout ? layout : "");
407
+ DEBUG_KBD("variant: %s", variant ? variant : "");
408
+
409
+ *keyboardLayoutId = find_keyboard_layout_in_xorg_rules(layout, variant);
410
+ }
411
+ free(rules_names.model);
412
+ free(rules_names.layout);
413
+ free(rules_names.variant);
414
+ free(rules_names.options);
415
+ free(rules);
416
+
417
+ return 0;
418
+ }
419
+
420
+ static int xkb_cmp(const void* pva, const void* pvb)
421
+ {
422
+ const XKB_KEY_NAME_SCANCODE* a = pva;
423
+ const XKB_KEY_NAME_SCANCODE* b = pvb;
424
+
425
+ if (!a && !b)
426
+ return 0;
427
+ if (!a)
428
+ return 1;
429
+ if (!b)
430
+ return -1;
431
+ return strcmp(a->xkb_keyname, b->xkb_keyname);
432
+ }
433
+
434
+ static BOOL try_add(size_t offset, const char* xkb_keyname, DWORD* x11_keycode_to_rdp_scancode,
435
+
436
+ size_t count)
437
+ {
438
+ static BOOL initialized = FALSE;
439
+ static XKB_KEY_NAME_SCANCODE copy[ARRAYSIZE(XKB_KEY_NAME_SCANCODE_TABLE)] = { 0 };
440
+ if (!initialized)
441
+ {
442
+ memcpy(copy, XKB_KEY_NAME_SCANCODE_TABLE, sizeof(copy));
443
+ qsort(copy, ARRAYSIZE(copy), sizeof(XKB_KEY_NAME_SCANCODE), xkb_cmp);
444
+ initialized = TRUE;
445
+ }
446
+
447
+ XKB_KEY_NAME_SCANCODE key = { 0 };
448
+ key.xkb_keyname = xkb_keyname;
449
+ XKB_KEY_NAME_SCANCODE* found =
450
+ bsearch(&key, copy, ARRAYSIZE(copy), sizeof(XKB_KEY_NAME_SCANCODE), xkb_cmp);
451
+ if (found)
452
+ {
453
+ DEBUG_KBD("%4s: keycode: 0x%02" PRIuz " -> rdp scancode: 0x%08" PRIx32 "", xkb_keyname,
454
+ offset, found->rdp_scancode);
455
+ x11_keycode_to_rdp_scancode[offset] = found->rdp_scancode;
456
+ return TRUE;
457
+ }
458
+ return FALSE;
459
+ }
460
+
461
+ int freerdp_keyboard_load_map_from_xkbfile(void* display, DWORD* x11_keycode_to_rdp_scancode,
462
+ size_t count)
463
+ {
464
+ int status = -1;
465
+
466
+ if (!display)
467
+ return -2;
468
+
469
+ XkbDescPtr xkb = XkbGetMap(display, 0, XkbUseCoreKbd);
470
+ if (!xkb)
471
+ {
472
+ DEBUG_KBD("XkbGetMap() == NULL");
473
+ return -3;
474
+ }
475
+
476
+ if (XkbGetNames(display, XkbKeyNamesMask, xkb) != Success)
477
+ {
478
+ DEBUG_KBD("XkbGetNames() != Success");
479
+ }
480
+ else
481
+ {
482
+ char xkb_keyname[XkbKeyNameLength + 1] = { 42, 42, 42, 42,
483
+ 0 }; /* end-of-string at index 5 */
484
+
485
+ DEBUG_KBD("XkbGetNames() == Success, min=%" PRIu8 ", max=%" PRIu8, xkb->min_key_code,
486
+ xkb->max_key_code);
487
+ for (size_t i = xkb->min_key_code; i <= MIN(xkb->max_key_code, count); i++)
488
+ {
489
+ BOOL found = FALSE;
490
+ strncpy(xkb_keyname, xkb->names->keys[i].name, XkbKeyNameLength);
491
+
492
+ DEBUG_KBD("KeyCode %" PRIuz " -> %s", i, xkb_keyname);
493
+ if (strnlen(xkb_keyname, ARRAYSIZE(xkb_keyname)) < 1)
494
+ continue;
495
+
496
+ found = try_add(i, xkb_keyname, x11_keycode_to_rdp_scancode, count);
497
+
498
+ if (!found)
499
+ {
500
+ DEBUG_KBD("%4s: keycode: 0x%02X -> no RDP scancode found", xkb_keyname, i);
501
+ }
502
+ else
503
+ status = 0;
504
+ }
505
+ }
506
+
507
+ XkbFreeKeyboard(xkb, 0, 1);
508
+
509
+ return status;
510
+ }
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/locale/keyboard_xkbfile.h ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * XKB Keyboard Mapping
4
+ *
5
+ * Copyright 2009-2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+
20
+ #ifndef FREERDP_LIB_LOCALE_KEYBOARD_XKB_H
21
+ #define FREERDP_LIB_LOCALE_KEYBOARD_XKB_H
22
+
23
+ #include <freerdp/types.h>
24
+ #include <freerdp/locale/keyboard.h>
25
+ #include <freerdp/api.h>
26
+
27
+ FREERDP_LOCAL int freerdp_keyboard_init_xkbfile(DWORD* keyboardLayoutId,
28
+ DWORD* x11_keycode_to_rdp_scancode, size_t count);
29
+
30
+ #endif /* FREERDP_LIB_LOCALE_KEYBOARD_XKB_H */
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/locale/liblocale.h ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * RDP Localization
4
+ *
5
+ * Copyright 2009-2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+
20
+ #ifndef FREERDP_LIB_LOCALE_LIB_H
21
+ #define FREERDP_LIB_LOCALE_LIB_H
22
+
23
+ #include <freerdp/config.h>
24
+
25
+ #include <freerdp/log.h>
26
+
27
+ #define KBD_TAG FREERDP_TAG("locale")
28
+ #ifdef WITH_DEBUG_KBD
29
+ #define DEBUG_KBD(...) WLog_DBG(KBD_TAG, __VA_ARGS__)
30
+ #else
31
+ #define DEBUG_KBD(...) \
32
+ do \
33
+ { \
34
+ } while (0)
35
+ #endif
36
+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leisz <martin.fleisz@thincast.com>
8
+ *
9
+ * Licensed under the Apache License, Version 2.0 (the "License");
10
+ * you may not use this file except in compliance with the License.
11
+ * You may obtain a copy of the License at
12
+ *
13
+ * http://www.apache.org/licenses/LICENSE-2.0
14
+ *
15
+ * Unless required by applicable law or agreed to in writing, software
16
+ * distributed under the License is distributed on an "AS IS" BASIS,
17
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18
+ * See the License for the specific language governing permissions and
19
+ * limitations under the License.
20
+ */
21
+
22
+ #include <freerdp/config.h>
23
+
24
+ #if defined(__APPLE__)
25
+ #include <CoreFoundation/CFString.h>
26
+ #include <CoreFoundation/CFLocale.h>
27
+ #endif
28
+
29
+ #include <stdio.h>
30
+ #include <stdlib.h>
31
+ #include <string.h>
32
+
33
+ #include <winpr/crt.h>
34
+ #include <winpr/assert.h>
35
+ #include <winpr/environment.h>
36
+
37
+ #include "liblocale.h"
38
+
39
+ #include <freerdp/locale/locale.h>
40
+
41
+ /*
42
+ * Refer to MSDN article "Locale Identifier Constants and Strings":
43
+ * http://msdn.microsoft.com/en-us/library/ms776260.aspx
44
+ */
45
+
46
+ static const SYSTEM_LOCALE SYSTEM_LOCALE_TABLE[] = {
47
+ { "af", "ZA", AFRIKAANS }, /* Afrikaans (South Africa) */
48
+ { "sq", "AL", ALBANIAN }, /* Albanian (Albania) */
49
+ { "gsw", "FR", ALSATIAN }, /* Windows Vista and later: Alsatian (France) */
50
+ { "am", "ET", AMHARIC }, /* Windows Vista and later: Amharic (Ethiopia) */
51
+ { "ar", "DZ", ARABIC_ALGERIA }, /* Arabic (Algeria) */
52
+ { "ar", "BH", ARABIC_BAHRAIN }, /* Arabic (Bahrain) */
53
+ { "ar", "EG", ARABIC_EGYPT }, /* Arabic (Egypt) */
54
+ { "ar", "IQ", ARABIC_IRAQ }, /* Arabic (Iraq) */
55
+ { "ar", "JO", ARABIC_JORDAN }, /* Arabic (Jordan) */
56
+ { "ar", "KW", ARABIC_KUWAIT }, /* Arabic (Kuwait) */
57
+ { "ar", "LB", ARABIC_LEBANON }, /* Arabic (Lebanon) */
58
+ { "ar", "LY", ARABIC_LIBYA }, /* Arabic (Libya) */
59
+ { "ar", "MA", ARABIC_MOROCCO }, /* Arabic (Morocco) */
60
+ { "ar", "OM", ARABIC_OMAN }, /* Arabic (Oman) */
61
+ { "ar", "QA", ARABIC_QATAR }, /* Arabic (Qatar) */
62
+ { "ar", "SA", ARABIC_SAUDI_ARABIA }, /* Arabic (Saudi Arabia) */
63
+ { "ar", "SY", ARABIC_SYRIA }, /* Arabic (Syria) */
64
+ { "ar", "TN", ARABIC_TUNISIA }, /* Arabic (Tunisia) */
65
+ { "ar", "AE", ARABIC_UAE }, /* Arabic (U.A.E.) */
66
+ { "ar", "YE", ARABIC_YEMEN }, /* Arabic (Yemen) */
67
+ { "az", "AZ", AZERI_LATIN }, /* Azeri (Latin) */
68
+ { "az", "Cyrl_AZ", AZERI_CYRILLIC }, /* Azeri (Cyrillic) */
69
+ { "hy", "AM", ARMENIAN }, /* Windows 2000 and later: Armenian (Armenia) */
70
+ { "as", "IN", ASSAMESE }, /* Windows Vista and later: Assamese (India) */
71
+ { "ba", "RU", BASHKIR }, /* Windows Vista and later: Bashkir (Russia) */
72
+ { "eu", "ES", BASQUE }, /* Basque (Basque) */
73
+ { "be", "BY", BELARUSIAN }, /* Belarusian (Belarus) */
74
+ { "bn", "IN", BENGALI_INDIA }, /* Windows XP SP2 and later: Bengali (India) */
75
+ { "br", "FR", BRETON }, /* Breton (France) */
76
+ { "bs", "BA", BOSNIAN_LATIN }, /* Bosnian (Latin) */
77
+ { "bg", "BG", BULGARIAN }, /* Bulgarian (Bulgaria) */
78
+ { "ca", "ES", CATALAN }, /* Catalan (Catalan) */
79
+ { "zh", "HK", CHINESE_HONG_KONG }, /* Chinese (Hong Kong SAR, PRC) */
80
+ { "zh", "MO", CHINESE_MACAU }, /* Windows 98/Me, Windows XP and later: Chinese (Macao SAR) */
81
+ { "zh", "CN", CHINESE_PRC }, /* Chinese (PRC) */
82
+ { "zh", "SG", CHINESE_SINGAPORE }, /* Chinese (Singapore) */
83
+ { "zh", "TW", CHINESE_TAIWAN }, /* Chinese (Taiwan) */
84
+ { "hr", "BA", CROATIAN_BOSNIA_HERZEGOVINA }, /* Windows XP SP2 and later: Croatian (Bosnia and
85
+ Herzegovina, Latin) */
86
+ { "hr", "HR", CROATIAN }, /* Croatian (Croatia) */
87
+ { "cs", "CZ", CZECH }, /* Czech (Czech Republic) */
88
+ { "da", "DK", DANISH }, /* Danish (Denmark) */
89
+ { "prs", "AF", DARI }, /* Windows XP and later: Dari (Afghanistan) */
90
+ { "dv", "MV", DIVEHI }, /* Windows XP and later: Divehi (Maldives) */
91
+ { "nl", "BE", DUTCH_BELGIAN }, /* Dutch (Belgium) */
92
+ { "nl", "NL", DUTCH_STANDARD }, /* Dutch (Netherlands) */
93
+ { "en", "AU", ENGLISH_AUSTRALIAN }, /* English (Australia) */
94
+ { "en", "BZ", ENGLISH_BELIZE }, /* English (Belize) */
95
+ { "en", "CA", ENGLISH_CANADIAN }, /* English (Canada) */
96
+ { "en", "CB", ENGLISH_CARIBBEAN }, /* English (Caribbean) */
97
+ { "en", "IN", ENGLISH_INDIA }, /* Windows Vista and later: English (India) */
98
+ { "en", "IE", ENGLISH_IRELAND }, /* English (Ireland) */
99
+ { "en", "JM", ENGLISH_JAMAICA }, /* English (Jamaica) */
100
+ { "en", "MY", ENGLISH_MALAYSIA }, /* Windows Vista and later: English (Malaysia) */
101
+ { "en", "NZ", ENGLISH_NEW_ZEALAND }, /* English (New Zealand) */
102
+ { "en", "PH",
103
+ ENGLISH_PHILIPPINES }, /* Windows 98/Me, Windows 2000 and later: English (Philippines) */
104
+ { "en", "SG", ENGLISH_SINGAPORE }, /* Windows Vista and later: English (Singapore) */
105
+ { "en", "ZA", ENGLISH_SOUTH_AFRICA }, /* English (South Africa) */
106
+ { "en", "TT", ENGLISH_TRINIDAD }, /* English (Trinidad and Tobago) */
107
+ { "en", "GB", ENGLISH_UNITED_KINGDOM }, /* English (United Kingdom) */
108
+ { "en", "US", ENGLISH_UNITED_STATES }, /* English (United States) */
109
+ { "en", "ZW",
110
+ ENGLISH_ZIMBABWE }, /* Windows 98/Me, Windows 2000 and later: English (Zimbabwe) */
111
+ { "et", "EE", ESTONIAN }, /* Estonian (Estonia) */
112
+ { "fo", "FO", FAEROESE }, /* Faroese (Faroe Islands) */
113
+ { "fil", "PH", FILIPINO }, /* Windows XP SP2 and later (downloadable); Windows Vista and later:
114
+ Filipino (Philippines) */
115
+ { "fi", "FI", FINNISH }, /* Finnish (Finland) */
116
+ { "fr", "BE", FRENCH_BELGIAN }, /* French (Belgium) */
117
+ { "fr", "CA", FRENCH_CANADIAN }, /* French (Canada) */
118
+ { "fr", "FR", FRENCH_STANDARD }, /* French (France) */
119
+ { "fr", "LU", FRENCH_LUXEMBOURG }, /* French (Luxembourg) */
120
+ { "fr", "MC", FRENCH_MONACO }, /* French (Monaco) */
121
+ { "fr", "CH", FRENCH_SWISS }, /* French (Switzerland) */
122
+ { "fy", "NL", FRISIAN }, /* Windows XP SP2 and later (downloadable); Windows Vista and later:
123
+ Frisian (Netherlands) */
124
+ { "gl", "ES", GALICIAN }, /* Windows XP and later: Galician (Spain) */
125
+ { "ka", "GE", GEORGIAN }, /* Windows 2000 and later: Georgian (Georgia) */
126
+ { "de", "AT", GERMAN_AUSTRIAN }, /* German (Austria) */
127
+ { "de", "DE", GERMAN_STANDARD }, /* German (Germany) */
128
+ { "de", "LI", GERMAN_LIECHTENSTEIN }, /* German (Liechtenstein) */
129
+ { "de", "LU", GERMAN_LUXEMBOURG }, /* German (Luxembourg) */
130
+ { "de", "CH", GERMAN_SWISS }, /* German (Switzerland) */
131
+ { "el", "GR", GREEK }, /* Greek (Greece) */
132
+ { "kl", "GL", GREENLANDIC }, /* Windows Vista and later: Greenlandic (Greenland) */
133
+ { "gu", "IN", GUJARATI }, /* Windows XP and later: Gujarati (India) */
134
+ { "he", "IL", HEBREW }, /* Hebrew (Israel) */
135
+ { "hi", "IN", HINDI }, /* Windows 2000 and later: Hindi (India) */
136
+ { "hu", "HU", HUNGARIAN }, /* Hungarian (Hungary) */
137
+ { "is", "IS", ICELANDIC }, /* Icelandic (Iceland) */
138
+ { "ig", "NG", IGBO }, /* Igbo (Nigeria) */
139
+ { "id", "ID", INDONESIAN }, /* Indonesian (Indonesia) */
140
+ { "ga", "IE", IRISH }, /* Windows XP SP2 and later (downloadable); Windows Vista and later:
141
+ Irish (Ireland) */
142
+ { "it", "IT", ITALIAN_STANDARD }, /* Italian (Italy) */
143
+ { "it", "CH", ITALIAN_SWISS }, /* Italian (Switzerland) */
144
+ { "ja", "JP", JAPANESE }, /* Japanese (Japan) */
145
+ { "kn", "IN", KANNADA }, /* Windows XP and later: Kannada (India) */
146
+ { "kk", "KZ", KAZAKH }, /* Windows 2000 and later: Kazakh (Kazakhstan) */
147
+ { "kh", "KH", KHMER }, /* Windows Vista and later: Khmer (Cambodia) */
148
+ { "qut", "GT", KICHE }, /* Windows Vista and later: K'iche (Guatemala) */
149
+ { "rw", "RW", KINYARWANDA }, /* Windows Vista and later: Kinyarwanda (Rwanda) */
150
+ { "kok", "IN", KONKANI }, /* Windows 2000 and later: Konkani (India) */
151
+ { "ko", "KR", KOREAN }, /* Korean (Korea) */
152
+ { "ky", "KG", KYRGYZ }, /* Windows XP and later: Kyrgyz (Kyrgyzstan) */
153
+ { "lo", "LA", LAO }, /* Windows Vista and later: Lao (Lao PDR) */
154
+ { "lv", "LV", LATVIAN }, /* Latvian (Latvia) */
155
+ { "lt", "LT", LITHUANIAN }, /* Lithuanian (Lithuania) */
156
+ { "dsb", "DE", LOWER_SORBIAN }, /* Windows Vista and later: Lower Sorbian (Germany) */
157
+ { "lb", "LU", LUXEMBOURGISH }, /* Windows XP SP2 and later (downloadable); Windows Vista and
158
+ later: Luxembourgish (Luxembourg) */
159
+ { "mk", "MK", MACEDONIAN }, /* Windows 2000 and later: Macedonian (Macedonia, FYROM) */
160
+ { "ms", "BN", MALAY_BRUNEI_DARUSSALAM }, /* Windows 2000 and later: Malay (Brunei Darussalam) */
161
+ { "ms", "MY", MALAY_MALAYSIA }, /* Windows 2000 and later: Malay (Malaysia) */
162
+ { "ml", "IN", MALAYALAM }, /* Windows XP SP2 and later: Malayalam (India) */
163
+ { "mt", "MT", MALTESE }, /* Windows XP SP2 and later: Maltese (Malta) */
164
+ { "mi", "NZ", MAORI }, /* Windows XP SP2 and later: Maori (New Zealand) */
165
+ { "arn", "CL", MAPUDUNGUN }, /* Windows XP SP2 and later (downloadable); Windows Vista and
166
+ later: Mapudungun (Chile) */
167
+ { "mr", "IN", MARATHI }, /* Windows 2000 and later: Marathi (India) */
168
+ { "moh", "CA", MOHAWK }, /* Windows XP SP2 and later (downloadable); Windows Vista and later:
169
+ Mohawk (Canada) */
170
+ { "mn", "MN", MONGOLIAN }, /* Mongolian */
171
+ { "ne", "NP", NEPALI }, /* Windows XP SP2 and later (downloadable); Windows Vista and later:
172
+ Nepali (Nepal) */
173
+ { "nb", "NO", NORWEGIAN_BOKMAL }, /* Norwegian (Bokmal, Norway) */
174
+ { "nn", "NO", NORWEGIAN_NYNORSK }, /* Norwegian (Nynorsk, Norway) */
175
+ { "oc", "FR", OCCITAN }, /* Occitan (France) */
176
+ { "or", "IN", ORIYA }, /* Oriya (India) */
177
+ { "ps", "AF", PASHTO }, /* Windows XP SP2 and later (downloadable); Windows Vista and later:
178
+ Pashto (Afghanistan) */
179
+ { "fa", "IR", FARSI }, /* Persian (Iran) */
180
+ { "pl", "PL", POLISH }, /* Polish (Poland) */
181
+ { "pt", "BR", PORTUGUESE_BRAZILIAN }, /* Portuguese (Brazil) */
182
+ { "pt", "PT", PORTUGUESE_STANDARD }, /* Portuguese (Portugal) */
183
+ { "pa", "IN", PUNJABI }, /* Windows XP and later: Punjabi (India) */
184
+ { "quz", "BO", QUECHUA_BOLIVIA }, /* Windows XP SP2 and later: Quechua (Bolivia) */
185
+ { "quz", "EC", QUECHUA_ECUADOR }, /* Windows XP SP2 and later: Quechua (Ecuador) */
186
+ { "quz", "PE", QUECHUA_PERU }, /* Windows XP SP2 and later: Quechua (Peru) */
187
+ { "ro", "RO", ROMANIAN }, /* Romanian (Romania) */
188
+ { "rm", "CH", ROMANSH }, /* Windows XP SP2 and later (downloadable); Windows Vista and later:
189
+ Romansh (Switzerland) */
190
+ { "ru", "RU", RUSSIAN }, /* Russian (Russia) */
191
+ { "smn", "FI", SAMI_INARI }, /* Windows XP SP2 and later: Sami (Inari, Finland) */
192
+ { "smj", "NO", SAMI_LULE_NORWAY }, /* Windows XP SP2 and later: Sami (Lule, Norway) */
193
+ { "smj", "SE", SAMI_LULE_SWEDEN }, /* Windows XP SP2 and later: Sami (Lule, Sweden) */
194
+ { "se", "FI", SAMI_NORTHERN_FINLAND }, /* Windows XP SP2 and later: Sami (Northern, Finland) */
195
+ { "se", "NO", SAMI_NORTHERN_NORWAY }, /* Windows XP SP2 and later: Sami (Northern, Norway) */
196
+ { "se", "SE", SAMI_NORTHERN_SWEDEN }, /* Windows XP SP2 and later: Sami (Northern, Sweden) */
197
+ { "sms", "FI", SAMI_SKOLT }, /* Windows XP SP2 and later: Sami (Skolt, Finland) */
198
+ { "sma", "NO", SAMI_SOUTHERN_NORWAY }, /* Windows XP SP2 and later: Sami (Southern, Norway) */
199
+ { "sma", "SE", SAMI_SOUTHERN_SWEDEN }, /* Windows XP SP2 and later: Sami (Southern, Sweden) */
200
+ { "sa", "IN", SANSKRIT }, /* Windows 2000 and later: Sanskrit (India) */
201
+ { "sr", "SP", SERBIAN_LATIN }, /* Serbian (Latin) */
202
+ { "sr", "SIH",
203
+ SERBIAN_LATIN_BOSNIA_HERZEGOVINA }, /* Serbian (Latin) (Bosnia and Herzegovina) */
204
+ { "sr", "Cyrl_SP", SERBIAN_CYRILLIC }, /* Serbian (Cyrillic) */
205
+ { "sr", "Cyrl_SIH",
206
+ SERBIAN_CYRILLIC_BOSNIA_HERZEGOVINA }, /* Serbian (Cyrillic) (Bosnia and Herzegovina) */
207
+ { "ns", "ZA", SESOTHO_SA_LEBOA }, /* Windows XP SP2 and later: Sesotho sa Leboa/Northern Sotho
208
+ (South Africa) */
209
+ { "tn", "ZA", TSWANA }, /* Windows XP SP2 and later: Setswana/Tswana (South Africa) */
210
+ { "si", "LK", SINHALA }, /* Windows Vista and later: Sinhala (Sri Lanka) */
211
+ { "sk", "SK", SLOVAK }, /* Slovak (Slovakia) */
212
+ { "sl", "SI", SLOVENIAN }, /* Slovenian (Slovenia) */
213
+ { "es", "AR", SPANISH_ARGENTINA }, /* Spanish (Argentina) */
214
+ { "es", "BO", SPANISH_BOLIVIA }, /* Spanish (Bolivia) */
215
+ { "es", "CL", SPANISH_CHILE }, /* Spanish (Chile) */
216
+ { "es", "CO", SPANISH_COLOMBIA }, /* Spanish (Colombia) */
217
+ { "es", "CR", SPANISH_COSTA_RICA }, /* Spanish (Costa Rica) */
218
+ { "es", "DO", SPANISH_DOMINICAN_REPUBLIC }, /* Spanish (Dominican Republic) */
219
+ { "es", "EC", SPANISH_ECUADOR }, /* Spanish (Ecuador) */
220
+ { "es", "SV", SPANISH_EL_SALVADOR }, /* Spanish (El Salvador) */
221
+ { "es", "GT", SPANISH_GUATEMALA }, /* Spanish (Guatemala) */
222
+ { "es", "HN", SPANISH_HONDURAS }, /* Spanish (Honduras) */
223
+ { "es", "MX", SPANISH_MEXICAN }, /* Spanish (Mexico) */
224
+ { "es", "NI", SPANISH_NICARAGUA }, /* Spanish (Nicaragua) */
225
+ { "es", "PA", SPANISH_PANAMA }, /* Spanish (Panama) */
226
+ { "es", "PY", SPANISH_PARAGUAY }, /* Spanish (Paraguay) */
227
+ { "es", "PE", SPANISH_PERU }, /* Spanish (Peru) */
228
+ { "es", "PR", SPANISH_PUERTO_RICO }, /* Spanish (Puerto Rico) */
229
+ { "es", "ES", SPANISH_MODERN_SORT }, /* Spanish (Spain) */
230
+ { "es", "ES", SPANISH_TRADITIONAL_SORT }, /* Spanish (Spain, Traditional Sort) */
231
+ { "es", "US", SPANISH_UNITED_STATES }, /* Windows Vista and later: Spanish (United States) */
232
+ { "es", "UY", SPANISH_URUGUAY }, /* Spanish (Uruguay) */
233
+ { "es", "VE", SPANISH_VENEZUELA }, /* Spanish (Venezuela) */
234
+ { "sw", "KE", SWAHILI }, /* Windows 2000 and later: Swahili (Kenya) */
235
+ { "sv", "FI", SWEDISH_FINLAND }, /* Swedish (Finland) */
236
+ { "sv", "SE", SWEDISH }, /* Swedish (Sweden) */
237
+ { "syr", "SY", SYRIAC }, /* Windows XP and later: Syriac (Syria) */
238
+ { "ta", "IN", TAMIL }, /* Windows 2000 and later: Tamil (India) */
239
+ { "tt", "RU", TATAR }, /* Windows XP and later: Tatar (Russia) */
240
+ { "te", "IN", TELUGU }, /* Windows XP and later: Telugu (India) */
241
+ { "th", "TH", THAI }, /* Thai (Thailand) */
242
+ { "bo", "BT", TIBETAN_BHUTAN }, /* Windows Vista and later: Tibetan (Bhutan) */
243
+ { "bo", "CN", TIBETAN_PRC }, /* Windows Vista and later: Tibetan (PRC) */
244
+ { "tr", "TR", TURKISH }, /* Turkish (Turkey) */
245
+ { "tk", "TM", TURKMEN }, /* Windows Vista and later: Turkmen (Turkmenistan) */
246
+ { "ug", "CN", UIGHUR }, /* Windows Vista and later: Uighur (PRC) */
247
+ { "uk", "UA", UKRAINIAN }, /* Ukrainian (Ukraine) */
248
+ { "wen", "DE", UPPER_SORBIAN }, /* Windows Vista and later: Upper Sorbian (Germany) */
249
+ { "tr", "IN", URDU_INDIA }, /* Urdu (India) */
250
+ { "ur", "PK", URDU }, /* Windows 98/Me, Windows 2000 and later: Urdu (Pakistan) */
251
+ { "uz", "UZ", UZBEK_LATIN }, /* Uzbek (Latin) */
252
+ { "uz", "Cyrl_UZ", UZBEK_CYRILLIC }, /* Uzbek (Cyrillic) */
253
+ { "vi", "VN", VIETNAMESE }, /* Windows 98/Me, Windows NT 4.0 and later: Vietnamese (Vietnam) */
254
+ { "cy", "GB", WELSH }, /* Windows XP SP2 and later: Welsh (United Kingdom) */
255
+ { "wo", "SN", WOLOF }, /* Windows Vista and later: Wolof (Senegal) */
256
+ { "xh", "ZA", XHOSA }, /* Windows XP SP2 and later: Xhosa/isiXhosa (South Africa) */
257
+ { "sah", "RU", YAKUT }, /* Windows Vista and later: Yakut (Russia) */
258
+ { "ii", "CN", YI }, /* Windows Vista and later: Yi (PRC) */
259
+ { "yo", "NG", YORUBA }, /* Windows Vista and later: Yoruba (Nigeria) */
260
+ { "zu", "ZA", ZULU } /* Windows XP SP2 and later: Zulu/isiZulu (South Africa) */
261
+ };
262
+
263
+ typedef struct
264
+ {
265
+ DWORD localeId;
266
+ const char* name;
267
+ } LOCALE_NAME;
268
+
269
+ static const LOCALE_NAME LOCALE_NAME_TABLE[] = {
270
+ { AFRIKAANS, "AFRIKAANS" },
271
+ { ALBANIAN, "ALBANIAN" },
272
+ { ALSATIAN, "ALSATIAN" },
273
+ { AMHARIC, "AMHARIC" },
274
+ { ARABIC_SAUDI_ARABIA, "ARABIC_SAUDI_ARABIA" },
275
+ { ARABIC_IRAQ, "ARABIC_IRAQ" },
276
+ { ARABIC_EGYPT, "ARABIC_EGYPT" },
277
+ { ARABIC_LIBYA, "ARABIC_LIBYA" },
278
+ { ARABIC_ALGERIA, "ARABIC_ALGERIA" },
279
+ { ARABIC_MOROCCO, "ARABIC_MOROCCO" },
280
+ { ARABIC_TUNISIA, "ARABIC_TUNISIA" },
281
+ { ARABIC_OMAN, "ARABIC_OMAN" },
282
+ { ARABIC_YEMEN, "ARABIC_YEMEN" },
283
+ { ARABIC_SYRIA, "ARABIC_SYRIA" },
284
+ { ARABIC_JORDAN, "ARABIC_JORDAN" },
285
+ { ARABIC_LEBANON, "ARABIC_LEBANON" },
286
+ { ARABIC_KUWAIT, "ARABIC_KUWAIT" },
287
+ { ARABIC_UAE, "ARABIC_UAE" },
288
+ { ARABIC_BAHRAIN, "ARABIC_BAHRAIN" },
289
+ { ARABIC_QATAR, "ARABIC_QATAR" },
290
+ { ARMENIAN, "ARMENIAN" },
291
+ { ASSAMESE, "ASSAMESE" },
292
+ { AZERI_LATIN, "AZERI_LATIN" },
293
+ { AZERI_CYRILLIC, "AZERI_CYRILLIC" },
294
+ { BASHKIR, "BASHKIR" },
295
+ { BASQUE, "BASQUE" },
296
+ { BELARUSIAN, "BELARUSIAN" },
297
+ { BENGALI_INDIA, "BENGALI_INDIA" },
298
+ { BOSNIAN_LATIN, "BOSNIAN_LATIN" },
299
+ { BRETON, "BRETON" },
300
+ { BULGARIAN, "BULGARIAN" },
301
+ { CATALAN, "CATALAN" },
302
+ { CHINESE_TAIWAN, "CHINESE_TAIWAN" },
303
+ { CHINESE_PRC, "CHINESE_PRC" },
304
+ { CHINESE_HONG_KONG, "CHINESE_HONG_KONG" },
305
+ { CHINESE_SINGAPORE, "CHINESE_SINGAPORE" },
306
+ { CHINESE_MACAU, "CHINESE_MACAU" },
307
+ { CROATIAN, "CROATIAN" },
308
+ { CROATIAN_BOSNIA_HERZEGOVINA, "CROATIAN_BOSNIA_HERZEGOVINA" },
309
+ { CZECH, "CZECH" },
310
+ { DANISH, "DANISH" },
311
+ { DARI, "DARI" },
312
+ { DIVEHI, "DIVEHI" },
313
+ { DUTCH_STANDARD, "DUTCH_STANDARD" },
314
+ { DUTCH_BELGIAN, "DUTCH_BELGIAN" },
315
+ { ENGLISH_UNITED_STATES, "ENGLISH_UNITED_STATES" },
316
+ { ENGLISH_UNITED_KINGDOM, "ENGLISH_UNITED_KINGDOM" },
317
+ { ENGLISH_AUSTRALIAN, "ENGLISH_AUSTRALIAN" },
318
+ { ENGLISH_CANADIAN, "ENGLISH_CANADIAN" },
319
+ { ENGLISH_NEW_ZEALAND, "ENGLISH_NEW_ZEALAND" },
320
+ { ENGLISH_INDIA, "ENGLISH_INDIA" },
321
+ { ENGLISH_IRELAND, "ENGLISH_IRELAND" },
322
+ { ENGLISH_MALAYSIA, "ENGLISH_MALAYSIA" },
323
+ { ENGLISH_SOUTH_AFRICA, "ENGLISH_SOUTH_AFRICA" },
324
+ { ENGLISH_JAMAICA, "ENGLISH_JAMAICA" },
325
+ { ENGLISH_CARIBBEAN, "ENGLISH_CARIBBEAN" },
326
+ { ENGLISH_BELIZE, "ENGLISH_BELIZE" },
327
+ { ENGLISH_TRINIDAD, "ENGLISH_TRINIDAD" },
328
+ { ENGLISH_ZIMBABWE, "ENGLISH_ZIMBABWE" },
329
+ { ENGLISH_PHILIPPINES, "ENGLISH_PHILIPPINES" },
330
+ { ENGLISH_SINGAPORE, "ENGLISH_SINGAPORE" },
331
+ { ESTONIAN, "ESTONIAN" },
332
+ { FAEROESE, "FAEROESE" },
333
+ { FARSI, "FARSI" },
334
+ { FILIPINO, "FILIPINO" },
335
+ { FINNISH, "FINNISH" },
336
+ { FRENCH_STANDARD, "FRENCH_STANDARD" },
337
+ { FRENCH_BELGIAN, "FRENCH_BELGIAN" },
338
+ { FRENCH_CANADIAN, "FRENCH_CANADIAN" },
339
+ { FRENCH_SWISS, "FRENCH_SWISS" },
340
+ { FRENCH_LUXEMBOURG, "FRENCH_LUXEMBOURG" },
341
+ { FRENCH_MONACO, "FRENCH_MONACO" },
342
+ { FRISIAN, "FRISIAN" },
343
+ { GEORGIAN, "GEORGIAN" },
344
+ { GALICIAN, "GALICIAN" },
345
+ { GERMAN_STANDARD, "GERMAN_STANDARD" },
346
+ { GERMAN_SWISS, "GERMAN_SWISS" },
347
+ { GERMAN_AUSTRIAN, "GERMAN_AUSTRIAN" },
348
+ { GERMAN_LUXEMBOURG, "GERMAN_LUXEMBOURG" },
349
+ { GERMAN_LIECHTENSTEIN, "GERMAN_LIECHTENSTEIN" },
350
+ { GREEK, "GREEK" },
351
+ { GREENLANDIC, "GREENLANDIC" },
352
+ { GUJARATI, "GUJARATI" },
353
+ { HEBREW, "HEBREW" },
354
+ { HINDI, "HINDI" },
355
+ { HUNGARIAN, "HUNGARIAN" },
356
+ { ICELANDIC, "ICELANDIC" },
357
+ { IGBO, "IGBO" },
358
+ { INDONESIAN, "INDONESIAN" },
359
+ { IRISH, "IRISH" },
360
+ { ITALIAN_STANDARD, "ITALIAN_STANDARD" },
361
+ { ITALIAN_SWISS, "ITALIAN_SWISS" },
362
+ { JAPANESE, "JAPANESE" },
363
+ { KANNADA, "KANNADA" },
364
+ { KAZAKH, "KAZAKH" },
365
+ { KHMER, "KHMER" },
366
+ { KICHE, "KICHE" },
367
+ { KINYARWANDA, "KINYARWANDA" },
368
+ { KONKANI, "KONKANI" },
369
+ { KOREAN, "KOREAN" },
370
+ { KYRGYZ, "KYRGYZ" },
371
+ { LAO, "LAO" },
372
+ { LATVIAN, "LATVIAN" },
373
+ { LITHUANIAN, "LITHUANIAN" },
374
+ { LOWER_SORBIAN, "LOWER_SORBIAN" },
375
+ { LUXEMBOURGISH, "LUXEMBOURGISH" },
376
+ { MACEDONIAN, "MACEDONIAN" },
377
+ { MALAY_MALAYSIA, "MALAY_MALAYSIA" },
378
+ { MALAY_BRUNEI_DARUSSALAM, "MALAY_BRUNEI_DARUSSALAM" },
379
+ { MALAYALAM, "MALAYALAM" },
380
+ { MALTESE, "MALTESE" },
381
+ { MAPUDUNGUN, "MAPUDUNGUN" },
382
+ { MAORI, "MAORI" },
383
+ { MARATHI, "MARATHI" },
384
+ { MOHAWK, "MOHAWK" },
385
+ { MONGOLIAN, "MONGOLIAN" },
386
+ { NEPALI, "NEPALI" },
387
+ { NORWEGIAN_BOKMAL, "NORWEGIAN_BOKMAL" },
388
+ { NORWEGIAN_NYNORSK, "NORWEGIAN_NYNORSK" },
389
+ { OCCITAN, "OCCITAN" },
390
+ { ORIYA, "ORIYA" },
391
+ { PASHTO, "PASHTO" },
392
+ { POLISH, "POLISH" },
393
+ { PORTUGUESE_BRAZILIAN, "PORTUGUESE_BRAZILIAN" },
394
+ { PORTUGUESE_STANDARD, "PORTUGUESE_STANDARD" },
395
+ { PUNJABI, "PUNJABI" },
396
+ { QUECHUA_BOLIVIA, "QUECHUA_BOLIVIA" },
397
+ { QUECHUA_ECUADOR, "QUECHUA_ECUADOR" },
398
+ { QUECHUA_PERU, "QUECHUA_PERU" },
399
+ { ROMANIAN, "ROMANIAN" },
400
+ { ROMANSH, "ROMANSH" },
401
+ { RUSSIAN, "RUSSIAN" },
402
+ { SAMI_INARI, "SAMI_INARI" },
403
+ { SAMI_LULE_NORWAY, "SAMI_LULE_NORWAY" },
404
+ { SAMI_LULE_SWEDEN, "SAMI_LULE_SWEDEN" },
405
+ { SAMI_NORTHERN_FINLAND, "SAMI_NORTHERN_FINLAND" },
406
+ { SAMI_NORTHERN_NORWAY, "SAMI_NORTHERN_NORWAY" },
407
+ { SAMI_NORTHERN_SWEDEN, "SAMI_NORTHERN_SWEDEN" },
408
+ { SAMI_SKOLT, "SAMI_SKOLT" },
409
+ { SAMI_SOUTHERN_NORWAY, "SAMI_SOUTHERN_NORWAY" },
410
+ { SAMI_SOUTHERN_SWEDEN, "SAMI_SOUTHERN_SWEDEN" },
411
+ { SANSKRIT, "SANSKRIT" },
412
+ { SERBIAN_LATIN, "SERBIAN_LATIN" },
413
+ { SERBIAN_LATIN_BOSNIA_HERZEGOVINA, "SERBIAN_LATIN_BOSNIA_HERZEGOVINA" },
414
+ { SERBIAN_CYRILLIC, "SERBIAN_CYRILLIC" },
415
+ { SERBIAN_CYRILLIC_BOSNIA_HERZEGOVINA, "SERBIAN_CYRILLIC_BOSNIA_HERZEGOVINA" },
416
+ { SESOTHO_SA_LEBOA, "SESOTHO_SA_LEBOA" },
417
+ { SINHALA, "SINHALA" },
418
+ { SLOVAK, "SLOVAK" },
419
+ { SLOVENIAN, "SLOVENIAN" },
420
+ { SPANISH_TRADITIONAL_SORT, "SPANISH_TRADITIONAL_SORT" },
421
+ { SPANISH_MEXICAN, "SPANISH_MEXICAN" },
422
+ { SPANISH_MODERN_SORT, "SPANISH_MODERN_SORT" },
423
+ { SPANISH_GUATEMALA, "SPANISH_GUATEMALA" },
424
+ { SPANISH_COSTA_RICA, "SPANISH_COSTA_RICA" },
425
+ { SPANISH_PANAMA, "SPANISH_PANAMA" },
426
+ { SPANISH_DOMINICAN_REPUBLIC, "SPANISH_DOMINICAN_REPUBLIC" },
427
+ { SPANISH_VENEZUELA, "SPANISH_VENEZUELA" },
428
+ { SPANISH_COLOMBIA, "SPANISH_COLOMBIA" },
429
+ { SPANISH_PERU, "SPANISH_PERU" },
430
+ { SPANISH_ARGENTINA, "SPANISH_ARGENTINA" },
431
+ { SPANISH_ECUADOR, "SPANISH_ECUADOR" },
432
+ { SPANISH_CHILE, "SPANISH_CHILE" },
433
+ { SPANISH_UNITED_STATES, "SPANISH_UNITED_STATES" },
434
+ { SPANISH_URUGUAY, "SPANISH_URUGUAY" },
435
+ { SPANISH_PARAGUAY, "SPANISH_PARAGUAY" },
436
+ { SPANISH_BOLIVIA, "SPANISH_BOLIVIA" },
437
+ { SPANISH_EL_SALVADOR, "SPANISH_EL_SALVADOR" },
438
+ { SPANISH_HONDURAS, "SPANISH_HONDURAS" },
439
+ { SPANISH_NICARAGUA, "SPANISH_NICARAGUA" },
440
+ { SPANISH_PUERTO_RICO, "SPANISH_PUERTO_RICO" },
441
+ { SWAHILI, "SWAHILI" },
442
+ { SWEDISH, "SWEDISH" },
443
+ { SWEDISH_FINLAND, "SWEDISH_FINLAND" },
444
+ { SYRIAC, "SYRIAC" },
445
+ { TAMIL, "TAMIL" },
446
+ { TATAR, "TATAR" },
447
+ { TELUGU, "TELUGU" },
448
+ { THAI, "THAI" },
449
+ { TIBETAN_BHUTAN, "TIBETAN_BHUTAN" },
450
+ { TIBETAN_PRC, "TIBETAN_PRC" },
451
+ { TSWANA, "TSWANA" },
452
+ { UKRAINIAN, "UKRAINIAN" },
453
+ { TURKISH, "TURKISH" },
454
+ { TURKMEN, "TURKMEN" },
455
+ { UIGHUR, "UIGHUR" },
456
+ { UPPER_SORBIAN, "UPPER_SORBIAN" },
457
+ { URDU, "URDU" },
458
+ { URDU_INDIA, "URDU_INDIA" },
459
+ { UZBEK_LATIN, "UZBEK_LATIN" },
460
+ { UZBEK_CYRILLIC, "UZBEK_CYRILLIC" },
461
+ { VIETNAMESE, "VIETNAMESE" },
462
+ { WELSH, "WELSH" },
463
+ { WOLOF, "WOLOF" },
464
+ { XHOSA, "XHOSA" },
465
+ { YAKUT, "YAKUT" },
466
+ { YI, "YI" },
467
+ { YORUBA, "YORUBA" },
468
+ { ZULU, "ZULU" }
469
+ };
470
+
471
+ typedef struct
472
+ {
473
+ DWORD locale; /* Locale ID */
474
+ DWORD keyboardLayouts[5]; /* array of associated keyboard layouts */
475
+ } LOCALE_KEYBOARD_LAYOUTS;
476
+
477
+ /* TODO: Use KBD_* defines instead of hardcoded values */
478
+
479
+ static const LOCALE_KEYBOARD_LAYOUTS LOCALE_KEYBOARD_LAYOUTS_TABLE[] = {
480
+ { AFRIKAANS, { 0x00000409, 0x00000409, 0x0, 0x0, 0x0 } },
481
+ { ALBANIAN, { 0x0000041c, 0x00000409, 0x0, 0x0, 0x0 } },
482
+ { ARABIC_SAUDI_ARABIA, { 0x00000409, 0x00000401, 0x0, 0x0, 0x0 } },
483
+ { ARABIC_IRAQ, { 0x00000409, 0x00000401, 0x0, 0x0, 0x0 } },
484
+ { ARABIC_EGYPT, { 0x00000409, 0x00000401, 0x0, 0x0, 0x0 } },
485
+ { ARABIC_LIBYA, { 0x0000040c, 0x00020401, 0x0, 0x0, 0x0 } },
486
+ { ARABIC_ALGERIA, { 0x0000040c, 0x00020401, 0x0, 0x0, 0x0 } },
487
+ { ARABIC_MOROCCO, { 0x0000040c, 0x00020401, 0x0, 0x0, 0x0 } },
488
+ { ARABIC_TUNISIA, { 0x0000040c, 0x00020401, 0x0, 0x0, 0x0 } },
489
+ { ARABIC_OMAN, { 0x00000409, 0x00000401, 0x0, 0x0, 0x0 } },
490
+ { ARABIC_YEMEN, { 0x00000409, 0x00000401, 0x0, 0x0, 0x0 } },
491
+ { ARABIC_SYRIA, { 0x00000409, 0x00000401, 0x0, 0x0, 0x0 } },
492
+ { ARABIC_JORDAN, { 0x00000409, 0x00000401, 0x0, 0x0, 0x0 } },
493
+ { ARABIC_LEBANON, { 0x00000409, 0x00000401, 0x0, 0x0, 0x0 } },
494
+ { ARABIC_KUWAIT, { 0x00000409, 0x00000401, 0x0, 0x0, 0x0 } },
495
+ { ARABIC_UAE, { 0x00000409, 0x00000401, 0x0, 0x0, 0x0 } },
496
+ { ARABIC_BAHRAIN, { 0x00000409, 0x00000401, 0x0, 0x0, 0x0 } },
497
+ { ARABIC_QATAR, { 0x00000409, 0x00000401, 0x0, 0x0, 0x0 } },
498
+ { ARMENIAN, { 0x0000042b, 0x00000409, 0x00000419, 0x0, 0x0 } },
499
+ { AZERI_LATIN, { 0x0000042c, 0x0000082c, 0x00000419, 0x0, 0x0 } },
500
+ { AZERI_CYRILLIC, { 0x0000082c, 0x0000042c, 0x00000419, 0x0, 0x0 } },
501
+ { BASQUE, { 0x0000040a, 0x00000409, 0x0, 0x0, 0x0 } },
502
+ { BELARUSIAN, { 0x00000423, 0x00000409, 0x00000419, 0x0, 0x0 } },
503
+ { BENGALI_INDIA, { 0x00000445, 0x00000409, 0x0, 0x0, 0x0 } },
504
+ { BOSNIAN_LATIN, { 0x0000141A, 0x00000409, 0x0, 0x0, 0x0 } },
505
+ { BULGARIAN, { 0x00000402, 0x00000409, 0x0, 0x0, 0x0 } },
506
+ { CATALAN, { 0x0000040a, 0x00000409, 0x0, 0x0, 0x0 } },
507
+ { CHINESE_TAIWAN, { 0x00000404, 0xe0080404, 0xE0010404, 0x0, 0x0 } },
508
+ { CHINESE_PRC, { 0x00000804, 0xe00e0804, 0xe0010804, 0xe0030804, 0xe0040804 } },
509
+ { CHINESE_HONG_KONG, { 0x00000409, 0xe0080404, 0x0, 0x0, 0x0 } },
510
+ { CHINESE_SINGAPORE, { 0x00000409, 0xe00e0804, 0xe0010804, 0xe0030804, 0xe0040804 } },
511
+ { CHINESE_MACAU, { 0x00000409, 0xe00e0804, 0xe0020404, 0xe0080404 } },
512
+ { CROATIAN, { 0x0000041a, 0x00000409, 0x0, 0x0, 0x0 } },
513
+ { CROATIAN_BOSNIA_HERZEGOVINA, { 0x0000041a, 0x00000409, 0x0, 0x0, 0x0 } },
514
+ { CZECH, { 0x00000405, 0x00000409, 0x0, 0x0, 0x0 } },
515
+ { DANISH, { 0x00000406, 0x00000409, 0x0, 0x0, 0x0 } },
516
+ { DIVEHI, { 0x00000409, 0x00000465, 0x0, 0x0, 0x0 } },
517
+ { DUTCH_STANDARD, { 0x00020409, 0x00000413, 0x00000409, 0x0, 0x0 } },
518
+ { DUTCH_BELGIAN, { 0x00000813, 0x00000409, 0x0, 0x0, 0x0 } },
519
+ { ENGLISH_UNITED_STATES, { 0x00000409, 0x0, 0x0, 0x0, 0x0 } },
520
+ { ENGLISH_UNITED_KINGDOM, { 0x00000809, 0x0, 0x0, 0x0, 0x0 } },
521
+ { ENGLISH_AUSTRALIAN, { 0x00000409, 0x0, 0x0, 0x0, 0x0 } },
522
+ { ENGLISH_CANADIAN, { 0x00000409, 0x00011009, 0x00001009, 0x0, 0x0 } },
523
+ { ENGLISH_NEW_ZEALAND, { 0x00000409, 0x0, 0x0, 0x0, 0x0 } },
524
+ { ENGLISH_IRELAND, { 0x00001809, 0x00011809, 0x0, 0x0, 0x0 } },
525
+ { ENGLISH_SOUTH_AFRICA, { 0x00000409, 0x0, 0x0, 0x0, 0x0 } },
526
+ { ENGLISH_JAMAICA, { 0x00000409, 0x0, 0x0, 0x0, 0x0 } },
527
+ { ENGLISH_CARIBBEAN, { 0x00000409, 0x0, 0x0, 0x0, 0x0 } },
528
+ { ENGLISH_BELIZE, { 0x00000409, 0x0, 0x0, 0x0, 0x0 } },
529
+ { ENGLISH_TRINIDAD, { 0x00000409, 0x0, 0x0, 0x0, 0x0 } },
530
+ { ENGLISH_ZIMBABWE, { 0x00000409, 0x0, 0x0, 0x0, 0x0 } },
531
+ { ENGLISH_PHILIPPINES, { 0x00000409, 0x0, 0x0, 0x0, 0x0 } },
532
+ { ESTONIAN, { 0x00000425, 0x0, 0x0, 0x0, 0x0 } },
533
+ { FAEROESE, { 0x00000406, 0x00000409, 0x0, 0x0, 0x0 } },
534
+ { FARSI, { 0x00000409, 0x00000429, 0x00000401, 0x0, 0x0 } },
535
+ { FINNISH, { 0x0000040b, 0x00000409, 0x0, 0x0, 0x0 } },
536
+ { FRENCH_STANDARD, { 0x0000040c, 0x00000409, 0x0, 0x0, 0x0 } },
537
+ { FRENCH_BELGIAN, { 0x0000080c, 0x00000409, 0x0, 0x0, 0x0 } },
538
+ { FRENCH_CANADIAN, { 0x00000C0C, 0x00011009, 0x00000409, 0x0, 0x0 } },
539
+ { FRENCH_SWISS, { 0x0000100c, 0x00000409, 0x0, 0x0, 0x0 } },
540
+ { FRENCH_LUXEMBOURG, { 0x0000040c, 0x00000409, 0x0, 0x0, 0x0 } },
541
+ { FRENCH_MONACO, { 0x0000040c, 0x00000409, 0x0, 0x0, 0x0 } },
542
+ { GEORGIAN, { 0x00000437, 0x00000409, 0x00000419, 0x0, 0x0 } },
543
+ { GALICIAN, { 0x0000040a, 0x00000409, 0x0, 0x0, 0x0 } },
544
+ { GERMAN_STANDARD, { 0x00000407, 0x00000409, 0x0, 0x0, 0x0 } },
545
+ { GERMAN_SWISS, { 0x00000807, 0x00000409, 0x0, 0x0, 0x0 } },
546
+ { GERMAN_AUSTRIAN, { 0x00000407, 0x00000409, 0x0, 0x0, 0x0 } },
547
+ { GERMAN_LUXEMBOURG, { 0x00000407, 0x00000409, 0x0, 0x0, 0x0 } },
548
+ { GERMAN_LIECHTENSTEIN, { 0x00000407, 0x00000409, 0x0, 0x0, 0x0 } },
549
+ { GREEK, { 0x00000408, 0x00000409, 0x0, 0x0, 0x0 } },
550
+ { GUJARATI, { 0x00000409, 0x00000447, 0x00010439, 0x0, 0x0 } },
551
+ { HEBREW, { 0x00000409, 0x0000040d, 0x0, 0x0, 0x0 } },
552
+ { HINDI, { 0x00000409, 0x00010439, 0x00000439, 0x0, 0x0 } },
553
+ { HUNGARIAN, { 0x0000040e, 0x00000409, 0x0, 0x0, 0x0 } },
554
+ { ICELANDIC, { 0x0000040f, 0x00000409, 0x0, 0x0, 0x0 } },
555
+ { INDONESIAN, { 0x00000409, 0x00000409, 0x0, 0x0, 0x0 } },
556
+ { ITALIAN_STANDARD, { 0x00000410, 0x00000409, 0x0, 0x0, 0x0 } },
557
+ { ITALIAN_SWISS, { 0x00000410, 0x00000409, 0x0, 0x0, 0x0 } },
558
+ { JAPANESE, { 0xe0010411, 0x0, 0x0, 0x0, 0x0 } },
559
+ { KANNADA, { 0x00000409, 0x0000044b, 0x00010439, 0x0, 0x0 } },
560
+ { KAZAKH, { 0x0000043f, 0x00000409, 0x00000419, 0x0, 0x0 } },
561
+ { KONKANI, { 0x00000409, 0x00000439, 0x0, 0x0, 0x0 } },
562
+ { KOREAN, { 0xE0010412, 0x0, 0x0, 0x0, 0x0 } },
563
+ { KYRGYZ, { 0x00000440, 0x00000409, 0x0, 0x0, 0x0 } },
564
+ { LATVIAN, { 0x00010426, 0x0, 0x0, 0x0, 0x0 } },
565
+ { LITHUANIAN, { 0x00010427, 0x0, 0x0, 0x0, 0x0 } },
566
+ { MACEDONIAN, { 0x0000042f, 0x00000409, 0x0, 0x0, 0x0 } },
567
+ { MALAY_MALAYSIA, { 0x00000409, 0x0, 0x0, 0x0, 0x0 } },
568
+ { MALAY_BRUNEI_DARUSSALAM, { 0x00000409, 0x0, 0x0, 0x0, 0x0 } },
569
+ { MALAYALAM, { 0x00000409, 0x0000044c, 0x0, 0x0, 0x0 } },
570
+ { MALTESE, { 0x00000409, 0x0000043a, 0x0, 0x0, 0x0 } },
571
+ { MAORI, { 0x00000409, 0x00000481, 0x0, 0x0, 0x0 } },
572
+ { MARATHI, { 0x00000409, 0x0000044e, 0x00000439, 0x0, 0x0 } },
573
+ { MONGOLIAN, { 0x00000450, 0x00000409, 0x0, 0x0, 0x0 } },
574
+ { NORWEGIAN_BOKMAL, { 0x00000414, 0x00000409, 0x0, 0x0, 0x0 } },
575
+ { NORWEGIAN_NYNORSK, { 0x00000414, 0x00000409, 0x0, 0x0, 0x0 } },
576
+ { POLISH, { 0x00010415, 0x00000415, 0x00000409, 0x0, 0x0 } },
577
+ { PORTUGUESE_BRAZILIAN, { 0x00000416, 0x00000409, 0x0, 0x0, 0x0 } },
578
+ { PORTUGUESE_STANDARD, { 0x00000816, 0x00000409, 0x0, 0x0, 0x0 } },
579
+ { PUNJABI, { 0x00000409, 0x00000446, 0x00010439, 0x0, 0x0 } },
580
+ { QUECHUA_BOLIVIA, { 0x00000409, 0x0000080A, 0x0, 0x0, 0x0 } },
581
+ { QUECHUA_ECUADOR, { 0x00000409, 0x0000080A, 0x0, 0x0, 0x0 } },
582
+ { QUECHUA_PERU, { 0x00000409, 0x0000080A, 0x0, 0x0, 0x0 } },
583
+ { ROMANIAN, { 0x00000418, 0x00000409, 0x0, 0x0, 0x0 } },
584
+ { RUSSIAN, { 0x00000419, 0x00000409, 0x0, 0x0, 0x0 } },
585
+ { SAMI_INARI, { 0x0001083b, 0x00000409, 0x0, 0x0, 0x0 } },
586
+ { SAMI_LULE_NORWAY, { 0x0000043b, 0x00000409, 0x0, 0x0, 0x0 } },
587
+ { SAMI_LULE_SWEDEN, { 0x0000083b, 0x00000409, 0x0, 0x0, 0x0 } },
588
+ { SAMI_NORTHERN_FINLAND, { 0x0001083b, 0x00000409, 0x0, 0x0, 0x0 } },
589
+ { SAMI_NORTHERN_NORWAY, { 0x0000043b, 0x00000409, 0x0, 0x0, 0x0 } },
590
+ { SAMI_NORTHERN_SWEDEN, { 0x0000083b, 0x00000409, 0x0, 0x0, 0x0 } },
591
+ { SAMI_SKOLT, { 0x0001083b, 0x00000409, 0x0, 0x0, 0x0 } },
592
+ { SAMI_SOUTHERN_NORWAY, { 0x0000043b, 0x00000409, 0x0, 0x0, 0x0 } },
593
+ { SAMI_SOUTHERN_SWEDEN, { 0x0000083b, 0x00000409, 0x0, 0x0, 0x0 } },
594
+ { SANSKRIT, { 0x00000409, 0x00000439, 0x0, 0x0, 0x0 } },
595
+ { SERBIAN_LATIN, { 0x0000081a, 0x00000409, 0x0, 0x0, 0x0 } },
596
+ { SERBIAN_LATIN_BOSNIA_HERZEGOVINA, { 0x0000081a, 0x00000409, 0x0, 0x0, 0x0 } },
597
+ { SERBIAN_CYRILLIC, { 0x00000c1a, 0x00000409, 0x0, 0x0, 0x0 } },
598
+ { SERBIAN_CYRILLIC_BOSNIA_HERZEGOVINA, { 0x00000c1a, 0x00000409, 0x0, 0x0, 0x0 } },
599
+ { SLOVAK, { 0x0000041b, 0x00000409, 0x0, 0x0, 0x0 } },
600
+ { SLOVENIAN, { 0x00000424, 0x00000409, 0x0, 0x0, 0x0 } },
601
+ { SPANISH_TRADITIONAL_SORT, { 0x0000040a, 0x00000409, 0x0, 0x0, 0x0 } },
602
+ { SPANISH_MEXICAN, { 0x0000080a, 0x00000409, 0x0, 0x0, 0x0 } },
603
+ { SPANISH_MODERN_SORT, { 0x0000040a, 0x00000409, 0x0, 0x0, 0x0 } },
604
+ { SPANISH_GUATEMALA, { 0x0000080a, 0x00000409, 0x0, 0x0, 0x0 } },
605
+ { SPANISH_COSTA_RICA, { 0x0000080a, 0x00000409, 0x0, 0x0, 0x0 } },
606
+ { SPANISH_PANAMA, { 0x0000080a, 0x00000409, 0x0, 0x0, 0x0 } },
607
+ { SPANISH_DOMINICAN_REPUBLIC, { 0x0000080a, 0x00000409, 0x0, 0x0, 0x0 } },
608
+ { SPANISH_VENEZUELA, { 0x0000080a, 0x00000409, 0x0, 0x0, 0x0 } },
609
+ { SPANISH_COLOMBIA, { 0x0000080a, 0x00000409, 0x0, 0x0, 0x0 } },
610
+ { SPANISH_PERU, { 0x0000080a, 0x00000409, 0x0, 0x0, 0x0 } },
611
+ { SPANISH_ARGENTINA, { 0x0000080a, 0x00000409, 0x0, 0x0, 0x0 } },
612
+ { SPANISH_ECUADOR, { 0x0000080a, 0x00000409, 0x0, 0x0, 0x0 } },
613
+ { SPANISH_CHILE, { 0x0000080a, 0x00000409, 0x0, 0x0, 0x0 } },
614
+ { SPANISH_URUGUAY, { 0x0000080a, 0x00000409, 0x0, 0x0, 0x0 } },
615
+ { SPANISH_PARAGUAY, { 0x0000080a, 0x00000409, 0x0, 0x0, 0x0 } },
616
+ { SPANISH_BOLIVIA, { 0x0000080a, 0x00000409, 0x0, 0x0, 0x0 } },
617
+ { SPANISH_EL_SALVADOR, { 0x0000080a, 0x00000409, 0x0, 0x0, 0x0 } },
618
+ { SPANISH_HONDURAS, { 0x0000080a, 0x00000409, 0x0, 0x0, 0x0 } },
619
+ { SPANISH_NICARAGUA, { 0x0000080a, 0x00000409, 0x0, 0x0, 0x0 } },
620
+ { SPANISH_PUERTO_RICO, { 0x0000080a, 0x00000409, 0x0, 0x0, 0x0 } },
621
+ { SWAHILI, { 0x00000409, 0x0, 0x0, 0x0, 0x0 } },
622
+ { SWEDISH, { 0x0000041d, 0x00000409, 0x0, 0x0, 0x0 } },
623
+ { SWEDISH_FINLAND, { 0x0000041d, 0x00000409, 0x0, 0x0, 0x0 } },
624
+ { SYRIAC, { 0x00000409, 0x0000045a, 0x0, 0x0, 0x0 } },
625
+ { TAMIL, { 0x00000409, 0x00000449, 0x0, 0x0, 0x0 } },
626
+ { TATAR, { 0x00000444, 0x00000409, 0x00000419, 0x0, 0x0 } },
627
+ { TELUGU, { 0x00000409, 0x0000044a, 0x00010439, 0x0, 0x0 } },
628
+ { THAI, { 0x00000409, 0x0000041e, 0x0, 0x0, 0x0 } },
629
+ { TSWANA, { 0x00000409, 0x0000041f, 0x0, 0x0, 0x0 } },
630
+ { UKRAINIAN, { 0x00000422, 0x00000409, 0x0, 0x0, 0x0 } },
631
+ { TURKISH, { 0x0000041f, 0x0000041f, 0x0, 0x0, 0x0 } },
632
+ { UKRAINIAN, { 0x00000422, 0x00000409, 0x0, 0x0, 0x0 } },
633
+ { URDU, { 0x00000401, 0x00000409, 0x0, 0x0, 0x0 } },
634
+ { UZBEK_LATIN, { 0x00000409, 0x00000843, 0x00000419, 0x0, 0x0 } },
635
+ { UZBEK_CYRILLIC, { 0x00000843, 0x00000409, 0x00000419, 0x0, 0x0 } },
636
+ { VIETNAMESE, { 0x00000409, 0x0000042a, 0x0, 0x0, 0x0 } },
637
+ { WELSH, { 0x00000452, 0x00000809, 0x0, 0x0, 0x0 } },
638
+ { XHOSA, { 0x00000409, 0x00000409, 0x0, 0x0, 0x0 } },
639
+ };
640
+
641
+ static BOOL freerdp_get_system_language_and_country_codes(char* language, size_t languageLen,
642
+ char* country, size_t countryLen)
643
+ {
644
+ WINPR_ASSERT(language);
645
+ WINPR_ASSERT(languageLen > 0);
646
+ WINPR_ASSERT(country);
647
+ WINPR_ASSERT(countryLen);
648
+
649
+ #if defined(__APPLE__)
650
+ {
651
+ CFStringRef langRef, countryRef;
652
+ CFLocaleRef localeRef = CFLocaleCopyCurrent();
653
+ if (!localeRef)
654
+ return FALSE;
655
+
656
+ langRef = (CFStringRef)CFLocaleGetValue(localeRef, kCFLocaleLanguageCode);
657
+ countryRef = (CFStringRef)CFLocaleGetValue(localeRef, kCFLocaleCountryCode);
658
+ if (!langRef || !countryRef)
659
+ {
660
+ CFRelease(localeRef);
661
+ return FALSE;
662
+ }
663
+
664
+ if (!CFStringGetCString(langRef, language, languageLen, kCFStringEncodingUTF8) ||
665
+ !CFStringGetCString(countryRef, country, countryLen, kCFStringEncodingUTF8))
666
+ {
667
+ CFRelease(localeRef);
668
+ return FALSE;
669
+ }
670
+
671
+ CFRelease(localeRef);
672
+ return TRUE;
673
+ }
674
+ #else
675
+ {
676
+ size_t dot = 0;
677
+ DWORD nSize = 0;
678
+ size_t underscore = 0;
679
+ char* env_lang = NULL;
680
+ LPCSTR lang = "LANG";
681
+ /* LANG = <language>_<country>.<encoding> */
682
+ nSize = GetEnvironmentVariableA(lang, NULL, 0);
683
+
684
+ if (!nSize)
685
+ return FALSE; /* LANG environment variable was not set */
686
+
687
+ env_lang = (char*)malloc(nSize);
688
+
689
+ if (!env_lang)
690
+ return FALSE;
691
+
692
+ if (GetEnvironmentVariableA(lang, env_lang, nSize) !=
693
+ nSize - 1) /* Get locale from environment variable LANG */
694
+ {
695
+ free(env_lang);
696
+ return FALSE;
697
+ }
698
+
699
+ underscore = strcspn(env_lang, "_");
700
+
701
+ if (underscore > 3)
702
+ {
703
+ free(env_lang);
704
+ return FALSE; /* The language name should not be more than 3 letters long */
705
+ }
706
+ else
707
+ {
708
+ /* Get language code */
709
+ size_t len = MIN(languageLen - 1u, underscore);
710
+ strncpy(language, env_lang, len);
711
+ language[len] = '\0';
712
+ }
713
+
714
+ dot = strcspn(env_lang, ".");
715
+
716
+ /* Get country code */
717
+ if (dot > underscore)
718
+ {
719
+ size_t len = MIN(countryLen - 1, dot - underscore - 1);
720
+ strncpy(country, &env_lang[underscore + 1], len);
721
+ country[len] = '\0';
722
+ }
723
+ else
724
+ {
725
+ free(env_lang);
726
+ return FALSE; /* Invalid locale */
727
+ }
728
+
729
+ free(env_lang);
730
+ return TRUE;
731
+ }
732
+ #endif
733
+ }
734
+
735
+ static const SYSTEM_LOCALE* freerdp_detect_system_locale(void)
736
+ {
737
+ char language[LOCALE_LANGUAGE_LEN] = { 0 };
738
+ char country[LOCALE_COUNTRY_LEN] = { 0 };
739
+ const SYSTEM_LOCALE* locale = NULL;
740
+
741
+ freerdp_get_system_language_and_country_codes(language, ARRAYSIZE(language), country,
742
+ ARRAYSIZE(country));
743
+
744
+ for (size_t i = 0; i < ARRAYSIZE(SYSTEM_LOCALE_TABLE); i++)
745
+ {
746
+ const SYSTEM_LOCALE* current = &SYSTEM_LOCALE_TABLE[i];
747
+
748
+ if ((strcmp(language, current->language) == 0) && (strcmp(country, current->country) == 0))
749
+ {
750
+ locale = current;
751
+ break;
752
+ }
753
+ }
754
+
755
+ return locale;
756
+ }
757
+
758
+ DWORD freerdp_get_system_locale_id(void)
759
+ {
760
+ const SYSTEM_LOCALE* locale = NULL;
761
+ locale = freerdp_detect_system_locale();
762
+
763
+ if (locale != NULL)
764
+ return locale->code;
765
+
766
+ return 0;
767
+ }
768
+
769
+ static const SYSTEM_LOCALE* get_locale_from_str(const char* name)
770
+ {
771
+ for (size_t i = 0; i < ARRAYSIZE(SYSTEM_LOCALE_TABLE); i++)
772
+ {
773
+ char buffer[LOCALE_LANGUAGE_LEN + LOCALE_COUNTRY_LEN + 2] = { 0 };
774
+ const SYSTEM_LOCALE* current = &SYSTEM_LOCALE_TABLE[i];
775
+
776
+ (void)_snprintf(buffer, sizeof(buffer), "%s_%s", current->language, current->country);
777
+ /* full match, e.g. en_US */
778
+ if ((strcmp(name, buffer) == 0))
779
+ return current;
780
+ /* simple language only match e.g. 'en' */
781
+ else if ((strcmp(name, current->language) == 0))
782
+ return current;
783
+ }
784
+ return NULL;
785
+ }
786
+
787
+ static INT64 get_layout_from_locale(const SYSTEM_LOCALE* locale)
788
+ {
789
+ for (size_t i = 0; i < ARRAYSIZE(LOCALE_KEYBOARD_LAYOUTS_TABLE); i++)
790
+ {
791
+ const LOCALE_KEYBOARD_LAYOUTS* current = &LOCALE_KEYBOARD_LAYOUTS_TABLE[i];
792
+ WINPR_ASSERT(current);
793
+
794
+ if (current->locale == locale->code)
795
+ {
796
+ /* Locale found in list of default keyboard layouts */
797
+ for (size_t j = 0; j < 5; j++)
798
+ {
799
+ if (current->keyboardLayouts[j] == ENGLISH_UNITED_STATES)
800
+ {
801
+ continue; /* Skip, try to get a more localized keyboard layout */
802
+ }
803
+ else if (current->keyboardLayouts[j] == 0)
804
+ {
805
+ /*
806
+ * If we skip the ENGLISH_UNITED_STATES keyboard layout but there are no
807
+ * other possible keyboard layout for the locale, we end up here with k > 1
808
+ */
809
+
810
+ if (j >= 1)
811
+ return ENGLISH_UNITED_STATES;
812
+
813
+ /* No more keyboard layouts */
814
+ break;
815
+ }
816
+ else
817
+ return current->keyboardLayouts[j];
818
+ }
819
+ }
820
+ }
821
+ return -1;
822
+ }
823
+
824
+ const char* freerdp_get_system_locale_name_from_id(DWORD localeId)
825
+ {
826
+ for (size_t index = 0; index < ARRAYSIZE(LOCALE_NAME_TABLE); index++)
827
+ {
828
+ const LOCALE_NAME* current = &LOCALE_NAME_TABLE[index];
829
+
830
+ if (localeId == current->localeId)
831
+ return current->name;
832
+ }
833
+
834
+ return NULL;
835
+ }
836
+
837
+ INT64 freerdp_get_locale_id_from_string(const char* locale)
838
+ {
839
+ const SYSTEM_LOCALE* loc = get_locale_from_str(locale);
840
+ if (!loc)
841
+ return -1;
842
+ return loc->code;
843
+ }
844
+
845
+ int freerdp_detect_keyboard_layout_from_system_locale(DWORD* keyboardLayoutId)
846
+ {
847
+ char language[LOCALE_LANGUAGE_LEN] = { 0 };
848
+ char country[LOCALE_COUNTRY_LEN] = { 0 };
849
+
850
+ freerdp_get_system_language_and_country_codes(language, ARRAYSIZE(language), country,
851
+ ARRAYSIZE(country));
852
+
853
+ if ((strcmp(language, "C") == 0) || (strcmp(language, "POSIX") == 0))
854
+ {
855
+ *keyboardLayoutId = ENGLISH_UNITED_STATES; /* U.S. Keyboard Layout */
856
+ return 0;
857
+ }
858
+
859
+ const SYSTEM_LOCALE* locale = freerdp_detect_system_locale();
860
+
861
+ if (!locale)
862
+ return -1;
863
+
864
+ DEBUG_KBD("Found locale : %s_%s", locale->language, locale->country);
865
+ INT64 rc = get_layout_from_locale(locale);
866
+ if (rc < 0)
867
+ return -1; /* Could not detect the current keyboard layout from locale */
868
+ *keyboardLayoutId = (DWORD)rc;
869
+ return 0;
870
+ }
871
+
872
+ const SYSTEM_LOCALE* freerdp_get_system_locale_list(size_t* count)
873
+ {
874
+ WINPR_ASSERT(count);
875
+ *count = ARRAYSIZE(SYSTEM_LOCALE_TABLE);
876
+ return SYSTEM_LOCALE_TABLE;
877
+ }
878
+
879
+ DWORD freerdp_get_keyboard_default_layout_for_locale(DWORD locale)
880
+ {
881
+ for (size_t x = 0; x < ARRAYSIZE(LOCALE_KEYBOARD_LAYOUTS_TABLE); x++)
882
+ {
883
+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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * RDP Keyboard layout ID detection from common X11 xkb keyboard layout names
4
+ *
5
+ * Copyright 2009-2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+
20
+ #include <freerdp/config.h>
21
+
22
+ #include "xkb_layout_ids.h"
23
+
24
+ #include <string.h>
25
+
26
+ #include <winpr/crt.h>
27
+
28
+ #include <freerdp/locale/keyboard.h>
29
+
30
+ #include "liblocale.h"
31
+
32
+ typedef struct
33
+ {
34
+ const char* variant; /* XKB Keyboard layout variant */
35
+ INT64 keyboardLayoutID; /* Keyboard Layout ID */
36
+ } XKB_VARIANT;
37
+
38
+ typedef struct
39
+ {
40
+ const char* layout; /* XKB Keyboard layout */
41
+ INT64 keyboardLayoutID; /* Keyboard Layout ID */
42
+ const XKB_VARIANT* variants;
43
+ } XKB_LAYOUT;
44
+
45
+ /* Those have been generated automatically and are waiting to be filled by hand */
46
+
47
+ /* USA */
48
+ static const XKB_VARIANT us_variants[] = {
49
+ { "chr", 0 }, /* Cherokee */
50
+ { "euro", 0 }, /* With EuroSign on 5 */
51
+ { "intl", KBD_UNITED_STATES_INTERNATIONAL }, /* International (with dead keys) */
52
+ { "alt-intl",
53
+ KBD_UNITED_STATES_INTERNATIONAL }, /* Alternative international (former us_intl) */
54
+ { "colemak", 0 }, /* Colemak */
55
+ { "dvorak", KBD_UNITED_STATES_DVORAK }, /* Dvorak */
56
+ { "dvorak-intl", KBD_UNITED_STATES_DVORAK }, /* Dvorak international */
57
+ { "dvorak-l", KBD_UNITED_STATES_DVORAK_FOR_LEFT_HAND }, /* Left handed Dvorak */
58
+ { "dvorak-r", KBD_UNITED_STATES_DVORAK_FOR_RIGHT_HAND }, /* Right handed Dvorak */
59
+ { "dvorak-classic", KBD_UNITED_STATES_DVORAK }, /* Classic Dvorak */
60
+ { "dvp", KBD_UNITED_STATES_DVORAK_PROGRAMMER }, /* Programmer Dvorak */
61
+ { "rus", 0 }, /* Russian phonetic */
62
+ { "mac", KBD_US }, /* Macintosh */
63
+ { "altgr-intl", KBD_UNITED_STATES_INTERNATIONAL }, /* International (AltGr dead keys) */
64
+ { "olpc2", KBD_US }, /* Group toggle on multiply/divide key */
65
+ { "", 0 },
66
+ };
67
+
68
+ /* Afghanistan */
69
+ static const XKB_VARIANT af_variants[] = {
70
+ { "ps", KBD_PASHTO }, /* Pashto */
71
+ { "uz", KBD_UZBEK_CYRILLIC }, /* Southern Uzbek */
72
+ { "olpc-ps", KBD_PASHTO }, /* OLPC Pashto */
73
+ { "olpc-fa", 0 }, /* OLPC Dari */
74
+ { "olpc-uz", KBD_UZBEK_CYRILLIC }, /* OLPC Southern Uzbek */
75
+ { "", 0 },
76
+ };
77
+
78
+ /* Arabic */
79
+ static const XKB_VARIANT ara_variants[] = {
80
+ { "azerty", KBD_ARABIC_102_AZERTY }, /* azerty */
81
+ { "azerty_digits", KBD_ARABIC_102_AZERTY }, /* azerty/digits */
82
+ { "digits", KBD_ARABIC_102_AZERTY }, /* digits */
83
+ { "qwerty", KBD_ARABIC_101 }, /* qwerty */
84
+ { "qwerty_digits", KBD_ARABIC_101 }, /* qwerty/digits */
85
+ { "buckwalter", KBD_US_ENGLISH_TABLE_FOR_IBM_ARABIC_238_L }, /* Buckwalter */
86
+ { "", 0 },
87
+ };
88
+
89
+ /* Armenia */
90
+ static const XKB_VARIANT am_variants[] = {
91
+ { "phonetic", 0 }, /* Phonetic */
92
+ { "phonetic-alt", 0 }, /* Alternative Phonetic */
93
+ { "eastern", KBD_ARMENIAN_EASTERN }, /* Eastern */
94
+ { "western", KBD_ARMENIAN_WESTERN }, /* Western */
95
+ { "eastern-alt", KBD_ARMENIAN_EASTERN }, /* Alternative Eastern */
96
+ { "", 0 },
97
+ };
98
+
99
+ /* Azerbaijan */
100
+ static const XKB_VARIANT az_variants[] = {
101
+ { "cyrillic", KBD_AZERI_CYRILLIC }, /* Cyrillic */
102
+ { "", 0 },
103
+ };
104
+
105
+ /* Belarus */
106
+ static const XKB_VARIANT by_variants[] = {
107
+ { "winkeys", KBD_BELARUSIAN }, /* Winkeys */
108
+ { "latin", KBD_BELARUSIAN }, /* Latin */
109
+ { "", 0 },
110
+ };
111
+
112
+ /* Belgium */
113
+ static const XKB_VARIANT be_variants[] = {
114
+ { "oss", KBD_BELGIAN_FRENCH }, /* Alternative */
115
+ { "oss_latin9", KBD_BELGIAN_FRENCH }, /* Alternative, latin-9 only */
116
+ { "oss_sundeadkeys", KBD_BELGIAN_PERIOD }, /* Alternative, Sun dead keys */
117
+ { "iso-alternate", KBD_BELGIAN_COMMA }, /* ISO Alternate */
118
+ { "nodeadkeys", KBD_BELGIAN_COMMA }, /* Eliminate dead keys */
119
+ { "sundeadkeys", KBD_BELGIAN_PERIOD }, /* Sun dead keys */
120
+ { "wang", KBD_BELGIAN_FRENCH }, /* Wang model 724 azerty */
121
+ { "", 0 },
122
+ };
123
+
124
+ /* Bangladesh */
125
+ static const XKB_VARIANT bd_variants[] = {
126
+ { "probhat", KBD_BENGALI_INSCRIPT }, /* Probhat */
127
+ { "", 0 },
128
+ };
129
+
130
+ /* India */
131
+ static const XKB_VARIANT in_variants[] = {
132
+ { "ben", KBD_BENGALI }, /* Bengali */
133
+ { "ben_probhat", KBD_BENGALI_INSCRIPT }, /* Bengali Probhat */
134
+ { "guj", KBD_GUJARATI }, /* Gujarati */
135
+ { "guru", 0 }, /* Gurmukhi */
136
+ { "jhelum", 0 }, /* Gurmukhi Jhelum */
137
+ { "kan", KBD_KANNADA }, /* Kannada */
138
+ { "mal", KBD_MALAYALAM }, /* Malayalam */
139
+ { "mal_lalitha", KBD_MALAYALAM }, /* Malayalam Lalitha */
140
+ { "ori", 0 }, /* Oriya */
141
+ { "tam_unicode", KBD_TAMIL }, /* Tamil Unicode */
142
+ { "tam_TAB", KBD_TAMIL }, /* Tamil TAB Typewriter */
143
+ { "tam_TSCII", KBD_TAMIL }, /* Tamil TSCII Typewriter */
144
+ { "tam", KBD_TAMIL }, /* Tamil */
145
+ { "tel", KBD_TELUGU }, /* Telugu */
146
+ { "urd-phonetic", KBD_URDU }, /* Urdu, Phonetic */
147
+ { "urd-phonetic3", KBD_URDU }, /* Urdu, Alternative phonetic */
148
+ { "urd-winkeys", KBD_URDU }, /* Urdu, Winkeys */
149
+ { "bolnagri", KBD_HINDI_TRADITIONAL }, /* Hindi Bolnagri */
150
+ { "hin-wx", KBD_HINDI_TRADITIONAL }, /* Hindi Wx */
151
+ { "", 0 },
152
+ };
153
+
154
+ /* Bosnia and Herzegovina */
155
+ static const XKB_VARIANT ba_variants[] = {
156
+ { "alternatequotes", KBD_BOSNIAN }, /* Use guillemets for quotes */
157
+ { "unicode", KBD_BOSNIAN }, /* Use Bosnian digraphs */
158
+ { "unicodeus", KBD_BOSNIAN }, /* US keyboard with Bosnian digraphs */
159
+ { "us", KBD_BOSNIAN_CYRILLIC }, /* US keyboard with Bosnian letters */
160
+ { "", 0 },
161
+ };
162
+
163
+ /* Brazil */
164
+ static const XKB_VARIANT br_variants[] = {
165
+ { "nodeadkeys", KBD_PORTUGUESE_BRAZILIAN_ABNT2 }, /* Eliminate dead keys */
166
+ { "dvorak", KBD_UNITED_STATES_DVORAK }, /* Dvorak */
167
+ { "nativo", KBD_PORTUGUESE_BRAZILIAN_ABNT2 }, /* Nativo */
168
+ { "nativo-us", KBD_PORTUGUESE_BRAZILIAN_ABNT2 }, /* Nativo for USA keyboards */
169
+ { "nativo-epo", KBD_PORTUGUESE_BRAZILIAN_ABNT2 }, /* Nativo for Esperanto */
170
+ { "", 0 },
171
+ };
172
+
173
+ /* Bulgaria */
174
+ static const XKB_VARIANT bg_variants[] = {
175
+ { "phonetic", KBD_BULGARIAN_LATIN }, /* Traditional Phonetic */
176
+ { "bas_phonetic", KBD_BULGARIAN_LATIN }, /* Standard Phonetic */
177
+ { "", 0 },
178
+ };
179
+
180
+ /* Morocco */
181
+ static const XKB_VARIANT ma_variants[] = {
182
+ { "french", KBD_FRENCH }, /* French */
183
+ { "tifinagh", 0 }, /* Tifinagh */
184
+ { "tifinagh-alt", 0 }, /* Tifinagh Alternative */
185
+ { "tifinagh-alt-phonetic", 0 }, /* Tifinagh Alternative Phonetic */
186
+ { "tifinagh-extended", 0 }, /* Tifinagh Extended */
187
+ { "tifinagh-phonetic", 0 }, /* Tifinagh Phonetic */
188
+ { "tifinagh-extended-phonetic", 0 }, /* Tifinagh Extended Phonetic */
189
+ { "", 0 },
190
+ };
191
+
192
+ /* Canada */
193
+ static const XKB_VARIANT ca_variants[] = {
194
+ { "fr", KBD_CANADIAN_FRENCH }, /* French Dvorak */
195
+ { "fr-dvorak", KBD_UNITED_STATES_DVORAK }, /* French Dvorak */
196
+ { "fr-legacy", KBD_CANADIAN_FRENCH_LEGACY }, /* French (legacy) */
197
+ { "multix", KBD_CANADIAN_MULTILINGUAL_STANDARD }, /* Multilingual */
198
+ { "multi", KBD_CANADIAN_MULTILINGUAL_STANDARD }, /* Multilingual, first part */
199
+ { "multi-2gr", KBD_CANADIAN_MULTILINGUAL_STANDARD }, /* Multilingual, second part */
200
+ { "ike", KBD_INUKTITUT_LATIN }, /* Inuktitut */
201
+ { "shs" /* codespell:ignore shs */, 0 }, /* Secwepemctsin */
202
+ { "kut", 0 }, /* Ktunaxa */
203
+ { "", 0 },
204
+ };
205
+
206
+ /* China */
207
+ static const XKB_VARIANT cn_variants[] = {
208
+ { "tib", 0 }, /* Tibetan */
209
+ { "tib_asciinum", 0 }, /* Tibetan (with ASCII numerals) */
210
+ { "", 0 },
211
+ };
212
+
213
+ /* Croatia */
214
+ static const XKB_VARIANT hr_variants[] = {
215
+ { "alternatequotes", KBD_CROATIAN }, /* Use guillemets for quotes */
216
+ { "unicode", KBD_CROATIAN }, /* Use Croatian digraphs */
217
+ { "unicodeus", KBD_CROATIAN }, /* US keyboard with Croatian digraphs */
218
+ { "us", KBD_CROATIAN }, /* US keyboard with Croatian letters */
219
+ { "", 0 },
220
+ };
221
+
222
+ /* Czechia */
223
+ static const XKB_VARIANT cz_variants[] = {
224
+ { "bksl", KBD_CZECH_PROGRAMMERS }, /* With &lt;\|&gt; key */
225
+ { "qwerty", KBD_CZECH_QWERTY }, /* qwerty */
226
+ { "qwerty_bksl", KBD_CZECH_QWERTY }, /* qwerty, extended Backslash */
227
+ { "ucw", KBD_CZECH }, /* UCW layout (accented letters only) */
228
+ { "", 0 },
229
+ };
230
+
231
+ /* Denmark */
232
+ static const XKB_VARIANT dk_variants[] = {
233
+ { "nodeadkeys", KBD_DANISH }, /* Eliminate dead keys */
234
+ { "mac", KBD_DANISH }, /* Macintosh */
235
+ { "mac_nodeadkeys", KBD_DANISH }, /* Macintosh, eliminate dead keys */
236
+ { "dvorak", KBD_UNITED_STATES_DVORAK }, /* Dvorak */
237
+ { "", 0 },
238
+ };
239
+
240
+ /* Netherlands */
241
+ static const XKB_VARIANT nl_variants[] = {
242
+ { "sundeadkeys", KBD_SWISS_FRENCH }, /* Sun dead keys */
243
+ { "mac", KBD_SWISS_FRENCH }, /* Macintosh */
244
+ { "std", KBD_SWISS_FRENCH }, /* Standard */
245
+ { "", 0 },
246
+ };
247
+
248
+ /* Estonia */
249
+ static const XKB_VARIANT ee_variants[] = {
250
+ { "nodeadkeys", KBD_US }, /* Eliminate dead keys */
251
+ { "dvorak", KBD_UNITED_STATES_DVORAK }, /* Dvorak */
252
+ { "us", KBD_UNITED_STATES_INTERNATIONAL }, /* US keyboard with Estonian letters */
253
+ { "", 0 },
254
+ };
255
+
256
+ /* Iran */
257
+ static const XKB_VARIANT ir_variants[] = {
258
+ { "pro", 0 }, /* Pro */
259
+ { "keypad", 0 }, /* Keypad */
260
+ { "pro_keypad", 0 }, /* Pro Keypad */
261
+ { "ku", 0 }, /* Kurdish, Latin Q */
262
+ { "ku_f", 0 }, /* Kurdish, (F) */
263
+ { "ku_alt", 0 }, /* Kurdish, Latin Alt-Q */
264
+ { "ku_ara", 0 }, /* Kurdish, Arabic-Latin */
265
+ { "", 0 },
266
+ };
267
+
268
+ /* Iraq */
269
+ static const XKB_VARIANT iq_variants[] = {
270
+ { "ku", 0 }, /* Kurdish, Latin Q */
271
+ { "ku_f", 0 }, /* Kurdish, (F) */
272
+ { "ku_alt", 0 }, /* Kurdish, Latin Alt-Q */
273
+ { "ku_ara", 0 }, /* Kurdish, Arabic-Latin */
274
+ { "", 0 },
275
+ };
276
+
277
+ /* Faroe Islands */
278
+ static const XKB_VARIANT fo_variants[] = {
279
+ { "nodeadkeys", 0 }, /* Eliminate dead keys */
280
+ { "", 0 },
281
+ };
282
+
283
+ /* Finland */
284
+ static const XKB_VARIANT fi_variants[] = {
285
+ { "nodeadkeys", 0 }, /* Eliminate dead keys */
286
+ { "smi", 0 }, /* Northern Saami */
287
+ { "classic", 0 }, /* Classic */
288
+ { "mac", 0 }, /* Macintosh */
289
+ { "", 0 },
290
+ };
291
+
292
+ /* France */
293
+ static const XKB_VARIANT fr_variants[] = {
294
+ { "nodeadkeys", 0 }, /* Eliminate dead keys */
295
+ { "sundeadkeys", 0 }, /* Sun dead keys */
296
+ { "oss", 0 }, /* Alternative */
297
+ { "oss_latin9", 0 }, /* Alternative, latin-9 only */
298
+ { "oss_nodeadkeys", 0 }, /* Alternative, eliminate dead keys */
299
+ { "oss_sundeadkeys", 0 }, /* Alternative, Sun dead keys */
300
+ { "latin9", 0 }, /* (Legacy) Alternative */
301
+ { "latin9_nodeadkeys", 0 }, /* (Legacy) Alternative, eliminate dead keys */
302
+ { "latin9_sundeadkeys", 0 }, /* (Legacy) Alternative, Sun dead keys */
303
+ { "bepo", KBD_FRENCH_BEPO }, /* Bepo, ergonomic, Dvorak way */
304
+ { "bepo_latin9", 0 }, /* Bepo, ergonomic, Dvorak way, latin-9 only */
305
+ { "dvorak", 0 }, /* Dvorak */
306
+ { "mac", 0 }, /* Macintosh */
307
+ { "bre", 0 }, /* Breton */
308
+ { "oci", 0 }, /* Occitan */
309
+ { "geo", 0 }, /* Georgian AZERTY Tskapo */
310
+ { "", 0 },
311
+ };
312
+
313
+ /* Ghana */
314
+ static const XKB_VARIANT gh_variants[] = {
315
+ { "generic", 0 }, /* Multilingual */
316
+ { "akan", 0 }, /* Akan */
317
+ { "ewe", 0 }, /* Ewe */
318
+ { "fula", 0 }, /* Fula */
319
+ { "ga", 0 }, /* Ga */
320
+ { "hausa", 0 }, /* Hausa */
321
+ { "", 0 },
322
+ };
323
+
324
+ /* Georgia */
325
+ static const XKB_VARIANT ge_variants[] = {
326
+ { "ergonomic", 0 }, /* Ergonomic */
327
+ { "mess", 0 }, /* MESS */
328
+ { "ru", 0 }, /* Russian */
329
+ { "os", 0 }, /* Ossetian */
330
+ { "", 0 },
331
+ };
332
+
333
+ /* Germany */
334
+ static const XKB_VARIANT de_variants[] = {
335
+ { "deadacute", KBD_GERMAN }, /* Dead acute */
336
+ { "deadgraveacute", KBD_GERMAN }, /* Dead grave acute */
337
+ { "nodeadkeys", KBD_GERMAN }, /* Eliminate dead keys */
338
+ { "ro", KBD_GERMAN }, /* Romanian keyboard with German letters */
339
+ { "ro_nodeadkeys",
340
+ KBD_GERMAN }, /* Romanian keyboard with German letters, eliminate dead keys */
341
+ { "dvorak", KBD_UNITED_STATES_DVORAK }, /* Dvorak */
342
+ { "sundeadkeys", KBD_GERMAN }, /* Sun dead keys */
343
+ { "neo", KBD_GERMAN_NEO }, /* Neo 2 */
344
+ { "mac", KBD_GERMAN }, /* Macintosh */
345
+ { "mac_nodeadkeys", KBD_GERMAN }, /* Macintosh, eliminate dead keys */
346
+ { "dsb", KBD_GERMAN }, /* Lower Sorbian */
347
+ { "dsb_qwertz", KBD_GERMAN }, /* Lower Sorbian (qwertz) */
348
+ { "qwerty", KBD_GERMAN_IBM }, /* qwerty */
349
+ { "", 0 },
350
+ };
351
+
352
+ /* Greece */
353
+ static const XKB_VARIANT gr_variants[] = {
354
+ { "simple", KBD_GREEK_220 }, /* Simple */
355
+ { "extended", KBD_GREEK_319 }, /* Extended */
356
+ { "nodeadkeys", KBD_GREEK_319 }, /* Eliminate dead keys */
357
+ { "polytonic", KBD_GREEK_POLYTONIC }, /* Polytonic */
358
+ { "", 0 },
359
+ };
360
+
361
+ /* Hungary */
362
+ static const XKB_VARIANT hu_variants[] = {
363
+ { "standard", KBD_HUNGARIAN_101_KEY }, /* Standard */
364
+ { "nodeadkeys", KBD_HUNGARIAN_101_KEY }, /* Eliminate dead keys */
365
+ { "qwerty", KBD_HUNGARIAN_101_KEY }, /* qwerty */
366
+ { "101_qwertz_comma_dead", KBD_HUNGARIAN_101_KEY }, /* 101/qwertz/comma/Dead keys */
367
+ { "101_qwertz_comma_nodead", KBD_HUNGARIAN_101_KEY }, /* 101/qwertz/comma/Eliminate dead keys */
368
+ { "101_qwertz_dot_dead", KBD_HUNGARIAN_101_KEY }, /* 101/qwertz/dot/Dead keys */
369
+ { "101_qwertz_dot_nodead", KBD_HUNGARIAN_101_KEY }, /* 101/qwertz/dot/Eliminate dead keys */
370
+ { "101_qwerty_comma_dead", KBD_HUNGARIAN_101_KEY }, /* 101/qwerty/comma/Dead keys */
371
+ { "101_qwerty_comma_nodead", KBD_HUNGARIAN_101_KEY }, /* 101/qwerty/comma/Eliminate dead keys */
372
+ { "101_qwerty_dot_dead", KBD_HUNGARIAN_101_KEY }, /* 101/qwerty/dot/Dead keys */
373
+ { "101_qwerty_dot_nodead", KBD_HUNGARIAN_101_KEY }, /* 101/qwerty/dot/Eliminate dead keys */
374
+ { "102_qwertz_comma_dead", KBD_HUNGARIAN_101_KEY }, /* 102/qwertz/comma/Dead keys */
375
+ { "102_qwertz_comma_nodead", KBD_HUNGARIAN_101_KEY }, /* 102/qwertz/comma/Eliminate dead keys */
376
+ { "102_qwertz_dot_dead", KBD_HUNGARIAN_101_KEY }, /* 102/qwertz/dot/Dead keys */
377
+ { "102_qwertz_dot_nodead", KBD_HUNGARIAN_101_KEY }, /* 102/qwertz/dot/Eliminate dead keys */
378
+ { "102_qwerty_comma_dead", KBD_HUNGARIAN_101_KEY }, /* 102/qwerty/comma/Dead keys */
379
+ { "102_qwerty_comma_nodead", KBD_HUNGARIAN_101_KEY }, /* 102/qwerty/comma/Eliminate dead keys */
380
+ { "102_qwerty_dot_dead", KBD_HUNGARIAN_101_KEY }, /* 102/qwerty/dot/Dead keys */
381
+ { "102_qwerty_dot_nodead", KBD_HUNGARIAN_101_KEY }, /* 102/qwerty/dot/Eliminate dead keys */
382
+ { "", 0 },
383
+ };
384
+
385
+ /* Iceland */
386
+ static const XKB_VARIANT is_variants[] = {
387
+ { "Sundeadkeys", KBD_ICELANDIC }, /* Sun dead keys */
388
+ { "nodeadkeys", KBD_ICELANDIC }, /* Eliminate dead keys */
389
+ { "mac", KBD_ICELANDIC }, /* Macintosh */
390
+ { "dvorak", KBD_UNITED_STATES_DVORAK }, /* Dvorak */
391
+ { "", 0 },
392
+ };
393
+
394
+ /* Israel */
395
+ static const XKB_VARIANT il_variants[] = {
396
+ { "lyx", KBD_HEBREW }, /* lyx */
397
+ { "phonetic", KBD_HEBREW }, /* Phonetic */
398
+ { "biblical", KBD_HEBREW }, /* Biblical Hebrew (Tiro) */
399
+ { "", 0 },
400
+ };
401
+
402
+ /* Italy */
403
+ static const XKB_VARIANT it_variants[] = {
404
+ { "nodeadkeys", KBD_ITALIAN_142 }, /* Eliminate dead keys */
405
+ { "mac", KBD_ITALIAN }, /* Macintosh */
406
+ { "geo", KBD_GEORGIAN }, /* Georgian */
407
+ { "", 0 },
408
+ };
409
+
410
+ /* Japan */
411
+ static const XKB_VARIANT jp_variants[] = {
412
+ { "kana", KBD_JAPANESE_INPUT_SYSTEM_MS_IME2002 }, /* Kana */
413
+ { "OADG109A", KBD_JAPANESE_INPUT_SYSTEM_MS_IME2002 }, /* OADG 109A */
414
+ { "", 0 },
415
+ };
416
+
417
+ /* Kyrgyzstan */
418
+ static const XKB_VARIANT kg_variants[] = {
419
+ { "phonetic", KBD_KYRGYZ_CYRILLIC }, /* Phonetic */
420
+ { "", 0 },
421
+ };
422
+
423
+ /* Kazakhstan */
424
+ static const XKB_VARIANT kz_variants[] = {
425
+ { "ruskaz", KBD_KAZAKH }, /* Russian with Kazakh */
426
+ { "kazrus", KBD_KAZAKH }, /* Kazakh with Russian */
427
+ { "", 0 },
428
+ };
429
+
430
+ /* Latin America */
431
+ static const XKB_VARIANT latam_variants[] = {
432
+ { "nodeadkeys", KBD_LATIN_AMERICAN }, /* Eliminate dead keys */
433
+ { "deadtilde", KBD_LATIN_AMERICAN }, /* Include dead tilde */
434
+ { "sundeadkeys", KBD_LATIN_AMERICAN }, /* Sun dead keys */
435
+ { "", 0 },
436
+ };
437
+
438
+ /* Lithuania */
439
+ static const XKB_VARIANT lt_variants[] = {
440
+ { "std", KBD_LITHUANIAN }, /* Standard */
441
+ { "us", KBD_LITHUANIAN_IBM }, /* US keyboard with Lithuanian letters */
442
+ { "ibm", KBD_LITHUANIAN_IBM }, /* IBM (LST 1205-92) */
443
+ { "lekp", KBD_LITHUANIAN }, /* LEKP */
444
+ { "lekpa", KBD_LITHUANIAN }, /* LEKPa */
445
+ { "balticplus", KBD_LITHUANIAN }, /* Baltic+ */
446
+ { "", 0 },
447
+ };
448
+
449
+ /* Latvia */
450
+ static const XKB_VARIANT lv_variants[] = {
451
+ { "apostrophe", KBD_LATVIAN }, /* Apostrophe (') variant */
452
+ { "tilde", KBD_LATVIAN }, /* Tilde (~) variant */
453
+ { "fkey", KBD_LATVIAN }, /* F-letter (F) variant */
454
+ { "", 0 },
455
+ };
456
+
457
+ /* Montenegro */
458
+ static const XKB_VARIANT me_variants[] = {
459
+ { "cyrillic", 0 }, /* Cyrillic */
460
+ { "cyrillicyz", 0 }, /* Cyrillic, Z and ZHE swapped */
461
+ { "latinunicode", 0 }, /* Latin unicode */
462
+ { "latinyz", 0 }, /* Latin qwerty */
463
+ { "latinunicodeyz", 0 }, /* Latin unicode qwerty */
464
+ { "cyrillicalternatequotes", 0 }, /* Cyrillic with guillemets */
465
+ { "latinalternatequotes", 0 }, /* Latin with guillemets */
466
+ { "", 0 },
467
+ };
468
+
469
+ /* Macedonia */
470
+ static const XKB_VARIANT mk_variants[] = {
471
+ { "nodeadkeys", KBD_FYRO_MACEDONIAN }, /* Eliminate dead keys */
472
+ { "", 0 },
473
+ };
474
+
475
+ /* Malta */
476
+ static const XKB_VARIANT mt_variants[] = {
477
+ { "us", KBD_MALTESE_48_KEY }, /* Maltese keyboard with US layout */
478
+ { "", 0 },
479
+ };
480
+
481
+ /* Norway */
482
+ static const XKB_VARIANT no_variants[] = {
483
+ { "nodeadkeys", KBD_NORWEGIAN }, /* Eliminate dead keys */
484
+ { "dvorak", KBD_UNITED_STATES_DVORAK }, /* Dvorak */
485
+ { "smi", KBD_NORWEGIAN_WITH_SAMI }, /* Northern Saami */
486
+ { "smi_nodeadkeys", KBD_SAMI_EXTENDED_NORWAY }, /* Northern Saami, eliminate dead keys */
487
+ { "mac", KBD_NORWEGIAN }, /* Macintosh */
488
+ { "mac_nodeadkeys", KBD_SAMI_EXTENDED_NORWAY }, /* Macintosh, eliminate dead keys */
489
+ { "", 0 },
490
+ };
491
+
492
+ /* Poland */
493
+ static const XKB_VARIANT pl_variants[] = {
494
+ { "qwertz", KBD_POLISH_214 }, /* qwertz */
495
+ { "dvorak", KBD_UNITED_STATES_DVORAK }, /* Dvorak */
496
+ { "dvorak_quotes", KBD_UNITED_STATES_DVORAK }, /* Dvorak, Polish quotes on quotemark key */
497
+ { "dvorak_altquotes", KBD_UNITED_STATES_DVORAK }, /* Dvorak, Polish quotes on key 1 */
498
+ { "csb", 0 }, /* Kashubian */
499
+ { "ru_phonetic_dvorak", KBD_UNITED_STATES_DVORAK }, /* Russian phonetic Dvorak */
500
+ { "", 0 },
501
+ };
502
+
503
+ /* Portugal */
504
+ static const XKB_VARIANT pt_variants[] = {
505
+ { "nodeadkeys", KBD_PORTUGUESE }, /* Eliminate dead keys */
506
+ { "sundeadkeys", KBD_PORTUGUESE }, /* Sun dead keys */
507
+ { "mac", KBD_PORTUGUESE }, /* Macintosh */
508
+ { "mac_nodeadkeys", KBD_PORTUGUESE }, /* Macintosh, eliminate dead keys */
509
+ { "mac_sundeadkeys", KBD_PORTUGUESE }, /* Macintosh, Sun dead keys */
510
+ { "nativo", KBD_PORTUGUESE }, /* Nativo */
511
+ { "nativo-us", KBD_PORTUGUESE }, /* Nativo for USA keyboards */
512
+ { "nativo-epo", KBD_PORTUGUESE }, /* Nativo for Esperanto */
513
+ { "", 0 },
514
+ };
515
+
516
+ /* Romania */
517
+ static const XKB_VARIANT ro_variants[] = {
518
+ { "cedilla", KBD_ROMANIAN }, /* Cedilla */
519
+ { "std", KBD_ROMANIAN }, /* Standard */
520
+ { "std_cedilla", KBD_ROMANIAN }, /* Standard (Cedilla) */
521
+ { "winkeys", KBD_ROMANIAN }, /* Winkeys */
522
+ { "crh_f", KBD_TURKISH_F }, /* Crimean Tatar (Turkish F) */
523
+ { "crh_alt", KBD_TURKISH_Q }, /* Crimean Tatar (Turkish Alt-Q) */
524
+ { "crh_dobruca1", KBD_TATAR }, /* Crimean Tatar (Dobruca-1 Q) */
525
+ { "crh_dobruca2", KBD_TATAR }, /* Crimean Tatar (Dobruca-2 Q) */
526
+ { "", 0 },
527
+ };
528
+
529
+ /* Russia */
530
+ static const XKB_VARIANT ru_variants[] = {
531
+ { "phonetic", KBD_RUSSIAN }, /* Phonetic */
532
+ { "phonetic_winkeys", KBD_RUSSIAN }, /* Phonetic Winkeys */
533
+ { "typewriter", KBD_RUSSIAN_TYPEWRITER }, /* Typewriter */
534
+ { "legacy", KBD_RUSSIAN }, /* Legacy */
535
+ { "tt", KBD_TATAR }, /* Tatar */
536
+ { "os_legacy", 0 }, /* Ossetian, legacy */
537
+ { "os_winkeys", 0 }, /* Ossetian, Winkeys */
538
+ { "cv", 0 }, /* Chuvash */
539
+ { "cv_latin", 0 }, /* Chuvash Latin */
540
+ { "udm", 0 }, /* Udmurt */
541
+ { "kom", 0 }, /* Komi */
542
+ { "sah", 0 }, /* Yakut */
543
+ { "xal", 0 }, /* Kalmyk */
544
+ { "dos", 0 }, /* DOS */
545
+ { "", 0 },
546
+ };
547
+
548
+ /* Serbia */
549
+ static const XKB_VARIANT rs_variants[] = {
550
+ { "yz", KBD_SERBIAN_CYRILLIC }, /* Z and ZHE swapped */
551
+ { "latin", KBD_SERBIAN_LATIN }, /* Latin */
552
+ { "latinunicode", KBD_SERBIAN_LATIN }, /* Latin Unicode */
553
+ { "latinyz", KBD_SERBIAN_LATIN }, /* Latin qwerty */
554
+ { "latinunicodeyz", KBD_SERBIAN_LATIN }, /* Latin Unicode qwerty */
555
+ { "alternatequotes", KBD_SERBIAN_CYRILLIC }, /* With guillemets */
556
+ { "latinalternatequotes", KBD_SERBIAN_LATIN }, /* Latin with guillemets */
557
+ { "", 0 },
558
+ };
559
+
560
+ /* Slovenia */
561
+ static const XKB_VARIANT si_variants[] = {
562
+ { "alternatequotes", KBD_SLOVENIAN }, /* Use guillemets for quotes */
563
+ { "us", KBD_UNITED_STATES_INTERNATIONAL }, /* US keyboard with Slovenian letters */
564
+ { "", 0 },
565
+ };
566
+
567
+ /* Slovakia */
568
+ static const XKB_VARIANT sk_variants[] = {
569
+ { "bksl", KBD_SLOVAK }, /* Extended Backslash */
570
+ { "qwerty", KBD_SLOVAK_QWERTY }, /* qwerty */
571
+ { "qwerty_bksl", KBD_SLOVAK_QWERTY }, /* qwerty, extended Backslash */
572
+ { "", 0 },
573
+ };
574
+
575
+ /* Spain */
576
+ static const XKB_VARIANT es_variants[] = {
577
+ { "nodeadkeys", KBD_SPANISH_VARIATION }, /* Eliminate dead keys */
578
+ { "deadtilde", KBD_SPANISH_VARIATION }, /* Include dead tilde */
579
+ { "sundeadkeys", KBD_SPANISH }, /* Sun dead keys */
580
+ { "dvorak", KBD_UNITED_STATES_DVORAK }, /* Dvorak */
581
+ { "ast", KBD_SPANISH_VARIATION }, /* Asturian variant with bottom-dot H and bottom-dot L */
582
+ { "cat", KBD_SPANISH_VARIATION }, /* Catalan variant with middle-dot L */
583
+ { "mac", KBD_SPANISH }, /* Macintosh */
584
+ { "", 0 },
585
+ };
586
+
587
+ /* Sweden */
588
+ static const XKB_VARIANT se_variants[] = {
589
+ { "nodeadkeys", KBD_SWEDISH }, /* Eliminate dead keys */
590
+ { "dvorak", KBD_UNITED_STATES_DVORAK }, /* Dvorak */
591
+ { "rus", KBD_RUSSIAN }, /* Russian phonetic */
592
+ { "rus_nodeadkeys", KBD_RUSSIAN }, /* Russian phonetic, eliminate dead keys */
593
+ { "smi", KBD_SWEDISH_WITH_SAMI }, /* Northern Saami */
594
+ { "mac", KBD_SWEDISH }, /* Macintosh */
595
+ { "svdvorak", KBD_UNITED_STATES_DVORAK }, /* Svdvorak */
596
+ { "", 0 },
597
+ };
598
+
599
+ /* Switzerland */
600
+ static const XKB_VARIANT ch_variants[] = {
601
+ { "de_nodeadkeys", KBD_SWISS_GERMAN }, /* German, eliminate dead keys */
602
+ { "de_sundeadkeys", KBD_SWISS_GERMAN }, /* German, Sun dead keys */
603
+ { "fr", KBD_SWISS_FRENCH }, /* French */
604
+ { "fr_nodeadkeys", KBD_SWISS_FRENCH }, /* French, eliminate dead keys */
605
+ { "fr_sundeadkeys", KBD_SWISS_FRENCH }, /* French, Sun dead keys */
606
+ { "fr_mac", KBD_SWISS_FRENCH }, /* French (Macintosh) */
607
+ { "de_mac", KBD_SWISS_GERMAN }, /* German (Macintosh) */
608
+ { "", 0 },
609
+ };
610
+
611
+ /* Syria */
612
+ static const XKB_VARIANT sy_variants[] = {
613
+ { "syc", KBD_SYRIAC }, /* Syriac */
614
+ { "syc_phonetic", KBD_SYRIAC_PHONETIC }, /* Syriac phonetic */
615
+ { "ku", 0 }, /* Kurdish, Latin Q */
616
+ { "ku_f", 0 }, /* Kurdish, (F) */
617
+ { "ku_alt", 0 }, /* Kurdish, Latin Alt-Q */
618
+ { "", 0 },
619
+ };
620
+
621
+ /* Tajikistan */
622
+ static const XKB_VARIANT tj_variants[] = {
623
+ { "legacy", 0 }, /* Legacy */
624
+ { "", 0 },
625
+ };
626
+
627
+ /* Sri Lanka */
628
+ static const XKB_VARIANT lk_variants[] = {
629
+ { "tam_unicode", KBD_TAMIL }, /* Tamil Unicode */
630
+ { "tam_TAB", KBD_TAMIL }, /* Tamil TAB Typewriter */
631
+ { "", 0 },
632
+ };
633
+
634
+ /* Thailand */
635
+ static const XKB_VARIANT th_variants[] = {
636
+ { "tis", KBD_THAI_KEDMANEE_NON_SHIFTLOCK }, /* TIS-820.2538 */
637
+ { "pat", KBD_THAI_PATTACHOTE }, /* Pattachote */
638
+ { "", 0 },
639
+ };
640
+
641
+ /* Turkey */
642
+ static const XKB_VARIANT tr_variants[] = {
643
+ { "f", KBD_TURKISH_F }, /* (F) */
644
+ { "alt", KBD_TURKISH_Q }, /* Alt-Q */
645
+ { "sundeadkeys", KBD_TURKISH_F }, /* Sun dead keys */
646
+ { "ku", 0 }, /* Kurdish, Latin Q */
647
+ { "ku_f", 0 }, /* Kurdish, (F) */
648
+ { "ku_alt", 0 }, /* Kurdish, Latin Alt-Q */
649
+ { "intl", KBD_TURKISH_F }, /* International (with dead keys) */
650
+ { "crh", KBD_TATAR }, /* Crimean Tatar (Turkish Q) */
651
+ { "crh_f", KBD_TURKISH_F }, /* Crimean Tatar (Turkish F) */
652
+ { "crh_alt", KBD_TURKISH_Q }, /* Crimean Tatar (Turkish Alt-Q) */
653
+ { "", 0 },
654
+ };
655
+
656
+ /* Ukraine */
657
+ static const XKB_VARIANT ua_variants[] = {
658
+ { "phonetic", KBD_UKRAINIAN }, /* Phonetic */
659
+ { "typewriter", KBD_UKRAINIAN }, /* Typewriter */
660
+ { "winkeys", KBD_UKRAINIAN }, /* Winkeys */
661
+ { "legacy", KBD_UKRAINIAN }, /* Legacy */
662
+ { "rstu", KBD_UKRAINIAN }, /* Standard RSTU */
663
+ { "rstu_ru", KBD_UKRAINIAN }, /* Standard RSTU on Russian layout */
664
+ { "homophonic", KBD_UKRAINIAN }, /* Homophonic */
665
+ { "crh", KBD_TATAR }, /* Crimean Tatar (Turkish Q) */
666
+ { "crh_f", KBD_TURKISH_F }, /* Crimean Tatar (Turkish F) */
667
+ { "crh_alt", KBD_TURKISH_Q }, /* Crimean Tatar (Turkish Alt-Q) */
668
+ { "", 0 },
669
+ };
670
+
671
+ /* United Kingdom */
672
+ static const XKB_VARIANT gb_variants[] = {
673
+ { "extd", KBD_UNITED_KINGDOM_EXTENDED }, /* Extended - Winkeys */
674
+ { "intl", KBD_UNITED_KINGDOM_EXTENDED }, /* International (with dead keys) */
675
+ { "dvorak", KBD_UNITED_STATES_DVORAK }, /* Dvorak */
676
+ { "dvorakukp", KBD_UNITED_STATES_DVORAK }, /* Dvorak (UK Punctuation) */
677
+ { "mac", KBD_UNITED_KINGDOM }, /* Macintosh */
678
+ { "colemak", 0 }, /* Colemak */
679
+ { "", 0 },
680
+ };
681
+
682
+ /* Uzbekistan */
683
+ static const XKB_VARIANT uz_variants[] = {
684
+ { "latin", 0 }, /* Latin */
685
+ { "crh", KBD_TATAR }, /* Crimean Tatar (Turkish Q) */
686
+ { "crh_f", KBD_TURKISH_F }, /* Crimean Tatar (Turkish F) */
687
+ { "crh_alt", KBD_TURKISH_Q }, /* Crimean Tatar (Turkish Alt-Q) */
688
+ { "", 0 },
689
+ };
690
+
691
+ /* Korea, Republic of */
692
+ static const XKB_VARIANT kr_variants[] = {
693
+ { "kr104", KBD_KOREAN_INPUT_SYSTEM_IME_2000 }, /* 101/104 key Compatible */
694
+ { "", 0 },
695
+ };
696
+
697
+ /* Ireland */
698
+ static const XKB_VARIANT ie_variants[] = {
699
+ { "CloGaelach", KBD_GAELIC }, /* CloGaelach */
700
+ { "UnicodeExpert", KBD_GAELIC }, /* UnicodeExpert */
701
+ { "ogam", KBD_GAELIC }, /* Ogham */
702
+ { "ogam_is434", KBD_GAELIC }, /* Ogham IS434 */
703
+ { "", 0 },
704
+ };
705
+
706
+ /* Pakistan */
707
+ static const XKB_VARIANT pk_variants[] = {
708
+ { "urd-crulp", 0 }, /* CRULP */
709
+ { "urd-nla", 0 }, /* NLA */
710
+ { "ara", KBD_ARABIC_101 }, /* Arabic */
711
+ { "", 0 },
712
+ };
713
+
714
+ /* Esperanto */
715
+ static const XKB_VARIANT epo_variants[] = {
716
+ { "legacy", 0 }, /* displaced semicolon and quote (obsolete) */
717
+ { "", 0 },
718
+ };
719
+
720
+ /* Nigeria */
721
+ static const XKB_VARIANT ng_variants[] = {
722
+ { "igbo", 0 }, /* Igbo */
723
+ { "yoruba", 0 }, /* Yoruba */
724
+ { "hausa", 0 }, /* Hausa */
725
+ { "", 0 },
726
+ };
727
+
728
+ /* Braille */
729
+ static const XKB_VARIANT brai_variants[] = {
730
+ { "left_hand", 0 }, /* Left hand */
731
+ { "right_hand", 0 }, /* Right hand */
732
+ { "", 0 },
733
+ };
734
+
735
+ /* Turkmenistan */
736
+ static const XKB_VARIANT tm_variants[] = {
737
+ { "alt", KBD_TURKISH_Q }, /* Alt-Q */
738
+ { "", 0 },
739
+ };
740
+
741
+ static const XKB_LAYOUT xkbLayouts[] = {
742
+ { "us", KBD_US, us_variants }, /* USA */
743
+ { "ad", 0, NULL }, /* Andorra */
744
+ { "af", KBD_FARSI, af_variants }, /* Afghanistan */
745
+ { "ara", KBD_ARABIC_101, ara_variants }, /* Arabic */
746
+ { "al", 0, NULL }, /* Albania */
747
+ { "am", KBD_ARMENIAN_EASTERN, am_variants }, /* Armenia */
748
+ { "az", KBD_AZERI_CYRILLIC, az_variants }, /* Azerbaijan */
749
+ { "by", KBD_BELARUSIAN, by_variants }, /* Belarus */
750
+ { "be", KBD_BELGIAN_FRENCH, be_variants }, /* Belgium */
751
+ { "bd", KBD_BENGALI, bd_variants }, /* Bangladesh */
752
+ { "in", KBD_HINDI_TRADITIONAL, in_variants }, /* India */
753
+ { "ba", KBD_CROATIAN, ba_variants }, /* Bosnia and Herzegovina */
754
+ { "br", KBD_PORTUGUESE_BRAZILIAN_ABNT, br_variants }, /* Brazil */
755
+ { "bg", KBD_BULGARIAN_LATIN, bg_variants }, /* Bulgaria */
756
+ { "ma", KBD_FRENCH, ma_variants }, /* Morocco */
757
+ { "mm", 0, NULL }, /* Myanmar */
758
+ { "ca", KBD_US, ca_variants }, /* Canada */
759
+ { "cd", 0, NULL }, /* Congo, Democratic Republic of the */
760
+ { "cn", KBD_CHINESE_TRADITIONAL_PHONETIC, cn_variants }, /* China */
761
+ { "hr", KBD_CROATIAN, hr_variants }, /* Croatia */
762
+ { "cz", KBD_CZECH, cz_variants }, /* Czechia */
763
+ { "dk", KBD_DANISH, dk_variants }, /* Denmark */
764
+ { "nl", KBD_DUTCH, nl_variants }, /* Netherlands */
765
+ { "bt", 0, NULL }, /* Bhutan */
766
+ { "ee", KBD_ESTONIAN, ee_variants }, /* Estonia */
767
+ { "ir", 0, ir_variants }, /* Iran */
768
+ { "iq", 0, iq_variants }, /* Iraq */
769
+ { "fo", 0, fo_variants }, /* Faroe Islands */
770
+ { "fi", KBD_FINNISH, fi_variants }, /* Finland */
771
+ { "fr", KBD_FRENCH, fr_variants }, /* France */
772
+ { "gh", 0, gh_variants }, /* Ghana */
773
+ { "gn", 0, NULL }, /* Guinea */
774
+ { "ge", KBD_GEORGIAN, ge_variants }, /* Georgia */
775
+ { "at", KBD_GERMAN, de_variants }, /* Austria */
776
+ { "de", KBD_GERMAN, de_variants }, /* Germany */
777
+ { "gr", KBD_GREEK, gr_variants }, /* Greece */
778
+ { "hu", KBD_HUNGARIAN, hu_variants }, /* Hungary */
779
+ { "is", KBD_ICELANDIC, is_variants }, /* Iceland */
780
+ { "il", KBD_HEBREW, il_variants }, /* Israel */
781
+ { "it", KBD_ITALIAN, it_variants }, /* Italy */
782
+ { "jp", KBD_JAPANESE_INPUT_SYSTEM_MS_IME2002, jp_variants }, /* Japan */
783
+ { "kg", 0, kg_variants }, /* Kyrgyzstan */
784
+ { "kh", 0, NULL }, /* Cambodia */
785
+ { "kz", KBD_KAZAKH, kz_variants }, /* Kazakhstan */
786
+ { "la", 0, NULL }, /* Laos */
787
+ { "latam", KBD_LATIN_AMERICAN, latam_variants }, /* Latin America */
788
+ { "lt", KBD_LITHUANIAN, lt_variants }, /* Lithuania */
789
+ { "lv", KBD_LATVIAN, lv_variants }, /* Latvia */
790
+ { "mao", KBD_MAORI, NULL }, /* Maori */
791
+ { "me", KBD_SERBIAN_LATIN, me_variants }, /* Montenegro */
792
+ { "mk", KBD_FYRO_MACEDONIAN, mk_variants }, /* Macedonia */
793
+ { "mt", KBD_MALTESE_48_KEY, mt_variants }, /* Malta */
794
+ { "mn", KBD_MONGOLIAN_CYRILLIC, NULL }, /* Mongolia */
795
+ { "no", KBD_NORWEGIAN, no_variants }, /* Norway */
796
+ { "pl", KBD_POLISH_PROGRAMMERS, pl_variants }, /* Poland */
797
+ { "pt", KBD_PORTUGUESE, pt_variants }, /* Portugal */
798
+ { "ro", KBD_ROMANIAN, ro_variants }, /* Romania */
799
+ { "ru", KBD_RUSSIAN, ru_variants }, /* Russia */
800
+ { "rs", KBD_SERBIAN_LATIN, rs_variants }, /* Serbia */
801
+ { "si", KBD_SLOVENIAN, si_variants }, /* Slovenia */
802
+ { "sk", KBD_SLOVAK, sk_variants }, /* Slovakia */
803
+ { "es", KBD_SPANISH, es_variants }, /* Spain */
804
+ { "se", KBD_SWEDISH, se_variants }, /* Sweden */
805
+ { "ch", KBD_SWISS_GERMAN, ch_variants }, /* Switzerland */
806
+ { "sy", KBD_SYRIAC, sy_variants }, /* Syria */
807
+ { "tj", 0, tj_variants }, /* Tajikistan */
808
+ { "lk", 0, lk_variants }, /* Sri Lanka */
809
+ { "th", KBD_THAI_KEDMANEE, th_variants }, /* Thailand */
810
+ { "tr", KBD_TURKISH_Q, tr_variants }, /* Turkey */
811
+ { "ua", KBD_UKRAINIAN, ua_variants }, /* Ukraine */
812
+ { "gb", KBD_UNITED_KINGDOM, gb_variants }, /* United Kingdom */
813
+ { "uz", KBD_UZBEK_CYRILLIC, uz_variants }, /* Uzbekistan */
814
+ { "vn", KBD_VIETNAMESE, NULL }, /* Vietnam */
815
+ { "kr", KBD_KOREAN_INPUT_SYSTEM_IME_2000, kr_variants }, /* Korea, Republic of */
816
+ { "ie", KBD_UNITED_KINGDOM, ie_variants }, /* Ireland */
817
+ { "pk", 0, pk_variants }, /* Pakistan */
818
+ { "mv", 0, NULL }, /* Maldives */
819
+ { "za", KBD_US, NULL }, /* South Africa */
820
+ { "epo", 0, epo_variants }, /* Esperanto */
821
+ { "np", KBD_NEPALI, NULL }, /* Nepal */
822
+ { "ng", 0, ng_variants }, /* Nigeria */
823
+ { "et", 0, NULL }, /* Ethiopia */
824
+ { "sn", 0, NULL }, /* Senegal */
825
+ { "brai", 0, brai_variants }, /* Braille */
826
+ { "tm", KBD_TURKISH_Q, tm_variants }, /* Turkmenistan */
827
+ };
828
+
829
+ static uint32_t convert(int64_t val)
830
+ {
831
+ WINPR_ASSERT(val <= UINT32_MAX);
832
+ WINPR_ASSERT(val >= INT32_MIN);
833
+ return WINPR_CXX_COMPAT_CAST(uint32_t, val);
834
+ }
835
+
836
+ static UINT32 find_keyboard_layout_variant(const XKB_LAYOUT* layout, const char* variant)
837
+ {
838
+ WINPR_ASSERT(layout);
839
+ WINPR_ASSERT(variant);
840
+
841
+ const XKB_VARIANT* variants = layout->variants;
842
+ if (variants)
843
+ {
844
+ const XKB_VARIANT* var = variants;
845
+ while (var->variant && (strlen(var->variant) != 0))
846
+ {
847
+ if (strcmp(var->variant, variant) == 0)
848
+ return convert(var->keyboardLayoutID);
849
+ var++;
850
+ }
851
+ }
852
+
853
+ return convert(layout->keyboardLayoutID);
854
+ }
855
+
856
+ UINT32 find_keyboard_layout_in_xorg_rules(const char* layout, const char* variant)
857
+ {
858
+ if ((layout == NULL) || (variant == NULL))
859
+ return 0;
860
+
861
+ DEBUG_KBD("xkbLayout: %s\txkbVariant: %s", layout, variant);
862
+
863
+ for (size_t i = 0; i < ARRAYSIZE(xkbLayouts); i++)
864
+ {
865
+ const XKB_LAYOUT* cur = &xkbLayouts[i];
866
+ if (strcmp(cur->layout, layout) == 0)
867
+ return find_keyboard_layout_variant(cur, variant);
868
+ }
869
+
870
+ return 0;
871
+ }
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/locale/xkb_layout_ids.h ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * RDP Keyboard layout ID detection from common X11 xkb keyboard layout names
4
+ *
5
+ * Copyright 2009-2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+
20
+ #ifndef FREERDP_LIB_LOCALE_XKB_LAYOUT_IDS_H
21
+ #define FREERDP_LIB_LOCALE_XKB_LAYOUT_IDS_H
22
+
23
+ #include <freerdp/types.h>
24
+ #include <freerdp/api.h>
25
+
26
+ FREERDP_LOCAL UINT32 find_keyboard_layout_in_xorg_rules(const char* layout, const char* variant);
27
+
28
+ #endif /* FREERDP_LIB_LOCALE_XKB_LAYOUT_IDS_H */
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/primitives/CMakeLists.txt ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # primitives
2
+
3
+ set(PRIMITIVES_SRCS
4
+ prim_add.c
5
+ prim_add.h
6
+ prim_andor.c
7
+ prim_andor.h
8
+ prim_alphaComp.c
9
+ prim_alphaComp.h
10
+ prim_colors.c
11
+ prim_colors.h
12
+ prim_copy.c
13
+ prim_copy.h
14
+ prim_set.c
15
+ prim_set.h
16
+ prim_shift.c
17
+ prim_shift.h
18
+ prim_sign.c
19
+ prim_sign.h
20
+ prim_YUV.c
21
+ prim_YUV.h
22
+ prim_YCoCg.c
23
+ prim_YCoCg.h
24
+ primitives.c
25
+ prim_internal.h
26
+ )
27
+
28
+ set(PRIMITIVES_SSE2_SRCS sse/prim_colors_sse2.c sse/prim_set_sse2.c)
29
+
30
+ set(PRIMITIVES_SSE3_SRCS sse/prim_add_sse3.c sse/prim_alphaComp_sse3.c sse/prim_andor_sse3.c sse/prim_shift_sse3.c)
31
+
32
+ set(PRIMITIVES_SSSE3_SRCS sse/prim_sign_ssse3.c sse/prim_YCoCg_ssse3.c)
33
+
34
+ set(PRIMITIVES_SSE4_1_SRCS sse/prim_copy_sse4_1.c sse/prim_YUV_sse4.1.c)
35
+
36
+ set(PRIMITIVES_SSE4_2_SRCS)
37
+
38
+ set(PRIMITIVES_AVX2_SRCS sse/prim_copy_avx2.c)
39
+
40
+ set(PRIMITIVES_NEON_SRCS neon/prim_colors_neon.c neon/prim_YCoCg_neon.c neon/prim_YUV_neon.c)
41
+
42
+ set(PRIMITIVES_OPENCL_SRCS opencl/prim_YUV_opencl.c)
43
+
44
+ if(WITH_OPENCL)
45
+ set(FILENAME "opencl/primitives.cl")
46
+ set_source_files_properties(${FILENAME} PROPERTIES HEADER_FILE_ONLY ON)
47
+ list(APPEND PRIMITIVES_OPENCL_SRCS ${FILENAME})
48
+
49
+ include(ConvertFileToHexArray)
50
+ file_to_hex_array(${FILENAME} FILEDATA)
51
+
52
+ set(HDR_FILE "${CMAKE_CURRENT_BINARY_DIR}/opencl/primitives-opencl-program.h")
53
+ cleaning_configure_file("${CMAKE_CURRENT_SOURCE_DIR}/opencl/primitives.h.in" ${HDR_FILE} @ONLY)
54
+ list(APPEND PRIMITIVES_OPENCL_SRCS ${HDR_FILE})
55
+
56
+ include_directories(${CMAKE_CURRENT_BINARY_DIR}/opencl)
57
+ freerdp_include_directory_add(${OpenCL_INCLUDE_DIRS})
58
+ freerdp_library_add(OpenCL::OpenCL)
59
+ endif()
60
+
61
+ set(PRIMITIVES_OPT_SRCS
62
+ ${PRIMITIVES_NEON_SRCS}
63
+ ${PRIMITIVES_SSE2_SRCS}
64
+ ${PRIMITIVES_SSE3_SRCS}
65
+ ${PRIMITIVES_SSSE3_SRCS}
66
+ ${PRIMITIVES_SSE4_1_SRCS}
67
+ ${PRIMITIVES_SSE4_2_SRCS}
68
+ ${PRIMITIVES_OPENCL_SRCS}
69
+ )
70
+
71
+ if(WITH_AVX2)
72
+ list(APPEND PRIMITIVES_OPT_SRCS ${PRIMITIVES_AVX2_SRCS})
73
+ endif()
74
+
75
+ set(PRIMITIVES_SRCS ${PRIMITIVES_SRCS} ${PRIMITIVES_OPT_SRCS})
76
+
77
+ include_directories(${CMAKE_CURRENT_SOURCE_DIR})
78
+ add_library(freerdp-primitives OBJECT ${PRIMITIVES_SRCS})
79
+
80
+ include(CompilerDetect)
81
+ include(DetectIntrinsicSupport)
82
+ if(WITH_SIMD)
83
+ set_simd_source_file_properties("sse2" ${PRIMITIVES_SSE2_SRCS})
84
+ set_simd_source_file_properties("sse3" ${PRIMITIVES_SSE3_SRCS})
85
+ set_simd_source_file_properties("ssse3" ${PRIMITIVES_SSSE3_SRCS})
86
+ set_simd_source_file_properties("sse4.1" ${PRIMITIVES_SSE4_1_SRCS})
87
+ set_simd_source_file_properties("sse4.2" ${PRIMITIVES_SSE4_2_SRCS})
88
+ set_simd_source_file_properties("avx2" ${PRIMITIVES_AVX2_SRCS})
89
+ set_simd_source_file_properties("neon" ${PRIMITIVES_OPT_SRCS})
90
+ endif()
91
+
92
+ freerdp_object_library_add(freerdp-primitives)
93
+
94
+ if(BUILD_BENCHMARK)
95
+ add_subdirectory(benchmark)
96
+ endif()
97
+
98
+ if(BUILD_TESTING_INTERNAL)
99
+ add_subdirectory(test)
100
+ endif()
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/primitives/prim_YCoCg.c ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* FreeRDP: A Remote Desktop Protocol Client
2
+ * YCoCg<->RGB Color conversion operations.
3
+ * vi:ts=4 sw=4:
4
+ *
5
+ * (c) Copyright 2014 Hewlett-Packard Development Company, L.P.
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+
20
+ #include <freerdp/config.h>
21
+
22
+ #include <freerdp/types.h>
23
+ #include <freerdp/primitives.h>
24
+
25
+ #include "prim_internal.h"
26
+ #include "prim_YCoCg.h"
27
+
28
+ /* helper function to convert raw 8 bit values to signed 16bit values.
29
+ */
30
+ static INT16 convert(UINT8 raw, int shift)
31
+ {
32
+ const int cll = shift - 1; /* -1 builds in the /2's */
33
+ return (INT16)((INT8)(raw << cll));
34
+ }
35
+
36
+ /* ------------------------------------------------------------------------- */
37
+ static pstatus_t general_YCoCgToRGB_8u_AC4R(const BYTE* pSrc, INT32 srcStep, BYTE* pDst,
38
+ UINT32 DstFormat, INT32 dstStep, UINT32 width,
39
+ UINT32 height, UINT8 shift, BOOL withAlpha)
40
+ {
41
+ const DWORD formatSize = FreeRDPGetBytesPerPixel(DstFormat);
42
+ fkt_writePixel writePixel = getPixelWriteFunction(DstFormat, TRUE);
43
+
44
+ for (size_t y = 0; y < height; y++)
45
+ {
46
+ const BYTE* sptr = &pSrc[y * WINPR_ASSERTING_INT_CAST(uint32_t, srcStep)];
47
+ BYTE* dptr = &pDst[y * WINPR_ASSERTING_INT_CAST(uint32_t, dstStep)];
48
+ for (size_t x = 0; x < width; x++)
49
+ {
50
+ /* Note: shifts must be done before sign-conversion. */
51
+ const INT16 Cg = convert(*sptr++, shift);
52
+ const INT16 Co = convert(*sptr++, shift);
53
+ const INT16 Y = *sptr++; /* UINT8->INT16 */
54
+ const INT16 T = (INT16)(Y - Cg);
55
+ const INT16 B = (INT16)(T + Co);
56
+ const INT16 G = (INT16)(Y + Cg);
57
+ const INT16 R = (INT16)(T - Co);
58
+ BYTE A = *sptr++;
59
+
60
+ if (!withAlpha)
61
+ A = 0xFFU;
62
+
63
+ dptr = writePixel(dptr, formatSize, DstFormat, CLIP(R), CLIP(G), CLIP(B), A);
64
+ }
65
+ }
66
+
67
+ return PRIMITIVES_SUCCESS;
68
+ }
69
+
70
+ /* ------------------------------------------------------------------------- */
71
+ void primitives_init_YCoCg(primitives_t* WINPR_RESTRICT prims)
72
+ {
73
+ prims->YCoCgToRGB_8u_AC4R = general_YCoCgToRGB_8u_AC4R;
74
+ }
75
+
76
+ void primitives_init_YCoCg_opt(primitives_t* WINPR_RESTRICT prims)
77
+ {
78
+ primitives_init_YCoCg_ssse3(prims);
79
+ primitives_init_YCoCg_neon(prims);
80
+ }
local-test-freerdp-delta-01/afc-freerdp/libfreerdp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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * Generic YUV/RGB conversion operations
4
+ *
5
+ * Copyright 2014 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
+ * Copyright 2015-2017 Armin Novak <armin.novak@thincast.com>
7
+ * Copyright 2015-2017 Norbert Federa <norbert.federa@thincast.com>
8
+ * Copyright 2015-2017 Vic Lee
9
+ * Copyright 2015-2017 Thincast Technologies GmbH
10
+ *
11
+ * Licensed under the Apache License, Version 2.0 (the "License");
12
+ * you may not use this file except in compliance with the License.
13
+ * You may obtain a copy of the License at
14
+ *
15
+ * http://www.apache.org/licenses/LICENSE-2.0
16
+ *
17
+ * Unless required by applicable law or agreed to in writing, software
18
+ * distributed under the License is distributed on an "AS IS" BASIS,
19
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20
+ * See the License for the specific language governing permissions and
21
+ * limitations under the License.
22
+ */
23
+
24
+ #include <winpr/wtypes.h>
25
+ #include <winpr/assert.h>
26
+ #include <winpr/cast.h>
27
+
28
+ #include <freerdp/config.h>
29
+
30
+ #include <freerdp/types.h>
31
+ #include <freerdp/primitives.h>
32
+ #include <freerdp/codec/color.h>
33
+ #include "prim_internal.h"
34
+ #include "prim_YUV.h"
35
+
36
+ static inline pstatus_t general_LumaToYUV444(const BYTE* WINPR_RESTRICT pSrcRaw[3],
37
+ const UINT32 srcStep[3],
38
+ BYTE* WINPR_RESTRICT pDstRaw[3],
39
+ const UINT32 dstStep[3],
40
+ const RECTANGLE_16* WINPR_RESTRICT roi)
41
+ {
42
+ const UINT32 nWidth = roi->right - roi->left;
43
+ const UINT32 nHeight = roi->bottom - roi->top;
44
+ const UINT32 halfWidth = (nWidth + 1) / 2;
45
+ const UINT32 halfHeight = (nHeight + 1) / 2;
46
+ const UINT32 oddY = 1;
47
+ const UINT32 evenY = 0;
48
+ const UINT32 oddX = 1;
49
+ const UINT32 evenX = 0;
50
+ const BYTE* pSrc[3] = { pSrcRaw[0] + 1ULL * roi->top * srcStep[0] + roi->left,
51
+ pSrcRaw[1] + 1ULL * roi->top / 2 * srcStep[1] + roi->left / 2,
52
+ pSrcRaw[2] + 1ULL * roi->top / 2 * srcStep[2] + roi->left / 2 };
53
+ BYTE* pDst[3] = { pDstRaw[0] + 1ULL * roi->top * dstStep[0] + roi->left,
54
+ pDstRaw[1] + 1ULL * roi->top * dstStep[1] + roi->left,
55
+ pDstRaw[2] + 1ULL * roi->top * dstStep[2] + roi->left };
56
+
57
+ /* Y data is already here... */
58
+ /* B1 */
59
+ for (size_t y = 0; y < nHeight; y++)
60
+ {
61
+ const BYTE* Ym = pSrc[0] + y * srcStep[0];
62
+ BYTE* pY = pDst[0] + dstStep[0] * y;
63
+ memcpy(pY, Ym, nWidth);
64
+ }
65
+
66
+ /* The first half of U, V are already here part of this frame. */
67
+ /* B2 and B3 */
68
+ for (UINT32 y = 0; y < halfHeight; y++)
69
+ {
70
+ const UINT32 val2y = (2UL * y + evenY);
71
+ const UINT32 val2y1 = val2y + oddY;
72
+ const BYTE* Um = pSrc[1] + 1ULL * y * srcStep[1];
73
+ const BYTE* Vm = pSrc[2] + 1ULL * y * srcStep[2];
74
+ BYTE* pU = pDst[1] + 1ULL * dstStep[1] * val2y;
75
+ BYTE* pV = pDst[2] + 1ULL * dstStep[2] * val2y;
76
+ BYTE* pU1 = pDst[1] + 1ULL * dstStep[1] * val2y1;
77
+ BYTE* pV1 = pDst[2] + 1ULL * dstStep[2] * val2y1;
78
+
79
+ for (UINT32 x = 0; x < halfWidth; x++)
80
+ {
81
+ const UINT32 val2x = 2UL * x + evenX;
82
+ const UINT32 val2x1 = val2x + oddX;
83
+ pU[val2x] = Um[x];
84
+ pV[val2x] = Vm[x];
85
+ pU[val2x1] = Um[x];
86
+ pV[val2x1] = Vm[x];
87
+ pU1[val2x] = Um[x];
88
+ pV1[val2x] = Vm[x];
89
+ pU1[val2x1] = Um[x];
90
+ pV1[val2x1] = Vm[x];
91
+ }
92
+ }
93
+
94
+ return PRIMITIVES_SUCCESS;
95
+ }
96
+
97
+ static inline pstatus_t general_ChromaV1ToYUV444(const BYTE* WINPR_RESTRICT pSrcRaw[3],
98
+ const UINT32 srcStep[3],
99
+ BYTE* WINPR_RESTRICT pDstRaw[3],
100
+ const UINT32 dstStep[3],
101
+ const RECTANGLE_16* WINPR_RESTRICT roi)
102
+ {
103
+ const UINT32 mod = 16;
104
+ UINT32 uY = 0;
105
+ UINT32 vY = 0;
106
+ const UINT32 nWidth = roi->right - roi->left;
107
+ const UINT32 nHeight = roi->bottom - roi->top;
108
+ const UINT32 halfWidth = (nWidth) / 2;
109
+ const UINT32 halfHeight = (nHeight) / 2;
110
+ const UINT32 oddY = 1;
111
+ const UINT32 evenY = 0;
112
+ const UINT32 oddX = 1;
113
+ /* The auxiliary frame is aligned to multiples of 16x16.
114
+ * We need the padded height for B4 and B5 conversion. */
115
+ const UINT32 padHeigth = nHeight + 16 - nHeight % 16;
116
+ const BYTE* pSrc[3] = { pSrcRaw[0] + 1ULL * roi->top * srcStep[0] + roi->left,
117
+ pSrcRaw[1] + 1ULL * roi->top / 2 * srcStep[1] + roi->left / 2,
118
+ pSrcRaw[2] + 1ULL * roi->top / 2 * srcStep[2] + roi->left / 2 };
119
+ BYTE* pDst[3] = { pDstRaw[0] + 1ULL * roi->top * dstStep[0] + roi->left,
120
+ pDstRaw[1] + 1ULL * roi->top * dstStep[1] + roi->left,
121
+ pDstRaw[2] + 1ULL * roi->top * dstStep[2] + roi->left };
122
+
123
+ /* The second half of U and V is a bit more tricky... */
124
+ /* B4 and B5 */
125
+ for (size_t y = 0; y < padHeigth; y++)
126
+ {
127
+ const BYTE* Ya = pSrc[0] + y * srcStep[0];
128
+ BYTE* pX = NULL;
129
+
130
+ if ((y) % mod < (mod + 1) / 2)
131
+ {
132
+ const size_t pos = (2 * uY++ + oddY);
133
+
134
+ if (pos >= nHeight)
135
+ continue;
136
+
137
+ pX = pDst[1] + dstStep[1] * pos;
138
+ }
139
+ else
140
+ {
141
+ const size_t pos = (2 * vY++ + oddY);
142
+
143
+ if (pos >= nHeight)
144
+ continue;
145
+
146
+ pX = pDst[2] + dstStep[2] * pos;
147
+ }
148
+
149
+ memcpy(pX, Ya, nWidth);
150
+ }
151
+
152
+ /* B6 and B7 */
153
+ for (UINT32 y = 0; y < halfHeight; y++)
154
+ {
155
+ const UINT32 val2y = (y * 2UL + evenY);
156
+ const BYTE* Ua = pSrc[1] + 1ULL * y * srcStep[1];
157
+ const BYTE* Va = pSrc[2] + 1ULL * y * srcStep[2];
158
+ BYTE* pU = pDst[1] + 1ULL * dstStep[1] * val2y;
159
+ BYTE* pV = pDst[2] + 1ULL * dstStep[2] * val2y;
160
+
161
+ for (UINT32 x = 0; x < halfWidth; x++)
162
+ {
163
+ const UINT32 val2x1 = (x * 2 + oddX);
164
+ pU[val2x1] = Ua[x];
165
+ pV[val2x1] = Va[x];
166
+ }
167
+ }
168
+
169
+ return PRIMITIVES_SUCCESS;
170
+ }
171
+
172
+ static inline pstatus_t general_ChromaV2ToYUV444(const BYTE* WINPR_RESTRICT pSrc[3],
173
+ const UINT32 srcStep[3], UINT32 nTotalWidth,
174
+ UINT32 nTotalHeight, BYTE* WINPR_RESTRICT pDst[3],
175
+ const UINT32 dstStep[3],
176
+ const RECTANGLE_16* WINPR_RESTRICT roi)
177
+ {
178
+ const UINT32 nWidth = roi->right - roi->left;
179
+ const UINT32 nHeight = roi->bottom - roi->top;
180
+ const UINT32 halfWidth = (nWidth + 1) / 2;
181
+ const UINT32 halfHeight = (nHeight + 1) / 2;
182
+ const UINT32 quaterWidth = (nWidth + 3) / 4;
183
+
184
+ /* B4 and B5: odd UV values for width/2, height */
185
+ for (UINT32 y = 0; y < nHeight; y++)
186
+ {
187
+ const UINT32 yTop = y + roi->top;
188
+ const BYTE* pYaU = pSrc[0] + 1ULL * srcStep[0] * yTop + roi->left / 2;
189
+ const BYTE* pYaV = pYaU + nTotalWidth / 2;
190
+ BYTE* pU = pDst[1] + 1ULL * dstStep[1] * yTop + roi->left;
191
+ BYTE* pV = pDst[2] + 1ULL * dstStep[2] * yTop + roi->left;
192
+
193
+ for (UINT32 x = 0; x < halfWidth; x++)
194
+ {
195
+ const UINT32 odd = 2UL * x + 1UL;
196
+ pU[odd] = *pYaU++;
197
+ pV[odd] = *pYaV++;
198
+ }
199
+ }
200
+
201
+ /* B6 - B9 */
202
+ for (size_t y = 0; y < halfHeight; y++)
203
+ {
204
+ const BYTE* pUaU = pSrc[1] + srcStep[1] * (y + roi->top / 2) + roi->left / 4;
205
+ const BYTE* pUaV = pUaU + nTotalWidth / 4;
206
+ const BYTE* pVaU = pSrc[2] + srcStep[2] * (y + roi->top / 2) + roi->left / 4;
207
+ const BYTE* pVaV = pVaU + nTotalWidth / 4;
208
+ BYTE* pU = pDst[1] + 1ULL * dstStep[1] * (2ULL * y + 1 + roi->top) + roi->left;
209
+ BYTE* pV = pDst[2] + 1ULL * dstStep[2] * (2ULL * y + 1 + roi->top) + roi->left;
210
+
211
+ for (size_t x = 0; x < quaterWidth; x++)
212
+ {
213
+ pU[4 * x + 0] = *pUaU++;
214
+ pV[4 * x + 0] = *pUaV++;
215
+ pU[4 * x + 2] = *pVaU++;
216
+ pV[4 * x + 2] = *pVaV++;
217
+ }
218
+ }
219
+
220
+ return PRIMITIVES_SUCCESS;
221
+ }
222
+
223
+ static pstatus_t general_YUV420CombineToYUV444(avc444_frame_type type,
224
+ const BYTE* WINPR_RESTRICT pSrc[3],
225
+ const UINT32 srcStep[3], UINT32 nWidth,
226
+ UINT32 nHeight, BYTE* WINPR_RESTRICT pDst[3],
227
+ const UINT32 dstStep[3],
228
+ const RECTANGLE_16* WINPR_RESTRICT roi)
229
+ {
230
+ if (!pSrc || !pSrc[0] || !pSrc[1] || !pSrc[2])
231
+ return -1;
232
+
233
+ if (!pDst || !pDst[0] || !pDst[1] || !pDst[2])
234
+ return -1;
235
+
236
+ if (!roi)
237
+ return -1;
238
+
239
+ switch (type)
240
+ {
241
+ case AVC444_LUMA:
242
+ return general_LumaToYUV444(pSrc, srcStep, pDst, dstStep, roi);
243
+
244
+ case AVC444_CHROMAv1:
245
+ return general_ChromaV1ToYUV444(pSrc, srcStep, pDst, dstStep, roi);
246
+
247
+ case AVC444_CHROMAv2:
248
+ return general_ChromaV2ToYUV444(pSrc, srcStep, nWidth, nHeight, pDst, dstStep, roi);
249
+
250
+ default:
251
+ return -1;
252
+ }
253
+ }
254
+
255
+ static pstatus_t
256
+ general_YUV444SplitToYUV420(const BYTE* WINPR_RESTRICT pSrc[3], const UINT32 srcStep[3],
257
+ BYTE* WINPR_RESTRICT pMainDst[3], const UINT32 dstMainStep[3],
258
+ BYTE* WINPR_RESTRICT pAuxDst[3], const UINT32 dstAuxStep[3],
259
+ const prim_size_t* WINPR_RESTRICT roi)
260
+ {
261
+ UINT32 uY = 0;
262
+ UINT32 vY = 0;
263
+
264
+ /* The auxiliary frame is aligned to multiples of 16x16.
265
+ * We need the padded height for B4 and B5 conversion. */
266
+ const UINT32 padHeigth = roi->height + 16 - roi->height % 16;
267
+ const UINT32 halfWidth = (roi->width + 1) / 2;
268
+ const UINT32 halfHeight = (roi->height + 1) / 2;
269
+
270
+ /* B1 */
271
+ for (size_t y = 0; y < roi->height; y++)
272
+ {
273
+ const BYTE* pSrcY = pSrc[0] + y * srcStep[0];
274
+ BYTE* pY = pMainDst[0] + y * dstMainStep[0];
275
+ memcpy(pY, pSrcY, roi->width);
276
+ }
277
+
278
+ /* B2 and B3 */
279
+ for (size_t y = 0; y < halfHeight; y++)
280
+ {
281
+ const BYTE* pSrcU = pSrc[1] + 2ULL * y * srcStep[1];
282
+ const BYTE* pSrcV = pSrc[2] + 2ULL * y * srcStep[2];
283
+ BYTE* pU = pMainDst[1] + y * dstMainStep[1];
284
+ BYTE* pV = pMainDst[2] + y * dstMainStep[2];
285
+
286
+ for (size_t x = 0; x < halfWidth; x++)
287
+ {
288
+ pU[x] = pSrcV[2 * x];
289
+ pV[x] = pSrcU[2 * x];
290
+ }
291
+ }
292
+
293
+ /* B4 and B5 */
294
+ for (size_t y = 0; y < padHeigth; y++)
295
+ {
296
+ BYTE* pY = pAuxDst[0] + y * dstAuxStep[0];
297
+
298
+ if (y % 16 < 8)
299
+ {
300
+ const size_t pos = (2 * uY++ + 1);
301
+ const BYTE* pSrcU = pSrc[1] + pos * srcStep[1];
302
+
303
+ if (pos >= roi->height)
304
+ continue;
305
+
306
+ memcpy(pY, pSrcU, roi->width);
307
+ }
308
+ else
309
+ {
310
+ const size_t pos = (2 * vY++ + 1);
311
+ const BYTE* pSrcV = pSrc[2] + pos * srcStep[2];
312
+
313
+ if (pos >= roi->height)
314
+ continue;
315
+
316
+ memcpy(pY, pSrcV, roi->width);
317
+ }
318
+ }
319
+
320
+ /* B6 and B7 */
321
+ for (size_t y = 0; y < halfHeight; y++)
322
+ {
323
+ const BYTE* pSrcU = pSrc[1] + 2 * y * srcStep[1];
324
+ const BYTE* pSrcV = pSrc[2] + 2 * y * srcStep[2];
325
+ BYTE* pU = pAuxDst[1] + y * dstAuxStep[1];
326
+ BYTE* pV = pAuxDst[2] + y * dstAuxStep[2];
327
+
328
+ for (size_t x = 0; x < halfWidth; x++)
329
+ {
330
+ pU[x] = pSrcU[2 * x + 1];
331
+ pV[x] = pSrcV[2 * x + 1];
332
+ }
333
+ }
334
+
335
+ return PRIMITIVES_SUCCESS;
336
+ }
337
+
338
+ static inline void general_YUV444ToRGB_DOUBLE_ROW(BYTE* WINPR_RESTRICT pRGB[2], UINT32 DstFormat,
339
+ const BYTE* WINPR_RESTRICT pY[2],
340
+ const BYTE* WINPR_RESTRICT pU[2],
341
+ const BYTE* WINPR_RESTRICT pV[2], size_t nWidth)
342
+ {
343
+ const DWORD formatSize = FreeRDPGetBytesPerPixel(DstFormat);
344
+ fkt_writePixel writePixel = getPixelWriteFunction(DstFormat, FALSE);
345
+
346
+ WINPR_ASSERT(nWidth % 2 == 0);
347
+ for (size_t x = 0; x < nWidth; x += 2)
348
+ {
349
+ for (size_t i = 0; i < 2; i++)
350
+ {
351
+ for (size_t j = 0; j < 2; j++)
352
+ {
353
+ const BYTE y = pY[i][x + j];
354
+ INT32 u = pU[i][x + j];
355
+ INT32 v = pV[i][x + j];
356
+ if ((i == 0) && (j == 0))
357
+ {
358
+ const INT32 subU = (INT32)pU[0][x + 1] + pU[1][x] + pU[1][x + 1];
359
+ const INT32 avgU = ((4 * u) - subU);
360
+ u = CONDITIONAL_CLIP(avgU, WINPR_ASSERTING_INT_CAST(BYTE, u));
361
+
362
+ const INT32 subV = (INT32)pV[0][x + 1] + pV[1][x] + pV[1][x + 1];
363
+ const INT32 avgV = ((4 * v) - subV);
364
+ v = CONDITIONAL_CLIP(avgV, WINPR_ASSERTING_INT_CAST(BYTE, v));
365
+ }
366
+ const BYTE r = YUV2R(y, u, v);
367
+ const BYTE g = YUV2G(y, u, v);
368
+ const BYTE b = YUV2B(y, u, v);
369
+ pRGB[i] = writePixel(pRGB[i], formatSize, DstFormat, r, g, b, 0);
370
+ }
371
+ }
372
+ }
373
+ }
374
+
375
+ static inline void general_YUV444ToRGB_SINGLE_ROW(BYTE* WINPR_RESTRICT pRGB, UINT32 DstFormat,
376
+ const BYTE* WINPR_RESTRICT pY,
377
+ const BYTE* WINPR_RESTRICT pU,
378
+ const BYTE* WINPR_RESTRICT pV, size_t nWidth)
379
+ {
380
+ fkt_writePixel writePixel = getPixelWriteFunction(DstFormat, FALSE);
381
+ const DWORD formatSize = FreeRDPGetBytesPerPixel(DstFormat);
382
+
383
+ WINPR_ASSERT(nWidth % 2 == 0);
384
+ for (size_t x = 0; x < nWidth; x += 2)
385
+ {
386
+ for (size_t j = 0; j < 2; j++)
387
+ {
388
+ const BYTE r = YUV2R(pY[x + j], pU[x + j], pV[x + j]);
389
+ const BYTE g = YUV2G(pY[x + j], pU[x + j], pV[x + j]);
390
+ const BYTE b = YUV2B(pY[x + j], pU[x + j], pV[x + j]);
391
+ pRGB = writePixel(pRGB, formatSize, DstFormat, r, g, b, 0);
392
+ }
393
+ }
394
+ }
395
+
396
+ static inline pstatus_t general_YUV444ToRGB_8u_P3AC4R_general(const BYTE* WINPR_RESTRICT pSrc[3],
397
+ const UINT32 srcStep[3],
398
+ BYTE* WINPR_RESTRICT pDst,
399
+ UINT32 dstStep, UINT32 DstFormat,
400
+ const prim_size_t* WINPR_RESTRICT roi)
401
+ {
402
+ WINPR_ASSERT(pSrc);
403
+ WINPR_ASSERT(pDst);
404
+ WINPR_ASSERT(roi);
405
+
406
+ const UINT32 nWidth = roi->width;
407
+ const UINT32 nHeight = roi->height;
408
+
409
+ size_t y = 0;
410
+ for (; y < nHeight - nHeight % 2; y += 2)
411
+ {
412
+ const BYTE* WINPR_RESTRICT pY[2] = { pSrc[0] + y * srcStep[0],
413
+ pSrc[0] + (y + 1) * srcStep[0] };
414
+ const BYTE* WINPR_RESTRICT pU[2] = { pSrc[1] + y * srcStep[1],
415
+ pSrc[1] + (y + 1) * srcStep[1] };
416
+ const BYTE* WINPR_RESTRICT pV[2] = { pSrc[2] + y * srcStep[2],
417
+ pSrc[2] + (y + 1) * srcStep[2] };
418
+ BYTE* WINPR_RESTRICT pRGB[] = { pDst + y * dstStep, pDst + (y + 1) * dstStep };
419
+
420
+ general_YUV444ToRGB_DOUBLE_ROW(pRGB, DstFormat, pY, pU, pV, nWidth);
421
+ }
422
+ for (; y < nHeight; y++)
423
+ {
424
+ const BYTE* WINPR_RESTRICT pY = pSrc[0] + y * srcStep[0];
425
+ const BYTE* WINPR_RESTRICT pU = pSrc[1] + y * srcStep[1];
426
+ const BYTE* WINPR_RESTRICT pV = pSrc[2] + y * srcStep[2];
427
+ BYTE* WINPR_RESTRICT pRGB = pDst + y * dstStep;
428
+
429
+ general_YUV444ToRGB_SINGLE_ROW(pRGB, DstFormat, pY, pU, pV, nWidth);
430
+ }
431
+
432
+ return PRIMITIVES_SUCCESS;
433
+ }
434
+
435
+ static inline void general_YUV444ToBGRX_DOUBLE_ROW(BYTE* WINPR_RESTRICT pRGB[2], UINT32 DstFormat,
436
+ const BYTE* WINPR_RESTRICT pY[2],
437
+ const BYTE* WINPR_RESTRICT pU[2],
438
+ const BYTE* WINPR_RESTRICT pV[2], size_t nWidth)
439
+ {
440
+ const DWORD formatSize = FreeRDPGetBytesPerPixel(DstFormat);
441
+
442
+ WINPR_ASSERT(nWidth % 2 == 0);
443
+ for (size_t x = 0; x < nWidth; x += 2)
444
+ {
445
+ const INT32 subU = pU[0][x + 1] + pU[1][x] + pU[1][x + 1];
446
+ const INT32 avgU = ((4 * pU[0][x]) - subU);
447
+ const BYTE useU = CONDITIONAL_CLIP(avgU, pU[0][x]);
448
+ const INT32 subV = pV[0][x + 1] + pV[1][x] + pV[1][x + 1];
449
+ const INT32 avgV = ((4 * pV[0][x]) - subV);
450
+ const BYTE useV = CONDITIONAL_CLIP(avgV, pV[0][x]);
451
+
452
+ const BYTE U[2][2] = { { useU, pU[0][x + 1] }, { pU[1][x], pU[1][x + 1] } };
453
+ const BYTE V[2][2] = { { useV, pV[0][x + 1] }, { pV[1][x], pV[1][x + 1] } };
454
+
455
+ for (size_t i = 0; i < 2; i++)
456
+ {
457
+ for (size_t j = 0; j < 2; j++)
458
+ {
459
+ const BYTE r = YUV2R(pY[i][x + j], U[i][j], V[i][j]);
460
+ const BYTE g = YUV2G(pY[i][x + j], U[i][j], V[i][j]);
461
+ const BYTE b = YUV2B(pY[i][x + j], U[i][j], V[i][j]);
462
+ pRGB[i] = writePixelBGRX(pRGB[i], formatSize, DstFormat, r, g, b, 0);
463
+ }
464
+ }
465
+ }
466
+ }
467
+
468
+ static inline void general_YUV444ToBGRX_SINGLE_ROW(BYTE* WINPR_RESTRICT pRGB, UINT32 DstFormat,
469
+ const BYTE* WINPR_RESTRICT pY,
470
+ const BYTE* WINPR_RESTRICT pU,
471
+ const BYTE* WINPR_RESTRICT pV, size_t nWidth)
472
+ {
473
+ const DWORD formatSize = FreeRDPGetBytesPerPixel(DstFormat);
474
+
475
+ WINPR_ASSERT(nWidth % 2 == 0);
476
+ for (size_t x = 0; x < nWidth; x += 2)
477
+ {
478
+ for (size_t j = 0; j < 2; j++)
479
+ {
480
+ const BYTE r = YUV2R(pY[x + j], pU[x + j], pV[x + j]);
481
+ const BYTE g = YUV2G(pY[x + j], pU[x + j], pV[x + j]);
482
+ const BYTE b = YUV2B(pY[x + j], pU[x + j], pV[x + j]);
483
+ pRGB = writePixelBGRX(pRGB, formatSize, DstFormat, r, g, b, 0);
484
+ }
485
+ }
486
+ }
487
+
488
+ static inline pstatus_t general_YUV444ToRGB_8u_P3AC4R_BGRX(const BYTE* WINPR_RESTRICT pSrc[3],
489
+ const UINT32 srcStep[3],
490
+ BYTE* WINPR_RESTRICT pDst,
491
+ UINT32 dstStep, UINT32 DstFormat,
492
+ const prim_size_t* WINPR_RESTRICT roi)
493
+ {
494
+ WINPR_ASSERT(pSrc);
495
+ WINPR_ASSERT(pDst);
496
+ WINPR_ASSERT(roi);
497
+
498
+ const UINT32 nWidth = roi->width;
499
+ const UINT32 nHeight = roi->height;
500
+
501
+ size_t y = 0;
502
+ for (; y < nHeight - nHeight % 2; y += 2)
503
+ {
504
+ const BYTE* pY[2] = { pSrc[0] + y * srcStep[0], pSrc[0] + (y + 1) * srcStep[0] };
505
+ const BYTE* pU[2] = { pSrc[1] + y * srcStep[1], pSrc[1] + (y + 1) * srcStep[1] };
506
+ const BYTE* pV[2] = { pSrc[2] + y * srcStep[2], pSrc[2] + (y + 1) * srcStep[2] };
507
+ BYTE* pRGB[] = { pDst + y * dstStep, pDst + (y + 1) * dstStep };
508
+
509
+ general_YUV444ToBGRX_DOUBLE_ROW(pRGB, DstFormat, pY, pU, pV, nWidth);
510
+ }
511
+
512
+ for (; y < nHeight; y++)
513
+ {
514
+ const BYTE* WINPR_RESTRICT pY = pSrc[0] + y * srcStep[0];
515
+ const BYTE* WINPR_RESTRICT pU = pSrc[1] + y * srcStep[1];
516
+ const BYTE* WINPR_RESTRICT pV = pSrc[2] + y * srcStep[2];
517
+ BYTE* WINPR_RESTRICT pRGB = pDst + y * dstStep;
518
+
519
+ general_YUV444ToBGRX_SINGLE_ROW(pRGB, DstFormat, pY, pU, pV, nWidth);
520
+ }
521
+ return PRIMITIVES_SUCCESS;
522
+ }
523
+
524
+ static pstatus_t general_YUV444ToRGB_8u_P3AC4R(const BYTE* WINPR_RESTRICT pSrc[3],
525
+ const UINT32 srcStep[3], BYTE* WINPR_RESTRICT pDst,
526
+ UINT32 dstStep, UINT32 DstFormat,
527
+ const prim_size_t* WINPR_RESTRICT roi)
528
+ {
529
+ switch (DstFormat)
530
+ {
531
+ case PIXEL_FORMAT_BGRA32:
532
+ case PIXEL_FORMAT_BGRX32:
533
+ return general_YUV444ToRGB_8u_P3AC4R_BGRX(pSrc, srcStep, pDst, dstStep, DstFormat, roi);
534
+
535
+ default:
536
+ return general_YUV444ToRGB_8u_P3AC4R_general(pSrc, srcStep, pDst, dstStep, DstFormat,
537
+ roi);
538
+ }
539
+ }
540
+ /**
541
+ * | R | ( | 256 0 403 | | Y | )
542
+ * | G | = ( | 256 -48 -120 | | U - 128 | ) >> 8
543
+ * | B | ( | 256 475 0 | | V - 128 | )
544
+ */
545
+ static pstatus_t general_YUV420ToRGB_8u_P3AC4R(const BYTE* WINPR_RESTRICT pSrc[3],
546
+ const UINT32 srcStep[3], BYTE* WINPR_RESTRICT pDst,
547
+ UINT32 dstStep, UINT32 DstFormat,
548
+ const prim_size_t* WINPR_RESTRICT roi)
549
+ {
550
+ UINT32 dstPad = 0;
551
+ UINT32 srcPad[3];
552
+ BYTE Y = 0;
553
+ BYTE U = 0;
554
+ BYTE V = 0;
555
+ UINT32 halfWidth = 0;
556
+ UINT32 halfHeight = 0;
557
+ const BYTE* pY = NULL;
558
+ const BYTE* pU = NULL;
559
+ const BYTE* pV = NULL;
560
+ BYTE* pRGB = pDst;
561
+ UINT32 nWidth = 0;
562
+ UINT32 nHeight = 0;
563
+ UINT32 lastRow = 0;
564
+ UINT32 lastCol = 0;
565
+ const DWORD formatSize = FreeRDPGetBytesPerPixel(DstFormat);
566
+ fkt_writePixel writePixel = getPixelWriteFunction(DstFormat, FALSE);
567
+ pY = pSrc[0];
568
+ pU = pSrc[1];
569
+ pV = pSrc[2];
570
+ lastCol = roi->width & 0x01;
571
+ lastRow = roi->height & 0x01;
572
+ nWidth = (roi->width + 1) & (uint32_t)~0x0001;
573
+ nHeight = (roi->height + 1) & (uint32_t)~0x0001;
574
+ halfWidth = nWidth / 2;
575
+ halfHeight = nHeight / 2;
576
+ srcPad[0] = (srcStep[0] - nWidth);
577
+ srcPad[1] = (srcStep[1] - halfWidth);
578
+ srcPad[2] = (srcStep[2] - halfWidth);
579
+ dstPad = (dstStep - (nWidth * 4));
580
+
581
+ for (UINT32 y = 0; y < halfHeight;)
582
+ {
583
+ if (++y == halfHeight)
584
+ lastRow <<= 1;
585
+
586
+ for (UINT32 x = 0; x < halfWidth;)
587
+ {
588
+ BYTE r = 0;
589
+ BYTE g = 0;
590
+ BYTE b = 0;
591
+
592
+ if (++x == halfWidth)
593
+ lastCol <<= 1;
594
+
595
+ U = *pU++;
596
+ V = *pV++;
597
+ /* 1st pixel */
598
+ Y = *pY++;
599
+ r = YUV2R(Y, U, V);
600
+ g = YUV2G(Y, U, V);
601
+ b = YUV2B(Y, U, V);
602
+ pRGB = writePixel(pRGB, formatSize, DstFormat, r, g, b, 0);
603
+
604
+ /* 2nd pixel */
605
+ if (!(lastCol & 0x02))
606
+ {
607
+ Y = *pY++;
608
+ r = YUV2R(Y, U, V);
609
+ g = YUV2G(Y, U, V);
610
+ b = YUV2B(Y, U, V);
611
+ pRGB = writePixel(pRGB, formatSize, DstFormat, r, g, b, 0);
612
+ }
613
+ else
614
+ {
615
+ pY++;
616
+ pRGB += formatSize;
617
+ lastCol >>= 1;
618
+ }
619
+ }
620
+
621
+ pY += srcPad[0];
622
+ pU -= halfWidth;
623
+ pV -= halfWidth;
624
+ pRGB += dstPad;
625
+
626
+ if (lastRow & 0x02)
627
+ break;
628
+
629
+ for (UINT32 x = 0; x < halfWidth;)
630
+ {
631
+ BYTE r = 0;
632
+ BYTE g = 0;
633
+ BYTE b = 0;
634
+
635
+ if (++x == halfWidth)
636
+ lastCol <<= 1;
637
+
638
+ U = *pU++;
639
+ V = *pV++;
640
+ /* 3rd pixel */
641
+ Y = *pY++;
642
+ r = YUV2R(Y, U, V);
643
+ g = YUV2G(Y, U, V);
644
+ b = YUV2B(Y, U, V);
645
+ pRGB = writePixel(pRGB, formatSize, DstFormat, r, g, b, 0);
646
+
647
+ /* 4th pixel */
648
+ if (!(lastCol & 0x02))
649
+ {
650
+ Y = *pY++;
651
+ r = YUV2R(Y, U, V);
652
+ g = YUV2G(Y, U, V);
653
+ b = YUV2B(Y, U, V);
654
+ pRGB = writePixel(pRGB, formatSize, DstFormat, r, g, b, 0);
655
+ }
656
+ else
657
+ {
658
+ pY++;
659
+ pRGB += formatSize;
660
+ lastCol >>= 1;
661
+ }
662
+ }
663
+
664
+ pY += srcPad[0];
665
+ pU += srcPad[1];
666
+ pV += srcPad[2];
667
+ pRGB += dstPad;
668
+ }
669
+
670
+ return PRIMITIVES_SUCCESS;
671
+ }
672
+
673
+ static inline void BGRX_fillYUV(size_t offset, const BYTE* WINPR_RESTRICT pRGB[2],
674
+ BYTE* WINPR_RESTRICT pY[2], BYTE* WINPR_RESTRICT pU[2],
675
+ BYTE* WINPR_RESTRICT pV[2])
676
+ {
677
+ WINPR_ASSERT(pRGB);
678
+ WINPR_ASSERT(pY);
679
+ WINPR_ASSERT(pU);
680
+ WINPR_ASSERT(pV);
681
+
682
+ const UINT32 SrcFormat = PIXEL_FORMAT_BGRX32;
683
+ const UINT32 bpp = 4;
684
+
685
+ for (size_t i = 0; i < 2; i++)
686
+ {
687
+ for (size_t j = 0; j < 2; j++)
688
+ {
689
+ BYTE B = 0;
690
+ BYTE G = 0;
691
+ BYTE R = 0;
692
+ const UINT32 color = FreeRDPReadColor(&pRGB[i][(offset + j) * bpp], SrcFormat);
693
+ FreeRDPSplitColor(color, SrcFormat, &R, &G, &B, NULL, NULL);
694
+ pY[i][offset + j] = RGB2Y(R, G, B);
695
+ pU[i][offset + j] = RGB2U(R, G, B);
696
+ pV[i][offset + j] = RGB2V(R, G, B);
697
+ }
698
+ }
699
+
700
+ /* Apply chroma filter */
701
+ const INT32 avgU = (pU[0][offset] + pU[0][offset + 1] + pU[1][offset] + pU[1][offset + 1]) / 4;
702
+ pU[0][offset] = CONDITIONAL_CLIP(avgU, pU[0][offset]);
703
+ const INT32 avgV = (pV[0][offset] + pV[0][offset + 1] + pV[1][offset] + pV[1][offset + 1]) / 4;
704
+ pV[0][offset] = CONDITIONAL_CLIP(avgV, pV[0][offset]);
705
+ }
706
+
707
+ static inline void BGRX_fillYUV_single(size_t offset, const BYTE* WINPR_RESTRICT pRGB,
708
+ BYTE* WINPR_RESTRICT pY, BYTE* WINPR_RESTRICT pU,
709
+ BYTE* WINPR_RESTRICT pV)
710
+ {
711
+ WINPR_ASSERT(pRGB);
712
+ WINPR_ASSERT(pY);
713
+ WINPR_ASSERT(pU);
714
+ WINPR_ASSERT(pV);
715
+
716
+ const UINT32 SrcFormat = PIXEL_FORMAT_BGRX32;
717
+ const UINT32 bpp = 4;
718
+
719
+ for (size_t j = 0; j < 2; j++)
720
+ {
721
+ BYTE B = 0;
722
+ BYTE G = 0;
723
+ BYTE R = 0;
724
+ const UINT32 color = FreeRDPReadColor(&pRGB[(offset + j) * bpp], SrcFormat);
725
+ FreeRDPSplitColor(color, SrcFormat, &R, &G, &B, NULL, NULL);
726
+ pY[offset + j] = RGB2Y(R, G, B);
727
+ pU[offset + j] = RGB2U(R, G, B);
728
+ pV[offset + j] = RGB2V(R, G, B);
729
+ }
730
+ }
731
+
732
+ static inline void general_BGRXToYUV444_DOUBLE_ROW(const BYTE* WINPR_RESTRICT pRGB[2],
733
+ BYTE* WINPR_RESTRICT pY[2],
734
+ BYTE* WINPR_RESTRICT pU[2],
735
+ BYTE* WINPR_RESTRICT pV[2], UINT32 nWidth)
736
+ {
737
+
738
+ WINPR_ASSERT((nWidth % 2) == 0);
739
+ for (size_t x = 0; x < nWidth; x += 2)
740
+ {
741
+ BGRX_fillYUV(x, pRGB, pY, pU, pV);
742
+ }
743
+ }
744
+
745
+ static inline void general_BGRXToYUV444_SINGLE_ROW(const BYTE* WINPR_RESTRICT pRGB,
746
+ BYTE* WINPR_RESTRICT pY, BYTE* WINPR_RESTRICT pU,
747
+ BYTE* WINPR_RESTRICT pV, UINT32 nWidth)
748
+ {
749
+
750
+ WINPR_ASSERT((nWidth % 2) == 0);
751
+ for (size_t x = 0; x < nWidth; x += 2)
752
+ {
753
+ BGRX_fillYUV_single(x, pRGB, pY, pU, pV);
754
+ }
755
+ }
756
+
757
+ static inline pstatus_t general_RGBToYUV444_8u_P3AC4R_BGRX(const BYTE* WINPR_RESTRICT pSrc,
758
+ const UINT32 srcStep,
759
+ BYTE* WINPR_RESTRICT pDst[3],
760
+ const UINT32 dstStep[3],
761
+ const prim_size_t* WINPR_RESTRICT roi)
762
+ {
763
+ const UINT32 nWidth = roi->width;
764
+ const UINT32 nHeight = roi->height;
765
+
766
+ size_t y = 0;
767
+ for (; y < nHeight - nHeight % 2; y += 2)
768
+ {
769
+ const BYTE* pRGB[] = { pSrc + y * srcStep, pSrc + (y + 1) * srcStep };
770
+ BYTE* pY[] = { pDst[0] + y * dstStep[0], pDst[0] + (y + 1) * dstStep[0] };
771
+ BYTE* pU[] = { pDst[1] + y * dstStep[1], pDst[1] + (y + 1) * dstStep[1] };
772
+ BYTE* pV[] = { pDst[2] + y * dstStep[2], pDst[2] + (y + 1) * dstStep[2] };
773
+
774
+ general_BGRXToYUV444_DOUBLE_ROW(pRGB, pY, pU, pV, nWidth);
775
+ }
776
+
777
+ for (; y < nHeight; y++)
778
+ {
779
+ const BYTE* pRGB = pSrc + y * srcStep;
780
+ BYTE* pY = pDst[0] + y * dstStep[0];
781
+ BYTE* pU = pDst[1] + y * dstStep[1];
782
+ BYTE* pV = pDst[2] + y * dstStep[2];
783
+
784
+ general_BGRXToYUV444_SINGLE_ROW(pRGB, pY, pU, pV, nWidth);
785
+ }
786
+
787
+ return PRIMITIVES_SUCCESS;
788
+ }
789
+
790
+ static inline void fillYUV(size_t offset, const BYTE* WINPR_RESTRICT pRGB[2], UINT32 SrcFormat,
791
+ BYTE* WINPR_RESTRICT pY[2], BYTE* WINPR_RESTRICT pU[2],
792
+ BYTE* WINPR_RESTRICT pV[2])
793
+ {
794
+ WINPR_ASSERT(pRGB);
795
+ WINPR_ASSERT(pY);
796
+ WINPR_ASSERT(pU);
797
+ WINPR_ASSERT(pV);
798
+ const UINT32 bpp = FreeRDPGetBytesPerPixel(SrcFormat);
799
+
800
+ INT32 avgU = 0;
801
+ INT32 avgV = 0;
802
+ for (size_t i = 0; i < 2; i++)
803
+ {
804
+ for (size_t j = 0; j < 2; j++)
805
+ {
806
+ BYTE B = 0;
807
+ BYTE G = 0;
808
+ BYTE R = 0;
809
+ const UINT32 color = FreeRDPReadColor(&pRGB[i][(offset + j) * bpp], SrcFormat);
810
+ FreeRDPSplitColor(color, SrcFormat, &R, &G, &B, NULL, NULL);
811
+ const BYTE y = RGB2Y(R, G, B);
812
+ const BYTE u = RGB2U(R, G, B);
813
+ const BYTE v = RGB2V(R, G, B);
814
+ avgU += u;
815
+ avgV += v;
816
+ pY[i][offset + j] = y;
817
+ pU[i][offset + j] = u;
818
+ pV[i][offset + j] = v;
819
+ }
820
+ }
821
+
822
+ /* Apply chroma filter */
823
+ avgU /= 4;
824
+ pU[0][offset] = CLIP(avgU);
825
+
826
+ avgV /= 4;
827
+ pV[0][offset] = CLIP(avgV);
828
+ }
829
+
830
+ static inline void fillYUV_single(size_t offset, const BYTE* WINPR_RESTRICT pRGB, UINT32 SrcFormat,
831
+ BYTE* WINPR_RESTRICT pY, BYTE* WINPR_RESTRICT pU,
832
+ BYTE* WINPR_RESTRICT pV)
833
+ {
834
+ WINPR_ASSERT(pRGB);
835
+ WINPR_ASSERT(pY);
836
+ WINPR_ASSERT(pU);
837
+ WINPR_ASSERT(pV);
838
+ const UINT32 bpp = FreeRDPGetBytesPerPixel(SrcFormat);
839
+
840
+ for (size_t j = 0; j < 2; j++)
841
+ {
842
+ BYTE B = 0;
843
+ BYTE G = 0;
844
+ BYTE R = 0;
845
+ const UINT32 color = FreeRDPReadColor(&pRGB[(offset + j) * bpp], SrcFormat);
846
+ FreeRDPSplitColor(color, SrcFormat, &R, &G, &B, NULL, NULL);
847
+ const BYTE y = RGB2Y(R, G, B);
848
+ const BYTE u = RGB2U(R, G, B);
849
+ const BYTE v = RGB2V(R, G, B);
850
+ pY[offset + j] = y;
851
+ pU[offset + j] = u;
852
+ pV[offset + j] = v;
853
+ }
854
+ }
855
+
856
+ static inline void general_RGBToYUV444_DOUBLE_ROW(const BYTE* WINPR_RESTRICT pRGB[2],
857
+ UINT32 SrcFormat, BYTE* WINPR_RESTRICT pY[2],
858
+ BYTE* WINPR_RESTRICT pU[2],
859
+ BYTE* WINPR_RESTRICT pV[2], UINT32 nWidth)
860
+ {
861
+
862
+ WINPR_ASSERT((nWidth % 2) == 0);
863
+ for (size_t x = 0; x < nWidth; x += 2)
864
+ {
865
+ fillYUV(x, pRGB, SrcFormat, pY, pU, pV);
866
+ }
867
+ }
868
+
869
+ static inline void general_RGBToYUV444_SINGLE_ROW(const BYTE* WINPR_RESTRICT pRGB, UINT32 SrcFormat,
870
+ BYTE* WINPR_RESTRICT pY, BYTE* WINPR_RESTRICT pU,
871
+ BYTE* WINPR_RESTRICT pV, UINT32 nWidth)
872
+ {
873
+
874
+ WINPR_ASSERT((nWidth % 2) == 0);
875
+ for (size_t x = 0; x < nWidth; x += 2)
876
+ {
877
+ fillYUV_single(x, pRGB, SrcFormat, pY, pU, pV);
878
+ }
879
+ }
880
+
881
+ static inline pstatus_t general_RGBToYUV444_8u_P3AC4R_RGB(const BYTE* WINPR_RESTRICT pSrc,
882
+ UINT32 SrcFormat, const UINT32 srcStep,
883
+ BYTE* WINPR_RESTRICT pDst[3],
884
+ const UINT32 dstStep[3],
885
+ const prim_size_t* WINPR_RESTRICT roi)
886
+ {
887
+ const UINT32 nWidth = roi->width;
888
+ const UINT32 nHeight = roi->height;
889
+
890
+ size_t y = 0;
891
+ for (; y < nHeight - nHeight % 2; y += 2)
892
+ {
893
+ const BYTE* pRGB[] = { pSrc + y * srcStep, pSrc + (y + 1) * srcStep };
894
+ BYTE* pY[] = { &pDst[0][y * dstStep[0]], &pDst[0][(y + 1) * dstStep[0]] };
895
+ BYTE* pU[] = { &pDst[1][y * dstStep[1]], &pDst[1][(y + 1) * dstStep[1]] };
896
+ BYTE* pV[] = { &pDst[2][y * dstStep[2]], &pDst[2][(y + 1) * dstStep[2]] };
897
+
898
+ general_RGBToYUV444_DOUBLE_ROW(pRGB, SrcFormat, pY, pU, pV, nWidth);
899
+ }
900
+ for (; y < nHeight; y++)
901
+ {
902
+ const BYTE* pRGB = pSrc + y * srcStep;
903
+ BYTE* pY = &pDst[0][y * dstStep[0]];
904
+ BYTE* pU = &pDst[1][y * dstStep[1]];
905
+ BYTE* pV = &pDst[2][y * dstStep[2]];
906
+
907
+ general_RGBToYUV444_SINGLE_ROW(pRGB, SrcFormat, pY, pU, pV, nWidth);
908
+ }
909
+
910
+ return PRIMITIVES_SUCCESS;
911
+ }
912
+
913
+ static pstatus_t general_RGBToYUV444_8u_P3AC4R(const BYTE* WINPR_RESTRICT pSrc, UINT32 SrcFormat,
914
+ const UINT32 srcStep, BYTE* WINPR_RESTRICT pDst[3],
915
+ const UINT32 dstStep[3],
916
+ const prim_size_t* WINPR_RESTRICT roi)
917
+ {
918
+ switch (SrcFormat)
919
+ {
920
+ case PIXEL_FORMAT_BGRA32:
921
+ case PIXEL_FORMAT_BGRX32:
922
+ return general_RGBToYUV444_8u_P3AC4R_BGRX(pSrc, srcStep, pDst, dstStep, roi);
923
+ default:
924
+ return general_RGBToYUV444_8u_P3AC4R_RGB(pSrc, SrcFormat, srcStep, pDst, dstStep, roi);
925
+ }
926
+ }
927
+
928
+ static inline pstatus_t general_RGBToYUV420_BGRX(const BYTE* WINPR_RESTRICT pSrc, UINT32 srcStep,
929
+ BYTE* WINPR_RESTRICT pDst[3],
930
+ const UINT32 dstStep[3],
931
+ const prim_size_t* WINPR_RESTRICT roi)
932
+ {
933
+ size_t x1 = 0;
934
+ size_t x2 = 4;
935
+ size_t x3 = srcStep;
936
+ size_t x4 = srcStep + 4;
937
+ size_t y1 = 0;
938
+ size_t y2 = 1;
939
+ size_t y3 = dstStep[0];
940
+ size_t y4 = dstStep[0] + 1;
941
+ UINT32 max_x = roi->width - 1;
942
+
943
+ size_t y = 0;
944
+ for (size_t i = 0; y < roi->height - roi->height % 2; y += 2, i++)
945
+ {
946
+ const BYTE* src = pSrc + y * srcStep;
947
+ BYTE* ydst = pDst[0] + y * dstStep[0];
948
+ BYTE* udst = pDst[1] + i * dstStep[1];
949
+ BYTE* vdst = pDst[2] + i * dstStep[2];
950
+
951
+ for (size_t x = 0; x < roi->width; x += 2)
952
+ {
953
+ BYTE R = 0;
954
+ BYTE G = 0;
955
+ BYTE B = 0;
956
+ INT32 Ra = 0;
957
+ INT32 Ga = 0;
958
+ INT32 Ba = 0;
959
+ /* row 1, pixel 1 */
960
+ Ba = B = *(src + x1 + 0);
961
+ Ga = G = *(src + x1 + 1);
962
+ Ra = R = *(src + x1 + 2);
963
+ ydst[y1] = RGB2Y(R, G, B);
964
+
965
+ if (x < max_x)
966
+ {
967
+ /* row 1, pixel 2 */
968
+ Ba += B = *(src + x2 + 0);
969
+ Ga += G = *(src + x2 + 1);
970
+ Ra += R = *(src + x2 + 2);
971
+ ydst[y2] = RGB2Y(R, G, B);
972
+ }
973
+
974
+ /* row 2, pixel 1 */
975
+ Ba += B = *(src + x3 + 0);
976
+ Ga += G = *(src + x3 + 1);
977
+ Ra += R = *(src + x3 + 2);
978
+ ydst[y3] = RGB2Y(R, G, B);
979
+
980
+ if (x < max_x)
981
+ {
982
+ /* row 2, pixel 2 */
983
+ Ba += B = *(src + x4 + 0);
984
+ Ga += G = *(src + x4 + 1);
985
+ Ra += R = *(src + x4 + 2);
986
+ ydst[y4] = RGB2Y(R, G, B);
987
+ }
988
+
989
+ Ba >>= 2;
990
+ Ga >>= 2;
991
+ Ra >>= 2;
992
+ *udst++ = RGB2U(Ra, Ga, Ba);
993
+ *vdst++ = RGB2V(Ra, Ga, Ba);
994
+ ydst += 2;
995
+ src += 8;
996
+ }
997
+ }
998
+
999
+ for (; y < roi->height; y++)
1000
+ {
1001
+ const BYTE* src = pSrc + y * srcStep;
1002
+ BYTE* ydst = pDst[0] + y * dstStep[0];
1003
+ BYTE* udst = pDst[1] + (y / 2) * dstStep[1];
1004
+ BYTE* vdst = pDst[2] + (y / 2) * dstStep[2];
1005
+
1006
+ for (size_t x = 0; x < roi->width; x += 2)
1007
+ {
1008
+ BYTE R = 0;
1009
+ BYTE G = 0;
1010
+ BYTE B = 0;
1011
+ INT32 Ra = 0;
1012
+ INT32 Ga = 0;
1013
+ INT32 Ba = 0;
1014
+ /* row 1, pixel 1 */
1015
+ Ba = B = *(src + x1 + 0);
1016
+ Ga = G = *(src + x1 + 1);
1017
+ Ra = R = *(src + x1 + 2);
1018
+ ydst[y1] = RGB2Y(R, G, B);
1019
+
1020
+ if (x < max_x)
1021
+ {
1022
+ /* row 1, pixel 2 */
1023
+ Ba += B = *(src + x2 + 0);
1024
+ Ga += G = *(src + x2 + 1);
1025
+ Ra += R = *(src + x2 + 2);
1026
+ ydst[y2] = RGB2Y(R, G, B);
1027
+ }
1028
+
1029
+ Ba >>= 2;
1030
+ Ga >>= 2;
1031
+ Ra >>= 2;
1032
+ *udst++ = RGB2U(Ra, Ga, Ba);
1033
+ *vdst++ = RGB2V(Ra, Ga, Ba);
1034
+ ydst += 2;
1035
+ src += 8;
1036
+ }
1037
+ }
1038
+
1039
+ return PRIMITIVES_SUCCESS;
1040
+ }
1041
+
1042
+ static inline pstatus_t general_RGBToYUV420_RGBX(const BYTE* WINPR_RESTRICT pSrc, UINT32 srcStep,
1043
+ BYTE* WINPR_RESTRICT pDst[3],
1044
+ const UINT32 dstStep[3],
1045
+ const prim_size_t* WINPR_RESTRICT roi)
1046
+ {
1047
+ size_t x1 = 0;
1048
+ size_t x2 = 4;
1049
+ size_t x3 = srcStep;
1050
+ size_t x4 = srcStep + 4;
1051
+ size_t y1 = 0;
1052
+ size_t y2 = 1;
1053
+ size_t y3 = dstStep[0];
1054
+ size_t y4 = dstStep[0] + 1;
1055
+ UINT32 max_x = roi->width - 1;
1056
+
1057
+ size_t y = 0;
1058
+ for (size_t i = 0; y < roi->height - roi->height % 2; y += 2, i++)
1059
+ {
1060
+ const BYTE* src = pSrc + y * srcStep;
1061
+ BYTE* ydst = pDst[0] + y * dstStep[0];
1062
+ BYTE* udst = pDst[1] + i * dstStep[1];
1063
+ BYTE* vdst = pDst[2] + i * dstStep[2];
1064
+
1065
+ for (UINT32 x = 0; x < roi->width; x += 2)
1066
+ {
1067
+ BYTE R = *(src + x1 + 0);
1068
+ BYTE G = *(src + x1 + 1);
1069
+ BYTE B = *(src + x1 + 2);
1070
+ /* row 1, pixel 1 */
1071
+ INT32 Ra = R;
1072
+ INT32 Ga = G;
1073
+ INT32 Ba = B;
1074
+ ydst[y1] = RGB2Y(R, G, B);
1075
+
1076
+ if (x < max_x)
1077
+ {
1078
+ /* row 1, pixel 2 */
1079
+ R = *(src + x2 + 0);
1080
+ G = *(src + x2 + 1);
1081
+ B = *(src + x2 + 2);
1082
+ Ra += R;
1083
+ Ga += G;
1084
+ Ba += B;
1085
+ ydst[y2] = RGB2Y(R, G, B);
1086
+ }
1087
+
1088
+ /* row 2, pixel 1 */
1089
+ R = *(src + x3 + 0);
1090
+ G = *(src + x3 + 1);
1091
+ B = *(src + x3 + 2);
1092
+
1093
+ Ra += R;
1094
+ Ga += G;
1095
+ Ba += B;
1096
+ ydst[y3] = RGB2Y(R, G, B);
1097
+
1098
+ if (x < max_x)
1099
+ {
1100
+ /* row 2, pixel 2 */
1101
+ R = *(src + x4 + 0);
1102
+ G = *(src + x4 + 1);
1103
+ B = *(src + x4 + 2);
1104
+
1105
+ Ra += R;
1106
+ Ga += G;
1107
+ Ba += B;
1108
+ ydst[y4] = RGB2Y(R, G, B);
1109
+ }
1110
+
1111
+ Ba >>= 2;
1112
+ Ga >>= 2;
1113
+ Ra >>= 2;
1114
+ *udst++ = RGB2U(Ra, Ga, Ba);
1115
+ *vdst++ = RGB2V(Ra, Ga, Ba);
1116
+ ydst += 2;
1117
+ src += 8;
1118
+ }
1119
+ }
1120
+
1121
+ for (; y < roi->height; y++)
1122
+ {
1123
+ const BYTE* src = pSrc + y * srcStep;
1124
+ BYTE* ydst = pDst[0] + y * dstStep[0];
1125
+ BYTE* udst = pDst[1] + (y / 2) * dstStep[1];
1126
+ BYTE* vdst = pDst[2] + (y / 2) * dstStep[2];
1127
+
1128
+ for (UINT32 x = 0; x < roi->width; x += 2)
1129
+ {
1130
+ BYTE R = *(src + x1 + 0);
1131
+ BYTE G = *(src + x1 + 1);
1132
+ BYTE B = *(src + x1 + 2);
1133
+ /* row 1, pixel 1 */
1134
+ INT32 Ra = R;
1135
+ INT32 Ga = G;
1136
+ INT32 Ba = B;
1137
+ ydst[y1] = RGB2Y(R, G, B);
1138
+
1139
+ if (x < max_x)
1140
+ {
1141
+ /* row 1, pixel 2 */
1142
+ R = *(src + x2 + 0);
1143
+ G = *(src + x2 + 1);
1144
+ B = *(src + x2 + 2);
1145
+ Ra += R;
1146
+ Ga += G;
1147
+ Ba += B;
1148
+ ydst[y2] = RGB2Y(R, G, B);
1149
+ }
1150
+
1151
+ Ba >>= 2;
1152
+ Ga >>= 2;
1153
+ Ra >>= 2;
1154
+ *udst++ = RGB2U(Ra, Ga, Ba);
1155
+ *vdst++ = RGB2V(Ra, Ga, Ba);
1156
+ ydst += 2;
1157
+ src += 8;
1158
+ }
1159
+ }
1160
+
1161
+ return PRIMITIVES_SUCCESS;
1162
+ }
1163
+
1164
+ static inline pstatus_t general_RGBToYUV420_ANY(const BYTE* WINPR_RESTRICT pSrc, UINT32 srcFormat,
1165
+ UINT32 srcStep, BYTE* WINPR_RESTRICT pDst[3],
1166
+ const UINT32 dstStep[3],
1167
+ const prim_size_t* WINPR_RESTRICT roi)
1168
+ {
1169
+ const UINT32 bpp = FreeRDPGetBytesPerPixel(srcFormat);
1170
+ size_t x1 = 0;
1171
+ size_t x2 = bpp;
1172
+ size_t x3 = srcStep;
1173
+ size_t x4 = srcStep + bpp;
1174
+ size_t y1 = 0;
1175
+ size_t y2 = 1;
1176
+ size_t y3 = dstStep[0];
1177
+ size_t y4 = dstStep[0] + 1;
1178
+ UINT32 max_x = roi->width - 1;
1179
+
1180
+ size_t y = 0;
1181
+ for (size_t i = 0; y < roi->height - roi->height % 2; y += 2, i++)
1182
+ {
1183
+ const BYTE* src = pSrc + y * srcStep;
1184
+ BYTE* ydst = pDst[0] + y * dstStep[0];
1185
+ BYTE* udst = pDst[1] + i * dstStep[1];
1186
+ BYTE* vdst = pDst[2] + i * dstStep[2];
1187
+
1188
+ for (size_t x = 0; x < roi->width; x += 2)
1189
+ {
1190
+ BYTE R = 0;
1191
+ BYTE G = 0;
1192
+ BYTE B = 0;
1193
+ INT32 Ra = 0;
1194
+ INT32 Ga = 0;
1195
+ INT32 Ba = 0;
1196
+ UINT32 color = 0;
1197
+ /* row 1, pixel 1 */
1198
+ color = FreeRDPReadColor(src + x1, srcFormat);
1199
+ FreeRDPSplitColor(color, srcFormat, &R, &G, &B, NULL, NULL);
1200
+ Ra = R;
1201
+ Ga = G;
1202
+ Ba = B;
1203
+ ydst[y1] = RGB2Y(R, G, B);
1204
+
1205
+ if (x < max_x)
1206
+ {
1207
+ /* row 1, pixel 2 */
1208
+ color = FreeRDPReadColor(src + x2, srcFormat);
1209
+ FreeRDPSplitColor(color, srcFormat, &R, &G, &B, NULL, NULL);
1210
+ Ra += R;
1211
+ Ga += G;
1212
+ Ba += B;
1213
+ ydst[y2] = RGB2Y(R, G, B);
1214
+ }
1215
+
1216
+ /* row 2, pixel 1 */
1217
+ color = FreeRDPReadColor(src + x3, srcFormat);
1218
+ FreeRDPSplitColor(color, srcFormat, &R, &G, &B, NULL, NULL);
1219
+ Ra += R;
1220
+ Ga += G;
1221
+ Ba += B;
1222
+ ydst[y3] = RGB2Y(R, G, B);
1223
+
1224
+ if (x < max_x)
1225
+ {
1226
+ /* row 2, pixel 2 */
1227
+ color = FreeRDPReadColor(src + x4, srcFormat);
1228
+ FreeRDPSplitColor(color, srcFormat, &R, &G, &B, NULL, NULL);
1229
+ Ra += R;
1230
+ Ga += G;
1231
+ Ba += B;
1232
+ ydst[y4] = RGB2Y(R, G, B);
1233
+ }
1234
+
1235
+ Ra >>= 2;
1236
+ Ga >>= 2;
1237
+ Ba >>= 2;
1238
+ *udst++ = RGB2U(Ra, Ga, Ba);
1239
+ *vdst++ = RGB2V(Ra, Ga, Ba);
1240
+ ydst += 2;
1241
+ src += 2ULL * bpp;
1242
+ }
1243
+ }
1244
+
1245
+ for (; y < roi->height; y++)
1246
+ {
1247
+ const BYTE* src = pSrc + y * srcStep;
1248
+ BYTE* ydst = pDst[0] + y * dstStep[0];
1249
+ BYTE* udst = pDst[1] + (y / 2) * dstStep[1];
1250
+ BYTE* vdst = pDst[2] + (y / 2) * dstStep[2];
1251
+
1252
+ for (size_t x = 0; x < roi->width; x += 2)
1253
+ {
1254
+ BYTE R = 0;
1255
+ BYTE G = 0;
1256
+ BYTE B = 0;
1257
+ /* row 1, pixel 1 */
1258
+ UINT32 color = FreeRDPReadColor(src + x1, srcFormat);
1259
+ FreeRDPSplitColor(color, srcFormat, &R, &G, &B, NULL, NULL);
1260
+ INT32 Ra = R;
1261
+ INT32 Ga = G;
1262
+ INT32 Ba = B;
1263
+ ydst[y1] = RGB2Y(R, G, B);
1264
+
1265
+ if (x < max_x)
1266
+ {
1267
+ /* row 1, pixel 2 */
1268
+ color = FreeRDPReadColor(src + x2, srcFormat);
1269
+ FreeRDPSplitColor(color, srcFormat, &R, &G, &B, NULL, NULL);
1270
+ Ra += R;
1271
+ Ga += G;
1272
+ Ba += B;
1273
+ ydst[y2] = RGB2Y(R, G, B);
1274
+ }
1275
+
1276
+ Ra >>= 2;
1277
+ Ga >>= 2;
1278
+ Ba >>= 2;
1279
+ *udst++ = RGB2U(Ra, Ga, Ba);
1280
+ *vdst++ = RGB2V(Ra, Ga, Ba);
1281
+ ydst += 2;
1282
+ src += 2ULL * bpp;
1283
+ }
1284
+ }
1285
+
1286
+ return PRIMITIVES_SUCCESS;
1287
+ }
1288
+
1289
+ static pstatus_t general_RGBToYUV420_8u_P3AC4R(const BYTE* WINPR_RESTRICT pSrc, UINT32 srcFormat,
1290
+ UINT32 srcStep, BYTE* WINPR_RESTRICT pDst[3],
1291
+ const UINT32 dstStep[3],
1292
+ const prim_size_t* WINPR_RESTRICT roi)
1293
+ {
1294
+ switch (srcFormat)
1295
+ {
1296
+ case PIXEL_FORMAT_BGRA32:
1297
+ case PIXEL_FORMAT_BGRX32:
1298
+ return general_RGBToYUV420_BGRX(pSrc, srcStep, pDst, dstStep, roi);
1299
+
1300
+ case PIXEL_FORMAT_RGBA32:
1301
+ case PIXEL_FORMAT_RGBX32:
1302
+ return general_RGBToYUV420_RGBX(pSrc, srcStep, pDst, dstStep, roi);
1303
+
1304
+ default:
1305
+ return general_RGBToYUV420_ANY(pSrc, srcFormat, srcStep, pDst, dstStep, roi);
1306
+ }
1307
+ }
1308
+
1309
+ static inline void int_general_RGBToAVC444YUV_BGRX_DOUBLE_ROW(
1310
+ size_t offset, const BYTE* WINPR_RESTRICT pSrcEven, const BYTE* WINPR_RESTRICT pSrcOdd,
1311
+ BYTE* WINPR_RESTRICT b1Even, BYTE* WINPR_RESTRICT b1Odd, BYTE* WINPR_RESTRICT b2,
1312
+ BYTE* WINPR_RESTRICT b3, BYTE* WINPR_RESTRICT b4, BYTE* WINPR_RESTRICT b5,
1313
+ BYTE* WINPR_RESTRICT b6, BYTE* WINPR_RESTRICT b7, UINT32 width)
1314
+ {
1315
+ WINPR_ASSERT((width % 2) == 0);
1316
+ for (size_t x = offset; x < width; x += 2)
1317
+ {
1318
+ const BYTE* srcEven = &pSrcEven[4ULL * x];
1319
+ const BYTE* srcOdd = &pSrcOdd[4ULL * x];
1320
+ const BOOL lastX = (x + 1) >= width;
1321
+ BYTE Y1e = 0;
1322
+ BYTE Y2e = 0;
1323
+ BYTE U1e = 0;
1324
+ BYTE V1e = 0;
1325
+ BYTE U2e = 0;
1326
+ BYTE V2e = 0;
1327
+ BYTE Y1o = 0;
1328
+ BYTE Y2o = 0;
1329
+ BYTE U1o = 0;
1330
+ BYTE V1o = 0;
1331
+ BYTE U2o = 0;
1332
+ BYTE V2o = 0;
1333
+ /* Read 4 pixels, 2 from even, 2 from odd lines */
1334
+ {
1335
+ const BYTE b = *srcEven++;
1336
+ const BYTE g = *srcEven++;
1337
+ const BYTE r = *srcEven++;
1338
+ srcEven++;
1339
+ Y1e = Y2e = Y1o = Y2o = RGB2Y(r, g, b);
1340
+ U1e = U2e = U1o = U2o = RGB2U(r, g, b);
1341
+ V1e = V2e = V1o = V2o = RGB2V(r, g, b);
1342
+ }
1343
+
1344
+ if (!lastX)
1345
+ {
1346
+ const BYTE b = *srcEven++;
1347
+ const BYTE g = *srcEven++;
1348
+ const BYTE r = *srcEven++;
1349
+ srcEven++;
1350
+ Y2e = RGB2Y(r, g, b);
1351
+ U2e = RGB2U(r, g, b);
1352
+ V2e = RGB2V(r, g, b);
1353
+ }
1354
+
1355
+ if (b1Odd)
1356
+ {
1357
+ const BYTE b = *srcOdd++;
1358
+ const BYTE g = *srcOdd++;
1359
+ const BYTE r = *srcOdd++;
1360
+ srcOdd++;
1361
+ Y1o = Y2o = RGB2Y(r, g, b);
1362
+ U1o = U2o = RGB2U(r, g, b);
1363
+ V1o = V2o = RGB2V(r, g, b);
1364
+ }
1365
+
1366
+ if (b1Odd && !lastX)
1367
+ {
1368
+ const BYTE b = *srcOdd++;
1369
+ const BYTE g = *srcOdd++;
1370
+ const BYTE r = *srcOdd++;
1371
+ srcOdd++;
1372
+ Y2o = RGB2Y(r, g, b);
1373
+ U2o = RGB2U(r, g, b);
1374
+ V2o = RGB2V(r, g, b);
1375
+ }
1376
+
1377
+ /* We have 4 Y pixels, so store them. */
1378
+ *b1Even++ = Y1e;
1379
+ *b1Even++ = Y2e;
1380
+
1381
+ if (b1Odd)
1382
+ {
1383
+ *b1Odd++ = Y1o;
1384
+ *b1Odd++ = Y2o;
1385
+ }
1386
+
1387
+ /* 2x 2y pixel in luma UV plane use averaging
1388
+ */
1389
+ {
1390
+ const BYTE Uavg = WINPR_ASSERTING_INT_CAST(BYTE, ((UINT16)U1e + U2e + U1o + U2o) / 4);
1391
+ const BYTE Vavg = WINPR_ASSERTING_INT_CAST(BYTE, ((UINT16)V1e + V2e + V1o + V2o) / 4);
1392
+ *b2++ = Uavg;
1393
+ *b3++ = Vavg;
1394
+ }
1395
+
1396
+ /* UV from 2x, 2y+1 */
1397
+ if (b1Odd)
1398
+ {
1399
+ *b4++ = U1o;
1400
+ *b5++ = V1o;
1401
+
1402
+ if (!lastX)
1403
+ {
1404
+ *b4++ = U2o;
1405
+ *b5++ = V2o;
1406
+ }
1407
+ }
1408
+
1409
+ /* UV from 2x+1, 2y */
1410
+ if (!lastX)
1411
+ {
1412
+ *b6++ = U2e;
1413
+ *b7++ = V2e;
1414
+ }
1415
+ }
1416
+ }
1417
+
1418
+ void general_RGBToAVC444YUV_BGRX_DOUBLE_ROW(size_t offset, const BYTE* WINPR_RESTRICT pSrcEven,
1419
+ const BYTE* WINPR_RESTRICT pSrcOdd,
1420
+ BYTE* WINPR_RESTRICT b1Even, BYTE* WINPR_RESTRICT b1Odd,
1421
+ BYTE* WINPR_RESTRICT b2, BYTE* WINPR_RESTRICT b3,
1422
+ BYTE* WINPR_RESTRICT b4, BYTE* WINPR_RESTRICT b5,
1423
+ BYTE* WINPR_RESTRICT b6, BYTE* WINPR_RESTRICT b7,
1424
+ UINT32 width)
1425
+ {
1426
+ int_general_RGBToAVC444YUV_BGRX_DOUBLE_ROW(offset, pSrcEven, pSrcOdd, b1Even, b1Odd, b2, b3, b4,
1427
+ b5, b6, b7, width);
1428
+ }
1429
+
1430
+ static inline pstatus_t general_RGBToAVC444YUV_BGRX(const BYTE* WINPR_RESTRICT pSrc, UINT32 srcStep,
1431
+ BYTE* WINPR_RESTRICT pDst1[3],
1432
+ const UINT32 dst1Step[3],
1433
+ BYTE* WINPR_RESTRICT pDst2[3],
1434
+ const UINT32 dst2Step[3],
1435
+ const prim_size_t* WINPR_RESTRICT roi)
1436
+ {
1437
+ /**
1438
+ * Note:
1439
+ * Read information in function general_RGBToAVC444YUV_ANY below !
1440
+ */
1441
+ size_t y = 0;
1442
+ for (; y < roi->height - roi->height % 2; y += 2)
1443
+ {
1444
+ const BYTE* srcEven = pSrc + 1ULL * y * srcStep;
1445
+ const BYTE* srcOdd = pSrc + 1ULL * (y + 1) * srcStep;
1446
+ const size_t i = y >> 1;
1447
+ const size_t n = (i & (uint32_t)~7) + i;
1448
+ BYTE* b1Even = pDst1[0] + 1ULL * y * dst1Step[0];
1449
+ BYTE* b1Odd = (b1Even + dst1Step[0]);
1450
+ BYTE* b2 = pDst1[1] + 1ULL * (y / 2) * dst1Step[1];
1451
+ BYTE* b3 = pDst1[2] + 1ULL * (y / 2) * dst1Step[2];
1452
+ BYTE* b4 = pDst2[0] + 1ULL * dst2Step[0] * n;
1453
+ BYTE* b5 = b4 + 8ULL * dst2Step[0];
1454
+ BYTE* b6 = pDst2[1] + 1ULL * (y / 2) * dst2Step[1];
1455
+ BYTE* b7 = pDst2[2] + 1ULL * (y / 2) * dst2Step[2];
1456
+ int_general_RGBToAVC444YUV_BGRX_DOUBLE_ROW(0, srcEven, srcOdd, b1Even, b1Odd, b2, b3, b4,
1457
+ b5, b6, b7, roi->width);
1458
+ }
1459
+ for (; y < roi->height; y++)
1460
+ {
1461
+ const BYTE* srcEven = pSrc + 1ULL * y * srcStep;
1462
+ BYTE* b1Even = pDst1[0] + 1ULL * y * dst1Step[0];
1463
+ BYTE* b2 = pDst1[1] + 1ULL * (y / 2) * dst1Step[1];
1464
+ BYTE* b3 = pDst1[2] + 1ULL * (y / 2) * dst1Step[2];
1465
+ BYTE* b6 = pDst2[1] + 1ULL * (y / 2) * dst2Step[1];
1466
+ BYTE* b7 = pDst2[2] + 1ULL * (y / 2) * dst2Step[2];
1467
+ int_general_RGBToAVC444YUV_BGRX_DOUBLE_ROW(0, srcEven, NULL, b1Even, NULL, b2, b3, NULL,
1468
+ NULL, b6, b7, roi->width);
1469
+ }
1470
+
1471
+ return PRIMITIVES_SUCCESS;
1472
+ }
1473
+
1474
+ static inline void general_RGBToAVC444YUV_RGBX_DOUBLE_ROW(
1475
+ const BYTE* WINPR_RESTRICT srcEven, const BYTE* WINPR_RESTRICT srcOdd,
1476
+ BYTE* WINPR_RESTRICT b1Even, BYTE* WINPR_RESTRICT b1Odd, BYTE* WINPR_RESTRICT b2,
1477
+ BYTE* WINPR_RESTRICT b3, BYTE* WINPR_RESTRICT b4, BYTE* WINPR_RESTRICT b5,
1478
+ BYTE* WINPR_RESTRICT b6, BYTE* WINPR_RESTRICT b7, UINT32 width)
1479
+ {
1480
+ WINPR_ASSERT((width % 2) == 0);
1481
+ for (UINT32 x = 0; x < width; x += 2)
1482
+ {
1483
+ const BOOL lastX = (x + 1) >= width;
1484
+ BYTE Y1e = 0;
1485
+ BYTE Y2e = 0;
1486
+ BYTE U1e = 0;
1487
+ BYTE V1e = 0;
1488
+ BYTE U2e = 0;
1489
+ BYTE V2e = 0;
1490
+ BYTE Y1o = 0;
1491
+ BYTE Y2o = 0;
1492
+ BYTE U1o = 0;
1493
+ BYTE V1o = 0;
1494
+ BYTE U2o = 0;
1495
+ BYTE V2o = 0;
1496
+ /* Read 4 pixels, 2 from even, 2 from odd lines */
1497
+ {
1498
+ const BYTE r = *srcEven++;
1499
+ const BYTE g = *srcEven++;
1500
+ const BYTE b = *srcEven++;
1501
+ srcEven++;
1502
+ Y1e = Y2e = Y1o = Y2o = RGB2Y(r, g, b);
1503
+ U1e = U2e = U1o = U2o = RGB2U(r, g, b);
1504
+ V1e = V2e = V1o = V2o = RGB2V(r, g, b);
1505
+ }
1506
+
1507
+ if (!lastX)
1508
+ {
1509
+ const BYTE r = *srcEven++;
1510
+ const BYTE g = *srcEven++;
1511
+ const BYTE b = *srcEven++;
1512
+ srcEven++;
1513
+ Y2e = RGB2Y(r, g, b);
1514
+ U2e = RGB2U(r, g, b);
1515
+ V2e = RGB2V(r, g, b);
1516
+ }
1517
+
1518
+ if (b1Odd)
1519
+ {
1520
+ const BYTE r = *srcOdd++;
1521
+ const BYTE g = *srcOdd++;
1522
+ const BYTE b = *srcOdd++;
1523
+ srcOdd++;
1524
+ Y1o = Y2o = RGB2Y(r, g, b);
1525
+ U1o = U2o = RGB2U(r, g, b);
1526
+ V1o = V2o = RGB2V(r, g, b);
1527
+ }
1528
+
1529
+ if (b1Odd && !lastX)
1530
+ {
1531
+ const BYTE r = *srcOdd++;
1532
+ const BYTE g = *srcOdd++;
1533
+ const BYTE b = *srcOdd++;
1534
+ srcOdd++;
1535
+ Y2o = RGB2Y(r, g, b);
1536
+ U2o = RGB2U(r, g, b);
1537
+ V2o = RGB2V(r, g, b);
1538
+ }
1539
+
1540
+ /* We have 4 Y pixels, so store them. */
1541
+ *b1Even++ = Y1e;
1542
+ *b1Even++ = Y2e;
1543
+
1544
+ if (b1Odd)
1545
+ {
1546
+ *b1Odd++ = Y1o;
1547
+ *b1Odd++ = Y2o;
1548
+ }
1549
+
1550
+ /* 2x 2y pixel in luma UV plane use averaging
1551
+ */
1552
+ {
1553
+ const BYTE Uavg = WINPR_ASSERTING_INT_CAST(BYTE, ((UINT16)U1e + U2e + U1o + U2o) / 4);
1554
+ const BYTE Vavg = WINPR_ASSERTING_INT_CAST(BYTE, ((UINT16)V1e + V2e + V1o + V2o) / 4);
1555
+ *b2++ = Uavg;
1556
+ *b3++ = Vavg;
1557
+ }
1558
+
1559
+ /* UV from 2x, 2y+1 */
1560
+ if (b1Odd)
1561
+ {
1562
+ *b4++ = U1o;
1563
+ *b5++ = V1o;
1564
+
1565
+ if (!lastX)
1566
+ {
1567
+ *b4++ = U2o;
1568
+ *b5++ = V2o;
1569
+ }
1570
+ }
1571
+
1572
+ /* UV from 2x+1, 2y */
1573
+ if (!lastX)
1574
+ {
1575
+ *b6++ = U2e;
1576
+ *b7++ = V2e;
1577
+ }
1578
+ }
1579
+ }
1580
+
1581
+ static inline pstatus_t general_RGBToAVC444YUV_RGBX(const BYTE* WINPR_RESTRICT pSrc, UINT32 srcStep,
1582
+ BYTE* WINPR_RESTRICT pDst1[3],
1583
+ const UINT32 dst1Step[3],
1584
+ BYTE* WINPR_RESTRICT pDst2[3],
1585
+ const UINT32 dst2Step[3],
1586
+ const prim_size_t* WINPR_RESTRICT roi)
1587
+ {
1588
+ /**
1589
+ * Note:
1590
+ * Read information in function general_RGBToAVC444YUV_ANY below !
1591
+ */
1592
+
1593
+ size_t y = 0;
1594
+ for (; y < roi->height - roi->height % 2; y += 2)
1595
+ {
1596
+ const BOOL last = (y >= (roi->height - 1));
1597
+ const BYTE* srcEven = pSrc + 1ULL * y * srcStep;
1598
+ const BYTE* srcOdd = pSrc + 1ULL * (y + 1) * srcStep;
1599
+ const size_t i = y >> 1;
1600
+ const size_t n = (i & (size_t)~7) + i;
1601
+ BYTE* b1Even = pDst1[0] + 1ULL * y * dst1Step[0];
1602
+ BYTE* b1Odd = !last ? (b1Even + dst1Step[0]) : NULL;
1603
+ BYTE* b2 = pDst1[1] + 1ULL * (y / 2) * dst1Step[1];
1604
+ BYTE* b3 = pDst1[2] + 1ULL * (y / 2) * dst1Step[2];
1605
+ BYTE* b4 = pDst2[0] + 1ULL * dst2Step[0] * n;
1606
+ BYTE* b5 = b4 + 8ULL * dst2Step[0];
1607
+ BYTE* b6 = pDst2[1] + 1ULL * (y / 2) * dst2Step[1];
1608
+ BYTE* b7 = pDst2[2] + 1ULL * (y / 2) * dst2Step[2];
1609
+ general_RGBToAVC444YUV_RGBX_DOUBLE_ROW(srcEven, srcOdd, b1Even, b1Odd, b2, b3, b4, b5, b6,
1610
+ b7, roi->width);
1611
+ }
1612
+ for (; y < roi->height; y++)
1613
+ {
1614
+ const BYTE* srcEven = pSrc + 1ULL * y * srcStep;
1615
+ BYTE* b1Even = pDst1[0] + 1ULL * y * dst1Step[0];
1616
+ BYTE* b2 = pDst1[1] + 1ULL * (y / 2) * dst1Step[1];
1617
+ BYTE* b3 = pDst1[2] + 1ULL * (y / 2) * dst1Step[2];
1618
+ BYTE* b6 = pDst2[1] + 1ULL * (y / 2) * dst2Step[1];
1619
+ BYTE* b7 = pDst2[2] + 1ULL * (y / 2) * dst2Step[2];
1620
+ general_RGBToAVC444YUV_RGBX_DOUBLE_ROW(srcEven, NULL, b1Even, NULL, b2, b3, NULL, NULL, b6,
1621
+ b7, roi->width);
1622
+ }
1623
+ return PRIMITIVES_SUCCESS;
1624
+ }
1625
+
1626
+ static inline void general_RGBToAVC444YUV_ANY_DOUBLE_ROW(
1627
+ const BYTE* WINPR_RESTRICT srcEven, const BYTE* WINPR_RESTRICT srcOdd, UINT32 srcFormat,
1628
+ BYTE* WINPR_RESTRICT b1Even, BYTE* WINPR_RESTRICT b1Odd, BYTE* WINPR_RESTRICT b2,
1629
+ BYTE* WINPR_RESTRICT b3, BYTE* WINPR_RESTRICT b4, BYTE* WINPR_RESTRICT b5,
1630
+ BYTE* WINPR_RESTRICT b6, BYTE* WINPR_RESTRICT b7, UINT32 width)
1631
+ {
1632
+ const UINT32 bpp = FreeRDPGetBytesPerPixel(srcFormat);
1633
+ for (UINT32 x = 0; x < width; x += 2)
1634
+ {
1635
+ const BOOL lastX = (x + 1) >= width;
1636
+ BYTE Y1e = 0;
1637
+ BYTE Y2e = 0;
1638
+ BYTE U1e = 0;
1639
+ BYTE V1e = 0;
1640
+ BYTE U2e = 0;
1641
+ BYTE V2e = 0;
1642
+ BYTE Y1o = 0;
1643
+ BYTE Y2o = 0;
1644
+ BYTE U1o = 0;
1645
+ BYTE V1o = 0;
1646
+ BYTE U2o = 0;
1647
+ BYTE V2o = 0;
1648
+ /* Read 4 pixels, 2 from even, 2 from odd lines */
1649
+ {
1650
+ BYTE r = 0;
1651
+ BYTE g = 0;
1652
+ BYTE b = 0;
1653
+ const UINT32 color = FreeRDPReadColor(srcEven, srcFormat);
1654
+ srcEven += bpp;
1655
+ FreeRDPSplitColor(color, srcFormat, &r, &g, &b, NULL, NULL);
1656
+ Y1e = Y2e = Y1o = Y2o = RGB2Y(r, g, b);
1657
+ U1e = U2e = U1o = U2o = RGB2U(r, g, b);
1658
+ V1e = V2e = V1o = V2o = RGB2V(r, g, b);
1659
+ }
1660
+
1661
+ if (!lastX)
1662
+ {
1663
+ BYTE r = 0;
1664
+ BYTE g = 0;
1665
+ BYTE b = 0;
1666
+ const UINT32 color = FreeRDPReadColor(srcEven, srcFormat);
1667
+ srcEven += bpp;
1668
+ FreeRDPSplitColor(color, srcFormat, &r, &g, &b, NULL, NULL);
1669
+ Y2e = RGB2Y(r, g, b);
1670
+ U2e = RGB2U(r, g, b);
1671
+ V2e = RGB2V(r, g, b);
1672
+ }
1673
+
1674
+ if (b1Odd)
1675
+ {
1676
+ BYTE r = 0;
1677
+ BYTE g = 0;
1678
+ BYTE b = 0;
1679
+ const UINT32 color = FreeRDPReadColor(srcOdd, srcFormat);
1680
+ srcOdd += bpp;
1681
+ FreeRDPSplitColor(color, srcFormat, &r, &g, &b, NULL, NULL);
1682
+ Y1o = Y2o = RGB2Y(r, g, b);
1683
+ U1o = U2o = RGB2U(r, g, b);
1684
+ V1o = V2o = RGB2V(r, g, b);
1685
+ }
1686
+
1687
+ if (b1Odd && !lastX)
1688
+ {
1689
+ BYTE r = 0;
1690
+ BYTE g = 0;
1691
+ BYTE b = 0;
1692
+ const UINT32 color = FreeRDPReadColor(srcOdd, srcFormat);
1693
+ srcOdd += bpp;
1694
+ FreeRDPSplitColor(color, srcFormat, &r, &g, &b, NULL, NULL);
1695
+ Y2o = RGB2Y(r, g, b);
1696
+ U2o = RGB2U(r, g, b);
1697
+ V2o = RGB2V(r, g, b);
1698
+ }
1699
+
1700
+ /* We have 4 Y pixels, so store them. */
1701
+ *b1Even++ = Y1e;
1702
+ *b1Even++ = Y2e;
1703
+
1704
+ if (b1Odd)
1705
+ {
1706
+ *b1Odd++ = Y1o;
1707
+ *b1Odd++ = Y2o;
1708
+ }
1709
+
1710
+ /* 2x 2y pixel in luma UV plane use averaging
1711
+ */
1712
+ {
1713
+ const BYTE Uavg = WINPR_ASSERTING_INT_CAST(
1714
+ BYTE, ((UINT16)U1e + (UINT16)U2e + (UINT16)U1o + (UINT16)U2o) / 4);
1715
+ const BYTE Vavg = WINPR_ASSERTING_INT_CAST(
1716
+ BYTE, ((UINT16)V1e + (UINT16)V2e + (UINT16)V1o + (UINT16)V2o) / 4);
1717
+ *b2++ = Uavg;
1718
+ *b3++ = Vavg;
1719
+ }
1720
+
1721
+ /* UV from 2x, 2y+1 */
1722
+ if (b1Odd)
1723
+ {
1724
+ *b4++ = U1o;
1725
+ *b5++ = V1o;
1726
+
1727
+ if (!lastX)
1728
+ {
1729
+ *b4++ = U2o;
1730
+ *b5++ = V2o;
1731
+ }
1732
+ }
1733
+
1734
+ /* UV from 2x+1, 2y */
1735
+ if (!lastX)
1736
+ {
1737
+ *b6++ = U2e;
1738
+ *b7++ = V2e;
1739
+ }
1740
+ }
1741
+ }
1742
+
1743
+ static inline pstatus_t
1744
+ general_RGBToAVC444YUV_ANY(const BYTE* WINPR_RESTRICT pSrc, UINT32 srcFormat, UINT32 srcStep,
1745
+ BYTE* WINPR_RESTRICT pDst1[3], const UINT32 dst1Step[3],
1746
+ BYTE* WINPR_RESTRICT pDst2[3], const UINT32 dst2Step[3],
1747
+ const prim_size_t* WINPR_RESTRICT roi)
1748
+ {
1749
+ /**
1750
+ * Note: According to [MS-RDPEGFX 2.2.4.4 RFX_AVC420_BITMAP_STREAM] the
1751
+ * width and height of the MPEG-4 AVC/H.264 codec bitstream MUST be aligned
1752
+ * to a multiple of 16.
1753
+ * Hence the passed destination YUV420/CHROMA420 buffers must have been
1754
+ * allocated accordingly !!
1755
+ */
1756
+ /**
1757
+ * [MS-RDPEGFX 3.3.8.3.2 YUV420p Stream Combination] defines the following "Bx areas":
1758
+ *
1759
+ * YUV420 frame (main view):
1760
+ * B1: From Y444 all pixels
1761
+ * B2: From U444 all pixels in even rows with even columns
1762
+ * B3: From V444 all pixels in even rows with even columns
1763
+ *
1764
+ * Chroma420 frame (auxiliary view):
1765
+ * B45: From U444 and V444 all pixels from all odd rows
1766
+ * (The odd U444 and V444 rows must be interleaved in 8-line blocks in B45 !!!)
1767
+ * B6: From U444 all pixels in even rows with odd columns
1768
+ * B7: From V444 all pixels in even rows with odd columns
1769
+ *
1770
+ * Microsoft's horrible unclear description in MS-RDPEGFX translated to pseudo code looks like
1771
+ * this:
1772
+ *
1773
+ * for (y = 0; y < fullHeight; y++)
1774
+ * {
1775
+ * for (x = 0; x < fullWidth; x++)
1776
+ * {
1777
+ * B1[x,y] = Y444[x,y];
1778
+ * }
1779
+ * }
1780
+ *
1781
+ * for (y = 0; y < halfHeight; y++)
1782
+ * {
1783
+ * for (x = 0; x < halfWidth; x++)
1784
+ * {
1785
+ * B2[x,y] = U444[2 * x, 2 * y];
1786
+ * B3[x,y] = V444[2 * x, 2 * y];
1787
+ * B6[x,y] = U444[2 * x + 1, 2 * y];
1788
+ * B7[x,y] = V444[2 * x + 1, 2 * y];
1789
+ * }
1790
+ * }
1791
+ *
1792
+ * for (y = 0; y < halfHeight; y++)
1793
+ * {
1794
+ * yU = (y / 8) * 16; // identify first row of correct 8-line U block in B45
1795
+ * yU += (y % 8); // add offset rows in destination block
1796
+ * yV = yU + 8; // the corresponding v line is always 8 rows ahead
1797
+ *
1798
+ * for (x = 0; x < fullWidth; x++)
1799
+ * {
1800
+ * B45[x,yU] = U444[x, 2 * y + 1];
1801
+ * B45[x,yV] = V444[x, 2 * y + 1];
1802
+ * }
1803
+ * }
1804
+ *
1805
+ */
1806
+ const BYTE* pMaxSrc = pSrc + 1ULL * (roi->height - 1) * srcStep;
1807
+
1808
+ for (size_t y = 0; y < roi->height; y += 2)
1809
+ {
1810
+ WINPR_ASSERT(y < UINT32_MAX);
1811
+
1812
+ const BOOL last = (y >= (roi->height - 1));
1813
+ const BYTE* srcEven = y < roi->height ? pSrc + y * srcStep : pMaxSrc;
1814
+ const BYTE* srcOdd = !last ? pSrc + (y + 1) * srcStep : pMaxSrc;
1815
+ const UINT32 i = (UINT32)y >> 1;
1816
+ const UINT32 n = (i & (uint32_t)~7) + i;
1817
+ BYTE* b1Even = pDst1[0] + y * dst1Step[0];
1818
+ BYTE* b1Odd = !last ? (b1Even + dst1Step[0]) : NULL;
1819
+ BYTE* b2 = pDst1[1] + (y / 2) * dst1Step[1];
1820
+ BYTE* b3 = pDst1[2] + (y / 2) * dst1Step[2];
1821
+ BYTE* b4 = pDst2[0] + 1ULL * dst2Step[0] * n;
1822
+ BYTE* b5 = b4 + 8ULL * dst2Step[0];
1823
+ BYTE* b6 = pDst2[1] + (y / 2) * dst2Step[1];
1824
+ BYTE* b7 = pDst2[2] + (y / 2) * dst2Step[2];
1825
+ general_RGBToAVC444YUV_ANY_DOUBLE_ROW(srcEven, srcOdd, srcFormat, b1Even, b1Odd, b2, b3, b4,
1826
+ b5, b6, b7, roi->width);
1827
+ }
1828
+
1829
+ return PRIMITIVES_SUCCESS;
1830
+ }
1831
+
1832
+ static inline pstatus_t general_RGBToAVC444YUV(const BYTE* WINPR_RESTRICT pSrc, UINT32 srcFormat,
1833
+ UINT32 srcStep, BYTE* WINPR_RESTRICT pDst1[3],
1834
+ const UINT32 dst1Step[3],
1835
+ BYTE* WINPR_RESTRICT pDst2[3],
1836
+ const UINT32 dst2Step[3],
1837
+ const prim_size_t* WINPR_RESTRICT roi)
1838
+ {
1839
+ if (!pSrc || !pDst1 || !dst1Step || !pDst2 || !dst2Step)
1840
+ return -1;
1841
+
1842
+ if (!pDst1[0] || !pDst1[1] || !pDst1[2])
1843
+ return -1;
1844
+
1845
+ if (!dst1Step[0] || !dst1Step[1] || !dst1Step[2])
1846
+ return -1;
1847
+
1848
+ if (!pDst2[0] || !pDst2[1] || !pDst2[2])
1849
+ return -1;
1850
+
1851
+ if (!dst2Step[0] || !dst2Step[1] || !dst2Step[2])
1852
+ return -1;
1853
+
1854
+ switch (srcFormat)
1855
+ {
1856
+
1857
+ case PIXEL_FORMAT_BGRA32:
1858
+ case PIXEL_FORMAT_BGRX32:
1859
+ return general_RGBToAVC444YUV_BGRX(pSrc, srcStep, pDst1, dst1Step, pDst2, dst2Step,
1860
+ roi);
1861
+
1862
+ case PIXEL_FORMAT_RGBA32:
1863
+ case PIXEL_FORMAT_RGBX32:
1864
+ return general_RGBToAVC444YUV_RGBX(pSrc, srcStep, pDst1, dst1Step, pDst2, dst2Step,
1865
+ roi);
1866
+
1867
+ default:
1868
+ return general_RGBToAVC444YUV_ANY(pSrc, srcFormat, srcStep, pDst1, dst1Step, pDst2,
1869
+ dst2Step, roi);
1870
+ }
1871
+
1872
+ return !PRIMITIVES_SUCCESS;
1873
+ }
1874
+
1875
+ static inline void general_RGBToAVC444YUVv2_ANY_DOUBLE_ROW(
1876
+ const BYTE* WINPR_RESTRICT srcEven, const BYTE* WINPR_RESTRICT srcOdd, UINT32 srcFormat,
1877
+ BYTE* WINPR_RESTRICT yLumaDstEven, BYTE* WINPR_RESTRICT yLumaDstOdd,
1878
+ BYTE* WINPR_RESTRICT uLumaDst, BYTE* WINPR_RESTRICT vLumaDst,
1879
+ BYTE* WINPR_RESTRICT yEvenChromaDst1, BYTE* WINPR_RESTRICT yEvenChromaDst2,
1880
+ BYTE* WINPR_RESTRICT yOddChromaDst1, BYTE* WINPR_RESTRICT yOddChromaDst2,
1881
+ BYTE* WINPR_RESTRICT uChromaDst1, BYTE* WINPR_RESTRICT uChromaDst2,
1882
+ BYTE* WINPR_RESTRICT vChromaDst1, BYTE* WINPR_RESTRICT vChromaDst2, UINT32 width)
1883
+ {
1884
+ const UINT32 bpp = FreeRDPGetBytesPerPixel(srcFormat);
1885
+
1886
+ WINPR_ASSERT((width % 2) == 0);
1887
+ for (UINT32 x = 0; x < width; x += 2)
1888
+ {
1889
+ BYTE Ya = 0;
1890
+ BYTE Ua = 0;
1891
+ BYTE Va = 0;
1892
+ BYTE Yb = 0;
1893
+ BYTE Ub = 0;
1894
+ BYTE Vb = 0;
1895
+ BYTE Yc = 0;
1896
+ BYTE Uc = 0;
1897
+ BYTE Vc = 0;
1898
+ BYTE Yd = 0;
1899
+ BYTE Ud = 0;
1900
+ BYTE Vd = 0;
1901
+ {
1902
+ BYTE b = 0;
1903
+ BYTE g = 0;
1904
+ BYTE r = 0;
1905
+ const UINT32 color = FreeRDPReadColor(srcEven, srcFormat);
1906
+ srcEven += bpp;
1907
+ FreeRDPSplitColor(color, srcFormat, &r, &g, &b, NULL, NULL);
1908
+ Ya = RGB2Y(r, g, b);
1909
+ Ua = RGB2U(r, g, b);
1910
+ Va = RGB2V(r, g, b);
1911
+ }
1912
+
1913
+ if (x < width - 1)
1914
+ {
1915
+ BYTE b = 0;
1916
+ BYTE g = 0;
1917
+ BYTE r = 0;
1918
+ const UINT32 color = FreeRDPReadColor(srcEven, srcFormat);
1919
+ srcEven += bpp;
1920
+ FreeRDPSplitColor(color, srcFormat, &r, &g, &b, NULL, NULL);
1921
+ Yb = RGB2Y(r, g, b);
1922
+ Ub = RGB2U(r, g, b);
1923
+ Vb = RGB2V(r, g, b);
1924
+ }
1925
+ else
1926
+ {
1927
+ Yb = Ya;
1928
+ Ub = Ua;
1929
+ Vb = Va;
1930
+ }
1931
+
1932
+ if (srcOdd)
1933
+ {
1934
+ BYTE b = 0;
1935
+ BYTE g = 0;
1936
+ BYTE r = 0;
1937
+ const UINT32 color = FreeRDPReadColor(srcOdd, srcFormat);
1938
+ srcOdd += bpp;
1939
+ FreeRDPSplitColor(color, srcFormat, &r, &g, &b, NULL, NULL);
1940
+ Yc = RGB2Y(r, g, b);
1941
+ Uc = RGB2U(r, g, b);
1942
+ Vc = RGB2V(r, g, b);
1943
+ }
1944
+ else
1945
+ {
1946
+ Yc = Ya;
1947
+ Uc = Ua;
1948
+ Vc = Va;
1949
+ }
1950
+
1951
+ if (srcOdd && (x < width - 1))
1952
+ {
1953
+ BYTE b = 0;
1954
+ BYTE g = 0;
1955
+ BYTE r = 0;
1956
+ const UINT32 color = FreeRDPReadColor(srcOdd, srcFormat);
1957
+ srcOdd += bpp;
1958
+ FreeRDPSplitColor(color, srcFormat, &r, &g, &b, NULL, NULL);
1959
+ Yd = RGB2Y(r, g, b);
1960
+ Ud = RGB2U(r, g, b);
1961
+ Vd = RGB2V(r, g, b);
1962
+ }
1963
+ else
1964
+ {
1965
+ Yd = Ya;
1966
+ Ud = Ua;
1967
+ Vd = Va;
1968
+ }
1969
+
1970
+ /* Y [b1] */
1971
+ *yLumaDstEven++ = Ya;
1972
+
1973
+ if (x < width - 1)
1974
+ *yLumaDstEven++ = Yb;
1975
+
1976
+ if (srcOdd)
1977
+ *yLumaDstOdd++ = Yc;
1978
+
1979
+ if (srcOdd && (x < width - 1))
1980
+ *yLumaDstOdd++ = Yd;
1981
+
1982
+ /* 2x 2y [b2,b3] */
1983
+ *uLumaDst++ = (Ua + Ub + Uc + Ud) / 4;
1984
+ *vLumaDst++ = (Va + Vb + Vc + Vd) / 4;
1985
+
1986
+ /* 2x+1, y [b4,b5] even */
1987
+ if (x < width - 1)
1988
+ {
1989
+ *yEvenChromaDst1++ = Ub;
1990
+ *yEvenChromaDst2++ = Vb;
1991
+ }
1992
+
1993
+ if (srcOdd)
1994
+ {
1995
+ /* 2x+1, y [b4,b5] odd */
1996
+ if (x < width - 1)
1997
+ {
1998
+ *yOddChromaDst1++ = Ud;
1999
+ *yOddChromaDst2++ = Vd;
2000
+ }
2001
+
2002
+ /* 4x 2y+1 [b6, b7] */
2003
+ if (x % 4 == 0)
2004
+ {
2005
+ *uChromaDst1++ = Uc;
2006
+ *uChromaDst2++ = Vc;
2007
+ }
2008
+ /* 4x+2 2y+1 [b8, b9] */
2009
+ else
2010
+ {
2011
+ *vChromaDst1++ = Uc;
2012
+ *vChromaDst2++ = Vc;
2013
+ }
2014
+ }
2015
+ }
2016
+ }
2017
+
2018
+ static inline pstatus_t
2019
+ general_RGBToAVC444YUVv2_ANY(const BYTE* WINPR_RESTRICT pSrc, UINT32 srcFormat, UINT32 srcStep,
2020
+ BYTE* WINPR_RESTRICT pDst1[3], const UINT32 dst1Step[3],
2021
+ BYTE* WINPR_RESTRICT pDst2[3], const UINT32 dst2Step[3],
2022
+ const prim_size_t* WINPR_RESTRICT roi)
2023
+ {
2024
+ /**
2025
+ * Note: According to [MS-RDPEGFX 2.2.4.4 RFX_AVC420_BITMAP_STREAM] the
2026
+ * width and height of the MPEG-4 AVC/H.264 codec bitstream MUST be aligned
2027
+ * to a multiple of 16.
2028
+ * Hence the passed destination YUV420/CHROMA420 buffers must have been
2029
+ * allocated accordingly !!
2030
+ */
2031
+ /**
2032
+ * [MS-RDPEGFX 3.3.8.3.3 YUV420p Stream Combination for YUV444v2 mode] defines the following "Bx
2033
+ * areas":
2034
+ *
2035
+ * YUV420 frame (main view):
2036
+ * B1: From Y444 all pixels
2037
+ * B2: From U444 all pixels in even rows with even rows and columns
2038
+ * B3: From V444 all pixels in even rows with even rows and columns
2039
+ *
2040
+ * Chroma420 frame (auxiliary view):
2041
+ * B45: From U444 and V444 all pixels from all odd columns
2042
+ * B67: From U444 and V444 every 4th pixel in odd rows
2043
+ * B89: From U444 and V444 every 4th pixel (initial offset of 2) in odd rows
2044
+ *
2045
+ * Chroma Bxy areas correspond to the left and right half of the YUV420 plane.
2046
+ * for (y = 0; y < fullHeight; y++)
2047
+ * {
2048
+ * for (x = 0; x < fullWidth; x++)
2049
+ * {
2050
+ * B1[x,y] = Y444[x,y];
2051
+ * }
2052
+ *
2053
+ * for (x = 0; x < halfWidth; x++)
2054
+ * {
2055
+ * B4[x,y] = U444[2 * x, 2 * y];
2056
+ * B5[x,y] = V444[2 * x, 2 * y];
2057
+ * }
2058
+ * }
2059
+ *
2060
+ * for (y = 0; y < halfHeight; y++)
2061
+ * {
2062
+ * for (x = 0; x < halfWidth; x++)
2063
+ * {
2064
+ * B2[x,y] = U444[2 * x, 2 * y];
2065
+ * B3[x,y] = V444[2 * x, 2 * y];
2066
+ * B6[x,y] = U444[4 * x, 2 * y + 1];
2067
+ * B7[x,y] = V444[4 * x, 2 * y + 1];
2068
+ * B8[x,y] = V444[4 * x + 2, 2 * y + 1];
2069
+ * B9[x,y] = V444[4 * x + 2, 2 * y] + 1;
2070
+ * }
2071
+ * }
2072
+ *
2073
+ */
2074
+ if (roi->height < 1 || roi->width < 1)
2075
+ return !PRIMITIVES_SUCCESS;
2076
+
2077
+ size_t y = 0;
2078
+ for (; y < roi->height - roi->height % 2; y += 2)
2079
+ {
2080
+ const BYTE* srcEven = (pSrc + y * srcStep);
2081
+ const BYTE* srcOdd = (y < roi->height - 1) ? (srcEven + srcStep) : NULL;
2082
+ BYTE* dstLumaYEven = (pDst1[0] + y * dst1Step[0]);
2083
+ BYTE* dstLumaYOdd = (dstLumaYEven + dst1Step[0]);
2084
+ BYTE* dstLumaU = (pDst1[1] + (y / 2) * dst1Step[1]);
2085
+ BYTE* dstLumaV = (pDst1[2] + (y / 2) * dst1Step[2]);
2086
+ BYTE* dstEvenChromaY1 = (pDst2[0] + y * dst2Step[0]);
2087
+ BYTE* dstEvenChromaY2 = dstEvenChromaY1 + roi->width / 2;
2088
+ BYTE* dstOddChromaY1 = dstEvenChromaY1 + dst2Step[0];
2089
+ BYTE* dstOddChromaY2 = dstEvenChromaY2 + dst2Step[0];
2090
+ BYTE* dstChromaU1 = (pDst2[1] + (y / 2) * dst2Step[1]);
2091
+ BYTE* dstChromaV1 = (pDst2[2] + (y / 2) * dst2Step[2]);
2092
+ BYTE* dstChromaU2 = dstChromaU1 + roi->width / 4;
2093
+ BYTE* dstChromaV2 = dstChromaV1 + roi->width / 4;
2094
+ general_RGBToAVC444YUVv2_ANY_DOUBLE_ROW(
2095
+ srcEven, srcOdd, srcFormat, dstLumaYEven, dstLumaYOdd, dstLumaU, dstLumaV,
2096
+ dstEvenChromaY1, dstEvenChromaY2, dstOddChromaY1, dstOddChromaY2, dstChromaU1,
2097
+ dstChromaU2, dstChromaV1, dstChromaV2, roi->width);
2098
+ }
2099
+ for (; y < roi->height; y++)
2100
+ {
2101
+ const BYTE* srcEven = (pSrc + y * srcStep);
2102
+ BYTE* dstLumaYEven = (pDst1[0] + y * dst1Step[0]);
2103
+ BYTE* dstLumaU = (pDst1[1] + (y / 2) * dst1Step[1]);
2104
+ BYTE* dstLumaV = (pDst1[2] + (y / 2) * dst1Step[2]);
2105
+ BYTE* dstEvenChromaY1 = (pDst2[0] + y * dst2Step[0]);
2106
+ BYTE* dstEvenChromaY2 = dstEvenChromaY1 + roi->width / 2;
2107
+ general_RGBToAVC444YUVv2_ANY_DOUBLE_ROW(
2108
+ srcEven, NULL, srcFormat, dstLumaYEven, NULL, dstLumaU, dstLumaV, dstEvenChromaY1,
2109
+ dstEvenChromaY2, NULL, NULL, NULL, NULL, NULL, NULL, roi->width);
2110
+ }
2111
+
2112
+ return PRIMITIVES_SUCCESS;
2113
+ }
2114
+
2115
+ static inline void int_general_RGBToAVC444YUVv2_BGRX_DOUBLE_ROW(
2116
+ size_t offset, const BYTE* WINPR_RESTRICT pSrcEven, const BYTE* WINPR_RESTRICT pSrcOdd,
2117
+ BYTE* WINPR_RESTRICT yLumaDstEven, BYTE* WINPR_RESTRICT yLumaDstOdd,
2118
+ BYTE* WINPR_RESTRICT uLumaDst, BYTE* WINPR_RESTRICT vLumaDst,
2119
+ BYTE* WINPR_RESTRICT yEvenChromaDst1, BYTE* WINPR_RESTRICT yEvenChromaDst2,
2120
+ BYTE* WINPR_RESTRICT yOddChromaDst1, BYTE* WINPR_RESTRICT yOddChromaDst2,
2121
+ BYTE* WINPR_RESTRICT uChromaDst1, BYTE* WINPR_RESTRICT uChromaDst2,
2122
+ BYTE* WINPR_RESTRICT vChromaDst1, BYTE* WINPR_RESTRICT vChromaDst2, UINT32 width)
2123
+ {
2124
+ WINPR_ASSERT((width % 2) == 0);
2125
+ WINPR_ASSERT(pSrcEven);
2126
+ WINPR_ASSERT(yLumaDstEven);
2127
+ WINPR_ASSERT(uLumaDst);
2128
+ WINPR_ASSERT(vLumaDst);
2129
+
2130
+ for (size_t x = offset; x < width; x += 2)
2131
+ {
2132
+ const BYTE* srcEven = &pSrcEven[4ULL * x];
2133
+ const BYTE* srcOdd = pSrcOdd ? &pSrcOdd[4ULL * x] : NULL;
2134
+ BYTE Ya = 0;
2135
+ BYTE Ua = 0;
2136
+ BYTE Va = 0;
2137
+ BYTE Yb = 0;
2138
+ BYTE Ub = 0;
2139
+ BYTE Vb = 0;
2140
+ BYTE Yc = 0;
2141
+ BYTE Uc = 0;
2142
+ BYTE Vc = 0;
2143
+ BYTE Yd = 0;
2144
+ BYTE Ud = 0;
2145
+ BYTE Vd = 0;
2146
+ {
2147
+ const BYTE b = *srcEven++;
2148
+ const BYTE g = *srcEven++;
2149
+ const BYTE r = *srcEven++;
2150
+ srcEven++;
2151
+ Ya = RGB2Y(r, g, b);
2152
+ Ua = RGB2U(r, g, b);
2153
+ Va = RGB2V(r, g, b);
2154
+ }
2155
+
2156
+ if (x < width - 1)
2157
+ {
2158
+ const BYTE b = *srcEven++;
2159
+ const BYTE g = *srcEven++;
2160
+ const BYTE r = *srcEven++;
2161
+ srcEven++;
2162
+ Yb = RGB2Y(r, g, b);
2163
+ Ub = RGB2U(r, g, b);
2164
+ Vb = RGB2V(r, g, b);
2165
+ }
2166
+ else
2167
+ {
2168
+ Yb = Ya;
2169
+ Ub = Ua;
2170
+ Vb = Va;
2171
+ }
2172
+
2173
+ if (srcOdd)
2174
+ {
2175
+ const BYTE b = *srcOdd++;
2176
+ const BYTE g = *srcOdd++;
2177
+ const BYTE r = *srcOdd++;
2178
+ srcOdd++;
2179
+ Yc = RGB2Y(r, g, b);
2180
+ Uc = RGB2U(r, g, b);
2181
+ Vc = RGB2V(r, g, b);
2182
+ }
2183
+ else
2184
+ {
2185
+ Yc = Ya;
2186
+ Uc = Ua;
2187
+ Vc = Va;
2188
+ }
2189
+
2190
+ if (srcOdd && (x < width - 1))
2191
+ {
2192
+ const BYTE b = *srcOdd++;
2193
+ const BYTE g = *srcOdd++;
2194
+ const BYTE r = *srcOdd++;
2195
+ srcOdd++;
2196
+ Yd = RGB2Y(r, g, b);
2197
+ Ud = RGB2U(r, g, b);
2198
+ Vd = RGB2V(r, g, b);
2199
+ }
2200
+ else
2201
+ {
2202
+ Yd = Ya;
2203
+ Ud = Ua;
2204
+ Vd = Va;
2205
+ }
2206
+
2207
+ /* Y [b1] */
2208
+ *yLumaDstEven++ = Ya;
2209
+
2210
+ if (x < width - 1)
2211
+ *yLumaDstEven++ = Yb;
2212
+
2213
+ if (srcOdd && yLumaDstOdd)
2214
+ *yLumaDstOdd++ = Yc;
2215
+
2216
+ if (srcOdd && (x < width - 1) && yLumaDstOdd)
2217
+ *yLumaDstOdd++ = Yd;
2218
+
2219
+ /* 2x 2y [b2,b3] */
2220
+ *uLumaDst++ = (Ua + Ub + Uc + Ud) / 4;
2221
+ *vLumaDst++ = (Va + Vb + Vc + Vd) / 4;
2222
+
2223
+ /* 2x+1, y [b4,b5] even */
2224
+ if (x < width - 1)
2225
+ {
2226
+ *yEvenChromaDst1++ = Ub;
2227
+ *yEvenChromaDst2++ = Vb;
2228
+ }
2229
+
2230
+ if (srcOdd)
2231
+ {
2232
+ /* 2x+1, y [b4,b5] odd */
2233
+ if (x < width - 1)
2234
+ {
2235
+ *yOddChromaDst1++ = Ud;
2236
+ *yOddChromaDst2++ = Vd;
2237
+ }
2238
+
2239
+ /* 4x 2y+1 [b6, b7] */
2240
+ if (x % 4 == 0)
2241
+ {
2242
+ *uChromaDst1++ = Uc;
2243
+ *uChromaDst2++ = Vc;
2244
+ }
2245
+ /* 4x+2 2y+1 [b8, b9] */
2246
+ else
2247
+ {
2248
+ *vChromaDst1++ = Uc;
2249
+ *vChromaDst2++ = Vc;
2250
+ }
2251
+ }
2252
+ }
2253
+ }
2254
+
2255
+ void general_RGBToAVC444YUVv2_BGRX_DOUBLE_ROW(
2256
+ size_t offset, const BYTE* WINPR_RESTRICT pSrcEven, const BYTE* WINPR_RESTRICT pSrcOdd,
2257
+ BYTE* WINPR_RESTRICT yLumaDstEven, BYTE* WINPR_RESTRICT yLumaDstOdd,
2258
+ BYTE* WINPR_RESTRICT uLumaDst, BYTE* WINPR_RESTRICT vLumaDst,
2259
+ BYTE* WINPR_RESTRICT yEvenChromaDst1, BYTE* WINPR_RESTRICT yEvenChromaDst2,
2260
+ BYTE* WINPR_RESTRICT yOddChromaDst1, BYTE* WINPR_RESTRICT yOddChromaDst2,
2261
+ BYTE* WINPR_RESTRICT uChromaDst1, BYTE* WINPR_RESTRICT uChromaDst2,
2262
+ BYTE* WINPR_RESTRICT vChromaDst1, BYTE* WINPR_RESTRICT vChromaDst2, UINT32 width)
2263
+ {
2264
+ int_general_RGBToAVC444YUVv2_BGRX_DOUBLE_ROW(
2265
+ offset, pSrcEven, pSrcOdd, yLumaDstEven, yLumaDstOdd, uLumaDst, vLumaDst, yEvenChromaDst1,
2266
+ yEvenChromaDst2, yOddChromaDst1, yOddChromaDst2, uChromaDst1, uChromaDst2, vChromaDst1,
2267
+ vChromaDst2, width);
2268
+ }
2269
+
2270
+ static inline pstatus_t general_RGBToAVC444YUVv2_BGRX(const BYTE* WINPR_RESTRICT pSrc,
2271
+ UINT32 srcStep, BYTE* WINPR_RESTRICT pDst1[3],
2272
+ const UINT32 dst1Step[3],
2273
+ BYTE* WINPR_RESTRICT pDst2[3],
2274
+ const UINT32 dst2Step[3],
2275
+ const prim_size_t* WINPR_RESTRICT roi)
2276
+ {
2277
+ if (roi->height < 1 || roi->width < 1)
2278
+ return !PRIMITIVES_SUCCESS;
2279
+
2280
+ size_t y = 0;
2281
+ for (; y < roi->height - roi->height % 2; y += 2)
2282
+ {
2283
+ const BYTE* srcEven = (pSrc + y * srcStep);
2284
+ const BYTE* srcOdd = (srcEven + srcStep);
2285
+ BYTE* dstLumaYEven = (pDst1[0] + y * dst1Step[0]);
2286
+ BYTE* dstLumaYOdd = (dstLumaYEven + dst1Step[0]);
2287
+ BYTE* dstLumaU = (pDst1[1] + (y / 2) * dst1Step[1]);
2288
+ BYTE* dstLumaV = (pDst1[2] + (y / 2) * dst1Step[2]);
2289
+ BYTE* dstEvenChromaY1 = (pDst2[0] + y * dst2Step[0]);
2290
+ BYTE* dstEvenChromaY2 = dstEvenChromaY1 + roi->width / 2;
2291
+ BYTE* dstOddChromaY1 = dstEvenChromaY1 + dst2Step[0];
2292
+ BYTE* dstOddChromaY2 = dstEvenChromaY2 + dst2Step[0];
2293
+ BYTE* dstChromaU1 = (pDst2[1] + (y / 2) * dst2Step[1]);
2294
+ BYTE* dstChromaV1 = (pDst2[2] + (y / 2) * dst2Step[2]);
2295
+ BYTE* dstChromaU2 = dstChromaU1 + roi->width / 4;
2296
+ BYTE* dstChromaV2 = dstChromaV1 + roi->width / 4;
2297
+ int_general_RGBToAVC444YUVv2_BGRX_DOUBLE_ROW(
2298
+ 0, srcEven, srcOdd, dstLumaYEven, dstLumaYOdd, dstLumaU, dstLumaV, dstEvenChromaY1,
2299
+ dstEvenChromaY2, dstOddChromaY1, dstOddChromaY2, dstChromaU1, dstChromaU2, dstChromaV1,
2300
+ dstChromaV2, roi->width);
2301
+ }
2302
+ for (; y < roi->height; y++)
2303
+ {
2304
+ const BYTE* srcEven = (pSrc + y * srcStep);
2305
+ BYTE* dstLumaYEven = (pDst1[0] + y * dst1Step[0]);
2306
+ BYTE* dstLumaU = (pDst1[1] + (y / 2) * dst1Step[1]);
2307
+ BYTE* dstLumaV = (pDst1[2] + (y / 2) * dst1Step[2]);
2308
+ BYTE* dstEvenChromaY1 = (pDst2[0] + y * dst2Step[0]);
2309
+ BYTE* dstEvenChromaY2 = dstEvenChromaY1 + roi->width / 2;
2310
+ int_general_RGBToAVC444YUVv2_BGRX_DOUBLE_ROW(
2311
+ 0, srcEven, NULL, dstLumaYEven, NULL, dstLumaU, dstLumaV, dstEvenChromaY1,
2312
+ dstEvenChromaY2, NULL, NULL, NULL, NULL, NULL, NULL, roi->width);
2313
+ }
2314
+
2315
+ return PRIMITIVES_SUCCESS;
2316
+ }
2317
+
2318
+ static pstatus_t general_RGBToAVC444YUVv2(const BYTE* WINPR_RESTRICT pSrc, UINT32 srcFormat,
2319
+ UINT32 srcStep, BYTE* WINPR_RESTRICT pDst1[3],
2320
+ const UINT32 dst1Step[3], BYTE* WINPR_RESTRICT pDst2[3],
2321
+ const UINT32 dst2Step[3],
2322
+ const prim_size_t* WINPR_RESTRICT roi)
2323
+ {
2324
+ switch (srcFormat)
2325
+ {
2326
+ case PIXEL_FORMAT_BGRA32:
2327
+ case PIXEL_FORMAT_BGRX32:
2328
+ return general_RGBToAVC444YUVv2_BGRX(pSrc, srcStep, pDst1, dst1Step, pDst2, dst2Step,
2329
+ roi);
2330
+
2331
+ default:
2332
+ return general_RGBToAVC444YUVv2_ANY(pSrc, srcFormat, srcStep, pDst1, dst1Step, pDst2,
2333
+ dst2Step, roi);
2334
+ }
2335
+
2336
+ return !PRIMITIVES_SUCCESS;
2337
+ }
2338
+
2339
+ void primitives_init_YUV(primitives_t* WINPR_RESTRICT prims)
2340
+ {
2341
+ prims->YUV420ToRGB_8u_P3AC4R = general_YUV420ToRGB_8u_P3AC4R;
2342
+ prims->YUV444ToRGB_8u_P3AC4R = general_YUV444ToRGB_8u_P3AC4R;
2343
+ prims->RGBToYUV420_8u_P3AC4R = general_RGBToYUV420_8u_P3AC4R;
2344
+ prims->RGBToYUV444_8u_P3AC4R = general_RGBToYUV444_8u_P3AC4R;
2345
+ prims->YUV420CombineToYUV444 = general_YUV420CombineToYUV444;
2346
+ prims->YUV444SplitToYUV420 = general_YUV444SplitToYUV420;
2347
+ prims->RGBToAVC444YUV = general_RGBToAVC444YUV;
2348
+ prims->RGBToAVC444YUVv2 = general_RGBToAVC444YUVv2;
2349
+ }
2350
+
2351
+ void primitives_init_YUV_opt(primitives_t* WINPR_RESTRICT prims)
2352
+ {
2353
+ primitives_init_YUV_sse41(prims);
2354
+ primitives_init_YUV_neon(prims);
2355
+ }
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/primitives/prim_YUV.h ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * Primitives copy
4
+ *
5
+ * Copyright 2024 Armin Novak <anovak@thincast.com>
6
+ * Copyright 2024 Thincast Technologies GmbH
7
+ *
8
+ * Licensed under the Apache License, Version 2.0 (the "License");
9
+ * you may not use this file except in compliance with the License.
10
+ * You may obtain a copy of the License at
11
+ *
12
+ * http://www.apache.org/licenses/LICENSE-2.0
13
+ *
14
+ * Unless required by applicable law or agreed to in writing, software
15
+ * distributed under the License is distributed on an "AS IS" BASIS,
16
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ * See the License for the specific language governing permissions and
18
+ * limitations under the License.
19
+ */
20
+
21
+ #ifndef FREERDP_LIB_PRIM_YUV_H
22
+ #define FREERDP_LIB_PRIM_YUV_H
23
+
24
+ #include <winpr/wtypes.h>
25
+ #include <freerdp/config.h>
26
+ #include <freerdp/primitives.h>
27
+
28
+ void primitives_init_YUV_sse41(primitives_t* WINPR_RESTRICT prims);
29
+ void primitives_init_YUV_neon(primitives_t* WINPR_RESTRICT prims);
30
+
31
+ #endif
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/primitives/prim_add.c ADDED
@@ -0,0 +1,82 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* FreeRDP: A Remote Desktop Protocol Client
2
+ * Add operations.
3
+ * vi:ts=4 sw=4:
4
+ *
5
+ * (c) Copyright 2012 Hewlett-Packard Development Company, L.P.
6
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may
7
+ * not use this file except in compliance with the License. You may obtain
8
+ * a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
9
+ * Unless required by applicable law or agreed to in writing, software
10
+ * distributed under the License is distributed on an "AS IS" BASIS,
11
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12
+ * or implied. See the License for the specific language governing
13
+ * permissions and limitations under the License.
14
+ *
15
+ */
16
+
17
+ #include <freerdp/config.h>
18
+
19
+ #include <stdint.h>
20
+
21
+ #include <freerdp/types.h>
22
+ #include <freerdp/primitives.h>
23
+
24
+ #include "prim_internal.h"
25
+ #include "prim_add.h"
26
+
27
+ /* ----------------------------------------------------------------------------
28
+ * 16-bit signed add with saturation (under and over).
29
+ */
30
+ static INLINE INT16 add(INT16 a, INT16 b)
31
+ {
32
+ INT32 k = (INT32)a + (INT32)b;
33
+
34
+ if (k > INT16_MAX)
35
+ return INT16_MAX;
36
+
37
+ if (k < INT16_MIN)
38
+ return INT16_MIN;
39
+
40
+ return (INT16)k;
41
+ }
42
+
43
+ static pstatus_t general_add_16s(const INT16* WINPR_RESTRICT pSrc1,
44
+ const INT16* WINPR_RESTRICT pSrc2, INT16* WINPR_RESTRICT pDst,
45
+ UINT32 len)
46
+ {
47
+ const UINT32 rem = len % 16;
48
+ const UINT32 align = len - rem;
49
+
50
+ for (UINT32 x = 0; x < align; x++)
51
+ *pDst++ = add(*pSrc1++, *pSrc2++);
52
+
53
+ for (UINT32 x = 0; x < rem; x++)
54
+ *pDst++ = add(*pSrc1++, *pSrc2++);
55
+
56
+ return PRIMITIVES_SUCCESS;
57
+ }
58
+
59
+ static pstatus_t general_add_16s_inplace(INT16* WINPR_RESTRICT pSrcDst1,
60
+ INT16* WINPR_RESTRICT pSrcDst2, UINT32 len)
61
+ {
62
+ for (UINT32 x = 0; x < len; x++)
63
+ {
64
+ INT16 v = add(pSrcDst1[x], pSrcDst2[x]);
65
+ pSrcDst1[x] = v;
66
+ pSrcDst2[x] = v;
67
+ }
68
+
69
+ return PRIMITIVES_SUCCESS;
70
+ }
71
+
72
+ /* ------------------------------------------------------------------------- */
73
+ void primitives_init_add(primitives_t* WINPR_RESTRICT prims)
74
+ {
75
+ prims->add_16s = general_add_16s;
76
+ prims->add_16s_inplace = general_add_16s_inplace;
77
+ }
78
+
79
+ void primitives_init_add_opt(primitives_t* WINPR_RESTRICT prims)
80
+ {
81
+ primitives_init_add_sse3(prims);
82
+ }
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/primitives/prim_add.h ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * Primitives copy
4
+ *
5
+ * Copyright 2024 Armin Novak <anovak@thincast.com>
6
+ * Copyright 2024 Thincast Technologies GmbH
7
+ *
8
+ * Licensed under the Apache License, Version 2.0 (the "License");
9
+ * you may not use this file except in compliance with the License.
10
+ * You may obtain a copy of the License at
11
+ *
12
+ * http://www.apache.org/licenses/LICENSE-2.0
13
+ *
14
+ * Unless required by applicable law or agreed to in writing, software
15
+ * distributed under the License is distributed on an "AS IS" BASIS,
16
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ * See the License for the specific language governing permissions and
18
+ * limitations under the License.
19
+ */
20
+
21
+ #ifndef FREERDP_LIB_PRIM_ADD_H
22
+ #define FREERDP_LIB_PRIM_ADD_H
23
+
24
+ #include <winpr/wtypes.h>
25
+ #include <freerdp/config.h>
26
+ #include <freerdp/primitives.h>
27
+
28
+ void primitives_init_add_sse3(primitives_t* WINPR_RESTRICT prims);
29
+
30
+ #endif
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/primitives/prim_alphaComp.c ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /* FreeRDP: A Remote Desktop Protocol Client
2
+ * Alpha blending routines.
3
+ * vi:ts=4 sw=4:
4
+ *
5
+ * (c) Copyright 2012 Hewlett-Packard Development Company, L.P.
6
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may
7
+ * not use this file except in compliance with the License. You may obtain
8
+ * a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
9
+ * Unless required by applicable law or agreed to in writing, software
10
+ * distributed under the License is distributed on an "AS IS" BASIS,
11
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12
+ * or implied. See the License for the specific language governing
13
+ * permissions and limitations under the License.
14
+ *
15
+ * Note: this code assumes the second operand is fully opaque,
16
+ * e.g.
17
+ * newval = alpha1*val1 + (1-alpha1)*val2
18
+ * rather than
19
+ * newval = alpha1*val1 + (1-alpha1)*alpha2*val2
20
+ */
21
+
22
+ #include <freerdp/config.h>
23
+
24
+ #include <freerdp/types.h>
25
+ #include <freerdp/primitives.h>
26
+
27
+ #include "prim_internal.h"
28
+ #include "prim_alphaComp.h"
29
+
30
+ #define ALPHA(_k_) (((_k_)&0xFF000000U) >> 24)
31
+
32
+ /* ------------------------------------------------------------------------- */
33
+ static pstatus_t general_alphaComp_argb(const BYTE* pSrc1, UINT32 src1Step, const BYTE* pSrc2,
34
+ UINT32 src2Step, BYTE* pDst, UINT32 dstStep, UINT32 width,
35
+ UINT32 height)
36
+ {
37
+ for (size_t y = 0; y < height; y++)
38
+ {
39
+ const UINT32* sptr1 = (const UINT32*)(pSrc1 + y * src1Step);
40
+ const UINT32* sptr2 = (const UINT32*)(pSrc2 + y * src2Step);
41
+ UINT32* dptr = (UINT32*)(pDst + y * dstStep);
42
+
43
+ for (size_t x = 0; x < width; x++)
44
+ {
45
+ const UINT32 src1 = *sptr1++;
46
+ const UINT32 src2 = *sptr2++;
47
+ UINT32 alpha = ALPHA(src1) + 1;
48
+
49
+ if (alpha == 256)
50
+ {
51
+ /* If alpha is 255+1, just copy src1. */
52
+ *dptr++ = src1;
53
+ }
54
+ else if (alpha <= 1)
55
+ {
56
+ /* If alpha is 0+1, just copy src2. */
57
+ *dptr++ = src2;
58
+ }
59
+ else
60
+ {
61
+ /* A perfectly accurate blend would do (a*src + (255-a)*dst)/255
62
+ * rather than adding one to alpha and dividing by 256, but this
63
+ * is much faster and only differs by one 16% of the time.
64
+ * I'm not sure who first designed the double-ops trick
65
+ * (Red Blue and Alpha Green).
66
+ */
67
+ UINT32 rb = 0;
68
+ UINT32 ag = 0;
69
+ UINT32 s2rb = src2 & 0x00FF00FFU;
70
+ UINT32 s2ag = (src2 >> 8) & 0x00FF00FFU;
71
+ UINT32 s1rb = src1 & 0x00FF00FFU;
72
+ UINT32 s1ag = (src1 >> 8) & 0x00FF00FFU;
73
+ UINT32 drb = s1rb - s2rb;
74
+ UINT32 dag = s1ag - s2ag;
75
+ drb *= alpha;
76
+ dag *= alpha;
77
+ rb = ((drb >> 8) + s2rb) & 0x00FF00FFU;
78
+ ag = (((dag >> 8) + s2ag) << 8) & 0xFF00FF00U;
79
+ *dptr++ = rb | ag;
80
+ }
81
+ }
82
+ }
83
+
84
+ return PRIMITIVES_SUCCESS;
85
+ }
86
+
87
+ /* ------------------------------------------------------------------------- */
88
+ void primitives_init_alphaComp(primitives_t* WINPR_RESTRICT prims)
89
+ {
90
+ prims->alphaComp_argb = general_alphaComp_argb;
91
+ }
92
+
93
+ void primitives_init_alphaComp_opt(primitives_t* WINPR_RESTRICT prims)
94
+ {
95
+ primitives_init_alphaComp_sse3(prims);
96
+ }
local-test-freerdp-delta-01/afc-freerdp/libfreerdp/primitives/prim_alphaComp.h ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * FreeRDP: A Remote Desktop Protocol Implementation
3
+ * Primitives copy
4
+ *
5
+ * Copyright 2024 Armin Novak <anovak@thincast.com>
6
+ * Copyright 2024 Thincast Technologies GmbH
7
+ *
8
+ * Licensed under the Apache License, Version 2.0 (the "License");
9
+ * you may not use this file except in compliance with the License.
10
+ * You may obtain a copy of the License at
11
+ *
12
+ * http://www.apache.org/licenses/LICENSE-2.0
13
+ *
14
+ * Unless required by applicable law or agreed to in writing, software
15
+ * distributed under the License is distributed on an "AS IS" BASIS,
16
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
+ * See the License for the specific language governing permissions and
18
+ * limitations under the License.
19
+ */
20
+
21
+ #ifndef FREERDP_LIB_PRIM_ALPHA_COMP_H
22
+ #define FREERDP_LIB_PRIM_ALPHA_COMP_H
23
+
24
+ #include <winpr/wtypes.h>
25
+ #include <freerdp/config.h>
26
+ #include <freerdp/primitives.h>
27
+
28
+ void primitives_init_alphaComp_sse3(primitives_t* WINPR_RESTRICT prims);
29
+
30
+ #endif