File: readrle.c

package info (click to toggle)
nomarch 1.4-4
  • links: PTS
  • area: main
  • in suites: bullseye, sid
  • size: 136 kB
  • sloc: ansic: 912; makefile: 70; sh: 23
file content (80 lines) | stat: -rw-r--r-- 1,513 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/* nomarch 1.0 - extract old `.arc' archives.
 * Copyright (C) 2001 Russell Marks. See main.c for license details.
 *
 * readrle.c - read RLE-compressed files.
 *
 * Also provides the generic outputrle() for the other RLE-using methods
 * to use.
 */

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>

#include "readrle.h"


static unsigned char *data_in_point,*data_in_max;
static unsigned char *data_out_point,*data_out_max;


static void rawoutput(int byte)
{
if(data_out_point<data_out_max)
  *data_out_point++=byte;
}


/* call with -1 before starting, to make sure state is initialised */
void outputrle(int chr,void (*outputfunc)(int))
{
static int lastchr=0,repeating=0;
int f;

if(chr==-1)
  {
  lastchr=repeating=0;
  return;
  }

if(repeating)
  {
  if(chr==0)
    (*outputfunc)(0x90);
  else
    for(f=1;f<chr;f++)
      (*outputfunc)(lastchr);
  repeating=0;
  }
else
  {
  if(chr==0x90)
    repeating=1;
  else
    {
    (*outputfunc)(chr);
    lastchr=chr;
    }
  }
}


unsigned char *convert_rle(unsigned char *data_in,
                           unsigned long in_len,
                           unsigned long orig_len)
{
unsigned char *data_out;

if((data_out=malloc(orig_len))==NULL)
  fprintf(stderr,"nomarch: out of memory!\n"),exit(1);

data_in_point=data_in; data_in_max=data_in+in_len;
data_out_point=data_out; data_out_max=data_out+orig_len;
outputrle(-1,NULL);

while(data_in_point<data_in_max)
  outputrle(*data_in_point++,rawoutput);

return(data_out);
}