-
Notifications
You must be signed in to change notification settings - Fork 2
/
insert.c
331 lines (277 loc) · 8.3 KB
/
insert.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
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
/******************************************************************************
* insert.c
*
* mp3nema - MP3 analysis and data hiding utility
*
* Copyright (C) 2008 Matt Davis (enferex) of 757Labs (www.757labs.com)
*
* insert.c is part of mp3nema.
* mp3nema is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* mp3nema is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with mp3nema. If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include "main.h"
#include "utils.h"
/* Destinations (MP3 files that the inject data is spanned across/into) */
typedef struct _data_dest_t data_dest_t;
struct _data_dest_t {char *fname; size_t size; int frames;};
static int count_frames(FILE *dst)
{
int n_frames;
id3_tag_t *tag;
mp3_frame_t *frame;
STREAM_OBJECT type;
n_frames = 0;
while ((type = util_next_mp3_frame_or_id3v2(dst, NULL, 0, 0, NULL, NULL)))
{
switch (type)
{
case STREAM_OBJECT_MP3_FRAME:
frame = mp3_get_frame(dst);
mp3_free_frame(frame);
++n_frames;
break;
case STREAM_OBJECT_ID3V2_TAG:
tag = id3_get_tag(dst);
free(tag);
break;
default: break;
}
}
return n_frames;
}
static void inject(FILE *dst, FILE *src, FILE *out, int bytes)
{
int i, n_frames, n_blocks, block_sz, remainder_sz;
long start, end;
unsigned char *buf, *block;
id3_tag_t *tag;
mp3_frame_t *frame;
/* Chunks of data to break src into */
remainder_sz = 0;
n_frames = count_frames(dst);
n_blocks = bytes / (n_frames - FRAMES_TO_IGNORE);
if ((n_blocks == 0) || ((block_sz = bytes / n_blocks) == 0))
{
n_blocks = 1;
block_sz = bytes;
}
else
remainder_sz = bytes % n_blocks;
fseek(dst, 0, SEEK_SET);
buf = NULL;
block = malloc(block_sz + remainder_sz);
for (i=0; i<n_frames; i++)
{
/* Start/end read points to copy frame/tag */
start = ftell(dst);
switch (util_next_mp3_frame_or_id3v2(dst, NULL, 0, 0, NULL, NULL))
{
case STREAM_OBJECT_MP3_FRAME:
frame = mp3_get_frame(dst);
mp3_free_frame(frame);
break;
case STREAM_OBJECT_ID3V2_TAG:
tag = id3_get_tag(dst);
free(tag);
break;
default:
break;
}
end = ftell(dst);
/* Copy tag/frame */
free(buf);
buf = calloc(1, end - start);
fseek(dst, start, SEEK_SET);
fread(buf, end - start, 1, dst);
fseek(dst, end, SEEK_SET);
fwrite(buf, end - start, 1, out);
/* Add in data (ignoring the first 'i' frames) */
if (i > FRAMES_TO_IGNORE && n_blocks)
{
/* Add in remainder data if odd size */
if ((n_blocks - 1) == 0)
block_sz += remainder_sz;
fread(block, block_sz, 1, src);
fwrite(block, block_sz, 1, out);
--n_blocks;
}
}
free(buf);
free(block);
}
/* Add a data destination object to the array given at index given */
static void add_dest(
data_dest_t *dests,
int idx,
const char *fpath,
const char *fname)
{
int n_frames;
struct stat st;
FILE *dst;
mp3_frame_t *frame;
id3_tag_t *tag;
STREAM_OBJECT type;
dests[idx].fname = malloc(2 + strlen(fname) + ((fpath)?strlen(fpath) : 0));
if (!fpath)
sprintf(dests[idx].fname, "%s", fname);
else
sprintf(dests[idx].fname, "%s/%s", fpath, fname);
stat(dests[idx].fname, &st);
dests[idx].size = st.st_size;
if (!(dst = fopen(dests[idx].fname, "r")))
{
dests[idx].frames = 0;
ERR("Could not open destination mp3 to obtain frame count");
return;
}
/* Count frames */
n_frames = 0;
while ((type = util_next_mp3_frame_or_id3v2(dst, NULL, 0, 0, NULL, NULL)))
{
switch (type)
{
case STREAM_OBJECT_MP3_FRAME:
frame = mp3_get_frame(dst);
mp3_free_frame(frame);
++n_frames;
break;
case STREAM_OBJECT_ID3V2_TAG:
tag = id3_get_tag(dst);
free(tag);
break;
default: break;
}
}
fclose(dst);
dests[idx].frames = n_frames;
}
/* Returns an array of data destinations (mp3 files) that the source data is to
* be injected into.
*/
static data_dest_t *load_data_dests(const char *f_or_dir_name, int *n_dests)
{
int n_files;
DIR *dir;
FILE *dst;
data_dest_t *dests;
struct dirent *entry;
struct stat st;
dests = NULL;
stat(f_or_dir_name, &st);
if (n_dests)
*n_dests = 0;
/* Directory or single file */
if (S_ISDIR(st.st_mode))
{
/* Count number of MP3 files */
if (!(dir = opendir(f_or_dir_name)))
{
ERR("Could not open directory of mp3 files to inject data into");
return NULL;
}
/* Assumes, by extension and not file data */
n_files = 0;
while ((entry = readdir(dir)))
if (strstr(entry->d_name, ".mp3"))
++n_files;
/* Load */
dests = malloc(sizeof(data_dest_t) * n_files);
n_files = 0;
rewinddir(dir);
while ((entry = readdir(dir)))
if (strstr(entry->d_name, ".mp3"))
add_dest(dests, n_files++, f_or_dir_name, entry->d_name);
closedir(dir);
}
else /* Treat as a single file */
{
if (!(dst = fopen(f_or_dir_name, "r")))
{
ERR("Could not open mp3 to inject data into");
return NULL;
}
dests = malloc(sizeof(data_dest_t));
add_dest(dests, 0, NULL, f_or_dir_name);
n_files = 1;
fclose(dst);
}
if (n_dests)
*n_dests = n_files;
return dests;
}
static void free_dests(data_dest_t *dests, int n_dests)
{
int i;
for (i=0; i<n_dests; i++)
free(dests[i].fname);
free(dests);
}
void handle_as_insert(
const char *f_or_dir_name,
flags_t flags,
const char *datasrc)
{
int i, n_dests, err;
char dest_modifier[16];
size_t src_sz, sz;
FILE *dest, *src, *out;
struct stat st;
data_dest_t *dests;
/* Where we pull data to insert into */
if (!(src = fopen(datasrc, "r")))
{
ERR("Could not open data file to read from");
return;
}
/* Single file or directory? */
if (!(dests = load_data_dests(f_or_dir_name, &n_dests)))
return;
/* Amount of data to inject */
stat(datasrc, &st);
src_sz = st.st_size;
fseek(src, 0, SEEK_SET);
/* For each file we are to span accross */
err = 0;
for (i=0; i<n_dests; i++)
{
snprintf(dest_modifier, sizeof(dest_modifier), "injected-%d", i+1);
out = util_create_file(f_or_dir_name, dest_modifier, "mp3", 0);
/* Insert info between frame skipping two frames so data
* is not always in the first frame.
*/
if (!(dest = fopen(dests[i].fname, "r")))
{
++err;
continue;
}
/* Last one? Add in remainder for odd sizes */
sz = src_sz / (n_dests - err);
if (i+1 == n_dests)
sz += src_sz % (n_dests - err);
fseek(dest, 0, SEEK_SET);
inject(dest, src, out, sz);
fclose(dest);
fclose(out);
}
/* Clean */
free_dests(dests, n_dests);
fclose(src);
}