diff --git a/libisofs/ecma119.c b/libisofs/ecma119.c index 0aa9ce5..c91f42a 100644 --- a/libisofs/ecma119.c +++ b/libisofs/ecma119.c @@ -49,18 +49,26 @@ void ecma119_image_free(Ecma119Image *t) { size_t i; - ecma119_node_free(t->root); - iso_image_unref(t->image); - iso_rbtree_destroy(t->files, iso_file_src_free); - iso_ring_buffer_free(t->buffer); + if (t == NULL) + return; + if (t->root != NULL) + ecma119_node_free(t->root); + if (t->image != NULL) + iso_image_unref(t->image); + if (t->files != NULL) + iso_rbtree_destroy(t->files, iso_file_src_free); + if (t->buffer != NULL) + iso_ring_buffer_free(t->buffer); for (i = 0; i < t->nwriters; ++i) { IsoImageWriter *writer = t->writers[i]; writer->free_data(writer); free(writer); } - free(t->input_charset); - free(t->output_charset); + if (t->input_charset != NULL) + free(t->input_charset); + if (t->output_charset != NULL) + free(t->output_charset); #ifdef Libisofs_with_checksumS if (t->checksum_ctx != NULL) { /* dispose checksum context */ @@ -71,7 +79,8 @@ void ecma119_image_free(Ecma119Image *t) free(t->checksum_buffer); #endif - free(t->writers); + if (t->writers != NULL) + free(t->writers); free(t); } @@ -891,6 +900,18 @@ void *write_function(void *arg) } } +#ifdef Libisofs_with_checksumS + + /* Transplant checksum buffer from Ecma119Image to IsoImage */ + iso_image_set_checksums(target->image, target->checksum_buffer, + target->checksum_range_start, + target->checksum_array_pos, + target->checksum_idx_counter + 2, 0); + target->checksum_buffer = NULL; + target->checksum_idx_counter = 0; + +#endif + iso_ring_buffer_writer_close(target->buffer, 0); pthread_exit(NULL); @@ -904,6 +925,13 @@ void *write_function(void *arg) "Image write error"); } iso_ring_buffer_writer_close(target->buffer, 1); + +#ifdef Libisofs_with_checksumS + + /* >>> ??? transplant checksum buffer from Ecma119Image to IsoImage */; + +#endif + pthread_exit(NULL); } @@ -1002,7 +1030,7 @@ int checksum_prepare_nodes(Ecma119Image *target, IsoNode *node, int flag) static int ecma119_image_new(IsoImage *src, IsoWriteOpts *opts, Ecma119Image **img) { - int ret, i, voldesc_size, nwriters; + int ret, i, voldesc_size, nwriters, image_checksums_mad = 0; Ecma119Image *target; int el_torito_writer_index = -1, file_src_writer_index= -1; @@ -1015,8 +1043,7 @@ int ecma119_image_new(IsoImage *src, IsoWriteOpts *opts, Ecma119Image **img) /* create the tree for file caching */ ret = iso_rbtree_new(iso_file_src_cmp, &(target->files)); if (ret < 0) { - free(target); - return ret; + goto target_cleanup; } target->image = src; @@ -1074,9 +1101,8 @@ int ecma119_image_new(IsoImage *src, IsoWriteOpts *opts, Ecma119Image **img) target->input_charset = strdup(iso_get_local_charset(0)); if (target->input_charset == NULL) { - iso_image_unref(src); - free(target); - return ISO_OUT_OF_MEM; + ret = ISO_OUT_OF_MEM; + goto target_cleanup; } if (opts->output_charset != NULL) { @@ -1085,9 +1111,8 @@ int ecma119_image_new(IsoImage *src, IsoWriteOpts *opts, Ecma119Image **img) target->output_charset = strdup(target->input_charset); } if (target->output_charset == NULL) { - iso_image_unref(src); - free(target); - return ISO_OUT_OF_MEM; + ret = ISO_OUT_OF_MEM; + goto target_cleanup; } #ifdef Libisofs_with_checksumS @@ -1137,13 +1162,16 @@ int ecma119_image_new(IsoImage *src, IsoWriteOpts *opts, Ecma119Image **img) if ((target->md5_file_checksums & 1) || target->md5_session_checksum) { nwriters++; + image_checksums_mad = 1; /* from here on the loaded checksums are + not consistent with isofs.cx any more. + */ ret = checksum_prepare_image(src, 0); if (ret < 0) - return ret; + goto target_cleanup; if (target->appendable) { ret = checksum_prepare_nodes(target, (IsoNode *) src->root, 0); if (ret < 0) - return ret; + goto target_cleanup; } target->checksum_idx_counter = 0; } @@ -1153,9 +1181,8 @@ int ecma119_image_new(IsoImage *src, IsoWriteOpts *opts, Ecma119Image **img) target->writers = malloc(nwriters * sizeof(void*)); if (target->writers == NULL) { - iso_image_unref(src); - free(target); - return ISO_OUT_OF_MEM; + ret = ISO_OUT_OF_MEM; + goto target_cleanup; } /* create writer for ECMA-119 structure */ @@ -1328,7 +1355,7 @@ int ecma119_image_new(IsoImage *src, IsoWriteOpts *opts, Ecma119Image **img) if (target->checksum_rlsb_tag_pos < 32) { ret = iso_md5_start(&(target->checksum_ctx)); if (ret < 0) - return ret; + goto target_cleanup; target->opts_overwrite = (char *) opts->overwrite; iso_md5_compute(target->checksum_ctx, target->opts_overwrite, target->checksum_rlsb_tag_pos * 2048); @@ -1360,8 +1387,13 @@ int ecma119_image_new(IsoImage *src, IsoWriteOpts *opts, Ecma119Image **img) iso_md5_end(&(target->checksum_ctx), target->image_md5); ret = iso_md5_start(&(target->checksum_ctx)); if (ret < 0) - return ret; + goto target_cleanup; } + /* Dispose old image checksum buffer. The one of target is supposed to + get attached at the end of write_function(). */ + iso_image_free_checksums(target->image, 0); + image_checksums_mad = 0; + #endif /* Libisofs_with_checksumS */ /* ensure the thread is created joinable */ @@ -1388,6 +1420,14 @@ int ecma119_image_new(IsoImage *src, IsoWriteOpts *opts, Ecma119Image **img) return ISO_SUCCESS; target_cleanup: ; + +#ifdef Libisofs_with_checksumS + + if(image_checksums_mad) /* No checksums is better than mad checksums */ + iso_image_free_checksums(target->image, 0); + +#endif /* Libisofs_with_checksumS */ + ecma119_image_free(target); return ret; } diff --git a/libisofs/image.c b/libisofs/image.c index 832fad1..475a7ee 100644 --- a/libisofs/image.c +++ b/libisofs/image.c @@ -134,10 +134,29 @@ void iso_image_unref(IsoImage *image) free(image->biblio_file_id); if (image->used_inodes != NULL) free(image->used_inodes); + iso_image_free_checksums(image, 0); free(image); } } + +int iso_image_free_checksums(IsoImage *image, int flag) +{ +#ifdef Libisofs_with_checksumS + + image->checksum_start_lba = 0; + image->checksum_end_lba = 0; + image->checksum_idx_count = 0; + if (image->checksum_array != NULL) + free(image->checksum_array); + image->checksum_array = NULL; + +#endif /* Libisofs_with_checksumS */ + + return 1; +} + + /** * Attach user defined data to the image. Use this if your application needs * to store addition info together with the IsoImage. If the image already @@ -559,3 +578,22 @@ int iso_image_get_session_md5(IsoImage *image, uint32_t *start_lba, #endif /* ! Libisofs_with_checksumS */ } + +int iso_image_set_checksums(IsoImage *image, char *checksum_array, + uint32_t start_lba, uint32_t end_lba, + uint32_t idx_count, int flag) +{ + +#ifdef Libisofs_with_checksumS + + iso_image_free_checksums(image, 0); + image->checksum_array = checksum_array; + image->checksum_start_lba = start_lba; + image->checksum_end_lba = end_lba; + image->checksum_idx_count = idx_count; + +#endif /* Libisofs_with_checksumS */ + + return 1; +} + diff --git a/libisofs/image.h b/libisofs/image.h index 253c6b0..58aa5b4 100644 --- a/libisofs/image.h +++ b/libisofs/image.h @@ -197,4 +197,18 @@ ino_t img_give_ino_number(IsoImage *image, int flag); */ int img_make_inos(IsoImage *image, IsoDir *dir, int flag); + +/* Free the checksum array of an image and reset its layout parameters +*/ +int iso_image_free_checksums(IsoImage *image, int flag); + + +/* Equip an ISO image with a new checksum array buffer (after isofs.ca and + isofs.cx have already been adjusted). +*/ +int iso_image_set_checksums(IsoImage *image, char *checksum_array, + uint32_t start_lba, uint32_t end_lba, + uint32_t idx_count, int flag); + + #endif /*LIBISO_IMAGE_H_*/