summaryrefslogtreecommitdiff
path: root/TAO/tao/encode.cpp
blob: 09b74bba6677744979d8abb198dded4fe5209921 (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
// ============================================================================
//
// = LIBRARY
//    TAO
//
// = FILENAME
//    encode.cpp
//
// = DESCRIPTION
//   Code for encoding different data types
//
//   The original code had a single static encoder function defined on the CDR
//   class that called traverse to interpret the data types. This version
//   defines a virtual method "encode" on each class and avoids calling traverse.
//
// = AUTHOR
//     Copyright 1994-1995 by Sun Microsystems Inc.
//     and Aniruddha Gokhale
//
// ============================================================================

#include "tao/corba.h"

extern CORBA::TypeCode TC_opaque;

// Encode instances of arbitrary data types based only on typecode.
// "data" points to the data type; if it's not a primitve data type,
// the TypeCode interpreter is used to recursively encode its
// components.  "context" is the marshaling stream on which to encode
// the data value.

CORBA::TypeCode::traverse_status
TAO_Marshal_Primitive::encode (CORBA::TypeCode_ptr tc,
			       const void *data,
			       const void *,
			       void *context,
			       CORBA::Environment &env)
{
  CORBA::Boolean continue_encoding = CORBA::B_TRUE;
  CDR *stream = (CDR *) context;  // context is the CDR stream
  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_encoding = stream->put_short (*(CORBA::Short *) data);
      break;
    case CORBA::tk_long:
    case CORBA::tk_ulong:
    case CORBA::tk_float:
    case CORBA::tk_enum:
      continue_encoding = stream->put_long (*(CORBA::Long *) data);
      break;
    case CORBA::tk_double:
    case CORBA::tk_longlong:
    case CORBA::tk_ulonglong:
      continue_encoding = stream->put_longlong (*(CORBA::LongLong *) data);
      break;
    case CORBA::tk_boolean:
      continue_encoding = stream->put_boolean (*(CORBA::Boolean *) data);
      break;
    case CORBA::tk_char:
    case CORBA::tk_octet:
      continue_encoding = stream->put_char (*(CORBA::Char *) data);
      break;
    case CORBA::tk_longdouble:
      continue_encoding = stream->put_longdouble (*(CORBA::LongDouble *) data);
      break;
    case CORBA::tk_wchar:
      continue_encoding = stream->put_wchar (*(wchar_t *) data);
      break;
    default:
      retval = CORBA::TypeCode::TRAVERSE_STOP;
      // we are not a primitive type
    }

  if (retval == CORBA::TypeCode::TRAVERSE_CONTINUE
      && continue_encoding == CORBA::B_TRUE)
    return CORBA::TypeCode::TRAVERSE_CONTINUE;
  else
    {
      env.exception (new CORBA::MARSHAL (CORBA::COMPLETED_MAYBE));
      dmsg ("TAO_Marshal_Primitive::encode detected error");
      return CORBA::TypeCode::TRAVERSE_STOP;
    }
}

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

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

  // Value maintained by the Any.
  void *value;

  CORBA::Boolean continue_encoding = CORBA::B_TRUE;

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

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

  elem_tc = any->type ();

  // Encode the typecode description for the element.
  if (stream->encode (CORBA::_tc_TypeCode, &elem_tc, 0, env)
      == CORBA::TypeCode::TRAVERSE_CONTINUE) {
    value = (void *) any->value ();

    // Switch on the data type and handle the cases for primitives
    // here for efficiency rather than calling.
    switch (elem_tc->kind_)
      {
      case CORBA::tk_null:
      case CORBA::tk_void:
        break;
      case CORBA::tk_short:
      case CORBA::tk_ushort:
        continue_encoding = stream->put_short (*(CORBA::Short *) value);
        break;
      case CORBA::tk_long:
      case CORBA::tk_ulong:
      case CORBA::tk_float:
      case CORBA::tk_enum:
        continue_encoding = stream->put_long (*(CORBA::Long *) value);
        break;
      case CORBA::tk_double:
      case CORBA::tk_longlong:
      case CORBA::tk_ulonglong:
        continue_encoding = stream->put_longlong (*(CORBA::LongLong *) value);
        break;
      case CORBA::tk_boolean:
        continue_encoding = stream->put_boolean (*(CORBA::Boolean *) value);
        break;
      case CORBA::tk_char:
      case CORBA::tk_octet:
        continue_encoding = stream->put_char (*(CORBA::Char *) value);
        break;
      case CORBA::tk_longdouble:
        continue_encoding = stream->put_longdouble (*(CORBA::LongDouble *) value);
        break;
      case CORBA::tk_wchar:
        continue_encoding = stream->put_wchar (*(wchar_t *) value);
        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->encode (elem_tc, value, 0, env);
        break;
      default:
        // anything else is an error
        retval = CORBA::TypeCode::TRAVERSE_STOP;
      }
  }

  if (retval == CORBA::TypeCode::TRAVERSE_CONTINUE
      && continue_encoding == CORBA::B_TRUE)
    return CORBA::TypeCode::TRAVERSE_CONTINUE;
  else
    {
      env.exception (new CORBA::MARSHAL (CORBA::COMPLETED_MAYBE));
      dmsg ("TAO_Marshal_Any::encode detected error");
      return CORBA::TypeCode::TRAVERSE_STOP;
    }
}

