summaryrefslogtreecommitdiff
path: root/TAO/tao/decode.cpp
blob: 2635ddea42cda221f1b1d6c26ac1d093b6e5f56e (plain)
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
// $Id$

// ============================================================================
//
// = LIBRARY
//    TAO
//
// = FILENAME
//    decode.cpp
//
// = DESCRIPTION
//   Code for decoding different data types
//
//   The original code had a single static decoder function defined on
//   the CDR class that called traverse to interpret the data
//   types. This version defines a virtual method "decode" on each
//   class and avoids calling traverse.
//
// = AUTHOR
//     Copyright 1994-1995 by Sun Microsystems Inc.
//     and
//     Aniruddha Gokhale
//
// ============================================================================

#include "tao/Marshal.h"
#include "tao/CDR.h"
#include "tao/Environment.h"
#include "tao/GIOP.h"
#include "tao/Any.h"
#include "tao/Principal.h"
#include "tao/MProfile.h"
#include "tao/Object.h"
#include "tao/Stub.h"
#include "tao/varout.h"
#include "tao/ORB.h"
#include "tao/Union.h"
#include "tao/ORB_Core.h"
#include "tao/debug.h"

ACE_RCSID(tao, decode, "$Id$")

// The decoder is exactly the reverse of the encoder, except that:
//
//      * Unmarshaling some data types involve allocating memory.  Such
//        types include sequences (the buffer), objrefs, Principals, Anys,
//        TypeCodes, and strings.
//
//      * The decoder is used when retrieving typecode parameters from
//        encapsulations.  This means it must deal with "CORBA::tk_indirect",
//        the magic value (~0u) signifying typecode indirection.
//
// This second case is identified by a bit of a hack: the second
// "data" value is used to hold the parent typecode, rather than being
// ignored.  This means that all other invocations of decoder () **
// MUST ** pass zero for the second data parameter, in case they
// decode a TypeCode.  If they didn't, this case might be signified
// inappropriately.
//
// XXX desirable to have a less hacky solution to that ... pull that
// code out into a separate routine called both by CDR::decoder () and
// by the code retrieving typecode parameters from encapsulations.

typedef TAO_Object_Field_T<CORBA::Object,CORBA::Object_var> TAO_Object_Field_Class;

CORBA::TypeCode::traverse_status
TAO_Marshal_Primitive::decode (CORBA::TypeCode_ptr  tc,
                               const void *data,
                               const void *,
                               void *context,
                               CORBA::Environment &ACE_TRY_ENV)
{
  CORBA::Boolean continue_decoding = 1;
  TAO_InputCDR *stream = (TAO_InputCDR *) context;
  CORBA::TypeCode::traverse_status   retval =
    CORBA::TypeCode::TRAVERSE_CONTINUE; // status of encode operation

  switch (tc->kind_)
    {
    case CORBA::tk_null:
    case CORBA::tk_void:
      break;
    case CORBA::tk_short:
    case CORBA::tk_ushort:
      continue_decoding = stream->read_short (*(CORBA::Short *) data);
      break;
    case CORBA::tk_long:
    case CORBA::tk_ulong:
    case CORBA::tk_float:
    case CORBA::tk_enum:
      continue_decoding = stream->read_long (*(CORBA::Long *) data);
      break;
    case CORBA::tk_double:
    case CORBA::tk_longlong:
    case CORBA::tk_ulonglong:
      continue_decoding = stream->read_longlong (*(CORBA::LongLong *) data);
      break;
    case CORBA::tk_boolean:
      continue_decoding = stream->read_boolean (*(CORBA::Boolean *) data);
      break;
    case CORBA::tk_char:
    case CORBA::tk_octet:
      continue_decoding = stream->read_char (*(CORBA::Char *) data);
      break;
    case CORBA::tk_longdouble:
      continue_decoding = stream->read_longdouble (*(CORBA::LongDouble *) data);
      break;
    case CORBA::tk_wchar:
      continue_decoding = stream->read_wchar (*(CORBA::WChar *) data);
      break;
    default:
      retval = CORBA::TypeCode::TRAVERSE_STOP;
      // we are not a primitive type
    }
  if (retval != CORBA::TypeCode::TRAVERSE_CONTINUE
      || continue_decoding != 1)
    {
      if (TAO_debug_level > 0)
        ACE_DEBUG ((LM_DEBUG,
                    "TAO_Marshal_Primitive::decode detected error\n"));
      ACE_THROW_RETURN (CORBA::MARSHAL (TAO_DEFAULT_MINOR_CODE, CORBA::COMPLETED_MAYBE),
                        CORBA::TypeCode::TRAVERSE_STOP);
    }
  return CORBA::TypeCode::TRAVERSE_CONTINUE;
}

CORBA::TypeCode::traverse_status
TAO_Marshal_Any::decode (CORBA::TypeCode_ptr,
                         const void *data,
                         const void *,
                         void *context,
                         CORBA::Environment &ACE_TRY_ENV)
{
  CORBA::Any *any = (CORBA::Any *) data;

  // Typecode of the element that makes the Any.
  CORBA::TypeCode_var elem_tc;

  // Context is the CDR stream.
  TAO_InputCDR *stream = (TAO_InputCDR *) context;

  // Status of encode operation.
  CORBA::TypeCode::traverse_status retval =
    CORBA::TypeCode::TRAVERSE_CONTINUE;

  // Decode the typecode description for the element.
  retval = stream->decode (CORBA::_tc_TypeCode,
                           &elem_tc.out (),
                           0,
                           ACE_TRY_ENV);
  ACE_CHECK_RETURN (retval);
  if (retval != CORBA::TypeCode::TRAVERSE_CONTINUE)
    return retval;

  // Let the Any maintain a pointer to the CDR stream
  // @@ ASG + CORYAN - The following commented line would have been a great
  // optimization. However, it turns out that although the Message_Block is
  // heap-allocated, the actual buffer i.e., data block is allocated on the
  // function call stack. Once we are out of these chain of functions and
  // return into the stub, we have lost the activation record for the
  // actual buffer. Hence it makes no sense keeping pointers to stack
  // memory.
  //
  // See TAO_Stub.cpp::do_static_call in which a GIOP_Invocation is
  // allocated on stack
#if 0
  any->cdr_ = ACE_Message_Block::duplicate ((ACE_Message_Block *)
                                            stream->start ());
#endif
  // one solution is to heap allocate the GIOP_Invocation. However, that
  // would be bad since not all requests will use Anys.
  //
  // One solution is to allocate a new Message_Block with its own heap
  // allocated data_block. (We may optimize this using allocators for known
  // sizes). We allocate a Message_Block of the size that is required by
  // the data type held by the Any. To find what is the size of this data
  // in the CDR, we traverse the CDR by skipping past this data type. We
  // then get an offset using the "begin" and "end" shown below that tells
  // us the size. The skipping is done on a temporary CDR stream and not on
  // the actual incoming CDR stream. Once we have allocated a new
  // Message_Block, we simply append the data into it from the original CDR
  // stream.
  char *begin, *end;
  TAO_InputCDR temp (*stream);

  begin = stream->rd_ptr ();
  retval = temp.skip (elem_tc.in (), ACE_TRY_ENV);
  ACE_CHECK_RETURN (retval);

  if (retval != CORBA::TypeCode::TRAVERSE_CONTINUE)
    return retval;

  end = temp.rd_ptr ();

  // We need to allocate more memory than in the original
  // stream, first to guarantee that the buffer is aligned in
  // memory and next because the realignment may introduce
  // extra padding. 2*MAX_ALIGNMENT should be enough.
  // @@EXC@@ This doesn't seem to be exception safe.
  TAO_OutputCDR out (end - begin + 2 * ACE_CDR::MAX_ALIGNMENT);

  retval = out.append (elem_tc.in (), stream, ACE_TRY_ENV);
  ACE_CHECK_RETURN (retval);
  if (retval != CORBA::TypeCode::TRAVERSE_CONTINUE)
    return retval;

  ACE_Message_Block::release (any->cdr_);
  if (any->any_owns_data_ && any->value_ != 0)
    DEEP_FREE (any->type_, any->value_, 0, ACE_TRY_ENV);
  ACE_CHECK_RETURN (CORBA::TypeCode::TRAVERSE_STOP);

  any->cdr_ = ACE_Message_Block::duplicate (out.begin ());
  any->value_ = 0;

  if (any->type_)
    CORBA::release (any->type_);

  any->type_ = elem_tc._retn ();
  any->any_owns_data_ = 0;

  if (retval != CORBA::TypeCode::TRAVERSE_CONTINUE)
    {
      if (TAO_debug_level > 0)
        ACE_DEBUG ((LM_DEBUG,
                    "TAO_Marshal_Any::decode detected error\n"));
      ACE_THROW_RETURN (CORBA::MARSHAL (TAO_DEFAULT_MINOR_CODE, CORBA::COMPLETED_MAYBE),
                        retval);
    }
  return retval;
}

