shithub: pdffs

Download patch

ref: 6d76ea1fa1c03836c1f1caedbc37c78a5aaa214a
parent: e74f20021a4e51323d1c0f9356e9ff2a8030139c
author: Sigrid Solveig Haflínudóttir <ftrvxmtrx@gmail.com>
date: Sat Apr 10 12:41:26 EDT 2021

RunLengthDecode

--- a/f_runlength.c
+++ b/f_runlength.c
@@ -2,11 +2,43 @@
 #include <libc.h>
 #include "pdf.h"
 
+/* 7.4.5 RunLengthDecode filter */
+
 static int
 flreadall(void *aux, Buffer *bi, Buffer *bo)
 {
+	int sz, i;
+	uchar *p;
+
 	USED(aux);
-	bufput(bo, bi->b, bi->sz);
+
+	p = bufdata(bi, &sz);
+	for(; sz > 0;){
+		i = *p++;
+		sz--;
+
+		if(i == 0x80) /* EOD */
+			break;
+		if(i > 0x80){
+			if(sz < 1){
+				werrstr("no byte to repeat");
+				return -1;
+			}
+			for(i = 257 - i; i > 0; i--)
+				bufput(bo, p, 1);
+		}else{
+			i++;
+			if(i > sz){
+				werrstr("short data");
+				return -1;
+			}
+			bufput(bo, p, i);
+			p += i;
+			sz -= i;
+		}
+	}
+	bi->off = bi->sz;
+
 	return 0;
 }