Re: [dev][st] OSC-52 payload being truncated

2017-08-18 Thread Suraj N. Kurapati
On Thu, 17 Aug 2017 23:40:34 -0700, Suraj N. Kurapati wrote:
> On Tue, 15 Aug 2017 01:05:47 -0700, Suraj N. Kurapati wrote:
> > I'm using st master at 7f990328e4fec8dfaaad311cb8af2304b58c872e
> > where this OSC-52 payload is exceeding strescseq.buf's STR_BUF_SIZE
>
> The second patch expands strescseq.buf's length to support copying up
> to 131k base64 (or 43k plain text) characters via the OSC-52 payload.

Oops, I made a mistake in that second patch.  The corrected version is
attached and available at https://github.com/sunaku/.st/compare/OSC-52

STR_BUF_SIZ: 131k payload (43k text) for OSC-52

diff --git a/st.c b/st.c
index 7c7ddff..3f56e28 100644
--- a/st.c
+++ b/st.c
@@ -48,7 +48,7 @@ char *argv0;
 #define UTF_INVALID   0xFFFD
 #define ESC_BUF_SIZ   (128*UTF_SIZ)
 #define ESC_ARG_SIZ   16
-#define STR_BUF_SIZ   ESC_BUF_SIZ
+#define STR_BUF_SIZ   (256*ESC_BUF_SIZ) /* OSC-52: 131k payload (43k text) */
 #define STR_ARG_SIZ   ESC_ARG_SIZ
 
 /* macros */
From 21748d80b1275bc9521fe3b2448e2f4ebd4a4f12 Mon Sep 17 00:00:00 2001
From: "Suraj N. Kurapati" 
Date: Thu, 17 Aug 2017 23:14:03 -0700
Subject: [PATCH 2/2] STR_BUF_SIZ: 131k payload (43k text) for OSC-52

---
 st.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/st.c b/st.c
index 7c7ddff..3f56e28 100644
--- a/st.c
+++ b/st.c
@@ -48,7 +48,7 @@ char *argv0;
 #define UTF_INVALID   0xFFFD
 #define ESC_BUF_SIZ   (128*UTF_SIZ)
 #define ESC_ARG_SIZ   16
-#define STR_BUF_SIZ   ESC_BUF_SIZ
+#define STR_BUF_SIZ   (256*ESC_BUF_SIZ) /* OSC-52: 131k payload (43k text) */
 #define STR_ARG_SIZ   ESC_ARG_SIZ
 
 /* macros */
-- 
2.14.1



pgpyl4ZbEuI6B.pgp
Description: OpenPGP digital signature


[dev] Re: [st] OSC-52 payload being truncated

2017-08-18 Thread Suraj N. Kurapati
On Tue, 15 Aug 2017 01:05:47 -0700, Suraj N. Kurapati wrote:
> I'm using st master at 7f990328e4fec8dfaaad311cb8af2304b58c872e where
> this OSC-52 payload is exceeding strescseq.buf's STR_BUF_SIZE length:
> 
> printf
> '\033]52;;Rm9yIGRlYnVnZ2luZywgcnVubmluZyB0bXV4IHdpdGggLXYgb3IgLXZ2IHdpbGwgZ2VuZXJhdGUgc2VydmVyIGFuZCBjbGllbnQgbG9nCmZpbGVzIGluIHRoZSBjdXJyZW50IGRpcmVjdG9yeS4KCnRtdXggbWFpbGluZyBsaXN0cyBhcmUgYXZhaWxhYmxlLiBGb3IgZ2VuZXJhbCBkaXNjdXNzaW9uIGFuZCBidWcgcmVwb3J0czoKCiAgICAgICAgaHR0cHM6Ly9ncm91cHMuZ
> 29vZ2xlLmNvbS9mb3J1bS8jIWZvcnVtL3RtdXgtdXNlcnMKCkFuZCBmb3IgR2l0IGNvbW1pdCBlbWFpbHM6CgogICAgICAgIGh0dHBzOi8vZ3JvdXBzLmdvb2dsZS5jb20vZm9ydW0vIyFmb3J1bS90bXV4LWdpdAoKU3Vic2NyaWJlIGJ5IHNlbmRpbmcgYW4gZW1haWwgdG8gPHRtdXgtdXNlcnMrcw==\a'

