2 * $Id: cdr.h,v 0.0.0.1 2004/11/26
4 * -------------------------------------------------------------------
6 * Open Real-Time Ethernet
8 * Copyright (C) 2001-2006
9 * Department of Control Engineering FEE CTU Prague, Czech Republic
10 * http://dce.felk.cvut.cz
11 * http://www.ocera.org
13 * Author: Petr Smolik petr.smolik@wo.cz
15 * Project Responsible: Zdenek Hanzalek
17 * Modified for FORB by Michal Sojka <sojkam1@fel.cvut.cz>, 2008
18 * --------------------------------------------------------------------
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * Original of source was from ORBit: A CORBA v2.2 ORB
34 * @author Michal Sojka <sojkam1@fel.cvut.cz>
35 * @date Sun Oct 12 16:55:44 2008
37 * @brief Handling of serialization/deserialization.
39 * Forked from ORTE project.
46 #include "basic_types.h"
47 #include "cdr_codec.h"
48 #include "object_type.h"
51 * Alignment of CORBA types mapped to C.
52 * These have *nothing* to do with CDR alignment.
54 #define ORTE_ALIGNOF_CORBA_OCTET 1
55 #define ORTE_ALIGNOF_CORBA_BOOLEAN 1
56 #define ORTE_ALIGNOF_CORBA_CHAR 1
57 #define ORTE_ALIGNOF_CORBA_WCHAR 2
58 #define ORTE_ALIGNOF_CORBA_SHORT 2
59 #define ORTE_ALIGNOF_CORBA_LONG 4
60 #define ORTE_ALIGNOF_CORBA_LONG_LONG 8
61 #define ORTE_ALIGNOF_CORBA_FLOAT 4
62 #define ORTE_ALIGNOF_CORBA_DOUBLE 8
63 #define ORTE_ALIGNOF_CORBA_LONG_DOUBLE 16
64 #define ORTE_ALIGNOF_CORBA_STRUCT 1
65 #define ORTE_ALIGNOF_CORBA_POINTER 4
73 * struct CDR_Codec - used for serialization/deserialization
76 * @buffer: buffer for data
77 * @buf_len: buffer length
78 * @wptr_max: maximal size of writing data
80 * @wptr_last: index of the data after the last written data
82 * @readonly: readonly attribute
83 * @release_buffer: use CORBA_TRUE if is necessary to free buffer memory after destruction of structure
84 * @orb: Needed for deserialization of object references.
86 * Struct @CDR_Codec is used by serialization and deserialization functions.
89 CDR_Endianness host_endian;
90 CDR_Endianness data_endian;
93 unsigned int wptr_max;
95 unsigned int wptr_last;
97 CORBA_boolean readonly;
98 CORBA_boolean release_buffer;
102 #define HEXDIGIT(c) (isdigit((guchar)(c))?(c)-'0':tolower((guchar)(c))-'a'+10)
103 #define HEXOCTET(a,b) ((HEXDIGIT((a)) << 4) | HEXDIGIT((b)))
105 static inline CORBA_long CDR_data_size(CDR_Codec *codec)
107 return codec->wptr_last - codec->rptr;
110 extern CORBA_boolean CDR_buffer_init(CDR_Codec *codec, const unsigned int size, const unsigned int first);
111 extern CORBA_boolean CDR_buffer_reset(CDR_Codec *codec, const unsigned int first);
112 extern CORBA_boolean CDR_buffer_prepend(CDR_Codec *codec, const unsigned int len);
113 extern CDR_Codec *CDR_codec_init(forb_orb orb);
114 extern CDR_Codec *CDR_codec_init_static(CDR_Codec *codec, forb_orb orb);
115 extern void CDR_codec_release_buffer(CDR_Codec *codec);
116 extern void CDR_codec_free(CDR_Codec *);
118 extern CORBA_boolean CDR_buffer_puts(CDR_Codec *codec, const void *data, const unsigned int len);
119 extern CORBA_boolean CDR_buffer_gets(CDR_Codec *codec, void *dest, const unsigned int len);
121 extern CORBA_boolean CDR_put_short(CDR_Codec *codec, CORBA_short s);
122 extern CORBA_boolean CDR_put_ushort(CDR_Codec *codec, CORBA_unsigned_short us);
123 extern CORBA_boolean CDR_put_long(CDR_Codec *codec, CORBA_long l);
124 extern CORBA_boolean CDR_put_ulong(CDR_Codec *codec, CORBA_unsigned_long ul);
125 /* #ifdef HAVE_CORBA_LONG_LONG */
126 extern CORBA_boolean CDR_put_long_long(CDR_Codec *codec, CORBA_long_long ll);
127 extern CORBA_boolean CDR_put_ulong_long(CDR_Codec *codec, CORBA_unsigned_long_long ull);
128 extern CORBA_boolean CDR_get_ulong_long(CDR_Codec *codec, CORBA_unsigned_long_long *ul);
129 extern CORBA_boolean CDR_get_long_long(CDR_Codec *codec, CORBA_long_long *ul);
131 extern CORBA_boolean CDR_put_float(CDR_Codec *codec, CORBA_float f);
132 extern CORBA_boolean CDR_put_double(CDR_Codec *codec, CORBA_double d);
133 extern CORBA_boolean CDR_put_long_double(CDR_Codec *codec, CORBA_long_double ld);
134 extern CORBA_boolean CDR_put_octet(CDR_Codec *codec, CORBA_octet datum);
135 extern CORBA_boolean CDR_put_octets(CDR_Codec *codec, void *data, unsigned long len);
136 extern CORBA_boolean CDR_put_char(CDR_Codec *codec, CORBA_char c);
137 extern CORBA_boolean CDR_put_boolean(CDR_Codec *codec, CORBA_boolean datum);
138 extern CORBA_boolean CDR_put_string(CDR_Codec *codec, const CORBA_char *str);
139 extern CORBA_boolean CDR_buffer_gets(CDR_Codec *codec, void *dest, const unsigned int len);
140 extern CORBA_boolean CDR_get_short(CDR_Codec *codec, CORBA_short *us);
141 extern CORBA_boolean CDR_get_ushort(CDR_Codec *codec, CORBA_unsigned_short *us);
142 extern CORBA_boolean CDR_get_long(CDR_Codec *codec, CORBA_long *l);
143 extern CORBA_boolean CDR_get_ulong(CDR_Codec *codec, CORBA_unsigned_long *ul);
144 extern CORBA_boolean CDR_get_octet(CDR_Codec *codec, CORBA_octet *datum);
145 extern CORBA_boolean CDR_get_boolean(CDR_Codec *codec, CORBA_boolean *b);
146 extern CORBA_boolean CDR_get_char(CDR_Codec *codec, CORBA_char *c);
147 extern CORBA_boolean CDR_get_string(CDR_Codec *codec, CORBA_char **str);
148 extern CORBA_boolean CDR_get_string_buff(CDR_Codec *codec, CORBA_char *str);
149 extern CORBA_boolean CDR_get_string_static(CDR_Codec *codec, CORBA_char **str);
150 extern CORBA_boolean CDR_get_seq_begin(CDR_Codec *codec, CORBA_unsigned_long *ul);
151 extern CORBA_boolean CDR_get_float(CDR_Codec *codec, CORBA_float *f);
152 extern CORBA_boolean CDR_get_double(CDR_Codec *codec, CORBA_double *d);
154 extern CORBA_boolean CDR_put_align(CDR_Codec *codec, unsigned bsize);
155 extern CORBA_boolean CDR_get_align(CDR_Codec *codec, unsigned bsize);
158 /* serialization functions */
159 #define CORBA_short_serialize(x,y) CDR_put_short((x),*(y))
160 #define CORBA_long_serialize(x,y) CDR_put_long((x),*(y))
161 #define CORBA_long_long_serialize(x,y) CDR_put_long_long((x),*(y))
162 #define CORBA_unsigned_short_serialize(x,y) CDR_put_ushort((x),*(y))
163 #define CORBA_unsigned_long_serialize(x,y) CDR_put_ulong((x),*(y))
164 #define CORBA_unsigned_long_long_serialize(x,y) CDR_put_ulong_long((x),*(y))
165 #define CORBA_float_serialize(x,y) CDR_put_float((x),*(y))
166 #define CORBA_double_serialize(x,y) CDR_put_double((x),*(y))
167 #define CORBA_char_serialize(x,y) CDR_put_char((x),*(y))
168 #define CORBA_boolean_serialize(x,y) CDR_put_boolean((x),*(y))
169 #define CORBA_octet_serialize(x,y) CDR_put_octet((x),*(y))
170 #define CORBA_long_double_serialize(x,y) CDR_put_long_double((x),*(y))
171 #define CORBA_string_serialize(x,y) CDR_put_string((x),*(y))
173 /* deserialization functions */
174 #define CORBA_short_deserialize(x,y) CDR_get_short((x),(y))
175 #define CORBA_long_deserialize(x,y) CDR_get_long((x),(y))
176 #define CORBA_long_long_deserialize(x,y) CDR_get_long_long((x),(y))
177 #define CORBA_unsigned_short_deserialize(x,y) CDR_get_ushort((x),(y))
178 #define CORBA_unsigned_long_deserialize(x,y) CDR_get_ulong((x),(y))
179 #define CORBA_unsigned_long_long_deserialize(x,y) CDR_get_ulong_long((x),(y))
180 #define CORBA_float_deserialize(x,y) CDR_get_float((x),(y))
181 #define CORBA_double_deserialize(x,y) CDR_get_double((x),(y))
182 #define CORBA_char_deserialize(x,y) CDR_get_char((x),(y))
183 #define CORBA_boolean_deserialize(x,y) CDR_get_boolean((x),(y))
184 #define CORBA_octet_deserialize(x,y) CDR_get_octet((x),(y))
185 #define CORBA_long_double_deserialize(x,y) CDR_get_long_double((x),(y))
186 #define CORBA_string_deserialize(x,y) CDR_get_string((x),(y))
188 /* get_max_size functions */
189 #define CORBA_short_get_max_size(x) ((x)->csize=\
190 (unsigned long)ALIGN_ADDRESS((x)->csize,ORTE_ALIGNOF_CORBA_SHORT)+ORTE_ALIGNOF_CORBA_SHORT)
191 #define CORBA_long_get_max_size(x) ((x)->csize=\
192 (unsigned long)ALIGN_ADDRESS((x)->csize,ORTE_ALIGNOF_CORBA_LONG)+ORTE_ALIGNOF_CORBA_LONG)
193 #define CORBA_unsigned_short_get_max_size(x) ((x)->csize=\
194 (unsigned long)ALIGN_ADDRESS((x)->csize,ORTE_ALIGNOF_CORBA_SHORT)+ORTE_ALIGNOF_CORBA_SHORT)
195 #define CORBA_unsigned_long_get_max_size(x) ((x)->csize=\
196 (unsigned long)ALIGN_ADDRESS((x)->csize,ORTE_ALIGNOF_CORBA_LONG)+ORTE_ALIGNOF_CORBA_LONG)
197 #define CORBA_float_get_max_size(x) ((x)->csize=\
198 (unsigned long)ALIGN_ADDRESS((x)->csize,ORTE_ALIGNOF_CORBA_FLOAT)+ORTE_ALIGNOF_CORBA_FLOAT)
199 #define CORBA_double_get_max_size(x) ((x)->csize=\
200 (unsigned long)ALIGN_ADDRESS((x)->csize,ORTE_ALIGNOF_CORBA_DOUBLE)+ORTE_ALIGNOF_CORBA_DOUBLE)
201 #define CORBA_char_get_max_size(x) ((x)->csize=\
202 (unsigned long)ALIGN_ADDRESS((x)->csize,ORTE_ALIGNOF_CORBA_CHAR)+ORTE_ALIGNOF_CORBA_CHAR)
203 #define CORBA_boolean_get_max_size(x) ((x)->csize=\
204 (unsigned long)ALIGN_ADDRESS((x)->csize,ORTE_ALIGNOF_CORBA_BOOLEAN)+ORTE_ALIGNOF_CORBA_BOOLEAN)
205 #define CORBA_octet_get_max_size(x) ((x)->csize=\
206 (unsigned long)ALIGN_ADDRESS((x)->csize,ORTE_ALIGNOF_CORBA_OCTET)+ORTE_ALIGNOF_CORBA_OCTET)
207 #define CORBA_long_double_get_max_size(x) ((x)->csize=\
208 (unsigned long)ALIGN_ADDRESS((x)->csize,ORTE_ALIGNOF_CORBA_LONG_DOUBLE)+ORTE_ALIGNOF_CORBA_DOUBLE)
209 #define CORBA_string_get_max_size(x,y) \
210 ((x)->csize=(unsigned long)ALIGN_ADDRESS((x)->csize,ORTE_ALIGNOF_CORBA_LONG) + ORTE_ALIGNOF_CORBA_LONG + y + 1)
212 #endif /* !_ORTE_CDR_H_ */