Fix a bunch of warnings.
[metze/wireshark/wip.git] / plugins / opcua / opcua_simpletypes.c
1 /******************************************************************************
2 ** $Id$
3 **
4 ** Copyright (C) 2006-2007 ascolab GmbH. All Rights Reserved.
5 ** Web: http://www.ascolab.com
6 **
7 ** This program is free software; you can redistribute it and/or
8 ** modify it under the terms of the GNU General Public License
9 ** as published by the Free Software Foundation; either version 2
10 ** of the License, or (at your option) any later version.
11 **
12 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
13 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
14 **
15 ** Project: OpcUa Wireshark Plugin
16 **
17 ** Description: Implementation of OpcUa built-in type parsers.
18 **              This contains all the simple types and some complex types.
19 **
20 ** Author: Gerhard Gappmeier <gerhard.gappmeier@ascolab.com>
21 ** Last change by: $Author: gergap $
22 **
23 ******************************************************************************/
24
25 #include "config.h"
26
27 #include <glib.h>
28 #include <epan/packet.h>
29 #include <epan/dissectors/packet-windows-common.h>
30 #include "opcua_simpletypes.h"
31 #include "opcua_hfindeces.h"
32 #include "opcua_extensionobjectids.h"
33 #include <epan/emem.h>
34
35 #define DIAGNOSTICINFO_ENCODINGMASK_SYMBOLICID_FLAG           0x01
36 #define DIAGNOSTICINFO_ENCODINGMASK_NAMESPACE_FLAG            0x02
37 #define DIAGNOSTICINFO_ENCODINGMASK_LOCALIZEDTEXT_FLAG        0x04
38 #define DIAGNOSTICINFO_ENCODINGMASK_ADDITIONALINFO_FLAG       0x08
39 #define DIAGNOSTICINFO_ENCODINGMASK_INNERSTATUSCODE_FLAG      0x10
40 #define DIAGNOSTICINFO_ENCODINGMASK_INNERDIAGNOSTICINFO_FLAG  0x20
41 #define LOCALIZEDTEXT_ENCODINGBYTE_LOCALE                     0x01
42 #define LOCALIZEDTEXT_ENCODINGBYTE_TEXT                       0x02
43 #define NODEID_URIMASK                                        0x80
44 #define NODEID_SERVERINDEXFLAG                                0x40
45 #define DATAVALUE_ENCODINGBYTE_VALUE                          0x01
46 #define DATAVALUE_ENCODINGBYTE_STATUSCODE                     0x02
47 #define DATAVALUE_ENCODINGBYTE_SOURCETIMESTAMP                0x04
48 #define DATAVALUE_ENCODINGBYTE_SERVERTIMESTAMP                0x08
49 #define DATAVALUE_ENCODINGBYTE_SOURCEPICOSECONDS              0x10
50 #define DATAVALUE_ENCODINGBYTE_SERVERPICOSECONDS              0x20
51 #define EXTOBJ_ENCODINGMASK_BINBODY_FLAG                      0x01
52 #define EXTOBJ_ENCODINGMASK_XMLBODY_FLAG                      0x02
53
54 /* Chosen arbitrarily */
55 #define MAX_ARRAY_LEN 10000
56
57 void dispatchExtensionObjectType(proto_tree *tree, tvbuff_t *tvb, gint *pOffset, int TypeId);
58
59 static int hf_opcua_diag_mask_symbolicflag = -1;
60 static int hf_opcua_diag_mask_namespaceflag = -1;
61 static int hf_opcua_diag_mask_localizedtextflag = -1;
62 static int hf_opcua_diag_mask_additionalinfoflag = -1;
63 static int hf_opcua_diag_mask_innerstatuscodeflag = -1;
64 static int hf_opcua_diag_mask_innerdiaginfoflag = -1;
65 static int hf_opcua_loctext_mask_localeflag = -1;
66 static int hf_opcua_loctext_mask_textflag = -1;
67 static int hf_opcua_datavalue_mask_valueflag = -1;
68 static int hf_opcua_datavalue_mask_statuscodeflag = -1;
69 static int hf_opcua_datavalue_mask_sourcetimestampflag = -1;
70 static int hf_opcua_datavalue_mask_servertimestampflag = -1;
71 static int hf_opcua_datavalue_mask_sourcepicoseconds = -1;
72 static int hf_opcua_datavalue_mask_serverpicoseconds = -1;
73 static int hf_opcua_nodeid_encodingmask = -1;
74 static int hf_opcua_variant_encodingmask = -1;
75 static int hf_opcua_nodeid_nsid = -1;
76 static int hf_opcua_nodeid_numeric = -1;
77 static int hf_opcua_localizedtext_locale = -1;
78 static int hf_opcua_localizedtext_text = -1;
79 static int hf_opcua_qualifiedname_id = -1;
80 static int hf_opcua_qualifiedname_name = -1;
81 static int hf_opcua_SourceTimestamp = -1;
82 static int hf_opcua_SourcePicoseconds = -1;
83 static int hf_opcua_ServerTimestamp = -1;
84 static int hf_opcua_ServerPicoseconds = -1;
85 static int hf_opcua_diag_symbolicid = -1;
86 static int hf_opcua_diag_namespace = -1;
87 static int hf_opcua_diag_localizedtext = -1;
88 static int hf_opcua_diag_additionalinfo = -1;
89 static int hf_opcua_diag_innerstatuscode = -1;
90 static int hf_opcua_extobj_mask_binbodyflag = -1;
91 static int hf_opcua_extobj_mask_xmlbodyflag = -1;
92 static int hf_opcua_ArraySize = -1;
93 static int hf_opcua_Uri = -1;
94 static int hf_opcua_ServerIndex = -1;
95
96 /** NodeId encoding mask table */
97 static const value_string g_nodeidmasks[] = {
98     { 0, "Two byte encoded Numeric" },
99     { 1, "Four byte encoded Numeric" },
100     { 2, "Numeric of arbitrary length" },
101     { 3, "String" },
102     { 4, "URI" },
103     { 5, "GUID" },
104     { 6, "ByteString" },
105     { 0x80, "UriMask" },
106     { 0, NULL }
107 };
108
109 /** UA Variant Type enum */
110 typedef enum _OpcUa_BuiltInType
111 {
112     OpcUaType_Null = 0,
113     OpcUaType_Boolean = 1,
114     OpcUaType_SByte = 2,
115     OpcUaType_Byte = 3,
116     OpcUaType_Int16 = 4,
117     OpcUaType_UInt16 = 5,
118     OpcUaType_Int32 = 6,
119     OpcUaType_UInt32 = 7,
120     OpcUaType_Int64 = 8,
121     OpcUaType_UInt64 = 9,
122     OpcUaType_Float = 10,
123     OpcUaType_Double = 11,
124     OpcUaType_String = 12,
125     OpcUaType_DateTime = 13,
126     OpcUaType_Guid = 14,
127     OpcUaType_ByteString = 15,
128     OpcUaType_XmlElement = 16,
129     OpcUaType_NodeId = 17,
130     OpcUaType_ExpandedNodeId = 18,
131     OpcUaType_StatusCode = 19,
132     OpcUaType_QualifiedName = 20,
133     OpcUaType_LocalizedText = 21,
134     OpcUaType_ExtensionObject = 22,
135     OpcUaType_DataValue = 23,
136     OpcUaType_Variant = 24,
137     OpcUaType_DiagnosticInfo = 25
138 }
139 OpcUa_BuiltInType;
140
141 /** Variant encoding mask table */
142 static const value_string g_VariantTypes[] = {
143     { 0, "Null" },
144     { 1, "Boolean" },
145     { 2, "SByte" },
146     { 3, "Byte" },
147     { 4, "Int16" },
148     { 5, "UInt16" },
149     { 6, "Int32" },
150     { 7, "UInt32" },
151     { 8, "Int64" },
152     { 9, "UInt64" },
153     { 10, "Float" },
154     { 11, "Double" },
155     { 12, "String" },
156     { 13, "DateTime" },
157     { 14, "Guid" },
158     { 15, "ByteString" },
159     { 16, "XmlElement" },
160     { 17, "NodeId" },
161     { 18, "ExpandedNodeId" },
162     { 19, "StatusCode" },
163     { 20, "QualifiedName" },
164     { 21, "LocalizedText" },
165     { 22, "ExtensionObject" },
166     { 23, "DataValue" },
167     { 24, "Variant" },
168     { 25, "DiagnosticInfo" },
169     { 0x80,   "Array of Null" },
170     { 0x80+1, "Array of Boolean" },
171     { 0x80+2, "Array of SByte" },
172     { 0x80+3, "Array of Byte" },
173     { 0x80+4, "Array of Int16" },
174     { 0x80+5, "Array of UInt16" },
175     { 0x80+6, "Array of Int32" },
176     { 0x80+7, "Array of UInt32" },
177     { 0x80+8, "Array of Int64" },
178     { 0x80+9, "Array of UInt64" },
179     { 0x80+10, "Array of Float" },
180     { 0x80+11, "Array of Double" },
181     { 0x80+12, "Array of String" },
182     { 0x80+13, "Array of DateTime" },
183     { 0x80+14, "Array of Guid" },
184     { 0x80+15, "Array of ByteString" },
185     { 0x80+16, "Array of XmlElement" },
186     { 0x80+17, "Array of NodeId" },
187     { 0x80+18, "Array of ExpandedNodeId" },
188     { 0x80+19, "Array of StatusCode" },
189     { 0x80+20, "Array of QualifiedName" },
190     { 0x80+21, "Array of LocalizedText" },
191     { 0x80+22, "Array of ExtensionObject" },
192     { 0x80+23, "Array of DataValue" },
193     { 0x80+24, "Array of Variant" },
194     { 0x80+25, "Array of DiagnosticInfo" },
195     { 0, NULL }
196 };
197 #define VARIANT_ARRAYDIMENSIONS 0x40
198 #define VARIANT_ARRAYMASK 0x80
199
200 /* trees */
201 static gint ett_opcua_array = -1;
202 static gint ett_opcua_diagnosticinfo = -1;
203 static gint ett_opcua_nodeid = -1;
204 static gint ett_opcua_localeid = -1;
205 static gint ett_opcua_localizedtext = -1;
206 static gint ett_opcua_qualifiedname = -1;
207 static gint ett_opcua_datavalue = -1;
208 static gint ett_opcua_variant = -1;
209 static gint ett_opcua_extensionobject = -1;
210 static gint ett_opcua_extobj_encodingmask = -1;
211 static gint *ett[] =
212 {
213   &ett_opcua_array,
214   &ett_opcua_diagnosticinfo,
215   &ett_opcua_nodeid,
216   &ett_opcua_localeid,
217   &ett_opcua_localizedtext,
218   &ett_opcua_qualifiedname,
219   &ett_opcua_datavalue,
220   &ett_opcua_variant,
221   &ett_opcua_extensionobject,
222   &ett_opcua_extobj_encodingmask
223 };
224
225 void registerSimpleTypes(int proto)
226 {
227     static hf_register_info hf[] =
228     {
229         /* full name  ,           abbreviation  ,       type     , display  , strings, bitmask, blurb, id, parent, ref_count, bitshift */
230         { &hf_opcua_diag_mask_symbolicflag,
231         {  "has symbolic id",           "opcua.has_symbolic_id", FT_BOOLEAN, 8, NULL, DIAGNOSTICINFO_ENCODINGMASK_SYMBOLICID_FLAG, NULL, HFILL }
232         },
233         { &hf_opcua_diag_mask_namespaceflag,
234         {  "has namespace",             "opcua.has_namespace", FT_BOOLEAN, 8, NULL, DIAGNOSTICINFO_ENCODINGMASK_NAMESPACE_FLAG, NULL, HFILL }
235         },
236         { &hf_opcua_diag_mask_localizedtextflag,
237         {  "has localizedtext",         "opcua.has_localizedtext", FT_BOOLEAN, 8, NULL, DIAGNOSTICINFO_ENCODINGMASK_LOCALIZEDTEXT_FLAG, NULL, HFILL }
238         },
239         { &hf_opcua_diag_mask_additionalinfoflag,
240         {  "has additional info",       "opcua.has_additional_info", FT_BOOLEAN, 8, NULL, DIAGNOSTICINFO_ENCODINGMASK_ADDITIONALINFO_FLAG, NULL, HFILL }
241         },
242         { &hf_opcua_diag_mask_innerstatuscodeflag,
243         {  "has inner statuscode",      "opcua.has_inner_statuscode", FT_BOOLEAN, 8, NULL, DIAGNOSTICINFO_ENCODINGMASK_INNERSTATUSCODE_FLAG, NULL, HFILL }
244         },
245         { &hf_opcua_diag_mask_innerdiaginfoflag,
246         {  "has inner diagnostic info", "opcua.has_inner_diagnostic_code", FT_BOOLEAN, 8, NULL, DIAGNOSTICINFO_ENCODINGMASK_INNERDIAGNOSTICINFO_FLAG, NULL, HFILL }
247         },
248         { &hf_opcua_loctext_mask_localeflag,
249         {  "has locale information", "opcua.has_locale_information", FT_BOOLEAN, 8, NULL, LOCALIZEDTEXT_ENCODINGBYTE_LOCALE, NULL, HFILL }
250         },
251         { &hf_opcua_loctext_mask_textflag,
252         {  "has text", "opcua.has_text", FT_BOOLEAN, 8, NULL, LOCALIZEDTEXT_ENCODINGBYTE_TEXT, NULL, HFILL }
253         },
254         { &hf_opcua_nodeid_encodingmask,
255         {  "NodeId EncodingMask",        "application.nodeid.encodingmask", FT_UINT8,   BASE_HEX,  VALS(g_nodeidmasks), 0x0,    NULL,    HFILL }
256         },
257         { &hf_opcua_nodeid_nsid,
258         {  "NodeId Namespace Id",        "application.nodeid.nsid",         FT_UINT16,  BASE_DEC,  NULL, 0x0,    NULL,    HFILL }
259         },
260         { &hf_opcua_nodeid_numeric,
261         {  "NodeId Identifier Numeric",  "application.nodeid.numeric",      FT_UINT32,  BASE_DEC,  NULL, 0x0,    NULL,    HFILL }
262         },
263         { &hf_opcua_localizedtext_locale, { "Locale", "opcua.Locale", FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL } },
264         { &hf_opcua_localizedtext_text,   { "Text", "opcua.Text", FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL } },
265         { &hf_opcua_qualifiedname_id,     { "Id", "opcua.Id", FT_UINT16, BASE_DEC,  NULL, 0x0, NULL, HFILL } },
266         { &hf_opcua_qualifiedname_name,   { "Name", "opcua.Name", FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL } },
267         { &hf_opcua_datavalue_mask_valueflag,           {  "has value", "opcua.has_value",            FT_BOOLEAN, 8, NULL, DATAVALUE_ENCODINGBYTE_VALUE, NULL, HFILL } },
268         { &hf_opcua_datavalue_mask_statuscodeflag,      {  "has statuscode", "opcua.has_statuscode",       FT_BOOLEAN, 8, NULL, DATAVALUE_ENCODINGBYTE_STATUSCODE, NULL, HFILL } },
269         { &hf_opcua_datavalue_mask_sourcetimestampflag, {  "has source timestamp", "opcua.has_source_timestamp", FT_BOOLEAN, 8, NULL, DATAVALUE_ENCODINGBYTE_SOURCETIMESTAMP, NULL, HFILL } },
270         { &hf_opcua_datavalue_mask_servertimestampflag, {  "has server timestamp", "opcua.has_server_timestamp", FT_BOOLEAN, 8, NULL, DATAVALUE_ENCODINGBYTE_SERVERTIMESTAMP, NULL, HFILL } },
271         { &hf_opcua_datavalue_mask_sourcepicoseconds, {  "has source picoseconds", "opcua.has_source_picoseconds", FT_BOOLEAN, 8, NULL, DATAVALUE_ENCODINGBYTE_SOURCEPICOSECONDS, NULL, HFILL } },
272         { &hf_opcua_datavalue_mask_serverpicoseconds, {  "has server picoseconds", "opcua.has_server_picoseconds", FT_BOOLEAN, 8, NULL, DATAVALUE_ENCODINGBYTE_SERVERPICOSECONDS, NULL, HFILL } },
273         { &hf_opcua_variant_encodingmask, { "Variant Type", "opcua.has_value", FT_UINT8, BASE_HEX, VALS(g_VariantTypes), 0x0, NULL, HFILL } },
274         { &hf_opcua_SourceTimestamp, { "SourceTimestamp", "opcua.SourceTimestamp", FT_ABSOLUTE_TIME, ABSOLUTE_TIME_LOCAL, NULL, 0x0, NULL, HFILL } },
275         { &hf_opcua_SourcePicoseconds, { "SourcePicoseconds", "opcua.SourcePicoseconds", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL } },
276         { &hf_opcua_ServerTimestamp, { "ServerTimestamp", "opcua.ServerTimestamp", FT_ABSOLUTE_TIME, ABSOLUTE_TIME_LOCAL, NULL, 0x0, NULL, HFILL } },
277         { &hf_opcua_ServerPicoseconds, { "ServerPicoseconds", "opcua.ServerPicoseconds", FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL } },
278         { &hf_opcua_diag_symbolicid,      { "SymbolicId", "opcua.SymbolicId",       FT_INT32, BASE_DEC, NULL, 0x0, NULL, HFILL } },
279         { &hf_opcua_diag_namespace,       { "Namespace", "opcua.Namespace",       FT_INT32, BASE_DEC, NULL, 0x0, NULL, HFILL } },
280         { &hf_opcua_diag_localizedtext,   { "LocaliezdText", "opcua.LocaliezdText",   FT_INT32, BASE_DEC, NULL, 0x0, NULL, HFILL } },
281         { &hf_opcua_diag_additionalinfo,  { "AdditionalInfo", "opcua.AdditionalInfo",  FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL } },
282         { &hf_opcua_diag_innerstatuscode, { "InnerStatusCode", "opcua.InnerStatusCode", FT_UINT32, BASE_HEX, NULL, 0x0, NULL, HFILL } },
283         { &hf_opcua_extobj_mask_binbodyflag, {  "has binary body", "opcua.has_binary_body", FT_BOOLEAN, 8, NULL, EXTOBJ_ENCODINGMASK_BINBODY_FLAG, NULL, HFILL } },
284         { &hf_opcua_extobj_mask_xmlbodyflag, {  "has xml body",    "opcua.has_xml_body", FT_BOOLEAN, 8, NULL, EXTOBJ_ENCODINGMASK_XMLBODY_FLAG, NULL, HFILL } },
285         { &hf_opcua_ArraySize, { "ArraySize", "opcua.ArraySize", FT_INT32, BASE_DEC, NULL, 0x0, NULL, HFILL } },
286         { &hf_opcua_Uri, { "Uri", "opcua.Uri", FT_STRING, BASE_NONE, NULL, 0x0, NULL, HFILL } },
287         { &hf_opcua_ServerIndex, { "ServerIndex", "opcua.ServerIndex", FT_UINT32, BASE_DEC, NULL, 0x0, NULL, HFILL } }
288     };
289
290     proto_register_field_array(proto, hf, array_length(hf));
291     proto_register_subtree_array(ett, array_length(ett));
292 }
293
294 void parseBoolean(proto_tree *tree, tvbuff_t *tvb, gint *pOffset, int hfIndex)
295 {
296     proto_tree_add_item(tree, hfIndex, tvb, *pOffset, 1, ENC_LITTLE_ENDIAN); *pOffset+=1;
297 }
298
299 void parseByte(proto_tree *tree, tvbuff_t *tvb, gint *pOffset, int hfIndex)
300 {
301     proto_tree_add_item(tree, hfIndex, tvb, *pOffset, 1, ENC_LITTLE_ENDIAN); *pOffset+=1;
302 }
303
304 void parseSByte(proto_tree *tree, tvbuff_t *tvb, gint *pOffset, int hfIndex)
305 {
306     proto_tree_add_item(tree, hfIndex, tvb, *pOffset, 1, ENC_LITTLE_ENDIAN); *pOffset+=1;
307 }
308
309 void parseUInt16(proto_tree *tree, tvbuff_t *tvb, gint *pOffset, int hfIndex)
310 {
311     proto_tree_add_item(tree, hfIndex, tvb, *pOffset, 2, ENC_LITTLE_ENDIAN); *pOffset+=2;
312 }
313
314 void parseInt16(proto_tree *tree, tvbuff_t *tvb, gint *pOffset, int hfIndex)
315 {
316     proto_tree_add_item(tree, hfIndex, tvb, *pOffset, 2, ENC_LITTLE_ENDIAN); *pOffset+=2;
317 }
318
319 void parseUInt32(proto_tree *tree, tvbuff_t *tvb, gint *pOffset, int hfIndex)
320 {
321     proto_tree_add_item(tree, hfIndex, tvb, *pOffset, 4, ENC_LITTLE_ENDIAN); *pOffset+=4;
322 }
323
324 void parseInt32(proto_tree *tree, tvbuff_t *tvb, gint *pOffset, int hfIndex)
325 {
326     proto_tree_add_item(tree, hfIndex, tvb, *pOffset, 4, ENC_LITTLE_ENDIAN); *pOffset+=4;
327 }
328
329 void parseUInt64(proto_tree *tree, tvbuff_t *tvb, gint *pOffset, int hfIndex)
330 {
331     proto_tree_add_item(tree, hfIndex, tvb, *pOffset, 8, ENC_LITTLE_ENDIAN); *pOffset+=8;
332 }
333
334 void parseInt64(proto_tree *tree, tvbuff_t *tvb, gint *pOffset, int hfIndex)
335 {
336     proto_tree_add_item(tree, hfIndex, tvb, *pOffset, 8, ENC_LITTLE_ENDIAN); *pOffset+=8;
337 }
338
339 void parseString(proto_tree *tree, tvbuff_t *tvb, gint *pOffset, int hfIndex)
340 {
341     char *szValue;
342     gint iOffset = *pOffset;
343     gint32 iLen = tvb_get_letohl(tvb, *pOffset);
344     iOffset+=4;
345
346     if (iLen == -1)
347     {
348         proto_item *item = proto_tree_add_item(tree, hfIndex, tvb, iOffset, 0, ENC_ASCII|ENC_NA);
349         proto_item_append_text(item, "[OpcUa Null String]");
350     }
351     else if (iLen == 0)
352     {
353         proto_item *item = proto_tree_add_item(tree, hfIndex, tvb, iOffset, 0, ENC_ASCII|ENC_NA);
354         proto_item_append_text(item, "[OpcUa Empty String]");
355     }
356     else if (iLen > 0)
357     {
358         proto_tree_add_item(tree, hfIndex, tvb, iOffset, iLen, ENC_ASCII|ENC_NA);
359         iOffset += iLen; /* eat the whole string */
360     }
361     else
362     {
363         proto_item *item = proto_tree_add_item(tree, hfIndex, tvb, iOffset, 0, ENC_ASCII|ENC_NA);
364         szValue = ep_strdup_printf("[Invalid String] Invalid length: %d", iLen);
365         proto_item_append_text(item, "%s", szValue);
366     }
367
368     *pOffset = iOffset;
369 }
370
371 void parseStatusCode(proto_tree *tree, tvbuff_t *tvb, gint *pOffset, int hfIndex)
372 {
373     proto_tree_add_item(tree, hfIndex, tvb, *pOffset, 4, ENC_LITTLE_ENDIAN);
374     *pOffset += 4;
375 }
376
377 void parseLocalizedText(proto_tree *tree, tvbuff_t *tvb, gint *pOffset, const char *szFieldName)
378 {
379     gint        iOffset = *pOffset;
380     guint8      EncodingMask;
381     proto_tree *mask_tree;
382     proto_tree *subtree;
383     proto_item *ti;
384
385     ti = proto_tree_add_text(tree, tvb, 0, -1, "%s: LocalizedText", szFieldName);
386     subtree = proto_item_add_subtree(ti, ett_opcua_localizedtext);
387
388     /* parse encoding mask */
389     EncodingMask = tvb_get_guint8(tvb, iOffset);
390     ti = proto_tree_add_text(subtree, tvb, 0, -1, "EncodingMask");
391     mask_tree = proto_item_add_subtree(ti, ett_opcua_localizedtext);
392     proto_tree_add_item(mask_tree, hf_opcua_loctext_mask_localeflag, tvb, iOffset, 1, ENC_LITTLE_ENDIAN);
393     proto_tree_add_item(mask_tree, hf_opcua_loctext_mask_textflag,   tvb, iOffset, 1, ENC_LITTLE_ENDIAN);
394     iOffset++;
395
396     if (EncodingMask & LOCALIZEDTEXT_ENCODINGBYTE_LOCALE)
397     {
398         parseString(subtree, tvb, &iOffset, hf_opcua_localizedtext_locale);
399     }
400
401     if (EncodingMask & LOCALIZEDTEXT_ENCODINGBYTE_TEXT)
402     {
403         parseString(subtree, tvb, &iOffset, hf_opcua_localizedtext_text);
404     }
405
406     *pOffset = iOffset;
407 }
408
409 void parseGuid(proto_tree *tree, tvbuff_t *tvb, gint *pOffset, int hfIndex)
410 {
411     proto_tree_add_item(tree, hfIndex, tvb, *pOffset, GUID_LEN, ENC_NA); *pOffset+=GUID_LEN;
412 }
413
414 void parseByteString(proto_tree *tree, tvbuff_t *tvb, gint *pOffset, int hfIndex)
415 {
416     char *szValue;
417     int iOffset = *pOffset;
418     gint32 iLen = tvb_get_letohl(tvb, iOffset);
419     iOffset += 4;
420
421     if (iLen == -1)
422     {
423         proto_item *item = proto_tree_add_item(tree, hfIndex, tvb, iOffset, 0, ENC_NA);
424         proto_item_append_text(item, "[OpcUa Null ByteString]");
425     }
426     else if (iLen == 0)
427     {
428         proto_item *item = proto_tree_add_item(tree, hfIndex, tvb, iOffset, 0, ENC_NA);
429         proto_item_append_text(item, "[OpcUa Empty ByteString]");
430     }
431     else if (iLen > 0)
432     {
433         proto_tree_add_item(tree, hfIndex, tvb, iOffset, iLen, ENC_NA);
434         iOffset += iLen; /* eat the whole bytestring */
435     }
436     else
437     {
438         proto_item *item = proto_tree_add_item(tree, hfIndex, tvb, iOffset, 0, ENC_NA);
439         szValue = ep_strdup_printf("[Invalid ByteString] Invalid length: %d", iLen);
440         proto_item_append_text(item, "%s", szValue);
441     }
442
443     *pOffset = iOffset;
444 }
445
446 void parseXmlElement(proto_tree *tree, tvbuff_t *tvb, gint *pOffset, int hfIndex)
447 {
448     parseByteString(tree, tvb, pOffset, hfIndex);
449 }
450
451 void parseFloat(proto_tree *tree, tvbuff_t *tvb, gint *pOffset, int hfIndex)
452 {
453      proto_tree_add_item(tree, hfIndex, tvb, *pOffset, (int)sizeof(gfloat), ENC_LITTLE_ENDIAN);
454      *pOffset += (int)sizeof(gfloat);
455 }
456
457 void parseDouble(proto_tree *tree, tvbuff_t *tvb, gint *pOffset, int hfIndex)
458 {
459      proto_tree_add_item(tree, hfIndex, tvb, *pOffset, (int)sizeof(gdouble), ENC_LITTLE_ENDIAN);
460      *pOffset += (int)sizeof(gdouble);
461 }
462
463 void parseDateTime(proto_tree *tree, tvbuff_t *tvb, gint *pOffset, int hfIndex)
464 {
465     *pOffset = dissect_nt_64bit_time(tvb, tree, *pOffset, hfIndex);
466 }
467
468 void parseDiagnosticInfo(proto_tree *tree, tvbuff_t *tvb, gint *pOffset, const char *szFieldName)
469 {
470     gint        iOffset = *pOffset;
471     guint8      EncodingMask;
472     proto_tree *mask_tree;
473     proto_tree *subtree;
474     proto_item *ti;
475
476     ti = proto_tree_add_text(tree, tvb, 0, -1, "%s: DiagnosticInfo", szFieldName);
477     subtree = proto_item_add_subtree(ti, ett_opcua_diagnosticinfo);
478
479     /* parse encoding mask */
480     EncodingMask = tvb_get_guint8(tvb, iOffset);
481     ti = proto_tree_add_text(subtree, tvb, 0, -1, "EncodingMask");
482     mask_tree = proto_item_add_subtree(ti, ett_opcua_diagnosticinfo);
483     proto_tree_add_item(mask_tree, hf_opcua_diag_mask_symbolicflag,        tvb, iOffset, 1, ENC_LITTLE_ENDIAN);
484     proto_tree_add_item(mask_tree, hf_opcua_diag_mask_namespaceflag,       tvb, iOffset, 1, ENC_LITTLE_ENDIAN);
485     proto_tree_add_item(mask_tree, hf_opcua_diag_mask_localizedtextflag,   tvb, iOffset, 1, ENC_LITTLE_ENDIAN);
486     proto_tree_add_item(mask_tree, hf_opcua_diag_mask_additionalinfoflag,  tvb, iOffset, 1, ENC_LITTLE_ENDIAN);
487     proto_tree_add_item(mask_tree, hf_opcua_diag_mask_innerstatuscodeflag, tvb, iOffset, 1, ENC_LITTLE_ENDIAN);
488     proto_tree_add_item(mask_tree, hf_opcua_diag_mask_innerdiaginfoflag,   tvb, iOffset, 1, ENC_LITTLE_ENDIAN);
489     iOffset++;
490
491     if (EncodingMask & DIAGNOSTICINFO_ENCODINGMASK_SYMBOLICID_FLAG)
492     {
493         parseInt32(subtree, tvb, &iOffset, hf_opcua_diag_symbolicid);
494     }
495     if (EncodingMask & DIAGNOSTICINFO_ENCODINGMASK_NAMESPACE_FLAG)
496     {
497         parseInt32(subtree, tvb, &iOffset, hf_opcua_diag_namespace);
498     }
499     if (EncodingMask & DIAGNOSTICINFO_ENCODINGMASK_LOCALIZEDTEXT_FLAG)
500     {
501         parseInt32(subtree, tvb, &iOffset, hf_opcua_diag_localizedtext);
502     }
503     if (EncodingMask & DIAGNOSTICINFO_ENCODINGMASK_ADDITIONALINFO_FLAG)
504     {
505         parseString(subtree, tvb, &iOffset, hf_opcua_diag_additionalinfo);
506     }
507     if (EncodingMask & DIAGNOSTICINFO_ENCODINGMASK_INNERSTATUSCODE_FLAG)
508     {
509         parseStatusCode(subtree, tvb, &iOffset, hf_opcua_diag_innerstatuscode);
510     }
511     if (EncodingMask & DIAGNOSTICINFO_ENCODINGMASK_INNERDIAGNOSTICINFO_FLAG)
512     {
513         parseDiagnosticInfo(subtree, tvb, &iOffset, "Inner DiagnosticInfo");
514     }
515
516     *pOffset = iOffset;
517 }
518
519 void parseQualifiedName(proto_tree *tree, tvbuff_t *tvb, gint *pOffset, const char *szFieldName)
520 {
521     proto_item *ti = proto_tree_add_text(tree, tvb, 0, -1, "%s: QualifiedName", szFieldName);
522     proto_tree *subtree = proto_item_add_subtree(ti, ett_opcua_qualifiedname);
523
524     parseUInt16(subtree, tvb, pOffset, hf_opcua_qualifiedname_id);
525     parseString(subtree, tvb, pOffset, hf_opcua_qualifiedname_name);
526 }
527
528 void parseDataValue(proto_tree *tree, tvbuff_t *tvb, gint *pOffset, const char *szFieldName)
529 {
530     proto_item *ti = proto_tree_add_text(tree, tvb, 0, -1, "%s: DataValue", szFieldName);
531     proto_tree *subtree = proto_item_add_subtree(ti, ett_opcua_datavalue);
532     proto_tree *mask_tree;
533     gint    iOffset = *pOffset;
534     guint8  EncodingMask;
535
536     EncodingMask = tvb_get_guint8(tvb, iOffset);
537     ti = proto_tree_add_text(subtree, tvb, 0, -1, "EncodingMask");
538     mask_tree = proto_item_add_subtree(ti, ett_opcua_datavalue);
539     proto_tree_add_item(mask_tree, hf_opcua_datavalue_mask_valueflag,           tvb, iOffset, 1, ENC_LITTLE_ENDIAN);
540     proto_tree_add_item(mask_tree, hf_opcua_datavalue_mask_statuscodeflag,      tvb, iOffset, 1, ENC_LITTLE_ENDIAN);
541     proto_tree_add_item(mask_tree, hf_opcua_datavalue_mask_sourcetimestampflag, tvb, iOffset, 1, ENC_LITTLE_ENDIAN);
542     proto_tree_add_item(mask_tree, hf_opcua_datavalue_mask_servertimestampflag, tvb, iOffset, 1, ENC_LITTLE_ENDIAN);
543     proto_tree_add_item(mask_tree, hf_opcua_datavalue_mask_sourcepicoseconds,   tvb, iOffset, 1, ENC_LITTLE_ENDIAN);
544     proto_tree_add_item(mask_tree, hf_opcua_datavalue_mask_serverpicoseconds,   tvb, iOffset, 1, ENC_LITTLE_ENDIAN);
545     iOffset++;
546
547     if (EncodingMask & DATAVALUE_ENCODINGBYTE_VALUE)
548     {
549         parseVariant(subtree, tvb, &iOffset, "Value");
550     }
551     if (EncodingMask & DATAVALUE_ENCODINGBYTE_STATUSCODE)
552     {
553         parseStatusCode(subtree, tvb, &iOffset, hf_opcua_StatusCode);
554     }
555     if (EncodingMask & DATAVALUE_ENCODINGBYTE_SOURCETIMESTAMP)
556     {
557         parseDateTime(subtree, tvb, &iOffset, hf_opcua_SourceTimestamp);
558     }
559     if (EncodingMask & DATAVALUE_ENCODINGBYTE_SOURCEPICOSECONDS)
560     {
561         parseUInt16(subtree, tvb, &iOffset, hf_opcua_SourcePicoseconds);
562     }
563     if (EncodingMask & DATAVALUE_ENCODINGBYTE_SERVERTIMESTAMP)
564     {
565         parseDateTime(subtree, tvb, &iOffset, hf_opcua_ServerTimestamp);
566     }
567     if (EncodingMask & DATAVALUE_ENCODINGBYTE_SERVERPICOSECONDS)
568     {
569         parseUInt16(subtree, tvb, &iOffset, hf_opcua_ServerPicoseconds);
570     }
571
572     *pOffset = iOffset;
573 }
574
575 void parseVariant(proto_tree *tree, tvbuff_t *tvb, gint *pOffset, const char *szFieldName)
576 {
577     proto_item *ti = proto_tree_add_text(tree, tvb, 0, -1, "%s: Variant", szFieldName);
578     proto_tree *subtree = proto_item_add_subtree(ti, ett_opcua_variant);
579     gint    iOffset = *pOffset;
580     guint8  EncodingMask;
581     gint32  ArrayLength = 0;
582
583     EncodingMask = tvb_get_guint8(tvb, iOffset);
584     proto_tree_add_item(subtree, hf_opcua_variant_encodingmask, tvb, iOffset, 1, ENC_LITTLE_ENDIAN);
585     iOffset++;
586
587     if (EncodingMask & VARIANT_ARRAYMASK)
588     {
589         ArrayLength = tvb_get_letohl(tvb, iOffset);
590
591         /* type is encoded in bits 0-5 */
592         switch(EncodingMask & 0x3f)
593         {
594         case OpcUaType_Null: break;
595         case OpcUaType_Boolean: parseArraySimple(subtree, tvb, &iOffset, hf_opcua_Boolean, parseBoolean); break;
596         case OpcUaType_SByte: parseArraySimple(subtree, tvb, &iOffset, hf_opcua_SByte, parseSByte); break;
597         case OpcUaType_Byte: parseArraySimple(subtree, tvb, &iOffset, hf_opcua_Byte, parseByte); break;
598         case OpcUaType_Int16: parseArraySimple(subtree, tvb, &iOffset, hf_opcua_Int16, parseInt16); break;
599         case OpcUaType_UInt16: parseArraySimple(subtree, tvb, &iOffset, hf_opcua_UInt16, parseUInt16); break;
600         case OpcUaType_Int32: parseArraySimple(subtree, tvb, &iOffset, hf_opcua_Int32, parseInt32); break;
601         case OpcUaType_UInt32: parseArraySimple(subtree, tvb, &iOffset, hf_opcua_UInt32, parseUInt32); break;
602         case OpcUaType_Int64: parseArraySimple(subtree, tvb, &iOffset, hf_opcua_Int64, parseInt64); break;
603         case OpcUaType_UInt64: parseArraySimple(subtree, tvb, &iOffset, hf_opcua_UInt64, parseUInt64); break;
604         case OpcUaType_Float: parseArraySimple(subtree, tvb, &iOffset, hf_opcua_Float, parseFloat); break;
605         case OpcUaType_Double: parseArraySimple(subtree, tvb, &iOffset, hf_opcua_Double, parseDouble); break;
606         case OpcUaType_String: parseArraySimple(subtree, tvb, &iOffset, hf_opcua_String, parseString); break;
607         case OpcUaType_DateTime: parseArraySimple(subtree, tvb, &iOffset, hf_opcua_DateTime, parseDateTime); break;
608         case OpcUaType_Guid: parseArraySimple(subtree, tvb, &iOffset, hf_opcua_Guid, parseGuid); break;
609         case OpcUaType_ByteString: parseArraySimple(subtree, tvb, &iOffset, hf_opcua_ByteString, parseByteString); break;
610         case OpcUaType_XmlElement: parseArraySimple(subtree, tvb, &iOffset, hf_opcua_XmlElement, parseXmlElement); break;
611         case OpcUaType_NodeId: parseArrayComplex(subtree, tvb, &iOffset, "NodeId", parseNodeId); break;
612         case OpcUaType_ExpandedNodeId: parseArrayComplex(subtree, tvb, &iOffset, "ExpandedNodeId", parseExpandedNodeId); break;
613         case OpcUaType_StatusCode: parseArraySimple(subtree, tvb, &iOffset, hf_opcua_StatusCode, parseStatusCode); break;
614         case OpcUaType_DiagnosticInfo: parseArrayComplex(subtree, tvb, &iOffset, "DiagnosticInfo", parseDiagnosticInfo); break;
615         case OpcUaType_QualifiedName: parseArrayComplex(subtree, tvb, &iOffset, "QualifiedName", parseQualifiedName); break;
616         case OpcUaType_LocalizedText: parseArrayComplex(subtree, tvb, &iOffset, "LocalizedText", parseLocalizedText); break;
617         case OpcUaType_ExtensionObject: parseArrayComplex(subtree, tvb, &iOffset, "ExtensionObject", parseExtensionObject); break;
618         case OpcUaType_DataValue: parseArrayComplex(subtree, tvb, &iOffset, "DataValue", parseDataValue); break;
619         case OpcUaType_Variant: parseArrayComplex(subtree, tvb, &iOffset, "Variant", parseVariant); break;
620         }
621
622         if (EncodingMask & VARIANT_ARRAYDIMENSIONS)
623         {
624             proto_item *ti_2 = proto_tree_add_text(tree, tvb, 0, -1, "Array Dimensions");
625             proto_tree *subtree_2 = proto_item_add_subtree(ti_2, ett_opcua_array);
626             int i;
627
628             if (ArrayLength < MAX_ARRAY_LEN)
629             {
630                 for (i=0; i<ArrayLength; i++)
631                 {
632                     parseInt32(subtree_2, tvb, pOffset, hf_opcua_Int32);
633                 }
634             }
635             else
636             {
637                 proto_item *pi;
638                 /* XXX - This should be expert_add_info_format, but we need pinfo for that */
639                 pi = proto_tree_add_text(tree, tvb, iOffset, 4, "Array length %d too large to process", ArrayLength);
640                 PROTO_ITEM_SET_GENERATED(pi);
641             }
642         }
643     }
644     else
645     {
646         /* type is encoded in bits 0-5 */
647         switch(EncodingMask & 0x3f)
648         {
649         case OpcUaType_Null: break;
650         case OpcUaType_Boolean: parseBoolean(subtree, tvb, &iOffset, hf_opcua_Boolean); break;
651         case OpcUaType_SByte: parseSByte(subtree, tvb, &iOffset, hf_opcua_SByte); break;
652         case OpcUaType_Byte: parseByte(subtree, tvb, &iOffset, hf_opcua_Byte); break;
653         case OpcUaType_Int16: parseInt16(subtree, tvb, &iOffset, hf_opcua_Int16); break;
654         case OpcUaType_UInt16: parseUInt16(subtree, tvb, &iOffset, hf_opcua_UInt16); break;
655         case OpcUaType_Int32: parseInt32(subtree, tvb, &iOffset, hf_opcua_Int32); break;
656         case OpcUaType_UInt32: parseUInt32(subtree, tvb, &iOffset, hf_opcua_UInt32); break;
657         case OpcUaType_Int64: parseInt64(subtree, tvb, &iOffset, hf_opcua_Int64); break;
658         case OpcUaType_UInt64: parseUInt64(subtree, tvb, &iOffset, hf_opcua_UInt64); break;
659         case OpcUaType_Float: parseFloat(subtree, tvb, &iOffset, hf_opcua_Float); break;
660         case OpcUaType_Double: parseDouble(subtree, tvb, &iOffset, hf_opcua_Double); break;
661         case OpcUaType_String: parseString(subtree, tvb, &iOffset, hf_opcua_String); break;
662         case OpcUaType_DateTime: parseDateTime(subtree, tvb, &iOffset, hf_opcua_DateTime); break;
663         case OpcUaType_Guid: parseGuid(subtree, tvb, &iOffset, hf_opcua_Guid); break;
664         case OpcUaType_ByteString: parseByteString(subtree, tvb, &iOffset, hf_opcua_ByteString); break;
665         case OpcUaType_XmlElement: parseXmlElement(subtree, tvb, &iOffset, hf_opcua_XmlElement); break;
666         case OpcUaType_NodeId: parseNodeId(subtree, tvb, &iOffset, "Value"); break;
667         case OpcUaType_ExpandedNodeId: parseExpandedNodeId(subtree, tvb, &iOffset, "Value"); break;
668         case OpcUaType_StatusCode: parseStatusCode(subtree, tvb, &iOffset, hf_opcua_StatusCode); break;
669         case OpcUaType_DiagnosticInfo: parseDiagnosticInfo(subtree, tvb, &iOffset, "Value"); break;
670         case OpcUaType_QualifiedName: parseQualifiedName(subtree, tvb, &iOffset, "Value"); break;
671         case OpcUaType_LocalizedText: parseLocalizedText(subtree, tvb, &iOffset, "Value"); break;
672         case OpcUaType_ExtensionObject: parseExtensionObject(subtree, tvb, &iOffset, "Value"); break;
673         case OpcUaType_DataValue: parseDataValue(subtree, tvb, &iOffset, "Value"); break;
674         case OpcUaType_Variant: parseVariant(subtree, tvb, &iOffset, "Value"); break;
675         }
676     }
677
678     *pOffset = iOffset;
679 }
680
681 /** General parsing function for arrays of simple types.
682  * All arrays have one 4 byte signed integer length information,
683  * followed by n data elements.
684  */
685 void parseArraySimple(proto_tree *tree, tvbuff_t *tvb, gint *pOffset, int hfIndex, fctSimpleTypeParser pParserFunction)
686 {
687     static const char szFieldName[] = "Array of Simple Type";
688     proto_item *ti = proto_tree_add_text(tree, tvb, 0, -1, "%s", szFieldName);
689     proto_tree *subtree = proto_item_add_subtree(ti, ett_opcua_array);
690     int i;
691     gint32 iLen;
692
693     /* read array length */
694     iLen = tvb_get_letohl(tvb, *pOffset);
695     proto_tree_add_item(subtree, hf_opcua_ArraySize, tvb, *pOffset, 4, ENC_LITTLE_ENDIAN);
696
697     if (iLen > MAX_ARRAY_LEN)
698     {
699         proto_item *pi;
700         pi = proto_tree_add_text(tree, tvb, *pOffset, 4, "Array length %d too large to process", iLen);
701         PROTO_ITEM_SET_GENERATED(pi);
702         return;
703     }
704
705     *pOffset += 4;
706     for (i=0; i<iLen; i++)
707     {
708         (*pParserFunction)(subtree, tvb, pOffset, hfIndex);
709     }
710 }
711
712 /** General parsing function for arrays of enums.
713  * All arrays have one 4 byte signed integer length information,
714  * followed by n data elements.
715  */
716 void parseArrayEnum(proto_tree *tree, tvbuff_t *tvb, gint *pOffset, fctEnumParser pParserFunction)
717 {
718     static const char szFieldName[] = "Array of Enum Type";
719     proto_item *ti = proto_tree_add_text(tree, tvb, 0, -1, "%s", szFieldName);
720     proto_tree *subtree = proto_item_add_subtree(ti, ett_opcua_array);
721     int i;
722     gint32 iLen;
723
724     /* read array length */
725     iLen = tvb_get_letohl(tvb, *pOffset);
726     proto_tree_add_item(subtree, hf_opcua_ArraySize, tvb, *pOffset, 4, ENC_LITTLE_ENDIAN);
727
728     if (iLen > MAX_ARRAY_LEN)
729     {
730         proto_item *pi;
731         pi = proto_tree_add_text(tree, tvb, *pOffset, 4, "Array length %d too large to process", iLen);
732         PROTO_ITEM_SET_GENERATED(pi);
733         return;
734     }
735
736     *pOffset += 4;
737     for (i=0; i<iLen; i++)
738     {
739         (*pParserFunction)(subtree, tvb, pOffset);
740     }
741 }
742
743 /** General parsing function for arrays of complex types.
744  * All arrays have one 4 byte signed integer length information,
745  * followed by n data elements.
746  */
747 void parseArrayComplex(proto_tree *tree, tvbuff_t *tvb, gint *pOffset, const char *szFieldName, fctComplexTypeParser pParserFunction)
748 {
749     proto_item *ti = proto_tree_add_text(tree, tvb, 0, -1, "Array of %s", szFieldName);
750     proto_tree *subtree = proto_item_add_subtree(ti, ett_opcua_array);
751     int i;
752     gint32 iLen;
753
754     /* read array length */
755     iLen = tvb_get_letohl(tvb, *pOffset);
756     proto_tree_add_item(subtree, hf_opcua_ArraySize, tvb, *pOffset, 4, ENC_LITTLE_ENDIAN);
757
758     if (iLen > MAX_ARRAY_LEN)
759     {
760         proto_item *pi;
761         pi = proto_tree_add_text(tree, tvb, *pOffset, 4, "Array length %d too large to process", iLen);
762         PROTO_ITEM_SET_GENERATED(pi);
763         return;
764     }
765
766     *pOffset += 4;
767     for (i=0; i<iLen; i++)
768     {
769         char szNum[20];
770         g_snprintf(szNum, 20, "[%i]", i);
771         (*pParserFunction)(subtree, tvb, pOffset, szNum);
772     }
773 }
774
775 void parseNodeId(proto_tree *tree, tvbuff_t *tvb, gint *pOffset, const char *szFieldName)
776 {
777     proto_item *ti = proto_tree_add_text(tree, tvb, 0, -1, "%s: NodeId", szFieldName);
778     proto_tree *subtree = proto_item_add_subtree(ti, ett_opcua_nodeid);
779     gint    iOffset = *pOffset;
780     guint8  EncodingMask;
781
782     EncodingMask = tvb_get_guint8(tvb, iOffset);
783     proto_tree_add_item(subtree, hf_opcua_nodeid_encodingmask, tvb, iOffset, 1, ENC_LITTLE_ENDIAN);
784     iOffset++;
785
786     switch(EncodingMask)
787     {
788     case 0x00: /* two byte node id */
789         proto_tree_add_item(subtree, hf_opcua_nodeid_numeric, tvb, iOffset, 1, ENC_LITTLE_ENDIAN);
790         iOffset+=1;
791         break;
792     case 0x01: /* four byte node id */
793         proto_tree_add_item(subtree, hf_opcua_nodeid_nsid, tvb, iOffset, 1, ENC_LITTLE_ENDIAN);
794         iOffset+=1;
795         proto_tree_add_item(subtree, hf_opcua_nodeid_numeric, tvb, iOffset, 2, ENC_LITTLE_ENDIAN);
796         iOffset+=2;
797         break;
798     case 0x02: /* numeric, that does not fit into four bytes */
799         proto_tree_add_item(subtree, hf_opcua_nodeid_nsid, tvb, iOffset, 2, ENC_LITTLE_ENDIAN);
800         iOffset+=2;
801         proto_tree_add_item(subtree, hf_opcua_nodeid_numeric, tvb, iOffset, 4, ENC_LITTLE_ENDIAN);
802         iOffset+=4;
803         break;
804     case 0x03: /* string */
805         proto_tree_add_item(subtree, hf_opcua_nodeid_nsid, tvb, iOffset, 2, ENC_LITTLE_ENDIAN);
806         iOffset+=2;
807         parseString(subtree, tvb, &iOffset, hf_opcua_String);
808         break;
809     case 0x04: /* guid */
810         proto_tree_add_item(subtree, hf_opcua_nodeid_nsid, tvb, iOffset, 2, ENC_LITTLE_ENDIAN);
811         iOffset+=2;
812         parseGuid(subtree, tvb, &iOffset, hf_opcua_Guid);
813         break;
814     case 0x05: /* byte string */
815         proto_tree_add_item(subtree, hf_opcua_nodeid_nsid, tvb, iOffset, 2, ENC_LITTLE_ENDIAN);
816         iOffset+=2;
817         parseByteString(subtree, tvb, &iOffset, hf_opcua_ByteString);
818         break;
819     };
820
821     *pOffset = iOffset;
822 }
823
824 void parseExtensionObject(proto_tree *tree, tvbuff_t *tvb, gint *pOffset, const char *szFieldName)
825 {
826     gint    iOffset = *pOffset;
827     guint8  EncodingMask;
828     guint32 TypeId;
829     proto_tree *extobj_tree;
830     proto_tree *mask_tree;
831     proto_item *ti;
832
833     /* add extension object subtree */
834     ti = proto_tree_add_text(tree, tvb, 0, -1, "%s : ExtensionObject", szFieldName);
835     extobj_tree = proto_item_add_subtree(ti, ett_opcua_extensionobject);
836
837     /* add nodeid subtree */
838     TypeId = getExtensionObjectType(tvb, &iOffset);
839     parseExpandedNodeId(extobj_tree, tvb, &iOffset, "TypeId");
840
841     /* parse encoding mask */
842     EncodingMask = tvb_get_guint8(tvb, iOffset);
843     ti = proto_tree_add_text(extobj_tree, tvb, 0, -1, "EncodingMask");
844     mask_tree = proto_item_add_subtree(ti, ett_opcua_extobj_encodingmask);
845     proto_tree_add_item(mask_tree, hf_opcua_extobj_mask_binbodyflag, tvb, iOffset, 1, ENC_LITTLE_ENDIAN);
846     proto_tree_add_item(mask_tree, hf_opcua_extobj_mask_xmlbodyflag, tvb, iOffset, 1, ENC_LITTLE_ENDIAN);
847     iOffset++;
848
849     if (EncodingMask & EXTOBJ_ENCODINGMASK_BINBODY_FLAG) /* has binary body ? */
850     {
851         dispatchExtensionObjectType(extobj_tree, tvb, &iOffset, TypeId);
852     }
853
854     *pOffset = iOffset;
855 }
856
857 void parseExpandedNodeId(proto_tree *tree, tvbuff_t *tvb, gint *pOffset, const char *szFieldName)
858 {
859     proto_item *ti = proto_tree_add_text(tree, tvb, 0, -1, "%s: ExpandedNodeId", szFieldName);
860     proto_tree *subtree = proto_item_add_subtree(ti, ett_opcua_nodeid);
861     gint    iOffset = *pOffset;
862     guint8  EncodingMask;
863
864     EncodingMask = tvb_get_guint8(tvb, iOffset);
865     proto_tree_add_item(subtree, hf_opcua_nodeid_encodingmask, tvb, iOffset, 1, ENC_LITTLE_ENDIAN);
866     iOffset++;
867
868     switch(EncodingMask)
869     {
870     case 0x00: /* two byte node id */
871         proto_tree_add_item(subtree, hf_opcua_nodeid_numeric, tvb, iOffset, 1, ENC_LITTLE_ENDIAN);
872         iOffset+=1;
873         break;
874     case 0x01: /* four byte node id */
875         proto_tree_add_item(subtree, hf_opcua_nodeid_nsid, tvb, iOffset, 1, ENC_LITTLE_ENDIAN);
876         iOffset+=1;
877         proto_tree_add_item(subtree, hf_opcua_nodeid_numeric, tvb, iOffset, 2, ENC_LITTLE_ENDIAN);
878         iOffset+=2;
879         break;
880     case 0x02: /* numeric, that does not fit into four bytes */
881         proto_tree_add_item(subtree, hf_opcua_nodeid_nsid, tvb, iOffset, 2, ENC_LITTLE_ENDIAN);
882         iOffset+=2;
883         proto_tree_add_item(subtree, hf_opcua_nodeid_numeric, tvb, iOffset, 4, ENC_LITTLE_ENDIAN);
884         iOffset+=4;
885         break;
886     case 0x03: /* string */
887         proto_tree_add_item(subtree, hf_opcua_nodeid_nsid, tvb, iOffset, 2, ENC_LITTLE_ENDIAN);
888         iOffset+=2;
889         parseString(subtree, tvb, &iOffset, hf_opcua_String);
890         break;
891     case 0x04: /* guid */
892         proto_tree_add_item(subtree, hf_opcua_nodeid_nsid, tvb, iOffset, 2, ENC_LITTLE_ENDIAN);
893         iOffset+=2;
894         parseGuid(subtree, tvb, &iOffset, hf_opcua_Guid);
895         break;
896     case 0x05: /* byte string */
897         proto_tree_add_item(subtree, hf_opcua_nodeid_nsid, tvb, iOffset, 2, ENC_LITTLE_ENDIAN);
898         iOffset+=2;
899         parseByteString(subtree, tvb, &iOffset, hf_opcua_ByteString);
900         break;
901     };
902
903     if (EncodingMask & NODEID_URIMASK)
904     {
905         parseString(subtree, tvb, &iOffset, hf_opcua_Uri);
906     }
907     if (EncodingMask & NODEID_SERVERINDEXFLAG)
908     {
909         parseUInt32(subtree, tvb, &iOffset, hf_opcua_ServerIndex);
910     }
911
912     *pOffset = iOffset;
913 }
914
915 guint32 getExtensionObjectType(tvbuff_t *tvb, gint *pOffset)
916 {
917     gint    iOffset = *pOffset;
918     guint8  EncodingMask;
919     guint32 Numeric = 0;
920
921     EncodingMask = tvb_get_guint8(tvb, iOffset);
922     iOffset++;
923
924     switch(EncodingMask)
925     {
926     case 0x00: /* two byte node id */
927         Numeric = tvb_get_guint8(tvb, iOffset);
928         iOffset+=1;
929         break;
930     case 0x01: /* four byte node id */
931         iOffset+=1;
932         Numeric = tvb_get_letohs(tvb, iOffset);
933         break;
934     case 0x02: /* numeric, that does not fit into four bytes */
935         iOffset+=4;
936         Numeric = tvb_get_letohl(tvb, iOffset);
937         break;
938     case 0x03: /* string */
939     case 0x04: /* uri */
940     case 0x05: /* guid */
941     case 0x06: /* byte string */
942         /* NOT USED */
943         break;
944     };
945
946     return Numeric;
947 }