diff --git a/src/tools/imgtool/filtbas.cpp b/src/tools/imgtool/filtbas.cpp index 1e2e4730f1a..eac4b9d5173 100644 --- a/src/tools/imgtool/filtbas.cpp +++ b/src/tools/imgtool/filtbas.cpp @@ -73,7 +73,7 @@ static imgtoolerr_t basic_readfile(const basictokens *tokens, const char *fork, imgtool::stream &destf) { imgtoolerr_t err; - imgtool::stream *mem_stream; + imgtool::stream::ptr mem_stream; uint8_t line_header[4]; uint16_t line_number; //, address; uint8_t b, shift; @@ -84,16 +84,13 @@ static imgtoolerr_t basic_readfile(const basictokens *tokens, /* open a memory stream */ mem_stream = imgtool::stream::open_mem(nullptr, 0); - if (mem_stream == nullptr) - { - err = IMGTOOLERR_OUTOFMEMORY; - goto done; - } + if (!mem_stream) + return IMGTOOLERR_OUTOFMEMORY; /* read actual file */ err = partition.read_file(filename, fork, *mem_stream, nullptr); if (err) - goto done; + return err; /* skip first few bytes */ mem_stream->seek(tokens->skip_bytes, SEEK_SET); @@ -160,9 +157,6 @@ static imgtoolerr_t basic_readfile(const basictokens *tokens, destf.puts(EOLN); } -done: - if (mem_stream != nullptr) - delete mem_stream; return err; } @@ -177,8 +171,7 @@ static imgtoolerr_t basic_writefile(const basictokens *tokens, imgtool::partition &partition, const char *filename, const char *fork, imgtool::stream &sourcef, util::option_resolution *opts) { - imgtoolerr_t err; - imgtool::stream *mem_stream; + imgtool::stream::ptr mem_stream; char buf[1024]; int eof = false; uint32_t len; @@ -194,11 +187,8 @@ static imgtoolerr_t basic_writefile(const basictokens *tokens, /* open a memory stream */ mem_stream = imgtool::stream::open_mem(nullptr, 0); - if (mem_stream == nullptr) - { - err = IMGTOOLERR_OUTOFMEMORY; - goto done; - } + if (!mem_stream) + return IMGTOOLERR_OUTOFMEMORY; /* skip first few bytes */ mem_stream->fill(0x00, tokens->skip_bytes); @@ -343,14 +333,7 @@ static imgtoolerr_t basic_writefile(const basictokens *tokens, } /* write actual file */ - err = partition.write_file(filename, fork, *mem_stream, opts, nullptr); - if (err) - goto done; - -done: - if (mem_stream != nullptr) - delete mem_stream; - return err; + return partition.write_file(filename, fork, *mem_stream, opts, nullptr); } diff --git a/src/tools/imgtool/filteoln.cpp b/src/tools/imgtool/filteoln.cpp index a4c3a0506a9..bb589e8fafe 100644 --- a/src/tools/imgtool/filteoln.cpp +++ b/src/tools/imgtool/filteoln.cpp @@ -56,28 +56,18 @@ static imgtoolerr_t convert_stream_eolns(imgtool::stream &source, imgtool::strea static imgtoolerr_t ascii_readfile(imgtool::partition &partition, const char *filename, const char *fork, imgtool::stream &destf) { imgtoolerr_t err; - imgtool::stream *mem_stream; + imgtool::stream::ptr mem_stream; mem_stream = imgtool::stream::open_mem(nullptr, 0); if (!mem_stream) - { - err = IMGTOOLERR_OUTOFMEMORY; - goto done; - } + return IMGTOOLERR_OUTOFMEMORY; err = partition.read_file(filename, fork, *mem_stream, nullptr); if (err) - goto done; + return err; mem_stream->seek(SEEK_SET, 0); - err = convert_stream_eolns(*mem_stream, destf, EOLN); - if (err) - goto done; - -done: - if (mem_stream) - delete mem_stream; - return err; + return convert_stream_eolns(*mem_stream, destf, EOLN); } @@ -85,32 +75,22 @@ done: static imgtoolerr_t ascii_writefile(imgtool::partition &partition, const char *filename, const char *fork, imgtool::stream &sourcef, util::option_resolution *opts) { imgtoolerr_t err; - imgtool::stream *mem_stream = nullptr; + imgtool::stream::ptr mem_stream; const char *eoln; /* create a stream */ mem_stream = imgtool::stream::open_mem(nullptr, 0); if (!mem_stream) - { - err = IMGTOOLERR_OUTOFMEMORY; - goto done; - } + return IMGTOOLERR_OUTOFMEMORY; eoln = partition.get_info_string(IMGTOOLINFO_STR_EOLN); err = convert_stream_eolns(sourcef, *mem_stream, eoln); if (err) - goto done; + return err; mem_stream->seek(SEEK_SET, 0); - err = partition.write_file(filename, fork, *mem_stream, opts, nullptr); - if (err) - goto done; - -done: - if (mem_stream) - delete mem_stream; - return err; + return partition.write_file(filename, fork, *mem_stream, opts, nullptr); } diff --git a/src/tools/imgtool/imgtool.cpp b/src/tools/imgtool/imgtool.cpp index d82a1f7411a..d945ea3f82f 100644 --- a/src/tools/imgtool/imgtool.cpp +++ b/src/tools/imgtool/imgtool.cpp @@ -914,7 +914,7 @@ imgtoolerr_t imgtool::image::internal_open(const imgtool_module *module, const c } // open the stream - stream = imgtool::stream::ptr(imgtool::stream::open(fname, read_or_write)); + stream = imgtool::stream::open(fname, read_or_write); if (!stream) { err = (imgtoolerr_t)(IMGTOOLERR_FILENOTFOUND | IMGTOOLERR_SRC_IMAGEFILE); @@ -1871,7 +1871,7 @@ imgtoolerr_t imgtool::partition::get_file(const char *filename, const char *fork const char *dest, filter_getinfoproc filter) { imgtoolerr_t err; - imgtool::stream *f; + imgtool::stream::ptr f; char *new_fname = nullptr; char *alloc_dest = nullptr; const char *filter_extension = nullptr; @@ -1919,8 +1919,6 @@ imgtoolerr_t imgtool::partition::get_file(const char *filename, const char *fork goto done; done: - if (f != nullptr) - delete f; if (alloc_dest != nullptr) free(alloc_dest); if (new_fname != nullptr) @@ -1938,7 +1936,7 @@ imgtoolerr_t imgtool::partition::put_file(const char *newfname, const char *fork const char *source, util::option_resolution *opts, filter_getinfoproc filter) { imgtoolerr_t err; - imgtool::stream *f = nullptr; + imgtool::stream::ptr f; imgtool_charset charset; char *alloc_newfname = nullptr; std::string basename; @@ -1971,8 +1969,6 @@ imgtoolerr_t imgtool::partition::put_file(const char *newfname, const char *fork done: /* clean up */ - if (f != nullptr) - delete f; if (alloc_newfname != nullptr) osd_free(alloc_newfname); return err; diff --git a/src/tools/imgtool/main.cpp b/src/tools/imgtool/main.cpp index 1acb37a8527..f1087798c0d 100644 --- a/src/tools/imgtool/main.cpp +++ b/src/tools/imgtool/main.cpp @@ -588,7 +588,7 @@ static int cmd_readsector(const struct command *c, int argc, char *argv[]) { imgtoolerr_t err; std::unique_ptr img; - imgtool::stream *stream = nullptr; + imgtool::stream::ptr stream; std::vector buffer; uint32_t track, head, sector; @@ -615,8 +615,6 @@ static int cmd_readsector(const struct command *c, int argc, char *argv[]) stream->write(&buffer[0], buffer.size()); done: - if (stream) - delete stream; if (err) reporterror(err, c, argv[0], argv[1], nullptr, nullptr, nullptr); return err ? -1 : 0; @@ -628,7 +626,7 @@ static int cmd_writesector(const struct command *c, int argc, char *argv[]) { imgtoolerr_t err; std::unique_ptr img; - imgtool::stream *stream = nullptr; + imgtool::stream::ptr stream; std::vector buffer; uint32_t size, track, head, sector; @@ -659,8 +657,6 @@ static int cmd_writesector(const struct command *c, int argc, char *argv[]) goto done; done: - if (stream) - delete stream; if (err) reporterror(err, c, argv[0], argv[1], nullptr, nullptr, nullptr); return err ? -1 : 0; diff --git a/src/tools/imgtool/modules/hp9845_tape.cpp b/src/tools/imgtool/modules/hp9845_tape.cpp index c8e450cb80a..f0fed452608 100644 --- a/src/tools/imgtool/modules/hp9845_tape.cpp +++ b/src/tools/imgtool/modules/hp9845_tape.cpp @@ -1399,20 +1399,17 @@ static bool dump_string(imgtool::stream &inp, imgtool::stream &out , unsigned le static imgtoolerr_t hp9845data_read_file(imgtool::partition &partition, const char *filename, const char *fork, imgtool::stream &destf) { - imgtool::stream *inp_data; + imgtool::stream::ptr inp_data; imgtoolerr_t res; uint8_t tmp[ 2 ]; inp_data = imgtool::stream::open_mem(NULL , 0); - if (inp_data == nullptr) { + if (!inp_data) return IMGTOOLERR_OUTOFMEMORY; - } res = hp9845_tape_read_file(partition , filename , fork , *inp_data); - if (res != IMGTOOLERR_SUCCESS) { - delete inp_data; + if (res != IMGTOOLERR_SUCCESS) return res; - } inp_data->seek(0, SEEK_SET); @@ -1540,12 +1537,11 @@ static bool split_string_n_dump(const char *s , imgtool::stream &dest) static imgtoolerr_t hp9845data_write_file(imgtool::partition &partition, const char *filename, const char *fork, imgtool::stream &sourcef, util::option_resolution *opts) { - imgtool::stream *out_data; + imgtool::stream::ptr out_data; out_data = imgtool::stream::open_mem(NULL , 0); - if (out_data == nullptr) { + if (!out_data) return IMGTOOLERR_OUTOFMEMORY; - } while (1) { char line[ 256 ]; @@ -1594,8 +1590,6 @@ static imgtoolerr_t hp9845data_write_file(imgtool::partition &partition, const c imgtoolerr_t res = hp9845_tape_write_file(partition, filename, fork, *out_data, opts); - delete out_data; - return res; } diff --git a/src/tools/imgtool/modules/mac.cpp b/src/tools/imgtool/modules/mac.cpp index d6c362123a8..36295ada397 100644 --- a/src/tools/imgtool/modules/mac.cpp +++ b/src/tools/imgtool/modules/mac.cpp @@ -6182,7 +6182,7 @@ static imgtoolerr_t mac_image_geticoninfo(imgtool::partition &partition, const c imgtoolerr_t err; imgtool_attribute attr_values[3]; uint32_t type_code, creator_code, finder_flags; - imgtool::stream *stream = NULL; + imgtool::stream::ptr stream; const void *resource_fork; uint64_t resource_fork_length; const void *bundle; @@ -6198,7 +6198,7 @@ static imgtoolerr_t mac_image_geticoninfo(imgtool::partition &partition, const c /* first retrieve type and creator code */ err = mac_image_getattrs(partition, path, attrs, attr_values); if (err) - goto done; + return err; type_code = (uint32_t) attr_values[0].i; creator_code = (uint32_t) attr_values[1].i; finder_flags = (uint32_t) attr_values[2].i; @@ -6210,15 +6210,12 @@ static imgtoolerr_t mac_image_geticoninfo(imgtool::partition &partition, const c stream = imgtool::stream::open_mem(NULL, 0); if (!stream) - { - err = IMGTOOLERR_SUCCESS; - goto done; - } + return IMGTOOLERR_SUCCESS; /* read in the resource fork */ err = mac_image_readfile(partition, path, "RESOURCE_FORK", *stream); if (err) - goto done; + return err; resource_fork = stream->getptr(); resource_fork_length = stream->size(); @@ -6226,7 +6223,7 @@ static imgtoolerr_t mac_image_geticoninfo(imgtool::partition &partition, const c bundle = mac_walk_resources(resource_fork, resource_fork_length, /* BNDL */ 0x424E444C, bundle_discriminator, &creator_code, NULL, &bundle_length); if (!bundle) - goto done; + return err; /* find the FREF and the icon family */ pos = 8; @@ -6250,7 +6247,7 @@ static imgtoolerr_t mac_image_geticoninfo(imgtool::partition &partition, const c pos += 6 + this_bundleentry_length * 4; } if (!fref_pos || !icn_pos) - goto done; + return err; /* look up the FREF */ for (i = 0; i < fref_bundleentry_length; i++) @@ -6267,7 +6264,7 @@ static imgtoolerr_t mac_image_geticoninfo(imgtool::partition &partition, const c } } if (i >= fref_bundleentry_length) - goto done; + return err; /* now look up the icon family */ resource_id = 0; @@ -6280,7 +6277,7 @@ static imgtoolerr_t mac_image_geticoninfo(imgtool::partition &partition, const c } } if (i >= icn_bundleentry_length) - goto done; + return err; /* fetch 32x32 icons (ICN#, icl4, icl8) */ if (load_icon((uint32_t *) iconinfo->icon32x32, resource_fork, resource_fork_length, @@ -6306,9 +6303,6 @@ static imgtoolerr_t mac_image_geticoninfo(imgtool::partition &partition, const c /* ics8 */ 0x69637338, resource_id, 32, 32, 8, mac_palette_8bpp, false); } -done: - if (stream) - delete stream; return err; } diff --git a/src/tools/imgtool/modules/macbin.cpp b/src/tools/imgtool/modules/macbin.cpp index 32f5af0e5de..604b16f6196 100644 --- a/src/tools/imgtool/modules/macbin.cpp +++ b/src/tools/imgtool/modules/macbin.cpp @@ -184,18 +184,15 @@ static imgtoolerr_t macbinary_readfile(imgtool::partition &partition, const char static imgtoolerr_t write_fork(imgtool::partition &partition, const char *filename, const char *fork, imgtool::stream &sourcef, uint64_t pos, uint64_t fork_len, util::option_resolution *opts) { - imgtoolerr_t err = IMGTOOLERR_SUCCESS; - imgtool::stream *mem_stream = NULL; + imgtoolerr_t err; + imgtool::stream::ptr mem_stream; size_t len; if (fork_len > 0) { - mem_stream = imgtool::stream::open_mem(NULL, 0); + mem_stream = imgtool::stream::open_mem(nullptr, 0); if (!mem_stream) - { - err = IMGTOOLERR_OUTOFMEMORY; - goto done; - } + return IMGTOOLERR_OUTOFMEMORY; sourcef.seek(pos, SEEK_SET); len = imgtool::stream::transfer(*mem_stream, sourcef, fork_len); @@ -205,13 +202,10 @@ static imgtoolerr_t write_fork(imgtool::partition &partition, const char *filena mem_stream->seek(0, SEEK_SET); err = partition.write_file(filename, fork, *mem_stream, opts, NULL); if (err) - goto done; + return err; } -done: - if (mem_stream) - delete mem_stream; - return err; + return IMGTOOLERR_SUCCESS; } diff --git a/src/tools/imgtool/modules/thomson.cpp b/src/tools/imgtool/modules/thomson.cpp index 4d4aba6d940..d7adce9dbb9 100644 --- a/src/tools/imgtool/modules/thomson.cpp +++ b/src/tools/imgtool/modules/thomson.cpp @@ -1318,16 +1318,14 @@ static imgtoolerr_t thomcrypt_read_file(imgtool::partition &part, const char *fork, imgtool::stream &dst) { uint8_t buf[3]; - imgtool::stream *org = imgtool::stream::open_mem( NULL, 0 ); + imgtool::stream::ptr org = imgtool::stream::open_mem(nullptr, 0); imgtoolerr_t err; if ( !org ) return IMGTOOLERR_OUTOFMEMORY; /* read file */ err = thom_read_file( part, name, fork, *org ); - if ( err ) { - delete org; - return err; - } + if (err) + return err; org->seek(0, SEEK_SET); if ( org->read(buf, 3 ) < 3 || buf[0] != 0xfe ) @@ -1344,7 +1342,6 @@ static imgtoolerr_t thomcrypt_read_file(imgtool::partition &part, thom_decrypt( dst, *org ); } - delete org; return IMGTOOLERR_SUCCESS; } @@ -1364,16 +1361,13 @@ static imgtoolerr_t thomcrypt_write_file(imgtool::partition &part, else { /* regular file */ - imgtool::stream *dst = imgtool::stream::open_mem( NULL, 0 ); - imgtoolerr_t err; + imgtool::stream::ptr dst = imgtool::stream::open_mem(nullptr, 0); if ( !dst ) return IMGTOOLERR_OUTOFMEMORY; dst->putc( '\xfe' ); dst->write(buf+1, 2); thom_encrypt( *dst, src ); dst->seek(0, SEEK_SET); - err = thom_write_file( part, name, fork, *dst, opts ); - delete dst; - return err; + return thom_write_file( part, name, fork, *dst, opts ); } } @@ -1402,7 +1396,7 @@ static imgtoolerr_t thom_basic_read_file(imgtool::partition &part, imgtool::stream &dst, const char *const table[2][128]) { - imgtool::stream *org = imgtool::stream::open_mem( NULL, 0 ); + imgtool::stream::ptr org = imgtool::stream::open_mem(nullptr, 0); imgtoolerr_t err; uint8_t buf[4]; int i; @@ -1411,10 +1405,8 @@ static imgtoolerr_t thom_basic_read_file(imgtool::partition &part, err = thomcrypt_read_file( part, name, fork, *org ); if (err) - { - delete org; return err; - } + org->seek(3, SEEK_SET); /* skip header */ while ( 1 ) @@ -1469,7 +1461,6 @@ static imgtoolerr_t thom_basic_read_file(imgtool::partition &part, } end: - delete org; return IMGTOOLERR_SUCCESS; } diff --git a/src/tools/imgtool/stream.cpp b/src/tools/imgtool/stream.cpp index 89dd3ef61fd..d5d0ee120b5 100644 --- a/src/tools/imgtool/stream.cpp +++ b/src/tools/imgtool/stream.cpp @@ -96,15 +96,15 @@ imgtool::stream::~stream() // open_zip //------------------------------------------------- -imgtool::stream *imgtool::stream::open_zip(const char *zipname, const char *subname, int read_or_write) +imgtool::stream::ptr imgtool::stream::open_zip(const char *zipname, const char *subname, int read_or_write) { if (read_or_write) - return nullptr; + return imgtool::stream::ptr(); /* check to see if the file exists */ FILE *f = fopen(zipname, "r"); if (!f) - return nullptr; + return imgtool::stream::ptr(); fclose(f); imgtool::stream::ptr imgfile(new imgtool::stream(true)); @@ -114,23 +114,23 @@ imgtool::stream *imgtool::stream::open_zip(const char *zipname, const char *subn util::archive_file::ptr z; util::archive_file::open_zip(zipname, z); if (!z) - return nullptr; + return imgtool::stream::ptr(); int zipent = z->first_file(); while ((zipent >= 0) && subname && strcmp(subname, z->current_name().c_str())) zipent = z->next_file(); if (zipent < 0) - return nullptr; + return imgtool::stream::ptr(); imgfile->filesize = z->current_uncompressed_length(); imgfile->buffer = reinterpret_cast(malloc(z->current_uncompressed_length())); if (!imgfile->buffer) - return nullptr; + return imgtool::stream::ptr(); if (z->decompress(imgfile->buffer, z->current_uncompressed_length()) != util::archive_file::error::NONE) - return nullptr; + return imgtool::stream::ptr(); - return imgfile.release(); + return imgfile; } @@ -139,7 +139,7 @@ imgtool::stream *imgtool::stream::open_zip(const char *zipname, const char *subn // open //------------------------------------------------- -imgtool::stream *imgtool::stream::open(const char *fname, int read_or_write) +imgtool::stream::ptr imgtool::stream::open(const char *fname, int read_or_write) { static const uint32_t write_modes[] = { @@ -148,7 +148,7 @@ imgtool::stream *imgtool::stream::open(const char *fname, int read_or_write) OPEN_FLAG_READ | OPEN_FLAG_WRITE, OPEN_FLAG_READ | OPEN_FLAG_WRITE | OPEN_FLAG_CREATE }; - imgtool::stream *s = nullptr; + imgtool::stream::ptr s; char c; /* maybe we are just a ZIP? */ @@ -184,14 +184,14 @@ imgtool::stream *imgtool::stream::open(const char *fname, int read_or_write) } /* ah well, it was worth a shot */ - return nullptr; + return imgtool::stream::ptr(); } imgtool::stream::ptr imgfile(new imgtool::stream(read_or_write ? false : true, std::move(f))); /* Normal file */ imgfile->name = fname; - return imgfile.release(); + return imgfile; } @@ -199,13 +199,13 @@ imgtool::stream *imgtool::stream::open(const char *fname, int read_or_write) // open_write_stream //------------------------------------------------- -imgtool::stream *imgtool::stream::open_write_stream(int size) +imgtool::stream::ptr imgtool::stream::open_write_stream(int size) { imgtool::stream::ptr imgfile(new imgtool::stream(false, size)); if (!imgfile->buffer) - return nullptr; + return imgtool::stream::ptr(); - return imgfile.release(); + return imgfile; } @@ -213,11 +213,11 @@ imgtool::stream *imgtool::stream::open_write_stream(int size) // open_mem //------------------------------------------------- -imgtool::stream *imgtool::stream::open_mem(void *buf, size_t sz) +imgtool::stream::ptr imgtool::stream::open_mem(void *buf, size_t sz) { imgtool::stream::ptr imgfile(new imgtool::stream(false, sz, buf)); - return imgfile.release(); + return imgfile; } @@ -461,14 +461,13 @@ int imgtool::stream::crc(unsigned long *result) int imgtool::stream::file_crc(const char *fname, unsigned long *result) { int err; - imgtool::stream *f; + imgtool::stream::ptr f; f = imgtool::stream::open(fname, OSD_FOPEN_READ); if (!f) return IMGTOOLERR_FILENOTFOUND; err = f->crc(result); - delete f; return err; } diff --git a/src/tools/imgtool/stream.h b/src/tools/imgtool/stream.h index 988c4ce4258..9a99d96fcbc 100644 --- a/src/tools/imgtool/stream.h +++ b/src/tools/imgtool/stream.h @@ -23,9 +23,9 @@ namespace imgtool ~stream(); - static imgtool::stream *open(const char *fname, int read_or_write); /* similar params to mame_fopen */ - static imgtool::stream *open_write_stream(int filesize); - static imgtool::stream *open_mem(void *buf, size_t sz); + static imgtool::stream::ptr open(const char *fname, int read_or_write); /* similar params to mame_fopen */ + static imgtool::stream::ptr open_write_stream(int filesize); + static imgtool::stream::ptr open_mem(void *buf, size_t sz); util::core_file *core_file(); uint32_t read(void *buf, uint32_t sz); @@ -75,7 +75,7 @@ namespace imgtool stream(bool wp, std::size_t size, void *buf); // private methods - static stream *open_zip(const char *zipname, const char *subname, int read_or_write); + static stream::ptr open_zip(const char *zipname, const char *subname, int read_or_write); }; }