aboutsummaryrefslogtreecommitdiff
path: root/src/example/dump.c
blob: 93713de390e7cddececcec8ac24f0abde1fb7117 (plain)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>

typedef struct
{
    int len;
    unsigned char *addr;
} Block;

static int Word(FILE *fp)
{
    int i0;
    int i1;

    i0 = getc(stdin);
    i1 = getc(stdin);

    return i0 | i1 << 8;
}

static int Triplet(FILE *fp)
{
    int i0;
    int i1;
    int i2;

    i0 = getc(stdin);
    i1 = getc(stdin);
    i2 = getc(stdin);

    return i0 | i1 << 8 | i2 << 16;
}

static Block *DumpBlock(FILE *fp)
{
    Block *block;

    Word(fp);	/* PILOT */
    Word(fp);	/* SYNC1 */
    Word(fp);	/* SYNC2 */
    Word(fp);	/* ZERO */
    Word(fp);	/* ONE */
    Word(fp);	/* PILOT LEN */
    getc(fp);	/* USED BITS */
    Word(fp);	/* PAUSE */

    block = malloc(sizeof *block);

    block->len = Triplet(fp);	/* LEN */
    block->addr = malloc(block->len);
    fread(block->addr, 1, block->len, fp);

    return block;
}

static void CompareEnds(Block **block, int count)
{
    int f;
    int n;
    int min;
    int done;

    min = 0xffffff;

    for(f=0; f < count; f++)
    {
    	if (block[f]->len < min)
	{
	    min = block[f]->len;
	}
    }

    done = 0;

    for(f = 0; f < min && !done; f++)
    {
    	for(n = 0; n < count - 1 && !done; n++)
	{
	    if (block[n]->addr[block[n]->len - 1 - f] !=
	    		block[n+1]->addr[block[n+1]->len - 1 - f])
	    {
	    	done = 1;
	    }
	}
    }

    if (done)
    {
	printf("Last %d bytes match\n", --f);

	for(n = 0; n < f; n++)
	{
	    if (n > 0)
	    {
	    	printf(" ");
	    }

	    printf("%x", (unsigned)block[0]->addr[block[0]->len - 1 - n]);
	}

	printf("\n");
    }
}

int main(int argc, char *argv)
{
    int ch;
    int count;
    int f;
    Block *block[256] = {0};

    ch = getc(stdin);

    while(ch != EOF)
    {
    	if (ch == 0x11)
	{
	    block[count++] = DumpBlock(stdin);
	}

	ch = getc(stdin);
    }

    printf("Got %d blocks\n", count);

    CompareEnds(block, count);
}