2 XXX all this offset>>3 and calculations of bytes in the tvb everytime
3 we put something in the tree is just silly. should be replaced with some
7 * Routines for dissection of ASN.1 Aligned PER
10 * $Id: packet-per.c,v 1.10 2003/07/31 10:26:35 sahlberg Exp $
12 * Ethereal - Network traffic analyzer
13 * By Gerald Combs <gerald@ethereal.com>
14 * Copyright 1998 Gerald Combs
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version 2
19 * of the License, or (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
36 #include <epan/packet.h>
42 #include "packet-per.h"
45 static int proto_per = -1;
46 static int hf_per_GeneralString_length = -1;
47 static int hf_per_extension_bit = -1;
48 static int hf_per_extension_present_bit = -1;
49 static int hf_per_choice_extension = -1;
50 static int hf_per_num_sequence_extensions = -1;
51 static int hf_per_small_number_bit = -1;
52 static int hf_per_optional_field_bit = -1;
53 static int hf_per_sequence_of_length = -1;
54 static int hf_per_object_identifier_length = -1;
55 static int hf_per_open_type_length = -1;
56 static int hf_per_octet_string_length = -1;
59 static gint ett_per_sequence_of_item = -1;
63 #define DEBUG_ENTRY(x) \
64 printf("#%d %s tvb:0x%08x\n",pinfo->fd->num,x,(int)tvb);
66 #define DEBUG_ENTRY(x) \
71 /* whether the PER helpers should put the internal PER fields into the tree
74 static guint display_internal_per_fields = TRUE;
78 static const true_false_string tfs_extension_present_bit = {
82 static const true_false_string tfs_extension_bit = {
83 "Extension bit is set",
84 "Extension bit is clear"
86 static const true_false_string tfs_small_number_bit = {
87 "The number is small, 0-63",
88 "The number is large, >63"
90 static const true_false_string tfs_optional_field_bit = {
99 /* this decodes and returns a length determinant according to 10.9 */
101 dissect_per_length_determinant(tvbuff_t *tvb, guint32 offset, packet_info *pinfo _U_, proto_tree *tree, int hf_index, guint32 *length)
112 offset=(offset&0xfffffff8)+8;
114 byte=tvb_get_guint8(tvb, offset>>3);
120 proto_tree_add_uint(tree, hf_index, tvb, (offset>>3)-1, 1, *length);
124 if((byte&0xc0)==0x80){
126 *length=((*length)<<8)+tvb_get_guint8(tvb, offset>>3);
129 proto_tree_add_uint(tree, hf_index, tvb, (offset>>3)-2, 2, *length);
133 NOT_DECODED_YET("10.9.3.8.1");
137 /* 10.6 normally small non-negative whole number */
139 dissect_per_normally_small_nonnegative_whole_number(tvbuff_t *tvb, guint32 offset, packet_info *pinfo, proto_tree *tree, int hf_index, guint32 *length)
141 gboolean small_number;
144 DEBUG_ENTRY("dissect_per_normally_small_nonnegative_whole_number");
149 offset=dissect_per_boolean(tvb, offset, pinfo, tree, hf_per_small_number_bit, &small_number, NULL);
155 offset=dissect_per_boolean(tvb, offset, pinfo, tree, -1, &small_number, NULL);
163 proto_tree_add_uint(tree, hf_index, tvb, (offset>>3)-1, 1, *length);
165 proto_tree_add_uint(tree, hf_index, tvb, (offset>>3), 1, *length);
172 offset=dissect_per_length_determinant(tvb, offset, pinfo, tree, hf_index, length);
179 /* this function reads a GeneralString */
180 /* currently based on pure guesswork since RFC2833 didnt tell me much
181 i guess that the PER encoding for this is a normally-small-whole-number
182 followed by a ascii string.
184 based on pure guesswork. it looks ok in the only capture i have where
185 there is a 1 byte general string encoded
188 dissect_per_GeneralString(tvbuff_t *tvb, guint32 offset, packet_info *pinfo, proto_tree *tree, int hf_index)
190 proto_tree *etr=NULL;
193 if(display_internal_per_fields){
197 offset=dissect_per_length_determinant(tvb, offset, pinfo, etr, hf_per_GeneralString_length, &length);
200 proto_tree_add_item(tree, hf_index, tvb, offset>>3, length, FALSE);
207 /* 19 this function dissects a sequence of */
209 dissect_per_sequence_of_helper(tvbuff_t *tvb, guint32 offset, packet_info *pinfo, proto_tree *tree, int (*func)(tvbuff_t *, int , packet_info *, proto_tree *), guint32 length)
213 DEBUG_ENTRY("dissect_per_sequence_of_helper");
214 for(i=0;i<length;i++){
215 guint32 lold_offset=offset;
219 litem=proto_tree_add_text(tree, tvb, offset>>3, 0, "Item %d", i);
220 ltree=proto_item_add_subtree(litem, ett_per_sequence_of_item);
222 offset=(*func)(tvb, offset, pinfo, ltree);
223 proto_item_set_len(litem, (offset>>3)!=(lold_offset>>3)?(offset>>3)-(lold_offset>>3):1);
229 dissect_per_sequence_of(tvbuff_t *tvb, guint32 offset, packet_info *pinfo, proto_tree *parent_tree, int hf_index, gint ett_index, int (*func)(tvbuff_t *, int , packet_info *, proto_tree *))
233 guint32 old_offset=offset;
236 DEBUG_ENTRY("dissect_per_sequence_of");
238 item=proto_tree_add_item(parent_tree, hf_index, tvb, offset>>3, 0, FALSE);
239 tree=proto_item_add_subtree(item, ett_index);
241 /* semi-constrained whole number for number of elements */
242 /* each element encoded as 10.9 */
243 offset=dissect_per_length_determinant(tvb, offset, pinfo, tree, hf_per_sequence_of_length, &length);
246 offset=dissect_per_sequence_of_helper(tvb, offset, pinfo, tree, func, length);
249 proto_item_set_len(item, (offset>>3)!=(old_offset>>3)?(offset>>3)-(old_offset>>3):1);
254 /* dissect a constrained IA5String that consists of the full ASCII set,
255 i.e. no FROM stuff limiting the alphabet
258 dissect_per_IA5String(tvbuff_t *tvb, guint32 offset, packet_info *pinfo, proto_tree *tree, int hf_index, int min_len, int max_len)
260 offset=dissect_per_octet_string(tvb, offset, pinfo, tree, hf_index, min_len, max_len);
265 /* XXX we dont do >64k length strings yet */
267 dissect_per_restricted_character_string(tvbuff_t *tvb, guint32 offset, packet_info *pinfo, proto_tree *tree, int hf_index, int min_len, int max_len, char *alphabet, int alphabet_length)
270 gboolean byte_aligned;
271 static char str[1024];
276 DEBUG_ENTRY("dissect_per_restricted_character_string");
277 /* xx.x if the length is 0 bytes there will be no encoding */
290 if(min_len!=max_len){
291 offset=dissect_per_constrained_integer(tvb, offset, pinfo,
292 tree, hf_per_octet_string_length, min_len, max_len,
293 &length, NULL, FALSE);
298 /* xx.x if length is fixed or constrained to be less than or equal to
299 two bytes, then it will not be byte aligned. */
301 if((min_len==max_len)&&(max_len<=2)){
310 offset=(offset&0xfffffff8)+8;
316 NOT_DECODED_YET("restricted char string too long");
320 /* 27.5.2 depending of the alphabet length, find how many bits
321 are used to encode each character */
323 if(alphabet_length<=2){
325 } else if(alphabet_length<=4){
327 } else if(alphabet_length<=8){
329 } else if(alphabet_length<=16){
331 } else if(alphabet_length<=32){
333 } else if(alphabet_length<=64){
335 } else if(alphabet_length<=128){
341 if(alphabet_length<=2){
343 } else if(alphabet_length<=4){
345 } else if(alphabet_length<=16){
352 for(char_pos=0;char_pos<length;char_pos++){
358 for(i=0;i<bits_per_char;i++){
359 offset=dissect_per_boolean(tvb, offset, pinfo, tree, -1, &bit, NULL);
362 str[char_pos]=alphabet[val];
365 proto_tree_add_string(tree, hf_index, tvb, (old_offset>>3), (offset>>3)-(old_offset>>3), str);
370 dissect_per_NumericString(tvbuff_t *tvb, guint32 offset, packet_info *pinfo, proto_tree *tree, int hf_index, int min_len, int max_len)
372 offset=dissect_per_restricted_character_string(tvb, offset, pinfo, tree, hf_index, min_len, max_len, " 0123456789", 11);
377 dissect_per_PrintableString(tvbuff_t *tvb, guint32 offset, packet_info *pinfo, proto_tree *tree, int hf_index, int min_len, int max_len)
379 offset=dissect_per_restricted_character_string(tvb, offset, pinfo, tree, hf_index, min_len, max_len, " '()+,-.*0123456789:=?ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", 74);
383 dissect_per_BMPString(tvbuff_t *tvb, guint32 offset, packet_info *pinfo, proto_tree *tree, int hf_index, int min_len, int max_len)
388 /* xx.x if the length is 0 bytes there will be no encoding */
401 if(min_len!=max_len){
402 offset=dissect_per_constrained_integer(tvb, offset, pinfo,
403 tree, hf_per_octet_string_length, min_len, max_len,
404 &length, NULL, FALSE);
408 /* align to byte boundary */
410 offset=(offset&0xfffffff8)+8;
414 NOT_DECODED_YET("BMPString too long");
418 str = tvb_fake_unicode(tvb, offset>>3, length, FALSE);
420 proto_tree_add_string(tree, hf_index, tvb, offset>>3, length*2, str);
422 offset+=(length<<3)*2;
428 /* this function dissects a constrained sequence of */
430 dissect_per_constrained_sequence_of(tvbuff_t *tvb, guint32 offset, packet_info *pinfo, proto_tree *parent_tree, int hf_index, gint ett_index, int (*func)(tvbuff_t *, int , packet_info *, proto_tree *), int min_len, int max_len)
434 guint32 old_offset=offset;
438 DEBUG_ENTRY("dissect_per_constrained_sequence_of");
439 item=proto_tree_add_item(parent_tree, hf_index, tvb, offset>>3, 0, FALSE);
440 tree=proto_item_add_subtree(item, ett_index);
442 /* 19.5 if min==max and min,max<64k ==> no length determinant */
443 if((min_len==max_len) && (min_len<65536)){
448 /* 19.6 ub>=64k or unset */
450 guint32 old_offset=offset;
451 /* semi-constrained whole number for number of elements */
452 /* each element encoded as 10.9 */
453 offset=dissect_per_length_determinant(tvb, offset, pinfo, tree, -1, &length);
455 proto_tree_add_uint(tree, hf_per_sequence_of_length, tvb, old_offset>>3, (offset>>3)!=(old_offset>>3)?(offset>>3)-(old_offset>>3):1, length);
459 /* constrained whole number for number of elements */
460 offset=dissect_per_constrained_integer(tvb, offset, pinfo,
461 tree, hf_per_sequence_of_length, min_len, max_len,
462 &length, NULL, FALSE);
467 offset=dissect_per_sequence_of_helper(tvb, offset, pinfo, tree, func, length);
470 proto_item_set_len(item, (offset>>3)!=(old_offset>>3)?(offset>>3)-(old_offset>>3):1);
474 /* this function dissects a constrained set of */
476 dissect_per_constrained_set_of(tvbuff_t *tvb, guint32 offset, packet_info *pinfo, proto_tree *parent_tree, int hf_index, gint ett_index, int (*func)(tvbuff_t *, int , packet_info *, proto_tree *), int min_len, int max_len)
478 /* for basic-per a set-of is encoded in the same way as a sequence-of */
479 DEBUG_ENTRY("dissect_per_constrained_set_of");
480 offset=dissect_per_constrained_sequence_of(tvb, offset, pinfo, parent_tree, hf_index, ett_index, func, min_len, max_len);
489 /* this function dissects a set of */
491 dissect_per_set_of(tvbuff_t *tvb, guint32 offset, packet_info *pinfo, proto_tree *parent_tree, int hf_index, gint ett_index, int (*func)(tvbuff_t *, int , packet_info *, proto_tree *))
493 /* for basic-per a set-of is encoded in the same way as a sequence-of */
494 DEBUG_ENTRY("dissect_per_set_of");
495 offset=dissect_per_sequence_of(tvb, offset, pinfo, parent_tree, hf_index, ett_index, func);
502 /* this function reads a OBJECT IDENTIFIER */
504 dissect_per_object_identifier(tvbuff_t *tvb, guint32 offset, packet_info *pinfo _U_, proto_tree *tree, int hf_index)
510 proto_tree *etr=NULL;
512 DEBUG_ENTRY("dissect_per_object_identifier");
514 if(display_internal_per_fields){
518 /* first byte is the count and it is byte aligned */
520 offset=(offset&0xfffffff8)+8;
522 count=tvb_get_guint8(tvb, offset>>3);
525 proto_tree_add_uint(etr, hf_per_object_identifier_length, tvb, offset>>3, 1, count);
529 for(i=0,strp=str;i<count;i++){
530 byte=tvb_get_guint8(tvb,offset>>3);
534 NOT_DECODED_YET("too long octet_string");
540 /* the first byte contains the first two object identifier components */
542 strp+=sprintf(strp,"0.%d",byte);
544 strp+=sprintf(strp,"1.%d",byte-40);
546 strp+=sprintf(strp,"2.%d",byte-80);
551 value=(value<<7)|(byte&0x7f);
556 strp+=sprintf(strp,".%d",value);
561 proto_tree_add_string(tree, hf_index, tvb, (offset>>3)-count, count, str);
569 /* this function reads a single bit */
571 dissect_per_boolean(tvbuff_t *tvb, guint32 offset, packet_info *pinfo _U_, proto_tree *tree, int hf_index, gboolean *bool, proto_item **item)
575 header_field_info *hfi;
578 DEBUG_ENTRY("dissect_per_boolean");
580 ch=tvb_get_guint8(tvb, offset>>3);
581 mask=1<<(7-(offset&0x07));
589 hfi = proto_registrar_get_nth(hf_index);
590 sprintf(str,"%s: %c%c%c%c %c%c%c%c %s",
592 mask&0x80?'0'+value:'.',
593 mask&0x40?'0'+value:'.',
594 mask&0x20?'0'+value:'.',
595 mask&0x10?'0'+value:'.',
596 mask&0x08?'0'+value:'.',
597 mask&0x04?'0'+value:'.',
598 mask&0x02?'0'+value:'.',
599 mask&0x01?'0'+value:'.',
600 value?"Bit is set":"Bit is clear"
602 it=proto_tree_add_boolean_format(tree, hf_index, tvb, offset>>3, 1, value, str);
617 /* we currently only handle integers up to 32 bits in length. */
619 dissect_per_integer(tvbuff_t *tvb, guint32 offset, packet_info *pinfo, proto_tree *tree, int hf_index, gint32 *value, proto_item **item)
626 offset=dissect_per_length_determinant(tvb, offset, pinfo, tree, -1, &length);
629 NOT_DECODED_YET("too long integer");
634 for(i=0;i<length;i++){
636 if(tvb_get_guint8(tvb, offset>>3)&0x80){
637 /* negative number */
640 /* positive number */
644 val=(val<<8)|tvb_get_guint8(tvb,offset>>3);
647 it=proto_tree_add_int(tree, hf_index, tvb, (offset>>3)-(length+1), length+1, val);
660 /* this function reads a constrained integer with or without a
661 PER visible extension marker present
663 has_extension==TRUE would map to asn constructs such as:
664 rfc-number INTEGER (1..32768, ...)
665 while has_extension==FALSE would map to:
666 t35CountryCode INTEGER (0..255)
668 it only handles integers that fit inside a 32 bit integer
674 10.5.6 unaligned version
675 10.5.7 aligned version
676 10.5.7.1 decoding of 0-255 1-8 bits
677 10.5.7.2 decoding og 0-256 8 bits
678 10.5.7.3 decoding of 0-65535 16 bits
682 dissect_per_constrained_integer(tvbuff_t *tvb, guint32 offset, packet_info *pinfo, proto_tree *tree, int hf_index, guint32 min, guint32 max, guint32 *value, proto_item **item, gboolean has_extension)
686 header_field_info *hfi;
691 DEBUG_ENTRY("dissect_per_constrained_integer");
693 gboolean extension_present;
694 offset=dissect_per_boolean(tvb, offset, pinfo, tree, -1, &extension_present, NULL);
695 if(extension_present){
696 offset=dissect_per_integer(tvb, offset, pinfo, tree,
703 hfi = proto_registrar_get_nth(hf_index);
707 /* just set range really big so it will fall through
708 to the bottom of the encoding */
719 it=proto_tree_add_uint_format(tree, hf_index, tvb, offset>>3, 0, min, "%s: %d", hfi->name, min);
742 } else if(range<=16){
744 } else if(range<=32){
746 } else if(range<=64){
748 } else if(range<=128){
750 } else if(range<=256){
753 /* prepare the string */
754 sprintf(str, "%s: ", hfi->name);
755 for(bit=0;bit<((int)(offset&0x07));bit++){
761 /* read the bits for the int */
762 for(i=0;i<num_bits;i++){
771 offset=dissect_per_boolean(tvb, offset, pinfo, tree, -1, &tmp, NULL);
788 it=proto_tree_add_uint_format(tree, hf_index, tvb, (offset-num_bits)>>3, length, val, "%s : %s (%d)", str, val_to_str(val, hfi->strings, "Unknown(%d)"),val);
790 it=proto_tree_add_uint(tree, hf_index, tvb, (offset-num_bits)>>3, length, val);
799 } else if(range==256){
804 /* in the aligned case, align to byte boundary */
806 offset=(offset&0xfffffff8)+8;
808 val=tvb_get_guint8(tvb, offset>>3);
812 it=proto_tree_add_uint(tree, hf_index, tvb, (offset>>3)-1, 1, val);
820 } else if(range<=65536){
825 /* in the aligned case, align to byte boundary */
827 offset=(offset&0xfffffff8)+8;
829 val=tvb_get_guint8(tvb, offset>>3);
832 val|=tvb_get_guint8(tvb, offset>>3);
836 it=proto_tree_add_uint(tree, hf_index, tvb, (offset>>3)-2, 2, val);
850 offset=dissect_per_boolean(tvb, offset, pinfo, tree, -1, &bit, NULL);
852 offset=dissect_per_boolean(tvb, offset, pinfo, tree, -1, &bit, NULL);
853 num_bytes=(num_bytes<<1)|bit;
855 num_bytes++; /* lower bound for length determinant is 1 */
859 offset=(offset&0xfffffff8)+8;
862 for(i=0;i<num_bytes;i++){
863 val=(val<<8)|tvb_get_guint8(tvb,offset>>3);
867 it=proto_tree_add_uint(tree, hf_index, tvb, (offset>>3)-(num_bytes+1), num_bytes+1, val);
877 NOT_DECODED_YET("10.5");
881 /* this functions decodes a CHOICE
882 it can only handle CHOICE INDEX values that fits inside a 32 bit integer.
889 22.7 extension marker == 0
890 22.8 extension marker == 1
893 dissect_per_choice(tvbuff_t *tvb, guint32 offset, packet_info *pinfo, proto_tree *tree, int hf_index, gint ett_index, per_choice_t *choice, char *name, guint32 *value)
895 gboolean extension_present, extension_flag;
896 int extension_root_entries;
897 guint32 choice_index;
901 guint32 old_offset=offset;
903 DEBUG_ENTRY("dissect_per_choice");
905 it=proto_tree_add_text(tree, tvb, offset>>3, 0, name);
906 tr=proto_item_add_subtree(it, ett_index);
909 /* first check if there should be an extension bit for this CHOICE.
910 we do this by just checking the first choice arm
912 if(choice[0].extension==NO_EXTENSIONS){
916 /* will be placed called again below to place it in the tree */
917 offset=dissect_per_boolean(tvb, offset, pinfo, tr, hf_per_extension_bit, &extension_flag, NULL);
920 /* count the number of entries in the extension_root */
921 extension_root_entries=0;
922 for(i=0;choice[i].name;i++){
923 switch(choice[i].extension){
926 extension_root_entries++;
931 if( (!extension_present)
932 || (extension_present && (extension_flag==0)) ){
933 guint32 choice_offset=offset;
934 proto_tree *choicetree;
935 proto_item *choiceitem;
936 proto_tree *etr=NULL;
940 /*qqq make it similar to the section below instead */
941 offset=dissect_per_constrained_integer(tvb, offset, pinfo,
942 tr, hf_index, 0, extension_root_entries-1,
943 &choice_index, &choiceitem, FALSE);
948 choicetree=proto_item_add_subtree(choiceitem, ett_index);
950 if(display_internal_per_fields){
954 /* find and call the appropriate callback */
955 for(i=0;choice[i].name;i++){
956 if(choice[i].value==(int)choice_index){
958 offset=choice[i].func(tvb, offset, pinfo, choicetree);
961 NOT_DECODED_YET(choice[i].name);
966 proto_item_set_len(choiceitem, (offset>>3)!=(choice_offset>>3)?(offset>>3)-(choice_offset>>3):1);
970 guint32 choice_offset;
971 proto_tree *choicetree;
972 proto_item *choiceitem;
973 proto_tree *etr=NULL;
975 if(display_internal_per_fields){
980 offset=dissect_per_normally_small_nonnegative_whole_number(tvb, offset, pinfo, etr, hf_per_choice_extension, &choice_index);
981 offset=dissect_per_length_determinant(tvb, offset, pinfo, etr, hf_per_open_type_length, &length);
984 choice_offset=offset;
985 choiceitem=proto_tree_add_text(tr, tvb, offset>>3, 0, "Choice");
986 choicetree=proto_item_add_subtree(choiceitem, ett_index);
989 for(i=0;choice[i].name;i++){
990 if(choice[i].extension==NOT_EXTENSION_ROOT){
1000 *value=choice_index;
1005 /* if we dont know how to decode this one, just step offset to the next structure */
1007 NOT_DECODED_YET("unknown choice extension");
1011 proto_item_set_text(choiceitem, choice[index].name);
1012 new_offset=choice[index].func(tvb, offset, pinfo, choicetree);
1014 if((new_offset>(offset+(length*8)))||((new_offset+8)<(offset+length*8))){
1015 printf("new_offset:%d offset:%d length*8:%d\n",new_offset,offset,length*8);
1016 /* g_assert_not_reached();*/
1021 proto_item_set_len(choiceitem, (offset>>3)!=(choice_offset>>3)?(offset>>3)-(choice_offset>>3):1);
1024 proto_item_set_len(it, (offset>>3)!=(old_offset>>3)?(offset>>3)-(old_offset>>3):1);
1030 index_get_optional_name(per_sequence_t *sequence, int index)
1034 for(i=0;sequence[i].name;i++){
1035 if((sequence[i].extension!=NOT_EXTENSION_ROOT)&&(sequence[i].optional==OPTIONAL)){
1037 return sequence[i].name;
1042 return "<unknown type>";
1046 index_get_extension_name(per_sequence_t *sequence, int index)
1050 for(i=0;sequence[i].name;i++){
1051 if(sequence[i].extension==NOT_EXTENSION_ROOT){
1053 return sequence[i].name;
1058 return "<unknown type>";
1061 /* this functions decodes a SEQUENCE
1062 it can only handle SEQUENCES with at most 32 DEFAULT or OPTIONAL fields
1064 18.2 optinal/default items in root
1065 18.3 we ignore the case where n>64K
1066 18.4 the root sequence
1074 dissect_per_sequence(tvbuff_t *tvb, guint32 offset, packet_info *pinfo, proto_tree *parent_tree, int hf_index, gint ett_index, per_sequence_t *sequence)
1076 gboolean extension_present, extension_flag, optional_field_flag;
1079 guint32 old_offset=offset;
1080 guint32 i, num_opts;
1081 guint32 optional_mask;
1084 DEBUG_ENTRY("dissect_per_sequence");
1086 item=proto_tree_add_item(parent_tree, hf_index, tvb, offset>>3, 0, FALSE);
1087 tree=proto_item_add_subtree(item, ett_index);
1090 /* first check if there should be an extension bit for this CHOICE.
1091 we do this by just checking the first choice arm
1095 if(sequence[0].extension==NO_EXTENSIONS){
1096 extension_present=0;
1098 proto_tree *etr=NULL;
1100 if(display_internal_per_fields){
1103 extension_present=1;
1104 offset=dissect_per_boolean(tvb, offset, pinfo, etr, hf_per_extension_bit, &extension_flag, NULL);
1108 for(i=0;sequence[i].name;i++){
1109 if((sequence[i].extension!=NOT_EXTENSION_ROOT)&&(sequence[i].optional==OPTIONAL)){
1115 for(i=0;i<num_opts;i++){
1116 proto_item *it=NULL;
1117 proto_tree *etr=NULL;
1118 if(display_internal_per_fields){
1121 offset=dissect_per_boolean(tvb, offset, pinfo, etr, hf_per_optional_field_bit, &optional_field_flag, &it);
1123 if(optional_field_flag){
1124 optional_mask|=0x01;
1127 proto_item_append_text(it, " (%s %s present)",
1128 index_get_optional_name(sequence, i),
1129 optional_field_flag?"is":"is NOT"
1136 for(i=0;sequence[i].name;i++){
1137 if( (sequence[i].extension==NO_EXTENSIONS)
1138 || (sequence[i].extension==EXTENSION_ROOT) ){
1139 if(sequence[i].optional==OPTIONAL){
1140 gboolean is_present;
1141 is_present=(1<<(num_opts-1))&optional_mask;
1147 if(sequence[i].func){
1148 offset=sequence[i].func(tvb, offset, pinfo, tree);
1150 NOT_DECODED_YET(sequence[i].name);
1157 gboolean extension_bit;
1158 guint32 num_known_extensions;
1159 guint32 num_extensions;
1160 guint32 extension_mask;
1161 proto_tree *etr=NULL;
1162 proto_item *it=NULL;
1164 if(display_internal_per_fields){
1168 offset=dissect_per_normally_small_nonnegative_whole_number(tvb, offset, pinfo, etr, hf_per_num_sequence_extensions, &num_extensions);
1169 /* the X.691 standard is VERY unclear here.
1170 there is no mention that the lower bound lb for this
1171 (apparently) semiconstrained value is 1,
1172 apart from the NOTE: comment in 18.8 that this value can
1174 In my book, there is a semantic difference between having
1175 a comment that says that the value can not be zero
1176 and stating that the lb is 1.
1177 I dont know if this is right or not but it makes
1178 some of the very few captures I have decode properly.
1180 It could also be that the captures I have are generated by
1181 a broken implementation.
1182 If this is wrong and you dont report it as a bug
1183 then it wont get fixed!
1188 for(i=0;i<num_extensions;i++){
1189 offset=dissect_per_boolean(tvb, offset, pinfo, etr, hf_per_extension_present_bit, &extension_bit, &it);
1190 extension_mask=(extension_mask<<1)|extension_bit;
1192 proto_item_append_text(it, " (%s %s present)",
1193 index_get_extension_name(sequence, i),
1194 extension_bit?"is":"is NOT"
1200 /* find how many extensions we know about */
1201 num_known_extensions=0;
1202 for(i=0;sequence[i].name;i++){
1203 if(sequence[i].extension==NOT_EXTENSION_ROOT){
1204 num_known_extensions++;
1208 /* decode the extensions one by one */
1209 for(i=0;i<num_extensions;i++){
1212 guint32 extension_index;
1215 if(!((1L<<(num_extensions-1-i))&extension_mask)){
1216 /* this extension is not encoded in this PDU */
1220 offset=dissect_per_length_determinant(tvb, offset, pinfo, etr, hf_per_open_type_length, &length);
1222 if(i>=num_known_extensions){
1223 /* we dont know how to decode this extension */
1225 NOT_DECODED_YET("unknown sequence extension");
1230 for(j=0,k=0;sequence[j].name;j++){
1231 if(sequence[j].extension==NOT_EXTENSION_ROOT){
1240 if(sequence[extension_index].func){
1241 new_offset=sequence[extension_index].func(tvb, offset, pinfo, tree);
1243 NOT_DECODED_YET(sequence[extension_index].name);
1251 proto_item_set_len(item, (offset>>3)!=(old_offset>>3)?(offset>>3)-(old_offset>>3):1);
1259 /* this fucntion dissects an OCTET STRING
1269 max_len or min_len == -1 means there is no lower/upper constraint
1271 hf_index can either be a FT_BYTES or an FT_STRING
1274 dissect_per_octet_string(tvbuff_t *tvb, guint32 offset, packet_info *pinfo, proto_tree *tree, int hf_index, int min_len, int max_len)
1277 header_field_info *hfi;
1279 hfi=proto_registrar_get_nth(hf_index);
1281 DEBUG_ENTRY("dissect_per_octet_string");
1282 /* 16.5 if the length is 0 bytes there will be no encoding */
1292 /* 16.6 if length is fixed and less than or equal to two bytes*/
1293 if((min_len==max_len)&&(max_len<=2)){
1294 static char bytes[4];
1295 guint32 i, old_offset=offset;
1299 offset=dissect_per_boolean(tvb, offset, pinfo, tree, -1, &bit, NULL);
1300 bytes[0]=(bytes[0]<<1)|bit;
1304 offset=dissect_per_boolean(tvb, offset, pinfo, tree, -1, &bit, NULL);
1305 bytes[1]=(bytes[1]<<1)|bit;
1309 if(hfi->type==FT_STRING){
1310 proto_tree_add_string(tree, hf_index, tvb, old_offset>>3, min_len+(offset&0x07)?1:0, bytes);
1312 proto_tree_add_bytes(tree, hf_index, tvb, old_offset>>3, min_len+(offset&0x07)?1:0, bytes);
1318 /* aligne to byte */
1320 offset=(offset&0xfffffff8)+8;
1323 /* 16.7 if length is fixed and less than to 64k*/
1324 if((min_len==max_len)&&(min_len<65536)){
1325 if(hfi->type==FT_STRING){
1326 proto_tree_add_string(tree, hf_index, tvb, offset>>3, min_len, tvb_get_ptr(tvb, offset>>3, min_len));
1328 proto_tree_add_bytes(tree, hf_index, tvb, offset>>3, min_len, tvb_get_ptr(tvb, offset>>3, min_len));
1336 offset=dissect_per_constrained_integer(tvb, offset, pinfo,
1337 tree, hf_per_octet_string_length, min_len, max_len,
1338 &length, NULL, FALSE);
1340 offset=dissect_per_length_determinant(tvb, offset, pinfo, tree, hf_per_octet_string_length, &length);
1343 if(hfi->type==FT_STRING){
1344 proto_tree_add_string(tree, hf_index, tvb, offset>>3, length, tvb_get_ptr(tvb, offset>>3, length));
1346 proto_tree_add_bytes(tree, hf_index, tvb, offset>>3, length, tvb_get_ptr(tvb, offset>>3, length));
1357 proto_register_per(void)
1359 static hf_register_info hf[] =
1361 { &hf_per_num_sequence_extensions,
1362 { "Number of Sequence Extensions", "per.num_sequence_extensions", FT_UINT32, BASE_DEC,
1363 NULL, 0, "Number of extensions encoded in this sequence", HFILL }},
1364 { &hf_per_choice_extension,
1365 { "Choice Extension", "per.choice_extension", FT_UINT32, BASE_DEC,
1366 NULL, 0, "Which extension of the Choice is encoded", HFILL }},
1367 { &hf_per_GeneralString_length,
1368 { "GeneralString Length", "per.generalstring_length", FT_UINT32, BASE_DEC,
1369 NULL, 0, "Length of the GeneralString", HFILL }},
1370 { &hf_per_extension_bit,
1371 { "Extension Bit", "per.extension_bit", FT_BOOLEAN, 8,
1372 TFS(&tfs_extension_bit), 0x01, "The extension bit of an aggregate", HFILL }},
1373 { &hf_per_extension_present_bit,
1374 { "Extension Present Bit", "per.extension_present_bit", FT_BOOLEAN, 8,
1375 TFS(&tfs_extension_present_bit), 0x01, "Whether this optional extension is present or not", HFILL }},
1376 { &hf_per_small_number_bit,
1377 { "Small Number Bit", "per.small_number_bit", FT_BOOLEAN, 8,
1378 TFS(&tfs_small_number_bit), 0x01, "The small number bit for a section 10.6 integer", HFILL }},
1379 { &hf_per_optional_field_bit,
1380 { "Optional Field Bit", "per.optional_field_bit", FT_BOOLEAN, 8,
1381 TFS(&tfs_optional_field_bit), 0x01, "This bit specifies the presence/absence of an optional field", HFILL }},
1382 { &hf_per_sequence_of_length,
1383 { "Sequence-Of Length", "per.sequence_of_length", FT_UINT32, BASE_DEC,
1384 NULL, 0, "Number of items in the Sequence Of", HFILL }},
1385 { &hf_per_object_identifier_length,
1386 { "Object Length", "per.object_length", FT_UINT32, BASE_DEC,
1387 NULL, 0, "Length of the object identifier", HFILL }},
1388 { &hf_per_open_type_length,
1389 { "Open Type Length", "per.open_type_length", FT_UINT32, BASE_DEC,
1390 NULL, 0, "Length of an open type encoding", HFILL }},
1391 { &hf_per_octet_string_length,
1392 { "Octet String Length", "per.octet_string_length", FT_UINT32, BASE_DEC,
1393 NULL, 0, "Number of bytes in the Octet String", HFILL }},
1396 static gint *ett[] =
1398 &ett_per_sequence_of_item
1400 module_t *per_module;
1402 proto_per = proto_register_protocol("Packet Encoding Rules (ASN.1 X.691)", "PER", "per");
1403 proto_register_field_array(proto_per, hf, array_length(hf));
1404 proto_register_subtree_array(ett, array_length(ett));
1406 per_module = prefs_register_protocol(proto_per, NULL);
1407 prefs_register_bool_preference(per_module, "display_internal_per_fields",
1408 "Display the internal PER fields in the tree",
1409 "Whether the dissector should put the internal PER data in the tree or if it should hide it",
1410 &display_internal_per_fields);
1415 proto_reg_handoff_per(void)