CORBA::TypeCode::traverse_status
TAO_Marshal_TypeCode::decode (CORBA::TypeCode_ptr,
                              const void *data,
                              const void *parent_typecode,
                              void *context,
                              CORBA::Environment  &ACE_TRY_ENV)
{
  CORBA::Boolean continue_decoding = 1;

  // Context is the CDR stream.
  TAO_InputCDR *stream = (TAO_InputCDR *) context;

  // Typecode to be decoded.
  CORBA::TypeCode_ptr *tcp;

  // Typecode kind.
  CORBA::ULong kind;

  static CORBA::TypeCode_ptr tc_consts [CORBA::TC_KIND_COUNT] =
  {
    CORBA::_tc_null,
    CORBA::_tc_void,
    CORBA::_tc_short,
    CORBA::_tc_long,
    CORBA::_tc_ushort,

    CORBA::_tc_ulong,
    CORBA::_tc_float,
    CORBA::_tc_double,
    CORBA::_tc_boolean,
    CORBA::_tc_char,

    CORBA::_tc_octet,
    CORBA::_tc_any,
    CORBA::_tc_TypeCode,
    CORBA::_tc_Principal,

    0, // CORBA::_tc_Object ... type ID is CORBA_Object
    0, // CORBA_tk_struct
    0, // CORBA_tk_union
    0, // CORBA_tk_enum
    0, // CORBA::_tc_string ... unbounded
    0, // CORBA_tk_sequence
    0, // CORBA_tk_array
    0, // CORBA_tk_alias
    0, // CORBA_tk_except

    CORBA::_tc_longlong,
    CORBA::_tc_ulonglong,
    CORBA::_tc_longdouble,
    CORBA::_tc_wchar,
    0           // CORBA::_tc_wstring ... unbounded
  };

  // TypeCode for the parent. The most likely situation when a parent will be
  // provided is when we are precomputing the private state of an IDL compiler
  // generated or an ORB owned TypeCode, OR we are decoding an indirected
  // TypeCode. In such circumstances, the decoded
  // TypeCode will share resources with its parent and cannot be freed until
  // its parent is being freed.
  CORBA::TypeCode_ptr parent = (CORBA::TypeCode_ptr) parent_typecode;

  // Decode the "kind" field of the typecode from the stream
  continue_decoding = stream->read_ulong (kind);

  if (continue_decoding == 1)
    {
      // The data has to be a TypeCode_ptr *.
      tcp = (CORBA::TypeCode_ptr *) data;

      // Typecodes with empty parameter lists all have preallocated
      // constants.  We use those to reduce memory consumption and
      // heap access ... also, to speed things up!
      if (kind < CORBA::TC_KIND_COUNT
          && (*tcp = tc_consts [(u_int) kind]) != 0)
        // parent is ignored
        *tcp = CORBA::TypeCode::_duplicate (tc_consts [(u_int) kind]);
      else if (kind == ~0u || kind < CORBA::TC_KIND_COUNT)
        {
          // Either a non-constant typecode or an indirected typecode.
          switch (kind)
            {
              // Need special handling for all kinds of typecodes that
              // have nonempty parameter lists ...
            default:
              // Error: missed a case!
              ACE_THROW_RETURN (CORBA::INTERNAL (TAO_DEFAULT_MINOR_CODE, CORBA::COMPLETED_MAYBE), CORBA::TypeCode::TRAVERSE_STOP);

              // Some have "simple" parameter lists ... some of these
              // also have preallocated constants that could be used.
            case CORBA::tk_string:
            case CORBA::tk_wstring:
              {
                CORBA::ULong bound;

                continue_decoding = stream->read_ulong (bound);
                if (continue_decoding)
                  {
                    if (bound == 0)
                      {
                        // unbounded string. Let us reuse the ORB owned
                        // _tc_string or _tc_wstring
                        if (kind == CORBA::tk_string)
                          *tcp = CORBA::TypeCode::_duplicate
                            (CORBA::_tc_string);
                        else
                          *tcp = CORBA::TypeCode::_duplicate
                            (CORBA::_tc_wstring);
                      }
                    else
                      {
                        // bounded string. Create a TypeCode. If it is does not
                        // have a parent, then the application must free it.

                        // allocate a new TypeCode
#if 1
                        // This may produce a memory leak, because
                        // callers are sloppy about removing this
                        // objects.
                        CORBA::Long _oc_bounded_string [] =
                        {TAO_ENCAP_BYTE_ORDER, 0};
                        // Bounded string. Save the bounds
                        _oc_bounded_string [1] = (CORBA::Long) bound;
                        *tcp = new CORBA::TypeCode (ACE_static_cast(CORBA::TCKind, kind),
                                                    8,
                                                    ACE_reinterpret_cast(char*,_oc_bounded_string),
                                                    0, sizeof
                                                    (CORBA::String_var), 0);
#elif 0
                        // This one fails because we are passing the
                        // parent but the buffer (_oc_bounded_string) is
                        // not pointing to the parent CDR stream
                        // (hence no sharing) and the length is wrong
                        // (should be 8 not bounds).
                        CORBA::Long _oc_bounded_string [] =
                        {TAO_ENCAP_BYTE_ORDER, 0};
                        // Bounded string. Save the bounds
                        _oc_bounded_string [1] = (CORBA::Long) bound;
                        *tcp = new CORBA::TypeCode ((CORBA::TCKind) kind,
                                                    bound, (char *) &_oc_bounded_string,
                                                    0, sizeof
                                                    (CORBA::String_var), parent);
#else
                        // This depends on the fact that <stream> is
                        // actually pointing to the parent CDR stream,
                        // it is untested.
                        *tcp = new CORBA::TypeCode ((CORBA::TCKind) kind,
                                                    8,
                                                    stream->rd_ptr () - 8,
                                                    0, 0, parent);
#endif
                      }
                  }
              }
            break;

            // Indirected typecodes, illegal at "top level" but we
            // allow unmarshaling of them here because we use the same
            // code to read "off the wire" (where they're illegal) and
            // to read out of an encapsulation stream.  We distinguish
            // the case where this is legal as described above.
            case ~0u:
              {
                if (parent_typecode == 0)
                    ACE_THROW_RETURN (CORBA::INTERNAL (TAO_DEFAULT_MINOR_CODE, CORBA::COMPLETED_MAYBE), CORBA::TypeCode::TRAVERSE_STOP);

                // Get the long indicating the encapsulation offset,
                // then set up indirection stream that's like "stream"
                // but has space enough only for the typecode and the
                // length for the encapsulated parameters.
                //
                // The offset must be negative
                CORBA::Long offset;

                continue_decoding = stream->read_long (offset);
                if (continue_decoding)
                  {
                    // Since indirected typecodes cannot occur at the
                    // topmost level, they can occur starting only at the
                    // second and subsequent levels. This means that a
                    // normal encoding of that typecode occurred somewhere
                    // before in the stream. As a result the offset field
                    // must always be negative. See the CORBA spec for details.
                    continue_decoding = (offset < 0);
                  }

                // Slava Galperin <galperin@teknowledge.com> clarifies
                // this:
                // CORBA Spec says:
                //
                // The encoding of such an indirection is as a
                // TypeCode with a TCKind value that has the special
                // value 2^32 -1 (0xffffffff, all ones). Such
                // typecodes have a single (simple) parameter, which
                // is the long offset (in units of octets) from the
                // simple parameter. (This means that an offset of
                // negative four (-4) is illegal because it will be
                // self-indirecting.)
                // (CORBA V2.2 CDR Transfer Syntax February 1998 page 13-17)
                //
                // This apparently assumes offset from the <em>
                // beginning </em> of the simple parameter.
                // [Right, because otherwise the value -8 would be
                // illegal]
                // Because at this point stream is positioned after
                // the parameter, we need to account for that when
                // constructing indir_stream by subtracting 4 (length
                // of the offset parameter itself).

                //                TAO_InputCDR indir_stream (*stream, 8, offset
                //                - 4);
                ACE_Message_Block *mb = (ACE_Message_Block *)stream->start ();
                TAO_InputCDR indir_stream (mb->rd_ptr () + offset - 4,
                                           -1 * (offset - 4));


                continue_decoding = (CORBA::Boolean) indir_stream.good_bit ();

                // Get "kind" and length of target typecode
                //
                // XXX this currently assumes the TCKind to which we
                // indirect is the same byte order as the "parent"
                // typecode -- not the right assumption; see how the
                // TypeCode interpreter does it.

                CORBA::ULong indir_kind = 0;
                CORBA::ULong indir_len = 0;

                // retrieve the typecode kind
                if (continue_decoding)
                  continue_decoding = indir_stream.read_ulong (indir_kind);

                if (continue_decoding
                    && indir_kind >= CORBA::TC_KIND_COUNT)
                  continue_decoding = 0;

                // now retrieve the encapsulation length
                if (continue_decoding)
                  continue_decoding = indir_stream.read_ulong (indir_len);

                // Now construct indirected typecode.  This shares the
                // typecode octets with the "parent" typecode,
                // increasing the amount of memory sharing and
                // reducing the cost of getting typecodes.
                if (continue_decoding)
                  {
                    *tcp = new CORBA::TypeCode ((CORBA::TCKind) indir_kind,
                                                indir_len,
                                                indir_stream.rd_ptr(),
                                                0,
                                                0,
                                                parent);
                  }
              }
            break;

            // The rest have "complex" parameter lists that are
            // encoded as bulk octets ...
            case CORBA::tk_objref:
            case CORBA::tk_struct:
            case CORBA::tk_union:
            case CORBA::tk_enum:
            case CORBA::tk_sequence:
            case CORBA::tk_array:
            case CORBA::tk_alias:
            case CORBA::tk_except:
              {
                CORBA::ULong length;

                // get the encapsulation length
                continue_decoding = stream->read_ulong (length);
                if (!continue_decoding)
                  break;

                // if length > MAXUNSIGNED, error ...
                u_int len = (u_int) length;

                // create a new typecode
                *tcp = new CORBA::TypeCode ((CORBA::TCKind) kind,
                                            len,
                                            stream->rd_ptr (),
                                            0,
                                            0,
                                            parent);
                // skip length number of bytes in the stream, else we may
                // leave the stream in an undefined state
                (void) stream->skip_bytes (length);
              }
            } // end of switch
        }
      else // bad kind_ value to be decoded
        {
          if (TAO_debug_level > 0)
            ACE_DEBUG ((LM_DEBUG,
                        "TAO_Marshal_TypeCode:decode: "
                        "Bad kind_ value in CDR stream\n"));
          ACE_THROW_RETURN ( CORBA::BAD_TYPECODE (), CORBA::TypeCode::TRAVERSE_STOP);
        }
    }

  if (continue_decoding != 1)
    {
      if (TAO_debug_level)
        ACE_DEBUG ((LM_DEBUG,
                    "TAO_Marshal_TypeCode::decode detected error\n"));
      ACE_THROW_RETURN (CORBA::MARSHAL (TAO_DEFAULT_MINOR_CODE, CORBA::COMPLETED_MAYBE),
                        CORBA::TypeCode::TRAVERSE_STOP);
    }
  return CORBA::TypeCode::TRAVERSE_CONTINUE;
}

