-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils-codecs.r
387 lines (333 loc) · 8.77 KB
/
utils-codecs.r
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
REBOL [
; -- Core Header attributes --
title: {utils-codecs | data encoding/decoding utilities and helpers.}
file: %utils-codecs.r
version: 1.0.0
date: 2013-9-10
author: "Maxim Olivier-Adlhoch"
purpose: {Collection of functions to help in manipulation of data to/from different encodings. Also includes functions which help in those tasks.}
web: http://www.revault.org/modules/utils-codecs.rmrk
source-encoding: "Windows-1252"
note: {Steel Library Manager (SLiM) is Required to use this module.}
; -- slim - Library Manager --
slim-name: 'utils-codecs
slim-version: 1.2.1
slim-prefix: none
slim-update: http://www.revault.org/downloads/modules/utils-codecs.r
; -- Licensing details --
copyright: "Copyright © 2013 Maxim Olivier-Adlhoch"
license-type: "Apache License v2.0"
license: {Copyright © 2013 Maxim Olivier-Adlhoch
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.}
;- / history
history: {
2013-09-10 - v1.0.0
-creation of history.
-license changed to Apache v2
}
;- \ history
;- / documentation
documentation: ""
;- \ documentation
]
;--------------------------------------
; unit testing setup
;--------------------------------------
;
; test-enter-slim 'utils-codecs
;
;--------------------------------------
slim/register [
;- .
;-----------------------------------------------------------------------------------------------------------
;
;- GLOBALS
;
;-----------------------------------------------------------------------------------------------------------
platform-little-endian: found? find [ win32 linux OSX ] platform-name
platform-big-endian: not platform-little-endian
; used for fast integer convertion (outputs small endian values on intel cpus)
ui32-struct: make struct! [value [integer!]] [0]
decimal-struct: make struct! [value [decimal!]] none
;- .
;-----------------------------------------------------------------------------------------------------------
;-
;- DATA CREATION
;-
;-----------------------------------------------------------------------------------------------------------
;--------------------------
;- bit()
;--------------------------
; purpose: Generates an integer which only has the specified bit set.
;
; inputs:
;
; returns:
;
; notes: Offsets larger than 32 will cycle through the 32 bits. (so bit 33 == bit 1 == bit 65)
;
; This is a 1 based spec. many C specs use a 0 based index, so adjust by one bit.
;
; tests:
;--------------------------
bit: func [
offset [integer!]
][
; we use 1 based index (so you use bits 1 - 32, as opposed to 0 - 31)
offset: offset - 1
shift/left 1 offset
]
;- .
;-----------------------------------------------------------------------------------------------------------
;-
;- DATA CONVERTION
;-
;-----------------------------------------------------------------------------------------------------------
;--------------------------
;- u32-to-binary()
;--------------------------
; purpose:
;
; inputs:
;
; returns:
;
; notes:
;
; tests:
;--------------------------
u32-to-binary: func [
n [number!]
/rev
][
if n > (2 ** 31 - 1) [
n: n - (2 ** 32)
]
n: load join "#{" [form to-hex to-integer n "}"]
either rev [head reverse n][n]
]
;--------------------------
;- load-u32()
;--------------------------
; purpose:
;
; inputs:
;
; returns:
;
; notes: may return a decimal if the number is larger than 31 bits.
;
; tests:
;--------------------------
load-u32: funcl [
data [string! binary!]
][
n: to-integer to binary! :data
if negative? n [
n: n + 2 ** 32
]
]
;--------------------------
;- i32-to-binary()
;--------------------------
; purpose:
;
; inputs:
;
; returns:
;
; notes:
;
; tests:
;--------------------------
i32-to-binary: funcl [
n [integer!]
/network "return binary in network byte order"
/big-endian
/little-endian
][
n: load join "#{" [form to-hex n "}"]
;-----
; by default we return the platform
any [
all [
any [network big-endian platform-big-endian ]
n
]
all [
any [little-endian platform-little-endian ]
head reverse n
]
]
]
;--------------------------
;- load-i32()
;--------------------------
; purpose:
;
; inputs:
;
; returns:
;
; notes: may return a decimal if the number is larger than 31 bits.
;
; tests:
;--------------------------
load-i32: funcl [
data [string! binary!]
/rev
][
n: to-integer as-binary either rev [ head reverse copy/part data 4 ][ data ]
]
;--------------------------
;- load-le-i32()
;--------------------------
; purpose:
;
; inputs:
;
; returns:
;
; notes: the same as load-i32, but expects a little-endian input binary
;
; tests:
;--------------------------
load-le-i32: funcl [
data [string! binary!]
][
to-integer as-binary head reverse copy/part data 4
]
;--------------------------
;- load-be-i32()
;--------------------------
; purpose:
;
; inputs:
;
; returns:
;
; notes: the same as load-i32, but expects a little-endian input binary
;
; tests:
;--------------------------
load-be-i32: funcl [
data [string! binary!]
][
n: to-integer as-binary data
]
;--------------------------
;- int-to-u32-le-binary()
;
; old name kept for backwards compatibility.
int-to-ui32-le-binary:
;--------------------------
int-to-u32-le-binary: func[i][
ui32-struct/value: i
copy third ui32-struct
]
;------------------------
;- int-to-i32-be-binary
;
; this function allows negative values.
;-----------------------
int-to-i32-be-binary: func [
val [integer!]
][
to binary! reduce [
shift (val and -16777216) 24 ; to-integer #{FF000000}
shift (val and 16711680) 16
shift (val and 65280) 8
val and 255
]
]
;--------------------------
;- to-binary()
;--------------------------
; purpose: enhances the core version of to-binary
; with one which is symmetric with to-integer wrt binaries and integers.
;
; inputs: any value
;
; notes: the core version of to-binary is useless with integer inputs
; (it does a to-string on the number first !?!?!).
;--------------------------
to-binary: func [
value
][
either integer? value [
int-to-i32-be-binary value
][
to binary! :value
]
]
;------------------------
;- int-to-u31-be-binary
;
; this function DOES NOT allow negative values.
;-----------------------
int-to-u31-be-binary: func [
[catch]
val
][
if val < 0 [
throw make error! "int-to-u31-be-binary() cannot use negative values."
]
make binary! reduce [
shift (val and 2130706432) 24 ; to-integer #{7F000000}
shift (val and 16711680) 16
shift (val and 65280) 8
val and 255
]
]
;--------------------------
;- decimal-to-binary()
;--------------------------
; purpose: returns the buffer memory used to represent a decimal! value in RAM.
;
; can be used to serialize decimal! values and reload them using binary-to-decimal
;
; returns: an 8 byte (64bit) IEEE value
;--------------------------
decimal-to-binary: funcl [
value [decimal!]
][
vin "decimal-to-binary()"
decimal-struct/value: value
vout
copy third decimal-struct
]
;--------------------------
;- binary-to-decimal()
;--------------------------
; purpose: loads a serialized 64 bit IEEE float as a rebol decimal! value.
;
; notes: should have been serialized with decimal-to-binary (may work with C double values)
;--------------------------
binary-to-decimal: funcl [
[catch]
value [binary!]
][
vin "binary-to-decimal()"
unless 8 = length? value [
throw make error! "binary-to-decimal() requires a binary of exactly 8 bytes."
]
change third decimal-struct value
vout
decimal-struct/value
]
]
;------------------------------------
; We are done testing this library.
;------------------------------------
;
; test-exit-slim
;
;------------------------------------