This file is indexed.

/usr/share/tcltk/combat0.8/cdr.tcl is in tcl-combat 0.8.1-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
#
# ======================================================================
# This file is part of Combat/Tcl, a Tcl CORBA Object Request Broker
#
# Please visit the Combat Web site at http://www.fpx.de/Combat/ for
# more information.
#
# Copyright (c) Frank Pilhofer, combat@fpx.de
#
# ======================================================================
#
# CVS Version Tag: $Id: cdr.tcl,v 1.25 2011-01-24 19:50:28 fp Exp $
#
# ----------------------------------------------------------------------
# procs to read and write CDR data
# ----------------------------------------------------------------------
#

namespace eval Combat {
    namespace eval CDR {
	#
	# Simple ReadBuffer for primitive types
	#

	itcl::class ReadBuffer {
	    private variable data
	    private variable index
	    private variable size
	    private variable encaps
	    private variable endofchunk
	    public variable byteorder 	;# 0 big-endian, 1 little-endian
	    public variable cdecoder	;# Char Codeset Decoder
	    public variable wdecoder	;# WChar Codeset Decoder

	    #
	    # byteorder can be 0 for big-endian or 1 for little-endian
	    #

	    constructor {_data} {
		set data $_data
		set index 0
		set size [::string length $data]
		set encaps [list]
		set cdecoder ""
		set wdecoder ""
		set endofchunk -1

		global tcl_platform
		if {$tcl_platform(byteOrder) == "bigEndian"} {
		    set byteorder 0
		} else {
		    set byteorder 1
		}
	    }

	    public method get_data {} {
		return $data
	    }

            public method tell {} {
                return $index
            }

            public method seek {pos} {
                set index $pos
            }

	    public method align {alignment} {
		if {($index % $alignment) != 0} {
		    set increment [expr {$alignment - ($index % $alignment)}]
		    incr index $increment
		}
		check_chunk
	    }

	    public method begin_encaps {} {
		set encaps_length [ulong]
		lappend encaps [list [tell] $byteorder $encaps_length]
		set byteorder [boolean]
		return $encaps_length
	    }

	    public method end_encaps {} {
		set ep [llength $encaps]
		if {$ep <= 0} {
		    error "no encapsulation to end here"
		}
		incr ep -1
		set edata [lindex $encaps $ep]
		incr ep -1
		set encaps [lrange $encaps 0 $ep]

		set byteorder [lindex $edata 1]
		set index [expr {[lindex $edata 0] + [lindex $edata 2]}]
	    }

	    public method begin_chunk {} {
		set encaps_length [ulong]
		set endofchunk [expr {[tell] + $encaps_length}]
	    }

	    public method end_chunk {} {
		if {$endofchunk != -1} {
		    set index $endofchunk
		    set endofchunk -1
		}
	    }

	    public method check_chunk {} {
		if {$endofchunk != -1 && $index >= $endofchunk} {
		    end_chunk
		    begin_chunk
		}
	    }

	    public method char {} {
		check_chunk
		if {$cdecoder != ""} {
		    set res [$cdecoder get_char $this]
		} else {
		    if {[binary scan $data @[set index]a res] != 1} {
			error "[$this info class]::char"
		    }
		    set res [encoding convertfrom iso8859-1 $res]
		    incr index
		}
		return $res
	    }

	    public method chars {length} {
		check_chunk
		if {$cdecoder != ""} {
		    set res [$cdecoder get_chars $this $length]
		} else {
		    if {[binary scan $data @[set index]a[set length] res] != 1} {
			error "[$this info class]::chars"
		    }
		    set res [encoding convertfrom iso8859-1 $res]
		    incr index $length
		    return $res
		}
		return $res
	    }

	    public method wchar {} {
		check_chunk
		if {$wdecoder == ""} {
		    error "no wchar decoder in [$this info class]::wchar"
		}
		return [$wdecoder get_wchar $this]
	    }

	    public method wchars {length} {
		check_chunk
		if {$wdecoder == ""} {
		    error "no wchar decoder in [$this info class]::wchars"
		}
		return [$wdecoder get_wchars $this]
	    }

	    public method octet {} {
		check_chunk
		if {[binary scan $data @[set index]a res] != 1} {
		    error "[$this info class]::octet"
		}
		incr index
		return $res
	    }

	    public method octets {length} {
		check_chunk
		if {[binary scan $data @[set index]a[set length] res] != 1} {
		    error "[$this info class]::octets"
		}
		incr index $length
		return $res
	    }
	    
	    public method short {} {
		align 2

		if {$byteorder == 0} {
		    set code [binary scan $data @[set index]S res]
		} else {
		    set code [binary scan $data @[set index]s res]
		}
		
		if {$code != 1} {
		    error "[$this info class]::short"
		}
		
		incr index 2
		return $res
	    }
	    
	    public method ushort {} {
		return [expr {([short] + 0x10000) % 0x10000}]
	    }
	    
	    public method long {} {
		align 4
		check_chunk
		
		if {$byteorder == 0} {
		    set code [binary scan $data @[set index]I res]
		} else {
		    set code [binary scan $data @[set index]i res]
		}
		
		if {$code != 1} {
		    error "[$this info class]::long"
		}
		
		incr index 4
		return $res
	    }
	    
	    #
	    # In Tcl 8.4, with its 64 bit integer support, we can use
	    # the same trick as above (see ushort) to get an unsigned
	    # value. Before 8.4, we just have to hope that the value
	    # is not misinterpreted. (You might want to throw an error
	    # if the value is negative, but then, some people insist
	    # on sending an unsigned -1 for signaling purposes.)
	    #
	    
	    public method ulong {} {
		set res [long]

		if {[info tclversion] > 8.3} {
		    if {$res < 0} {
			set res [expr {4294967295 + $res}]
			incr res
		    }
		}

		return $res
	    }
	    
	    #
	    # In Tcl 8.4, we can use the 64 bit support. In older versions,
	    # we complain about off-range values.
	    #

	    public method longlong {} {
		align 8
		check_chunk

		if {[info tclversion] > 8.3} {
		    if {$byteorder == 0} {
			set code [binary scan $data @[set index]W res]
		    } else {
			set code [binary scan $data @[set index]w res]
		    }
		
		    if {$code != 1} {
			error "[$this info class]::longlong"
		    }
		} else {
		    if {$byteorder == 0} {
			set high [long]
			set res [long]
		    } else {
			set res [long]
			set high [long]
		    }

		    if {($high != 0 && $high != -1) ||
			($high == 0 && $res < 0) ||
			($high == -1 && $res > 0)} {
			error "[$this info class]::longlong: value out of range"
		    }
		}

		incr index 8
		return $res
	    }
	    
	    public method ulonglong {} {
		return [longlong]
	    }
	    
	    #
	    # groan ... Tcl only handles native floating point values
	    #
	    
	    public method float {} {
		align 4

		if {$byteorder == 0} {
		    set code [binary scan $data @[set index]cccc se1 e2f1 f2 f3]
		} else {
		    set code [binary scan $data @[set index]cccc f3 f2 e2f1 se1]
		}
		
		if {$code != 4} {
		    error "[$this info class]::float"
		}
		
		set se1  [expr {($se1 + 0x100) % 0x100}]
		set e2f1 [expr {($e2f1 + 0x100) % 0x100}]
		set f2   [expr {($f2 + 0x100) % 0x100}]
		set f3   [expr {($f3 + 0x100) % 0x100}]
		
		set sign [expr {$se1 >> 7}]
		set exponent [expr {(($se1 & 0x7f) << 1 | ($e2f1 >> 7))}]
		set f1 [expr {$e2f1 & 0x7f}]

		if {$exponent == 0} {
		    set res 0.0
		} else {
		    set fraction [expr {double($f1)*0.0078125 + \
			    double($f2)*3.0517578125e-05 + \
			    double($f3)*1.19209289550781e-07}]
		
		    set res [expr {($sign ? -1. : 1.) * \
			    pow(2.,double($exponent-127)) * \
			    (1. + $fraction)}]
		}
		
		incr index 4
		return $res
	    }

	    public method double {} {
		align 8
		
		if {$byteorder == 0} {
		    set code [binary scan $data @[set index]cccccccc \
			    se1 e2f1 f2 f3 f4 f5 f6 f7]
		} else {
		    set code [binary scan $data @[set index]cccccccc \
			    f7 f6 f5 f4 f3 f2 e2f1 se1]
		}
		
		if {$code != 8} {
		    error "[$this info class]::double"
		}
		
		set se1  [expr {($se1 + 0x100) % 0x100}]
		set e2f1 [expr {($e2f1 + 0x100) % 0x100}]
		set f2   [expr {($f2 + 0x100) % 0x100}]
		set f3   [expr {($f3 + 0x100) % 0x100}]
		set f4   [expr {($f4 + 0x100) % 0x100}]
		set f5   [expr {($f5 + 0x100) % 0x100}]
		set f6   [expr {($f6 + 0x100) % 0x100}]
		set f7   [expr {($f7 + 0x100) % 0x100}]
		
		set sign [expr {$se1 >> 7}]
		set exponent [expr {(($se1 & 0x7f) << 4 | ($e2f1 >> 4))}]
		set f1 [expr {$e2f1 & 0x0f}]

		if {$exponent == 0} {
		    set res 0.0
		} else {
		    set fraction [expr {double($f1)*0.0625 + \
			    double($f2)*0.000244140625 + \
			    double($f3)*9.5367431640625e-07 + \
			    double($f4)*3.7252902984619141e-09 + \
			    double($f5)*1.4551915228366852e-11 + \
			    double($f6)*5.6843418860808015e-14 + \
			    double($f7)*2.2204460492503131e-16}]
		
		    set res [expr {($sign ? -1. : 1.) * \
			    pow(2.,double($exponent-1023)) * \
			    (1. + $fraction)}]
		}
		
		incr index 8
		return $res
	    }

	    #
	    # I don't think this is getting any more precise ...
	    #

	    public method longdouble {} {
		align 8
		
		if {$byteorder == 0} {
		    set code [binary scan $data @[set index]cccccccccccccccc \
				  se1 e2 f1 f2 f3 f4 f5 f6 f7 f8 f9 f10 f11 f12 f13 f14]
		} else {
		    set code [binary scan $data @[set index]cccccccccccccccc \
				  f14 f13 f12 f11 f10 f9 f8 f7 f6 f5 f4 f3 f2 f1 e2 se1]
		}
		
		if {$code != 16} {
		    error "[$this info class]::longdouble"
		}
		
		set se1  [expr {($se1 + 0x100) % 0x100}]
		set e2   [expr {($e2 + 0x100) % 0x100}]
		set f1   [expr {($f1 + 0x100) % 0x100}]
		set f2   [expr {($f2 + 0x100) % 0x100}]
		set f3   [expr {($f3 + 0x100) % 0x100}]
		set f4   [expr {($f4 + 0x100) % 0x100}]
		set f5   [expr {($f5 + 0x100) % 0x100}]
		set f6   [expr {($f6 + 0x100) % 0x100}]
		set f7   [expr {($f7 + 0x100) % 0x100}]
		set f8   [expr {($f8 + 0x100) % 0x100}]
		set f9   [expr {($f9 + 0x100) % 0x100}]
		set f10  [expr {($f10 + 0x100) % 0x100}]
		set f11  [expr {($f11 + 0x100) % 0x100}]
		set f12  [expr {($f12 + 0x100) % 0x100}]
		set f13  [expr {($f13 + 0x100) % 0x100}]
		set f14  [expr {($f14 + 0x100) % 0x100}]
		
		set sign [expr {$se1 >> 7}]
		set exponent [expr {(($se1 & 0x7f) << 8 | $e2)}]

		if {$exponent == 0} {
		    set res 0.0
		} else {
		    set fraction [expr {double($f1)*0.00390625 + \
			    double($f2)*1.52587890625e-005 + \
			    double($f3)*5.96046447754e-008 + \
			    double($f4)*2.32830643654e-010 + \
			    double($f5)*9.09494701773e-013 + \
			    double($f6)*3.5527136788e-015 + \
			    double($f7)*1.38777878078e-017 + \
			    double($f8)*5.42101086243e-020 + \
			    double($f9)*2.11758236814e-022 + \
			    double($f10)*8.27180612553e-025 + \
			    double($f11)*3.23117426779e-027 + \
			    double($f12)*1.26217744835e-029 + \
			    double($f13)*4.93038065763e-032 + \
			    double($f14)*1.92592994439e-034}]
		
		    set res [expr {($sign ? -1. : 1.) * \
			    pow(2.,double($exponent-16383)) * \
			    (1. + $fraction)}]
		}
		
		incr index 16
		return $res
	    }

	    public method boolean {} {
		check_chunk
		if {[binary scan $data @[set index]c res] != 1} {
		    error "[$this info class]::boolean"
		}
		if {$res != 0 && $res != 1} {
		    error "illegal boolean value $res"
		}
		incr index
		return $res
	    }
	    
	    public method string {} {
		if {$cdecoder != ""} {
		    check_chunk
		    set res [$cdecoder get_string $this]
		} else {
		    #
		    # backwards compatibility using iso8859-1
		    #

		    #
		    # length includes the trailing null, which we don't want
		    #
		
		    set length [ulong]
		    set dlength [expr {$length - 1}]
		    set code [binary scan $data @[set index]a[set dlength] res]
		
		    if {$code != 1} {
			error "[$this info class]::string"
		    }

		    set res [encoding convertfrom iso8859-1 $res]
		    incr index $length
		}
		return $res
	    }

	    public method wstring {} {
		if {$wdecoder == ""} {
		    error "no wchar decoder in [$this info class]::wstring"
		}
		check_chunk
		return [$wdecoder get_wstring $this]
	    }
	}