CORBA::TypeCode::traverse_status
TAO_Marshal_TypeCode::encode (CORBA::TypeCode_ptr,
			      const void *data,
			      const void *,
			      void *context,
			      CORBA::Environment &env)
{
  CORBA::Boolean continue_encoding = CORBA::B_TRUE;
  CDR *stream = (CDR *) context;  // context is the CDR stream
  CORBA::TypeCode_ptr tc2;         // typecode to be encoded

  tc2 = *(CORBA::TypeCode_ptr *) data;  // the data has to be a TypeCode_ptr

  // encode the "kind" field of the typecode
  continue_encoding = stream->put_ulong ((CORBA::ULong) tc2->kind_);
  if (continue_encoding == CORBA::B_TRUE)
    {
      // now encode the parameters, if any
      switch (tc2->kind_)
        {
          // Most TypeCodes have empty parameter lists
        default:
          break;

          // A few have "simple" parameter lists
        case CORBA::tk_string:
        case CORBA::tk_wstring:
          continue_encoding = stream->put_ulong (tc2->length_);
          break;

          // Indirected typecodes can't occur at "top level" like
          // this, only nested inside others!
        case ~0:
          dmsg ("indirected typecode at top level!");
          continue_encoding = CORBA::B_FALSE;
          break;

          // The rest have "complex" parameter lists that are
          // already encoded as bulk octets ... put length, then
          // 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:
          {
            continue_encoding = stream->put_ulong (tc2->length_);

            for (u_int i = 0; i < tc2->length_ && continue_encoding; i++)
              continue_encoding = stream->put_octet (tc2->buffer_ [i]);
          }
        }
    }
  if (continue_encoding == CORBA::B_TRUE)
    return CORBA::TypeCode::TRAVERSE_CONTINUE;
  else
    {
      env.exception (new CORBA::MARSHAL (CORBA::COMPLETED_MAYBE));
      dmsg ("TAO_Marshal_TypeCode::encode detected error");
      return CORBA::TypeCode::TRAVERSE_STOP;
    }
}

// encode Principal
CORBA::TypeCode::traverse_status
TAO_Marshal_Principal::encode (CORBA::TypeCode_ptr,
			       const void *data,
			       const void *,
			       void *context,
			       CORBA::Environment &env)
{
  CORBA::Boolean continue_encoding = CORBA::B_TRUE;
  CDR *stream = (CDR *) context;  // context is the CDR stream

  CORBA::Principal_ptr p = *(CORBA::Principal_ptr *) data;

  if (p != 0)
    {
      continue_encoding = stream->put_long (p->id.length);

      for (u_int i = 0;
	   continue_encoding && i < p->id.length;
	   i++)
        continue_encoding = stream->put_octet (p->id.buffer [i]);
    }
  else
    continue_encoding = stream->put_long (0);
  if (continue_encoding == CORBA::B_TRUE)
    return CORBA::TypeCode::TRAVERSE_CONTINUE;
  else
    {
      env.exception (new CORBA::MARSHAL (CORBA::COMPLETED_MAYBE));
      dmsg ("TAO_Marshal_Principal::encode detected error");
      return CORBA::TypeCode::TRAVERSE_STOP;
    }
}

// encode obj ref
CORBA::TypeCode::traverse_status
TAO_Marshal_ObjRef::encode (CORBA::TypeCode_ptr,
			    const void *data,
			    const void *,
			    void *context,
			    CORBA::Environment &env)
{
  CDR *stream = (CDR *) context;  // context is the CDR stream

  // Current version:  objref is really an IIOP_Object.
  //
  // This will change in the future; STUB_Object knows how to
  // marshal itself, that will be used.
  //
  // XXX this doesn't actually verify that the stuff got written
  // OK to the "wire" ...
  CORBA::Object_ptr obj = *(CORBA::Object_ptr*) data;

  // NIL objrefs ... marshal as empty type hint, no elements.

  if (CORBA::is_nil (obj))
    {
      return CORBA::TypeCode::TRAVERSE_CONTINUE;
    }
  else
    {

      // All other objrefs ... narrow to a "real type" that we
      // recognize, then marshal.
      //
      // XXX this will be changed so it narrows to STUB_Object and
      // then asks that surrogate/proxy to marshal itself.
      //
      // For now, the original code is minimally changed.

      IIOP_Object *objdata;
      IIOP::Profile *profile;

      if (obj->QueryInterface (IID_IIOP_Object, (void **) &objdata) != NOERROR)
        {
          env.exception (new CORBA::MARSHAL (CORBA::COMPLETED_NO));
          return CORBA::TypeCode::TRAVERSE_STOP;
        }
      obj->Release ();
      profile = &objdata->profile;

      // STRING, a type ID hint
      stream->encode (CORBA::_tc_string, &objdata->type_id, 0, env);

      // UNSIGNED LONG, value one, count of the sequence of
      // encapsulated protocol profiles;
      stream->put_ulong (1);

      // UNSIGNED LONG, tag for this protocol profile;
      stream->put_ulong (TAO_IOP_TAG_INTERNET_IOP);

      // UNSIGNED LONG, number of succeeding bytes in the
      // encapsulation.  We don't actually need to make the
      // encapsulation, as nothing needs stronger alignment than
      // this longword; it guarantees the rest is aligned for us.
      u_int hostlen;

      hostlen = ACE_OS::strlen ((char *) profile->host);
      stream->put_ulong (1                              // byte order
                         + 3                            // version + pad byte
                         + 4                            // sizeof (strlen)
                         + hostlen + 1                  // strlen + null
                         + (~hostlen & 01)              // optional pad byte
                         + 2                            // port
                         + (hostlen & 02)               // optional pad short
                         + 4                            // sizeof (key length)
                         + profile->object_key.length); // key length

      // CHAR describing byte order, starting the encapsulation

      stream->put_char (TAO_ENCAP_BYTE_ORDER);

      // IIOP::Version, two characters (version 1.0) padding
      stream->put_char (profile->iiop_version.major);
      stream->put_char (profile->iiop_version.minor);

      // STRING hostname from profile
      stream->encode (CORBA::_tc_string, &profile->host, 0, env);

      // UNSIGNED SHORT port number
      stream->put_ushort (profile->port);

      // OCTET SEQUENCE for object key
      stream->encode (&TC_opaque, &profile->object_key, 0, env);
      return CORBA::TypeCode::TRAVERSE_CONTINUE;
    }
}

