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