Skip to content

Commit

Permalink
Merge pull request agalakhov#45 from missla/tests
Browse files Browse the repository at this point in the history
Make tests compilation easy
  • Loading branch information
agalakhov authored Apr 9, 2022
2 parents 9d0584b + fecc8d1 commit 00e9793
Show file tree
Hide file tree
Showing 6 changed files with 328 additions and 29 deletions.
5 changes: 4 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ AC_INIT([captdriver], [0.1.3])

AC_CONFIG_SRCDIR([src/std.h])

AM_INIT_AUTOMAKE([1.9 no-dist-gzip dist-xz tar-ustar])
AM_INIT_AUTOMAKE([1.9 no-dist-gzip dist-xz tar-ustar subdir-objects])
AM_SILENT_RULES([yes])

AC_LANG([C])
AC_PROG_CC

Expand All @@ -18,5 +20,6 @@ AC_SUBST(CUPS_LIBS)

AC_CONFIG_FILES([Makefile
src/Makefile
tests/Makefile
])
AC_OUTPUT
56 changes: 40 additions & 16 deletions tests/captdefilter.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

#include "word.h"
#include "../src/word.h"
#include "scoa-decompress.h"
#include "hiscoa-decompress.h"

#include <stdbool.h>
Expand All @@ -18,6 +19,7 @@ static struct page page;

static bool hi_mode = false;
static unsigned line_size;
static uint8_t *buffer_line;
static struct hiscoa_params hiscoa_params;

