ua_types.c 35 KB

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