Skip to content

Commit f94ebb9

Browse files
committed
CHANGE: bitwise operation on binary: the shorter argument is used repeatedly
1 parent 31e72ec commit f94ebb9

File tree

5 files changed

+80
-14
lines changed

5 files changed

+80
-14
lines changed

src/core/n-sets.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
** REBOL [R3] Language Interpreter and Run-time Environment
44
**
55
** Copyright 2012 REBOL Technologies
6+
** Copyright 2012-2022 Rebol Open Source Developers
67
** REBOL is a trademark of REBOL Technologies
78
**
89
** Licensed under the Apache License, Version 2.0 (the "License");
@@ -199,7 +200,7 @@ enum {
199200
i = 0; // special case
200201
break;
201202
}
202-
ser = Xandor_Binary(i, val1, val2);
203+
ser = Xandor_Bitset(i, val1, val2);
203204
Set_Series(REB_BITSET, D_RET, ser);
204205
break;
205206

src/core/s-ops.c

+57-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
** REBOL [R3] Language Interpreter and Run-time Environment
44
**
55
** Copyright 2012 REBOL Technologies
6+
** Copyright 2012-2022 Rebol Open Source Developers
67
** REBOL is a trademark of REBOL Technologies
78
**
89
** Licensed under the Apache License, Version 2.0 (the "License");
@@ -22,7 +23,7 @@
2223
** Module: s-ops.c
2324
** Summary: string handling utilities
2425
** Section: strings
25-
** Author: Carl Sassenrath
26+
** Author: Carl Sassenrath, Oldes
2627
** Notes:
2728
**
2829
***********************************************************************/
@@ -188,9 +189,9 @@
188189

189190
/***********************************************************************
190191
**
191-
*/ REBSER *Xandor_Binary(REBCNT action, REBVAL *value, REBVAL *arg)
192+
*/ REBSER *Xandor_Bitset(REBCNT action, REBVAL *value, REBVAL *arg)
192193
/*
193-
** Only valid for BINARY data.
194+
** Only valid for Bitsets data.
194195
**
195196
***********************************************************************/
196197
{
@@ -205,10 +206,6 @@
205206
t1 = VAL_LEN(arg);
206207

207208
mt = MIN(t0, t1); // smaller array size
208-
// For AND - result is size of shortest input:
209-
// if (action == A_AND || (action == 0 && t1 >= t0))
210-
// t2 = mt;
211-
// else
212209
t2 = MAX(t0, t1);
213210

214211
series = Make_Binary(t2);
@@ -238,6 +235,59 @@
238235
return series;
239236
}
240237

238+
/***********************************************************************
239+
**
240+
*/ REBSER *Xandor_Binary(REBCNT action, REBVAL *value, REBVAL *arg)
241+
/*
242+
** Only valid for BINARY data.
243+
**
244+
***********************************************************************/
245+
{
246+
REBSER *series;
247+
REBYTE *p0, *p1, *p2;
248+
REBCNT i, j, mt, t1, t0, t2;
249+
250+
t0 = VAL_LEN(value);
251+
t1 = VAL_LEN(arg);
252+
253+
if (t0 >= t1) {
254+
mt = t1;
255+
t2 = t0;
256+
p0 = VAL_BIN_DATA(value);
257+
p1 = VAL_BIN_DATA(arg);
258+
}
259+
else {
260+
mt = t0;
261+
t2 = t1;
262+
p1 = VAL_BIN_DATA(value);
263+
p0 = VAL_BIN_DATA(arg);
264+
}
265+
series = Make_Binary(t2);
266+
SERIES_TAIL(series) = t2;
267+
p2 = BIN_HEAD(series);
268+
269+
switch (action) {
270+
case A_AND:
271+
for (i = 0, j = 0; j <= t2; i++, j++) {
272+
if (i == mt) i = 0;
273+
p2[j] = p0[j] & p1[i];
274+
}
275+
break;
276+
case A_OR:
277+
for (i = 0, j = 0; j <= t2; i++, j++) {
278+
if (i == mt) i = 0;
279+
p2[j] = p0[j] | p1[i];
280+
}
281+
break;
282+
case A_XOR:
283+
for (i = 0, j = 0; j <= t2; i++, j++) {
284+
if (i == mt) i = 0;
285+
p2[j] = p0[j] ^ p1[i];
286+
}
287+
break;
288+
}
289+
return series;
290+
}
241291

242292
/***********************************************************************
243293
**

src/core/t-bitset.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
** REBOL [R3] Language Interpreter and Run-time Environment
44
**
55
** Copyright 2012 REBOL Technologies
6+
** Copyright 2012-2022 Rebol Open Source Developers
67
** REBOL is a trademark of REBOL Technologies
78
**
89
** Licensed under the Apache License, Version 2.0 (the "License");
@@ -740,7 +741,7 @@
740741
case A_XOR:
741742
if (!IS_BITSET(arg) && !IS_BINARY(arg))
742743
Trap_Math_Args(VAL_TYPE(arg), action);
743-
VAL_SERIES(value) = ser = Xandor_Binary(action, value, arg);
744+
VAL_SERIES(value) = ser = Xandor_Bitset(action, value, arg);
744745
Trim_Tail_Zeros(ser);
745746
break;
746747

src/modules/httpd.reb

+2-5
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Rebol [
3434
10-May-2020 "Oldes" {Implemented directory listing, logging and multipart POST processing}
3535
02-Jul-2020 "Oldes" {Added possibility to stop server and return data from client (useful for OAuth2)}
3636
]
37-
needs: [mime-types]
37+
needs: [3.10.1 mime-types]
3838
]
3939

4040
append system/options/log [httpd: 1]
@@ -755,10 +755,7 @@ sys/make-scheme [
755755
either mask? [
756756
request-data: make binary! len
757757
masks: take/part data 4
758-
payload: take/part data len
759-
while [not tail? payload][
760-
append request-data masks xor take/part payload 4
761-
]
758+
request-data: masks xor take/part data len
762759
][
763760
request-data: take/part data len
764761
]

src/tests/units/series-test.r3

+17
Original file line numberDiff line numberDiff line change
@@ -2306,6 +2306,23 @@ Rebol [
23062306

23072307
===end-group===
23082308

2309+
2310+
===start-group=== "Bitwise operations on binary"
2311+
bin1: #{DEADBEAFF00D}
2312+
bin2: #{BEAF}
2313+
--test-- "AND on binary"
2314+
--assert (bin1 AND bin2) == #{9EADBEAFB00D}
2315+
--assert (bin2 AND bin1) == #{9EADBEAFB00D}
2316+
--test-- "OR on binary"
2317+
--assert (bin1 OR bin2) == #{FEAFBEAFFEAF}
2318+
--assert (bin2 OR bin1) == #{FEAFBEAFFEAF}
2319+
--test-- "XOR on binary"
2320+
--assert (bin1 XOR bin2) == #{600200004EA2}
2321+
--assert (bin2 XOR bin1) == #{600200004EA2}
2322+
--assert (#{600200004EA2} XOR bin2) == bin1
2323+
===end-group===
2324+
2325+
23092326
===start-group=== "TO-*"
23102327

23112328
--test-- "to-path"

0 commit comments

Comments
 (0)