ua_types.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186
  1. #include "ua_util.h"
  2. #include "ua_types.h"
  3. #include "ua_statuscodes.h"
  4. #include "ua_types_generated.h"
  5. /*****************/
  6. /* Helper Macros */
  7. /*****************/
  8. #define UA_TYPE_DEFAULT(TYPE) \
  9. UA_TYPE_NEW_DEFAULT(TYPE) \
  10. UA_TYPE_INIT_DEFAULT(TYPE) \
  11. UA_TYPE_DELETEMEMBERS_NOACTION(TYPE) \
  12. UA_TYPE_DELETE_DEFAULT(TYPE) \
  13. UA_TYPE_COPY_DEFAULT(TYPE) \
  14. #define UA_TYPE_NEW_DEFAULT(TYPE) \
  15. TYPE * TYPE##_new() { \
  16. TYPE *p = UA_malloc(sizeof(TYPE)); \
  17. if(p) TYPE##_init(p); \
  18. return p; \
  19. }
  20. #define UA_TYPE_INIT_DEFAULT(TYPE) \
  21. void TYPE##_init(TYPE * p) { \
  22. *p = (TYPE)0; \
  23. }
  24. #define UA_TYPE_DELETEMEMBERS_NOACTION(TYPE) \
  25. void TYPE##_deleteMembers(TYPE *p) { \
  26. }
  27. #define UA_TYPE_DELETE_DEFAULT(TYPE) \
  28. void TYPE##_delete(TYPE *p) { \
  29. TYPE##_deleteMembers(p); \
  30. UA_free(p); \
  31. }
  32. #define UA_TYPE_COPY_DEFAULT(TYPE) \
  33. UA_StatusCode TYPE##_copy(TYPE const *src, TYPE *dst) { \
  34. *dst = *src; \
  35. return UA_STATUSCODE_GOOD; \
  36. }
  37. /*****************/
  38. /* Builtin Types */
  39. /*****************/
  40. /* Boolean */
  41. UA_TYPE_DEFAULT(UA_Boolean)
  42. /* SByte */
  43. UA_TYPE_DEFAULT(UA_SByte)
  44. /* Byte */
  45. UA_TYPE_DEFAULT(UA_Byte)
  46. /* Int16 */
  47. UA_TYPE_DEFAULT(UA_Int16)
  48. /* UInt16 */
  49. UA_TYPE_DEFAULT(UA_UInt16)
  50. /* Int32 */
  51. UA_TYPE_DEFAULT(UA_Int32)
  52. /* UInt32 */
  53. UA_TYPE_DEFAULT(UA_UInt32)
  54. /* Int64 */
  55. UA_TYPE_DEFAULT(UA_Int64)
  56. /* UInt64 */
  57. UA_TYPE_DEFAULT(UA_UInt64)
  58. /* Float */
  59. UA_TYPE_DEFAULT(UA_Float)
  60. /* Double */
  61. UA_TYPE_DEFAULT(UA_Double)
  62. /* String */
  63. UA_TYPE_NEW_DEFAULT(UA_String)
  64. UA_TYPE_DELETE_DEFAULT(UA_String)
  65. void UA_String_init(UA_String *p) {
  66. p->length = -1;
  67. p->data = UA_NULL;
  68. }
  69. void UA_String_deleteMembers(UA_String *p) {
  70. UA_free(p->data);
  71. p->data = UA_NULL;
  72. }
  73. UA_StatusCode UA_String_copy(UA_String const *src, UA_String *dst) {
  74. UA_String_init(dst);
  75. if(src->length > 0) {
  76. if(!(dst->data = UA_malloc((UA_UInt32)src->length)))
  77. return UA_STATUSCODE_BADOUTOFMEMORY;
  78. UA_memcpy((void *)dst->data, src->data, (UA_UInt32)src->length);
  79. }
  80. dst->length = src->length;
  81. return UA_STATUSCODE_GOOD;
  82. }
  83. UA_String UA_String_fromChars(char const *src) {
  84. UA_String str;
  85. size_t length = strlen(src);
  86. if(length == 0) {
  87. str.length = 0;
  88. str.data = UA_NULL;
  89. return str;
  90. }
  91. str.data = UA_malloc(length);
  92. if(!str.data) {
  93. str.length = -1;
  94. return str;
  95. }
  96. UA_memcpy(str.data, src, length);
  97. str.length = length;
  98. return str;
  99. }
  100. #define UA_STRING_COPYPRINTF_BUFSIZE 1024
  101. UA_StatusCode UA_String_copyprintf(char const *fmt, UA_String *dst, ...) {
  102. char src[UA_STRING_COPYPRINTF_BUFSIZE];
  103. va_list ap;
  104. va_start(ap, dst);
  105. #if defined(__GNUC__) || defined(__clang__)
  106. #pragma GCC diagnostic push
  107. #pragma GCC diagnostic ignored "-Wformat-nonliteral"
  108. #endif
  109. // vsnprintf should only take a literal and no variable to be secure
  110. UA_Int32 len = vsnprintf(src, UA_STRING_COPYPRINTF_BUFSIZE, fmt, ap);
  111. #if defined(__GNUC__) || defined(__clang__)
  112. #pragma GCC diagnostic pop
  113. #endif
  114. va_end(ap);
  115. if(len < 0) // FIXME: old glibc 2.0 would return -1 when truncated
  116. return UA_STATUSCODE_BADINTERNALERROR;
  117. // since glibc 2.1 vsnprintf returns the len that would have resulted if buf were large enough
  118. len = ( len > UA_STRING_COPYPRINTF_BUFSIZE ? UA_STRING_COPYPRINTF_BUFSIZE : len );
  119. if(!(dst->data = UA_malloc((UA_UInt32)len)))
  120. return UA_STATUSCODE_BADOUTOFMEMORY;
  121. UA_memcpy((void *)dst->data, src, (UA_UInt32)len);
  122. dst->length = len;
  123. return UA_STATUSCODE_GOOD;
  124. }
  125. UA_Boolean UA_String_equal(const UA_String *string1, const UA_String *string2) {
  126. if(string1->length <= 0 && string2->length <= 0)
  127. return UA_TRUE;
  128. if(string1->length != string2->length)
  129. return UA_FALSE;
  130. // casts are needed to overcome signed warnings
  131. UA_Int32 is = strncmp((char const *)string1->data, (char const *)string2->data, (size_t)string1->length);
  132. return (is == 0) ? UA_TRUE : UA_FALSE;
  133. }
  134. /* DateTime */
  135. #define UNIX_EPOCH_BIAS_SEC 11644473600LL // Number of seconds from 1 Jan. 1601 00:00 to 1 Jan 1970 00:00 UTC
  136. #define HUNDRED_NANOSEC_PER_USEC 10LL
  137. #define HUNDRED_NANOSEC_PER_SEC (HUNDRED_NANOSEC_PER_USEC * 1000000LL)
  138. #ifdef __MINGW32__
  139. #ifndef _TIMEZONE_DEFINED
  140. #define _TIMEZONE_DEFINED
  141. struct timezone {
  142. int tz_minuteswest;
  143. int tz_dsttime;
  144. };
  145. #endif
  146. #endif
  147. #ifdef _WIN32
  148. static const unsigned __int64 epoch = 116444736000000000;
  149. int gettimeofday(struct timeval *tp, struct timezone *tzp);
  150. int gettimeofday(struct timeval *tp, struct timezone *tzp) {
  151. FILETIME ft;
  152. SYSTEMTIME st;
  153. ULARGE_INTEGER ul;
  154. GetSystemTime(&st);
  155. SystemTimeToFileTime(&st, &ft);
  156. ul.LowPart = ft.dwLowDateTime;
  157. ul.HighPart = ft.dwHighDateTime;
  158. tp->tv_sec = (ul.QuadPart - epoch) / 10000000L;
  159. tp->tv_usec = st.wMilliseconds * 1000;
  160. return 0;
  161. }
  162. #endif
  163. UA_DateTime UA_DateTime_now() {
  164. UA_DateTime dateTime;
  165. struct timeval tv;
  166. gettimeofday(&tv, UA_NULL);
  167. dateTime = (tv.tv_sec + UNIX_EPOCH_BIAS_SEC)
  168. * HUNDRED_NANOSEC_PER_SEC + tv.tv_usec * HUNDRED_NANOSEC_PER_USEC;
  169. return dateTime;
  170. }
  171. UA_DateTimeStruct UA_DateTime_toStruct(UA_DateTime atime) {
  172. UA_DateTimeStruct dateTimeStruct;
  173. //calcualting the the milli-, micro- and nanoseconds
  174. dateTimeStruct.nanoSec = (UA_Int16)((atime % 10) * 100);
  175. dateTimeStruct.microSec = (UA_Int16)((atime % 10000) / 10);
  176. dateTimeStruct.milliSec = (UA_Int16)((atime % 10000000) / 10000);
  177. //calculating the unix time with #include <time.h>
  178. time_t secSinceUnixEpoch = (atime/10000000) - UNIX_EPOCH_BIAS_SEC;
  179. struct tm ts = *gmtime(&secSinceUnixEpoch);
  180. dateTimeStruct.sec = (UA_Int16)ts.tm_sec;
  181. dateTimeStruct.min = (UA_Int16)ts.tm_min;
  182. dateTimeStruct.hour = (UA_Int16)ts.tm_hour;
  183. dateTimeStruct.day = (UA_Int16)ts.tm_mday;
  184. dateTimeStruct.month = (UA_Int16)(ts.tm_mon + 1);
  185. dateTimeStruct.year = (UA_Int16)(ts.tm_year + 1900);
  186. return dateTimeStruct;
  187. }
  188. UA_StatusCode UA_DateTime_toString(UA_DateTime atime, UA_String *timeString) {
  189. // length of the string is 31 (incl. \0 at the end)
  190. if(!(timeString->data = UA_malloc(32)))
  191. return UA_STATUSCODE_BADOUTOFMEMORY;
  192. timeString->length = 31;
  193. UA_DateTimeStruct tSt = UA_DateTime_toStruct(atime);
  194. sprintf((char*)timeString->data, "%02d/%02d/%04d %02d:%02d:%02d.%03d.%03d.%03d", tSt.month, tSt.day, tSt.year,
  195. tSt.hour, tSt.min, tSt.sec, tSt.milliSec, tSt.microSec, tSt.nanoSec);
  196. return UA_STATUSCODE_GOOD;
  197. }
  198. /* Guid */
  199. UA_TYPE_NEW_DEFAULT(UA_Guid)
  200. UA_TYPE_DELETEMEMBERS_NOACTION(UA_Guid)
  201. UA_TYPE_DELETE_DEFAULT(UA_Guid)
  202. UA_Boolean UA_Guid_equal(const UA_Guid *g1, const UA_Guid *g2) {
  203. if(memcmp(g1, g2, sizeof(UA_Guid)) == 0)
  204. return UA_TRUE;
  205. return UA_FALSE;
  206. }
  207. UA_Guid UA_Guid_random(UA_UInt32 *seed) {
  208. UA_Guid result;
  209. result.data1 = RAND(seed);
  210. UA_UInt32 r = RAND(seed);
  211. result.data2 = (UA_UInt16) r;
  212. result.data3 = (UA_UInt16) (r >> 16);
  213. r = RAND(seed);
  214. result.data4[0] = (UA_Byte)r;
  215. result.data4[1] = (UA_Byte)(r >> 4);
  216. result.data4[2] = (UA_Byte)(r >> 8);
  217. result.data4[3] = (UA_Byte)(r >> 12);
  218. r = RAND(seed);
  219. result.data4[4] = (UA_Byte)r;
  220. result.data4[5] = (UA_Byte)(r >> 4);
  221. result.data4[6] = (UA_Byte)(r >> 8);
  222. result.data4[7] = (UA_Byte)(r >> 12);
  223. return result;
  224. }
  225. void UA_Guid_init(UA_Guid *p) {
  226. p->data1 = 0;
  227. p->data2 = 0;
  228. p->data3 = 0;
  229. memset(p->data4, 0, sizeof(UA_Byte)*8);
  230. }
  231. UA_StatusCode UA_Guid_copy(UA_Guid const *src, UA_Guid *dst) {
  232. UA_memcpy((void *)dst, (const void *)src, sizeof(UA_Guid));
  233. return UA_STATUSCODE_GOOD;
  234. }
  235. /* ByteString */
  236. UA_StatusCode UA_ByteString_newMembers(UA_ByteString *p, UA_Int32 length) {
  237. if(length > 0) {
  238. if(!(p->data = UA_malloc((UA_UInt32)length)))
  239. return UA_STATUSCODE_BADOUTOFMEMORY;
  240. p->length = length;
  241. } else {
  242. p->data = UA_NULL;
  243. if(length == 0)
  244. p->length = 0;
  245. else
  246. p->length = -1;
  247. }
  248. return UA_STATUSCODE_GOOD;
  249. }
  250. /* XmlElement */
  251. /* NodeId */
  252. UA_TYPE_NEW_DEFAULT(UA_NodeId)
  253. UA_TYPE_DELETE_DEFAULT(UA_NodeId)
  254. void UA_NodeId_init(UA_NodeId *p) {
  255. p->identifierType = UA_NODEIDTYPE_NUMERIC;
  256. p->namespaceIndex = 0;
  257. memset(&p->identifier, 0, sizeof(p->identifier));
  258. }
  259. UA_StatusCode UA_NodeId_copy(UA_NodeId const *src, UA_NodeId *dst) {
  260. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  261. switch(src->identifierType) {
  262. case UA_NODEIDTYPE_NUMERIC:
  263. *dst = *src;
  264. return UA_STATUSCODE_GOOD;
  265. case UA_NODEIDTYPE_STRING: // Table 6, second entry
  266. retval |= UA_String_copy(&src->identifier.string, &dst->identifier.string);
  267. break;
  268. case UA_NODEIDTYPE_GUID: // Table 6, third entry
  269. retval |= UA_Guid_copy(&src->identifier.guid, &dst->identifier.guid);
  270. break;
  271. case UA_NODEIDTYPE_BYTESTRING: // Table 6, "OPAQUE"
  272. retval |= UA_ByteString_copy(&src->identifier.byteString, &dst->identifier.byteString);
  273. break;
  274. default:
  275. UA_NodeId_init(dst);
  276. return UA_STATUSCODE_BADINTERNALERROR;
  277. }
  278. dst->namespaceIndex = src->namespaceIndex;
  279. dst->identifierType = src->identifierType;
  280. if(retval) {
  281. UA_NodeId_deleteMembers(dst);
  282. UA_NodeId_init(dst);
  283. }
  284. return retval;
  285. }
  286. static UA_Boolean UA_NodeId_isBasicType(UA_NodeId const *id) {
  287. return id ->namespaceIndex == 0 && 1 <= id ->identifier.numeric &&
  288. id ->identifier.numeric <= 25;
  289. }
  290. void UA_NodeId_deleteMembers(UA_NodeId *p) {
  291. switch(p->identifierType) {
  292. case UA_NODEIDTYPE_STRING:
  293. case UA_NODEIDTYPE_BYTESTRING:
  294. UA_ByteString_deleteMembers(&p->identifier.byteString);
  295. break;
  296. default:
  297. break;
  298. }
  299. }
  300. UA_Boolean UA_NodeId_equal(const UA_NodeId *n1, const UA_NodeId *n2) {
  301. if(n1->namespaceIndex != n2->namespaceIndex || n1->identifierType!=n2->identifierType)
  302. return UA_FALSE;
  303. switch(n1->identifierType) {
  304. case UA_NODEIDTYPE_NUMERIC:
  305. if(n1->identifier.numeric == n2->identifier.numeric)
  306. return UA_TRUE;
  307. else
  308. return UA_FALSE;
  309. case UA_NODEIDTYPE_STRING:
  310. return UA_String_equal(&n1->identifier.string, &n2->identifier.string);
  311. case UA_NODEIDTYPE_GUID:
  312. return UA_Guid_equal(&n1->identifier.guid, &n2->identifier.guid);
  313. case UA_NODEIDTYPE_BYTESTRING:
  314. return UA_ByteString_equal(&n1->identifier.byteString, &n2->identifier.byteString);
  315. }
  316. return UA_FALSE;
  317. }
  318. UA_Boolean UA_NodeId_isNull(const UA_NodeId *p) {
  319. switch(p->identifierType) {
  320. case UA_NODEIDTYPE_NUMERIC:
  321. if(p->namespaceIndex != 0 || p->identifier.numeric != 0)
  322. return UA_FALSE;
  323. break;
  324. case UA_NODEIDTYPE_STRING:
  325. if(p->namespaceIndex != 0 || p->identifier.string.length > 0)
  326. return UA_FALSE;
  327. break;
  328. case UA_NODEIDTYPE_GUID:
  329. if(p->namespaceIndex != 0 ||
  330. memcmp(&p->identifier.guid, (char[sizeof(UA_Guid)]) { 0 }, sizeof(UA_Guid)) != 0)
  331. return UA_FALSE;
  332. break;
  333. case UA_NODEIDTYPE_BYTESTRING:
  334. if(p->namespaceIndex != 0 || p->identifier.byteString.length > 0)
  335. return UA_FALSE;
  336. break;
  337. default:
  338. return UA_FALSE;
  339. }
  340. return UA_TRUE;
  341. }
  342. /* ExpandedNodeId */
  343. UA_TYPE_NEW_DEFAULT(UA_ExpandedNodeId)
  344. UA_TYPE_DELETE_DEFAULT(UA_ExpandedNodeId)
  345. void UA_ExpandedNodeId_deleteMembers(UA_ExpandedNodeId *p) {
  346. UA_NodeId_deleteMembers(&p->nodeId);
  347. UA_String_deleteMembers(&p->namespaceUri);
  348. }
  349. void UA_ExpandedNodeId_init(UA_ExpandedNodeId *p) {
  350. UA_NodeId_init(&p->nodeId);
  351. UA_String_init(&p->namespaceUri);
  352. p->serverIndex = 0;
  353. }
  354. UA_StatusCode UA_ExpandedNodeId_copy(UA_ExpandedNodeId const *src, UA_ExpandedNodeId *dst) {
  355. UA_StatusCode retval = UA_NodeId_copy(&src->nodeId, &dst->nodeId);
  356. retval |= UA_String_copy(&src->namespaceUri, &dst->namespaceUri);
  357. dst->serverIndex = src->serverIndex;
  358. if(retval) {
  359. UA_ExpandedNodeId_deleteMembers(dst);
  360. UA_ExpandedNodeId_init(dst);
  361. }
  362. return retval;
  363. }
  364. UA_Boolean UA_ExpandedNodeId_isNull(const UA_ExpandedNodeId *p) {
  365. return UA_NodeId_isNull(&p->nodeId);
  366. }
  367. /* StatusCode */
  368. /* QualifiedName */
  369. UA_TYPE_NEW_DEFAULT(UA_QualifiedName)
  370. UA_TYPE_DELETE_DEFAULT(UA_QualifiedName)
  371. void UA_QualifiedName_deleteMembers(UA_QualifiedName *p) {
  372. UA_String_deleteMembers(&p->name);
  373. }
  374. void UA_QualifiedName_init(UA_QualifiedName *p) {
  375. UA_String_init(&p->name);
  376. p->namespaceIndex = 0;
  377. }
  378. UA_StatusCode UA_QualifiedName_copy(UA_QualifiedName const *src, UA_QualifiedName *dst) {
  379. UA_StatusCode retval = UA_String_copy(&src->name, &dst->name);
  380. dst->namespaceIndex = src->namespaceIndex;
  381. if(retval) {
  382. UA_QualifiedName_deleteMembers(dst);
  383. UA_QualifiedName_init(dst);
  384. }
  385. return retval;
  386. }
  387. /* LocalizedText */
  388. UA_TYPE_NEW_DEFAULT(UA_LocalizedText)
  389. UA_TYPE_DELETE_DEFAULT(UA_LocalizedText)
  390. void UA_LocalizedText_deleteMembers(UA_LocalizedText *p) {
  391. UA_String_deleteMembers(&p->locale);
  392. UA_String_deleteMembers(&p->text);
  393. }
  394. void UA_LocalizedText_init(UA_LocalizedText *p) {
  395. UA_String_init(&p->locale);
  396. UA_String_init(&p->text);
  397. }
  398. UA_StatusCode UA_LocalizedText_copy(UA_LocalizedText const *src, UA_LocalizedText *dst) {
  399. UA_StatusCode retval = UA_String_copy(&src->locale, &dst->locale);
  400. retval |= UA_String_copy(&src->text, &dst->text);
  401. if(retval) {
  402. UA_LocalizedText_deleteMembers(dst);
  403. UA_LocalizedText_init(dst);
  404. }
  405. return retval;
  406. }
  407. /* ExtensionObject */
  408. UA_TYPE_NEW_DEFAULT(UA_ExtensionObject)
  409. UA_TYPE_DELETE_DEFAULT(UA_ExtensionObject)
  410. void UA_ExtensionObject_deleteMembers(UA_ExtensionObject *p) {
  411. UA_NodeId_deleteMembers(&p->typeId);
  412. UA_ByteString_deleteMembers(&p->body);
  413. }
  414. void UA_ExtensionObject_init(UA_ExtensionObject *p) {
  415. UA_NodeId_init(&p->typeId);
  416. p->encoding = UA_EXTENSIONOBJECT_ENCODINGMASK_NOBODYISENCODED;
  417. UA_ByteString_init(&p->body);
  418. }
  419. UA_StatusCode UA_ExtensionObject_copy(UA_ExtensionObject const *src, UA_ExtensionObject *dst) {
  420. UA_StatusCode retval = UA_ByteString_copy(&src->body, &dst->body);
  421. retval |= UA_NodeId_copy(&src->typeId, &dst->typeId);
  422. dst->encoding = src->encoding;
  423. if(retval) {
  424. UA_ExtensionObject_deleteMembers(dst);
  425. UA_ExtensionObject_init(dst);
  426. }
  427. return retval;
  428. }
  429. /* DataValue */
  430. UA_TYPE_NEW_DEFAULT(UA_DataValue)
  431. UA_TYPE_DELETE_DEFAULT(UA_DataValue)
  432. void UA_DataValue_deleteMembers(UA_DataValue *p) {
  433. UA_Variant_deleteMembers(&p->value);
  434. }
  435. void UA_DataValue_init(UA_DataValue *p) {
  436. *((UA_Byte*)p) = 0; // zero out the bitfield
  437. p->serverPicoseconds = 0;
  438. UA_DateTime_init(&p->serverTimestamp);
  439. p->sourcePicoseconds = 0;
  440. UA_DateTime_init(&p->sourceTimestamp);
  441. UA_StatusCode_init(&p->status);
  442. UA_Variant_init(&p->value);
  443. }
  444. UA_StatusCode UA_DataValue_copy(UA_DataValue const *src, UA_DataValue *dst) {
  445. UA_DataValue_init(dst);
  446. *((UA_Byte*)dst) = *((const UA_Byte*)src); // the bitfield
  447. UA_StatusCode retval = UA_DateTime_copy(&src->serverTimestamp, &dst->serverTimestamp);
  448. retval |= UA_DateTime_copy(&src->sourceTimestamp, &dst->sourceTimestamp);
  449. retval |= UA_Variant_copy(&src->value, &dst->value);
  450. dst->serverPicoseconds = src->serverPicoseconds;
  451. dst->sourcePicoseconds = src->sourcePicoseconds;
  452. dst->status = src->status;
  453. if(retval) {
  454. UA_DataValue_deleteMembers(dst);
  455. UA_DataValue_init(dst);
  456. }
  457. return retval;
  458. }
  459. /* Variant */
  460. UA_TYPE_NEW_DEFAULT(UA_Variant)
  461. UA_TYPE_DELETE_DEFAULT(UA_Variant)
  462. void UA_Variant_init(UA_Variant *p) {
  463. p->storageType = UA_VARIANT_DATA;
  464. p->arrayLength = -1;
  465. p->data = UA_NULL;
  466. p->arrayDimensions = UA_NULL;
  467. p->arrayDimensionsSize = -1;
  468. p->type = &UA_TYPES[UA_TYPES_BOOLEAN];
  469. }
  470. void UA_Variant_deleteMembers(UA_Variant *p) {
  471. if(p->storageType != UA_VARIANT_DATA)
  472. return;
  473. if(p->data) {
  474. if(p->arrayLength == -1)
  475. p->arrayLength = 1;
  476. UA_Array_delete(p->data, p->type, p->arrayLength);
  477. p->data = UA_NULL;
  478. p->arrayLength = -1;
  479. }
  480. if(p->arrayDimensions) {
  481. UA_free(p->arrayDimensions);
  482. p->arrayDimensions = UA_NULL;
  483. }
  484. }
  485. UA_StatusCode UA_Variant_copy(UA_Variant const *src, UA_Variant *dst) {
  486. UA_Variant_init(dst);
  487. UA_Int32 tmp = src->arrayLength;
  488. if(src->arrayLength == -1 && src->data)
  489. tmp = 1;
  490. UA_StatusCode retval = UA_Array_copy(src->data, &dst->data, src->type, tmp);
  491. if(retval != UA_STATUSCODE_GOOD) {
  492. UA_Variant_deleteMembers(dst);
  493. UA_Variant_init(dst);
  494. return retval;
  495. }
  496. dst->arrayLength = src->arrayLength;
  497. dst->type = src->type;
  498. dst->storageType = UA_VARIANT_DATA;
  499. if(src->arrayDimensions) {
  500. retval |= UA_Array_copy(src->arrayDimensions, (void **)&dst->arrayDimensions,
  501. &UA_TYPES[UA_TYPES_INT32], src->arrayDimensionsSize);
  502. if(retval != UA_STATUSCODE_GOOD) {
  503. UA_Variant_deleteMembers(dst);
  504. UA_Variant_init(dst);
  505. return retval;
  506. }
  507. dst->arrayDimensionsSize = src->arrayDimensionsSize;
  508. }
  509. return retval;
  510. }
  511. /**
  512. * Tests if a range is compatible with a variant. If yes, the following values are set:
  513. * - total: how many elements are indicated by the range
  514. * - block_size: how big is each contiguous block of elements in the variant denoted by the range
  515. * - block_distance: how many elements are between the blocks (beginning to beginning)
  516. * - first_elem: where does the first block begin
  517. */
  518. static UA_StatusCode testRangeWithVariant(const UA_Variant *v, const UA_NumericRange range, size_t *total,
  519. size_t *block_size, size_t *block_distance, size_t *first_elem) {
  520. /* Test the integrity of the source variant dimensions */
  521. UA_Int32 dims_count = 1;
  522. const UA_Int32 *dims = &v->arrayLength; // default: the array has only one dimension
  523. if(v->arrayDimensionsSize > 0) {
  524. dims_count = v->arrayDimensionsSize;
  525. dims = v->arrayDimensions;
  526. UA_Int32 elements = 1;
  527. for(UA_Int32 i = 0; i < dims_count; i++)
  528. elements *= dims[i];
  529. if(elements != v->arrayLength)
  530. return UA_STATUSCODE_BADINTERNALERROR;
  531. }
  532. /* Test the integrity of the range */
  533. size_t count = 1;
  534. if(range.dimensionsSize != dims_count)
  535. return UA_STATUSCODE_BADINTERNALERROR;
  536. for(UA_Int32 i = 0; i < range.dimensionsSize; i++) {
  537. if(range.dimensions[i].min > range.dimensions[i].max)
  538. return UA_STATUSCODE_BADINDEXRANGEINVALID;
  539. if(range.dimensions[i].max > (UA_UInt32)(dims[i]))
  540. return UA_STATUSCODE_BADINDEXRANGENODATA;
  541. count *= (range.dimensions[i].max - range.dimensions[i].min) + 1;
  542. }
  543. /* Compute the block size and the position of the first element */
  544. size_t bs = 0;
  545. size_t bd = 0;
  546. size_t fe = 0;
  547. size_t running_dimssize = 1; // elements per block of dimensions k to k_max
  548. UA_Boolean found_contiguous = UA_FALSE;
  549. for(UA_Int32 k = dims_count - 1; k >= 0; k--) {
  550. if(!found_contiguous && (range.dimensions[k].min != 0 ||
  551. range.dimensions[k].max + 1 != (UA_UInt32)dims[k])) {
  552. found_contiguous = UA_TRUE;
  553. bs = (range.dimensions[k].max - range.dimensions[k].min + 1) * running_dimssize;
  554. bd = dims[k] * running_dimssize;
  555. }
  556. fe += running_dimssize * range.dimensions[k].min;
  557. running_dimssize *= dims[k];
  558. }
  559. *total = count;
  560. *block_size = bs;
  561. *block_distance = bd;
  562. *first_elem = fe;
  563. return UA_STATUSCODE_GOOD;
  564. }
  565. UA_StatusCode UA_Variant_copyRange(const UA_Variant *src, UA_Variant *dst, const UA_NumericRange range) {
  566. size_t count, block_size, block_distance, first_elem;
  567. UA_StatusCode retval = testRangeWithVariant(src, range, &count, &block_size, &block_distance, &first_elem);
  568. if(retval != UA_STATUSCODE_GOOD)
  569. return retval;
  570. UA_Variant_init(dst);
  571. size_t elem_size = src->type->memSize;
  572. dst->data = UA_malloc(elem_size * count);
  573. if(!dst->data)
  574. return UA_STATUSCODE_BADOUTOFMEMORY;
  575. /* Copy the range */
  576. size_t block_count = count / block_size;
  577. uintptr_t nextdst = (uintptr_t)dst->data;
  578. uintptr_t nextsrc = (uintptr_t)src->data + (elem_size * first_elem);
  579. if(src->type->fixedSize) {
  580. for(size_t i = 0; i < block_count; i++) {
  581. memcpy((void*)nextdst, (void*)nextsrc, elem_size * block_size);
  582. nextdst += block_size * elem_size;
  583. nextsrc += block_distance * elem_size;
  584. }
  585. } else {
  586. for(size_t i = 0; i < block_count; i++) {
  587. for(size_t j = 0; j < block_size && retval == UA_STATUSCODE_GOOD; j++) {
  588. retval = UA_copy((const void*)nextsrc, (void*)nextdst, src->type);
  589. nextdst += elem_size;
  590. nextsrc += elem_size;
  591. }
  592. nextsrc += (block_distance - block_size) * elem_size;
  593. }
  594. if(retval != UA_STATUSCODE_GOOD) {
  595. size_t copied = ((nextdst - elem_size) - (uintptr_t)dst->data) / elem_size;
  596. UA_Array_delete(dst->data, src->type, copied);
  597. return retval;
  598. }
  599. }
  600. /* Copy the range dimensions*/
  601. if(src->arrayDimensionsSize > 0) {
  602. dst->arrayDimensions = UA_malloc(sizeof(UA_Int32) * src->arrayDimensionsSize);
  603. if(!dst->arrayDimensions) {
  604. UA_Array_delete(dst->data, src->type, count);
  605. return UA_STATUSCODE_BADOUTOFMEMORY;
  606. }
  607. for(UA_Int32 k = 0; k < src->arrayDimensionsSize; k++)
  608. dst->arrayDimensions[k] = range.dimensions[k].max - range.dimensions[k].min + 1;
  609. dst->arrayDimensionsSize = src->arrayDimensionsSize;
  610. }
  611. dst->arrayLength = count;
  612. dst->type = src->type;
  613. return UA_STATUSCODE_GOOD;
  614. }
  615. UA_StatusCode UA_Variant_setRange(UA_Variant *v, void *data, const UA_NumericRange range) {
  616. size_t count, block_size, block_distance, first_elem;
  617. UA_StatusCode retval = testRangeWithVariant(v, range, &count, &block_size, &block_distance, &first_elem);
  618. if(retval != UA_STATUSCODE_GOOD)
  619. return retval;
  620. size_t block_count = count / block_size;
  621. size_t elem_size = v->type->memSize;
  622. uintptr_t nextdst = (uintptr_t)v->data + (first_elem * elem_size);
  623. uintptr_t nextsrc = (uintptr_t)data;
  624. if(v->type->fixedSize) {
  625. for(size_t i = 0; i < block_count; i++) {
  626. memcpy((void*)nextdst, (void*)nextsrc, elem_size * block_size);
  627. nextdst += block_size * elem_size;
  628. nextsrc += block_distance * elem_size;
  629. }
  630. } else {
  631. for(size_t i = 0; i < block_count; i++) {
  632. for(size_t j = 0; j < block_size; j++) {
  633. UA_deleteMembers((void*)nextdst, v->type);
  634. nextdst += elem_size;
  635. }
  636. nextdst -= block_size * elem_size;
  637. memcpy((void*)nextdst, (void*)nextsrc, elem_size * block_size);
  638. nextdst += block_size * elem_size;
  639. nextsrc += block_distance * elem_size;
  640. }
  641. }
  642. return UA_STATUSCODE_GOOD;
  643. }
  644. UA_StatusCode UA_Variant_setCopyRange(const UA_Variant *src, UA_Variant *dst, void *data,
  645. const UA_NumericRange range) {
  646. // todo: don't copy the entire variant but only the retained parts
  647. UA_StatusCode retval = UA_Variant_copy(src, dst);
  648. if(retval != UA_STATUSCODE_GOOD)
  649. return retval;
  650. retval = UA_Variant_setRange(dst, data, range);
  651. if(retval != UA_STATUSCODE_GOOD) {
  652. UA_Variant_deleteMembers(dst);
  653. return retval;
  654. }
  655. return UA_STATUSCODE_GOOD;
  656. }
  657. UA_StatusCode UA_Variant_setScalar(UA_Variant *v, void *p, const UA_DataType *type) {
  658. return UA_Variant_setArray(v, p, -1, type);
  659. }
  660. UA_StatusCode UA_Variant_setScalarCopy(UA_Variant *v, const void *p, const UA_DataType *type) {
  661. void *new = UA_malloc(type->memSize);
  662. if(!new)
  663. return UA_STATUSCODE_BADOUTOFMEMORY;
  664. UA_StatusCode retval = UA_copy(p, new, type);
  665. if(retval != UA_STATUSCODE_GOOD) {
  666. UA_delete(new, type);
  667. return retval;
  668. }
  669. return UA_Variant_setArray(v, new, -1, type);
  670. }
  671. UA_StatusCode UA_Variant_setArray(UA_Variant *v, void *array, UA_Int32 noElements,
  672. const UA_DataType *type) {
  673. v->type = type;
  674. v->arrayLength = noElements;
  675. v->data = array;
  676. return UA_STATUSCODE_GOOD;
  677. }
  678. UA_StatusCode UA_Variant_setArrayCopy(UA_Variant *v, const void *array, UA_Int32 noElements,
  679. const UA_DataType *type) {
  680. void *new;
  681. UA_StatusCode retval = UA_Array_copy(array, &new, type, noElements);
  682. if(retval != UA_STATUSCODE_GOOD)
  683. return retval;
  684. return UA_Variant_setArray(v, new, noElements, type);
  685. }
  686. /* DiagnosticInfo */
  687. UA_TYPE_NEW_DEFAULT(UA_DiagnosticInfo)
  688. UA_TYPE_DELETE_DEFAULT(UA_DiagnosticInfo)
  689. void UA_DiagnosticInfo_deleteMembers(UA_DiagnosticInfo *p) {
  690. UA_String_deleteMembers(&p->additionalInfo);
  691. if(p->hasInnerDiagnosticInfo && p->innerDiagnosticInfo) {
  692. UA_DiagnosticInfo_delete(p->innerDiagnosticInfo);
  693. p->innerDiagnosticInfo = UA_NULL;
  694. }
  695. }
  696. void UA_DiagnosticInfo_init(UA_DiagnosticInfo *p) {
  697. *((UA_Byte*)p) = 0; // zero out the bitfield
  698. p->symbolicId = 0;
  699. p->namespaceUri = 0;
  700. p->localizedText = 0;
  701. p->locale = 0;
  702. UA_String_init(&p->additionalInfo);
  703. p->innerDiagnosticInfo = UA_NULL;
  704. UA_StatusCode_init(&p->innerStatusCode);
  705. }
  706. UA_StatusCode UA_DiagnosticInfo_copy(UA_DiagnosticInfo const *src, UA_DiagnosticInfo *dst) {
  707. UA_DiagnosticInfo_init(dst);
  708. *((UA_Byte*)dst) = *((const UA_Byte*)src); // the bitfield
  709. dst->symbolicId = src->symbolicId;
  710. dst->namespaceUri = src->namespaceUri;
  711. dst->localizedText = src->localizedText;
  712. dst->locale = src->locale;
  713. dst->innerStatusCode = src->innerStatusCode;
  714. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  715. if(src->hasAdditionalInfo)
  716. retval = UA_String_copy(&src->additionalInfo, &dst->additionalInfo);
  717. if(src->hasInnerDiagnosticInfo && src->innerDiagnosticInfo) {
  718. if((dst->innerDiagnosticInfo = UA_malloc(sizeof(UA_DiagnosticInfo)))) {
  719. retval |= UA_DiagnosticInfo_copy(src->innerDiagnosticInfo, dst->innerDiagnosticInfo);
  720. dst->hasInnerDiagnosticInfo = src->hasInnerDiagnosticInfo;
  721. }
  722. else
  723. retval |= UA_STATUSCODE_BADOUTOFMEMORY;
  724. }
  725. if(retval) {
  726. UA_DiagnosticInfo_deleteMembers(dst);
  727. UA_DiagnosticInfo_init(dst);
  728. }
  729. return retval;
  730. }
  731. /*******************/
  732. /* Structure Types */
  733. /*******************/
  734. void UA_init(void *p, const UA_DataType *dataType) {
  735. /* Do not check if the index is a builtin-type here. Builtins will be called
  736. with their very own _init functions normally. In the off case, that the
  737. generic function is called with the index of a builtin, their layout
  738. contains a single member of the builtin type, that will be inited in the
  739. for loop. */
  740. uintptr_t ptr = (uintptr_t)p;
  741. UA_Byte membersSize = dataType->membersSize;
  742. for(size_t i=0;i<membersSize; i++) {
  743. const UA_DataTypeMember *member = &dataType->members[i];
  744. if(member->isArray) {
  745. /* Padding contains bit-magic to split into padding before and after
  746. the length integer */
  747. ptr += (member->padding >> 3);
  748. *((UA_Int32*)ptr) = -1;
  749. ptr += sizeof(UA_Int32) + (member->padding & 0x07);
  750. *((void**)ptr) = UA_NULL;
  751. ptr += sizeof(void*);
  752. continue;
  753. }
  754. ptr += member->padding;
  755. if(!member->namespaceZero) {
  756. // pointer arithmetic
  757. const UA_DataType *memberType = dataType - dataType->typeIndex + member->memberTypeIndex;
  758. UA_init((void*)ptr, memberType);
  759. ptr += memberType->memSize;
  760. continue;
  761. }
  762. switch(member->memberTypeIndex) {
  763. case UA_TYPES_BOOLEAN:
  764. case UA_TYPES_SBYTE:
  765. case UA_TYPES_BYTE:
  766. *(UA_Byte*)ptr = 0;
  767. break;
  768. case UA_TYPES_INT16:
  769. case UA_TYPES_UINT16:
  770. *(UA_Int16*)ptr = 0;
  771. break;
  772. case UA_TYPES_INT32:
  773. case UA_TYPES_UINT32:
  774. case UA_TYPES_STATUSCODE:
  775. case UA_TYPES_FLOAT:
  776. *(UA_Int32*)ptr = 0;
  777. break;
  778. case UA_TYPES_INT64:
  779. case UA_TYPES_UINT64:
  780. case UA_TYPES_DOUBLE:
  781. case UA_TYPES_DATETIME:
  782. *(UA_Int64*)ptr = 0;
  783. break;
  784. case UA_TYPES_GUID:
  785. UA_Guid_init((UA_Guid*)ptr);
  786. break;
  787. case UA_TYPES_NODEID:
  788. UA_NodeId_init((UA_NodeId*)ptr);
  789. break;
  790. case UA_TYPES_EXPANDEDNODEID:
  791. UA_ExpandedNodeId_init((UA_ExpandedNodeId*)ptr);
  792. break;
  793. case UA_TYPES_LOCALIZEDTEXT:
  794. UA_LocalizedText_init((UA_LocalizedText*)ptr);
  795. break;
  796. case UA_TYPES_EXTENSIONOBJECT:
  797. UA_ExtensionObject_init((UA_ExtensionObject*)ptr);
  798. break;
  799. case UA_TYPES_DATAVALUE:
  800. UA_DataValue_init((UA_DataValue*)ptr);
  801. break;
  802. case UA_TYPES_VARIANT:
  803. UA_Variant_init((UA_Variant*)ptr);
  804. break;
  805. case UA_TYPES_DIAGNOSTICINFO:
  806. UA_DiagnosticInfo_init((UA_DiagnosticInfo*)ptr);
  807. break;
  808. default:
  809. // QualifiedName, LocalizedText and strings are treated as structures, also
  810. UA_init((void*)ptr, &UA_TYPES[member->memberTypeIndex]);
  811. }
  812. ptr += UA_TYPES[member->memberTypeIndex].memSize;
  813. }
  814. }
  815. void * UA_new(const UA_DataType *dataType) {
  816. void *p = UA_malloc(dataType->memSize);
  817. if(p)
  818. UA_init(p, dataType);
  819. return p;
  820. }
  821. UA_StatusCode UA_copy(const void *src, void *dst, const UA_DataType *dataType) {
  822. if(dataType->fixedSize) {
  823. memcpy(dst, src, dataType->memSize);
  824. return UA_STATUSCODE_GOOD;
  825. }
  826. UA_init(dst, dataType);
  827. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  828. uintptr_t ptrs = (uintptr_t)src;
  829. uintptr_t ptrd = (uintptr_t)dst;
  830. UA_Byte membersSize = dataType->membersSize;
  831. for(size_t i=0;i<membersSize; i++) {
  832. const UA_DataTypeMember *member = &dataType->members[i];
  833. const UA_DataType *memberType;
  834. if(member->namespaceZero)
  835. memberType = &UA_TYPES[member->memberTypeIndex];
  836. else
  837. memberType = dataType - dataType->typeIndex + member->memberTypeIndex;
  838. if(member->isArray) {
  839. ptrs += (member->padding >> 3);
  840. ptrd += (member->padding >> 3);
  841. UA_Int32 *dstNoElements = (UA_Int32*)ptrd;
  842. const UA_Int32 noElements = *((const UA_Int32*)ptrs);
  843. ptrs += sizeof(UA_Int32) + (member->padding & 0x07);
  844. ptrd += sizeof(UA_Int32) + (member->padding & 0x07);
  845. retval = UA_Array_copy(*(void* const*)ptrs, (void**)ptrd, memberType, noElements);
  846. if(retval != UA_STATUSCODE_GOOD) {
  847. UA_deleteMembers(dst, dataType);
  848. UA_init(dst, dataType);
  849. return retval;
  850. }
  851. *dstNoElements = noElements;
  852. ptrs += sizeof(void*);
  853. ptrd += sizeof(void*);
  854. continue;
  855. }
  856. ptrs += member->padding;
  857. ptrd += member->padding;
  858. if(!member->namespaceZero) {
  859. retval = UA_copy((const void*)ptrs, (void*)ptrd, memberType);
  860. if(retval != UA_STATUSCODE_GOOD) {
  861. UA_deleteMembers(dst, dataType);
  862. UA_init(dst, dataType);
  863. return retval;
  864. }
  865. ptrs += memberType->memSize;
  866. ptrd += memberType->memSize;
  867. continue;
  868. }
  869. switch(member->memberTypeIndex) {
  870. case UA_TYPES_BOOLEAN:
  871. case UA_TYPES_SBYTE:
  872. case UA_TYPES_BYTE:
  873. *((UA_Byte*)ptrd) = *((const UA_Byte*)ptrs);
  874. break;
  875. case UA_TYPES_INT16:
  876. case UA_TYPES_UINT16:
  877. *((UA_Int16*)ptrd) = *((const UA_Byte*)ptrs);
  878. break;
  879. case UA_TYPES_INT32:
  880. case UA_TYPES_UINT32:
  881. case UA_TYPES_STATUSCODE:
  882. case UA_TYPES_FLOAT:
  883. *((UA_Int32*)ptrd) = *((const UA_Int32*)ptrs);
  884. break;
  885. case UA_TYPES_INT64:
  886. case UA_TYPES_UINT64:
  887. case UA_TYPES_DOUBLE:
  888. case UA_TYPES_DATETIME:
  889. *((UA_Int64*)ptrd) = *((const UA_Int64*)ptrs);
  890. break;
  891. case UA_TYPES_GUID:
  892. *((UA_Guid*)ptrd) = *((const UA_Guid*)ptrs);
  893. break;
  894. case UA_TYPES_NODEID:
  895. retval |= UA_NodeId_copy((const UA_NodeId*)ptrs, (UA_NodeId*)ptrd);
  896. break;
  897. case UA_TYPES_EXPANDEDNODEID:
  898. retval |= UA_ExpandedNodeId_copy((const UA_ExpandedNodeId*)ptrs, (UA_ExpandedNodeId*)ptrd);
  899. break;
  900. case UA_TYPES_LOCALIZEDTEXT:
  901. retval |= UA_LocalizedText_copy((const UA_LocalizedText*)ptrs, (UA_LocalizedText*)ptrd);
  902. break;
  903. case UA_TYPES_EXTENSIONOBJECT:
  904. retval |= UA_ExtensionObject_copy((const UA_ExtensionObject*)ptrs, (UA_ExtensionObject*)ptrd);
  905. break;
  906. case UA_TYPES_DATAVALUE:
  907. retval |= UA_DataValue_copy((const UA_DataValue*)ptrs, (UA_DataValue*)ptrd);
  908. break;
  909. case UA_TYPES_VARIANT:
  910. retval |= UA_Variant_copy((const UA_Variant*)ptrs, (UA_Variant*)ptrd);
  911. break;
  912. case UA_TYPES_DIAGNOSTICINFO:
  913. retval |= UA_DiagnosticInfo_copy((const UA_DiagnosticInfo*)ptrs, (UA_DiagnosticInfo*)ptrd);
  914. break;
  915. default:
  916. // QualifiedName, LocalizedText and strings are treated as structures, also
  917. retval |= UA_copy((const void *)ptrs, (void*)ptrd, memberType);
  918. }
  919. ptrs += memberType->memSize;
  920. ptrd += memberType->memSize;
  921. }
  922. if(retval != UA_STATUSCODE_GOOD) {
  923. UA_deleteMembers(dst, dataType);
  924. UA_init(dst, dataType);
  925. }
  926. return retval;
  927. }
  928. void UA_deleteMembers(void *p, const UA_DataType *dataType) {
  929. uintptr_t ptr = (uintptr_t)p;
  930. if(dataType->fixedSize)
  931. return;
  932. UA_Byte membersSize = dataType->membersSize;
  933. for(size_t i=0;i<membersSize; i++) {
  934. const UA_DataTypeMember *member = &dataType->members[i];
  935. const UA_DataType *memberType;
  936. if(member->namespaceZero)
  937. memberType = &UA_TYPES[member->memberTypeIndex];
  938. else
  939. memberType = dataType - dataType->typeIndex + member->memberTypeIndex;
  940. if(member->isArray) {
  941. ptr += (member->padding >> 3);
  942. UA_Int32 noElements = *((UA_Int32*)ptr);
  943. ptr += sizeof(UA_Int32) + (member->padding & 0x07);
  944. UA_Array_delete(*(void**)ptr, memberType, noElements);
  945. *(void**)ptr = UA_NULL;
  946. ptr += sizeof(void*);
  947. continue;
  948. }
  949. ptr += member->padding;
  950. if(!member->namespaceZero) {
  951. UA_deleteMembers((void*)ptr, memberType);
  952. ptr += memberType->memSize;
  953. continue;
  954. }
  955. switch(member->memberTypeIndex) {
  956. case UA_TYPES_BOOLEAN:
  957. case UA_TYPES_SBYTE:
  958. case UA_TYPES_BYTE:
  959. case UA_TYPES_INT16:
  960. case UA_TYPES_UINT16:
  961. case UA_TYPES_INT32:
  962. case UA_TYPES_UINT32:
  963. case UA_TYPES_STATUSCODE:
  964. case UA_TYPES_FLOAT:
  965. case UA_TYPES_INT64:
  966. case UA_TYPES_UINT64:
  967. case UA_TYPES_DOUBLE:
  968. case UA_TYPES_DATETIME:
  969. case UA_TYPES_GUID:
  970. break;
  971. case UA_TYPES_NODEID:
  972. UA_NodeId_deleteMembers((UA_NodeId*)ptr);
  973. break;
  974. case UA_TYPES_EXPANDEDNODEID:
  975. UA_ExpandedNodeId_deleteMembers((UA_ExpandedNodeId*)ptr);
  976. break;
  977. case UA_TYPES_LOCALIZEDTEXT:
  978. UA_LocalizedText_deleteMembers((UA_LocalizedText*)ptr);
  979. break;
  980. case UA_TYPES_EXTENSIONOBJECT:
  981. UA_ExtensionObject_deleteMembers((UA_ExtensionObject*)ptr);
  982. break;
  983. case UA_TYPES_DATAVALUE:
  984. UA_DataValue_deleteMembers((UA_DataValue*)ptr);
  985. break;
  986. case UA_TYPES_VARIANT:
  987. UA_Variant_deleteMembers((UA_Variant*)ptr);
  988. break;
  989. case UA_TYPES_DIAGNOSTICINFO:
  990. UA_DiagnosticInfo_deleteMembers((UA_DiagnosticInfo*)ptr);
  991. break;
  992. default:
  993. // QualifiedName, LocalizedText and strings are treated as structures, also
  994. UA_deleteMembers((void*)ptr, memberType);
  995. }
  996. ptr += memberType->memSize;
  997. }
  998. }
  999. void UA_delete(void *p, const UA_DataType *dataType) {
  1000. UA_deleteMembers(p, dataType);
  1001. UA_free(p);
  1002. }
  1003. /******************/
  1004. /* Array Handling */
  1005. /******************/
  1006. void* UA_Array_new(const UA_DataType *dataType, UA_Int32 noElements) {
  1007. if(noElements <= 0)
  1008. return UA_NULL;
  1009. if((UA_Int32)dataType->memSize * noElements < 0 || dataType->memSize * noElements > MAX_ARRAY_SIZE )
  1010. return UA_NULL;
  1011. if(dataType->fixedSize)
  1012. return calloc(noElements, dataType->memSize);
  1013. void *p = malloc(dataType->memSize * (size_t)noElements);
  1014. if(!p)
  1015. return p;
  1016. uintptr_t ptr = (uintptr_t)p;
  1017. for(int i = 0; i<noElements; i++) {
  1018. UA_init((void*)ptr, dataType);
  1019. ptr += dataType->memSize;
  1020. }
  1021. return p;
  1022. }
  1023. UA_StatusCode UA_Array_copy(const void *src, void **dst, const UA_DataType *dataType, UA_Int32 noElements) {
  1024. if(noElements <= 0) {
  1025. *dst = UA_NULL;
  1026. return UA_STATUSCODE_GOOD;
  1027. }
  1028. if(!(*dst = UA_malloc((size_t)noElements * dataType->memSize)))
  1029. return UA_STATUSCODE_BADOUTOFMEMORY;
  1030. if(dataType->fixedSize) {
  1031. memcpy(*dst, src, dataType->memSize * (size_t)noElements);
  1032. return UA_STATUSCODE_GOOD;
  1033. }
  1034. uintptr_t ptrs = (uintptr_t)src;
  1035. uintptr_t ptrd = (uintptr_t)*dst;
  1036. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  1037. for(int i=0;i<noElements; i++) {
  1038. retval = UA_copy((void*)ptrs, (void*)ptrd, dataType);
  1039. ptrs += dataType->memSize;
  1040. ptrd += dataType->memSize;
  1041. }
  1042. if(retval != UA_STATUSCODE_GOOD)
  1043. UA_Array_delete(*dst, dataType, noElements);
  1044. return retval;
  1045. }
  1046. void UA_Array_delete(void *p, const UA_DataType *dataType, UA_Int32 noElements) {
  1047. if(!dataType->fixedSize) {
  1048. uintptr_t ptr = (uintptr_t)p;
  1049. for(UA_Int32 i = 0; i<noElements; i++) {
  1050. UA_deleteMembers((void*)ptr, dataType);
  1051. ptr += dataType->memSize;
  1052. }
  1053. }
  1054. UA_free(p);
  1055. }