summaryrefslogtreecommitdiff
path: root/TAO/tao/IIOP_Interpreter.cpp
blob: 2a096f9babada0a6a99ecac99e78d0818bf26820 (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
// $Id$

// @(#)interp.cpp       1.4 95/11/04
// Copyright 1994-1995 by Sun Microsystems Inc.
// All Rights Reserved

#include "tao/corba.h"

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

TAO_IIOP_Interpreter::Table_Element
TAO_IIOP_Interpreter::table_[CORBA::TC_KIND_COUNT] =
{
  { 0, 1, 0 },                            // CORBA::tk_null
  { 0, 1, 0 },                            // CORBA::tk_void

  { 0, 1, 0, 0 },                         // CORBA::tk_short
  { 0, 1, 0, 0 },                         // CORBA::tk_long
  { 0, 1, 0, 0 },                         // CORBA::tk_ushort
  { 0, 1, 0, 0 },                         // CORBA::tk_ulong

  { 0, 1, 0, 0 },                         // CORBA::tk_float
  { 0, 1, 0, 0 },                         // CORBA::tk_double

  { 0, 1, 0, 0 },                         // CORBA::tk_boolean
  { 0, 1, 0, 0 },                         // CORBA::tk_char
  { 0, 1, 0, 0 },                         // CORBA::tk_octet
  { 0, 1, 0, 0 },                         // CORBA::tk_any

  { 0, 1, 0, 0 },                         // CORBA::tk_TypeCode
  { 0, 1, 0, 0 },                         // CORBA::tk_Principal
  { 0, 1, 0, skip_encapsulation },        // CORBA::tk_objref

  { 0, 1, calc_struct_attributes, 0 },    // CORBA::tk_struct
  { 0, 1, calc_union_attributes, 0 },     // CORBA::tk_union

  { 0, 1, 0, skip_encapsulation },        // CORBA::tk_enum
  { 0, 1, 0, skip_long },                 // CORBA::tk_string
#if defined (TAO_NO_COPY_OCTET_SEQUENCES)
  { 0, 1, calc_seq_attributes, 0 },        // CORBA::tk_sequence
#else
  { 0, 1, 0, skip_encapsulation },        // CORBA::tk_sequence
#endif
  { 0, 1, calc_array_attributes, 0 },     // CORBA::tk_array

  // = Two TCKind values added in 94-11-7
  { 0, 1, calc_alias_attributes, 0 },     // CORBA::tk_alias
  { 0, 1, calc_exception_attributes, 0 }, // CORBA::tk_except

  // = Five extended IDL data types, defined in Appendix A of 94-9-32
  // but here with different numeric TCKind codes.  These types
  // represent extensions to CORBA (specifically, to IDL) which are
  // not yet standardized.

  { 0, 1, 0, 0 },                         // CORBA::tk_longlong
  { 0, 1, 0, 0 },                         // CORBA::tk_ulonglong
  { 0, 1, 0, 0 },                         // CORBA::tk_longdouble
  { 0, 1, 0, 0 },                         // CORBA::tk_wchar
  { 0, 1, 0, skip_long }                  // CORBA::tk_wstring
};

// Runtime initialization of the table above; note that this compiles
// down to a set of assignment statements, with the real work done by
// the C++ compiler when this file gets compiled.
//
// "Natural alignment" is a policy that the processor controls the
// alignment of data based on its type.  There's variation; some CPUs
// have a maximum alignment requirement of two or four bytes, others
// have some type-specific exceptions to the normal "alignment ==
// size" rule.
//
// "Fixed" alignment ignores data type when establishing alignment;
// not all processors support such policies, and those which do often
// pay a cost to do so (viz. RISC/CISC discussions).  The primary
// example of an OS family that chose "fixed" alignment is Microsoft's
// x86 systems, which normally align on one byte boundaries to promote
// data space efficiency.
//
// NOTE: typical PC compiler options let you specify other alignments,
// but none are "natural".  Also, they don't apply consistently to all
// data types.  Change the "one byte" assumption with extreme caution!
// And make sure all header files (e.g. generated by an IDL compiler)
// make sure that alignment of IDL-defined data types is consistent
// (one byte).

  enum TCKIND
  {
    tk_null               = 0,
    tk_void               = 1,
    tk_short              = 2,
    tk_long               = 3,
    tk_ushort             = 4,
    tk_ulong              = 5,
    tk_float              = 6,
    tk_double             = 7,
    tk_boolean            = 8,
    tk_char               = 9,
    tk_octet              = 10,
    tk_any                = 11,
    tk_TypeCode           = 12,
    tk_Principal          = 13,
    tk_objref             = 14,
    tk_struct             = 15,
    tk_union              = 16,
    tk_enum               = 17,
    tk_string             = 18,
    tk_sequence           = 19,
    tk_array              = 20,
    tk_alias              = 21,           // 94-11-7
    tk_except             = 22,           // 94-11-7

    // these five are OMG-IDL data type extensions
    tk_longlong           = 23,           // 94-9-32 Appendix A (+ 2)
    tk_ulonglong          = 24,           // 94-9-32 Appendix A (+ 2)
    tk_longdouble         = 25,           // 94-9-32 Appendix A (+ 2)
    tk_wchar              = 26,           // 94-9-32 Appendix A (+ 2)
    tk_wstring            = 27,           // 94-9-32 Appendix A (+ 2)

    // This symbol is not defined by CORBA 2.0.  It's used to speed up
    // dispatch based on TCKind values, and lets many important ones
    // just be table lookups.  It must always be the last enum value!!

    TC_KIND_COUNT
  };

#if defined (TAO_HAS_FIXED_BYTE_ALIGNMENT)
  // Have a bogus one
  #define declare_entry(x,t) struct align_struct_ ## t { }

  #define setup_entry(x,t) \
    { \
      TAO_IIOP_Interpreter::table_ [t].size_ = sizeof (x); \
      TAO_IIOP_Interpreter::table_ [t].alignment_ = 1; \
    }
#else  /* ! TAO_HAS_FIXED_BYTE_ALIGNMENT */
  // unix, ACE_WIN32, VXWORKS, __Lynx__, at least
  #define declare_entry(x,t) \
    struct align_struct_ ## t \
    { \
      x one; \
      char dummy [TAO_MAXIMUM_NATIVE_TYPE_SIZE + 1 - sizeof(x)]; \
      x two; \
    }

  #define setup_entry(x,t) \
    { \
      align_struct_ ## t align; \
      TAO_IIOP_Interpreter::table_ [t].size_ = sizeof (x); \
      TAO_IIOP_Interpreter::table_ [t].alignment_ = \
      (char *) &align.two - (char *) &align.one - TAO_MAXIMUM_NATIVE_TYPE_SIZE; \
    }
#endif /* ! TAO_HAS_FIXED_BYTE_ALIGNMENT */

// Fills in fixed size and alignment values.

declare_entry (CORBA::Short, tk_short);
declare_entry (CORBA::Long, tk_long);
declare_entry (CORBA::UShort, tk_ushort);
declare_entry (CORBA::ULong, tk_ulong);

declare_entry (CORBA::Float, tk_float);
declare_entry (CORBA::Double, tk_double);

declare_entry (CORBA::Boolean, tk_boolean);
declare_entry (CORBA::Char, tk_char);
declare_entry (CORBA::Octet, tk_octet);
declare_entry (CORBA::Any, tk_any);

declare_entry (CORBA::TypeCode_ptr, tk_TypeCode);
declare_entry (CORBA::Principal_ptr, tk_Principal);
declare_entry (TAO_Object_Field_T<CORBA_Object>, tk_objref);

declare_entry (CORBA::String, tk_string);
#if !defined (TAO_NO_COPY_OCTET_SEQUENCES)
declare_entry (TAO_opaque, tk_sequence);
#endif

declare_entry (CORBA::LongLong, tk_longlong);
declare_entry (CORBA::ULongLong, tk_ulonglong);
declare_entry (CORBA::LongDouble, tk_longdouble);
declare_entry (CORBA::WChar, tk_wchar);
declare_entry (CORBA::WString, tk_wstring);

void
TAO_IIOP_Interpreter::init (void)
{
  setup_entry (CORBA::Short, tk_short);
  setup_entry (CORBA::Long, tk_long);
  setup_entry (CORBA::UShort, tk_ushort);
  setup_entry (CORBA::ULong, tk_ulong);

  setup_entry (CORBA::Float, tk_float);
  setup_entry (CORBA::Double, tk_double);

  setup_entry (CORBA::Boolean, tk_boolean);
  setup_entry (CORBA::Char, tk_char);
  setup_entry (CORBA::Octet, tk_octet);
  setup_entry (CORBA::Any, tk_any);

  setup_entry (CORBA::TypeCode_ptr, tk_TypeCode);
  setup_entry (CORBA::Principal_ptr, tk_Principal);
  setup_entry (CORBA::Object_ptr, tk_objref);

  enum generic_enum {a, b, c, d};

  // XXX workaround for G++ 2.6.3 bug
  // setup_entry (generic_enum, CORBA::tk_enum);
  TAO_IIOP_Interpreter::table_ [CORBA::tk_enum].size_ =
    sizeof (generic_enum);
  TAO_IIOP_Interpreter::table_ [CORBA::tk_enum].alignment_ =
    sizeof (generic_enum);

  setup_entry (CORBA::String, tk_string);
#if !defined (TAO_NO_COPY_OCTET_SEQUENCES)
  setup_entry (TAO_opaque, tk_sequence);
#endif /* defined (TAO_NO_COPY_OCTET_SEQUENCES) */

  setup_entry (CORBA::LongLong, tk_longlong);
  setup_entry (CORBA::ULongLong, tk_ulonglong);
  setup_entry (CORBA::LongDouble, tk_longdouble);
  setup_entry (CORBA::WChar, tk_wchar);
  setup_entry (CORBA::WString, tk_wstring);
}

#undef  setup

CORBA::Boolean
TAO_IIOP_Interpreter::skip_encapsulation (TAO_InputCDR *stream)
{
  return stream->skip_string ();
}

CORBA::Boolean
TAO_IIOP_Interpreter::skip_long (TAO_InputCDR *stream)
{
  CORBA::ULong  scratch;

  return stream->read_ulong (scratch);
}

// For a given typecode, figure out its size and alignment needs.
// This version is used mostly when traversing other typecodes, and
// follows these rules:
//
// - Some typecodes are illegal (can't be nested inside others);
// - Indirections are allowed;
// - The whole typecode (including TCKind enum) is in the stream
//
// When the routine returns, the stream has skipped this TypeCode.
//
// "size" is returned, "alignment" is an 'out' parameter.  If it is
// non-null, "tc" is initialized to hold the contents of the TypeCode;
// it depends on the contents of the original stream to be valid.
//
// XXX explore splitting apart returning the size/alignment data and
// the TypeCode initialization; union traversal would benefit a bit,
// but it would need more than that to make it as speedy as struct
// traversal.

size_t
TAO_IIOP_Interpreter::calc_nested_size_and_alignment_i (CORBA::TypeCode_ptr tc,
                                       TAO_InputCDR* stream,
                                       CORBA::TCKind kind,
                                       size_t &alignment,
                                       CORBA::Environment &env)
{
  CORBA::ULong temp;
  // Just a temporary to retrieve CORBA::TCKind variables as ULong's

  // Check for illegal TCKind enum values ... out of range, or which
  // represent data values that can't be nested.  (Some can't even
  // exist freestanding!)

  if (kind >= CORBA::TC_KIND_COUNT
      || kind <= CORBA::tk_void
      || kind == CORBA::tk_except)
    {
      env.exception (new CORBA::BAD_TYPECODE (CORBA::COMPLETED_NO));
      return 0;
    }

  // Use attribute calculator routine if it exists; these are needed
  // only for variable-sized data types, with encapsulated parameter
  // lists that affect the size and alignment of "top level" memory
  // needed to hold an instance of this type.

  if (TAO_IIOP_Interpreter::table_[kind].calc_ != 0)
    {
      assert (TAO_IIOP_Interpreter::table_[kind].size_ == 0);

      // Pull encapsulation length out of the stream.
      if (stream->read_ulong (temp) == 0)
        {
          env.exception (new CORBA::BAD_TYPECODE (CORBA::COMPLETED_NO));
          return 0;
        }

      // Initialize the TypeCode if requested
      if (tc)
        {
          tc->kind_ = kind;
          tc->buffer_ = stream->rd_ptr ();
          tc->length_ = temp;
        }

      // Set up a separate stream for the parameters; it may easily
      // have a different byte order, and this is as simple a way as
      // any to ensure correctness.  Then use the calculator routine
      // to calculate size and alignment.

      assert (temp <= UINT_MAX);

      TAO_InputCDR nested (*stream, temp);

      if (nested.good_bit () == 0)
        {
          env.exception (new CORBA::BAD_TYPECODE (CORBA::COMPLETED_NO));
          return 0;
        }

      size_t size = TAO_IIOP_Interpreter::table_[kind].calc_ (&nested,
                                                              alignment,
                                                              env);
      if (env.exception () != 0)
        {
          return 0;
        }

      // Check for garbage at end of parameter lists, or other cases
      // where parameters and the size allocated to them don't jive.

      stream->skip_bytes (temp);

      if (stream->rd_ptr () != nested.rd_ptr ())
        {
          env.exception (new CORBA::BAD_TYPECODE (CORBA::COMPLETED_NO));
          return 0;
        }
      return size;
    }
  assert (TAO_IIOP_Interpreter::table_[kind].size_ != 0);

  // Reinitialize the TypeCode if requested; this consumes any
  // TypeCode parameters in the stream.  They only exist for TCKind
  // values that have parameters, but which represent fixed-size data
  // types in the binary representation: CORBA::tk_string, CORBA::tk_wstring,
  // CORBA::tk_objref, CORBA::tk_enum, and CORBA::tk_sequence.

  if (tc)
    {
      CORBA::ULong len;

      tc->kind_ = kind;
      switch (kind)
        {
        default:
          assert (TAO_IIOP_Interpreter::table_[kind].skipper_ == 0);
          break;

        case CORBA::tk_string:
        case CORBA::tk_wstring:
          if (stream->read_ulong (len) == 0)
            {
              env.exception (new CORBA::BAD_TYPECODE (CORBA::COMPLETED_NO));
              return 0;
            }
          tc->length_ = len;
          break;

        case CORBA::tk_enum:
        case CORBA::tk_objref:
        case CORBA::tk_sequence:
          if (stream->read_ulong (len) == 0)
            {
              env.exception (new CORBA::BAD_TYPECODE (CORBA::COMPLETED_NO));
              return 0;
            }
          tc->length_ = len;

          assert (len < UINT_MAX);
          tc->buffer_ = stream->rd_ptr ();
          stream->skip_bytes (len);
          break;
        }

      // Otherwise, consume any parameters without stuffing them into
      // a temporary TypeCode.
    }
  else if (TAO_IIOP_Interpreter::table_[kind].skipper_ != 0
           && TAO_IIOP_Interpreter::table_[kind].skipper_ (stream) == 0)
    {
      env.exception (new CORBA::BAD_TYPECODE (CORBA::COMPLETED_NO));
      return 0;
    }

  // Return statically known values.
  alignment = TAO_IIOP_Interpreter::table_[kind].alignment_;
  return TAO_IIOP_Interpreter::table_[kind].size_;
}

size_t
TAO_IIOP_Interpreter::calc_nested_size_and_alignment (CORBA::TypeCode_ptr tc,
                                            TAO_InputCDR *stream,
                                            size_t &alignment,
                                            CORBA::Environment &env)
{
  // Get the "kind" ... if this is an indirection, this is a guess
  // which will soon be updated.
  CORBA::ULong temp;
  if (stream->read_ulong (temp) == 0)
    {
      env.exception (new CORBA::BAD_TYPECODE (CORBA::COMPLETED_NO));
      return 0;
    }

  env.clear ();

  CORBA::TCKind kind = (CORBA::TCKind) temp;

  if (kind != (CORBA::TCKind) ~0u)
    {
      return TAO_IIOP_Interpreter::calc_nested_size_and_alignment_i
        (tc,
         stream,
         kind,
         alignment,
         env);
    }

  // Get indirection, sanity check it, set up new stream pointing
  // there.
  //
  // XXX access to "real" size limit for this typecode and use it
  // to check for errors before indirect and to limit the new
  // stream's length.  ULONG_MAX is too much!

  // @@ ASG @@ - comparison with -8 or -4. I think the spec says it must be
  // larger than -4 (absolute value)
  // 09/04/98 - check this
  //

  CORBA::Long offset;
  if (!stream->read_long (offset)
      || offset >= -8
      || ((-offset) & 0x03) != 0)
    {
      env.exception (new CORBA::BAD_TYPECODE (CORBA::COMPLETED_NO));
      return 0;
    }

  // Notice how we change the sign of the offset to estimate the
  // maximum size.
  TAO_InputCDR indirected_stream (*stream, -offset, offset);

  // Fetch indirected-to TCKind.
  if (!indirected_stream.read_ulong (temp))
    {
      env.exception (new CORBA::BAD_TYPECODE (CORBA::COMPLETED_NO));
      return 0;
    }
  kind = (CORBA::TCKind) temp;

  return TAO_IIOP_Interpreter::calc_nested_size_and_alignment_i (tc,
                                                &indirected_stream,
                                                kind,
                                                alignment,
                                                env);
}

// Given typecode bytes for a structure (or exception), figure out its
// alignment and size; return size, alignment is an 'out' parameter.
// Only "CORBA::tk_struct" (or "CORBA::tk_except") has been taken out of the stream
// parameter holding the bytes.
//
// We use a one-pass algorithm, calculating size and inter-element
// padding while recording the strongest alignment restriction.  Then
// we correct the size to account for tail-padding.
//
// This routine recognizes that exceptions are just structs with some
// additional information.  Different environments may differ in what
// that additional information is, so this routine may need to be
// taught about compiler-specific representation of that additional
// "RTTI" data.

size_t
TAO_IIOP_Interpreter::calc_struct_and_except_attributes (TAO_InputCDR *stream,
                                               size_t &alignment,
                                               CORBA::Boolean is_exception,
                                               CORBA::Environment &env)
{
  CORBA::ULong  members;
  size_t size;

  // Exceptions are like structs, with key additions (all of which
  // might need to be be applied to structures!): vtable, typecode,
  // and refcount.  The size must include these "hidden" members.
  //
  // NOTE: in environments with "true" C++ exceptions, there may need
  // to be a slot for additional "RTTI" information; maybe it is part
  // of the vtable, or maybe not.  Or, that information (needed to
  // determine which 'catch' clauses apply) may only be provided by
  // the compiler to the runtime support for the "throw" statement.

  if (is_exception)
    {
      size = sizeof (CORBA::Exception);
      alignment = TAO_IIOP_Interpreter::table_[CORBA::tk_TypeCode].alignment_;
    }
  else
    {
      alignment = 1;
      size = 0;
    }

  // skip rest of header (type ID and name) and collect the number of
  // struct members

  if (!stream->skip_string ()
      || !stream->skip_string ()
      || !stream->read_ulong (members))
    {
      env.exception (new CORBA::BAD_TYPECODE (CORBA::COMPLETED_NO));
      return 0;
    }

  // iterate over all the members, skipping their names and looking
  // only at type data.

  for ( ; members != 0; members--) {
    size_t member_size;
    size_t member_alignment;

    // Skip name of the member.
    if (!stream->skip_string ())
      {
        env.exception (new CORBA::BAD_TYPECODE (CORBA::COMPLETED_NO));
        return 0;
      }

    // Get size and alignment of the member, accounting for
    // indirection and the various kinds of parameter encoding.

    member_size = calc_nested_size_and_alignment (0,
                                                  stream,
                                                  member_alignment,
                                                  env);
    if (env.exception () != 0)
      return 0;

    // Round up the struct size to handle member alignment (by adding
    // internal padding), then update the current size to handle the
    // member's size.

    size = (size_t) align_binary (size, member_alignment);
    size += member_size;

    // Finally update the overall structure alignment requirement, if
    // this element must be more strongly aligned.

    if (member_alignment > alignment)
      alignment = member_alignment;
  };

  // Round up the structure size to match its overall alignment.  This
  // adds tail padding, if needed.
  return (size_t) align_binary (size, alignment);
}

// Calculate size and alignment for a structure.

size_t
TAO_IIOP_Interpreter::calc_struct_attributes (TAO_InputCDR *stream,
                                    size_t &alignment,
                                    CORBA::Environment &env)
{
  return calc_struct_and_except_attributes (stream,
                                            alignment,
                                            0,
                                            env);
}

// Calculate size and alignment for an exception.

size_t
TAO_IIOP_Interpreter::calc_exception_attributes (TAO_InputCDR *stream,
                                       size_t &alignment,
                                       CORBA::Environment &env)
{
  return calc_struct_and_except_attributes (stream,
                                            alignment,
                                            1,
                                            env);
}

// Calculate and return sizes for both parts of a union, as needed by
// other code.  Return value is the overall size.  The padded size of
// the discriminant is needed to traverse the two values separately.
// Unfortunately that is not quite practical to do with a single pass
// over the typecode: the inter-element padding changes depending on
// the strictest alignment required by _any_ arm of the union.

size_t
TAO_IIOP_Interpreter::calc_key_union_attributes (TAO_InputCDR *stream,
                                                 size_t &overall_alignment,
                                                 size_t &discrim_size_with_pad,
                                                 CORBA::Environment &env)
{
  CORBA::ULong members;
  CORBA::ULong temp;
  size_t discrim_size;
  size_t value_alignment;
  size_t value_size;

  overall_alignment = value_alignment = 1;
  value_size = discrim_size_with_pad = 0;

  // Skip initial optional members (type ID and name).

  if (!stream->skip_string ()                   // type ID
      || !stream->skip_string ())
    {   // typedef name
      env.exception (new CORBA::BAD_TYPECODE (CORBA::COMPLETED_NO));
      return 0;
    }

  // Calculate discriminant size and alignment: it's the first member
  // of the "struct" representing the union.  We detect illegal
  // discriminant kinds a bit later.

  CORBA::TypeCode discrim_tc (CORBA::tk_void);

  discrim_size = calc_nested_size_and_alignment (&discrim_tc,
                                                 stream,
                                                 overall_alignment,
                                                 env);
  if (env.exception () != 0)
    return 0;

  // skip "default used" indicator, and save "member count"

  if (!stream->read_ulong (temp)                 // default used
      || !stream->read_ulong (members))
    {   // member count
      env.exception (new CORBA::BAD_TYPECODE (CORBA::COMPLETED_NO));
      return 0;
    }

  // iterate over the tuples for all the members; all we care about is
  // their types, which can affect either alignment or padding
  // requirement for the union part of the construct.

  for ( ; members != 0; members--) {
    size_t member_size, member_alignment;

    // Skip member label; its size varies with discriminant type, but
    // here we don't care about its content.  This is where illegal
    // discriminant kinds are detected.
    //
    // NOTE:  This modifies 94-9-32 Appendix A to stipulate that
    // "long long" values are not legal as discriminants.

    switch (discrim_tc.kind_)
      {
      case CORBA::tk_short:
      case CORBA::tk_ushort:
      case CORBA::tk_wchar:
        {
          CORBA::Short s;

          if (!stream->read_short (s))
            {
              env.exception (new CORBA::BAD_TYPECODE (CORBA::COMPLETED_NO));
              return 0;
            }
        }
      break;

      case CORBA::tk_long:
      case CORBA::tk_ulong:
      case CORBA::tk_enum:
        {
          CORBA::Long l;

          if (!stream->read_long (l))
            {
              env.exception (new CORBA::BAD_TYPECODE (CORBA::COMPLETED_NO));
              return 0;
            }
        }
      break;

      case CORBA::tk_boolean:
      case CORBA::tk_char:
        {
          CORBA::Char c;

          if (!stream->read_char (c))
            {
              env.exception (new CORBA::BAD_TYPECODE (CORBA::COMPLETED_NO));
              return 0;
            }
        }
      break;

      default:
        env.exception (new CORBA::BAD_TYPECODE (CORBA::COMPLETED_NO));
        return 0;
      }

    // We also don't care about any member name.

    if (!stream->skip_string ())
      {
        env.exception (new CORBA::BAD_TYPECODE (CORBA::COMPLETED_NO));
        return 0;
      }

    // Get the member size and alignment.
    // However, for variable sized member types and types that have
    // constructors, these become members of the pointer types. We need to
    // determine if we are dealing with such a member and accordingly adjust
    // the size and alignment
    CORBA::Boolean var_sized_member = 0;
    TAO_InputCDR temp (*stream);
    if (calc_union_attr_is_var_sized_member (&temp, var_sized_member) == -1)
      {
        env.exception (new CORBA::BAD_TYPECODE (CORBA::COMPLETED_NO));
        return 0;
     }

    if (var_sized_member)
      {
        // define a dummy structure to compute alignment of pointer type
        struct align_ptr
        {
          void *one;
          char dummy [TAO_MAXIMUM_NATIVE_TYPE_SIZE + 1 - sizeof (void*)];
          void *two;
        };
        align_ptr ap;

        member_size = sizeof (void*);
        member_alignment = (char *) &ap.two - (char *) &ap.one
          - TAO_MAXIMUM_NATIVE_TYPE_SIZE;
        (void) CORBA::TypeCode::skip_typecode (*stream);
      }
    else
      {
        // proceed with the normal way of computing the size and alignment
        member_size = calc_nested_size_and_alignment (0,
                                                      stream,
                                                      member_alignment,
                                                      env);
      }

    if (env.exception () != 0)
      return 0;

    // Save the largest member and alignment.  They don't need to be
    // changed in sync -- e.g. "long double" size is larger than its
    // alignment restriction on SPARC, x86, and some m68k platforms.
    if (member_size > value_size)
      value_size = member_size;
    if (member_alignment > value_alignment)
      value_alignment = member_alignment;
  }

  // Round up the discriminator's size to include padding it needs in
  // order to be followed by the value.
  discrim_size_with_pad = (size_t) align_binary (discrim_size,
                                                 value_alignment);

  // Now calculate the overall size of the structure, which is the
  // discriminator, inter-element padding, value, and tail padding.
  // We know all of those except tail padding, which is a function of
  // the overall alignment.  (Ensures that arrays of these can be
  // safely allocated and accessed!)

  if (value_alignment > overall_alignment)
    overall_alignment = value_alignment;

  return (size_t) align_binary (discrim_size_with_pad + value_size
                                + sizeof (TAO_Base_Union),
                                overall_alignment);
}

// Calculate size and alignment for a CORBA discriminated union.
//
// Note that this is really a two-element structure.  The first
// element is the discriminator; the second is the value.  All normal
// structure padding/alignment rules apply.  In particular, all arms
// of the union have the same initial address (adequately aligned for
// any of the members).

size_t
TAO_IIOP_Interpreter::calc_union_attributes (TAO_InputCDR *stream,
                                   size_t &alignment,
                                   CORBA::Environment &env)
{
  size_t scratch;

  return calc_key_union_attributes (stream, alignment, scratch, env);
}

// Calculate size and alignment for a typedeffed type.

size_t
TAO_IIOP_Interpreter::calc_alias_attributes (TAO_InputCDR *stream,
                                   size_t &alignment,
                                   CORBA::Environment &env)
{
  // Skip type ID and name in the parameter stream

  if (!stream->skip_string ()                   // type ID
      || !stream->skip_string ())               // typedef name
    {
      env.exception (new CORBA::BAD_TYPECODE (CORBA::COMPLETED_NO));
      return 0;
    }

  // The typedef is identical to the type for which it stands.
  return calc_nested_size_and_alignment (0, stream, alignment, env);
}

// Calculate size and alignment of an array.  (All such arrays are
// described as single dimensional, even though the IDL definition may
// specify a multidimensional array ... such arrays are treated as
// nested single dimensional arrays.)

size_t
TAO_IIOP_Interpreter::calc_array_attributes (TAO_InputCDR *stream,
                                   size_t &alignment,
                                   CORBA::Environment &env)
{
  size_t member_size;
  CORBA::ULong member_count;

  // get size and alignment of the array member

  member_size = calc_nested_size_and_alignment (0, stream, alignment, env);
  if (env.exception () != 0)
    return 0;

  // Get and check count of members.

  if (stream->read_ulong (member_count) == 0
      || member_count > UINT_MAX)
    {
      env.exception (new CORBA::BAD_TYPECODE (CORBA::COMPLETED_NO));
      return 0;
    }

  // Array size is a function only of member number and count
  return member_size * (size_t) member_count;
}

#if defined (TAO_NO_COPY_OCTET_SEQUENCES)
// Calculate size and alignment of a sequence.
// If octet sequence optimizations are enabled the size of octet
// sequences differ from the size of a regular sequence.

size_t
TAO_IIOP_Interpreter::calc_seq_attributes (TAO_InputCDR *stream,
                                           size_t &alignment,
                                           CORBA::Environment &env)
{
  CORBA::TCKind kind;

  // Get the "kind" ... if this is an indirection, this is a guess
  // which will soon be updated.
  CORBA::ULong temp;
  if (stream->read_ulong (temp) == 0)
    {
      env.exception (new CORBA::BAD_TYPECODE (CORBA::COMPLETED_NO));
      return 0;
    }

  if (temp == ~0u)
    {
      // Get indirection, sanity check it, set up new stream pointing
      // there.
      //
      // XXX access to "real" size limit for this typecode and use it
      // to check for errors before indirect and to limit the new
      // stream's length.  ULONG_MAX is too much!
      CORBA::Long offset;
      if (!stream->read_long (offset)
          || offset >= -8
          || ((-offset) & 0x03) != 0)
        {
          env.exception (new CORBA::BAD_TYPECODE (CORBA::COMPLETED_NO));
          return 0;
        }
      // Notice how we change the sign of the offset to estimate the
      // maximum size.
      TAO_InputCDR indirected_stream (*stream, -offset, offset);

      // Fetch indirected-to TCKind; this *cannot* be an indirection
      // again because multiple indirections are non-complaint.
      if (indirected_stream.read_ulong (temp) == 0
          || temp == ~0u)
        {
          env.exception (new CORBA::BAD_TYPECODE (CORBA::COMPLETED_NO));
          return 0;
        }
    }

  kind = ACE_static_cast(CORBA::TCKind, temp);

  // Skip the rest of the stream because we don't use it.
  if (stream->skip_bytes (stream->length ()) == 0)
    {
      env.exception (new CORBA::BAD_TYPECODE (CORBA::COMPLETED_NO));
      return 0;
    }

  size_t size;
  if (kind == CORBA::tk_octet)
    {
      declare_entry (TAO_opaque, tk_sequence);
      align_struct_tk_sequence align;
      size = sizeof (TAO_opaque);
#if defined (TAO_HAS_FIXED_BYTE_ALIGNMENT)
      alignment = 1;
#else
      alignment =
        (char*)&align.two - (char*)&align.one - TAO_MAXIMUM_NATIVE_TYPE_SIZE;
#endif /* TAO_HAS_FIXED_BYTE_ALIGNMENT */
    }
  else
    {
      // Note: GHS can't handle TAO_Unbounded_Sequence<CORBA::Long> below.
      // Instead, use TAO_Unbounded_String_Sequence.
      declare_entry (TAO_Unbounded_String_Sequence, tk_sequence);
      size = sizeof (TAO_Unbounded_String_Sequence);
      align_struct_tk_sequence align;
#if defined (TAO_HAS_FIXED_BYTE_ALIGNMENT)
      alignment = 1;
#else
      alignment =
        (char*)&align.two - (char*)&align.one - TAO_MAXIMUM_NATIVE_TYPE_SIZE;
#endif /* TAO_HAS_FIXED_BYTE_ALIGNMENT */
    }
  return size;
}
#endif /* defined (TAO_NO_COPY_OCTET_SEQUENCES) */

// Cast the discriminant values to the right type and compare them.

CORBA::Boolean
TAO_IIOP_Interpreter::match_value (CORBA::TCKind kind,
                         TAO_InputCDR *tc_stream,
                         const void *value,
                         CORBA::Environment &env)
{
  CORBA::Boolean retval = 0;

  switch (kind)
    {
    case CORBA::tk_short:
    case CORBA::tk_ushort:
      {
        CORBA::UShort discrim;

        if (tc_stream->read_ushort (discrim) != 0)
          retval = (discrim == *(CORBA::UShort *)value);
        else
          env.exception (new CORBA::BAD_TYPECODE (CORBA::COMPLETED_NO));
      }
    break;

    case CORBA::tk_long:
    case CORBA::tk_ulong:
      {
        CORBA::ULong discrim;

        if (tc_stream->read_ulong (discrim) != 0)
          retval = (discrim == *(CORBA::ULong *)value);
        else
          env.exception (new CORBA::BAD_TYPECODE (CORBA::COMPLETED_NO));
      }
    break;

    case CORBA::tk_enum:
      {
        CORBA::ULong discrim;

        if (tc_stream->read_ulong (discrim) != 0)
          retval = (discrim == *(unsigned *)value);
        else
          env.exception (new CORBA::BAD_TYPECODE (CORBA::COMPLETED_NO));
      }
    break;

    case CORBA::tk_boolean:
      {
        CORBA::Boolean discrim;

        if (tc_stream->read_boolean (discrim) != 0)
          retval = (discrim == *(CORBA::Boolean *)value);
        else
          env.exception (new CORBA::BAD_TYPECODE (CORBA::COMPLETED_NO));
      }
    break;

    case CORBA::tk_char:
      {
        CORBA::Char discrim;

        if (tc_stream->read_char (discrim) != 0)
          retval = (discrim == *(CORBA::Char *)value);
        else
          env.exception (new CORBA::BAD_TYPECODE (CORBA::COMPLETED_NO));
      }
    break;

    case CORBA::tk_wchar:
      {
        CORBA::WChar discrim;

        if (tc_stream->read_wchar (discrim) != 0)
          retval = (discrim == *(CORBA::WChar *)value);
        else
          env.exception (new CORBA::BAD_TYPECODE (CORBA::COMPLETED_NO));
      }
    break;

    default:
      env.exception (new CORBA::BAD_TYPECODE (CORBA::COMPLETED_NO));
    }

  return retval;
}

int
TAO_IIOP_Interpreter
::calc_union_attr_is_var_sized_member (TAO_InputCDR *stream,
                                       CORBA::Boolean &flag)
{
  CORBA::Environment env;
  CORBA::ULong temp;
  flag = 0;

  // Get the tk_ "kind"  field
  if (stream->read_ulong (temp) == 0)
    {
      // error
      return -1;
    }

  env.clear ();

  CORBA::TCKind kind = (CORBA::TCKind) temp;

  switch (kind)
    {
    case CORBA::tk_null:
    case CORBA::tk_void:
      // error
      return -1;
    case CORBA::tk_short:
    case CORBA::tk_ushort:
    case CORBA::tk_long:
    case CORBA::tk_ulong:
    case CORBA::tk_longlong:
    case CORBA::tk_ulonglong:
    case CORBA::tk_float:
    case CORBA::tk_double:
    case CORBA::tk_longdouble:
    case CORBA::tk_boolean:
    case CORBA::tk_char:
    case CORBA::tk_wchar:
    case CORBA::tk_octet:
    case CORBA::tk_enum:
    case CORBA::tk_Principal:
      // not variable sized
      return 0;
    case CORBA::tk_any:
    case CORBA::tk_TypeCode:
    case CORBA::tk_objref:
    case CORBA::tk_union:
    case CORBA::tk_string:
    case CORBA::tk_wstring:
    case CORBA::tk_sequence:
    case CORBA::tk_array:
    case CORBA::tk_except:
      // always variable sized
      flag = 1;
      return 0;
    case CORBA::tk_alias:
      // find out what its base says
      {
        CORBA::ULong encap;

        // Pull encapsulation length out of the stream.
        if (stream->read_ulong (encap) == 0)
          {
            return -1;
          }

        assert (encap <= UINT_MAX);

        TAO_InputCDR nested (*stream, temp);

        if (nested.good_bit () == 0)
          {
            return -1;
          }

        // Skip type ID and name in the parameter stream
        if (!nested.skip_string ()                   // type ID
            || !nested.skip_string ())               // typedef name
          {
            return -1;
          }

        //        stream->skip_bytes (encap);
        return calc_union_attr_is_var_sized_member (&nested, flag);
      }
      ACE_NOTREACHED (break);
    case CORBA::tk_struct:
      // explore further based on members
      {
        CORBA::ULong encap;

        // Pull encapsulation length out of the stream.
        if (stream->read_ulong (encap) == 0)
          {
            return -1;
          }

        assert (encap <= UINT_MAX);

        TAO_InputCDR nested (*stream, temp);

        if (nested.good_bit () == 0)
          {
            return -1;
          }

        //        stream.skip_bytes (encap);
        // Skip type ID and name in the parameter stream
        if (!nested.skip_string ()                   // type ID
            || !nested.skip_string ())               // typedef name
          {
            return -1;
          }

        CORBA::ULong member_count;
        if (nested.read_ulong (member_count) == 0)
          {
            return -1;
          }
        for (CORBA::ULong i = 0; i < member_count && !flag; i++)
          {
            // stop this loop the moment we discover that a member is variable
            // in size

            // skip the name
            if (nested.skip_string () == 0)
              {
                return -1;
              }
            TAO_InputCDR member_tc (nested);
            if (calc_union_attr_is_var_sized_member (&member_tc, flag) == -1)
              {
                return -1;
              }
            CORBA::TypeCode::skip_typecode (nested);
          }
      }
      return flag;
      ACE_NOTREACHED (break);
    case ~0:
      // TO-DO
      return 0;
    default:
      // error
      return -1;
    }
  // cannot reach here
  ACE_NOTREACHED (return -1);
}

#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
template class TAO_Unbounded_Sequence<CORBA::Long>;
#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
#pragma instantiate TAO_Unbounded_Sequence<CORBA::Long>
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */