summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2019-06-21 19:32:41 -0700
committerKaz Kylheku <kaz@kylheku.com>2019-06-21 19:32:41 -0700
commitf2afb365b4a068e2f0ec8cbe3acb252418bff925 (patch)
tree0d6ff12a9b73ebcb93e3231b946a870fdee1d4d0
parent1b95abb838e3309e814a874996e49c5003bade0d (diff)
downloadtxr-f2afb365b4a068e2f0ec8cbe3acb252418bff925.tar.gz
txr-f2afb365b4a068e2f0ec8cbe3acb252418bff925.tar.bz2
txr-f2afb365b4a068e2f0ec8cbe3acb252418bff925.zip
base64: integrate with buffers.
* filter.c (base64_encode): Handle an object of either string or buffer type by using make_byte_input_stream. (base64_decode_buf): New function. (filter_init): Register base64-decode-buf intrinsic. * filter.h (base64_decode_buf): Declared. * stream.c (make_byte_input_stream): New function. * stream.h (make_byte_input_stream): Declared. * txr.1: Updated.
-rw-r--r--filter.c14
-rw-r--r--filter.h1
-rw-r--r--stream.c17
-rw-r--r--stream.h1
-rw-r--r--txr.129
5 files changed, 54 insertions, 8 deletions
diff --git a/filter.c b/filter.c
index 43b3245c..ce03f575 100644
--- a/filter.c
+++ b/filter.c
@@ -40,6 +40,7 @@
#include "filter.h"
#include "eval.h"
#include "stream.h"
+#include "buf.h"
val filters_s;
val filter_k, lfilt_k, rfilt_k, tohtml_k, fromhtml_k;
@@ -799,7 +800,7 @@ val base64_stream_enc(val out, val in, val nbytes, val wrap_cols)
val base64_encode(val str, val wrap_cols)
{
- val in = make_string_byte_input_stream(str);
+ val in = make_byte_input_stream(str);
val out = make_string_output_stream();
(void) base64_stream_enc(out, in, nil, wrap_cols);
@@ -902,6 +903,16 @@ val base64_decode(val str)
return get_string_from_stream(out);
}
+val base64_decode_buf(val str)
+{
+ val in = make_string_input_stream(str);
+ val out = make_buf_stream(nil);
+
+ (void) base64_stream_dec(out, in);
+
+ return get_buf_from_stream(out);
+}
+
static val html_encode(val str)
{
return trie_filter_string(get_filter(tohtml_k), str);
@@ -984,6 +995,7 @@ void filter_init(void)
reg_fun(intern(lit("base64-stream-dec"), user_package), func_n2(base64_stream_dec));
reg_fun(intern(lit("base64-encode"), user_package), func_n2o(base64_encode, 1));
reg_fun(intern(lit("base64-decode"), user_package), func_n1(base64_decode));
+ reg_fun(intern(lit("base64-decode-buf"), user_package), func_n1(base64_decode_buf));
reg_fun(intern(lit("html-encode"), user_package), func_n1(html_encode));
reg_fun(intern(lit("html-encode*"), user_package),
func_n1(html_encode_star));
diff --git a/filter.h b/filter.h
index ca395339..77eb4c33 100644
--- a/filter.h
+++ b/filter.h
@@ -49,6 +49,7 @@ val base64_stream_dec(val out_stream, val in_stream);
val base64_encode(val str, val wrap_cols);
val base64_decode(val str);
+val base64_decode_buf(val str);
void filter_init(void);
diff --git a/stream.c b/stream.c
index 666e0c30..027c1c94 100644
--- a/stream.c
+++ b/stream.c
@@ -4588,6 +4588,23 @@ val path_cat(val dir_name, val base_name)
return scat(lit("/"), dir_name, base_name, nao);
}
+val make_byte_input_stream(val obj)
+{
+ val self = lit("make-byte-input-stream");
+
+ switch (type(obj)) {
+ case LIT:
+ case STR:
+ case LSTR:
+ return make_string_byte_input_stream(obj);
+ case BUF:
+ return make_buf_stream(obj);
+ default:
+ uw_throwf(file_error_s, lit("~a: ~s is neither a string nor buffer"),
+ self, obj, nao);
+ }
+}
+
void stream_init(void)
{
prot1(&ap_regex);
diff --git a/stream.h b/stream.h
index cba0330a..2887b305 100644
--- a/stream.h
+++ b/stream.h
@@ -239,5 +239,6 @@ val pure_rel_path_p(val path);
val base_name(val path);
val dir_name(val path);
val path_cat(val dir_name, val base_name);
+val make_byte_input_stream(val obj);
void stream_init(void);
diff --git a/txr.1 b/txr.1
index f252e7c5..5700febe 100644
--- a/txr.1
+++ b/txr.1
@@ -57779,27 +57779,41 @@ be suitable for insertion into a HTML template, depending on the
context of its insertion. It is suitable as text placed between
tags but not necessarily as tag attribute material.
-.coNP Functions @ base64-encode and @ base64-decode
+.coNP Functions @, base64-encode @ base64-decode and @ base64-decode-buf
.synb
-.mets (base64-encode < string <> [ column-width ])
+.mets (base64-encode >> [ string | << buf ] <> [ column-width ])
.mets (base64-decode < string)
+.mets (base64-decode-buf < string)
.syne
.desc
The
.code base64-encode
function converts the UTF-8 representation of
-.meta string
+.metn string ,
+or the contents of
+.metn buf ,
to Base64 and returns that representation as a string.
+The second argument must either be a character string, or
+a buffer object.
+
The
.code base64-decode
functions performs the opposite conversion; it extracts the
bytes encoded in a Base64 string, and decodes them as UTF-8
to return a character string.
-The Base64 encoding divides the UTF-8 representation into groups of
-six bits, each representing the values 0 to 63. Each value is then
-mapped to the characters
+The
+.code base64-decode-buf
+extracts the bytes encoded in a Base64 string, and returns
+a new buffer object containing these bytes.
+
+The Base64 encoding divides the UTF-8 representation of
+.meta string
+or the bytes contained in
+.meta buf
+into groups of six bits, each representing the values 0 to 63. Each value is
+then mapped to the characters
.code A
to
.codn Z ,
@@ -57825,7 +57839,8 @@ function enforces this convention, but
.code base64-decode
doesn't require these padding characters.
-Base64-encoding an empty string results in an empty string.
+Base64-encoding an empty string or zero-length buffer results in an empty
+string.
If the
.meta column-width