I've written two patches to address this issue, which are attached
herein and available at https://github.com/sunaku/.st/compare/OSC-52

The first patch addresses the intolerance of linebreaks in base64dec():

base64dec: skip non-printable characters like \r\n

Non-printable characters, such as line breaks, in a base64 encoded
string violate the "string length must be a multiple of four" rule.

This patch pads the result buffer by one extra unit of four bytes,
and skips over non-printable characters found in the input string.

The second patch expands strescseq.buf's length to support copying up
to 131k base64 (or 43k plain text) characters via the OSC-52 payload.

Please review these patches, optionally morphing them per your taste,
and add their functionality into st on behalf of tmux users like me.

Thanks for your consideration.
From a9c4772bc66057d78d346b9a7e8b45b220c5b812 Mon Sep 17 00:00:00 2001
From: "Suraj N. Kurapati" 
Date: Thu, 17 Aug 2017 23:00:10 -0700
Subject: [PATCH 1/2] base64dec: skip non-printable characters like \r\n

Non-printable characters, such as line breaks, in a base64 encoded
string violate the "string length must be a multiple of four" rule.

This patch pads the result buffer by one extra unit of four bytes,
and skips over non-printable characters found in the input string.
---
 st.c | 17 -
 1 file changed, 12 insertions(+), 5 deletions(-)

diff --git a/st.c b/st.c
index ae93ade..7c7ddff 100644
--- a/st.c
+++ b/st.c
@@ -386,6 +386,13 @@ static const char base64_digits[] = {
 	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
 };
 
+char
+base64dec_getc(const char **src)
+{
+	while (**src && !isprint(**src)) (*src)++;
+	return *((*src)++);
+}
+
 char *
 base64dec(const char *src)
 {
@@ -393,13 +400,13 @@ base64dec(const char *src)
 	char *result, *dst;
 
 	if (in_len % 4)
-		return NULL;
+		in_len += 4 - (in_len % 4);
 	result = dst = xmalloc(in_len / 4 * 3 + 1);
 	while (*src) {
-		int a = base64_digits[(unsigned char) *src++];
-		int b = base64_digits[(unsigned char) *src++];
-		int c = base64_digits[(unsigned char) *src++];
-		int d = base64_digits[(unsigned char) *src++];
+		int a = base64_digits[(unsigned char) base64dec_getc()];
+		int b = base64_digits[(unsigned char) base64dec_getc()];
+		int c = base64_digits[(unsigned char) base64dec_getc()];
+		int d = base64_digits[(unsigned char) base64dec_getc()];
 
 		*dst++ = (a << 2) | ((b & 0x30) >> 4);
 		if (c == -1)
-- 
2.14.1

From 1b2f24c5f2c4a920c6d23a6b686d72e0613e9d1b Mon Sep 17 00:00:00 2001
From: "Suraj N. Kurapati" 
Date: Thu, 17 Aug 2017 23:14:03 -0700
Subject: [PATCH 2/2] OSC-52: allow 131k base64 (43k plain) text payload

---
 st.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/st.c b/st.c
index 7c7ddff..912af98 100644
--- a/st.c
+++ b/st.c
@@ -48,7 +48,7 @@ char *argv0;
 #define UTF_INVALID   0xFFFD
 #define ESC_BUF_SIZ   (128*UTF_SIZ)
 #define ESC_ARG_SIZ   16
-#define STR_BUF_SIZ   ESC_BUF_SIZ
+#define STR_BUF_SIZ   (256*UTF_SIZ) /* OSC-52: 131k base64 (43k plain) text */
 #define STR_ARG_SIZ   ESC_ARG_SIZ
 
 /* macros */
-- 
2.14.1



pgp0M_bzRPfrh.pgp
Description: OpenPGP digital signature