a4c48f1413dda3adef0ed9064d111854343a075e
[mono.git] / mono / metadata / mono-endian.c
1 /*
2  * mono-endian.c:
3  *
4  * Author:
5  *      Mono Project (http://www.mono-project.com)
6  *
7  * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
8  * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
9  * Licensed under the MIT license. See LICENSE file in the project root for full license information.
10  */
11 #include <config.h>
12 #include "mono-endian.h"
13
14 #if NO_UNALIGNED_ACCESS
15
16 typedef union {
17         char c [2];
18         guint16 i;
19 } mono_rint16;
20
21 typedef union {
22         char c [4];
23         guint32 i;
24 } mono_rint32;
25
26 typedef union {
27         char c [8];
28         guint64 i;
29 } mono_rint64;
30
31 guint16 
32 mono_read16 (const unsigned char *x)
33 {
34         mono_rint16 r;
35 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
36         r.c [0] = x [0];
37         r.c [1] = x [1];
38 #else
39         r.c [1] = x [0];
40         r.c [0] = x [1];
41 #endif
42         return r.i;
43 }
44
45 guint32 
46 mono_read32 (const unsigned char *x)
47 {
48         mono_rint32 r;
49 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
50         r.c [0] = x [0];
51         r.c [1] = x [1];
52         r.c [2] = x [2];
53         r.c [3] = x [3];
54 #else
55         r.c [3] = x [0];
56         r.c [2] = x [1];
57         r.c [1] = x [2];
58         r.c [0] = x [3];
59 #endif
60         return r.i;
61 }
62
63 guint64 
64 mono_read64 (const unsigned char *x)
65 {
66         mono_rint64 r;
67 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
68         r.c [0] = x [0];
69         r.c [1] = x [1];
70         r.c [2] = x [2];
71         r.c [3] = x [3];
72         r.c [4] = x [4];
73         r.c [5] = x [5];
74         r.c [6] = x [6];
75         r.c [7] = x [7];
76 #else
77         r.c [7] = x [0];
78         r.c [6] = x [1];
79         r.c [5] = x [2];
80         r.c [4] = x [3];
81         r.c [3] = x [4];
82         r.c [2] = x [5];
83         r.c [1] = x [6];
84         r.c [0] = x [7];
85 #endif
86         return r.i;
87 }
88
89 #endif