Missing file: this is what you get working on holidays :-(
[mono.git] / mono / metadata / mono-endian.c
1 #include "mono-endian.h"
2
3 #if NO_UNALIGNED_ACCESS
4
5 typedef struct {
6         char c [2];
7         guint16 i;
8 } mono_rint16;
9
10 typedef struct {
11         char c [4];
12         guint32 i;
13 } mono_rint32;
14
15 typedef struct {
16         char c [8];
17         guint64 i;
18 } mono_rint64;
19
20 guint16 
21 mono_read16 (const unsigned char *x)
22 {
23         mono_rint16 r;
24 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
25         r.c [0] = x [0];
26         r.c [1] = x [1];
27 #else
28         r.c [1] = x [0];
29         r.c [0] = x [1];
30 #endif
31         return r.i;
32 }
33
34 guint32 
35 mono_read32 (const unsigned char *x)
36 {
37         mono_rint32 r;
38 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
39         r.c [0] = x [0];
40         r.c [1] = x [1];
41         r.c [2] = x [2];
42         r.c [3] = x [3];
43 #else
44         r.c [3] = x [0];
45         r.c [2] = x [1];
46         r.c [1] = x [2];
47         r.c [0] = x [3];
48 #endif
49         return r.i;
50 }
51
52 guint64 
53 mono_read64 (const unsigned char *x)
54 {
55         mono_rint64 r;
56 #if G_BYTE_ORDER != G_LITTLE_ENDIAN
57         r.c [0] = x [0];
58         r.c [1] = x [1];
59         r.c [2] = x [2];
60         r.c [3] = x [3];
61         r.c [4] = x [4];
62         r.c [5] = x [5];
63         r.c [6] = x [6];
64         r.c [7] = x [7];
65 #else
66         r.c [7] = x [0];
67         r.c [6] = x [1];
68         r.c [5] = x [2];
69         r.c [4] = x [3];
70         r.c [3] = x [4];
71         r.c [2] = x [5];
72         r.c [1] = x [6];
73         r.c [0] = x [7];
74 #endif
75         return r.i;
76 }
77
78 #endif