Drop main() prototype. Syncs with NetBSD-8
[minix.git] / external / bsd / libpcap / dist / bpf_image.c
blob8ea57741a87871368b3bbb9e2ffeb62c332905cc
1 /* $NetBSD: bpf_image.c,v 1.2 2014/11/19 19:33:30 christos Exp $ */
3 /*
4 * Copyright (c) 1990, 1991, 1992, 1994, 1995, 1996
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that: (1) source code distributions
9 * retain the above copyright notice and this paragraph in its entirety, (2)
10 * distributions including binary code include the above copyright notice and
11 * this paragraph in its entirety in the documentation or other materials
12 * provided with the distribution, and (3) all advertising materials mentioning
13 * features or use of this software display the following acknowledgement:
14 * ``This product includes software developed by the University of California,
15 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
16 * the University nor the names of its contributors may be used to endorse
17 * or promote products derived from this software without specific prior
18 * written permission.
19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
20 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
24 #include <sys/cdefs.h>
25 __RCSID("$NetBSD: bpf_image.c,v 1.2 2014/11/19 19:33:30 christos Exp $");
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
31 #ifdef WIN32
32 #include <pcap-stdinc.h>
33 #else /* WIN32 */
34 #if HAVE_INTTYPES_H
35 #include <inttypes.h>
36 #elif HAVE_STDINT_H
37 #include <stdint.h>
38 #endif
39 #ifdef HAVE_SYS_BITYPES_H
40 #include <sys/bitypes.h>
41 #endif
42 #include <sys/types.h>
43 #endif /* WIN32 */
45 #include <stdio.h>
46 #include <string.h>
48 #include "pcap-int.h"
50 #ifdef HAVE_OS_PROTO_H
51 #include "os-proto.h"
52 #endif
54 char *
55 bpf_image(p, n)
56 const struct bpf_insn *p;
57 int n;
59 int v;
60 const char *fmt, *op;
61 static char image[256];
62 char operand[64];
64 v = p->k;
65 switch (p->code) {
67 default:
68 op = "unimp";
69 fmt = "0x%x";
70 v = p->code;
71 break;
73 case BPF_RET|BPF_K:
74 op = "ret";
75 fmt = "#%d";
76 break;
78 case BPF_RET|BPF_A:
79 op = "ret";
80 fmt = "";
81 break;
83 case BPF_LD|BPF_W|BPF_ABS:
84 op = "ld";
85 fmt = "[%d]";
86 break;
88 case BPF_LD|BPF_H|BPF_ABS:
89 op = "ldh";
90 fmt = "[%d]";
91 break;
93 case BPF_LD|BPF_B|BPF_ABS:
94 op = "ldb";
95 fmt = "[%d]";
96 break;
98 case BPF_LD|BPF_W|BPF_LEN:
99 op = "ld";
100 fmt = "#pktlen";
101 break;
103 case BPF_LD|BPF_W|BPF_IND:
104 op = "ld";
105 fmt = "[x + %d]";
106 break;
108 case BPF_LD|BPF_H|BPF_IND:
109 op = "ldh";
110 fmt = "[x + %d]";
111 break;
113 case BPF_LD|BPF_B|BPF_IND:
114 op = "ldb";
115 fmt = "[x + %d]";
116 break;
118 case BPF_LD|BPF_IMM:
119 op = "ld";
120 fmt = "#0x%x";
121 break;
123 case BPF_LDX|BPF_IMM:
124 op = "ldx";
125 fmt = "#0x%x";
126 break;
128 case BPF_LDX|BPF_MSH|BPF_B:
129 op = "ldxb";
130 fmt = "4*([%d]&0xf)";
131 break;
133 case BPF_LD|BPF_MEM:
134 op = "ld";
135 fmt = "M[%d]";
136 break;
138 case BPF_LDX|BPF_MEM:
139 op = "ldx";
140 fmt = "M[%d]";
141 break;
143 case BPF_ST:
144 op = "st";
145 fmt = "M[%d]";
146 break;
148 case BPF_STX:
149 op = "stx";
150 fmt = "M[%d]";
151 break;
153 case BPF_JMP|BPF_JA:
154 op = "ja";
155 fmt = "%d";
156 v = n + 1 + p->k;
157 break;
159 case BPF_JMP|BPF_JGT|BPF_K:
160 op = "jgt";
161 fmt = "#0x%x";
162 break;
164 case BPF_JMP|BPF_JGE|BPF_K:
165 op = "jge";
166 fmt = "#0x%x";
167 break;
169 case BPF_JMP|BPF_JEQ|BPF_K:
170 op = "jeq";
171 fmt = "#0x%x";
172 break;
174 case BPF_JMP|BPF_JSET|BPF_K:
175 op = "jset";
176 fmt = "#0x%x";
177 break;
179 case BPF_JMP|BPF_JGT|BPF_X:
180 op = "jgt";
181 fmt = "x";
182 break;
184 case BPF_JMP|BPF_JGE|BPF_X:
185 op = "jge";
186 fmt = "x";
187 break;
189 case BPF_JMP|BPF_JEQ|BPF_X:
190 op = "jeq";
191 fmt = "x";
192 break;
194 case BPF_JMP|BPF_JSET|BPF_X:
195 op = "jset";
196 fmt = "x";
197 break;
199 case BPF_ALU|BPF_ADD|BPF_X:
200 op = "add";
201 fmt = "x";
202 break;
204 case BPF_ALU|BPF_SUB|BPF_X:
205 op = "sub";
206 fmt = "x";
207 break;
209 case BPF_ALU|BPF_MUL|BPF_X:
210 op = "mul";
211 fmt = "x";
212 break;
214 case BPF_ALU|BPF_DIV|BPF_X:
215 op = "div";
216 fmt = "x";
217 break;
219 case BPF_ALU|BPF_MOD|BPF_X:
220 op = "mod";
221 fmt = "x";
222 break;
224 case BPF_ALU|BPF_AND|BPF_X:
225 op = "and";
226 fmt = "x";
227 break;
229 case BPF_ALU|BPF_OR|BPF_X:
230 op = "or";
231 fmt = "x";
232 break;
234 case BPF_ALU|BPF_XOR|BPF_X:
235 op = "xor";
236 fmt = "x";
237 break;
239 case BPF_ALU|BPF_LSH|BPF_X:
240 op = "lsh";
241 fmt = "x";
242 break;
244 case BPF_ALU|BPF_RSH|BPF_X:
245 op = "rsh";
246 fmt = "x";
247 break;
249 case BPF_ALU|BPF_ADD|BPF_K:
250 op = "add";
251 fmt = "#%d";
252 break;
254 case BPF_ALU|BPF_SUB|BPF_K:
255 op = "sub";
256 fmt = "#%d";
257 break;
259 case BPF_ALU|BPF_MUL|BPF_K:
260 op = "mul";
261 fmt = "#%d";
262 break;
264 case BPF_ALU|BPF_DIV|BPF_K:
265 op = "div";
266 fmt = "#%d";
267 break;
269 case BPF_ALU|BPF_MOD|BPF_K:
270 op = "mod";
271 fmt = "#%d";
272 break;
274 case BPF_ALU|BPF_AND|BPF_K:
275 op = "and";
276 fmt = "#0x%x";
277 break;
279 case BPF_ALU|BPF_OR|BPF_K:
280 op = "or";
281 fmt = "#0x%x";
282 break;
284 case BPF_ALU|BPF_XOR|BPF_K:
285 op = "xor";
286 fmt = "#0x%x";
287 break;
289 case BPF_ALU|BPF_LSH|BPF_K:
290 op = "lsh";
291 fmt = "#%d";
292 break;
294 case BPF_ALU|BPF_RSH|BPF_K:
295 op = "rsh";
296 fmt = "#%d";
297 break;
299 case BPF_ALU|BPF_NEG:
300 op = "neg";
301 fmt = "";
302 break;
304 case BPF_MISC|BPF_TAX:
305 op = "tax";
306 fmt = "";
307 break;
309 case BPF_MISC|BPF_TXA:
310 op = "txa";
311 fmt = "";
312 break;
314 (void)snprintf(operand, sizeof operand, fmt, v);
315 if (BPF_CLASS(p->code) == BPF_JMP && BPF_OP(p->code) != BPF_JA) {
316 (void)snprintf(image, sizeof image,
317 "(%03d) %-8s %-16s jt %d\tjf %d",
318 n, op, operand, n + 1 + p->jt, n + 1 + p->jf);
319 } else {
320 (void)snprintf(image, sizeof image,
321 "(%03d) %-8s %s",
322 n, op, operand);
324 return image;