[Automatic manuf and enterprise-numbers update for 2010-04-04]
[obnox/wireshark/wip.git] / epan / pint.h
1 /* pint.h
2  * Definitions for extracting and translating integers safely and portably
3  * via pointers.
4  *
5  * $Id$
6  *
7  * Wireshark - Network traffic analyzer
8  * By Gerald Combs <gerald@wireshark.org>
9  * Copyright 1998 Gerald Combs
10  *
11  *
12  * This program is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU General Public License
14  * as published by the Free Software Foundation; either version 2
15  * of the License, or (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25  */
26
27 #ifndef __PINT_H__
28 #define __PINT_H__
29
30 #include <glib.h>
31
32 /* Pointer versions of g_ntohs and g_ntohl.  Given a pointer to a member of a
33  * byte array, returns the value of the two or four bytes at the pointer.
34  * The pletoh[sl] versions return the little-endian representation.
35  */
36
37 #define pntohs(p)   ((guint16)                       \
38                      ((guint16)*((const guint8 *)(p)+0)<<8|  \
39                       (guint16)*((const guint8 *)(p)+1)<<0))
40
41 #define pntoh24(p)  ((guint32)*((const guint8 *)(p)+0)<<16|  \
42                      (guint32)*((const guint8 *)(p)+1)<<8|  \
43                      (guint32)*((const guint8 *)(p)+2)<<0)
44
45 #define pntohl(p)   ((guint32)*((const guint8 *)(p)+0)<<24|  \
46                      (guint32)*((const guint8 *)(p)+1)<<16|  \
47                      (guint32)*((const guint8 *)(p)+2)<<8|   \
48                      (guint32)*((const guint8 *)(p)+3)<<0)
49 #define pntoh64(p)  ((guint64)*((const guint8 *)(p)+0)<<56|  \
50                      (guint64)*((const guint8 *)(p)+1)<<48|  \
51                      (guint64)*((const guint8 *)(p)+2)<<40|  \
52                      (guint64)*((const guint8 *)(p)+3)<<32|  \
53                      (guint64)*((const guint8 *)(p)+4)<<24|  \
54                      (guint64)*((const guint8 *)(p)+5)<<16|  \
55                      (guint64)*((const guint8 *)(p)+6)<<8|   \
56                      (guint64)*((const guint8 *)(p)+7)<<0)
57
58
59 #define pletohs(p)  ((guint16)                       \
60                      ((guint16)*((const guint8 *)(p)+1)<<8|  \
61                       (guint16)*((const guint8 *)(p)+0)<<0))
62
63 #define pletoh24(p) ((guint32)*((const guint8 *)(p)+2)<<16|  \
64                      (guint32)*((const guint8 *)(p)+1)<<8|  \
65                      (guint32)*((const guint8 *)(p)+0)<<0)
66
67 #define pletohl(p)  ((guint32)*((const guint8 *)(p)+3)<<24|  \
68                      (guint32)*((const guint8 *)(p)+2)<<16|  \
69                      (guint32)*((const guint8 *)(p)+1)<<8|   \
70                      (guint32)*((const guint8 *)(p)+0)<<0)
71 #define pletoh64(p) ((guint64)*((const guint8 *)(p)+7)<<56|  \
72                      (guint64)*((const guint8 *)(p)+6)<<48|  \
73                      (guint64)*((const guint8 *)(p)+5)<<40|  \
74                      (guint64)*((const guint8 *)(p)+4)<<32|  \
75                      (guint64)*((const guint8 *)(p)+3)<<24|  \
76                      (guint64)*((const guint8 *)(p)+2)<<16|  \
77                      (guint64)*((const guint8 *)(p)+1)<<8|   \
78                      (guint64)*((const guint8 *)(p)+0)<<0)
79
80 /* Pointer routines to put items out in a particular byte order.
81  * These will work regardless of the byte alignment of the pointer.
82  */
83
84 #define phtons(p, v) \
85         {                               \
86         ((guint8*)(p))[0] = (guint8)((v) >> 8); \
87         ((guint8*)(p))[1] = (guint8)((v) >> 0); \
88         }
89
90 #define phtonl(p, v) \
91         {                               \
92         ((guint8*)(p))[0] = (guint8)((v) >> 24);        \
93         ((guint8*)(p))[1] = (guint8)((v) >> 16);        \
94         ((guint8*)(p))[2] = (guint8)((v) >> 8); \
95         ((guint8*)(p))[3] = (guint8)((v) >> 0); \
96         }
97
98
99 /* Macros to byte-swap 32-bit and 16-bit quantities. */
100 #define BSWAP32(x) \
101         ((((x)&0xFF000000)>>24) | \
102          (((x)&0x00FF0000)>>8) | \
103          (((x)&0x0000FF00)<<8) | \
104          (((x)&0x000000FF)<<24))
105 #define BSWAP16(x) \
106          ((((x)&0xFF00)>>8) | \
107           (((x)&0x00FF)<<8))
108
109 /* Turn host-byte-order values into little-endian values. */
110 #define htoles(s) GUINT16_TO_LE(s)
111 #define htolel(l) GUINT32_TO_LE(l)
112
113 #endif /* PINT_H */