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
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.
26 #include <pcap-types.h>
33 #ifdef HAVE_OS_PROTO_H
38 bpf_image(const struct bpf_insn *p, int n)
41 static char image[256];
49 (void)pcap_snprintf(operand_buf, sizeof operand_buf, "0x%x", p->code);
50 operand = operand_buf;
55 (void)pcap_snprintf(operand_buf, sizeof operand_buf, "#%d", p->k);
56 operand = operand_buf;
64 case BPF_LD|BPF_W|BPF_ABS:
66 (void)pcap_snprintf(operand_buf, sizeof operand_buf, "[%d]", p->k);
67 operand = operand_buf;
70 case BPF_LD|BPF_H|BPF_ABS:
72 (void)pcap_snprintf(operand_buf, sizeof operand_buf, "[%d]", p->k);
73 operand = operand_buf;
76 case BPF_LD|BPF_B|BPF_ABS:
78 (void)pcap_snprintf(operand_buf, sizeof operand_buf, "[%d]", p->k);
79 operand = operand_buf;
82 case BPF_LD|BPF_W|BPF_LEN:
87 case BPF_LD|BPF_W|BPF_IND:
89 (void)pcap_snprintf(operand_buf, sizeof operand_buf, "[x + %d]", p->k);
90 operand = operand_buf;
93 case BPF_LD|BPF_H|BPF_IND:
95 (void)pcap_snprintf(operand_buf, sizeof operand_buf, "[x + %d]", p->k);
96 operand = operand_buf;
99 case BPF_LD|BPF_B|BPF_IND:
101 (void)pcap_snprintf(operand_buf, sizeof operand_buf, "[x + %d]", p->k);
102 operand = operand_buf;
107 (void)pcap_snprintf(operand_buf, sizeof operand_buf, "#0x%x", p->k);
108 operand = operand_buf;
111 case BPF_LDX|BPF_IMM:
113 (void)pcap_snprintf(operand_buf, sizeof operand_buf, "#0x%x", p->k);
114 operand = operand_buf;
117 case BPF_LDX|BPF_MSH|BPF_B:
119 (void)pcap_snprintf(operand_buf, sizeof operand_buf, "4*([%d]&0xf)", p->k);
120 operand = operand_buf;
125 (void)pcap_snprintf(operand_buf, sizeof operand_buf, "M[%d]", p->k);
126 operand = operand_buf;
129 case BPF_LDX|BPF_MEM:
131 (void)pcap_snprintf(operand_buf, sizeof operand_buf, "M[%d]", p->k);
132 operand = operand_buf;
137 (void)pcap_snprintf(operand_buf, sizeof operand_buf, "M[%d]", p->k);
138 operand = operand_buf;
143 (void)pcap_snprintf(operand_buf, sizeof operand_buf, "M[%d]", p->k);
144 operand = operand_buf;
149 (void)pcap_snprintf(operand_buf, sizeof operand_buf, "%d", n + 1 + p->k);
150 operand = operand_buf;
153 case BPF_JMP|BPF_JGT|BPF_K:
155 (void)pcap_snprintf(operand_buf, sizeof operand_buf, "#0x%x", p->k);
156 operand = operand_buf;
159 case BPF_JMP|BPF_JGE|BPF_K:
161 (void)pcap_snprintf(operand_buf, sizeof operand_buf, "#0x%x", p->k);
162 operand = operand_buf;
165 case BPF_JMP|BPF_JEQ|BPF_K:
167 (void)pcap_snprintf(operand_buf, sizeof operand_buf, "#0x%x", p->k);
168 operand = operand_buf;
171 case BPF_JMP|BPF_JSET|BPF_K:
173 (void)pcap_snprintf(operand_buf, sizeof operand_buf, "#0x%x", p->k);
174 operand = operand_buf;
177 case BPF_JMP|BPF_JGT|BPF_X:
182 case BPF_JMP|BPF_JGE|BPF_X:
187 case BPF_JMP|BPF_JEQ|BPF_X:
192 case BPF_JMP|BPF_JSET|BPF_X:
197 case BPF_ALU|BPF_ADD|BPF_X:
202 case BPF_ALU|BPF_SUB|BPF_X:
207 case BPF_ALU|BPF_MUL|BPF_X:
212 case BPF_ALU|BPF_DIV|BPF_X:
217 case BPF_ALU|BPF_MOD|BPF_X:
222 case BPF_ALU|BPF_AND|BPF_X:
227 case BPF_ALU|BPF_OR|BPF_X:
232 case BPF_ALU|BPF_XOR|BPF_X:
237 case BPF_ALU|BPF_LSH|BPF_X:
242 case BPF_ALU|BPF_RSH|BPF_X:
247 case BPF_ALU|BPF_ADD|BPF_K:
249 (void)pcap_snprintf(operand_buf, sizeof operand_buf, "#%d", p->k);
250 operand = operand_buf;
253 case BPF_ALU|BPF_SUB|BPF_K:
255 (void)pcap_snprintf(operand_buf, sizeof operand_buf, "#%d", p->k);
256 operand = operand_buf;
259 case BPF_ALU|BPF_MUL|BPF_K:
261 (void)pcap_snprintf(operand_buf, sizeof operand_buf, "#%d", p->k);
262 operand = operand_buf;
265 case BPF_ALU|BPF_DIV|BPF_K:
267 (void)pcap_snprintf(operand_buf, sizeof operand_buf, "#%d", p->k);
268 operand = operand_buf;
271 case BPF_ALU|BPF_MOD|BPF_K:
273 (void)pcap_snprintf(operand_buf, sizeof operand_buf, "#%d", p->k);
274 operand = operand_buf;
277 case BPF_ALU|BPF_AND|BPF_K:
279 (void)pcap_snprintf(operand_buf, sizeof operand_buf, "#0x%x", p->k);
280 operand = operand_buf;
283 case BPF_ALU|BPF_OR|BPF_K:
285 (void)pcap_snprintf(operand_buf, sizeof operand_buf, "#0x%x", p->k);
286 operand = operand_buf;
289 case BPF_ALU|BPF_XOR|BPF_K:
291 (void)pcap_snprintf(operand_buf, sizeof operand_buf, "#0x%x", p->k);
292 operand = operand_buf;
295 case BPF_ALU|BPF_LSH|BPF_K:
297 (void)pcap_snprintf(operand_buf, sizeof operand_buf, "#%d", p->k);
298 operand = operand_buf;
301 case BPF_ALU|BPF_RSH|BPF_K:
303 (void)pcap_snprintf(operand_buf, sizeof operand_buf, "#%d", p->k);
304 operand = operand_buf;
307 case BPF_ALU|BPF_NEG:
312 case BPF_MISC|BPF_TAX:
317 case BPF_MISC|BPF_TXA:
322 if (BPF_CLASS(p->code) == BPF_JMP && BPF_OP(p->code) != BPF_JA) {
323 (void)pcap_snprintf(image, sizeof image,
324 "(%03d) %-8s %-16s jt %d\tjf %d",
325 n, op, operand, n + 1 + p->jt, n + 1 + p->jf);
327 (void)pcap_snprintf(image, sizeof image,