1 : /******************************************************************************
2 : * $Id: gstEndian.h 10645 2007-01-18 02:22:39Z warmerdam $
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 : // have to do swapping on Linux and Windows
43 : #ifdef CPL_LSB
44 : #define swapping
45 : #else
46 : #endif
47 :
48 : #ifdef swapping
49 : size_t swapped_fread(void *ptr, size_t size, size_t nitems, FILE *stream);
50 : size_t swapped_fwrite(const void *ptr, size_t size, size_t nitems, FILE
51 : *stream);
52 :
53 1000 : static inline void gst_swap64(void * value)
54 : {
55 : // 0x1122334455667788 --> 0x8877665544332211
56 :
57 : *(uint64 *)(value) =
58 : ( ((*(uint64 *)(value) & 0x00000000000000ff) << 56) |
59 : ((*(uint64 *)(value) & 0x000000000000ff00) << 40) |
60 : ((*(uint64 *)(value) & 0x0000000000ff0000) << 24) |
61 : ((*(uint64 *)(value) & 0x00000000ff000000) << 8) |
62 : ((*(uint64 *)(value) >> 8) & 0x00000000ff000000) |
63 : ((*(uint64 *)(value) >> 24) & 0x0000000000ff0000) |
64 : ((*(uint64 *)(value) >> 40) & 0x000000000000ff00) |
65 1000 : ((*(uint64 *)(value) >> 56) & 0x00000000000000ff) );
66 1000 : }
67 :
68 3366 : static inline void gst_swap32(void * value)
69 : {
70 : // 0x12 34 56 78 --> 0x78 56 34 12
71 :
72 : *(uint32 *)(value) =
73 : ( ((*(uint32 *)(value) & 0x000000ff) << 24) |
74 : ((*(uint32 *)(value) & 0x0000ff00) << 8) |
75 : ((*(uint32 *)(value) >> 8) & 0x0000ff00) |
76 3366 : ((*(uint32 *)(value) >> 24) & 0x000000ff) );
77 3366 : }
78 :
79 1800 : static inline void gst_swap16(void * value)
80 : {
81 : *(uint16 *)(value) =
82 : ( ((*(uint16 *)(value) & 0x00ff) << 8) |
83 1800 : ((*(uint16 *)(value) >> 8) & 0x00ff) );
84 1800 : }
85 :
86 766 : static inline void gst_swapbytes(void * value, int size)
87 : {
88 766 : switch (size) {
89 : case 1:
90 : // do nothing
91 0 : break;
92 : case 2:
93 0 : gst_swap16(value);
94 0 : break;
95 : case 4:
96 666 : gst_swap32(value);
97 666 : break;
98 : case 8:
99 100 : gst_swap64(value);
100 100 : break;
101 : default:
102 : fprintf(stderr, "gst_swapbytes unsupported size %i - not swapping\n",
103 0 : size);
104 : break;
105 : } // switch
106 766 : }
107 :
108 : #define gst_swapb( value ) gst_swapbytes( &value, sizeof(value))
109 :
110 : #else // swapping
111 :
112 : #define swapped_fread(ptr, size, nitems, stream) \
113 : fread(ptr, size, nitems, stream)
114 : #define swapped_fwrite(ptr, size, nitems, stream) \
115 : fwrite(ptr, size, nitems, stream)
116 :
117 : #define gst_swap64( vlaue )
118 : #define gst_swap32( vlaue )
119 : #define gst_swap16( vlaue )
120 : #define gst_swapbytes( value, size )
121 : #define gst_swapb( value )
122 :
123 : #endif // swapping
124 :
125 : } // gstEndian namespace
126 :
127 : #endif // ! _gstEndian_h_
|