File: cd5.c

package info (click to toggle)
cd5 0.1-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 88 kB
  • ctags: 16
  • sloc: ansic: 130; makefile: 53
file content (267 lines) | stat: -rw-r--r-- 6,317 bytes parent folder | download | duplicates (4)
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
/* cd5.c - checksum multi track cdrom
 *
 * Copyright (C) 2007 Yann Droneaud <ydroneaud@meuh.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, 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 */

/*
 * Quick and dirty program to compute checksum of individual track
 *
 * It reports for each track:
 *  - size (read)
 *  - MD5 digest
 *
 * Supports
 *   - multi track CD-ROM
 *   - mode 1
 *   - Linux ioctl()
 *   - libmhash
 *
 * Doesn't support
 *  - DVD
 *  - Audio CD
 *  - Multi session
 *  - Other operating system
 *
 * TODO:
 *  - support options
 *  - support other track format
 *  - support multi session
 *  - support other interfaces (ioctl, packet, ...)
 *  - support other OS
 *
 * Known problem:
 *  - Track are often bigger than the data written inside
 *    The program will report an error at the end of the data
 *    I don't know the way to detect the end of data.
 *    The checksum is good, and the size too.
 *
 * Be aware:
 *  - Track shorter than 4s -> 300 frames -> 614400 bytes are padded
 *    To check the checksum, pad the img/iso file to 614400 and run md5sum
 *
 * Build:
 *  - gcc -W -Wall cd5.c -o cd5 -lmhash
 *
 */

#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>

#include <linux/cdrom.h>

#include <mhash.h>

#include <assert.h>

#define VERSION "0.1" /* cd5 version 0.1 */

static char *device = "/dev/cdrom";

struct track
{
  int track_no;      /* track number */
  int track_data;    /* is data ? */
  int track_addr;    /* LBA */
  int track_length;  /* size */
};

union cdrom_read_arg{
  struct cdrom_msf msf;           /* input */
  char buffer[CD_FRAMESIZE];      /* return */
};

static const unsigned char base16[] = "0123456789abcdef";

int main(void)
{
  int i;

  int fd;

  struct cdrom_tochdr tochdr; /* TOC header (track number) */
  struct cdrom_tocentry tocentry; /* TOC entry */

  struct track *tracks;

  int track_count;
  int track_offset;

  int caps; /* capability */

  fd = open(device, O_RDONLY | O_NONBLOCK);

  if (fd == -1) {
    fprintf(stderr, "open(%s) failed: %s\n", device, strerror(errno));
    return 1;
  }

  /* This test that the device openned is CDROM compatible */
  caps = ioctl(fd, CDROM_GET_CAPABILITY);
  if (caps == -1) {
    fprintf(stderr, "ioctl(%s, CDROM_GET_CAPABILITY) failed: %s\n", device, strerror(errno));

    close(fd);    
  }

  /* Read number of track */
  if (ioctl(fd, CDROMREADTOCHDR, &tochdr) != 0) {
    fprintf(stderr, "ioctl(%s, CDROMREADTOCHDR) failed: %s\n", device, strerror(errno));

    close(fd);

    return 2;
  }

  track_offset = tochdr.cdth_trk0;
  track_count = 1 + (tochdr.cdth_trk1 - tochdr.cdth_trk0);

  printf("Track %d to %d (%d tracks)\n", 
	 track_offset, track_offset + (track_count - 1),
	 track_count);

  /* allocate memory for the track information */
  tracks = (struct track *) malloc(sizeof(struct track) * track_count);
  if (tracks == NULL) {

    fprintf(stderr, "malloc() failed: %s\n", strerror(errno));
      
    close(fd);
      
    return 1;
  }

  /* Read all track */
  for(i = 0; i < track_count; i++) {

    /* Read track information */
    tocentry.cdte_track  = (__u8)i + track_offset;
    tocentry.cdte_format = CDROM_LBA;
    
    if (ioctl(fd, CDROMREADTOCENTRY, &tocentry) != 0) {
      fprintf(stderr, "ioctl(%s, CDROMREADTOCENTRY(%d)) failed: %s\n", 
	      device, i, strerror(errno));
      
      continue;
    }

    tracks[i].track_no = tocentry.cdte_track;
    tracks[i].track_data = ((tocentry.cdte_ctrl & CDROM_DATA_TRACK) != 0);

    tracks[i].track_addr = tocentry.cdte_addr.lba;

    /* compute track size */
    if (i > 0) {
      tracks[i - 1].track_length = 
	tracks[i].track_addr - tracks[i - 1].track_addr;
    }
  }

  /* Read last track information */
  tocentry.cdte_track  = (__u8)CDROM_LEADOUT;
  tocentry.cdte_format = CDROM_LBA;
  
  if (ioctl(fd, CDROMREADTOCENTRY, &tocentry) != 0) {
    fprintf(stderr, "ioctl(%s, CDROMREADTOCENTRY(CDROM_LEADOUT)) failed: %s\n",
	    device, strerror(errno));
    
    close(fd);
    
    return 3;
  }
  
  /* compute last track size */
  tracks[i - 1].track_length = 
    tocentry.cdte_addr.lba - tracks[i - 1].track_addr;


  /* Now compute check sum for each track */
  for(i = 0; i < track_count; i ++) {

    MHASH md5;
    unsigned char hash[128 / 8]; /* MD5 buffer */

    int j;

    md5 = mhash_init(MHASH_MD5);
    if (md5 == MHASH_FAILED) {
      fprintf(stderr, "Can't setup MD5 hash\n");
      return 5;
    }

    printf(" Track %d, %s, %d + %d",
	   tracks[i].track_no, 
	   (tracks[i].track_data) ? "data" : "audio",
	   tracks[i].track_addr,
	   tracks[i].track_length);

    /* read the track and update digest */
    for(j = 0; j < tracks[i].track_length; j ++) {

      union cdrom_read_arg data;

      int lba;

      lba = tracks[i].track_addr + j;

      /* Convert LBA to MSF format */
      data.msf.cdmsf_frame0 = lba % CD_FRAMES;
      lba /= CD_FRAMES;
      lba += 2;
      data.msf.cdmsf_sec0 = lba % CD_SECS;
      data.msf.cdmsf_min0 = lba / CD_SECS;

      /* Read a block */
      if (ioctl(fd, CDROMREADMODE1, &data) != 0) {
	fprintf(stderr, 
		"ioctl(%s, CDROMREADMODE1(%d)) failed: %s\n",
		device, tracks[i].track_addr + j, strerror(errno));
	
	break;
      }

      /* update digest */
      mhash(md5, &data.buffer, CD_FRAMESIZE);
    }

    /* get digest */
    mhash_deinit(md5, hash);

    /* Print read size */
    printf(" | %d ", j * CD_FRAMESIZE);

    for(j = 0; j < (128 / 8); j ++) {
      printf("%02x", hash[j]);
    }

    printf("\n");
  }

  free(tracks);

  close(fd);
	 
  return 0;
}