home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / fns101.zip / Add-Ons / Fnorb / src / cdr.c < prev    next >
C/C++ Source or Header  |  1999-06-28  |  26KB  |  830 lines

  1. /***************************************************************
  2.  
  3.   Copyright (C) DSTC Pty Ltd (ACN 052 372 577) 1997, 1998, 1999
  4.   Unpublished work.  All Rights Reserved.
  5.  
  6.   The software contained on this media is the property of the
  7.   DSTC Pty Ltd.  Use of this software is strictly in accordance
  8.   with the license agreement in the accompanying LICENSE.HTML
  9.   file.  If your distribution of this software does not contain
  10.   a LICENSE.HTML file then you have no rights to use this
  11.   software in any manner and should contact DSTC at the address
  12.   below to determine an appropriate licensing arrangement.
  13.  
  14.      DSTC Pty Ltd
  15.      Level 7, GP South
  16.      Staff House Road
  17.      University of Queensland
  18.      St Lucia, 4072
  19.      Australia
  20.      Tel: +61 7 3365 4310
  21.      Fax: +61 7 3365 4311
  22.      Email: enquiries@dstc.edu.au
  23.  
  24.   This software is being provided "AS IS" without warranty of
  25.   any kind.  In no event shall DSTC Pty Ltd be liable for
  26.   damage of any kind arising out of or in connection with
  27.   the use or performance of this software.
  28.  
  29.  
  30.   Project:      Fnorb
  31.   File:         $Source: /units/arch/src/Fnorb-1.0/src/RCS/cdr.c,v $
  32.   Version:      @(#)$RCSfile: cdr.c,v $ $Revision: 1.9 $
  33.  
  34.  
  35.   Description:    Marshalling/unmarshalling functions for the CORBA Common Data
  36.                 Representation (CDR).
  37.  
  38. ****************************************************************/
  39.  
  40. #if !defined(lint)
  41. static const char rcsid[] = "@(#)$RCSfile: cdr.c,v $ $Revision: 1.9 $";
  42. #endif
  43.  
  44. /*
  45.  * Standard library includes (e.g., stdio)
  46.  */
  47. #include <stdlib.h>                /* malloc...        */
  48. #include <string.h>                /* memcpy...        */
  49.  
  50. /*
  51.  * Application library includes (e.g., X, DCE)
  52.  */
  53.  
  54. /*
  55.  * DSTC library includes (e.g., dstc_stdlib.h)
  56.  */
  57.  
  58. /*
  59.  * Project library includes              
  60.  */
  61. #include "fnorb.h"
  62.  
  63. /*
  64.  * Local file includes
  65.  */
  66. #include "cdr.h"
  67.  
  68. /* Useful macros! */
  69. #define MEMCPY(src, target, n) (void)memcpy((void*)(src), (void*)(target), (n))
  70. #define SWAP(byte_order) ((byte_order) != HOST_BYTE_ORDER)
  71.  
  72.  
  73. /*
  74.  * The 'cdr_count_' functions calculate the space required to marshal data
  75.  * onto an octet stream (including any padding that is required to abide by
  76.  * the CDR alignment rules).
  77.  */
  78. unsigned long
  79. cdr_count_boolean(CORBA_octet* os,            /* Octet stream base.    */
  80.           CORBA_octet** p,        /* Current position.    */
  81.           CORBA_boolean byte_order,    /* Byte-order.        */
  82.           CORBA_boolean val)        /* Value to marshal.    */
  83. {
  84.     /* N.B. The parameters 'os', 'byte_order' and 'val',  are not used     */
  85.     /* ---- in this function, but are left in to keep the interface to    */
  86.     /*      all of the 'cdr_' functions consistent.            */
  87.     (*p)++;
  88.  
  89.     return 1; /* Always 1 as no alignment required. */
  90. }
  91.  
  92. unsigned long
  93. cdr_count_char(CORBA_octet* os,            /* Octet stream base.    */
  94.            CORBA_octet** p,            /* Current position.    */
  95.            CORBA_boolean byte_order,    /* Byte-order.        */
  96.            CORBA_char val)            /* Value to marshal.    */
  97. {
  98.     /* N.B. The parameters 'os', 'byte_order' and 'val',  are not used     */
  99.     /* ---- in this function, but are left in to keep the interface to    */
  100.     /*      all of the 'cdr_' functions consistent.            */
  101.     (*p)++;
  102.  
  103.     return 1; /* Always 1 as no alignment required. */
  104. }
  105.  
  106. unsigned long
  107. cdr_count_octet(CORBA_octet* os,            /* Octet stream base.    */
  108.         CORBA_octet** p,        /* Current position.    */
  109.         CORBA_boolean byte_order,    /* Byte-order.        */
  110.         CORBA_octet val)        /* Value to marshal.    */
  111. {
  112.     /* N.B. The parameters 'os', 'byte_order' and 'val',  are not used     */
  113.     /* ---- in this function, but are left in to keep the interface to    */
  114.     /*      all of the 'cdr_' functions consistent.            */
  115.     (*p)++;
  116.  
  117.     return 1; /* Always 1 as no alignment required. */
  118. }
  119.  
  120. unsigned long
  121. cdr_count_unsigned_short(CORBA_octet* os,      /* Octet stream base.    */
  122.              CORBA_octet** p,      /* Current position.    */
  123.              CORBA_boolean byte_order,/* Byte-order.    */
  124.              CORBA_unsigned_short val)/* Value to marshal.    */
  125. {
  126.     /* N.B. The parameters 'byte_order' and 'val',  are not used in     */
  127.     /* ---- this function, but are left in to keep the interface to all */
  128.     /*      of the 'cdr_' functions consistent.                */
  129.  
  130.     CORBA_octet* start = *p;
  131.  
  132.     /* Within the octet stream the value must be aligned on a word    */
  133.     /* boundary that is a multiple of its size.                */
  134.     while ((unsigned long) ((*p) - os) % 2 != 0) {
  135.     (*p)++;
  136.     }
  137.     (*p) += 2;
  138.  
  139.     return (*p) - start;
  140. }
  141.  
  142. unsigned long
  143. cdr_count_short(CORBA_octet* os,        /* Octet stream base.    */
  144.         CORBA_octet** p,        /* Current position.    */
  145.         CORBA_boolean byte_order,    /* Byte-order.        */
  146.         CORBA_short val)        /* Value to marshal.    */
  147. {
  148.     return cdr_count_unsigned_short(os, p, byte_order, val);
  149. }
  150.  
  151. unsigned long
  152. cdr_count_unsigned_long(CORBA_octet* os,     /* Octet stream base.    */
  153.             CORBA_octet** p,      /* Current position.    */
  154.             CORBA_boolean byte_order,/* Byte-order.        */
  155.             CORBA_unsigned_long val) /* Value to marshal.    */
  156. {
  157.     /* N.B. The parameters 'byte_order' and 'val',  are not used in     */
  158.     /* ---- this function, but are left in to keep the interface to all */
  159.     /*      of the 'cdr_' functions consistent.                */
  160.  
  161.     CORBA_octet* start = *p;
  162.  
  163.     /* Within the octet stream the value must be aligned on a word    */
  164.     /* boundary that is a multiple of its size.                */
  165.     while ((unsigned long) ((*p) - os) % 4 != 0) {
  166.     (*p)++;
  167.     }
  168.     (*p) += 4;
  169.  
  170.     return (*p) - start;
  171. }
  172.  
  173. unsigned long
  174. cdr_count_long(CORBA_octet* os,            /* Octet stream base.    */
  175.            CORBA_octet** p,            /* Current position.    */
  176.            CORBA_boolean byte_order,    /* Byte-order.        */
  177.            CORBA_long val)            /* Value to marshal.    */
  178. {
  179.     return cdr_count_unsigned_long(os, p, byte_order, val);
  180. }
  181.  
  182. unsigned long
  183. cdr_count_unsigned_longlong(CORBA_octet* os,            /* Octet stream base.*/
  184.                 CORBA_octet** p,        /* Current position. */
  185.                 CORBA_boolean byte_order,   /* Byte-order.       */
  186.                 CORBA_unsigned_longlong val)/* Value to marshal. */
  187. {
  188.     /* N.B. The parameters 'byte_order' and 'val',  are not used in     */
  189.     /* ---- this function, but are left in to keep the interface to all */
  190.     /*      of the 'cdr_' functions consistent.                */
  191.  
  192.     CORBA_octet* start = *p;
  193.  
  194.     /* Within the octet stream the value must be aligned on a word    */
  195.     /* boundary that is a multiple of its size.                */
  196.     while ((unsigned long) ((*p) - os) % 8 != 0) {
  197.     (*p)++;
  198.     }
  199.     (*p) += 8;
  200.  
  201.     return (*p) - start;
  202. }
  203.  
  204. unsigned long
  205. cdr_count_longlong(CORBA_octet* os,        /* Octet stream base.    */
  206.            CORBA_octet** p,        /* Current position.    */
  207.            CORBA_boolean byte_order,    /* Byte-order.        */
  208.            CORBA_longlong val)        /* Value to marshal.    */
  209. {
  210.     return cdr_count_unsigned_longlong(os, p, byte_order, val);
  211. }
  212.  
  213. unsigned long
  214. cdr_count_float(CORBA_octet* os,        /* Octet stream base.    */
  215.         CORBA_octet** p,        /* Current position.    */
  216.         CORBA_boolean byte_order,    /* Byte-order.        */
  217.         CORBA_float val)        /* Value to marshal.    */
  218. {
  219.     /* N.B. The parameters 'byte_order' and 'val',  are not used in     */
  220.     /* ---- this function, but are left in to keep the interface to all */
  221.     /*      of the 'cdr_' functions consistent.                */
  222.  
  223.     CORBA_octet* start = *p;
  224.  
  225.     /* Within the octet stream the value must be aligned on a word    */
  226.     /* boundary that is a multiple of its size.                */
  227.     while ((unsigned long) ((*p) - os) % 4 != 0) {
  228.     (*p)++;
  229.     }
  230.     (*p) += 4;
  231.  
  232.     return (*p) - start;
  233. }
  234.  
  235. unsigned long
  236. cdr_count_double(CORBA_octet* os,        /* Octet stream base.    */
  237.          CORBA_octet** p,        /* Current position.    */
  238.          CORBA_boolean byte_order,    /* Byte-order.        */
  239.          CORBA_double val)        /* Value to marshal.    */
  240. {
  241.     /* N.B. The parameters 'byte_order' and 'val',  are not used in     */
  242.     /* ---- this function, but are left in to keep the interface to all */
  243.     /*      of the 'cdr_' functions consistent.                */
  244.  
  245.     CORBA_octet* start = *p;
  246.  
  247.     /* Within the octet stream the value must be aligned on a word    */
  248.     /* boundary that is a multiple of its size.                */
  249.     while ((unsigned long) ((*p) - os) % 8 != 0) {
  250.     (*p)++;
  251.     }
  252.     (*p) += 8;
  253.  
  254.     return (*p) - start;
  255. }
  256.  
  257. unsigned long
  258. cdr_count_string(CORBA_octet* os,        /* Octet stream base.    */
  259.          CORBA_octet** p,        /* Current position.    */
  260.          CORBA_boolean byte_order,    /* Byte-order.        */
  261.          CORBA_char* val)        /* Value to marshal.    */
  262. {
  263.     CORBA_octet* start = *p;
  264.     CORBA_unsigned_long len = strlen((char*) val) + 1;
  265.  
  266.     /* Calculate the space required for the length of the string.    */
  267.     (void) cdr_count_unsigned_long(os, p, byte_order, len);
  268.  
  269.     /* Now add on the space for the string itself.            */
  270.     (*p) += len;
  271.  
  272.     return (*p) - start;
  273. }
  274.  
  275. unsigned long
  276. cdr_count_octets(CORBA_octet* os,        /* Octet stream base.    */
  277.          CORBA_octet** p,        /* Current position.    */
  278.          CORBA_boolean byte_order,    /* Byte-order.        */
  279.          CORBA_octet* val,        /* Value to marshal.    */
  280.          CORBA_unsigned_long len)    /* Number of octets.    */
  281. {
  282.     /* N.B. The parameter 'val' is not used in this function, but is      */
  283.     /* ---- left in to keep the interface to all of the 'cdr_'         */
  284.     /*      functions consistent.                    */
  285.  
  286.     CORBA_octet* start = *p;
  287.  
  288.     /* Calculate the space required for the length of the sequence.    */
  289.     (void) cdr_count_unsigned_long(os, p, byte_order, len);
  290.  
  291.     /* Now add on the space for the sequence itself.            */
  292.     (*p) += len;
  293.     
  294.     return (*p) - start;
  295. }
  296.  
  297. /*
  298.  * MARSHALLING functions.
  299.  *
  300.  * All of the functions defined in this section are 'assert friendly' i.e. they
  301.  * return NON-zero for success and zero for failure.  The return value is
  302.  * actually the number of octets that were required to marshal the data
  303.  * (including any padding).  This may or may not be useful ;^)
  304.  */
  305. unsigned long
  306. cdr_put_boolean(CORBA_octet* os,            /* Octet stream base.    */
  307.         CORBA_octet** p,        /* Current position.    */
  308.         CORBA_boolean byte_order,    /* Byte-order.        */
  309.         CORBA_boolean val)        /* Value to marshal.    */
  310. {
  311.     /* N.B. The parameters 'os' and 'byte_order',  are not used in this    */
  312.     /* ---- function, but are left in to keep the interface to all of     */
  313.     /*      the 'cdr_' functions consistent.            */
  314.     (*p)[0] = val;
  315.     (*p)++;
  316.  
  317.     return 1; /* Always 1 as no alignment required. */
  318. }
  319.  
  320. unsigned long
  321. cdr_put_char(CORBA_octet* os,            /* Octet stream base.    */
  322.          CORBA_octet** p,            /* Current position.    */
  323.          CORBA_boolean byte_order,        /* Byte-order.        */
  324.          CORBA_char val)            /* Value to marshal.    */
  325. {
  326.     /* N.B. The parameters 'os' and 'byte_order',  are not used in this    */
  327.     /* ---- function, but are left in to keep the interface to all of     */
  328.     /*      the 'cdr_' functions consistent.            */
  329.     (*p)[0] = val;
  330.     (*p)++;
  331.  
  332.     return 1; /* Always 1 as no alignment required. */
  333. }
  334.  
  335. unsigned long
  336. cdr_put_octet(CORBA_octet* os,            /* Octet stream base.    */
  337.           CORBA_octet** p,            /* Current position.    */
  338.           CORBA_boolean byte_order,        /* Byte-order.        */
  339.           CORBA_octet val)            /* Value to marshal.    */
  340. {
  341.     /* N.B. The parameters 'os' and 'byte_order',  are not used in this    */
  342.     /* ---- function, but are left in to keep the interface to all of     */
  343.     /*      the 'cdr_' functions consistent.            */
  344.     (*p)[0] = val;
  345.     (*p)++;
  346.  
  347.     return 1; /* Always 1 as no alignment required. */
  348. }
  349.  
  350. unsigned long
  351. cdr_put_unsigned_short(CORBA_octet* os,         /* Octet stream base.    */
  352.                CORBA_octet** p,         /* Current position.    */
  353.                CORBA_boolean byte_order, /* Byte-order.        */
  354.                CORBA_unsigned_short val) /* Value to marshal.    */
  355. {
  356.     CORBA_octet* start = *p;
  357.  
  358.     /* Within the octet stream the value must be aligned on a word    */
  359.     /* boundary that is a multiple of its size.                */
  360.     while ((unsigned long) ((*p) - os) % 2 != 0) {
  361.     (*p)++;
  362.     }
  363.  
  364.     if (SWAP(byte_order)) {
  365.     (*p)[0] = ((CORBA_octet*) &val)[1];
  366.     (*p)[1] = ((CORBA_octet*) &val)[0];
  367.     }
  368.     else {
  369.     MEMCPY(*p, &val, 2);
  370.     }
  371.     (*p) += 2;
  372.  
  373.     return (*p) - start;
  374. }
  375.  
  376. unsigned long
  377. cdr_put_short(CORBA_octet* os,            /* Octet stream base.    */
  378.           CORBA_octet** p,            /* Current position.    */
  379.           CORBA_boolean byte_order,        /* Byte-order.        */
  380.           CORBA_short val)             /* Value to marshal.    */
  381. {
  382.     return cdr_put_unsigned_short(os,p, byte_order,(CORBA_unsigned_short) val);
  383. }
  384.  
  385. unsigned long
  386. cdr_put_unsigned_long(CORBA_octet* os,        /* Octet stream base.    */
  387.               CORBA_octet** p,        /* Current position.    */
  388.               CORBA_boolean byte_order,    /* Byte-order.        */
  389.               CORBA_unsigned_long val)    /* Value to marshal.    */
  390. {
  391.     CORBA_octet* start = *p;
  392.  
  393.     /* Within the octet stream the value must be aligned on a word    */
  394.     /* boundary that is a multiple of its size.                */
  395.     while ((unsigned long) ((*p) - os) % 4 != 0) {
  396.     (*p)++;
  397.     }
  398.  
  399.     if (SWAP(byte_order)) {
  400.      (*p)[0] = ((CORBA_octet*) &val)[3];
  401.      (*p)[1] = ((CORBA_octet*) &val)[2];
  402.      (*p)[2] = ((CORBA_octet*) &val)[1];
  403.      (*p)[3] = ((CORBA_octet*) &val)[0];
  404.     }
  405.     else {
  406.     MEMCPY(*p, &val, 4);
  407.     }
  408.     (*p) += 4;
  409.  
  410.     return (*p) - start;
  411. }
  412.  
  413. unsigned long
  414. cdr_put_long(CORBA_octet* os,            /* Octet stream base.    */
  415.          CORBA_octet** p,            /* Current position.    */
  416.          CORBA_boolean byte_order,        /* Byte-order.        */
  417.          CORBA_long val)             /* Value to marshal.    */
  418. {
  419.     return cdr_put_unsigned_long(os, p, byte_order, (CORBA_unsigned_long) val);
  420. }
  421.  
  422. unsigned long
  423. cdr_put_unsigned_longlong(CORBA_octet* os,             /* Octet stream base. */
  424.               CORBA_octet** p,           /* Current position.  */
  425.               CORBA_boolean byte_order,    /* Byte-order.         */
  426.               CORBA_unsigned_longlong val) /* Value to marshal.  */
  427. {
  428.     CORBA_octet* start = *p;
  429.  
  430.     /* Within the octet stream the value must be aligned on a word    */
  431.     /* boundary that is a multiple of its size.                */
  432.     while ((unsigned long) ((*p) - os) % 8 != 0) {
  433.     (*p)++;
  434.     }
  435.  
  436.     if (SWAP(byte_order)) {
  437.     (*p)[0] = (val.octets)[7];
  438.     (*p)[1] = (val.octets)[6];
  439.     (*p)[2] = (val.octets)[5];
  440.     (*p)[3] = (val.octets)[4];
  441.     (*p)[4] = (val.octets)[3];
  442.     (*p)[5] = (val.octets)[2];
  443.     (*p)[6] = (val.octets)[1];
  444.     (*p)[7] = (val.octets)[0];
  445.     }
  446.     else {
  447.     MEMCPY(*p, val.octets, 8);
  448.     }
  449.     (*p) += 8;
  450.  
  451.     return (*p) - start;
  452. }
  453.  
  454. unsigned long
  455. cdr_put_longlong(CORBA_octet* os,        /* Octet stream base.    */
  456.          CORBA_octet** p,        /* Current position.    */
  457.          CORBA_boolean byte_order,    /* Byte-order.        */
  458.          CORBA_longlong val)         /* Value to marshal.    */
  459. {
  460.     return cdr_put_unsigned_longlong(os, p, byte_order, val);
  461. }
  462.  
  463. unsigned long
  464. cdr_put_float(CORBA_octet* os,            /* Octet stream base.    */
  465.           CORBA_octet** p,            /* Current position.    */
  466.           CORBA_boolean byte_order,        /* Byte-order.        */
  467.           CORBA_float val)            /* Value to marshal.    */
  468. {
  469.     CORBA_octet* start = *p;
  470.  
  471.     /* Within the octet stream the value must be aligned on a word    */
  472.     /* boundary that is a multiple of its size.                */
  473.     while ((unsigned long) ((*p) - os) % 4 != 0) {
  474.     (*p)++;
  475.     }
  476.  
  477.     if (SWAP(byte_order)) {
  478.     (*p)[0] = ((CORBA_octet*) &val)[3];
  479.     (*p)[1] = ((CORBA_octet*) &val)[2];
  480.     (*p)[2] = ((CORBA_octet*) &val)[1];
  481.     (*p)[3] = ((CORBA_octet*) &val)[0];
  482.     }
  483.     else {
  484.     MEMCPY(*p, &val, 4);
  485.     }
  486.     (*p) += 4;
  487.  
  488.     return (*p) - start;
  489. }
  490.  
  491. unsigned long
  492. cdr_put_double(CORBA_octet* os,            /* Octet stream base.    */
  493.            CORBA_octet** p,            /* Current position.    */
  494.            CORBA_boolean byte_order,    /* Byte-order.        */
  495.            CORBA_double val)        /* Value to marshal.    */
  496. {
  497.     CORBA_octet* start = *p;
  498.  
  499.     /* Within the octet stream the value must be aligned on a word    */
  500.     /* boundary that is a multiple of its size.                */
  501.     while ((unsigned long) ((*p) - os) % 8 != 0) {
  502.     (*p)++;
  503.     }
  504.  
  505.     if (SWAP(byte_order)) {
  506.     (*p)[0] = ((CORBA_octet*) &val)[7];
  507.     (*p)[1] = ((CORBA_octet*) &val)[6];
  508.     (*p)[2] = ((CORBA_octet*) &val)[5];
  509.     (*p)[3] = ((CORBA_octet*) &val)[4];
  510.     (*p)[4] = ((CORBA_octet*) &val)[3];
  511.     (*p)[5] = ((CORBA_octet*) &val)[2];
  512.     (*p)[6] = ((CORBA_octet*) &val)[1];
  513.     (*p)[7] = ((CORBA_octet*) &val)[0];
  514.     }
  515.     else {
  516.     MEMCPY(*p, &val, 8);
  517.     }
  518.     (*p) += 8;
  519.  
  520.     return (*p) - start;
  521. }
  522.  
  523. unsigned long
  524. cdr_put_string(CORBA_octet* os,            /* Octet stream base.    */
  525.            CORBA_octet** p,            /* Current position.    */
  526.            CORBA_boolean byte_order,    /* Byte-order.        */
  527.            CORBA_char* val)            /* Value to marshal.    */
  528. {
  529.     CORBA_octet* start = *p;
  530.     CORBA_unsigned_long len = strlen((char*) val) + 1;
  531.  
  532.     /* Marshal the length of the string (including the NULL!).        */
  533.     cdr_put_unsigned_long(os, p, byte_order, len);
  534.  
  535.     /* Now marshal the actual contents of the string.            */
  536.     MEMCPY(*p, val, len);
  537.     (*p) += len;
  538.     
  539.     return (*p) - start;
  540. }
  541.  
  542. unsigned long
  543. cdr_put_octets(CORBA_octet* os,            /* Octet stream base.    */
  544.            CORBA_octet** p,            /* Current position.    */
  545.            CORBA_boolean byte_order,    /* Byte-order.        */
  546.            CORBA_octet* val,        /* Value to marshal.    */
  547.            CORBA_unsigned_long len)        /* Number of octets.    */
  548. {
  549.     CORBA_octet* start = *p;
  550.  
  551.     /* Marshal the length of the sequence.                */
  552.     cdr_put_unsigned_long(os, p, byte_order, len);
  553.  
  554.     /* Now marshal the actual octets.                    */
  555.     MEMCPY(*p, val, len);
  556.     (*p) += len;
  557.  
  558.     return (*p) - start;
  559. }
  560.  
  561. /*
  562.  * UNMARSHALLING functions.
  563.  *
  564.  * All of the functions defined in this section are 'assert friendly' i.e. they
  565.  * return NON-zero for success and zero for failure.  The return value is
  566.  * actually the length of the data that was unmarshalled (NOT INCLUDING
  567.  * PADDING OR LENGTH WORDS).  This may or may not be useful ;^)
  568.  */
  569. unsigned long
  570. cdr_get_boolean(CORBA_octet* os,            /* Octet stream base.    */
  571.         CORBA_octet** p,        /* Current position.    */
  572.         CORBA_boolean byte_order,    /* Byte-order.        */
  573.         CORBA_boolean* val)        /* Unmarshalled value.    */
  574. {
  575.     /* N.B. The parameters 'os' and 'byte_order' are not used in this     */
  576.     /* ---- function, but are left in to keep the interface to all of    */
  577.     /*      the 'cdr_get' functions consistent.                */
  578.     *val = (CORBA_boolean) (*p)[0];
  579.     (*p)++;
  580.  
  581.     return 1;
  582. }
  583.  
  584. unsigned long
  585. cdr_get_char(CORBA_octet* os,            /* Octet stream base    */
  586.          CORBA_octet** p,            /* Current position.    */
  587.          CORBA_boolean byte_order,        /* Byte-order.        */
  588.          CORBA_char* val)            /* Unmarshalled value.    */
  589. {
  590.     /* N.B. The parameters 'os' and 'byte_order' are not used in this     */
  591.     /* ---- function, but are left in to keep the interface to all of    */
  592.     /*      the 'cdr_get' functions consistent.                */
  593.     *val = (CORBA_char) (*p)[0];
  594.     (*p)++;
  595.  
  596.     return 1;
  597. }
  598.  
  599. unsigned long
  600. cdr_get_octet(CORBA_octet* os,            /* Octet stream base.    */
  601.           CORBA_octet** p,            /* Current position.    */
  602.           CORBA_boolean byte_order,        /* Byte-order.        */
  603.           CORBA_octet* val)            /* Unmarshalled value.    */
  604. {
  605.     /* N.B. The parameters 'os' and 'byte_order' are not used in this     */
  606.     /* ---- function, but are left in to keep the interface to all of    */
  607.     /*      the 'cdr_get' functions consistent.                */
  608.     *val = (CORBA_octet) (*p)[0];
  609.     (*p)++;
  610.  
  611.     return 1;
  612. }
  613.  
  614. unsigned long
  615. cdr_get_unsigned_short(CORBA_octet* os,         /* Octet stream base.    */
  616.                CORBA_octet** p,         /* Current position.    */
  617.                CORBA_boolean byte_order, /* Byte-order.        */
  618.                CORBA_unsigned_short* val)/* Unmarshalled value.    */
  619. {
  620.     /* Within the octet stream the value must be aligned on a word    */
  621.     /* boundary that is a multiple of its size.                */
  622.     while ((unsigned long) ((*p) - os) % 2 != 0) {
  623.     (*p)++;
  624.     }
  625.  
  626.     if (SWAP(byte_order)) {
  627.     ((CORBA_octet*) val)[1] = (*p)[0];
  628.     ((CORBA_octet*) val)[0] = (*p)[1];
  629.     }
  630.     else {
  631.     MEMCPY(val, *p, 2);
  632.     }
  633.     (*p) += 2;
  634.  
  635.     return 2;
  636. }
  637.  
  638. unsigned long
  639. cdr_get_short(CORBA_octet* os,            /* Octet stream base.    */
  640.           CORBA_octet** p,            /* Current position.    */
  641.           CORBA_boolean byte_order,        /* Byte-order.        */
  642.           CORBA_short* val)             /* Unmarshalled value.    */
  643. {
  644.     return cdr_get_unsigned_short(os,p,byte_order,(CORBA_unsigned_short*) val);
  645. }
  646.  
  647. unsigned long
  648. cdr_get_unsigned_long(CORBA_octet* os,        /* Octet stream base.    */
  649.               CORBA_octet** p,        /* Current position.    */
  650.               CORBA_boolean byte_order,    /* Byte-order.        */
  651.               CORBA_unsigned_long* val)    /* Unmarshalled value.    */
  652. {
  653.     /* Within the octet stream the value must be aligned on a word    */
  654.     /* boundary that is a multiple of its size.                */
  655.     while ((unsigned long) ((*p) - os) % 4 != 0) {
  656.     (*p)++;
  657.     }
  658.  
  659.     if (SWAP(byte_order)) {
  660.     ((CORBA_octet*) val)[3] = (*p)[0];
  661.     ((CORBA_octet*) val)[2] = (*p)[1];
  662.     ((CORBA_octet*) val)[1] = (*p)[2];
  663.     ((CORBA_octet*) val)[0] = (*p)[3];
  664.     }
  665.     else {
  666.     MEMCPY(val, *p, 4);
  667.     }
  668.     (*p) += 4;
  669.  
  670.     return 4;
  671. }
  672.  
  673. unsigned long
  674. cdr_get_long(CORBA_octet* os,            /* Octet stream base.    */
  675.          CORBA_octet** p,            /* Current position.    */
  676.          CORBA_boolean byte_order,        /* Byte-order.        */
  677.          CORBA_long* val)             /* Unmarshalled value.    */
  678. {
  679.     return cdr_get_unsigned_long(os, p, byte_order, (CORBA_unsigned_long*)val);
  680. }
  681.  
  682. unsigned long
  683. cdr_get_unsigned_longlong(CORBA_octet* os,    /* Octet stream base.    */
  684.               CORBA_octet** p,    /* Current position.    */
  685.               CORBA_boolean byte_order,/* Byte-order.    */
  686.               CORBA_unsigned_longlong* val)    /* Unmarshalled value.*/
  687. {
  688.     /* Within the octet stream the value must be aligned on a word    */
  689.     /* boundary that is a multiple of its size.                */
  690.     while ((unsigned long) ((*p) - os) % 8 != 0) {
  691.     (*p)++;
  692.     }
  693.  
  694.     if (SWAP(byte_order)) {
  695.     (val->octets)[7] = (*p)[0];
  696.     (val->octets)[6] = (*p)[1];
  697.     (val->octets)[5] = (*p)[2];
  698.     (val->octets)[4] = (*p)[3];
  699.     (val->octets)[3] = (*p)[4];
  700.     (val->octets)[2] = (*p)[5];
  701.     (val->octets)[1] = (*p)[6];
  702.     (val->octets)[0] = (*p)[7];
  703.     }
  704.     else {
  705.     MEMCPY(val->octets, *p, 8);
  706.     }
  707.     (*p) += 8;
  708.  
  709.     return 8;
  710. }
  711.  
  712. unsigned long
  713. cdr_get_longlong(CORBA_octet* os,        /* Octet stream base.    */
  714.          CORBA_octet** p,        /* Current position.    */
  715.          CORBA_boolean byte_order,    /* Byte-order.        */
  716.          CORBA_longlong* val)          /* Unmarshalled value.    */
  717. {
  718.     return cdr_get_unsigned_longlong(os, p, byte_order,
  719.                      (CORBA_unsigned_longlong*) val);
  720. }
  721.  
  722. unsigned long
  723. cdr_get_float(CORBA_octet* os,            /* Octet stream base.    */
  724.           CORBA_octet** p,            /* Current position.    */
  725.           CORBA_boolean byte_order,        /* Byte-order.        */
  726.           CORBA_float* val)            /* Unmarshalled value.    */
  727. {
  728.     /* Within the octet stream the value must be aligned on a word    */
  729.     /* boundary that is a multiple of its size.                */
  730.     while ((unsigned long) ((*p) - os) % 4 != 0) {
  731.     (*p)++;
  732.     }
  733.  
  734.     if (SWAP(byte_order)) {
  735.     ((CORBA_octet*) val)[3] = (*p)[0];
  736.     ((CORBA_octet*) val)[2] = (*p)[1];
  737.     ((CORBA_octet*) val)[1] = (*p)[2];
  738.     ((CORBA_octet*) val)[0] = (*p)[3];
  739.     }
  740.     else {
  741.     MEMCPY(val, *p, 4);
  742.     }
  743.     (*p) += 4;
  744.  
  745.     return 4;
  746. }
  747.  
  748. unsigned long
  749. cdr_get_double(CORBA_octet* os,            /* Octet stream base.    */
  750.            CORBA_octet** p,            /* Current position.    */
  751.            CORBA_boolean byte_order,    /* Byte-order.        */
  752.            CORBA_double* val)        /* Unmarshalled value.    */
  753. {
  754.     /* Within the octet stream the value must be aligned on a word    */
  755.     /* boundary that is a multiple of its size.                */
  756.     while ((unsigned long) ((*p) - os) % 8 != 0) {
  757.     (*p)++;
  758.     }
  759.  
  760.     if (SWAP(byte_order)) {
  761.     ((CORBA_octet*) val)[7] = (*p)[0];
  762.     ((CORBA_octet*) val)[6] = (*p)[1];
  763.     ((CORBA_octet*) val)[5] = (*p)[2];
  764.     ((CORBA_octet*) val)[4] = (*p)[3];
  765.     ((CORBA_octet*) val)[3] = (*p)[4];
  766.     ((CORBA_octet*) val)[2] = (*p)[5];
  767.     ((CORBA_octet*) val)[1] = (*p)[6];
  768.     ((CORBA_octet*) val)[0] = (*p)[7];
  769.     }
  770.     else {
  771.     MEMCPY(val, *p, 8);
  772.     }
  773.     (*p) += 8;
  774.  
  775.     return 8;
  776. }
  777.  
  778. unsigned long
  779. cdr_get_string(CORBA_octet* os,            /* Octet stream base.    */
  780.            CORBA_octet** p,            /* Current position.    */
  781.            CORBA_boolean byte_order,    /* Byte-order.        */
  782.            CORBA_char** val)        /* Unmarshalled value.    */
  783. {
  784.     CORBA_unsigned_long len;
  785.  
  786.     /* Get the length of the string (the length *includes* the NULL!).    */
  787.     cdr_get_unsigned_long(os, p, byte_order, &len);
  788.  
  789.     /* Strings CANNOT be zero length because the length always includes    */
  790.     /* the NULL terminator.  Therefore, even an 'empty' string is of    */
  791.     /* length 1.                            */
  792.     if (len == 0) {
  793.     return 0;
  794.     }
  795.  
  796.     *val = (CORBA_char*) malloc(len);
  797.     if (*val != 0) {
  798.     /* Now get the actual contents of the string.            */
  799.     MEMCPY(*val, *p, len);
  800.     (*p) += len;
  801.     }
  802.     
  803.     return len;
  804. }
  805.  
  806. unsigned long
  807. cdr_get_octets(CORBA_octet* os,            /* Octet stream base.    */
  808.            CORBA_octet** p,            /* Current position.    */
  809.            CORBA_boolean byte_order,    /* Byte-order.        */
  810.            CORBA_octet** val)        /* Unmarshalled value.    */
  811. {
  812.     CORBA_unsigned_long len;
  813.  
  814.     /* Get the number of octets.                    */
  815.     cdr_get_unsigned_long(os, p, byte_order, &len);
  816.     if (len != 0) {
  817.     *val = (CORBA_octet*) malloc(len);
  818.     if (*val != 0) {
  819.         /* Now get the actual octets.                    */
  820.         MEMCPY(*val, *p, len);
  821.         (*p) += len;
  822.     }
  823.     }
  824.     
  825.     return len;
  826. }
  827.  
  828. /************************************************************************/
  829. /* end of cdr.c */
  830.