summaryrefslogtreecommitdiffstats
path: root/tinyNET/src/tnet_endianness.c
blob: f204f0e0f411d4043b334951b89cc0b36cd3a2f0 (plain)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* Copyright (C) 2010-2011 Mamadou Diop.
*
* Contact: Mamadou Diop <diopmamadou(at)doubango[dot]org>
*
* This file is part of Open Source Doubango Framework.
*
* DOUBANGO is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* DOUBANGO 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with DOUBANGO.
*
*/

/**@file tnet_endianness.c
 * @brief Byte Ordering.
 *
 * @author Mamadou Diop <diopmamadou(at)doubango[dot]org>
 *

 */
#include "tnet_endianness.h"

#include "tnet.h"

extern tsk_bool_t tnet_isBigEndian;

/** Converts a 16-bit value from host to TCP/IP network byte order (big-endian).
* @param x The 16-bit (in host byte order) value to convert.
* @retval @a x in TCP/IP network byte order.
*/
unsigned short tnet_htons(unsigned short x)
{
    if(tnet_is_BE()) {
        return x;
    }
    else {
        return ((((uint16_t)(x) & 0xff00) >> 8)		|
                (((uint16_t)(x) & 0x00ff) << 8));
    }
}

/* Memory alignment hack */
unsigned short tnet_htons_2(const void* px)
{
    unsigned short y = TSK_TO_UINT16((const uint8_t*)px);
    return tnet_htons(y);
}

/** Converts a 32-bit value from host to TCP/IP network byte order (big-endian).
* @param x The 32-bit (in host byte order) value to convert.
* @retval @a x in TCP/IP network byte order.
*/
unsigned long tnet_htonl(unsigned long x)
{
    if(tnet_is_BE()) {
        return x;
    }
    else {
        return ((((uint32_t)(x) & 0xff000000) >> 24)	| \
                (((uint32_t)(x) & 0x00ff0000) >> 8)		| \
                (((uint32_t)(x) & 0x0000ff00) << 8)		| \
                (((uint32_t)(x) & 0x000000ff) << 24));
    }
}

/* Memory alignment hack */
unsigned long tnet_htonl_2(const void* px)
{
    unsigned long y = TSK_TO_UINT32((const uint8_t*)px);
    return tnet_htonl(y);
}

/** Indicates whether we are on a Big Endian host or not.<br>
* <b>IMPORTANT</b>: Before calling this function, you should initialize the network stack by using
* @ref tnet_startup().
* @retval @a tsk_true if the program is runnin on a Big Endian host and @a tsk_false otherwise.
*/
tsk_bool_t tnet_is_BE()
{
    /* If LITTLE_ENDIAN or BIG_ENDIAN macros have been defined in config.h ==> use them
    * otherwise ==> dyn retrieve the endianness
    */
#if LITTLE_ENDIAN
    return tsk_false;
#elif BIG_ENDIAN
    return tsk_true;
#else
    return tnet_isBigEndian;
#endif
}
OpenPOWER on IntegriCloud