// encode structs
CORBA::TypeCode::traverse_status
TAO_Marshal_Struct::encode (CORBA::TypeCode_ptr tc,
			    const void *data,
			    const void *,
			    void *context,
			    CORBA::Environment &env)
{
  CDR *stream = (CDR *) context;
  CORBA::TypeCode::traverse_status retval = CORBA::TypeCode::TRAVERSE_CONTINUE;
  CORBA::Boolean continue_encoding = CORBA::B_TRUE;
  CORBA::TypeCode_ptr param;
  CORBA::Long size, alignment;

  if (env.exception () == 0)
    {
      int member_count = tc->member_count (env);

      for (int i = 0;
	   i < member_count && retval == CORBA::TypeCode::TRAVERSE_CONTINUE
	     && continue_encoding == CORBA::B_TRUE;
	   i++)
	{
	  param = tc->member_type (i, env);
	  if (env.exception () == 0)
	    {
	      size = param->size (env);
	      if (env.exception () == 0)
		{
		  alignment = param->alignment (env);
		  if (env.exception () == 0)
		    {
		      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_encoding = stream->put_short (*(CORBA::Short *) data);
			  break;
			case CORBA::tk_long:
			case CORBA::tk_ulong:
			case CORBA::tk_float:
			case CORBA::tk_enum:
			  continue_encoding = stream->put_long (*(CORBA::Long *) data);
			  break;
			case CORBA::tk_double:
			case CORBA::tk_longlong:
			case CORBA::tk_ulonglong:
			  continue_encoding = stream->put_longlong (*(CORBA::LongLong *) data);
			  break;
			case CORBA::tk_boolean:
			  continue_encoding = stream->put_boolean (*(CORBA::Boolean *) data);
			  break;
			case CORBA::tk_char:
			case CORBA::tk_octet:
			  continue_encoding = stream->put_char (*(CORBA::Char *) data);
			  break;
			case CORBA::tk_longdouble:
			  continue_encoding = stream->put_longdouble (*(CORBA::LongDouble *) data);
			  break;
			case CORBA::tk_wchar:
			  continue_encoding = stream->put_wchar (*(wchar_t *) 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->encode (param, data, 0, env);
			  break;
			default:
			  break;
			}
		      data = (char *) data + size;
		    }
		  else
		    return CORBA::TypeCode::TRAVERSE_STOP;
		}
	      else
		return CORBA::TypeCode::TRAVERSE_STOP;
	    }
	  else
	    return CORBA::TypeCode::TRAVERSE_STOP;
	}
    }
  else
      return CORBA::TypeCode::TRAVERSE_STOP;

  if (retval == CORBA::TypeCode::TRAVERSE_CONTINUE
      && continue_encoding == CORBA::B_TRUE)
    return CORBA::TypeCode::TRAVERSE_CONTINUE;
  else
    {
      env.exception (new CORBA::MARSHAL (CORBA::COMPLETED_MAYBE));
      dmsg ("marshaling encode_struct detected error");
      return CORBA::TypeCode::TRAVERSE_STOP;
    }
}

