Line data Source code
1 : /******************************************************************************
2 : * $Id$
3 : *
4 : * Project: FIT Driver
5 : * Purpose: Implement FIT Support - not using the SGI iflFIT library.
6 : * Author: Philip Nemec, nemec@keyholecorp.com
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 2001, Keyhole, Inc.
10 : *
11 : * Permission is hereby granted, free of charge, to any person obtaining a
12 : * copy of this software and associated documentation files (the "Software"),
13 : * to deal in the Software without restriction, including without limitation
14 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15 : * and/or sell copies of the Software, and to permit persons to whom the
16 : * Software is furnished to do so, subject to the following conditions:
17 : *
18 : * The above copyright notice and this permission notice shall be included
19 : * in all copies or substantial portions of the Software.
20 : *
21 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27 : * DEALINGS IN THE SOFTWARE.
28 : ****************************************************************************/
29 :
30 : #ifndef gstEndian_h_
31 : #define gstEndian_h_
32 :
33 : // endian swapping tools
34 :
35 : #include <stdio.h>
36 : #include "cpl_port.h"
37 :
38 : #include "gstTypes.h"
39 :
40 : namespace gstEndian
41 : {
42 :
43 : // have to do swapping on Linux and Windows
44 : #ifdef CPL_LSB
45 : #define swapping
46 : #else
47 : #endif
48 :
49 : #ifdef swapping
50 : size_t swapped_fread(void *ptr, size_t size, size_t nitems, FILE *stream);
51 : size_t swapped_fwrite(const void *ptr, size_t size, size_t nitems,
52 : FILE *stream);
53 :
54 506 : static inline void gst_swap64(void *value)
55 : {
56 : // 0x1122334455667788 --> 0x8877665544332211
57 :
58 506 : *(uint64 *)(value) = (((*(uint64 *)(value)&0x00000000000000ff) << 56) |
59 506 : ((*(uint64 *)(value)&0x000000000000ff00) << 40) |
60 506 : ((*(uint64 *)(value)&0x0000000000ff0000) << 24) |
61 506 : ((*(uint64 *)(value)&0x00000000ff000000) << 8) |
62 506 : ((*(uint64 *)(value) >> 8) & 0x00000000ff000000) |
63 506 : ((*(uint64 *)(value) >> 24) & 0x0000000000ff0000) |
64 506 : ((*(uint64 *)(value) >> 40) & 0x000000000000ff00) |
65 506 : ((*(uint64 *)(value) >> 56) & 0x00000000000000ff));
66 506 : }
67 :
68 1905 : static inline void gst_swap32(void *value)
69 : {
70 : // 0x12 34 56 78 --> 0x78 56 34 12
71 :
72 1905 : *(uint32 *)(value) = (((*(uint32 *)(value)&0x000000ff) << 24) |
73 1905 : ((*(uint32 *)(value)&0x0000ff00) << 8) |
74 1905 : ((*(uint32 *)(value) >> 8) & 0x0000ff00) |
75 1905 : ((*(uint32 *)(value) >> 24) & 0x000000ff));
76 1905 : }
77 :
78 800 : static inline void gst_swap16(void *value)
79 : {
80 800 : *(uint16 *)(value) = (((*(uint16 *)(value)&0x00ff) << 8) |
81 800 : ((*(uint16 *)(value) >> 8) & 0x00ff));
82 800 : }
83 :
84 811 : static inline void gst_swapbytes(void *value, int size)
85 : {
86 811 : switch (size)
87 : {
88 0 : case 1:
89 : // do nothing
90 0 : break;
91 0 : case 2:
92 0 : gst_swap16(value);
93 0 : break;
94 705 : case 4:
95 705 : gst_swap32(value);
96 705 : break;
97 106 : case 8:
98 106 : gst_swap64(value);
99 106 : break;
100 0 : default:
101 0 : fprintf(stderr,
102 : "gst_swapbytes unsupported size %i - not swapping\n", size);
103 0 : break;
104 : } // switch
105 811 : }
106 :
107 : #define gst_swapb(value) gst_swapbytes(&value, sizeof(value))
108 :
109 : #else // swapping
110 :
111 : #define swapped_fread(ptr, size, nitems, stream) \
112 : fread(ptr, size, nitems, stream)
113 : #define swapped_fwrite(ptr, size, nitems, stream) \
114 : fwrite(ptr, size, nitems, stream)
115 :
116 : #define gst_swap64(value)
117 : #define gst_swap32(value)
118 : #define gst_swap16(value)
119 : #define gst_swapbytes(value, size)
120 : #define gst_swapb(value)
121 :
122 : #endif // swapping
123 :
124 : } // namespace gstEndian
125 :
126 : #endif // ! gstEndian_h_
|