patch zlib check
[platform/upstream/cdrkit.git] / genisoimage / diag / isodebug.c
1 /*
2  * This file has been modified for the cdrkit suite.
3  *
4  * The behaviour and appearence of the program code below can differ to a major
5  * extent from the version distributed by the original author(s).
6  *
7  * For details, see Changelog file distributed with the cdrkit package. If you
8  * received this file from another source then ask the distributing person for
9  * a log of modifications.
10  *
11  */
12
13 /* @(#)isodebug.c       1.10 05/05/01 Copyright 1996-2004 J. Schilling */
14 /*
15  *      Copyright (c) 1996-2004 J. Schilling
16  */
17 /*
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License version 2
20  * as published by the Free Software Foundation.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License along with
28  * this program; see the file COPYING.  If not, write to the Free Software
29  * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30  */
31
32 #include <mconfig.h>
33 #include <stdio.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <statdefs.h>
37 #include <stdxlib.h>
38 #include <unixstd.h>
39 #include <strdefs.h>
40 #include <standard.h>
41 #include <utypes.h>
42 #include <intcvt.h>
43 #include <schily.h>
44
45 #include "../scsi.h"
46 #include "../../wodim/defaults.h"
47
48 #define _delta(from, to)        ((to) - (from) + 1)
49
50 #define VD_BOOT         0
51 #define VD_PRIMARY      1
52 #define VD_SUPPLEMENT   2
53 #define VD_PARTITION    3
54 #define VD_TERM         255
55
56 #define VD_ID           "CD001"
57
58 struct  iso9660_voldesc {
59         char    vd_type         [_delta(1, 1)];
60         char    vd_id           [_delta(2, 6)];
61         char    vd_version      [_delta(7, 7)];
62         char    vd_fill         [_delta(8, 2048)];
63 };
64
65 struct  iso9660_boot_voldesc {
66         char    vd_type         [_delta(1, 1)];
67         char    vd_id           [_delta(2, 6)];
68         char    vd_version      [_delta(7, 7)];
69         char    vd_bootsys      [_delta(8, 39)];
70         char    vd_bootid       [_delta(40, 71)];
71         char    vd_bootcode     [_delta(72, 2048)];
72 };
73
74 struct  iso9660_pr_voldesc {
75         char    vd_type                 [_delta(1,      1)];
76         char    vd_id                   [_delta(2,      6)];
77         char    vd_version              [_delta(7,      7)];
78         char    vd_unused1              [_delta(8,      8)];
79         char    vd_system_id            [_delta(9,      40)];
80         char    vd_volume_id            [_delta(41,     72)];
81         char    vd_unused2              [_delta(73,     80)];
82         char    vd_volume_space_size    [_delta(81,     88)];
83         char    vd_unused3              [_delta(89,     120)];
84         char    vd_volume_set_size      [_delta(121,    124)];
85         char    vd_volume_seq_number    [_delta(125,    128)];
86         char    vd_lbsize               [_delta(129,    132)];
87         char    vd_path_table_size      [_delta(133,    140)];
88         char    vd_pos_path_table_l     [_delta(141,    144)];
89         char    vd_opt_pos_path_table_l [_delta(145,    148)];
90         char    vd_pos_path_table_m     [_delta(149,    152)];
91         char    vd_opt_pos_path_table_m [_delta(153,    156)];
92         char    vd_root_dir             [_delta(157,    190)];
93         char    vd_volume_set_id        [_delta(191,    318)];
94         char    vd_publisher_id         [_delta(319,    446)];
95         char    vd_data_preparer_id     [_delta(447,    574)];
96         char    vd_application_id       [_delta(575,    702)];
97         char    vd_copyr_file_id        [_delta(703,    739)];
98         char    vd_abstr_file_id        [_delta(740,    776)];
99         char    vd_bibl_file_id         [_delta(777,    813)];
100         char    vd_create_time          [_delta(814,    830)];
101         char    vd_mod_time             [_delta(831,    847)];
102         char    vd_expiry_time          [_delta(848,    864)];
103         char    vd_effective_time       [_delta(865,    881)];
104         char    vd_file_struct_vers     [_delta(882,    882)];
105         char    vd_reserved1            [_delta(883,    883)];
106         char    vd_application_use      [_delta(884,    1395)];
107         char    vd_fill                 [_delta(1396,   2048)];
108 };
109
110 #define GET_UBYTE(a)    a_to_u_byte(a)
111 #define GET_SBYTE(a)    a_to_byte(a)
112 #define GET_SHORT(a)    a_to_u_2_byte(&((unsigned char *) (a))[0])
113 #define GET_BSHORT(a)   a_to_u_2_byte(&((unsigned char *) (a))[2])
114 #define GET_INT(a)      a_to_4_byte(&((unsigned char *) (a))[0])
115 #define GET_LINT(a)     la_to_4_byte(&((unsigned char *) (a))[0])
116 #define GET_BINT(a)     a_to_4_byte(&((unsigned char *) (a))[4])
117
118 #define infile  in_image
119 FILE            *infile = NULL;
120
121 static void     usage(int excode);
122 static char     *isodinfo(FILE *f);
123
124 static void
125 usage(int excode)
126 {
127         errmsgno(EX_BAD, "Usage: %s [options] image\n",
128                                                 get_progname());
129
130         fprintf(stderr, "Options:\n");
131         fprintf(stderr, "\t-help,-h     Print this help\n");
132         fprintf(stderr, "\t-version     Print version info and exit\n");
133         fprintf(stderr, "\t-i filename  Filename to read ISO-9660 image from\n");
134         fprintf(stderr, "\tdev=target   SCSI target to use as CD/DVD-Recorder\n");
135         fprintf(stderr, "\nIf neither -i nor dev= are speficied, <image> is needed.\n");
136         exit(excode);
137 }
138
139 static char *
140 isodinfo(FILE *f)
141 {
142 static  struct iso9660_voldesc          vd;
143         struct iso9660_pr_voldesc       *vp;
144 #ifndef USE_SCG
145         struct stat                     sb;
146         mode_t                          mode;
147 #endif
148         BOOL                            found = FALSE;
149         off_t                           sec_off = 16L;
150
151 #ifndef USE_SCG
152         /*
153          * First check if a bad guy tries to call isosize()
154          * with an unappropriate file descriptor.
155          * return -1 in this case.
156          */
157         if (isatty(fileno(f)))
158                 return (NULL);
159         if (fstat(fileno(f), &sb) < 0)
160                 return (NULL);
161         mode = sb.st_mode & S_IFMT;
162         if (!S_ISREG(mode) && !S_ISBLK(mode) && !S_ISCHR(mode))
163                 return (NULL);
164 #endif
165
166         vp = (struct iso9660_pr_voldesc *) &vd;
167
168         do {
169 #ifdef  USE_SCG
170                 readsecs(sec_off, &vd, 1);
171 #else
172                 if (lseek(fileno(f), (off_t)(sec_off * 2048L), SEEK_SET) == -1)
173                         return (NULL);
174                 read(fileno(f), &vd, sizeof (vd));
175 #endif
176                 sec_off++;
177
178                 if (GET_UBYTE(vd.vd_type) == VD_PRIMARY) {
179                         found = TRUE;
180 /*                      break;*/
181                 }
182
183         } while (GET_UBYTE(vd.vd_type) != VD_TERM);
184
185         if (GET_UBYTE(vd.vd_type) != VD_TERM)
186                 return (NULL);
187
188 #ifdef  USE_SCG
189         readsecs(sec_off, &vd, 1);
190 #else
191         if (lseek(fileno(f), (off_t)(sec_off * 2048L), SEEK_SET) == -1)
192                 return (NULL);
193         read(fileno(f), &vd, sizeof (vd));
194 #endif
195         sec_off++;
196
197         return ((char *)&vd);
198 }
199
200 int
201 main(int argc, char *argv[])
202 {
203         int     cac;
204         char    * const *cav;
205         char    *opts = "help,h,version,i*,dev*";
206         BOOL    help = FALSE;
207         BOOL    prvers = FALSE;
208         char    *filename = NULL;
209         char    *devname = NULL;
210         char    *p;
211         char    *eol;
212
213         save_args(argc, argv);
214
215         cac = argc - 1;
216         cav = argv + 1;
217         if (getallargs(&cac, &cav, opts, &help, &help, &prvers,
218                         &filename, &devname) < 0) {
219                 errmsgno(EX_BAD, "Bad Option: '%s'\n", cav[0]);
220                 usage(EX_BAD);
221         }
222         if (help)
223                 usage(0);
224         if (prvers) {
225                 printf("isodebug %s (%s)\n", CDRKIT_VERSION, HOST_SYSTEM);
226                 exit(0);
227         }
228         cac = argc - 1;
229         cav = argv + 1;
230         if (filename == NULL && devname == NULL) {
231                 if (getfiles(&cac, &cav, opts) != 0) {
232                         filename = cav[0];
233                         cac--, cav++;
234                 }
235         }
236         if (getfiles(&cac, &cav, opts) != 0) {
237                 errmsgno(EX_BAD, "Bad Argument: '%s'\n", cav[0]);
238                 usage(EX_BAD);
239         }
240         if (filename != NULL && devname != NULL) {
241                 errmsgno(EX_BAD, "Only one of -i or dev= allowed\n");
242                 usage(EX_BAD);
243         }
244 #ifdef  USE_SCG
245         if (filename == NULL && devname == NULL)
246                 cdr_defaults(&devname, NULL, NULL, NULL);
247 #endif
248         if (filename == NULL && devname == NULL) {
249                 errmsgno(EX_BAD, "ISO-9660 image not specified\n");
250                 usage(EX_BAD);
251         }
252
253         if (filename != NULL)
254                 infile = fopen(filename, "rb");
255         else
256                 filename = devname;
257
258         if (infile != NULL) {
259                 /* EMPTY */;
260 #ifdef  USE_SCG
261         } else if (scsidev_open(filename) < 0) {
262 #else
263         } else {
264 #endif
265                 comerr("Cannot open '%s'\n", filename);
266         }
267
268         p = isodinfo(infile);
269         if (p == NULL) {
270                 printf("No ISO-9660 image debug info.\n");
271         } else if (strncmp(p, "MKI ", 4) == 0) {
272                 eol = strchr(p, '\n');
273                 if (eol)
274                         *eol = '\0';
275                 printf("ISO-9660 image created at %s\n", &p[4]);
276                 if (eol) {
277                         printf("\nCmdline: '%s'\n", &eol[1]);
278                 }
279         }
280         return (0);
281 }