// Encode Principal.

CORBA::TypeCode::traverse_status
TAO_Marshal_Principal::decode (CORBA::TypeCode_ptr,
                               const void *data,
                               const void *,
                               void *context,
                               CORBA::Environment &ACE_TRY_ENV)
{
  // Context is the CDR stream.
  TAO_InputCDR *stream = (TAO_InputCDR *) context;

  CORBA::Principal_ptr x;

  if ((*stream >> x) == 0)
    {
      *(CORBA_Principal_ptr*)data = CORBA::Principal::_nil ();
      ACE_THROW_RETURN (CORBA::MARSHAL (TAO_DEFAULT_MINOR_CODE, CORBA::COMPLETED_MAYBE),
                        CORBA::TypeCode::TRAVERSE_STOP);
    }

  *(CORBA_Principal **)data = x;
  return CORBA::TypeCode::TRAVERSE_CONTINUE;
}

// Decode obj ref.  An IOR
CORBA::TypeCode::traverse_status
TAO_Marshal_ObjRef::decode (CORBA::TypeCode_ptr,
                            const void *data, // where the result will go
                            const void *,
                            void *context,
                            CORBA::Environment &ACE_TRY_ENV)
{
  // Context is the CDR stream.
  TAO_InputCDR *stream = (TAO_InputCDR *) context;

  CORBA::Object_ptr object;

  if ((*stream >> object) == 0)
    {
      *(CORBA_Object_ptr*)data = CORBA::Object::_nil ();
      ACE_THROW_RETURN (CORBA::MARSHAL (TAO_DEFAULT_MINOR_CODE, CORBA::COMPLETED_MAYBE), CORBA::TypeCode::TRAVERSE_STOP);
    }

  *(CORBA_Object **)data = object;
  return CORBA::TypeCode::TRAVERSE_CONTINUE;
}