	#
	# Simple WriteBuffer for primitive types
	#

	itcl::class WriteBuffer {
	    private variable data
	    private variable encaps
	    private variable chunk
	    public variable byteorder 	;# 0 big-endian, 1 little-endian
	    public variable cencoder	;# Char Codeset Encoder
	    public variable wencoder	;# WChar Codeset Encoder

	    constructor {} {
		set data ""
		set encaps [list]
		set cencoder ""
		set wencoder ""

		global tcl_platform
		if {$tcl_platform(byteOrder) == "bigEndian"} {
		    set byteorder 0
		} else {
		    set byteorder 1
		}
	    }

	    public method get_data {} {
		return $data
	    }

	    public method tell {} {
		return [::string length $data]
	    }

	    public method align {alignment} {
		set index [::string length $data]
		if {($index % $alignment) != 0} {
		    set increment [expr {$alignment - ($index % $alignment)}]
		    switch $increment {
			1 { append data f }
			2 { append data fo }
			3 { append data foo }
			4 { append data foob }
			5 { append data fooba }
			6 { append data foobar }
			7 { append data foobar! }
			default {
			    error "[$this info class]::align"
			}
		    }
		}
	    }

	    public method begin_encaps {{nbo {}}} {
		align 4
		ulong 0				;# dummy length
		lappend encaps [list [tell] $byteorder]
		if {$nbo == ""} {
		    boolean $byteorder
		} else {
		    set byteorder $nbo
		    boolean $byteorder
		}
	    }

	    public method end_encaps {} {
		set ep [llength $encaps]
		if {$ep <= 0} {
		    error "no encapsulation to end here"
		}
		incr ep -1
		set edata [lindex $encaps $ep]
		incr ep -1
		set encaps [lrange $encaps 0 $ep]

		set encaps_begin [lindex $edata 0]
		set byteorder [lindex $edata 1]
		set encaps_length [expr {[tell] - $encaps_begin}]

		if {$byteorder == 0} {
		    set data [::string replace $data \
			    [expr {$encaps_begin - 4}] \
			    [expr {$encaps_begin - 1}] \
			    [binary format I $encaps_length]]
		} else {
		    set data [::string replace $data \
			    [expr {$encaps_begin - 4}] \
			    [expr {$encaps_begin - 1}] \
			    [binary format i $encaps_length]]
		}
	    }

	    public method begin_chunk {} {
		align 4
		ulong 0				;# dummy length
		set chunk [tell]
	    }

	    public method end_chunk {} {
		set encaps_length [expr {[tell] - $chunk}]

		if {$byteorder == 0} {
		    set data [::string replace $data \
			    [expr {$chunk - 4}] [expr {$chunk - 1}] \
			    [binary format I $encaps_length]]
		} else {
		    set data [::string replace $data \
			    [expr {$chunk - 4}] [expr {$chunk - 1}] \
			    [binary format i $encaps_length]]
		}
	    }

	    public method char {val} {
		if {$cencoder != ""} {
		    $cencoder put_char $this $val
		} else {
		    set ival [encoding convertto iso8859-1 $val]
		    if {[::string length $ival] != 1} {
			error "[$this info class]::char"
		    }
		    append data $ival
		}
	    }

	    public method chars {val} {
		if {$cencoder != ""} {
		    $cencoder put_chars $this $val
		} else {
		    append data [encoding convertto iso8859-1 $val]
		}
	    }

	    public method wchar {val} {
		if {$wencoder == ""} {
		    error "no wchar encoder in [$this info class]::wchar"
		}
		$wencoder put_wchar $this $val
	    }

	    public method wchars {val} {
		if {$wencoder == ""} {
		    error "no wchar encoder in [$this info class]::wchars"
		}
		$wencoder put_wchars $this $val
	    }

	    public method octet {val} {
		if {[::string length $val] != 1} {
		    error "[$this info class]::octet"
		}
		append data $val
	    }

	    public method octets {val} {
		append data $val
	    }

	    public method short {val} {
		align 2

		if {$byteorder == 0} {
		    set bytes [binary format S $val]
		} else {
		    set bytes [binary format s $val]
		}

		append data $bytes
	    }

	    public method ushort {val} {
		short $val
	    }

	    public method long {val} {
		align 4

		if {$byteorder == 0} {
		    set bytes [binary format I $val]
		} else {
		    set bytes [binary format i $val]
		}

		append data $bytes
	    }

	    public method ulong {val} {
		long $val
	    }

	    public method longlong {val} {
		align 8

		if {[info tclversion] > 8.3} {
		    if {$byteorder == 0} {
			set bytes [binary format W $val]
		    } else {
			set bytes [binary format w $val]
		    }
		    append data $bytes
		} else {
		    if {$byteorder == 0} {
			if {$val >= 0} {
			    long 0
			    long $val
			} else {
			    long -1
			    long $val
			}
		    } else {
			if {$val >= 0} {
			    long $val
			    long 0
			} else {
			    long $val
			    long -1
			}
		    }
		}
	    }

	    #
	    # taking our luck with encoding -1 ...
	    #

	    public method ulonglong {val} {
		longlong $val
	    }

	    #
	    # groan ... Tcl only handles native floating point values
	    #
	    
	    public method float {val} {
		align 4

		if {$val > 0} {
		    set sign 0
		} else {
		    set sign 1
		    set val [expr {-1. * $val}]
		}

		#
		# If the following math fails, then it's because of the
		# logarithm. That means that val is indistinguishable from
		# zero
		#

		if {[catch {
		    set exponent [expr {int(floor(log($val)/0.69314718055994529))+127}]
		    set fraction [expr {($val/pow(2.,double($exponent-127)))-1.}]
		}]} {
		    set exponent 0
		    set fraction 0.0
		} else {
		    #
		    # round off too-small values to zero, throw error for
		    # too-large values
		    #

		    if {$exponent < 0} {
			set exponent 0
			set fraction 0.0
		    } elseif {$exponent > 255} {
			error "value $val outside legal range for a float"
		    }
		}

		set fraction [expr {$fraction * 128.}]
		set f1f      [expr {floor($fraction)}]
		set fraction [expr {($fraction - $f1f) * 256.}]
		set f2f      [expr {floor($fraction)}]
		set fraction [expr {($fraction - $f2f) * 256.}]
		set f3f      [expr {floor($fraction)}]

		set f1       [expr {int($f1f)}]
		set f2       [expr {int($f2f)}]
		set f3       [expr {int($f3f)}]

		set se1      [expr {($sign ? 128 : 0) | ($exponent >> 1)}]
		set e2f1     [expr {(($exponent & 0x1) << 7) | $f1}]

		if {$byteorder == 0} {
		    set bytes [binary format cccc $se1 $e2f1 $f2 $f3]
		} else {
		    set bytes [binary format cccc $f3 $f2 $e2f1 $se1]
		}

		append data $bytes
	    }

	    public method double {val} {
		align 8

		if {$val > 0} {
		    set sign 0
		} else {
		    set sign 1
		    set val [expr {-1. * $val}]
		}

		#
		# If the following math fails, then it's because of the
		# logarithm. That means that val is indistinguishable from
		# zero
		#

		if {[catch {
		    set exponent [expr {int(floor(log($val)/0.69314718055994529))+1023}]
		    set fraction [expr {($val/pow(2.,double($exponent-1023)))-1.}]
		}]} {
		    set exponent 0
		    set fraction 0.0
		} else {
		    #
		    # round off too-small values to zero, throw error for
		    # too-large values
		    #

		    if {$exponent < 0} {
			set exponent 0
			set fraction 0.0
		    } elseif {$exponent > 2047} {
			error "value $val outside legal range for a double"
		    }
		}

		set fraction [expr {$fraction * 16.}]
		set f1f      [expr {floor($fraction)}]
		set fraction [expr {($fraction - $f1f) * 256.}]
		set f2f      [expr {floor($fraction)}]
		set fraction [expr {($fraction - $f2f) * 256.}]
		set f3f      [expr {floor($fraction)}]
		set fraction [expr {($fraction - $f3f) * 256.}]
		set f4f      [expr {floor($fraction)}]
		set fraction [expr {($fraction - $f4f) * 256.}]
		set f5f      [expr {floor($fraction)}]
		set fraction [expr {($fraction - $f5f) * 256.}]
		set f6f      [expr {floor($fraction)}]
		set fraction [expr {($fraction - $f6f) * 256.}]
		set f7f      [expr {floor($fraction)}]

		set f1       [expr {int($f1f)}]
		set f2       [expr {int($f2f)}]
		set f3       [expr {int($f3f)}]
		set f4       [expr {int($f4f)}]
		set f5       [expr {int($f5f)}]
		set f6       [expr {int($f6f)}]
		set f7       [expr {int($f7f)}]

		set se1      [expr {($sign ? 128 : 0) | ($exponent >> 4)}]
		set e2f1     [expr {(($exponent & 0x0f) << 4) | $f1}]

		if {$byteorder == 0} {
		    set bytes [binary format cccccccc \
			    $se1 $e2f1 $f2 $f3 $f4 $f5 $f6 $f7]
		} else {
		    set bytes [binary format cccccccc \
			    $f7 $f6 $f5 $f4 $f3 $f2 $e2f1 $se1]
		}

		append data $bytes
	    }

	    public method longdouble {val} {
		align 8

		if {$val > 0} {
		    set sign 0
		} else {
		    set sign 1
		    set val [expr {-1. * $val}]
		}

		#
		# If the following math fails, then it's because of the
		# logarithm. That means that val is indistinguishable from
		# zero
		#

		if {[catch {
		    set exponent [expr {int(floor(log($val)/0.69314718055994529))+16383}]
		    set fraction [expr {($val/pow(2.,double($exponent-16383)))-1.}]
		}]} {
		    set exponent 0
		    set fraction 0.0
		} else {
		    #
		    # round off too-small values to zero, throw error for
		    # too-large values
		    #

		    if {$exponent < 0} {
			set exponent 0
			set fraction 0.0
		    } elseif {$exponent > 32767} {
			error "value $val outside legal range for a longdouble"
		    }
		}

		set fraction [expr {$fraction * 256.}]
		set f1f      [expr {floor($fraction)}]
		set fraction [expr {($fraction - $f1f) * 256.}]
		set f2f      [expr {floor($fraction)}]
		set fraction [expr {($fraction - $f2f) * 256.}]
		set f3f      [expr {floor($fraction)}]
		set fraction [expr {($fraction - $f3f) * 256.}]
		set f4f      [expr {floor($fraction)}]
		set fraction [expr {($fraction - $f4f) * 256.}]
		set f5f      [expr {floor($fraction)}]
		set fraction [expr {($fraction - $f5f) * 256.}]
		set f6f      [expr {floor($fraction)}]
		set fraction [expr {($fraction - $f6f) * 256.}]
		set f7f      [expr {floor($fraction)}]
		set fraction [expr {($fraction - $f7f) * 256.}]
		set f8f      [expr {floor($fraction)}]
		set fraction [expr {($fraction - $f8f) * 256.}]
		set f9f      [expr {floor($fraction)}]
		set fraction [expr {($fraction - $f9f) * 256.}]
		set f10f     [expr {floor($fraction)}]
		set fraction [expr {($fraction - $f10f) * 256.}]
		set f11f     [expr {floor($fraction)}]
		set fraction [expr {($fraction - $f11f) * 256.}]
		set f12f     [expr {floor($fraction)}]
		set fraction [expr {($fraction - $f12f) * 256.}]
		set f13f     [expr {floor($fraction)}]
		set fraction [expr {($fraction - $f13f) * 256.}]
		set f14f     [expr {floor($fraction)}]

		set f1       [expr {int($f1f)}]
		set f2       [expr {int($f2f)}]
		set f3       [expr {int($f3f)}]
		set f4       [expr {int($f4f)}]
		set f5       [expr {int($f5f)}]
		set f6       [expr {int($f6f)}]
		set f7       [expr {int($f7f)}]
		set f8       [expr {int($f8f)}]
		set f9       [expr {int($f9f)}]
		set f10      [expr {int($f10f)}]
		set f11      [expr {int($f11f)}]
		set f12      [expr {int($f12f)}]
		set f13      [expr {int($f13f)}]
		set f14      [expr {int($f14f)}]

		set se1      [expr {($sign ? 128 : 0) | ($exponent >> 8)}]
		set e2       [expr {$exponent & 0xff}]

		if {$byteorder == 0} {
		    set bytes [binary format cccccccccccccccc \
				   $se1 $e2 $f1 $f2 $f3 $f4 $f5 $f6 $f7 $f8 $f9 $f10 $f11 $f12 $f13 $f14]
		} else {
		    set bytes [binary format cccccccccccccccc \
				   $f14 $f13 $f12 $f11 $f10 $f9 $f8 $f7 $f6 $f5 $f4 $f3 $f2 $f1 $e2 $se1]
		}

		append data $bytes
	    }

	    public method boolean {val} {
		if {[catch {
		    if {$val} {
			append data \1
		    } else {
			append data \0
		    }
		}]} {
		    error "illegal boolean value $val"
		}
	    }

	    public method string {val} {
		if {$cencoder != ""} {
		    $cencoder put_string $this $val
		} else {
		    #
		    # backwards compatibility using iso8859-1
		    #

		    set ival [encoding convertto iso8859-1 $val]
		    set length [::string length $ival]
		    incr length ;# terminating null
		    ulong $length
		    append data $ival \0
		}
	    }

	    public method wstring {val} {
		if {$wencoder == ""} {
		    error "no wchar encoder in [$this info class]::wstring"
		}
		$wencoder put_wstring $this $val
	    }
	}

