File: jzexe.c

package info (click to toggle)
jzip 210r20001005d-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 1,088 kB
  • sloc: ansic: 11,700; makefile: 64; sh: 27
file content (397 lines) | stat: -rw-r--r-- 10,136 bytes parent folder | download | duplicates (4)
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397

/* $Id: jzexe.c,v 1.1.1.1 2000/05/10 14:21:34 jholder Exp $   
 * --------------------------------------------------------------------
 * see doc/License.txt for License Information   
 * --------------------------------------------------------------------
 * 
 * File name: $Id: jzexe.c,v 1.1.1.1 2000/05/10 14:21:34 jholder Exp $  
 *   
 * Description:    
 *    
 * Modification history:      
 * $Log: jzexe.c,v $
 * Revision 1.1.1.1  2000/05/10 14:21:34  jholder
 *
 * imported
 *
 *
 * --------------------------------------------------------------------
 */

/*
 * jzexe.c  version 1.1
 * 
 * This program creates standalone game executables from Infocom
 * format story files. It does so by concatenating the JZip executable
 * with the story file and then patching the excutable to start reading
 * the story from the correct file position.
 *
 * Written by Magnus Olsson (mol@df.lth.se), 20 November, 1995,
 * as part of the JZip distribution. 
 * You may use this code in any way you like as long as it is
 * attributed to its author.
 *
 */

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

#include "jzexe.h"

#ifndef TRUE
#define TRUE 1
#define FALSE 0
#endif

#ifndef SEEK_SET
#define SEEK_SET 0
#endif

/* The size of the buffer used when copying files */
#define BUFSIZE 16384

static unsigned char *buf;
static unsigned char *jzipmagic = ( unsigned char * ) MAGIC_STRING;

#if defined(MSDOS) || defined(__MSDOS__)
#define READ_MODE "rb"
#define WRITE_MODE "wb"
#else
#define READ_MODE "r"
#define WRITE_MODE "w"
#endif

#define NAME_LENGTH 200
static char in_name[NAME_LENGTH + 1];
static char out_name[NAME_LENGTH + 1];

/* The name of the JZip executable */
#if defined(MSDOS) || defined(__MSDOS__)
#define JZIP_NAME "jzip.exe"
#else
#define JZIP_NAME "/usr/games/jzip"
#endif

void abort_program( int exit_code )
{
   fprintf( stderr, "\nProgram aborted.\n" );
   /* Remove output file; it's probably corrupted. */
   remove( out_name );
   exit( exit_code );
}

FILE *open_file( char *name, int input )
{
   char *mode;
   FILE *f;
   int tempfd;

   if ( input )
   {
      strncpy( in_name, name, NAME_LENGTH );
      /* If name is too long, in_name won't be null terminated: fix this,
       * just in case */
      in_name[NAME_LENGTH] = 0;
      mode = READ_MODE;

      f = fopen(name, mode);
   }
   else
   {
      strncpy( out_name, name, NAME_LENGTH );
      out_name[NAME_LENGTH] = 0;
      mode = WRITE_MODE;

      tempfd = creat(name, 0755);
       if (tempfd == -1) {
               fprintf(stderr, "Error creating file %s\n", name);
               abort_program(1);
       }
       f = fdopen(tempfd, WRITE_MODE);
   }
   if ( !f )
   {
      fprintf( stderr, "Error opening file %s\n", name );
      abort_program( 1 );
   }
   return f;
}

/* 
 * Search through the file f for the magic string. If found, return
 * the offset stored in the patch area, otherwise abort the program.
 */
long search( FILE * f )
{
   int c, i;

   i = 0;
   while ( ( c = getc( f ) ) > -1 )
   {
      if ( ( unsigned char ) c != jzipmagic[i] )
      {                         
         if ( ( unsigned char ) c == jzipmagic[0] ) 
            i = 1;
         else
            i = 0;
      }
      else if ( ++i == MAGIC_END )
      {
         /* Found magic string. */
         long offset;

         /* Next byte must be zero. */
         if ( getc( f ) != 0 )
         {
            fprintf( stderr, "Error: standalone flag != 0\n" );
            abort_program( 1 );
         }
         offset = getc( f );
         offset += 256L * getc( f );
         offset += 65536L * getc( f );
         return offset;
      }
   }
   fprintf( stderr, "Couldn't find magic string." );
   abort_program( 1 );
   return 0; /* should not return */
}

/*
 * Copy the file in to the file out. Set length to the length in bytes
 * of the input file, and set magic_pos to the offset of the first
 * occurrence of MAGIC_STRING. 
 */
void copy_and_search( FILE * in, FILE * out, long *length, long *magic_pos )
{
   int c, magic_idx;            
   size_t buf_idx, buf_end;     
   long pos;

   pos = 0;
   buf_idx = buf_end = 0;
   magic_idx = 0;
   *magic_pos = 0;

   for ( ;; )
   {                            
      if ( buf_idx >= buf_end )
      {
         /* Reached end of input buffer; read in a new chunk */
         buf_end = fread( buf, 1, BUFSIZE, in );
         if ( buf_end < 1 )
         {
            /* Reached end of input file. 
             * pos is now the number of bytes read. */
            *length = pos;
            return;
         }
         if ( fwrite( buf, 1, buf_end, out ) != buf_end )
         {
            fprintf( stderr, "Write error on file %s\n", out_name );
            fclose( in );
            fclose( out );
            abort_program( 1 );
         }
         buf_idx = 0;
      }

      c = buf[buf_idx++];       /* Get current character */
      ++pos;                    /* Offset of next byte */

      /* The following search algorithm utilizes the fact that the
       * first character of the magic string is unique. */
      if ( ( unsigned char ) c != jzipmagic[magic_idx] ) 
         if ( ( unsigned char ) c == jzipmagic[0] ) 
            magic_idx = 1;
         else
            magic_idx = 0;
      else if ( ++magic_idx == MAGIC_END )
      {
         /* Matched the entire magic string */
         if ( *magic_pos > 0 )
         {
            /* If this condition occurs, the magic string isn't unique,
             * with potentially bad consequences. Re-compile with a
             * different magic string. */
            fprintf( stderr, "Found more than one instance of the magic string.\n" );
            fclose( in );
            fclose( out );
            abort_program( 1 );
         }
         *magic_pos = pos;
      }
   }
}

