[Imgtool] Changed imgtool::stream::open*() to return imgtool::stream::ptr

Updated quite a bit of client code that manually owned these pointers
This commit is contained in:
Nathan Woods 2016-10-22 09:48:41 -04:00
parent ab9fd24fbb
commit d167f6a5e1
10 changed files with 69 additions and 142 deletions

View File

@ -73,7 +73,7 @@ static imgtoolerr_t basic_readfile(const basictokens *tokens,
const char *fork, imgtool::stream &destf) const char *fork, imgtool::stream &destf)
{ {
imgtoolerr_t err; imgtoolerr_t err;
imgtool::stream *mem_stream; imgtool::stream::ptr mem_stream;
uint8_t line_header[4]; uint8_t line_header[4];
uint16_t line_number; //, address; uint16_t line_number; //, address;
uint8_t b, shift; uint8_t b, shift;
@ -84,16 +84,13 @@ static imgtoolerr_t basic_readfile(const basictokens *tokens,
/* open a memory stream */ /* open a memory stream */
mem_stream = imgtool::stream::open_mem(nullptr, 0); mem_stream = imgtool::stream::open_mem(nullptr, 0);
if (mem_stream == nullptr) if (!mem_stream)
{ return IMGTOOLERR_OUTOFMEMORY;
err = IMGTOOLERR_OUTOFMEMORY;
goto done;
}
/* read actual file */ /* read actual file */
err = partition.read_file(filename, fork, *mem_stream, nullptr); err = partition.read_file(filename, fork, *mem_stream, nullptr);
if (err) if (err)
goto done; return err;
/* skip first few bytes */ /* skip first few bytes */
mem_stream->seek(tokens->skip_bytes, SEEK_SET); mem_stream->seek(tokens->skip_bytes, SEEK_SET);
@ -160,9 +157,6 @@ static imgtoolerr_t basic_readfile(const basictokens *tokens,
destf.puts(EOLN); destf.puts(EOLN);
} }
done:
if (mem_stream != nullptr)
delete mem_stream;
return err; return err;
} }
@ -177,8 +171,7 @@ static imgtoolerr_t basic_writefile(const basictokens *tokens,
imgtool::partition &partition, const char *filename, imgtool::partition &partition, const char *filename,
const char *fork, imgtool::stream &sourcef, util::option_resolution *opts) const char *fork, imgtool::stream &sourcef, util::option_resolution *opts)
{ {
imgtoolerr_t err; imgtool::stream::ptr mem_stream;
imgtool::stream *mem_stream;
char buf[1024]; char buf[1024];
int eof = FALSE; int eof = FALSE;
uint32_t len; uint32_t len;
@ -194,11 +187,8 @@ static imgtoolerr_t basic_writefile(const basictokens *tokens,
/* open a memory stream */ /* open a memory stream */
mem_stream = imgtool::stream::open_mem(nullptr, 0); mem_stream = imgtool::stream::open_mem(nullptr, 0);
if (mem_stream == nullptr) if (!mem_stream)
{ return IMGTOOLERR_OUTOFMEMORY;
err = IMGTOOLERR_OUTOFMEMORY;
goto done;
}
/* skip first few bytes */ /* skip first few bytes */
mem_stream->fill(0x00, tokens->skip_bytes); mem_stream->fill(0x00, tokens->skip_bytes);
@ -343,14 +333,7 @@ static imgtoolerr_t basic_writefile(const basictokens *tokens,
} }
/* write actual file */ /* write actual file */
err = partition.write_file(filename, fork, *mem_stream, opts, nullptr); return partition.write_file(filename, fork, *mem_stream, opts, nullptr);
if (err)
goto done;
done:
if (mem_stream != nullptr)
delete mem_stream;
return err;
} }

View File