	#
	# ------------------------------------------------------------
	# CDR Decoder and Encoder for complex types
	# ------------------------------------------------------------
	#

	itcl::class Decoder {
	    private variable buffer
	    private variable tcrecursion
	    private variable marshalrecursion
	    private variable valuerecursion
	    private variable nestinglevel
	    private variable chunking

	    constructor {_data} {
		set buffer [namespace current]::[::Combat::CDR::ReadBuffer \#auto $_data]
		set nestinglevel 0
		set chunking 0
	    }

	    destructor {
		itcl::delete object $buffer
	    }

	    public method get_buffer {} {
		return $buffer
	    }

	    public method get_data {} {
		return [$buffer get_data]
	    }


	    private method get_indirect_string {} {
		set pos [$buffer tell]
		set ref [$buffer long]
		if {$ref != -1} {
		    $buffer seek $pos
		    return [$buffer string]
		}
		set oldpos [$buffer tell]
		set offset [$buffer long]
		$buffer seek [expr {$oldpos + $offset}]
		set res [$buffer string]
		$buffer seek $oldpos
		$buffer long
		return $res
	    }

	    private method get_indirect_string_seq {} {
		set pos [$buffer tell]
		set ref [$buffer long]
		set res [list]
		if {$ref != -1} {
		    for {set i 0} {$i < $ref} {incr i} {
			lappend res [get_indirect_string]
		    }
		    return $res
		}
		set oldpos [$buffer tell]
		set offset [$buffer long]
		$buffer seek [expr {$oldpos + $offset}]
		set ref [$buffer ulong]
		for {set i 0} {$i < $ref} {incr i} {
		    lappend res [get_indirect_string]
		}
		$buffer seek $oldpos
		$buffer long
		return $res
	    }

	    private method TypeCode {} {
		set kind [$buffer long]

		#
		# Handle recursion and indirection
		#
		# When entering a potentially recursive typecode like
		# a struct or valuetype, we store the tc's original
		# position in the tcrecursion array. Therefore, if we
		# find an indirection to an entry in that array, the
		# typecode is recursive. Otherwise, it's just an
		# indirection - we then just skip to the old position
		# and re-scan the entire tc again. (Otherwise we'd
		# have to cache all typecodes in the stream.)
		#

		if {$kind == -1} {
		    set curpos [$buffer tell]
		    set offset [$buffer long]
		    set origpos [expr {$curpos + $offset}]
		    if {[info exists tcrecursion($origpos)]} {
			return [list recursive $tcrecursion($origpos)]
		    }
		    $buffer seek $origpos
		    set tc [TypeCode]
		    $buffer seek [expr {$curpos + 4}]
		    return $tc
		}

		set tckind [lindex {null void short long {unsigned short} \
			{unsigned long} float double boolean char octet \
			any TypeCode Principal Object struct union enum \
			string sequence array alias exception {long long} \
			{unsigned long long} {long double} wchar wstring \
			fixed valuetype valuebox native abstractinterface} \
			$kind]

		if {$tckind == ""} {
		    error "unknown tckind $kind"
		}

		#
		# done in case of "empty" tckinds
		#

		if {($kind >= 0 && $kind <= 13) || \
			($kind >= 23 && $kind <= 26)} {
		    return $tckind
		}

		#
		# handle "simple" tckinds
		#

		switch -- $tckind {
		    string -
		    wstring {
			set bound [$buffer ulong]
			if {$bound == 0} {
			    return $tckind
			}
			return [list $tckind $bound]
		    }
		    fixed {
			set digits [$buffer ushort]
			set scale [$buffer ushort]
			return [list $tckind $digits $scale]
		    }
		}

		#
		# Complex types with encapsulation
		#

		set tckindpos [expr {[$buffer tell] - 4}]
		$buffer begin_encaps

		switch -- $tckind {
		    Object {
			set repoid [$buffer string]
			set name [$buffer string]
			set res [list Object $repoid]
		    }
		    struct -
		    exception {
			set repoid [$buffer string]
			set tcrecursion($tckindpos) $repoid
			set name [$buffer string]
			set count [$buffer ulong]
			set members [list]
			for {set i 0} {$i < $count} {incr i} {
			    set member_name [$buffer string]
			    set member_type [Demarshal TypeCode]
			    lappend members $member_name $member_type
			}
			unset tcrecursion($tckindpos)
			set res [list $tckind $repoid $members]
		    }
		    union {
			set repoid [$buffer string]
			set tcrecursion($tckindpos) $repoid
			set name [$buffer string]
			set disctype [Demarshal TypeCode]
			set default [$buffer long]
			set count [$buffer ulong]
			set members [list]
			for {set i 0} {$i < $count} {incr i} {
			    set member_label [Demarshal $disctype]
			    set member_name [$buffer string]
			    set member_type [Demarshal TypeCode]
			    if {$i == $default} {
				lappend members "(default)" $member_type
			    } else {
				lappend members $member_label $member_type
			    }
			}
			unset tcrecursion($tckindpos)
			set res [list union $repoid $disctype $members]
		    }
		    enum {
			set repoid [$buffer string]
			set name [$buffer string]
			set count [$buffer ulong]
			set members [list]
			for {set i 0} {$i < $count} {incr i} {
			    set member_name [$buffer string]
			    lappend members $member_name
			}
			set res [list enum $members]
		    }
		    sequence {
			set element_type [Demarshal TypeCode]
			set bound [$buffer ulong]
			if {$bound == 0} {
			    set res [list sequence $element_type]
			} else {
			    set res [list sequence $element_type $bound]
			}
		    }
		    array {
			set element_type [Demarshal TypeCode]
			set length [$buffer ulong]
			set res [list array $element_type $length]
		    }
		    alias {
			set repoid [$buffer string]
			set name [$buffer string]
			set original_type [Demarshal TypeCode]
			set res $original_type
		    }
		    valuetype {
			set repoid [$buffer string]
			set tcrecursion($tckindpos) $repoid
			set name [$buffer string]

			switch -- [$buffer short] {
			    0 {
				set modifier ""
			    }
			    1 {
				set modifier "custom"
			    }
			    2 {
				set modifier "abstract"
			    }
			    3 {
				set modifier "truncatable"
			    }
			    default {
				error "illegal valuetype modifier"
			    }
			}

			set basetc [Demarshal TypeCode]
			
			if {$basetc == "null"} {
			    set basetc 0
			}
			
			set count [$buffer ulong]
			set members [list]
			for {set i 0} {$i < $count} {incr i} {
			    set member_name [$buffer string]
			    set member_type [Demarshal TypeCode]
			    switch -- [$buffer short] {
				0 {
				    set member_visi "private"
				}
				1 {
				    set member_visi "public"
				}
				default {
				    error "illegal member visibility"
				}
			    }
			    lappend members $member_visi $member_name \
				    $member_type
			}

			unset tcrecursion($tckindpos)
			set res [list valuetype $repoid $members \
				$basetc $modifier]
		    }
		    valuebox {
			set repoid [$buffer string]
			set name [$buffer string]
			set original_type [Demarshal TypeCode]
			set res [list valuebox $repoid $original_type]
		    }
		    native {
			set repoid [$buffer string]
			set name [$buffer string]
			set res [list native $repoid]
		    }
		    abstractinterface {
			set repoid [$buffer string]
			set name [$buffer string]
			set res [list abstractinterface $repoid]
		    }
		    default {
			error "unsupported type $tckind ($kind)"
		    }
		}

		$buffer end_encaps
		return $res
	    }

	    private method any {} {
		set type [Demarshal TypeCode]
		set value [Demarshal $type]
		return [list $type $value]
	    }

	    private method bstring {tc} {
		set length [lindex $tc 1]
		set got [$buffer string]

		if {$length > 0 && [string length $got] > $length} {
		    error "Combat::CDR::Decoder: string exceeds bound"
		}

		return $got
	    }

	    private method bwstring {tc} {
		set length [lindex $tc 1]
		set got [$buffer wstring]

		if {$length > 0 && [string length $got] > $length} {
		    error "Combat::CDR::Decoder: wstring exceeds bound"
		}

		return $got
	    }

	    private method struct {tc} {
		set repoid [lindex $tc 1]
		set marshalrecursion($repoid) $tc
		set result [list]
		foreach {name type} [lindex $tc 2] {
		    set value [Demarshal $type]
		    lappend result $name $value
		}
		catch {unset marshalrecursion($repoid)}
		return $result
	    }

	    private method union {tc} {
		set repoid [lindex $tc 1]
		set marshalrecursion($repoid) $tc
		set default ""
		set dtype [lindex $tc 2]
		set disc [Demarshal $dtype]

		foreach {dval type} [lindex $tc 3] {
		    if {$dval == "(default)"} {
			set default $type
		    } elseif {$dval == $disc} {
			set value [Demarshal $type]
			catch {unset marshalrecursion($repoid)}
			return [list $disc $value]
		    }
		}
		if {$default != ""} {
		    set value [Demarshal $default]
		    catch {unset marshalrecursion($repoid)}
		    return [list $disc $value]
		}
		catch {unset marshalrecursion($repoid)}
		return [list $disc]
	    }

	    private method exception {tc} {
		set repoid [$buffer string]
		if {$repoid != [lindex $tc 1]} {
		    error "Combat::CDR::Decoder: wrong exception tc"
		}
		set result [list]
		foreach {name type} [lindex $tc 2] {
		    set value [Demarshal $type]
		    lappend result $name $value
		}
		return [list $repoid $result]
	    }

	    private method sequence {tc} {
		set type [lindex $tc 1]
		set length [$buffer ulong]
		if {[llength $tc] == 3} {
		    set max [lindex $tc 2]
		    if {$max > 0 && $length > $max} {
			error "Combat::CDR::Decoder: sequence exceeds bound"
		    }
		}
		if {$type == "octet"} {
		    return [$buffer octets $length]
		} elseif {$type == "char"} {
		    return [$buffer chars $length]
		} elseif {$type == "wchar"} {
		    return [$buffer wchars $length]
		}
		set res [list]
		for {set i 0} {$i < $length} {incr i} {
		    lappend res [Demarshal $type]
		}
		return $res
	    }

	    private method Array {tc} {
		set type [lindex $tc 1]
		set length [lindex $tc 2]
		if {$type == "octet"} {
		    return [$buffer octets $length]
		} elseif {$type == "char"} {
		    return [$buffer chars $length]
		} elseif {$type == "wchar"} {
		    return [$buffer wchars $length]
		}
		set res [list]
		for {set i 0} {$i < $length} {incr i} {
		    lappend res [Demarshal $type]
		}
		return $res
	    }

	    private method enum {tc} {
		set value [$buffer ulong]
		return [lindex [lindex $tc 1] $value]
	    }

	    private method Object {tc} {
		set ior [::Combat::IOP::DemarshalIOR $buffer]
		if {[llength [$ior cget -profiles]] == 0 && \
			[$ior cget -type_id] == ""} {
		    itcl::delete object $ior
		    return 0
		}

		#
		# Use the object references's own Repository Id (which might
		# be different from the one in $tc), unless we don't have
		# info about that type
		#

		set marshalid [lindex $tc 1]
		set repoid [$ior cget -type_id]

		if {$repoid == "" || \
			(![::Combat::SimpleTypeRepository::HaveTypeInfoForId $repoid] && \
			[::Combat::SimpleTypeRepository::HaveTypeInfoForId $marshalid])} {
		    set repoid $marshalid
		}

		set obj [namespace current]::[::Combat::CORBA::Object \#auto]
		$obj configure -ior $ior
		$obj configure -type_id $repoid
		return [::Combat::CORBA::ORB::MakeObjProc $obj]
	    }

	    private method ValueType {tc} {
		set repoid [lindex $tc 1]
		$buffer align 4
		set valuepos [$buffer tell]
		set valuetag [$buffer long]

		if {$valuetag == 0} {
		    return 0
		}

		if {[lindex $tc 4] == "custom"} {
		    error "cannot demarshal custom valuetype"
		}

		if {$valuetag == -1} {
		    set markerpos [$buffer tell]
		    set offset [$buffer long]
		    set origpos [expr {$markerpos + $offset}]
		    if {![info exists valuerecursion($origpos)]} {
			error "illegal or recursive value type indirection"
		    }
		    return $valuerecursion($origpos)
		}

		if {[expr {$valuetag & 8}] == 8 || $chunking} {
		    set chunked 1
		} else {
		    set chunked 0
		}

		if {[expr {$valuetag & 1}] == 1} {
		    set codebase [get_indirect_string]
		}

		switch [expr {$valuetag & 6}] {
		    0 {
			set repoids [list $repoid]
		    }
		    2 {
			set repoids [list [get_indirect_string]]
		    }
		    6 {
			set repoids [get_indirect_string_seq]
		    }
		    default {
			error "oops"
		    }
		}

		set marshaltc ""

		foreach id $repoids {
		    if {$id == $repoid} {
			set marshaltc $tc
			break
		    }
		    if {![catch {::corba::type of $id} testtc]} {
			set marshaltc $testtc
			break
		    }
		}

		if {$marshaltc == ""} {
		    error "oops"
		}

		set itc $marshaltc
		set tcs [list]

		while {$itc != 0} {
		    set tcs [linsert $tcs 0 $itc]
		    set itc [lindex $itc 3]
		}

		set repoid [lindex $marshaltc 1]
		set marshalrecursion($repoid) $marshaltc
		
		if {$chunked} {
		    set chunking 1
		    incr nestinglevel
		    $buffer begin_chunk
		}

		set result [list]

		foreach mtc $tcs {
		    foreach {visi name type} [lindex $mtc 2] {
			set value [Demarshal $type]
			lappend result $name $value
		    }
		}

		lappend result _tc_ $marshaltc

		if {$chunked} {
		    $buffer end_chunk
		    set level [expr {$nestinglevel + 1}]

		    while {$level > $nestinglevel} {
			set pos [$buffer tell]
			set tag [$buffer long]

			if {$tag > 0 && $tag < 2147483392} {
			    $buffer seek $pos
			    $buffer begin_chunk
			    $buffer end_chunk
			} elseif {$tag < 0} {
			    set level [expr {-$tag}]
			} else {
			    $buffer seek $pos
			    ValueType [list valuetype \
				    IDL:omg.org/CORBA/ValueBase:1.0 \
				    [list] "" ""]
			}
		    }
		    
		    if {$level < $nestinglevel} {
			$buffer seek $pos
		    }

		    if {[incr nestinglevel -1] == 0} {
			set chunking 0
		    }
		}

		set valuerecursion($valuepos) $result
		catch {unset marshalrecursion($repoid)}
		return $result
	    }

	    private method ValueBox {tc} {
		set vtc [list valuetype [lindex $tc 1] \
			[list public foo [lindex $tc 2]] 0 ""]
		
		set vt [ValueType $vtc]
		if {$vt == "0"} {
		    return $vt
		}
		foreach {name value} $vt {
		    if {$name == "foo"} {
			set res $value
			break
		    }
		}
		return $res
	    }

	    private method AbstractInterface {tc} {
		set disc [$buffer boolean]
		if {$disc} {
		    return [Object [list Object IDL:omg.org/CORBA/Object:1.0]]
		} else {
		    return [ValueType [list valuetype \
			    IDL:omg.org/CORBA/ValueBase:1.0 [list] "" ""]]
		}
	    }

	    private method recursive {tc} {
		set repoid [lindex $tc 1]
		if {![info exists marshalrecursion($repoid)]} {
		    error "illegal marshalling recursion for $repoid"
		}
		set savetc $marshalrecursion($repoid)
 		set res [Demarshal $marshalrecursion($repoid)] 
		set marshalrecursion($repoid) $savetc
		return $res
	    }

	    public method Demarshal {tc} {
		#
		# handle primitive types
		#
		switch $tc {
		    null                 { return }
		    void                 { return }
		    boolean              { return [$buffer boolean] }
		    short                { return [$buffer short] }
		    long                 { return [$buffer long] }
		    {unsigned short}     { return [$buffer ushort] }
		    {unsigned long}      { return [$buffer ulong] }
		    {long long}          { return [$buffer longlong] }
		    {unsigned long long} { return [$buffer ulonglong] }
		    float                { return [$buffer float] }
		    double               { return [$buffer double] }
		    {long double}        { return [$buffer longdouble] }
		    char                 { return [$buffer char] }
		    octet                { return [$buffer octet] }
		    wchar                { return [$buffer wchar] }
		    string               { return [$buffer string] }
		    wstring              { return [$buffer wstring] }
		    any                  { return [any] }
		    TypeCode             { return [TypeCode] }
		}

		#
		# constructed types
		#

		set type [lindex $tc 0]

		switch $type {
		    string               { return [bstring $tc] }
		    wstring              { return [bwstring $tc] }
		    struct               { return [struct $tc] }
		    union                { return [union $tc] }
		    exception            { return [exception $tc] }
		    sequence             { return [sequence $tc] }
		    array                { return [Array $tc] }
		    enum                 { return [enum $tc] }
		    Object               { return [Object $tc] }
		    valuetype            { return [ValueType $tc] }
		    valuebox             { return [ValueBox $tc] }
		    abstractinterface    { return [AbstractInterface $tc] }
		    recursive            { return [recursive $tc] }
		}

		error "unknown type: $tc"
	    }
	}

	itcl::class Encoder {
	    private variable buffer
	    private variable tcrecursion
	    private variable marshalrecursion
	    private variable nestinglevel
	    private variable chunking

	    constructor {} {
		set buffer [namespace current]::[::Combat::CDR::WriteBuffer \#auto]
		set nestinglevel 0
		set chunking 0
	    }

	    destructor {
		itcl::delete object $buffer
	    }

	    public method get_data {} {
		return [$buffer get_data]
	    }

	    public method get_buffer {} {
		return $buffer
	    }

	    private method TypeCode {value} {
		set tckind [lindex $value 0]
		if {$value == "unsigned short" || \
			$value == "unsigned long" || \
			$value == "long long" || \
			$value == "unsigned long long" || \
			$value == "long double"} {
		    set tckind $value
		}

		#
		# Handle recursion
		#

		if {$tckind == "recursive"} {
		    set repoid [lindex $value 1]
		    if {![info exists tcrecursion($repoid)]} {
			error "illegal recursion for $repoid"
		    }
		    $buffer long -1
		    set offset [expr {$tcrecursion($repoid) - [$buffer tell]}]
		    $buffer long $offset
		    return
		}


		set kind [lsearch {null void short long {unsigned short} \
			{unsigned long} float double boolean char octet \
			any TypeCode Principal Object struct union enum \
			string sequence array alias exception {long long} \
			{unsigned long long} {long double} wchar wstring \
			fixed valuetype valuebox native abstractinterface} \
			$tckind]

		if {$kind == -1} {
		    error "unknown tckind $tckind"
		}

		#
		# done in case of "empty" tckinds
		#

		if {($kind >= 0 && $kind <= 13) || \
			($kind >= 23 && $kind <= 26)} {
		    $buffer ulong $kind
		    return
		}

		#
		# handle "simple" tckinds
		#

		switch -- $tckind {
		    string -
		    wstring {
			$buffer ulong $kind
			if {[llength $value] == 2} {
			    set bound [lindex $value 1]
			    $buffer ulong $bound
			} else {
			    $buffer ulong 0
			}
			return
		    }
		    fixed {
			$buffer ulong $kind
			$buffer ulong [lindex $value 1]
			$buffer ulong [lindex $value 2]
			return
		    }
		}

		#
		# Complex types with encapsulation
		#

		$buffer align 4
		set tckindpos [$buffer tell]
		$buffer ulong $kind
		$buffer begin_encaps

		switch -- $tckind {
		    Object {
			$buffer string [lindex $value 1]
			$buffer string ""
		    }
		    struct -
		    exception {
			set repoid [lindex $value 1]
			set tcrecursion($repoid) $tckindpos
			$buffer string $repoid
			$buffer string ""
			set members [lindex $value 2]
			$buffer ulong [expr {[llength $members] / 2}]
			foreach {member_name member_type} $members {
			    $buffer string $member_name
			    Marshal TypeCode $member_type
			}
			unset tcrecursion($repoid)
		    }
		    union {
			set repoid [lindex $value 1]
			set tcrecursion($repoid) $tckindpos
			set disctype [lindex $value 2]
			set members [lindex $value 3]
			set default -1
			set count 0
			foreach {member_label member_type} $members {
			    if {$member_label == "(default)"} {
				set default $count
				break
			    }
			    incr count
			}
			$buffer string $repoid
			$buffer string ""
			Marshal TypeCode $disctype
			$buffer long $default
			$buffer ulong [expr {[llength $members] / 2}]
			set count 0
			foreach {member_label member_type} $members {
			    if {$count == $default} {
				switch -- $disctype {
				    boolean {
					$buffer boolean 0
				    }
				    short {
					$buffer short 0
				    }
				    {unsigned short} {
					$buffer ushort 0
				    }
				    long {
					$buffer long 0
				    }
				    {unsigned long} {
					$buffer ulong 0
				    }
				    default { # should be enum
					$buffer ulong 0
				    }
				}
			    } else {
				Marshal $disctype $member_label
			    }
			    $buffer string ""
			    Marshal TypeCode $member_type
			    incr count
			}
			unset tcrecursion($repoid)
		    }
		    enum {
			$buffer string ""
			$buffer string ""
			$buffer ulong [llength [lindex $value 1]]
			foreach member [lindex $value 1] {
			    $buffer string $member
			}
		    }
		    sequence {
			Marshal TypeCode [lindex $value 1]
			if {[llength $value] == 3} {
			    $buffer ulong [lindex $value 2]
			} else {
			    $buffer ulong 0
			}
		    }
		    array {
			Marshal TypeCode [lindex $value 1]
			$buffer ulong [lindex $value 2]
		    }
		    valuetype {
			set repoid [lindex $value 1]
			set tcrecursion($repoid) $tckindpos
			$buffer string $repoid
			$buffer string ""
			switch -- [lindex $value 4] {
			    "" {
				$buffer short 0
			    }
			    custom {
				$buffer short 1
			    }
			    abstract {
				$buffer short 2
			    }
			    truncatable {
				$buffer short 3
			    }
			    default {
				error "illegal modifier for valuetype tc"
			    }
			}
			if {[lindex $value 3] == 0} {
			    Marshal TypeCode null
			} else {
			    Marshal TypeCode [lindex $value 3]
			}
			set members [lindex $value 2]
			$buffer ulong [expr {[llength $members] / 3}]
			foreach {visi member_name member_type} $members {
			    $buffer string $member_name
			    Marshal TypeCode $member_type
			    switch -- $visi {
				private {
				    $buffer short 0
				}
				public {
				    $buffer short 1
				}
				default {
				    error "illegal visibility $visi"
				}
			    }
			}
			unset tcrecursion($repoid)
		    }
		    valuebox {
			$buffer string [lindex $value 1]
			$buffer string ""
			Marshal TypeCode [lindex $value 2]
		    }
		    native {
			$buffer string [lindex $value 1]
			$buffer string ""
		    }
		    abstractinterface {
			$buffer string [lindex $value 1]
			$buffer string ""
		    }
		    alias {
			error "oops, there are no such things as aliases"
		    }
		    default {
			error "unsupported type $tckind"
		    }
		}

		$buffer end_encaps
	    }

	    private method any {value} {
		Marshal TypeCode [lindex $value 0]
		Marshal [lindex $value 0] [lindex $value 1]
	    }

	    private method bstring {tc value} {
		set length [lindex $tc 1]
		if {$length > 0 && [string length $value] > $length} {
		    error "Combat::CDR::Encoder: string exceeds bound"
		}
		$buffer string $value
	    }

	    private method bwstring {tc value} {
		set length [lindex $tc 1]
		if {$length > 0 && [string length $value] > $length} {
		    error "Combat::CDR::Encoder: wstring exceeds bound"
		}
		$buffer wstring $value
	    }

	    private method struct {tc value} {
		set repoid [lindex $tc 1]
		set marshalrecursion($repoid) $tc
		set members [lindex $tc 2]
		if {[llength $value] != [llength $members]} {
		    error "Combat::CDR::Encoder: wrong # of struct members for struct $repoid"
		}
		foreach {member_name member_value} $value {
		    set map($member_name) $member_value
		}
		foreach {member_name member_type} $members {
		    if {![info exists map($member_name)]} {
			error "Combat::CDR::Encoder: missing member $member_name in struct $repoid"
		    }
		    Marshal $member_type $map($member_name)
		}
		catch {unset marshalrecursion($repoid)}
		unset map
	    }

	    private method find_union_case {value cases} {
		foreach {dval type} $cases {
		    if {$dval != "(default)" && $dval == $value} {
			return 1
		    }
		}
		return 0
	    }

	    private method find_default_for_union {dtype cases} {
		switch -- $dtype {
		    boolean {
			foreach val {0 1} {
			    if {![find_union_case $val $cases]} {
				return $val
			    }
			}
		    }
		    short -
		    {unsigned short} -
		    long -
		    {unsigned long} {
			for {set val 0} {$val < 32767} {incr val} {
			    if {![find_union_case $val $cases]} {
				return $val
			    }
			    if {![find_union_case [expr {-$val}] $cases]} {
				return [expr {-$val}]
			    }
			}
		    }
		}
		if {[lindex $dtype 0] == "enum"} {
		    foreach val [lindex $dtype 1] {
			if {![find_union_case $val $cases]} {
			    return $val
			}
		    }
		}
		error "find_default_for_union"
	    }
	    
	    private method union {tc value} {
		set repoid [lindex $tc 1]
		set marshalrecursion($repoid) $tc
		set default ""
		set dtype [lindex $tc 2]
		set disc [lindex $value 0]

		if {$disc == "(default)"} {
		    set disc [find_default_for_union $dtype [lindex $tc 3]]
		}

		Marshal $dtype $disc
		foreach {dval type} [lindex $tc 3] {
		    if {$dval == "(default)"} {
			set default $type
		    } elseif {$dval == $disc} {
			Marshal $type [lindex $value 1]
			catch {unset marshalrecursion($repoid)}
			return
		    } elseif {$dtype == "boolean"} {
			set lv [expr {$dval ? 1 : 0}]
			set dv [expr {$disc ? 1 : 0}]
			if {$lv == $dv} {
			    Marshal $type [lindex $value 1]
			    catch {unset marshalrecursion($repoid)}
			    return
			}
		    }
		}			    
		if {$default != ""} {
		    Marshal $default [lindex $value 1]
		}
		catch {unset marshalrecursion($repoid)}
		return
	    }

	    private method exception {tc value} {
		$buffer string [lindex $value 0]
		if {[llength $value] == 2} {
		    foreach {member_name member_value} [lindex $value 1] {
			set map($member_name) $member_value
		    }
		    foreach {member_name member_type} [lindex $tc 2] {
			Marshal $member_type $map($member_name)
		    }
		}
		catch {unset map}
	    }

	    private method sequence {tc value} {
		set type [lindex $tc 1]
		if {$type == "char" || $type == "octet" || $type == "wchar"} {
		    set length [::string length $value]
		} else {
		    set length [llength $value]
		}
		if {[llength $tc] == 3} {
		    set max [lindex $tc 2]
		    if {$max > 0 && $length > $max} {
			error "Combat::CDR::Encoder: sequence exceeds bound"
		    }
		}

		$buffer ulong $length

		if {$type == "octet"} {
		    $buffer octets $value
		} elseif {$type == "char"} {
		    $buffer chars $value
		} elseif {$type == "wchar"} {
		    $buffer wchars $value
		} else {
		    foreach element $value {
			Marshal $type $element
		    }
		}
	    }

	    private method Array {tc value} {
		set type [lindex $tc 1]
		if {$type == "char" || $type == "octet" || $type == "wchar"} {
		    set length [::string length $value]
		} else {
		    set length [llength $value]
		}
		if {$length != [lindex $tc 2]} {
		    error "Combat::CDR::Encoder: array length does not match"
		}
		if {$type == "octet"} {
		    $buffer octets $value
		} elseif {$type == "char"} {
		    $buffer chars $value
		} elseif {$type == "wchar"} {
		    $buffer wchars $value
		} else {
		    foreach element $value {
			Marshal $type $element
		    }
		}
	    }

	    private method enum {tc value} {
		set idx [lsearch -exact [lindex $tc 1] $value]
		if {$idx == -1} {
		    error "Combat::CDR::Encoder: illegal enum value $value"
		}
		$buffer ulong $idx
	    }

	    private method Object {tc value} {
		if {$value == 0} {
		    set ior [::Combat::IOP::IOR \#auto]
		    $ior marshal $buffer
		    itcl::delete object $ior
		    return
		}
		set obj [::Combat::CORBA::ORB::GetObjFromRef $value]
		set ior [$obj get_ior]
		$ior marshal $buffer
	    }

	    private method ValueType {tc value} {
		if {$value == 0} {
		    $buffer long 0
		    return
		}

		set repoid [lindex $tc 1]
		set marshalrecursion($repoid) $tc

		foreach {member_name member_value} $value {
		    set map($member_name) $member_value
		}

		if {[info exists map(_tc_)]} {
		    set marshaltc $map(_tc_)
		} else {
		    set marshaltc $tc
		}

		if {[lindex $marshaltc 4] == "custom"} {
		    error "cannot marshal custom valuetype"
		}

		set value_tag 2147483392	;# 0x7fffff00
		set value_tag [expr {$value_tag | 6}]

		if {[lindex $marshaltc 4] == "truncatable" || $chunking} {
		    set chunked 1
		} else {
		    set chunked 0
		}

		if {$chunked} {
		    set value_tag [expr {$value_tag | 8}]
		}

		set repoids [list]
		set itc $marshaltc

		while {[lindex $itc 4] == "truncatable"} {
		    lappend repoids [lindex $itc 1]
		    set itc [lindex $itc 3]
		}

		lappend repoids [lindex $itc 1]

		#
		# end previous chunk first
		#

		if {$nestinglevel > 0 && $chunked} {
		    $buffer end_chunk
		    $buffer long [expr {-$nestinglevel}]
		}

		if {$chunked} {
		    set chunking 1
		    incr nestinglevel
		}

		$buffer long $value_tag
		$buffer ulong [llength $repoids]

		foreach id $repoids {
		    $buffer string $id
		}

		set found 0
		set itc $marshaltc
		set tcs [list]

		while {$itc != 0} {
		    if {$repoid == [lindex $itc 1]} {
			set found 1
		    }
		    set tcs [linsert $tcs 0 $itc]
		    set itc [lindex $itc 3]
		}

		if {$repoid == "IDL:omg.org/CORBA/ValueBase:1.0"} {
		    set found 1
		}

		if {!$found} {
		    error "Combat::CDR::Encoder: _tc_ not compatible with expected type $repoid"
		}

		if {$chunked} {
		    $buffer begin_chunk
		}

		foreach mtc $tcs {
		    foreach {visi member_name member_type} [lindex $mtc 2] {
			if {![info exists map($member_name)]} {
			    error "Combat::CDR::Encoder: missing member $member_name in value $repoid"
			}
			Marshal $member_type $map($member_name)
		    }
		}

		if {$chunked} {
		    $buffer end_chunk
		    $buffer long [expr {-$nestinglevel}]

		    if {[incr nestinglevel -1] == 0} {
			set chunking 0
		    }
		}

		catch {unset map}
		catch {unset marshalrecursion($repoid)}
	    }

	    private method ValueBox {tc value} {
		set vtc [list valuetype [lindex $tc 1] \
			[list public foo [lindex $tc 2]] 0 ""]
		if {$value == 0} {
		    ValueType $vtc $value
		} else {
		    ValueType $vtc [list foo $value]
		}
	    }

	    private method AbstractInterface {tc value} {
		if {$value == "0"} {
		    $buffer boolean 0
		    $buffer long 0
		    return
		}

		if {[llength $value] == 1} {
		    $buffer boolean 1
		    Object [list Object IDL:omg.org/CORBA/Object:1.0] $value
		} else {
		    $buffer boolean 0
		    ValueType [list valuetype \
			    IDL:omg.org/CORBA/ValueBase:1.0 \
			    [list] "" ""] $value
		}
	    }

	    private method recursive {tc value} {
		set repoid [lindex $tc 1]
		if {![info exists marshalrecursion($repoid)]} {
		    error "illegal marshalling recursion for $repoid"
		}
		set savetc $marshalrecursion($repoid)
 		set res [Marshal $marshalrecursion($repoid) $value]
		set marshalrecursion($repoid) $savetc
		return $res
	    }

	    public method Marshal {tc value} {
		#
		# handle primitive types
		#

		switch $tc {
		    null                 { return }
		    void                 { return }
		    boolean              { $buffer boolean $value ; return }
		    short                { $buffer short $value ; return }
		    long                 { $buffer long $value ; return }
		    {unsigned short}     { $buffer ushort $value ; return }
		    {unsigned long}      { $buffer ulong $value ; return }
		    {long long}          { $buffer longlong $value ; return }
		    {unsigned long long} { $buffer ulonglong $value ; return }
		    float                { $buffer float $value ; return }
		    double               { $buffer double $value ; return }
		    {long double}        { $buffer longdouble $value ; return }
		    char                 { $buffer char $value ; return }
		    octet                { $buffer octet $value ; return }
		    wchar                { $buffer wchar $value ; return }
		    string               { $buffer string $value ; return }
		    wstring              { $buffer wstring $value ; return }
		    any                  { any $value ; return }
		    TypeCode             { TypeCode $value ; return }
		}

		#
		# constructed types
		#

		set type [lindex $tc 0]

		switch $type {
		    string               { bstring $tc $value ; return }
		    wstring              { bwstring $tc $value ; return }
		    struct               { struct $tc $value ; return }
		    union                { union $tc $value ; return }
		    exception            { exception $tc $value ; return }
		    sequence             { sequence $tc $value ; return }
		    array                { Array $tc $value ; return }
		    enum                 { enum $tc $value ; return }
		    Object               { Object $tc $value ; return }
		    valuetype            { ValueType $tc $value ; return }
		    valuebox             { ValueBox $tc $value ; return }
		    abstractinterface    { AbstractInterface $tc $value ; return }
		    recursive            { recursive $tc $value ; return }
		}

		error "unknown type: $tc"
	    }
	}
    }
}