[DVDnav-discuss] [PATCH] Cleanup ifoFree()

Steve Dibb beandog at gentoo.org
Tue Jan 6 03:31:49 CET 2015


- Move all ifoFree_*() functions only called once directly into
  ifoFree()
- Proper checks for NULL pointers, freeing pointers
---
 src/dvdread/ifo_read.h |  13 +-
 src/ifo_read.c         | 503
  ++++++++++++++++++++++++------------------------- 2 files changed,
  244 insertions(+), 272 deletions(-)

diff --git a/src/dvdread/ifo_read.h b/src/dvdread/ifo_read.h
index 97f4179..d29c3f9 100644
--- a/src/dvdread/ifo_read.h
+++ b/src/dvdread/ifo_read.h
@@ -209,19 +209,8 @@ int ifoRead_TXTDT_MGI(ifo_handle_t *);
  * below are safe:  they will not mind if you attempt to free part of
an IFO
  * file which was not read in or which does not exist.
  */
-void ifoFree_PTL_MAIT(ifo_handle_t *);
-void ifoFree_VTS_ATRT(ifo_handle_t *);
-void ifoFree_TT_SRPT(ifo_handle_t *);
-void ifoFree_VTS_PTT_SRPT(ifo_handle_t *);
-void ifoFree_FP_PGC(ifo_handle_t *);
-void ifoFree_PGCIT(ifo_handle_t *);
-void ifoFree_PGCI_UT(ifo_handle_t *);
 void ifoFree_VTS_TMAPT(ifo_handle_t *);
-void ifoFree_C_ADT(ifo_handle_t *);
-void ifoFree_TITLE_C_ADT(ifo_handle_t *);
-void ifoFree_VOBU_ADMAP(ifo_handle_t *);
-void ifoFree_TITLE_VOBU_ADMAP(ifo_handle_t *);
-void ifoFree_TXTDT_MGI(ifo_handle_t *);
+void ifoFree_PGCI_UT(ifo_handle_t *);
 
 #ifdef __cplusplus
 };
