opcua_binaryEncDec.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. /*
  10. * convert byte array to Byte
  11. */
  12. Byte convertToByte(const char *buf, int pos)
  13. {
  14. return (Byte)buf[pos];
  15. }
  16. /*
  17. * convert byte array to UInt16
  18. */
  19. UInt16 convertToUInt16(const char* buf, int pos)
  20. {
  21. Byte t1 = buf[pos];
  22. Int32 t2 = (UInt16)(buf[pos+1] << 8);
  23. return t1 + t2;
  24. }
  25. /*
  26. * convert byte array to Int32
  27. */
  28. Int32 convertToInt32(const char* buf, int pos)
  29. {
  30. SByte t1 = buf[pos];
  31. Int32 t2 = (UInt32)(buf[pos+1] << 8);
  32. Int32 t3 = (UInt32)(buf[pos+2] << 16);
  33. Int32 t4 = (UInt32)(buf[pos+3] << 24);
  34. return t1 + t2 + t3 + t4;
  35. }
  36. /*
  37. * convert byte array to UInt32
  38. */
  39. UInt32 convertToUInt32(const char* buf, int pos)
  40. {
  41. Byte t1 = buf[pos];
  42. UInt32 t2 = (UInt32)(buf[pos+1] << 8);
  43. UInt32 t3 = (UInt32)(buf[pos+2] << 16);
  44. UInt32 t4 = (UInt32)(buf[pos+3] << 24);
  45. return t1 + t2 + t3 + t4;
  46. }
  47. void convertUInt32ToByteArray(UInt32 value,char *buf,int pos)
  48. {
  49. memcpy(buf,&value,sizeof(value));
  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. /*
  57. * convert byte array to Int64
  58. */
  59. Int64 convertToInt64(const char* buf, int pos)
  60. {
  61. SByte t1 = buf[pos];
  62. UInt64 t2 = (UInt64)(buf[pos+1] << 8);
  63. UInt64 t3 = (UInt64)(buf[pos+2] << 16);
  64. UInt64 t4 = (UInt64)(buf[pos+3] << 24);
  65. UInt64 t5 = (UInt64)(buf[pos+4] << 32);
  66. UInt64 t6 = (UInt64)(buf[pos+5] << 40);
  67. UInt64 t7 = (UInt64)(buf[pos+6] << 48);
  68. UInt64 t8 = (UInt64)(buf[pos+7] << 56);
  69. return t1 + t2 + t3 + t4 + t5 + t6 + t7 + t8;
  70. }
  71. convertToUAString(const char* buf, int pos,UA_String *dstUAString)
  72. {
  73. dstUAString->Length = convertToInt32(buf,pos);
  74. if(dstUAString->Length > 0)
  75. {
  76. dstUAString->Data = &(buf[sizeof(UInt32)]);
  77. }
  78. else
  79. {
  80. dstUAString->Length = 0;
  81. dstUAString->Data = NULL;
  82. }
  83. }
  84. convertToUAGuid(const char* buf, int pos,UA_Guid* dstGUID)
  85. {
  86. int counter = 0;
  87. UInt32 i = 0;
  88. for(i = 1; i <= 4; i++)
  89. {
  90. dstGUID->Data1[i] = convertToUInt32(*buf, pos+counter);
  91. counter += sizeof(UInt32);
  92. }
  93. for(i = 1; i <= 2; i++)
  94. {
  95. dstGUID->Data2[i] = convertToUInt16(*buf, pos+counter);
  96. counter += sizeof(UInt16);
  97. }
  98. for(i = 1; i <= 2; i++)
  99. {
  100. dstGUID->Data3[i] = convertToUInt16(*buf, pos+counter);
  101. counter += sizeof(UInt16);
  102. }
  103. for(i = 1; i <= 8; i++)
  104. {
  105. dstGUID->Data4[i] = convertToByte(*buf, pos+counter);
  106. counter += sizeof(Byte);
  107. }
  108. }
  109. UA_ByteString convertToUAByteString(const char* buf, int pos){
  110. UA_ByteString tmpUAByteString;
  111. int counter = sizeof(Int32);
  112. int i = 0;
  113. tmpUAByteString.Length = convertToInt32(buf, pos);
  114. Byte byteStringData[tmpUAByteString.Length];
  115. if(tmpUAByteString.Length == -1){
  116. return tmpUAByteString;
  117. }else{
  118. for(i = 0; i < tmpUAByteString.Length; i++)
  119. {
  120. byteStringData[i] = convertToByte(buf, pos+counter);
  121. counter += sizeof(Byte);
  122. }
  123. }
  124. tmpUAByteString.Data = byteStringData;
  125. return tmpUAByteString;
  126. }
  127. UA_DateTime convertToUADateTime(const char* buf, int pos){
  128. UA_DateTime tmpUADateTime;
  129. tmpUADateTime = convertToInt64(buf, pos);
  130. return tmpUADateTime;
  131. }
  132. UA_StatusCode convertToUAStatusCode(const char* buf, int pos){
  133. return convertToUInt32(buf, pos);
  134. }
  135. void convertToUANodeId(const char* buf, int pos, UA_NodeId* dstNodeId){
  136. dstNodeId->EncodingByte = convertToInt32(buf, 0);
  137. int counter = sizeof(UInt32);
  138. UA_NodeIdEncodingValuesType encodingType = dstNodeId->EncodingByte;
  139. switch(encodingType)
  140. {
  141. case NIEVT_TWO_BYTE:
  142. {
  143. dstNodeId->Identifier.Numeric = convertToInt32(buf, counter);
  144. counter += sizeof(UInt16);
  145. break;
  146. }
  147. case NIEVT_FOUR_BYTE:
  148. {
  149. dstNodeId->Identifier.Numeric = convertToInt32(buf, counter);
  150. counter += sizeof(Int64);
  151. break;
  152. }
  153. case NIEVT_NUMERIC:
  154. {
  155. dstNodeId->Identifier.Numeric = convertToInt32(buf, counter);
  156. counter += sizeof(UInt32);
  157. break;
  158. }
  159. case NIEVT_STRING:
  160. {
  161. convertToUAString(buf, counter,&dstNodeId->Identifier.String);
  162. counter += sizeof(sizeof(UInt32) + dstNodeId->Identifier.String.Length);
  163. break;
  164. }
  165. case NIEVT_GUID:
  166. {
  167. convertToUAGuid(buf, counter,&dstNodeId->Identifier.Guid);
  168. counter += sizeof(UA_Guid);
  169. break;
  170. }
  171. case NIEVT_BYTESTRING:
  172. {
  173. dstNodeId->Identifier.OPAQUE = convertToUAByteString(buf, counter);
  174. //If Length == -1 then the ByteString is null
  175. if(dstNodeId->Identifier.OPAQUE.Length == -1)
  176. {
  177. counter += sizeof(Int32);
  178. }else{
  179. counter += sizeof(Int32)+sizeof(Byte)*dstNodeId->Identifier.OPAQUE.Length;
  180. }
  181. break;
  182. }
  183. default:
  184. break;
  185. }
  186. }