// Decode structs.
CORBA::TypeCode::traverse_status
TAO_Marshal_Struct::decode (CORBA::TypeCode_ptr  tc,
                            const void *data,
                            const void *,
                            void *context,
                            CORBA::Environment &ACE_TRY_ENV)
{
  TAO_InputCDR *stream = (TAO_InputCDR *) context;
  CORBA::TypeCode::traverse_status retval =
    CORBA::TypeCode::TRAVERSE_CONTINUE;
  CORBA::Boolean continue_decoding = 1;
  CORBA::TypeCode_ptr param;
  CORBA::Long size, alignment, align_offset;

  void *start_addr = (void *)data;

  // Number of fields in the struct.
  int member_count = tc->member_count (ACE_TRY_ENV);
  ACE_CHECK_RETURN (CORBA::TypeCode::TRAVERSE_STOP);

  for (int i = 0; i < member_count
         && retval == CORBA::TypeCode::TRAVERSE_CONTINUE
         && continue_decoding == 1;
       i++)
    {
      param = tc->member_type (i, ACE_TRY_ENV);
      ACE_CHECK_RETURN (CORBA::TypeCode::TRAVERSE_STOP);

      size = param->size (ACE_TRY_ENV);
      ACE_CHECK_RETURN (CORBA::TypeCode::TRAVERSE_STOP);

      alignment = param->alignment (ACE_TRY_ENV);
      ACE_CHECK_RETURN (CORBA::TypeCode::TRAVERSE_STOP);

      align_offset =
        (ptr_arith_t) ptr_align_binary (data, alignment)
        - (ptr_arith_t) data
        + (ptr_arith_t) ptr_align_binary (start_addr, alignment)
        - (ptr_arith_t) start_addr;
      // if both the start_addr and data are not aligned as per
      // the alignment, we do not add the offset
      data = (const void *) ((ptr_arith_t) data +
                             ((align_offset == alignment) ?
                              0 : align_offset));
      switch (param->kind_)
        {
        case CORBA::tk_null:
        case CORBA::tk_void:
          break;
        case CORBA::tk_short:
        case CORBA::tk_ushort:
          continue_decoding =
            stream->read_short (*(CORBA::Short *) data);
          break;
        case CORBA::tk_long:
        case CORBA::tk_ulong:
        case CORBA::tk_float:
        case CORBA::tk_enum:
          continue_decoding =
            stream->read_long (*(CORBA::Long *) data);
          break;
        case CORBA::tk_double:
        case CORBA::tk_longlong:
        case CORBA::tk_ulonglong:
          continue_decoding =
            stream->read_longlong (*(CORBA::LongLong *) data);
          break;
        case CORBA::tk_boolean:
          continue_decoding =
            stream->read_boolean (*(CORBA::Boolean *) data);
          break;
        case CORBA::tk_char:
        case CORBA::tk_octet:
          continue_decoding =
            stream->read_char (*(CORBA::Char *) data);
          break;
        case CORBA::tk_longdouble:
          continue_decoding =
            stream->read_longdouble (*(CORBA::LongDouble *) data);
          break;
        case CORBA::tk_wchar:
          continue_decoding =
            stream->read_wchar (*(CORBA::WChar *) data);
          break;
        case CORBA::tk_TypeCode:
        case CORBA::tk_any:
        case CORBA::tk_Principal:
        case CORBA::tk_struct:
        case CORBA::tk_union:
        case CORBA::tk_sequence:
        case CORBA::tk_array:
        case CORBA::tk_alias:
        case CORBA::tk_except:
        case CORBA::tk_string:
        case CORBA::tk_wstring:
          retval = stream->decode (param, data, 0, ACE_TRY_ENV);
          break;

        case CORBA::tk_objref:
          {
            CORBA_Object_ptr object;
            retval = stream->decode (param, &object, 0, ACE_TRY_ENV);
            ACE_CHECK_RETURN (CORBA::TypeCode::TRAVERSE_STOP);

            if (retval == CORBA::TypeCode::TRAVERSE_CONTINUE)
              {
                TAO_Object_Field_Class* field =
                  ACE_reinterpret_cast (TAO_Object_Field_Class *,
                                        ACE_const_cast (void *, data));
                field->_downcast (object, ACE_TRY_ENV);
              }
          }
          break;

        default:
          break;
        }
      data = (char *) data + size;
    }
  ACE_CHECK_RETURN (CORBA::TypeCode::TRAVERSE_STOP);

  if (retval != CORBA::TypeCode::TRAVERSE_CONTINUE)
    {
      if (TAO_debug_level > 0)
        ACE_DEBUG ((LM_DEBUG,
                    "TAO_Marshal_Struct::decode detected error\n"));

      ACE_THROW_RETURN (CORBA::MARSHAL (TAO_DEFAULT_MINOR_CODE, CORBA::COMPLETED_MAYBE),
                        CORBA::TypeCode::TRAVERSE_STOP);
    }
  return CORBA::TypeCode::TRAVERSE_CONTINUE;
}

