Skip to content

Commit 61b39ca

Browse files
committed
FEAT/CHANGE: removing /base refinement from debase and enbase functions
So eliminating ugly `??base/base` code, for example instead of: ``` debase/base data 16 ``` it is now just: ``` debase data 16 ``` I'm still not sure if the `base` value should not be as a first argument, but so far I will just keep it as a second one. Implements: Oldes/Rebol-issues#2411
1 parent e60a7b6 commit 61b39ca

File tree

7 files changed

+59
-70
lines changed

7 files changed

+59
-70
lines changed

src/boot/natives.r

+4-6
Original file line numberDiff line numberDiff line change
@@ -403,18 +403,16 @@ construct: native [
403403
]
404404

405405
debase: native [
406-
{Decodes binary-coded string (BASE-64 default) to binary value.}
406+
{Decodes binary-coded string to binary value.}
407407
value [binary! string!] {The string to decode}
408-
/base {Binary base to use}
409-
base-value [integer!] {The base to convert from: 85, 64, 16, or 2}
408+
base [integer!] {Binary base to use: 85, 64, 16, or 2}
410409
/url {Base 64 Decoding with URL and Filename Safe Alphabet}
411410
]
412411

413412
enbase: native [
414-
{Encodes a string into a binary-coded string (BASE-64 default).}
413+
{Encodes a string into a binary-coded string.}
415414
value [binary! string!] {If string, will be UTF8 encoded}
416-
/base {Binary base to use}
417-
base-value [integer!] {The base to convert to: 85, 64, 16, or 2}
415+
base [integer!] {Binary base to use: 85, 64, 16, or 2}
418416
/url {Base 64 Encoding with URL and Filename Safe Alphabet}
419417
]
420418

src/core/n-strings.c