@ -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) static imgtoolerr_t ascii_readfile(imgtool::partition &partition, const char *filename, const char *fork, imgtool::stream &destf)
{ {
imgtoolerr_t err; imgtoolerr_t err;
imgtool::stream *mem_stream; imgtool::stream::ptr mem_stream;
mem_stream = imgtool::stream::open_mem(nullptr, 0); mem_stream = imgtool::stream::open_mem(nullptr, 0);
if (!mem_stream) if (!mem_stream)
{ return IMGTOOLERR_OUTOFMEMORY;
err = IMGTOOLERR_OUTOFMEMORY;
goto done;
}
err = partition.read_file(filename, fork, *mem_stream, nullptr); err = partition.read_file(filename, fork, *mem_stream, nullptr);
if (err) if (err)
goto done; return err;
mem_stream->seek(SEEK_SET, 0); mem_stream->seek(SEEK_SET, 0);
err = convert_stream_eolns(*mem_stream, destf, EOLN); return convert_stream_eolns(*mem_stream, destf, EOLN);
if (err)
goto done;
done:
if (mem_stream)
delete mem_stream;
return err;
} }
@ -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) static imgtoolerr_t ascii_writefile(imgtool::partition &partition, const char *filename, const char *fork, imgtool::stream &sourcef, util::option_resolution *opts)
{ {
imgtoolerr_t err; imgtoolerr_t err;
imgtool::stream *mem_stream = nullptr; imgtool::stream::ptr mem_stream;
const char *eoln; const char *eoln;
/* create a stream */ /* create a stream */
mem_stream = imgtool::stream::open_mem(nullptr, 0); mem_stream = imgtool::stream::open_mem(nullptr, 0);
if (!mem_stream) if (!mem_stream)
{ return IMGTOOLERR_OUTOFMEMORY;
err = IMGTOOLERR_OUTOFMEMORY;
goto done;
}
eoln = partition.get_info_string(IMGTOOLINFO_STR_EOLN); eoln = partition.get_info_string(IMGTOOLINFO_STR_EOLN);
err = convert_stream_eolns(sourcef, *mem_stream, eoln); err = convert_stream_eolns(sourcef, *mem_stream, eoln);
if (err) if (err)
goto done; return err;
mem_stream->seek(SEEK_SET, 0); mem_stream->seek(SEEK_SET, 0);
err = partition.write_file(filename, fork, *mem_stream, opts, nullptr); return partition.write_file(filename, fork, *mem_stream, opts, nullptr);
if (err)
goto done;
done:
if (mem_stream)
delete mem_stream;
return err;
} }

View File