diff --git a/src/ifo_read.c b/src/ifo_read.c
index 807ebac..97c9b8e 100644
--- a/src/ifo_read.c
+++ b/src/ifo_read.c
@@ -87,9 +87,7 @@ static int ifoRead_VOBU_ADMAP_internal(ifo_handle_t
*ifofile, static int ifoRead_PGCIT_internal(ifo_handle_t *ifofile,
pgcit_t *pgcit, unsigned int offset);
 
-static void ifoFree_PGC(pgc_t **pgc);
-static void ifoFree_PGC_COMMAND_TBL(pgc_command_tbl_t *cmd_tbl);
-static void ifoFree_PGCIT_internal(pgcit_t **pgcit);
+static void ifoFree_PGC(pgc_t *pgc);
 
 static inline int DVDFileSeekForce_( dvd_file_t *dvd_file, uint32_t
offset, int force_size ) { return (DVDFileSeekForce(dvd_file,
(int)offset, force_size) == (int)offset); @@ -487,30 +485,152 @@ void
ifoClose(ifo_handle_t *ifofile) { if(!ifofile)
     return;
 
-  ifoFree_VOBU_ADMAP(ifofile);
-  ifoFree_TITLE_VOBU_ADMAP(ifofile);
-  ifoFree_C_ADT(ifofile);
-  ifoFree_TITLE_C_ADT(ifofile);
-  ifoFree_TXTDT_MGI(ifofile);
-  ifoFree_VTS_ATRT(ifofile);
-  ifoFree_PTL_MAIT(ifofile);
-  ifoFree_PGCI_UT(ifofile);
-  ifoFree_TT_SRPT(ifofile);
-  ifoFree_FP_PGC(ifofile);
-  ifoFree_PGCIT(ifofile);
-  ifoFree_VTS_PTT_SRPT(ifofile);
-  ifoFree_VTS_TMAPT(ifofile);
-
-  if(ifofile->vmgi_mat)
-    free(ifofile->vmgi_mat);
+  // ifoFree_VOBU_ADMAP(ifofile);
+  if(ifofile->menu_vobu_admap) {
+    free(ifofile->menu_vobu_admap->vobu_start_sectors);
+    free(ifofile->menu_vobu_admap);
+  }
 
-  if(ifofile->vtsi_mat)
-    free(ifofile->vtsi_mat);
+  // ifoFree_TITLE_VOBU_ADMAP(ifofile);
+  if(ifofile->vts_vobu_admap) {
+    free(ifofile->vts_vobu_admap->vobu_start_sectors);
+    free(ifofile->vts_vobu_admap);
+  }
+
+  // ifoFree_C_ADT(ifofile);
+  if(ifofile->menu_c_adt) {
+    free(ifofile->menu_c_adt->cell_adr_table);
+    free(ifofile->menu_c_adt);
+  }
+
+  // ifoFree_TITLE_C_ADT(ifofile);
+  if(ifofile->vts_c_adt) {
+    free(ifofile->vts_c_adt->cell_adr_table);
+    free(ifofile->vts_c_adt);
+  }
+
+  // ifoFree_TXTDT_MGI(ifofile);
+  if(ifofile->txtdt_mgi)
+    free(ifofile->txtdt_mgi);
+
+  // ifoFree_VTS_ATRT(ifofile);
+  if(ifofile->vts_atrt) {
+    free(ifofile->vts_atrt->vts);
+    free(ifofile->vts_atrt->vts_atrt_offsets);
+    free(ifofile->vts_atrt);
+  }
+
+  // ifoFree_PTL_MAIT(ifofile);
+  if(ifofile->ptl_mait) {
+    if(ifofile->ptl_mait->countries) {
+      unsigned int i;
+
+      for(i = 0; i < ifofile->ptl_mait->nr_of_countries; i++) {
+        free(ifofile->ptl_mait->countries[i].pf_ptl_mai);
+      }
+      free(ifofile->ptl_mait->countries);
+    }
+    free(ifofile->ptl_mait);
+  }
+
+  // ifoFree_PGCI_UT(ifofile);
+  if(ifofile->pgci_ut) {
+    unsigned int a, b;
+
+    for(a = 0; a < ifofile->pgci_ut->nr_of_lus; a++) {
+
+      for(b = 0; b < ifofile->pgci_ut->lu[a].pgcit->nr_of_pgci_srp;
b++) { +
+
if(ifofile->pgci_ut->lu[a].pgcit->pgci_srp[b].pgc->command_tbl) {
+
free(ifofile->pgci_ut->lu[a].pgcit->pgci_srp[b].pgc->command_tbl->pre_cmds);
+
free(ifofile->pgci_ut->lu[a].pgcit->pgci_srp[b].pgc->command_tbl->post_cmds);
+
free(ifofile->pgci_ut->lu[a].pgcit->pgci_srp[b].pgc->command_tbl->cell_cmds);
+
free(ifofile->pgci_ut->lu[a].pgcit->pgci_srp[b].pgc->command_tbl);
+        }
+
free(ifofile->pgci_ut->lu[a].pgcit->pgci_srp[b].pgc->program_map);
+
free(ifofile->pgci_ut->lu[a].pgcit->pgci_srp[b].pgc->cell_playback);
+
free(ifofile->pgci_ut->lu[a].pgcit->pgci_srp[b].pgc->cell_position);
+        free(ifofile->pgci_ut->lu[a].pgcit->pgci_srp[b].pgc);
+
+      }
+      free(ifofile->pgci_ut->lu[a].pgcit->pgci_srp);
+      free(ifofile->pgci_ut->lu[a].pgcit);
+    }
+    free(ifofile->pgci_ut->lu);
+    free(ifofile->pgci_ut);
+  }
+
+  // ifoFree_TT_SRPT(ifofile);
+  if(ifofile->tt_srpt) {
+    free(ifofile->tt_srpt->title);
+    free(ifofile->tt_srpt);
+  }
+
+  // ifoFree_FP_PGC(ifofile);
+  if(ifofile->first_play_pgc) {
+    if(ifofile->first_play_pgc->command_tbl) {
+      free(ifofile->first_play_pgc->command_tbl->pre_cmds);
+      free(ifofile->first_play_pgc->command_tbl->post_cmds);
+      free(ifofile->first_play_pgc->command_tbl->cell_cmds);
+      free(ifofile->first_play_pgc->command_tbl);
+    }
+    free(ifofile->first_play_pgc->program_map);
+    free(ifofile->first_play_pgc->cell_playback);
+    free(ifofile->first_play_pgc->cell_position);
+  }
+
+  // ifoFree_PGCIT(ifofile);
+  if(ifofile->vts_pgcit) {
+
+      unsigned int b;
+
+      for(b = 0; b < ifofile->vts_pgcit->nr_of_pgci_srp; b++) {
+
+        if(ifofile->vts_pgcit->pgci_srp[b].pgc->command_tbl) {
+
free(ifofile->vts_pgcit->pgci_srp[b].pgc->command_tbl->pre_cmds);
+
free(ifofile->vts_pgcit->pgci_srp[b].pgc->command_tbl->post_cmds);
+
free(ifofile->vts_pgcit->pgci_srp[b].pgc->command_tbl->cell_cmds);
+          free(ifofile->vts_pgcit->pgci_srp[b].pgc->command_tbl);
+        }
+        free(ifofile->vts_pgcit->pgci_srp[b].pgc->program_map);
+        free(ifofile->vts_pgcit->pgci_srp[b].pgc->cell_playback);
+        free(ifofile->vts_pgcit->pgci_srp[b].pgc->cell_position);
+        free(ifofile->vts_pgcit->pgci_srp[b].pgc);
+
+      }
+      free(ifofile->vts_pgcit->pgci_srp);
+      free(ifofile->vts_pgcit);
+  }
+
+  // ifoFree_VTS_PTT_SRPT(ifofile);
+  if(ifofile->vts_ptt_srpt) {
+    unsigned int i;
+    for(i = 0; i < ifofile->vts_ptt_srpt->nr_of_srpts; i++) {
+      free(ifofile->vts_ptt_srpt->title[i].ptt);
+    }
+    free(ifofile->vts_ptt_srpt->ttu_offset);
+    free(ifofile->vts_ptt_srpt->title);
+    free(ifofile->vts_ptt_srpt);
+  }
+
+  if(ifofile->vts_tmapt) {
+    unsigned int i;
+
+    for(i = 0; i < ifofile->vts_tmapt->nr_of_tmaps; i++) {
+      if(ifofile->vts_tmapt->tmap[i].map_ent) {
+        free(ifofile->vts_tmapt->tmap[i].map_ent);
+      }
+    }
+    free(ifofile->vts_tmapt->tmap);
+    free(ifofile->vts_tmapt->tmap_offset);
+    free(ifofile->vts_tmapt);
+  }
+
+  free(ifofile->vmgi_mat);
+  free(ifofile->vtsi_mat);
 
   DVDCloseFile(ifofile->file);
-  ifofile->file = 0;
   free(ifofile);
-  ifofile = 0;
 }
 
 
@@ -525,19 +645,19 @@ static int ifoRead_VMG(ifo_handle_t *ifofile) {
 
   if(!DVDFileSeek_(ifofile->file, 0)) {
     free(ifofile->vmgi_mat);
-    ifofile->vmgi_mat = 0;
+    ifofile->vmgi_mat = NULL;
     return 0;
   }
 
   if(!DVDReadBytes(ifofile->file, vmgi_mat, sizeof(vmgi_mat_t))) {
     free(ifofile->vmgi_mat);
-    ifofile->vmgi_mat = 0;
+    ifofile->vmgi_mat = NULL;
     return 0;
   }
 
   if(strncmp("DVDVIDEO-VMG", vmgi_mat->vmg_identifier, 12) != 0) {
     free(ifofile->vmgi_mat);
-    ifofile->vmgi_mat = 0;
+    ifofile->vmgi_mat = NULL;
     return 0;
   }
 
@@ -748,6 +868,7 @@ static int ifoRead_PGC_COMMAND_TBL(ifo_handle_t
*ifofile, 
     if(!(DVDReadBytes(ifofile->file, cmd_tbl->pre_cmds,
pre_cmds_size))) { free(cmd_tbl->pre_cmds);
+      cmd_tbl->pre_cmds = NULL;
       return 0;
     }
   }
@@ -756,14 +877,15 @@ static int ifoRead_PGC_COMMAND_TBL(ifo_handle_t
*ifofile, unsigned int post_cmds_size = cmd_tbl->nr_of_post *
COMMAND_DATA_SIZE; cmd_tbl->post_cmds = malloc(post_cmds_size);
     if(!cmd_tbl->post_cmds) {
-      if(cmd_tbl->pre_cmds)
-        free(cmd_tbl->pre_cmds);
+      free(cmd_tbl->pre_cmds);
+      cmd_tbl->pre_cmds = NULL;
       return 0;
     }
     if(!(DVDReadBytes(ifofile->file, cmd_tbl->post_cmds,
post_cmds_size))) {
-      if(cmd_tbl->pre_cmds)
-        free(cmd_tbl->pre_cmds);
+      free(cmd_tbl->pre_cmds);
+      cmd_tbl->pre_cmds = NULL;
       free(cmd_tbl->post_cmds);
+      cmd_tbl->post_cmds = NULL;
       return 0;
     }
   }
@@ -772,18 +894,19 @@ static int ifoRead_PGC_COMMAND_TBL(ifo_handle_t
*ifofile, unsigned int cell_cmds_size = cmd_tbl->nr_of_cell *
COMMAND_DATA_SIZE; cmd_tbl->cell_cmds = malloc(cell_cmds_size);
     if(!cmd_tbl->cell_cmds) {
-      if(cmd_tbl->pre_cmds)
-        free(cmd_tbl->pre_cmds);
-      if(cmd_tbl->post_cmds)
-        free(cmd_tbl->post_cmds);
+      free(cmd_tbl->pre_cmds);
+      cmd_tbl->pre_cmds = NULL;
+      free(cmd_tbl->post_cmds);
+      cmd_tbl->post_cmds = NULL;
       return 0;
     }
     if(!(DVDReadBytes(ifofile->file, cmd_tbl->cell_cmds,
cell_cmds_size))) {
-      if(cmd_tbl->pre_cmds)
-        free(cmd_tbl->pre_cmds);
-      if(cmd_tbl->post_cmds)
-        free(cmd_tbl->post_cmds);
+      free(cmd_tbl->pre_cmds);
+      cmd_tbl->pre_cmds = NULL;
+      free(cmd_tbl->post_cmds);
+      cmd_tbl->post_cmds = NULL;
       free(cmd_tbl->cell_cmds);
+      cmd_tbl->cell_cmds = NULL;
       return 0;
     }
   }
@@ -795,18 +918,6 @@ static int ifoRead_PGC_COMMAND_TBL(ifo_handle_t
*ifofile, }
 
 
-static void ifoFree_PGC_COMMAND_TBL(pgc_command_tbl_t *cmd_tbl) {
-  if(cmd_tbl) {
-    if(cmd_tbl->nr_of_pre && cmd_tbl->pre_cmds)
-      free(cmd_tbl->pre_cmds);
-    if(cmd_tbl->nr_of_post && cmd_tbl->post_cmds)
-      free(cmd_tbl->post_cmds);
-    if(cmd_tbl->nr_of_cell && cmd_tbl->cell_cmds)
-      free(cmd_tbl->cell_cmds);
-    free(cmd_tbl);
-  }
-}
-
 static int ifoRead_PGC_PROGRAM_MAP(ifo_handle_t *ifofile,
                                    pgc_program_map_t *program_map,
                                    unsigned int nr, unsigned int
offset) { @@ -922,6 +1033,8 @@ static int ifoRead_PGC(ifo_handle_t
*ifofile, pgc_t *pgc, unsigned int offset) { 
     if(!ifoRead_PGC_COMMAND_TBL(ifofile, pgc->command_tbl,
                                 offset + pgc->command_tbl_offset)) {
+      free(pgc->command_tbl);
+      pgc->command_tbl = NULL;
       return 0;
     }
   } else {
@@ -935,6 +1048,8 @@ static int ifoRead_PGC(ifo_handle_t *ifofile,
pgc_t *pgc, unsigned int offset) { }
     if(!ifoRead_PGC_PROGRAM_MAP(ifofile,
pgc->program_map,pgc->nr_of_programs, offset +
pgc->program_map_offset)) {
+      free(pgc->program_map);
+      pgc->program_map = NULL;
       return 0;
     }
   } else {
@@ -949,6 +1064,8 @@ static int ifoRead_PGC(ifo_handle_t *ifofile,
pgc_t *pgc, unsigned int offset)
{ if(!ifoRead_CELL_PLAYBACK_TBL(ifofile, pgc->cell_playback,
pgc->nr_of_cells, offset + pgc->cell_playback_offset)) {
+      free(pgc->cell_playback);
+      pgc->cell_playback = NULL;
       return 0;
     }
   } else {
@@ -963,6 +1080,8 @@ static int ifoRead_PGC(ifo_handle_t *ifofile,
pgc_t *pgc, unsigned int offset)
{ if(!ifoRead_CELL_POSITION_TBL(ifofile, pgc->cell_position,
pgc->nr_of_cells, offset + pgc->cell_position_offset)) {
+      free(pgc->cell_position);
+      pgc->cell_position = NULL;
       return 0;
     }
   } else {
@@ -992,38 +1111,33 @@ int ifoRead_FP_PGC(ifo_handle_t *ifofile) {
   ifofile->first_play_pgc->ref_count = 1;
   if(!ifoRead_PGC(ifofile, ifofile->first_play_pgc,
                   ifofile->vmgi_mat->first_play_pgc)) {
-    ifoFree_PGC(&ifofile->first_play_pgc);
+    ifoFree_PGC(ifofile->first_play_pgc);
     return 0;
   }
 
   return 1;
 }
 
-static void ifoFree_PGC(pgc_t **pgc) {
-  if(pgc && *pgc && (--(*pgc)->ref_count) <= 0) {
-    ifoFree_PGC_COMMAND_TBL((*pgc)->command_tbl);
-    if((*pgc)->program_map)
-      free((*pgc)->program_map);
-    if((*pgc)->cell_playback)
-      free((*pgc)->cell_playback);
-    if((*pgc)->cell_position)
-      free((*pgc)->cell_position);
-    free(*pgc);
-  }
-  if (pgc) {
-    *pgc = NULL;
-  }
-}
 
-void ifoFree_FP_PGC(ifo_handle_t *ifofile) {
-  if(!ifofile)
+static void ifoFree_PGC(pgc_t *pgc) {
+
+  if(!pgc)
     return;
 
-  if(ifofile->first_play_pgc) {
-    ifoFree_PGC(&ifofile->first_play_pgc);
+  if(pgc->command_tbl) {
+    free(pgc->command_tbl->pre_cmds);
+    free(pgc->command_tbl->post_cmds);
+    free(pgc->command_tbl->cell_cmds);
+    free(pgc->command_tbl);
   }
-}
+  free(pgc->program_map);
+  free(pgc->cell_playback);
+  free(pgc->cell_position);
+  free(pgc);
 
+  pgc = NULL;
+
+}
 
 int ifoRead_TT_SRPT(ifo_handle_t *ifofile) {
   tt_srpt_t *tt_srpt;
@@ -1051,6 +1165,7 @@ int ifoRead_TT_SRPT(ifo_handle_t *ifofile) {
   if(!(DVDReadBytes(ifofile->file, tt_srpt, TT_SRPT_SIZE))) {
     fprintf(stderr, "libdvdread: Unable to read read TT_SRPT.\n");
     free(tt_srpt);
+    ifofile->tt_srpt = NULL;
     return 0;
   }
 
@@ -1062,12 +1177,13 @@ int ifoRead_TT_SRPT(ifo_handle_t *ifofile) {
   tt_srpt->title = malloc(info_length);
   if(!tt_srpt->title) {
     free(tt_srpt);
-    ifofile->tt_srpt = 0;
+    ifofile->tt_srpt = NULL;
     return 0;
   }
   if(!(DVDReadBytes(ifofile->file, tt_srpt->title, info_length))) {
     fprintf(stderr, "libdvdread: Unable to read read TT_SRPT.\n");
-    ifoFree_TT_SRPT(ifofile);
+    free(ifofile->tt_srpt->title);
+    free(ifofile->tt_srpt);
     return 0;
   }
 
@@ -1121,18 +1237,6 @@ int ifoRead_TT_SRPT(ifo_handle_t *ifofile) {
 }
 
 
-void ifoFree_TT_SRPT(ifo_handle_t *ifofile) {
-  if(!ifofile)
-    return;
-
-  if(ifofile->tt_srpt) {
-    free(ifofile->tt_srpt->title);
-    free(ifofile->tt_srpt);
-    ifofile->tt_srpt = 0;
-  }
-}
-
-
 int ifoRead_VTS_PTT_SRPT(ifo_handle_t *ifofile) {
   vts_ptt_srpt_t *vts_ptt_srpt = NULL;
   int info_length, i, j;
@@ -1231,8 +1335,10 @@ int ifoRead_VTS_PTT_SRPT(ifo_handle_t *ifofile) {
     vts_ptt_srpt->title[i].nr_of_ptts = n / 4;
     vts_ptt_srpt->title[i].ptt = malloc(n * sizeof(ptt_info_t));
     if(!vts_ptt_srpt->title[i].ptt) {
-      for(n = 0; n < i; n++)
+      for(n = 0; n < i; n++) {
         free(vts_ptt_srpt->title[n].ptt);
+	vts_ptt_srpt->title[n].ptt = NULL;
+      }
 
       goto fail;
     }
@@ -1276,29 +1382,14 @@ int ifoRead_VTS_PTT_SRPT(ifo_handle_t *ifofile)
{ 
 fail:
   free(data);
-  ifofile->vts_ptt_srpt = 0;
+  ifofile->vts_ptt_srpt = NULL;
   free(vts_ptt_srpt->title);
+  vts_ptt_srpt->title = NULL;
   free(vts_ptt_srpt);
   return 0;
 }
 
 
-void ifoFree_VTS_PTT_SRPT(ifo_handle_t *ifofile) {
-  if(!ifofile)
-    return;
-
-  if(ifofile->vts_ptt_srpt) {
-    int i;
-    for(i = 0; i < ifofile->vts_ptt_srpt->nr_of_srpts; i++)
-      free(ifofile->vts_ptt_srpt->title[i].ptt);
-    free(ifofile->vts_ptt_srpt->ttu_offset);
-    free(ifofile->vts_ptt_srpt->title);
-    free(ifofile->vts_ptt_srpt);
-    ifofile->vts_ptt_srpt = 0;
-  }
-}
-
-
 int ifoRead_PTL_MAIT(ifo_handle_t *ifofile) {
   ptl_mait_t *ptl_mait;
   int info_length;
@@ -1423,21 +1514,6 @@ int ifoRead_PTL_MAIT(ifo_handle_t *ifofile) {
   return 1;
 }
 
-void ifoFree_PTL_MAIT(ifo_handle_t *ifofile) {
-  if(!ifofile)
-    return;
-
-  if(ifofile->ptl_mait) {
-    unsigned int i;
-
-    for(i = 0; i < ifofile->ptl_mait->nr_of_countries; i++) {
-      free(ifofile->ptl_mait->countries[i].pf_ptl_mai);
-    }
-    free(ifofile->ptl_mait->countries);
-    free(ifofile->ptl_mait);
-    ifofile->ptl_mait = NULL;
-  }
-}
 
 int ifoRead_VTS_TMAPT(ifo_handle_t *ifofile) {
   vts_tmapt_t *vts_tmapt;
@@ -1557,23 +1633,6 @@ int ifoRead_VTS_TMAPT(ifo_handle_t *ifofile) {
   return 1;
 }
 
-void ifoFree_VTS_TMAPT(ifo_handle_t *ifofile) {
-  if(!ifofile)
-    return;
-
-  if(ifofile->vts_tmapt) {
-    unsigned int i;
-
-    for(i = 0; i < ifofile->vts_tmapt->nr_of_tmaps; i++)
-      if(ifofile->vts_tmapt->tmap[i].map_ent)
-        free(ifofile->vts_tmapt->tmap[i].map_ent);
-    free(ifofile->vts_tmapt->tmap);
-    free(ifofile->vts_tmapt->tmap_offset);
-    free(ifofile->vts_tmapt);
-    ifofile->vts_tmapt = NULL;
-  }
-}
-
 
 int ifoRead_TITLE_C_ADT(ifo_handle_t *ifofile) {
 
@@ -1593,7 +1652,7 @@ int ifoRead_TITLE_C_ADT(ifo_handle_t *ifofile) {
   if(!ifoRead_C_ADT_internal(ifofile, ifofile->vts_c_adt,
                              ifofile->vtsi_mat->vts_c_adt)) {
     free(ifofile->vts_c_adt);
-    ifofile->vts_c_adt = 0;
+    ifofile->vts_c_adt = NULL;
     return 0;
   }
 
@@ -1624,7 +1683,7 @@ int ifoRead_C_ADT(ifo_handle_t *ifofile) {
 
   if(!ifoRead_C_ADT_internal(ifofile, ifofile->menu_c_adt, sector)) {
     free(ifofile->menu_c_adt);
-    ifofile->menu_c_adt = 0;
+    ifofile->menu_c_adt = NULL;
     return 0;
   }
 
@@ -1670,6 +1729,7 @@ static int ifoRead_C_ADT_internal(ifo_handle_t
*ifofile, if(info_length &&
      !(DVDReadBytes(ifofile->file, c_adt->cell_adr_table,
info_length))) { free(c_adt->cell_adr_table);
+    c_adt->cell_adr_table = NULL;
     return 0;
   }
 
@@ -1689,30 +1749,6 @@ static int ifoRead_C_ADT_internal(ifo_handle_t
*ifofile, return 1;
 }
 
-
-static void ifoFree_C_ADT_internal(c_adt_t *c_adt) {
-  if(c_adt) {
-    free(c_adt->cell_adr_table);
-    free(c_adt);
-  }
-}
-
-void ifoFree_C_ADT(ifo_handle_t *ifofile) {
-  if(!ifofile)
-    return;
-
-  ifoFree_C_ADT_internal(ifofile->menu_c_adt);
-  ifofile->menu_c_adt = 0;
-}
-
-void ifoFree_TITLE_C_ADT(ifo_handle_t *ifofile) {
-  if(!ifofile)
-    return;
-
-  ifoFree_C_ADT_internal(ifofile->vts_c_adt);
-  ifofile->vts_c_adt = 0;
-}
-
 int ifoRead_TITLE_VOBU_ADMAP(ifo_handle_t *ifofile) {
   if(!ifofile)
     return 0;
@@ -1730,7 +1766,7 @@ int ifoRead_TITLE_VOBU_ADMAP(ifo_handle_t
*ifofile) { if(!ifoRead_VOBU_ADMAP_internal(ifofile,
ifofile->vts_vobu_admap, ifofile->vtsi_mat->vts_vobu_admap)) {
     free(ifofile->vts_vobu_admap);
-    ifofile->vts_vobu_admap = 0;
+    ifofile->vts_vobu_admap = NULL;
     return 0;
   }
 
@@ -1761,7 +1797,7 @@ int ifoRead_VOBU_ADMAP(ifo_handle_t *ifofile) {
 
   if(!ifoRead_VOBU_ADMAP_internal(ifofile, ifofile->menu_vobu_admap,
sector)) { free(ifofile->menu_vobu_admap);
-    ifofile->menu_vobu_admap = 0;
+    ifofile->menu_vobu_admap = NULL;
     return 0;
   }
 
@@ -1796,6 +1832,7 @@ static int
ifoRead_VOBU_ADMAP_internal(ifo_handle_t
*ifofile, !(DVDReadBytes(ifofile->file, vobu_admap->vobu_start_sectors,
info_length))) { free(vobu_admap->vobu_start_sectors);
+    vobu_admap->vobu_start_sectors = NULL;
     return 0;
   }
 
@@ -1806,29 +1843,6 @@ static int
ifoRead_VOBU_ADMAP_internal(ifo_handle_t *ifofile, }
 
 
-static void ifoFree_VOBU_ADMAP_internal(vobu_admap_t *vobu_admap) {
-  if(vobu_admap) {
-    free(vobu_admap->vobu_start_sectors);
-    free(vobu_admap);
-  }
-}
-
-void ifoFree_VOBU_ADMAP(ifo_handle_t *ifofile) {
-  if(!ifofile)
-    return;
-
-  ifoFree_VOBU_ADMAP_internal(ifofile->menu_vobu_admap);
-  ifofile->menu_vobu_admap = 0;
-}
-
-void ifoFree_TITLE_VOBU_ADMAP(ifo_handle_t *ifofile) {
-  if(!ifofile)
-    return;
-
-  ifoFree_VOBU_ADMAP_internal(ifofile->vts_vobu_admap);
-  ifofile->vts_vobu_admap = 0;
-}
-
 int ifoRead_PGCIT(ifo_handle_t *ifofile) {
 
   if(!ifofile)
@@ -1848,7 +1862,7 @@ int ifoRead_PGCIT(ifo_handle_t *ifofile) {
   if(!ifoRead_PGCIT_internal(ifofile, ifofile->vts_pgcit,
                              ifofile->vtsi_mat->vts_pgcit *
DVD_BLOCK_LEN)) { free(ifofile->vts_pgcit);
-    ifofile->vts_pgcit = 0;
+    ifofile->vts_pgcit = NULL;
     return 0;
   }
 
@@ -1924,7 +1938,7 @@ static int ifoRead_PGCIT_internal(ifo_handle_t
*ifofile, pgcit_t *pgcit, if(!pgcit->pgci_srp[i].pgc) {
       int j;
       for(j = 0; j < i; j++) {
-        ifoFree_PGC(&pgcit->pgci_srp[j].pgc);
+	ifoFree_PGC(pgcit->pgci_srp[j].pgc);
       }
       goto fail;
     }
@@ -1933,9 +1947,8 @@ static int ifoRead_PGCIT_internal(ifo_handle_t
*ifofile, pgcit_t *pgcit, offset + pgcit->pgci_srp[i].pgc_start_byte)) {
       int j;
       for(j = 0; j <= i; j++) {
-        ifoFree_PGC(&pgcit->pgci_srp[j].pgc);
+	ifoFree_PGC(pgcit->pgci_srp[j].pgc);
       }
-      free(pgcit->pgci_srp[i].pgc);
       goto fail;
     }
   }
@@ -1947,28 +1960,6 @@ fail:
   return 0;
 }
 
-static void ifoFree_PGCIT_internal(pgcit_t **pgcit) {
-  if(pgcit && *pgcit && (--(*pgcit)->ref_count <= 0)) {
-    int i;
-    for(i = 0; i < (*pgcit)->nr_of_pgci_srp; i++)
-    {
-      ifoFree_PGC(&(*pgcit)->pgci_srp[i].pgc);
-    }
-    free((*pgcit)->pgci_srp);
-    free(*pgcit);
-  }
-  if (pgcit)
-    *pgcit = NULL;
-}
-
-void ifoFree_PGCIT(ifo_handle_t *ifofile) {
-  if(!ifofile)
-    return;
-
-  if(ifofile->vts_pgcit) {
-    ifoFree_PGCIT_internal(&ifofile->vts_pgcit);
-  }
-}
 
 static int find_dup_lut(pgci_lu_t *lu, uint32_t start_byte, int count)
{ int i;
@@ -2009,13 +2000,13 @@ int ifoRead_PGCI_UT(ifo_handle_t *ifofile) {
 
   if(!DVDFileSeek_(ifofile->file, sector * DVD_BLOCK_LEN)) {
     free(ifofile->pgci_ut);
-    ifofile->pgci_ut = 0;
+    ifofile->pgci_ut = NULL;
     return 0;
   }
 
   if(!(DVDReadBytes(ifofile->file, ifofile->pgci_ut, PGCI_UT_SIZE))) {
     free(ifofile->pgci_ut);
-    ifofile->pgci_ut = 0;
+    ifofile->pgci_ut = NULL;
     return 0;
   }
 
@@ -2033,13 +2024,13 @@ int ifoRead_PGCI_UT(ifo_handle_t *ifofile) {
   data = malloc(info_length);
   if(!data) {
     free(pgci_ut);
-    ifofile->pgci_ut = 0;
+    ifofile->pgci_ut = NULL;
     return 0;
   }
   if(!(DVDReadBytes(ifofile->file, data, info_length))) {
     free(data);
     free(pgci_ut);
-    ifofile->pgci_ut = 0;
+    ifofile->pgci_ut = NULL;
     return 0;
   }
 
@@ -2047,7 +2038,7 @@ int ifoRead_PGCI_UT(ifo_handle_t *ifofile) {
   if(!pgci_ut->lu) {
     free(data);
     free(pgci_ut);
-    ifofile->pgci_ut = 0;
+    ifofile->pgci_ut = NULL;
     return 0;
   }
   ptr = data;
@@ -2083,11 +2074,16 @@ int ifoRead_PGCI_UT(ifo_handle_t *ifofile) {
     if(!pgci_ut->lu[i].pgcit) {
       unsigned int j;
       for(j = 0; j < i; j++) {
-        ifoFree_PGCIT_internal(&pgci_ut->lu[j].pgcit);
+        if(pgci_ut->lu[j].pgcit->pgci_srp) {
+          for(i = 0; i < pgci_ut->lu[j].pgcit->nr_of_pgci_srp; i++) {
+            ifoFree_PGC(pgci_ut->lu[j].pgcit->pgci_srp[i].pgc);
+          }
+        }
       }
       free(pgci_ut->lu);
+      pgci_ut->lu = NULL;
       free(pgci_ut);
-      ifofile->pgci_ut = 0;
+      ifofile->pgci_ut = NULL;
       return 0;
     }
     pgci_ut->lu[i].pgcit->ref_count = 1;
@@ -2096,11 +2092,16 @@ int ifoRead_PGCI_UT(ifo_handle_t *ifofile) {
                                + pgci_ut->lu[i].lang_start_byte)) {
       unsigned int j;
       for(j = 0; j <= i; j++) {
-        ifoFree_PGCIT_internal(&pgci_ut->lu[j].pgcit);
+        if(pgci_ut->lu[j].pgcit->pgci_srp) {
+          for(i = 0; i < pgci_ut->lu[j].pgcit->nr_of_pgci_srp; i++) {
+            ifoFree_PGC(pgci_ut->lu[j].pgcit->pgci_srp[i].pgc);
+          }
+        }
       }
       free(pgci_ut->lu);
+      pgci_ut->lu = NULL;
       free(pgci_ut);
-      ifofile->pgci_ut = 0;
+      ifofile->pgci_ut = NULL;
       return 0;
     }
     /* FIXME: Iterate and verify that all menus that should exists
accordingly @@ -2111,22 +2112,6 @@ int ifoRead_PGCI_UT(ifo_handle_t
*ifofile) { }
 
 
-void ifoFree_PGCI_UT(ifo_handle_t *ifofile) {
-  if(!ifofile)
-    return;
-
-  if(ifofile->pgci_ut) {
-    unsigned int i;
-
-    for(i = 0; i < ifofile->pgci_ut->nr_of_lus; i++) {
-      ifoFree_PGCIT_internal(&ifofile->pgci_ut->lu[i].pgcit);
-    }
-    free(ifofile->pgci_ut->lu);
-    free(ifofile->pgci_ut);
-    ifofile->pgci_ut = 0;
-  }
-}
-
 static int ifoRead_VTS_ATTRIBUTES(ifo_handle_t *ifofile,
                                   vts_attributes_t *vts_attributes,
                                   unsigned int offset) {
@@ -2206,7 +2191,7 @@ int ifoRead_VTS_ATRT(ifo_handle_t *ifofile) {
 
   if(!(DVDReadBytes(ifofile->file, vts_atrt, VTS_ATRT_SIZE))) {
     free(vts_atrt);
-    ifofile->vts_atrt = 0;
+    ifofile->vts_atrt = NULL;
     return 0;
   }
 
@@ -2223,7 +2208,7 @@ int ifoRead_VTS_ATRT(ifo_handle_t *ifofile) {
   data = malloc(info_length);
   if(!data) {
     free(vts_atrt);
-    ifofile->vts_atrt = 0;
+    ifofile->vts_atrt = NULL;
     return 0;
   }
 
@@ -2232,7 +2217,7 @@ int ifoRead_VTS_ATRT(ifo_handle_t *ifofile) {
   if(!(DVDReadBytes(ifofile->file, data, info_length))) {
     free(data);
     free(vts_atrt);
-    ifofile->vts_atrt = 0;
+    ifofile->vts_atrt = NULL;
     return 0;
   }
 
@@ -2246,7 +2231,7 @@ int ifoRead_VTS_ATRT(ifo_handle_t *ifofile) {
   if(!vts_atrt->vts) {
     free(data);
     free(vts_atrt);
-    ifofile->vts_atrt = 0;
+    ifofile->vts_atrt = NULL;
     return 0;
   }
   for(i = 0; i < vts_atrt->nr_of_vtss; i++) {
@@ -2255,7 +2240,7 @@ int ifoRead_VTS_ATRT(ifo_handle_t *ifofile) {
                                (sector * DVD_BLOCK_LEN) + offset)) {
       free(data);
       free(vts_atrt);
-      ifofile->vts_atrt = 0;
+      ifofile->vts_atrt = NULL;
       return 0;
     }
 
@@ -2268,19 +2253,6 @@ int ifoRead_VTS_ATRT(ifo_handle_t *ifofile) {
 }
 
 
-void ifoFree_VTS_ATRT(ifo_handle_t *ifofile) {
-  if(!ifofile)
-    return;
-
-  if(ifofile->vts_atrt) {
-    free(ifofile->vts_atrt->vts);
-    free(ifofile->vts_atrt->vts_atrt_offsets);
-    free(ifofile->vts_atrt);
-    ifofile->vts_atrt = 0;
-  }
-}
-
-
 int ifoRead_TXTDT_MGI(ifo_handle_t *ifofile) {
   txtdt_mgi_t *txtdt_mgi;
 
@@ -2307,7 +2279,7 @@ int ifoRead_TXTDT_MGI(ifo_handle_t *ifofile) {
   if(!(DVDReadBytes(ifofile->file, txtdt_mgi, TXTDT_MGI_SIZE))) {
     fprintf(stderr, "libdvdread: Unable to read TXTDT_MGI.\n");
     free(txtdt_mgi);
-    ifofile->txtdt_mgi = 0;
+    ifofile->txtdt_mgi = NULL;
     return 0;
   }
 
@@ -2315,12 +2287,23 @@ int ifoRead_TXTDT_MGI(ifo_handle_t *ifofile) {
   return 1;
 }
 
-void ifoFree_TXTDT_MGI(ifo_handle_t *ifofile) {
+void ifoFree_VTS_TMAPT(ifo_handle_t *ifofile) {
+
   if(!ifofile)
     return;
 
-  if(ifofile->txtdt_mgi) {
-    free(ifofile->txtdt_mgi);
-    ifofile->txtdt_mgi = 0;
+  if(ifofile->vts_tmapt) {
+    unsigned int i;
+
+    for(i = 0; i < ifofile->vts_tmapt->nr_of_tmaps; i++) {
+      if(ifofile->vts_tmapt->tmap[i].map_ent) {
+        free(ifofile->vts_tmapt->tmap[i].map_ent);
+      }
+    }
+    free(ifofile->vts_tmapt->tmap);
+    free(ifofile->vts_tmapt->tmap_offset);
+    free(ifofile->vts_tmapt);
+    ifofile->vts_tmapt = NULL;
   }
+
 }
-- 
2.0.4


More information about the DVDnav-discuss mailing list