File: method.c

package info (click to toggle)
dvdisaster 0.72.4-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 20,088 kB
  • ctags: 6,526
  • sloc: ansic: 29,301; php: 3,324; sh: 370; xml: 296; makefile: 73; cs: 33
file content (325 lines) | stat: -rw-r--r-- 7,864 bytes parent folder | download | duplicates (2)
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*  dvdisaster: Additional error correction for optical media.
 *  Copyright (C) 2004-2012 Carsten Gnoerlich.
 *  Project home page: http://www.dvdisaster.com
 *  Email: carsten@dvdisaster.com  -or-  cgnoerlich@fsfe.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.
 *
 *  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,
 *  or direct your browser at http://www.gnu.org.
 */

#include "dvdisaster.h"

/***
 *** Collect the available methods
 ***/

/*
 * Invite all methods for registration 
 */

void CollectMethods(void)
{
  BindMethods();
}

/*
 * All methods register by calling this 
 */

void RegisterMethod(Method *method)
{
  g_ptr_array_add(Closure->methodList, method);
}

/*
 * List the available methods
 */

void ListMethods(void)
{  char name[5];
   unsigned int i;

   PrintCLI(_("\nList of available methods:\n\n"));
   name[4] = 0;
 
   for(i=0; i<Closure->methodList->len; i++)
   {  Method *method = g_ptr_array_index(Closure->methodList, i);

      strncpy(name, method->name, 4);
      PrintCLI("%s -- %s\n",name,method->description);
   }
}

/*
 * Call the method destructors
 */

void CallMethodDestructors(void)
{  unsigned int i;

   for(i=0; i<Closure->methodList->len; i++)  
   {  Method *method = g_ptr_array_index(Closure->methodList, i);
 
      method->destroy(method);
      if(method->menuEntry) g_free(method->menuEntry);
      if(method->description) g_free(method->description);
      if(method->lastEh)
	g_free(method->lastEh);
   }
}

/***
 *** Determine methods from name or ecc information
 ***/

/*
 * Find a method by name
 */

Method *FindMethod(char *name)
{  unsigned int i;

   for(i=0; i<Closure->methodList->len; i++) 
   {  Method *method = g_ptr_array_index(Closure->methodList, i);

      if(!strncmp(method->name, name, 4))
        return method;
   }

   return NULL;
}

/*
 * Search for ecc headers in RS02 style image files.
 * Note that udf.c has a similar function FindHeaderInMedium().
 */

static int read_fingerprint(LargeFile *file, unsigned char *fingerprint, gint64 sector)
{  struct MD5Context md5ctxt;
   unsigned char buf[2048];
   int n;

   if(!LargeSeek(file, 2048LL*sector))
     return FALSE;

   n = LargeRead(file, buf, 2048);

   if(n != 2048) return FALSE;

   if(CheckForMissingSector(buf, sector, NULL, 0) != SECTOR_PRESENT)
     return FALSE;

   MD5Init(&md5ctxt);
   MD5Update(&md5ctxt, buf, 2048);
   MD5Final(fingerprint, &md5ctxt);

   return TRUE;
}

