shithub: scc

Download patch

ref: 261ecb5719d526648014a60787a35caaeb2bf593
parent: 5214729f407f56f038c8a8fec2eca597f018c93d
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Wed Mar 7 15:51:05 EST 2018

[lib/scc] Add bpack() and bunpack()

These are the big endian versions of lunpack and lpack. This patch
also fixes lpack() and lunpack() because they were doing the
big endian job.

--- /dev/null
+++ b/lib/scc/bpack.c
@@ -1,0 +1,52 @@
+#include <stdarg.h>
+
+#include "../../inc/scc.h"
+
+int
+bpack(unsigned char *dst, char *fmt, ...)
+{
+	unsigned char *bp;
+	unsigned s;
+	unsigned long l;
+	unsigned long long q;
+	va_list va;
+
+	bp = dst;
+	va_start(va, fmt);
+	while (*fmt) {
+		switch (*fmt++) {
+		case 'c':
+			*bp++ = va_arg(va, unsigned);
+			break;
+		case 's':
+			s = va_arg(va, unsigned);
+			*bp++ = s >> 8;
+			*bp++ = s;
+			break;
+		case 'l':
+			l = va_arg(va, unsigned long);
+			*bp++ = l >> 24;
+			*bp++ = l >> 16;
+			*bp++ = l >> 8;
+			*bp++ = l;
+			break;
+		case 'q':
+			q = va_arg(va, unsigned long long);
+			*bp++ = q >> 56;
+			*bp++ = q >> 48;
+			*bp++ = q >> 40;
+			*bp++ = q >> 32;
+			*bp++ = q >> 24;
+			*bp++ = q >> 16;
+			*bp++ = q >> 8;
+			*bp++ = q;
+			break;
+		default:
+			va_end(va);
+			return -1;
+		}
+	}
+	va_end(va);
+
+	return bp - dst;
+}
--- /dev/null
+++ b/lib/scc/bunpack.c
@@ -1,0 +1,71 @@
+static char sccsid[] = "@(#) ./lib/scc/lunpack.c";
+
+#include <ctype.h>
+#include <stdarg.h>
+
+#include "../../inc/scc.h"
+
+int
+lunpack(unsigned char *src, char *fmt, ...)
+{
+	unsigned char *bp, *cp;
+	unsigned short *sp;
+	unsigned s;
+	unsigned long *lp, l;
+	unsigned long long *qp, q;
+	va_list va;
+	size_t n;
+	int d;
+
+	bp = src;
+	va_start(va, fmt);
+	while (*fmt) {
+		switch (*fmt++) {
+		case '\'':
+			for (n = 0; isdigit(*fmt); n += d) {
+				n *= 10;
+				d = *fmt++ - '0';
+			}
+			cp = va_arg(va, char *);
+			while (n--)
+				*cp++ = *bp++;
+			break;
+		case 'c':
+			cp = va_arg(va, unsigned char *);
+			*cp = *bp++;
+			break;
+		case 's':
+			sp = va_arg(va, unsigned short *);
+			s =  (unsigned) *bp++ << 8;
+			s |= (unsigned) *bp++;
+			*sp = s;
+			break;
+		case 'l':
+			lp = va_arg(va, unsigned long *);
+			l =  (unsigned long) *bp++ << 24;
+			l |= (unsigned long) *bp++ << 16;
+			l |= (unsigned long) *bp++ << 8;
+			l |= (unsigned long) *bp++;
+			*lp = l;
+			break;
+		case 'q':
+			qp = va_arg(va, unsigned long long *);
+			q =  (unsigned long long) *bp++ << 56;
+			q |= (unsigned long long) *bp++ << 48;
+			q |= (unsigned long long) *bp++ << 40;
+			q |= (unsigned long long) *bp++ << 32;
+			q |= (unsigned long long) *bp++ << 24;
+			q |= (unsigned long long) *bp++ << 16;
+			q |= (unsigned long long) *bp++ << 8;
+			q |= (unsigned long long) *bp++;
+			*qp = q;
+			break;
+		default:
+			va_end(va);
+			return -1;
+		}
+	}
+	va_end(va);
+
+	return bp - src;
+}
--- a/lib/scc/libdep.mk
+++ b/lib/scc/libdep.mk
@@ -9,5 +9,7 @@
           $(LIBDIR)/casecmp.o \
           $(LIBDIR)/lunpack.o \
           $(LIBDIR)/lpack.o \
