summaryrefslogtreecommitdiff
path: root/mzf2wav/src/mzf2wav.c
blob: 9440fb597c72184a638abaf2081fdeb44833a7bc (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
/*
 * mzf2wav by: Jeroen F. J. Laros.
 *
 * Last change on: Sep 11 2003.
 * 
 * This program is freeware and may be used without paying any registration 
 * fees. It may be distributed freely provided it is done so using the 
 * original, unmodified version. Usage of parts of the source code is granted, 
 * provided the author is referenced. For private use only. Re-selling or any 
 * commercial use of this program or parts of it is strictly forbidden. The
 * author is not responsible for any damage or data loss as a result of using 
 * this program.
 */
#include <stdio.h>
#include <stdlib.h> // Just to remove a warning (malloc).
#include "methods.h"
#include "args.h"

// Global variables.
FILE *OUT = NULL;
int speed_1 = 0;
char *filename = NULL,
     *outfile = NULL;
void (*method)(byte *) = trans;

//Private function prototypes.
byte *readfile(FILE *); // Read the file into memory.

// Read the file into memory.
byte *readfile(FILE *IN) {
  byte *image = (byte *)malloc(2),
       *temp = NULL;
  word i = 0,
       t = 0;

  if (!image)
    return NULL;
  while (fread(&image[i], 1, 1, IN)) {
    temp = (byte *)realloc((byte *)image, i + 2);
    if (!temp) {
      free(image);
      return NULL;
    }//if
    image = temp;
    i++;
  }//while
  t = assert(image, i);
  if (t) {
    printf("The MZF file size does not match the image size.\n");
    if (t > 1) {
      printf("This is not a valid MZF file.\n");
      free(image);
      return NULL;
    }//if
  }//if
  return image;
}//readfile

// Main.
int main(int argc, char **argv) {
  FILE *IN = NULL;
  byte *image = NULL;
  int i = 0;
  
  setvars(argc, argv);

  IN = fopen(filename, "rb");
  if (!IN) {
    printf("Unable to open file: %s for reading.\n\n", argv[1]);
    error(2);
  }//if
  
  image = readfile(IN);
  if (!image) {
    printf("Out of memory or assertion error.\n\n");
    fclose(IN);
    error(3);
  }//if
  OUT = fopen(outfile, "wb");
  if (!OUT) {
    printf("Error: unable to open output file: %s for writing.\n", outfile);
    free(image);
    fclose(IN);
    error(6);
  }//if

  while (fread(&image[i], 1, 1, IN))
    i++;

  setspeed(speed_1);
  
  writewavheader();
  method(image);
  setheader();

  fclose(OUT);
  free(image);
  fclose(IN);
  
  return 0;
}//main