2
* $Id: exe_sear.c,v 1.3 2003/10/16 10:32:46 andrew_belov Exp $
3
* ---------------------------------------------------------------------------
4
* Routines that fetch overlay data are located in this file.
10
#ifdef ELF_EXECUTABLES
15
#endif /* __QNXNTO__ */
18
DEBUGHDR(__FILE__) /* Debug information block */
20
/* ARJSFX module order */
27
/* Define the ELF magic numbers if not defined yet */
29
#if defined(ELF_EXECUTABLES)&&!defined(ELFMAG)
30
static char elfmag[4]={'E', 'L', 'F', 0x7F};
39
static char overlay_sig[]="RJ_SFX";
41
/* Looks for ARJ_SFX signature */
43
static void browse(FILE *stream)
46
unsigned long cur_pos, sig_pos;
53
cur_pos=ftell(stream);
54
bytes_read=fread(buf, 1, sizeof(buf), stream);
61
if(*buf_ptr=='A'&&!strcmp(buf_ptr+1, overlay_sig))
63
sig_pos=cur_pos+(unsigned long)i;
64
if(mget_dword(buf_ptr+8)==sig_pos)
70
if(bytes_read==sizeof(buf))
71
fseek(stream, -16L, SEEK_CUR);
74
cur_pos+=(unsigned long)i+12L;
75
fseek(stream, cur_pos, SEEK_SET);
78
/* Writes the contents of a FAR block to the output stream */
80
static void farblock_output(FILE *stream, char FAR *block, unsigned long len)
85
if(fputc((int)*(block++), stream)==EOF)
89
file_write(block, 1, len, stream);
95
/* Reads the EXE size from the header */
97
static unsigned long get_exe_size(FILE *stream)
100
unsigned long result=EXESIZE_ARJ;
101
#elif SFX_LEVEL==ARJSFXV
102
unsigned long result=EXESIZE_ARJSFXV;
104
unsigned long result=EXESIZE_ARJSFX;
106
#ifndef ELF_EXECUTABLES
107
unsigned int remainder, blocks;
111
unsigned long ref_point;
112
unsigned long cur_pos;
116
#ifndef ELF_EXECUTABLES /* Presume standard DOS or OS/2 EXE */
117
fseek(stream, 2L, SEEK_SET);
118
remainder=fget_word(stream);
119
blocks=fget_word(stream);
120
result=(unsigned long)(blocks-1)*512L+(unsigned long)remainder;
122
#else /* ELF (Linux, OS/2 PPC, Solaris...) */
123
fread(&ehdr, 1, sizeof(ehdr), stream);
124
if(memcmp(ehdr.e_ident, ELFMAG, SELFMAG))
126
result=ehdr.e_shoff+(unsigned long)ehdr.e_shentsize*ehdr.e_shnum;
127
fseek(stream, ehdr.e_shoff, SEEK_SET);
128
for(i=0; i<ehdr.e_shnum; i++)
130
fseek(stream, ehdr.e_shoff+(unsigned long)i*ehdr.e_shentsize, SEEK_SET);
131
cur_pos=ftell(stream);
132
fread(&shdr, 1, sizeof(shdr), stream);
133
ref_point=shdr.sh_offset+shdr.sh_size;
134
/* Ignore uninitialized sections (BSS) */
135
if(ref_point>result&&shdr.sh_type!=SHT_NOBITS)
144
/* Performs all actions related to picking a block */
146
static void fetch_block(int num)
149
unsigned long exe_size;
155
unsigned int desc_word;
157
stream=file_open_noarch(exe_name, m_rb);
158
exe_size=get_exe_size(stream);
159
fseek(stream, exe_size, SEEK_SET);
161
for(t_num=0; t_num<num; t_num++)
163
fseek(stream, t_pos, SEEK_CUR);
165
t_pos=fget_longword(stream);
168
block_len=(int)min(sizeof(buf), t_pos);
171
bytes_read=fread(buf, 1, block_len, stream);
174
crc32_for_block(buf, bytes_read);
175
farblock_output(aostream, (char FAR *)buf, (unsigned long)bytes_read);
176
t_pos-=(unsigned long)bytes_read;
177
block_len=(int)min(sizeof(buf), t_pos);
180
desc_word=SFXDESC_NONSFX;
181
if(create_sfx==SFXCRT_SFX&&multivolume_option)
182
desc_word=SFXDESC_SFXV;
183
else if(create_sfx==SFXCRT_SFX)
184
desc_word=SFXDESC_SFX;
185
else if(create_sfx==SFXCRT_SFXJR)
186
desc_word=SFXDESC_SFXJR;
187
fput_word(desc_word, aostream);
190
fput_word(desc_word, aostream);
192
fput_dword(crc32term, aostream);
195
/* Picks the ARJSFX and stores ARJSFX run-time data */
202
/* Picks the ARJSFXJR and stores ARJSFXJR run-time data */
206
fetch_block(MN_SFXJR);
209
/* Picks the ARJSFXV and stores ARJSFXV run-time data */
213
fetch_block(MN_SFXV);
216
/* Picks the SFXSTUB */
220
fetch_block(MN_SFXSTUB);
225
#if SFX_LEVEL<=ARJSFXV
227
/* SFX seek routine */
231
unsigned long exe_size;
233
unsigned int block_size, bytes_read;
236
exe_size=get_exe_size(aistream);
237
fseek(aistream, exe_size, SEEK_SET);
238
main_hdr_offset=find_header(0, aistream);
239
fseek(aistream, -8L, SEEK_CUR);
240
exe_size=ftell(aistream);
241
reg_id=fget_word(aistream); /* Descriptive word */
242
reg_id=fget_word(aistream);
243
fcrc=fget_longword(aistream);
246
fseek(aistream, 0L, SEEK_SET);
248
block_size=min((unsigned long)sizeof(buf), exe_size);
251
bytes_read=fread(buf, 1, block_size, aistream);
254
crc32_for_block(buf, bytes_read);
255
exe_size-=(unsigned long)bytes_read;
256
block_size=min((unsigned long)block_size, exe_size);
258
if((crc32term^CRC_MASK)!=fcrc&&!skip_integrity_test)
259
error(M_DAMAGED_SFX);
260
fseek(aistream, main_hdr_offset, SEEK_SET);