tools.anubis
4.1 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
*
* User: David RENE
* Date: 28/12/2006
* Time: 01:35
* (c) Otherwise Technology
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
read system/types.anubis
read system/string.anubis
public type Byte_Net_Result:
failure,
time_out,
success(Int8).
public define Byte_Net_Result
read_network_byte
(
RAddr(Int8) conn,
Int32 t_out
)=
if read(conn, 1, t_out) is
{
failure then failure,
success(byte) then
if nth(0, byte) is
{
failure then time_out,
success(c) then success(c)
}
}.
public type Bytes_Net_Result:
failure,
time_out,
success(ByteArray),
truncated(ByteArray).
public define Bytes_Net_Result
read_network_bytes
(
RAddr(Int8) conn,
Int32 how_many,
Int32 t_out
)=
if read(conn, how_many, t_out) is
{
failure then failure,
success(bytes) then
with len = length(bytes),
//if the returned length size is 0, it means timeout
if len = 0 then
time_out
else if len = how_many then
success(bytes)
else
truncated(bytes)
}.
public define One
put_Int32_to_network
(
ByteArray dest,
Int32 where,
Int32 what
) =
forget(put(dest,where ,truncate_to_int8(what>>24) ));
forget(put(dest,where+1,truncate_to_int8((what>>16) & 0xFF)));
forget(put(dest,where+2,truncate_to_int8((what>>8) & 0xFF)));
forget(put(dest,where+3,truncate_to_int8(what & 0xFF))).
public define Int32
get_Int32_from_network
(
ByteArray src,
Int32 where
) =
with b0 = int8_to_int32(force_nth(where + 3, src)),
b1 = int8_to_int32(force_nth(where + 2, src)),
b2 = int8_to_int32(force_nth(where + 1, src)),
b3 = int8_to_int32(force_nth(where , src)),
b0 | (b1 << 8) | (b2 << 16) | (b3 << 24).
public define Int64
host_to_lendian_Int64
(
ByteArray src,
) =
with b7 = int8_to_int32(force_nth(7, src)),
b6 = int8_to_int32(force_nth(6, src)),
b5 = int8_to_int32(force_nth(5, src)),
b4 = int8_to_int32(force_nth(4, src)),
b3 = int8_to_int32(force_nth(3, src)),
b2 = int8_to_int32(force_nth(2, src)),
b1 = int8_to_int32(force_nth(1, src)),
b0 = int8_to_int32(force_nth(0, src)),
int64( b0 | (b1 << 8) | (b2 << 16) | (b3 << 24),
b4 | (b5 << 8) | (b6 << 16) | (b7 << 24)).
public define Int16
lendian_to_host_Int16
(
ByteArray src,
) =
int16(force_nth(1, src), force_nth(0, src)).
public define Int32
lendian_to_host_Int32
(
ByteArray src,
) =
with b3 = int8_to_int32(force_nth(3, src)),
b2 = int8_to_int32(force_nth(2, src)),
b1 = int8_to_int32(force_nth(1, src)),
b0 = int8_to_int32(force_nth(0, src)),
b0 | (b1 << 8) | (b2 << 16) | (b3 << 24).
public define Int64
lendian_to_host_Int64
(
ByteArray src,
) =
with b7 = int8_to_int32(force_nth(7, src)),
b6 = int8_to_int32(force_nth(6, src)),
b5 = int8_to_int32(force_nth(5, src)),
b4 = int8_to_int32(force_nth(4, src)),
b3 = int8_to_int32(force_nth(3, src)),
b2 = int8_to_int32(force_nth(2, src)),
b1 = int8_to_int32(force_nth(1, src)),
b0 = int8_to_int32(force_nth(0, src)),
int64( b4 | (b5 << 8) | (b6 << 16) | (b7 << 24), //high
b0 | (b1 << 8) | (b2 << 16) | (b3 << 24)). //low
public define Maybe(Float)
lendian_to_host_Float64
(
ByteArray src,
) =
/* print("from the network 0x" + to_hexa(src) + "\n");
with new_val = constant_byte_array(8,0),
forget(put(new_val, 0, (force_nth(7, src))));
forget(put(new_val, 1, (force_nth(6, src))));
forget(put(new_val, 2, (force_nth(5, src))));
forget(put(new_val, 3, (force_nth(4, src))));
forget(put(new_val, 4, (force_nth(3, src))));
forget(put(new_val, 5, (force_nth(2, src))));
forget(put(new_val, 6, (force_nth(1, src))));
forget(put(new_val, 7, (force_nth(0, src))));
print("After conversion 0x" + to_hexa(new_val) + "\n");
*/
(Maybe(Float)) unserialize(src).