ua_types.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112
  1. #include <stdarg.h> // va_start, va_end
  2. #include <time.h>
  3. #include <stdio.h> // printf
  4. #include <string.h> // strlen
  5. #define __USE_POSIX
  6. #include <stdlib.h> // malloc, free, rand
  7. #ifdef _WIN32
  8. #include <windows.h>
  9. #else
  10. #include <sys/time.h>
  11. #endif
  12. #include "ua_util.h"
  13. #ifdef _WIN32
  14. #define RAND(SEED) (UA_UInt32)rand()
  15. #else
  16. #define RAND(SEED) (UA_UInt32)rand_r(SEED)
  17. #endif
  18. #ifdef UA_DEBUG
  19. #include <inttypes.h>
  20. #endif
  21. #include "ua_types.h"
  22. #include "ua_types_macros.h"
  23. #include "ua_types_encoding_binary.h"
  24. #include "ua_namespace_0.h"
  25. #include "ua_statuscodes.h"
  26. /* Boolean */
  27. void UA_Boolean_init(UA_Boolean *p) {
  28. *p = UA_FALSE;
  29. }
  30. UA_TYPE_DELETE_DEFAULT(UA_Boolean)
  31. UA_TYPE_DELETEMEMBERS_NOACTION(UA_Boolean)
  32. UA_TYPE_NEW_DEFAULT(UA_Boolean)
  33. UA_TYPE_COPY_DEFAULT(UA_Boolean)
  34. #ifdef UA_DEBUG
  35. void UA_Boolean_print(const UA_Boolean *p, FILE *stream) {
  36. if(*p) fprintf(stream, "UA_TRUE");
  37. else fprintf(stream, "UA_FALSE");
  38. }
  39. #endif
  40. /* SByte */
  41. UA_TYPE_DEFAULT(UA_SByte)
  42. #ifdef UA_DEBUG
  43. void UA_SByte_print(const UA_SByte *p, FILE *stream) {
  44. if(!p || !stream) return;
  45. UA_SByte x = *p;
  46. fprintf(stream, "%s%x\n", x < 0 ? "-" : "", x < 0 ? -x : x);
  47. }
  48. #endif
  49. /* Byte */
  50. UA_TYPE_DEFAULT(UA_Byte)
  51. #ifdef UA_DEBUG
  52. void UA_Byte_print(const UA_Byte *p, FILE *stream) {
  53. if(!p || !stream) return;
  54. fprintf(stream, "%x", *p);
  55. }
  56. #endif
  57. /* Int16 */
  58. UA_TYPE_DEFAULT(UA_Int16)
  59. #ifdef UA_DEBUG
  60. void UA_Int16_print(const UA_Int16 *p, FILE *stream) {
  61. if(!p || !stream) return;
  62. fprintf(stream, "%d", *p);
  63. }
  64. #endif
  65. /* UInt16 */
  66. UA_TYPE_DEFAULT(UA_UInt16)
  67. #ifdef UA_DEBUG
  68. void UA_UInt16_print(const UA_UInt16 *p, FILE *stream) {
  69. if(!p || !stream) return;
  70. fprintf(stream, "%u", *p);
  71. }
  72. #endif
  73. /* Int32 */
  74. UA_TYPE_DEFAULT(UA_Int32)
  75. #ifdef UA_DEBUG
  76. void UA_Int32_print(const UA_Int32 *p, FILE *stream) {
  77. if(!p || !stream) return;
  78. fprintf(stream, "%d", *p);
  79. }
  80. #endif
  81. /* UInt32 */
  82. UA_TYPE_DEFAULT(UA_UInt32)
  83. #ifdef UA_DEBUG
  84. void UA_UInt32_print(const UA_UInt32 *p, FILE *stream) {
  85. if(!p || !stream) return;
  86. fprintf(stream, "%u", *p);
  87. }
  88. #endif
  89. /* Int64 */
  90. UA_TYPE_DEFAULT(UA_Int64)
  91. #ifdef UA_DEBUG
  92. void UA_Int64_print(const UA_Int64 *p, FILE *stream) {
  93. if(!p || !stream) return;
  94. fprintf(stream, "%" PRIi64, *p);
  95. }
  96. #endif
  97. /* UInt64 */
  98. UA_TYPE_DEFAULT(UA_UInt64)
  99. #ifdef UA_DEBUG
  100. void UA_UInt64_print(const UA_UInt64 *p, FILE *stream) {
  101. if(!p || !stream) return;
  102. fprintf(stream, "%" PRIu64, *p);
  103. }
  104. #endif
  105. /* Float */
  106. UA_TYPE_DEFAULT(UA_Float)
  107. #ifdef UA_DEBUG
  108. void UA_Float_print(const UA_Float *p, FILE *stream) {
  109. if(!p || !stream) return;
  110. fprintf(stream, "%f", *p);
  111. }
  112. #endif
  113. /* Double */
  114. UA_TYPE_DEFAULT(UA_Double)
  115. #ifdef UA_DEBUG
  116. void UA_Double_print(const UA_Double *p, FILE *stream) {
  117. if(!p || !stream) return;
  118. fprintf(stream, "%f", *p);
  119. }
  120. #endif
  121. /* String */
  122. UA_TYPE_NEW_DEFAULT(UA_String)
  123. void UA_String_init(UA_String *p) {
  124. p->length = -1;
  125. p->data = UA_NULL;
  126. }
  127. UA_TYPE_DELETE_DEFAULT(UA_String)
  128. void UA_String_deleteMembers(UA_String *p) {
  129. UA_free(p->data);
  130. }
  131. UA_StatusCode UA_String_copy(UA_String const *src, UA_String *dst) {
  132. UA_String_init(dst);
  133. if(src->length > 0) {
  134. if(!(dst->data = UA_malloc((UA_UInt32)src->length)))
  135. return UA_STATUSCODE_BADOUTOFMEMORY;
  136. UA_memcpy((void *)dst->data, src->data, (UA_UInt32)src->length);
  137. }
  138. dst->length = src->length;
  139. return UA_STATUSCODE_GOOD;
  140. }
  141. #ifdef UA_DEBUG
  142. void UA_String_print(const UA_String *p, FILE *stream) {
  143. fprintf(stream, "(UA_String){%d,", p->length);
  144. if(p->data)
  145. fprintf(stream, "\"%.*s\"}", p->length, p->data);
  146. else
  147. fprintf(stream, "UA_NULL}");
  148. }
  149. #endif
  150. /* The c-string needs to be null-terminated. the string cannot be smaller than zero. */
  151. UA_Int32 UA_String_copycstring(char const *src, UA_String *dst) {
  152. UA_UInt32 length = (UA_UInt32) strlen(src);
  153. if(length == 0) {
  154. dst->length = 0;
  155. dst->data = UA_NULL;
  156. return UA_STATUSCODE_GOOD;
  157. }
  158. dst->data = UA_malloc(length);
  159. if(dst->data != UA_NULL) {
  160. UA_memcpy(dst->data, src, length);
  161. dst->length = (UA_Int32) (length & ~(1<<31)); // the highest bit is always zero to avoid overflows into negative values
  162. } else {
  163. dst->length = -1;
  164. return UA_STATUSCODE_BADOUTOFMEMORY;
  165. }
  166. return UA_STATUSCODE_GOOD;
  167. }
  168. #define UA_STRING_COPYPRINTF_BUFSIZE 1024
  169. UA_StatusCode UA_String_copyprintf(char const *fmt, UA_String *dst, ...) {
  170. char src[UA_STRING_COPYPRINTF_BUFSIZE];
  171. va_list ap;
  172. va_start(ap, dst);
  173. #if defined(__GNUC__) || defined(__clang__)
  174. #pragma GCC diagnostic push
  175. #pragma GCC diagnostic ignored "-Wformat-nonliteral"
  176. #endif
  177. // vsnprintf should only take a literal and no variable to be secure
  178. UA_Int32 len = vsnprintf(src, UA_STRING_COPYPRINTF_BUFSIZE, fmt, ap);
  179. #if defined(__GNUC__) || defined(__clang__)
  180. #pragma GCC diagnostic pop
  181. #endif
  182. va_end(ap);
  183. if(len < 0) // FIXME: old glibc 2.0 would return -1 when truncated
  184. return UA_STATUSCODE_BADINTERNALERROR;
  185. // since glibc 2.1 vsnprintf returns the len that would have resulted if buf were large enough
  186. len = ( len > UA_STRING_COPYPRINTF_BUFSIZE ? UA_STRING_COPYPRINTF_BUFSIZE : len );
  187. if(!(dst->data = UA_malloc((UA_UInt32)len)))
  188. return UA_STATUSCODE_BADOUTOFMEMORY;
  189. UA_memcpy((void *)dst->data, src, (UA_UInt32)len);
  190. dst->length = len;
  191. return UA_STATUSCODE_GOOD;
  192. }
  193. UA_Boolean UA_String_equal(const UA_String *string1, const UA_String *string2) {
  194. if(string1->length <= 0 && string2->length <= 0)
  195. return UA_TRUE;
  196. if(string1->length != string2->length)
  197. return UA_FALSE;
  198. // casts are needed to overcome signed warnings
  199. UA_Int32 is = strncmp((char const *)string1->data, (char const *)string2->data, (size_t)string1->length);
  200. return (is == 0) ? UA_TRUE : UA_FALSE;
  201. }
  202. #ifdef UA_DEBUG
  203. void UA_String_printf(char const *label, const UA_String *string) {
  204. printf("%s {Length=%d, Data=%.*s}\n", label, string->length,
  205. string->length, (char *)string->data);
  206. }
  207. #endif
  208. #ifdef UA_DEBUG
  209. void UA_String_printx(char const *label, const UA_String *string) {
  210. printf("%s {Length=%d, Data=", label, string->length);
  211. if(string->length > 0) {
  212. for(UA_Int32 i = 0;i < string->length;i++) {
  213. printf("%c%d", i == 0 ? '{' : ',', (string->data)[i]);
  214. // if (i > 0 && !(i%20)) { printf("\n\t"); }
  215. }
  216. } else
  217. printf("{");
  218. printf("}}\n");
  219. }
  220. #endif
  221. #ifdef UA_DEBUG
  222. void UA_String_printx_hex(char const *label, const UA_String *string) {
  223. printf("%s {Length=%d, Data=", label, string->length);
  224. if(string->length > 0) {
  225. for(UA_Int32 i = 0;i < string->length;i++)
  226. printf("%c%x", i == 0 ? '{' : ',', (string->data)[i]);
  227. } else
  228. printf("{");
  229. printf("}}\n");
  230. }
  231. #endif
  232. /* DateTime */
  233. UA_TYPE_AS(UA_DateTime, UA_Int64)
  234. #define UNIX_EPOCH_BIAS_SEC 11644473600LL // Number of seconds from 1 Jan. 1601 00:00 to 1 Jan 1970 00:00 UTC
  235. #define HUNDRED_NANOSEC_PER_USEC 10LL
  236. #define HUNDRED_NANOSEC_PER_SEC (HUNDRED_NANOSEC_PER_USEC * 1000000LL)
  237. #ifdef __MINGW32__
  238. struct timezone {
  239. int tz_minuteswest;
  240. int tz_dsttime;
  241. };
  242. #endif
  243. #ifdef _WIN32
  244. static const unsigned __int64 epoch = 116444736000000000;
  245. int gettimeofday(struct timeval *tp, struct timezone *tzp);
  246. int gettimeofday(struct timeval *tp, struct timezone *tzp) {
  247. FILETIME ft;
  248. SYSTEMTIME st;
  249. ULARGE_INTEGER ul;
  250. GetSystemTime(&st);
  251. SystemTimeToFileTime(&st, &ft);
  252. ul.LowPart = ft.dwLowDateTime;
  253. ul.HighPart = ft.dwHighDateTime;
  254. tp->tv_sec = (ul.QuadPart - epoch) / 10000000L;
  255. tp->tv_usec = st.wMilliseconds * 1000;
  256. return 0;
  257. }
  258. #endif
  259. UA_DateTime UA_DateTime_now() {
  260. UA_DateTime dateTime;
  261. struct timeval tv;
  262. gettimeofday(&tv, UA_NULL);
  263. dateTime = (tv.tv_sec + UNIX_EPOCH_BIAS_SEC)
  264. * HUNDRED_NANOSEC_PER_SEC + tv.tv_usec * HUNDRED_NANOSEC_PER_USEC;
  265. return dateTime;
  266. }
  267. UA_DateTimeStruct UA_DateTime_toStruct(UA_DateTime atime) {
  268. UA_DateTimeStruct dateTimeStruct;
  269. //calcualting the the milli-, micro- and nanoseconds
  270. dateTimeStruct.nanoSec = (UA_Int16)((atime % 10) * 100);
  271. dateTimeStruct.microSec = (UA_Int16)((atime % 10000) / 10);
  272. dateTimeStruct.milliSec = (UA_Int16)((atime % 10000000) / 10000);
  273. //calculating the unix time with #include <time.h>
  274. time_t secSinceUnixEpoch = (atime/10000000) - UNIX_EPOCH_BIAS_SEC;
  275. struct tm ts = *gmtime(&secSinceUnixEpoch);
  276. dateTimeStruct.sec = (UA_Int16)ts.tm_sec;
  277. dateTimeStruct.min = (UA_Int16)ts.tm_min;
  278. dateTimeStruct.hour = (UA_Int16)ts.tm_hour;
  279. dateTimeStruct.day = (UA_Int16)ts.tm_mday;
  280. dateTimeStruct.month = (UA_Int16)(ts.tm_mon + 1);
  281. dateTimeStruct.year = (UA_Int16)(ts.tm_year + 1900);
  282. return dateTimeStruct;
  283. }
  284. UA_StatusCode UA_DateTime_toString(UA_DateTime atime, UA_String *timeString) {
  285. // length of the string is 31 (incl. \0 at the end)
  286. if(!(timeString->data = UA_malloc(32)))
  287. return UA_STATUSCODE_BADOUTOFMEMORY;
  288. timeString->length = 31;
  289. UA_DateTimeStruct tSt = UA_DateTime_toStruct(atime);
  290. sprintf((char*)timeString->data, "%2d/%2d/%4d %2d:%2d:%2d.%3d.%3d.%3d", tSt.month, tSt.day, tSt.year,
  291. tSt.hour, tSt.min, tSt.sec, tSt.milliSec, tSt.microSec, tSt.nanoSec);
  292. return UA_STATUSCODE_GOOD;
  293. }
  294. /* Guid */
  295. UA_TYPE_DELETE_DEFAULT(UA_Guid)
  296. UA_TYPE_DELETEMEMBERS_NOACTION(UA_Guid)
  297. UA_Boolean UA_Guid_equal(const UA_Guid *g1, const UA_Guid *g2) {
  298. if(memcmp(g1, g2, sizeof(UA_Guid)) == 0)
  299. return UA_TRUE;
  300. return UA_FALSE;
  301. }
  302. UA_Guid UA_Guid_random(UA_UInt32 *seed) {
  303. UA_Guid result;
  304. result.data1 = RAND(seed);
  305. UA_UInt32 r = RAND(seed);
  306. result.data2 = (UA_UInt16) r;
  307. result.data3 = (UA_UInt16) (r >> 16);
  308. r = RAND(seed);
  309. result.data4[0] = (UA_Byte)r;
  310. result.data4[1] = (UA_Byte)(r >> 4);
  311. result.data4[2] = (UA_Byte)(r >> 8);
  312. result.data4[3] = (UA_Byte)(r >> 12);
  313. r = RAND(seed);
  314. result.data4[4] = (UA_Byte)r;
  315. result.data4[5] = (UA_Byte)(r >> 4);
  316. result.data4[6] = (UA_Byte)(r >> 8);
  317. result.data4[7] = (UA_Byte)(r >> 12);
  318. return result;
  319. }
  320. void UA_Guid_init(UA_Guid *p) {
  321. p->data1 = 0;
  322. p->data2 = 0;
  323. p->data3 = 0;
  324. memset(p->data4, 0, sizeof(UA_Byte)*8);
  325. }
  326. UA_TYPE_NEW_DEFAULT(UA_Guid)
  327. UA_StatusCode UA_Guid_copy(UA_Guid const *src, UA_Guid *dst) {
  328. UA_memcpy((void *)dst, (const void *)src, sizeof(UA_Guid));
  329. return UA_STATUSCODE_GOOD;
  330. }
  331. #ifdef UA_DEBUG
  332. void UA_Guid_print(const UA_Guid *p, FILE *stream) {
  333. fprintf(stream, "(UA_Guid){%u, %u %u {%x,%x,%x,%x,%x,%x,%x,%x}}", p->data1, p->data2, p->data3, p->data4[0],
  334. p->data4[1], p->data4[2], p->data4[3], p->data4[4], p->data4[5], p->data4[6], p->data4[7]);
  335. }
  336. #endif
  337. /* ByteString */
  338. UA_TYPE_AS(UA_ByteString, UA_String)
  339. UA_Boolean UA_ByteString_equal(const UA_ByteString *string1, const UA_ByteString *string2) {
  340. return UA_String_equal((const UA_String *)string1, (const UA_String *)string2);
  341. }
  342. #ifdef UA_DEBUG
  343. void UA_ByteString_printf(char *label, const UA_ByteString *string) {
  344. UA_String_printf(label, (const UA_String *)string);
  345. }
  346. #endif
  347. #ifdef UA_DEBUG
  348. void UA_ByteString_printx(char *label, const UA_ByteString *string) {
  349. UA_String_printx(label, (const UA_String *)string);
  350. }
  351. #endif
  352. #ifdef UA_DEBUG
  353. void UA_ByteString_printx_hex(char *label, const UA_ByteString *string) {
  354. UA_String_printx_hex(label, (const UA_String *)string);
  355. }
  356. #endif
  357. /** Creates a ByteString of the indicated length. The content is not set to zero. */
  358. UA_StatusCode UA_ByteString_newMembers(UA_ByteString *p, UA_Int32 length) {
  359. if(length > 0) {
  360. if(!(p->data = UA_malloc((UA_UInt32)length)))
  361. return UA_STATUSCODE_BADOUTOFMEMORY;
  362. p->length = length;
  363. } else {
  364. p->data = UA_NULL;
  365. if(length == 0)
  366. p->length = 0;
  367. else
  368. p->length = -1;
  369. }
  370. return UA_STATUSCODE_GOOD;
  371. }
  372. /* XmlElement */
  373. UA_TYPE_AS(UA_XmlElement, UA_ByteString)
  374. /* NodeId */
  375. void UA_NodeId_init(UA_NodeId *p) {
  376. p->identifierType = UA_NODEIDTYPE_NUMERIC;
  377. p->namespaceIndex = 0;
  378. memset(&p->identifier, 0, sizeof(p->identifier));
  379. }
  380. UA_TYPE_NEW_DEFAULT(UA_NodeId)
  381. UA_StatusCode UA_NodeId_copy(UA_NodeId const *src, UA_NodeId *dst) {
  382. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  383. switch(src->identifierType) {
  384. case UA_NODEIDTYPE_NUMERIC:
  385. *dst = *src;
  386. return UA_STATUSCODE_GOOD;
  387. break;
  388. case UA_NODEIDTYPE_STRING: // Table 6, second entry
  389. retval |= UA_String_copy(&src->identifier.string, &dst->identifier.string);
  390. break;
  391. case UA_NODEIDTYPE_GUID: // Table 6, third entry
  392. retval |= UA_Guid_copy(&src->identifier.guid, &dst->identifier.guid);
  393. break;
  394. case UA_NODEIDTYPE_BYTESTRING: // Table 6, "OPAQUE"
  395. retval |= UA_ByteString_copy(&src->identifier.byteString, &dst->identifier.byteString);
  396. break;
  397. default:
  398. UA_NodeId_init(dst);
  399. return UA_STATUSCODE_BADINTERNALERROR;
  400. }
  401. dst->identifierType = src->identifierType;
  402. if(retval)
  403. UA_NodeId_deleteMembers(dst);
  404. return retval;
  405. }
  406. static UA_Boolean UA_NodeId_isBasicType(UA_NodeId const *id) {
  407. return id->namespaceIndex == 0 &&
  408. id->identifierType == UA_NODEIDTYPE_NUMERIC &&
  409. id->identifier.numeric <= UA_DIAGNOSTICINFO;
  410. }
  411. UA_TYPE_DELETE_DEFAULT(UA_NodeId)
  412. void UA_NodeId_deleteMembers(UA_NodeId *p) {
  413. switch(p->identifierType) {
  414. case UA_NODEIDTYPE_NUMERIC:
  415. // nothing to do
  416. break;
  417. case UA_NODEIDTYPE_STRING: // Table 6, second entry
  418. UA_String_deleteMembers(&p->identifier.string);
  419. break;
  420. case UA_NODEIDTYPE_GUID: // Table 6, third entry
  421. UA_Guid_deleteMembers(&p->identifier.guid);
  422. break;
  423. case UA_NODEIDTYPE_BYTESTRING: // Table 6, "OPAQUE"
  424. UA_ByteString_deleteMembers(&p->identifier.byteString);
  425. break;
  426. }
  427. }
  428. #ifdef UA_DEBUG
  429. void UA_NodeId_print(const UA_NodeId *p, FILE *stream) {
  430. fprintf(stream, "(UA_NodeId){");
  431. switch(p->identifierType) {
  432. case UA_NODEIDTYPE_NUMERIC:
  433. fprintf(stream, "UA_NODEIDTYPE_NUMERIC");
  434. break;
  435. case UA_NODEIDTYPE_STRING:
  436. fprintf(stream, "UA_NODEIDTYPE_STRING");
  437. break;
  438. case UA_NODEIDTYPE_BYTESTRING:
  439. fprintf(stream, "UA_NODEIDTYPE_BYTESTRING");
  440. break;
  441. case UA_NODEIDTYPE_GUID:
  442. fprintf(stream, "UA_NODEIDTYPE_GUID");
  443. break;
  444. default:
  445. fprintf(stream, "ERROR");
  446. break;
  447. }
  448. fprintf(stream, ",%u,", p->namespaceIndex);
  449. switch(p->identifierType) {
  450. case UA_NODEIDTYPE_NUMERIC:
  451. fprintf(stream, ".identifier.numeric=%u", p->identifier.numeric);
  452. break;
  453. case UA_NODEIDTYPE_STRING:
  454. fprintf(stream, ".identifier.string=%.*s", p->identifier.string.length, p->identifier.string.data);
  455. break;
  456. case UA_NODEIDTYPE_BYTESTRING:
  457. fprintf(stream, ".identifier.byteString=%.*s", p->identifier.byteString.length,
  458. p->identifier.byteString.data);
  459. break;
  460. case UA_NODEIDTYPE_GUID:
  461. fprintf(stream, ".identifer.guid=");
  462. UA_Guid_print(&p->identifier.guid, stream);
  463. break;
  464. default:
  465. fprintf(stream, "ERROR");
  466. break;
  467. }
  468. fprintf(stream, "}");
  469. }
  470. #endif
  471. UA_Boolean UA_NodeId_equal(const UA_NodeId *n1, const UA_NodeId *n2) {
  472. if(n1->namespaceIndex != n2->namespaceIndex)
  473. return UA_FALSE;
  474. switch(n1->identifierType) {
  475. case UA_NODEIDTYPE_NUMERIC:
  476. if(n1->identifier.numeric == n2->identifier.numeric)
  477. return UA_TRUE;
  478. else
  479. return UA_FALSE;
  480. case UA_NODEIDTYPE_STRING:
  481. return UA_String_equal(&n1->identifier.string, &n2->identifier.string);
  482. case UA_NODEIDTYPE_GUID:
  483. return UA_Guid_equal(&n1->identifier.guid, &n2->identifier.guid);
  484. case UA_NODEIDTYPE_BYTESTRING:
  485. return UA_ByteString_equal(&n1->identifier.byteString, &n2->identifier.byteString);
  486. }
  487. return UA_FALSE;
  488. }
  489. UA_Boolean UA_NodeId_isNull(const UA_NodeId *p) {
  490. switch(p->identifierType) {
  491. case UA_NODEIDTYPE_NUMERIC:
  492. if(p->namespaceIndex != 0 || p->identifier.numeric != 0)
  493. return UA_FALSE;
  494. break;
  495. case UA_NODEIDTYPE_STRING:
  496. if(p->namespaceIndex != 0 || p->identifier.string.length != 0)
  497. return UA_FALSE;
  498. break;
  499. case UA_NODEIDTYPE_GUID:
  500. if(p->namespaceIndex != 0 ||
  501. memcmp(&p->identifier.guid, (char[sizeof(UA_Guid)]) { 0 }, sizeof(UA_Guid)) != 0)
  502. return UA_FALSE;
  503. break;
  504. case UA_NODEIDTYPE_BYTESTRING:
  505. if(p->namespaceIndex != 0 || p->identifier.byteString.length != 0)
  506. return UA_FALSE;
  507. break;
  508. default:
  509. return UA_FALSE;
  510. }
  511. return UA_TRUE;
  512. }
  513. /* ExpandedNodeId */
  514. UA_TYPE_DELETE_DEFAULT(UA_ExpandedNodeId)
  515. void UA_ExpandedNodeId_deleteMembers(UA_ExpandedNodeId *p) {
  516. UA_NodeId_deleteMembers(&p->nodeId);
  517. UA_String_deleteMembers(&p->namespaceUri);
  518. }
  519. void UA_ExpandedNodeId_init(UA_ExpandedNodeId *p) {
  520. UA_NodeId_init(&p->nodeId);
  521. UA_String_init(&p->namespaceUri);
  522. p->serverIndex = 0;
  523. }
  524. UA_TYPE_NEW_DEFAULT(UA_ExpandedNodeId)
  525. UA_StatusCode UA_ExpandedNodeId_copy(UA_ExpandedNodeId const *src, UA_ExpandedNodeId *dst) {
  526. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  527. retval |= UA_String_copy(&src->namespaceUri, &dst->namespaceUri);
  528. retval |= UA_NodeId_copy(&src->nodeId, &dst->nodeId);
  529. dst->serverIndex = src->serverIndex;
  530. if(retval)
  531. UA_ExpandedNodeId_deleteMembers(dst);
  532. return retval;
  533. }
  534. #ifdef UA_DEBUG
  535. void UA_ExpandedNodeId_print(const UA_ExpandedNodeId *p, FILE *stream) {
  536. fprintf(stream, "(UA_ExpandedNodeId){");
  537. UA_NodeId_print(&p->nodeId, stream);
  538. fprintf(stream, ",");
  539. UA_String_print(&p->namespaceUri, stream);
  540. fprintf(stream, ",");
  541. UA_UInt32_print(&p->serverIndex, stream);
  542. fprintf(stream, "}");
  543. }
  544. #endif
  545. UA_Boolean UA_ExpandedNodeId_isNull(const UA_ExpandedNodeId *p) {
  546. return UA_NodeId_isNull(&p->nodeId);
  547. }
  548. /* StatusCode */
  549. UA_TYPE_AS(UA_StatusCode, UA_UInt32)
  550. /* QualifiedName */
  551. UA_TYPE_DELETE_DEFAULT(UA_QualifiedName)
  552. void UA_QualifiedName_deleteMembers(UA_QualifiedName *p) {
  553. UA_String_deleteMembers(&p->name);
  554. }
  555. void UA_QualifiedName_init(UA_QualifiedName *p) {
  556. UA_String_init(&p->name);
  557. p->namespaceIndex = 0;
  558. }
  559. UA_TYPE_NEW_DEFAULT(UA_QualifiedName)
  560. UA_StatusCode UA_QualifiedName_copy(UA_QualifiedName const *src, UA_QualifiedName *dst) {
  561. UA_StatusCode retval = UA_String_copy(&src->name, &dst->name);
  562. dst->namespaceIndex = src->namespaceIndex;
  563. if(retval)
  564. UA_QualifiedName_deleteMembers(dst);
  565. return retval;
  566. }
  567. UA_StatusCode UA_QualifiedName_copycstring(char const *src, UA_QualifiedName *dst) {
  568. dst->namespaceIndex = 0;
  569. return UA_String_copycstring(src, &dst->name);
  570. }
  571. #ifdef UA_DEBUG
  572. void UA_QualifiedName_print(const UA_QualifiedName *p, FILE *stream) {
  573. fprintf(stream, "(UA_QualifiedName){");
  574. UA_UInt16_print(&p->namespaceIndex, stream);
  575. fprintf(stream, ",");
  576. UA_String_print(&p->name, stream);
  577. fprintf(stream, "}");
  578. }
  579. #endif
  580. #ifdef UA_DEBUG
  581. void UA_QualifiedName_printf(char const *label, const UA_QualifiedName *qn) {
  582. printf("%s {NamespaceIndex=%u, Length=%d, Data=%.*s}\n", label, qn->namespaceIndex,
  583. qn->name.length, qn->name.length, (char *)qn->name.data);
  584. }
  585. #endif
  586. /* LocalizedText */
  587. UA_TYPE_DELETE_DEFAULT(UA_LocalizedText)
  588. void UA_LocalizedText_deleteMembers(UA_LocalizedText *p) {
  589. UA_String_deleteMembers(&p->locale);
  590. UA_String_deleteMembers(&p->text);
  591. }
  592. void UA_LocalizedText_init(UA_LocalizedText *p) {
  593. UA_String_init(&p->locale);
  594. UA_String_init(&p->text);
  595. }
  596. UA_TYPE_NEW_DEFAULT(UA_LocalizedText)
  597. UA_StatusCode UA_LocalizedText_copycstring(char const *src, UA_LocalizedText *dst) {
  598. UA_StatusCode retval = UA_String_copycstring("en", &dst->locale); // TODO: Are language codes upper case?
  599. retval |= UA_String_copycstring(src, &dst->text);
  600. if(retval) {
  601. UA_LocalizedText_deleteMembers(dst);
  602. UA_LocalizedText_init(dst);
  603. }
  604. return retval;
  605. }
  606. UA_StatusCode UA_LocalizedText_copy(UA_LocalizedText const *src, UA_LocalizedText *dst) {
  607. UA_Int32 retval = UA_String_copy(&src->locale, &dst->locale);
  608. retval |= UA_String_copy(&src->text, &dst->text);
  609. if(retval)
  610. UA_LocalizedText_deleteMembers(dst);
  611. return retval;
  612. }
  613. #ifdef UA_DEBUG
  614. void UA_LocalizedText_print(const UA_LocalizedText *p, FILE *stream) {
  615. fprintf(stream, "(UA_LocalizedText){");
  616. UA_String_print(&p->locale, stream);
  617. fprintf(stream, ",");
  618. UA_String_print(&p->text, stream);
  619. fprintf(stream, "}");
  620. }
  621. #endif
  622. /* ExtensionObject */
  623. UA_TYPE_DELETE_DEFAULT(UA_ExtensionObject)
  624. void UA_ExtensionObject_deleteMembers(UA_ExtensionObject *p) {
  625. UA_NodeId_deleteMembers(&p->typeId);
  626. UA_ByteString_deleteMembers(&p->body);
  627. }
  628. void UA_ExtensionObject_init(UA_ExtensionObject *p) {
  629. UA_NodeId_init(&p->typeId);
  630. p->encoding = UA_EXTENSIONOBJECT_ENCODINGMASK_NOBODYISENCODED;
  631. UA_ByteString_init(&p->body);
  632. }
  633. UA_TYPE_NEW_DEFAULT(UA_ExtensionObject)
  634. UA_StatusCode UA_ExtensionObject_copy(UA_ExtensionObject const *src, UA_ExtensionObject *dst) {
  635. UA_StatusCode retval = UA_ByteString_copy(&src->body, &dst->body);
  636. retval |= UA_NodeId_copy(&src->typeId, &dst->typeId);
  637. dst->encoding = src->encoding;
  638. if(retval)
  639. UA_ExtensionObject_deleteMembers(dst);
  640. return retval;
  641. }
  642. #ifdef UA_DEBUG
  643. void UA_ExtensionObject_print(const UA_ExtensionObject *p, FILE *stream) {
  644. fprintf(stream, "(UA_ExtensionObject){");
  645. UA_NodeId_print(&p->typeId, stream);
  646. if(p->encoding == UA_EXTENSIONOBJECT_ENCODINGMASK_BODYISBYTESTRING)
  647. fprintf(stream, ",UA_EXTENSIONOBJECT_ENCODINGMASK_BODYISBYTESTRING,");
  648. else if(p->encoding == UA_EXTENSIONOBJECT_ENCODINGMASK_BODYISXML)
  649. fprintf(stream, ",UA_EXTENSIONOBJECT_ENCODINGMASK_BODYISXML,");
  650. else
  651. fprintf(stream, ",UA_EXTENSIONOBJECT_ENCODINGMASK_NOBODYISENCODED,");
  652. UA_ByteString_print(&p->body, stream);
  653. fprintf(stream, "}");
  654. }
  655. #endif
  656. /* DataValue */
  657. UA_TYPE_DELETE_DEFAULT(UA_DataValue)
  658. void UA_DataValue_deleteMembers(UA_DataValue *p) {
  659. UA_Variant_deleteMembers(&p->value);
  660. }
  661. void UA_DataValue_init(UA_DataValue *p) {
  662. p->encodingMask = 0;
  663. p->serverPicoseconds = 0;
  664. UA_DateTime_init(&p->serverTimestamp);
  665. p->sourcePicoseconds = 0;
  666. UA_DateTime_init(&p->sourceTimestamp);
  667. UA_StatusCode_init(&p->status);
  668. UA_Variant_init(&p->value);
  669. }
  670. UA_TYPE_NEW_DEFAULT(UA_DataValue)
  671. UA_StatusCode UA_DataValue_copy(UA_DataValue const *src, UA_DataValue *dst) {
  672. UA_StatusCode retval = UA_DateTime_copy(&src->serverTimestamp, &dst->serverTimestamp);
  673. retval |= UA_DateTime_copy(&src->sourceTimestamp, &dst->sourceTimestamp);
  674. retval |= UA_Variant_copy(&src->value, &dst->value);
  675. dst->encodingMask = src->encodingMask;
  676. dst->serverPicoseconds = src->serverPicoseconds;
  677. dst->sourcePicoseconds = src->sourcePicoseconds;
  678. dst->status = src->status;
  679. if(retval)
  680. UA_DataValue_deleteMembers(dst);
  681. return retval;
  682. }
  683. #ifdef UA_DEBUG
  684. void UA_DataValue_print(const UA_DataValue *p, FILE *stream) {
  685. fprintf(stream, "(UA_DataValue){");
  686. UA_Byte_print(&p->encodingMask, stream);
  687. fprintf(stream, ",");
  688. UA_Variant_print(&p->value, stream);
  689. fprintf(stream, ",");
  690. UA_StatusCode_print(&p->status, stream);
  691. fprintf(stream, ",");
  692. UA_DateTime_print(&p->sourceTimestamp, stream);
  693. fprintf(stream, ",");
  694. UA_Int16_print(&p->sourcePicoseconds, stream);
  695. fprintf(stream, ",");
  696. UA_DateTime_print(&p->serverTimestamp, stream);
  697. fprintf(stream, ",");
  698. UA_Int16_print(&p->serverPicoseconds, stream);
  699. fprintf(stream, "}");
  700. }
  701. #endif
  702. /* Variant */
  703. UA_TYPE_DELETE_DEFAULT(UA_Variant)
  704. void UA_Variant_deleteMembers(UA_Variant *p) {
  705. if(p->storageType == UA_VARIANT_DATA) {
  706. if(p->storage.data.dataPtr) {
  707. if(p->storage.data.arrayLength == 1)
  708. p->vt->delete(p->storage.data.dataPtr);
  709. else
  710. UA_Array_delete(p->storage.data.dataPtr, p->storage.data.arrayLength, p->vt);
  711. p->storage.data.dataPtr = UA_NULL;
  712. }
  713. if(p->storage.data.arrayDimensions) {
  714. UA_free(p->storage.data.arrayDimensions);
  715. p->storage.data.arrayDimensions = UA_NULL;
  716. }
  717. return;
  718. }
  719. if(p->storageType == UA_VARIANT_DATASOURCE) {
  720. p->storage.datasource.delete(p->storage.datasource.identifier);
  721. }
  722. }
  723. UA_TYPE_NEW_DEFAULT(UA_Variant)
  724. void UA_Variant_init(UA_Variant *p) {
  725. p->storageType = UA_VARIANT_DATA;
  726. p->storage.data.arrayLength = -1; // no element, p->data == UA_NULL
  727. p->storage.data.dataPtr = UA_NULL;
  728. p->storage.data.arrayDimensions = UA_NULL;
  729. p->storage.data.arrayDimensionsLength = -1;
  730. p->vt = &UA_TYPES[UA_INVALIDTYPE];
  731. }
  732. /** This function performs a deep copy. The resulting StorageType is UA_VARIANT_DATA. */
  733. UA_StatusCode UA_Variant_copy(UA_Variant const *src, UA_Variant *dst) {
  734. UA_Variant_init(dst);
  735. // get the data
  736. UA_VariantData *dstdata = &dst->storage.data;
  737. const UA_VariantData *srcdata;
  738. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  739. if(src->storageType == UA_VARIANT_DATA || src->storageType == UA_VARIANT_DATA_NODELETE)
  740. srcdata = &src->storage.data;
  741. else {
  742. retval |= src->storage.datasource.read(src->storage.datasource.identifier, &srcdata);
  743. if(retval)
  744. return retval;
  745. }
  746. // now copy the data to the destination
  747. retval |= UA_Array_copy(srcdata->dataPtr, srcdata->arrayLength, src->vt, &dstdata->dataPtr);
  748. if(retval == UA_STATUSCODE_GOOD) {
  749. dst->storageType = UA_VARIANT_DATA;
  750. dst->vt = src->vt;
  751. dstdata->arrayLength = srcdata->arrayLength;
  752. if(srcdata->arrayDimensions) {
  753. retval |= UA_Array_copy(srcdata->arrayDimensions, srcdata->arrayDimensionsLength, &UA_TYPES[UA_INT32],
  754. (void **)&dstdata->arrayDimensions);
  755. if(retval == UA_STATUSCODE_GOOD)
  756. dstdata->arrayDimensionsLength = srcdata->arrayDimensionsLength;
  757. else {
  758. UA_Variant_deleteMembers(dst);
  759. UA_Variant_init(dst);
  760. }
  761. }
  762. }
  763. // release the data source if necessary
  764. if(src->storageType == UA_VARIANT_DATASOURCE)
  765. src->storage.datasource.release(src->storage.datasource.identifier, srcdata);
  766. return retval;
  767. }
  768. /** Copies data into a variant. The target variant has always a storagetype UA_VARIANT_DATA */
  769. UA_StatusCode UA_Variant_copySetValue(UA_Variant *v, const UA_TypeVTable *vt, const void *value) {
  770. UA_Variant_init(v);
  771. v->vt = vt;
  772. v->storage.data.arrayLength = 1; // no array but a single entry
  773. UA_StatusCode retval = UA_STATUSCODE_GOOD;
  774. if((v->storage.data.dataPtr = vt->new()))
  775. retval |= vt->copy(value, v->storage.data.dataPtr);
  776. else
  777. retval = UA_STATUSCODE_BADOUTOFMEMORY;
  778. if(retval) {
  779. UA_Variant_deleteMembers(v);
  780. UA_Variant_init(v);
  781. }
  782. return retval;
  783. }
  784. UA_StatusCode UA_Variant_copySetArray(UA_Variant *v, const UA_TypeVTable *vt, UA_Int32 arrayLength, const void *array) {
  785. UA_Variant_init(v);
  786. v->vt = vt;
  787. v->storage.data.arrayLength = arrayLength;
  788. UA_StatusCode retval = UA_Array_copy(array, arrayLength, vt, &v->storage.data.dataPtr);
  789. if(retval) {
  790. UA_Variant_deleteMembers(v);
  791. UA_Variant_init(v);
  792. }
  793. return retval;
  794. }
  795. #ifdef UA_DEBUG
  796. void UA_Variant_print(const UA_Variant *p, FILE *stream) {
  797. UA_UInt32 ns0id = UA_ns0ToVTableIndex(&p->vt->typeId);
  798. if(p->storageType == UA_VARIANT_DATASOURCE) {
  799. fprintf(stream, "Variant from a Datasource");
  800. return;
  801. }
  802. fprintf(stream, "(UA_Variant){/*%s*/", p->vt->name);
  803. if(p->vt == &UA_TYPES[ns0id])
  804. fprintf(stream, "UA_TYPES[%d]", ns0id);
  805. else
  806. fprintf(stream, "ERROR (not a builtin type)");
  807. UA_Int32_print(&p->storage.data.arrayLength, stream);
  808. fprintf(stream, ",");
  809. UA_Array_print(p->storage.data.dataPtr, p->storage.data.arrayLength, p->vt, stream);
  810. fprintf(stream, ",");
  811. UA_Int32_print(&p->storage.data.arrayDimensionsLength, stream);
  812. fprintf(stream, ",");
  813. UA_Array_print(p->storage.data.arrayDimensions, p->storage.data.arrayDimensionsLength,
  814. &UA_TYPES[UA_INT32], stream);
  815. fprintf(stream, "}");
  816. }
  817. #endif
  818. /* DiagnosticInfo */
  819. UA_TYPE_DELETE_DEFAULT(UA_DiagnosticInfo)
  820. void UA_DiagnosticInfo_deleteMembers(UA_DiagnosticInfo *p) {
  821. UA_String_deleteMembers(&p->additionalInfo);
  822. if((p->encodingMask & UA_DIAGNOSTICINFO_ENCODINGMASK_INNERDIAGNOSTICINFO) && p->innerDiagnosticInfo) {
  823. UA_DiagnosticInfo_delete(p->innerDiagnosticInfo);
  824. p->innerDiagnosticInfo = UA_NULL;
  825. }
  826. }
  827. void UA_DiagnosticInfo_init(UA_DiagnosticInfo *p) {
  828. UA_String_init(&p->additionalInfo);
  829. p->encodingMask = 0;
  830. p->innerDiagnosticInfo = UA_NULL;
  831. UA_StatusCode_init(&p->innerStatusCode);
  832. p->locale = 0;
  833. p->localizedText = 0;
  834. p->namespaceUri = 0;
  835. p->symbolicId = 0;
  836. }
  837. UA_TYPE_NEW_DEFAULT(UA_DiagnosticInfo)
  838. UA_StatusCode UA_DiagnosticInfo_copy(UA_DiagnosticInfo const *src, UA_DiagnosticInfo *dst) {
  839. UA_DiagnosticInfo_init(dst);
  840. UA_StatusCode retval = UA_String_copy(&src->additionalInfo, &dst->additionalInfo);
  841. dst->encodingMask = src->encodingMask;
  842. retval |= UA_StatusCode_copy(&src->innerStatusCode, &dst->innerStatusCode);
  843. if((src->encodingMask & UA_DIAGNOSTICINFO_ENCODINGMASK_INNERDIAGNOSTICINFO) && src->innerDiagnosticInfo) {
  844. if((dst->innerDiagnosticInfo = UA_malloc(sizeof(UA_DiagnosticInfo))))
  845. retval |= UA_DiagnosticInfo_copy(src->innerDiagnosticInfo, dst->innerDiagnosticInfo);
  846. else
  847. retval |= UA_STATUSCODE_BADOUTOFMEMORY;
  848. }
  849. dst->locale = src->locale;
  850. dst->localizedText = src->localizedText;
  851. dst->namespaceUri = src->namespaceUri;
  852. dst->symbolicId = src->symbolicId;
  853. if(retval) {
  854. UA_DiagnosticInfo_deleteMembers(dst);
  855. UA_DiagnosticInfo_init(dst);
  856. }
  857. return retval;
  858. }
  859. #ifdef UA_DEBUG
  860. void UA_DiagnosticInfo_print(const UA_DiagnosticInfo *p, FILE *stream) {
  861. fprintf(stream, "(UA_DiagnosticInfo){");
  862. UA_Byte_print(&p->encodingMask, stream);
  863. fprintf(stream, ",");
  864. UA_Int32_print(&p->symbolicId, stream);
  865. fprintf(stream, ",");
  866. UA_Int32_print(&p->namespaceUri, stream);
  867. fprintf(stream, ",");
  868. UA_Int32_print(&p->localizedText, stream);
  869. fprintf(stream, ",");
  870. UA_Int32_print(&p->locale, stream);
  871. fprintf(stream, ",");
  872. UA_String_print(&p->additionalInfo, stream);
  873. fprintf(stream, ",");
  874. UA_StatusCode_print(&p->innerStatusCode, stream);
  875. fprintf(stream, ",");
  876. if(p->innerDiagnosticInfo) {
  877. fprintf(stream, "&");
  878. UA_DiagnosticInfo_print(p->innerDiagnosticInfo, stream);
  879. } else
  880. fprintf(stream, "UA_NULL");
  881. fprintf(stream, "}");
  882. }
  883. #endif
  884. /* InvalidType */
  885. void UA_InvalidType_delete(UA_InvalidType *p) {
  886. return;
  887. }
  888. void UA_InvalidType_deleteMembers(UA_InvalidType *p) {
  889. return;
  890. }
  891. void UA_InvalidType_init(UA_InvalidType *p) {
  892. return;
  893. }
  894. UA_StatusCode UA_InvalidType_copy(UA_InvalidType const *src, UA_InvalidType *dst) {
  895. return UA_STATUSCODE_BADINTERNALERROR;
  896. }
  897. UA_InvalidType * UA_InvalidType_new() {
  898. return UA_NULL;
  899. }
  900. #ifdef UA_DEBUG
  901. void UA_InvalidType_print(const UA_InvalidType *p, FILE *stream) {
  902. fprintf(stream, "(UA_InvalidType){ERROR (invalid type)}");
  903. }
  904. #endif
  905. /*********/
  906. /* Array */
  907. /*********/
  908. UA_StatusCode UA_Array_new(void **p, UA_Int32 noElements, const UA_TypeVTable *vt) {
  909. if(noElements <= 0) {
  910. *p = UA_NULL;
  911. return UA_STATUSCODE_GOOD;
  912. }
  913. // Arrays cannot be larger than 2^16 elements. This was randomly chosen so
  914. // that the development VM does not blow up during fuzzing tests.
  915. if(noElements > (1<<15)) {
  916. *p = UA_NULL;
  917. return UA_STATUSCODE_BADOUTOFMEMORY;
  918. }
  919. /* the cast to UInt32 can be made since noElements is positive */
  920. if(!(*p = UA_malloc(vt->memSize * (UA_UInt32)noElements)))
  921. return UA_STATUSCODE_BADOUTOFMEMORY;
  922. UA_Array_init(*p, noElements, vt);
  923. return UA_STATUSCODE_GOOD;
  924. }
  925. void UA_Array_init(void *p, UA_Int32 noElements, const UA_TypeVTable *vt) {
  926. UA_Byte *cp = (UA_Byte *)p; // so compilers allow pointer arithmetic
  927. UA_UInt32 memSize = vt->memSize;
  928. for(UA_Int32 i = 0;i<noElements;i++) {
  929. vt->init(cp);
  930. cp += memSize;
  931. }
  932. }
  933. void UA_Array_delete(void *p, UA_Int32 noElements, const UA_TypeVTable *vt) {
  934. UA_Byte *cp = (UA_Byte *)p; // so compilers allow pointer arithmetic
  935. UA_UInt32 memSize = vt->memSize;
  936. for(UA_Int32 i = 0;i<noElements;i++) {
  937. vt->deleteMembers(cp);
  938. cp += memSize;
  939. }
  940. if(noElements > 0)
  941. UA_free(p);
  942. }
  943. UA_StatusCode UA_Array_copy(const void *src, UA_Int32 noElements, const UA_TypeVTable *vt, void **dst) {
  944. UA_StatusCode retval = UA_Array_new(dst, noElements, vt);
  945. if(retval)
  946. return retval;
  947. const UA_Byte *csrc = (const UA_Byte *)src; // so compilers allow pointer arithmetic
  948. UA_Byte *cdst = (UA_Byte *)*dst;
  949. UA_UInt32 memSize = vt->memSize;
  950. UA_Int32 i = 0;
  951. for(;i < noElements && retval == UA_STATUSCODE_GOOD;i++) {
  952. retval |= vt->copy(csrc, cdst);
  953. csrc += memSize;
  954. cdst += memSize;
  955. }
  956. if(retval) {
  957. i--; // undo last increase
  958. UA_Array_delete(*dst, i, vt);
  959. *dst = UA_NULL;
  960. }
  961. return retval;
  962. }
  963. #ifdef UA_DEBUG
  964. void UA_Array_print(const void *p, UA_Int32 noElements, const UA_TypeVTable *vt, FILE *stream) {
  965. fprintf(stream, "(%s){", vt->name);
  966. const char *cp = (const char *)p; // so compilers allow pointer arithmetic
  967. UA_UInt32 memSize = vt->memSize;
  968. for(UA_Int32 i = 0;i < noElements;i++) {
  969. vt->print(cp, stream);
  970. fprintf(stream, ",");
  971. cp += memSize;
  972. }
  973. }
  974. #endif