@ -914,7 +914,7 @@ imgtoolerr_t imgtool::image::internal_open(const imgtool_module *module, const c
} }
// open the stream // open the stream
stream = imgtool::stream::ptr(imgtool::stream::open(fname, read_or_write)); stream = imgtool::stream::open(fname, read_or_write);
if (!stream) if (!stream)
{ {
err = (imgtoolerr_t)(IMGTOOLERR_FILENOTFOUND | IMGTOOLERR_SRC_IMAGEFILE); 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) const char *dest, filter_getinfoproc filter)
{ {
imgtoolerr_t err; imgtoolerr_t err;
imgtool::stream *f; imgtool::stream::ptr f;
char *new_fname = nullptr; char *new_fname = nullptr;
char *alloc_dest = nullptr; char *alloc_dest = nullptr;
const char *filter_extension = nullptr; const char *filter_extension = nullptr;
@ -1919,8 +1919,6 @@ imgtoolerr_t imgtool::partition::get_file(const char *filename, const char *fork
goto done; goto done;
done: done:
if (f != nullptr)
delete f;
if (alloc_dest != nullptr) if (alloc_dest != nullptr)
free(alloc_dest); free(alloc_dest);
if (new_fname != nullptr) 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) const char *source, util::option_resolution *opts, filter_getinfoproc filter)
{ {
imgtoolerr_t err; imgtoolerr_t err;
imgtool::stream *f = nullptr; imgtool::stream::ptr f;
imgtool_charset charset; imgtool_charset charset;
char *alloc_newfname = nullptr; char *alloc_newfname = nullptr;
std::string basename; std::string basename;
@ -1971,8 +1969,6 @@ imgtoolerr_t imgtool::partition::put_file(const char *newfname, const char *fork
done: done:
/* clean up */ /* clean up */
if (f != nullptr)
delete f;
if (alloc_newfname != nullptr) if (alloc_newfname != nullptr)
osd_free(alloc_newfname); osd_free(alloc_newfname);
return err; return err;

View File

@ -588,7 +588,7 @@ static int cmd_readsector(const struct command *c, int argc, char *argv[])
{ {
imgtoolerr_t err; imgtoolerr_t err;
std::unique_ptr<imgtool::image> img; std::unique_ptr<imgtool::image> img;
imgtool::stream *stream = nullptr; imgtool::stream::ptr stream;
std::vector<uint8_t> buffer; std::vector<uint8_t> buffer;
uint32_t track, head, sector; 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()); stream->write(&buffer[0], buffer.size());
done: done:
if (stream)
delete stream;
if (err) if (err)
reporterror(err, c, argv[0], argv[1], nullptr, nullptr, nullptr); reporterror(err, c, argv[0], argv[1], nullptr, nullptr, nullptr);
return err ? -1 : 0; return err ? -1 : 0;
@ -628,7 +626,7 @@ static int cmd_writesector(const struct command *c, int argc, char *argv[])
{ {
imgtoolerr_t err; imgtoolerr_t err;
std::unique_ptr<imgtool::image> img; std::unique_ptr<imgtool::image> img;
imgtool::stream *stream = nullptr; imgtool::stream::ptr stream;
std::vector<uint8_t> buffer; std::vector<uint8_t> buffer;
uint32_t size, track, head, sector; uint32_t size, track, head, sector;
@ -659,8 +657,6 @@ static int cmd_writesector(const struct command *c, int argc, char *argv[])
goto done; goto done;
done: done:
if (stream)
delete stream;
if (err) if (err)
reporterror(err, c, argv[0], argv[1], nullptr, nullptr, nullptr); reporterror(err, c, argv[0], argv[1], nullptr, nullptr, nullptr);
return err ? -1 : 0; return err ? -1 : 0;

View File

@ -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) 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; imgtoolerr_t res;
uint8_t tmp[ 2 ]; uint8_t tmp[ 2 ];
inp_data = imgtool::stream::open_mem(NULL , 0); inp_data = imgtool::stream::open_mem(NULL , 0);
if (inp_data == nullptr) { if (!inp_data)
return IMGTOOLERR_OUTOFMEMORY; return IMGTOOLERR_OUTOFMEMORY;
}
res = hp9845_tape_read_file(partition , filename , fork , *inp_data); res = hp9845_tape_read_file(partition , filename , fork , *inp_data);
if (res != IMGTOOLERR_SUCCESS) { if (res != IMGTOOLERR_SUCCESS)
delete inp_data;
return res; return res;
}
inp_data->seek(0, SEEK_SET); 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) 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); out_data = imgtool::stream::open_mem(NULL , 0);
if (out_data == nullptr) { if (!out_data)
return IMGTOOLERR_OUTOFMEMORY; return IMGTOOLERR_OUTOFMEMORY;
}
while (1) { while (1) {
char line[ 256 ]; 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); imgtoolerr_t res = hp9845_tape_write_file(partition, filename, fork, *out_data, opts);
delete out_data;
return res; return res;
} }

View File

@ -6182,7 +6182,7 @@ static imgtoolerr_t mac_image_geticoninfo(imgtool::partition &partition, const c
imgtoolerr_t err; imgtoolerr_t err;
imgtool_attribute attr_values[3]; imgtool_attribute attr_values[3];
uint32_t type_code, creator_code, finder_flags; uint32_t type_code, creator_code, finder_flags;
imgtool::stream *stream = NULL; imgtool::stream::ptr stream;
const void *resource_fork; const void *resource_fork;
uint64_t resource_fork_length; uint64_t resource_fork_length;
const void *bundle; const void *bundle;
@ -6198,7 +6198,7 @@ static imgtoolerr_t mac_image_geticoninfo(imgtool::partition &partition, const c
/* first retrieve type and creator code */ /* first retrieve type and creator code */
err = mac_image_getattrs(partition, path, attrs, attr_values); err = mac_image_getattrs(partition, path, attrs, attr_values);
if (err) if (err)
goto done; return err;
type_code = (uint32_t) attr_values[0].i; type_code = (uint32_t) attr_values[0].i;
creator_code = (uint32_t) attr_values[1].i; creator_code = (uint32_t) attr_values[1].i;
finder_flags = (uint32_t) attr_values[2].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); stream = imgtool::stream::open_mem(NULL, 0);
if (!stream) if (!stream)
{ return IMGTOOLERR_SUCCESS;
err = IMGTOOLERR_SUCCESS;
goto done;
}
/* read in the resource fork */ /* read in the resource fork */
err = mac_image_readfile(partition, path, "RESOURCE_FORK", *stream); err = mac_image_readfile(partition, path, "RESOURCE_FORK", *stream);
if (err) if (err)
goto done; return err;
resource_fork = stream->getptr(); resource_fork = stream->getptr();
resource_fork_length = stream->size(); 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 = mac_walk_resources(resource_fork, resource_fork_length, /* BNDL */ 0x424E444C,
bundle_discriminator, &creator_code, NULL, &bundle_length); bundle_discriminator, &creator_code, NULL, &bundle_length);
if (!bundle) if (!bundle)
goto done; return err;
/* find the FREF and the icon family */ /* find the FREF and the icon family */
pos = 8; pos = 8;
@ -6250,7 +6247,7 @@ static imgtoolerr_t mac_image_geticoninfo(imgtool::partition &partition, const c
pos += 6 + this_bundleentry_length * 4; pos += 6 + this_bundleentry_length * 4;
} }
if (!fref_pos || !icn_pos) if (!fref_pos || !icn_pos)
goto done; return err;
/* look up the FREF */ /* look up the FREF */
for (i = 0; i < fref_bundleentry_length; i++) 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) if (i >= fref_bundleentry_length)
goto done; return err;
/* now look up the icon family */ /* now look up the icon family */
resource_id = 0; resource_id = 0;
@ -6280,7 +6277,7 @@ static imgtoolerr_t mac_image_geticoninfo(imgtool::partition &partition, const c
} }
} }
if (i >= icn_bundleentry_length) if (i >= icn_bundleentry_length)
goto done; return err;
/* fetch 32x32 icons (ICN#, icl4, icl8) */ /* fetch 32x32 icons (ICN#, icl4, icl8) */
if (load_icon((uint32_t *) iconinfo->icon32x32, resource_fork, resource_fork_length, 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); /* ics8 */ 0x69637338, resource_id, 32, 32, 8, mac_palette_8bpp, FALSE);
} }
done:
if (stream)
delete stream;
return err; return err;
} }

