1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 ZeroDDS Contributors
//! Taking bytes off the wire.
//!
//! Every read that cannot be satisfied is an error naming what was
//! expected and where. A reader that returned a zero for a truncated
//! stream would hand the caller a value that was never sent.
use crate::writer::{
BYTE_ORDER_MARK, E_FLAG_MUST_UNDERSTAND, E_MEMBER_ID_MASK, FixedDigits, ParameterRole,
PlCdr1Framing, PlCdr1Reading, SWAPPED_BYTE_ORDER_MARK, WcharFraming, WcharPosition, WcharWidth,
WirePosition, WstringLengthCounts, WstringLengthPolicy, WstringTerminator, WstringUnitOrder,
};
use crate::writer::{
E_FLAG_IMPL_EXTENSION, PID_EXTENDED, PID_IGNORE, PID_IMPL_EXTENSION, PID_LIST_END,
};
use crate::{Endianness, WireError};
use alloc::collections::BTreeMap;
use alloc::format;
use alloc::string::{String, ToString};
use alloc::vec::Vec;
use zerodds_float128::Float128;
/// The widest `fixed` IDL 4.2 §7.4.1.4.4.3.4 admits, in octets:
/// `fixed<31, S>` is thirty-two nibbles. A value-precision field that presents
/// no sign nibble within it is a stream this reader refuses rather than one it
/// keeps reading.
const MAX_FIXED_OCTETS: usize = 16;
/// The lowest nibble value that is a sign and not a digit.
///
/// CORBA 3.3 part 2 §9.3.2.8: a digit is `0` to `9`, and every nibble at or
/// above this is a sign. It is what makes the value-precision form
/// self-delimiting. `zerodds_ast_layout::key::FixedDigitCount::FIRST_SIGN_NIBBLE`
/// is the planner's half of the same constant, and
/// `crates/wire-codec/tests/fixed_digits_agree.rs` holds the two together.
const FIRST_SIGN_NIBBLE: u8 = 0x0a;
/// Reads one sample's bytes.
#[derive(Debug, Clone)]
pub struct Reader<'a> {
bytes: &'a [u8],
at: usize,
endianness: Endianness,
/// Where alignment is counted from.
///
/// Zero for a sample, and zero inside a DHEADER as well, because
/// XCDR counts alignment from the encapsulation and not from a
/// nested value's own start. A PL_CDR1 parameter is the exception:
/// §7.4.1.2 makes each one "a CDR-encapsulated block of data", so
/// its value counts from the block. `Writer` has the same field for
/// the same clause.
origin: usize,
/// The encoding's `MAXALIGN`. The writer's counterpart — a reader
/// that skipped padding the writer never wrote reads the wrong
/// bytes from the second member on.
max_align: usize,
/// How the peer is expected to have framed a PL_CDR1 parameter list.
///
/// The same type the writer takes, and set from the same policy, so
/// the two cannot drift: a reader that accepted any framing would
/// pass every wrong-dialect run silently, and one that carried its
/// own copy of the rules would disagree with the writer the first
/// time either changed.
///
/// Deliberately `PlCdr1Framing` and not a dialect. `zerodds-wire`
/// has no dependencies and is not going to gain one — it executes a
/// plan and does not know what a dialect is. The settings that *are*
/// dialect-level reach the read path the other way, through the
/// layout plan the reader walks, which is computed from the same
/// `LayoutKey` the writer used.
pl_cdr1: PlCdr1Framing,
/// How wide a `Char16` the peer wrote is. The writer's counterpart:
/// a reader two bytes narrower than the stream returns the right
/// character and then reads every later member two bytes early.
wchar_width: WcharWidth,
/// How the peer framed a scalar `Char16`. See
/// [`crate::writer::WcharFraming`]: an ORB writes an octet count and
/// then the octets, and a reader that expected a bare code unit
/// takes the count for half a character.
wchar_framing: WcharFraming,
/// In what byte order the peer put wide code units. See
/// [`crate::writer::WstringUnitOrder`].
wstring_unit_order: WstringUnitOrder,
/// Whether a PL_CDR1 stream in another framing than `pl_cdr1` is
/// read or reported. See [`PlCdr1Reading`].
pl_cdr1_reading: PlCdr1Reading,
}
impl<'a> Reader<'a> {
/// A reader over `bytes` in the given byte order.
#[must_use]
pub const fn new(bytes: &'a [u8], endianness: Endianness) -> Self {
Self {
bytes,
at: 0,
endianness,
origin: 0,
max_align: 8,
pl_cdr1: PlCdr1Framing::DEFAULT,
wchar_width: WcharWidth::Two,
wchar_framing: WcharFraming::Fixed,
wstring_unit_order: WstringUnitOrder::Stream,
pl_cdr1_reading: PlCdr1Reading::Strict,
}
}
/// A reader whose alignment is capped at `max_align`. Four for
/// XCDR2, eight for XCDR1.
#[must_use]
pub const fn with_max_align(bytes: &'a [u8], endianness: Endianness, max_align: usize) -> Self {
Self {
bytes,
at: 0,
endianness,
origin: 0,
max_align: if max_align < 1 { 1 } else { max_align },
pl_cdr1: PlCdr1Framing::DEFAULT,
wchar_width: WcharWidth::Two,
wchar_framing: WcharFraming::Fixed,
wstring_unit_order: WstringUnitOrder::Stream,
pl_cdr1_reading: PlCdr1Reading::Strict,
}
}
/// The same reader expecting a `Char16` at another width.
///
/// Set from the same
/// `zerodds_ast_layout::WstringLengthPolicy::character_width` the
/// writer was set from, which is what keeps the two agreeing.
#[must_use]
pub const fn with_wchar_width(mut self, width: WcharWidth) -> Self {
self.wchar_width = width;
self
}
/// The `Char16` width in force.
#[must_use]
pub const fn wchar_width(&self) -> WcharWidth {
self.wchar_width
}
/// The same reader expecting another scalar `Char16` framing.
///
/// Set from the same `zerodds_ast_layout::WcharFraming` the writer
/// and the layout plan were set from.
#[must_use]
pub const fn with_wchar_framing(mut self, framing: WcharFraming) -> Self {
self.wchar_framing = framing;
self
}
/// The `Char16` framing in force.
#[must_use]
pub const fn wchar_framing(&self) -> WcharFraming {
self.wchar_framing
}
/// The same reader expecting another wide code-unit order.
#[must_use]
pub const fn with_wstring_unit_order(mut self, order: WstringUnitOrder) -> Self {
self.wstring_unit_order = order;
self
}
/// The wide code-unit order in force.
#[must_use]
pub const fn wstring_unit_order(&self) -> WstringUnitOrder {
self.wstring_unit_order
}
/// The same reader expecting another PL_CDR1 framing.
#[must_use]
pub const fn with_pl_cdr1_framing(mut self, framing: PlCdr1Framing) -> Self {
self.pl_cdr1 = framing;
self
}
/// The framing in force.
#[must_use]
pub const fn pl_cdr1_framing(&self) -> PlCdr1Framing {
self.pl_cdr1
}
/// The same reader taking, or reporting, a PL_CDR1 stream in another
/// framing than the one it was given.
///
/// [`PlCdr1Reading::Strict`] is the default. Under
/// [`PlCdr1Reading::Tolerant`] the reader accepts any flags on
/// `PID_EXTENDED` and `PID_LIST_END` (XTypes 1.3 §7.4.1.2.1), the
/// short and the extended header of a parameter whichever form the
/// framing names (§7.4.1.2.2), and a declared length that is the
/// value's own length or that length rounded up to four (§7.4.1.2).
#[must_use]
pub const fn with_pl_cdr1_reading(mut self, reading: PlCdr1Reading) -> Self {
self.pl_cdr1_reading = reading;
self
}
/// The PL_CDR1 reading in force.
#[must_use]
pub const fn pl_cdr1_reading(&self) -> PlCdr1Reading {
self.pl_cdr1_reading
}
/// The alignment cap in force.
#[must_use]
pub const fn max_align(&self) -> usize {
self.max_align
}
/// The byte order this reader was given.
///
/// A caller that has to decide something the stream's order settles
/// and that this reader does not read for it — a CORBA `wstring`
/// without a byte-order mark takes the message's order, CORBA 3.3
/// part 2 §9.3.1.6 — has to be able to ask.
#[must_use]
pub const fn endianness(&self) -> Endianness {
self.endianness
}
/// Where it is.
#[must_use]
pub const fn position(&self) -> usize {
self.at
}
/// How many bytes are left.
#[must_use]
pub const fn remaining(&self) -> usize {
self.bytes.len().saturating_sub(self.at)
}
/// Move to `target`, which is a position this reader has already
/// been told about.
///
/// The one way a header-driven decoder steps over a member it does
/// not declare: XTypes 1.3 §7.4.3.4.2 gives every `MMEMBER` an
/// extent, and §7.4.1.2 gives every PL_CDR1 parameter a length, so
/// where the next header starts is always known even when what sits
/// between is not.
///
/// # Errors
/// [`WireError::Truncated`] for a target past the end of the stream
/// and [`WireError::OutOfRange`] for one behind the current
/// position. Neither is silently clamped: a target behind the cursor
/// means the extent and the value disagree, and a reader that
/// stayed put would read the *value's* bytes as the next header.
pub fn skip_to(&mut self, target: usize) -> Result<(), WireError> {
if target < self.at {
return Err(WireError::OutOfRange {
at: self.at,
expected: alloc::format!(
"a member extent ending at or after {}, and this one ends at {target}",
self.at
),
});
}
if target > self.bytes.len() {
return Err(WireError::Truncated {
at: self.at,
needed: target - self.at,
available: self.remaining(),
});
}
self.at = target;
Ok(())
}
/// A string this reader has just taken off the wire, against the
/// bound of the member it is going into.
///
/// A method rather than a free call so the offset in a
/// [`WireError::Discarded`] is this reader's own. The narrowing
/// itself is [`crate::try_construct::fit_string`], which is where
/// the clause quotations are.
///
/// # Errors
/// As [`crate::try_construct::fit_string`].
pub fn fit_string(
&self,
value: String,
bound: usize,
behaviour: crate::TryConstruct,
) -> Result<crate::Narrowed<String>, WireError> {
crate::try_construct::fit_string(self.at, value, bound, behaviour)
}
/// A `wstring` this reader has just taken off the wire, against the
/// bound of the member it is going into.
///
/// The bound counts **UTF-16 code units** and not octets, which is
/// why this is its own method and not [`Reader::fit_string`] with
/// another argument. The narrowing itself is
/// [`crate::try_construct::fit_wstring`].
///
/// # Errors
/// As [`crate::try_construct::fit_wstring`].
pub fn fit_wstring(
&self,
value: String,
bound: usize,
behaviour: crate::TryConstruct,
) -> Result<crate::Narrowed<String>, WireError> {
crate::try_construct::fit_wstring(self.at, value, bound, behaviour)
}
/// A sequence this reader has just taken off the wire, against the
/// bound of the member it is going into.
///
/// # Errors
/// As [`crate::try_construct::fit_sequence`].
pub fn fit_sequence<T>(
&self,
value: Vec<T>,
bound: usize,
behaviour: crate::TryConstruct,
) -> Result<crate::Narrowed<Vec<T>>, WireError> {
crate::try_construct::fit_sequence(self.at, value, bound, behaviour)
}
/// A map this reader has just taken off the wire, against the bound
/// of the member it is going into.
///
/// The bound counts **entries**, which is the unit Table 17's
/// `O2.length <= T1.length` is in for a `map<K, V, N>`.
///
/// # Errors
/// As [`crate::try_construct::fit_map`].
pub fn fit_map<K: Ord, V>(
&self,
value: BTreeMap<K, V>,
bound: usize,
behaviour: crate::TryConstruct,
) -> Result<crate::Narrowed<BTreeMap<K, V>>, WireError> {
crate::try_construct::fit_map(self.at, value, bound, behaviour)
}
/// A map held as its entries (XTypes 1.3 §7.5.1.3.6), against the bound of the member it
/// is going into.
///
/// # Errors
/// As [`crate::try_construct::fit_map_entries`].
pub fn fit_map_entries<K, V>(
&self,
value: Vec<crate::MapEntry<K, V>>,
bound: usize,
behaviour: crate::TryConstruct,
) -> Result<crate::Narrowed<Vec<crate::MapEntry<K, V>>>, WireError> {
crate::try_construct::fit_map_entries(self.at, value, bound, behaviour)
}
/// Skip padding to `align`.
///
/// # Errors
/// [`WireError::Truncated`] when the padding the writer must have
/// written is not there. The offset reported is where the padding
/// began, which is where the stream stopped agreeing with itself —
/// letting the position run past the buffer instead put every later
/// error at an offset the stream does not have.
pub fn align_to(&mut self, align: usize) -> Result<(), WireError> {
let align = if align < self.max_align {
align
} else {
self.max_align
};
if align <= 1 {
return Ok(());
}
let offset = self.at - self.origin;
let padding = (align - (offset % align)) % align;
if padding > self.remaining() {
return Err(WireError::Truncated {
at: self.at,
needed: padding,
available: self.remaining(),
});
}
self.at += padding;
Ok(())
}
/// Begin a collection element whose alignment is counted from its
/// own first octet, returning the origin it replaced.
///
/// The read-side pair of [`Writer::begin_packed_element`](crate::Writer::begin_packed_element), and it
/// has to move with it: a reader that aligned the departure from the
/// encapsulation would consume the padding its writer did not emit
/// and misread every element after the first.
pub fn begin_packed_element(&mut self) -> usize {
let saved = self.origin;
self.origin = self.at;
saved
}
/// Restore the stream origin a packed element displaced. The pair of
/// [`Self::begin_packed_element`].
pub fn finish_packed_element(&mut self, saved: usize) {
self.origin = saved;
}
// -- primitives ------------------------------------------------------
/// `boolean`.
///
/// # Errors
/// [`WireError::Truncated`], or [`WireError::OutOfRange`] for a byte
/// that is neither 0 nor 1 — a boolean that is 2 is a stream that
/// disagrees with its own type.
pub fn read_bool(&mut self) -> Result<bool, WireError> {
let at = self.at;
match self.read_u8()? {
0 => Ok(false),
1 => Ok(true),
_ => Err(WireError::OutOfRange {
at,
expected: String::from("a boolean of 0 or 1"),
}),
}
}
/// `octet` and `uint8`.
///
/// # Errors
/// [`WireError::Truncated`].
pub fn read_u8(&mut self) -> Result<u8, WireError> {
let byte = *self.bytes.get(self.at).ok_or(WireError::Truncated {
at: self.at,
needed: 1,
available: self.remaining(),
})?;
self.at += 1;
Ok(byte)
}
/// `int8`.
///
/// # Errors
/// As [`Reader::read_u8`].
pub fn read_i8(&mut self) -> Result<i8, WireError> {
#[allow(clippy::cast_possible_wrap)]
Ok(self.read_u8()? as i8)
}
/// `char`.
///
/// # Errors
/// As [`Reader::read_u8`].
pub fn read_char(&mut self) -> Result<char, WireError> {
Ok(char::from(self.read_u8()?))
}
/// `wchar`, at the width [`Reader::wchar_width`] names.
///
/// The four-byte form is RTI's XCDR1 one, where the high sixteen
/// bits are zero padding around a code unit. A value that uses them
/// is refused rather than truncated: narrowing it here would return
/// a character the peer did not send.
///
/// `position` selects the framing exactly as it does on the writing
/// side, and for the same reason: under
/// [`WcharFraming::GiopLengthPrefixedMemberOnly`] a member carries a
/// count octet and an element does not, so a reader given the wrong
/// position takes the first half of a code unit for a count and
/// refuses — or, at the one width where the count is a plausible
/// unit, returns a character nobody sent.
///
/// # Errors
/// [`WireError::Truncated`], or [`WireError::OutOfRange`] for a code
/// point that is not a character, or for a four-byte character
/// holding more than one UTF-16 code unit.
pub fn read_wchar(&mut self, position: WcharPosition) -> Result<char, WireError> {
let width = self.wchar_width;
let at = self.at;
let value = if self.wchar_framing.has_length_octet(position) {
self.read_giop_wchar()?
} else {
self.align_to(width.bytes())?;
match width {
WcharWidth::Two => u32::from(self.read_u16()?),
WcharWidth::Four => self.read_u32()?,
}
};
if value > 0xFFFF {
return Err(WireError::OutOfRange {
at,
expected: String::from("a four-byte wchar holding one UTF-16 code unit"),
});
}
char::from_u32(value).ok_or(WireError::OutOfRange {
at,
expected: String::from("a Unicode code point"),
})
}
/// [`Reader::read_wchar`] at the member position.
///
/// The reading counterpart of `Writer::write_wchar_member`, and
/// there for the same reason: a collection combinator takes a path
/// of one argument.
///
/// # Errors
/// As [`Reader::read_wchar`].
pub fn read_wchar_member(&mut self) -> Result<char, WireError> {
self.read_wchar(WcharPosition::Member)
}
/// [`Reader::read_wchar`] at the collection-element position.
///
/// # Errors
/// As [`Reader::read_wchar`].
pub fn read_wchar_element(&mut self) -> Result<char, WireError> {
self.read_wchar(WcharPosition::Element)
}
/// CORBA §9.3.1.6's GIOP 1.2 `wchar`: an octet count, then that many
/// octets, aligned to one.
///
/// The count is checked against the configured code-set width rather
/// than believed. A stream whose first octet says `0xE4` — which is
/// what a bare two-octet `wchar` looks like read this way — would
/// otherwise consume 228 octets of whatever followed and return a
/// character assembled out of the next four members. The count is
/// the one field a peer controls and the one a reader must never let
/// decide how much it reads.
///
/// # Errors
/// [`WireError::OutOfRange`] for a count that is not the configured
/// width, [`WireError::Truncated`] for a stream that ends inside the
/// octets.
fn read_giop_wchar(&mut self) -> Result<u32, WireError> {
let at = self.at;
let order = self.wstring_unit_order;
let expected = self.wchar_width.bytes();
let declared = usize::from(self.read_u8()?);
if declared != expected {
return Err(WireError::OutOfRange {
at,
expected: format!(
"a GIOP 1.2 wchar of {expected} octets, the configured transmission code set, \
and not {declared}"
),
});
}
let octets = self.take(expected)?;
let mut value: u32 = 0;
// A scalar cannot carry a byte-order mark, so `ByteOrderMark`
// means CORBA's default here — big-endian — exactly as
// `BigEndianAlways` does. `Writer::write_wchar` has the run.
let little = matches!(order, WstringUnitOrder::Stream) && self.endianness.is_little();
if little {
for (shift, octet) in octets.iter().enumerate() {
value |= u32::from(*octet) << (8 * shift);
}
} else {
for octet in octets {
value = (value << 8) | u32::from(*octet);
}
}
Ok(value)
}
/// A bitmask, read from the holder its `@bit_bound` names, reduced to
/// the bits the type declares.
///
/// `declared` is the set of positions the bitmask names, one bit per
/// position, and it is an argument rather than a runtime table
/// because the type is the generated code's and the wire is this
/// crate's.
///
/// DDS-XTypes 1.3 §7.4.1.1.3.2: "the states of the remaining
/// serialized bits are not specified, and those bits are not
/// considered to be part of the bitmask", and Table 18: "Preserve bit
/// values by index for all bits identified in both T1 and T2." A bit
/// the stream sets and the type does not name is therefore not part
/// of the value this type reads, and it is dropped; it is not a
/// reason to refuse the sample, which would make a reader refuse a
/// writer whose type Table 18 declares assignable.
///
/// # Errors
/// [`WireError::Truncated`] for a short stream, and
/// [`WireError::OutOfRange`] for a bound outside 1..=64.
pub fn read_bitmask(&mut self, bit_bound: u16, declared: u64) -> Result<u64, WireError> {
if bit_bound == 0 || bit_bound > 64 {
return Err(WireError::OutOfRange {
at: self.at,
expected: alloc::format!(
"a bitmask bound between 1 and 64, and this one is {bit_bound}"
),
});
}
let bits = match bit_bound {
0..=8 => u64::from(self.read_u8()?),
9..=16 => u64::from(self.read_u16()?),
17..=32 => u64::from(self.read_u32()?),
_ => self.read_u64()?,
};
Ok(bits & declared)
}
/// A bitset's anchor integer, of the width the plan sized it at.
///
/// The counterpart of `Writer::write_bitset`; the unpacking of the
/// fields out of the anchor is the generated code's, because the
/// positions belong to the type and not to the wire.
///
/// # Errors
/// [`WireError::Truncated`] for a short stream, and
/// [`WireError::OutOfRange`] for an anchor that is not one, two,
/// four or eight octets.
pub fn read_bitset(&mut self, anchor_bytes: u32) -> Result<u64, WireError> {
match anchor_bytes {
1 => Ok(u64::from(self.read_u8()?)),
2 => Ok(u64::from(self.read_u16()?)),
4 => Ok(u64::from(self.read_u32()?)),
8 => self.read_u64(),
other => Err(WireError::OutOfRange {
at: self.at,
expected: alloc::format!(
"a bitset anchor of 1, 2, 4 or 8 octets, and this one is {other}"
),
}),
}
}
/// `unsigned short`.
///
/// # Errors
/// [`WireError::Truncated`].
pub fn read_u16(&mut self) -> Result<u16, WireError> {
self.align_to(2)?;
let bytes: [u8; 2] = self.take(2)?.try_into().map_err(|_| WireError::Truncated {
at: self.at,
needed: 2,
available: self.remaining(),
})?;
Ok(if self.endianness.is_little() {
u16::from_le_bytes(bytes)
} else {
u16::from_be_bytes(bytes)
})
}
/// A 16-bit value in **little-endian** order whatever the stream's
/// order is. The counterpart of `Writer::write_u16_little`, and see
/// there for the one caller it has.
///
/// # Errors
/// [`WireError::Truncated`].
pub fn read_u16_little(&mut self) -> Result<u16, WireError> {
self.align_to(2)?;
let bytes: [u8; 2] = self.take(2)?.try_into().map_err(|_| WireError::Truncated {
at: self.at,
needed: 2,
available: self.remaining(),
})?;
Ok(u16::from_le_bytes(bytes))
}
/// A 16-bit value in **big-endian** order whatever the stream's
/// order is. The counterpart of `Writer::write_u16_big`.
///
/// # Errors
/// [`WireError::Truncated`].
pub fn read_u16_big(&mut self) -> Result<u16, WireError> {
self.align_to(2)?;
let bytes: [u8; 2] = self.take(2)?.try_into().map_err(|_| WireError::Truncated {
at: self.at,
needed: 2,
available: self.remaining(),
})?;
Ok(u16::from_be_bytes(bytes))
}
/// A 32-bit value in **little-endian** order whatever the stream's
/// order is. The counterpart of `Writer::write_u32_little`.
///
/// # Errors
/// [`WireError::Truncated`].
pub fn read_u32_little(&mut self) -> Result<u32, WireError> {
self.align_to(4)?;
let bytes: [u8; 4] = self.take(4)?.try_into().map_err(|_| WireError::Truncated {
at: self.at,
needed: 4,
available: self.remaining(),
})?;
Ok(u32::from_le_bytes(bytes))
}
/// A 32-bit value in **big-endian** order whatever the stream's
/// order is. The counterpart of `Writer::write_u32_big`.
///
/// # Errors
/// [`WireError::Truncated`].
pub fn read_u32_big(&mut self) -> Result<u32, WireError> {
self.align_to(4)?;
let bytes: [u8; 4] = self.take(4)?.try_into().map_err(|_| WireError::Truncated {
at: self.at,
needed: 4,
available: self.remaining(),
})?;
Ok(u32::from_be_bytes(bytes))
}
/// `short`.
///
/// # Errors
/// As [`Reader::read_u16`].
pub fn read_i16(&mut self) -> Result<i16, WireError> {
#[allow(clippy::cast_possible_wrap)]
Ok(self.read_u16()? as i16)
}
/// `unsigned long`.
///
/// # Errors
/// [`WireError::Truncated`].
pub fn read_u32(&mut self) -> Result<u32, WireError> {
self.align_to(4)?;
let bytes: [u8; 4] = self.take(4)?.try_into().map_err(|_| WireError::Truncated {
at: self.at,
needed: 4,
available: self.remaining(),
})?;
Ok(if self.endianness.is_little() {
u32::from_le_bytes(bytes)
} else {
u32::from_be_bytes(bytes)
})
}
/// `long`.
///
/// # Errors
/// As [`Reader::read_u32`].
pub fn read_i32(&mut self) -> Result<i32, WireError> {
#[allow(clippy::cast_possible_wrap)]
Ok(self.read_u32()? as i32)
}
/// `unsigned long long`.
///
/// # Errors
/// [`WireError::Truncated`].
pub fn read_u64(&mut self) -> Result<u64, WireError> {
self.align_to(8)?;
let bytes: [u8; 8] = self.take(8)?.try_into().map_err(|_| WireError::Truncated {
at: self.at,
needed: 8,
available: self.remaining(),
})?;
Ok(if self.endianness.is_little() {
u64::from_le_bytes(bytes)
} else {
u64::from_be_bytes(bytes)
})
}
/// `long long`.
///
/// # Errors
/// As [`Reader::read_u64`].
pub fn read_i64(&mut self) -> Result<i64, WireError> {
#[allow(clippy::cast_possible_wrap)]
Ok(self.read_u64()? as i64)
}
/// `float`.
///
/// # Errors
/// As [`Reader::read_u32`].
pub fn read_f32(&mut self) -> Result<f32, WireError> {
Ok(f32::from_bits(self.read_u32()?))
}
/// `double`.
///
/// # Errors
/// As [`Reader::read_u64`].
pub fn read_f64(&mut self) -> Result<f64, WireError> {
Ok(f64::from_bits(self.read_u64()?))
}
/// `long double`: all sixteen bytes, as IEEE 754 binary128.
///
/// The counterpart of [`Writer::write_f128`](crate::Writer::write_f128). This used to read the
/// first eight bytes as a `double` and skip the rest, which decoded
/// every value a `double` cannot hold as a different number and said
/// nothing.
///
/// The bytes are taken whole and reversed as a whole for a
/// big-endian stream, per CORBA 3.3 part 2 Figure 9.2. Alignment is
/// eight, capped to four under XCDR2 by
/// [`Reader::align_to`]'s `MAXALIGN`.
///
/// # Errors
/// [`WireError::Truncated`].
pub fn read_f128(&mut self) -> Result<Float128, WireError> {
self.align_to(8)?;
let bytes = self.take(16)?;
let mut sixteen = [0u8; 16];
sixteen.copy_from_slice(bytes);
Ok(if self.endianness.is_little() {
Float128::from_le_bytes(sixteen)
} else {
Float128::from_be_bytes(sixteen)
})
}
// -- strings ---------------------------------------------------------
/// `fixed<P, S>`: packed BCD, CORBA 3.3 part 2 §9.3.2.8, as a decimal
/// string.
///
/// A string and not a number, for the reason the twenty other language
/// columns of this project carry it as text too: `fixed<31, 2>` fits no
/// integer Rust has, and a binary float would be the wrong answer rather
/// than an approximate one, because `fixed` exists to be exact.
///
/// The text that comes back is the one spelling the declaration admits —
/// no leading zeros past a single one before the point, exactly `S`
/// after it, and a `-` only on a value that is neither zero nor
/// positive. `1.5` and `0001.500` are the same sample under
/// `fixed<7, 3>` and only one string can come out of it.
///
/// # Errors
/// [`WireError::Truncated`] for a stream that ends inside the field.
/// [`WireError::OutOfRange`] for a declaration outside IDL 4.2 rule
/// (42), a nibble that is neither a digit nor a sign, or — under
/// [`FixedDigits::ValuePrecision`] or [`FixedDigits::ValueStringDigits`] —
/// a field that presents no sign nibble within the widest `fixed` IDL
/// admits.
pub fn read_fixed(
&mut self,
digits: u8,
scale: u8,
policy: FixedDigits,
) -> Result<String, WireError> {
// The same bound as the writer's, on this side too: rule (42) caps
// the BCD form at 31 digits, and a reader asked for a wider field
// would take octets no declaration can name.
if digits == 0 || digits > 31 || scale > digits {
return Err(WireError::refused(
self.position(),
"a fixed whose digit count and scale can describe a value",
));
}
let mut read = Vec::with_capacity(usize::from(digits) + 2);
let nibbles = match policy {
FixedDigits::Declared => {
// The declaration decides how wide the field is, so the
// octet count is known before a byte is read.
let nibbles = usize::from(digits) + 1;
for _ in 0..nibbles.div_ceil(2) {
let byte = self.read_u8()?;
read.push(byte >> 4);
read.push(byte & 0x0f);
}
nibbles
}
FixedDigits::ValuePrecision | FixedDigits::ValueStringDigits => {
// The value decides, so the field's end is read out of the
// octets: §9.3.2.8 makes a digit `0` to `9` and a sign `0xa`
// to `0xf`, and the nibble count is even by construction, so
// the field ends at the first octet whose **low** nibble is
// a sign. That is what makes this form decodable at all. A
// field wider than `fixed<31, S>`'s sixteen octets is a
// stream that never presents a sign nibble, and is refused
// rather than read on.
let mut octets = 0usize;
loop {
if octets >= MAX_FIXED_OCTETS {
return Err(WireError::refused(
self.position(),
"a sign nibble within the widest fixed IDL admits",
));
}
let byte = self.read_u8()?;
read.push(byte >> 4);
read.push(byte & 0x0f);
octets += 1;
if byte & 0x0f >= FIRST_SIGN_NIBBLE {
break;
}
}
read.len()
}
};
// A leading zero nibble pads the pair when the nibble count is odd;
// it is not a digit and the writer put it there for the packing
// alone. Under the value's count the pad is already inside what was
// read, and stripping a leading zero changes no value.
let body = read.get(read.len() - nibbles..).unwrap_or(&[]);
let Some((sign, value)) = body.split_last() else {
return Err(WireError::refused(
self.position(),
"at least a sign nibble",
));
};
let negative = match sign {
0x0d | 0x0b => true,
0x0c | 0x0a | 0x0f | 0x0e => false,
_ => {
return Err(WireError::refused(
self.position(),
"a sign nibble, 0xc or 0xd",
));
}
};
// The point sits `scale` digits from the **right** and not `P - S`
// from the left. Under the declared count the two are the same
// arithmetic, because the field carries exactly `P` digits; under
// the value's count it carries fewer, and a value with fewer digits
// than the scale — `1c` under `fixed<5, 2>` — needs its integer part
// written as a single zero. Enough leading zeros are prepended for
// the integer part to have at least one digit, which makes the split
// below unconditional.
let mut spelled = String::with_capacity(value.len() + usize::from(scale) + 1);
for _ in value.len()..usize::from(scale) + 1 {
spelled.push('0');
}
for digit in value {
if *digit > 9 {
return Err(WireError::refused(
self.position(),
"a decimal digit nibble",
));
}
spelled.push(char::from(b'0' + digit));
}
// The one spelling the declaration admits: leading zeros beyond a
// single one go, and a negative zero comes back positive because
// §9.3.2.8 has one zero and gives it the positive sign nibble.
let split = spelled.len() - usize::from(scale);
let whole = spelled[..split].trim_start_matches('0');
let fraction = &spelled[split..];
let mut text = String::with_capacity(spelled.len() + 2);
if negative && !(whole.is_empty() && fraction.bytes().all(|byte| byte == b'0')) {
text.push('-');
}
text.push_str(if whole.is_empty() { "0" } else { whole });
if scale > 0 {
text.push('.');
text.push_str(fraction);
}
// A terminated BCD field can still exceed this declaration's bound
// under ValuePrecision. Apply the writer's shared rule before
// returning a value that the same type could never write back.
crate::writer::parse_fixed(&text, digits, scale, self.position())?;
Ok(text)
}
/// A string: a length including the terminator, the bytes, the NUL.
///
/// # Errors
/// [`WireError::ImpossibleLength`] for a length the stream cannot
/// hold — allocating for it is how a reader turns a bad message into
/// an out-of-memory. [`WireError::NotUtf8`] for bytes that are not.
pub fn read_string(&mut self) -> Result<String, WireError> {
// Aligned before the offset is taken, so `at` names the length
// field and not the padding in front of it. An error whose
// offset points at padding cannot be matched to a length field
// by anyone reading a hex dump.
self.align_to(4)?;
let at = self.at;
let length = self.read_u32()? as usize;
if length == 0 || length > self.remaining() + 1 {
return Err(WireError::ImpossibleLength {
at,
claimed: length,
available: self.remaining(),
});
}
// The length counts the terminator; the text is one byte less.
let text_at = self.at;
let text = self.take(length - 1)?;
let out = core::str::from_utf8(text)
.map_err(|_| WireError::NotUtf8 { at })?
.to_string();
// §7.4.1.1.2 puts a NUL at the end and the length counts it. A
// last byte that is not one is a frame that disagrees with
// itself, and reading past it takes the next member's first
// byte for a terminator.
let terminator_at = self.at;
if self.read_u8()? != 0 {
return Err(WireError::OutOfRange {
at: terminator_at,
expected: String::from("a NUL terminating the string, as its length counts"),
});
}
// A NUL inside the text is worse than a wrong terminator: every
// C-based peer stops at it, so the value this reader returns and
// the value the peer returns differ for bytes both accepted.
if let Some(offset) = out.bytes().position(|byte| byte == 0) {
return Err(WireError::OutOfRange {
at: text_at + offset,
expected: String::from("no NUL inside a string a C peer would stop at"),
});
}
Ok(out)
}
/// A `wstring`, in the cell `policy` and `order` name.
///
/// The default is XTypes 1.3 §7.4.1.1.2 — an octet count, "twice
/// the number of characters in the string", no terminator and no
/// byte-order mark to strip. Eight of the nine measured
/// implementations write something else in at least one axis, and
/// [`WstringLengthPolicy`] carries the octets and the argument.
///
/// # Nothing here looks at the length to decide which cell it is
///
/// The length is the field a hostile peer controls, and the cells
/// are not separable anyway: Fast DDS's `@final struct { wstring w;
/// long n; }` with `w = "ab", n = 7` is `02000000 61006200
/// 07000000`, and the specification's encoding of the same type with
/// `w = "a", n = 7` is `02000000 6100`, two octets of padding,
/// `07000000`. Twelve octets each, differing only in the content of
/// padding that no clause fixes. A reader choosing between those two
/// samples would not be resolving an ambiguity, it would be taking
/// its answer from the length field.
///
/// So the configured cell is read, and a length that cell cannot
/// have produced is refused with the cell it *does* fit named in
/// the error — a diagnosis after the refusal, not a parse decided
/// by the stream.
///
/// # Errors
///
/// [`WireError::WrongFraming`] for a length the configured cell
/// cannot have produced: an octet count the unit width does not
/// divide, or a count of zero where C2 says the count includes a
/// terminator. Rounding an odd octet count down would leave the
/// remainder in the stream and put every later member at the wrong
/// offset.
///
/// [`WireError::ImpossibleLength`] for a length past the end of the
/// stream — allocating for it is how a reader turns a bad message
/// into an out-of-memory.
///
/// [`WireError::OutOfRange`] for a four-octet character above
/// `0xFFFF` (the wide form zero-extends a code *unit*, so a value
/// that does not fit one is not a form this reader knows how to
/// narrow), for a terminator that is not a NUL where one is
/// expected, for a NUL inside the text where a terminator is
/// expected — every C peer stops at it, so this reader's value and
/// the peer's would differ for octets both accepted — and for units
/// that are not well-formed UTF-16. An unpaired surrogate is
/// refused rather than replaced: U+FFFD in its place would be a
/// character the sender never had.
///
/// That last refusal catches less than it looks. Fast CDR's
/// truncation of `U+1D11E` is `U+D11E`, a Hangul syllable and
/// well-formed UTF-16, so the wrong character arrives with nothing
/// to object to. Only a code point whose low sixteen bits land in
/// `D800..=DFFF` fails here — which is why
/// [`Writer::write_wstring`](crate::Writer::write_wstring) refuses to produce the truncation in
/// the first place.
pub fn read_wstring(
&mut self,
policy: WstringLengthPolicy,
order: WstringUnitOrder,
position: WirePosition,
) -> Result<String, WireError> {
// Aligned before the offset is taken, so `at` names the length
// field and not the padding in front of it.
self.align_to(4)?;
let at = self.at;
let declared = self.read_u32()? as usize;
let width = policy.character_width(position);
// The three coordinates, not a list of named forms. C1 says
// whether the count is of units or of the octets they occupy,
// C3 says what an octet count divides by, and C2 is handled
// after the run because a terminator occupies a unit on the
// wire whichever way it is counted.
let count = match policy.counts_at(position) {
WstringLengthCounts::Characters => declared,
WstringLengthCounts::Octets => {
if declared % width != 0 {
return Err(wstring_form_mismatch(policy, declared, at));
}
declared / width
}
};
let mut units = self.read_wstring_units(count, width, order, at)?;
if matches!(policy.terminator_at(position), WstringTerminator::Present) {
// The count includes the NUL, so a count of zero is a frame
// that does not even hold its own terminator.
if declared < 1 {
return Err(wstring_form_mismatch(policy, declared, at));
}
let terminator = units.pop();
if terminator != Some(0) {
return Err(WireError::OutOfRange {
at,
expected: String::from(
"a NUL character ending the wstring, as its count includes one",
),
});
}
// A NUL inside the text is the narrow string's problem in
// wide form. Only where a terminator is expected — a cell
// without one is entitled to carry U+0000 as a character.
if let Some(offset) = units.iter().position(|unit| *unit == 0) {
return Err(WireError::OutOfRange {
at,
expected: format!(
"no NUL inside a wstring a C peer would stop at, and this one has \
a NUL at character {offset} of {}",
units.len()
),
});
}
}
// The mark is not a character of the string, and under
// `ByteOrderMark` it also decides the order of everything behind
// it — which `read_wstring_units` could not know until it had
// read it.
if order.has_mark() {
strip_byte_order_mark(&mut units);
}
String::from_utf16(&units).map_err(|_| WireError::OutOfRange {
at,
expected: String::from("well-formed UTF-16, not an unpaired surrogate"),
})
}
/// A `wstring` whose characters arrive as their own UTF-8 octets.
///
/// The read side of `Writer::write_wstring_utf8` and of
/// `WstringCodeUnits::Utf8`: the declared length is the UTF-8 octet
/// count, and the terminator, when the cell carries one, is the last
/// of those octets. A count the stream cannot hold is refused before
/// a byte is read, for the reason [`Reader::read_wstring`] gives.
///
/// # Errors
/// [`WireError::ImpossibleLength`] for a length larger than the
/// stream can hold, [`WireError::OutOfRange`] for a terminator cell
/// whose last octet is not a NUL or whose run carries one earlier,
/// and [`WireError::NotUtf8`] for octets that are not well-formed
/// UTF-8.
pub fn read_wstring_utf8(
&mut self,
policy: WstringLengthPolicy,
position: WirePosition,
) -> Result<String, WireError> {
// `read_u32` aligns to four, so `at` names the length field and
// not the padding in front of it.
let at = self.at;
let declared = self.read_u32()? as usize;
if declared > self.remaining() {
return Err(WireError::ImpossibleLength {
at,
claimed: declared,
available: self.remaining(),
});
}
let mut bytes = Vec::with_capacity(declared);
for _ in 0..declared {
bytes.push(self.read_u8()?);
}
if matches!(policy.terminator_at(position), WstringTerminator::Present) {
if bytes.pop() != Some(0) {
return Err(WireError::OutOfRange {
at,
expected: String::from(
"a NUL octet ending the wstring, as its count includes one",
),
});
}
if bytes.contains(&0) {
return Err(WireError::OutOfRange {
at,
expected: String::from(
"no NUL inside a wstring a C peer would stop at, and this one has one",
),
});
}
}
String::from_utf8(bytes).map_err(|_| WireError::NotUtf8 { at })
}
/// `count` characters of `width` octets each, as UTF-16 code units.
///
/// The four-octet form is RTI's XCDR1 one and TAO's, where a
/// character is a code unit zero-extended; a value above `0xFFFF`
/// there is not a form this reader knows how to narrow, so it is
/// refused rather than truncated — truncating is the defect the
/// whole policy exists because of.
fn read_wstring_units(
&mut self,
count: usize,
width: usize,
order: WstringUnitOrder,
at: usize,
) -> Result<Vec<u16>, WireError> {
let octets = count.checked_mul(width).unwrap_or(usize::MAX);
if octets > self.remaining() {
return Err(WireError::ImpossibleLength {
at,
claimed: octets,
available: self.remaining(),
});
}
let mut units = Vec::with_capacity(count);
for _ in 0..count {
if width == 4 {
// `ByteOrderMark` reads little-endian here and the mark
// is re-examined once the whole run is in: a mark that
// says `FFFE` means every unit has to be swapped back,
// which is `strip_byte_order_mark`'s second job.
let wide = match order {
WstringUnitOrder::Stream => self.read_u32()?,
WstringUnitOrder::BigEndianAlways => self.read_u32_big()?,
WstringUnitOrder::ByteOrderMark | WstringUnitOrder::HostUnswapped => {
self.read_u32_little()?
}
};
units.push(u16::try_from(wide).map_err(|_| WireError::OutOfRange {
at,
expected: format!(
"a four-octet wstring character holding one UTF-16 code unit, \
not {wide:08x}"
),
})?);
} else {
units.push(match order {
WstringUnitOrder::Stream => self.read_u16()?,
WstringUnitOrder::BigEndianAlways => self.read_u16_big()?,
WstringUnitOrder::ByteOrderMark | WstringUnitOrder::HostUnswapped => {
self.read_u16_little()?
}
});
}
}
Ok(units)
}
// -- framing ---------------------------------------------------------
/// A DHEADER: the length the members that follow occupy.
///
/// # Errors
/// [`WireError::ImpossibleLength`] when it claims more than is left.
pub fn read_dheader(&mut self) -> Result<usize, WireError> {
self.align_to(4)?;
let at = self.at;
let length = self.read_u32()? as usize;
if length > self.remaining() {
return Err(WireError::ImpossibleLength {
at,
claimed: length,
available: self.remaining(),
});
}
Ok(length)
}
/// An EMHEADER, unpacked into its parts, its NEXTINT and the extent
/// of the member it introduces.
///
/// # The NEXTINT of length codes 5 to 7 is not consumed
///
/// XTypes 1.3 §7.4.3.4.2 gives the NEXTINT of codes 5, 6 and 7 two
/// jobs: it "shall be reused also as part of the serialized member
/// ... because the serialization of certain members also starts
/// with an integer length, which would take exactly the same value
/// as NEXTINT. Therefore the use of length codes 5 to 7 saves 4
/// bytes in the serialization."
///
/// `Writer::write_emheader` reserves a placeholder for code 4 only,
/// for that reason. This read it for every code from 4 up and left
/// the position four bytes into the member — so the sequence that
/// followed lost its own length word and every byte after it was
/// read at the wrong offset. It is peeked now, and the member is
/// read from where it starts.
///
/// # The extent
///
/// `extent` is how many bytes the member occupies counted from the
/// position this call leaves the reader at, which is the number a
/// caller needs whichever code it turned out to be:
///
/// | code | extent |
/// |---|---|
/// | 0, 1, 2, 3 | 1, 2, 4, 8 |
/// | 4 | the NEXTINT, which this call consumed |
/// | 5 | 4 + NEXTINT |
/// | 6 | 4 + 4 × NEXTINT |
/// | 7 | 4 + 8 × NEXTINT |
///
/// # Errors
/// [`WireError::Truncated`], or [`WireError::ImpossibleLength`] for
/// a NEXTINT claiming more than the stream holds. Allocating on an
/// unchecked NEXTINT is how a reader turns a malformed message into
/// an out-of-memory; this returned `Ok` for `ffffffff`.
pub fn read_emheader(&mut self) -> Result<EmHeaderRead, WireError> {
let word = self.read_u32()?;
#[allow(clippy::cast_possible_truncation)]
let length_code = ((word >> 28) & 0x7) as u8;
let member_id = word & 0x0FFF_FFFF;
let must_understand = (word >> 31) != 0;
let at = self.at;
let (next_int, extent) = match length_code {
0 => (None, 1),
1 => (None, 2),
2 => (None, 4),
3 => (None, 8),
4 => {
let next = self.read_u32()? as usize;
(Some(next), next)
}
// Peeked, not consumed: the member's own serialization
// begins at this word.
code => {
let next = self.peek_u32()? as usize;
let scale = match code {
5 => 1,
6 => 4,
_ => 8,
};
let body = next.checked_mul(scale).ok_or(WireError::ImpossibleLength {
at,
claimed: next,
available: self.remaining(),
})?;
let extent = body.checked_add(4).ok_or(WireError::ImpossibleLength {
at,
claimed: next,
available: self.remaining(),
})?;
(Some(next), extent)
}
};
if extent > self.remaining() {
return Err(WireError::ImpossibleLength {
at,
claimed: extent,
available: self.remaining(),
});
}
Ok(EmHeaderRead {
member_id,
length_code,
must_understand,
next_int,
extent,
})
}
/// A PL_CDR1 parameter header, in whichever form it was written.
///
/// # The flag bits are not part of the id
///
/// XTypes 1.3 §7.4.1.2 splits the sixteen-bit field: the member id
/// is the low fourteen bits, bit 14 is must-understand and bit 15 is
/// implementation-specific. Cyclone DDS sets must-understand on
/// every parameter it writes, so its `PID_EXTENDED` goes out as
/// `0x7f01` and its sentinel as `0x7f02` —
/// `crates/ast-layout/tests/fixtures/vendor_bytes.md` §2 records
/// both, and RTI Connext writes the same sentinel.
///
/// Comparing the whole word against `0x3f01` and `0x3f02` therefore
/// recognised neither. A Cyclone parameter list read that way takes
/// the extended header's id and length for eight bytes of value, and
/// runs on past the sentinel into whatever follows it, with no error
/// anywhere. Two of the three implementations this project is
/// measured against write those bytes.
///
/// # Errors
/// [`WireError::Truncated`]; [`WireError::ImpossibleLength`] for a
/// declared length longer than the stream holds;
/// [`WireError::OutOfRange`] for an extended header whose short
/// length is not the 8 §7.4.1.2 fixes it at.
pub fn read_pid(&mut self) -> Result<PidRead, WireError> {
self.read_pid_as(ParameterRole::Member)
}
/// A PL_CDR1 parameter header in a named role.
///
/// The bytes are the same bytes; the role decides which length
/// convention [`Reader::finish_pid`] checks the declared length
/// against, because one implementation pads a `@mutable` member's
/// and not an `@optional` member's. See [`ParameterRole`].
///
/// # Errors
/// As [`Reader::read_pid`].
pub fn read_pid_as(&mut self, role: ParameterRole) -> Result<PidRead, WireError> {
self.align_to(4)?;
let pid = self.read_u16()?;
let at = self.at;
let length = self.read_u16()?;
let must_understand = pid & PID_MUST_UNDERSTAND != 0;
let id = pid & PID_ID_MASK;
if id == PID_EXTENDED & PID_ID_MASK {
// The marker's flags are asked at
// `WirePosition::ExtendedMarker` and nowhere else. They are
// not a property of the framing form: Cyclone and RTI write
// different forms and the same `7f01`, while Fast DDS and
// OpenDDS write the same form and `3f01` against `ff01`.
let expected = self
.pl_cdr1
.pid_flags
.flags_at(WirePosition::ExtendedMarker);
let found = pid & !PID_ID_MASK;
if found != expected && !self.pl_cdr1_reading.is_tolerant() {
return Err(self.wrong_framing(at, "PID_EXTENDED", expected, found));
}
if length != 8 {
return Err(WireError::OutOfRange {
at,
expected: String::from("a PID_EXTENDED header of 8 bytes, per §7.4.1.2"),
});
}
let member_header = self.read_u32()?;
let at = self.at;
let length = self.read_u32()? as usize;
if length > self.remaining() {
return Err(WireError::ImpossibleLength {
at,
claimed: length,
available: self.remaining(),
});
}
// The four flag bits are not part of the id. XTypes 1.3
// §7.4.1.2.1 splits the `eMemberHeader` into `FLAG_1` to
// `FLAG_4` and a 28-bit member id, and `FLAG_2` is the
// member's must-understand.
//
// Taking the whole word for the id gave a Cyclone member
// declared `@must_understand` an id of `0x40000001`, which
// matches nothing the model declares, and the member was
// **silently dropped**. Cyclone writes `01000040` for
// `@id(1) @must_understand` and `02000000` for the plain
// member beside it — `vendor_bytes.md` §2.
//
// The marker's own must-understand bit is a different bit
// in a different word and is checked above; Cyclone sets it
// on every parameter, so reading the member's property off
// it would make the whole list must-understand.
return Ok(PidRead {
pid: member_header & E_MEMBER_ID_MASK,
length,
end: false,
must_understand: member_header & E_FLAG_MUST_UNDERSTAND != 0,
// `FLAG_1` is `FLAG_IMPL_EXTENSION` in the extended
// header exactly as bit 15 is in the short one.
impl_extension: member_header & E_FLAG_IMPL_EXTENSION != 0,
// The extended header carries a 28-bit id and has no
// reserved range: 0x3F01 upwards are ordinary member ids
// there, which is why the short form needs the extended
// one for them at all.
reserved: false,
ignore: false,
role,
saved_origin: self.begin_parameter_value(),
});
}
let length = length as usize;
let end = id == PID_LIST_END & PID_ID_MASK;
if !end && self.pl_cdr1.require_extended && !self.pl_cdr1_reading.is_tolerant() {
return Err(WireError::WrongFraming {
at,
expected: alloc::string::String::from(
"PID_EXTENDED for every parameter, as Cyclone DDS writes",
),
found: alloc::format!("the short header for id {id}"),
fits: Some("--pl-cdr1-headers specification or short-true-length"),
});
}
// The sentinel's flags are asked at `WirePosition::Sentinel` and
// nowhere else. They are not the member's: OpenDDS sets both
// flags on every member id and closes with a bare `3f02`, while
// Cyclone and RTI leave the member bare and close with `7f02`.
let sentinel_flags = self.pl_cdr1.pid_flags.flags_at(WirePosition::Sentinel);
if end && pid & !PID_ID_MASK != sentinel_flags && !self.pl_cdr1_reading.is_tolerant() {
return Err(self.wrong_framing(at, "PID_LIST_END", sentinel_flags, pid & !PID_ID_MASK));
}
if !end && length > self.remaining() {
return Err(WireError::ImpossibleLength {
at,
claimed: length,
available: self.remaining(),
});
}
let saved_origin = if end {
self.origin
} else {
self.begin_parameter_value()
};
Ok(PidRead {
pid: u32::from(id),
length,
end,
must_understand,
// Under a framing that puts `FLAG_IMPL_EXTENSION` on every
// member id the bit no longer tells a vendor-private
// parameter from a user member — OpenDDS writes `c0010004`
// for `@id(1) long a`. It is reported absent there because
// under that framing it carries no information, and
// reporting it would send every member down the
// unknown-parameter path. The loss is the writing
// implementation's, not this reader's: a stream in which
// every id sets the bit has no way left to say "vendor
// private".
impl_extension: pid & PID_IMPL_EXTENSION != 0
&& self.pl_cdr1.pid_flags.flags_at(WirePosition::Member) & PID_IMPL_EXTENSION == 0,
// 0x3F01 and 0x3F02 never reach here — both are handled
// above — so what this catches is `PID_IGNORE` and the rest
// of §7.4.1.2.2's reserved range.
// The threshold is the framing's and not a constant: one
// implementation carries the whole low fourteen bits, and
// reading `@id(16383)` as a reserved parameter there drops
// the member without a word.
reserved: !end && id > self.pl_cdr1.highest_short_id,
ignore: id == PID_IGNORE,
role,
saved_origin,
})
}
/// Start a parameter's value, returning the origin it replaces.
///
/// The mirror of `Writer::begin_parameter_value`, and it has to
/// exist for the same clause. XTypes 1.3 §7.4.1.2: "Each element, or
/// parameter, within a parameter list data structure is simply a
/// CDR-encapsulated block of data." A block counts alignment from
/// its own start, so an eight-byte member at the head of one is
/// already aligned.
///
/// The writer was given this and the reader was not. Cyclone,
/// RTI and Fast DDS all put `Ext`'s `double` directly behind its
/// header — `crates/ast-layout/tests/fixtures/vendor_bytes.md` §2
/// records the header as `01000800`, an eight-byte value with no
/// padding in front of it. Counting from the stream instead put four
/// bytes of padding there that no vendor wrote, so the double came
/// out of the following bytes and every member after it was read at
/// the wrong offset. No error anywhere: eight bytes were available,
/// and eight bytes are a double.
fn begin_parameter_value(&mut self) -> usize {
let saved = self.origin;
self.origin = self.at;
saved
}
/// End a parameter's value and step to the next header.
///
/// The padding to four follows the value and is outside the
/// declared length, so the next parameter is found by alignment and
/// not by adding the length — which is what lets one reader take
/// RTI's rounded-up length and Fast DDS's exact one.
///
/// The padding is consumed here only under a framing that writes it
/// (`PlCdr1Framing::pad_after_value`), mirroring
/// `Writer::finish_pid`: under Fast DDS's and OpenDDS's framing a
/// parameter writes no padding of its own — the next header aligns
/// itself in `read_pid_as` — and a *final* `@optional` parameter of
/// a non-mutable type ends the stream where its value does, so an
/// unconditional alignment here read two bytes past the end of a
/// stream the writer had closed correctly (measured:
/// `zerodds-rust/opendds-xcdr1-*/annotation-must-understand-unannotated`).
///
/// # Errors
/// [`WireError::Truncated`] where the framing's padding is not
/// there.
pub fn finish_pid(&mut self, header: PidRead) -> Result<(), WireError> {
if !header.end {
self.check_declared_length(&header)?;
}
self.origin = header.saved_origin;
if self.pl_cdr1.pad_after_value {
return self.align_to(4);
}
Ok(())
}
/// End a parameter whose member this reader does not declare.
///
/// [`Reader::finish_pid`] finds the next header by alignment from
/// wherever the value's read left the cursor, which is the right
/// answer when the value *was* read; it also checks the declared
/// length against how far the cursor moved, which for a member
/// nobody read has moved nowhere. This one steps over the declared
/// length instead. §7.4.1.2 gives every parameter one, which is what
/// makes an unknown member skippable at all.
///
/// # Errors
/// [`WireError::Truncated`] where the declared length runs past the
/// stream, and whatever the alignment to the next header returns.
pub fn skip_parameter(&mut self, header: PidRead) -> Result<(), WireError> {
if !header.end {
// `origin` is the value's own start until it is put back,
// so the value ends a declared length after it.
let end = self.origin.saturating_add(header.length);
self.skip_to(end)?;
}
self.origin = header.saved_origin;
// The same mirror as `finish_pid`: only a framing that wrote
// the padding has it to skip, and the next header aligns
// itself.
if self.pl_cdr1.pad_after_value {
return self.align_to(4);
}
Ok(())
}
/// The declared length against what the configured framing would
/// have declared for the value that was just read.
///
/// `overran` in the probe already refuses a value longer than the
/// header allows, which is the malformed case. This is the other
/// one: a length that is well formed and belongs to a different
/// implementation. `short c` of `vendor_bytes.md`'s `ext_mutable`
/// is the whole difference — two bytes of value, declared as four
/// by Cyclone and RTI and as two by Fast DDS — so a member whose
/// value already fills its four-byte slot reveals nothing and is
/// silently accepted either way. That is a property of the bytes
/// and not a gap here.
fn check_declared_length(&self, header: &PidRead) -> Result<(), WireError> {
// `origin` is still the parameter value's own start: the caller
// restores it after this returns.
let read = self.at.saturating_sub(self.origin);
let padded = read.next_multiple_of(4);
let pads = self.pl_cdr1.pads_declared_length(header.role);
let expected = if pads { padded } else { read };
if header.length == expected {
return Ok(());
}
// §7.4.1.2: the length is the serialized member's own; DDSI-RTPS
// 2.5 §9.4.2.11 rounds it up to four. A tolerant reader takes
// either, and only those two.
if self.pl_cdr1_reading.is_tolerant() && (header.length == read || header.length == padded)
{
return Ok(());
}
// The other convention, and only that one: a length matching
// neither is a malformed stream and gets the plain error.
let other = if pads { read } else { padded };
if header.length != other {
return Err(WireError::OutOfRange {
at: self.at,
expected: alloc::format!(
"a parameter length of {expected} for the {read} byte(s) that followed it"
),
});
}
Err(WireError::WrongFraming {
at: self.at,
expected: alloc::string::String::from(if pads {
"a parameter length rounded up to four (DDSI-RTPS 2.5 §9.4.2.11)"
} else {
"a parameter length that stops at the value (XTypes 1.3 §7.4.1.2)"
}),
found: alloc::format!("{} for {read} byte(s) of value", header.length),
fits: Some(if pads {
"--pl-cdr1-headers specification or short-true-length"
} else {
"--pl-cdr1-headers short-rounded-length or always-extended"
}),
})
}
/// A must-understand bit that is not the one the framing calls for.
fn wrong_framing(&self, at: usize, what: &str, expected: u16, found: u16) -> WireError {
// Named rather than printed as a number: `0x4000` and `0xc000`
// are the two flags of §7.4.1.2, and a reader who has to look
// them up has been told a hexadecimal literal rather than what
// is wrong.
let names = |flags: u16| match (
flags & PID_MUST_UNDERSTAND != 0,
flags & PID_IMPL_EXTENSION != 0,
) {
(false, false) => "no flags",
(true, false) => "the must-understand bit",
(false, true) => "the implementation-extension bit",
(true, true) => "the must-understand and implementation-extension bits",
};
WireError::WrongFraming {
at,
expected: alloc::format!("{what} with {}", names(expected)),
found: alloc::format!("it carries {}", names(found)),
// Both positions are `PidFlagPolicy`'s now, so the flag that
// fits is `--pid-flags` and not the framing form's.
fits: Some(
match (
found & PID_MUST_UNDERSTAND != 0,
found & PID_IMPL_EXTENSION != 0,
what,
) {
(_, true, _) => "--pid-flags impl-extension-members",
(true, false, "PID_EXTENDED") => "--pid-flags sentinel-must-understand",
(false, false, "PID_EXTENDED") => "--pid-flags bare",
(true, false, _) => "--pid-flags sentinel-must-understand",
(false, false, _) => "--pid-flags bare or impl-extension-members",
},
),
}
}
// -- collections -----------------------------------------------------
/// A sequence: a count, then each element.
///
/// # Errors
/// [`WireError::ImpossibleLength`] for a count larger than the
/// stream can hold — a count is one element or more, so a stream
/// with fewer bytes than elements cannot be honest about it.
pub fn read_sequence<T, F: Fn(&mut Self) -> Result<T, WireError>>(
&mut self,
element: F,
) -> Result<Vec<T>, WireError> {
self.align_to(4)?;
let at = self.at;
let count = self.read_u32()? as usize;
if count > self.remaining() {
return Err(WireError::ImpossibleLength {
at,
claimed: count,
available: self.remaining(),
});
}
// No `with_capacity` on the claimed count. The count is checked
// against the bytes left, which bounds it — but an element is
// not a byte, and `Vec::<String>::with_capacity(n)` on a count
// that is honest about bytes reserves twenty-four times the
// message. Growing as elements arrive never exceeds twice what
// was actually read.
let mut out = Vec::new();
for _ in 0..count {
out.push(element(self)?);
}
Ok(out)
}
/// An array of `count` elements, with no count on the wire.
///
/// # Errors
/// Whatever the element reader returns.
pub fn read_array<T, F: Fn(&mut Self) -> Result<T, WireError>>(
&mut self,
element: F,
count: usize,
) -> Result<Vec<T>, WireError> {
let mut out = Vec::with_capacity(count.min(self.remaining()));
for _ in 0..count {
out.push(element(self)?);
}
Ok(out)
}
/// A sequence behind a delimiter. XTypes 1.3 §7.4.3.5 rule (12).
///
/// The delimiter is read and discarded: it exists so a reader can
/// skip a collection it cannot decode, and this reader decodes it.
///
/// # Errors
/// As [`Reader::read_sequence`], plus a short buffer at the
/// delimiter.
pub fn read_delimited_sequence<T, F: Fn(&mut Self) -> Result<T, WireError>>(
&mut self,
element: F,
) -> Result<Vec<T>, WireError> {
self.within_delimiter(|inner| inner.read_sequence(element))
}
/// An array behind a delimiter. Rule (9), the array counterpart.
///
/// # Errors
/// As [`Reader::read_array`], plus a short buffer at the delimiter.
pub fn read_delimited_array<T, F: Fn(&mut Self) -> Result<T, WireError>>(
&mut self,
element: F,
count: usize,
) -> Result<Vec<T>, WireError> {
self.within_delimiter(|inner| inner.read_array(element, count))
}
/// A map: an entry count, then each entry's key followed by its
/// value.
///
/// The count is entries and not halves — see
/// [`crate::Writer::write_map`]
/// for the clause and the recorded foreign measurement it is read
/// against.
///
/// # A repeated key is refused
///
/// [`BTreeMap::insert`] would answer a second entry under a key it
/// already holds by returning the first and keeping the second: the
/// sample would decode, report success, and hold fewer entries than
/// the count on the wire said. That is the silent half of a bad
/// decode, so it is an error here. A `map` is not supposed to carry
/// one, which is exactly why a stream that does is a statement about
/// the stream.
///
/// # Errors
/// [`WireError::ImpossibleLength`] for a count larger than the
/// stream can hold, [`WireError::OutOfRange`] for a repeated key,
/// and whatever either half's function returns.
pub fn read_map<K, V, FK, FV>(
&mut self,
key: FK,
value: FV,
) -> Result<BTreeMap<K, V>, WireError>
where
K: Ord,
FK: Fn(&mut Self) -> Result<K, WireError>,
FV: Fn(&mut Self) -> Result<V, WireError>,
{
self.align_to(4)?;
let at = self.at;
let count = self.read_u32()? as usize;
// An entry is two values and neither is empty, so a stream with
// fewer bytes left than entries cannot be honest about its
// count. The same check `read_sequence` makes, and for the same
// reason: it bounds the loop before anything is allocated.
if count > self.remaining() {
return Err(WireError::ImpossibleLength {
at,
claimed: count,
available: self.remaining(),
});
}
let mut out = BTreeMap::new();
for _ in 0..count {
let at = self.at;
let k = key(self)?;
let v = value(self)?;
if out.insert(k, v).is_some() {
return Err(WireError::OutOfRange {
at,
expected: format!(
"{count} entries under distinct keys, and this one repeats a key already \
in the map"
),
});
}
}
Ok(out)
}
/// A map as the sequence of its entries (XTypes 1.3 §7.5.1.3.6, §7.4.1.1.4): the entries
/// in the order the stream carries them. Unlike [`Reader::read_map`] this does not refuse
/// a key that repeats: the wire carries a sequence, and what a repeated key means is the
/// application's.
///
/// # Errors
/// [`WireError::ImpossibleLength`] for a count the stream cannot hold, and whatever either
/// half's function returns.
pub fn read_map_entries<K, V, FK, FV>(
&mut self,
key: FK,
value: FV,
) -> Result<Vec<crate::MapEntry<K, V>>, WireError>
where
FK: Fn(&mut Self) -> Result<K, WireError>,
FV: Fn(&mut Self) -> Result<V, WireError>,
{
self.align_to(4)?;
let at = self.at;
let count = self.read_u32()? as usize;
if count > self.remaining() {
return Err(WireError::ImpossibleLength {
at,
claimed: count,
available: self.remaining(),
});
}
let mut out = Vec::with_capacity(count);
for _ in 0..count {
let k = key(self)?;
let v = value(self)?;
out.push(crate::MapEntry::new(k, v));
}
Ok(out)
}
/// The entries of a map behind a delimiter. Rule (15).
///
/// # Errors
/// As [`Reader::read_map_entries`], plus a short buffer at the delimiter.
pub fn read_delimited_map_entries<K, V, FK, FV>(
&mut self,
key: FK,
value: FV,
) -> Result<Vec<crate::MapEntry<K, V>>, WireError>
where
FK: Fn(&mut Self) -> Result<K, WireError>,
FV: Fn(&mut Self) -> Result<V, WireError>,
{
self.within_delimiter(|inner| inner.read_map_entries(key, value))
}
/// A map behind a delimiter. XTypes 1.3 §7.4.3.5 rule (15).
///
/// The delimiter is read and discarded, as
/// [`Reader::read_delimited_sequence`] does.
///
/// # Errors
/// As [`Reader::read_map`], plus a short buffer at the delimiter.
pub fn read_delimited_map<K, V, FK, FV>(
&mut self,
key: FK,
value: FV,
) -> Result<BTreeMap<K, V>, WireError>
where
K: Ord,
FK: Fn(&mut Self) -> Result<K, WireError>,
FV: Fn(&mut Self) -> Result<V, WireError>,
{
self.within_delimiter(|inner| inner.read_map(key, value))
}
/// An array of a length the *type* fixes, as `[T; N]`.
///
/// The count is in the type — XTypes 1.3 §7.4.3.5 — so a generated
/// field for `long grid[2][3]` is `[[i32; 3]; 2]` and not a `Vec`.
/// [`Reader::read_array`] hands back a `Vec`, which is the right
/// answer for a caller that wants one and does not fit that field:
/// a `Vec` of the wrong length assigns to nothing, and one of the
/// right length still has to be converted. `N` is inferred from the
/// destination, so the generated call carries no turbofish.
///
/// # Errors
/// As [`Reader::read_array`], plus [`WireError::OutOfRange`] if the
/// elements read do not number `N` — which cannot happen given the
/// loop below, and is returned rather than unwrapped because a
/// panic in a decoder is a denial of service a peer can send.
pub fn read_array_n<T, const N: usize, F: Fn(&mut Self) -> Result<T, WireError>>(
&mut self,
element: F,
) -> Result<[T; N], WireError> {
let at = self.at;
let mut out = Vec::with_capacity(N.min(self.remaining().saturating_add(1)));
for _ in 0..N {
out.push(element(self)?);
}
out.try_into().map_err(|_| WireError::OutOfRange {
at,
expected: alloc::format!("an array of {N} elements"),
})
}
/// A fixed-length array behind a delimiter. Rule (9).
///
/// # Errors
/// As [`Reader::read_array_n`], plus a short buffer at the
/// delimiter.
pub fn read_delimited_array_n<T, const N: usize, F: Fn(&mut Self) -> Result<T, WireError>>(
&mut self,
element: F,
) -> Result<[T; N], WireError> {
self.within_delimiter(|inner| inner.read_array_n(element))
}
/// Read a delimited value, held to the extent the DHEADER declared.
///
/// The delimiter was read and thrown away. A collection whose
/// elements ran past it went on consuming the *next* member's bytes
/// and returned them as its own — a decode that succeeds and is
/// wrong, which is the harder half of the silent-drop class. One
/// that stopped short left bytes inside its own extent unread, and
/// said nothing about those either.
///
/// The inner reader sees the stream truncated at the extent, so it
/// cannot reach past it, and the position has to land exactly on the
/// end. Alignment is counted from the stream's origin and not from
/// the delimiter, so the inner reader keeps this one's offset:
/// XTypes 1.3 §7.4.1 aligns from the encapsulation.
///
/// # Errors
/// [`WireError::ImpossibleLength`] for a delimiter longer than the
/// stream, [`WireError::OutOfRange`] where the value did not fill
/// its extent, plus whatever the value's own read returned.
fn within_delimiter<T, F: FnOnce(&mut Self) -> Result<T, WireError>>(
&mut self,
read: F,
) -> Result<T, WireError> {
let length = self.read_dheader()?;
let end = self
.at
.checked_add(length)
.ok_or(WireError::ImpossibleLength {
at: self.at,
claimed: length,
available: self.remaining(),
})?;
let mut inner = Self {
bytes: self.bytes.get(..end).ok_or(WireError::ImpossibleLength {
at: self.at,
claimed: length,
available: self.remaining(),
})?,
at: self.at,
endianness: self.endianness,
origin: self.origin,
max_align: self.max_align,
pl_cdr1: self.pl_cdr1,
wchar_width: self.wchar_width,
wchar_framing: self.wchar_framing,
wstring_unit_order: self.wstring_unit_order,
pl_cdr1_reading: self.pl_cdr1_reading,
};
let value = read(&mut inner)?;
if inner.at != end {
return Err(WireError::OutOfRange {
at: inner.at,
expected: alloc::format!("a value filling the {length} bytes its DHEADER declared"),
});
}
self.at = end;
Ok(value)
}
/// The next four bytes as a word, without moving.
///
/// The one place a header has to be inspected and left where it is.
fn peek_u32(&self) -> Result<u32, WireError> {
let end = self.at.checked_add(4).ok_or(WireError::Truncated {
at: self.at,
needed: 4,
available: self.remaining(),
})?;
let bytes: [u8; 4] = self
.bytes
.get(self.at..end)
.and_then(|slice| slice.try_into().ok())
.ok_or(WireError::Truncated {
at: self.at,
needed: 4,
available: self.remaining(),
})?;
Ok(if self.endianness.is_little() {
u32::from_le_bytes(bytes)
} else {
u32::from_be_bytes(bytes)
})
}
/// `count` bytes as they lie, with no alignment and no
/// interpretation.
///
/// The opaque payloads have no other reader: a GIOP request body is
/// a byte string whose type the receiving skeleton knows and this
/// reader does not, a CDR encapsulation carries its own byte-order
/// octet and has to be read by a second reader, and an octet
/// sequence is its length followed by exactly this.
///
/// # Errors
/// [`WireError::Truncated`] when fewer than `count` bytes remain.
/// The offset reported is where the read began.
pub fn read_bytes(&mut self, count: usize) -> Result<&'a [u8], WireError> {
self.take(count)
}
fn take(&mut self, count: usize) -> Result<&'a [u8], WireError> {
let end = self.at.checked_add(count).ok_or(WireError::Truncated {
at: self.at,
needed: count,
available: self.remaining(),
})?;
let slice = self.bytes.get(self.at..end).ok_or(WireError::Truncated {
at: self.at,
needed: count,
available: self.remaining(),
})?;
self.at = end;
Ok(slice)
}
}
/// An EMHEADER as read.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct EmHeaderRead {
/// The member id.
pub member_id: u32,
/// The length code.
pub length_code: u8,
/// Whether a reader must fail rather than skip this member.
pub must_understand: bool,
/// The NEXTINT, for the codes that carry one. Consumed for code 4
/// and left in place for codes 5 to 7, where it is also the
/// member's own first word.
pub next_int: Option<usize>,
/// How many bytes the member occupies from here.
pub extent: usize,
}
/// A parameter header as read.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PidRead {
/// The parameter id, with the flag bits removed.
pub pid: u32,
/// The value's unpadded length.
pub length: usize,
/// Whether this is the list's end.
pub end: bool,
/// Whether the writer set the must-understand flag. Cyclone sets it
/// on every parameter and Fast DDS on none; both are streams this
/// project reads.
pub must_understand: bool,
/// Whether the writer set `FLAG_IMPL_EXTENSION`.
///
/// XTypes 1.3 §7.4.1.2.2: the parameter is the writing
/// implementation's own, and a user-defined member "will never set
/// the FLAG_IMPL_EXTENSION bit". The flag used to be masked off with
/// the rest, which made a vendor-private parameter indistinguishable
/// from the user member sharing its low fourteen bits — and the
/// vendor's payload was then read into that member with no error.
pub impl_extension: bool,
/// Whether the id is inside the reserved range, which no member has.
///
/// §7.4.1.2.2 reserves 0x3F01 to 0x3FFF. `PID_EXTENDED` and
/// `PID_LIST_END` are handled before this field is ever read; what
/// is left is `PID_IGNORE` and the range above it, and none of them
/// is a member id. A short parameter carrying one used to be matched
/// against a member declared with that number through the *extended*
/// header, where it is a legal id.
pub reserved: bool,
/// `PID_IGNORE`, which §7.4.1.2.2 says every consumer shall ignore.
///
/// Kept apart from [`PidRead::reserved`] because it outranks
/// [`PidRead::must_understand`]: the clause has no exception in it,
/// so a reader that discarded the sample over the flag would refuse
/// a stream the clause tells it to accept.
pub ignore: bool,
/// Which length convention this parameter is checked against.
role: ParameterRole,
/// The stream origin this parameter's block displaced.
saved_origin: usize,
}
impl PidRead {
/// Whether this parameter can be one of the reading type's members.
///
/// False for a vendor-private parameter and for one whose id is in
/// the reserved range. Both are streams a conforming writer may
/// send, and neither is any member of the type being read, so both
/// take the unknown-parameter path — skipped, or the sample
/// discarded if the writer set must-understand. XTypes 1.3
/// §7.4.1.2.2, §7.2.2.4.4.4.6.
#[must_use]
pub const fn is_user_member(&self) -> bool {
!self.impl_extension && !self.reserved && !self.ignore
}
}
/// The bit XTypes 1.3 §7.4.1.2 gives to must-understand.
const PID_MUST_UNDERSTAND: u16 = 0x4000;
/// The low fourteen bits, which are the id itself.
const PID_ID_MASK: u16 = 0x3FFF;
/// Take the byte-order mark off the front and apply what it said.
///
/// The units arrived little-endian, because that is what omniORB writes
/// on this host and what [`WstringUnitOrder::ByteOrderMark`] therefore
/// reads. Three cases, and the third is the one that matters:
///
/// - `FEFF` — the mark is the right way up, the units are as read.
/// - `FFFE` — the mark is byte-swapped, so every unit behind it is too,
/// and each one is swapped back. This is what a conforming big-endian
/// omniORB peer produces and what a reader that ignored the mark
/// would return as different characters, well formed and the right
/// length.
/// - no mark — CORBA 3.3 part 2 §9.3.1.6's rule for a GIOP 1.2
/// `wstring` without one is UTF-16 big-endian, so every unit read
/// little-endian is swapped. That rule is what makes this variant read
/// a JacORB peer as well as an omniORB one.
///
/// The mark is never a character of the returned string in any of the
/// three, which is the half §7.4.1.1.2 cares about.
///
/// The stream's own byte order is not an argument and does not need to
/// be: reading the units little-endian and then obeying the mark is
/// correct in all four combinations of writer host and stream order,
/// because the mark is written in the same order as the content it
/// introduces. That is what a byte-order mark is for.
fn strip_byte_order_mark(units: &mut Vec<u16>) {
match units.first().copied() {
Some(BYTE_ORDER_MARK) => {
units.remove(0);
}
Some(SWAPPED_BYTE_ORDER_MARK) => {
units.remove(0);
for unit in units.iter_mut() {
*unit = unit.swap_bytes();
}
}
_ => {
for unit in units.iter_mut() {
*unit = unit.swap_bytes();
}
}
}
}
/// A length the configured cell cannot have produced.
///
/// [`WireError::WrongFraming`] and not [`WireError::OutOfRange`], for
/// the reason that variant exists: a reader given the wrong dialect and
/// a reader given a corrupt stream both stop, and only one of them is
/// fixed by changing a flag. The error names the cell the length *does*
/// fit and the setting that selects it — telling a user "an odd octet
/// count" and nothing else costs an afternoon to place.
///
/// Sweeps [`WstringLengthPolicy::ALL_CELLS`] and not the measured five:
/// a cell left out of the sweep is a cell this diagnostic can never
/// name, and naming the cell the writer used is the whole point of it.
fn wstring_form_mismatch(policy: WstringLengthPolicy, declared: usize, at: usize) -> WireError {
let mut names = String::new();
let mut first = None;
for other in WstringLengthPolicy::ALL_CELLS {
if other == policy || !other.admits_length(declared) {
continue;
}
if first.is_none() {
first = Some(other.flag());
} else {
names.push_str(" or ");
}
names.push_str(other.as_str());
}
let found = if first.is_none() {
format!("{declared}, which fits no other wstring length policy either")
} else {
format!("{declared}, which fits {names}")
};
WireError::WrongFraming {
at,
expected: format!(
"a wstring length the {} policy can produce",
policy.as_str()
),
found,
fits: first,
}
}