shithub: riscv

Download patch

ref: e934530ee4512f033952e4efa80a80058607b741
parent: c848ca62673689df20e7fef74d1d5180f37fcea6
author: Ori Bernstein <ori@eigenstate.org>
date: Sat Jul 3 16:03:17 EDT 2021

libc: add encode(2) variants for custom alphabets

There are a number of alphabets in common use for base32
and base64 encoding, such as url-safe encodings.

This adds support for passing a function to encode into
arbitary alphabets.

--- a/sys/include/libc.h
+++ b/sys/include/libc.h
@@ -385,10 +385,15 @@
 extern	char*	cleanname(char*);
 extern	int	decrypt(void*, void*, int);
 extern	int	encrypt(void*, void*, int);
+
 extern	int	dec64(uchar*, int, char*, int);
 extern	int	enc64(char*, int, uchar*, int);
+extern	int	dec64x(uchar*, int, char*, int, int (*)(int));
+extern	int	enc64x(char*, int, uchar*, int, int (*)(int));
 extern	int	dec32(uchar*, int, char*, int);
 extern	int	enc32(char*, int, uchar*, int);
+extern	int	dec32x(uchar*, int, char*, int, int (*)(int));
+extern	int	enc32x(char*, int, uchar*, int, int (*)(int));
 extern	int	dec16(uchar*, int, char*, int);
 extern	int	enc16(char*, int, uchar*, int);
 extern	int	dec64chr(int);
@@ -397,6 +402,7 @@
 extern	int	enc32chr(int);
 extern	int	dec16chr(int);
 extern	int	enc16chr(int);
+
 extern	int	encodefmt(Fmt*);
 extern	void	exits(char*);
 extern	double	frexp(double, int*);
--- a/sys/man/2/encode
+++ b/sys/man/2/encode
@@ -1,6 +1,7 @@
 .TH ENCODE 2
 .SH NAME
 dec64, enc64, dec32, enc32, dec16, enc16, \
+dec64x, enc64x, dec32x, enc32x, \
 dec64chr, enc64chr, dec32chr, enc32chr, dec16chr, enc16chr, \
 encodefmt \- encoding byte arrays as strings
 .SH SYNOPSIS
@@ -12,15 +13,27 @@
 int	dec64(uchar *out, int lim, char *in, int n)
 .PP
 .B
+int	dec64x(uchar *out, int lim, char *in, int n, int (*map)(int))
+.PP
+.B
 int	enc64(char *out, int lim, uchar *in, int n)
 .PP
 .B
+int	enc64x(char *out, int lim, uchar *in, int n, int (*map)(int))
+.PP
+.B
 int	dec32(uchar *out, int lim, char *in, int n)
 .PP
 .B
+int	dec32x(uchar *out, int lim, char *in, int n, int (*map)(int))
+.PP
+.B
 int	enc32(char *out, int lim, uchar *in, int n)
 .PP
 .B
+int	enc32x(char *out, int lim, uchar *in, int n, int (*map)(int))
+.PP
+.B
 int	dec16(uchar *out, int lim, char *in, int n)
 .PP
 .B
@@ -82,6 +95,31 @@
 .I enc64chr
 encode a symbol of the alphabet given a value.
 if the value is out of range then zero is returned.
+.PP
+The
+.I enc64x
+and
+.I enc32x
+variants are identical to the above, except that they take a
+function mapping from an arbitrary index in the alphabet to
+the encoded character.
+For example, in the following 32-character alphabet,
+.EX
+.I ABCDEFGHIJKLMNOPQRSTUVWXYZ234567
+.EE
+the chr function would map the value
+.I 3
+to the character
+.IR D .
+
+The
+.I dec64x
+and
+.I dec32x
+variants are similar to the above, however the function passed
+maps from a character within the alphabet to the index within
+the alphabet.
+
 .PP
 .I Encodefmt
 can be used with
--- a/sys/src/libc/port/u32.c
+++ b/sys/src/libc/port/u32.c
@@ -25,7 +25,7 @@
 }
 
 int
