get mxge to build, stage 29/many
[dragonfly.git] / contrib / libpcap / bpf_image.c
blob91f7cef43b5c0dfbaa08c36ec99ace3789df2acf
1 /*
2 * Copyright (c) 1990, 1991, 1992, 1994, 1995, 1996
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
16 * written permission.
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 #ifndef lint
23 static const char rcsid[] _U_ =
24 "@(#) $Header: /tcpdump/master/libpcap/bpf_image.c,v 1.27.2.1 2008/01/02 04:22:16 guy Exp $ (LBL)";
25 #endif
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
31 #include <stdio.h>
32 #include <string.h>
34 #include "pcap-int.h"
36 #ifdef HAVE_OS_PROTO_H
37 #include "os-proto.h"
38 #endif
40 char *
41 bpf_image(p, n)
42 const struct bpf_insn *p;
43 int n;
45 int v;
46 const char *fmt, *op;
47 static char image[256];
48 char operand[64];
50 v = p->k;
51 switch (p->code) {
53 default:
54 op = "unimp";
55 fmt = "0x%x";
56 v = p->code;
57 break;
59 case BPF_RET|BPF_K:
60 op = "ret";
61 fmt = "#%d";
62 break;
64 case BPF_RET|BPF_A:
65 op = "ret";
66 fmt = "";
67 break;
69 case BPF_LD|BPF_W|BPF_ABS:
70 op = "ld";
71 fmt = "[%d]";
72 break;
74 case BPF_LD|BPF_H|BPF_ABS:
75 op = "ldh";
76 fmt = "[%d]";
77 break;
79 case BPF_LD|BPF_B|BPF_ABS:
80 op = "ldb";
81 fmt = "[%d]";
82 break;
84 case BPF_LD|BPF_W|BPF_LEN:
85 op = "ld";
86 fmt = "#pktlen";
87 break;
89 case BPF_LD|BPF_W|BPF_IND:
90 op = "ld";
91 fmt = "[x + %d]";
92 break;
94 case BPF_LD|BPF_H|BPF_IND:
95 op = "ldh";
96 fmt = "[x + %d]";
97 break;
99 case BPF_LD|BPF_B|BPF_IND:
100 op = "ldb";
101 fmt = "[x + %d]";
102 break;
104 case BPF_LD|BPF_IMM:
105 op = "ld";
106 fmt = "#0x%x";
107 break;
109 case BPF_LDX|BPF_IMM:
110 op = "ldx";
111 fmt = "#0x%x";
112 break;
114 case BPF_LDX|BPF_MSH|BPF_B:
115 op = "ldxb";
116 fmt = "4*([%d]&0xf)";
117 break;
119 case BPF_LD|BPF_MEM:
120 op = "ld";
121 fmt = "M[%d]";
122 break;
124 case BPF_LDX|BPF_MEM:
125 op = "ldx";
126 fmt = "M[%d]";
127 break;
129 case BPF_ST:
130 op = "st";
131 fmt = "M[%d]";
132 break;
134 case BPF_STX:
135 op = "stx";
136 fmt = "M[%d]";
137 break;
139 case BPF_JMP|BPF_JA:
140 op = "ja";
141 fmt = "%d";
142 v = n + 1 + p->k;
143 break;
145 case BPF_JMP|BPF_JGT|BPF_K:
146 op = "jgt";
147 fmt = "#0x%x";
148 break;
150 case BPF_JMP|BPF_JGE|BPF_K:
151 op = "jge";
152 fmt = "#0x%x";
153 break;
155 case BPF_JMP|BPF_JEQ|BPF_K:
156 op = "jeq";
157 fmt = "#0x%x";
158 break;
160 case BPF_JMP|BPF_JSET|BPF_K:
161 op = "jset";
162 fmt = "#0x%x";
163 break;
165 case BPF_JMP|BPF_JGT|BPF_X:
166 op = "jgt";
167 fmt = "x";
168 break;
170 case BPF_JMP|BPF_JGE|BPF_X:
171 op = "jge";
172 fmt = "x";
173 break;
175 case BPF_JMP|BPF_JEQ|BPF_X:
176 op = "jeq";
177 fmt = "x";
178 break;
180 case BPF_JMP|BPF_JSET|BPF_X:
181 op = "jset";
182 fmt = "x";
183 break;
185 case BPF_ALU|BPF_ADD|BPF_X:
186 op = "add";
187 fmt = "x";
188 break;
190 case BPF_ALU|BPF_SUB|BPF_X:
191 op = "sub";
192 fmt = "x";
193 break;
195 case BPF_ALU|BPF_MUL|BPF_X:
196 op = "mul";
197 fmt = "x";
198 break;
200 case BPF_ALU|BPF_DIV|BPF_X:
201 op = "div";
202 fmt = "x";
203 break;
205 case BPF_ALU|BPF_AND|BPF_X:
206 op = "and";
207 fmt = "x";
208 break;
210 case BPF_ALU|BPF_OR|BPF_X:
211 op = "or";
212 fmt = "x";
213 break;
215 case BPF_ALU|BPF_LSH|BPF_X:
216 op = "lsh";
217 fmt = "x";
218 break;
220 case BPF_ALU|BPF_RSH|BPF_X:
221 op = "rsh";
222 fmt = "x";
223 break;
225 case BPF_ALU|BPF_ADD|BPF_K:
226 op = "add";
227 fmt = "#%d";
228 break;
230 case BPF_ALU|BPF_SUB|BPF_K:
231 op = "sub";
232 fmt = "#%d";
233 break;
235 case BPF_ALU|BPF_MUL|BPF_K:
236 op = "mul";
237 fmt = "#%d";
238 break;
240 case BPF_ALU|BPF_DIV|BPF_K:
241 op = "div";
242 fmt = "#%d";
243 break;
245 case BPF_ALU|BPF_AND|BPF_K:
246 op = "and";
247 fmt = "#0x%x";
248 break;
250 case BPF_ALU|BPF_OR|BPF_K:
251 op = "or";
252 fmt = "#0x%x";
253 break;
255 case BPF_ALU|BPF_LSH|BPF_K:
256 op = "lsh";
257 fmt = "#%d";
258 break;
260 case BPF_ALU|BPF_RSH|BPF_K:
261 op = "rsh";
262 fmt = "#%d";
263 break;
265 case BPF_ALU|BPF_NEG:
266 op = "neg";
267 fmt = "";
268 break;
270 case BPF_MISC|BPF_TAX:
271 op = "tax";
272 fmt = "";
273 break;
275 case BPF_MISC|BPF_TXA:
276 op = "txa";
277 fmt = "";
278 break;
280 (void)snprintf(operand, sizeof operand, fmt, v);
281 (void)snprintf(image, sizeof image,
282 (BPF_CLASS(p->code) == BPF_JMP &&
283 BPF_OP(p->code) != BPF_JA) ?
284 "(%03d) %-8s %-16s jt %d\tjf %d"
285 : "(%03d) %-8s %s",
286 n, op, operand, n + 1 + p->jt, n + 1 + p->jf);
287 return image;