Move some code (including the optional objects) into libwsutil
[obnox/wireshark/wip.git] / wiretap / ber.c
1 /* ber.c
2  *
3  * Basic Encoding Rules (BER) file reading
4  *
5  * $Id$
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This 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.
16  *
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <errno.h>
27
28 #ifdef HAVE_SYS_STAT_H
29 #include <sys/stat.h>
30 #endif
31
32 #include "wtap-int.h"
33 #include "file_wrappers.h"
34 #include "buffer.h"
35 #include "ber.h"
36
37
38 #define BER_CLASS_UNI   0
39 #define BER_CLASS_APP   1
40 #define BER_CLASS_CON   2
41
42 #define BER_UNI_TAG_SEQ 16      /* SEQUENCE, SEQUENCE OF */
43 #define BER_UNI_TAG_SET 17      /* SET, SET OF */
44
45 static gboolean ber_read(wtap *wth, int *err, gchar **err_info, gint64 *data_offset)
46 {
47   guint8 *buf;
48   gint64 file_size;
49   int packet_size;
50   struct stat statb;
51
52   *err = 0;
53
54   /* there is only ever one packet */
55   if(wth->data_offset)
56     return FALSE;
57
58   *data_offset = wth->data_offset;
59
60   if ((file_size = wtap_file_size(wth, err)) == -1)
61     return FALSE;
62
63   if (file_size > WTAP_MAX_PACKET_SIZE) {
64     /*
65      * Probably a corrupt capture file; don't blow up trying
66      * to allocate space for an immensely-large packet.
67      */
68     *err = WTAP_ERR_BAD_RECORD;
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);
71     return FALSE;
72   }
73   packet_size = (int)file_size;
74
75   buffer_assure_space(wth->frame_buffer, packet_size);
76   buf = buffer_start_ptr(wth->frame_buffer);
77
78   wtap_file_read_expected_bytes(buf, packet_size, wth->fh, err);
79
80   wth->data_offset += packet_size;
81
82   wth->phdr.caplen = packet_size;
83   wth->phdr.len = packet_size;
84
85   if (fstat(wth->fd, &statb) == -1) {
86     *err = errno;
87     return FALSE;
88   }
89
90   wth->phdr.ts.secs = statb.st_mtime;
91   wth->phdr.ts.nsecs = 0;
92
93   return TRUE;
94 }
95
96 static gboolean ber_seek_read(wtap *wth, gint64 seek_off, union wtap_pseudo_header *pseudo_header _U_,
97                               guint8 *pd, int length, int *err, gchar **err_info _U_)
98 {
99   int packet_size = length;
100
101   /* there is only one packet */
102   if(seek_off > 0) {
103     *err = 0;
104     return FALSE;
105   }
106
107   if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
108     return FALSE;
109
110   wtap_file_read_expected_bytes(pd, packet_size, wth->random_fh, err);
111
112   return TRUE;
113 }
114
115 int ber_open(wtap *wth, int *err, gchar **err_info _U_)
116 {
117 #define BER_BYTES_TO_CHECK 8
118   guint8 bytes[BER_BYTES_TO_CHECK];
119   int bytes_read;
120   guint8 ber_id;
121   gint8 ber_class;
122   gint8 ber_tag;
123   gboolean ber_pc;
124   guint8 oct, nlb = 0;
125   int len = 0;
126   gint64 file_size;
127   int offset = 0, i;
128
129   bytes_read = file_read(&bytes, 1, BER_BYTES_TO_CHECK, wth->fh);
130   if (bytes_read != BER_BYTES_TO_CHECK) {
131     *err = file_error(wth->fh);
132     return (*err != 0) ? -1 : 0;
133   }
134
135   ber_id = bytes[offset++];
136
137   ber_class = (ber_id>>6) & 0x03;
138   ber_pc = (ber_id>>5) & 0x01;
139   ber_tag = ber_id & 0x1F;
140
141   /* it must be constructed and either a SET or a SEQUENCE */
142   /* or a CONTEXT less than 32 (arbitrary) */
143   /* XXX: do we also want to allow APPLICATION */
144   if(!(ber_pc &&
145        (((ber_class == BER_CLASS_UNI) && ((ber_tag == BER_UNI_TAG_SET) || (ber_tag == BER_UNI_TAG_SEQ))) ||
146         ((ber_class == BER_CLASS_CON) && (ber_tag < 32)))))
147     return 0;
148
149   /* now check the length */
150   oct = bytes[offset++];
151
152   if(oct != 0x80) {
153     /* not indefinite length encoded */
154
155     if(!(oct & 0x80))
156       /* length fits into a single byte */
157       len = oct;
158     else {
159       nlb = oct & 0x7F; /* number of length bytes */
160
161       if((nlb > 0) && (nlb <= (BER_BYTES_TO_CHECK - 2))) {
162         /* not indefinite length and we have read enough bytes to compute the length */
163         i = nlb;
164         while(i--) {
165           oct = bytes[offset++];
166           len = (len<<8) + oct;
167         }
168       }
169     }
170
171     len += (2 + nlb); /* add back Tag and Length bytes */
172     file_size = wtap_file_size(wth, err);
173
174     if(len != file_size) {
175       return 0; /* not ASN.1 */
176     }
177   } else {
178     /* Indefinite length encoded - assume it is BER */
179   }
180
181   /* seek back to the start of the file  */
182   if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
183     return -1;
184
185   wth->file_type = WTAP_FILE_BER;
186   wth->file_encap = WTAP_ENCAP_BER;
187   wth->snapshot_length = 0;
188
189   wth->subtype_read = ber_read;
190   wth->subtype_seek_read = ber_seek_read;
191   wth->tsprecision = WTAP_FILE_TSPREC_SEC;
192
193   return 1;
194 }