Mercurial > projects > libpws
comparison compat/endian.c @ 0:d541e748cfd8
Initial revision
author | Guido Berhoerster <guido+libpws@berhoerster.name> |
---|---|
date | Tue, 10 Feb 2015 11:29:54 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:d541e748cfd8 |
---|---|
1 /* | |
2 * Copyright (C) 2015 Guido Berhoerster <guido+pws@berhoerster.name> | |
3 * | |
4 * Permission is hereby granted, free of charge, to any person obtaining | |
5 * a copy of this software and associated documentation files (the | |
6 * "Software"), to deal in the Software without restriction, including | |
7 * without limitation the rights to use, copy, modify, merge, publish, | |
8 * distribute, sublicense, and/or sell copies of the Software, and to | |
9 * permit persons to whom the Software is furnished to do so, subject to | |
10 * the following conditions: | |
11 * | |
12 * The above copyright notice and this permission notice shall be included | |
13 * in all copies or substantial portions of the Software. | |
14 * | |
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
18 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY | |
19 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, | |
20 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | |
21 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
22 */ | |
23 | |
24 #include <arpa/inet.h> | |
25 | |
26 #include "pws-compat.h" | |
27 #include "endian.h" | |
28 | |
29 uint16_t | |
30 le16toh(uint16_t little16) | |
31 { | |
32 unsigned char *b = (unsigned char *)&little16; | |
33 | |
34 return ((b[0] << 0) | (b[1] << 8)); | |
35 } | |
36 | |
37 uint16_t | |
38 htole16(uint16_t host16) | |
39 { | |
40 uint16_t little16; | |
41 unsigned char *b = (unsigned char *)&little16; | |
42 | |
43 b[0] = (unsigned char)(host16 >> 0); | |
44 b[1] = (unsigned char)(host16 >> 8); | |
45 | |
46 return (little16); | |
47 } | |
48 | |
49 uint32_t | |
50 le32toh(uint32_t little32) | |
51 { | |
52 unsigned char *b = (unsigned char *)&little32; | |
53 | |
54 return ((b[0] << 0) | (b[1] << 8) | (b[2] << 16) | (b[3] << 24)); | |
55 } | |
56 | |
57 uint32_t | |
58 htole32(uint32_t host32) | |
59 { | |
60 uint32_t little32; | |
61 unsigned char *b = (unsigned char *)&little32; | |
62 | |
63 b[0] = (unsigned char)(host32 >> 0); | |
64 b[1] = (unsigned char)(host32 >> 8); | |
65 b[2] = (unsigned char)(host32 >> 16); | |
66 b[3] = (unsigned char)(host32 >> 24); | |
67 | |
68 return (little32); | |
69 } | |
70 | |
71 uint16_t | |
72 be16toh(uint16_t big16) | |
73 { | |
74 return (ntohs(big16)); | |
75 } | |
76 | |
77 uint16_t | |
78 htobe16(uint16_t host16) | |
79 { | |
80 return (htons(host16)); | |
81 } | |
82 | |
83 uint32_t | |
84 be32toh(uint32_t big32) | |
85 { | |
86 return (ntohl(big32)); | |
87 } | |
88 | |
89 uint32_t | |
90 htobe32(uint32_t host32) | |
91 { | |
92 return (htonl(host32)); | |
93 } |