-
Notifications
You must be signed in to change notification settings - Fork 0
/
un-protect.c
127 lines (114 loc) · 3.35 KB
/
un-protect.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
/*
* Copyright (C) 2002 John Todd Larason <jtl@molehill.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.
*/
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#ifdef __unix__
# include <unistd.h>
#endif
#ifdef WIN32
# include "gnu_getopt\getopt.h"
#endif
#include "rtv.h"
#include "ndx.h"
static int handle(FILE * in, FILE * out)
{
struct ndx_record this;
/* header */
if (read_ndx_record(in, &this) <= 0) {
fprintf(stderr, "ERROR: couldn't read header\n");
return -1;
}
if (this.flag_1 != 2 || this.commercial_flag != 2) {
fprintf(stderr, "ERROR: invalid header\n");
return -1;
}
this.video_offset = 0; /* in header, part of that is really a flag */
write_ndx_record(out, &this);
while (read_ndx_record(in, &this) > 0) {
this.unk_fe = 0xfe;
this.macrovision = 0;
this.macrovision_count = 0;
write_ndx_record(out, &this);
}
return 0;
}
static void usage(const char * str)
{
if (str)
fprintf(stderr, "*** ERROR: %s\n\n", str);
fprintf(stderr,
"Usage: commercial [-i <i>] [-o <o>] [-s <s>] [-e <e>] [-r] [filename]\n"
" -i -- input filename\n"
" -o -- output filename\n"
"\n"
"If no files are given, reads from stdin and writes to stdout\n"
"If [filename] is given, it's used for both input and output\n"
);
exit(str ? 1 : 0);
}
static void error(const char * str)
{
fprintf(stderr, "*** ERROR: %s\n", str);
exit(1);
}
int main(int argc, char ** argv)
{
FILE * in = NULL,
* out = NULL;
int ch;
while ((ch = getopt(argc, argv, "i:o:h")) != EOF) {
switch(ch) {
case 'i':
if (in)
usage("Can only specify input once");
in = fopen(optarg, "r");
if (!in)
error("Couldn't open input file");
break;
case 'o':
if (out)
usage("Can only specify output once");
out = fopen(optarg, "w");
if (!out)
error("Couldn't open output file");
break;
case 'h':
usage(NULL);
break;
default:
usage("");
}
}
if (argc - optind > 1)
usage("If input and output are separate files, must use -i and -o");
if (argc - optind == 0) {
if (!in)
in = stdin;
if (!out)
out = stdout;
}
if (argc - optind == 1) {
if (in || out)
usage("If you use -i or -o, use both or std{in,out}");
in = fopen(argv[optind], "r");
if (!in)
error("Couldn't open file to read");
out = fopen(argv[optind], "r+");
if (!out)
error("Couldn't open file to write");
}
return handle(in, out);
}