main_codec.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Copyright (C) 2007-2010 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.2.1
  21. * @contact Joerg.Heuer@siemens.com
  22. *
  23. * <p>Sample program to illustrate how to read an EXI stream and
  24. * directly write it again to an output</p>
  25. *
  26. * <p>e.g., data/test/sessionSetupReq.xml.exi out/test/sessionSetupReq.xml.exi</p>
  27. ********************************************************************/
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include "EXIDecoder.h"
  31. #include "StringTable.h"
  32. #include "EXIEncoder.h"
  33. #include "EXITypes.h"
  34. #include "Bitstream.h"
  35. #define ARRAY_SIZE_BYTES 50
  36. #define ARRAY_SIZE_STRINGS 50
  37. /* avoids warning: initializer element is not computable at load time */
  38. uint8_t data[ARRAY_SIZE_BYTES];
  39. uint32_t codepoints[ARRAY_SIZE_STRINGS];
  40. int main_codec(int argc, char *argv[]) {
  41. int errn = 0;
  42. unsigned int i;
  43. bitstream_t iStream, oStream;
  44. size_t posDecode, posEncode;
  45. /* EXI set-up */
  46. exi_state_t stateDecode;
  47. exi_state_t stateEncode;
  48. exi_event_t event;
  49. eqname_t eqn;
  50. exi_value_t val;
  51. /* BINARY memory setup */
  52. bytes_t bytes = { ARRAY_SIZE_BYTES, data, 0 };
  53. /* STRING memory setuo */
  54. string_ucs_t string = { ARRAY_SIZE_STRINGS, codepoints, 0 };
  55. const char * localName;
  56. const char * namespaceURI;
  57. int noEndOfDocument = 1; /* true */
  58. if (argc != 3) {
  59. printf("Usage: %s exiInput exiOutput\n", argv[0]);
  60. return -1;
  61. }
  62. /* parse EXI stream to internal byte structures */
  63. toBitstream(argv[1], &iStream);
  64. /* input */
  65. posDecode = 0;
  66. iStream.pos = &posDecode;
  67. iStream.buffer = 0;
  68. iStream.capacity = 0;
  69. /* output */
  70. posEncode = 0;
  71. oStream.data = malloc(sizeof(uint8_t)*iStream.size);
  72. oStream.size = iStream.size;
  73. oStream.pos = &posEncode;
  74. oStream.buffer = 0;
  75. oStream.capacity = 8;
  76. val.binary = bytes;
  77. val.string = string;
  78. /* init decoder (read header, set initial state) */
  79. exiInitDecoder(&iStream, &stateDecode);
  80. /* init encoder (write header, set initial state) */
  81. exiInitEncoder(&oStream, &stateEncode);
  82. printf("[DECODE] >>> EXI >>> [ENCODE] \n");
  83. do {
  84. if (errn < 0) {
  85. printf("[Encode-ERROR] %d \n", errn);
  86. return errn;
  87. }
  88. errn = exiDecodeNextEvent(&iStream, &stateDecode, &event);
  89. if (errn < 0) {
  90. printf("[Decode-ERROR] %d \n", errn);
  91. return errn;
  92. }
  93. switch (event) {
  94. case START_DOCUMENT:
  95. /* decode */
  96. errn = exiDecodeStartDocument(&iStream, &stateDecode);
  97. if (errn < 0) {
  98. printf("[Decode-ERROR] %d \n", errn);
  99. return errn;
  100. }
  101. printf(">> START_DOCUMENT \n");
  102. /* encode */
  103. errn = exiEncodeStartDocument(&oStream, &stateEncode);
  104. break;
  105. case END_DOCUMENT:
  106. /* decode */
  107. errn = exiDecodeEndDocument(&iStream, &stateDecode);
  108. if (errn < 0) {
  109. printf("[Decode-ERROR] %d \n", errn);
  110. return errn;
  111. }
  112. printf(">> END_DOCUMENT \n");
  113. /* encode */
  114. errn = exiEncodeEndDocument(&oStream, &stateEncode);
  115. /* signalize end of document */
  116. noEndOfDocument = 0; /* false */
  117. break;
  118. case START_ELEMENT:
  119. /* decode */
  120. errn = exiDecodeStartElement(&iStream, &stateDecode, &eqn);
  121. if (errn < 0) {
  122. printf("[Decode-ERROR] %d \n", errn);
  123. return errn;
  124. }
  125. exiGetLocalName(eqn.namespaceURI, eqn.localPart, &localName);
  126. exiGetUri(eqn.namespaceURI, &namespaceURI);
  127. printf(">> SE {%s}%s \n", namespaceURI, localName);
  128. /* encode */
  129. errn = exiEncodeStartElement(&oStream, &stateEncode, &eqn);
  130. break;
  131. case END_ELEMENT:
  132. /* decode */
  133. errn = exiDecodeEndElement(&iStream, &stateDecode, &eqn);
  134. if (errn < 0) {
  135. printf("[Decode-ERROR] %d \n", errn);
  136. return errn;
  137. }
  138. exiGetLocalName(eqn.namespaceURI, eqn.localPart, &localName);
  139. exiGetUri(eqn.namespaceURI, &namespaceURI);
  140. printf("<< EE {%s}%s \n", namespaceURI, localName);
  141. /* encode */
  142. errn = exiEncodeEndElement(&oStream, &stateEncode, &eqn);
  143. break;
  144. case CHARACTERS:
  145. /* decode */
  146. errn = exiDecodeCharacters(&iStream, &stateDecode, &val);
  147. if (errn < 0) {
  148. printf("[Decode-ERROR] %d \n", errn);
  149. return errn;
  150. }
  151. if (val.type == INTEGER_BIG) {
  152. printf(" CH int64 : %lld \n", val.int64);
  153. } else if (val.type == BINARY_BASE64 || val.type == BINARY_HEX) {
  154. printf(" CH Binary (len == %d) : ", val.binary.len);
  155. for(i=0; i<val.binary.len; i++) {
  156. printf(" [%d]", val.binary.data[i]);
  157. }
  158. printf("\n");
  159. } else if (val.type == BOOLEAN) {
  160. printf(" CH Boolean : %d \n", val.boolean);
  161. } else if (val.type == STRING) {
  162. printf(" CH String (len==%d) : '", val.string.len);
  163. for(i=0;i<val.string.len; i++) {
  164. printf("%c", (char)val.string.codepoints[i]);
  165. }
  166. printf("'\n");
  167. } else {
  168. printf(" CH ?? \n");
  169. }
  170. /* encode */
  171. errn = exiEncodeCharacters(&oStream, &stateEncode, &val);
  172. break;
  173. case ATTRIBUTE:
  174. /* decode */
  175. errn = exiDecodeAttribute(&iStream, &stateDecode, &eqn, &val);
  176. if (errn < 0) {
  177. printf("[Decode-ERROR] %d \n", errn);
  178. return errn;
  179. }
  180. exiGetLocalName(eqn.namespaceURI, eqn.localPart, &localName);
  181. exiGetUri(eqn.namespaceURI, &namespaceURI);
  182. printf(" AT {%s}%s \n", namespaceURI, localName);
  183. /* encode */
  184. errn = exiEncodeAttribute(&oStream, &stateEncode, &eqn, &val);
  185. break;
  186. default:
  187. /* ERROR */
  188. return -1;
  189. }
  190. } while (noEndOfDocument);
  191. /* write to file */
  192. writeBytesToFile(oStream.data, iStream.size, argv[2]);
  193. return 0;
  194. }