Skip to content

Instantly share code, notes, and snippets.

Last active February 13, 2021 19:46
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
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 [
copy type [some chars #"/" some chars]
(port/spec/type: :type)
(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
Copy link

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