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