ua_types.c 34 KB

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