xref: /openbsd/usr.sbin/tcpdump/extract.h (revision 6b3cd88a)
1 /*	$OpenBSD: extract.h,v 1.10 2021/09/16 12:34:12 visa Exp $	*/
2 
3 /*
4  * Copyright (c) 1992, 1993, 1994, 1995, 1996
5  *	The Regents of the University of California.  All rights reserved.
6  *
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.
22  *
23  * @(#) $Id: extract.h,v 1.10 2021/09/16 12:34:12 visa Exp $ (LBL)
24  */
25 
26 /* Network to host order macros */
27 
28 #define EXTRACT_16BITS(p) \
29 	((u_int16_t)*((const u_int8_t *)(p) + 0) << 8 | \
30 	(u_int16_t)*((const u_int8_t *)(p) + 1))
31 
32 #define EXTRACT_32BITS(p) \
33 	((u_int32_t)*((const u_int8_t *)(p) + 0) << 24 | \
34 	(u_int32_t)*((const u_int8_t *)(p) + 1) << 16 | \
35 	(u_int32_t)*((const u_int8_t *)(p) + 2) << 8 | \
36 	(u_int32_t)*((const u_int8_t *)(p) + 3))
37 
38 #define EXTRACT_24BITS(p) \
39 	((u_int32_t)*((const u_int8_t *)(p) + 0) << 16 | \
40 	(u_int32_t)*((const u_int8_t *)(p) + 1) << 8 | \
41 	(u_int32_t)*((const u_int8_t *)(p) + 2))
42 
43 /* Little endian protocol host order macros */
44 
45 #define EXTRACT_LE_8BITS(p) (*(p))
46 #define EXTRACT_LE_16BITS(p) \
47 	((u_int16_t)*((const u_int8_t *)(p) + 1) << 8 | \
48 	(u_int16_t)*((const u_int8_t *)(p) + 0))
49 #define EXTRACT_LE_32BITS(p) \
50 	((u_int32_t)*((const u_int8_t *)(p) + 3) << 24 | \
51 	(u_int32_t)*((const u_int8_t *)(p) + 2) << 16 | \
52 	(u_int32_t)*((const u_int8_t *)(p) + 1) << 8 | \
53 	(u_int32_t)*((const u_int8_t *)(p) + 0))
54 #define EXTRACT_LE_64BITS(p) \
55 	((u_int64_t)*((const u_int8_t *)(p) + 7) << 56 | \
56 	(u_int64_t)*((const u_int8_t *)(p) + 6) << 48 | \
57 	(u_int64_t)*((const u_int8_t *)(p) + 5) << 40 | \
58 	(u_int64_t)*((const u_int8_t *)(p) + 4) << 32 | \
59 	(u_int64_t)*((const u_int8_t *)(p) + 3) << 24 | \
60 	(u_int64_t)*((const u_int8_t *)(p) + 2) << 16 | \
61 	(u_int64_t)*((const u_int8_t *)(p) + 1) << 8 | \
62 	(u_int64_t)*((const u_int8_t *)(p) + 0))
63