+5-9
Original file line numberDiff line numberDiff line change
@@ -453,16 +453,14 @@ static struct digest {
453453
**
454454
***********************************************************************/
455455
{
456-
REBINT base = 64;
456+
REBINT base = VAL_INT32(D_ARG(2));
457457
REBSER *ser;
458458
REBCNT index;
459459
REBCNT len = 0;
460460

461461
ser = Prep_Bin_Str(D_ARG(1), &index, &len); // result may be a SHARED BUFFER!
462462

463-
if (D_REF(2)) base = VAL_INT32(D_ARG(3)); // /base
464-
465-
if (!Decode_Binary(D_RET, BIN_SKIP(ser, index), len, base, 0, D_REF(4)))
463+
if (!Decode_Binary(D_RET, BIN_SKIP(ser, index), len, base, 0, D_REF(3)))
466464
Trap1(RE_INVALID_DATA, D_ARG(1));
467465

468466
return R_RET;
@@ -478,19 +476,17 @@ static struct digest {
478476
**
479477
***********************************************************************/
480478
{
481-
REBINT base = 64;
482479
REBSER *ser = NULL;
483480
REBCNT index;
484481
REBVAL *arg = D_ARG(1);
482+
REBINT base = VAL_INT32(D_ARG(2));
485483

486484
Set_Binary(arg, Prep_Bin_Str(arg, &index, 0)); // may be SHARED buffer
487485
VAL_INDEX(arg) = index;
488486

489-
if (D_REF(2)) base = VAL_INT32(D_ARG(3));
490-
491487
switch (base) {
492488
case 64:
493-
ser = Encode_Base64(arg, 0, FALSE, D_REF(4));
489+
ser = Encode_Base64(arg, 0, FALSE, D_REF(3));
494490
break;
495491
case 16:
496492
ser = Encode_Base16(arg, 0, FALSE);
@@ -507,7 +503,7 @@ static struct digest {
507503

508504
break;
509505
default:
510-
Trap_Arg(D_ARG(3));
506+
Trap_Arg(D_ARG(2));
511507
}
512508

513509
Set_String(D_RET, ser);

src/mezz/codec-swf.r

+1-1
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ import module [
328328
][
329329
lineType: none
330330
;StyleChangeRecord
331-
;print ["SCR:" mold copy/part bin/buffer 5 (enbase/base to-binary to-char states 2)]
331+
;print ["SCR:" mold copy/part bin/buffer 5 (enbase to-binary to-char states 2)]
332332
append records 'style
333333
insert/only tail records reduce [
334334
either 0 < (states and 1 ) [ readSBPair ][none] ;move

src/mezz/codec-unixtime.r

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ register-codec [
2323
epoch [number! string! binary!] "Date in unix time format (string is uspposed to be in base-16 format)"
2424
/utc {Will not add time zone}
2525
][
26-
if string? epoch [ epoch: debase/base epoch 16 ]
26+
if string? epoch [ epoch: debase epoch 16 ]
2727
if binary? epoch [ epoch: to integer! epoch ]
2828
days: to integer! tmp: epoch / 86400
2929
hours: to integer! time: (tmp - days) * 24
@@ -54,7 +54,7 @@ register-codec [
5454
binary/write bin: #{} [ui32 :unix]
5555
switch type [
5656
binary! [ return bin ]
57-
string! [ return enbase/base bin 16]
57+
string! [ return enbase bin 16]
5858
integer! [ return unix ] ; just for consistency
5959
]
6060
cause-error 'script 'invalid-arg type

src/mezz/mezz-crypt.r

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,11 @@ import module [
6868
][ return none ]
6969

7070
either binary [
71-
try [debase base64-data]
71+
try [debase base64-data 64]
7272
][
7373
compose/only [
7474
label: (trim/tail label)
75-
binary: (try [debase base64-data])
75+
binary: (try [debase base64-data 64])
7676
header: (new-line/skip header true 2)
7777
pre-text: (trim/head/tail pre-text)
7878
post-text: (trim/head/tail post-text)

src/mezz/prot-smtp.r

+5-5
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,11 @@ sync-smtp-handler: func [ event
154154
case [
155155
find/part response "334 VXNlcm5hbWU6" 16 [
156156
; username being requested
157-
write client to-binary net-log/C join enbase client/spec/user CRLF
157+
write client to-binary net-log/C join enbase client/spec/user 64 CRLF
158158
]
159159
find/part response "334 UGFzc3dvcmQ6" 16 [
160160
; pass being requested
161-
write client to-binary net-log/C join enbase client/spec/pass CRLF
161+
write client to-binary net-log/C join enbase client/spec/pass 64 CRLF
162162
client/spec/state: 'PASSWORD
163163
]
164164
true [
@@ -172,11 +172,11 @@ sync-smtp-handler: func [ event
172172
case [
173173
find/part response "334 " 4 [
174174
auth-key: skip response 4
175-
auth-key: debase auth-key
175+
auth-key: debase auth-key 64
176176
; compute challenge response
177177
auth-key: checksum/method/key auth-key 'md5 client/spec/pass
178178
write client to-binary net-log/C join
179-
enbase reform [client/spec/user lowercase enbase/base auth-key 16] CRLF
179+
enbase reform [client/spec/user lowercase enbase auth-key 16] 64 CRLF
180180
client/spec/state: 'PASSWORD
181181
]
182182
true [
@@ -238,7 +238,7 @@ any
238238
write client to-binary net-log/C rejoin ["MAIL FROM: <" client/spec/email/from ">" CRLF] ][
239239
switch/default client/spec/state [
240240
PLAIN [
241-
write client to-binary net-log/C rejoin [ "AUTH PLAIN " enbase rejoin [client/spec/user #"^@" client/spec/user #"^@" client/spec/pass] CRLF ]
241+
write client to-binary net-log/C rejoin [ "AUTH PLAIN " enbase rejoin [client/spec/user #"^@" client/spec/user #"^@" client/spec/pass] 64 CRLF ]
242242
client/spec/state: 'PASSWORD
243243
]
244244
LOGIN [

src/tests/units/enbase-test.r3

+40-45
Original file line numberDiff line numberDiff line change
@@ -10,41 +10,36 @@ Rebol [
1010

1111
===start-group=== "enbase basic"
1212
;from https://tools.ietf.org/html/rfc4648#section-10
13-
--test-- "enbase-1" --assert "" = enbase ""
14-
--test-- "enbase-2" --assert "Zg==" = enbase "f"
15-
--test-- "enbase-3" --assert "Zm8=" = enbase "fo"
16-
--test-- "enbase-4" --assert "Zm9v" = enbase "foo"
17-
--test-- "enbase-5" --assert "Zm9vYg==" = enbase "foob"
18-
--test-- "enbase-6" --assert "Zm9vYmE=" = enbase "fooba"
19-
--test-- "enbase-7" --assert "Zm9vYmFy" = enbase "foobar"
20-
21-
--test-- "enbase-8" --assert "" = enbase/base "" 16
22-
--test-- "enbase-9" --assert "66" = enbase/base "f" 16
23-
--test-- "enbase-10" --assert "666F" = enbase/base "fo" 16
24-
--test-- "enbase-11" --assert "666F6F" = enbase/base "foo" 16
25-
--test-- "enbase-12" --assert "666F6F62" = enbase/base "foob" 16
26-
--test-- "enbase-13" --assert "666F6F6261" = enbase/base "fooba" 16
27-
--test-- "enbase-14" --assert "666F6F626172" = enbase/base "foobar" 16
13+
--test-- "enbase-1" --assert "" = enbase "" 64
14+
--test-- "enbase-2" --assert "Zg==" = enbase "f" 64
15+
--test-- "enbase-3" --assert "Zm8=" = enbase "fo" 64
16+
--test-- "enbase-4" --assert "Zm9v" = enbase "foo" 64
17+
--test-- "enbase-5" --assert "Zm9vYg==" = enbase "foob" 64
18+
--test-- "enbase-6" --assert "Zm9vYmE=" = enbase "fooba" 64
19+
--test-- "enbase-7" --assert "Zm9vYmFy" = enbase "foobar" 64
20+
21+
--test-- "enbase-8" --assert "" = enbase "" 16
22+
--test-- "enbase-9" --assert "66" = enbase "f" 16
23+
--test-- "enbase-10" --assert "666F" = enbase "fo" 16
24+
--test-- "enbase-11" --assert "666F6F" = enbase "foo" 16
25+
--test-- "enbase-12" --assert "666F6F62" = enbase "foob" 16
26+
--test-- "enbase-13" --assert "666F6F6261" = enbase "fooba" 16
27+
--test-- "enbase-14" --assert "666F6F626172" = enbase "foobar" 16
2828
===end-group===
2929

3030
===start-group=== "enbase with position not at head"
3131
;@@ https://github.com/rebol/rebol-issues/issues/319
3232
bin: #{FF00FF00}
33-
--test-- "enbase-2" --assert #{00FF00} = debase/base enbase/base next bin 2 2
34-
--test-- "enbase-16" --assert #{00FF00} = debase/base enbase/base next bin 16 16
35-
--test-- "enbase-64" --assert #{00FF00} = debase enbase next bin
33+
--test-- "enbase-2" --assert #{00FF00} = debase enbase next bin 2 2
34+
--test-- "enbase-16" --assert #{00FF00} = debase enbase next bin 16 16
35+
--test-- "enbase-64" --assert #{00FF00} = debase enbase next bin 64 64
3636
===end-group===
3737

3838
===start-group=== "debase 64"
39-
4039
--test-- "debase 64 1"
41-
--assert strict-equal? "A simple string" to string! debase "QSBzaW1wbGUgc3RyaW5n"
40+
--assert strict-equal? "A simple string" to string! debase "QSBzaW1wbGUgc3RyaW5n" 64
4241
--test-- "debase 64 2"
43-
--assert strict-equal? "A multi-line\nstring" to string! debase "QSBtdWx0aS1saW5lXG5zdHJpbmc="
44-
--test-- "debase 64 3"
45-
--assert strict-equal? "A simple string" to string! debase/base "QSBzaW1wbGUgc3RyaW5n" 64
46-
--test-- "debase 64 4"
47-
--assert strict-equal? "A multi-line\nstring" to string! debase/base "QSBtdWx0aS1saW5lXG5zdHJpbmc=" 64
42+
--assert strict-equal? "A multi-line\nstring" to string! debase "QSBtdWx0aS1saW5lXG5zdHJpbmc=" 64
4843

4944
===end-group===
5045

@@ -58,37 +53,37 @@ Rebol [
5853
0GarZRmB_eSN9383LcOLn6_dO--xi12jzDwusC-eOkHWEsqtFZESc6BfI7noOPqv
5954
hJ1phCnvWh6IeYI2w9QOYEUipUTI8np6LbgGY9Fs98rqVt5AXLIhWkWywlVmtVrB
6055
p0igcN_IoypGlUPQGe77Rw
61-
}]
56+
} 64]
6257
--assert binary? try [debase {
6358
cC4hiUPoj9Eetdgtv3hF80EGrhuB__dzERat0XF9g2VtQgr9PJbu3XOiZj5RZmh7
6459
AAuHIm4Bh-0Qc_lF5YKt_O8W2Fp5jujGbds9uJdbF9CUAr7t1dnZcAcQjbKBYNX4
6560
BAynRFdiuB--f_nZLgrnbyTyWzO75vRK5h6xBArLIARNPvkSjtQBMHlb1L07Qe7K
6661
0GarZRmB_eSN9383LcOLn6_dO--xi12jzDwusC-eOkHWEsqtFZESc6BfI7noOPqv
6762
hJ1phCnvWh6IeYI2w9QOYEUipUTI8np6LbgGY9Fs98rqVt5AXLIhWkWywlVmtVrB
6863
p0igcN_IoypGlUPQGe77Rw
69-
}]
64+
} 64]
7065
--test-- "debase 64 url 2 - missing padding in the input"
7166
;should be ok with the padding
7267
--assert binary? try [debase {
7368
eyJpc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTkzODAsDQogImh0dHA6Ly9leGFt
7469
cGxlLmNvbS9pc19yb290Ijp0cnVlfQ==
75-
}]
70+
} 64]
7671
;should throw error when missing padding and not /url refinement is used
7772
--assert error? try [debase {
7873
eyJpc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTkzODAsDQogImh0dHA6Ly9leGFt
7974
cGxlLmNvbS9pc19yb290Ijp0cnVlfQ
80-
}]
75+
} 64]
8176
--assert binary? try [debase/url {
8277
eyJpc3MiOiJqb2UiLA0KICJleHAiOjEzMDA4MTkzODAsDQogImh0dHA6Ly9leGFt
8378
cGxlLmNvbS9pc19yb290Ijp0cnVlfQ
84-
}]
79+
} 64]
8580
--test-- "debase 64 url 3"
8681
key1: "qL8R4QIcQ_ZsRqOAbeRfcZhilN_MksRtDaErMA=="
87-
bin: try [debase/url key1]
88-
--assert true? all [binary? bin key1 = enbase/url bin]
82+
bin: try [debase/url key1 64]
83+
--assert true? all [binary? bin key1 = enbase/url bin 64]
8984
;debase is working also when input is missing the padding
9085
key2: "qL8R4QIcQ_ZsRqOAbeRfcZhilN_MksRtDaErMA"
91-
--assert bin = try [debase/url key2]
86+
--assert bin = try [debase/url key2 64]
9287

9388
===end-group===
9489

@@ -97,11 +92,11 @@ Rebol [
9792
--test-- "debase 16 1"
9893
--assert strict-equal?
9994
"A simple string"
100-
to string! debase/base "412073696d706c6520737472696e67" 16
95+
to string! debase "412073696d706c6520737472696e67" 16
10196
--test-- "debase 16 2"
10297
--assert strict-equal?
10398
"A multi-line\nstring"
104-
to string! debase/base "41206d756c74692d6c696e655c6e737472696e67" 16
99+
to string! debase "41206d756c74692d6c696e655c6e737472696e67" 16
105100

106101
===end-group===
107102

@@ -110,12 +105,12 @@ Rebol [
110105
--test-- "debase 2 1"
111106
--assert strict-equal?
112107
"^(04)^(01)"
113-
to string! debase/base "0000010000000001" 2
108+
to string! debase "0000010000000001" 2
114109

115110
===end-group===
116111

117112
if any [
118-
not error? err: try [enbase/base "a" 85]
113+
not error? err: try [enbase "a" 85]
119114
err/id <> 'feature-na
120115
][
121116
base85-str-tests: [
@@ -157,25 +152,25 @@ if any [
157152

158153

159154
===start-group=== "enbase-85"
160-
--test-- "enbase/base str 85"
155+
--test-- "enbase str 85"
161156
foreach [inp out] base85-str-tests [
162-
--assert out = enbase/base inp 85
157+
--assert out = enbase inp 85
163158
]
164159
===end-group===
165160

166161
===start-group=== "debase-85"
167-
--test-- "debase/base str 85"
162+
--test-- "debase str 85"
168163
foreach [out inp] base85-str-tests [
169-
--assert out = probe to-string debase/base inp 85
164+
--assert out = to-string debase inp 85
170165
]
171166
--test-- "debase 85 with spaces"
172167
foreach [out inp] base85-spaces-tests [
173-
--assert out = debase/base inp 85
168+
--assert out = debase inp 85
174169
]
175170
--test-- "invalid debase85 input"
176-
--assert error? try [debase/base "abcx" 85]
177-
--assert error? try [debase/base "~>" 85]
178-
--assert error? try [debase/base {s8W-"} 85]
171+
--assert error? try [debase "abcx" 85]
172+
--assert error? try [debase "~>" 85]
173+
--assert error? try [debase {s8W-"} 85]
179174
===end-group===
180175
]
181176

0 commit comments

Comments
 (0)