~ubuntu-branches/ubuntu/vivid/isomd5sum/vivid

« back to all changes in this revision

Viewing changes to libimplantisomd5.c

  • Committer: Bazaar Package Importer
  • Author(s): Ryan Finnie
  • Date: 2006-10-01 13:31:56 UTC
  • Revision ID: james.westby@ubuntu.com-20061001133156-r2tx2xkds64y9ryx
Tags: upstream-11.1.0.95
ImportĀ upstreamĀ versionĀ 11.1.0.95

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright 2001 Red Hat, Inc.                                    */
 
2
/* Michael Fulbright msf@redhat.com                                */
 
3
 
 
4
/*   4/2005     Dustin Kirkland (dustin.kirkland@gmail.com)        */
 
5
/*      Added support for checkpoint fragment sums;                */
 
6
/*      Allows for exiting media check when bad fragment md5sum'ed */
 
7
 
 
8
#include <stdio.h>
 
9
#include <stdlib.h>
 
10
#include <sys/types.h>
 
11
#include <sys/stat.h>
 
12
#include <fcntl.h>
 
13
#include <string.h>
 
14
#include <unistd.h>
 
15
 
 
16
#include "md5.h"
 
17
#include "libimplantisomd5.h"
 
18
 
 
19
#define APPDATA_OFFSET 883
 
20
#define SIZE_OFFSET 84
 
21
 
 
22
/* Length in characters of string used for fragment md5sum checking  */
 
23
#define FRAGMENT_SUM_LENGTH 60
 
24
/* FRAGMENT_COUNT must be an integral divisor or FRAGMENT_SUM_LENGTH */
 
25
/* 60 => 2, 3, 4, 5, 6, 10, 12, 15, 20, or 30 */
 
26
#define FRAGMENT_COUNT 20
 
27
 
 
28
/* number of sectors to ignore at end of iso when computing sum */
 
29
#define SKIPSECTORS 15
 
30
 
 
31
#define MAX(x, y)  ((x > y) ? x : y)
 
32
#define MIN(x, y)  ((x < y) ? x : y)
 
33
 
 
34
/* finds primary volume descriptor and returns info from it */
 
35
/* mediasum must be a preallocated buffer at least 33 bytes long */
 
36
static int parsepvd(int isofd, char *mediasum, long long *isosize) {
 
37
    unsigned char buf[2048];
 
38
    long long offset;
 
39
    unsigned char *p __attribute__((unused));
 
40
 
 
41
    if (lseek(isofd, 16*2048, SEEK_SET) == -1)
 
42
        return ((long long)-1);
 
43
    
 
44
    offset = (16 * 2048);
 
45
    for (;1;) {
 
46
        if (read(isofd, buf, 2048L) == -1)
 
47
            return ((long long)-1);
 
48
 
 
49
        if (buf[0] == 1)
 
50
            /* found primary volume descriptor */
 
51
            break;
 
52
        else if (buf[0] == 255)
 
53
            /* hit end and didn't find primary volume descriptor */
 
54
            return ((long long)-1);
 
55
        offset += 2048L;
 
56
    }
 
57
    
 
58
    /* read out md5sum */
 
59
#if 0
 
60
    memcpy(mediasum, buf + APPDATA_OFFSET + 13, 32);
 
61
    mediasum[32] = '\0';
 
62
 
 
63
    for (p=mediasum; *p; p++)
 
64
        if (*p != ' ')
 
65
            break;
 
66
 
 
67
    /* if the md5sum was all spaces, we didn't find md5sum */
 
68
    if (!*p)
 
69
        return -1;
 
70
#endif
 
71
 
 
72
    /* get isosize */
 
73
    *isosize = (buf[SIZE_OFFSET]*0x1000000+buf[SIZE_OFFSET+1]*0x10000 +
 
74
                buf[SIZE_OFFSET+2]*0x100 + buf[SIZE_OFFSET+3]) * 2048LL;
 
75
 
 
76
    return offset;
 
77
}
 