// encode unions
CORBA::TypeCode::traverse_status
TAO_Marshal_Union::encode (CORBA::TypeCode_ptr tc,
			   const void *data,
			   const void *data2,
			   void *context,
			   CORBA::Environment &env)
{
  CDR *stream = (CDR *) context;  // context is the CDR stream

  CORBA::TypeCode_ptr discrim_tc = tc->discriminator_type (env);
  // get the discriminator type

  if (env.exception () == 0)
    {
      CORBA::TypeCode_ptr member_tc;
      CORBA::Any_ptr member_label;
      CORBA::ULong discrim_size_with_pad;
      const void *discrim_val;
      CORBA::ULong member_count;
      CORBA::Long default_index;
      CORBA::TypeCode_ptr default_tc = 0;
      CORBA::Boolean discrim_matched = CORBA::B_FALSE;

      // encode the discriminator value
      CORBA::TypeCode::traverse_status retval =
	stream->encode (discrim_tc, data, data2, env);

      if (retval == CORBA::TypeCode::TRAVERSE_CONTINUE)
        {
          discrim_size_with_pad = tc->TAO_discrim_pad_size (env);
          if (env.exception () == 0)
            {
              discrim_val = data; // save the pointer to the discriminator
				  // value
              // 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 (env);
              if (env.exception () == 0)
                {
		  // get the member count
                  member_count = tc->member_count (env);
                  if (env.exception () == 0)
                    {
                      // 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 (int i = 0; member_count-- != 0; i++)
                        {
                          member_label = tc->member_label (i, env);
                          if (env.exception () == 0)
                            {
                              // do the matching
                              switch (member_label->type ()->kind (env))
                                {
                                case CORBA::tk_short:
                                case CORBA::tk_ushort:
                                  if (*(CORBA::Short *) member_label->value () == *(CORBA::Short *) discrim_val)
				    discrim_matched = CORBA::B_TRUE;
                                  break;
                                case CORBA::tk_long:
                                case CORBA::tk_ulong:
                                case CORBA::tk_enum:
                                  if (*(CORBA::ULong *) member_label->value () == *(CORBA::ULong *) discrim_val)
				    discrim_matched = CORBA::B_TRUE;
                                  break;
                                case CORBA::tk_char:
                                  if (*(CORBA::Char *) member_label->value () == *(CORBA::Char *) discrim_val)
				    discrim_matched = CORBA::B_TRUE;
                                  break;
                                case CORBA::tk_wchar:
                                  if (*(CORBA::WChar *) member_label->value () == *(CORBA::WChar *) discrim_val)
				    discrim_matched = CORBA::B_TRUE;
                                  break;
                                case CORBA::tk_boolean:
                                  if (*(CORBA::Boolean *) member_label->value () == *(CORBA::Boolean *) discrim_val)
				    discrim_matched = CORBA::B_TRUE;
                                  break;
                                default:
                                  env.exception (new CORBA::BAD_TYPECODE (CORBA::COMPLETED_NO));
                                  dmsg ("Union::encode - Bad discriminant type");
                                  return CORBA::TypeCode::TRAVERSE_STOP;
                                }// end of switch

                              // get the member typecode
                              member_tc = tc->member_type (i, env);
                              if (env.exception () == 0)
                                {
                                  if (default_index >= 0 && default_index-- == 0)
                                    {
                                      // have we reached the default label?, if so,
                                      // save a handle to the typecode for the default
                                      default_tc = member_tc;
                                    }
                                  if (discrim_matched)
				    // marshal according to the matched typecode
				    return stream->encode (member_tc, data,
							   data2, env);
                                }
                              else // error getting member type
                                {
                                  env.exception (new CORBA::BAD_TYPECODE (CORBA::COMPLETED_NO));
                                  dmsg1 ("Union::encode - error getting member type:%d",i);
                                  return CORBA::TypeCode::TRAVERSE_STOP;
                                }

                            }
                          else // error getting member label
                            {
                              env.exception (new CORBA::BAD_TYPECODE (CORBA::COMPLETED_NO));
                              dmsg1 ("Union::encode - error member label : %d", i);
                              return CORBA::TypeCode::TRAVERSE_STOP;
                            }
                        } // end of while
                      // we are here only if there was no match
                      if (default_tc)
			return stream->encode (default_tc, data, data2, env);
                      else
			{
			  env.exception (new CORBA::MARSHAL (CORBA::COMPLETED_NO));
			  dmsg ("Union::encode - failed. No match and no default case");
			  return CORBA::TypeCode::TRAVERSE_STOP;
			}
                    }
                  else // error getting member count
                    {
                      env.exception (new CORBA::MARSHAL (CORBA::COMPLETED_NO));
                      dmsg ("Union::encode - error getting member count");
                      return CORBA::TypeCode::TRAVERSE_STOP;
                    }
                }
              else // error getting default index
                {
                  env.exception (new CORBA::BAD_TYPECODE (CORBA::COMPLETED_NO));
                  dmsg ("Union::encode - error getting default used");
                  return CORBA::TypeCode::TRAVERSE_STOP;
                }
            }
          else // error getting discrim_pad_size
            {
              env.exception (new CORBA::BAD_TYPECODE (CORBA::COMPLETED_NO));
              dmsg ("Union::encode - error getting discrim padded size");
              return CORBA::TypeCode::TRAVERSE_STOP;
            }
        }
      else // error encoding discriminant
        {
          env.exception (new CORBA::MARSHAL (CORBA::COMPLETED_MAYBE));
          dmsg ("Union::encode - error encoding discriminant");
          return CORBA::TypeCode::TRAVERSE_STOP;
        }
    }
  else // error getting the discriminant
    {
      env.exception (new CORBA::BAD_TYPECODE (CORBA::COMPLETED_NO));
      dmsg ("Union::encode - error getting the discriminant typecode");
      return CORBA::TypeCode::TRAVERSE_STOP;
    }
}

// encode string
CORBA::TypeCode::traverse_status
TAO_Marshal_String::encode (CORBA::TypeCode_ptr tc,
			    const void *data,
			    const void *,
			    void *context,
			    CORBA::Environment &env)
{
  CORBA::Boolean continue_encoding = CORBA::B_TRUE;
  CDR *stream = (CDR *) context;  // context is the CDR stream
  CORBA::String str = *(CORBA::String *) data;

  // Be nice to programmers: treat nulls as empty strings not
  // errors. (OMG-IDL supports languages that don't use the C/C++
  // notion of null v. empty strings; nulls aren't part of the OMG-IDL
  // string model.)
  if (str != 0)
    {
      // Verify string satisfies bounds requirements.  We're not so
      // permissive as to send messages violating the interface spec
      // by having excessively long strings!
      CORBA::ULong bounds = tc->length (env);

      if (env.exception () == 0)
        {
          // get the actual length of the string
	  CORBA::ULong len = ACE_OS::strlen ((char *) str);

          // if it is an unbounded string or if the length is less
          // than the bounds for an unbounded string
          if (bounds == 0 || len <= bounds)
            {

              // Encode the string, followed by a NUL character.

              for (continue_encoding = stream->put_ulong (len + 1); // length +
								    // 1 for
								    // the NULL
								    // character
                   continue_encoding != CORBA::B_FALSE && *str;
                   continue_encoding = stream->put_char (*str++))
                continue;
	      // put a NULL terminating character
              stream->put_char (0);
              return CORBA::TypeCode::TRAVERSE_CONTINUE;
            }
          else
	    return CORBA::TypeCode::TRAVERSE_STOP;
        }
      else
	return CORBA::TypeCode::TRAVERSE_STOP;
    }
  else
    {
      // empty string
      stream->put_ulong (1);
      stream->put_char (0);
      return CORBA::TypeCode::TRAVERSE_CONTINUE;
    }
}

// encode sequence
CORBA::TypeCode::traverse_status
TAO_Marshal_Sequence::encode (CORBA::TypeCode_ptr tc,
                              const void *data,
                              const void *,
                              void *context,
                              CORBA::Environment &env)
{
  CORBA::Boolean continue_encoding = CORBA::B_TRUE;
  CDR *stream = (CDR *) context;
  CORBA::OctetSeq *seq = (CORBA::OctetSeq *) data;
  CORBA::TypeCode::traverse_status retval =
    CORBA::TypeCode::TRAVERSE_CONTINUE;  // return status
  CORBA::TypeCode_ptr    tc2;  // typecode of the element
  size_t  size; // size of element
  CORBA::ULong  len = seq ? seq->length : 0;
  char *value;

  // First marshal the sequence length, verifying that it's within the
  // sequence bounds ...

  if (len > 0)
    {
      // retrieve the bounds of the sequence
      CORBA::ULong bounds = tc->length (env);
      if (env.exception () == 0)
        {
          // encode only if it is an unbounded sequence or if length is
          // less/equal to the bounds
          if (bounds == 0 || len <= bounds)
            {
              bounds = len;  // number of times you encode
              continue_encoding = stream->put_ulong (seq->length);
              if (continue_encoding && seq->length != 0)
                {
                  // get element typecode
                  tc2 = tc->content_type (env);
                  if (env.exception () == 0)
                    {
                      size = tc2->size (env);
                      if (env.exception () == 0)
                        {
                          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
                              while (bounds-- && continue_encoding == CORBA::B_TRUE)
                                {
                                  continue_encoding = stream->put_short (*(CORBA::Short *) value);
                                  value += size;
                                }
                              //                              CORBA::release (tc2);
                              if (continue_encoding == CORBA::B_TRUE)
				return CORBA::TypeCode::TRAVERSE_CONTINUE;
                              break;
                            case CORBA::tk_long:
                            case CORBA::tk_ulong:
                            case CORBA::tk_float:
                              // For primitives, compute the size only once
                              while (bounds-- && continue_encoding == CORBA::B_TRUE)
                                {
                                  continue_encoding = stream->put_long (*(CORBA::Long *) value);
                                  value += size;
                                }
                              //                              CORBA::release (tc2);
                              if (continue_encoding == CORBA::B_TRUE)
				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
                              while (bounds-- && continue_encoding == CORBA::B_TRUE)
                                {
                                  continue_encoding = stream->put_longlong (*(CORBA::LongLong *) value);
                                  value += size;
                                }
                              //                              CORBA::release (tc2);
                              if (continue_encoding == CORBA::B_TRUE)
				return CORBA::TypeCode::TRAVERSE_CONTINUE;
                              break;
                            case CORBA::tk_boolean:
                              // For primitives, compute the size only once
                              while (bounds-- && continue_encoding == CORBA::B_TRUE)
                                {
                                  continue_encoding = stream->put_boolean (*(CORBA::Boolean *) value);
                                  value += size;
                                }
                              //                              CORBA::release (tc2);
                              if (continue_encoding == CORBA::B_TRUE)
				return CORBA::TypeCode::TRAVERSE_CONTINUE;
                              break;
                            case CORBA::tk_char:
                            case CORBA::tk_octet:
                              // For primitives, compute the size only once
                              while (bounds-- && continue_encoding == CORBA::B_TRUE)
                                {
                                  continue_encoding = stream->put_char (*(CORBA::Char *) value);
                                  value += size;
                                }
                              //                              CORBA::release (tc2);
                              if (continue_encoding == CORBA::B_TRUE)
				return CORBA::TypeCode::TRAVERSE_CONTINUE;
                              break;
                            case CORBA::tk_longdouble:
                              // For primitives, compute the size only once
                              while (bounds-- && continue_encoding == CORBA::B_TRUE)
                                {
                                  continue_encoding = stream->put_longdouble (*(CORBA::LongDouble *) value);
                                  value += size;
                                }
                              //                              CORBA::release (tc2);
                              if (continue_encoding == CORBA::B_TRUE)
				return CORBA::TypeCode::TRAVERSE_CONTINUE;
                              break;
                            case CORBA::tk_wchar:
                              // For primitives, compute the size only once
                              while (bounds-- && continue_encoding == CORBA::B_TRUE)
                                {
                                  continue_encoding = stream->put_wchar (*(CORBA::WChar *) value);
                                  value += size;
                                }
                              //                              CORBA::release (tc2);
                              if (continue_encoding == CORBA::B_TRUE)
				return CORBA::TypeCode::TRAVERSE_CONTINUE;
                              break;
                            case CORBA::tk_enum:
                              // For primitives, compute the size only once
                              while (bounds-- && continue_encoding == CORBA::B_TRUE)
                                {
                                  continue_encoding = stream->put_long (*(CORBA::Long *) value);
                                  value += size;
                                }
                              //                              CORBA::release (tc2);
                              if (continue_encoding == CORBA::B_TRUE)
				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->encode (tc2, value, 0, env);
                                  value += size;
                                }
                              //                              CORBA::release (tc2);
                              if (retval == CORBA::TypeCode::TRAVERSE_CONTINUE)
                                return CORBA::TypeCode::TRAVERSE_CONTINUE;
                              break;
                            default:
                              break;
                            } // end of switch
                        } // no exception computing size
                    } // no exception computing content type
                } // seq length not 0
            } // within bounds or unbounded
        } // no exception computing bounds
    } // length is > 0
  else
    {
      // length is 0, encode it
      continue_encoding = stream->put_ulong (len);
      if (continue_encoding == CORBA::B_TRUE)
        return CORBA::TypeCode::TRAVERSE_CONTINUE;

    }
  // error exit
  env.exception (new CORBA::MARSHAL (CORBA::COMPLETED_NO));
  dmsg ("marshaling TAO_Marshal_Sequence::encode detected error");
  return CORBA::TypeCode::TRAVERSE_STOP;
}

