ua_types.c 35 KB

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