opcua_binaryEncDec.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /*
  2. * opcua_binaryEncDec.c
  3. *
  4. * Created on: Dec 18, 2013
  5. * Author: opcua
  6. */
  7. #include "opcua_binaryEncDec.h"
  8. #include "opcua_types.h"
  9. const char *TEST_PASSED = "PASSED";
  10. /*
  11. * convert byte array to Byte
  12. */
  13. Byte convertToByte(char* buf, int pos)
  14. {
  15. return (Byte)buf[pos];
  16. }
  17. /*
  18. * convert byte array to UInt16
  19. */
  20. UInt16 convertToUInt16(char* buf, int pos)
  21. {
  22. Byte t1 = buf[pos];
  23. Int32 t2 = (UInt16)(buf[pos+1] << 8);
  24. return t1 + t2;
  25. }
  26. /*
  27. * convert byte array to Int32
  28. */
  29. Int32 convertToInt32(char* buf, int pos)
  30. {
  31. SByte t1 = buf[pos];
  32. Int32 t2 = (UInt32)(buf[pos+1] << 8);
  33. Int32 t3 = (UInt32)(buf[pos+2] << 16);
  34. Int32 t4 = (UInt32)(buf[pos+3] << 24);
  35. return t1 + t2 + t3 + t4;
  36. }
  37. /*
  38. * convert byte array to UInt32
  39. */
  40. UInt32 convertToUInt32(char* buf, int pos)
  41. {
  42. Byte t1 = buf[pos];
  43. UInt32 t2 = (UInt32)(buf[pos+1] << 8);
  44. UInt32 t3 = (UInt32)(buf[pos+2] << 16);
  45. UInt32 t4 = (UInt32)(buf[pos+3] << 24);
  46. return t1 + t2 + t3 + t4;
  47. }
  48. void convertUInt32ToByteArray(UInt32 value,char *buf,int pos)
  49. {
  50. buf[pos] = (char)(value && 0xFF);
  51. buf[pos + 1] = (char)((value >> 8) && 0xFF);
  52. buf[pos + 2] = (char)((value >> 16) && 0xFF);
  53. buf[pos + 3] = (char)((value >> 24) && 0xFF);
  54. }
  55. /*
  56. * convert byte array to Int64
  57. */
  58. Int64 convertToInt64(char* buf, int pos)
  59. {
  60. SByte t1 = buf[pos];
  61. UInt64 t2 = (UInt64)(buf[pos+1] << 8);
  62. UInt64 t3 = (UInt64)(buf[pos+2] << 16);
  63. UInt64 t4 = (UInt64)(buf[pos+3] << 24);
  64. UInt64 t5 = (UInt64)(buf[pos+4] << 32);
  65. UInt64 t6 = (UInt64)(buf[pos+5] << 40);
  66. UInt64 t7 = (UInt64)(buf[pos+6] << 48);
  67. UInt64 t8 = (UInt64)(buf[pos+7] << 56);
  68. return t1 + t2 + t3 + t4 + t5 + t6 + t7 + t8;
  69. }
  70. <<<<<<< HEAD
  71. Int64 convertToInt64_test(char* buf, int pos)
  72. {
  73. printf("");
  74. }
  75. UA_String convertToUAString(char* buf, int pos)
  76. =======
  77. convertToUAString(char* buf, int pos,UA_String *dstUAString)
  78. >>>>>>> branch 'master' of https://github.com/Stasik0/Open62541.git
  79. {
  80. dstUAString->Length = convertToInt32(buf,pos);
  81. if(dstUAString->Length > 0)
  82. {
  83. dstUAString->Data = &(buf[sizeof(UInt32)]);
  84. }
  85. else
  86. {
  87. dstUAString->Length = 0;
  88. dstUAString->Data = NULL;
  89. }
  90. }
  91. convertToUAGuid(char* buf, int pos,UA_Guid* dstGUID)
  92. {
  93. int counter = 0;
  94. UInt32 i = 0;
  95. for(i = 1; i <= 4; i++)
  96. {
  97. <<<<<<< HEAD
  98. tmpUAGuid.Data1[i] = convertToUInt32(buf, pos+counter);
  99. =======
  100. dstGUID->Data1[i] = convertToUInt32(*buf, pos+counter);
  101. >>>>>>> branch 'master' of https://github.com/Stasik0/Open62541.git
  102. counter += sizeof(UInt32);
  103. }
  104. for(i = 1; i <= 2; i++)
  105. {
  106. <<<<<<< HEAD
  107. tmpUAGuid.Data2[i] = convertToUInt16(buf, pos+counter);
  108. =======
  109. dstGUID->Data2[i] = convertToUInt16(*buf, pos+counter);
  110. >>>>>>> branch 'master' of https://github.com/Stasik0/Open62541.git
  111. counter += sizeof(UInt16);
  112. }
  113. for(i = 1; i <= 2; i++)
  114. {
  115. <<<<<<< HEAD
  116. tmpUAGuid.Data3[i] = convertToUInt16(buf, pos+counter);
  117. =======
  118. dstGUID->Data3[i] = convertToUInt16(*buf, pos+counter);
  119. >>>>>>> branch 'master' of https://github.com/Stasik0/Open62541.git
  120. counter += sizeof(UInt16);
  121. }
  122. for(i = 1; i <= 8; i++)
  123. {
  124. <<<<<<< HEAD
  125. tmpUAGuid.Data4[i] = convertToByte(buf, pos+counter);
  126. =======
  127. dstGUID->Data4[i] = convertToByte(*buf, pos+counter);
  128. >>>>>>> branch 'master' of https://github.com/Stasik0/Open62541.git
  129. counter += sizeof(Byte);
  130. }
  131. }
  132. <<<<<<< HEAD
  133. UA_ByteString convertToUAByteString(char* buf, int pos){
  134. UA_ByteString tmpUAByteString;
  135. int counter = sizeof(Int32);
  136. int i = 0;
  137. tmpUAByteString.Length = convertToInt32(buf, pos);
  138. Byte byteStringData[tmpUAByteString.Length];
  139. if(tmpUAByteString.Length == -1){
  140. return tmpUAByteString;
  141. }else{
  142. for(i = 0; i < tmpUAByteString.Length; i++)
  143. {
  144. byteStringData[i] = convertToByte(buf, pos+counter);
  145. counter += sizeof(Byte);
  146. }
  147. }
  148. tmpUAByteString.Data = byteStringData;
  149. return tmpUAByteString;
  150. }
  151. UA_DateTime convertToUADateTime(char* buf, int pos){
  152. UA_DateTime tmpUADateTime;
  153. tmpUADateTime = convertToInt64(buf, pos);
  154. return tmpUADateTime;
  155. }
  156. UA_StatusCode convertToUAStatusCode(char* buf, int pos){
  157. return convertToUInt32(buf, pos);
  158. }
  159. UA_NodeId convertToUANodeId(char* buf, int pos){
  160. UA_NodeId tmpUANodeId;
  161. tmpUANodeId.EncodingByte = convertToInt32(buf, 0);
  162. =======
  163. void convertToUANodeId(char* buf, int pos, UA_NodeId* dstNodeId){
  164. dstNodeId->EncodingByte = convertToInt32(buf, 0);
  165. >>>>>>> branch 'master' of https://github.com/Stasik0/Open62541.git
  166. int counter = sizeof(UInt32);
  167. UA_NodeIdEncodingValuesType encodingType = dstNodeId->EncodingByte;
  168. switch(encodingType)
  169. {
  170. case NIEVT_TWO_BYTE:
  171. {
  172. <<<<<<< HEAD
  173. tmpUANodeId.Identifier.Numeric = convertToInt32(buf, counter);
  174. =======
  175. dstNodeId->Identifier.Numeric = convertToInt32(buf, counter);
  176. >>>>>>> branch 'master' of https://github.com/Stasik0/Open62541.git
  177. counter += sizeof(UInt16);
  178. break;
  179. }
  180. case NIEVT_FOUR_BYTE:
  181. {
  182. <<<<<<< HEAD
  183. tmpUANodeId.Identifier.Numeric = convertToInt32(buf, counter);
  184. =======
  185. dstNodeId->Identifier.Numeric = convertToInt32(buf, counter);
  186. >>>>>>> branch 'master' of https://github.com/Stasik0/Open62541.git
  187. counter += sizeof(Int64);
  188. break;
  189. }
  190. case NIEVT_NUMERIC:
  191. {
  192. <<<<<<< HEAD
  193. tmpUANodeId.Identifier.Numeric = convertToInt32(buf, counter);
  194. =======
  195. dstNodeId->Identifier.Numeric = convertToInt32(buf, counter);
  196. >>>>>>> branch 'master' of https://github.com/Stasik0/Open62541.git
  197. counter += sizeof(UInt32);
  198. break;
  199. }
  200. case NIEVT_STRING:
  201. {
  202. <<<<<<< HEAD
  203. tmpUANodeId.Identifier.String = convertToUAString(buf, counter);
  204. counter += sizeof(sizeof(UInt32) + tmpUANodeId.Identifier.String.Length*sizeof(char));
  205. =======
  206. convertToUAString(buf, counter,&dstNodeId->Identifier.String);
  207. counter += sizeof(sizeof(UInt32) + dstNodeId->Identifier.String.Length);
  208. >>>>>>> branch 'master' of https://github.com/Stasik0/Open62541.git
  209. break;
  210. }
  211. case NIEVT_GUID:
  212. {
  213. <<<<<<< HEAD
  214. tmpUANodeId.Identifier.Guid = convertToUAGuid(buf, counter);
  215. =======
  216. convertToUAGuid(buf, counter,&dstNodeId->Identifier.Guid);
  217. >>>>>>> branch 'master' of https://github.com/Stasik0/Open62541.git
  218. counter += sizeof(UA_Guid);
  219. break;
  220. }
  221. case NIEVT_BYTESTRING:
  222. {
  223. tmpUANodeId.Identifier.OPAQUE = convertToUAByteString(buf, counter);
  224. //If Length == -1 then the ByteString is null
  225. if(tmpUANodeId.Identifier.OPAQUE.Length == -1)
  226. {
  227. counter += sizeof(Int32);
  228. }else{
  229. counter += sizeof(Int32)+sizeof(Byte)*tmpUANodeId.Identifier.OPAQUE.Length;
  230. }
  231. break;
  232. }
  233. default:
  234. break;
  235. }
  236. }