Skip to content

Commit 1d2daff

Browse files
committed
FEAT: bincode: reading of STRING-BYTES and OCTEL-BYTES (string and octal number from fixed size bytes)
1 parent 94508ee commit 1d2daff

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

src/boot/words.r

+3-1
Original file line numberDiff line numberDiff line change
@@ -226,4 +226,6 @@ devices-out
226226

227227
msdos-datetime
228228
msdos-date
229-
msdos-time
229+
msdos-time
230+
octal-bytes
231+
string-bytes

src/core/u-bincode.c

+23-5
Original file line numberDiff line numberDiff line change
@@ -1203,6 +1203,8 @@ static REBCNT EncodedU32_Size(u32 value) {
12031203
SET_INT32(temp, cp[0] + (cp[1] << 8) + (cp[2] << 16));
12041204
break;
12051205
case SYM_BYTES:
1206+
case SYM_OCTAL_BYTES:
1207+
case SYM_STRING_BYTES:
12061208
//puts("bytes");
12071209
next = ++value;
12081210
if (IS_GET_WORD(next)) next = Get_Var(next);
@@ -1218,10 +1220,27 @@ static REBCNT EncodedU32_Size(u32 value) {
12181220
ASSERT_READ_SIZE(value, cp, ep, n);
12191221
readNBytes:
12201222
//printf("num: %i\n", n);
1221-
VAL_SET(temp, REB_BINARY);
1222-
bin_new = Copy_Series_Part(bin, VAL_INDEX(buffer_read), n);
1223-
VAL_SERIES(temp) = bin_new;
1224-
VAL_INDEX(temp) = 0;
1223+
if (cmd == SYM_BYTES || cmd == SYM_STRING_BYTES) {
1224+
1225+
bin_new = Copy_Series_Part(bin, VAL_INDEX(buffer_read), n);
1226+
VAL_SERIES(temp) = bin_new;
1227+
VAL_INDEX(temp) = 0;
1228+
if (cmd == SYM_BYTES) {
1229+
VAL_SET(temp, REB_BINARY);
1230+
}
1231+
else {
1232+
VAL_SET(temp, REB_STRING);
1233+
VAL_TAIL(temp) = strnlen(VAL_BIN(temp), n);
1234+
}
1235+
}
1236+
else {// octal number
1237+
u = 0;
1238+
i = 0;
1239+
while ((i < n) && cp[i]) {
1240+
u = (u << 3) | (u64)(cp[i++] - '0');
1241+
}
1242+
SET_INTEGER(temp, u);
1243+
}
12251244
break;
12261245
case SYM_STRING:
12271246
bp = cp;
@@ -1403,7 +1422,6 @@ static REBCNT EncodedU32_Size(u32 value) {
14031422
+ mst->time.minute * 60
14041423
+ mst->time.hour * 3600);
14051424
break;
1406-
14071425
default:
14081426
Trap1(RE_INVALID_SPEC, value);
14091427
}

src/tests/units/bincode-test.r3

+8
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,14 @@ is-protected-error?: func[code][
351351

352352
--assert error? try [binary/write b [msdos-date 15:33:18]] ;<- date required
353353

354+
--test-- "BinCode - STRING-BYTES (read)"
355+
;- used for fixed size strings (used for example in TAR files)
356+
--assert ["test" 1]= binary/read #{746573740000000001} [STRING-BYTES 8 UI8]
357+
358+
--test-- "BinCode - OCTAL-BYTES (read)"
359+
;- used for fixed size octal numbers (used for example in TAR files)
360+
--assert 8 = binary/read/with #{3130} 'OCTAL-BYTES 2
361+
354362
===end-group===
355363

356364

0 commit comments

Comments
 (0)