File: mode_cmp.c

package info (click to toggle)
shntool 3.0.10-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 1,220 kB
  • ctags: 1,118
  • sloc: ansic: 8,515; sh: 3,761; makefile: 36
file content (429 lines) | stat: -rw-r--r-- 11,848 bytes parent folder | download
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*  mode_cmp.c - cmp mode module
 *  Copyright (C) 2000-2009  Jason Jordan <shnutils@freeshell.org>
 *
 *  This program 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 2
 *  of the License, or (at your option) any later version.
 *
 *  This program 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 this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include "mode.h"

CVSID("$Id: mode_cmp.c,v 1.91 2009/03/17 17:23:05 jason Exp $")

static bool cmp_main(int,char **);
static void cmp_help(void);

mode_module mode_cmp = {
  "cmp",
  "shncmp",
  "Compares PCM WAVE data in two files",
  CVSIDSTR,
  FALSE,
  cmp_main,
  cmp_help
};

#define CMP_MATCH_SIZE 2352  /* arbitrary number of bytes that must match before attempting
                                to check whether shifted data in the files is identical */
static bool align = FALSE;
static bool list = FALSE;
static int fuzz = 0;
static wlong shift_secs = 3;

static void cmp_help()
{
  st_info("Usage: %s [OPTIONS] [file1 file2]\n",st_progname());
  st_info("\n");
  st_info("Mode-specific options:\n");
  st_info("\n");
  st_info("  -c secs check the first secs seconds of data for byte shift (default is %d)\n",shift_secs);
  st_info("  -f fuzz fuzz factor: allow up to fuzz mismatches when detecting a byte-shift\n");
  st_info("  -h      show this help screen\n");
  st_info("  -l      list offsets and values of all differing bytes\n");
  st_info("  -s      check if WAVE data in the files is identical modulo a byte-shift\n");
  st_info("\n");
}

static void parse(int argc,char **argv,int *first_arg)
{
  int c;

  while ((c = st_getopt(argc,argv,"c:f:ls")) != -1) {
    switch (c) {
      case 'c':
        if (NULL == optarg)
          st_help("missing seconds for byte-shift comparison");
        shift_secs = atoi(optarg);
        if (shift_secs <= 0)
          st_help("seconds for byte-shift comparison must be positive");
        break;
      case 'f':
        if (NULL == optarg)
          st_help("missing fuzz factor");
        fuzz = atoi(optarg);
        if (fuzz < 0)
          st_help("fuzz factor cannot be negative");
        break;
      case 'l':
        list = TRUE;
        break;
      case 's':
        align = TRUE;
        break;
    }
  }

  if (fuzz > 0 && !align)
    st_help("fuzz factor can only be used with byte-shift");

  if (optind != argc && optind != argc - 2)
    st_help("need exactly two files to process");

  *first_arg = optind;
}

static void check_headers(wave_info *info1,wave_info *info2,int shift)
{
  int data_size1,data_size2,real_shift = (shift < 0) ? -shift : shift;

  data_size1 = info1->data_size;
  data_size2 = info2->data_size;

  if (shift > 0)
    data_size1 -= real_shift;
  else if (shift < 0)
    data_size2 -= real_shift;

  if (info1->wave_format != info2->wave_format)
    st_error("WAVE format differs between these files");

  if (info1->channels != info2->channels)
    st_error("number of channels differs between these files");

  if (info1->samples_per_sec != info2->samples_per_sec)
    st_error("samples per second differs between these files");

  if (info1->avg_bytes_per_sec != info2->avg_bytes_per_sec)
    st_error("average bytes per second differs between these files");

  if (info1->bits_per_sample != info2->bits_per_sample)
    st_error("bits per sample differs between these files");

  if (info1->block_align != info2->block_align)
    st_warning("block align differs between these files");

  if (data_size1 != data_size2)
    st_warning("WAVE data size differs between these files -- will check up to smaller size");
}

static int memfuzzycmp(unsigned char *str1,unsigned char *str2,int len,int fuzz)
{
  int i,firstbad = -1,badcount = 0;

  for (i=0;i<len;i++) {
    if (str1[i] != str2[i]) {
      if (0 == fuzz)
        return i;
      if (firstbad < 0)
        firstbad = i;
      badcount++;
      if (badcount > fuzz)
        return firstbad;
    }
  }

  return -1;
}

