exip  Alpha 0.5.4
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
streamEncode.c
Go to the documentation of this file.
1 /*==================================================================*\
2 | EXIP - Embeddable EXI Processor in C |
3 |--------------------------------------------------------------------|
4 | This work is licensed under BSD 3-Clause License |
5 | The full license terms and conditions are located in LICENSE.txt |
6 \===================================================================*/
7 
18 #include "streamEncode.h"
19 #include "streamWrite.h"
20 #include "stringManipulate.h"
21 #include "ioUtil.h"
22 #include <math.h>
23 
24 
25 errorCode encodeNBitUnsignedInteger(EXIStream* strm, unsigned char n, unsigned int int_val)
26 {
27  DEBUG_MSG(INFO, DEBUG_STREAM_IO, (">> %d [0x%X] (%u bits)", int_val, int_val, n));
29  {
30  return writeNBits(strm, n, int_val);
31  }
32  else
33  {
34  unsigned int byte_number = n / 8 + (n % 8 != 0);
35  int tmp_byte_buf;
36  unsigned int i;
37 
38  if(strm->buffer.bufLen < strm->context.bufferIndx + byte_number)
39  {
40  // The buffer end is reached: there are fewer than nbits bits left in the buffer
41  // Flush the buffer if possible
42  errorCode tmp_err_code = EXIP_UNEXPECTED_ERROR;
43 
45  }
46 
47  for(i = 0; i < byte_number*8; i += 8)
48  {
49  tmp_byte_buf = (int_val >> i) & 0xFF;
50  strm->buffer.buf[strm->context.bufferIndx] = tmp_byte_buf;
51  strm->context.bufferIndx++;
52  }
53  }
54  return EXIP_OK;
55 }
56 
57 errorCode encodeBoolean(EXIStream* strm, boolean bool_val)
58 {
59  //TODO: when pattern facets are available in the schema datatype - handle it differently
60  DEBUG_MSG(INFO, DEBUG_STREAM_IO, (">> 0x%X (bool)", bool_val));
61  return encodeNBitUnsignedInteger(strm, 1, bool_val);
62 }
63 
65 {
66  errorCode tmp_err_code = EXIP_UNEXPECTED_ERROR;
67  unsigned int tmp_byte_buf = 0;
68 
69  DEBUG_MSG(INFO, DEBUG_STREAM_IO, (" Write %lu (unsigned)\n", (long unsigned int)int_val));
70  do
71  {
72  tmp_byte_buf = (unsigned int) (int_val & 0x7F);
73  int_val = int_val >> 7;
74  if(int_val)
75  tmp_byte_buf |= 0x80;
76 
77  DEBUG_MSG(INFO, DEBUG_STREAM_IO, (">> 0x%.2X", tmp_byte_buf));
78  TRY(writeNBits(strm, 8, tmp_byte_buf));
79  }
80  while(int_val);
81 
82  return EXIP_OK;
83 }
84 
85 errorCode encodeString(EXIStream* strm, const String* string_val)
86 {
87  // Assume no Restricted Character Set is defined
88  //TODO: Handle the case when Restricted Character Set is defined
89 
90  errorCode tmp_err_code = EXIP_UNEXPECTED_ERROR;
91 
92  DEBUG_MSG(INFO, DEBUG_STREAM_IO, (" Prepare to write string"));
93  TRY(encodeUnsignedInteger(strm, (UnsignedInteger)(string_val->length)));
94 
95  return encodeStringOnly(strm, string_val);
96 }
97 
98 errorCode encodeStringOnly(EXIStream* strm, const String* string_val)
99 {
100  // Assume no Restricted Character Set is defined
101  //TODO: Handle the case when Restricted Character Set is defined
102 
103  errorCode tmp_err_code = EXIP_UNEXPECTED_ERROR;
104  uint32_t tmp_val = 0;
105  Index i = 0;
106  Index readerPosition = 0;
107 #if DEBUG_STREAM_IO == ON && EXIP_DEBUG_LEVEL == INFO
108  DEBUG_MSG(INFO, DEBUG_STREAM_IO, ("\n Write string, len %u: ", (unsigned int) string_val->length));
109  printString(string_val);
110  DEBUG_MSG(INFO, DEBUG_STREAM_IO, ("\n"));
111 #endif
112 
113  for(i = 0; i < string_val->length; i++)
114  {
115  tmp_val = readCharFromString(string_val, &readerPosition);
116 
117  TRY(encodeUnsignedInteger(strm, (UnsignedInteger) tmp_val));
118  }
119 
120  return EXIP_OK;
121 }
122 
123 errorCode encodeBinary(EXIStream* strm, char* binary_val, Index nbytes)
124 {
125  errorCode tmp_err_code = EXIP_UNEXPECTED_ERROR;
126  Index i = 0;
127 
128  TRY(encodeUnsignedInteger(strm, (UnsignedInteger) nbytes));
129 
130  DEBUG_MSG(INFO, DEBUG_STREAM_IO, (" Write %u (binary bytes)\n", (unsigned int) nbytes));
131  for(i = 0; i < nbytes; i++)
132  {
133  TRY(writeNBits(strm, 8, (unsigned int) binary_val[i]));
134  }
135  DEBUG_MSG(INFO, DEBUG_STREAM_IO, ("\n"));
136  return EXIP_OK;
137 }
138 
140 {
141  errorCode tmp_err_code = EXIP_UNEXPECTED_ERROR;
142  UnsignedInteger uval;
143  unsigned char sign;
144  if(sint_val >= 0)
145  {
146  sign = 0;
147  uval = (UnsignedInteger) sint_val;
148  }
149  else
150  {
151  sint_val += 1;
152  uval = (UnsignedInteger) -sint_val;
153  sign = 1;
154  }
155  DEBUG_MSG(INFO, DEBUG_STREAM_IO, (" Write %ld (signed)", (long int)sint_val));
156  TRY(writeNextBit(strm, sign));
157  DEBUG_MSG(INFO, DEBUG_STREAM_IO, ("\n"));
158  return encodeUnsignedInteger(strm, uval);
159 }
160 
162 {
163  errorCode tmp_err_code = EXIP_UNEXPECTED_ERROR;
164  boolean sign;
165  UnsignedInteger integr_part = 0;
166  UnsignedInteger fract_part_rev = 0;
167  UnsignedInteger m;
168  int e = dec_val.exponent;
169 
170  if(dec_val.mantissa >= 0)
171  {
172  sign = FALSE;
173  integr_part = (UnsignedInteger) dec_val.mantissa;
174  }
175  else
176  {
177  sign = TRUE;
178  integr_part = (UnsignedInteger) -dec_val.mantissa;
179  }
180 
181  m = integr_part;
182 
183  TRY(encodeBoolean(strm, sign));
184 
185  if(dec_val.exponent > 0)
186  {
187  while(e)
188  {
189  integr_part = integr_part*10;
190  e--;
191  }
192  }
193  else if(dec_val.exponent < 0)
194  {
195  while(e)
196  {
197  integr_part = integr_part/10;
198  e++;
199  }
200  }
201 
202  TRY(encodeUnsignedInteger(strm, integr_part));
203  e = dec_val.exponent;
204  while(e < 0)
205  {
206  fract_part_rev = fract_part_rev*10 + m%10;
207  m = m/10;
208  e++;
209  }
210 
211  TRY(encodeUnsignedInteger(strm, fract_part_rev));
212 
213  return EXIP_OK;
214 }
215 
217 {
218  errorCode tmp_err_code = EXIP_UNEXPECTED_ERROR;
219 
220  DEBUG_MSG(ERROR, DEBUG_STREAM_IO, (">Float value: %ldE%d\n", (long int)fl_val.mantissa, fl_val.exponent));
221 
222  TRY(encodeIntegerValue(strm, (Integer) fl_val.mantissa)); //encode mantissa
223  TRY(encodeIntegerValue(strm, (Integer) fl_val.exponent)); //encode exponent
224 
225  return EXIP_OK;
226 }
227 
229 {
230  errorCode tmp_err_code = EXIP_UNEXPECTED_ERROR;
231 
232  if(dtType == VALUE_TYPE_DATE_TIME || dtType == VALUE_TYPE_DATE || dtType == VALUE_TYPE_YEAR)
233  {
234  /* Year component */
235  TRY(encodeIntegerValue(strm, (Integer) dt_val.dateTime.tm_year - 100));
236  }
237 
238  if(dtType == VALUE_TYPE_DATE_TIME || dtType == VALUE_TYPE_DATE || dtType == VALUE_TYPE_MONTH)
239  {
240  /* MonthDay component */
241  unsigned int monDay = 0;
242 
243  monDay = dt_val.dateTime.tm_mon + 1;
244  monDay = monDay * 32;
245 
246  monDay += dt_val.dateTime.tm_mday;
247 
248  TRY(encodeNBitUnsignedInteger(strm, 9, monDay));
249  }
250 
251  if(dtType == VALUE_TYPE_DATE_TIME || dtType == VALUE_TYPE_TIME)
252  {
253  /* Time component */
254  unsigned int timeVal = 0;
255 
256  timeVal += dt_val.dateTime.tm_hour;
257  timeVal = timeVal * 64;
258  timeVal += dt_val.dateTime.tm_min;
259  timeVal = timeVal * 64;
260  timeVal += dt_val.dateTime.tm_sec;
261 
262  TRY(encodeNBitUnsignedInteger(strm, 17, timeVal));
263 
265  {
266  /* FractionalSecs component */
267  UnsignedInteger fSecs = 0;
268  unsigned int tmp;
269  unsigned int i = 1;
270  unsigned int j = 0;
271 
272  tmp = dt_val.fSecs.value;
273 
274  while(tmp != 0)
275  {
276  fSecs = fSecs*i + (tmp % 10);
277  tmp = tmp / 10;
278 
279  i = 10;
280  j++;
281  }
282 
283  for(i = 0; i < dt_val.fSecs.offset + 1 - j; j++)
284  {
285  fSecs = fSecs*10;
286  }
287 
288  TRY(encodeBoolean(strm, TRUE));
289  TRY(encodeUnsignedInteger(strm, fSecs));
290  }
291  else
292  {
293  TRY(encodeBoolean(strm, FALSE));
294  }
295 
296  }
297 
299  {
300  // 11-bit Unsigned Integer representing a signed integer offset by 896
301  unsigned int timeZone = 896;
302  TRY(encodeBoolean(strm, TRUE));
303  if(dt_val.TimeZone < -896)
304  {
305  timeZone = 0;
306  DEBUG_MSG(WARNING, DEBUG_STREAM_IO, (">Invalid TimeZone value: %d\n", dt_val.TimeZone));
307  }
308  else if(dt_val.TimeZone > 955)
309  {
310  timeZone = 955;
311  DEBUG_MSG(WARNING, DEBUG_STREAM_IO, (">Invalid TimeZone value: %d\n", dt_val.TimeZone));
312  }
313  else
314  timeZone += dt_val.TimeZone;
315  TRY(encodeNBitUnsignedInteger(strm, 11, timeZone));
316  }
317  else
318  {
319  TRY(encodeBoolean(strm, FALSE));
320  }
321 
322  return EXIP_OK;
323 }
324 
326 {
327  errorCode tmp_err_code = EXIP_UNEXPECTED_ERROR;
328  int i;
329 
330  for(i = 0; i < ec.length; i++)
331  {
332  TRY(encodeNBitUnsignedInteger(strm, ec.bits[i], (unsigned int) ec.part[i]));
333  }
334 
335  return EXIP_OK;
336 }