Skip to content

Commit 53e3db0

Browse files
committed
FEAT: Including quoted-printable codec
1 parent 24bb10a commit 53e3db0

File tree

4 files changed

+97
-0
lines changed

4 files changed

+97
-0
lines changed

make/rebol3.nest

+2
Original file line numberDiff line numberDiff line change
@@ -728,6 +728,7 @@ include-codec-plist: [mezz-lib-files: %mezz/codec-plist.reb ]
728728
include-codec-swf: [mezz-lib-files: %mezz/codec-swf.reb ]
729729
include-codec-wav: [mezz-lib-files: %mezz/codec-wav.reb ]
730730
include-codec-unixtime: [mezz-lib-files: %mezz/codec-unixtime.reb ]
731+
include-codec-quoted-printable: [mezz-lib-files: %mezz/codec-quoted-printable.reb]
731732
; mezzanines:
732733
include-mezz-ansi: [mezz-lib-files: %mezz/mezz-ansi.reb ]
733734
include-mezz-date: [mezz-lib-files: %mezz/mezz-date.reb ]
@@ -768,6 +769,7 @@ include-rebol-core: [
768769
:include-codec-gzip
769770
:include-codec-zip
770771
:include-codec-tar
772+
:include-codec-quoted-printable
771773

772774
config: INCLUDE_SHA224
773775
config: INCLUDE_SHA384

src/boot/sysobj.reb

+2
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ catalog: object [
5858
; chars which does not have to be url-encoded:
5959
uri: #[bitset! #{000000005BFFFFF5FFFFFFE17FFFFFE2}] ;A-Z a-z 0-9 !#$&'()*+,-./:;=?@_~
6060
uri-component: #[bitset! #{0000000041E6FFC07FFFFFE17FFFFFE2}] ;A-Z a-z 0-9 !'()*-._~
61+
quoted-printable: #[bitset! #{FFFFFFFFFFFFFFFBFFFFFFFFFFFFFFFF}]
6162
]
6263
checksums: [adler32 crc24 crc32 tcp md4 md5 sha1 sha224 sha256 sha384 sha512 ripemd160]
6364
compressions: [gzip deflate zlib lzma crush]
@@ -116,6 +117,7 @@ modules: object [
116117
plist: https://raw.githubusercontent.com/Oldes/Rebol3/master/src/mezz/codec-plist.reb
117118
bbcode: https://raw.githubusercontent.com/Oldes/Rebol3/master/src/mezz/codec-bbcode.reb
118119
html-entities: https://raw.githubusercontent.com/Oldes/Rebol3/master/src/mezz/codec-html-entities.reb
120+
quoted-printable: https://raw.githubusercontent.com/Oldes/Rebol3/master/src/mezz/codec-quoted-printable.reb
119121
;; and..
120122
window: none ;- internal extension for gui (on Windows so far!)
121123
]

src/mezz/codec-quoted-printable.reb

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
REBOL [
2+
Name: quoted-printable
3+
Type: module
4+
;Options: [delay]
5+
Version: 1.0.0
6+
Title: "Codec: quoted-printable encoding"
7+
Author: "Oldes"
8+
Rights: "Copyright (C) 2022 Oldes. All rights reserved."
9+
License: "BSD-3"
10+
Test: %tests/units/codec-test.r3
11+
Specification: https://en.wikipedia.org/wiki/Quoted-printable
12+
]
13+
14+
register-codec [
15+
name: 'quoted-printable
16+
type: 'text
17+
title: "quoted-printable encoding"
18+
19+
decode: function [
20+
"Decodes quoted-printable data"
21+
data [binary! any-string!]
22+
][
23+
output: either binary? data [ copy data ][ to binary! data ]
24+
; remove soft line breaks
25+
parse output [any [to #"=" remove [#"=" [LF | CR LF]] | skip] to end]
26+
to data dehex/escape output #"="
27+
]
28+
29+
encode: function/with [
30+
"Encodes data using quoted-printable encoding"
31+
data [binary! any-string!]
32+
][
33+
output: enhex/escape/except to binary! data #"=" :quoted-printable
34+
assert [number? :max-line-length]
35+
36+
if 0 < length: to integer! max-line-length - 1 [
37+
; limit line length to 76 chars
38+
parse output [any [
39+
; skip max-line-length - 1 chars
40+
length skip
41+
; insert =CRLF if there is not end yet
42+
[end | 1 skip end | insert #{3D0D0A}]
43+
]]
44+
]
45+
to data output
46+
] system/catalog/bitsets
47+
48+
max-line-length: 76
49+
]

src/tests/units/codecs-test.r3

+44
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,50 @@ if find codecs 'html-entities [
577577
===end-group===
578578
]
579579

580+
try [import 'quoted-printable]
581+
if find codecs 'quoted-printable [
582+
was-length: :codecs/quoted-printable/max-line-length
583+
===start-group=== "Quoted-Printable codec"
584+
--test-- "encode Quoted-Printable"
585+
--assert (encode 'quoted-printable "Holčička") == "Hol=C4=8Di=C4=8Dka"
586+
--assert (encode 'quoted-printable @Holčička ) == @Hol=C4=8Di=C4=8Dka
587+
--assert (encode 'quoted-printable %Holčička ) == %Hol=C4=8Di=C4=8Dka
588+
--assert (encode 'quoted-printable #{486F6CC48D69C48D6B61}) == #{486F6C3D43343D3844693D43343D38446B61}
589+
--test-- "decode Quoted-Printable"
590+
--assert (decode 'quoted-printable "Hol=C4=8Di=C4=8Dka") == "Holčička"
591+
--assert (decode 'quoted-printable @Hol=C4=8Di=C4=8Dka ) == @Holčička
592+
--assert (decode 'quoted-printable %Hol=C4=8Di=C4=8Dka ) == %Holčička
593+
--assert (decode 'quoted-printable #{486F6C3D43343D3844693D43343D38446B61}) == #{486F6CC48D69C48D6B61}
594+
595+
--test-- "multiline Quoted-Printable"
596+
codecs/quoted-printable/max-line-length: 76
597+
text-encoded: {J'interdis aux marchands de vanter trop leurs marchandises. Car ils se font=^M
598+
vite p=C3=A9dagogues et t'enseignent comme but ce qui n'est par essence qu=^M
599+
'un moyen, et te trompant ainsi sur la route =C3=A0 suivre les voil=C3=^M
600+
=A0 bient=C3=B4t qui te d=C3=A9gradent, car si leur musique est vulgaire il=^M
601+
s te fabriquent pour te la vendre une =C3=A2me vulgaire.^M
602+
=E2=80=94=E2=80=89Antoine de Saint-Exup=C3=A9ry, Citadelle (1948)}
603+
text-expected: {J'interdis aux marchands de vanter trop leurs marchandises. Car ils se font vite pédagogues et t'enseignent comme but ce qui n'est par essence qu'un moyen, et te trompant ainsi sur la route à suivre les voilà bientôt qui te dégradent, car si leur musique est vulgaire ils te fabriquent pour te la vendre une âme vulgaire.^M
604+
— Antoine de Saint-Exupéry, Citadelle (1948)}
605+
606+
--assert text-expected == decode 'quoted-printable text-encoded
607+
--assert text-expected == decode 'quoted-printable encode 'quoted-printable text-expected
608+
609+
--test-- "encode with line length limit"
610+
codecs/quoted-printable/max-line-length: 4
611+
--assert "a" = encode 'quoted-printable "a"
612+
--assert "ab" = encode 'quoted-printable "ab"
613+
--assert "abc" = encode 'quoted-printable "abc"
614+
--assert "abcd" = encode 'quoted-printable "abcd"
615+
--assert "abc=^M^/de" = encode 'quoted-printable "abcde"
616+
--assert "abc=^M^/def" = encode 'quoted-printable "abcdef"
617+
--assert "abc=^M^/defg" = encode 'quoted-printable "abcdefg"
618+
619+
===end-group===
620+
codecs/quoted-printable/max-line-length: :was-length
621+
]
622+
623+
580624
try [import 'plist]
581625
if find codecs 'plist [
582626
===start-group=== "PLIST codec"

0 commit comments

Comments
 (0)