libpurple/protocols/gg/lib/endian.c

changeset 38882
bea4cc95b40f
parent 38881
25cb836b9cec
parent 38182
783878958371
child 38883
90462fef3dd8
equal deleted inserted replaced
38881:25cb836b9cec 38882:bea4cc95b40f
1 /* $Id$ */
2
3 /*
4 * (C) Copyright 2001-2010 Wojtek Kaniewski <wojtekka@irc.pl>
5 * Robert J. Woźny <speedy@ziew.org>
6 * Arkadiusz Miśkiewicz <arekm@pld-linux.org>
7 * Tomasz Chiliński <chilek@chilan.com>
8 * Adam Wysocki <gophi@ekg.chmurka.net>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License Version
12 * 2.1 as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307,
22 * USA.
23 */
24
25 /**
26 * \file endian.c
27 *
28 * \brief Konwersja między różnymi kolejnościami bajtów
29 */
30
31 #include "libgadu.h"
32 #include "internal.h"
33
34 /**
35 * \internal Zamienia kolejność bajtów w 64-bitowym słowie.
36 *
37 * Ze względu na little-endianowość protokołu Gadu-Gadu, na maszynach
38 * big-endianowych odwraca kolejność bajtów w słowie.
39 *
40 * \param x Liczba do zamiany
41 *
42 * \return Liczba z odpowiednią kolejnością bajtów
43 *
44 * \ingroup helper
45 */
46 uint64_t gg_fix64(uint64_t x)
47 {
48 #ifndef GG_CONFIG_BIGENDIAN
49 return x;
50 #else
51 return (uint64_t)
52 (((x & (uint64_t) 0x00000000000000ffULL) << 56) |
53 ((x & (uint64_t) 0x000000000000ff00ULL) << 40) |
54 ((x & (uint64_t) 0x0000000000ff0000ULL) << 24) |
55 ((x & (uint64_t) 0x00000000ff000000ULL) << 8) |
56 ((x & (uint64_t) 0x000000ff00000000ULL) >> 8) |
57 ((x & (uint64_t) 0x0000ff0000000000ULL) >> 24) |
58 ((x & (uint64_t) 0x00ff000000000000ULL) >> 40) |
59 ((x & (uint64_t) 0xff00000000000000ULL) >> 56));
60 #endif
61 }
62
63 /**
64 * \internal Zamienia kolejność bajtów w 32-bitowym słowie.
65 *
66 * Ze względu na little-endianowość protokołu Gadu-Gadu, na maszynach
67 * big-endianowych odwraca kolejność bajtów w słowie.
68 *
69 * \param x Liczba do zamiany
70 *
71 * \return Liczba z odpowiednią kolejnością bajtów
72 *
73 * \ingroup helper
74 */
75 uint32_t gg_fix32(uint32_t x)
76 {
77 #ifndef GG_CONFIG_BIGENDIAN
78 return x;
79 #else
80 return (uint32_t)
81 (((x & (uint32_t) 0x000000ffU) << 24) |
82 ((x & (uint32_t) 0x0000ff00U) << 8) |
83 ((x & (uint32_t) 0x00ff0000U) >> 8) |
84 ((x & (uint32_t) 0xff000000U) >> 24));
85 #endif
86 }
87
88 /**
89 * \internal Zamienia kolejność bajtów w 16-bitowym słowie.
90 *
91 * Ze względu na little-endianowość protokołu Gadu-Gadu, na maszynach
92 * big-endianowych zamienia kolejność bajtów w słowie.
93 *
94 * \param x Liczba do zamiany
95 *
96 * \return Liczba z odpowiednią kolejnością bajtów
97 *
98 * \ingroup helper
99 */
100 uint16_t gg_fix16(uint16_t x)
101 {
102 #ifndef GG_CONFIG_BIGENDIAN
103 return x;
104 #else
105 return (uint16_t)
106 (((x & (uint16_t) 0x00ffU) << 8) |
107 ((x & (uint16_t) 0xff00U) >> 8));
108 #endif
109 }

mercurial