aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/endian_tools.h
blob: 191b42470b84183f65ac45f949fc3f89527e9027 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
 * This file is part of AtracDEnc.
 *
 * AtracDEnc is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * AtracDEnc is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with AtracDEnc; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#ifndef LIB_ENDIAN_H
#define LIB_ENDIAN_H

#include <stdint.h>

static inline uint32_t swapbyte32_on_le(uint32_t in) {
#ifdef BIGENDIAN_ORDER
    return in;
#else
    return ((in & 0xff) << 24) | ((in & 0xff00) << 8) | ((in & 0xff0000) >> 8) | ((in & 0xff000000) >> 24);
#endif
}

static inline uint16_t swapbyte16_on_le(uint16_t in) {
#ifdef BIGENDIAN_ORDER
    return in;
#else
    return ((in & 0xff) << 8) | ((in & 0xff00) >> 8);
#endif
}

static inline uint32_t swapbyte32_on_be(uint32_t in) {
#ifdef BIGENDIAN_ORDER
    return ((in & 0xff) << 24) | ((in & 0xff00) << 8) | ((in & 0xff0000) >> 8) | ((in & 0xff000000) >> 24);
#else
    return in;
#endif
}

static inline uint16_t swapbyte16_on_be(uint16_t in) {
#ifdef BIGENDIAN_ORDER
    return ((in & 0xff) << 8) | ((in & 0xff00) >> 8);
#else
    return in;
#endif
}

#ifdef __cplusplus

static inline int16_t conv_ntoh(int16_t in) {
    return (int16_t)swapbyte16_on_le((uint16_t)in);
}

static inline uint16_t conv_ntoh(uint16_t in) {
    return swapbyte16_on_le(in);
}

static inline int32_t conv_ntoh(int32_t in) {
    return (int32_t)swapbyte32_on_le((uint32_t)in);
}

static inline uint32_t conv_ntoh(uint32_t in) {
    return swapbyte32_on_le(in);
}

#endif

#endif