Skip to content

Instantly share code, notes, and snippets.

@rgchris
Last active January 7, 2021 22:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rgchris/e0b7bd0dabe701193cff4e03c4bf95b9 to your computer and use it in GitHub Desktop.
Save rgchris/e0b7bd0dabe701193cff4e03c4bf95b9 to your computer and use it in GitHub Desktop.
Rebol [
Title: "Ascii 85 Encoder/Decoder for Rebol 2"
Date: 6-Jan-2021
Author: "Christopher Ross-Gill"
Version: 1.0.0
]
pow-85: [52200625 614125 7225 85 1]
ascii85: charset [#"!" - #"u"]
whitespace: charset as-string #{00090A0C0D20}
uint32-to-number: func [
value [binary!]
][
add to integer! value pick [4294967296 0] value/1 > 127
]
number-to-uint32: func [
value [integer! decimal!]
][
debase/base to-hex to integer! subtract value pick [4294967296 0] value > 2147483647 16
]
from-ascii85: func [
encoding [binary! string!]
/local content part size
][
encoding: as-binary encoding
content: make binary! #{}
size: 4
parse/all/case encoding [
any [
ascii85 encoding:
(part: multiply add pick [-33.0 -33] encoding/1 > 73 to-integer encoding/-1 pow-85/1)
any whitespace
ascii85 encoding:
(part: -33 + encoding/-1 * pow-85/2 + part)
[
any whitespace
ascii85 encoding:
(part: -33 + encoding/-1 * pow-85/3 + part)
[
any whitespace
ascii85 encoding:
(part: -33 + encoding/-1 * pow-85/4 + part)
[
any whitespace
ascii85 encoding:
(part: -33 + encoding/-1 * pow-85/5 + part)
|
(
part: part + pow-85/4
size: 3
)
]
|
(
part: part + pow-85/3
size: 2
)
]
|
(
part: part + pow-85/2
size: 1
)
]
(
; should probably have a size check here
part: number-to-uint32 part
repeat offset size [
append content to char! part/:offset
]
)
|
#"z"
(append content #{00000000})
|
some whitespace
|
end break
|
encoding:
(make error! rejoin ["Ascii85 Encoding Error at (" index? encoding "): " copy/part encoding 6])
]
]
content
]
to-ascii85: func [
content [binary! string!]
/local encoding part size
][
content: as-binary content
size: 5
also encoding: copy "" ; make string! to integer! 1.25 * length? content
while [not tail? content][
part:
add pick [4294967296 0] content/1 > 127
add any [content/4 (size: 4 0)]
add shift/left any [content/3 (size: 3 0)] 8
add shift/left any [content/2 (size: 2 0)] 16
shift/left content/1 24
content: skip content 4
either all [
zero? part
size = 5
][
insert tail encoding #"z"
][
repeat offset size [
insert tail encoding #"!" + to integer! part / pow-85/:offset
part: to integer! part // pow-85/:offset
]
]
]
]
Rebol [
Title: "Ascii 85 Encoder/Decoder"
Date: 6-Jan-2021
Author: "Christopher Ross-Gill"
Type: module
Name: rgchris.ascii85
Version: 1.0.0
Exports: [to-ascii85 from-ascii85]
]
factors: [52200625 614125 7225 85 1]
ascii85: charset [33 - 117]
whitespace: charset [0 9 10 12 13 32]
from-ascii85: func [
encoding [binary! text!]
<local> content part size
][
encoding: as binary! encoding
content: make binary! #{}
size: 4
parse encoding [
opt "<~"
any [
ascii85 encoding:
(part: encoding/-1 - 33 * factors/1)
any whitespace
ascii85 encoding:
(part: encoding/-1 - 33 * factors/2 + part)
[
any whitespace
ascii85 encoding:
(part: encoding/-1 - 33 * factors/3 + part)
[
any whitespace
ascii85 encoding:
(part: encoding/-1 - 33 * factors/4 + part)
[
any whitespace
ascii85 encoding:
(part: encoding/-1 - 33 * factors/5 + part)
|
(
part: part + factors/4
size: 3
)
]
|
(
part: part + factors/3
size: 2
)
]
|
(
part: part + factors/2
size: 1
)
]
(
; should probably have a size check here
part: enbin [be + 4] part
repeat offset size [
append content part/:offset
]
)
|
#"z"
(append content #{00000000})
|
some whitespace
|
opt "~>" end break
|
encoding:
(fail unspaced ["Ascii85 Encoding Error at (" index-of encoding "): " copy/part encoding 6])
]
]
content
]
to-ascii85: func [
content [binary! text!]
<local> encoding part size
][
; to do--permit optional linebreaks
content: as binary! content
encoding: make text! ""
size: 5
parse content [
any [
content:
skip
(part: shift content/1 24)
[
skip
(part: me + shift content/2 16)
[
skip
(part: me + shift content/3 8)
[
skip
(part: me + content/4)
|
(size: 4)
]
|
(size: 3)
]
|
(size: 2)
]
(
either all [
zero? part
size = 5
][
append encoding #"z"
][
repeat offset size [
append encoding add #"!" to integer! part / factors/:offset
part: part mod factors/:offset
]
]
)
]
]
encoding
]
Rebol [
Title: "Ascii85 Tests"
Date: 6-Jan-2021
]
{Man is distinguished, not only by his reason, but by this singular passion from other animals, which is a lust of the mind, that by a perseverance of delight in the continued and indefatigable generation of knowledge, exceeds the short vehemence of any carnal pleasure.}
{9jqo^^BlbD-BleB1DJ+*+F(f,q/0JhKF<GL>Cj@.4Gp$d7F!,L7@<6@)/0JDEF<G%<+EV:2F!,O<DJ+*.@<*K0@<6L(Df-\0Ec5e;DffZ(EZee.Bl.9pF"AGXBPCsi+DGm>@3BB/F*&OCAfu2/AKYi(DIb:@FD,*)+C]U=@3BN#EcYf8ATD3s@q?d$AftVqCh[NqF<G:8+EV:.+Cf>-FD5W8ARlolDIal(DId<j@<?3r@:F%a+D58'ATD4$Bl@l3De:,-DJs`8ARoFb/0JMK@qB4^^F!,R<AKZ&-DfTqBG%G>uD.RTpAKYo'+CT/5+Cei#DII?(E,9)oF*2M7/c}
#{DECAFBAD} "hTG_t"
#{0000000000} "z!!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment