1 | //========================================================================= |
---|
2 | // FILENAME : misc.c |
---|
3 | // DESCRIPTION : Miscelleneous funcs |
---|
4 | //========================================================================= |
---|
5 | // Copyright (c) 2008- NETGEAR, Inc. All Rights Reserved. |
---|
6 | //========================================================================= |
---|
7 | |
---|
8 | /* This program is free software; you can redistribute it and/or modify |
---|
9 | * it under the terms of the GNU General Public License as published by |
---|
10 | * the Free Software Foundation; either version 2 of the License, or |
---|
11 | * (at your option) any later version. |
---|
12 | * |
---|
13 | * This program is distributed in the hope that it will be useful, |
---|
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
16 | * GNU General Public License for more details. |
---|
17 | * |
---|
18 | * You should have received a copy of the GNU General Public License |
---|
19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
20 | */ |
---|
21 | |
---|
22 | #include <stdio.h> |
---|
23 | #include <string.h> |
---|
24 | #include <endian.h> |
---|
25 | |
---|
26 | #include "misc.h" |
---|
27 | |
---|
28 | inline __u16 |
---|
29 | le16_to_cpu(__u16 le16) |
---|
30 | { |
---|
31 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
---|
32 | return le16; |
---|
33 | #else |
---|
34 | __u16 be16 = ((le16 << 8) & 0xff00) | ((le16 >> 8) & 0x00ff); |
---|
35 | return be16; |
---|
36 | #endif |
---|
37 | } |
---|
38 | |
---|
39 | inline __u32 |
---|
40 | le32_to_cpu(__u32 le32) |
---|
41 | { |
---|
42 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
---|
43 | return le32; |
---|
44 | #else |
---|
45 | __u32 be32 = |
---|
46 | ((le32 << 24) & 0xff000000) | |
---|
47 | ((le32 << 8) & 0x00ff0000) | |
---|
48 | ((le32 >> 8) & 0x0000ff00) | |
---|
49 | ((le32 >> 24) & 0x000000ff); |
---|
50 | return be32; |
---|
51 | #endif |
---|
52 | } |
---|
53 | |
---|
54 | inline __u64 |
---|
55 | le64_to_cpu(__u64 le64) |
---|
56 | { |
---|
57 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
---|
58 | return le64; |
---|
59 | #else |
---|
60 | __u64 be64; |
---|
61 | __u8 *le64p = (__u8*)&le64; |
---|
62 | __u8 *be64p = (__u8*)&be64; |
---|
63 | be64p[0] = le64p[7]; |
---|
64 | be64p[1] = le64p[6]; |
---|
65 | be64p[2] = le64p[5]; |
---|
66 | be64p[3] = le64p[4]; |
---|
67 | be64p[4] = le64p[3]; |
---|
68 | be64p[5] = le64p[2]; |
---|
69 | be64p[6] = le64p[1]; |
---|
70 | be64p[7] = le64p[0]; |
---|
71 | return be64; |
---|
72 | #endif |
---|
73 | } |
---|
74 | |
---|
75 | inline __u8 |
---|
76 | fget_byte(FILE *fp) |
---|
77 | { |
---|
78 | __u8 d; |
---|
79 | |
---|
80 | (void)fread(&d, sizeof(d), 1, fp); |
---|
81 | return d; |
---|
82 | } |
---|
83 | |
---|
84 | inline __u16 |
---|
85 | fget_le16(FILE *fp) |
---|
86 | { |
---|
87 | __u16 d; |
---|
88 | |
---|
89 | (void)fread(&d, sizeof(d), 1, fp); |
---|
90 | d = le16_to_cpu(d); |
---|
91 | return d; |
---|
92 | } |
---|
93 | |
---|
94 | inline __u32 |
---|
95 | fget_le32(FILE *fp) |
---|
96 | { |
---|
97 | __u32 d; |
---|
98 | |
---|
99 | (void)fread(&d, sizeof(d), 1, fp); |
---|
100 | d = le32_to_cpu(d); |
---|
101 | return d; |
---|
102 | } |
---|
103 | |
---|
104 | inline __u32 |
---|
105 | cpu_to_be32(__u32 cpu32) |
---|
106 | { |
---|
107 | #if __BYTE_ORDER == __LITTLE_ENDIAN |
---|
108 | __u32 be32 = |
---|
109 | ((cpu32 << 24) & 0xff000000) | |
---|
110 | ((cpu32 << 8) & 0x00ff0000) | |
---|
111 | ((cpu32 >> 8) & 0x0000ff00) | |
---|
112 | ((cpu32 >> 24) & 0x000000ff); |
---|
113 | return be32; |
---|
114 | #else |
---|
115 | return cpu32; |
---|
116 | #endif |
---|
117 | } |
---|