3 * Basic Encoding Rules (BER) file reading
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #ifdef HAVE_SYS_STAT_H
31 #include "file_wrappers.h"
36 #define BER_CLASS_UNI 0
37 #define BER_CLASS_APP 1
38 #define BER_CLASS_CON 2
40 #define BER_UNI_TAG_SEQ 16 /* SEQUENCE, SEQUENCE OF */
41 #define BER_UNI_TAG_SET 17 /* SET, SET OF */
43 static gboolean ber_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
52 offset = file_tell(wth->fh);
54 /* there is only ever one packet */
58 *data_offset = offset;
60 if ((file_size = wtap_file_size(wth, err)) == -1)
63 if (file_size > WTAP_MAX_PACKET_SIZE) {
65 * Probably a corrupt capture file; don't blow up trying
66 * to allocate space for an immensely-large packet.
68 *err = WTAP_ERR_BAD_FILE;
69 *err_info = g_strdup_printf("ber: File has %" G_GINT64_MODIFIER "d-byte packet, bigger than maximum of %u",
70 file_size, WTAP_MAX_PACKET_SIZE);
73 packet_size = (int)file_size;
75 buffer_assure_space(wth->frame_buffer, packet_size);
76 buf = buffer_start_ptr(wth->frame_buffer);
78 wtap_file_read_expected_bytes(buf, packet_size, wth->fh, err, err_info);
80 wth->phdr.presence_flags = 0; /* yes, we have no bananas^Wtime stamp */
82 wth->phdr.caplen = packet_size;
83 wth->phdr.len = packet_size;
85 wth->phdr.ts.secs = 0;
86 wth->phdr.ts.nsecs = 0;
91 static gboolean ber_seek_read(wtap *wth, gint64 seek_off, union wtap_pseudo_header *pseudo_header _U_,
92 guint8 *pd, int length, int *err, gchar **err_info)
94 int packet_size = length;
96 /* there is only one packet */
102 if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
105 wtap_file_read_expected_bytes(pd, packet_size, wth->random_fh, err, err_info);
110 int ber_open(wtap *wth, int *err, gchar **err_info)
112 #define BER_BYTES_TO_CHECK 8
113 guint8 bytes[BER_BYTES_TO_CHECK];
124 bytes_read = file_read(&bytes, BER_BYTES_TO_CHECK, wth->fh);
125 if (bytes_read != BER_BYTES_TO_CHECK) {
126 *err = file_error(wth->fh, err_info);
127 return (*err != 0) ? -1 : 0;
130 ber_id = bytes[offset++];
132 ber_class = (ber_id>>6) & 0x03;
133 ber_pc = (ber_id>>5) & 0x01;
134 ber_tag = ber_id & 0x1F;
136 /* it must be constructed and either a SET or a SEQUENCE */
137 /* or a CONTEXT less than 32 (arbitrary) */
138 /* XXX: do we also want to allow APPLICATION */
140 (((ber_class == BER_CLASS_UNI) && ((ber_tag == BER_UNI_TAG_SET) || (ber_tag == BER_UNI_TAG_SEQ))) ||
141 ((ber_class == BER_CLASS_CON) && (ber_tag < 32)))))
144 /* now check the length */
145 oct = bytes[offset++];
148 /* not indefinite length encoded */
151 /* length fits into a single byte */
154 nlb = oct & 0x7F; /* number of length bytes */
156 if((nlb > 0) && (nlb <= (BER_BYTES_TO_CHECK - 2))) {
157 /* not indefinite length and we have read enough bytes to compute the length */
160 oct = bytes[offset++];
161 len = (len<<8) + oct;
166 len += (2 + nlb); /* add back Tag and Length bytes */
167 file_size = wtap_file_size(wth, err);
169 if(len != file_size) {
170 return 0; /* not ASN.1 */
173 /* Indefinite length encoded - assume it is BER */
176 /* seek back to the start of the file */
177 if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
180 wth->file_type = WTAP_FILE_BER;
181 wth->file_encap = WTAP_ENCAP_BER;
182 wth->snapshot_length = 0;
184 wth->subtype_read = ber_read;
185 wth->subtype_seek_read = ber_seek_read;
186 wth->tsprecision = WTAP_FILE_TSPREC_SEC;