static void open_file(wave_info *info)
{
  if (!open_input_stream(info))
    st_error("could not reopen input file: [%s]",info->filename);
}

static bool cmp_files(wave_info *info1,wave_info *info2,int shift)
{
  unsigned char *buf1,*buf2;
  wlong bytes_to_check,
        bytes_checked = 0,
        bytes,
        shifted_data_size1 = info1->data_size,
        shifted_data_size2 = info2->data_size,
        xfer_size;
  int offset, real_shift, i;
  bool differed = FALSE, did_l_header = FALSE, success;
  progress_info proginfo;

  success = FALSE;

  check_headers(info1,info2,shift);

  proginfo.initialized = FALSE;
  proginfo.prefix = "Comparing";
  proginfo.clause = "and";
  proginfo.filename1 = info1->filename;
  proginfo.filedesc1 = info1->m_ss;
  proginfo.filename2 = info2->filename;
  proginfo.filedesc2 = info2->m_ss;
  proginfo.bytes_total = 1;

  prog_update(&proginfo);

  xfer_size = max(XFER_SIZE,(shift_secs * info1->rate));

  /* kluge to work around free(buf2) dumping core if malloc()'d separately */
  if (NULL == (buf1 = malloc(2 * xfer_size * sizeof(unsigned char)))) {
    prog_error(&proginfo);
    st_error("could not allocate %d-byte comparison buffer",xfer_size);
  }

  buf2 = buf1 + xfer_size;

  real_shift = (shift < 0) ? -shift : shift;

  open_file(info1);
  open_file(info2);

  discard_header(info1);
  discard_header(info2);

  if (shift > 0) {
    if (read_n_bytes(info1->input,buf1,real_shift,NULL) != real_shift) {
      prog_error(&proginfo);
      st_error("error while shifting %d bytes from file: [%s]",real_shift,info1->filename);
    }

    shifted_data_size1 -= real_shift;
  }
  else if (shift < 0) {
    if (read_n_bytes(info2->input,buf2,real_shift,NULL) != real_shift) {
      prog_error(&proginfo);
      st_error("error while shifting %d bytes from file: [%s]",real_shift,info2->filename);
    }

    shifted_data_size2 -= real_shift;
  }

  bytes_to_check = min(shifted_data_size1,shifted_data_size2);

  proginfo.filedesc1 = info1->m_ss;
  proginfo.filedesc2 = info2->m_ss;
  proginfo.bytes_total = bytes_to_check;

  while (bytes_to_check > 0) {
    bytes = min(bytes_to_check,xfer_size);
    if (read_n_bytes(info1->input,buf1,(int)bytes,&proginfo) != (int)bytes) {
      prog_error(&proginfo);
      st_error("error while reading %d bytes from file: [%s]",(int)bytes,info1->filename);
    }

    if (read_n_bytes(info2->input,buf2,(int)bytes,NULL) != (int)bytes) {
      prog_error(&proginfo);
      st_error("error while reading %d bytes from file: [%s]",(int)bytes,info2->filename);
    }

    if (-1 != (offset = memfuzzycmp(buf1,buf2,bytes,0))) {
      differed = TRUE;
      if (!list) {
        prog_error(&proginfo);
        st_error("WAVE data differs at byte offset: %lu",bytes_checked+(wlong)offset+1);
      }
      i = 0;
      while (offset >= 0) {
        i += offset + 1;
        if (!did_l_header) {
          prog_error(&proginfo);
          st_info("\n");
          st_info("    offset   1   2\n");
          st_info("   ----------------\n");
          did_l_header = TRUE;
        }
        st_info("%10ld %3d %3d\n",bytes_checked + i,(int)*(buf1 + i - 1),(int)*(buf2 + i - 1));
        offset = memfuzzycmp(buf1 + i,buf2 + i,bytes - i,0);
      }
    }
    bytes_to_check -= bytes;
    bytes_checked += bytes;
  }

  close_input_stream(info1);
  close_input_stream(info2);

  if (!differed) {
    success = TRUE;
    prog_success(&proginfo);
    st_info("\n");
    st_info("%s of these files are identical",(0 == shift) ? "Contents" : "Aligned contents");
    if (shifted_data_size1 != shifted_data_size2)
      st_info(" (up to the first %lu bytes of WAVE data)",min(shifted_data_size1,shifted_data_size2));
    st_info(".\n");
  }
  else {
    success = FALSE;
    st_info("\n");
    st_info("%s of these files differed as indicated above.\n",(0 == shift) ? "Contents" : "Aligned contents");
  }

  st_free(buf1);

  return success;
}

