shithub: pdffs

ref: 14a773539df70e5fe32c31979abf032eba58ecf4
dir: /f_runlength.c/

View raw version
#include <u.h>
#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);

	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);
			p++;
		}else{
			i++;
			if(i > sz){
				werrstr("short data");
				return -1;
			}
			bufput(bo, p, i);
			p += i;
			sz -= i;
		}
	}
	bi->off = bi->sz;

	return 0;
}

Filter filterRunLength = {
	.name = "RunLengthDecode",
	.readall = flreadall,
};