AbstractDecoderChannel.c 18 KB

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