Skip to content

Commit

Permalink
Drop fwpkg_new()
Browse files Browse the repository at this point in the history
  • Loading branch information
matwey committed Aug 2, 2019
1 parent 2315110 commit 97180ec
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 76 deletions.
3 changes: 1 addition & 2 deletions include/fwpkg.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ struct fwpkg {
const char* error_str;
};

struct fwpkg* fwpkg_new();
int fwpkg_from_file(struct fwpkg* fwpkg, const char* filename);
int fwpkg_from_preload(struct fwpkg* fwpkg);
void fwpkg_free(struct fwpkg* fwpkg);
void fwpkg_destroy(struct fwpkg* fwpkg);

const char* fwpkg_get_error_string(struct fwpkg* fwpkg);

Expand Down
27 changes: 9 additions & 18 deletions src/fwpkg.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,12 @@ static int fwpkg_locate_files(struct fwpkg* fwpkg) {
return 0;
}

struct fwpkg* fwpkg_new() {
struct fwpkg* fwpkg = malloc(sizeof(struct fwpkg));

if (!fwpkg) {
return NULL;
}

memset(fwpkg, 0, sizeof(struct fwpkg));

return fwpkg;
}

int fwpkg_from_file(struct fwpkg* fwpkg, const char* filename) {
zip_error_t ze;
int error;

memset(fwpkg, 0, sizeof(struct fwpkg));

fwpkg->pkg = zip_open(filename, ZIP_CHECKCONS | ZIP_RDONLY, &error);
if (!fwpkg->pkg) {
zip_error_init_with_code(&ze, error);
Expand All @@ -100,9 +90,12 @@ int fwpkg_from_file(struct fwpkg* fwpkg, const char* filename) {

int fwpkg_from_preload(struct fwpkg* fwpkg) {
zip_error_t error;
zip_source_t *src = zip_source_buffer_create((const void*)_binary_ov3_fwpkg_start,
(const void*)_binary_ov3_fwpkg_end - (const void*)_binary_ov3_fwpkg_start, 0, &error);
zip_source_t *src = NULL;

memset(fwpkg, 0, sizeof(struct fwpkg));

src = zip_source_buffer_create((const void*)_binary_ov3_fwpkg_start,
(const void*)_binary_ov3_fwpkg_end - (const void*)_binary_ov3_fwpkg_start, 0, &error);
if (!src) {
fwpkg->error_str = zip_error_strerror(&error);
return -1;
Expand All @@ -117,10 +110,8 @@ int fwpkg_from_preload(struct fwpkg* fwpkg) {
return fwpkg_locate_files(fwpkg);
}

void fwpkg_free(struct fwpkg* fwpkg) {
if (fwpkg->pkg)
zip_discard(fwpkg->pkg);
free(fwpkg);
void fwpkg_destroy(struct fwpkg* fwpkg) {
zip_discard(fwpkg->pkg);
}

const char* fwpkg_get_error_string(struct fwpkg* fwpkg) {
Expand Down
84 changes: 35 additions & 49 deletions test/fwpkg.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,103 +3,90 @@

#include <fwpkg.h>

START_TEST (test_fwpkg_new1) {
struct fwpkg* fwpkg = fwpkg_new();
ck_assert_ptr_ne(fwpkg, NULL);
fwpkg_free(fwpkg);
}
END_TEST
START_TEST (test_fwpkg_load1) {
struct fwpkg* fwpkg = fwpkg_new();
struct fwpkg fwpkg;
int ret;
ck_assert_ptr_ne(fwpkg, NULL);
ret = fwpkg_from_file(fwpkg, PROJECT_ROOT "/ov3.fwpkg");
ret = fwpkg_from_file(&fwpkg, PROJECT_ROOT "/ov3.fwpkg");
ck_assert_int_eq(ret, 0);
fwpkg_free(fwpkg);
fwpkg_destroy(&fwpkg);
}
END_TEST
START_TEST (test_fwpkg_load2) {
struct fwpkg* fwpkg = fwpkg_new();
struct fwpkg fwpkg;
int ret;
ck_assert_ptr_ne(fwpkg, NULL);
ret = fwpkg_from_preload(fwpkg);
ret = fwpkg_from_preload(&fwpkg);
ck_assert_int_eq(ret, 0);
fwpkg_free(fwpkg);
fwpkg_destroy(&fwpkg);
}
END_TEST
START_TEST (test_fwpkg_size1) {
struct fwpkg* fwpkg = fwpkg_new();
struct fwpkg fwpkg;
int ret;
ck_assert_ptr_ne(fwpkg, NULL);
ret = fwpkg_from_file(fwpkg, PROJECT_ROOT "/ov3.fwpkg");
ret = fwpkg_from_file(&fwpkg, PROJECT_ROOT "/ov3.fwpkg");
ck_assert_int_eq(ret, 0);
ck_assert_uint_eq(fwpkg_map_size(fwpkg), 2080);
ck_assert_uint_eq(fwpkg_bitstream_size(fwpkg), 340972);
fwpkg_free(fwpkg);
ck_assert_uint_eq(fwpkg_map_size(&fwpkg), 2080);
ck_assert_uint_eq(fwpkg_bitstream_size(&fwpkg), 340972);
fwpkg_destroy(&fwpkg);
}
END_TEST
START_TEST (test_fwpkg_read1) {
char* buf;
size_t size;
struct fwpkg* fwpkg = fwpkg_new();
struct fwpkg fwpkg;
int ret;
ck_assert_ptr_ne(fwpkg, NULL);
ret = fwpkg_from_file(fwpkg, PROJECT_ROOT "/ov3.fwpkg");
ret = fwpkg_from_file(&fwpkg, PROJECT_ROOT "/ov3.fwpkg");
ck_assert_int_eq(ret, 0);
size = fwpkg_map_size(fwpkg);
size = fwpkg_map_size(&fwpkg);
buf = malloc(size);
ck_assert_int_eq(fwpkg_read_map(fwpkg, buf, &size), 0);
ck_assert_uint_eq(size, fwpkg_map_size(fwpkg));
ck_assert_int_eq(fwpkg_read_map(&fwpkg, buf, &size), 0);
ck_assert_uint_eq(size, fwpkg_map_size(&fwpkg));
free(buf);
fwpkg_free(fwpkg);
fwpkg_destroy(&fwpkg);
}
END_TEST
START_TEST (test_fwpkg_read2) {
char* buf;
size_t size;
struct fwpkg* fwpkg = fwpkg_new();
struct fwpkg fwpkg;
int ret;
ck_assert_ptr_ne(fwpkg, NULL);
ret = fwpkg_from_file(fwpkg, PROJECT_ROOT "/ov3.fwpkg");
ret = fwpkg_from_file(&fwpkg, PROJECT_ROOT "/ov3.fwpkg");
ck_assert_int_eq(ret, 0);
size = fwpkg_bitstream_size(fwpkg);
size = fwpkg_bitstream_size(&fwpkg);
buf = malloc(size);
ck_assert_int_eq(fwpkg_read_bitstream(fwpkg, buf, &size), 0);
ck_assert_uint_eq(size, fwpkg_bitstream_size(fwpkg));
ck_assert_int_eq(fwpkg_read_bitstream(&fwpkg, buf, &size), 0);
ck_assert_uint_eq(size, fwpkg_bitstream_size(&fwpkg));
free(buf);
fwpkg_free(fwpkg);
fwpkg_destroy(&fwpkg);
}
END_TEST
START_TEST (test_fwpkg_read3) {
char* buf;
size_t size;
struct fwpkg* fwpkg = fwpkg_new();
struct fwpkg fwpkg;
int ret;
ck_assert_ptr_ne(fwpkg, NULL);
ret = fwpkg_from_preload(fwpkg);
ret = fwpkg_from_preload(&fwpkg);
ck_assert_int_eq(ret, 0);
size = fwpkg_map_size(fwpkg);
size = fwpkg_map_size(&fwpkg);
buf = malloc(size);
ck_assert_int_eq(fwpkg_read_map(fwpkg, buf, &size), 0);
ck_assert_uint_eq(size, fwpkg_map_size(fwpkg));
ck_assert_int_eq(fwpkg_read_map(&fwpkg, buf, &size), 0);
ck_assert_uint_eq(size, fwpkg_map_size(&fwpkg));
free(buf);
fwpkg_free(fwpkg);
fwpkg_destroy(&fwpkg);
}
END_TEST
START_TEST (test_fwpkg_read4) {
char* buf;
size_t size;
struct fwpkg* fwpkg = fwpkg_new();
struct fwpkg fwpkg;
int ret;
ck_assert_ptr_ne(fwpkg, NULL);
ret = fwpkg_from_preload(fwpkg);
ret = fwpkg_from_preload(&fwpkg);
ck_assert_int_eq(ret, 0);
size = fwpkg_bitstream_size(fwpkg);
size = fwpkg_bitstream_size(&fwpkg);
buf = malloc(size);
ck_assert_int_eq(fwpkg_read_bitstream(fwpkg, buf, &size), 0);
ck_assert_uint_eq(size, fwpkg_bitstream_size(fwpkg));
ck_assert_int_eq(fwpkg_read_bitstream(&fwpkg, buf, &size), 0);
ck_assert_uint_eq(size, fwpkg_bitstream_size(&fwpkg));
free(buf);
fwpkg_free(fwpkg);
fwpkg_destroy(&fwpkg);
}
END_TEST

Expand All @@ -111,7 +98,6 @@ Suite* range_suite(void) {

tc_core = tcase_create("Core");

tcase_add_test(tc_core, test_fwpkg_new1);
tcase_add_test(tc_core, test_fwpkg_load1);
tcase_add_test(tc_core, test_fwpkg_load2);
tcase_add_test(tc_core, test_fwpkg_size1);
Expand Down
14 changes: 7 additions & 7 deletions tools/load/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,28 @@ int main(int argc, char** argv) {
int ret;
size_t size;
uint8_t* firmware;
struct fwpkg* fwpkg = fwpkg_new();
struct fwpkg fwpkg;
struct bit bit;
struct cha cha;
struct chb chb;

ret = fwpkg_from_preload(fwpkg);
ret = fwpkg_from_preload(&fwpkg);
if (ret == -1) {
fprintf(stderr, fwpkg_get_error_string(fwpkg));
fprintf(stderr, fwpkg_get_error_string(&fwpkg));
return 1;
}

size = fwpkg_bitstream_size(fwpkg);
size = fwpkg_bitstream_size(&fwpkg);
firmware = malloc(size);
if (firmware == NULL) {
fprintf(stderr, "Cannot allocate memory for firmware %zu", size);
return 1;
}

ret = fwpkg_read_bitstream(fwpkg, firmware, &size);
ret = fwpkg_read_bitstream(&fwpkg, firmware, &size);

if (ret == -1) {
fprintf(stderr, fwpkg_get_error_string(fwpkg));
fprintf(stderr, fwpkg_get_error_string(&fwpkg));
return 1;
}

Expand Down Expand Up @@ -110,7 +110,7 @@ int main(int argc, char** argv) {

chb_destroy(&chb);
cha_destroy(&cha);
fwpkg_free(fwpkg);
fwpkg_destroy(&fwpkg);
free(firmware);

return 0;
Expand Down

0 comments on commit 97180ec

Please sign in to comment.