ua_types.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292
  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 2014-2017 (c) Fraunhofer IOSB (Author: Julius Pfrommer)
  6. * Copyright 2014, 2016-2017 (c) Florian Palm
  7. * Copyright 2014-2016 (c) Sten Grüner
  8. * Copyright 2014 (c) Leon Urbas
  9. * Copyright 2015 (c) Chris Iatrou
  10. * Copyright 2015 (c) Markus Graube
  11. * Copyright 2015 (c) Reza Ebrahimi
  12. * Copyright 2015-2016 (c) Oleksiy Vasylyev
  13. * Copyright 2017 (c) Stefan Profanter, fortiss GmbH
  14. * Copyright 2016 (c) Lorenz Haas
  15. */
  16. #include <open62541/types.h>
  17. #include <open62541/types_generated.h>
  18. #include <open62541/types_generated_handling.h>
  19. #include "ua_util_internal.h"
  20. #include "libc_time.h"
  21. #include "pcg_basic.h"
  22. /* Datatype Handling
  23. * -----------------
  24. * This file contains handling functions for the builtin types and functions
  25. * handling of structured types and arrays. These need type descriptions in a
  26. * UA_DataType structure. The UA_DataType structures as well as all non-builtin
  27. * datatypes are autogenerated. */
  28. /* Global definition of NULL type instances. These are always zeroed out, as
  29. * mandated by the C/C++ standard for global values with no initializer. */
  30. const UA_String UA_STRING_NULL = {0, NULL};
  31. const UA_ByteString UA_BYTESTRING_NULL = {0, NULL};
  32. const UA_Guid UA_GUID_NULL = {0, 0, 0, {0,0,0,0,0,0,0,0}};
  33. const UA_NodeId UA_NODEID_NULL = {0, UA_NODEIDTYPE_NUMERIC, {0}};
  34. const UA_ExpandedNodeId UA_EXPANDEDNODEID_NULL = {{0, UA_NODEIDTYPE_NUMERIC, {0}}, {0, NULL}, 0};
  35. typedef UA_StatusCode (*UA_copySignature)(const void *src, void *dst,
  36. const UA_DataType *type);
  37. typedef void (*UA_clearSignature)(void *p, const UA_DataType *type);
  38. extern const UA_copySignature copyJumpTable[UA_DATATYPEKINDS];
  39. extern const UA_clearSignature clearJumpTable[UA_DATATYPEKINDS];
  40. /* TODO: The standard-defined types are ordered. See if binary search is
  41. * more efficient. */
  42. const UA_DataType *
  43. UA_findDataType(const UA_NodeId *typeId) {
  44. if(typeId->identifierType != UA_NODEIDTYPE_NUMERIC)
  45. return NULL;
  46. /* Always look in built-in types first
  47. * (may contain data types from all namespaces) */
  48. for(size_t i = 0; i < UA_TYPES_COUNT; ++i) {
  49. if(UA_TYPES[i].typeId.identifier.numeric == typeId->identifier.numeric
  50. && UA_TYPES[i].typeId.namespaceIndex == typeId->namespaceIndex)
  51. return &UA_TYPES[i];
  52. }
  53. /* TODO When other namespace look in custom types, too, requires access to custom types array here! */
  54. /*if(typeId->namespaceIndex != 0) {
  55. size_t customTypesArraySize;
  56. const UA_DataType *customTypesArray;
  57. UA_getCustomTypes(&customTypesArraySize, &customTypesArray);
  58. for(size_t i = 0; i < customTypesArraySize; ++i) {
  59. if(customTypesArray[i].typeId.identifier.numeric == typeId->identifier.numeric
  60. && customTypesArray[i].typeId.namespaceIndex == typeId->namespaceIndex)
  61. return &customTypesArray[i];
  62. }
  63. }*/
  64. return NULL;
  65. }
  66. /***************************/
  67. /* Random Number Generator */
  68. /***************************/
  69. //TODO is this safe for multithreading?
  70. static pcg32_random_t UA_rng = PCG32_INITIALIZER;
  71. void
  72. UA_random_seed(u64 seed) {
  73. pcg32_srandom_r(&UA_rng, seed, (u64)UA_DateTime_now());
  74. }
  75. u32
  76. UA_UInt32_random(void) {
  77. return (u32)pcg32_random_r(&UA_rng);
  78. }
  79. /*****************/
  80. /* Builtin Types */
  81. /*****************/
  82. UA_String
  83. UA_String_fromChars(const char *src) {
  84. UA_String s; s.length = 0; s.data = NULL;
  85. if(!src)
  86. return s;
  87. s.length = strlen(src);
  88. if(s.length > 0) {
  89. s.data = (u8*)UA_malloc(s.length);
  90. if(!s.data) {
  91. s.length = 0;
  92. return s;
  93. }
  94. memcpy(s.data, src, s.length);
  95. } else {
  96. s.data = (u8*)UA_EMPTY_ARRAY_SENTINEL;
  97. }
  98. return s;
  99. }
  100. UA_Boolean
  101. UA_String_equal(const UA_String *s1, const UA_String *s2) {
  102. if(s1->length != s2->length)
  103. return false;
  104. if(s1->length == 0)
  105. return true;
  106. i32 is = memcmp((char const*)s1->data,
  107. (char const*)s2->data, s1->length);
  108. return (is == 0) ? true : false;
  109. }
  110. static UA_StatusCode
  111. String_copy(UA_String const *src, UA_String *dst, const UA_DataType *_) {
  112. UA_StatusCode retval = UA_Array_copy(src->data, src->length, (void**)&dst->data,
  113. &UA_TYPES[UA_TYPES_BYTE]);
  114. if(retval == UA_STATUSCODE_GOOD)
  115. dst->length = src->length;
  116. return retval;
  117. }
  118. static void
  119. String_clear(UA_String *s, const UA_DataType *_) {
  120. UA_Array_delete(s->data, s->length, &UA_TYPES[UA_TYPES_BYTE]);
  121. }
  122. /* QualifiedName */
  123. static UA_StatusCode
  124. QualifiedName_copy(const UA_QualifiedName *src, UA_QualifiedName *dst, const UA_DataType *_) {
  125. dst->namespaceIndex = src->namespaceIndex;
  126. return String_copy(&src->name, &dst->name, NULL);
  127. }
  128. static void
  129. QualifiedName_clear(UA_QualifiedName *p, const UA_DataType *_) {
  130. String_clear(&p->name, NULL);
  131. }
  132. UA_Boolean
  133. UA_QualifiedName_equal(const UA_QualifiedName *qn1,
  134. const UA_QualifiedName *qn2) {
  135. if(qn1 == NULL || qn2 == NULL)
  136. return false;
  137. if(qn1->namespaceIndex != qn2->namespaceIndex)
  138. return false;
  139. if(qn1->name.length != qn2->name.length)
  140. return false;
  141. return (memcmp((char const*)qn1->name.data,
  142. (char const*)qn2->name.data, qn1->name.length) == 0);
  143. }
  144. /* DateTime */
  145. UA_DateTimeStruct
  146. UA_DateTime_toStruct(UA_DateTime t) {
  147. /* Calculating the the milli-, micro- and nanoseconds */
  148. UA_DateTimeStruct dateTimeStruct;
  149. if(t >= 0) {
  150. dateTimeStruct.nanoSec = (u16)((t % 10) * 100);
  151. dateTimeStruct.microSec = (u16)((t % 10000) / 10);
  152. dateTimeStruct.milliSec = (u16)((t % 10000000) / 10000);
  153. } else {
  154. dateTimeStruct.nanoSec = (u16)(((t % 10 + t) % 10) * 100);
  155. dateTimeStruct.microSec = (u16)(((t % 10000 + t) % 10000) / 10);
  156. dateTimeStruct.milliSec = (u16)(((t % 10000000 + t) % 10000000) / 10000);
  157. }
  158. /* Calculating the unix time with #include <time.h> */
  159. long long secSinceUnixEpoch = (long long)(t / UA_DATETIME_SEC)
  160. - (long long)(UA_DATETIME_UNIX_EPOCH / UA_DATETIME_SEC);
  161. struct mytm ts;
  162. memset(&ts, 0, sizeof(struct mytm));
  163. __secs_to_tm(secSinceUnixEpoch, &ts);
  164. dateTimeStruct.sec = (u16)ts.tm_sec;
  165. dateTimeStruct.min = (u16)ts.tm_min;
  166. dateTimeStruct.hour = (u16)ts.tm_hour;
  167. dateTimeStruct.day = (u16)ts.tm_mday;
  168. dateTimeStruct.month = (u16)(ts.tm_mon + 1);
  169. dateTimeStruct.year = (u16)(ts.tm_year + 1900);
  170. return dateTimeStruct;
  171. }
  172. UA_DateTime
  173. UA_DateTime_fromStruct(UA_DateTimeStruct ts) {
  174. /* Seconds since the Unix epoch */
  175. struct mytm tm;
  176. memset(&tm, 0, sizeof(struct mytm));
  177. tm.tm_year = ts.year - 1900;
  178. tm.tm_mon = ts.month - 1;
  179. tm.tm_mday = ts.day;
  180. tm.tm_hour = ts.hour;
  181. tm.tm_min = ts.min;
  182. tm.tm_sec = ts.sec;
  183. long long sec_epoch = __tm_to_secs(&tm);
  184. UA_DateTime t = UA_DATETIME_UNIX_EPOCH;
  185. t += sec_epoch * UA_DATETIME_SEC;
  186. t += ts.milliSec * UA_DATETIME_MSEC;
  187. t += ts.microSec * UA_DATETIME_USEC;
  188. t += ts.nanoSec / 100;
  189. return t;
  190. }
  191. /* Guid */
  192. UA_Boolean
  193. UA_Guid_equal(const UA_Guid *g1, const UA_Guid *g2) {
  194. if(memcmp(g1, g2, sizeof(UA_Guid)) == 0)
  195. return true;
  196. return false;
  197. }
  198. UA_Guid
  199. UA_Guid_random(void) {
  200. UA_Guid result;
  201. result.data1 = (u32)pcg32_random_r(&UA_rng);
  202. u32 r = (u32)pcg32_random_r(&UA_rng);
  203. result.data2 = (u16) r;
  204. result.data3 = (u16) (r >> 16);
  205. r = (u32)pcg32_random_r(&UA_rng);
  206. result.data4[0] = (u8)r;
  207. result.data4[1] = (u8)(r >> 4);
  208. result.data4[2] = (u8)(r >> 8);
  209. result.data4[3] = (u8)(r >> 12);
  210. r = (u32)pcg32_random_r(&UA_rng);
  211. result.data4[4] = (u8)r;
  212. result.data4[5] = (u8)(r >> 4);
  213. result.data4[6] = (u8)(r >> 8);
  214. result.data4[7] = (u8)(r >> 12);
  215. return result;
  216. }
  217. /* ByteString */
  218. UA_StatusCode
  219. UA_ByteString_allocBuffer(UA_ByteString *bs, size_t length) {
  220. UA_ByteString_init(bs);
  221. if(length == 0)
  222. return UA_STATUSCODE_GOOD;
  223. bs->data = (u8*)UA_malloc(length);
  224. if(!bs->data)
  225. return UA_STATUSCODE_BADOUTOFMEMORY;
  226. bs->length = length;
  227. return UA_STATUSCODE_GOOD;
  228. }
  229. /* NodeId */
  230. static void
  231. NodeId_clear(UA_NodeId *p, const UA_DataType *_) {
  232. switch(p->identifierType) {
  233. case UA_NODEIDTYPE_STRING:
  234. case UA_NODEIDTYPE_BYTESTRING:
  235. String_clear(&p->identifier.string, NULL);
  236. break;
  237. default: break;
  238. }
  239. }
  240. static UA_StatusCode
  241. NodeId_copy(UA_NodeId const *src, UA_NodeId *dst, const UA_DataType *_) {
  242. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  243. switch(src->identifierType) {
  244. case UA_NODEIDTYPE_NUMERIC:
  245. *dst = *src;
  246. return UA_STATUSCODE_GOOD;
  247. case UA_NODEIDTYPE_STRING:
  248. retval |= UA_String_copy(&src->identifier.string,
  249. &dst->identifier.string);
  250. break;
  251. case UA_NODEIDTYPE_GUID:
  252. retval |= UA_Guid_copy(&src->identifier.guid, &dst->identifier.guid);
  253. break;
  254. case UA_NODEIDTYPE_BYTESTRING:
  255. retval |= UA_ByteString_copy(&src->identifier.byteString,
  256. &dst->identifier.byteString);
  257. break;
  258. default:
  259. return UA_STATUSCODE_BADINTERNALERROR;
  260. }
  261. dst->namespaceIndex = src->namespaceIndex;
  262. dst->identifierType = src->identifierType;
  263. return retval;
  264. }
  265. UA_Boolean
  266. UA_NodeId_isNull(const UA_NodeId *p) {
  267. if(p->namespaceIndex != 0)
  268. return false;
  269. switch (p->identifierType) {
  270. case UA_NODEIDTYPE_NUMERIC:
  271. return (p->identifier.numeric == 0);
  272. case UA_NODEIDTYPE_STRING:
  273. return UA_String_equal(&p->identifier.string, &UA_STRING_NULL);
  274. case UA_NODEIDTYPE_GUID:
  275. return UA_Guid_equal(&p->identifier.guid, &UA_GUID_NULL);
  276. case UA_NODEIDTYPE_BYTESTRING:
  277. return UA_ByteString_equal(&p->identifier.byteString, &UA_BYTESTRING_NULL);
  278. }
  279. return false;
  280. }
  281. /* Absolute ordering for NodeIds */
  282. UA_Order
  283. UA_NodeId_order(const UA_NodeId *n1, const UA_NodeId *n2) {
  284. /* Compare namespaceIndex */
  285. if(n1->namespaceIndex < n2->namespaceIndex)
  286. return UA_ORDER_LESS;
  287. if(n1->namespaceIndex > n2->namespaceIndex)
  288. return UA_ORDER_MORE;
  289. /* Compare identifierType */
  290. if(n1->identifierType < n2->identifierType)
  291. return UA_ORDER_LESS;
  292. if(n1->identifierType > n2->identifierType)
  293. return UA_ORDER_MORE;
  294. /* Compare the identifier */
  295. switch(n1->identifierType) {
  296. case UA_NODEIDTYPE_NUMERIC:
  297. if(n1->identifier.numeric < n2->identifier.numeric)
  298. return UA_ORDER_LESS;
  299. if(n1->identifier.numeric > n2->identifier.numeric)
  300. return UA_ORDER_MORE;
  301. break;
  302. case UA_NODEIDTYPE_GUID:
  303. if(n1->identifier.guid.data1 < n2->identifier.guid.data1) {
  304. return UA_ORDER_LESS;
  305. } else if(n1->identifier.guid.data1 > n2->identifier.guid.data1) {
  306. return UA_ORDER_MORE;
  307. } else if(n1->identifier.guid.data2 < n2->identifier.guid.data2) {
  308. return UA_ORDER_LESS;
  309. } else if(n1->identifier.guid.data2 > n2->identifier.guid.data2) {
  310. return UA_ORDER_MORE;
  311. } else if(n1->identifier.guid.data3 < n2->identifier.guid.data3) {
  312. return UA_ORDER_LESS;
  313. } else if(n1->identifier.guid.data3 > n2->identifier.guid.data3) {
  314. return UA_ORDER_MORE;
  315. } else {
  316. int cmp = memcmp(n1->identifier.guid.data4, n2->identifier.guid.data4, 8);
  317. if(cmp < 0) return UA_ORDER_LESS;
  318. if(cmp > 0) return UA_ORDER_MORE;
  319. }
  320. break;
  321. case UA_NODEIDTYPE_STRING:
  322. case UA_NODEIDTYPE_BYTESTRING: {
  323. size_t minLength = UA_MIN(n1->identifier.string.length, n2->identifier.string.length);
  324. int cmp = strncmp((const char*)n1->identifier.string.data,
  325. (const char*)n2->identifier.string.data,
  326. minLength);
  327. if(cmp < 0)
  328. return UA_ORDER_LESS;
  329. if(cmp > 0)
  330. return UA_ORDER_MORE;
  331. if(n1->identifier.string.length < n2->identifier.string.length)
  332. return UA_ORDER_LESS;
  333. if(n1->identifier.string.length > n2->identifier.string.length)
  334. return UA_ORDER_MORE;
  335. break;
  336. }
  337. default:
  338. break;
  339. }
  340. return UA_ORDER_EQ;
  341. }
  342. /* FNV non-cryptographic hash function. See
  343. * https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function */
  344. #define FNV_PRIME_32 16777619
  345. static u32
  346. fnv32(u32 fnv, const u8 *buf, size_t size) {
  347. for(size_t i = 0; i < size; ++i) {
  348. fnv = fnv ^ (buf[i]);
  349. fnv = fnv * FNV_PRIME_32;
  350. }
  351. return fnv;
  352. }
  353. u32
  354. UA_NodeId_hash(const UA_NodeId *n) {
  355. switch(n->identifierType) {
  356. case UA_NODEIDTYPE_NUMERIC:
  357. default:
  358. // shift knuth multiplication to use highest 32 bits and after addition make sure we don't have an integer overflow
  359. return (u32)((n->namespaceIndex + ((n->identifier.numeric * (u64)2654435761) >> (32))) & UINT32_C(4294967295)); /* Knuth's multiplicative hashing */
  360. case UA_NODEIDTYPE_STRING:
  361. case UA_NODEIDTYPE_BYTESTRING:
  362. return fnv32(n->namespaceIndex, n->identifier.string.data, n->identifier.string.length);
  363. case UA_NODEIDTYPE_GUID:
  364. return fnv32(n->namespaceIndex, (const u8*)&n->identifier.guid, sizeof(UA_Guid));
  365. }
  366. }
  367. /* ExpandedNodeId */
  368. static void
  369. ExpandedNodeId_clear(UA_ExpandedNodeId *p, const UA_DataType *_) {
  370. NodeId_clear(&p->nodeId, _);
  371. String_clear(&p->namespaceUri, NULL);
  372. }
  373. static UA_StatusCode
  374. ExpandedNodeId_copy(UA_ExpandedNodeId const *src, UA_ExpandedNodeId *dst,
  375. const UA_DataType *_) {
  376. UA_StatusCode retval = NodeId_copy(&src->nodeId, &dst->nodeId, NULL);
  377. retval |= UA_String_copy(&src->namespaceUri, &dst->namespaceUri);
  378. dst->serverIndex = src->serverIndex;
  379. return retval;
  380. }
  381. UA_Order
  382. UA_ExpandedNodeId_order(const UA_ExpandedNodeId *n1,
  383. const UA_ExpandedNodeId *n2) {
  384. if(n1->serverIndex > n2->serverIndex)
  385. return UA_ORDER_MORE;
  386. if(n1->serverIndex < n2->serverIndex)
  387. return UA_ORDER_LESS;
  388. if(n1->namespaceUri.length > 0) {
  389. if(n1->namespaceUri.length > n2->namespaceUri.length)
  390. return UA_ORDER_MORE;
  391. if(n1->namespaceUri.length < n2->namespaceUri.length)
  392. return UA_ORDER_LESS;
  393. int cmp = strncmp((const char*)n1->namespaceUri.data,
  394. (const char*)n2->namespaceUri.data,
  395. n1->namespaceUri.length);
  396. if(cmp < 0)
  397. return UA_ORDER_LESS;
  398. if(cmp > 0)
  399. return UA_ORDER_MORE;
  400. }
  401. return UA_NodeId_order(&n1->nodeId, &n2->nodeId);
  402. }
  403. u32
  404. UA_ExpandedNodeId_hash(const UA_ExpandedNodeId *n) {
  405. u32 h = UA_NodeId_hash(&n->nodeId);
  406. h = fnv32(h, (const UA_Byte*)&n->serverIndex, 4);
  407. return fnv32(h, n->namespaceUri.data, n->namespaceUri.length);
  408. }
  409. /* ExtensionObject */
  410. static void
  411. ExtensionObject_clear(UA_ExtensionObject *p, const UA_DataType *_) {
  412. switch(p->encoding) {
  413. case UA_EXTENSIONOBJECT_ENCODED_NOBODY:
  414. case UA_EXTENSIONOBJECT_ENCODED_BYTESTRING:
  415. case UA_EXTENSIONOBJECT_ENCODED_XML:
  416. NodeId_clear(&p->content.encoded.typeId, NULL);
  417. String_clear(&p->content.encoded.body, NULL);
  418. break;
  419. case UA_EXTENSIONOBJECT_DECODED:
  420. if(p->content.decoded.data)
  421. UA_delete(p->content.decoded.data, p->content.decoded.type);
  422. break;
  423. default:
  424. break;
  425. }
  426. }
  427. static UA_StatusCode
  428. ExtensionObject_copy(UA_ExtensionObject const *src, UA_ExtensionObject *dst,
  429. const UA_DataType *_) {
  430. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  431. switch(src->encoding) {
  432. case UA_EXTENSIONOBJECT_ENCODED_NOBODY:
  433. case UA_EXTENSIONOBJECT_ENCODED_BYTESTRING:
  434. case UA_EXTENSIONOBJECT_ENCODED_XML:
  435. dst->encoding = src->encoding;
  436. retval = NodeId_copy(&src->content.encoded.typeId,
  437. &dst->content.encoded.typeId, NULL);
  438. retval |= UA_ByteString_copy(&src->content.encoded.body,
  439. &dst->content.encoded.body);
  440. break;
  441. case UA_EXTENSIONOBJECT_DECODED:
  442. case UA_EXTENSIONOBJECT_DECODED_NODELETE:
  443. if(!src->content.decoded.type || !src->content.decoded.data)
  444. return UA_STATUSCODE_BADINTERNALERROR;
  445. dst->encoding = UA_EXTENSIONOBJECT_DECODED;
  446. dst->content.decoded.type = src->content.decoded.type;
  447. retval = UA_Array_copy(src->content.decoded.data, 1,
  448. &dst->content.decoded.data, src->content.decoded.type);
  449. break;
  450. default:
  451. break;
  452. }
  453. return retval;
  454. }
  455. /* Variant */
  456. static void
  457. Variant_clear(UA_Variant *p, const UA_DataType *_) {
  458. if(p->storageType != UA_VARIANT_DATA)
  459. return;
  460. if(p->type && p->data > UA_EMPTY_ARRAY_SENTINEL) {
  461. if(p->arrayLength == 0)
  462. p->arrayLength = 1;
  463. UA_Array_delete(p->data, p->arrayLength, p->type);
  464. p->data = NULL;
  465. }
  466. if((void*)p->arrayDimensions > UA_EMPTY_ARRAY_SENTINEL)
  467. UA_free(p->arrayDimensions);
  468. }
  469. static UA_StatusCode
  470. Variant_copy(UA_Variant const *src, UA_Variant *dst, const UA_DataType *_) {
  471. size_t length = src->arrayLength;
  472. if(UA_Variant_isScalar(src))
  473. length = 1;
  474. UA_StatusCode retval = UA_Array_copy(src->data, length,
  475. &dst->data, src->type);
  476. if(retval != UA_STATUSCODE_GOOD)
  477. return retval;
  478. dst->arrayLength = src->arrayLength;
  479. dst->type = src->type;
  480. if(src->arrayDimensions) {
  481. retval = UA_Array_copy(src->arrayDimensions, src->arrayDimensionsSize,
  482. (void**)&dst->arrayDimensions, &UA_TYPES[UA_TYPES_INT32]);
  483. if(retval != UA_STATUSCODE_GOOD)
  484. return retval;
  485. dst->arrayDimensionsSize = src->arrayDimensionsSize;
  486. }
  487. return UA_STATUSCODE_GOOD;
  488. }
  489. void
  490. UA_Variant_setScalar(UA_Variant *v, void * UA_RESTRICT p,
  491. const UA_DataType *type) {
  492. UA_Variant_init(v);
  493. v->type = type;
  494. v->arrayLength = 0;
  495. v->data = p;
  496. }
  497. UA_StatusCode
  498. UA_Variant_setScalarCopy(UA_Variant *v, const void *p,
  499. const UA_DataType *type) {
  500. void *n = UA_malloc(type->memSize);
  501. if(!n)
  502. return UA_STATUSCODE_BADOUTOFMEMORY;
  503. UA_StatusCode retval = UA_copy(p, n, type);
  504. if(retval != UA_STATUSCODE_GOOD) {
  505. UA_free(n);
  506. //cppcheck-suppress memleak
  507. return retval;
  508. }
  509. UA_Variant_setScalar(v, n, type);
  510. //cppcheck-suppress memleak
  511. return UA_STATUSCODE_GOOD;
  512. }
  513. void UA_Variant_setArray(UA_Variant *v, void * UA_RESTRICT array,
  514. size_t arraySize, const UA_DataType *type) {
  515. UA_Variant_init(v);
  516. v->data = array;
  517. v->arrayLength = arraySize;
  518. v->type = type;
  519. }
  520. UA_StatusCode
  521. UA_Variant_setArrayCopy(UA_Variant *v, const void *array,
  522. size_t arraySize, const UA_DataType *type) {
  523. UA_Variant_init(v);
  524. UA_StatusCode retval = UA_Array_copy(array, arraySize, &v->data, type);
  525. if(retval != UA_STATUSCODE_GOOD)
  526. return retval;
  527. v->arrayLength = arraySize;
  528. v->type = type;
  529. return UA_STATUSCODE_GOOD;
  530. }
  531. /* Test if a range is compatible with a variant. If yes, the following values
  532. * are set:
  533. * - total: how many elements are in the range
  534. * - block: how big is each contiguous block of elements in the variant that
  535. * maps into the range
  536. * - stride: how many elements are between the blocks (beginning to beginning)
  537. * - first: where does the first block begin */
  538. static UA_StatusCode
  539. computeStrides(const UA_Variant *v, const UA_NumericRange range,
  540. size_t *total, size_t *block, size_t *stride, size_t *first) {
  541. /* Test for max array size (64bit only) */
  542. #if (SIZE_MAX > 0xffffffff)
  543. if(v->arrayLength > UA_UINT32_MAX)
  544. return UA_STATUSCODE_BADINTERNALERROR;
  545. #endif
  546. /* Test the integrity of the source variant dimensions, make dimensions
  547. * vector of one dimension if none defined */
  548. u32 arrayLength = (u32)v->arrayLength;
  549. const u32 *dims = &arrayLength;
  550. size_t dims_count = 1;
  551. if(v->arrayDimensionsSize > 0) {
  552. size_t elements = 1;
  553. dims_count = v->arrayDimensionsSize;
  554. dims = (u32*)v->arrayDimensions;
  555. for(size_t i = 0; i < dims_count; ++i)
  556. elements *= dims[i];
  557. if(elements != v->arrayLength)
  558. return UA_STATUSCODE_BADINTERNALERROR;
  559. }
  560. UA_assert(dims_count > 0);
  561. /* Test the integrity of the range and compute the max index used for every
  562. * dimension. The standard says in Part 4, Section 7.22:
  563. *
  564. * When reading a value, the indexes may not specify a range that is within
  565. * the bounds of the array. The Server shall return a partial result if some
  566. * elements exist within the range. */
  567. size_t count = 1;
  568. UA_STACKARRAY(UA_UInt32, realmax, dims_count);
  569. if(range.dimensionsSize != dims_count)
  570. return UA_STATUSCODE_BADINDEXRANGENODATA;
  571. for(size_t i = 0; i < dims_count; ++i) {
  572. if(range.dimensions[i].min > range.dimensions[i].max)
  573. return UA_STATUSCODE_BADINDEXRANGEINVALID;
  574. if(range.dimensions[i].min >= dims[i])
  575. return UA_STATUSCODE_BADINDEXRANGENODATA;
  576. if(range.dimensions[i].max < dims[i])
  577. realmax[i] = range.dimensions[i].max;
  578. else
  579. realmax[i] = dims[i] - 1;
  580. count *= (realmax[i] - range.dimensions[i].min) + 1;
  581. }
  582. *total = count;
  583. /* Compute the stride length and the position of the first element */
  584. *block = count; /* Assume the range describes the entire array. */
  585. *stride = v->arrayLength; /* So it can be copied as a contiguous block. */
  586. *first = 0;
  587. size_t running_dimssize = 1;
  588. UA_Boolean found_contiguous = false;
  589. for(size_t k = dims_count; k > 0;) {
  590. --k;
  591. size_t dimrange = 1 + realmax[k] - range.dimensions[k].min;
  592. if(!found_contiguous && dimrange != dims[k]) {
  593. /* Found the maximum block that can be copied contiguously */
  594. found_contiguous = true;
  595. *block = running_dimssize * dimrange;
  596. *stride = running_dimssize * dims[k];
  597. }
  598. *first += running_dimssize * range.dimensions[k].min;
  599. running_dimssize *= dims[k];
  600. }
  601. return UA_STATUSCODE_GOOD;
  602. }
  603. /* Is the type string-like? */
  604. static UA_Boolean
  605. isStringLike(const UA_DataType *type) {
  606. if(type == &UA_TYPES[UA_TYPES_STRING] ||
  607. type == &UA_TYPES[UA_TYPES_BYTESTRING] ||
  608. type == &UA_TYPES[UA_TYPES_XMLELEMENT])
  609. return true;
  610. return false;
  611. }
  612. /* Returns the part of the string that lies within the rangedimension */
  613. static UA_StatusCode
  614. copySubString(const UA_String *src, UA_String *dst,
  615. const UA_NumericRangeDimension *dim) {
  616. if(dim->min > dim->max)
  617. return UA_STATUSCODE_BADINDEXRANGEINVALID;
  618. if(dim->min >= src->length)
  619. return UA_STATUSCODE_BADINDEXRANGENODATA;
  620. size_t length;
  621. if(dim->max < src->length)
  622. length = dim->max - dim->min + 1;
  623. else
  624. length = src->length - dim->min;
  625. UA_StatusCode retval = UA_ByteString_allocBuffer(dst, length);
  626. if(retval != UA_STATUSCODE_GOOD)
  627. return retval;
  628. memcpy(dst->data, &src->data[dim->min], length);
  629. return UA_STATUSCODE_GOOD;
  630. }
  631. UA_StatusCode
  632. UA_Variant_copyRange(const UA_Variant *src, UA_Variant *dst,
  633. const UA_NumericRange range) {
  634. if(!src->type)
  635. return UA_STATUSCODE_BADINVALIDARGUMENT;
  636. UA_Boolean isScalar = UA_Variant_isScalar(src);
  637. UA_Boolean stringLike = isStringLike(src->type);
  638. UA_Variant arraySrc;
  639. /* Extract the range for copying at this level. The remaining range is dealt
  640. * with in the "scalar" type that may define an array by itself (string,
  641. * variant, ...). */
  642. UA_NumericRange thisrange, nextrange;
  643. UA_NumericRangeDimension scalarThisDimension = {0,0}; /* a single entry */
  644. if(isScalar) {
  645. /* Replace scalar src with array of length 1 */
  646. arraySrc = *src;
  647. arraySrc.arrayLength = 1;
  648. src = &arraySrc;
  649. /* Deal with all range dimensions within the scalar */
  650. thisrange.dimensions = &scalarThisDimension;
  651. thisrange.dimensionsSize = 1;
  652. nextrange = range;
  653. } else {
  654. /* Deal with as many range dimensions as possible right now */
  655. size_t dims = src->arrayDimensionsSize;
  656. if(dims == 0)
  657. dims = 1;
  658. if(dims > range.dimensionsSize)
  659. return UA_STATUSCODE_BADINDEXRANGEINVALID;
  660. thisrange = range;
  661. thisrange.dimensionsSize = dims;
  662. nextrange.dimensions = &range.dimensions[dims];
  663. nextrange.dimensionsSize = range.dimensionsSize - dims;
  664. }
  665. /* Compute the strides */
  666. size_t count, block, stride, first;
  667. UA_StatusCode retval = computeStrides(src, thisrange, &count,
  668. &block, &stride, &first);
  669. if(retval != UA_STATUSCODE_GOOD)
  670. return retval;
  671. /* Allocate the array */
  672. UA_Variant_init(dst);
  673. dst->data = UA_Array_new(count, src->type);
  674. if(!dst->data)
  675. return UA_STATUSCODE_BADOUTOFMEMORY;
  676. /* Copy the range */
  677. size_t block_count = count / block;
  678. size_t elem_size = src->type->memSize;
  679. uintptr_t nextdst = (uintptr_t)dst->data;
  680. uintptr_t nextsrc = (uintptr_t)src->data + (elem_size * first);
  681. if(nextrange.dimensionsSize == 0) {
  682. /* no nextrange */
  683. if(src->type->pointerFree) {
  684. for(size_t i = 0; i < block_count; ++i) {
  685. memcpy((void*)nextdst, (void*)nextsrc, elem_size * block);
  686. nextdst += block * elem_size;
  687. nextsrc += stride * elem_size;
  688. }
  689. } else {
  690. for(size_t i = 0; i < block_count; ++i) {
  691. for(size_t j = 0; j < block; ++j) {
  692. retval = UA_copy((const void*)nextsrc,
  693. (void*)nextdst, src->type);
  694. nextdst += elem_size;
  695. nextsrc += elem_size;
  696. }
  697. nextsrc += (stride - block) * elem_size;
  698. }
  699. }
  700. } else {
  701. /* nextrange can only be used for variants and stringlike with remaining
  702. * range of dimension 1 */
  703. if(src->type != &UA_TYPES[UA_TYPES_VARIANT]) {
  704. if(!stringLike)
  705. retval = UA_STATUSCODE_BADINDEXRANGENODATA;
  706. if(nextrange.dimensionsSize != 1)
  707. retval = UA_STATUSCODE_BADINDEXRANGENODATA;
  708. }
  709. /* Copy the content */
  710. for(size_t i = 0; i < block_count; ++i) {
  711. for(size_t j = 0; j < block && retval == UA_STATUSCODE_GOOD; ++j) {
  712. if(stringLike)
  713. retval = copySubString((const UA_String*)nextsrc,
  714. (UA_String*)nextdst,
  715. nextrange.dimensions);
  716. else
  717. retval = UA_Variant_copyRange((const UA_Variant*)nextsrc,
  718. (UA_Variant*)nextdst,
  719. nextrange);
  720. nextdst += elem_size;
  721. nextsrc += elem_size;
  722. }
  723. nextsrc += (stride - block) * elem_size;
  724. }
  725. }
  726. /* Clean up if copying failed */
  727. if(retval != UA_STATUSCODE_GOOD) {
  728. UA_Array_delete(dst->data, count, src->type);
  729. dst->data = NULL;
  730. return retval;
  731. }
  732. /* Done if scalar */
  733. dst->type = src->type;
  734. if(isScalar)
  735. return retval;
  736. /* Copy array dimensions */
  737. dst->arrayLength = count;
  738. if(src->arrayDimensionsSize > 0) {
  739. dst->arrayDimensions =
  740. (u32*)UA_Array_new(thisrange.dimensionsSize, &UA_TYPES[UA_TYPES_UINT32]);
  741. if(!dst->arrayDimensions) {
  742. Variant_clear(dst, NULL);
  743. return UA_STATUSCODE_BADOUTOFMEMORY;
  744. }
  745. dst->arrayDimensionsSize = thisrange.dimensionsSize;
  746. for(size_t k = 0; k < thisrange.dimensionsSize; ++k)
  747. dst->arrayDimensions[k] =
  748. thisrange.dimensions[k].max - thisrange.dimensions[k].min + 1;
  749. }
  750. return UA_STATUSCODE_GOOD;
  751. }
  752. /* TODO: Allow ranges to reach inside a scalars that are array-like, e.g.
  753. * variant and strings. This is already possible for reading... */
  754. static UA_StatusCode
  755. Variant_setRange(UA_Variant *v, void *array, size_t arraySize,
  756. const UA_NumericRange range, UA_Boolean copy) {
  757. /* Compute the strides */
  758. size_t count, block, stride, first;
  759. UA_StatusCode retval = computeStrides(v, range, &count,
  760. &block, &stride, &first);
  761. if(retval != UA_STATUSCODE_GOOD)
  762. return retval;
  763. if(count != arraySize)
  764. return UA_STATUSCODE_BADINDEXRANGEINVALID;
  765. /* Move/copy the elements */
  766. size_t block_count = count / block;
  767. size_t elem_size = v->type->memSize;
  768. uintptr_t nextdst = (uintptr_t)v->data + (first * elem_size);
  769. uintptr_t nextsrc = (uintptr_t)array;
  770. if(v->type->pointerFree || !copy) {
  771. for(size_t i = 0; i < block_count; ++i) {
  772. memcpy((void*)nextdst, (void*)nextsrc, elem_size * block);
  773. nextsrc += block * elem_size;
  774. nextdst += stride * elem_size;
  775. }
  776. } else {
  777. for(size_t i = 0; i < block_count; ++i) {
  778. for(size_t j = 0; j < block; ++j) {
  779. clearJumpTable[v->type->typeKind]((void*)nextdst, v->type);
  780. retval |= UA_copy((void*)nextsrc, (void*)nextdst, v->type);
  781. nextdst += elem_size;
  782. nextsrc += elem_size;
  783. }
  784. nextdst += (stride - block) * elem_size;
  785. }
  786. }
  787. /* If members were moved, initialize original array to prevent reuse */
  788. if(!copy && !v->type->pointerFree)
  789. memset(array, 0, sizeof(elem_size)*arraySize);
  790. return retval;
  791. }
  792. UA_StatusCode
  793. UA_Variant_setRange(UA_Variant *v, void * UA_RESTRICT array,
  794. size_t arraySize, const UA_NumericRange range) {
  795. return Variant_setRange(v, array, arraySize, range, false);
  796. }
  797. UA_StatusCode
  798. UA_Variant_setRangeCopy(UA_Variant *v, const void *array,
  799. size_t arraySize, const UA_NumericRange range) {
  800. return Variant_setRange(v, (void*)(uintptr_t)array,
  801. arraySize, range, true);
  802. }
  803. /* LocalizedText */
  804. static void
  805. LocalizedText_clear(UA_LocalizedText *p, const UA_DataType *_) {
  806. String_clear(&p->locale, NULL);
  807. String_clear(&p->text, NULL);
  808. }
  809. static UA_StatusCode
  810. LocalizedText_copy(UA_LocalizedText const *src, UA_LocalizedText *dst,
  811. const UA_DataType *_) {
  812. UA_StatusCode retval = UA_String_copy(&src->locale, &dst->locale);
  813. retval |= UA_String_copy(&src->text, &dst->text);
  814. return retval;
  815. }
  816. /* DataValue */
  817. static void
  818. DataValue_clear(UA_DataValue *p, const UA_DataType *_) {
  819. Variant_clear(&p->value, NULL);
  820. }
  821. static UA_StatusCode
  822. DataValue_copy(UA_DataValue const *src, UA_DataValue *dst,
  823. const UA_DataType *_) {
  824. memcpy(dst, src, sizeof(UA_DataValue));
  825. UA_Variant_init(&dst->value);
  826. UA_StatusCode retval = Variant_copy(&src->value, &dst->value, NULL);
  827. if(retval != UA_STATUSCODE_GOOD)
  828. DataValue_clear(dst, NULL);
  829. return retval;
  830. }
  831. /* DiagnosticInfo */
  832. static void
  833. DiagnosticInfo_clear(UA_DiagnosticInfo *p, const UA_DataType *_) {
  834. String_clear(&p->additionalInfo, NULL);
  835. if(p->hasInnerDiagnosticInfo && p->innerDiagnosticInfo) {
  836. DiagnosticInfo_clear(p->innerDiagnosticInfo, NULL);
  837. UA_free(p->innerDiagnosticInfo);
  838. }
  839. }
  840. static UA_StatusCode
  841. DiagnosticInfo_copy(UA_DiagnosticInfo const *src, UA_DiagnosticInfo *dst,
  842. const UA_DataType *_) {
  843. memcpy(dst, src, sizeof(UA_DiagnosticInfo));
  844. UA_String_init(&dst->additionalInfo);
  845. dst->innerDiagnosticInfo = NULL;
  846. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  847. if(src->hasAdditionalInfo)
  848. retval = UA_String_copy(&src->additionalInfo, &dst->additionalInfo);
  849. if(src->hasInnerDiagnosticInfo && src->innerDiagnosticInfo) {
  850. dst->innerDiagnosticInfo = (UA_DiagnosticInfo*)UA_malloc(sizeof(UA_DiagnosticInfo));
  851. if(dst->innerDiagnosticInfo) {
  852. retval |= DiagnosticInfo_copy(src->innerDiagnosticInfo,
  853. dst->innerDiagnosticInfo, NULL);
  854. dst->hasInnerDiagnosticInfo = true;
  855. } else {
  856. dst->hasInnerDiagnosticInfo = false;
  857. retval |= UA_STATUSCODE_BADOUTOFMEMORY;
  858. }
  859. }
  860. return retval;
  861. }
  862. /********************/
  863. /* Structured Types */
  864. /********************/
  865. void *
  866. UA_new(const UA_DataType *type) {
  867. void *p = UA_calloc(1, type->memSize);
  868. return p;
  869. }
  870. static UA_StatusCode
  871. copyByte(const u8 *src, u8 *dst, const UA_DataType *_) {
  872. *dst = *src;
  873. return UA_STATUSCODE_GOOD;
  874. }
  875. static UA_StatusCode
  876. copy2Byte(const u16 *src, u16 *dst, const UA_DataType *_) {
  877. *dst = *src;
  878. return UA_STATUSCODE_GOOD;
  879. }
  880. static UA_StatusCode
  881. copy4Byte(const u32 *src, u32 *dst, const UA_DataType *_) {
  882. *dst = *src;
  883. return UA_STATUSCODE_GOOD;
  884. }
  885. static UA_StatusCode
  886. copy8Byte(const u64 *src, u64 *dst, const UA_DataType *_) {
  887. *dst = *src;
  888. return UA_STATUSCODE_GOOD;
  889. }
  890. static UA_StatusCode
  891. copyGuid(const UA_Guid *src, UA_Guid *dst, const UA_DataType *_) {
  892. *dst = *src;
  893. return UA_STATUSCODE_GOOD;
  894. }
  895. static UA_StatusCode
  896. copyStructure(const void *src, void *dst, const UA_DataType *type) {
  897. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  898. uintptr_t ptrs = (uintptr_t)src;
  899. uintptr_t ptrd = (uintptr_t)dst;
  900. const UA_DataType *typelists[2] = { UA_TYPES, &type[-type->typeIndex] };
  901. for(size_t i = 0; i < type->membersSize; ++i) {
  902. const UA_DataTypeMember *m= &type->members[i];
  903. const UA_DataType *mt = &typelists[!m->namespaceZero][m->memberTypeIndex];
  904. if(!m->isArray) {
  905. ptrs += m->padding;
  906. ptrd += m->padding;
  907. retval |= copyJumpTable[mt->typeKind]((const void*)ptrs, (void*)ptrd, mt);
  908. ptrs += mt->memSize;
  909. ptrd += mt->memSize;
  910. } else {
  911. ptrs += m->padding;
  912. ptrd += m->padding;
  913. size_t *dst_size = (size_t*)ptrd;
  914. const size_t size = *((const size_t*)ptrs);
  915. ptrs += sizeof(size_t);
  916. ptrd += sizeof(size_t);
  917. retval |= UA_Array_copy(*(void* const*)ptrs, size, (void**)ptrd, mt);
  918. if(retval == UA_STATUSCODE_GOOD)
  919. *dst_size = size;
  920. else
  921. *dst_size = 0;
  922. ptrs += sizeof(void*);
  923. ptrd += sizeof(void*);
  924. }
  925. }
  926. return retval;
  927. }
  928. static UA_StatusCode
  929. copyNotImplemented(const void *src, void *dst, const UA_DataType *type) {
  930. return UA_STATUSCODE_BADNOTIMPLEMENTED;
  931. }
  932. const UA_copySignature copyJumpTable[UA_DATATYPEKINDS] = {
  933. (UA_copySignature)copyByte, /* Boolean */
  934. (UA_copySignature)copyByte, /* SByte */
  935. (UA_copySignature)copyByte, /* Byte */
  936. (UA_copySignature)copy2Byte, /* Int16 */
  937. (UA_copySignature)copy2Byte, /* UInt16 */
  938. (UA_copySignature)copy4Byte, /* Int32 */
  939. (UA_copySignature)copy4Byte, /* UInt32 */
  940. (UA_copySignature)copy8Byte, /* Int64 */
  941. (UA_copySignature)copy8Byte, /* UInt64 */
  942. (UA_copySignature)copy4Byte, /* Float */
  943. (UA_copySignature)copy8Byte, /* Double */
  944. (UA_copySignature)String_copy,
  945. (UA_copySignature)copy8Byte, /* DateTime */
  946. (UA_copySignature)copyGuid, /* Guid */
  947. (UA_copySignature)String_copy, /* ByteString */
  948. (UA_copySignature)String_copy, /* XmlElement */
  949. (UA_copySignature)NodeId_copy,
  950. (UA_copySignature)ExpandedNodeId_copy,
  951. (UA_copySignature)copy4Byte, /* StatusCode */
  952. (UA_copySignature)QualifiedName_copy,
  953. (UA_copySignature)LocalizedText_copy,
  954. (UA_copySignature)ExtensionObject_copy,
  955. (UA_copySignature)DataValue_copy,
  956. (UA_copySignature)Variant_copy,
  957. (UA_copySignature)DiagnosticInfo_copy,
  958. (UA_copySignature)copyNotImplemented, /* Decimal */
  959. (UA_copySignature)copy4Byte, /* Enumeration */
  960. (UA_copySignature)copyStructure,
  961. (UA_copySignature)copyNotImplemented, /* Structure with Optional Fields */
  962. (UA_copySignature)copyNotImplemented, /* Union */
  963. (UA_copySignature)copyNotImplemented /* BitfieldCluster*/
  964. };
  965. UA_StatusCode
  966. UA_copy(const void *src, void *dst, const UA_DataType *type) {
  967. memset(dst, 0, type->memSize); /* init */
  968. UA_StatusCode retval = copyJumpTable[type->typeKind](src, dst, type);
  969. if(retval != UA_STATUSCODE_GOOD)
  970. UA_clear(dst, type);
  971. return retval;
  972. }
  973. static void
  974. clearStructure(void *p, const UA_DataType *type) {
  975. uintptr_t ptr = (uintptr_t)p;
  976. const UA_DataType *typelists[2] = { UA_TYPES, &type[-type->typeIndex] };
  977. for(size_t i = 0; i < type->membersSize; ++i) {
  978. const UA_DataTypeMember *m = &type->members[i];
  979. const UA_DataType *mt = &typelists[!m->namespaceZero][m->memberTypeIndex];
  980. if(!m->isArray) {
  981. ptr += m->padding;
  982. clearJumpTable[mt->typeKind]((void*)ptr, mt);
  983. ptr += mt->memSize;
  984. } else {
  985. ptr += m->padding;
  986. size_t length = *(size_t*)ptr;
  987. ptr += sizeof(size_t);
  988. UA_Array_delete(*(void**)ptr, length, mt);
  989. ptr += sizeof(void*);
  990. }
  991. }
  992. }
  993. static void nopClear(void *p, const UA_DataType *type) { }
  994. const
  995. UA_clearSignature clearJumpTable[UA_DATATYPEKINDS] = {
  996. (UA_clearSignature)nopClear, /* Boolean */
  997. (UA_clearSignature)nopClear, /* SByte */
  998. (UA_clearSignature)nopClear, /* Byte */
  999. (UA_clearSignature)nopClear, /* Int16 */
  1000. (UA_clearSignature)nopClear, /* UInt16 */
  1001. (UA_clearSignature)nopClear, /* Int32 */
  1002. (UA_clearSignature)nopClear, /* UInt32 */
  1003. (UA_clearSignature)nopClear, /* Int64 */
  1004. (UA_clearSignature)nopClear, /* UInt64 */
  1005. (UA_clearSignature)nopClear, /* Float */
  1006. (UA_clearSignature)nopClear, /* Double */
  1007. (UA_clearSignature)String_clear, /* String */
  1008. (UA_clearSignature)nopClear, /* DateTime */
  1009. (UA_clearSignature)nopClear, /* Guid */
  1010. (UA_clearSignature)String_clear, /* ByteString */
  1011. (UA_clearSignature)String_clear, /* XmlElement */
  1012. (UA_clearSignature)NodeId_clear,
  1013. (UA_clearSignature)ExpandedNodeId_clear,
  1014. (UA_clearSignature)nopClear, /* StatusCode */
  1015. (UA_clearSignature)QualifiedName_clear,
  1016. (UA_clearSignature)LocalizedText_clear,
  1017. (UA_clearSignature)ExtensionObject_clear,
  1018. (UA_clearSignature)DataValue_clear,
  1019. (UA_clearSignature)Variant_clear,
  1020. (UA_clearSignature)DiagnosticInfo_clear,
  1021. (UA_clearSignature)nopClear, /* Decimal, not implemented */
  1022. (UA_clearSignature)nopClear, /* Enumeration */
  1023. (UA_clearSignature)clearStructure,
  1024. (UA_clearSignature)nopClear, /* Struct with Optional Fields, not implemented*/
  1025. (UA_clearSignature)nopClear, /* Union, not implemented*/
  1026. (UA_clearSignature)nopClear /* BitfieldCluster, not implemented*/
  1027. };
  1028. void
  1029. UA_clear(void *p, const UA_DataType *type) {
  1030. clearJumpTable[type->typeKind](p, type);
  1031. memset(p, 0, type->memSize); /* init */
  1032. }
  1033. void
  1034. UA_delete(void *p, const UA_DataType *type) {
  1035. clearJumpTable[type->typeKind](p, type);
  1036. UA_free(p);
  1037. }
  1038. /******************/
  1039. /* Array Handling */
  1040. /******************/
  1041. void *
  1042. UA_Array_new(size_t size, const UA_DataType *type) {
  1043. if(size > UA_INT32_MAX)
  1044. return NULL;
  1045. if(size == 0)
  1046. return UA_EMPTY_ARRAY_SENTINEL;
  1047. return UA_calloc(size, type->memSize);
  1048. }
  1049. UA_StatusCode
  1050. UA_Array_copy(const void *src, size_t size,
  1051. void **dst, const UA_DataType *type) {
  1052. if(size == 0) {
  1053. if(src == NULL)
  1054. *dst = NULL;
  1055. else
  1056. *dst= UA_EMPTY_ARRAY_SENTINEL;
  1057. return UA_STATUSCODE_GOOD;
  1058. }
  1059. if(!type)
  1060. return UA_STATUSCODE_BADINTERNALERROR;
  1061. /* calloc, so we don't have to check retval in every iteration of copying */
  1062. *dst = UA_calloc(size, type->memSize);
  1063. if(!*dst)
  1064. return UA_STATUSCODE_BADOUTOFMEMORY;
  1065. if(type->pointerFree) {
  1066. memcpy(*dst, src, type->memSize * size);
  1067. return UA_STATUSCODE_GOOD;
  1068. }
  1069. uintptr_t ptrs = (uintptr_t)src;
  1070. uintptr_t ptrd = (uintptr_t)*dst;
  1071. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  1072. for(size_t i = 0; i < size; ++i) {
  1073. retval |= UA_copy((void*)ptrs, (void*)ptrd, type);
  1074. ptrs += type->memSize;
  1075. ptrd += type->memSize;
  1076. }
  1077. if(retval != UA_STATUSCODE_GOOD) {
  1078. UA_Array_delete(*dst, size, type);
  1079. *dst = NULL;
  1080. }
  1081. return retval;
  1082. }
  1083. void
  1084. UA_Array_delete(void *p, size_t size, const UA_DataType *type) {
  1085. if(!type->pointerFree) {
  1086. uintptr_t ptr = (uintptr_t)p;
  1087. for(size_t i = 0; i < size; ++i) {
  1088. UA_clear((void*)ptr, type);
  1089. ptr += type->memSize;
  1090. }
  1091. }
  1092. UA_free((void*)((uintptr_t)p & ~(uintptr_t)UA_EMPTY_ARRAY_SENTINEL));
  1093. }
  1094. UA_Boolean
  1095. UA_DataType_isNumeric(const UA_DataType *type) {
  1096. /* All data types between UA_TYPES_BOOLEAN and UA_TYPES_DOUBLE are numeric */
  1097. for(size_t i = UA_TYPES_BOOLEAN; i <= UA_TYPES_DOUBLE; ++i)
  1098. if(&UA_TYPES[i] == type)
  1099. return true;
  1100. return false;
  1101. }
  1102. /**********************/
  1103. /* Parse NumericRange */
  1104. /**********************/
  1105. static size_t
  1106. readDimension(UA_Byte *buf, size_t buflen, UA_NumericRangeDimension *dim) {
  1107. size_t progress = UA_readNumber(buf, buflen, &dim->min);
  1108. if(progress == 0)
  1109. return 0;
  1110. if(buflen <= progress + 1 || buf[progress] != ':') {
  1111. dim->max = dim->min;
  1112. return progress;
  1113. }
  1114. ++progress;
  1115. size_t progress2 = UA_readNumber(&buf[progress], buflen - progress, &dim->max);
  1116. if(progress2 == 0)
  1117. return 0;
  1118. /* invalid range */
  1119. if(dim->min >= dim->max)
  1120. return 0;
  1121. return progress + progress2;
  1122. }
  1123. UA_StatusCode
  1124. UA_NumericRange_parseFromString(UA_NumericRange *range, const UA_String *str) {
  1125. size_t idx = 0;
  1126. size_t dimensionsMax = 0;
  1127. UA_NumericRangeDimension *dimensions = NULL;
  1128. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  1129. size_t offset = 0;
  1130. while(true) {
  1131. /* alloc dimensions */
  1132. if(idx >= dimensionsMax) {
  1133. UA_NumericRangeDimension *newds;
  1134. size_t newdssize = sizeof(UA_NumericRangeDimension) * (dimensionsMax + 2);
  1135. newds = (UA_NumericRangeDimension*)UA_realloc(dimensions, newdssize);
  1136. if(!newds) {
  1137. retval = UA_STATUSCODE_BADOUTOFMEMORY;
  1138. break;
  1139. }
  1140. dimensions = newds;
  1141. dimensionsMax = dimensionsMax + 2;
  1142. }
  1143. /* read the dimension */
  1144. size_t progress = readDimension(&str->data[offset], str->length - offset,
  1145. &dimensions[idx]);
  1146. if(progress == 0) {
  1147. retval = UA_STATUSCODE_BADINDEXRANGEINVALID;
  1148. break;
  1149. }
  1150. offset += progress;
  1151. ++idx;
  1152. /* loop into the next dimension */
  1153. if(offset >= str->length)
  1154. break;
  1155. if(str->data[offset] != ',') {
  1156. retval = UA_STATUSCODE_BADINDEXRANGEINVALID;
  1157. break;
  1158. }
  1159. ++offset;
  1160. }
  1161. if(retval == UA_STATUSCODE_GOOD && idx > 0) {
  1162. range->dimensions = dimensions;
  1163. range->dimensionsSize = idx;
  1164. } else
  1165. UA_free(dimensions);
  1166. return retval;
  1167. }