"Fossies" - the Fresh Open Source Software Archive

Member "libisoburn-1.5.4/libisoburn/isofs_wrap.c" (7 Dec 2020, 20918 Bytes) of package /linux/misc/libisoburn-1.5.4.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "isofs_wrap.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 1.5.0_vs_1.5.2.

    1 
    2 /*
    3   cc -g -c isofs_wrap.c
    4 */
    5 
    6 /*
    7   libisofs related functions of libisoburn.
    8 
    9   Copyright 2007 - 2009  Vreixo Formoso Lopes <metalpain2002@yahoo.es>
   10   Copyright 2007 - 2020  Thomas Schmitt <scdbackup@gmx.net>
   11   Provided under GPL version 2 or later.
   12 */
   13 
   14 #ifdef HAVE_CONFIG_H
   15 #include "../config.h"
   16 #endif
   17 
   18 #include <stdlib.h>
   19 #include <string.h>
   20 #include <stdio.h>
   21 
   22 #ifndef Xorriso_standalonE
   23 
   24 #include <libburn/libburn.h>
   25 
   26 #include <libisofs/libisofs.h>
   27 
   28 #else /* ! Xorriso_standalonE */
   29 
   30 #include "../libisofs/libisofs.h"
   31 #include "../libburn/libburn.h"
   32 
   33 #endif /* Xorriso_standalonE */
   34 
   35 #include "libisoburn.h"
   36 #include "isoburn.h"
   37 
   38 #define BP(a,b) [(b) - (a) + 1]
   39 
   40 struct ecma119_pri_vol_desc
   41 {
   42     uint8_t vol_desc_type       BP(1, 1);
   43     uint8_t std_identifier      BP(2, 6);
   44     uint8_t vol_desc_version    BP(7, 7);
   45     uint8_t unused1         BP(8, 8);
   46     uint8_t system_id       BP(9, 40);
   47     uint8_t volume_id       BP(41, 72);
   48     uint8_t unused2         BP(73, 80);
   49     uint8_t vol_space_size      BP(81, 88);
   50     uint8_t unused3         BP(89, 120);
   51     uint8_t vol_set_size        BP(121, 124);
   52     uint8_t vol_seq_number      BP(125, 128);
   53     uint8_t block_size      BP(129, 132);
   54     uint8_t path_table_size     BP(133, 140);
   55     uint8_t l_path_table_pos    BP(141, 144);
   56     uint8_t opt_l_path_table_pos    BP(145, 148);
   57     uint8_t m_path_table_pos    BP(149, 152);
   58     uint8_t opt_m_path_table_pos    BP(153, 156);
   59     uint8_t root_dir_record     BP(157, 190);
   60     uint8_t vol_set_id      BP(191, 318);
   61     uint8_t publisher_id        BP(319, 446);
   62     uint8_t data_prep_id        BP(447, 574);
   63     uint8_t application_id      BP(575, 702);
   64     uint8_t copyright_file_id   BP(703, 739);
   65     uint8_t abstract_file_id    BP(740, 776);
   66     uint8_t bibliographic_file_id   BP(777, 813);
   67     uint8_t vol_creation_time   BP(814, 830);
   68     uint8_t vol_modification_time   BP(831, 847);
   69     uint8_t vol_expiration_time BP(848, 864);
   70     uint8_t vol_effective_time  BP(865, 881);
   71     uint8_t file_structure_version  BP(882, 882);
   72     uint8_t reserved1       BP(883, 883);
   73     uint8_t app_use         BP(884, 1395);
   74     uint8_t reserved2       BP(1396, 2048);
   75 };
   76 
   77 static
   78 uint32_t iso_read_lsb(const uint8_t *buf, int bytes)
   79 {
   80     int i;
   81     uint32_t ret = 0;
   82 
   83     for (i=0; i<bytes; i++) {
   84         ret += ((uint32_t) buf[i]) << (i*8);
   85     }
   86     return ret;
   87 }
   88 
   89 
   90 /* API function. See libisoburn.h
   91 */
   92 IsoImage *isoburn_get_attached_image(struct burn_drive *d)
   93 {
   94  int ret;
   95  struct isoburn *o= NULL;
   96  ret = isoburn_find_emulator(&o, d, 0);
   97  if (ret < 0)
   98    return NULL;
   99   
  100  if (o == NULL) {
  101    return NULL;
  102  }
  103  iso_image_ref(o->image);
  104  return o->image;
  105 }
  106 
  107 
  108 /* API */
  109 int isoburn_get_attached_start_lba(struct burn_drive *d)
  110 {
  111  int ret;
  112  struct isoburn *o= NULL;
  113 
  114  ret = isoburn_find_emulator(&o, d, 0);
  115  if (ret < 0 || o == NULL)
  116    return -1;
  117  if(o->image == NULL)
  118    return -1;
  119  return o->image_start_lba;
  120 }
  121 
  122 
  123 static void isoburn_idle_free_function(void *ignored)
  124 {
  125  return;
  126 }
  127 
  128 
  129 int isoburn_root_defaults(IsoImage *image, int flag)
  130 {
  131  IsoNode *root_node;
  132  mode_t root_mode= 0755;
  133 
  134  root_node= (IsoNode *) iso_image_get_root(image);
  135  iso_node_set_permissions(root_node, root_mode);
  136  return(1);
  137 }
  138 
  139 
  140 /* API function. See libisoburn.h
  141 */
  142 int isoburn_read_image(struct burn_drive *d,
  143                        struct isoburn_read_opts *read_opts, 
  144                        IsoImage **image)
  145 {
  146  int ret, int_num, dummy, ignore_aclea= 0;
  147  IsoReadOpts *ropts= NULL;
  148  IsoReadImageFeatures *features= NULL;
  149  uint32_t ms_block;
  150  char *msg= NULL;
  151  enum burn_disc_status status= BURN_DISC_BLANK;
  152  IsoDataSource *ds= NULL;
  153  struct isoburn *o= NULL;
  154  IsoImage *new_image= NULL;
  155 
  156  msg= calloc(1, 160);
  157 
  158  if(d != NULL) {
  159    ret = isoburn_find_emulator(&o, d, 0);
  160    if (ret < 0 || o == NULL)
  161      {ret= 0; goto ex;}
  162    status = isoburn_disc_get_status(d);
  163    o->image_start_lba= -1;
  164  }
  165  if(read_opts==NULL) {
  166    isoburn_msgs_submit(o, 0x00060000,
  167                        "Program error: isoburn_read_image: read_opts==NULL",
  168                        0, "FATAL", 0);
  169    {ret= -1; goto ex;}
  170  }
  171  if (d == NULL || status == BURN_DISC_BLANK || read_opts->pretend_blank) {
  172 create_blank_image:;
  173    /*
  174     * Blank disc, we create a new image without files.
  175     */
  176    
  177    if (d == NULL) {
  178      /* New empty image without relation to a drive */
  179      if (image==NULL) {
  180        isoburn_msgs_submit(o, 0x00060000,
  181                            "Program error: isoburn_read_image: image==NULL",
  182                            0, "FATAL", 0);
  183        {ret= -1; goto ex;}
  184      }
  185      /* create a new image */
  186      ret = iso_image_new("ISOIMAGE", image);
  187      if (ret < 0) {
  188        isoburn_report_iso_error(ret, "Cannot create image", 0, "FATAL", 0);
  189        goto ex;
  190      }
  191      new_image= *image;
  192    } else {
  193      /* Blank new image for the drive */
  194      if(o->image != NULL)
  195        ignore_aclea= iso_image_get_ignore_aclea(o->image);
  196      iso_image_unref(o->image);
  197      ret = iso_image_new("ISOIMAGE", &o->image);
  198      if (ret < 0) {
  199        isoburn_report_iso_error(ret, "Cannot create image", 0, "FATAL", 0);
  200        goto ex;
  201      }
  202      if (image != NULL) {
  203        *image = o->image;
  204        iso_image_ref(*image); /*protects object from premature free*/
  205      }
  206      iso_image_set_ignore_aclea(o->image, ignore_aclea);
  207      ret= isoburn_root_defaults(o->image, 0);
  208      if(ret <= 0)
  209        goto ex;
  210      new_image= o->image;
  211    }
  212    ret= iso_image_set_truncate_mode(new_image, read_opts->truncate_mode,
  213                                     read_opts->truncate_length);
  214    if(ret < 0)
  215      goto ex;
  216    {ret= 1; goto ex;}
  217  }
  218  
  219  if (status != BURN_DISC_APPENDABLE && status != BURN_DISC_FULL) {
  220    isoburn_msgs_submit(o, 0x00060000,
  221                     "Program error: isoburn_read_image: incorrect disc status",
  222                     0, "FATAL", 0);
  223    {ret= -4; goto ex;}
  224  }
  225  
  226  ret = isoburn_disc_get_msc1(d, &int_num);
  227  if (ret <= 0)
  228    {ret= -2; goto ex;}
  229  ms_block= int_num;
  230  if (o != NULL)
  231    o->image_start_lba= ms_block;
  232  ret = isoburn_read_iso_head(d, int_num, &dummy, NULL, 0);
  233  if (ret <= 0) {
  234    sprintf(msg, "No ISO 9660 image at LBA %d. Creating blank image.", int_num);
  235    isoburn_msgs_submit(o, 0x00060000, msg, 0, "WARNING", 0);
  236    goto create_blank_image;
  237  }
  238 
  239  if(read_opts->displacement != 0 && abs(read_opts->displacement_sign) == 1) {
  240    /* Apply reverse displacement to session start */
  241    if(read_opts->displacement_sign == -1) {
  242      if(ms_block+ read_opts->displacement < ms_block) {
  243 displacement_rollover:;
  244        sprintf(msg, "Displacement offset leads outside 32 bit range.");
  245        isoburn_msgs_submit(o, 0x00060000, msg, 0, "FAILURE", 0);
  246        {ret= 0; goto ex;}
  247      }
  248      ms_block+= read_opts->displacement;
  249    } else {
  250      if(ms_block < read_opts->displacement)
  251        goto displacement_rollover;
  252      ms_block-= read_opts->displacement;
  253    }
  254  }
  255 
  256 
  257  /* create the data source */
  258  ret = iso_read_opts_new(&ropts, 0);
  259  if (ret < 0) {
  260    isoburn_report_iso_error(ret, "Cannot create write opts", 0, "FATAL", 0);
  261    goto ex;
  262  }
  263 
  264  /* Important: do not return until iso_read_opts_free() */
  265 
  266  iso_read_opts_set_start_block(ropts, ms_block);
  267  iso_read_opts_set_no_rockridge(ropts, read_opts->norock);
  268  iso_read_opts_set_no_aaip(ropts, read_opts->noaaip);
  269  if(read_opts->nomd5 == 2)
  270    int_num= 2;
  271  else if(read_opts->nomd5 == 1)
  272    int_num= 1;
  273  else
  274    int_num= 0;
  275  iso_read_opts_set_no_md5(ropts, int_num);
  276  if(read_opts->do_ecma119_map)
  277    iso_read_opts_set_ecma119_map(ropts, read_opts->map_mode);
  278  if(read_opts->do_joliet_map)
  279    iso_read_opts_set_joliet_map(ropts, read_opts->joliet_map_mode);
  280  iso_read_opts_set_new_inos(ropts, read_opts->noino);
  281 
  282  iso_read_opts_set_no_joliet(ropts, read_opts->nojoliet);
  283  iso_read_opts_set_no_iso1999(ropts, read_opts->noiso1999);
  284  iso_read_opts_set_preferjoliet(ropts, read_opts->preferjoliet);
  285  iso_read_opts_set_default_permissions(ropts,
  286                                        read_opts->mode, read_opts->dirmode);
  287  iso_read_opts_set_default_uid(ropts, read_opts->uid);
  288  iso_read_opts_set_default_gid(ropts, read_opts->gid);
  289  iso_read_opts_set_input_charset(ropts, read_opts->input_charset);
  290  iso_read_opts_auto_input_charset(ropts, read_opts->auto_input_charset);
  291  iso_read_opts_load_system_area(ropts, 1);
  292  iso_read_opts_keep_import_src(ropts, 1);
  293  ret= iso_image_set_truncate_mode(o->image, read_opts->truncate_mode,
  294                                   read_opts->truncate_length);
  295  if(ret < 0)
  296    goto ex;
  297 
  298  ds = isoburn_data_source_new(d, read_opts->displacement,
  299                          read_opts->displacement_sign,
  300                          read_opts->cache_tiles, read_opts->cache_tile_blocks);
  301  if (ds == NULL) {
  302    isoburn_report_iso_error(ret, "Cannot create IsoDataSource object", 0,
  303                             "FATAL", 0);
  304    ret= -1; goto ex;
  305  }
  306  if(o->iso_data_source!=NULL)
  307    iso_data_source_unref(o->iso_data_source);
  308  o->iso_data_source= ds;
  309  iso_image_attach_data(o->image, o->read_pacifier_handle,
  310                        isoburn_idle_free_function);
  311  if(o->read_pacifier_handle==NULL)
  312    iso_tree_set_report_callback(o->image, NULL);
  313  else
  314    iso_tree_set_report_callback(o->image, o->read_pacifier);
  315  
  316  ret = iso_image_import(o->image, ds, ropts, &features);
  317  iso_tree_set_report_callback(o->image, NULL);
  318  iso_read_opts_free(ropts);
  319  ropts= NULL;
  320 
  321  if (ret < 0) {
  322    isoburn_report_iso_error(ret, "Cannot import image", 0, "FAILURE", 0);
  323    goto ex;
  324  }
  325  /* Important: do not return until free(features) */
  326  if (image!=NULL) {
  327    *image = o->image;
  328    iso_image_ref(*image); /*protects object from premature free*/
  329  }
  330  read_opts->hasRR = iso_read_image_features_has_rockridge(features);
  331  read_opts->hasJoliet = iso_read_image_features_has_joliet(features);
  332  read_opts->hasIso1999 = iso_read_image_features_has_iso1999(features);
  333  read_opts->hasElTorito = iso_read_image_features_has_eltorito(features);
  334  read_opts->size = iso_read_image_features_get_size(features);
  335  read_opts->tree_loaded = iso_read_image_features_tree_loaded(features);
  336  read_opts->rr_loaded = iso_read_image_features_rr_loaded(features);
  337  ret= 1;
  338 ex:;
  339  if(msg != NULL)
  340    free(msg);
  341  if(ropts != NULL)
  342    iso_read_opts_free(ropts);
  343  if(features != NULL)
  344     iso_read_image_features_destroy(features);
  345  return(ret);
  346 }
  347 
  348 
  349 /* API function. See libisoburn.h
  350 */
  351 int isoburn_attach_image(struct burn_drive *d, IsoImage *image)
  352 {
  353  int ret;
  354  struct isoburn *o;
  355 
  356  ret = isoburn_find_emulator(&o, d, 0);
  357  if (ret < 0 || o == NULL)
  358    return 0;
  359  if (image == NULL) {
  360    isoburn_msgs_submit(o, 0x00060000,
  361                        "Program error: isoburn_attach_image: image==NULL",
  362                        0, "FATAL", 0);
  363    return -1;
  364  }
  365  if(o->image != NULL)
  366    iso_image_unref(o->image);
  367  o->image = image;
  368  o->image_start_lba = -1;
  369  return(1);
  370 }
  371 
  372 
  373 /* API */
  374 int isoburn_attach_start_lba(struct burn_drive *d, int lba, int flag)
  375 {
  376  int ret;
  377  struct isoburn *o;
  378 
  379  ret = isoburn_find_emulator(&o, d, 0);
  380  if(ret < 0)
  381    return ret;
  382  if(o == NULL)
  383    return 0;
  384  if(o->image == NULL)
  385    return 0;
  386  o->image_start_lba = lba;
  387  return 1;
  388 }
  389 
  390 
  391 /* API function. See libisoburn.h
  392 */
  393 int isoburn_activate_session(struct burn_drive *drive)
  394 {
  395  int ret, do_sync = 1;
  396  struct isoburn *o;
  397 
  398  ret = isoburn_find_emulator(&o, drive, 0);
  399  if (ret < 0)
  400    return -1;
  401 
  402  if (o->emulation_mode != 1)
  403    return 1; /* don't need to activate session */
  404  if (o->fabricated_msc2 >= 0)
  405    return 1; /* blind growing: do not alter anything outside the session */
  406  
  407  if (!(o->fabricated_disc_status == BURN_DISC_APPENDABLE ||
  408        (o->fabricated_disc_status == BURN_DISC_BLANK &&
  409         o->zero_nwa > 0)))
  410    return 1;
  411  ret = burn_drive_get_drive_role(drive);
  412  if (ret != 1)
  413    do_sync = !! o->do_fsync;
  414 
  415  ret = burn_random_access_write(drive, (off_t) 0, (char*)o->target_iso_head, 
  416                                 o->target_iso_head_size, do_sync);
  417 
  418  return ret;
  419 }
  420 
  421 
  422 /** API @since 0.6.2
  423 */
  424 int isoburn_get_img_partition_offset(struct burn_drive *drive,
  425                                      uint32_t *block_offset_2k)
  426 {
  427  int ret;
  428  struct isoburn *o;
  429 
  430  ret = isoburn_find_emulator(&o, drive, 0);
  431  if(ret < 0 || o == NULL)
  432    return -1;
  433  *block_offset_2k= o->loaded_partition_offset;
  434  if(o->loaded_partition_offset == 0)
  435    return(0);
  436  if(o->target_iso_head_size == (off_t) Libisoburn_target_head_sizE
  437                            + (off_t) 2048 * (off_t) o->loaded_partition_offset)
  438    return(1);
  439  return(2);
  440 }
  441 
  442 
  443 /* Check for MBR signature and a first partition that starts at a 2k block 
  444    and ends where the image ends.
  445    If not too large or too small, accept its start as partition offset.
  446 */
  447 static int isoburn_inspect_partition(struct isoburn *o, uint32_t img_size,
  448                                      int flag)
  449 {
  450  uint8_t *mbr, *part, *buf= NULL;
  451  uint32_t offst, numsec;
  452  struct ecma119_pri_vol_desc *pvm;
  453  off_t data_count;
  454  int ret;
  455  char *msg= NULL;
  456  static int max_offst= 512 - 32;
  457 
  458  buf= (uint8_t *) calloc(1, 2048);
  459  msg= calloc(1, 160);
  460  if(buf == NULL || msg == NULL)
  461    {ret= -1; goto ex;}
  462 
  463  mbr= o->target_iso_head;
  464  part= mbr + 446;
  465  if(mbr[510] != 0x55 || mbr[511] != 0xAA)
  466    {ret= 2; goto ex;} /* not an MBR */
  467 
  468  /* Does the first partition entry look credible ? */
  469  if(part[0] != 0x80 && part[0] != 0x00)
  470    {ret= 2; goto ex;} /* Invalid partition status */
  471  if(part[1] == 0 && part[2] == 0 && part[3] == 0)
  472    {ret= 2; goto ex;} /* Zero C/H/S start address */
  473 
  474  /* Does it match the normal ISO image ? */
  475  offst= iso_read_lsb(part + 8, 4);
  476  numsec= iso_read_lsb(part + 12, 4);
  477  if(offst < 64)
  478    {ret= 2; goto ex;} /* Zero or unusably small partition start */
  479  if((offst % 4) || (numsec % 4))
  480    {ret= 2; goto ex;} /* Not aligned to 2k */
  481  if(numsec < 72)
  482    {ret= 2; goto ex;} /* No room for volume descriptors */
  483  offst/= 4;
  484  numsec/= 4;
  485  if(offst + numsec > img_size)
  486    {ret= 2; goto ex;} /* Partition end exceeds image end */
  487 
  488  /* Is there a PVD at the partition start ? */
  489  ret = burn_read_data(o->drive, (off_t) (offst + 16) * (off_t) 2048,
  490                       (char*) buf, 2048, &data_count, 32);
  491  if(ret <= 0)
  492    {ret= 2; goto ex;}
  493  pvm = (struct ecma119_pri_vol_desc *) buf;
  494  if (strncmp((char*) pvm->std_identifier, "CD001", 5) != 0)
  495    {ret= 2; goto ex;} /* not a PVD */
  496  if (pvm->vol_desc_type[0] != 1 || pvm->vol_desc_version[0] != 1 
  497      || pvm->file_structure_version[0] != 1 )
  498    {ret= 2; goto ex;} /* failed sanity check */
  499 
  500  if(iso_read_lsb(pvm->vol_space_size, 4) + offst > img_size)
  501    {ret= 2; goto ex;} /* Image ends do not match plausibly */
  502 
  503  /* Now it is credible. Not yet clear is whether it is acceptable. */
  504  o->loaded_partition_offset= offst;
  505 
  506  /* If the partition start is too large: Report but do not accept. */
  507  if(offst > (uint32_t) max_offst) {/* Not more than 1 MB of .target_iso_head */
  508    sprintf(msg,
  509       "Detected partition offset of %.f blocks. Maximum for load buffer is %d",
  510       (double) offst, max_offst);
  511    isoburn_msgs_submit(NULL, 0x00060000, msg, 0, "WARNING", 0);
  512    {ret= 3; goto ex;}
  513  }
  514 
  515  /* Accept partition start and adjust buffer size */
  516  ret= isoburn_adjust_target_iso_head(o, offst, 0);
  517  if(ret <= 0)
  518    goto ex;
  519 
  520  ret= 1;
  521 ex:;
  522  if(buf != NULL)
  523    free(buf);
  524  if(msg != NULL)
  525    free(msg);
  526  return(ret);
  527 }
  528 
  529 
  530 /** Initialize the emulation of multi-session on random access media.
  531     The need for emulation is confirmed already.
  532     @param o A freshly created isoburn object. isoburn_create_data_source() was
  533              already called, nevertheless.
  534     @param flag bit0= read-only
  535     @return <=0 error , 1 = success
  536 */
  537 int isoburn_start_emulation(struct isoburn *o, int flag)
  538 {
  539  int ret, i, capacity = -1, role, dummy;
  540  off_t data_count, to_read;
  541  struct burn_drive *drive;
  542  struct ecma119_pri_vol_desc *pvm;
  543  enum burn_disc_status s;
  544  char *path= NULL, *msg= NULL;
  545 
  546  path= calloc(1, BURN_DRIVE_ADR_LEN);
  547  msg= calloc(1, 2 * BURN_DRIVE_ADR_LEN);
  548  if(path == NULL || msg == NULL)
  549    {ret= -1; goto ex;} 
  550 
  551  if(o==NULL) {
  552    isoburn_msgs_submit(NULL, 0x00060000,
  553                        "Program error: isoburn_start_emulation: o==NULL",
  554                        0, "FATAL", 0);
  555    {ret= -1; goto ex;}
  556  }
  557 
  558  drive= o->drive;
  559 
  560  if(flag & 1)
  561    o->fabricated_disc_status= BURN_DISC_FULL;
  562 
  563  /* We can assume 0 as start block for image.
  564     The data there point to the most recent session.
  565  */
  566  role = burn_drive_get_drive_role(drive);
  567  ret = burn_get_read_capacity(drive, &capacity, 0);
  568  if (ret <= 0)
  569    capacity = -1;
  570  if (role == 5) { /* random access write-only medium */
  571    s = burn_disc_get_status(drive);
  572    o->fabricated_disc_status= s;
  573    burn_disc_track_lba_nwa(drive, NULL, 0, &dummy, &(o->nwa));
  574    if(o->nwa < o->zero_nwa)
  575      o->zero_nwa= 0;
  576    {ret= 1; goto ex;}
  577  } else if (capacity > 0 || role == 2 || role == 4) {
  578    /* Might be a block device on a system where libburn cannot determine its
  579       size.  Try to read anyway. */
  580    to_read = o->target_iso_head_size;
  581    memset(o->target_iso_head, 0, to_read);
  582    if(capacity > 0 && (off_t) capacity * (off_t) 2048 < to_read)
  583      to_read = (off_t) capacity * (off_t) 2048;
  584    ret = burn_read_data(drive, (off_t) 0, (char*)o->target_iso_head, 
  585                         to_read, &data_count, 32 | 8);
  586    if (ret <= 0) {
  587      /* an error means a disc with no ISO image */
  588      o->media_read_error= 1;
  589      if (ret == -2) {
  590        path[0]= 0;
  591        burn_drive_d_get_adr(drive, path);
  592        sprintf(msg, "Pseudo drive '%s' does not allow reading", path);
  593        isoburn_msgs_submit(NULL, 0x00060000, msg, 0, "NOTE", 0);
  594        o->fabricated_disc_status= BURN_DISC_BLANK;
  595      } else if (capacity > 0)
  596        o->fabricated_disc_status= BURN_DISC_FULL;
  597      else if(!(flag & 1))
  598        o->fabricated_disc_status= BURN_DISC_BLANK;
  599      {ret= 1; goto ex;}
  600    }
  601  } else {
  602    /* No read capacity means blank medium */
  603    if(!(flag & 1))
  604      o->fabricated_disc_status= BURN_DISC_BLANK;
  605    {ret= 1; goto ex;}
  606  }
  607 
  608  /* check first 64K. If 0's, the disc is treated as a blank disc, and thus
  609     overwritten without extra check. */
  610  i = Libisoburn_target_head_sizE;
  611  while (i && !o->target_iso_head[i-1]) 
  612    --i;
  613 
  614  if (!i) {
  615    if(!(flag & 1))
  616      o->fabricated_disc_status= BURN_DISC_BLANK;
  617    {ret= 1; goto ex;}
  618  }
  619 
  620  pvm = (struct ecma119_pri_vol_desc *)(o->target_iso_head + 16 * 2048);
  621 
  622  if (strncmp((char*)pvm->std_identifier, "CD001", 5) == 0) {
  623    off_t size;
  624 
  625    /* sanity check */
  626    if (pvm->vol_desc_type[0] != 1 || pvm->vol_desc_version[0] != 1 
  627        || pvm->file_structure_version[0] != 1 ) {
  628      /* TODO for now I treat this as a full disc */
  629      o->fabricated_disc_status= BURN_DISC_FULL;
  630      {ret= 1; goto ex;}
  631    }
  632 
  633    /* ok, PVM found, set size */
  634    size = (off_t) iso_read_lsb(pvm->vol_space_size, 4);
  635    ret= isoburn_inspect_partition(o, (uint32_t) size, 0);
  636    if (ret <= 0)
  637      goto ex;
  638    size *= (off_t) 2048; /* block size in bytes */
  639    isoburn_set_start_byte(o, size, 0);
  640    if(!(flag & 1))
  641      o->fabricated_disc_status= BURN_DISC_APPENDABLE;
  642  } else if (strncmp((char*)pvm->std_identifier, "CDXX1", 5) == 0 ||
  643             (strncmp((char*)pvm->std_identifier, "CDxx1", 5) == 0 &&
  644              pvm->vol_desc_type[0] == 'x')) {
  645 
  646    /* empty image */
  647    isoburn_set_start_byte(o, o->zero_nwa * 2048, 0);
  648    if(!(flag & 1))
  649      o->fabricated_disc_status= BURN_DISC_BLANK;
  650  } else {
  651    /* treat any disc in an unknown format as full */
  652    o->fabricated_disc_status= BURN_DISC_FULL;
  653  }
  654 
  655  ret= 1;
  656 ex:;
  657  if(path != NULL)
  658    free(path);
  659  if(msg != NULL)
  660    free(msg);
  661  return(ret);
  662 }
  663 
  664 
  665 /** Alters and writes the first 64 kB of a "medium" to invalidate
  666     an ISO image. (It shall stay restorable by skilled humans, though).
  667     The result shall especially keep libisoburn from accepting the medium
  668     image as ISO filesystem.
  669     @param o A fully activated isoburn object. isoburn_start_emulation()
  670              was already called.
  671     @return <=0 error , 1 = success
  672 */
  673 int isoburn_invalidate_iso(struct isoburn *o, int flag)
  674 {
  675  int end_ed_found= 0, i;
  676  char *head;
  677 
  678  head= (char *) o->target_iso_head;
  679  /* 
  680   * replace CD001 with CDXX1 in PVM.
  681   */
  682  memcpy(head + 16 * 2048 + 1, "CDXX1", 5);
  683 
  684  /* Invalidate further ECMA-119 volume descriptors and possible UDF volume
  685     recognition sequence */
  686  for(i= 17 * 2048; i < 32 * 2048; i+= 2048) {
  687    if(end_ed_found) {
  688      if(head[i] == 0 && strncmp(head + i + 1, "BEA01", 5) == 0)
  689        memcpy(head + i + 1, "BEAX1", 5);
  690      else if(head[i] == 0 && strncmp(head + i + 1, "NSR", 3) == 0)
  691        memcpy(head + i + 1, "NSRX", 4);
  692      else if(head[i] == 0 && strncmp(head + i + 1, "TEA", 3) == 0) 
  693        memcpy(head + i + 1, "TEAX", 4);
  694    } else if(strncmp(head + i + 1, "CD001", 5) == 0) {
  695      if(((unsigned char *) head)[i] == 0xff)
  696        end_ed_found= 1;
  697      memcpy(head + i + 3, "XX", 2);
  698    }
  699  }
  700 
  701  return isoburn_activate_session(o->drive);
  702 }
  703 
  704 
  705 /* API @since 0.1.0 */
  706 int isoburn_set_read_pacifier(struct burn_drive *drive,
  707                               int (*read_pacifier)(IsoImage*, IsoFileSource*),
  708                               void *read_handle)
  709 {
  710  int ret;
  711  struct isoburn *o;
  712 
  713  ret = isoburn_find_emulator(&o, drive, 0);
  714  if(ret < 0 || o == NULL)
  715    return -1;
  716  o->read_pacifier_handle= read_handle;
  717  o->read_pacifier= read_pacifier;
  718  return(1);
  719 }
  720