+          $(LIBDIR)/bunpack.o \
+          $(LIBDIR)/bpack.o \
           $(LIBDIR)/wmyro.o \
           $(LIBDIR)/rmyro.o \
--- a/lib/scc/lpack.c
+++ b/lib/scc/lpack.c
@@ -3,7 +3,7 @@
 #include "../../inc/scc.h"
 
 int
-lpack(unsigned char *dst, char *fmt, ...)
+bpack(unsigned char *dst, char *fmt, ...)
 {
 	unsigned char *bp;
 	unsigned s;
@@ -20,26 +20,26 @@
 			break;
 		case 's':
 			s = va_arg(va, unsigned);
-			*bp++ = s >> 8;
 			*bp++ = s;
+			*bp++ = s >> 8;
 			break;
 		case 'l':
 			l = va_arg(va, unsigned long);
-			*bp++ = l >> 24;
-			*bp++ = l >> 16;
-			*bp++ = l >> 8;
 			*bp++ = l;
+			*bp++ = l >> 8;
+			*bp++ = l >> 16;
+			*bp++ = l >> 24;
 			break;
 		case 'q':
 			q = va_arg(va, unsigned long long);
-			*bp++ = q >> 56;
-			*bp++ = q >> 48;
-			*bp++ = q >> 40;
-			*bp++ = q >> 32;
-			*bp++ = q >> 24;
-			*bp++ = q >> 16;
-			*bp++ = q >> 8;
 			*bp++ = q;
+			*bp++ = q >> 8;
+			*bp++ = q >> 16;
+			*bp++ = q >> 24;
+			*bp++ = q >> 32;
+			*bp++ = q >> 40;
+			*bp++ = q >> 48;
+			*bp++ = q >> 56;
 			break;
 		default:
 			va_end(va);
--- a/lib/scc/lunpack.c
+++ b/lib/scc/lunpack.c
@@ -1,3 +1,6 @@
+static char sccsid[] = "@(#) ./lib/scc/lunpack.c";
+
+#include <ctype.h>
 #include <stdarg.h>
 
 #include "../../inc/scc.h"
@@ -11,11 +14,22 @@
 	unsigned long *lp, l;
 	unsigned long long *qp, q;
 	va_list va;
+	size_t n;
+	int d;
 
 	bp = src;
 	va_start(va, fmt);
 	while (*fmt) {
 		switch (*fmt++) {
+		case '\'':
+			for (n = 0; isdigit(*fmt); n += d) {
+				n *= 10;
+				d = *fmt++ - '0';
+			}
+			cp = va_arg(va, char *);
+			while (n--)
+				*cp++ = *bp++;
+			break;
 		case 'c':
 			cp = va_arg(va, unsigned char *);
 			*cp = *bp++;
@@ -22,28 +36,28 @@
 			break;
 		case 's':
 			sp = va_arg(va, unsigned short *);
-			s =  (unsigned) *bp++ << 8;
-			s |= (unsigned) *bp++;
+			s =  (unsigned) *bp++;
+			s |= (unsigned) *bp++ << 8;
 			*sp = s;
 			break;
 		case 'l':
 			lp = va_arg(va, unsigned long *);
-			l =  (unsigned long) *bp++ << 24;
-			l |= (unsigned long) *bp++ << 16;
+			l = (unsigned long) *bp++;
 			l |= (unsigned long) *bp++ << 8;
-			l |= (unsigned long) *bp++;
+			l |= (unsigned long) *bp++ << 16;
+			l |= (unsigned long) *bp++ << 24;
 			*lp = l;
 			break;
 		case 'q':
 			qp = va_arg(va, unsigned long long *);
-			q =  (unsigned long long) *bp++ << 56;
-			q |= (unsigned long long) *bp++ << 48;
-			q |= (unsigned long long) *bp++ << 40;
-			q |= (unsigned long long) *bp++ << 32;
-			q |= (unsigned long long) *bp++ << 24;
-			q |= (unsigned long long) *bp++ << 16;
+			q = (unsigned long long) *bp++;
 			q |= (unsigned long long) *bp++ << 8;
-			q |= (unsigned long long) *bp++;
+			q |= (unsigned long long) *bp++ << 16;
+			q |= (unsigned long long) *bp++ << 24;
+			q |= (unsigned long long) *bp++ << 32;
+			q |= (unsigned long long) *bp++ << 40;
+			q |= (unsigned long long) *bp++ << 48;
+			q |= (unsigned long long) *bp++ << 56;
 			*qp = q;
 			break;
 		default: