annotate th_endian.h @ 49:598609fb49b0

Change how "config.h" is included, etc.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 03 Oct 2011 15:40:54 +0300
parents a25f5d22483e
children 1607c0691dee
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
1 /*
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
2 * Endianess handling
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
3 * Programmed and designed by Matti 'ccr' Hamalainen
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
4 * (C) Copyright 2002-2007 Tecnic Software productions (TNSP)
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
5 *
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
6 * Please read file 'COPYING' for information on license and distribution.
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
7 */
49
598609fb49b0 Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
8 #ifndef TH_ENDIAN_H
598609fb49b0 Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
9 #define TH_ENDIAN_H
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
10
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
11 #ifdef HAVE_CONFIG_H
49
598609fb49b0 Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
12 #include "config.h"
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
13 #endif
49
598609fb49b0 Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
14 #include "th_types.h"
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
15 #include <stdio.h>
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
16
5
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
17 #ifdef __cplusplus
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
18 extern "C" {
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
19 #endif
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
20
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
21
5
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
22 /* Endianess swapping macros
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
23 */
10
a25f5d22483e Updates.
Matti Hamalainen <ccr@tnsp.org>
parents: 5
diff changeset
24 #define TH_SWAP_16_LE_BE(value) ((uint16_t) ( \
5
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
25 (uint16_t) ((uint16_t) (value) >> 8) | \
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
26 (uint16_t) ((uint16_t) (value) << 8)) )
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
27
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
28
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
29 #define TH_SWAP_32_LE_BE(value) ((uint32_t) ( \
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
30 (((uint32_t) (value) & (uint32_t) 0x000000ffU) << 24) | \
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
31 (((uint32_t) (value) & (uint32_t) 0x0000ff00U) << 8) | \
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
32 (((uint32_t) (value) & (uint32_t) 0x00ff0000U) >> 8) | \
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
33 (((uint32_t) (value) & (uint32_t) 0xff000000U) >> 24)))
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
34
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
35 #ifdef TH_HAVE_64BIT
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
36 #define TH_SWAP_64_LE_BE(value) ((uint64_t) ( \
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
37 (((uint64_t) (value) & (uint64_t) 0x00000000000000ffULL) << 56) | \
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
38 (((uint64_t) (value) & (uint64_t) 0x000000000000ff00ULL) << 40) | \
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
39 (((uint64_t) (value) & (uint64_t) 0x0000000000ff0000ULL) << 24) | \
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
40 (((uint64_t) (value) & (uint64_t) 0x00000000ff000000ULL) << 8) | \
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
41 (((uint64_t) (value) & (uint64_t) 0x000000ff00000000ULL) >> 8) | \
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
42 (((uint64_t) (value) & (uint64_t) 0x0000ff0000000000ULL) >> 24) | \
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
43 (((uint64_t) (value) & (uint64_t) 0x00ff000000000000ULL) >> 40) | \
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
44 (((uint64_t) (value) & (uint64_t) 0xff00000000000000ULL) >> 56)))
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
45 #endif
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
46
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
47 /* Macros that swap only when needed ...
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
48 */
5
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
49 #ifdef TH_BIGENDIAN
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
50
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
51 #define TH_LE16_TO_NATIVE(value) TH_SWAP_16_LE_BE(value)
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
52 #define TH_LE32_TO_NATIVE(value) TH_SWAP_32_LE_BE(value)
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
53 #define TH_NATIVE_TO_LE16(value) TH_SWAP_16_LE_BE(value)
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
54 #define TH_NATIVE_TO_LE32(value) TH_SWAP_32_LE_BE(value)
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
55
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
56 #define TH_BE16_TO_NATIVE(value) ((uint16_t) (value))
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
57 #define TH_BE32_TO_NATIVE(value) ((uint32_t) (value))
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
58 #define TH_NATIVE_TO_BE16(value) ((uint16_t) (value))
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
59 #define TH_NATIVE_TO_BE32(value) ((uint32_t) (value))
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
60
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
61 #ifdef TH_HAVE_64BIT
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
62 #define TH_LE64_TO_NATIVE(value) TH_SWAP_64_LE_BE(value)
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
63 #define TH_NATIVE_TO_LE64(value) TH_SWAP_64_LE_BE(value)
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
64 #define TH_BE64_TO_NATIVE(value) ((uint64_t) (value))
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
65 #define TH_NATIVE_TO_BE64(value) ((uint64_t) (value))
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
66 #endif
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
67
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
68 #else /* !TH_BIGENDIAN */
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
69
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
70 #define TH_LE16_TO_NATIVE(value) ((uint16_t) (value))
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
71 #define TH_LE32_TO_NATIVE(value) ((uint32_t) (value))
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
72 #define TH_NATIVE_TO_LE16(value) ((uint16_t) (value))
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
73 #define TH_NATIVE_TO_LE32(value) ((uint32_t) (value))
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
74
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
75 #define TH_BE16_TO_NATIVE(value) TH_SWAP_16_LE_BE(value)
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
76 #define TH_BE32_TO_NATIVE(value) TH_SWAP_32_LE_BE(value)
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
77 #define TH_NATIVE_TO_BE16(value) TH_SWAP_16_LE_BE(value)
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
78 #define TH_NATIVE_TO_BE32(value) TH_SWAP_32_LE_BE(value)
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
79
5
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
80 #ifdef TH_HAVE_64BIT
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
81 #define TH_LE64_TO_NATIVE(value) ((uint64_t) (value))
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
82 #define TH_NATIVE_TO_LE64(value) ((uint64_t) (value))
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
83 #define TH_BE64_TO_NATIVE(value) TH_SWAP_64_LE_BE(value)
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
84 #define TH_NATIVE_TO_BE64(value) TH_SWAP_64_LE_BE(value)
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
85 #endif
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
86
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
87 #endif
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
88
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
89
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
90 /* Endian-handling file read/write routines
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
91 */
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
92 BOOL th_read_str(FILE *f, uint8_t *s, size_t l);
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
93 BOOL th_write_str(FILE *f, uint8_t *s, size_t l);
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
94
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
95 #define TH_DEFINE_HEADER(xname, xtype) \
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
96 BOOL th_read_ ## xname (FILE *f, xtype *v); \
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
97 BOOL th_write_ ## xname (FILE *f, xtype v);
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
98
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
99 TH_DEFINE_HEADER(le16, uint16_t)
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
100 TH_DEFINE_HEADER(le32, uint32_t)
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
101
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
102 TH_DEFINE_HEADER(be16, uint16_t)
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
103 TH_DEFINE_HEADER(be32, uint32_t)
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
104
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
105 #ifdef TH_HAVE_64BIT
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
106 TH_DEFINE_HEADER(be64, uint64_t)
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
107 TH_DEFINE_HEADER(le64, uint64_t)
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
108 #endif
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
109
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
110 #undef TH_DEFINE_HEADER
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
111
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
112 /* Cause warnings for old functions */
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
113 #define TH_READ_LE16 fdksajlkfdsljf lol
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
114 #define TH_READ_LE32 fdksajlkfdsljf lol
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
115 #define TH_READ_BE16 fdksajlkfdsljf lol
8552edc844a7 Re-wrote endianess handling.
Matti Hamalainen <ccr@tnsp.org>
parents: 0
diff changeset
116 #define TH_READ_BE32 fdksajlkfdsljf lol
0
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
117
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
118 #ifdef __cplusplus
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
119 }
bd61a80a6c54 Initial import into Mercurial repository. Discarding old cvs/svn history
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
120 #endif
49
598609fb49b0 Change how "config.h" is included, etc.
Matti Hamalainen <ccr@tnsp.org>
parents: 10
diff changeset
121 #endif /* TH_ENDIAN_H */