View File

@ -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, 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) imgtool::stream &sourcef, uint64_t pos, uint64_t fork_len, util::option_resolution *opts)
{ {
imgtoolerr_t err = IMGTOOLERR_SUCCESS; imgtoolerr_t err;
imgtool::stream *mem_stream = NULL; imgtool::stream::ptr mem_stream;
size_t len; size_t len;
if (fork_len > 0) if (fork_len > 0)
{ {
mem_stream = imgtool::stream::open_mem(NULL, 0); mem_stream = imgtool::stream::open_mem(nullptr, 0);
if (!mem_stream) if (!mem_stream)
{ return IMGTOOLERR_OUTOFMEMORY;
err = IMGTOOLERR_OUTOFMEMORY;
goto done;
}
sourcef.seek(pos, SEEK_SET); sourcef.seek(pos, SEEK_SET);
len = imgtool::stream::transfer(*mem_stream, sourcef, fork_len); 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); mem_stream->seek(0, SEEK_SET);
err = partition.write_file(filename, fork, *mem_stream, opts, NULL); err = partition.write_file(filename, fork, *mem_stream, opts, NULL);
if (err) if (err)
goto done; return err;
} }
done: return IMGTOOLERR_SUCCESS;
if (mem_stream)
delete mem_stream;
return err;
} }

View File

@ -1318,16 +1318,14 @@ static imgtoolerr_t thomcrypt_read_file(imgtool::partition &part,
const char *fork, imgtool::stream &dst) const char *fork, imgtool::stream &dst)
{ {
uint8_t buf[3]; 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; imgtoolerr_t err;
if ( !org ) return IMGTOOLERR_OUTOFMEMORY; if ( !org ) return IMGTOOLERR_OUTOFMEMORY;
/* read file */ /* read file */
err = thom_read_file( part, name, fork, *org ); err = thom_read_file( part, name, fork, *org );
if ( err ) { if (err)
delete org; return err;
return err;
}
org->seek(0, SEEK_SET); org->seek(0, SEEK_SET);
if ( org->read(buf, 3 ) < 3 || buf[0] != 0xfe ) 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 ); thom_decrypt( dst, *org );
} }
delete org;
return IMGTOOLERR_SUCCESS; return IMGTOOLERR_SUCCESS;
} }
@ -1364,16 +1361,13 @@ static imgtoolerr_t thomcrypt_write_file(imgtool::partition &part,
else else
{ {
/* regular file */ /* regular file */
imgtool::stream *dst = imgtool::stream::open_mem( NULL, 0 ); imgtool::stream::ptr dst = imgtool::stream::open_mem(nullptr, 0);
imgtoolerr_t err;
if ( !dst ) return IMGTOOLERR_OUTOFMEMORY; if ( !dst ) return IMGTOOLERR_OUTOFMEMORY;
dst->putc( '\xfe' ); dst->putc( '\xfe' );
dst->write(buf+1, 2); dst->write(buf+1, 2);
thom_encrypt( *dst, src ); thom_encrypt( *dst, src );
dst->seek(0, SEEK_SET); dst->seek(0, SEEK_SET);
err = thom_write_file( part, name, fork, *dst, opts ); return thom_write_file( part, name, fork, *dst, opts );
delete dst;
return err;
} }
} }
@ -1402,7 +1396,7 @@ static imgtoolerr_t thom_basic_read_file(imgtool::partition &part,
imgtool::stream &dst, imgtool::stream &dst,
const char *const table[2][128]) 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; imgtoolerr_t err;
uint8_t buf[4]; uint8_t buf[4];
int i; int i;
@ -1411,10 +1405,8 @@ static imgtoolerr_t thom_basic_read_file(imgtool::partition &part,
err = thomcrypt_read_file( part, name, fork, *org ); err = thomcrypt_read_file( part, name, fork, *org );
if (err) if (err)
{
delete org;
return err; return err;
}
org->seek(3, SEEK_SET); /* skip header */ org->seek(3, SEEK_SET); /* skip header */
while ( 1 ) while ( 1 )
@ -1469,7 +1461,6 @@ static imgtoolerr_t thom_basic_read_file(imgtool::partition &part,
} }
end: end:
delete org;
return IMGTOOLERR_SUCCESS; return IMGTOOLERR_SUCCESS;
} }

