#include "globals.h"
#include "../fileio/fileglob.h"
#include "sit.h"
#include "masks.h"

unsigned char getb();

void sit_rle(ibytes)
unsigned long ibytes;
{
    int ch, lastch, n, i;

    while(ibytes != 0) {
	ch = getb(infp) & BYTEMASK;
	ibytes--;
	if(ch == ESC) {
	    n = (getb(infp) & BYTEMASK) - 1;
	    ibytes--;
	    if(n < 0) {
		*out_ptr++ = ESC;
		lastch = ESC;
		n = 1;
	    } else {
		for(i = 0; i < n; i++) {
		    *out_ptr++ = lastch;
		}
	    }
	} else {
	    *out_ptr++ = ch;
	    lastch = ch;
	}
    }
}