/*
 * Copy the input file to the output file.
 */
void copy( FILE * in, FILE * out )
{
   unsigned bytes_read;
   long total = 0;

   do
   {
      bytes_read = fread( buf, 1, BUFSIZE, in );
      if ( bytes_read > 0 )
         if ( fwrite( buf, 1, bytes_read, out ) != bytes_read )
         {
            fprintf( stderr, "Write error on file %s\n", out_name );
            fclose( in );
            fclose( out );
            abort_program( 1 );
         }
      total += bytes_read;
   }
   while ( bytes_read > 0 );

   if ( total == 0 )
      fprintf( stderr, "Nothing read from file %s - probably an error.\n", in_name );
}

/*
 * Patch the output file so it will read Z code from the correct position.
 */
void patch( FILE * f, long pos, long value )
{
   int i;

   fflush( f );
   if ( fseek( f, pos, SEEK_SET ) )
   {
      fprintf( stderr, "Seek error on file %s\n", out_name );
      fclose( f );
      abort_program( 1 );
   }
   /* Set STANDALONE_FLAG to FALSE */
   fputc( FALSE, f );
   /* Write the length of the JZip interpreter to the file as a
    * little-endian, 24-bit number. */
   for ( i = 0; i < 3; ++i )
   {
      fputc( ( int ) ( value & 0xff ), f );
      value >>= 8;
   }
}

void create_executable( char *story, char *jzip )
{
   FILE *in, *out;
   long length, pos;
   char fn[NAME_LENGTH + 4];
   char *ext;

   strcpy( in_name, "" );
   strcpy( out_name, "" );

   if ( strlen( story ) > NAME_LENGTH || strlen( jzip ) > NAME_LENGTH )
   {
      fprintf( stderr, "Filename too long.\n" );
      abort_program( 1 );
   }
   strcpy( fn, story );
   ext = strrchr( fn, '.' );
   if ( ext )
      *ext = '\0';
   strcat( fn, ".exe" );

   printf( "Creating standalone story %s\n", fn );
   out = open_file( fn, FALSE );

   printf( "Copying JZip interpreter (%s).\n", jzip );
   in = open_file( jzip, TRUE );

   copy_and_search( in, out, &length, &pos );
   fclose( in );
   if ( pos == 0 )
   {
      fprintf( stderr, "Error: couldn't find magic string.\n" );
      fprintf( stderr, "Check that you have the right version of JZip.\n" );
      abort_program( 1 );
   }

   printf( "Copying story file %s.\n", story );
   in = open_file( story, TRUE );
   copy( in, out );
   fclose( in );

   printf( "Patching interpreter.\n" );
   patch( out, pos, length );
   fclose( out );

   printf( "Done!\n" );
}

void extract_zcode( char *filename )
{
   FILE *in, *out;
   long offset;
   char fn[NAME_LENGTH + 4];
   char *ext;
   int z_version;

   strcpy( in_name, "" );
   strcpy( out_name, "" );

   if ( strlen( filename ) > NAME_LENGTH )
   {
      fprintf( stderr, "Filename too long.\n" );
      abort_program( 1 );
   }

   in = open_file( filename, TRUE );
   offset = search( in );

   fseek( in, offset, SEEK_SET );
   z_version = fgetc( in );
   if ( z_version < 1 || z_version > 8 )
   {
      fprintf( stderr, "Unsupported Z code version: %d\n", z_version );
      abort_program( 1 );
   }
   strcpy( fn, filename );
   ext = strrchr( fn, '.' );
   if ( ext == 0 )
      ext = fn + strlen( fn );
   sprintf( ext, ".z%d", z_version );

   printf( "Extracting Z code to story file %s\n", fn );
   out = open_file( fn, FALSE );

   fputc( z_version, out );
   copy( in, out );
   fclose( in );
   fclose( out );
}

#define USAGE "JZexe Ver. 1.1 - creates standalone Infocom-format games for MS-DOS and Linux.\n\
(c) 1995 by Magnus Olsson (mol@df.lth.se)\n\n\
Usage: jzexe story_file\n\
       jzexe story_file jzip_file\n\n\
These two commands create a standalone game from an Infocom story file.\n\
In the first case, it is assumed that the JZip interpreter is called jzip.exe\n\
and resides in the current directory.\n\n\
       jzexe -x game.exe\n\n\
This command extracts the Z code from a standalone executable and creates\n\
a story file that can be played with JZip or any other interpreter.\n\
Note that the extension '.exe' of the game file must be given.\n"

int main( int argc, char **argv )
{
   buf = ( unsigned char * ) malloc( BUFSIZE * sizeof ( unsigned char ) );

   if ( argc == 3 && strcmp( argv[1], "-x" ) == 0 )
      extract_zcode( argv[2] );
   else if ( argc == 2 )
      create_executable( argv[1], JZIP_NAME );
   else if ( argc == 3 )
      create_executable( argv[1], argv[2] );
   else
      fprintf( stderr, USAGE );

   exit( 0 );
}