struct upkg_private {
const struct upkg_file_ops *fops;
+ int (*dtor)(void *handle);
void *f;
struct upkg_file *last_file;
/* Default I/O operations for ordinary files. */
static size_t file_read(void *buf, size_t size, void *handle)
{
- return fread(buf, 1, size, handle);
+ return fread(buf, 1, size, (FILE *)handle);
}
static int file_seek(void *handle, long offset, int whence)
{
- return fseek(handle, offset, whence);
+ return fseek((FILE *)handle, offset, whence);
}
static long file_tell(void *handle)
{
- return ftell(handle);
+ return ftell((FILE *)handle);
}
static int file_eof(void *handle)
{
- return feof(handle);
+ return feof((FILE *)handle);
+}
+
+static int file_close(void *handle)
+{
+ return fclose((FILE *)handle);
}
const struct upkg_file_ops upkg_default_fops = {
len = 0;
rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
- if (rc == 0 || len < 0 || len >= pkg->name_count) goto err;
+ if (rc == 0 || len >= pkg->name_count) goto err;
import->class_package = pkg->priv->names[tmp].name;
len += rc;
rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
- if (rc == 0 || len < 0 || len >= pkg->name_count) goto err;
+ if (rc == 0 || len >= pkg->name_count) goto err;
import->class_name = pkg->priv->names[tmp].name;
len += rc;
len += 4;
rc = upkg_decode_index(&tmp, buf+len, nbuf-len);
- if (rc == 0 || len < 0 || len >= pkg->name_count) goto err;
+ if (rc == 0 || len >= pkg->name_count) goto err;
import->object_name = pkg->priv->names[tmp].name;
len += rc;
return -1;
}
-struct upkg *upkg_open(void *f, const struct upkg_file_ops *fops)
+struct upkg *upkg_open(void *f, const struct upkg_file_ops *fops,
+ int (*destructor)(void *handle))
{
unsigned char hdr_buf[UPKG_HDR_SIZE];
struct upkg *pkg;
return NULL;
}
pkg->priv->fops = fops;
+ pkg->priv->dtor = destructor;
pkg->priv->f = f;
if (pkg_init_guid(pkg) != 0) {
return NULL;
}
- pkg = upkg_open(f, &upkg_default_fops);
+ pkg = upkg_open(f, &upkg_default_fops, file_close);
if (!pkg) {
fclose(f);
}
{
int rc = 0;
- if (pkg->priv->f) {
- rc = fclose(pkg->priv->f);
+ if (pkg->priv->dtor) {
+ rc = pkg->priv->dtor(pkg->priv->f);
+ }
- for (unsigned i = 0; i < pkg->name_count; i++) {
- free(pkg->priv->names[i].name);
- }
+ for (unsigned i = 0; i < pkg->name_count; i++) {
+ free(pkg->priv->names[i].name);
}
free(pkg->priv->imports);