78
 
 
79
 
 
80
static unsigned int writeAppData(unsigned char *appdata, char *valstr, unsigned int loc) {
 
81
    if (loc + strlen(valstr) > 511) {
 
82
        printf("Attempted to write too much appdata, exiting...\n");
 
83
        exit(-1);
 
84
    }
 
85
 
 
86
    memcpy(appdata + loc, valstr, strlen(valstr));
 
87
 
 
88
    return loc+strlen(valstr);
 
89
}
 
90
 
 
91
 
 
92
 
 
93
 
 
94
int implantISOFile(char *fname, int supported, int forceit, int quiet, char **errstr) {
 
95
    int i;
 
96
    int isofd;
 
97
    int nread;
 
98
    int dirty;
 
99
    int pvd_offset;
 
100
    int current_fragment = 0;
 
101
    int previous_fragment = 0;
 
102
    int nattempt;
 
103
    long long isosize, total;
 
104
    unsigned char md5sum[16];
 
105
    unsigned char fragmd5sum[16];
 
106
    unsigned int loc;
 
107
    unsigned int bufsize = 32768;
 
108
    unsigned char *buf;
 
109
    unsigned char orig_appdata[512];
 
110
    unsigned char new_appdata[512];
 
111
    char mediasum[33];
 
112
    char md5str[40];
 
113
    char fragstr[FRAGMENT_SUM_LENGTH+1];
 
114
    MD5_CTX md5ctx, fragmd5ctx;
 
115
 
 
116
    isofd = open(fname, O_RDWR);
 
117
 
 
118
    if (isofd < 0) {
 
119
        *errstr = "Error - Unable to open file %s\n\n";
 
120
        return -1;
 
121
    }
 
122
 
 
123
    pvd_offset = parsepvd(isofd, mediasum, &isosize);
 
124
    if (pvd_offset < 0) {
 
125
        *errstr = "Could not find primary volumne!\n\n";
 
126
        return -1;
 
127
    }
 
128
 
 
129
    lseek(isofd, pvd_offset + APPDATA_OFFSET, SEEK_SET);
 
130
    nread = read(isofd, orig_appdata, 512);
 
131
 
 
132
    if (!forceit) {
 
133
        dirty = 0;
 
134
        for (i=0; i < 512; i++)
 
135
            if (orig_appdata[i] != ' ')
 
136
                dirty = 1;
 
137
 
 
138
        if (dirty) {
 
139
            *errstr = "Application data has been used - not implanting md5sum!\n";
 
140
            return -1;
 
141
        }
 
142
    } else {
 
143
        /* write out blanks to erase old app data */
 
144
        lseek(isofd, pvd_offset + APPDATA_OFFSET, SEEK_SET);
 
145
        memset(new_appdata, ' ', 512);
 
146
        i = write(isofd, new_appdata, 512);
 
147
        if (i<0) {
 
148
            printf("write failed %d\n", i);
 
149
            perror("");
 
150
        }
 
151
    }
 
152
 
 
153
    /* now do md5sum */
 
154
    lseek(isofd, 0L, SEEK_SET);
 
155
 
 
156
    MD5_Init(&md5ctx);
 
157
    *fragstr = '\0';
 
158
    buf = malloc(bufsize * sizeof(unsigned char));
 
159
 
 
160
    total = 0;
 
161
    /* read up to 15 sectors from end, due to problems reading last few */
 
162
    /* sectors on burned CDs                                            */
 
163
    while (total < isosize - SKIPSECTORS*2048) {
 
164
        nattempt = MIN(isosize - SKIPSECTORS*2048 - total, bufsize);
 
165
        nread = read(isofd, buf, nattempt);
 
166
 
 
167
        if (nread <= 0)
 
168
            break;
 
169
        
 
170
        MD5_Update(&md5ctx, buf, nread);
 
171
 
 
172
        /* if we're onto the next fragment, calculate the previous sum and write */
 
173
        current_fragment = total * (FRAGMENT_COUNT+1) / (isosize - SKIPSECTORS*2048);
 
174
        if ( current_fragment != previous_fragment ) {
 
175
            memcpy(&fragmd5ctx, &md5ctx, sizeof(MD5_CTX));
 
176
            MD5_Final(fragmd5sum, &fragmd5ctx);
 
177
            for (i=0; i<FRAGMENT_SUM_LENGTH/FRAGMENT_COUNT; i++) {
 
178
                char tmpstr[2];
 
179
                snprintf(tmpstr, 2, "%01x", fragmd5sum[i]);
 
180
                strncat(fragstr, tmpstr, 2);
 
181
            }
 
182
            /*  printf("\nFragment [%i]: %s\n", previous_fragment, fragstr);  */
 
183
            previous_fragment = current_fragment;
 
184
        }
 
185
 
 
186
        total = total + nread;
 
187
    }
 
188
    free(buf);
 
189
 
 
190
    MD5_Final(md5sum, &md5ctx);
 
191
 
 
192
    *md5str = '\0';
 
193
    for (i=0; i<16; i++) {
 
194
        char tmpstr[4];
 
195
        snprintf (tmpstr, 4, "%02x", md5sum[i]);
 
196
        strncat(md5str, tmpstr, 2);
 
197
    }
 
198
 
 
199
    if (!quiet) {
 
200
        printf("Inserting md5sum into iso image...\n");
 
201
        printf("md5 = %s\n", md5str);
 
202
        printf("Inserting fragment md5sums into iso image...\n");
 
203
        printf("fragmd5 = %s\n", fragstr);
 
204
        printf("frags = %d\n", FRAGMENT_COUNT);
 
205
    }
 
206
    /*    memcpy(new_appdata, orig_appdata, 512); */
 
207
    memset(new_appdata, ' ', 512);
 
208
 
 
209
    loc = 0;
 
210
    loc = writeAppData(new_appdata, "ISO MD5SUM = ", loc);
 
211
    loc = writeAppData(new_appdata, md5str, loc);
 
212
    loc = writeAppData(new_appdata, ";", loc);
 
213
 
 
214
    buf = malloc(512 * sizeof(unsigned char));
 
215
    snprintf((char *)buf, 512, "SKIPSECTORS = %d", SKIPSECTORS);
 
216
 
 
217
    loc = writeAppData(new_appdata, (char *)buf, loc);
 
218
    loc = writeAppData(new_appdata, ";", loc);
 
219
    free(buf);
 
220
 
 
221
    if (supported) {
 
222
        if (!quiet)
 
223
            printf("Setting supported flag to 1\n");
 
224
        loc = writeAppData(new_appdata, "RHLISOSTATUS=1", loc);
 
225
    } else {
 
226
        if (!quiet)
 
227
            printf("Setting supported flag to 0\n");
 
228
        loc = writeAppData(new_appdata, "RHLISOSTATUS=0", loc);
 
229
    }
 
230
        
 
231
    loc = writeAppData(new_appdata, ";", loc);
 
232
 
 
233
    loc = writeAppData(new_appdata, "FRAGMENT SUMS = ", loc);
 
234
    loc = writeAppData(new_appdata, fragstr, loc);
 
235
    loc = writeAppData(new_appdata, ";", loc);
 
236
 
 
237
    buf = malloc(512 * sizeof(unsigned char));
 
238
    snprintf((char *)buf, 512, "FRAGMENT COUNT = %d", FRAGMENT_COUNT);
 
239
    loc = writeAppData(new_appdata, (char *)buf, loc);
 
240
    loc = writeAppData(new_appdata, ";", loc);
 
241
    free(buf);
 
242
 
 
243
    loc = writeAppData(new_appdata, "THIS IS NOT THE SAME AS RUNNING MD5SUM ON THIS ISO!!", loc);
 
244
    
 
245
    i = lseek(isofd, pvd_offset + APPDATA_OFFSET, SEEK_SET);
 
246
    if (i<0)
 
247
        printf("seek failed\n");
 
248
 
 
249
    i = write(isofd, new_appdata, 512);
 
250
    if (i<0) {
 
251
        printf("write failed %d\n", i);
 
252
        perror("");
 
253
    }
 
254
 
 
255
    close(isofd);
 
256
    errstr = NULL;
 
257
    return 0;
 
258
}