commit e008618784c381cb217148b22a8f0a7b3577a298
parent ef4624e45334281860e4b8666626bd9f1a1d2108
Author: glenda <glenda@device>
Date: Mon, 24 Oct 2022 21:30:36 +0000
add proquint
Diffstat:
6 files changed, 194 insertions(+), 0 deletions(-)
diff --git a/src/proquint/License.txt b/src/proquint/License.txt
@@ -0,0 +1,31 @@
+License.txt
+
+Copyright (c) 2009 Daniel S. Wilkerson
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are
+met:
+
+ Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in
+ the documentation and/or other materials provided with the
+ distribution.
+
+ Neither the name of Daniel S. Wilkerson nor the names of its
+ contributors may be used to endorse or promote products derived
+ from this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/src/proquint/main.c b/src/proquint/main.c
@@ -0,0 +1,24 @@
+#include <u.h>
+#include <libc.h>
+
+#include "proquint.h"
+
+void
+main(int argc, char **argv) {
+ argc--; argv++;
+ for(; *argv; --argc, ++argv) {
+ char *s = *argv;
+ if (!s[0]) continue; /* this would be very strange */
+ char *e;
+ ulong d = strtoul(s, &e, 0);
+ if (e != s) {
+ char quint[12]; /* two quints + separator + \0 */
+ uint2quint(quint, d, '-');
+ print("%s ", quint);
+ } else {
+ u32int uint0 = quint2uint(s);
+ print("0x%ux ", uint0);
+ }
+ }
+ print("\n");
+}
diff --git a/src/proquint/mkfile b/src/proquint/mkfile
@@ -0,0 +1,13 @@
+</$objtype/mkfile
+
+BIN=/$objtype/bin
+TARG=main
+OFILES=proquint.$O main.$O
+HFILES=proquint.h
+
+</sys/src/cmd/mkone
+
+distrib:V: proquint.tgz
+
+proquint.tgz: $HFILES ${OFILES:%$O=%c} License.txt readme mkfile
+ tar czf $target $prereq
diff --git a/src/proquint/proquint.c b/src/proquint/proquint.c
@@ -0,0 +1,90 @@
+#include <u.h>
+#include <libc.h>
+#include "proquint.h"
+
+/* Map uints to consonants. */
+static char uint2consonant[] = {
+ 'b', 'd', 'f', 'g',
+ 'h', 'j', 'k', 'l',
+ 'm', 'n', 'p', 'r',
+ 's', 't', 'v', 'z'
+};
+
+/* Map uints to vowels. */
+static char uint2vowel[] = {
+ 'a', 'i', 'o', 'u'
+};
+
+void
+uint2quint(char *quint /*output*/, u32int i, int sepChar) {
+ /* K&R section 2.9: "Right shifting an unsigned quantity always
+ fills vacated it with zero." */
+ u32int j;
+
+# define APPEND(X) *quint = (X); ++quint
+# define MASK_FIRST4 0xF0000000
+# define MASK_FIRST2 0xC0000000
+# define HANDLE_CONSONANT \
+ j = i & MASK_FIRST4; \
+ i <<= 4; j >>= 28; \
+ APPEND(uint2consonant[j])
+
+# define HANDLE_VOWEL \
+ j = i & MASK_FIRST2; \
+ i <<= 2; j >>= 30; \
+ APPEND(uint2vowel[j])
+
+ HANDLE_CONSONANT;
+ HANDLE_VOWEL;
+ HANDLE_CONSONANT;
+ HANDLE_VOWEL;
+ HANDLE_CONSONANT;
+
+ if (sepChar) {
+ APPEND(((char) sepChar));
+ }
+
+ HANDLE_CONSONANT;
+ HANDLE_VOWEL;
+ HANDLE_CONSONANT;
+ HANDLE_VOWEL;
+ HANDLE_CONSONANT;
+
+# undef HANDLE_VOWEL
+# undef HANDLE_CONSONANT
+# undef APPEND
+# undef MASK_FIRST2
+# undef MASK_FIRST4
+}
+
+u32int
+quint2uint(char *quint) {
+ u32int res = 0;
+ char c;
+ for(; (c=*quint); ++quint) {
+ switch(c) {
+ case 'b': res <<= 4; res += 0; break;
+ case 'd': res <<= 4; res += 1; break;
+ case 'f': res <<= 4; res += 2; break;
+ case 'g': res <<= 4; res += 3; break;
+ case 'h': res <<= 4; res += 4; break;
+ case 'j': res <<= 4; res += 5; break;
+ case 'k': res <<= 4; res += 6; break;
+ case 'l': res <<= 4; res += 7; break;
+ case 'm': res <<= 4; res += 8; break;
+ case 'n': res <<= 4; res += 9; break;
+ case 'p': res <<= 4; res += 10; break;
+ case 'r': res <<= 4; res += 11; break;
+ case 's': res <<= 4; res += 12; break;
+ case 't': res <<= 4; res += 13; break;
+ case 'v': res <<= 4; res += 14; break;
+ case 'z': res <<= 4; res += 15; break;
+ case 'a': res <<= 2; res += 0; break;
+ case 'i': res <<= 2; res += 1; break;
+ case 'o': res <<= 2; res += 2; break;
+ case 'u': res <<= 2; res += 3; break;
+ default: break;
+ }
+ }
+ return res;
+}
diff --git a/src/proquint/proquint.h b/src/proquint/proquint.h
@@ -0,0 +1,22 @@
+/*
+Convert between proquint, hex, and decimal strings.
+
+Four-bits as a consonant:
+ 0 1 2 3 4 5 6 7 8 9 A B C D E F
+ b d f g h j k l m n p r s t v z
+
+Two-bits as a vowel:
+ 0 1 2 3
+ a i o u
+
+Whole 16-bit word, where "con" = consonant, "vo" = vowel.
+ 0 1 2 3 4 5 6 7 8 9 A B C D E F
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ |con0 |vo1|con2 |vo3|con4 |
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ |-Hex0--|-Hex1--|-Hex2--|-Hex3--|
+ +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+*/
+
+void uint2quint(char *quint /*output*/, u32int i, int sepChar);
+u32int quint2uint(char *quint);
diff --git a/src/proquint/readme b/src/proquint/readme
@@ -0,0 +1,14 @@
+Port of original proquint implementation to Plan9 OS.
+
+Porting was surprisingly easy, for "library" code the only thing that
+had to be done is fixing types. "main" code was almost fully
+rewritten using built-in string-to-integer function. Input format
+changed slightly as a result.
+
+Other things done are style changes, replacing spaces with tabs,
+removing empty lines, and I also took libery of removing
+authorship-related comments due to personal aesthetic tastes. License
+file and this readme are good enough I believe.
+
+Original code: https://github.com/dsw/proquint
+Original proposal: https://arxiv.org/html/0901.4016