main_codec.c 6.4 KB

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