AbstractDecoderChannel.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579
  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 2012-01-31
  22. * @contact Joerg.Heuer@siemens.com
  23. *
  24. * <p>Code generated by EXIdizer</p>
  25. ********************************************************************/
  26. #include "DecoderChannel.h"
  27. #include "CoderChannel.h"
  28. #include "BitInputStream.h"
  29. #include "EXITypes.h"
  30. #ifndef ABSTRACT_DECODER_CHANNEL_C
  31. #define ABSTRACT_DECODER_CHANNEL_C
  32. /* unsigned long == 64 bits, 10 * 7bits = 70 bits */
  33. #define MAX_OCTETS_FOR_UNSIGNED_INTEGER_64 10
  34. /* unsigned int == 32 bits, 5 * 7bits = 35 bits */
  35. #define MAX_OCTETS_FOR_UNSIGNED_INTEGER_32 5
  36. /* buffer for reading (arbitrary) large integer values */
  37. static unsigned int maskedOctets[MAX_OCTETS_FOR_UNSIGNED_INTEGER_64];
  38. static int _decodeUnsignedInteger(bitstream_t* stream, integer_t* iv, int negative) {
  39. int errn = 0;
  40. int i, k;
  41. uint8_t b;
  42. for (i = 0; i < MAX_OCTETS_FOR_UNSIGNED_INTEGER_64; i++) {
  43. /* Read the next octet */
  44. errn = decode(stream, &b);
  45. /* If the most significant bit of the octet was 1,
  46. another octet is going to come */
  47. if (b < 128) {
  48. /* no more octets */
  49. /* For negative values, the Unsigned Integer holds the
  50. * magnitude of the value minus 1 */
  51. switch(i) {
  52. case 0: /* 7 bits */
  53. if (negative) {
  54. iv->val.int8 = - ( b + 1);
  55. iv->type = INTEGER_8;
  56. } else {
  57. iv->val.uint8 = b;
  58. iv->type = UNSIGNED_INTEGER_8;
  59. }
  60. return 0;
  61. case 1: /* 14 bits */
  62. maskedOctets[i] = b;
  63. iv->val.uint16 = 0;
  64. for (k = i; k >= 0 ; k--) {
  65. iv->val.uint16 = (iv->val.uint16 << 7) | maskedOctets[k];
  66. }
  67. if (negative) {
  68. iv->val.int16 = - ( iv->val.uint16 + 1 );
  69. iv->type = INTEGER_16;
  70. } else {
  71. iv->type = UNSIGNED_INTEGER_16;
  72. }
  73. return 0;
  74. case 2: /* 21 bits */
  75. case 3: /* 28 bits */
  76. maskedOctets[i] = b;
  77. iv->val.uint32 = 0;
  78. for (k = i; k >= 0 ; k--) {
  79. iv->val.uint32 = (iv->val.uint32 << 7) | maskedOctets[k];
  80. }
  81. if (negative) {
  82. iv->val.int32 = - ( iv->val.uint32 + 1 );
  83. if (iv->val.int32 <= INT16_MAX && iv->val.int32 >= INT16_MIN ) {
  84. iv->type = INTEGER_16;
  85. } else {
  86. iv->type = INTEGER_32;
  87. }
  88. } else {
  89. if (iv->val.uint32 <= UINT16_MAX) {
  90. iv->type = UNSIGNED_INTEGER_16;
  91. } else {
  92. iv->type = UNSIGNED_INTEGER_32;
  93. }
  94. }
  95. return 0;
  96. case 4: /* 35 bits */
  97. case 5: /* 42 bits */
  98. case 6: /* 49 bits */
  99. case 7: /* 56 bits */
  100. case 8: /* 63 bits */
  101. case 9: /* 70 bits */
  102. maskedOctets[i] = b;
  103. iv->val.uint64 = 0;
  104. for (k = i; k >= 0 ; k--) {
  105. iv->val.uint64 = (iv->val.uint64 << 7) | maskedOctets[k];
  106. }
  107. if (negative) {
  108. if (i > 8) {
  109. /* too large */
  110. return EXI_UNSUPPORTED_INTEGER_VALUE;
  111. }
  112. iv->val.int64 = - ( iv->val.uint64 + 1 );
  113. if (iv->val.int64 <= INT32_MAX && iv->val.int64 >= INT32_MIN ) {
  114. iv->type = INTEGER_32;
  115. } else {
  116. iv->type = INTEGER_64;
  117. }
  118. } else {
  119. if (iv->val.uint64 <= UINT32_MAX) {
  120. iv->type = UNSIGNED_INTEGER_32;
  121. /* iv->val.uint32 = iv->val.uint64;*/
  122. } else {
  123. iv->type = UNSIGNED_INTEGER_64;
  124. }
  125. }
  126. return 0;
  127. default:
  128. return EXI_UNSUPPORTED_INTEGER_VALUE;
  129. }
  130. } else {
  131. /* the 7 least significant bits hold the actual value */
  132. maskedOctets[i] = (b & 127);
  133. }
  134. }
  135. return EXI_UNSUPPORTED_INTEGER_VALUE;
  136. }
  137. int decodeUnsignedInteger(bitstream_t* stream, integer_t* iv) {
  138. return _decodeUnsignedInteger(stream, iv, 0);
  139. }
  140. int decodeUnsignedInteger16(bitstream_t* stream, uint16_t* uint16) {
  141. unsigned int mShift = 0;
  142. int errn = 0;
  143. uint8_t b;
  144. *uint16 = 0;
  145. do {
  146. /* 1. Read the next octet */
  147. errn = decode(stream, &b);
  148. /* 2. Multiply the value of the unsigned number represented by the 7
  149. * least significant
  150. * bits of the octet by the current multiplier and add the result to
  151. * the current value */
  152. *uint16 += (b & 127) << mShift;
  153. /* 3. Multiply the multiplier by 128 */
  154. mShift += 7;
  155. /* 4. If the most significant bit of the octet was 1, go back to step 1 */
  156. } while (errn >= 0 && (b >> 7) == 1);
  157. return errn;
  158. }
  159. int decodeUnsignedInteger32(bitstream_t* stream, uint32_t* uint32) {
  160. /* 0XXXXXXX ... 1XXXXXXX 1XXXXXXX */
  161. unsigned int mShift = 0;
  162. int errn = 0;
  163. uint8_t b;
  164. *uint32 = 0;
  165. do {
  166. /* 1. Read the next octet */
  167. errn = decode(stream, &b);
  168. /* 2. Multiply the value of the unsigned number represented by the 7
  169. * least significant
  170. * bits of the octet by the current multiplier and add the result to
  171. * the current value */
  172. *uint32 += (b & 127) << mShift;
  173. /* 3. Multiply the multiplier by 128 */
  174. mShift += 7;
  175. /* 4. If the most significant bit of the octet was 1, go back to step 1 */
  176. } while (errn >= 0 && (b >> 7) == 1);
  177. return errn;
  178. }
  179. /**
  180. * Decode an arbitrary precision non negative integer using a sequence of
  181. * octets. The most significant bit of the last octet is set to zero to
  182. * indicate sequence termination. Only seven bits per octet are used to
  183. * store the integer's value.
  184. */
  185. int decodeUnsignedInteger64(bitstream_t* stream, uint64_t* uint64) {
  186. unsigned int mShift = 0;
  187. int errn = 0;
  188. uint8_t b;
  189. *uint64 = 0L;
  190. do {
  191. errn = decode(stream, &b);
  192. *uint64 += ((uint64_t) (b & 127)) << mShift;
  193. mShift += 7;
  194. } while (errn >= 0 && (b >> 7) == 1);
  195. return errn;
  196. }
  197. int decodeInteger(bitstream_t* stream, integer_t* iv) {
  198. int b;
  199. int errn = decodeBoolean(stream, &b);
  200. if (errn < 0) {
  201. return errn;
  202. }
  203. return _decodeUnsignedInteger(stream, iv, b);
  204. }
  205. /**
  206. * Decode an arbitrary precision integer using a sign bit followed by a
  207. * sequence of octets. The most significant bit of the last octet is set to
  208. * zero to indicate sequence termination. Only seven bits per octet are used
  209. * to store the integer's value.
  210. */
  211. int decodeInteger32(bitstream_t* stream, int32_t* int32) {
  212. int b;
  213. uint32_t uint32;
  214. int errn = decodeBoolean(stream, &b);
  215. if (errn < 0) {
  216. return errn;
  217. }
  218. if (b) {
  219. /* For negative values, the Unsigned Integer holds the
  220. * magnitude of the value minus 1 */
  221. errn = decodeUnsignedInteger32(stream, &uint32);
  222. *int32 = -(uint32 + 1);
  223. } else {
  224. /* positive */
  225. errn = decodeUnsignedInteger32(stream, &uint32);
  226. *int32 = (int32_t)(uint32);
  227. }
  228. return errn;
  229. }
  230. /**
  231. * Decode an arbitrary precision integer using a sign bit followed by a
  232. * sequence of octets. The most significant bit of the last octet is set to
  233. * zero to indicate sequence termination. Only seven bits per octet are used
  234. * to store the integer's value.
  235. */
  236. int decodeInteger64(bitstream_t* stream, int64_t* int64) {
  237. int b;
  238. uint64_t uint64;
  239. int errn = decodeBoolean(stream, &b);
  240. if (errn < 0) {
  241. return errn;
  242. }
  243. if (b) {
  244. /* For negative values, the Unsigned Integer holds the
  245. * magnitude of the value minus 1 */
  246. errn = decodeUnsignedInteger64(stream, &uint64);
  247. *int64 = -(uint64 + 1);
  248. } else {
  249. /* positive */
  250. errn = decodeUnsignedInteger64(stream, &uint64);
  251. *int64 = (int64_t)(uint64);
  252. }
  253. return errn;
  254. }
  255. /**
  256. * Decode a Float datatype as two consecutive Integers.
  257. * The first Integer represents the mantissa of the floating point
  258. * number and the second Integer represents the base-10 exponent
  259. * of the floating point number.
  260. */
  261. int decodeFloat(bitstream_t* stream, float_me_t* f) {
  262. int errn = decodeInteger64(stream, &f->mantissa);
  263. if (errn < 0) {
  264. return errn;
  265. }
  266. return decodeInteger32(stream, &f->exponent);
  267. }
  268. /**
  269. * Decode a decimal represented as a Boolean sign followed by two Unsigned
  270. * Integers. A sign value of zero (0) is used to represent positive Decimal
  271. * values and a sign value of one (1) is used to represent negative Decimal
  272. * values The first Integer represents the integral portion of the Decimal
  273. * value. The second positive integer represents the fractional portion of
  274. * the decimal with the digits in reverse order to preserve leading zeros.
  275. */
  276. int decodeDecimal(bitstream_t* stream, decimal_t* d) {
  277. int errn = decodeBoolean(stream, &d->negative);
  278. if (errn < 0) {
  279. return errn;
  280. }
  281. errn = decodeUnsignedInteger(stream, &d->integral);
  282. if (errn < 0) {
  283. return errn;
  284. }
  285. errn = decodeUnsignedInteger(stream, &d->reverseFraction);
  286. return errn;
  287. }
  288. /**
  289. * Decode a sequence of characters for a given length.
  290. */
  291. int decodeStringOnly(bitstream_t* stream, uint16_t len, string_ucs_t* s) {
  292. if (len > s->size) {
  293. /* not enough space */
  294. return EXI_ERROR_OUT_OF_STRING_BUFFER;
  295. }
  296. decodeCharacters(stream, len, s->codepoints);
  297. s->len = len;
  298. return 0;
  299. }
  300. /**
  301. * Decode a length prefixed sequence of characters.
  302. */
  303. int decodeString(bitstream_t* stream, string_ucs_t* s) {
  304. int errn = decodeUnsignedInteger16(stream, &s->len);
  305. if (errn < 0) {
  306. return errn;
  307. }
  308. return decodeStringOnly(stream, s->len, s);
  309. }
  310. int decodeStringASCII(bitstream_t* stream, string_ascii_t* s) {
  311. uint16_t slen;
  312. int errn = decodeUnsignedInteger16(stream, &slen);
  313. if (errn < 0) {
  314. return errn;
  315. }
  316. if (s->size < slen) {
  317. return EXI_ERROR_OUT_OF_ASCII_BUFFER;
  318. }
  319. return decodeCharactersASCII(stream, slen, s->chars);
  320. }
  321. int decodeStringValue(bitstream_t* stream, string_ucs_t* s) {
  322. int errn = decodeUnsignedInteger16(stream, &s->len);
  323. if (errn < 0) {
  324. return errn;
  325. }
  326. switch (s->len) {
  327. case 0:
  328. /* local value partition */
  329. return EXI_UNSUPPORTED_STRING_TABLE_LOCAL_HIT;
  330. case 1:
  331. /* found in global value partition */
  332. return EXI_UNSUPPORTED_STRING_TABLE_GLOBAL_HIT;
  333. default:
  334. /* not found in global value (and local value) partition
  335. * ==> string literal is encoded as a String with the length
  336. * incremented by two */
  337. return decodeStringOnly(stream, ((s->len) - 2), s);
  338. /* After encoding the string value, it is added to both the
  339. * associated "local" value string table partition and the global
  340. * value string table partition */
  341. /* addValue(context, value); */
  342. }
  343. }
  344. int decodeRCSStringValue(bitstream_t* stream, rcs_t* rcs, string_ucs_t* s) {
  345. unsigned int i;
  346. uint32_t cp;
  347. int errn = decodeUnsignedInteger16(stream, &s->len);
  348. if (errn < 0) {
  349. return errn;
  350. }
  351. switch (s->len) {
  352. case 0:
  353. /* local value partition */
  354. return EXI_UNSUPPORTED_STRING_TABLE_LOCAL_HIT;
  355. case 1:
  356. /* found in global value partition */
  357. return EXI_UNSUPPORTED_STRING_TABLE_GLOBAL_HIT;
  358. default:
  359. /* not found in global value (and local value) partition
  360. * ==> string literal is encoded as a String with the length
  361. * incremented by two */
  362. s->len = s->len - 2;
  363. if (s->len > s->size) {
  364. /* not enough space */
  365. return EXI_ERROR_OUT_OF_STRING_BUFFER;
  366. }
  367. for (i = 0; i < s->len && errn >= 0; i++) {
  368. errn = decodeNBitUnsignedInteger(stream, rcs->codingLength, &cp);
  369. if (errn < 0) {
  370. return errn;
  371. }
  372. s->codepoints[i] = rcs->codepoints[cp];
  373. }
  374. /* After encoding the string value, it is added to both the
  375. * associated "local" value string table partition and the global
  376. * value string table partition */
  377. /* addValue(context, value); */
  378. return 0;
  379. }
  380. }
  381. /**
  382. * Decode a sequence of characters according to a given length.
  383. * Each character is represented by its UCS [ISO/IEC 10646]
  384. * code point encoded as an Unsigned Integer
  385. */
  386. int decodeCharacters(bitstream_t* stream, uint16_t len, uint32_t* chars) {
  387. unsigned int i;
  388. int errn = 0;
  389. for (i = 0; i < len && errn >= 0; i++) {
  390. errn = decodeUnsignedInteger32(stream, &chars[i]);
  391. if (errn < 0) {
  392. return errn;
  393. }
  394. }
  395. return errn;
  396. }
  397. int decodeCharactersASCII(bitstream_t* stream, uint16_t len, char* chars) {
  398. unsigned int i;
  399. uint32_t c;
  400. int errn = 0;
  401. for (i = 0; i < len && errn >= 0; i++) {
  402. errn = decodeUnsignedInteger32(stream, &c);
  403. if (errn < 0) {
  404. return errn;
  405. }
  406. if (c > 127) {
  407. return EXI_ERROR_CONVERSION_NO_ASCII_CHARACTERS;
  408. }
  409. chars[i] = c;
  410. }
  411. chars[i] = '\0';
  412. return errn;
  413. }
  414. /**
  415. * Decode a binary value as a length-prefixed sequence of octets.
  416. */
  417. int decodeBinary(bitstream_t* stream, bytes_t* bytes) {
  418. unsigned int i;
  419. uint8_t b;
  420. int errn = decodeUnsignedInteger16(stream, &bytes->len);
  421. if (errn < 0) {
  422. return errn;
  423. }
  424. if (bytes->len > bytes->size) {
  425. /* not enough space */
  426. return EXI_ERROR_OUT_OF_BYTE_BUFFER;
  427. }
  428. for (i = 0; i < bytes->len && errn >= 0; i++) {
  429. errn = decode(stream, &b);
  430. if (errn < 0) {
  431. return errn;
  432. }
  433. bytes->data[i] = (uint8_t)b;
  434. }
  435. return errn;
  436. }
  437. /**
  438. * Decode Date-Time as sequence of values representing the individual
  439. * components of the Date-Time.
  440. */
  441. int decodeDateTime(bitstream_t* stream, exi_datetime_type_t type, datetime_t* datetime){
  442. int errn;
  443. datetime->type = type;
  444. datetime->year = 0;
  445. datetime->monthDay = 0;
  446. datetime->time = 0;
  447. datetime->presenceFractionalSecs = 0;
  448. datetime->fractionalSecs = 0;
  449. datetime->presenceTimezone = 0;
  450. datetime->timezone = 0;
  451. switch (type) {
  452. case exiGYear: /* Year, [Time-Zone] */
  453. errn = decodeInteger32(stream, &datetime->year);
  454. if (errn < 0) {
  455. return errn;
  456. }
  457. datetime->year += DATETIME_YEAR_OFFSET;
  458. break;
  459. case exiGYearMonth: /* Year, MonthDay, [TimeZone] */
  460. case exiDate:
  461. errn = decodeInteger32(stream, &datetime->year);
  462. if (errn < 0) {
  463. return errn;
  464. }
  465. datetime->year += DATETIME_YEAR_OFFSET;
  466. errn = decodeNBitUnsignedInteger(stream, DATETIME_NUMBER_BITS_MONTHDAY, &datetime->monthDay);
  467. break;
  468. case exiDateTime: /* Year, MonthDay, Time, [FractionalSecs], [TimeZone] */
  469. /* e.g. "0001-01-01T00:00:00.111+00:33" */
  470. errn = decodeInteger32(stream, &datetime->year);
  471. if (errn < 0) {
  472. return errn;
  473. }
  474. datetime->year += DATETIME_YEAR_OFFSET;
  475. errn = decodeNBitUnsignedInteger(stream, DATETIME_NUMBER_BITS_MONTHDAY, &datetime->monthDay);
  476. if (errn < 0) {
  477. return errn;
  478. }
  479. /* Note: *no* break */
  480. case exiTime: /* Time, [FractionalSecs], [TimeZone] */
  481. /* e.g. "12:34:56.135" */
  482. errn = decodeNBitUnsignedInteger(stream, DATETIME_NUMBER_BITS_TIME, &datetime->time);
  483. if (errn < 0) {
  484. return errn;
  485. }
  486. errn = decodeBoolean(stream, &datetime->presenceFractionalSecs);
  487. if (errn < 0) {
  488. return errn;
  489. }
  490. if (datetime->presenceFractionalSecs) {
  491. errn = decodeUnsignedInteger32(stream, &datetime->fractionalSecs);
  492. }
  493. break;
  494. case exiGMonth: /* MonthDay, [TimeZone] */
  495. /* e.g. "--12" */
  496. case exiGMonthDay: /* MonthDay, [TimeZone] */
  497. /* e.g. "--01-28" */
  498. case exiGDay: /* MonthDay, [TimeZone] */
  499. /* "---16" */
  500. errn = decodeNBitUnsignedInteger(stream, DATETIME_NUMBER_BITS_MONTHDAY, &datetime->monthDay );
  501. break;
  502. default:
  503. return -1;
  504. }
  505. errn = decodeBoolean(stream, &datetime->presenceTimezone );
  506. if (errn < 0) {
  507. return errn;
  508. }
  509. if (datetime->presenceTimezone) {
  510. errn = decodeNBitUnsignedInteger(stream, DATETIME_NUMBER_BITS_TIMEZONE, &datetime->timezone);
  511. datetime->timezone -= DATETIME_TIMEZONE_OFFSET_IN_MINUTES;
  512. }
  513. return errn;
  514. }
  515. #endif