// Encode unions.
CORBA::TypeCode::traverse_status
TAO_Marshal_Union::decode (CORBA::TypeCode_ptr  tc,
                           const void *data,
                           const void *data2,
                           void *context,
                           CORBA::Environment &ACE_TRY_ENV)
{
  // Context is the CDR stream.
  TAO_InputCDR *stream = (TAO_InputCDR *) context;

  CORBA::TypeCode_ptr discrim_tc;
  CORBA::TypeCode_ptr member_tc = 0;
  CORBA::Any_ptr member_label;
  CORBA::ULong discrim_size_with_pad;
  const void *discrim_val;
  CORBA::ULong member_count;
  CORBA::Long  default_index;
  CORBA::ULong i;
  CORBA::TypeCode_ptr default_tc = 0;
  CORBA::Boolean discrim_matched = 0;
  TAO_Base_Union *base_union = (TAO_Base_Union *)data;
  void *member_val;

  discrim_tc = tc->discriminator_type (ACE_TRY_ENV);
  // get the discriminator type
  ACE_CHECK_RETURN (CORBA::TypeCode::TRAVERSE_STOP);

  // decode the discriminator value
  discrim_val = base_union->_discriminant ();
  stream->decode (discrim_tc, discrim_val, data2, ACE_TRY_ENV);
  ACE_CHECK_RETURN (CORBA::TypeCode::TRAVERSE_STOP);

  discrim_size_with_pad = tc->TAO_discrim_pad_size (ACE_TRY_ENV);
  ACE_CHECK_RETURN (CORBA::TypeCode::TRAVERSE_STOP);

  // move the pointer to point to the actual value
  data = (char *) data + discrim_size_with_pad;
  data2 = (char *) data2 + discrim_size_with_pad;

  // now get ready to marshal the actual union value
  default_index = tc->default_index (ACE_TRY_ENV);
  ACE_CHECK_RETURN (CORBA::TypeCode::TRAVERSE_STOP);

  member_count = tc->member_count (ACE_TRY_ENV);
  ACE_CHECK_RETURN (CORBA::TypeCode::TRAVERSE_STOP);

  // check which label value matches with the discriminator
  // value. Accordingly, marshal the corresponding
  // member_type. If none match, check if default exists
  // and marshal accordingly. Otherwise it is an error.

  for (i = 0; member_count-- != 0; i++)
    {
      member_label = tc->member_label (i, ACE_TRY_ENV);
      ACE_CHECK_RETURN (CORBA::TypeCode::TRAVERSE_STOP);

      // do the matching
      CORBA::TypeCode_var type = member_label->type ();
      switch (type->kind (ACE_TRY_ENV)) // kind() doesn't throw any exception.
        {
        case CORBA::tk_short:
          {
            CORBA::Short s;
            *member_label >>= s;
            if (s == *(CORBA::Short *) discrim_val)
              discrim_matched = 1;
          }
          break;
        case CORBA::tk_ushort:
          {
            CORBA::UShort s;
            *member_label >>= s;
            if (s == *(CORBA::UShort *) discrim_val)
              discrim_matched = 1;
          }
          break;
        case CORBA::tk_long:
          {
            CORBA::Long l;
            *member_label >>= l;
            if (l == *(CORBA::Long *) discrim_val)
              discrim_matched = 1;
          }
          break;
        case CORBA::tk_ulong:
          {
            CORBA::ULong l;
            *member_label >>= l;
            if (l == *(CORBA::ULong *) discrim_val)
              discrim_matched = 1;
          }
          break;
        case CORBA::tk_enum:
          {
            CORBA::ULong ul;
            TAO_InputCDR stream ((ACE_Message_Block *)
                                 member_label->_tao_get_cdr ());
            (void)stream.decode (discrim_tc, &ul, 0, ACE_TRY_ENV);
            //@@EXC@@ Rethrow CORBA::MARSHAL (TAO_DEFAULT_MINOR_CODE, CORBA::COMPLETED_MAYBE)?
            ACE_CHECK_RETURN (CORBA::TypeCode::TRAVERSE_STOP);

            if (ul == *(CORBA::ULong *) discrim_val)
              discrim_matched = 1;
          }
          break;
        case CORBA::tk_char:
          {
            CORBA::Char c;
            *member_label >>= CORBA::Any::to_char (c);
            if (c == *(CORBA::Char *) discrim_val)
              discrim_matched = 1;
          }
          break;
        case CORBA::tk_wchar:
          CORBA::WChar wc;
          *member_label >>= CORBA::Any::to_wchar (wc);
          if (wc == *(CORBA::WChar *) discrim_val)
            discrim_matched = 1;
          break;
        case CORBA::tk_boolean:
          {
            CORBA::Boolean b;
            *member_label >>= CORBA::Any::to_boolean (b);
            if (b == *(CORBA::Boolean *) discrim_val)
              discrim_matched = 1;
          }
          break;
        default:
          ACE_THROW_RETURN (CORBA::BAD_TYPECODE (), CORBA::TypeCode::TRAVERSE_STOP);
        }// end of switch

      // get the member typecode
      member_tc = tc->member_type (i, ACE_TRY_ENV);
      ACE_CHECK_RETURN (CORBA::TypeCode::TRAVERSE_STOP);

      // have we reached the default label?, if so,
      // save a handle to the typecode for the default
      if (default_index >= 0 && default_index-- == 0)
        default_tc = member_tc;
      if (discrim_matched)
        {
          member_val = base_union->_access (1);
          // marshal according to the matched typecode
          if (member_tc->kind () == CORBA::tk_objref)
            {
              CORBA_Object_ptr object;
              int retval =
                stream->decode (member_tc, &object, data2, ACE_TRY_ENV);
              ACE_CHECK_RETURN (CORBA::TypeCode::TRAVERSE_STOP);

              if (retval == CORBA::TypeCode::TRAVERSE_CONTINUE)
                {
                  // we know that the object pointer is stored in a
                  // TAO_Object_Field_T parametrized type
                  TAO_Object_Field_Class* field =
                    ACE_reinterpret_cast (TAO_Object_Field_Class *,
                                          member_val);
                  field->_downcast (object, ACE_TRY_ENV);
                  ACE_CHECK_RETURN (CORBA::TypeCode::TRAVERSE_STOP);
                }
              return CORBA::TypeCode::TRAVERSE_CONTINUE;
            }
          else
            {
              return stream->decode (member_tc, member_val,
                                     data2, ACE_TRY_ENV);
            }
        }
    } // end of for loop

  // we are here only if there was no match
  if (default_tc)
    {
      member_val = base_union->_access (1);
      if (default_tc->kind () == CORBA::tk_objref)
        {
          CORBA_Object_ptr object;
          int retval =
            stream->decode (member_tc, &object, data2, ACE_TRY_ENV);
          ACE_CHECK_RETURN (CORBA::TypeCode::TRAVERSE_STOP);

          if (retval == CORBA::TypeCode::TRAVERSE_CONTINUE)
            {
              // we know that the object pointer is stored in a
              // TAO_Object_Field_T parametrized type
              TAO_Object_Field_Class* field =
                ACE_reinterpret_cast (TAO_Object_Field_Class *,
                                      member_val);
              field->_downcast (object, ACE_TRY_ENV);
              ACE_CHECK_RETURN (CORBA::TypeCode::TRAVERSE_STOP);
            }
          return CORBA::TypeCode::TRAVERSE_CONTINUE;
        }
      else
        {
          return stream->decode (default_tc, member_val,
                                 data2, ACE_TRY_ENV);
        }
    }
  else
    return CORBA::TypeCode::TRAVERSE_CONTINUE;
}

// decode string
CORBA::TypeCode::traverse_status
TAO_Marshal_String::decode (CORBA::TypeCode_ptr,
                            const void *data,
                            const void *,
                            void *context,
                            CORBA::Environment &ACE_TRY_ENV)
{
  CORBA::Boolean continue_decoding = 1;
  // Context is the CDR stream.
  TAO_InputCDR *stream = (TAO_InputCDR *) context;

  CORBA::String* str_ptr = (CORBA::String*)data;

  // On decode, omit the check against specified string bounds, and
  // cope with illegal "zero length" strings (all lengths on the wire
  // must include a NUL).
  //
  // This is on the principle of being gracious in what we accept; we
  // don't generate messages that fail to comply with protocol specs,
  // but we will accept them when it's clear how to do so.

  continue_decoding = stream->read_string (*str_ptr);
  if (continue_decoding != 1)
    {
      if (TAO_debug_level > 0)
        ACE_DEBUG ((LM_DEBUG,
                    "TAO_Marshal_String::decode detected error\n"));
      ACE_THROW_RETURN (CORBA::MARSHAL (TAO_DEFAULT_MINOR_CODE, CORBA::COMPLETED_MAYBE),
                        CORBA::TypeCode::TRAVERSE_STOP);
    }
  return CORBA::TypeCode::TRAVERSE_CONTINUE;
}

// Decode sequence.

