Previous 199869 Revisions Next

r20100 Sunday 6th January, 2013 at 12:37:35 UTC by Oliver Stöneberg
improved error reporting of parse_wav_sample() and fixed potential resource leaks / use more proper errorcodes when encountering an error when parsing an input file (nw)
[src/lib/util]chdcd.c

trunk/src/lib/util/chdcd.c
r20099r20100
154154   filerr = osd_open(filename, OPEN_FLAG_READ, &file, &fsize);
155155   if (filerr != FILERR_NONE)
156156   {
157      osd_close(file);
157      printf("ERROR: could not open (%s)\n", filename);
158158      return 0;
159159   }
160160
r20099r20100
162162   osd_read(file, buf, 0, 4, &actual);
163163   offset += actual;
164164   if (offset < 4)
165   {
166      osd_close(file);
167      printf("ERROR: unexpected offset %d (%s)\n", actual, filename);
165168      return 0;
169   }
166170   if (memcmp(&buf[0], "RIFF", 4) != 0)
171   {
172      osd_close(file);
173      printf("ERROR: could not find RIFF header (%s)\n", filename);
167174      return 0;
175   }
168176
169177   /* get the total size */
170178   osd_read(file, &filesize, offset, 4, &actual);
171179   offset += actual;
172180   if (offset < 8)
181   {
182      osd_close(file);
183      printf("ERROR: unexpected offset %d (%s)\n", actual, filename);
173184      return 0;
185   }
174186   filesize = LITTLE_ENDIANIZE_INT32(filesize);
175187
176188   /* read the RIFF file type and make sure it's a WAVE file */
177189   osd_read(file, buf, offset, 4, &actual);
178190   offset += actual;
179191   if (offset < 12)
192   {
193      osd_close(file);
194      printf("ERROR: unexpected offset %d (%s)\n", actual, filename);
180195      return 0;
196   }
181197   if (memcmp(&buf[0], "WAVE", 4) != 0)
198   {
199      osd_close(file);
200      printf("ERROR:could not find WAVE header (%s)\n", filename);
182201      return 0;
202   }
183203
184204   /* seek until we find a format tag */
185205   while (1)
r20099r20100
195215      /* seek to the next block */
196216      offset += length;
197217      if (offset >= filesize)
218      {
219         osd_close(file);
220         printf("ERROR:could not find fmt tag (%s)\n", filename);
198221         return 0;
222      }
199223   }
200224
201225   /* read the format -- make sure it is PCM */
r20099r20100
203227   offset += actual;
204228   temp16 = LITTLE_ENDIANIZE_INT16(temp16);
205229   if (temp16 != 1)
230   {
231      osd_close(file);
232      printf("ERROR: unsupported format %d - only PCM is supported (%s)\n", temp16, filename);
206233      return 0;
234   }
207235
208236   /* number of channels -- only mono is supported */
209237   osd_read(file, &temp16, offset, 2, &actual);
210238   offset += actual;
211239   temp16 = LITTLE_ENDIANIZE_INT16(temp16);
212240   if (temp16 != 2)
241   {
242      osd_close(file);
243      printf("ERROR: unsupported number of channels %d - only mono is supported (%s)\n", temp16, filename);
213244      return 0;
245   }
214246
215247   /* sample rate */
216248   osd_read(file, &rate, offset, 4, &actual);
217249   offset += actual;
218250   rate = LITTLE_ENDIANIZE_INT32(rate);
219251   if (rate != 44100)
252   {
253      osd_close(file);
254      printf("ERROR: unsupported samplerate %d - only 44100 is supported (%s)\n", rate, filename);
220255      return 0;
256   }
221257
222258   /* bytes/second and block alignment are ignored */
223259   osd_read(file, buf, offset, 6, &actual);
r20099r20100
227263   osd_read(file, &bits, offset, 2, &actual);
228264   offset += actual;
229265   if (bits != 16)
266   {
267      osd_close(file);
268      printf("ERROR: unsupported bits/sample %d - only 16 is supported (%s)\n", bits, filename);
230269      return 0;
270   }
231271
232272   /* seek past any extra data */
233273   offset += length - 16;
r20099r20100
246286      /* seek to the next block */
247287      offset += length;
248288      if (offset >= filesize)
289      {
290         osd_close(file);
291         printf("ERROR: could not find data tag (%s)\n", filename);
249292         return 0;
293      }
250294   }
251295
296   osd_close(file);
297
252298   /* if there was a 0 length data block, we're done */
253299   if (length == 0)
254300      return 0;
255301
256   osd_close(file);
257
258302   *dataoffs = offset;
259303
260304   return length;
r20099r20100
333377   {
334378      printf("ERROR: Not a Nero 5.5 or later image!\n");
335379      fclose(infile);
336      return CHDERR_FILE_NOT_FOUND;
380      return CHDERR_UNSUPPORTED_VERSION;
337381   }
338382
339383   chain_offs = buffer[11] | (buffer[10]<<8) | (buffer[9]<<16) | (buffer[8]<<24);
r20099r20100
342386   {
343387      printf("ERROR: File size is > 4GB, this version of CHDMAN cannot handle it.");
344388      fclose(infile);
345      return CHDERR_FILE_NOT_FOUND;
389      return CHDERR_UNSUPPORTED_FORMAT;
346390   }
347391
348392//  printf("NER5 detected, chain offset: %x\n", chain_offs);
r20099r20100
403447               case 0x0300:   // Mode 2 Form 1
404448                  printf("ERROR: Mode 2 Form 1 tracks not supported\n");
405449                  fclose(infile);
406                  return CHDERR_NOT_SUPPORTED;
450                  return CHDERR_UNSUPPORTED_FORMAT;
407451
408452               case 0x0500:   // raw data
409453                  printf("ERROR: Raw data tracks not supported\n");
410454                  fclose(infile);
411                  return CHDERR_NOT_SUPPORTED;
455                  return CHDERR_UNSUPPORTED_FORMAT;
412456
413457               case 0x0600:   // 2352 byte mode 2 raw
414458                  outtoc.tracks[track-1].trktype = CD_TRACK_MODE2_RAW;
r20099r20100
423467               case 0x0f00:   // raw data with sub-channel
424468                  printf("ERROR: Raw data tracks with sub-channel not supported\n");
425469                  fclose(infile);
426                  return CHDERR_NOT_SUPPORTED;
470                  return CHDERR_UNSUPPORTED_FORMAT;
427471
428472               case 0x1000:   // audio with sub-channel
429473                  printf("ERROR: Audio tracks with sub-channel not supported\n");
430474                  fclose(infile);
431                  return CHDERR_NOT_SUPPORTED;
475                  return CHDERR_UNSUPPORTED_FORMAT;
432476
433477               case 0x1100:   // raw Mode 2 Form 1 with sub-channel
434478                  printf("ERROR: Raw Mode 2 Form 1 tracks with sub-channel not supported\n");
435479                  fclose(infile);
436                  return CHDERR_NOT_SUPPORTED;
480                  return CHDERR_UNSUPPORTED_FORMAT;
437481
438482               default:
439483                  printf("ERROR: Unknown track type %x, contact MAMEDEV!\n", mode);
440484                  fclose(infile);
441                  return CHDERR_NOT_SUPPORTED;
485                  return CHDERR_UNSUPPORTED_FORMAT;
442486            }
443487
444488            outtoc.tracks[track-1].datasize = size;
r20099r20100
520564      outinfo.track[0].swap = false;
521565   } else {
522566      printf("ERROR: Unrecognized track type\n");
523      return CHDERR_FILE_NOT_FOUND;
567      return CHDERR_UNSUPPORTED_FORMAT;
524568   }
525569
526570   outtoc.tracks[0].subtype = CD_SUB_NONE;
r20099r20100
720764               wavlen = parse_wav_sample(lastfname, &wavoffs);
721765               if (!wavlen)
722766               {
723                  file_error err;
724                  core_file *fhand;
725
726                  err = core_fopen(lastfname, OPEN_FLAG_READ, &fhand);
727                  if (err != FILERR_NONE) printf("holy moley!\n");
728                  else core_fclose(fhand);
729
730767                  printf("ERROR: couldn't read [%s] or not a valid .WAV\n", lastfname.cstr());
731                  return CHDERR_FILE_NOT_FOUND;
768                  return CHDERR_INVALID_DATA;
732769               }
733770            }
734771            else
735772            {
736773               printf("ERROR: Unhandled track type %s\n", token);
737               return CHDERR_FILE_NOT_FOUND;
774               return CHDERR_UNSUPPORTED_FORMAT;
738775            }
739776         }
740777         else if (!strcmp(token, "TRACK"))
r20099r20100
774811            if (outtoc.tracks[trknum].datasize == 0)
775812            {
776813               printf("ERROR: Unknown track type [%s].  Contact MAMEDEV.\n", token);
777               return CHDERR_FILE_NOT_FOUND;
814               return CHDERR_UNSUPPORTED_FORMAT;
778815            }
779816
780817            /* next (optional) token on the line is the subcode type */
r20099r20100
897934               if (!outtoc.tracks[trknum].frames)
898935               {
899936                  printf("ERROR: unable to determine size of track %d, missing INDEX 01 markers?\n", trknum+1);
900                  return CHDERR_FILE_NOT_FOUND;
937                  return CHDERR_INVALID_DATA;
901938               }
902939            }
903940            else   /* data files are different */
r20099r20100
10791116            if (outtoc.tracks[trknum].datasize == 0)
10801117            {
10811118               printf("ERROR: Unknown track type [%s].  Contact MAMEDEV.\n", token);
1082               return CHDERR_FILE_NOT_FOUND;
1119               return CHDERR_UNSUPPORTED_FORMAT;
10831120            }
10841121
10851122            /* next (optional) token on the line is the subcode type */

Previous 199869 Revisions Next


© 1997-2024 The MAME Team