Debian Codegraph @

cdimage.c

raw symbols SCIP
1 /*
2 * cdimage.c
3 * Layered data source for CD images in raw mode.
4 *
5 * Copyright (c) 2003 Christoph Pfisterer
6 *
7 * Permission is hereby granted, free of charge, to any person
8 * obtaining a copy of this software and associated documentation
9 * files (the "Software"), to deal in the Software without
10 * restriction, including without limitation the rights to use, copy,
11 * modify, merge, publish, distribute, sublicense, and/or sell copies
12 * of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
22 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
26 */
27
28 #include "global.h"
29
30 /*
31 * types
32 */
33
34 typedef struct cdimage_source {
35 SOURCE c;
36 u8 off;
37 } CDIMAGE_SOURCE;
38
39 /*
40 * helper functions
41 */
42
43 static SOURCE *init_cdimage_source(SOURCE *foundation, u8 offset);
44 static int read_block_cdimage(SOURCE *s, u8 pos, void *buf);
45
46 /*
47 * cd image detection
48 */
49
50 static unsigned char syncbytes[12] =
51 { 0, 255, 255, 255, 255, 255,
52 255, 255, 255, 255, 255, 0 };
53
54 void detect_cdimage(SECTION *section, int level)
55 {
56 int mode, off;
57 unsigned char *buf;
58 SOURCE *s;
59
60 if (get_buffer(section, 0, 2352, (void **)&buf) < 2352)
61 return;
62
63 /* check sync bytes as signature */
64 if (memcmp(buf, syncbytes, 12) != 0)
65 return;
66
67 /* get mode of the track -- this determines sector layout */
68 mode = buf[15];
69 if (mode == 1) {
70 /* standard data track */
71 print_line(level, "Raw CD image, Mode 1");
72 off = 16;
73 } else if (mode == 2) {
74 /* free-form track, assume XA form 1 */
75 print_line(level, "Raw CD image, Mode 2, assuming Form 1");
76 off = 24;
77 } else
78 return;
79
80 /* create and analyze wrapped source */
81 s = init_cdimage_source(section->source, section->pos + off);
82 analyze_source(s, level);
83 close_source(s);
84
85 /* don't run other analyzers */
86 stop_detect();
87 }
88
89 /*
90 * initialize the cd image source
91 */
92
93 static SOURCE *init_cdimage_source(SOURCE *foundation, u8 offset)
94 {
95 CDIMAGE_SOURCE *src;
96
97 src = (CDIMAGE_SOURCE *)malloc(sizeof(CDIMAGE_SOURCE));
98 if (src == NULL)
99 bailout("Out of memory");
100 memset(src, 0, sizeof(CDIMAGE_SOURCE));
101
102 if (foundation->size_known) {
103 src->c.size_known = 1;
104 src->c.size = ((foundation->size - offset + 304) / 2352) * 2048;
105 /* TODO: pass the size in from the SECTION record and use it */
106 }
107 src->c.blocksize = 2048;
108 src->c.foundation = foundation;
109 src->c.read_block = read_block_cdimage;
110 src->c.close = NULL;
111 src->off = offset;
112
113 return (SOURCE *)src;
114 }
115
116 /*
117 * raw read
118 */
119
120 static int read_block_cdimage(SOURCE *s, u8 pos, void *buf)
121 {
122 SOURCE *fs = s->foundation;
123 u8 filepos;
124
125 /* translate position */
126 filepos = (pos / 2048) * 2352 + ((CDIMAGE_SOURCE *)s)->off;
127
128 /* read from lower layer */
129 if (get_buffer_real(fs, filepos, 2048, buf, NULL) < 2048)
130 return 0;
131
132 return 1;
133 }
134
135 /* EOF */