ua_types.c 35 KB

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