// encode array
CORBA::TypeCode::traverse_status
TAO_Marshal_Array::encode (CORBA::TypeCode_ptr tc,
                           const void *data,
                           const void *,
                           void *context,
                           CORBA::Environment &env)
{
  CORBA::Boolean continue_encoding = CORBA::B_TRUE;
  CDR *stream = (CDR *) context;
  CORBA::TypeCode::traverse_status retval =
    CORBA::TypeCode::TRAVERSE_CONTINUE;  // return status
  size_t  size; // size of element
  CORBA::ULong  bounds;
  char *value = (char *) data;

  // retrieve the bounds of the array
  bounds = tc->length (env);
  if (env.exception () == 0)
    {
      // get element typecode.
      CORBA::TypeCode_ptr tc2 = tc->content_type (env);

      if (env.exception () == 0)
        {
          size = tc2->size (env);
          if (env.exception () == 0)
            {
              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
                  while (bounds-- && continue_encoding == CORBA::B_TRUE)
                    {
                      continue_encoding = stream->put_short (*(CORBA::Short *) value);
                      value += size;
                    }
                  //              CORBA::release (tc2);
                  if (continue_encoding == CORBA::B_TRUE)
		    return CORBA::TypeCode::TRAVERSE_CONTINUE;
                  break;
                case CORBA::tk_long:
                case CORBA::tk_ulong:
                case CORBA::tk_float:
                  // For primitives, compute the size only once
                  while (bounds-- && continue_encoding == CORBA::B_TRUE)
                    {
                      continue_encoding = stream->put_long (*(CORBA::Long *) value);
                      value += size;
                    }
                  //              CORBA::release (tc2);
                  if (continue_encoding == CORBA::B_TRUE)
		    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
                  while (bounds-- && continue_encoding == CORBA::B_TRUE)
                    {
                      continue_encoding = stream->put_longlong (*(CORBA::LongLong *) value);
                      value += size;
                    }
                  //              CORBA::release (tc2);
                  if (continue_encoding == CORBA::B_TRUE)
		    return CORBA::TypeCode::TRAVERSE_CONTINUE;
                  break;
                case CORBA::tk_boolean:
                  // For primitives, compute the size only once
                  while (bounds-- && continue_encoding == CORBA::B_TRUE)
                    {
                      continue_encoding = stream->put_boolean (*(CORBA::Boolean *) value);
                      value += size;
                    }
                  //              CORBA::release (tc2);
                  if (continue_encoding == CORBA::B_TRUE)
		    return CORBA::TypeCode::TRAVERSE_CONTINUE;
                  break;
                case CORBA::tk_char:
                case CORBA::tk_octet:
                  // For primitives, compute the size only once
                  while (bounds-- && continue_encoding == CORBA::B_TRUE)
                    {
                      continue_encoding = stream->put_char (*(CORBA::Char *) value);
                      value += size;
                    }
                  //              CORBA::release (tc2);
                  if (continue_encoding == CORBA::B_TRUE)
		    return CORBA::TypeCode::TRAVERSE_CONTINUE;
                  break;
                case CORBA::tk_longdouble:
                  // For primitives, compute the size only once
                  while (bounds-- && continue_encoding == CORBA::B_TRUE)
                    {
                      continue_encoding = stream->put_longdouble (*(CORBA::LongDouble *) value);
                      value += size;
                    }
                  //              CORBA::release (tc2);
                  if (continue_encoding == CORBA::B_TRUE)
                    {
                      return CORBA::TypeCode::TRAVERSE_CONTINUE;
                    }
                  break;
                case CORBA::tk_wchar:
                  // For primitives, compute the size only once
                  while (bounds-- && continue_encoding == CORBA::B_TRUE)
                    {
                      continue_encoding = stream->put_wchar (*(CORBA::WChar *) value);
                      value += size;
                    }
                  //              CORBA::release (tc2);
                  if (continue_encoding == CORBA::B_TRUE)
		    return CORBA::TypeCode::TRAVERSE_CONTINUE;
                  break;
                case CORBA::tk_enum:
                  // For primitives, compute the size only once
                  while (bounds-- && continue_encoding == CORBA::B_TRUE)
                    {
                      continue_encoding = stream->put_long (*(CORBA::Long *) value);
                      value += size;
                    }
                  //              CORBA::release (tc2);
                  if (continue_encoding == CORBA::B_TRUE)
		    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->encode (tc2, value, 0, env);
                      value += size;
                    }
                  //              CORBA::release (tc2);
                  if (retval == CORBA::TypeCode::TRAVERSE_CONTINUE)
                    return CORBA::TypeCode::TRAVERSE_CONTINUE;
                  break;
                default:
                  break;
                } // end of switch
            } // no exception computing size
        } // no exception computing content type
    } // no exception computing bounds
  // error exit
  env.exception (new CORBA::MARSHAL (CORBA::COMPLETED_NO));
  dmsg ("marshaling TAO_Marshal_Sequence::encode detected error");
  return CORBA::TypeCode::TRAVERSE_STOP;
}

CORBA::TypeCode::traverse_status
TAO_Marshal_Alias::encode (CORBA::TypeCode_ptr tc,
			   const void *data,
			   const void *,
			   void *context,
			   CORBA::Environment &env)
{
  CORBA::TypeCode_ptr tc2;  // typecode of the aliased type
  CORBA::Boolean continue_encoding = CORBA::B_TRUE;
  CDR *stream = (CDR *) context;  // context is the CDR stream
  CORBA::TypeCode::traverse_status   retval =
    CORBA::TypeCode::TRAVERSE_CONTINUE; // status of encode operation
  char *value = (char *) data;

  tc2 = tc->content_type (env);
  if (env.exception () == 0)
    {
    // 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_encoding = stream->put_short (*(CORBA::Short *) value);
        break;
      case CORBA::tk_long:
      case CORBA::tk_ulong:
      case CORBA::tk_float:
      case CORBA::tk_enum:
        continue_encoding = stream->put_long (*(CORBA::Long *) value);
        break;
      case CORBA::tk_double:
      case CORBA::tk_longlong:
      case CORBA::tk_ulonglong:
        continue_encoding = stream->put_longlong (*(CORBA::LongLong *) value);
        break;
      case CORBA::tk_boolean:
        continue_encoding = stream->put_boolean (*(CORBA::Boolean *) value);
        break;
      case CORBA::tk_char:
      case CORBA::tk_octet:
        continue_encoding = stream->put_char (*(CORBA::Char *) value);
        break;
      case CORBA::tk_longdouble:
        continue_encoding = stream->put_longdouble (*(CORBA::LongDouble *) value);
        break;
      case CORBA::tk_wchar:
        continue_encoding = stream->put_wchar (*(wchar_t *) value);
        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->encode (tc2, value, 0, env);
        break;
      default:
        // anything else is an error
        retval = CORBA::TypeCode::TRAVERSE_STOP;
      }
  }
  //  tc2->Release ();
  if (retval == CORBA::TypeCode::TRAVERSE_CONTINUE
      && continue_encoding == CORBA::B_TRUE)
    return CORBA::TypeCode::TRAVERSE_CONTINUE;
  else
    {
      env.exception (new CORBA::MARSHAL (CORBA::COMPLETED_MAYBE));
      dmsg ("TAO_Marshal_Alias::encode detected error");
      return CORBA::TypeCode::TRAVERSE_STOP;
    }
}