EccHeader* FindHeaderInImage(char *filename)
{  EccHeader *eh = NULL;
   LargeFile *file;
   unsigned char buf[4096];
   gint64 length,sectors,pos;
   gint64 header_modulo;
   gint64 last_fp = -1;
   unsigned char fingerprint[16];

   if(!LargeStat(filename, &length))
     return NULL;

   file = LargeOpen(filename, O_RDONLY, IMG_PERMS);
   if(!file) return NULL;

   header_modulo = (gint64)1<<62;
   sectors = length / 2048;

   /*** Search for the headers */

   while(header_modulo >= 32)
   {  pos = sectors & ~(header_modulo - 1);

//printf("Trying modulo %lld\n", header_modulo);

      while(pos > 0)
      {  if(LargeSeek(file, 2048*pos))
	 {  int n;

//printf(" trying sector %lld\n", pos);
	    n = LargeRead(file, buf, sizeof(EccHeader));

	    if(n != sizeof(EccHeader))
	      goto check_next_header;

	    eh = (EccHeader*)buf;

	    /* Medium read error in ecc header? */

	    if(   (CheckForMissingSector(buf, pos, NULL, 0) != SECTOR_PRESENT)
	       || (CheckForMissingSector(buf+2048, pos+1, NULL, 0) != SECTOR_PRESENT))
	    {  
//printf(" header at %lld: read error\n", (long long int)pos);
	       goto check_next_header;
	    }

	    /* See if the magic cookie is there */

	    if(!strncmp((char*)eh->cookie, "*dvdisaster*", 12))
	    {  guint32 recorded_crc = eh->selfCRC;
	       guint32 real_crc;

//printf(" header at %lld: magic cookie found\n", (long long int)pos);

#ifdef HAVE_BIG_ENDIAN
	       eh->selfCRC = 0x47504c00;
#else
	       eh->selfCRC = 0x4c5047;
#endif
	       real_crc = Crc32((unsigned char*)eh, sizeof(EccHeader));

	       if(real_crc == recorded_crc)
	       {  eh = g_malloc(sizeof(EccHeader));
		  memcpy(eh, buf, sizeof(EccHeader));
#ifdef HAVE_BIG_ENDIAN
		  SwapEccHeaderBytes(eh);
#endif
		  eh->selfCRC = recorded_crc;
//printf(" --> CRC okay, using it\n");

		  if(last_fp != eh->fpSector)
		  {  int status;

		     status = read_fingerprint(file, fingerprint, eh->fpSector);
		     last_fp = eh->fpSector;

		     if(!status)  /* be optimistic if fingerprint sector is unreadable */
		     {  LargeClose(file);
		        return eh;
		     }
		  }

		  if(!memcmp(fingerprint, eh->mediumFP, 16))  /* good fingerprint */
		  {  LargeClose(file);
		     return eh;
		  }

		  /* might be a header from a larger previous session.
		     discard it and continue */

		  g_free(eh);
	       }
//printf(" CRC failed, skipping it\n");
	       goto check_next_header;
	    }
	    else
	    {
//printf(" no cookie, skipping current modulo\n");
	      goto check_next_modulo;
	    }
	 }

      check_next_header:
	pos -= header_modulo;
      }

   check_next_modulo:
      header_modulo >>= 1;
   }

   LargeClose(file);
   return NULL;
}

/*
 * Find method for a given ecc file (like in RS01)
 * or augmented image (like in the RS02 image format).
 * Since locating the header is expensive in the RS02 case,
 * it is cached in the corresponding Method struct.
 */

Method *EccFileMethod(int process_error)
{  LargeFile *ecc_file = NULL;
   Method *method;
   EccHeader *eh;
   char method_name[5];
   gint64 length;

   /* First see if an ecc file is available */

   method_name[0] = 0;

   if((ecc_file = LargeOpen(Closure->eccName, O_RDONLY, 0)))
   {  EccHeader eh;
      int n;

      n = LargeRead(ecc_file, &eh, sizeof(EccHeader));
      LargeClose(ecc_file);

      if(n != sizeof(EccHeader))
	goto no_ecc_file;

      if(strncmp((char*)eh.cookie, "*dvdisaster*", 12))
	goto no_ecc_file;

      memcpy(method_name, eh.method, 4); method_name[4] = 0;

      if((method = FindMethod(method_name)))
	return method;
   }

   /* No ecc file, see if the image contains hidden ecc information */

no_ecc_file:
   if(!LargeStat(Closure->imageName, &length))
   {  if(process_error)
      {  if(Closure->guiMode)
	      CreateMessage(_("Image file %s not present.\n"), GTK_MESSAGE_ERROR, Closure->imageName, strerror(errno));
         else Stop(_("Image file %s not present.\n"), Closure->imageName, strerror(errno));
      }
      return NULL;
   }

   eh = FindHeaderInImage(Closure->imageName);

   if(eh)
   {  memcpy(method_name, eh->method, 4); method_name[4] = 0;

      if((method = FindMethod(method_name)))
      {  if(method->lastEh) g_free(method->lastEh);
	 method->lastEh = eh;

	 return method;
      }
      g_free(eh);
   }

   /* No ecc augmented image */

   if(process_error)
   {  if(Closure->guiMode)
      {  if(method_name[0])
	      CreateMessage(_("\nUnknown method %s.\n"), GTK_MESSAGE_ERROR, method_name);
         else CreateMessage(_("\nNeither ecc file nor ecc data in image found.\n"), GTK_MESSAGE_ERROR);
      }
      else
      {  if(method_name[0])
	      Stop(_("\nUnknown method %s.\n"), method_name);
         else Stop(_("\nNeither ecc file nor ecc data in image found.\n"));
      }
   }

   return NULL;
}