ua_types.c 40 KB

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