Squelch an MSVC++ complaint.
[obnox/wireshark/wip.git] / packet-x25.c
index 51141f796916464372ac8badbf3499d775c4061c..01f71eb1f9719620b301d453ffe30f37a2e13b29 100644 (file)
@@ -1,8 +1,8 @@
 /* packet-x25.c
  * Routines for x25 packet disassembly
- * Olivier Abad <abad@daba.dhis.net>
+ * Olivier Abad <oabad@cybercable.fr>
  *
- * $Id: packet-x25.c,v 1.23 2000/04/13 18:18:53 gram Exp $
+ * $Id: packet-x25.c,v 1.53 2001/07/18 15:49:29 oabad Exp $
  *
  * Ethereal - Network traffic analyzer
  * By Gerald Combs <gerald@zing.org>
 #include <glib.h>
 #include <stdlib.h>
 #include <string.h>
-#include "etypes.h"
+#include "llcsaps.h"
 #include "packet.h"
-#include "packet-ip.h"
-#include "packet-osi.h"
 #include "nlpid.h"
 
 #define FROM_DCE                       0x80
 #define X25_FAC_PRIORITY               0xD2
 
 static int proto_x25 = -1;
+static int hf_x25_gfi = -1;
+static int hf_x25_abit = -1;
 static int hf_x25_qbit = -1;
 static int hf_x25_dbit = -1;
 static int hf_x25_mod = -1;
 static int hf_x25_lcn = -1;
 static int hf_x25_type = -1;
-static int hf_x25_p_r = -1;
-static int hf_x25_mbit = -1;
-static int hf_x25_p_s = -1;
-static int proto_ex25 = -1;
-static int hf_ex25_qbit = -1;
-static int hf_ex25_dbit = -1;
-static int hf_ex25_mod = -1;
-static int hf_ex25_lcn = -1;
-static int hf_ex25_type = -1;
-static int hf_ex25_p_r = -1;
-static int hf_ex25_mbit = -1;
-static int hf_ex25_p_s = -1;
+static int hf_x25_p_r_mod8 = -1;
+static int hf_x25_p_r_mod128 = -1;
+static int hf_x25_mbit_mod8 = -1;
+static int hf_x25_mbit_mod128 = -1;
+static int hf_x25_p_s_mod8 = -1;
+static int hf_x25_p_s_mod128 = -1;
 
 static gint ett_x25 = -1;
+static gint ett_x25_gfi = -1;
 static gint ett_x25_fac = -1;
 static gint ett_x25_fac_unknown = -1;
 static gint ett_x25_fac_mark = -1;
@@ -158,6 +153,9 @@ static const value_string vals_x25_type[] = {
        { 0,   NULL}
 };
 
+static dissector_handle_t ip_handle;
+static dissector_handle_t ositp_handle;
+
 /*
  * each vc_info node contains :
  *   the time of the first frame using this dissector (secs and usecs)
@@ -171,7 +169,7 @@ static const value_string vals_x25_type[] = {
 typedef struct _vc_info {
        guint32 first_frame_secs, first_frame_usecs;
        guint32 last_frame_secs, last_frame_usecs;
-       void (*dissect)(const u_char *, int, frame_data *, proto_tree *);
+       dissector_handle_t dissect;
        struct _vc_info *next;
 } vc_info;
 
@@ -189,7 +187,7 @@ typedef struct _global_vc_info {
 
 static global_vc_info *hash_table[64];
 
-void
+static void
 free_vc_info(vc_info *pt)
 {
   vc_info *vci = pt;
@@ -223,10 +221,9 @@ reinit_x25_hashtable(void)
   }
 }
 
-void
+static void
 x25_hash_add_proto_start(guint16 vc, guint32 frame_secs, guint32 frame_usecs,
-                        void (*dissect)(const u_char *, int, frame_data *,
-                                      proto_tree *))
+                        dissector_handle_t dissect)
 {
   int idx = vc % 64;
   global_vc_info *hash_ent;
@@ -306,7 +303,7 @@ x25_hash_add_proto_start(guint16 vc, guint32 frame_secs, guint32 frame_usecs,
   }
 }
 
-void
+static void
 x25_hash_add_proto_end(guint16 vc, guint32 frame_secs, guint32 frame_usecs)
 {
   global_vc_info *hash_ent = hash_table[vc%64];
@@ -322,7 +319,8 @@ x25_hash_add_proto_end(guint16 vc, guint32 frame_secs, guint32 frame_usecs)
   vci->last_frame_usecs = frame_usecs;
 }
 
-void (*x25_hash_get_dissect(guint32 frame_secs, guint32 frame_usecs, guint16 vc))(const u_char *, int, frame_data *, proto_tree *)
+static dissector_handle_t
+x25_hash_get_dissect(guint32 frame_secs, guint32 frame_usecs, guint16 vc)
 {
   global_vc_info *hash_ent = hash_table[vc%64];
   vc_info *vci;
@@ -377,10 +375,22 @@ static char *clear_code(unsigned char code)
        return "DTE Originated";
     if (code == 0x01)
        return "Number Busy";
+    if (code == 0x03)
+       return "Invalid Facility Requested";
+    if (code == 0x05)
+       return "Network Congestion";
     if (code == 0x09)
        return "Out Of Order";
+    if (code == 0x0B)
+       return "Access Barred";
+    if (code == 0x0D)
+       return "Not Obtainable";
     if (code == 0x11)
        return "Remote Procedure Error";
+    if (code == 0x13)
+       return "Local Procedure Error";
+    if (code == 0x15)
+       return "RPOA Out Of Order";
     if (code == 0x19)
        return "Reverse Charging Acceptance Not Subscribed";
     if (code == 0x21)
@@ -389,18 +399,6 @@ static char *clear_code(unsigned char code)
        return "Fast Select Acceptance Not Subscribed";
     if (code == 0x39)
        return "Destination Absent";
-    if (code == 0x03)
-       return "Invalid Facility Requested";
-    if (code == 0x0B)
-       return "Access Barred";
-    if (code == 0x13)
-       return "Local Procedure Error";
-    if (code == 0x05)
-       return "Network Congestion";
-    if (code == 0x0D)
-       return "Not Obtainable";
-    if (code == 0x15)
-       return "RPOA Out Of Order";
 
     sprintf(buffer, "Unknown %02X", code);
 
@@ -547,6 +545,87 @@ static char *clear_diag(unsigned char code)
        return "Unknown called DNIC";
     if (code == 122)
        return "Maintenance action";
+    if (code == 144)
+       return "Timer expired or retransmission count surpassed";
+    if (code == 145)
+       return "Timer expired or retransmission count surpassed for INTERRUPT";
+    if (code == 146)
+       return "Timer expired or retransmission count surpassed for DATA "
+              "packet transmission";
+    if (code == 147)
+       return "Timer expired or retransmission count surpassed for REJECT";
+    if (code == 160)
+       return "DTE-specific signals";
+    if (code == 161)
+       return "DTE operational";
+    if (code == 162)
+       return "DTE not operational";
+    if (code == 163)
+       return "DTE resource constraint";
+    if (code == 164)
+       return "Fast select not subscribed";
+    if (code == 165)
+       return "Invalid partially full DATA packet";
+    if (code == 166)
+       return "D-bit procedure not supported";
+    if (code == 167)
+       return "Registration/Cancellation confirmed";
+    if (code == 224)
+       return "OSI network service problem";
+    if (code == 225)
+       return "Disconnection (transient condition)";
+    if (code == 226)
+       return "Disconnection (permanent condition)";
+    if (code == 227)
+       return "Connection rejection - reason unspecified (transient "
+              "condition)";
+    if (code == 228)
+       return "Connection rejection - reason unspecified (permanent "
+              "condition)";
+    if (code == 229)
+       return "Connection rejection - quality of service not available "
+               "transient condition)";
+    if (code == 230)
+       return "Connection rejection - quality of service not available "
+               "permanent condition)";
+    if (code == 231)
+       return "Connection rejection - NSAP unreachable (transient condition)";
+    if (code == 232)
+       return "Connection rejection - NSAP unreachable (permanent condition)";
+    if (code == 233)
+       return "reset - reason unspecified";
+    if (code == 234)
+       return "reset - congestion";
+    if (code == 235)
+       return "Connection rejection - NSAP address unknown (permanent "
+               "condition)";
+    if (code == 240)
+       return "Higher layer initiated";
+    if (code == 241)
+       return "Disconnection - normal";
+    if (code == 242)
+       return "Disconnection - abnormal";
+    if (code == 243)
+       return "Disconnection - incompatible information in user data";
+    if (code == 244)
+       return "Connection rejection - reason unspecified (transient "
+               "condition)";
+    if (code == 245)
+       return "Connection rejection - reason unspecified (permanent "
+               "condition)";
+    if (code == 246)
+       return "Connection rejection - quality of service not available "
+               "(transient condition)";
+    if (code == 247)
+       return "Connection rejection - quality of service not available "
+               "(permanent condition)";
+    if (code == 248)
+       return "Connection rejection - incompatible information in user data";
+    if (code == 249)
+       return "Connection rejection - unrecognizable protocol indentifier "
+               "in user data";
+    if (code == 250)
+       return "Reset - user resynchronization";
 
     sprintf(buffer, "Unknown %d", code);
 
@@ -619,38 +698,39 @@ static char *registration_code(unsigned char code)
     return buffer;
 }
 
-void
-dump_facilities(proto_tree *tree, int *offset, const guint8 *p)
+static void
+dump_facilities(proto_tree *tree, int *offset, tvbuff_t *tvb)
 {
-    const guint8 *ptr = p;
+    guint8 fac, byte1, byte2, byte3;
     guint32 len;      /* facilities length */
     proto_item *ti=0;
     proto_tree *fac_tree = 0;
     proto_tree *fac_subtree;
 
