AbstractDecoderChannel.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. /*
  2. * Copyright (C) 2007-2015 Siemens AG
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser General Public License as published
  6. * by the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /*******************************************************************
  18. *
  19. * @author Daniel.Peintner.EXT@siemens.com
  20. * @version 0.9.3
  21. * @contact Joerg.Heuer@siemens.com
  22. *
  23. * <p>Code generated by EXIdizer</p>
  24. * <p>Schema: V2G_CI_MsgDef.xsd</p>
  25. *
  26. *
  27. ********************************************************************/
  28. #include "DecoderChannel.h"
  29. #include "EXIConfig.h"
  30. #include "EXIOptions.h"
  31. #include "BitInputStream.h"
  32. #include "EXITypes.h"
  33. #include "MethodsBag.h"
  34. /*#include "v2gEXICoder.h"*/
  35. #include "ErrorCodes.h"
  36. #if MEMORY_ALLOCATION == DYNAMIC_ALLOCATION
  37. #include "DynamicMemory.h"
  38. #endif /* DYNAMIC_ALLOCATION */
  39. #ifndef ABSTRACT_DECODER_CHANNEL_C
  40. #define ABSTRACT_DECODER_CHANNEL_C
  41. /* unsigned long == 64 bits, 10 * 7bits = 70 bits */
  42. #define MAX_OCTETS_FOR_UNSIGNED_INTEGER_64 10
  43. /* unsigned int == 32 bits, 5 * 7bits = 35 bits */
  44. #define MAX_OCTETS_FOR_UNSIGNED_INTEGER_32 5
  45. /* buffer for reading (arbitrary) large integer values */
  46. static uint8_t maskedOctets[MAX_OCTETS_FOR_UNSIGNED_INTEGER_64];
  47. static int _decodeUnsignedInteger(bitstream_t* stream, exi_integer_t* iv, int negative) {
  48. int errn = 0;
  49. int i = 0;
  50. uint8_t b;
  51. int k;
  52. do {
  53. /* Read the next octet */
  54. errn = decode(stream, &b);
  55. if (errn == 0) {
  56. if(i < MAX_OCTETS_FOR_UNSIGNED_INTEGER_64) {
  57. /* the 7 least significant bits hold the actual value */
  58. maskedOctets[i++] = (b & 127);
  59. } else {
  60. errn = EXI_UNSUPPORTED_INTEGER_VALUE;
  61. }
  62. }
  63. } while( errn == 0 && b >= 128 ); /* no more octets ? */
  64. if ( errn == 0 ) {
  65. /* For negative values, the Unsigned Integer holds the
  66. * magnitude of the value minus 1 */
  67. switch(i) {
  68. case 1: /* 7 bits */
  69. if (negative) {
  70. iv->val.int8 = (int8_t)(-( b + 1));
  71. iv->type = EXI_INTEGER_8;
  72. } else {
  73. iv->val.uint8 = b;
  74. iv->type = EXI_UNSIGNED_INTEGER_8;
  75. }
  76. break;
  77. case 2: /* 14 bits */
  78. iv->val.uint16 = 0;
  79. for (k = i-1; k >= 0 ; k--) {
  80. iv->val.uint16 = (uint16_t)((iv->val.uint16 << 7) | maskedOctets[k]);
  81. }
  82. if (negative) {
  83. iv->val.int16 = (int16_t)( -( iv->val.uint16 + 1 ));
  84. iv->type = EXI_INTEGER_16;
  85. } else {
  86. iv->type = EXI_UNSIGNED_INTEGER_16;
  87. }
  88. break;
  89. case 3: /* 21 bits */
  90. case 4: /* 28 bits */
  91. iv->val.uint32 = 0;
  92. for (k = i-1; k >= 0 ; k--) {
  93. iv->val.uint32 = (iv->val.uint32 << 7) | maskedOctets[k];
  94. }
  95. if (negative) {
  96. iv->val.int32 = (-(int32_t)(iv->val.uint32 + 1));
  97. if (iv->val.int32 <= INT16_MAX && iv->val.int32 >= INT16_MIN ) {
  98. iv->type = EXI_INTEGER_16;
  99. } else {
  100. iv->type = EXI_INTEGER_32;
  101. }
  102. } else {
  103. if (iv->val.uint32 <= UINT16_MAX) {
  104. iv->type = EXI_UNSIGNED_INTEGER_16;
  105. } else {
  106. iv->type = EXI_UNSIGNED_INTEGER_32;
  107. }
  108. }
  109. break;
  110. case 5: /* 35 bits */
  111. case 6: /* 42 bits */
  112. case 7: /* 49 bits */
  113. case 8: /* 56 bits */
  114. case 9: /* 63 bits */
  115. case 10: /* 70 bits */
  116. iv->val.uint64 = 0;
  117. for (k = i-1; k >= 0 ; k--) {
  118. iv->val.uint64 = (iv->val.uint64 << 7) | maskedOctets[k];
  119. }
  120. if (negative) {
  121. if (i > 9) {
  122. /* too large */
  123. return EXI_UNSUPPORTED_INTEGER_VALUE;
  124. }
  125. iv->val.int64 = (-(int64_t)(iv->val.uint64 + 1));
  126. if (iv->val.int64 <= INT32_MAX && iv->val.int64 >= INT32_MIN ) {
  127. iv->type = EXI_INTEGER_32;
  128. } else {
  129. iv->type = EXI_INTEGER_64;
  130. }
  131. } else {
  132. if (iv->val.uint64 <= UINT32_MAX) {
  133. iv->type = EXI_UNSIGNED_INTEGER_32;
  134. /* iv->val.uint32 = iv->val.uint64;*/
  135. } else {
  136. iv->type = EXI_UNSIGNED_INTEGER_64;
  137. }
  138. }
  139. break;
  140. default:
  141. errn = EXI_UNSUPPORTED_INTEGER_VALUE;
  142. break;
  143. }
  144. }
  145. return errn;
  146. }
  147. int decodeUnsignedInteger(bitstream_t* stream, exi_integer_t* iv) {
  148. return _decodeUnsignedInteger(stream, iv, 0);
  149. }
  150. int decodeUnsignedInteger16(bitstream_t* stream, uint16_t* uint16) {
  151. unsigned int mShift = 0;
  152. int errn = 0;
  153. uint8_t b;
  154. *uint16 = 0;
  155. do {
  156. /* 1. Read the next octet */
  157. errn = decode(stream, &b);
  158. /* 2. Multiply the value of the unsigned number represented by the 7
  159. * least significant
  160. * bits of the octet by the current multiplier and add the result to
  161. * the current value */
  162. *uint16 = (uint16_t)(*uint16 + ((b & 127) << mShift));
  163. /* 3. Multiply the multiplier by 128 */
  164. mShift += 7;
  165. /* 4. If the most significant bit of the octet was 1, go back to step 1 */
  166. } while (errn == 0 && (b >> 7) == 1);
  167. return errn;
  168. }
  169. int decodeUnsignedInteger32(bitstream_t* stream, uint32_t* uint32) {
  170. /* 0XXXXXXX ... 1XXXXXXX 1XXXXXXX */
  171. unsigned int mShift = 0;
  172. int errn = 0;
  173. uint8_t b;
  174. *uint32 = 0;
  175. do {
  176. /* 1. Read the next octet */
  177. errn = decode(stream, &b);
  178. /* 2. Multiply the value of the unsigned number represented by the 7
  179. * least significant
  180. * bits of the octet by the current multiplier and add the result to
  181. * the current value */
  182. *uint32 += (uint32_t)((b & 127) << mShift);
  183. /* 3. Multiply the multiplier by 128 */
  184. mShift += 7;
  185. /* 4. If the most significant bit of the octet was 1, go back to step 1 */
  186. } while (errn == 0 && (b >> 7) == 1);
  187. return errn;
  188. }
  189. /**
  190. * Decode an arbitrary precision non negative integer using a sequence of
  191. * octets. The most significant bit of the last octet is set to zero to
  192. * indicate sequence termination. Only seven bits per octet are used to
  193. * store the integer's value.
  194. */
  195. int decodeUnsignedInteger64(bitstream_t* stream, uint64_t* uint64) {
  196. unsigned int mShift = 0;
  197. int errn = 0;
  198. uint8_t b;
  199. *uint64 = 0L;
  200. do {
  201. errn = decode(stream, &b);
  202. *uint64 += ((uint64_t) (b & 127)) << mShift;
  203. mShift += 7;
  204. } while (errn == 0 && (b >> 7) == 1);
  205. return errn;
  206. }
  207. int decodeInteger(bitstream_t* stream, exi_integer_t* iv) {
  208. int b;
  209. int errn = decodeBoolean(stream, &b);
  210. if (errn == 0) {
  211. errn = _decodeUnsignedInteger(stream, iv, b);
  212. }
  213. return errn;
  214. }
  215. /**
  216. * Decode an arbitrary precision integer using a sign bit followed by a
  217. * sequence of octets. The most significant bit of the last octet is set to
  218. * zero to indicate sequence termination. Only seven bits per octet are used
  219. * to store the integer's value.
  220. */
  221. int decodeInteger16(bitstream_t* stream, int16_t* int16) {
  222. int b;
  223. uint16_t uint16;
  224. int errn = decodeBoolean(stream, &b);
  225. if (errn == 0) {
  226. if (b) {
  227. /* For negative values, the Unsigned Integer holds the
  228. * magnitude of the value minus 1 */
  229. errn = decodeUnsignedInteger16(stream, &uint16);
  230. *int16 = (int16_t)(-(uint16 + 1));
  231. } else {
  232. /* positive */
  233. errn = decodeUnsignedInteger16(stream, &uint16);
  234. *int16 = (int16_t)(uint16);
  235. }
  236. }
  237. return errn;
  238. }
  239. /**
  240. * Decode an arbitrary precision integer using a sign bit followed by a
  241. * sequence of octets. The most significant bit of the last octet is set to
  242. * zero to indicate sequence termination. Only seven bits per octet are used
  243. * to store the integer's value.
  244. */
  245. int decodeInteger32(bitstream_t* stream, int32_t* int32) {
  246. int b;
  247. uint32_t uint32;
  248. int errn = decodeBoolean(stream, &b);
  249. if (errn == 0) {
  250. if (b) {
  251. /* For negative values, the Unsigned Integer holds the
  252. * magnitude of the value minus 1 */
  253. errn = decodeUnsignedInteger32(stream, &uint32);
  254. *int32 = (-(int32_t)(uint32 + 1));
  255. } else {
  256. /* positive */
  257. errn = decodeUnsignedInteger32(stream, &uint32);
  258. *int32 = (int32_t)(uint32);
  259. }
  260. }
  261. return errn;
  262. }
  263. /**
  264. * Decode an arbitrary precision integer using a sign bit followed by a
  265. * sequence of octets. The most significant bit of the last octet is set to
  266. * zero to indicate sequence termination. Only seven bits per octet are used
  267. * to store the integer's value.
  268. */
  269. int decodeInteger64(bitstream_t* stream, int64_t* int64) {
  270. int b;
  271. uint64_t uint64;
  272. int errn = decodeBoolean(stream, &b);
  273. if (errn == 0) {
  274. if (b) {
  275. /* For negative values, the Unsigned Integer holds the
  276. * magnitude of the value minus 1 */
  277. errn = decodeUnsignedInteger64(stream, &uint64);
  278. *int64 = (-(int64_t)(uint64 + 1));
  279. } else {
  280. /* positive */
  281. errn = decodeUnsignedInteger64(stream, &uint64);
  282. *int64 = (int64_t)(uint64);
  283. }
  284. }
  285. return errn;
  286. }
  287. /**
  288. * Decode a Float datatype as two consecutive Integers.
  289. * The first Integer represents the mantissa of the floating point
  290. * number and the second Integer represents the base-10 exponent
  291. * of the floating point number.
  292. */
  293. int decodeFloat(bitstream_t* stream, exi_float_me_t* f) {
  294. int errn = decodeInteger64(stream, &f->mantissa);
  295. if (errn == 0) {
  296. errn = decodeInteger16(stream, &f->exponent);
  297. }
  298. return errn;
  299. }
  300. /**
  301. * Decode a decimal represented as a Boolean sign followed by two Unsigned
  302. * Integers. A sign value of zero (0) is used to represent positive Decimal
  303. * values and a sign value of one (1) is used to represent negative Decimal
  304. * values The first Integer represents the integral portion of the Decimal
  305. * value. The second positive integer represents the fractional portion of
  306. * the decimal with the digits in reverse order to preserve leading zeros.
  307. */
  308. int decodeDecimal(bitstream_t* stream, exi_decimal_t* d) {
  309. int errn = decodeBoolean(stream, &d->negative);
  310. if (errn == 0) {
  311. errn = decodeUnsignedInteger(stream, &d->integral);
  312. if (errn == 0) {
  313. errn = decodeUnsignedInteger(stream, &d->reverseFraction);
  314. }
  315. }
  316. return errn;
  317. }
  318. /**
  319. * Decode a sequence of characters for a given length.
  320. */
  321. int decodeStringOnly(bitstream_t* stream, uint16_t len, exi_string_t* s) {
  322. int errn = 0;
  323. int extraChar = 0;
  324. #if STRING_REPRESENTATION == STRING_REPRESENTATION_ASCII
  325. extraChar = 1; /* null terminator */
  326. #endif /* STRING_REPRESENTATION_ASCII */
  327. if ( (len + extraChar) > s->size) {
  328. #if MEMORY_ALLOCATION == STATIC_ALLOCATION
  329. errn = EXI_ERROR_OUT_OF_STRING_BUFFER;
  330. #endif /* STATIC_ALLOCATION */
  331. #if MEMORY_ALLOCATION == DYNAMIC_ALLOCATION
  332. if(s->size > 0) {
  333. errn = exiFreeDynamicStringMemory(s);
  334. if(errn) {
  335. return errn;
  336. }
  337. }
  338. errn = exiAllocateDynamicStringMemory(s, s->len);
  339. #endif /* DYNAMIC_ALLOCATION */
  340. }
  341. if(errn == 0) {
  342. errn = decodeCharacters(stream, len, s->characters);
  343. s->len = len;
  344. }
  345. return errn;
  346. }
  347. /**
  348. * Decode a length prefixed sequence of characters.
  349. */
  350. int decodeString(bitstream_t* stream, exi_string_t* s) {
  351. int errn = decodeUnsignedInteger16(stream, &s->len);
  352. if (errn == 0) {
  353. errn = decodeStringOnly(stream, s->len, s);
  354. }
  355. return errn;
  356. }
  357. static int _readStringValueLocalHit(bitstream_t* stream, exi_state_t* state, uint16_t qnameID, uint32_t* localID) {
  358. int errn;
  359. uint16_t codingLength;
  360. if( state->stringTable.sizeLocalStrings > qnameID ) {
  361. errn = exiGetCodingLength(state->stringTable.numberOfLocalStrings[qnameID], &codingLength);
  362. if(errn == 0) {
  363. errn = decodeNBitUnsignedInteger(stream, codingLength, localID);
  364. }
  365. } else {
  366. errn = EXI_ERROR_OUT_OF_BOUNDS;
  367. }
  368. return errn;
  369. }
  370. static int _readStringValueGlobalHit(bitstream_t* stream, exi_state_t* state, uint32_t* globalID) {
  371. int errn;
  372. uint16_t codingLength;
  373. errn = exiGetCodingLength(state->stringTable.numberOfGlobalStrings, &codingLength);
  374. if(errn == 0) {
  375. errn = decodeNBitUnsignedInteger(stream, codingLength, globalID);
  376. }
  377. return errn;
  378. }
  379. int decodeStringValue(bitstream_t* stream, exi_state_t* state, uint16_t qnameID, exi_string_value_t* s) {
  380. uint16_t L;
  381. int errn = decodeUnsignedInteger16(stream, &L);
  382. if (errn == 0) {
  383. switch (L) {
  384. case 0:
  385. /* local value partition */
  386. s->type = EXI_STRING_VALUE_LOCAL_HIT;
  387. errn = _readStringValueLocalHit(stream, state, qnameID, &s->localID);
  388. break;
  389. case 1:
  390. /* found in global value partition */
  391. s->type = EXI_STRING_VALUE_GLOBAL_HIT;
  392. errn = _readStringValueGlobalHit(stream, state, &s->globalID);
  393. break;
  394. default:
  395. /* not found in global value (and local value) partition
  396. * ==> string literal is encoded as a String with the length
  397. * incremented by two */
  398. s->type = EXI_STRING_VALUE_MISS;
  399. s->miss.len = L = (uint16_t)(L - 2);
  400. errn = decodeStringOnly(stream, L, &(s->miss));
  401. if(errn == 0) {
  402. #if EXI_OPTION_VALUE_PARTITION_CAPACITY != 0
  403. #if EXI_OPTION_VALUE_MAX_LENGTH != 0
  404. /* If length L is greater than zero the string S is added */
  405. if(L > 0) {
  406. /* After encoding the string value, it is added to both the
  407. * associated "local" value string table partition and the global
  408. * value string table partition */
  409. #if EXI_OPTION_VALUE_MAX_LENGTH < 0
  410. errn = exiAddStringValue(&state->stringTable, &(s->miss), qnameID);
  411. # else /* EXI_v2gVALUE_MAX_LENGTH < 0 */
  412. if (L <= EXI_OPTION_VALUE_MAX_LENGTH) {
  413. errn = exiAddStringValue(&state->stringTable, &(s->miss), qnameID);
  414. }
  415. #endif /* EXI_OPTION_VALUE_MAX_LENGTH < 0 */
  416. }
  417. #endif /* EXI_OPTION_VALUE_MAX_LENGTH != 0 */
  418. #endif /* EXI_OPTION_VALUE_PARTITION_CAPACITY != 0 */
  419. }
  420. break;
  421. }
  422. }
  423. return errn;
  424. }
  425. int decodeRCSStringValue(bitstream_t* stream, exi_state_t* state, uint16_t qnameID, exi_rcs_t* rcs, exi_string_value_t* s) {
  426. unsigned int i;
  427. uint32_t cp;
  428. uint16_t L;
  429. int errn = decodeUnsignedInteger16(stream, &L);
  430. if (errn == 0) {
  431. switch (L) {
  432. case 0:
  433. /* local value partition */
  434. s->type = EXI_STRING_VALUE_LOCAL_HIT;
  435. errn = _readStringValueLocalHit(stream, state, qnameID, &s->localID);
  436. break;
  437. case 1:
  438. /* found in global value partition */
  439. s->type = EXI_STRING_VALUE_GLOBAL_HIT;
  440. errn = _readStringValueGlobalHit(stream, state, &s->globalID);
  441. break;
  442. default:
  443. /* not found in global value (and local value) partition
  444. * ==> string literal is encoded as a String with the length
  445. * incremented by two */
  446. s->type = EXI_STRING_VALUE_MISS;
  447. s->miss.len = L = (uint16_t)(L - 2);
  448. if (L > s->miss.size) {
  449. #if MEMORY_ALLOCATION == STATIC_ALLOCATION
  450. errn = EXI_ERROR_OUT_OF_STRING_BUFFER;
  451. #endif /* STATIC_ALLOCATION */
  452. #if MEMORY_ALLOCATION == DYNAMIC_ALLOCATION
  453. if(s->miss.size > 0) {
  454. errn = exiFreeDynamicStringMemory(&(s->miss));
  455. }
  456. if(errn == 0) {
  457. errn = exiAllocateDynamicStringMemory(&(s->miss), L);
  458. }
  459. #endif /* DYNAMIC_ALLOCATION */
  460. }
  461. if(errn == 0) {
  462. for (i = 0; i < L && errn >= 0; i++) {
  463. errn = decodeNBitUnsignedInteger(stream, rcs->codingLength, &cp);
  464. s->miss.characters[i] = rcs->characters[cp];
  465. }
  466. #if EXI_OPTION_VALUE_PARTITION_CAPACITY != 0
  467. #if EXI_OPTION_VALUE_MAX_LENGTH != 0
  468. /* If length L is greater than zero the string S is added */
  469. if(errn == 0 && L > 0) {
  470. /* After encoding the string value, it is added to both the
  471. * associated "local" value string table partition and the global
  472. * value string table partition */
  473. #if EXI_OPTION_VALUE_MAX_LENGTH < 0
  474. errn = exiAddStringValue(&state->stringTable, &(s->miss), qnameID);
  475. # else /* EXI_OPTION_VALUE_MAX_LENGTH < 0 */
  476. if (L <= EXI_OPTION_VALUE_MAX_LENGTH) {
  477. errn = exiAddStringValue(&state->stringTable, &(s->miss), qnameID);
  478. }
  479. #endif /* EXI_OPTION_VALUE_MAX_LENGTH < 0 */
  480. }
  481. #endif /* EXI_OPTION_VALUE_MAX_LENGTH != 0 */
  482. #endif /* EXI_OPTION_VALUE_PARTITION_CAPACITY != 0 */
  483. }
  484. break;
  485. }
  486. }
  487. return errn;
  488. }
  489. /**
  490. * Decode a sequence of characters according to a given length.
  491. * Each character is represented by its UCS [ISO/IEC 10646]
  492. * code point encoded as an Unsigned Integer
  493. */
  494. int decodeCharacters(bitstream_t* stream, uint16_t len, exi_string_character_t* chars) {
  495. unsigned int i;
  496. int errn = 0;
  497. #if STRING_REPRESENTATION == STRING_REPRESENTATION_ASCII
  498. uint8_t b;
  499. for (i = 0; i < len && errn == 0; i++) {
  500. errn = decode(stream, &b);
  501. if(b < 128) {
  502. chars[i] = (exi_string_character_t)b;
  503. } else {
  504. errn = EXI_ERROR_STRINGVALUES_CHARACTER;
  505. }
  506. }
  507. /* null terminator \0 */
  508. chars[i] = '\0';
  509. #endif /* STRING_REPRESENTATION_ASCII */
  510. #if STRING_REPRESENTATION == STRING_REPRESENTATION_UCS
  511. for (i = 0; i < len && errn == 0; i++) {
  512. errn = decodeUnsignedInteger32(stream, &chars[i]);
  513. }
  514. #endif /* STRING_REPRESENTATION_UCS */
  515. return errn;
  516. }
  517. int decodeRCSCharacters(bitstream_t* stream, uint16_t len, exi_string_character_t* chars, uint16_t rcsCodeLength, uint16_t rcsSize, const exi_string_character_t rcsSet[]) {
  518. unsigned int i;
  519. int errn = 0;
  520. uint32_t uint32;
  521. #if STRING_REPRESENTATION == STRING_REPRESENTATION_ASCII
  522. uint8_t b;
  523. #endif /* STRING_REPRESENTATION_ASCII */
  524. for (i = 0; i < len && errn == 0; i++) {
  525. errn = decodeNBitUnsignedInteger(stream, rcsCodeLength, &uint32);
  526. if(errn == 0) {
  527. if ( uint32 == rcsSize ) {
  528. /* RCS deviation */
  529. #if STRING_REPRESENTATION == STRING_REPRESENTATION_ASCII
  530. errn = decode(stream, &b);
  531. if(b < 128) {
  532. chars[i] = (exi_string_character_t)b;
  533. } else {
  534. errn = EXI_ERROR_STRINGVALUES_CHARACTER;
  535. }
  536. #endif /* STRING_REPRESENTATION_ASCII */
  537. #if STRING_REPRESENTATION == STRING_REPRESENTATION_UCS
  538. errn = decodeUnsignedInteger32(stream, &chars[i]);
  539. #endif /* STRING_REPRESENTATION_UCS */
  540. } else {
  541. /* RCS match */
  542. #if STRING_REPRESENTATION == STRING_REPRESENTATION_ASCII
  543. chars[i] = rcsSet[uint32];
  544. #endif /* STRING_REPRESENTATION_ASCII */
  545. #if STRING_REPRESENTATION == STRING_REPRESENTATION_UCS
  546. chars[i] = rcsSet[uint32];
  547. #endif /* STRING_REPRESENTATION_UCS */
  548. }
  549. }
  550. }
  551. return errn;
  552. }
  553. /**
  554. * Decode a binary value as a length-prefixed sequence of octets.
  555. */
  556. int decodeBinary(bitstream_t* stream, exi_bytes_t* bytes) {
  557. int errn = decodeUnsignedInteger16(stream, &bytes->len);
  558. if (errn == 0) {
  559. if (bytes->len > bytes->size) {
  560. #if MEMORY_ALLOCATION == STATIC_ALLOCATION
  561. errn = EXI_ERROR_OUT_OF_BYTE_BUFFER;
  562. #endif /* STATIC_ALLOCATION */
  563. #if MEMORY_ALLOCATION == DYNAMIC_ALLOCATION
  564. errn = exiFreeDynamicBinaryMemory(bytes);
  565. if(errn == 0) {
  566. errn = exiAllocateDynamicBinaryMemory(bytes, bytes->len);
  567. }
  568. #endif /* DYNAMIC_ALLOCATION */
  569. }
  570. errn = decodeBytes(stream, bytes->len, bytes->data);
  571. }
  572. return errn;
  573. }
  574. int decodeBytes(bitstream_t* stream, uint16_t len, uint8_t* data) {
  575. unsigned int i;
  576. int errn = 0;
  577. uint8_t b;
  578. for (i = 0; i < len && errn == 0; i++) {
  579. errn = decode(stream, &b);
  580. data[i] = (uint8_t)b;
  581. }
  582. return errn;
  583. }
  584. /**
  585. * Decode Date-Time as sequence of values representing the individual
  586. * components of the Date-Time.
  587. */
  588. int decodeDateTime(bitstream_t* stream, exi_datetime_type_t type, exi_datetime_t* datetime) {
  589. int errn = 0;
  590. datetime->type = type;
  591. datetime->year = 0;
  592. datetime->monthDay = 0;
  593. datetime->time = 0;
  594. datetime->presenceFractionalSecs = 0;
  595. datetime->fractionalSecs = 0;
  596. datetime->presenceTimezone = 0;
  597. datetime->timezone = 0;
  598. switch (type) {
  599. case EXI_DATETIME_GYEAR: /* Year, [Time-Zone] */
  600. errn = decodeInteger32(stream, &datetime->year);
  601. if (errn == 0) {
  602. datetime->year += DATETIME_YEAR_OFFSET;
  603. }
  604. break;
  605. case EXI_DATETIME_GYEARMONTH: /* Year, MonthDay, [TimeZone] */
  606. case EXI_DATETIME_DATE:
  607. errn = decodeInteger32(stream, &datetime->year);
  608. if (errn == 0) {
  609. datetime->year += DATETIME_YEAR_OFFSET;
  610. errn = decodeNBitUnsignedInteger(stream, DATETIME_NUMBER_BITS_MONTHDAY, &datetime->monthDay);
  611. }
  612. break;
  613. case EXI_DATETIME_DATETIME: /* Year, MonthDay, Time, [FractionalSecs], [TimeZone] */
  614. /* e.g. "0001-01-01T00:00:00.111+00:33" */
  615. errn = decodeInteger32(stream, &datetime->year);
  616. if (errn == 0) {
  617. datetime->year += DATETIME_YEAR_OFFSET;
  618. errn = decodeNBitUnsignedInteger(stream, DATETIME_NUMBER_BITS_MONTHDAY, &datetime->monthDay);
  619. if (errn != 0) {
  620. break;
  621. }
  622. }
  623. /* no break */
  624. case EXI_DATETIME_TIME: /* Time, [FractionalSecs], [TimeZone] */
  625. /* e.g. "12:34:56.135" */
  626. errn = decodeNBitUnsignedInteger(stream, DATETIME_NUMBER_BITS_TIME, &datetime->time);
  627. if (errn == 0) {
  628. errn = decodeBoolean(stream, &datetime->presenceFractionalSecs);
  629. if (errn == 0) {
  630. if (datetime->presenceFractionalSecs) {
  631. errn = decodeUnsignedInteger32(stream, &datetime->fractionalSecs);
  632. }
  633. }
  634. }
  635. break;
  636. case EXI_DATETIME_GMONTH: /* MonthDay, [TimeZone] */
  637. /* e.g. "--12" */
  638. case EXI_DATETIME_GMONTHDAY: /* MonthDay, [TimeZone] */
  639. /* e.g. "--01-28" */
  640. case EXI_DATETIME_GDAY: /* MonthDay, [TimeZone] */
  641. /* "---16" */
  642. errn = decodeNBitUnsignedInteger(stream, DATETIME_NUMBER_BITS_MONTHDAY, &datetime->monthDay );
  643. break;
  644. default:
  645. errn = EXI_UNSUPPORTED_DATETIME_TYPE;
  646. break;
  647. }
  648. if(errn == 0) {
  649. errn = decodeBoolean(stream, &datetime->presenceTimezone );
  650. if (errn == 0 && datetime->presenceTimezone) {
  651. errn = decodeNBitUnsignedInteger(stream, DATETIME_NUMBER_BITS_TIMEZONE, &datetime->timezone);
  652. datetime->timezone -= DATETIME_TIMEZONE_OFFSET_IN_MINUTES;
  653. }
  654. }
  655. return errn;
  656. }
  657. #endif