Skip to content

Instantly share code, notes, and snippets.

@rgchris
Last active February 13, 2021 19:46
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/64d0046590a3f28e712ca0d0f5e701bd to your computer and use it in GitHub Desktop.
Save rgchris/64d0046590a3f28e712ca0d0f5e701bd to your computer and use it in GitHub Desktop.
Scheme for Encoding/Decoding Data URIs
Rebol [
Title: "Data URI Scheme"
Date: 13-Feb-2021
Author: "Christopher Ross-Gill"
]
use [chars type] [
chars: complement charset "^-^/^M ,/;"
sys/make-scheme [
name: 'data
init: func [port] [
for-each word [
type base64 data
][
any [
in port/spec word
extend port/spec word _
]
]
case [
not url? port/spec/ref [
fail "Must use URL! to initiate a data uri"
]
not parse port/spec/ref [
"data:"
copy type [some chars #"/" some chars]
(port/spec/type: :type)
[
";base64"
(port/spec/base64: yes)
|
(port/spec/base64: no)
]
#"," port/spec/data:
][
fail "Not a valid data URI"
]
]
]
actor: [
read: func [port] [
either port/spec/base64 [
debase/base form port/spec/data 64
][
as binary! dehex form port/spec/data
]
]
write: func [port data] [
head insert clear port/spec/data either port/spec/base64 [
trim/all enbase/base as binary! data 64
][
enhex data
]
]
]
]
]
@rgchris
Copy link
Author

rgchris commented Feb 13, 2021

read to url! "data:text/plain,Foo"
read to url! "data:text/plain;base64,Rm9v"
write to url! "data:text/plain," "Bar"
write to url! "data:text/plain;base64," "Bar"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment