23b6fe7b3caffab9839fab0fd6ef704492a24b4f
[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  */
10 #include <config.h>
11 #include "mono-endian.h"
12
13 #if NO_UNALIGNED_ACCESS
14
15 typedef union {
16         char c [2];
17         guint16 i;
18 } mono_rint16;
19
20 typedef union {
21         char c [4];
22         guint32 i;
23 } mono_rint32;
24
25 typedef union {
26         char c [8];
27         guint64 i;
28 } mono_rint64;
29
30 guint16 
31 mono_read16 (const unsigned char *x)
32 {
33         mono_rint16 r;
34 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
35         r.c [0] = x [0];
36         r.c [1] = x [1];
37 #else
38         r.c [1] = x [0];
39         r.c [0] = x [1];
40 #endif
41         return r.i;
42 }
43
44 guint32 
45 mono_read32 (const unsigned char *x)
46 {
47         mono_rint32 r;
48 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
49         r.c [0] = x [0];
50         r.c [1] = x [1];
51         r.c [2] = x [2];
52         r.c [3] = x [3];
53 #else
54         r.c [3] = x [0];
55         r.c [2] = x [1];
56         r.c [1] = x [2];
57         r.c [0] = x [3];
58 #endif
59         return r.i;
60 }
61
62 guint64 
63 mono_read64 (const unsigned char *x)
64 {
65         mono_rint64 r;
66 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
67         r.c [0] = x [0];
68         r.c [1] = x [1];
69         r.c [2] = x [2];
70         r.c [3] = x [3];
71         r.c [4] = x [4];
72         r.c [5] = x [5];
73         r.c [6] = x [6];
74         r.c [7] = x [7];
75 #else
76         r.c [7] = x [0];
77         r.c [6] = x [1];
78         r.c [5] = x [2];
79         r.c [4] = x [3];
80         r.c [3] = x [4];
81         r.c [2] = x [5];
82         r.c [1] = x [6];
83         r.c [0] = x [7];
84 #endif
85         return r.i;
86 }
87
88 #endif