static uint8_t *page_reserve(size_t size)
Expand Down Expand Up @@ -96,14 +98,16 @@ static void decode_hiscoa_band_data(const uint8_t *buf, size_t size, unsigned li

static void decode_hiscoa_band(const uint8_t *buf, size_t size)
{
unsigned lines = WORD(buf[2], buf[3]);
decode_hiscoa_band_data(buf + 4, size - 4, lines);
unsigned lines = WORD(buf[0], buf[1]);
decode_hiscoa_band_data(buf + 2, size - 2, lines);
}
static void decode_hiscoa_band2(const uint8_t *buf, size_t size)

static void decode_hiscoa_band3(const uint8_t *buf, size_t size)
{
unsigned lines = WORD_(buf[2], buf[3]);
decode_hiscoa_band_data(buf + 8, size - 8, lines);
unsigned lines = WORD(buf[0], buf[1]);
decode_hiscoa_band_data(buf + 6, size - 6, lines);
}

static void dispatch(uint16_t cmd, const uint8_t *buf, size_t size)
{
switch (cmd) {
Expand All @@ -122,6 +126,9 @@ static void dispatch(uint16_t cmd, const uint8_t *buf, size_t size)
dump(buf, size);
line_size = WORD(buf[26], buf[27]);
fprintf(stderr, " decoded: L=%u bytes, %u pixels\n", line_size, line_size * 8);
buffer_line=calloc(line_size, sizeof(uint8_t));
if (! buffer_line)
abort();
break;
case 0xD0A4:
fprintf(stderr, " -(Hi-SCoA parameters)-\n");
Expand All @@ -135,23 +142,22 @@ static void dispatch(uint16_t cmd, const uint8_t *buf, size_t size)
break;
case 0xC0A0:
if (! hi_mode) {
fprintf(stderr, " -(SCoA data)-\n");
dump(buf, 16);
} else {
fprintf(stderr, " -(Hi-SCoA data from printer debug)-\n");
dump(buf, 4);
dump(buf, 16);
decode_hiscoa_band_data(buf, size, 70);
}
break;
case 0x8000:
fprintf(stderr, " -(Hi-SCoA data)-\n");
dump(buf, 4);
dump(buf + 2, 4);
decode_hiscoa_band(buf, size);
break;
case 0x8200:
fprintf(stderr, " -(Hi-SCoA data 0x8200)-\n");
dump(buf, 4);
decode_hiscoa_band2(buf, size);
fprintf(stderr, " -(Hi-SCoA data)-\n");
dump(buf + 6, 4);
decode_hiscoa_band3(buf, size);
break;
case 0xC0A4:
fprintf(stderr, " --end--\n");
Expand Down Expand Up @@ -197,30 +203,48 @@ int main(int argc, char **argv)
pos = 4;
cmd = WORD(buf[0], buf[1]);
switch (cmd) {
case 0x8000:
fread(buf + pos, 1, 2, input);
pos += 2;
len = WORD(buf[4], buf[5]);
len <<= 16;
len += WORD(buf[2], buf[3]);
break;
case 0x8200:
fread(buf + pos, 1, 4, input);
pos += 4;
len = WORD(buf[6], buf[7]);
len <<= 16;
len += WORD(buf[4], buf[5]);
pos=0;
fseek(input, -8, SEEK_CUR);
break;
default:
len = WORD(buf[2], buf[3]);
break;
}
fprintf(stderr, "CMD %04X len=%u\n", cmd, len);
if (fread(buf + pos, 1, len - pos, input) != len - pos) {
fprintf(stderr, "! unable to read %u bytes\n", len - pos);
fprintf(stderr, "! unable to read %li bytes\n", len - pos);
break;
}
switch (cmd) {
case 0x8000:
dispatch(cmd, buf + pos, len - pos);
break;
case 0x8200:
dispatch(cmd, buf + pos - 6, len - pos + 6);
break;
default:
dispatch(cmd, buf + pos, len - pos);
break;
}
dispatch(cmd, buf + pos, len - pos);
}

if (page.size)
page_output();

if (buffer_line)
free(buffer_line);

if (argc > 1)
fclose(input);
return 0;
Expand Down
227 changes: 227 additions & 0 deletions tests/captdefilter.c.orig
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@

#include "word.h"
#include "hiscoa-decompress.h"

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>

struct page {
size_t reserve;
size_t size;
uint8_t *data;
};

static struct page page;

static bool hi_mode = false;
static unsigned line_size;
static struct hiscoa_params hiscoa_params;

static uint8_t *page_reserve(size_t size)
{
size_t reserve = page.reserve;
while (page.size + size > reserve)
reserve *= 2;
if (reserve != page.reserve) {
page.reserve = reserve;
page.data = realloc(page.data, page.reserve);
if (! page.data)
abort();
}
return page.data + page.size;
}

static void page_add(const uint8_t *buf, size_t size)
{
memcpy(page_reserve(size), buf, size);
page.size += size;
}

static void page_output(void)
{
unsigned w = line_size * 8;
unsigned h = page.size / line_size;
printf("P4\n#\n%u %u\n", w, h);
fwrite(page.data, line_size, h, stdout);
fprintf(stderr, "output page %ux%u px\n", w, h);
page.size = 0;
}

static void dump(const uint8_t *buf, size_t size)
{
size_t i;
for (i = 1; i <= size; ++i) {
fprintf(stderr, " %02x", buf[i - 1]);
if (i % 16 == 0 || i == size)
fprintf(stderr, "\n");
}
}

static void decode_hiscoa_params(const uint8_t *buf, size_t size)
{
hiscoa_params.origin_3 = (int8_t)buf[0];
hiscoa_params.origin_5 = (int8_t)buf[1];
/* buf[2] - ??? */
/* buf[3] - ??? */
hiscoa_params.origin_0 = (int8_t) buf[4]; /* ??? */
hiscoa_params.origin_2 = (int8_t) buf[5];
hiscoa_params.origin_4 = (int16_t) WORD(buf[6], buf[7]);
}

static void decode_hiscoa_band_data(const uint8_t *buf, size_t size, unsigned lines)
{
const uint8_t *src;
size_t srcsize;
uint8_t *dest;
size_t destsize = 0;
unsigned type;
src = buf;
srcsize = size;
hiscoa_decompress_band((const void **)&src, &srcsize,
NULL, &destsize, line_size, &hiscoa_params);
dest = page_reserve(destsize);
src = buf;
srcsize = size;
type = hiscoa_decompress_band((const void **)&src, &srcsize,
dest, &destsize, line_size, &hiscoa_params);
page.size += destsize;
fprintf(stderr, " eob = %u, lines = %u, expected bytes = %u\n",
type, lines, lines * line_size);
fprintf(stderr, " unconsumed size = %u, decompressed size = %u\n",
(unsigned) srcsize, (unsigned) destsize);
}

static void decode_hiscoa_band(const uint8_t *buf, size_t size)
{
unsigned lines = WORD(buf[2], buf[3]);
decode_hiscoa_band_data(buf + 4, size - 4, lines);
}
static void decode_hiscoa_band2(const uint8_t *buf, size_t size)
{
unsigned lines = WORD_(buf[2], buf[3]);
decode_hiscoa_band_data(buf + 8, size - 8, lines);
}
static void dispatch(uint16_t cmd, const uint8_t *buf, size_t size)
{
switch (cmd) {
case 0xD0A9:
fprintf(stderr, " --(multi-command)--\n");
while (size) {
uint16_t cc = WORD(buf[0], buf[1]);
unsigned cs = WORD(buf[2], buf[3]);
dispatch(cc, buf + 4, cs - 4);
buf += cs;
size -= cs;
}
break;
case 0xD0A0:
fprintf(stderr, " -(compression parameters)-\n");
dump(buf, size);
line_size = WORD(buf[26], buf[27]);
fprintf(stderr, " decoded: L=%u bytes, %u pixels\n", line_size, line_size * 8);
break;
case 0xD0A4:
fprintf(stderr, " -(Hi-SCoA parameters)-\n");
dump(buf, size);
decode_hiscoa_params(buf, size);
hi_mode = true;
fprintf(stderr, " decoded: [0]=%i+L [2]=%i+L [4]=%i [3]=%i [5]=%i\n",
hiscoa_params.origin_0, hiscoa_params.origin_2,
hiscoa_params.origin_4,
hiscoa_params.origin_3, hiscoa_params.origin_5);
break;
case 0xC0A0:
if (! hi_mode) {
fprintf(stderr, " -(SCoA data)-\n");
dump(buf, 16);
} else {
fprintf(stderr, " -(Hi-SCoA data from printer debug)-\n");
dump(buf, 4);
decode_hiscoa_band_data(buf, size, 70);
}
break;
case 0x8000:
fprintf(stderr, " -(Hi-SCoA data)-\n");
dump(buf, 4);
decode_hiscoa_band(buf, size);
break;
case 0x8200:
fprintf(stderr, " -(Hi-SCoA data 0x8200)-\n");
dump(buf, 4);
decode_hiscoa_band2(buf, size);
break;
case 0xC0A4:
fprintf(stderr, " --end--\n");
page_output();
break;
default:
dump(buf, size);
break;
}
}

int main(int argc, char **argv)
{
unsigned iband;
static uint8_t buf[1<<20];

FILE *input = stdin;

if (argc > 1) {
input = fopen(argv[1], "r");
if (! input)
abort();
}

page.reserve = 1024;
page.size = 0;
page.data = malloc(page.reserve);
if (! page.data)
abort();

while (1) {
int s;
uint16_t cmd;
uint32_t len;
size_t pos;
s = fread(buf, 1, 4, input);
if (s == 0)
break;
if (s != 4) {
fprintf(stderr, "! unable to read header\n");
break;
}
pos = 4;
cmd = WORD(buf[0], buf[1]);
switch (cmd) {
case 0x8200:
fread(buf + pos, 1, 4, input);
pos += 4;
len = WORD(buf[6], buf[7]);
len <<= 16;
len += WORD(buf[4], buf[5]);
pos=0;
fseek(input, -8, SEEK_CUR);
break;
default:
len = WORD(buf[2], buf[3]);
break;
}
fprintf(stderr, "CMD %04X len=%u\n", cmd, len);
if (fread(buf + pos, 1, len - pos, input) != len - pos) {
fprintf(stderr, "! unable to read %u bytes\n", len - pos);
break;
}
dispatch(cmd, buf + pos, len - pos);
}

if (page.size)
page_output();

if (argc > 1)
fclose(input);
return 0;
}
Loading

0 comments on commit 00e9793

Please sign in to comment.