CORBA::TypeCode::traverse_status
TAO_Marshal_Sequence::decode (CORBA::TypeCode_ptr  tc,
                              const void *data,
                              const void *,
                              void *context,
                              CORBA::Environment &ACE_TRY_ENV)
{
  CORBA::Boolean continue_decoding = 1;
  TAO_InputCDR *stream = (TAO_InputCDR *) context;
  TAO_Base_Sequence *seq = (TAO_Base_Sequence *)data;
  // Return status.
  CORBA::TypeCode::traverse_status retval =
    CORBA::TypeCode::TRAVERSE_CONTINUE;
  // Typecode of the element.
  CORBA::TypeCode_ptr tc2;
  // Size of element.
  size_t size;
  CORBA::ULong bounds;
  char *value;

  // First unmarshal the sequence length ... we trust it to be right
  // here, on the "be gracious in what you accept" principle.  We
  // don't generate illegal sequences (i.e. length > bounds).

  continue_decoding = stream->read_ulong (bounds);

  if (continue_decoding)
    {
      // No point decoding an empty sequence.
      if (bounds > 0)
        {
          // Get element typecode.
          tc2 = tc->content_type (ACE_TRY_ENV);
          ACE_CHECK_RETURN (CORBA::TypeCode::TRAVERSE_STOP);

          size = tc2->size (ACE_TRY_ENV);
          ACE_CHECK_RETURN (CORBA::TypeCode::TRAVERSE_STOP);

#if defined (TAO_NO_COPY_OCTET_SEQUENCES)
          // The treatment of octet sequences is completely
          // different.
          if (tc2->kind_ == CORBA::tk_octet
              && ACE_BIT_DISABLED (stream->start ()->flags (),
                                   ACE_Message_Block::DONT_DELETE))
            {
              TAO_Unbounded_Sequence<CORBA::Octet>* seq2 =
                ACE_dynamic_cast(TAO_Unbounded_Sequence<CORBA::Octet>*, seq);
              seq2->replace (bounds, stream->start ());
              seq2->mb ()->wr_ptr (seq2->mb ()->rd_ptr () + bounds);
              stream->skip_bytes (bounds);
              return CORBA::TypeCode::TRAVERSE_CONTINUE;
            }
#endif /* defined (TAO_NO_COPY_OCTET_SEQUENCES) */

          // Allocate the buffer using the virtual
          // _allocate_buffer method, hence the right
          // constructors are invoked and size for the array
          // is OK.  The sequence will release it, since its
          // release_ field is 1.
          if (seq->maximum_ < bounds)
            {
              seq->_deallocate_buffer ();
              seq->maximum_ = bounds;
              seq->release_ = 1;
              seq->buffer_ = 0;
              seq->_allocate_buffer (bounds);
            }
          // In any case the sequence length is changed.
          seq->length_ = bounds;


          value = (char *) seq->buffer_;

          switch (tc2->kind_)
            {
            case CORBA::tk_null:
            case CORBA::tk_void:
              return CORBA::TypeCode::TRAVERSE_CONTINUE;

            case CORBA::tk_short:
            case CORBA::tk_ushort:
              // For primitives, compute the size only once
              continue_decoding = continue_decoding &&
                stream->read_short_array
                ((CORBA::Short *) value, bounds);
              if (continue_decoding == 1)
                return CORBA::TypeCode::TRAVERSE_CONTINUE;
              break;

            case CORBA::tk_long:
            case CORBA::tk_ulong:
            case CORBA::tk_float:
            case CORBA::tk_enum:
              // For primitives, compute the size only once
              continue_decoding = continue_decoding &&
                stream->read_long_array
                ((CORBA::Long *) value, bounds);
              if (continue_decoding == 1)
                return CORBA::TypeCode::TRAVERSE_CONTINUE;
              break;

            case CORBA::tk_double:
            case CORBA::tk_longlong:
            case CORBA::tk_ulonglong:
              // For primitives, compute the size only once
              continue_decoding = continue_decoding &&
                stream->read_longlong_array
                ((CORBA::LongLong *) value, bounds);
              if (continue_decoding == 1)
                return CORBA::TypeCode::TRAVERSE_CONTINUE;
              break;

            case CORBA::tk_boolean:
              // For primitives, compute the size only once
              continue_decoding = continue_decoding &&
                stream->read_octet_array
                ((CORBA::Octet *) value, bounds);
              if (continue_decoding == 1)
                return CORBA::TypeCode::TRAVERSE_CONTINUE;
              break;

            case CORBA::tk_char:
              // For primitives, compute the size only once
              continue_decoding = continue_decoding &&
                stream->read_char_array
                ((CORBA::Char *) value, bounds);
              if (continue_decoding == 1)
                return CORBA::TypeCode::TRAVERSE_CONTINUE;
              break;

            case CORBA::tk_octet:
              // For primitives, compute the size only once
              continue_decoding = continue_decoding &&
                stream->read_octet_array
                ((CORBA::Octet *) value, bounds);
              if (continue_decoding == 1)
                return CORBA::TypeCode::TRAVERSE_CONTINUE;
              break;

            case CORBA::tk_longdouble:
              // For primitives, compute the size only once
              continue_decoding = continue_decoding &&
                stream->read_longdouble_array
                ((CORBA::LongDouble *) value, bounds);
              if (continue_decoding == 1)
                return CORBA::TypeCode::TRAVERSE_CONTINUE;
              break;

            case CORBA::tk_wchar:
              // For primitives, compute the size only once
              continue_decoding = continue_decoding &&
                stream->read_wchar_array
                ((CORBA::WChar *) value, bounds);
              if (continue_decoding == 1)
                return CORBA::TypeCode::TRAVERSE_CONTINUE;
              break;

              // handle all aggregate types here
            case CORBA::tk_string:
            case CORBA::tk_wstring:
            case CORBA::tk_any:
            case CORBA::tk_TypeCode:
            case CORBA::tk_Principal:
            case CORBA::tk_struct:
            case CORBA::tk_union:
            case CORBA::tk_sequence:
            case CORBA::tk_array:
            case CORBA::tk_alias:
            case CORBA::tk_except:
              // For those aggregate types whose size is
              // constant, we compute it only once.
              while (bounds-- && retval == CORBA::TypeCode::TRAVERSE_CONTINUE)
                {
                  retval = stream->decode (tc2, value, 0, ACE_TRY_ENV);
                  ACE_CHECK_RETURN (CORBA::TypeCode::TRAVERSE_STOP);
                  value += size;
                }
              // CORBA::release (tc2);
              return retval;

            case CORBA::tk_objref:
              {
                size = sizeof (CORBA_Object_ptr);
                while (bounds-- &&
                       retval == CORBA::TypeCode::TRAVERSE_CONTINUE)
                  {
                    CORBA_Object_ptr ptr;
                    retval = stream->decode (tc2, &ptr, 0,  ACE_TRY_ENV);
                    ACE_CHECK_RETURN (CORBA::TypeCode::TRAVERSE_STOP);

                    seq->_downcast (value, ptr, ACE_TRY_ENV);
                    ACE_CHECK_RETURN (CORBA::TypeCode::TRAVERSE_STOP);

                    CORBA::release (ptr);
                    value += size;
                  }
                return retval;
              }

            default:
              break;
            } // end of switch
        } // length is > 0
      else
        return CORBA::TypeCode::TRAVERSE_CONTINUE;
    }
  ACE_THROW_RETURN (CORBA::MARSHAL (),
                    CORBA::TypeCode::TRAVERSE_STOP);
}

// Decode array.