static void open_and_read_beginning(wave_info *info,unsigned char *buf,int bytes)
{
  open_file(info);

  discard_header(info);

  if (read_n_bytes(info->input,buf,(int)bytes,NULL) != (int)bytes)
    st_error("error while reading %d bytes from file: [%s]",(int)bytes,info->filename);

  close_input_stream(info);
}

static bool shift_comparison(wave_info *info1,wave_info *info2)
{
  unsigned char *buf1,*buf2;
  wlong bytes,cmp_size;
  int i,shift = 0,real_shift = 0;
  bool found_possible_shift = FALSE;
  progress_info proginfo;

  proginfo.initialized = FALSE;
  proginfo.prefix = "Scanning";
  proginfo.clause = "and";
  proginfo.filename1 = info1->filename;
  proginfo.filedesc1 = NULL;
  proginfo.filename2 = info2->filename;
  proginfo.filedesc2 = NULL;
  proginfo.bytes_total = 1;

  prog_update(&proginfo);

  cmp_size = shift_secs * info1->rate;
  bytes = min(min(info1->data_size,info2->data_size),cmp_size);

  if (NULL == (buf1 = malloc(2 * bytes * sizeof(unsigned char)))) {
    prog_error(&proginfo);
    st_error("could not allocate %d-byte comparison buffer",bytes);
  }

  buf2 = buf1 + bytes;

  open_and_read_beginning(info1,buf1,bytes);
  open_and_read_beginning(info2,buf2,bytes);

  for (i=0;i<bytes-CMP_MATCH_SIZE+1;i++) {
    if (-1 == memfuzzycmp(buf1 + i,buf2,bytes - i,fuzz)) {
      shift = i;
      real_shift = i;
      found_possible_shift = TRUE;
      break;
    }
    if (-1 == memfuzzycmp(buf1,buf2 + i,bytes - i,fuzz)) {
      shift = -i;
      real_shift = i;
      found_possible_shift = TRUE;
      break;
    }
  }

  st_free(buf1);

  if (!found_possible_shift) {
    prog_error(&proginfo);
    st_error("these files do not share identical data within the first %ld bytes.",cmp_size);
  }

  prog_success(&proginfo);

  st_info("\n");

  if (fuzz > 0)
    st_info("With fuzz factor %d, file",fuzz);
  else
    st_info("File");

  if (0 == shift) {
    st_info("s are identical so far.\n");
  }
  else {
    st_info(" the %s file seems to have ",(0 < shift) ? "first" : "second");
    st_info("%d extra bytes (%d extra samples",real_shift,(real_shift + 2)/4);
    if (!PROB_NOT_CD(info1) && !PROB_NOT_CD(info2))
      st_info(", or %d extra sectors",(real_shift+(CD_BLOCK_SIZE/2))/CD_BLOCK_SIZE);
    st_info(")\n");
    st_info("\n");
    st_info("These extra bytes will be ignored in the full comparison.\n");
  }

  st_info("\n");
  st_info("Preparing to do a full comparison...\n");
  st_info("\n");

  return cmp_files(info1,info2,shift);
}

static bool straight_comparison(wave_info *info1,wave_info *info2)
{
  return cmp_files(info1,info2,0);
}

static bool process_files(char *filename1,char *filename2)
{
  wave_info *info1,*info2;
  bool success;

  if (NULL == (info1 = new_wave_info(filename1)))
    return FALSE;

  if (NULL == (info2 = new_wave_info(filename2)))
    return FALSE;

  if (align)
    success = shift_comparison(info1,info2);
  else
    success = straight_comparison(info1,info2);

  st_free(info1);
  st_free(info2);

  return success;
}

static bool process(int argc,char **argv,int start)
{
  char *filename1,*filename2;
  bool success;

  success = FALSE;

  input_init(start,argc,argv);

  if (!(filename1 = input_get_filename()) || !(filename2 = input_get_filename())) {
    st_error("need exactly two files to process");
  }

  success = process_files(filename1,filename2);

  return success;
}

static bool cmp_main(int argc,char **argv)
{
  int first_arg;

  parse(argc,argv,&first_arg);

  return process(argc,argv,first_arg);
}