-dec32(uchar *dest, int ndest, char *src, int nsrc)
+dec32x(uchar *dest, int ndest, char *src, int nsrc, int (*chr)(int))
 {
 	uchar *start;
 	int i, j, u[8];
@@ -35,7 +35,7 @@
 	start = dest;
 	while(nsrc>=8){
 		for(i=0; i<8; i++){
-			j = dec32chr(src[i]);
+			j = chr(src[i]);
 			if(j < 0)
 				j = 0;
 			u[i] = j;
@@ -52,7 +52,7 @@
 		if(nsrc == 1 || nsrc == 3 || nsrc == 6)
 			return -1;
 		for(i=0; i<nsrc; i++){
-			j = dec32chr(src[i]);
+			j = chr(src[i]);
 			if(j < 0)
 				j = 0;
 			u[i] = j;
@@ -73,7 +73,7 @@
 }
 
 int
-enc32(char *dest, int ndest, uchar *src, int nsrc)
+enc32x(char *dest, int ndest, uchar *src, int nsrc, int (*chr)(int))
 {
 	char *start;
 	int j;
@@ -83,50 +83,62 @@
 	start = dest;
 	while(nsrc>=5){
 		j = (0x1f & (src[0]>>3));
-		*dest++ = enc32chr(j);
+		*dest++ = chr(j);
 		j = (0x1c & (src[0]<<2)) | (0x03 & (src[1]>>6));
-		*dest++ = enc32chr(j);
+		*dest++ = chr(j);
 		j = (0x1f & (src[1]>>1));
-		*dest++ = enc32chr(j);
+		*dest++ = chr(j);
 		j = (0x10 & (src[1]<<4)) | (0x0f & (src[2]>>4));
-		*dest++ = enc32chr(j);
+		*dest++ = chr(j);
 		j = (0x1e & (src[2]<<1)) | (0x01 & (src[3]>>7));
-		*dest++ = enc32chr(j);
+		*dest++ = chr(j);
 		j = (0x1f & (src[3]>>2));
-		*dest++ = enc32chr(j);
+		*dest++ = chr(j);
 		j = (0x18 & (src[3]<<3)) | (0x07 & (src[4]>>5));
-		*dest++ = enc32chr(j);
+		*dest++ = chr(j);
 		j = (0x1f & (src[4]));
-		*dest++ = enc32chr(j);
+		*dest++ = chr(j);
 		src  += 5;
 		nsrc -= 5;
 	}
 	if(nsrc){
 		j = (0x1f & (src[0]>>3));
-		*dest++ = enc32chr(j);
+		*dest++ = chr(j);
 		j = (0x1c & (src[0]<<2));
 		if(nsrc == 1)
 			goto out;
 		j |= (0x03 & (src[1]>>6));
-		*dest++ = enc32chr(j);
+		*dest++ = chr(j);
 		j = (0x1f & (src[1]>>1));
-		*dest++ = enc32chr(j);
+		*dest++ = chr(j);
 		j = (0x10 & (src[1]<<4));
 		if(nsrc == 2)
 			goto out;
 		j |= (0x0f & (src[2]>>4));
-		*dest++ = enc32chr(j);
+		*dest++ = chr(j);
 		j = (0x1e & (src[2]<<1));
 		if(nsrc == 3)
 			goto out;
 		j |= (0x01 & (src[3]>>7));
-		*dest++ = enc32chr(j);
+		*dest++ = chr(j);
 		j = (0x1f & (src[3]>>2));
-		*dest++ = enc32chr(j);
+		*dest++ = chr(j);
 		j = (0x18 & (src[3]<<3));
 out:
-		*dest++ = enc32chr(j);
+		*dest++ = chr(j);
 	}
 	*dest = 0;
 	return dest-start;
+}
+
+int
+enc32(char *dest, int ndest, uchar *src, int nsrc)
+{
+	return enc32x(dest, ndest, src, nsrc, enc32chr);
+}
+
+int
+dec32(uchar *dest, int ndest, char *src, int nsrc)
+{
+	return dec32x(dest, ndest, src, nsrc, dec32chr);
 }
--- a/sys/src/libc/port/u64.c
+++ b/sys/src/libc/port/u64.c
@@ -30,7 +30,7 @@
 }
 
 int
-dec64(uchar *out, int lim, char *in, int n)
+dec64x(uchar *out, int lim, char *in, int n, int (*chr)(int))
 {
 	ulong b24;
 	uchar *start = out;
@@ -40,7 +40,7 @@
 	b24 = 0;
 	i = 0;
 	while(n-- > 0){
-		c = dec64chr(*in++);
+		c = chr(*in++);
 		if(c < 0)
 			continue;
 		switch(i){
@@ -84,7 +84,7 @@
 }
 
 int
-enc64(char *out, int lim, uchar *in, int n)
+enc64x(char *out, int lim, uchar *in, int n, int (*chr)(int))
 {
 	int i;
 	ulong b24;
@@ -97,10 +97,10 @@
 		b24 |= *in++;
 		if(out + 4 >= e)
 			goto exhausted;
-		*out++ = enc64chr(b24>>18);
-		*out++ = enc64chr((b24>>12)&0x3f);
-		*out++ = enc64chr((b24>>6)&0x3f);
-		*out++ = enc64chr(b24&0x3f);
+		*out++ = chr(b24>>18);
+		*out++ = chr((b24>>12)&0x3f);
+		*out++ = chr((b24>>6)&0x3f);
+		*out++ = chr(b24&0x3f);
 	}
 
 	switch(n%3){
@@ -109,9 +109,9 @@
 		b24 |= *in<<8;
 		if(out + 4 >= e)
 			goto exhausted;
-		*out++ = enc64chr(b24>>18);
-		*out++ = enc64chr((b24>>12)&0x3f);
-		*out++ = enc64chr((b24>>6)&0x3f);
+		*out++ = chr(b24>>18);
+		*out++ = chr((b24>>12)&0x3f);
+		*out++ = chr((b24>>6)&0x3f);
 		*out++ = '=';
 		break;
 	case 1:
@@ -118,8 +118,8 @@
 		b24 = *in<<16;
 		if(out + 4 >= e)
 			goto exhausted;
-		*out++ = enc64chr(b24>>18);
-		*out++ = enc64chr((b24>>12)&0x3f);
+		*out++ = chr(b24>>18);
+		*out++ = chr((b24>>12)&0x3f);
 		*out++ = '=';
 		*out++ = '=';
 		break;
@@ -127,4 +127,16 @@
 exhausted:
 	*out = 0;
 	return out - start;
+}
+
+int
+enc64(char *out, int lim, uchar *in, int n)
+{
+	return enc64x(out, lim, in, n, enc64chr);
+}
+
+int
+dec64(uchar *out, int lim, char *in, int n)
+{
+	return dec64x(out, lim, in, n, dec64chr);
 }