CORBA::TypeCode::traverse_status
TAO_Marshal_Array::decode (CORBA::TypeCode_ptr  tc,
                           const void *data,
                           const void *,
                           void *context,
                           CORBA::Environment &ACE_TRY_ENV)
{
  CORBA::Boolean continue_decoding = 1;
  TAO_InputCDR *stream = (TAO_InputCDR *) context;

  // Return status.
  CORBA::TypeCode::traverse_status retval =
    CORBA::TypeCode::TRAVERSE_CONTINUE;

  // Typecode of the element.
  CORBA::TypeCode_ptr tc2;

  // Size of element.
  size_t  size;
  CORBA::ULong  bounds;
  char *value = (char *) data;

  // retrieve the bounds of the array
  bounds = tc->length (ACE_TRY_ENV);
  ACE_CHECK_RETURN (CORBA::TypeCode::TRAVERSE_STOP);

  // get element typecode
  tc2 = tc->content_type (ACE_TRY_ENV);
  ACE_CHECK_RETURN (CORBA::TypeCode::TRAVERSE_STOP);

  size = tc2->size (ACE_TRY_ENV);
  ACE_CHECK_RETURN (CORBA::TypeCode::TRAVERSE_STOP);

  switch (tc2->kind_)
    {
    case CORBA::tk_null:
    case CORBA::tk_void:
      return CORBA::TypeCode::TRAVERSE_CONTINUE;

    case CORBA::tk_short:
    case CORBA::tk_ushort:
      // For primitives, compute the size only once
      continue_decoding = continue_decoding &&
        stream->read_short_array
        ((CORBA::Short *) value, bounds);
      if (continue_decoding == 1)
        return CORBA::TypeCode::TRAVERSE_CONTINUE;
      break;

    case CORBA::tk_long:
    case CORBA::tk_ulong:
    case CORBA::tk_float:
    case CORBA::tk_enum:
      // For primitives, compute the size only once
      continue_decoding = continue_decoding &&
        stream->read_long_array
        ((CORBA::Long *) value, bounds);
      if (continue_decoding == 1)
        return CORBA::TypeCode::TRAVERSE_CONTINUE;
      break;

    case CORBA::tk_double:
    case CORBA::tk_longlong:
    case CORBA::tk_ulonglong:
      // For primitives, compute the size only once
      continue_decoding = continue_decoding &&
        stream->read_longlong_array
        ((CORBA::LongLong *) value, bounds);
      if (continue_decoding == 1)
        return CORBA::TypeCode::TRAVERSE_CONTINUE;
      break;

    case CORBA::tk_boolean:
      // For primitives, compute the size only once
      continue_decoding = continue_decoding &&
        stream->read_octet_array
        ((CORBA::Octet *) value, bounds);
      if (continue_decoding == 1)
        return CORBA::TypeCode::TRAVERSE_CONTINUE;
      break;

    case CORBA::tk_char:
    case CORBA::tk_octet:
      // For primitives, compute the size only once
      continue_decoding = continue_decoding &&
        stream->read_octet_array
        ((CORBA::Octet *) value, bounds);
      if (continue_decoding == 1)
        return CORBA::TypeCode::TRAVERSE_CONTINUE;
      break;

    case CORBA::tk_longdouble:
      // For primitives, compute the size only once
      continue_decoding = continue_decoding &&
        stream->read_longdouble_array
        ((CORBA::LongDouble *) value, bounds);
      if (continue_decoding == 1)
        return CORBA::TypeCode::TRAVERSE_CONTINUE;
      break;

    case CORBA::tk_wchar:
      // For primitives, compute the size only once
      continue_decoding = continue_decoding &&
        stream->read_wchar_array
        ((CORBA::WChar *) value, bounds);
      if (continue_decoding == 1)
        return CORBA::TypeCode::TRAVERSE_CONTINUE;
      break;

      // handle all aggregate types here
    case CORBA::tk_any:
    case CORBA::tk_TypeCode:
    case CORBA::tk_Principal:
    case CORBA::tk_objref:
    case CORBA::tk_struct:
    case CORBA::tk_union:
    case CORBA::tk_string:
    case CORBA::tk_sequence:
    case CORBA::tk_array:
    case CORBA::tk_alias:
    case CORBA::tk_except:
    case CORBA::tk_wstring:
      // For those aggregate types whose size is constant, we
      // compute it only once
      while (bounds-- && retval == CORBA::TypeCode::TRAVERSE_CONTINUE)
        {
          retval = stream->decode (tc2, value, 0, ACE_TRY_ENV);
          ACE_CHECK_RETURN (CORBA::TypeCode::TRAVERSE_STOP);

          value += size;
        }
      // CORBA::release (tc2);
      return retval;
    default:
      break;
    } // end of switch

  // error exit
  if (TAO_debug_level > 0)
    ACE_DEBUG ((LM_DEBUG,
                "TAO_Marshal_Sequence::decode detected error\n"));
  ACE_THROW_RETURN (CORBA::MARSHAL (),
                    CORBA::TypeCode::TRAVERSE_STOP);
}

// Decode alias.
CORBA::TypeCode::traverse_status
TAO_Marshal_Alias::decode (CORBA::TypeCode_ptr  tc,
                           const void *data,
                           const void *,
                           void *context,
                           CORBA::Environment &ACE_TRY_ENV)
{
  // Typecode of the aliased type.
  CORBA::TypeCode_ptr tc2;
  CORBA::Boolean continue_decoding = 1;

  // Context is the CDR stream.
  TAO_InputCDR *stream = (TAO_InputCDR *) context;

  // Status of decode operation.
  CORBA::TypeCode::traverse_status retval =
    CORBA::TypeCode::TRAVERSE_CONTINUE;
  char *value = (char *) data;

  tc2 = tc->content_type (ACE_TRY_ENV);
  // @@EXC@@ Rethrow CORBA::MARSHAL (TAO_DEFAULT_MINOR_CODE, CORBA::COMPLETED_MAYBE)?
  ACE_CHECK_RETURN (CORBA::TypeCode::TRAVERSE_STOP);

  // Switch on the data type and handle the cases for primitives
  // here for efficiency rather than calling.
  switch (tc2->kind_)
    {
    case CORBA::tk_null:
    case CORBA::tk_void:
      break;
    case CORBA::tk_short:
    case CORBA::tk_ushort:
      continue_decoding =
        stream->read_short (*(CORBA::Short *) value);
      break;
    case CORBA::tk_long:
    case CORBA::tk_ulong:
    case CORBA::tk_float:
    case CORBA::tk_enum:
      continue_decoding =
        stream->read_long (*(CORBA::Long *) value);
      break;
    case CORBA::tk_double:
    case CORBA::tk_longlong:
    case CORBA::tk_ulonglong:
      continue_decoding =
        stream->read_longlong (*(CORBA::LongLong *) value);
      break;
    case CORBA::tk_boolean:
      continue_decoding =
        stream->read_boolean (*(CORBA::Boolean *) value);
      break;
    case CORBA::tk_char:
    case CORBA::tk_octet:
      continue_decoding =
        stream->read_char (*(CORBA::Char *) value);
      break;
    case CORBA::tk_longdouble:
      continue_decoding =
        stream->read_longdouble (*(CORBA::LongDouble *) value);
      break;
    case CORBA::tk_wchar:
      continue_decoding =
        stream->read_wchar (*(CORBA::WChar *) value);
      break;
    case CORBA::tk_string:
    case CORBA::tk_wstring:
    case CORBA::tk_any:
    case CORBA::tk_TypeCode:
    case CORBA::tk_Principal:
    case CORBA::tk_objref:
    case CORBA::tk_struct:
    case CORBA::tk_union:
    case CORBA::tk_sequence:
    case CORBA::tk_array:
    case CORBA::tk_alias:
    case CORBA::tk_except:
      retval = stream->decode (tc2, data, 0, ACE_TRY_ENV);
      // @@EXC@@ Rethrow CORBA::MARSHAL (TAO_DEFAULT_MINOR_CODE, CORBA::COMPLETED_MAYBE)?
      ACE_CHECK_RETURN (CORBA::TypeCode::TRAVERSE_STOP);
      break;
    default:
      // anything else is an error
      retval = CORBA::TypeCode::TRAVERSE_STOP;
    }

  if (retval != CORBA::TypeCode::TRAVERSE_CONTINUE
      || continue_decoding != 1)
    {
      if (TAO_debug_level > 0)
        ACE_DEBUG ((LM_DEBUG,
                    "TAO_Marshal_Except::decode detected error\n"));
      ACE_THROW_RETURN (CORBA::MARSHAL (TAO_DEFAULT_MINOR_CODE, CORBA::COMPLETED_MAYBE),
                        CORBA::TypeCode::TRAVERSE_STOP);
    }
  return CORBA::TypeCode::TRAVERSE_CONTINUE;
}