-    len = *ptr++;
+    len = tvb_get_guint8(tvb, *offset);
     if (len && tree) {
-       ti = proto_tree_add_text(tree, *offset, len + 1,
+       ti = proto_tree_add_text(tree, tvb, *offset, len + 1,
                                 "Facilities");
        fac_tree = proto_item_add_subtree(ti, ett_x25_fac);
-       proto_tree_add_text(fac_tree, *offset, 1,
+       proto_tree_add_text(fac_tree, tvb, *offset, 1,
                            "Facilities length: %d", len);
     }
     (*offset)++;
 
     while (len > 0) {
-       switch(*ptr & X25_FAC_CLASS_MASK) {
+       fac = tvb_get_guint8(tvb, *offset);
+       switch(fac & X25_FAC_CLASS_MASK) {
        case X25_FAC_CLASS_A:
-           switch (*ptr) {
+           switch (fac) {
            case X25_FAC_COMP_MARK:
                if (fac_tree)
-                   ti = proto_tree_add_text(fac_tree, *offset, 1,
+                   ti = proto_tree_add_text(fac_tree, tvb, *offset, 1,
                            "Code : 00 (Marker)");
-               switch (ptr[1]) {
+               switch (tvb_get_guint8(tvb, *offset + 1)) {
                case 0x00:
                    if (fac_tree) {
                        fac_subtree = proto_item_add_subtree(ti, ett_x25_fac_mark);
-                       proto_tree_add_text(fac_subtree, *offset+1, 1,
+                       proto_tree_add_text(fac_subtree, tvb, *offset+1, 1,
                                            "Parameter : 00 (Network complementary "
                                            "services - calling DTE)");
                    }
@@ -658,7 +738,7 @@ dump_facilities(proto_tree *tree, int *offset, const guint8 *p)
                case 0xFF:
                    if (fac_tree) {
                        fac_subtree = proto_item_add_subtree(ti, ett_x25_fac_mark);
-                       proto_tree_add_text(fac_subtree, *offset+1, 1,
+                       proto_tree_add_text(fac_subtree, tvb, *offset+1, 1,
                                            "Parameter : FF (Network complementary "
                                            "services - called DTE)");
                    }
@@ -666,7 +746,7 @@ dump_facilities(proto_tree *tree, int *offset, const guint8 *p)
                case 0x0F:
                    if (fac_tree) {
                        fac_subtree = proto_item_add_subtree(ti, ett_x25_fac_mark);
-                       proto_tree_add_text(fac_subtree, *offset+1, 1,
+                       proto_tree_add_text(fac_subtree, tvb, *offset+1, 1,
                                            "Parameter : 0F (DTE complementary "
                                            "services)");
                    }
@@ -674,31 +754,32 @@ dump_facilities(proto_tree *tree, int *offset, const guint8 *p)
                default:
                    if (fac_tree) {
                        fac_subtree = proto_item_add_subtree(ti, ett_x25_fac_mark);
-                       proto_tree_add_text(fac_subtree, *offset+1, 1,
+                       proto_tree_add_text(fac_subtree, tvb, *offset+1, 1,
                                            "Parameter : %02X (Unknown marker)",
-                                           ptr[1]);
+                                           tvb_get_guint8(tvb, *offset+1));
                    }
                    break;
                }
                break;
            case X25_FAC_REVERSE:
                if (fac_tree) {
-                   ti = proto_tree_add_text(fac_tree, *offset, 1, "Code : %02X "
-                           "(Reverse charging / Fast select)", *ptr);
+                   ti = proto_tree_add_text(fac_tree, tvb, *offset, 1, "Code : %02X "
+                           "(Reverse charging / Fast select)", fac);
                    fac_subtree = proto_item_add_subtree(ti, ett_x25_fac_reverse);
-                   proto_tree_add_text(fac_subtree, *offset+1, 1, "Parameter : %02X",
-                           ptr[1]);
-                   if (ptr[1] & 0xC0)
-                       proto_tree_add_text(fac_subtree, *offset+1, 1,
+                   byte1 = tvb_get_guint8(tvb, *offset + 1);
+                   proto_tree_add_text(fac_subtree, tvb, *offset+1, 1,
+                           "Parameter : %02X", byte1);
+                   if (byte1 & 0xC0)
+                       proto_tree_add_text(fac_subtree, tvb, *offset+1, 1,
                                "11.. .... = Fast select with restriction");
-                   else if (ptr[1] & 0x80)
-                       proto_tree_add_text(fac_subtree, *offset+1, 1,
+                   else if (byte1 & 0x80)
+                       proto_tree_add_text(fac_subtree, tvb, *offset+1, 1,
                                "10.. .... = Fast select - no restriction");
                    else
-                       proto_tree_add_text(fac_subtree, *offset+1, 1,
+                       proto_tree_add_text(fac_subtree, tvb, *offset+1, 1,
                                "00.. .... = Fast select not requested");
-                   proto_tree_add_text(fac_subtree, *offset+1, 1,
-                           decode_boolean_bitfield(ptr[1], 0x01, 1*8,
+                   proto_tree_add_text(fac_subtree, tvb, *offset+1, 1,
+                           decode_boolean_bitfield(byte1, 0x01, 1*8,
                                "Reverse charging requested",
                                "Reverse charging not requested"));
                }
@@ -707,10 +788,11 @@ dump_facilities(proto_tree *tree, int *offset, const guint8 *p)
                if (fac_tree) {
                    char tmpbuf[80];
 
-                   ti = proto_tree_add_text(fac_tree, *offset, 1, "Code : %02X "
-                           "(Throughput class negociation)", *ptr);
+                   ti = proto_tree_add_text(fac_tree, tvb, *offset, 1, "Code : %02X "
+                           "(Throughput class negociation)", fac);
                    fac_subtree = proto_item_add_subtree(ti, ett_x25_fac_throughput);
-                   switch (ptr[1] >> 4)
+                   byte1 = tvb_get_guint8(tvb, *offset + 1);
+                   switch (byte1 >> 4)
                    {
                    case 3:
                    case 4:
@@ -722,7 +804,7 @@ dump_facilities(proto_tree *tree, int *offset, const guint8 *p)
                    case 10:
                    case 11:
                        sprintf(tmpbuf, "From the called DTE : %%u (%d bps)",
-                               75*(1<<((ptr[1] >> 4)-3)));
+                               75*(1<<((byte1 >> 4)-3)));
                        break;
                    case 12:
                        sprintf(tmpbuf, "From the called DTE : %%u (48000 bps)");
@@ -733,9 +815,9 @@ dump_facilities(proto_tree *tree, int *offset, const guint8 *p)
                    default:
                        sprintf(tmpbuf, "From the called DTE : %%u (Reserved)");
                    }
-                   proto_tree_add_text(fac_subtree, *offset+1, 1,
-                           decode_numeric_bitfield(ptr[1], 0xF0, 1*8, tmpbuf));
-                   switch (ptr[1] & 0x0F)
+                   proto_tree_add_text(fac_subtree, tvb, *offset+1, 1,
+                           decode_numeric_bitfield(byte1, 0xF0, 1*8, tmpbuf));
+                   switch (byte1 & 0x0F)
                    {
                    case 3:
                    case 4:
@@ -747,7 +829,7 @@ dump_facilities(proto_tree *tree, int *offset, const guint8 *p)
                    case 10:
                    case 11:
                        sprintf(tmpbuf, "From the calling DTE : %%u (%d bps)",
-                               75*(1<<((ptr[1] & 0x0F)-3)));
+                               75*(1<<((byte1 & 0x0F)-3)));
                        break;
                    case 12:
                        sprintf(tmpbuf, "From the calling DTE : %%u (48000 bps)");
@@ -758,85 +840,84 @@ dump_facilities(proto_tree *tree, int *offset, const guint8 *p)
                    default:
                        sprintf(tmpbuf, "From the calling DTE : %%u (Reserved)");
                    }
-                   proto_tree_add_text(fac_subtree, *offset+1, 1,
-                           decode_numeric_bitfield(ptr[1], 0x0F, 1*8, tmpbuf));
+                   proto_tree_add_text(fac_subtree, tvb, *offset+1, 1,
+                           decode_numeric_bitfield(byte1, 0x0F, 1*8, tmpbuf));
                }
                break;
            case X25_FAC_CUG:
                if (fac_tree) {
-                   ti = proto_tree_add_text(fac_tree, *offset, 1, "Code : %02X "
-                           "(Closed user group selection)", *ptr);
+                   ti = proto_tree_add_text(fac_tree, tvb, *offset, 1, "Code : %02X "
+                           "(Closed user group selection)", fac);
                    fac_subtree = proto_item_add_subtree(ti, ett_x25_fac_cug);
-                   proto_tree_add_text(fac_subtree, *offset+1, 1,
-                           "Closed user group: %02X", ptr[1]);
+                   proto_tree_add_text(fac_subtree, tvb, *offset+1, 1,
+                           "Closed user group: %02X", tvb_get_guint8(tvb, *offset+1));
                }
                break;
            case X25_FAC_CALLED_MODIF:
                if (fac_tree) {
-                   ti = proto_tree_add_text(fac_tree, *offset, 1, "Code : %02X "
-                           "(Called address modified)", *ptr);
+                   ti = proto_tree_add_text(fac_tree, tvb, *offset, 1, "Code : %02X "
+                           "(Called address modified)", fac);
                    fac_subtree = proto_item_add_subtree(ti,
                            ett_x25_fac_called_modif);
-                   proto_tree_add_text(fac_tree, *offset, 2,
-                           "Parameter %02X", ptr[1]);
+                   proto_tree_add_text(fac_tree, tvb, *offset+1, 1,
+                           "Parameter %02X", tvb_get_guint8(tvb, *offset+1));
                }
                break;
            case X25_FAC_CUG_OUTGOING_ACC:
                if (fac_tree) {
-                   ti = proto_tree_add_text(fac_tree, *offset, 1, "Code : %02X "
+                   ti = proto_tree_add_text(fac_tree, tvb, *offset, 1, "Code : %02X "
                            "(Closed user group with outgoing access selection)",
-                           *ptr);
+                           fac);
                    fac_subtree = proto_item_add_subtree(ti,
                            ett_x25_fac_cug_outgoing_acc);
-                   proto_tree_add_text(fac_subtree, *offset+1, 1,
-                           "Closed user group: %02X", ptr[1]);
+                   proto_tree_add_text(fac_subtree, tvb, *offset+1, 1,
+                           "Closed user group: %02X", tvb_get_guint8(tvb, *offset+1));
                }
                break;
            case X25_FAC_THROUGHPUT_MIN:
                if (fac_tree) {
-                   ti = proto_tree_add_text(fac_tree, *offset, 1, "Code : %02X "
-                           "(Minimum throughput class)", *ptr);
+                   ti = proto_tree_add_text(fac_tree, tvb, *offset, 1, "Code : %02X "
+                           "(Minimum throughput class)", fac);
                    fac_subtree = proto_item_add_subtree(ti,
                            ett_x25_fac_throughput_min);
-                   proto_tree_add_text(fac_subtree, *offset+1, 1,
-                           "Parameter %02X", ptr[1]);
+                   proto_tree_add_text(fac_subtree, tvb, *offset+1, 1,
+                           "Parameter %02X", tvb_get_guint8(tvb, *offset+1));
                }
                break;
            case X25_FAC_EXPRESS_DATA:
                if (fac_tree) {
-                   ti = proto_tree_add_text(fac_tree, *offset, 1, "Code : %02X "
-                           "(Negociation of express data)", *ptr);
+                   ti = proto_tree_add_text(fac_tree, tvb, *offset, 1, "Code : %02X "
+                           "(Negociation of express data)", fac);
                    fac_subtree = proto_item_add_subtree(ti,
                            ett_x25_fac_express_data);
-                   proto_tree_add_text(fac_subtree, *offset+1, 1,
-                           "Parameter %02X", ptr[1]);
+                   proto_tree_add_text(fac_subtree, tvb, *offset+1, 1,
+                           "Parameter %02X", tvb_get_guint8(tvb, *offset+1));
                }
                break;
            default:
                if (fac_tree) {
-                   ti = proto_tree_add_text(fac_tree, *offset, 1,
-                           "Code : %02X (Unknown class A)", *ptr);
+                   ti = proto_tree_add_text(fac_tree, tvb, *offset, 1,
+                           "Code : %02X (Unknown class A)", fac);
                    fac_subtree = proto_item_add_subtree(ti, ett_x25_fac_unknown);
-                   proto_tree_add_text(fac_subtree, *offset+1, 1,
-                           "Parameter %02X", ptr[1]);
+                   proto_tree_add_text(fac_subtree, tvb, *offset+1, 1,
+                           "Parameter %02X", tvb_get_guint8(tvb, *offset+1));
                }
                break;
            }
            (*offset) += 2;
            len -= 2;
-           ptr += 2;
            break;
        case X25_FAC_CLASS_B:
-           switch (*ptr) {
+           switch (fac) {
            case X25_FAC_BILATERAL_CUG:
                if (fac_tree) {
-                   ti = proto_tree_add_text(fac_tree, *offset, 1, "Code : %02X "
-                           "(Bilateral closed user group selection)", *ptr);
+                   ti = proto_tree_add_text(fac_tree, tvb, *offset, 1, "Code : %02X "
+                           "(Bilateral closed user group selection)", fac);
                    fac_subtree = proto_item_add_subtree(ti,
                            ett_x25_fac_bilateral_cug);
-                   proto_tree_add_text(fac_subtree, *offset+1, 2,
-                                       "Bilateral CUG: %02X%02X",
-                                       ptr[1], ptr[2]);
+                   proto_tree_add_text(fac_subtree, tvb, *offset+1, 2,
+                                       "Bilateral CUG: %04X",
+                                       tvb_get_ntohs(tvb, *offset+1));
                }
                break;
            case X25_FAC_PACKET_SIZE:
@@ -844,10 +925,11 @@ dump_facilities(proto_tree *tree, int *offset, const guint8 *p)
                {
                    char tmpbuf[80];
 
-                   ti = proto_tree_add_text(fac_tree, *offset, 1, "Code : %02X "
-                           "(Packet size)", *ptr);
+                   ti = proto_tree_add_text(fac_tree, tvb, *offset, 1, "Code : %02X "
+                           "(Packet size)", fac);
                    fac_subtree = proto_item_add_subtree(ti, ett_x25_fac_packet_size);
-                   switch (ptr[1])
+                   byte1 = tvb_get_guint8(tvb, *offset + 1);
+                   switch (byte1)
                    {
                    case 0x04:
                        sprintf(tmpbuf, "From the called DTE : %%u (16)");
@@ -880,10 +962,11 @@ dump_facilities(proto_tree *tree, int *offset, const guint8 *p)
                        sprintf(tmpbuf, "From the called DTE : %%u (Unknown)");
                        break;
                    }
-                   proto_tree_add_text(fac_subtree, *offset+1, 1,
-                           decode_numeric_bitfield(ptr[1], 0x0F, 1*8, tmpbuf));
+                   proto_tree_add_text(fac_subtree, tvb, *offset+1, 1,
+                           decode_numeric_bitfield(byte1, 0x0F, 1*8, tmpbuf));
 
-                   switch (ptr[2])
+                   byte2 = tvb_get_guint8(tvb, *offset + 1);
+                   switch (byte2)
                    {
                    case 0x04:
                        sprintf(tmpbuf, "From the calling DTE : %%u (16)");
@@ -916,128 +999,132 @@ dump_facilities(proto_tree *tree, int *offset, const guint8 *p)
                        sprintf(tmpbuf, "From the calling DTE : %%u (Unknown)");
                        break;
                    }
-                   proto_tree_add_text(fac_subtree, *offset+2, 1,
-                           decode_numeric_bitfield(ptr[2], 0x0F, 1*8, tmpbuf));
+                   proto_tree_add_text(fac_subtree, tvb, *offset+2, 1,
+                           decode_numeric_bitfield(byte2, 0x0F, 1*8, tmpbuf));
                }
                break;
            case X25_FAC_WINDOW_SIZE:
                if (fac_tree) {
-                   ti = proto_tree_add_text(fac_tree, *offset, 1, "Code : %02X "
-                           "(Window size)", *ptr);
+                   ti = proto_tree_add_text(fac_tree, tvb, *offset, 1, "Code : %02X "
+                           "(Window size)", fac);
                    fac_subtree = proto_item_add_subtree(ti, ett_x25_fac_window_size);
-                   proto_tree_add_text(fac_subtree, *offset+1, 1,
-                           decode_numeric_bitfield(ptr[1], 0x7F, 1*8,
-                           "From the called DTE: %u"));
-                   proto_tree_add_text(fac_subtree, *offset+2, 1,
-                           decode_numeric_bitfield(ptr[2], 0x7F, 1*8,
-                           "From the calling DTE: %u"));
+                   proto_tree_add_text(fac_subtree, tvb, *offset+1, 1,
+                           decode_numeric_bitfield(tvb_get_guint8(tvb, *offset+1),
+                               0x7F, 1*8, "From the called DTE: %u"));
+                   proto_tree_add_text(fac_subtree, tvb, *offset+2, 1,
+                           decode_numeric_bitfield(tvb_get_guint8(tvb, *offset+2),
+                               0x7F, 1*8, "From the calling DTE: %u"));
                }
                break;
            case X25_FAC_RPOA_SELECTION:
                if (fac_tree) {
-                   ti = proto_tree_add_text(fac_tree, *offset, 1, "Code : %02X "
-                           "(RPOA selection)", *ptr);
+                   ti = proto_tree_add_text(fac_tree, tvb, *offset, 1, "Code : %02X "
+                           "(RPOA selection)", fac);
                    fac_subtree = proto_item_add_subtree(ti,
                            ett_x25_fac_rpoa_selection);
-                   proto_tree_add_text(fac_subtree, *offset+1, 2,
-                                       "Data network identification code : %02X%02X",
-                                       ptr[1], ptr[2]);
+                   proto_tree_add_text(fac_subtree, tvb, *offset+1, 2,
+                                       "Data network identification code : %04X",
+                                       tvb_get_ntohs(tvb, *offset+1));
                }
                break;
            case X25_FAC_TRANSIT_DELAY:
                if (fac_tree) {
-                   ti = proto_tree_add_text(fac_tree, *offset, 1, "Code : %02X "
-                           "(Transit delay selection and indication)", *ptr);
+                   ti = proto_tree_add_text(fac_tree, tvb, *offset, 1, "Code : %02X "
+                           "(Transit delay selection and indication)", fac);
                    fac_subtree = proto_item_add_subtree(ti,
                            ett_x25_fac_transit_delay);
-                   proto_tree_add_text(fac_subtree, *offset+1, 2,
+                   proto_tree_add_text(fac_subtree, tvb, *offset+1, 2,
                                        "Transit delay: %d ms",
-                                       (ptr[1]<<8) + ptr[2]);
+                                       tvb_get_ntohs(tvb, *offset+1));
                }
                break;
            default:
                if (fac_tree) {
-                   ti = proto_tree_add_text(fac_tree, *offset, 1,
-                           "Code : %02X (Unknown class B)", *ptr);
+                   ti = proto_tree_add_text(fac_tree, tvb, *offset, 1,
+                           "Code : %02X (Unknown class B)", fac);
                    fac_subtree = proto_item_add_subtree(ti, ett_x25_fac_unknown);
-                   proto_tree_add_text(fac_subtree, *offset+1, 2,
-                           "Parameter %02X%02X", ptr[1], ptr[2]);
+                   proto_tree_add_text(fac_subtree, tvb, *offset+1, 2,
+                           "Parameter %04X", tvb_get_ntohs(tvb, *offset+1));
                }
                break;
            }
            (*offset) += 3;
            len -= 3;
-           ptr += 3;
            break;
        case X25_FAC_CLASS_C:
            if (fac_tree) {
-               ti = proto_tree_add_text(fac_tree, *offset, 1,
-                       "Code : %02X (Unknown class C)", *ptr);
+               ti = proto_tree_add_text(fac_tree, tvb, *offset, 1,
+                       "Code : %02X (Unknown class C)", fac);
                fac_subtree = proto_item_add_subtree(ti, ett_x25_fac_unknown);
-               proto_tree_add_text(fac_subtree, *offset+1, 3,
-                       "Parameter %02X%02X%02X",
-                       ptr[1], ptr[2], ptr[3]);
+               proto_tree_add_text(fac_subtree, tvb, *offset+1, 3,
+                       "Parameter %06X",
+                       tvb_get_ntoh24(tvb, *offset+1));
            }
            (*offset) += 4;
            len -= 4;
-           ptr += 4;
            break;
        case X25_FAC_CLASS_D:
-           switch (*ptr) {
+           switch (fac) {
            case X25_FAC_CALL_TRANSFER:
                if (fac_tree) {
                    int i;
                    char tmpbuf[256];
 
-                   ti = proto_tree_add_text(fac_tree, *offset, 1, "Code : %02X "
-                           "(Call redirection or deflection notification)", *ptr);
+                   ti = proto_tree_add_text(fac_tree, tvb, *offset, 1, "Code : %02X "
+                           "(Call redirection or deflection notification)", fac);
                    fac_subtree = proto_item_add_subtree(ti, ett_x25_fac_call_transfer);
-                   proto_tree_add_text(fac_subtree, *offset+1, 1,
-                           "Length : %u", ptr[1]);
-                   if ((ptr[2] & 0xC0) == 0xC0) {
-                       proto_tree_add_text(fac_subtree, *offset+2, 1,
+                   byte1 = tvb_get_guint8(tvb, *offset+1);
+                   proto_tree_add_text(fac_subtree, tvb, *offset+1, 1,
+                           "Length : %u", byte1);
+                   byte2 = tvb_get_guint8(tvb, *offset+2);
+                   if ((byte2 & 0xC0) == 0xC0) {
+                       proto_tree_add_text(fac_subtree, tvb, *offset+2, 1,
                                "Reason : call deflection by the originally "
                                "called DTE address");
                    }
                    else {
-                       switch (ptr[2]) {
+                       switch (byte2) {
                        case 0x01:
-                           proto_tree_add_text(fac_subtree, *offset+2, 1,
+                           proto_tree_add_text(fac_subtree, tvb, *offset+2, 1,
                                    "Reason : originally called DTE busy");
                            break;
                        case 0x07:
-                           proto_tree_add_text(fac_subtree, *offset+2, 1,
+                           proto_tree_add_text(fac_subtree, tvb, *offset+2, 1,
                                    "Reason : call dist. within a hunt group");
                            break;
                        case 0x09:
-                           proto_tree_add_text(fac_subtree, *offset+2, 1,
+                           proto_tree_add_text(fac_subtree, tvb, *offset+2, 1,
                                    "Reason : originally called DTE out of order");
                            break;
                        case 0x0F:
-                           proto_tree_add_text(fac_subtree, *offset+2, 1,
+                           proto_tree_add_text(fac_subtree, tvb, *offset+2, 1,
                                    "Reason : systematic call redirection");
                            break;
                        default:
-                           proto_tree_add_text(fac_subtree, *offset+2, 1,
+                           proto_tree_add_text(fac_subtree, tvb, *offset+2, 1,
                                    "Reason : unknown");
                            break;
                        }
                    }
-                   proto_tree_add_text(fac_subtree, *offset+3, 1,
-                           "Number of semi-octets in DTE address : %u", ptr[3]);
-                   for (i = 0; i < ptr[3]; i++) {
+                   byte3 = tvb_get_guint8(tvb, *offset+3);
+                   proto_tree_add_text(fac_subtree, tvb, *offset+3, 1,
+                           "Number of semi-octets in DTE address : %u",
+                           byte3);
+                   for (i = 0; i < byte3; i++) {
                        if (i % 2 == 0) {
-                           tmpbuf[i] = ((ptr[4+i/2] >> 4) & 0x0F) + '0';
+                           tmpbuf[i] = ((tvb_get_guint8(tvb, *offset+4+i/2) >> 4)
+                                   & 0x0F) + '0';
                            /* if > 9, convert to the right hexadecimal letter */
                            if (tmpbuf[i] > '9') tmpbuf[i] += ('A' - '0' - 10);
                        } else {
-                           tmpbuf[i] = (ptr[4+i/2] & 0x0F) + '0';
+                           tmpbuf[i] = (tvb_get_guint8(tvb, *offset+4+i/2)
+                                   & 0x0F) + '0';
                            /* if > 9, convert to the right hexadecimal letter */
                            if (tmpbuf[i] > '9') tmpbuf[i] += ('A' - '0' - 10);
                        }
                    }
                    tmpbuf[i] = 0;
-                   proto_tree_add_text(fac_subtree, *offset+4, ptr[1] - 2,
+                   proto_tree_add_text(fac_subtree, tvb, *offset+4, byte1 - 2,
                            "DTE address : %s", tmpbuf);
                }
                break;
@@ -1046,27 +1133,31 @@ dump_facilities(proto_tree *tree, int *offset, const guint8 *p)
                    int i;
                    char tmpbuf[256];
 
-                   ti = proto_tree_add_text(fac_tree, *offset, 1, "Code : %02X "
-                           "(Calling address extension)", *ptr);
+                   ti = proto_tree_add_text(fac_tree, tvb, *offset, 1, "Code : %02X "
+                           "(Calling address extension)", fac);
                    fac_subtree = proto_item_add_subtree(ti,
                            ett_x25_fac_calling_addr_ext);
-                   proto_tree_add_text(fac_subtree, *offset+1, 1,
-                           "Length : %u", ptr[1]);
-                   proto_tree_add_text(fac_subtree, *offset+2, 1,
-                           "Number of semi-octets in DTE address : %u", ptr[2]);
-                   for (i = 0; i < ptr[2]; i++) {
+                   byte1 = tvb_get_guint8(tvb, *offset+1);
+                   proto_tree_add_text(fac_subtree, tvb, *offset+1, 1,
+                           "Length : %u", byte1);
+                   byte2 = tvb_get_guint8(tvb, *offset+2);
+                   proto_tree_add_text(fac_subtree, tvb, *offset+2, 1,
+                           "Number of semi-octets in DTE address : %u", byte2);
+                   for (i = 0; i < byte2; i++) {
                        if (i % 2 == 0) {
-                           tmpbuf[i] = ((ptr[3+i/2] >> 4) & 0x0F) + '0';
+                           tmpbuf[i] = ((tvb_get_guint8(tvb, *offset+3+i/2) >> 4)
+                                   & 0x0F) + '0';
                            /* if > 9, convert to the right hexadecimal letter */
                            if (tmpbuf[i] > '9') tmpbuf[i] += ('A' - '0' - 10);
                        } else {
-                           tmpbuf[i] = (ptr[3+i/2] & 0x0F) + '0';
+                           tmpbuf[i] = (tvb_get_guint8(tvb, *offset+3+i/2)
+                                   & 0x0F) + '0';
                            /* if > 9, convert to the right hexadecimal letter */
                            if (tmpbuf[i] > '9') tmpbuf[i] += ('A' - '0' - 10);
                        }
                    }
                    tmpbuf[i] = 0;
-                   proto_tree_add_text(fac_subtree, *offset+3, ptr[1] - 1,
+                   proto_tree_add_text(fac_subtree, tvb, *offset+3, byte1 - 1,
                            "DTE address : %s", tmpbuf);
                }
                break;
@@ -1075,39 +1166,44 @@ dump_facilities(proto_tree *tree, int *offset, const guint8 *p)
                    int i;
                    char tmpbuf[256];
 
-                   ti = proto_tree_add_text(fac_tree, *offset, 1, "Code : %02X "
-                           "(Called address extension)", *ptr);
+                   ti = proto_tree_add_text(fac_tree, tvb, *offset, 1, "Code : %02X "
+                           "(Called address extension)", fac);
                    fac_subtree = proto_item_add_subtree(ti,
                            ett_x25_fac_called_addr_ext);
-                   proto_tree_add_text(fac_subtree, *offset+1, 1,
-                           "Length : %u", ptr[1]);
-                   proto_tree_add_text(fac_subtree, *offset+2, 1,
-                           "Number of semi-octets in DTE address : %u", ptr[2]);
-                   for (i = 0; i < ptr[2]; i++) {
+                   byte1 = tvb_get_guint8(tvb, *offset+1);
+                   proto_tree_add_text(fac_subtree, tvb, *offset+1, 1,
+                           "Length : %u", byte1);
+                   byte2 = tvb_get_guint8(tvb, *offset+2);
+                   proto_tree_add_text(fac_subtree, tvb, *offset+2, 1,
+                           "Number of semi-octets in DTE address : %u", byte2);
+                   for (i = 0; i < byte2; i++) {
                        if (i % 2 == 0) {
-                           tmpbuf[i] = ((ptr[3+i/2] >> 4) & 0x0F) + '0';
+                           tmpbuf[i] = ((tvb_get_guint8(tvb, *offset+3+i/2) >> 4)
+                                   & 0x0F) + '0';
                            /* if > 9, convert to the right hexadecimal letter */
                            if (tmpbuf[i] > '9') tmpbuf[i] += ('A' - '0' - 10);
                        } else {
-                           tmpbuf[i] = (ptr[3+i/2] & 0x0F) + '0';
+                           tmpbuf[i] = (tvb_get_guint8(tvb, *offset+3+i/2)
+                                   & 0x0F) + '0';
                            /* if > 9, convert to the right hexadecimal letter */
                            if (tmpbuf[i] > '9') tmpbuf[i] += ('A' - '0' - 10);
                        }
                    }
                    tmpbuf[i] = 0;
-                   proto_tree_add_text(fac_subtree, *offset+3, ptr[1] - 1,
+                   proto_tree_add_text(fac_subtree, tvb, *offset+3, byte1 - 1,
                            "DTE address : %s", tmpbuf);
                }
                break;
            case X25_FAC_ETE_TRANSIT_DELAY:
                if (fac_tree) {
-                   ti = proto_tree_add_text(fac_tree, *offset, 1, "Code : %02X "
-                           "(End to end transit delay)", *ptr);
+                   ti = proto_tree_add_text(fac_tree, tvb, *offset, 1, "Code : %02X "
+                           "(End to end transit delay)", fac);
                    fac_subtree = proto_item_add_subtree(ti,
                            ett_x25_fac_ete_transit_delay);
-                   proto_tree_add_text(fac_subtree, *offset+1, 1,
-                           "Length : %u", ptr[1]);
-                   proto_tree_add_text(fac_subtree, *offset+2, ptr[1], "Value");
+                   byte1 = tvb_get_guint8(tvb, *offset+1);
+                   proto_tree_add_text(fac_subtree, tvb, *offset+1, 1,
+                           "Length : %u", byte1);
+                   proto_tree_add_text(fac_subtree, tvb, *offset+2, byte1, "Value");
                }
                break;
            case X25_FAC_CALL_DEFLECT:
@@ -1115,106 +1211,119 @@ dump_facilities(proto_tree *tree, int *offset, const guint8 *p)
                    int i;
                    char tmpbuf[256];
 
-                   ti = proto_tree_add_text(fac_tree, *offset, 1, "Code : %02X "
-                           "(Call deflection selection)", *ptr);
+                   ti = proto_tree_add_text(fac_tree, tvb, *offset, 1, "Code : %02X "
+                           "(Call deflection selection)", fac);
                    fac_subtree = proto_item_add_subtree(ti,
                            ett_x25_fac_call_deflect);
-                   proto_tree_add_text(fac_subtree, *offset+1, 1,
-                           "Length : %u", ptr[1]);
-                   if ((ptr[2] & 0xC0) == 0xC0)
-                       proto_tree_add_text(fac_subtree, *offset+2, 1,
+                   byte1 = tvb_get_guint8(tvb, *offset+1);
+                   proto_tree_add_text(fac_subtree, tvb, *offset+1, 1,
+                           "Length : %u", byte1);
+                   byte2 = tvb_get_guint8(tvb, *offset+2);
+                   if ((byte2 & 0xC0) == 0xC0)
+                       proto_tree_add_text(fac_subtree, tvb, *offset+2, 1,
                                "Reason : call DTE originated");
                    else
-                       proto_tree_add_text(fac_subtree, *offset+2, 1,
+                       proto_tree_add_text(fac_subtree, tvb, *offset+2, 1,
                                "Reason : unknown");
-                   proto_tree_add_text(fac_subtree, *offset+3, 1,
+                   byte3 = tvb_get_guint8(tvb, *offset+3);
+                   proto_tree_add_text(fac_subtree, tvb, *offset+3, 1,
                            "Number of semi-octets in the alternative DTE address : %u",
-                           ptr[3]);
-                   for (i = 0; i < ptr[3]; i++) {
+                           byte3);
+                   for (i = 0; i < byte3; i++) {
                        if (i % 2 == 0) {
-                           tmpbuf[i] = ((ptr[4+i/2] >> 4) & 0x0F) + '0';
+                           tmpbuf[i] = ((tvb_get_guint8(tvb, *offset+4+i/2) >> 4)
+                                   & 0x0F) + '0';
                            /* if > 9, convert to the right hexadecimal letter */
                            if (tmpbuf[i] > '9') tmpbuf[i] += ('A' - '0' - 10);
                        } else {
-                           tmpbuf[i] = (ptr[4+i/2] & 0x0F) + '0';
+                           tmpbuf[i] = (tvb_get_guint8(tvb, *offset+4+i/2)
+                                   & 0x0F) + '0';
                            /* if > 9, convert to the right hexadecimal letter */
                            if (tmpbuf[i] > '9') tmpbuf[i] += ('A' - '0' - 10);
                        }
                    }
                    tmpbuf[i] = 0;
-                   proto_tree_add_text(fac_subtree, *offset+4, ptr[1] - 2,
+                   proto_tree_add_text(fac_subtree, tvb, *offset+4, byte1 - 2,
                            "Alternative DTE address : %s", tmpbuf);
                }
                break;
            case X25_FAC_PRIORITY:
                if (fac_tree) {
-                   ti = proto_tree_add_text(fac_tree, *offset, 1,
-                           "Code : %02X (Priority)", *ptr);
+                   ti = proto_tree_add_text(fac_tree, tvb, *offset, 1,
+                           "Code : %02X (Priority)", fac);
                    fac_subtree = proto_item_add_subtree(ti, ett_x25_fac_priority);
-                   proto_tree_add_text(fac_subtree, *offset+1, 1,
-                           "Length : %u", ptr[1]);
-                   proto_tree_add_text(fac_subtree, *offset+2, ptr[1], "Value");
+                   byte1 = tvb_get_guint8(tvb, *offset+1);
+                   proto_tree_add_text(fac_subtree, tvb, *offset+1, 1,
+                           "Length : %u", byte1);
+                   proto_tree_add_text(fac_subtree, tvb, *offset+2, byte1, "Value");
                }
                break;
            default:
                if (fac_tree) {
-                   ti = proto_tree_add_text(fac_tree, *offset, 1,
-                           "Code : %02X (Unknown class D)", *ptr);
+                   ti = proto_tree_add_text(fac_tree, tvb, *offset, 1,
+                           "Code : %02X (Unknown class D)", fac);
                    fac_subtree = proto_item_add_subtree(ti, ett_x25_fac_unknown);
-                   proto_tree_add_text(fac_subtree, *offset+1, 1,
-                           "Length : %u", ptr[1]);
-                   proto_tree_add_text(fac_subtree, *offset+2, ptr[1], "Value");
+                   byte1 = tvb_get_guint8(tvb, *offset+1);
+                   proto_tree_add_text(fac_subtree, tvb, *offset+1, 1,
+                           "Length : %u", byte1);
+                   proto_tree_add_text(fac_subtree, tvb, *offset+2, byte1, "Value");
                }
            }
-           (*offset) += ptr[1]+2;
-           len -= ptr[1]+2;
-           ptr += ptr[1]+2;
+           byte1 = tvb_get_guint8(tvb, *offset+1);
+           (*offset) += byte1+2;
+           len -= byte1+2;
            break;
        }
     }
 }
 
-void
-x25_ntoa(proto_tree *tree, int *offset, const guint8 *p,
+static void
+x25_ntoa(proto_tree *tree, int *offset, tvbuff_t *tvb,
         frame_data *fd, gboolean toa)
 {
     int len1, len2;
     int i;
     char addr1[16], addr2[16];
     char *first, *second;
+    guint8 byte;
+    int localoffset;
 
-    len1  = (*p >> 4) & 0x0F;
-    len2 = (*p >> 0) & 0x0F;
+    byte = tvb_get_guint8(tvb, *offset);
+    len1 = (byte >> 4) & 0x0F;
+    len2 = (byte >> 0) & 0x0F;
     if (tree) {
-       proto_tree_add_text(tree, *offset, 1,
-               decode_numeric_bitfield(*p, 0xF0, 1*8,
+       proto_tree_add_text(tree, tvb, *offset, 1,
+               decode_numeric_bitfield(byte, 0xF0, 1*8,
                    toa ? "Called address length : %u" :
                          "Calling address length : %u"));
-       proto_tree_add_text(tree, *offset, 1,
-               decode_numeric_bitfield(*p, 0x0F, 1*8,
+       proto_tree_add_text(tree, tvb, *offset, 1,
+               decode_numeric_bitfield(byte, 0x0F, 1*8,
                    toa ? "Calling address length : %u" :
                          "Called address length : %u"));
     }
     (*offset)++;
 
-    p++;
+    localoffset = *offset;
+    byte = tvb_get_guint8(tvb, localoffset);
 
     first=addr1;
     second=addr2;
     for (i = 0; i < (len1 + len2); i++) {
        if (i < len1) {
            if (i % 2 != 0) {
-               *first++ = ((*p >> 0) & 0x0F) + '0';
-               p++;
+               *first++ = ((byte >> 0) & 0x0F) + '0';
+               localoffset++;
+               byte = tvb_get_guint8(tvb, localoffset);
            } else {
-               *first++ = ((*p >> 4) & 0x0F) + '0';
+               *first++ = ((byte >> 4) & 0x0F) + '0';
            }
        } else {
            if (i % 2 != 0) {
-               *second++ = ((*p >> 0) & 0x0F) + '0';
-               p++;
+               *second++ = ((byte >> 0) & 0x0F) + '0';
+               localoffset++;
+               byte = tvb_get_guint8(tvb, localoffset);
            } else {
-               *second++ = ((*p >> 4) & 0x0F) + '0';
+               *second++ = ((byte >> 4) & 0x0F) + '0';
            }
        }
     }
@@ -1232,7 +1341,7 @@ x25_ntoa(proto_tree *tree, int *offset, const guint8 *p,
                col_add_str(fd, COL_RES_DL_SRC, addr1);
        }
        if (tree)
-           proto_tree_add_text(tree, *offset,
+           proto_tree_add_text(tree, tvb, *offset,
                                (len1 + 1) / 2,
                                "%s address : %s",
                                toa ? "Called" : "Calling",
@@ -1248,7 +1357,7 @@ x25_ntoa(proto_tree *tree, int *offset, const guint8 *p,
                col_add_str(fd, COL_RES_DL_DST, addr2);
        }
        if (tree)
-           proto_tree_add_text(tree, *offset + len1/2,
+           proto_tree_add_text(tree, tvb, *offset + len1/2,
                                (len2+1)/2+(len1%2+(len2+1)%2)/2,
                                "%s address : %s",
                                toa ? "Calling" : "Called",
@@ -1257,92 +1366,88 @@ x25_ntoa(proto_tree *tree, int *offset, const guint8 *p,
     (*offset) += ((len1 + len2 + 1) / 2);
 }
 
-int
-get_x25_pkt_len(const char *data, frame_data *fd, int offset)
+static int
+get_x25_pkt_len(tvbuff_t *tvb)
 {
-    int length, called_len, calling_len, dte_len, dce_len;
+    guint length, called_len, calling_len, dte_len, dce_len;
+    guint8 byte2, bytex;
 
-    /* packet size should always be > 3 */
-    if (!BYTES_ARE_IN_FRAME(offset, 3)) return END_OF_FRAME;
-
-    switch ((guint8)data[2])
+    byte2 = tvb_get_guint8(tvb, 2);
+    switch (byte2)
     {
     case X25_CALL_REQUEST:
-       if (BYTES_ARE_IN_FRAME(offset, 4)) /* pkt size >= 4 */
-       {
-           called_len  = (data[3] >> 0) & 0x0F;
-           calling_len = (data[3] >> 4) & 0x0F;
-           length = 4 + (called_len + calling_len + 1) / 2; /* addr */
-           if (length+offset < pi.captured_len)
-               length += (1 + data[length]); /* facilities */
-       }
-       else length = END_OF_FRAME;
-       return MIN(END_OF_FRAME,length);
+       bytex = tvb_get_guint8(tvb, 3);
+       called_len  = (bytex >> 0) & 0x0F;
+       calling_len = (bytex >> 4) & 0x0F;
+       length = 4 + (called_len + calling_len + 1) / 2; /* addr */
+       if (length < tvb_reported_length(tvb))
+           length += (1 + tvb_get_guint8(tvb, length)); /* facilities */
+
+       return MIN(tvb_reported_length(tvb),length);
 
     case X25_CALL_ACCEPTED:
-       if (BYTES_ARE_IN_FRAME(offset, 4)) /* pkt size >= 4 */
-       {
-           called_len  = (data[3] >> 0) & 0x0F;
-           calling_len = (data[3] >> 4) & 0x0F;
-           length = 4 + (called_len + calling_len + 1) / 2; /* addr */
-           if (length+offset < pi.captured_len)
-               length += (1 + data[length]); /* facilities */
-       }
-       else length = END_OF_FRAME;
-       return MIN(END_OF_FRAME,length);
+       /* The calling/called address length byte (following the packet type)
+        * is not mandatory, so we must check the packet length before trying
+        * to read it */
+       if (tvb_reported_length(tvb) == 3)
+           return(3);
+       bytex = tvb_get_guint8(tvb, 3);
+       called_len  = (bytex >> 0) & 0x0F;
+       calling_len = (bytex >> 4) & 0x0F;
+       length = 4 + (called_len + calling_len + 1) / 2; /* addr */
+       if (length < tvb_reported_length(tvb))
+           length += (1 + tvb_get_guint8(tvb, length)); /* facilities */
+
+       return MIN(tvb_reported_length(tvb),length);
 
     case X25_CLEAR_REQUEST:
     case X25_RESET_REQUEST:
     case X25_RESTART_REQUEST:
-       return MIN(END_OF_FRAME,5);
+       return MIN(tvb_reported_length(tvb),5);
 
     case X25_DIAGNOSTIC:
-       return MIN(END_OF_FRAME,4);
+       return MIN(tvb_reported_length(tvb),4);
 
     case X25_CLEAR_CONFIRMATION:
     case X25_INTERRUPT:
     case X25_INTERRUPT_CONFIRMATION:
     case X25_RESET_CONFIRMATION:
     case X25_RESTART_CONFIRMATION:
-       return MIN(END_OF_FRAME,3);
+       return MIN(tvb_reported_length(tvb),3);
 
     case X25_REGISTRATION_REQUEST:
-       if (BYTES_ARE_IN_FRAME(offset, 4)) /* pkt size >= 4 */
-       {
-           dce_len  = (data[3] >> 0) & 0x0F;
-           dte_len = (data[3] >> 4) & 0x0F;
-           length = 4 + (dte_len + dce_len + 1) / 2; /* addr */
-           if (length+offset < pi.captured_len)
-               length += (1 + data[length]); /* registration */
-       }
-       else length = END_OF_FRAME;
-       return MIN(END_OF_FRAME,length);
+       bytex = tvb_get_guint8(tvb, 3);
+       dce_len = (bytex >> 0) & 0x0F;
+       dte_len = (bytex >> 4) & 0x0F;
+       length = 4 + (dte_len + dce_len + 1) / 2; /* addr */
+       if (length < tvb_reported_length(tvb))
+           length += (1 + tvb_get_guint8(tvb, length)); /* registration */
+
+       return MIN(tvb_reported_length(tvb),length);
 
     case X25_REGISTRATION_CONFIRMATION:
-       if (BYTES_ARE_IN_FRAME(offset, 6)) /* pkt size >= 6 */
-       {
-           dce_len  = (data[5] >> 0) & 0x0F;
-           dte_len = (data[5] >> 4) & 0x0F;
-           length = 6 + (dte_len + dce_len + 1) / 2; /* addr */
-           if (length+offset < pi.captured_len)
-               length += (1 + data[length]); /* registration */
-       }
-       else length = END_OF_FRAME;
-       return MIN(END_OF_FRAME,length);
+       bytex = tvb_get_guint8(tvb, 5);
+       dce_len = (bytex >> 0) & 0x0F;
+       dte_len = (bytex >> 4) & 0x0F;
+       length = 6 + (dte_len + dce_len + 1) / 2; /* addr */
+       if (length < tvb_reported_length(tvb))
+           length += (1 + tvb_get_guint8(tvb, length)); /* registration */
+
+       return MIN(tvb_reported_length(tvb),length);
     }
-           
-    if ((data[2] & 0x01) == X25_DATA) return MIN(END_OF_FRAME,3);
 
-    switch (data[2] & 0x1F)
+    if ((byte2 & 0x01) == X25_DATA) return MIN(tvb_reported_length(tvb),3);
+
+    switch (byte2 & 0x1F)
     {
     case X25_RR:
-       return MIN(END_OF_FRAME,3);
+       return MIN(tvb_reported_length(tvb),3);
 
     case X25_RNR:
-       return MIN(END_OF_FRAME,3);
+       return MIN(tvb_reported_length(tvb),3);
 
     case X25_REJ:
-       return MIN(END_OF_FRAME,3);
+       return MIN(tvb_reported_length(tvb),3);
     }
 
     return 0;
@@ -1363,97 +1468,115 @@ static const value_string sharing_strategy_vals[] = {
         {0x00,            NULL}
 };
 
-void
-dissect_x25(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
+static void
+dissect_x25(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
 {
-    proto_tree *x25_tree=0, *ti;
-    int localoffset=offset;
-    int x25_pkt_len;
+    proto_tree *x25_tree=0, *gfi_tree=0;
+    proto_item *ti;
+    guint localoffset=0;
+    guint x25_pkt_len;
     int modulo;
     guint16 vc;
-    void (*dissect)(const u_char *, int, frame_data *, proto_tree *);
+    dissector_handle_t dissect;
     gboolean toa;         /* TOA/NPI address format */
+    guint16 bytes0_1;
+    guint8 pkt_type;
+    tvbuff_t *next_tvb;
 
-    if (check_col(fd, COL_PROTOCOL))
-       col_add_str(fd, COL_PROTOCOL, "X.25");
+    if (check_col(pinfo->fd, COL_PROTOCOL))
+       col_set_str(pinfo->fd, COL_PROTOCOL, "X.25");
 
-    modulo = ((pd[localoffset] & 0x20) ? 128 : 8);
+    bytes0_1 = tvb_get_ntohs(tvb, 0);
 
-    if (pd[localoffset] & 0x80) toa = TRUE;
+    modulo = ((bytes0_1 & 0x2000) ? 128 : 8);
+    vc     = (int)(bytes0_1 & 0x0FFF);
+
+    if (bytes0_1 & 0x8000) toa = TRUE;
     else toa = FALSE;
 
-    x25_pkt_len = get_x25_pkt_len(&pd[localoffset], fd, offset);
+    x25_pkt_len = get_x25_pkt_len(tvb);
     if (x25_pkt_len < 3) /* packet too short */
     {
-       if (check_col(fd, COL_INFO))
-           col_add_str(fd, COL_INFO, "Invalid/short X.25 packet");
+       if (check_col(pinfo->fd, COL_INFO))
+           col_set_str(pinfo->fd, COL_INFO, "Invalid/short X.25 packet");
        if (tree)
-           proto_tree_add_protocol_format(tree, (modulo == 8 ? proto_x25 : proto_ex25),
-                           localoffset, END_OF_FRAME,
-                           "Invalid/short X.25 packet");
+           proto_tree_add_protocol_format(tree, proto_x25, tvb, 0,
+                   tvb_length(tvb), "Invalid/short X.25 packet");
        return;
     }
-    vc = (int)(pd[localoffset] & 0x0F)*256 + (int)pd[localoffset+1];
+
+    pkt_type = tvb_get_guint8(tvb, 2);
+
     if (tree) {
-       ti = proto_tree_add_item(tree, (modulo == 8) ? proto_x25 : proto_ex25,
-               localoffset, x25_pkt_len, NULL);
+       ti = proto_tree_add_item(tree, proto_x25, tvb, 0, x25_pkt_len, FALSE);
        x25_tree = proto_item_add_subtree(ti, ett_x25);
-       if (pd[localoffset] & 0x80)
-           proto_tree_add_item(x25_tree, (modulo == 8) ? hf_x25_qbit : hf_ex25_qbit,
-                   localoffset, 2, pd[localoffset]*256+pd[localoffset+1]);
-       if (pd[localoffset] & 0x40)
-           proto_tree_add_item(x25_tree, (modulo == 8) ? hf_x25_dbit : hf_ex25_dbit,
-                   localoffset, 2, pd[localoffset]*256+pd[localoffset+1]);
-       proto_tree_add_item(x25_tree, (modulo == 8) ? hf_x25_mod : hf_ex25_mod,
-               localoffset, 2, pd[localoffset]*256+pd[localoffset+1]);
+       ti = proto_tree_add_item(x25_tree, hf_x25_gfi, tvb, 0, 2, FALSE);
+       gfi_tree = proto_item_add_subtree(ti, ett_x25_gfi);
+       if ((pkt_type & 0x01) == X25_DATA)
+           proto_tree_add_boolean(gfi_tree, hf_x25_qbit, tvb, 0, 2,
+                   bytes0_1);
+       else if (pkt_type == X25_CALL_REQUEST ||
+               pkt_type == X25_CALL_ACCEPTED ||
+               pkt_type == X25_CLEAR_REQUEST ||
+               pkt_type == X25_CLEAR_CONFIRMATION)
+           proto_tree_add_boolean(gfi_tree, hf_x25_abit, tvb, 0, 2,
+                   bytes0_1);
+       if (pkt_type == X25_CALL_REQUEST || pkt_type == X25_CALL_ACCEPTED ||
+               (pkt_type & 0x01) == X25_DATA)
+           proto_tree_add_boolean(gfi_tree, hf_x25_dbit, tvb, 0, 2,
+                   bytes0_1);
+       proto_tree_add_uint(gfi_tree, hf_x25_mod, tvb, 0, 2, bytes0_1);
     }
-    switch (pd[localoffset+2]) {
+
+    switch (pkt_type) {
     case X25_CALL_REQUEST:
-       if (check_col(fd, COL_INFO))
-           col_add_fstr(fd, COL_INFO, "%s VC:%d",
-                   (fd->pseudo_header.x25.flags & FROM_DCE) ? "Inc. call"
-                                                            : "Call req." ,
+       if (check_col(pinfo->fd, COL_INFO))
+           col_add_fstr(pinfo->fd, COL_INFO, "%s VC:%d",
+                   (pinfo->pseudo_header->x25.flags & FROM_DCE) ? "Inc. call"
+                                                                : "Call req." ,
                     vc);
        if (x25_tree) {
-           proto_tree_add_item(x25_tree, (modulo == 8) ? hf_x25_lcn : hf_ex25_lcn,
-                   localoffset, 2, pd[localoffset]*256+pd[localoffset+1]);
-           proto_tree_add_uint_format(x25_tree,
-                   (modulo == 8) ? hf_x25_type : hf_ex25_type,
-                   localoffset+2, 1, X25_CALL_REQUEST,
-                   (fd->pseudo_header.x25.flags & FROM_DCE) ? "Incoming call"
-                                                            : "Call request");
+           proto_tree_add_uint(x25_tree, hf_x25_lcn, tvb,
+                   0, 2, bytes0_1);
+           proto_tree_add_uint_format(x25_tree, hf_x25_type, tvb, 2, 1,
+                   X25_CALL_REQUEST,
+                   (pinfo->pseudo_header->x25.flags & FROM_DCE) ? "Incoming call"
+                                                                : "Call request");
        }
-       localoffset += 3;
-       if (localoffset < x25_pkt_len+offset) /* calling/called addresses */
-           x25_ntoa(x25_tree, &localoffset, &pd[localoffset], fd, toa);
+       localoffset = 3;
+       if (localoffset < x25_pkt_len) /* calling/called addresses */
+           x25_ntoa(x25_tree, &localoffset, tvb, pinfo->fd, toa);
 
-       if (localoffset < x25_pkt_len+offset) /* facilities */
-           dump_facilities(x25_tree, &localoffset, &pd[localoffset]);
+       if (localoffset < x25_pkt_len) /* facilities */
+           dump_facilities(x25_tree, &localoffset, tvb);
 
-       if (IS_DATA_IN_FRAME(localoffset)) /* user data */
+       if (localoffset < tvb_reported_length(tvb)) /* user data */
        {
            guint8 spi;
            guint8 prt_id;
 
            /* Compare the first octet of the CALL REQUEST packet with
               various ISO 9577 NLPIDs, as per Annex A of ISO 9577. */
-           spi = pd[localoffset];
+           spi = tvb_get_guint8(tvb, localoffset);
            switch (spi) {
 
-           /* XXX - handle other NLPIDs, e.g. PPP? */
-
+           /*
+            * XXX - handle other NLPIDs, e.g. PPP?
+            * See RFC 1356 for information on at least some other
+            * ways of running other protocols atop X.25.
+            */
            case NLPID_IP:
-               x25_hash_add_proto_start(vc, fd->abs_secs,
-                                        fd->abs_usecs, dissect_ip);
+               x25_hash_add_proto_start(vc, pinfo->fd->abs_secs,
+                                        pinfo->fd->abs_usecs, ip_handle);
                if (x25_tree)
-                   proto_tree_add_text(x25_tree, localoffset, 1,
+                   proto_tree_add_text(x25_tree, tvb, localoffset, 1,
                                        "X.224 secondary protocol ID: IP");
                localoffset++;
                break;
 
            default:
-               if ((pd[localoffset] >= 0x03 && pd[localoffset] <= 0x82)
-                   && pd[localoffset+1] == 0x01) {
+               if ((spi >= 0x03 && spi <= 0x82)
+                   && tvb_get_guint8(tvb, localoffset+1) == 0x01) {
                    /* ISO 9577 claims that a SPI in that range is a
                       length field for X.224/ISO 8073 or X.264/ISO 11570;
                       however, some of them collide with NLPIDs such
@@ -1476,33 +1599,35 @@ dissect_x25(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
                       So we'll assume that's what it is, as the SPI
                       is in the right range for a length, and the UN
                       field is 0x01. */
-                   prt_id = pd[localoffset+2];
+                   prt_id = tvb_get_guint8(tvb, localoffset+2);
                    if (x25_tree) {
-                       proto_tree_add_text(x25_tree, localoffset, 1,
+                       proto_tree_add_text(x25_tree, tvb, localoffset, 1,
                                        "X.264 length indicator: %u",
-                                       pd[localoffset]);
-                       proto_tree_add_text(x25_tree, localoffset+1, 1,
+                                       spi);
+                       proto_tree_add_text(x25_tree, tvb, localoffset+1, 1,
                                        "X.264 UN TPDU identifier: 0x%02X",
-                                       pd[localoffset+1]);
-                       proto_tree_add_text(x25_tree, localoffset+2, 1,
+                                       tvb_get_guint8(tvb, localoffset+1));
+                       proto_tree_add_text(x25_tree, tvb, localoffset+2, 1,
                                        "X.264 protocol identifier: %s",
-                                       val_to_str(prt_id, prt_id_vals, "Unknown (0x%02X)"));
-                       proto_tree_add_text(x25_tree, localoffset+3, 1,
+                                       val_to_str(prt_id, prt_id_vals,
+                                           "Unknown (0x%02X)"));
+                       proto_tree_add_text(x25_tree, tvb, localoffset+3, 1,
                                        "X.264 sharing strategy: %s",
-                                       val_to_str(pd[localoffset+3], sharing_strategy_vals, "Unknown (0x%02X)"));
+                                       val_to_str(tvb_get_guint8(tvb, localoffset+3),
+                                           sharing_strategy_vals, "Unknown (0x%02X)"));
                    }
 
                    /* XXX - dissect the variable part? */
 
                    /* The length doesn't include the length octet itself. */
-                   localoffset += pd[localoffset] + 1;
+                   localoffset += spi + 1;
 
                    switch (prt_id) {
 
                    case PRT_ID_ISO_8073:
                        /* ISO 8073 COTP */
-                       x25_hash_add_proto_start(vc, fd->abs_secs,
-                                        fd->abs_usecs, dissect_cotp);
+                       x25_hash_add_proto_start(vc, pinfo->fd->abs_secs,
+                                        pinfo->fd->abs_usecs, ositp_handle);
                        break;
 
                    default:
@@ -1511,444 +1636,422 @@ dissect_x25(const u_char *pd, int offset, frame_data *fd, proto_tree *tree)
                } else {
                unknown:
                    if (x25_tree) {
-                       if (IS_DATA_IN_FRAME(localoffset))
-                          proto_tree_add_text(x25_tree, localoffset,
-                                       pi.captured_len-localoffset, "Data");
+                       proto_tree_add_text(x25_tree, tvb, localoffset,
+                               tvb_reported_length(tvb)-localoffset, "Data");
                    }
-                   localoffset = pi.captured_len;
+                   localoffset = tvb_reported_length(tvb);
                }
            }
        }
        break;
     case X25_CALL_ACCEPTED:
-       if(check_col(fd, COL_INFO))
-           col_add_fstr(fd, COL_INFO, "%s VC:%d",
-                   (fd->pseudo_header.x25.flags & FROM_DCE) ? "Call conn."
-                                                            : "Call acc." ,
+       if(check_col(pinfo->fd, COL_INFO))
+           col_add_fstr(pinfo->fd, COL_INFO, "%s VC:%d",
+                   (pinfo->pseudo_header->x25.flags & FROM_DCE) ? "Call conn."
+                                                                : "Call acc." ,
                    vc);
        if (x25_tree) {
-           proto_tree_add_item(x25_tree, (modulo == 8) ? hf_x25_lcn : hf_ex25_lcn,
-                   localoffset, 2, pd[localoffset]*256+pd[localoffset+1]);
-           proto_tree_add_uint_format(x25_tree,
-                   (modulo == 8) ? hf_x25_type : hf_ex25_type,
-                   localoffset+2, 1, X25_CALL_ACCEPTED,
-                   (fd->pseudo_header.x25.flags & FROM_DCE) ? "Call connected"
-                                                            : "Call accepted");
+           proto_tree_add_uint(x25_tree, hf_x25_lcn, tvb, 0, 2, bytes0_1);
+           proto_tree_add_uint_format(x25_tree, hf_x25_type, tvb, 2, 1,
+                   X25_CALL_ACCEPTED,
+                   (pinfo->pseudo_header->x25.flags & FROM_DCE) ? "Call connected"
+                                                                : "Call accepted");
        }
-       localoffset += 3;
-        if (localoffset < x25_pkt_len+offset) /* calling/called addresses */
-           x25_ntoa(x25_tree, &localoffset, &pd[localoffset], fd, toa);
+       localoffset = 3;
+        if (localoffset < x25_pkt_len) /* calling/called addresses */
+           x25_ntoa(x25_tree, &localoffset, tvb, pinfo->fd, toa);
 
-       if (localoffset < x25_pkt_len+offset) /* facilities */
-           dump_facilities(x25_tree, &localoffset, &pd[localoffset]);
+       if (localoffset < x25_pkt_len) /* facilities */
+           dump_facilities(x25_tree, &localoffset, tvb);
 
-       if (IS_DATA_IN_FRAME(localoffset)) { /* user data */
+       if (localoffset < tvb_reported_length(tvb)) { /* user data */
            if (x25_tree)
-               proto_tree_add_text(x25_tree, localoffset,
-                                   pi.captured_len-localoffset, "Data");
-           localoffset=pi.captured_len;
+               proto_tree_add_text(x25_tree, tvb, localoffset,
+                                   tvb_reported_length(tvb)-localoffset, "Data");
+           localoffset=tvb_reported_length(tvb);
        }
        break;
     case X25_CLEAR_REQUEST:
-       if(check_col(fd, COL_INFO)) {
-           col_add_fstr(fd, COL_INFO, "%s VC:%d %s - %s",
-                   (fd->pseudo_header.x25.flags & FROM_DCE) ? "Clear ind."
-                                                            : "Clear req." ,
-                   vc, clear_code(pd[localoffset+3]),
-                   clear_diag(pd[localoffset+4]));
+       if(check_col(pinfo->fd, COL_INFO)) {
+           col_add_fstr(pinfo->fd, COL_INFO, "%s VC:%d %s - %s",
+                   (pinfo->pseudo_header->x25.flags & FROM_DCE) ? "Clear ind."
+                                                                : "Clear req." ,
+                   vc, clear_code(tvb_get_guint8(tvb, 3)),
+                   clear_diag(tvb_get_guint8(tvb, 4)));
        }
-       x25_hash_add_proto_end(vc, fd->abs_secs, fd->abs_usecs);
+       x25_hash_add_proto_end(vc, pinfo->fd->abs_secs, pinfo->fd->abs_usecs);
        if (x25_tree) {
-           proto_tree_add_item(x25_tree, (modulo == 8) ? hf_x25_lcn : hf_ex25_lcn,
-                   localoffset, 2, pd[localoffset]*256+pd[localoffset+1]);
-           proto_tree_add_uint_format(x25_tree,
-                   (modulo == 8) ? hf_x25_type : hf_ex25_type,
-                   localoffset+2, 1, X25_CLEAR_REQUEST,
-                   (fd->pseudo_header.x25.flags & FROM_DCE) ? "Clear indication"
-                                                            : "Clear request");
-           if (localoffset+3 < x25_pkt_len+offset)
-               proto_tree_add_text(x25_tree, localoffset+3, 1,
-                       "Cause : %s", clear_code(pd[localoffset+3]));
-           if (localoffset+4 < x25_pkt_len+offset)
-               proto_tree_add_text(x25_tree, localoffset+4, 1,
-                       "Diagnostic : %s",
-                       clear_diag(pd[localoffset+4]));
+           proto_tree_add_uint(x25_tree, hf_x25_lcn, tvb, 0, 2, bytes0_1);
+           proto_tree_add_uint_format(x25_tree, hf_x25_type, tvb,
+                   localoffset+2, 1, X25_CLEAR_REQUEST,
+                   (pinfo->pseudo_header->x25.flags & FROM_DCE) ? "Clear indication"
+                                                                : "Clear request");
+           proto_tree_add_text(x25_tree, tvb, 3, 1,
+                   "Cause : %s", clear_code(tvb_get_guint8(tvb, 3)));
+           proto_tree_add_text(x25_tree, tvb, 4, 1,
+                   "Diagnostic : %s", clear_diag(tvb_get_guint8(tvb, 4)));
        }
-       localoffset += x25_pkt_len;
+       localoffset = x25_pkt_len;
        break;
     case X25_CLEAR_CONFIRMATION:
-       if(check_col(fd, COL_INFO))
-           col_add_fstr(fd, COL_INFO, "Clear Conf. VC:%d", vc);
+       if(check_col(pinfo->fd, COL_INFO))
+           col_add_fstr(pinfo->fd, COL_INFO, "Clear Conf. VC:%d", vc);
        if (x25_tree) {
-           proto_tree_add_item(x25_tree, (modulo == 8) ? hf_x25_lcn : hf_ex25_lcn,
-                   localoffset, 2, pd[localoffset]*256+pd[localoffset+1]);
-           proto_tree_add_item(x25_tree, (modulo == 8) ? hf_x25_type : hf_ex25_type,
-                   localoffset+2, 1, X25_CLEAR_CONFIRMATION);
+           proto_tree_add_uint(x25_tree, hf_x25_lcn, tvb, 0, 2, bytes0_1);
+           proto_tree_add_uint(x25_tree, hf_x25_type, tvb, 2, 1,
+                   X25_CLEAR_CONFIRMATION);
        }
-       localoffset += x25_pkt_len;
+       localoffset = x25_pkt_len;
 
-       if (IS_DATA_IN_FRAME(localoffset)) /* extended clear conf format */
-           x25_ntoa(x25_tree, &localoffset, &pd[localoffset], fd, toa);
+       if (localoffset < tvb_reported_length(tvb)) /* extended clear conf format */
+           x25_ntoa(x25_tree, &localoffset, tvb, pinfo->fd, toa);
 
-       if (IS_DATA_IN_FRAME(localoffset)) /* facilities */
-           dump_facilities(x25_tree, &localoffset, &pd[localoffset]);
+       if (localoffset < tvb_reported_length(tvb)) /* facilities */
+           dump_facilities(x25_tree, &localoffset, tvb);
        break;
     case X25_DIAGNOSTIC:
-       if(check_col(fd, COL_INFO)) {
-           col_add_fstr(fd, COL_INFO, "Diag. %d", (int)pd[localoffset+3]);
+       if(check_col(pinfo->fd, COL_INFO)) {
+           col_add_fstr(pinfo->fd, COL_INFO, "Diag. %d",
+                   (int)tvb_get_guint8(tvb, 3));
        }
        if (x25_tree) {
-           proto_tree_add_item(x25_tree, (modulo == 8) ? hf_x25_type : hf_ex25_type,
-                   localoffset+2, 1, X25_DIAGNOSTIC);
-           if (localoffset+3 < x25_pkt_len+offset)
-               proto_tree_add_text(x25_tree, localoffset+3, 1,
-                       "Diagnostic : %d", (int)pd[localoffset+3]);
+           proto_tree_add_uint(x25_tree, hf_x25_type, tvb, 2, 1,
+                   X25_DIAGNOSTIC);
+           proto_tree_add_text(x25_tree, tvb, 3, 1,
+                   "Diagnostic : %d", (int)tvb_get_guint8(tvb, 3));
        }
-       localoffset += x25_pkt_len;
+       localoffset = x25_pkt_len;
        break;
     case X25_INTERRUPT:
-       if(check_col(fd, COL_INFO))
-           col_add_fstr(fd, COL_INFO, "Interrupt VC:%d", vc);
+       if(check_col(pinfo->fd, COL_INFO))
+           col_add_fstr(pinfo->fd, COL_INFO, "Interrupt VC:%d", vc);
        if (x25_tree) {
-           proto_tree_add_item(x25_tree, (modulo == 8) ? hf_x25_lcn : hf_ex25_lcn,
-                   localoffset, 2, pd[localoffset]*256+pd[localoffset+1]);
-           proto_tree_add_item(x25_tree, (modulo == 8) ? hf_x25_type : hf_ex25_type,
-                   localoffset+2, 1, X25_INTERRUPT);
+           proto_tree_add_uint(x25_tree, hf_x25_lcn, tvb, 0, 2, bytes0_1);
+           proto_tree_add_uint(x25_tree, hf_x25_type, tvb, 2, 1,
+                   X25_INTERRUPT);
        }
-       localoffset += x25_pkt_len;
+       localoffset = x25_pkt_len;
        break;
     case X25_INTERRUPT_CONFIRMATION:
-       if(check_col(fd, COL_INFO))
-           col_add_fstr(fd, COL_INFO, "Interrupt Conf. VC:%d", vc);
+       if(check_col(pinfo->fd, COL_INFO))
+           col_add_fstr(pinfo->fd, COL_INFO, "Interrupt Conf. VC:%d", vc);
        if (x25_tree) {
-           proto_tree_add_item(x25_tree, (modulo == 8) ? hf_x25_lcn : hf_ex25_lcn,
-                   localoffset, 2, pd[localoffset]*256+pd[localoffset+1]);
-           proto_tree_add_item(x25_tree, (modulo == 8) ? hf_x25_type : hf_ex25_type,
-                   localoffset+2, 1, X25_INTERRUPT_CONFIRMATION);
+           proto_tree_add_uint(x25_tree, hf_x25_lcn, tvb, 0, 2, bytes0_1);
+           proto_tree_add_uint(x25_tree, hf_x25_type, tvb, 2, 1,
+                   X25_INTERRUPT_CONFIRMATION);
        }
-       localoffset += x25_pkt_len;
+       localoffset = x25_pkt_len;
        break;
     case X25_RESET_REQUEST:
-       if(check_col(fd, COL_INFO)) {
-           col_add_fstr(fd, COL_INFO, "%s VC:%d %s - Diag.:%d",
-                   (fd->pseudo_header.x25.flags & FROM_DCE) ? "Reset ind."
-                                                            : "Reset req.",
-                   vc, reset_code(pd[localoffset+3]),
-                   (int)pd[localoffset+4]);
+       if(check_col(pinfo->fd, COL_INFO)) {
+           col_add_fstr(pinfo->fd, COL_INFO, "%s VC:%d %s - Diag.:%d",
+                   (pinfo->pseudo_header->x25.flags & FROM_DCE) ? "Reset ind."
+                                                                : "Reset req.",
+                   vc, reset_code(tvb_get_guint8(tvb, 3)),
+                   (int)tvb_get_guint8(tvb, 4));
        }
-       x25_hash_add_proto_end(vc, fd->abs_secs, fd->abs_usecs);
+       x25_hash_add_proto_end(vc, pinfo->fd->abs_secs, pinfo->fd->abs_usecs);
        if (x25_tree) {
-           proto_tree_add_item(x25_tree, (modulo == 8) ? hf_x25_lcn : hf_ex25_lcn,
-                   localoffset, 2, pd[localoffset]*256+pd[localoffset+1]);
-           proto_tree_add_uint_format(x25_tree,
-                   (modulo == 8) ? hf_x25_type : hf_ex25_type, localoffset+2, 1,
+           proto_tree_add_uint(x25_tree, hf_x25_lcn, tvb, 0, 2, bytes0_1);
+           proto_tree_add_uint_format(x25_tree, hf_x25_type, tvb, 2, 1,
                    X25_RESET_REQUEST,
-                   (fd->pseudo_header.x25.flags & FROM_DCE) ? "Reset indication"
-                                                             : "Reset request");
-           if (localoffset+3 < x25_pkt_len+offset)
-               proto_tree_add_text(x25_tree, localoffset+3, 1,
-                       "Cause : %s", reset_code(pd[localoffset+3]));
-           if (localoffset+4 < x25_pkt_len+offset)
-               proto_tree_add_text(x25_tree, localoffset+4, 1,
-                       "Diagnostic : %d", (int)pd[localoffset+4]);
+                   (pinfo->pseudo_header->x25.flags & FROM_DCE) ? "Reset indication"
+                                                                 : "Reset request");
+           proto_tree_add_text(x25_tree, tvb, 3, 1,
+                   "Cause : %s", reset_code(tvb_get_guint8(tvb, 3)));
+           proto_tree_add_text(x25_tree, tvb, 4, 1,
+                   "Diagnostic : %d", (int)tvb_get_guint8(tvb, 4));
        }
-       localoffset += x25_pkt_len;
+       localoffset = x25_pkt_len;
        break;
     case X25_RESET_CONFIRMATION:
-       if(check_col(fd, COL_INFO))
-           col_add_fstr(fd, COL_INFO, "Reset conf. VC:%d", vc);
+       if(check_col(pinfo->fd, COL_INFO))
+           col_add_fstr(pinfo->fd, COL_INFO, "Reset conf. VC:%d", vc);
        if (x25_tree) {
-           proto_tree_add_item(x25_tree, (modulo == 8) ? hf_x25_lcn : hf_ex25_lcn,
-                   localoffset, 2, pd[localoffset]*256+pd[localoffset+1]);
-           proto_tree_add_item(x25_tree, (modulo == 8) ? hf_x25_type : hf_ex25_type,
-                   localoffset+2, 1, X25_RESET_CONFIRMATION);
+           proto_tree_add_uint(x25_tree, hf_x25_lcn, tvb, 0, 2, bytes0_1);
+           proto_tree_add_uint(x25_tree, hf_x25_type, tvb, 2, 1,
+                   X25_RESET_CONFIRMATION);
        }
-       localoffset += x25_pkt_len;
+       localoffset = x25_pkt_len;
        break;
     case X25_RESTART_REQUEST:
-       if(check_col(fd, COL_INFO)) {
-           col_add_fstr(fd, COL_INFO, "%s %s - Diag.:%d",
-                   (fd->pseudo_header.x25.flags & FROM_DCE) ? "Restart ind."
-                                                            : "Restart req.",
-                   restart_code(pd[localoffset+3]),
-                   (int)pd[localoffset+4]);
+       if(check_col(pinfo->fd, COL_INFO)) {
+           col_add_fstr(pinfo->fd, COL_INFO, "%s %s - Diag.:%d",
+                   (pinfo->pseudo_header->x25.flags & FROM_DCE) ? "Restart ind."
+                                                                : "Restart req.",
+                   restart_code(tvb_get_guint8(tvb, 3)),
+                   (int)tvb_get_guint8(tvb, 3));
        }
        if (x25_tree) {
-           proto_tree_add_uint_format(x25_tree,
-                   (modulo == 8) ? hf_x25_type : hf_ex25_type, localoffset+2, 1,
+           proto_tree_add_uint_format(x25_tree, hf_x25_type, tvb, 2, 1,
                    X25_RESTART_REQUEST,
-                   (fd->pseudo_header.x25.flags & FROM_DCE) ? "Restart indication"
-                                                            : "Restart request");
-           if (localoffset+3 < x25_pkt_len+offset)
-               proto_tree_add_text(x25_tree, localoffset+3, 1,
-                       "Cause : %s", restart_code(pd[localoffset+3]));
-           if (localoffset+4 < x25_pkt_len+offset)
-               proto_tree_add_text(x25_tree, localoffset+4, 1,
-                       "Diagnostic : %d", (int)pd[localoffset+4]);
+                   (pinfo->pseudo_header->x25.flags & FROM_DCE) ? "Restart indication"
+                                                                : "Restart request");
+           proto_tree_add_text(x25_tree, tvb, 3, 1,
+                   "Cause : %s", restart_code(tvb_get_guint8(tvb, 3)));
+           proto_tree_add_text(x25_tree, tvb, 4, 1,
+                   "Diagnostic : %d", (int)tvb_get_guint8(tvb, 4));
        }
-       localoffset += x25_pkt_len;
+       localoffset = x25_pkt_len;
        break;
     case X25_RESTART_CONFIRMATION:
-       if(check_col(fd, COL_INFO))
-           col_add_str(fd, COL_INFO, "Restart conf.");
+       if(check_col(pinfo->fd, COL_INFO))
+           col_set_str(pinfo->fd, COL_INFO, "Restart conf.");
        if (x25_tree)
-           proto_tree_add_item(x25_tree, (modulo == 8) ? hf_x25_type : hf_ex25_type,
-                   localoffset+2, 1, X25_RESTART_CONFIRMATION);
-       localoffset += x25_pkt_len;
+           proto_tree_add_uint(x25_tree, hf_x25_type, tvb, 2, 1,
+                   X25_RESTART_CONFIRMATION);
+       localoffset = x25_pkt_len;
        break;
     case X25_REGISTRATION_REQUEST:
-       if(check_col(fd, COL_INFO))
-           col_add_str(fd, COL_INFO, "Registration req.");
+       if(check_col(pinfo->fd, COL_INFO))
+           col_set_str(pinfo->fd, COL_INFO, "Registration req.");
        if (x25_tree)
-           proto_tree_add_item(x25_tree, (modulo == 8) ? hf_x25_type : hf_ex25_type,
-                   localoffset+2, 1, X25_REGISTRATION_REQUEST);
-       localoffset += 3;
-       if (localoffset < x25_pkt_len+offset)
-           x25_ntoa(x25_tree, &localoffset, &pd[localoffset], fd, FALSE);
+           proto_tree_add_uint(x25_tree, hf_x25_type, tvb, 2, 1,
+                   X25_REGISTRATION_REQUEST);
+       localoffset = 3;
+       if (localoffset < x25_pkt_len)
+           x25_ntoa(x25_tree, &localoffset, tvb, pinfo->fd, FALSE);
 
        if (x25_tree) {
-           if (localoffset < x25_pkt_len+offset)
-               proto_tree_add_text(x25_tree, localoffset, 1,
-                       "Registration length: %d", pd[localoffset] & 0x7F);
-           if (localoffset+1 < x25_pkt_len+offset)
-               proto_tree_add_text(x25_tree, localoffset+1,
-                       pd[localoffset] & 0x7F, "Registration");
+           if (localoffset < x25_pkt_len)
+               proto_tree_add_text(x25_tree, tvb, localoffset, 1,
+                       "Registration length: %d",
+                       tvb_get_guint8(tvb, localoffset) & 0x7F);
+           if (localoffset+1 < x25_pkt_len)
+               proto_tree_add_text(x25_tree, tvb, localoffset+1,
+                       tvb_get_guint8(tvb, localoffset) & 0x7F,
+                       "Registration");
        }
-       localoffset = pi.captured_len;
+       localoffset = tvb_reported_length(tvb);
        break;
     case X25_REGISTRATION_CONFIRMATION:
-       if(check_col(fd, COL_INFO))
-           col_add_str(fd, COL_INFO, "Registration conf.");
+       if(check_col(pinfo->fd, COL_INFO))
+           col_set_str(pinfo->fd, COL_INFO, "Registration conf.");
        if (x25_tree) {
-           proto_tree_add_item(x25_tree, (modulo == 8) ? hf_x25_type : hf_ex25_type,
-                   localoffset+2, 1, X25_REGISTRATION_CONFIRMATION);
-           if (localoffset+3 < x25_pkt_len+offset)
-               proto_tree_add_text(x25_tree, localoffset+3, 1,
-                       "Cause: %s", registration_code(pd[localoffset+3]));
-           if (localoffset+4 < x25_pkt_len+offset)
-               proto_tree_add_text(x25_tree, localoffset+4, 1,
-                       "Diagnostic: %s", registration_code(pd[localoffset+4]));
+           proto_tree_add_uint(x25_tree, hf_x25_type, tvb, 2, 1,
+                   X25_REGISTRATION_CONFIRMATION);
+           proto_tree_add_text(x25_tree, tvb, 3, 1,
+                   "Cause: %s", registration_code(tvb_get_guint8(tvb, 3)));
+           proto_tree_add_text(x25_tree, tvb, 4, 1,
+                   "Diagnostic: %s", registration_code(tvb_get_guint8(tvb, 4)));
        }
-       localoffset += 5;
-       if (localoffset < x25_pkt_len+offset)
-           x25_ntoa(x25_tree, &localoffset, &pd[localoffset], fd, TRUE);
+       localoffset = 5;
+       if (localoffset < x25_pkt_len)
+           x25_ntoa(x25_tree, &localoffset, tvb, pinfo->fd, TRUE);
 
        if (x25_tree) {
-           if (localoffset < x25_pkt_len+offset)
-               proto_tree_add_text(x25_tree, localoffset, 1,
-                       "Registration length: %d", pd[localoffset] & 0x7F);
-           if (localoffset+1 < x25_pkt_len+offset)
-               proto_tree_add_text(x25_tree, localoffset+1,
-                       pd[localoffset] & 0x7F, "Registration");
+           if (localoffset < x25_pkt_len)
+               proto_tree_add_text(x25_tree, tvb, localoffset, 1,
+                       "Registration length: %d",
+                       tvb_get_guint8(tvb, localoffset) & 0x7F);
+           if (localoffset+1 < x25_pkt_len)
+               proto_tree_add_text(x25_tree, tvb, localoffset+1,
+                       tvb_get_guint8(tvb, localoffset) & 0x7F,
+                       "Registration");
        }
-       localoffset = pi.captured_len;
+       localoffset = tvb_reported_length(tvb);
        break;
     default :
-       localoffset += 2;
-       if ((pd[localoffset] & 0x01) == X25_DATA)
+       localoffset = 2;
+       if ((pkt_type & 0x01) == X25_DATA)
        {
-           if(check_col(fd, COL_INFO)) {
+           if(check_col(pinfo->fd, COL_INFO)) {
                if (modulo == 8)
-                   col_add_fstr(fd, COL_INFO,
+                   col_add_fstr(pinfo->fd, COL_INFO,
                            "Data VC:%d P(S):%d P(R):%d %s", vc,
-                           (pd[localoffset] >> 1) & 0x07,
-                           (pd[localoffset] >> 5) & 0x07,
-                           ((pd[localoffset]>>4) & 0x01) ? " M" : "");
+                           (pkt_type >> 1) & 0x07,
+                           (pkt_type >> 5) & 0x07,
+                           ((pkt_type >> 4) & 0x01) ? " M" : "");
                else
-                   col_add_fstr(fd, COL_INFO,
+                   col_add_fstr(pinfo->fd, COL_INFO,
                            "Data VC:%d P(S):%d P(R):%d %s", vc,
-                           pd[localoffset+1] >> 1,
-                           pd[localoffset] >> 1,
-                           (pd[localoffset+1] & 0x01) ? " M" : "");
+                           tvb_get_guint8(tvb, localoffset+1) >> 1,
+                           pkt_type >> 1,
+                           (tvb_get_guint8(tvb, localoffset+1) & 0x01) ? " M" : "");
            }
            if (x25_tree) {
-               proto_tree_add_item(x25_tree, (modulo == 8) ? hf_x25_lcn : hf_ex25_lcn,
-                       localoffset-2, 2, pd[localoffset-2]*256+pd[localoffset-1]);
+               proto_tree_add_uint(x25_tree, hf_x25_lcn, tvb, localoffset-2,
+                       2, bytes0_1);
+               proto_tree_add_uint_hidden(x25_tree, hf_x25_type, tvb,
+                       localoffset, 1, X25_DATA);
                if (modulo == 8) {
-                   proto_tree_add_item_hidden(x25_tree, hf_x25_type, localoffset, 1,
-                           X25_DATA);
-                   proto_tree_add_item(x25_tree, hf_x25_p_r, localoffset, 1,
-                           pd[localoffset]);
-                   if (pd[localoffset] & 0x10)
-                       proto_tree_add_item(x25_tree, hf_x25_mbit, localoffset, 1,
-                           pd[localoffset]);
-                   proto_tree_add_item(x25_tree, hf_x25_p_s, localoffset, 1,
-                           pd[localoffset]);
-                   proto_tree_add_text(x25_tree, localoffset, 1,
-                           decode_boolean_bitfield(pd[localoffset], 0x01, 1*8,
+                   proto_tree_add_uint(x25_tree, hf_x25_p_r_mod8, tvb,
+                           localoffset, 1, pkt_type);
+                   if (pkt_type & 0x10)
+                       proto_tree_add_boolean(x25_tree, hf_x25_mbit_mod8, tvb,
+                           localoffset, 1, pkt_type);
+                   proto_tree_add_uint(x25_tree, hf_x25_p_s_mod8, tvb,
+                           localoffset, 1, pkt_type);
+                   proto_tree_add_text(x25_tree, tvb, localoffset, 1,
+                           decode_boolean_bitfield(pkt_type, 0x01, 1*8,
                                NULL, "DATA"));
                }
                else {
-                   proto_tree_add_item_hidden(x25_tree, hf_ex25_type, localoffset, 1,
-                           X25_DATA);
-                   proto_tree_add_item(x25_tree, hf_x25_p_r, localoffset, 1,
-                           pd[localoffset]);
-                   proto_tree_add_item(x25_tree, hf_x25_p_s, localoffset+1, 1,
-                           pd[localoffset+1]);
-                   if (pd[localoffset+1] & 0x01)
-                       proto_tree_add_item(x25_tree, hf_ex25_mbit, localoffset+1, 1,
-                           pd[localoffset+1]);
+                   proto_tree_add_uint(x25_tree, hf_x25_p_r_mod128, tvb,
+                           localoffset, 1, pkt_type);
+                   proto_tree_add_uint(x25_tree, hf_x25_p_s_mod128, tvb,
+                           localoffset+1, 1,
+                           tvb_get_guint8(tvb, localoffset+1));
+                   if (tvb_get_guint8(tvb, localoffset+1) & 0x01)
+                       proto_tree_add_boolean(x25_tree, hf_x25_mbit_mod128, tvb,
+                               localoffset+1, 1,
+                               tvb_get_guint8(tvb, localoffset+1));
                }
            }
            localoffset += (modulo == 8) ? 1 : 2;
            break;
        }
-       switch (pd[localoffset] & 0x1F)
+       switch (pkt_type & 0x1F)
        {
        case X25_RR:
-           if(check_col(fd, COL_INFO)) {
+           if(check_col(pinfo->fd, COL_INFO)) {
                if (modulo == 8)
-                   col_add_fstr(fd, COL_INFO, "RR VC:%d P(R):%d",
-                           vc, (pd[localoffset] >> 5) & 0x07);
+                   col_add_fstr(pinfo->fd, COL_INFO, "RR VC:%d P(R):%d",
+                           vc, (pkt_type >> 5) & 0x07);
                else
-                   col_add_fstr(fd, COL_INFO, "RR VC:%d P(R):%d",
-                           vc, pd[localoffset+1] >> 1);
+                   col_add_fstr(pinfo->fd, COL_INFO, "RR VC:%d P(R):%d",
+                           vc, tvb_get_guint8(tvb, localoffset+1) >> 1);
            }
            if (x25_tree) {
-               proto_tree_add_item(x25_tree, (modulo == 8) ? hf_x25_lcn : hf_ex25_lcn,
-                       localoffset-2, 2, pd[localoffset-2]*256+pd[localoffset-1]);
+               proto_tree_add_uint(x25_tree, hf_x25_lcn, tvb, localoffset-2,
+                       2, bytes0_1);
                if (modulo == 8) {
-                   proto_tree_add_item(x25_tree, hf_x25_p_r,
-                           localoffset, 1, pd[localoffset]);
-                   proto_tree_add_item(x25_tree, hf_x25_type, localoffset, 1, X25_RR);
+                   proto_tree_add_uint(x25_tree, hf_x25_p_r_mod8, tvb,
+                           localoffset, 1, pkt_type);
+                   proto_tree_add_uint(x25_tree, hf_x25_type, tvb,
+                           localoffset, 1, X25_RR);
                }
                else {
-                   proto_tree_add_item(x25_tree, hf_ex25_type, localoffset, 1, X25_RR);
-                   proto_tree_add_item(x25_tree, hf_ex25_p_r,
-                           localoffset+1, 1, pd[localoffset+1]);
+                   proto_tree_add_uint(x25_tree, hf_x25_type, tvb,
+                           localoffset, 1, X25_RR);
+                   proto_tree_add_item(x25_tree, hf_x25_p_r_mod128, tvb,
+                           localoffset+1, 1, FALSE);
                }
            }
            break;
 
        case X25_RNR:
-           if(check_col(fd, COL_INFO)) {
+           if(check_col(pinfo->fd, COL_INFO)) {
                if (modulo == 8)
-                   col_add_fstr(fd, COL_INFO, "RNR VC:%d P(R):%d",
-                           vc, (pd[localoffset] >> 5) & 0x07);
+                   col_add_fstr(pinfo->fd, COL_INFO, "RNR VC:%d P(R):%d",
+                           vc, (pkt_type >> 5) & 0x07);
                else
-                   col_add_fstr(fd, COL_INFO, "RNR VC:%d P(R):%d",
-                           vc, pd[localoffset+1] >> 1);
+                   col_add_fstr(pinfo->fd, COL_INFO, "RNR VC:%d P(R):%d",
+                           vc, tvb_get_guint8(tvb, localoffset+1) >> 1);
            }
            if (x25_tree) {
-               proto_tree_add_item(x25_tree, (modulo == 8) ? hf_x25_lcn : hf_ex25_lcn,
-                       localoffset-2, 2, pd[localoffset-2]*256+pd[localoffset-1]);
+               proto_tree_add_uint(x25_tree, hf_x25_lcn, tvb, localoffset-2,
+                       2, bytes0_1);
                if (modulo == 8) {
-                   proto_tree_add_item(x25_tree, hf_x25_p_r,
-                           localoffset, 1, pd[localoffset]);
-                   proto_tree_add_item(x25_tree, hf_x25_type, localoffset, 1, X25_RNR);
+                   proto_tree_add_uint(x25_tree, hf_x25_p_r_mod8, tvb,
+                           localoffset, 1, pkt_type);
+                   proto_tree_add_uint(x25_tree, hf_x25_type, tvb,
+                           localoffset, 1, X25_RNR);
                }
                else {
-                   proto_tree_add_item(x25_tree, hf_ex25_type, localoffset, 1, X25_RNR);
-                   proto_tree_add_item(x25_tree, hf_ex25_p_r,
-                           localoffset+1, 1, pd[localoffset+1]);
+                   proto_tree_add_uint(x25_tree, hf_x25_type, tvb,
+                           localoffset, 1, X25_RNR);
+                   proto_tree_add_item(x25_tree, hf_x25_p_r_mod128, tvb,
+                           localoffset+1, 1, FALSE);
                }
            }
            break;
 
        case X25_REJ:
-           if(check_col(fd, COL_INFO)) {
+           if(check_col(pinfo->fd, COL_INFO)) {
                if (modulo == 8)
-                   col_add_fstr(fd, COL_INFO, "REJ VC:%d P(R):%d",
-                           vc, (pd[localoffset] >> 5) & 0x07);
+                   col_add_fstr(pinfo->fd, COL_INFO, "REJ VC:%d P(R):%d",
+                           vc, (pkt_type >> 5) & 0x07);
                else
-                   col_add_fstr(fd, COL_INFO, "REJ VC:%d P(R):%d",
-                           vc, pd[localoffset+1] >> 1);
+                   col_add_fstr(pinfo->fd, COL_INFO, "REJ VC:%d P(R):%d",
+                           vc, tvb_get_guint8(tvb, localoffset+1) >> 1);
            }
            if (x25_tree) {
-               proto_tree_add_item(x25_tree, (modulo == 8) ? hf_x25_lcn : hf_ex25_lcn,
-                       localoffset-2, 2, pd[localoffset-2]*256+pd[localoffset-1]);
+               proto_tree_add_uint(x25_tree, hf_x25_lcn, tvb, localoffset-2,
+                       2, bytes0_1);
                if (modulo == 8) {
-                   proto_tree_add_item(x25_tree, hf_x25_p_r,
-                           localoffset, 1, pd[localoffset]);
-                   proto_tree_add_item(x25_tree, hf_x25_type, localoffset, 1, X25_REJ);
+                   proto_tree_add_uint(x25_tree, hf_x25_p_r_mod8, tvb,
+                           localoffset, 1, pkt_type);
+                   proto_tree_add_uint(x25_tree, hf_x25_type, tvb,
+                           localoffset, 1, X25_REJ);
                }
                else {
-                   proto_tree_add_item(x25_tree, hf_ex25_type, localoffset, 1, X25_REJ);
-                   proto_tree_add_item(x25_tree, hf_ex25_p_r,
-                           localoffset+1, 1, pd[localoffset+1]);
+                   proto_tree_add_uint(x25_tree, hf_x25_type, tvb,
+                           localoffset, 1, X25_REJ);
+                   proto_tree_add_item(x25_tree, hf_x25_p_r_mod128, tvb,
+                           localoffset+1, 1, FALSE);
                }
            }
        }
        localoffset += (modulo == 8) ? 1 : 2;
     }
 
-    if (!IS_DATA_IN_FRAME(localoffset)) return;
+    if (localoffset >= tvb_reported_length(tvb)) return;
 
+    next_tvb = tvb_new_subset(tvb, localoffset, -1, -1);
     /* search the dissector in the hash table */
-    if ((dissect = x25_hash_get_dissect(fd->abs_secs, fd->abs_usecs, vc)))
-      (*dissect)(pd, localoffset, fd, tree);
+    if ((dissect = x25_hash_get_dissect(pinfo->fd->abs_secs, pinfo->fd->abs_usecs, vc)))
+       call_dissector(dissect, next_tvb, pinfo, tree);
     else {
-      if (pd[localoffset] == 0x45) /* If the Call Req. has not been captured,
-                                   * assume these packets carry IP */
-      {
-         x25_hash_add_proto_start(vc, fd->abs_secs,
-                                  fd->abs_usecs, dissect_ip);
-         dissect_ip(pd, localoffset, fd, tree);
-      }
-      else {
-         dissect_data(pd, localoffset, fd, tree);
-      }
+       /* If the Call Req. has not been captured, assume these packets carry IP */
+       if (tvb_get_guint8(tvb, localoffset) == 0x45) {
+           x25_hash_add_proto_start(vc, pinfo->fd->abs_secs,
+                   pinfo->fd->abs_usecs, ip_handle);
+           call_dissector(ip_handle, next_tvb, pinfo, tree);
+       }
+       else {
+           dissect_data(next_tvb, 0, pinfo, tree);
+       }
     }
 }
 
 void
 proto_register_x25(void)
 {
-    static hf_register_info hf8[] = {
-       { &hf_x25_qbit,
-         { "Q Bit", "x25.q", FT_BOOLEAN, 2, NULL, 0x8000,
-               "Qualifier Bit" } },
+    static hf_register_info hf[] = {
+       { &hf_x25_gfi,
+         { "GFI", "x.25.gfi", FT_UINT16, BASE_BIN, NULL, 0xF000,
+               "General format identifier", HFILL }},
+       { &hf_x25_abit,
+         { "A Bit", "x.25.a", FT_BOOLEAN, 16, NULL, 0x8000,
+               "Address Bit", HFILL }},
        { &hf_x25_qbit,
-         { "D Bit", "x25.d", FT_BOOLEAN, 2, NULL, 0x4000,
-               "Delivery Confirmation Bit" } },
+         { "Q Bit", "x.25.q", FT_BOOLEAN, 16, NULL, 0x8000,
+               "Qualifier Bit", HFILL }},
+       { &hf_x25_dbit,
+         { "D Bit", "x.25.d", FT_BOOLEAN, 16, NULL, 0x4000,
+               "Delivery Confirmation Bit", HFILL }},
        { &hf_x25_mod,
-         { "Modulo", "x25.mod", FT_UINT16, BASE_DEC, VALS(vals_modulo), 0x3000,
-               "Specifies whether the frame is modulo 8 or 128" } },
+         { "Modulo", "x.25.mod", FT_UINT16, BASE_DEC, VALS(vals_modulo), 0x3000,
+               "Specifies whether the frame is modulo 8 or 128", HFILL }},
        { &hf_x25_lcn,
-         { "Logical Channel", "x25.lcn", FT_UINT16, BASE_HEX, NULL, 0x0FFF,
-               "Logical Channel Number" } },
+         { "Logical Channel", "x.25.lcn", FT_UINT16, BASE_DEC, NULL, 0x0FFF,
+               "Logical Channel Number", HFILL }},
        { &hf_x25_type,
-         { "Packet Type", "x25.type", FT_UINT8, BASE_HEX, VALS(vals_x25_type), 0x0,
-               "Packet Type" } },
-       { &hf_x25_p_r,
-         { "P(R)", "x25.p_r", FT_UINT8, BASE_HEX, NULL, 0xE0,
-               "Packet Receive Sequence Number" } },
-       { &hf_x25_mbit,
-         { "M Bit", "x25.m", FT_BOOLEAN, 1, NULL, 0x10,
-               "More Bit" } },
-       { &hf_x25_p_s,
-         { "P(S)", "x25.p_s", FT_UINT8, BASE_HEX, NULL, 0x0E,
-               "Packet Send Sequence Number" } },
-    };
-
-    static hf_register_info hf128[] = {
-       { &hf_ex25_qbit,
-         { "Q Bit", "ex25.q", FT_BOOLEAN, 2, NULL, 0x8000,
-               "Qualifier Bit" } },
-       { &hf_ex25_qbit,
-         { "D Bit", "ex25.d", FT_BOOLEAN, 2, NULL, 0x4000,
-               "Delivery Confirmation Bit" } },
-       { &hf_ex25_mod,
-         { "Modulo", "ex25.mod", FT_UINT16, BASE_DEC, VALS(vals_modulo), 0x3000,
-               "Specifies whether the frame is modulo 8 or 128" } },
-       { &hf_ex25_lcn,
-         { "Logical Channel", "ex25.lcn", FT_UINT16, BASE_HEX, NULL, 0x0FFF,
-               "Logical Channel Number" } },
-       { &hf_ex25_type,
-         { "Packet Type", "ex25.type", FT_UINT8, BASE_HEX, VALS(vals_x25_type), 0x0,
-               "Packet Type" } },
-       { &hf_ex25_p_r,
-         { "P(R)", "ex25.p_r", FT_UINT8, BASE_HEX, NULL, 0xFE,
-               "Packet Receive Sequence Number" } },
-       { &hf_ex25_mbit,
-         { "M Bit", "ex25.m", FT_BOOLEAN, 1, NULL, 0x01,
-               "More Bit" } },
-       { &hf_ex25_p_s,
-         { "P(S)", "ex25.p_s", FT_UINT8, BASE_HEX, NULL, 0xFE,
-               "Packet Send Sequence Number" } },
+         { "Packet Type", "x.25.type", FT_UINT8, BASE_HEX, VALS(vals_x25_type), 0x0,
+               "Packet Type", HFILL }},
+       { &hf_x25_p_r_mod8,
+         { "P(R)", "x.25.p_r", FT_UINT8, BASE_HEX, NULL, 0xE0,
+               "Packet Receive Sequence Number", HFILL }},
+       { &hf_x25_p_r_mod128,
+         { "P(R)", "x.25.p_r", FT_UINT8, BASE_HEX, NULL, 0xFE,
+               "Packet Receive Sequence Number", HFILL }},
+       { &hf_x25_mbit_mod8,
+         { "M Bit", "x.25.m", FT_BOOLEAN, 8, NULL, 0x10,
+               "More Bit", HFILL }},
+       { &hf_x25_mbit_mod128,
+         { "M Bit", "x.25.m", FT_BOOLEAN, 8, NULL, 0x01,
+               "More Bit", HFILL }},
+       { &hf_x25_p_s_mod8,
+         { "P(S)", "x.25.p_s", FT_UINT8, BASE_HEX, NULL, 0x0E,
+               "Packet Send Sequence Number", HFILL }},
+       { &hf_x25_p_s_mod128,
+         { "P(S)", "x.25.p_s", FT_UINT8, BASE_HEX, NULL, 0xFE,
+               "Packet Send Sequence Number", HFILL }},
     };
     static gint *ett[] = {
         &ett_x25,
+       &ett_x25_gfi,
        &ett_x25_fac,
        &ett_x25_fac_unknown,
        &ett_x25_fac_mark,
@@ -1972,16 +2075,22 @@ proto_register_x25(void)
        &ett_x25_fac_priority
     };
 
-    proto_x25 = proto_register_protocol ("X.25", "x25");
-    proto_ex25 = proto_register_protocol ("Extended X.25 (modulo 128)", "ex25");
-    proto_register_field_array (proto_x25, hf8, array_length(hf8));
-    proto_register_field_array (proto_ex25, hf128, array_length(hf128));
+    proto_x25 = proto_register_protocol ("X.25", "X.25", "x.25");
+    proto_register_field_array (proto_x25, hf, array_length(hf));
     proto_register_subtree_array(ett, array_length(ett));
     register_init_routine(&reinit_x25_hashtable);
+
+    register_dissector("x.25", dissect_x25, proto_x25);
 }
 
 void
 proto_reg_handoff_x25(void)
 {
-       dissector_add("ethertype", ETHERTYPE_X25L3, dissect_x25);
+    /*
+     * Get handles for the IP and OSI TP (COTP/CLTP) dissectors.
+     */
+    ip_handle = find_dissector("ip");
+    ositp_handle = find_dissector("ositp");
+
+    dissector_add("llc.dsap", SAP_X25, dissect_x25, proto_x25);
 }