[go: up one dir, main page]

File: queen_extract.c

package info (click to toggle)
dynamite 0.1.1-2
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, buster, jessie, jessie-kfreebsd, stretch, trixie, wheezy
  • size: 1,572 kB
  • ctags: 129
  • sloc: sh: 9,533; ansic: 613; makefile: 80
file content (123 lines) | stat: -rw-r--r-- 2,527 bytes parent folder | download | duplicates (3)
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
/* $Id: queen_extract.c 1212 2003-08-26 18:09:57Z twogood $ */

/*
   Example code for libdynamite.

   This code extracts the files from the installer to the adventure game "Flight
   of the Amazon Queen" from 1995.

   It takes the installation data files QUEEN._[1-6] and extracts these files:

   QUEEN.1
   DOS4GW.EXE
   SETUP.EXE
   SETUP.SB
   SETUP.MUS
   SETUP.RL
   QUEEN.EXE
   AQ.BAT
 */

#define _BSD_SOURCE 1
#include "libdynamite.h"
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_FILENAME_SIZE   30

#define FCLOSE(file)    if (file) { fclose(file); file = NULL; }

typedef struct _Cookie
{
  int index;
  FILE* input_file;
  FILE* output_file;
} Cookie;

static void next_file(Cookie* cookie)
{
  char filename[16];
  FCLOSE(cookie->input_file);
  cookie->index++;
  snprintf(filename, sizeof(filename), "QUEEN._%i", cookie->index);
  cookie->input_file = fopen(filename, "r");

  if (!cookie->input_file && cookie->index <= 6)
  {
    fprintf(stderr, "Failed to open file '%s'\n", filename);
  }
}

static size_t reader(void* buffer, size_t size, void* cookie)
{
  size_t result = fread(buffer, 1, size, ((Cookie*)cookie)->input_file);

  if (result < size)
  {
    next_file((Cookie*)cookie);
    if (((Cookie*)cookie)->input_file)
    {
      buffer += result;
      result += fread(buffer, 1, size - result, ((Cookie*)cookie)->input_file);
    }
  }

  return result;
}

static size_t writer(void* buffer, size_t size, void* cookie)
{
  return fwrite(buffer, 1, size, ((Cookie*)cookie)->output_file);
}

int main(int argc, char** argv)
{
  int result = -1;
  Cookie cookie;

  memset(&cookie, 0, sizeof(Cookie));

  next_file(&cookie);

  for (;;)
  {
    uint8_t name_size;
    char* name = NULL;

#if 0
    printf("Volume %i, offset 0x%08lx\n", cookie.index, ftell(cookie.input_file));
#endif
    
    if (sizeof(name_size) != reader(&name_size, sizeof(name_size), &cookie))
      break;

    name = malloc(MAX_FILENAME_SIZE);

    if (MAX_FILENAME_SIZE != reader(name, MAX_FILENAME_SIZE, &cookie))
    {
      fprintf(stderr, "read name failed\n");
      break;
    }

    name[name_size] = '\0';
    printf("extracting: %s\n", name);

    cookie.output_file = fopen(name, "w");

    result = dynamite_explode(reader, writer, &cookie);

    FCLOSE(cookie.output_file);

    if (DYNAMITE_SUCCESS != result)
    {
      fprintf(stderr, "Error %i\n", result);
      break;
    }
  }

  FCLOSE(cookie.input_file);
  FCLOSE(cookie.output_file);
  return result;
}