This repository has been archived by the owner on Nov 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsndextract.c
262 lines (235 loc) · 6.09 KB
/
sndextract.c
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
#include "sndextract.h"
#include "format.h"
void show_intro();
void show_message(const char *message);
void show_progress(const unsigned long int start,const unsigned long int stop);
FILE *open_input_file(const char *name);
FILE *create_output_file(const char *name);
void go_offset(FILE *file,const unsigned long int offset);
unsigned long int get_file_size(FILE *file);
void data_dump(FILE *input,FILE *output,const size_t length);
void fast_data_dump(FILE *input,FILE *output,const size_t length);
void write_output_file(FILE *input,const char *name,const size_t length);
char *get_string_memory(const size_t length);
size_t get_extension_position(const char *source);
char *get_short_name(const char *name);
char *get_name(const unsigned long int index,const char *short_name,const char *extension);
void check_signature(const char *signature);
head read_head(FILE *file);
subhead read_subhead(FILE *file);
void extract(FILE *input,const char *name);
void extract_last(FILE *input,const char *name,const unsigned long int snd_size);
void work(const char *file);
int main(int argc, char *argv[])
{
show_intro();
if (argc<2)
{
show_message("You must give a target file name as the command-line argument!");
}
else
{
show_message("Extracting a sounds... Please wait");
work(argv[1]);
show_message("The work has been finished");
}
return 0;
}
void show_intro()
{
putchar('\n');
puts("SND EXTRACT");
puts("Version 2.5.7");
puts("Mugen sound extractor by Popov Evgeniy Alekseyevich, 2008-2024 years");
puts("This program is distributed under GNU GENERAL PUBLIC LICENSE");
}
void show_message(const char *message)
{
putchar('\n');
puts(message);
}
void show_progress(const unsigned long int start,const unsigned long int stop)
{
unsigned long int progress;
progress=(start+1)*100;
progress/=stop;
putchar('\r');
printf("Amount of the extracted files: %lu from %lu.Progress:%lu%%",start+1,stop,progress);
}
FILE *open_input_file(const char *name)
{
FILE *target;
target=fopen(name,"rb");
if (target==NULL)
{
puts("Can't open the input file");
exit(1);
}
return target;
}
FILE *create_output_file(const char *name)
{
FILE *target;
target=fopen(name,"wb");
if (target==NULL)
{
show_message("Can't create the ouput file");
exit(2);
}
return target;
}
void go_offset(FILE *file,const unsigned long int offset)
{
if (fseek(file,offset,SEEK_SET)!=0)
{
show_message("Can't jump to the target offset");
exit(3);
}
}
unsigned long int get_file_size(FILE *file)
{
unsigned long int length;
fseek(file,0,SEEK_END);
length=ftell(file);
rewind(file);
return length;
}
void data_dump(FILE *input,FILE *output,const size_t length)
{
unsigned char data;
size_t index;
data=0;
for (index=0;index<length;++index)
{
fread(&data,sizeof(unsigned char),1,input);
fwrite(&data,sizeof(unsigned char),1,output);
}
}
void fast_data_dump(FILE *input,FILE *output,const size_t length)
{
unsigned char *buffer=NULL;
buffer=(unsigned char*)calloc(length,sizeof(unsigned char));
if (buffer==NULL)
{
data_dump(input,output,length);
}
else
{
fread(buffer,sizeof(unsigned char),length,input);
fwrite(buffer,sizeof(unsigned char),length,output);
free(buffer);
}
}
void write_output_file(FILE *input,const char *name,const size_t length)
{
FILE *output;
output=create_output_file(name);
fast_data_dump(input,output,length);
fclose(output);
}
char *get_string_memory(const size_t length)
{
char *memory=NULL;
memory=(char*)calloc(length+1,sizeof(char));
if(memory==NULL)
{
show_message("Can't allocate memory");
exit(4);
}
return memory;
}
size_t get_extension_position(const char *source)
{
size_t index;
for(index=strlen(source);index>0;--index)
{
if(source[index]=='.')
{
break;
}
}
if (index==0) index=strlen(source);
return index;
}
char *get_short_name(const char *name)
{
size_t length;
char *result=NULL;
length=get_extension_position(name);
result=get_string_memory(length);
strncpy(result,name,length);
return result;
}
char *get_name(const unsigned long int index,const char *short_name,const char *extension)
{
char *name=NULL;
size_t length;
length=strlen(short_name)+strlen(extension)+12;
name=get_string_memory(length);
sprintf(name,"%s%lu%s",short_name,index,extension);
return name;
}
void check_signature(const char *signature)
{
if (strncmp(signature,"ElecbyteSnd",12)!=0)
{
puts("The bad signature of a mugen sound pseudo-archive");
exit(5);
}
}
head read_head(FILE *file)
{
head snd_head;
fread(&snd_head,sizeof(head),1,file);
check_signature(snd_head.signature);
return snd_head;
}
subhead read_subhead(FILE *file)
{
subhead snd_subhead;
fread(&snd_subhead,sizeof(subhead),1,file);
return snd_subhead;
}
void extract(FILE *input,const char *name)
{
subhead snd_subhead;
unsigned long int length;
snd_subhead=read_subhead(input);
length=snd_subhead.next_offset-ftell(input);
write_output_file(input,name,(size_t)length);
go_offset(input,snd_subhead.next_offset);
}
void extract_last(FILE *input,const char *name,const unsigned long int snd_size)
{
unsigned long int length;
length=ftell(input)+(unsigned long int)sizeof(subhead);
go_offset(input,length);
length=snd_size-ftell(input);
write_output_file(input,name,(size_t)length);
}
void work(const char *file)
{
FILE *input;
head snd_head;
unsigned long int index,snd_size;
char *wave_name=NULL;
char *short_name=NULL;
short_name=get_short_name(file);
input=open_input_file(file);
snd_size=get_file_size(input);
snd_head=read_head(input);
go_offset(input,snd_head.first_offset);
for (index=0;index<snd_head.nb_sound-1;++index)
{
show_progress(index,snd_head.nb_sound);
wave_name=get_name(index+1,short_name,".wav");
extract(input,wave_name);
free(wave_name);
}
show_progress(index,snd_head.nb_sound);
wave_name=get_name(index+1,short_name,".wav");
extract_last(input,wave_name,snd_size);
free(wave_name);
free(short_name);
fclose(input);
}