~ubuntu-branches/ubuntu/vivid/macutils/vivid

« back to all changes in this revision

Viewing changes to binhex/dofile.c

  • Committer: Bazaar Package Importer
  • Author(s): Eric Sharkey
  • Date: 2001-10-27 15:22:18 UTC
  • Revision ID: james.westby@ubuntu.com-20011027152218-oikhfgiaja3hjf5f
Tags: upstream-2.0b3
ImportĀ upstreamĀ versionĀ 2.0b3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "../fileio/machdr.h"
 
2
#include "../fileio/rdfile.h"
 
3
 
 
4
extern int dorep;
 
5
extern unsigned long binhex_crcinit;
 
6
extern unsigned long binhex_updcrc();
 
7
 
 
8
#define RUNCHAR 0x90
 
9
 
 
10
static int pos_ptr;
 
11
static char codes[] =
 
12
    "!\"#$%&'()*+,-012345689@ABCDEFGHIJKLMNPQRSTUVXYZ[`abcdefhijklmpqr";
 
13
static int state;
 
14
static int savebits;
 
15
static int rep_char;
 
16
static int rep_count;
 
17
 
 
18
void doheader();
 
19
void dofork();
 
20
void outbyte();
 
21
void finish();
 
22
void outbyte1();
 
23
void out6bit();
 
24
void outchar();
 
25
 
 
26
void dofile()
 
27
{
 
28
    (void)printf("(This file must be converted; you knew that already.)\n");
 
29
    (void)printf("\n");
 
30
    pos_ptr = 1;
 
31
    state = 0;
 
32
    rep_char = -1;
 
33
    rep_count = 0;
 
34
    outchar(':');
 
35
    doheader();
 
36
    dofork(data_fork, data_size);
 
37
    dofork(rsrc_fork, rsrc_size);
 
38
    finish();
 
39
    (void)putchar(':');
 
40
    (void)putchar('\n');
 
41
}
 
42
 
 
43
void doheader()
 
44
{
 
45
unsigned long crc;
 
46
int i, n;
 
47
 
 
48
    crc = binhex_crcinit;
 
49
    n = file_info[I_NAMEOFF];
 
50
    crc = binhex_updcrc(crc, file_info + I_NAMEOFF, n + 1);
 
51
    for(i = 0; i <= n; i++) {
 
52
        outbyte(file_info[I_NAMEOFF + i]);
 
53
    }
 
54
    n = 0;
 
55
    crc = binhex_updcrc(crc, (char *)&n, 1);
 
56
    outbyte(0);
 
57
    crc = binhex_updcrc(crc, file_info + I_TYPEOFF, 4);
 
58
    for(i = 0; i < 4; i++) {
 
59
        outbyte(file_info[I_TYPEOFF + i]);
 
60
    }
 
61
    crc = binhex_updcrc(crc, file_info + I_AUTHOFF, 4);
 
62
    for(i = 0; i < 4; i++) {
 
63
        outbyte(file_info[I_AUTHOFF + i]);
 
64
    }
 
65
    crc = binhex_updcrc(crc, file_info + I_FLAGOFF, 2);
 
66
    for(i = 0; i < 2; i++) {
 
67
        outbyte(file_info[I_FLAGOFF + i]);
 
68
    }
 
69
    crc = binhex_updcrc(crc, file_info + I_DLENOFF, 4);
 
70
    for(i = 0; i < 4; i++) {
 
71
        outbyte(file_info[I_DLENOFF + i]);
 
72
    }
 
73
    crc = binhex_updcrc(crc, file_info + I_RLENOFF, 4);
 
74
    for(i = 0; i < 4; i++) {
 
75
        outbyte(file_info[I_RLENOFF + i]);
 
76
    }
 
77
    outbyte((int)(crc >> 8));
 
78
    outbyte((int)(crc & 0xff));
 
79
}
 
80
 
 
81
void dofork(fork, size)
 
82
char *fork;
 
83
int size;
 
84
{
 
85
unsigned long crc;
 
86
int i;
 
87
 
 
88
    crc = binhex_updcrc(binhex_crcinit, fork, size);
 
89
    for(i = 0; i < size; i++) {
 
90
        outbyte(fork[i]);
 
91
    }
 
92
    outbyte((int)(crc >> 8));
 
93
    outbyte((int)(crc & 0xff));
 
94
}
 
95
 
 
96
void outbyte(b)
 
97
int b;
 
98
{
 
99
    b &= 0xff;
 
100
    if(dorep && (b == rep_char)) {
 
101
        if(++rep_count == 254) {
 
102
            outbyte1(RUNCHAR);
 
103
            outbyte1(255);
 
104
            rep_char = -1;
 
105
            rep_count = 0;
 
106
        }
 
107
    } else {
 
108
        if(rep_count > 0) {
 
109
            if(rep_count > 3) {
 
110
                outbyte1(RUNCHAR);
 
111
                outbyte1(rep_count + 1);
 
112
            } else {
 
113
                while(rep_count-- > 0) {
 
114
                    outbyte1(rep_char);
 
115
                }
 
116
            }
 
117
        }
 
118
        outbyte1(b);
 
119
        if(b == RUNCHAR) {
 
120
            outbyte1(0);
 
121
            rep_char = -1;
 
122
        } else {
 
123
            rep_char = b;
 
124
        }
 
125
        rep_count = 0;
 
126
    }
 
127
}
 
128
 
 
129
void finish()
 
130
{
 
131
    if(rep_count > 0) {
 
132
        if(rep_count > 3) {
 
133
            outbyte1(RUNCHAR);
 
134
            outbyte1(rep_count + 1);
 
135
        } else {
 
136
            while(rep_count-- > 0) {
 
137
                outbyte1(rep_char);
 
138
            }
 
139
        }
 
140
    }
 
141
    switch(state) {
 
142
    case 1:
 
143
        out6bit(savebits << 4);
 
144
        break;
 
145
    case 2:
 
146
        out6bit(savebits << 2);
 
147
        break;
 
148
    default:
 
149
        break;
 
150
    }
 
151
}
 
152
 
 
153
void outbyte1(b)
 
154
int b;
 
155
{
 
156
    switch(state) {
 
157
    case 0:
 
158
        out6bit(b >> 2);
 
159
        savebits = b & 0x3;
 
160
        state = 1;
 
161
        break;
 
162
    case 1:
 
163
        b |= (savebits << 8);
 
164
        out6bit(b >> 4);
 
165
        savebits = b & 0xf;
 
166
        state = 2;
 
167
        break;
 
168
    case 2:
 
169
        b |= (savebits << 8);
 
170
        out6bit(b >> 6);
 
171
        out6bit(b & 0x3f);
 
172
        state = 0;
 
173
        break;
 
174
    }
 
175
}
 
176
 
 
177
void out6bit(c)
 
178
char c;
 
179
{
 
180
    outchar(codes[c & 0x3f]);
 
181
}
 
182
 
 
183
void outchar(c)
 
184
char c;
 
185
{
 
186
    (void)putchar(c);
 
187
    if(++pos_ptr > 64) {
 
188
        (void)putchar('\n');
 
189
        pos_ptr = 1;
 
190
    }
 
191
}
 
192