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