ua2json.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  4. *
  5. * Copyright 2018 (c) Fraunhofer IOSB (Author: Julius Pfrommer)
  6. */
  7. /* Enable POSIX features */
  8. #if !defined(_XOPEN_SOURCE)
  9. # define _XOPEN_SOURCE 600
  10. #endif
  11. #ifndef _DEFAULT_SOURCE
  12. # define _DEFAULT_SOURCE
  13. #endif
  14. /* On older systems we need to define _BSD_SOURCE.
  15. * _DEFAULT_SOURCE is an alias for that. */
  16. #ifndef _BSD_SOURCE
  17. # define _BSD_SOURCE
  18. #endif
  19. #include <stdio.h>
  20. #include <ua_types.h>
  21. /* Internal headers */
  22. #include "ua_types_generated.h"
  23. #include "ua_types_generated_handling.h"
  24. #include "ua_types_encoding_binary.h"
  25. #include "ua_types_encoding_json.h"
  26. static UA_StatusCode
  27. encode(const UA_ByteString *buf, UA_ByteString *out,
  28. const UA_DataType *type) {
  29. void *data = malloc(type->memSize);
  30. if(!data)
  31. return UA_STATUSCODE_BADOUTOFMEMORY;
  32. size_t offset = 0;
  33. UA_StatusCode retval = UA_decodeBinary(buf, &offset, data, type, NULL);
  34. if(retval != UA_STATUSCODE_GOOD) {
  35. free(data);
  36. return retval;
  37. }
  38. if(offset != buf->length) {
  39. UA_delete(data, type);
  40. fprintf(stderr, "Input buffer not completely read\n");
  41. return UA_STATUSCODE_BADINTERNALERROR;
  42. }
  43. size_t jsonLength = UA_calcSizeJson(data, type, NULL, 0, NULL, 0, true);
  44. retval = UA_ByteString_allocBuffer(out, jsonLength);
  45. if(retval != UA_STATUSCODE_GOOD) {
  46. UA_delete(data, type);
  47. return retval;
  48. }
  49. uint8_t *bufPos = &out->data[0];
  50. const uint8_t *bufEnd = &out->data[out->length];
  51. retval = UA_encodeJson(data, type, &bufPos, &bufEnd, NULL, 0, NULL, 0, true);
  52. UA_delete(data, type);
  53. if(retval != UA_STATUSCODE_GOOD) {
  54. UA_ByteString_deleteMembers(out);
  55. return retval;
  56. }
  57. out->length = (size_t)((uintptr_t)bufPos - (uintptr_t)out->data);
  58. return UA_STATUSCODE_GOOD;
  59. }
  60. static UA_StatusCode
  61. decode(const UA_ByteString *buf, UA_ByteString *out,
  62. const UA_DataType *type) {
  63. void *data = malloc(type->memSize);
  64. if(!data)
  65. return UA_STATUSCODE_BADOUTOFMEMORY;
  66. UA_StatusCode retval = UA_decodeJson(buf, data, type);
  67. if(retval != UA_STATUSCODE_GOOD) {
  68. free(data);
  69. return retval;
  70. }
  71. size_t binLength = UA_calcSizeBinary(data, type);
  72. retval = UA_ByteString_allocBuffer(out, binLength);
  73. if(retval != UA_STATUSCODE_GOOD) {
  74. UA_delete(data, type);
  75. return retval;
  76. }
  77. uint8_t *bufPos = &out->data[0];
  78. const uint8_t *bufEnd = &out->data[out->length];
  79. retval = UA_encodeBinary(data, type, &bufPos, &bufEnd, NULL, NULL);
  80. UA_delete(data, type);
  81. if(retval != UA_STATUSCODE_GOOD) {
  82. UA_ByteString_deleteMembers(out);
  83. return retval;
  84. }
  85. out->length = (size_t)((uintptr_t)bufPos - (uintptr_t)out->data);
  86. return UA_STATUSCODE_GOOD;
  87. }
  88. static void
  89. usage(void) {
  90. printf("Usage: ua2json [encode|decode] [-t dataType] [-o outputFile] [inputFile]\n"
  91. "- encode/decode: Translate UA binary input to UA JSON / "
  92. "Translate UA JSON input to UA binary (required)\n"
  93. "- dataType: UA DataType of the input (default: Variant)\n"
  94. "- outputFile: Output target (default: write to stdout)\n"
  95. "- inputFile: Input source (default: read from stdin)\n");
  96. }
  97. int main(int argc, char **argv) {
  98. UA_Boolean encode_option = true;
  99. const char *datatype_option = "Variant";
  100. const char *input_option = NULL;
  101. const char *output_option = NULL;
  102. UA_ByteString outbuf = UA_BYTESTRING_NULL;
  103. UA_ByteString buf = UA_BYTESTRING_NULL;
  104. FILE *in = stdin;
  105. FILE *out = stdout;
  106. int retcode = -1;
  107. /* Read the command line options */
  108. if(argc < 2) {
  109. usage();
  110. return 0;
  111. }
  112. if(strcmp(argv[1], "encode") == 0) {
  113. encode_option = true;
  114. } else if(strcmp(argv[1], "decode") == 0) {
  115. encode_option = false;
  116. } else {
  117. fprintf(stderr, "Error: The first argument must be \"encode\" or \"decode\"\n");
  118. return -1;
  119. }
  120. for(int argpos = 2; argpos < argc; argpos++) {
  121. if(strcmp(argv[argpos], "--help") == 0) {
  122. usage();
  123. return 0;
  124. }
  125. if(strcmp(argv[argpos], "-t") == 0) {
  126. if(argpos + 1 == argc) {
  127. usage();
  128. return -1;
  129. }
  130. argpos++;
  131. datatype_option = argv[argpos];
  132. continue;
  133. }
  134. if(strcmp(argv[argpos], "-o") == 0) {
  135. if(argpos + 1 == argc) {
  136. usage();
  137. return -1;
  138. }
  139. argpos++;
  140. output_option = argv[argpos];
  141. continue;
  142. }
  143. if(argpos + 1 == argc) {
  144. input_option = argv[argpos];
  145. continue;
  146. }
  147. usage();
  148. return -1;
  149. }
  150. /* Find the data type */
  151. const UA_DataType *type = NULL;
  152. for(size_t i = 0; i < UA_TYPES_COUNT; ++i) {
  153. if(strcmp(datatype_option, UA_TYPES[i].typeName) == 0) {
  154. type = &UA_TYPES[i];
  155. break;
  156. }
  157. }
  158. if(!type) {
  159. fprintf(stderr, "Error: Datatype not found\n");
  160. return -1;
  161. }
  162. /* Open files */
  163. if(input_option) {
  164. in = fopen(input_option, "rb");
  165. if(!in) {
  166. fprintf(stderr, "Could not open input file %s\n", input_option);
  167. goto cleanup;
  168. }
  169. }
  170. if(output_option) {
  171. out = fopen(output_option, "wb");
  172. if(!out) {
  173. fprintf(stderr, "Could not open output file %s\n", output_option);
  174. goto cleanup;
  175. }
  176. }
  177. /* Read input until EOF */
  178. size_t pos = 0;
  179. size_t length = 128;
  180. while(true) {
  181. if(pos >= buf.length) {
  182. length = length * 8;
  183. UA_Byte *r = (UA_Byte*)realloc(buf.data, length);
  184. if(!r) {
  185. fprintf(stderr, "Out of memory\n");
  186. goto cleanup;
  187. }
  188. buf.length = length;
  189. buf.data = r;
  190. }
  191. ssize_t c = read(fileno(in), &buf.data[pos], length - pos);
  192. if(c == 0)
  193. break;
  194. if(c < 0) {
  195. fprintf(stderr, "Reading from input failed\n");
  196. goto cleanup;
  197. }
  198. pos += (size_t)c;
  199. }
  200. if(pos == 0) {
  201. fprintf(stderr, "No input\n");
  202. goto cleanup;
  203. }
  204. buf.length = pos;
  205. /* Convert */
  206. UA_StatusCode result;
  207. if(encode_option)
  208. result = encode(&buf, &outbuf, type);
  209. else
  210. result = decode(&buf, &outbuf, type);
  211. if(result != UA_STATUSCODE_GOOD) {
  212. fprintf(stderr, "Error: Parsing failed with code %s\n",
  213. UA_StatusCode_name(result));
  214. goto cleanup;
  215. }
  216. /* Print the output and quit */
  217. fwrite(outbuf.data, 1, outbuf.length, out);
  218. retcode = 0;
  219. cleanup:
  220. UA_ByteString_deleteMembers(&buf);
  221. UA_ByteString_deleteMembers(&outbuf);
  222. if(in != stdin)
  223. fclose(in);
  224. if(out != stdout)
  225. fclose(out);
  226. return retcode;
  227. }