// encode exception
CORBA::TypeCode::traverse_status
TAO_Marshal_Except::encode (CORBA::TypeCode_ptr tc,
			    const void *data,
			    const void *,
			    void *context,
			    CORBA::Environment &env)
{
  CORBA::TypeCode::traverse_status retval = CORBA::TypeCode::TRAVERSE_CONTINUE;
  CORBA::Boolean continue_encoding = CORBA::B_TRUE;

  if (env.exception () == 0)
    {
      CDR *stream = (CDR *) context;
      CORBA::TypeCode_ptr param;
      CORBA::Long size, alignment;

      data = (char *) data + sizeof (CORBA::Exception);

      int member_count = tc->member_count (env);

      for (int i = 0;
	   i < member_count && retval == CORBA::TypeCode::TRAVERSE_CONTINUE
	     && continue_encoding == CORBA::B_TRUE;
	   i++)
	{
	  param = tc->member_type (i, env);
	  if (env.exception () == 0)
	    {
	      size = param->size (env);
	      if (env.exception () == 0)
		{
		  alignment = param->alignment (env);
		  if (env.exception () == 0)
		    {
		      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_encoding = stream->put_short (*(CORBA::Short *) data);
			break;
		      case CORBA::tk_long:
		      case CORBA::tk_ulong:
		      case CORBA::tk_float:
		      case CORBA::tk_enum:
			continue_encoding = stream->put_long (*(CORBA::Long *) data);
			break;
		      case CORBA::tk_double:
		      case CORBA::tk_longlong:
		      case CORBA::tk_ulonglong:
			continue_encoding = stream->put_longlong (*(CORBA::LongLong *) data);
			break;
		      case CORBA::tk_boolean:
			continue_encoding = stream->put_boolean (*(CORBA::Boolean *) data);
			break;
		      case CORBA::tk_char:
		      case CORBA::tk_octet:
			continue_encoding = stream->put_char (*(CORBA::Char *) data);
			break;
		      case CORBA::tk_longdouble:
			continue_encoding = stream->put_longdouble (*(CORBA::LongDouble *) data);
			break;
		      case CORBA::tk_wchar:
			continue_encoding = stream->put_wchar (*(wchar_t *) 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->encode (param, data, 0, env);
			break;
		      default:
			break;
		      }
		      data = (char *) data + size;
		    }
		  else
		    return CORBA::TypeCode::TRAVERSE_STOP;
		}
	      else
		return CORBA::TypeCode::TRAVERSE_STOP;
	    }
	  else
	    return CORBA::TypeCode::TRAVERSE_STOP;
	}
    }
  else
    return CORBA::TypeCode::TRAVERSE_STOP;

  if (retval == CORBA::TypeCode::TRAVERSE_CONTINUE
      && continue_encoding == CORBA::B_TRUE)
    return CORBA::TypeCode::TRAVERSE_CONTINUE;
  else
    {
      env.exception (new CORBA::MARSHAL (CORBA::COMPLETED_MAYBE));
      dmsg ("TAO_Marshal_Except detected error");
      return CORBA::TypeCode::TRAVERSE_STOP;
    }
}


// encode wstring
CORBA::TypeCode::traverse_status
TAO_Marshal_WString::encode (CORBA::TypeCode_ptr tc,
			     const void *data,
			     const void *,
			     void *context,
			     CORBA::Environment &env)
{
  CORBA::Boolean continue_encoding = CORBA::B_TRUE;

  CORBA::WChar *str = *(CORBA::WChar **) data;
  CDR *stream = (CDR *) context;  // context is the CDR stream

  // Be nice to programmers: treat nulls as empty strings not
  // errors. (OMG-IDL supports languages that don't use the
  // C/C++ notion of null v. empty strings; nulls aren't part of
  // the OMG-IDL string model.)
  if (str != 0)
    {
      // Verify string satisfies bounds requirements.  We're not so
      // permissive as to send messages violating the interface spec
      // by having excessively long strings!
      CORBA::ULong bounds = tc->length (env);

      if (env.exception () == 0)
        {
          // get the actual length of the string
	  CORBA::ULong len = ACE_OS::strlen (str);

          // if it is an unbounded string or if the length is less than the
          // bounds for an unbounded string
          if ((bounds == 0) || (len <= bounds))
            {

              // Encode the string, followed by a NUL character.

              for (continue_encoding = stream->put_ulong (len + 1);
                   continue_encoding != CORBA::B_FALSE && *str;
                   continue_encoding = stream->put_wchar (*str++))
                continue;

              stream->put_wchar (0);
              return CORBA::TypeCode::TRAVERSE_CONTINUE;
            }
          else
	    return CORBA::TypeCode::TRAVERSE_STOP;
        }
      else
	return CORBA::TypeCode::TRAVERSE_STOP;
    }
  else
    {
      // empty string
      stream->put_ulong (1);
      stream->put_wchar (0);
      return CORBA::TypeCode::TRAVERSE_CONTINUE;
    }
}