// Decode exception For exceptions, the "hidden" type ID near the
// front of the on-wire representation was previously unmarshaled and
// mapped to the "tc" typcode we're using to traverse the memory ...
// at the same time its vtable, refcount, and other state was
// established.
//
// NOTE: This is asymmetric with respect to encoding exceptions.
CORBA::TypeCode::traverse_status
TAO_Marshal_Except::decode (CORBA::TypeCode_ptr  tc,
                            const void *data,
                            const void *,
                            void *context,
                            CORBA::Environment &ACE_TRY_ENV)
{
  TAO_InputCDR *stream = (TAO_InputCDR *) context;
  CORBA::TypeCode::traverse_status retval =
    CORBA::TypeCode::TRAVERSE_CONTINUE;
  CORBA::Boolean continue_decoding = 1;
  CORBA::TypeCode_ptr param;
  CORBA::Long size, alignment;

  data = (char *) data + sizeof (CORBA::Exception);
  // @@ (ASG) The reason this is done is because we want to skip the size
  // of the the base class and its private data members (type_ and
  // refcount_). After skipping these data members, we will have the data
  // members of the derived class which must be encoded.

  // Number of fields in the struct.
  int member_count = tc->member_count (ACE_TRY_ENV);
  // @@EXC@@ Rethrow CORBA::MARSHAL (TAO_DEFAULT_MINOR_CODE, CORBA::COMPLETED_MAYBE)?
  ACE_CHECK_RETURN (CORBA::TypeCode::TRAVERSE_STOP);

  for (int i = 0; i < member_count
         && retval == CORBA::TypeCode::TRAVERSE_CONTINUE
         && continue_decoding == 1; i++)
    {
      param = tc->member_type (i, ACE_TRY_ENV);
      // @@EXC@@ Rethrow CORBA::MARSHAL (TAO_DEFAULT_MINOR_CODE, CORBA::COMPLETED_MAYBE)?
      ACE_CHECK_RETURN (CORBA::TypeCode::TRAVERSE_STOP);

      size = param->size (ACE_TRY_ENV);
      // @@EXC@@ Rethrow CORBA::MARSHAL (TAO_DEFAULT_MINOR_CODE, CORBA::COMPLETED_MAYBE)?
      ACE_CHECK_RETURN (CORBA::TypeCode::TRAVERSE_STOP);

      alignment = param->alignment (ACE_TRY_ENV);
      // @@EXC@@ Rethrow CORBA::MARSHAL (TAO_DEFAULT_MINOR_CODE, CORBA::COMPLETED_MAYBE)?
      ACE_CHECK_RETURN (CORBA::TypeCode::TRAVERSE_STOP);

      data = ptr_align_binary (data, alignment);
      switch (param->kind_)
        {
        case CORBA::tk_null:
        case CORBA::tk_void:
          break;
        case CORBA::tk_short:
        case CORBA::tk_ushort:
          continue_decoding =
            stream->read_short (*(CORBA::Short *) data);
          break;
        case CORBA::tk_long:
        case CORBA::tk_ulong:
        case CORBA::tk_float:
        case CORBA::tk_enum:
          continue_decoding =
            stream->read_long (*(CORBA::Long *) data);
          break;
        case CORBA::tk_double:
        case CORBA::tk_longlong:
        case CORBA::tk_ulonglong:
          continue_decoding =
            stream->read_longlong (*(CORBA::LongLong *) data);
          break;
        case CORBA::tk_boolean:
          continue_decoding =
            stream->read_boolean (*(CORBA::Boolean *) data);
          break;
        case CORBA::tk_char:
        case CORBA::tk_octet:
          continue_decoding =
            stream->read_char (*(CORBA::Char *) data);
          break;
        case CORBA::tk_longdouble:
          continue_decoding =
            stream->read_longdouble (*(CORBA::LongDouble *) data);
          break;
        case CORBA::tk_wchar:
          continue_decoding =
            stream->read_wchar (*(CORBA::WChar *) data);
          break;
        case CORBA::tk_any:
        case CORBA::tk_TypeCode:
        case CORBA::tk_Principal:
        case CORBA::tk_objref:
        case CORBA::tk_struct:
        case CORBA::tk_union:
        case CORBA::tk_string:
        case CORBA::tk_sequence:
        case CORBA::tk_array:
        case CORBA::tk_alias:
        case CORBA::tk_except:
        case CORBA::tk_wstring:
          retval = stream->decode (param, data, 0, ACE_TRY_ENV);
          // @@EXC@@ Rethrow CORBA::MARSHAL (TAO_DEFAULT_MINOR_CODE, CORBA::COMPLETED_MAYBE)?
          ACE_CHECK_RETURN (CORBA::TypeCode::TRAVERSE_STOP);
          break;
        default:
          break;
        }
      data = (char *) data + size;
    }

  if (retval != CORBA::TypeCode::TRAVERSE_CONTINUE
      || continue_decoding != 1)
    {
      if (TAO_debug_level > 0)
        ACE_DEBUG ((LM_DEBUG,
                    "TAO_Marshal_Except::decode detected error\n"));
      ACE_THROW_RETURN (CORBA::MARSHAL (TAO_DEFAULT_MINOR_CODE, CORBA::COMPLETED_MAYBE),
                        CORBA::TypeCode::TRAVERSE_STOP);
    }
  return CORBA::TypeCode::TRAVERSE_CONTINUE;
}

// decode wstring
CORBA::TypeCode::traverse_status
TAO_Marshal_WString::decode (CORBA::TypeCode_ptr,
                             const void *data,
                             const void *,
                             void *context,
                             CORBA::Environment &ACE_TRY_ENV)
{
  CORBA::Boolean continue_decoding = 1;
  TAO_InputCDR *stream = (TAO_InputCDR *) context;
  CORBA::WChar *str = *(CORBA::WChar **) data;
  CORBA::ULong len;

  // On decode, omit the check against specified wstring bounds, and
  // cope with illegal "zero length" strings (all lengths on the wire
  // must include a NUL).
  //
  // This is on the principle of being gracious in what we accept; we
  // don't generate messages that fail to comply with protocol specs,
  // but we will accept them when it's clear how to do so.

  continue_decoding = stream->read_ulong (len);

  ACE_NEW_RETURN (str,
                  CORBA::WChar [(size_t) (len)],
                  CORBA::TypeCode::TRAVERSE_CONTINUE);
  *((CORBA::WChar **) data) = str;

  if (len != 0)
    while (continue_decoding != 0 && len--)
      {
        continue_decoding = stream->read_wchar (*str);
        str++;
      }

  if (continue_decoding != 1)
    {
      if (TAO_debug_level > 0)
        ACE_DEBUG ((LM_DEBUG,
                    "TAO_Marshal_WString::decode detected error\n"));
      ACE_THROW_RETURN (CORBA::MARSHAL (TAO_DEFAULT_MINOR_CODE, CORBA::COMPLETED_MAYBE),
                        CORBA::TypeCode::TRAVERSE_STOP);
    }
  return CORBA::TypeCode::TRAVERSE_CONTINUE;
}