xref: /aosp_15_r20/external/libpcap/bpf_dump.c (revision 8b26181f966a6af5cf6981a6f474313de533bb28)
1*8b26181fSAndroid Build Coastguard Worker /*
2*8b26181fSAndroid Build Coastguard Worker  * Copyright (c) 1992, 1993, 1994, 1995, 1996
3*8b26181fSAndroid Build Coastguard Worker  *	The Regents of the University of California.  All rights reserved.
4*8b26181fSAndroid Build Coastguard Worker  *
5*8b26181fSAndroid Build Coastguard Worker  * Redistribution and use in source and binary forms, with or without
6*8b26181fSAndroid Build Coastguard Worker  * modification, are permitted provided that: (1) source code distributions
7*8b26181fSAndroid Build Coastguard Worker  * retain the above copyright notice and this paragraph in its entirety, (2)
8*8b26181fSAndroid Build Coastguard Worker  * distributions including binary code include the above copyright notice and
9*8b26181fSAndroid Build Coastguard Worker  * this paragraph in its entirety in the documentation or other materials
10*8b26181fSAndroid Build Coastguard Worker  * provided with the distribution, and (3) all advertising materials mentioning
11*8b26181fSAndroid Build Coastguard Worker  * features or use of this software display the following acknowledgement:
12*8b26181fSAndroid Build Coastguard Worker  * ``This product includes software developed by the University of California,
13*8b26181fSAndroid Build Coastguard Worker  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14*8b26181fSAndroid Build Coastguard Worker  * the University nor the names of its contributors may be used to endorse
15*8b26181fSAndroid Build Coastguard Worker  * or promote products derived from this software without specific prior
16*8b26181fSAndroid Build Coastguard Worker  * written permission.
17*8b26181fSAndroid Build Coastguard Worker  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18*8b26181fSAndroid Build Coastguard Worker  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19*8b26181fSAndroid Build Coastguard Worker  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20*8b26181fSAndroid Build Coastguard Worker  */
21*8b26181fSAndroid Build Coastguard Worker 
22*8b26181fSAndroid Build Coastguard Worker #ifdef HAVE_CONFIG_H
23*8b26181fSAndroid Build Coastguard Worker #include <config.h>
24*8b26181fSAndroid Build Coastguard Worker #endif
25*8b26181fSAndroid Build Coastguard Worker 
26*8b26181fSAndroid Build Coastguard Worker #include <pcap.h>
27*8b26181fSAndroid Build Coastguard Worker #include <stdio.h>
28*8b26181fSAndroid Build Coastguard Worker 
29*8b26181fSAndroid Build Coastguard Worker #include "optimize.h"
30*8b26181fSAndroid Build Coastguard Worker 
31*8b26181fSAndroid Build Coastguard Worker void
bpf_dump(const struct bpf_program * p,int option)32*8b26181fSAndroid Build Coastguard Worker bpf_dump(const struct bpf_program *p, int option)
33*8b26181fSAndroid Build Coastguard Worker {
34*8b26181fSAndroid Build Coastguard Worker 	const struct bpf_insn *insn;
35*8b26181fSAndroid Build Coastguard Worker 	int i;
36*8b26181fSAndroid Build Coastguard Worker 	int n = p->bf_len;
37*8b26181fSAndroid Build Coastguard Worker 
38*8b26181fSAndroid Build Coastguard Worker 	insn = p->bf_insns;
39*8b26181fSAndroid Build Coastguard Worker 	if (option > 2) {
40*8b26181fSAndroid Build Coastguard Worker 		printf("%d\n", n);
41*8b26181fSAndroid Build Coastguard Worker 		for (i = 0; i < n; ++insn, ++i) {
42*8b26181fSAndroid Build Coastguard Worker 			printf("%u %u %u %u\n", insn->code,
43*8b26181fSAndroid Build Coastguard Worker 			       insn->jt, insn->jf, insn->k);
44*8b26181fSAndroid Build Coastguard Worker 		}
45*8b26181fSAndroid Build Coastguard Worker 		return ;
46*8b26181fSAndroid Build Coastguard Worker 	}
47*8b26181fSAndroid Build Coastguard Worker 	if (option > 1) {
48*8b26181fSAndroid Build Coastguard Worker 		for (i = 0; i < n; ++insn, ++i)
49*8b26181fSAndroid Build Coastguard Worker 			printf("{ 0x%x, %d, %d, 0x%08x },\n",
50*8b26181fSAndroid Build Coastguard Worker 			       insn->code, insn->jt, insn->jf, insn->k);
51*8b26181fSAndroid Build Coastguard Worker 		return;
52*8b26181fSAndroid Build Coastguard Worker 	}
53*8b26181fSAndroid Build Coastguard Worker 	for (i = 0; i < n; ++insn, ++i) {
54*8b26181fSAndroid Build Coastguard Worker #ifdef BDEBUG
55*8b26181fSAndroid Build Coastguard Worker 		if (i < NBIDS && bids[i] > 0)
56*8b26181fSAndroid Build Coastguard Worker 			printf("[%02d]", bids[i] - 1);
57*8b26181fSAndroid Build Coastguard Worker 		else
58*8b26181fSAndroid Build Coastguard Worker 			printf(" -- ");
59*8b26181fSAndroid Build Coastguard Worker #endif
60*8b26181fSAndroid Build Coastguard Worker 		puts(bpf_image(insn, i));
61*8b26181fSAndroid Build Coastguard Worker 	}
62*8b26181fSAndroid Build Coastguard Worker }
63