View File

@ -96,15 +96,15 @@ imgtool::stream::~stream()
// open_zip // 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) if (read_or_write)
return nullptr; return imgtool::stream::ptr();
/* check to see if the file exists */ /* check to see if the file exists */
FILE *f = fopen(zipname, "r"); FILE *f = fopen(zipname, "r");
if (!f) if (!f)
return nullptr; return imgtool::stream::ptr();
fclose(f); fclose(f);
imgtool::stream::ptr imgfile(new imgtool::stream(true)); 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::ptr z;
util::archive_file::open_zip(zipname, z); util::archive_file::open_zip(zipname, z);
if (!z) if (!z)
return nullptr; return imgtool::stream::ptr();
int zipent = z->first_file(); int zipent = z->first_file();
while ((zipent >= 0) && subname && strcmp(subname, z->current_name().c_str())) while ((zipent >= 0) && subname && strcmp(subname, z->current_name().c_str()))
zipent = z->next_file(); zipent = z->next_file();
if (zipent < 0) if (zipent < 0)
return nullptr; return imgtool::stream::ptr();
imgfile->filesize = z->current_uncompressed_length(); imgfile->filesize = z->current_uncompressed_length();
imgfile->buffer = reinterpret_cast<std::uint8_t *>(malloc(z->current_uncompressed_length())); imgfile->buffer = reinterpret_cast<std::uint8_t *>(malloc(z->current_uncompressed_length()));
if (!imgfile->buffer) if (!imgfile->buffer)
return nullptr; return imgtool::stream::ptr();
if (z->decompress(imgfile->buffer, z->current_uncompressed_length()) != util::archive_file::error::NONE) 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 // 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[] = 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_READ | OPEN_FLAG_WRITE | OPEN_FLAG_CREATE OPEN_FLAG_READ | OPEN_FLAG_WRITE | OPEN_FLAG_CREATE
}; };
imgtool::stream *s = nullptr; imgtool::stream::ptr s;
char c; char c;
/* maybe we are just a ZIP? */ /* 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 */ /* 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))); imgtool::stream::ptr imgfile(new imgtool::stream(read_or_write ? false : true, std::move(f)));
/* Normal file */ /* Normal file */
imgfile->name = fname; 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 // 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)); imgtool::stream::ptr imgfile(new imgtool::stream(false, size));
if (!imgfile->buffer) 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 // 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)); 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 imgtool::stream::file_crc(const char *fname, unsigned long *result)
{ {
int err; int err;
imgtool::stream *f; imgtool::stream::ptr f;
f = imgtool::stream::open(fname, OSD_FOPEN_READ); f = imgtool::stream::open(fname, OSD_FOPEN_READ);
if (!f) if (!f)
return IMGTOOLERR_FILENOTFOUND; return IMGTOOLERR_FILENOTFOUND;
err = f->crc(result); err = f->crc(result);
delete f;
return err; return err;
} }

View File

@ -23,9 +23,9 @@ namespace imgtool
~stream(); ~stream();
static imgtool::stream *open(const char *fname, int read_or_write); /* similar params to mame_fopen */ static imgtool::stream::ptr open(const char *fname, int read_or_write); /* similar params to mame_fopen */
static imgtool::stream *open_write_stream(int filesize); static imgtool::stream::ptr open_write_stream(int filesize);
static imgtool::stream *open_mem(void *buf, size_t sz); static imgtool::stream::ptr open_mem(void *buf, size_t sz);
util::core_file *core_file(); util::core_file *core_file();
uint32_t read(void *buf, uint32_t sz); uint32_t read(void *buf, uint32_t sz);
@ -75,7 +75,7 @@ namespace imgtool
stream(bool wp, std::size_t size, void *buf); stream(bool wp, std::size_t size, void *buf);
// private methods // 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);
}; };
} }