Skip to content

Commit

Permalink
COMMON: Added sail_alloc_meta_data_and_value_from_[un]known_key()
Browse files Browse the repository at this point in the history
  • Loading branch information
HappySeaFox committed Oct 17, 2023
1 parent f247e4d commit 2164f9a
Show file tree
Hide file tree
Showing 12 changed files with 112 additions and 65 deletions.
26 changes: 26 additions & 0 deletions src/libsail-common/meta_data.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,32 @@ sail_status_t sail_alloc_meta_data_from_unknown_key(const char *key_unknown, str
return SAIL_OK;
}

sail_status_t sail_alloc_meta_data_and_value_from_known_key(enum SailMetaData key, struct sail_meta_data **meta_data) {

struct sail_meta_data *meta_data_local;
SAIL_TRY(sail_alloc_meta_data_from_known_key(key, &meta_data_local));

SAIL_TRY_OR_CLEANUP(sail_alloc_variant(&meta_data_local->value),
/* cleanup */ sail_destroy_meta_data(meta_data_local));

*meta_data = meta_data_local;

return SAIL_OK;
}

sail_status_t sail_alloc_meta_data_and_value_from_unknown_key(const char *key_unknown, struct sail_meta_data **meta_data) {

struct sail_meta_data *meta_data_local;
SAIL_TRY(sail_alloc_meta_data_from_unknown_key(key_unknown, &meta_data_local));

SAIL_TRY_OR_CLEANUP(sail_alloc_variant(&meta_data_local->value),
/* cleanup */ sail_destroy_meta_data(meta_data_local));

*meta_data = meta_data_local;

return SAIL_OK;
}

void sail_destroy_meta_data(struct sail_meta_data *meta_data) {

if (meta_data == NULL) {
Expand Down
18 changes: 18 additions & 0 deletions src/libsail-common/meta_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,24 @@ SAIL_EXPORT sail_status_t sail_alloc_meta_data_from_known_key(enum SailMetaData
SAIL_EXPORT sail_status_t sail_alloc_meta_data_from_unknown_key(const char *key_unknown,
struct sail_meta_data **meta_data);

/*
* Allocates new meta data from the specified known key and the nested value.
* The key must not be SAIL_META_DATA_UNKNOWN.
*
* Returns SAIL_OK on success.
*/
SAIL_EXPORT sail_status_t sail_alloc_meta_data_and_value_from_known_key(enum SailMetaData key,
struct sail_meta_data **meta_data);

/*
* Allocates new meta data from the specified unknown key and the nested value.
* Makes a deep copy of the key. Sets the key to SAIL_META_DATA_UNKNOWN.
*
* Returns SAIL_OK on success.
*/
SAIL_EXPORT sail_status_t sail_alloc_meta_data_and_value_from_unknown_key(const char *key_unknown,
struct sail_meta_data **meta_data);

/*
* Destroys the specified meta data.
*/
Expand Down
4 changes: 1 addition & 3 deletions src/sail-codecs/avif/helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,7 @@ sail_status_t avif_private_fetch_meta_data(enum SailMetaData key, const struct a

SAIL_TRY(sail_alloc_meta_data_node(&meta_data_node_local));

SAIL_TRY_OR_CLEANUP(sail_alloc_meta_data_from_known_key(key, &meta_data_node_local->meta_data),
/* cleanup */ sail_destroy_meta_data_node(meta_data_node_local));
SAIL_TRY_OR_CLEANUP(sail_alloc_variant(&meta_data_node_local->meta_data->value),
SAIL_TRY_OR_CLEANUP(sail_alloc_meta_data_and_value_from_known_key(key, &meta_data_node_local->meta_data),
/* cleanup */ sail_destroy_meta_data_node(meta_data_node_local));
SAIL_TRY_OR_CLEANUP(sail_set_variant_data(meta_data_node_local->meta_data->value, avif_rw_data->data, avif_rw_data->size),
/* cleanup */ sail_destroy_meta_data_node(meta_data_node_local));
Expand Down
4 changes: 1 addition & 3 deletions src/sail-codecs/gif/helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ static sail_status_t save_str_in_meta_data(const char *str, unsigned length_wo_n

SAIL_TRY(sail_alloc_meta_data_node(&meta_data_node_local));

SAIL_TRY_OR_CLEANUP(sail_alloc_meta_data_from_known_key(key, &meta_data_node_local->meta_data),
/* cleanup */ sail_destroy_meta_data_node(meta_data_node_local));
SAIL_TRY_OR_CLEANUP(sail_alloc_variant(&meta_data_node_local->meta_data->value),
SAIL_TRY_OR_CLEANUP(sail_alloc_meta_data_and_value_from_known_key(key, &meta_data_node_local->meta_data),
/* cleanup */ sail_destroy_meta_data_node(meta_data_node_local));
SAIL_TRY_OR_CLEANUP(sail_set_variant_substring(meta_data_node_local->meta_data->value, str, length_wo_null),
/* cleanup */ sail_destroy_meta_data_node(meta_data_node_local));
Expand Down
4 changes: 1 addition & 3 deletions src/sail-codecs/jpeg/helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,7 @@ sail_status_t jpeg_private_fetch_meta_data(struct jpeg_decompress_struct *decomp
struct sail_meta_data_node *meta_data_node;
SAIL_TRY(sail_alloc_meta_data_node(&meta_data_node));

SAIL_TRY_OR_CLEANUP(sail_alloc_meta_data_from_known_key(SAIL_META_DATA_COMMENT, &meta_data_node->meta_data),
/* cleanup */ sail_destroy_meta_data_node(meta_data_node));
SAIL_TRY_OR_CLEANUP(sail_alloc_variant(&meta_data_node->meta_data->value),
SAIL_TRY_OR_CLEANUP(sail_alloc_meta_data_and_value_from_known_key(SAIL_META_DATA_COMMENT, &meta_data_node->meta_data),
/* cleanup */ sail_destroy_meta_data_node(meta_data_node));
SAIL_TRY_OR_CLEANUP(sail_set_variant_substring(meta_data_node->meta_data->value, (const char *)it->data, it->data_length),
/* cleanup */ sail_destroy_meta_data_node(meta_data_node));
Expand Down
11 changes: 3 additions & 8 deletions src/sail-codecs/jpegxl/helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,7 @@ sail_status_t jpegxl_private_fetch_name(JxlDecoder *decoder, uint32_t name_lengt
SAIL_TRY_OR_CLEANUP(sail_alloc_meta_data_node(&meta_data_node_local),
/* cleanup */ sail_free(name));

SAIL_TRY_OR_CLEANUP(sail_alloc_meta_data_from_known_key(SAIL_META_DATA_NAME, &meta_data_node_local->meta_data),
/* cleanup */ sail_free(name), sail_destroy_meta_data_node(meta_data_node_local));
SAIL_TRY_OR_CLEANUP(sail_alloc_variant(&meta_data_node_local->meta_data->value),
SAIL_TRY_OR_CLEANUP(sail_alloc_meta_data_and_value_from_known_key(SAIL_META_DATA_NAME, &meta_data_node_local->meta_data),
/* cleanup */ sail_free(name), sail_destroy_meta_data_node(meta_data_node_local));
SAIL_TRY_OR_CLEANUP(sail_set_variant_shallow_string(meta_data_node_local->meta_data->value, name),
/* cleanup */ sail_free(name), sail_destroy_meta_data_node(meta_data_node_local));
Expand Down Expand Up @@ -318,17 +316,14 @@ sail_status_t jpegxl_private_fetch_metadata(JxlDecoder *decoder, struct sail_met
SAIL_LOG_AND_RETURN(SAIL_ERROR_UNDERLYING_CODEC);
}

struct sail_meta_data_node *meta_data_node_local = NULL;

void *data;
SAIL_TRY(sail_malloc(size, &data));

struct sail_meta_data_node *meta_data_node_local;
SAIL_TRY_OR_CLEANUP(sail_alloc_meta_data_node(&meta_data_node_local),
/* cleanup */ sail_free(data));

SAIL_TRY_OR_CLEANUP(sail_alloc_meta_data_from_known_key(meta_data, &meta_data_node_local->meta_data),
/* cleanup */ sail_free(data), sail_destroy_meta_data_node(meta_data_node_local));
SAIL_TRY_OR_CLEANUP(sail_alloc_variant(&meta_data_node_local->meta_data->value),
SAIL_TRY_OR_CLEANUP(sail_alloc_meta_data_and_value_from_known_key(meta_data, &meta_data_node_local->meta_data),
/* cleanup */ sail_free(data), sail_destroy_meta_data_node(meta_data_node_local));
SAIL_TRY_OR_CLEANUP(sail_set_variant_shallow_data(meta_data_node_local->meta_data->value, data, size),
/* cleanup */ sail_free(data), sail_destroy_meta_data_node(meta_data_node_local));
Expand Down
17 changes: 5 additions & 12 deletions src/sail-codecs/png/helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,11 @@ static sail_status_t hex_string_to_meta_data_node(const char *hex_str, enum Sail
SAIL_TRY(sail_hex_string_to_data(start, &data, &data_size));

struct sail_meta_data_node *meta_data_node_local;

SAIL_TRY_OR_CLEANUP(sail_alloc_meta_data_node(&meta_data_node_local),
/* cleanup */ sail_free(data));
SAIL_TRY_OR_CLEANUP(sail_alloc_meta_data_from_known_key(key, &meta_data_node_local->meta_data),

SAIL_TRY_OR_CLEANUP(sail_alloc_meta_data_and_value_from_known_key(key, &meta_data_node_local->meta_data),
/* cleanup */ sail_free(data));
SAIL_TRY_OR_CLEANUP(sail_alloc_variant(&meta_data_node_local->meta_data->value),
/* cleanup */ sail_destroy_meta_data_node(meta_data_node_local),
free(data));
SAIL_TRY_OR_CLEANUP(sail_set_variant_data(meta_data_node_local->meta_data->value, data, data_size),
/* cleanup */ sail_destroy_meta_data_node(meta_data_node_local),
free(data));
Expand Down Expand Up @@ -313,15 +310,13 @@ sail_status_t png_private_fetch_meta_data(png_structp png_ptr, png_infop info_pt
SAIL_TRY(sail_alloc_meta_data_node(&meta_data_node));

if (meta_data == SAIL_META_DATA_UNKNOWN) {
SAIL_TRY_OR_CLEANUP(sail_alloc_meta_data_from_unknown_key(lines[i].key, &meta_data_node->meta_data),
SAIL_TRY_OR_CLEANUP(sail_alloc_meta_data_and_value_from_unknown_key(lines[i].key, &meta_data_node->meta_data),
/* cleanup */ sail_destroy_meta_data_node(meta_data_node));
} else {
SAIL_TRY_OR_CLEANUP(sail_alloc_meta_data_from_known_key(meta_data, &meta_data_node->meta_data),
SAIL_TRY_OR_CLEANUP(sail_alloc_meta_data_and_value_from_known_key(meta_data, &meta_data_node->meta_data),
/* cleanup */ sail_destroy_meta_data_node(meta_data_node));
}

SAIL_TRY_OR_CLEANUP(sail_alloc_variant(&meta_data_node->meta_data->value),
/* cleanup */ sail_destroy_meta_data_node(meta_data_node));
SAIL_TRY_OR_CLEANUP(sail_set_variant_string(meta_data_node->meta_data->value, lines[i].text),
/* cleanup */ sail_destroy_meta_data_node(meta_data_node));
}
Expand All @@ -337,9 +332,7 @@ sail_status_t png_private_fetch_meta_data(png_structp png_ptr, png_infop info_pt
struct sail_meta_data_node *meta_data_node;

SAIL_TRY(sail_alloc_meta_data_node(&meta_data_node));
SAIL_TRY_OR_CLEANUP(sail_alloc_meta_data_from_known_key(SAIL_META_DATA_EXIF, &meta_data_node->meta_data),
/* cleanup */ sail_destroy_meta_data_node(meta_data_node));
SAIL_TRY_OR_CLEANUP(sail_alloc_variant(&meta_data_node->meta_data->value),
SAIL_TRY_OR_CLEANUP(sail_alloc_meta_data_and_value_from_known_key(SAIL_META_DATA_EXIF, &meta_data_node->meta_data),
/* cleanup */ sail_destroy_meta_data_node(meta_data_node));
SAIL_TRY_OR_CLEANUP(sail_set_variant_data(meta_data_node->meta_data->value, exif, exif_size),
/* cleanup */ sail_destroy_meta_data_node(meta_data_node));
Expand Down
21 changes: 7 additions & 14 deletions src/sail-codecs/tga/helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,7 @@ sail_status_t tga_private_fetch_extension(struct sail_io *io, double *gamma, str

if (strlen(author) > 0) {
SAIL_TRY(sail_alloc_meta_data_node(last_meta_data_node));
SAIL_TRY(sail_alloc_meta_data_from_known_key(SAIL_META_DATA_AUTHOR, &(*last_meta_data_node)->meta_data));
SAIL_TRY(sail_alloc_variant(&(*last_meta_data_node)->meta_data->value));
SAIL_TRY(sail_alloc_meta_data_and_value_from_known_key(SAIL_META_DATA_AUTHOR, &(*last_meta_data_node)->meta_data));
SAIL_TRY(sail_set_variant_string((*last_meta_data_node)->meta_data->value, author));
last_meta_data_node = &(*last_meta_data_node)->next;
}
Expand All @@ -179,8 +178,7 @@ sail_status_t tga_private_fetch_extension(struct sail_io *io, double *gamma, str

if (strlen(comments) > 0) {
SAIL_TRY(sail_alloc_meta_data_node(last_meta_data_node));
SAIL_TRY(sail_alloc_meta_data_from_known_key(SAIL_META_DATA_COMMENT, &(*last_meta_data_node)->meta_data));
SAIL_TRY(sail_alloc_variant(&(*last_meta_data_node)->meta_data->value));
SAIL_TRY(sail_alloc_meta_data_and_value_from_known_key(SAIL_META_DATA_COMMENT, &(*last_meta_data_node)->meta_data));
SAIL_TRY(sail_set_variant_string((*last_meta_data_node)->meta_data->value, comments));
last_meta_data_node = &(*last_meta_data_node)->next;
}
Expand Down Expand Up @@ -211,8 +209,7 @@ sail_status_t tga_private_fetch_extension(struct sail_io *io, double *gamma, str
const unsigned long timestamp = (unsigned long)mktime(&time_tm);

SAIL_TRY(sail_alloc_meta_data_node(last_meta_data_node));
SAIL_TRY(sail_alloc_meta_data_from_known_key(SAIL_META_DATA_CREATION_TIME, &(*last_meta_data_node)->meta_data));
SAIL_TRY(sail_alloc_variant(&(*last_meta_data_node)->meta_data->value));
SAIL_TRY(sail_alloc_meta_data_and_value_from_known_key(SAIL_META_DATA_CREATION_TIME, &(*last_meta_data_node)->meta_data));
SAIL_TRY(sail_set_variant_unsigned_long((*last_meta_data_node)->meta_data->value, timestamp));

last_meta_data_node = &(*last_meta_data_node)->next;
Expand All @@ -227,8 +224,7 @@ sail_status_t tga_private_fetch_extension(struct sail_io *io, double *gamma, str

if (strlen(job) > 0) {
SAIL_TRY(sail_alloc_meta_data_node(last_meta_data_node));
SAIL_TRY(sail_alloc_meta_data_from_known_key(SAIL_META_DATA_JOB, &(*last_meta_data_node)->meta_data));
SAIL_TRY(sail_alloc_variant(&(*last_meta_data_node)->meta_data->value));
SAIL_TRY(sail_alloc_meta_data_and_value_from_known_key(SAIL_META_DATA_JOB, &(*last_meta_data_node)->meta_data));
SAIL_TRY(sail_set_variant_string((*last_meta_data_node)->meta_data->value, job));
last_meta_data_node = &(*last_meta_data_node)->next;
}
Expand All @@ -246,8 +242,7 @@ sail_status_t tga_private_fetch_extension(struct sail_io *io, double *gamma, str
snprintf(timestamp, sizeof(timestamp), "%05d:%02d:%02d", hour, minute, second);

SAIL_TRY(sail_alloc_meta_data_node(last_meta_data_node));
SAIL_TRY(sail_alloc_meta_data_from_known_key(SAIL_META_DATA_TIME_CONSUMED, &(*last_meta_data_node)->meta_data));
SAIL_TRY(sail_alloc_variant(&(*last_meta_data_node)->meta_data->value));
SAIL_TRY(sail_alloc_meta_data_and_value_from_known_key(SAIL_META_DATA_TIME_CONSUMED, &(*last_meta_data_node)->meta_data));
SAIL_TRY(sail_set_variant_string((*last_meta_data_node)->meta_data->value, timestamp));
last_meta_data_node = &(*last_meta_data_node)->next;
}
Expand All @@ -261,8 +256,7 @@ sail_status_t tga_private_fetch_extension(struct sail_io *io, double *gamma, str

if (strlen(software) > 0) {
SAIL_TRY(sail_alloc_meta_data_node(last_meta_data_node));
SAIL_TRY(sail_alloc_meta_data_from_known_key(SAIL_META_DATA_SOFTWARE, &(*last_meta_data_node)->meta_data));
SAIL_TRY(sail_alloc_variant(&(*last_meta_data_node)->meta_data->value));
SAIL_TRY(sail_alloc_meta_data_and_value_from_known_key(SAIL_META_DATA_SOFTWARE, &(*last_meta_data_node)->meta_data));
SAIL_TRY(sail_set_variant_string((*last_meta_data_node)->meta_data->value, software));
last_meta_data_node = &(*last_meta_data_node)->next;
}
Expand All @@ -286,8 +280,7 @@ sail_status_t tga_private_fetch_extension(struct sail_io *io, double *gamma, str
}

SAIL_TRY(sail_alloc_meta_data_node(last_meta_data_node));
SAIL_TRY(sail_alloc_meta_data_from_known_key(SAIL_META_DATA_SOFTWARE_VERSION, &(*last_meta_data_node)->meta_data));
SAIL_TRY(sail_alloc_variant(&(*last_meta_data_node)->meta_data->value));
SAIL_TRY(sail_alloc_meta_data_and_value_from_known_key(SAIL_META_DATA_SOFTWARE_VERSION, &(*last_meta_data_node)->meta_data));
SAIL_TRY(sail_set_variant_string((*last_meta_data_node)->meta_data->value, version_string));
last_meta_data_node = &(*last_meta_data_node)->next;
}
Expand Down
6 changes: 2 additions & 4 deletions src/sail-codecs/tiff/helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -326,11 +326,9 @@ static sail_status_t fetch_single_meta_data(TIFF *tiff, int tag, enum SailMetaDa

if (TIFFGetField(tiff, tag, &data)) {
struct sail_meta_data_node *meta_data_node;

SAIL_TRY(sail_alloc_meta_data_node(&meta_data_node));
SAIL_TRY_OR_CLEANUP(sail_alloc_meta_data_from_known_key(key, &meta_data_node->meta_data),
/* cleanup */ sail_destroy_meta_data_node(meta_data_node));
SAIL_TRY_OR_CLEANUP(sail_alloc_variant(&meta_data_node->meta_data->value),

SAIL_TRY_OR_CLEANUP(sail_alloc_meta_data_and_value_from_known_key(key, &meta_data_node->meta_data),
/* cleanup */ sail_destroy_meta_data_node(meta_data_node));
SAIL_TRY_OR_CLEANUP(sail_set_variant_string(meta_data_node->meta_data->value, data),
/* cleanup */ sail_destroy_meta_data_node(meta_data_node));
Expand Down
4 changes: 1 addition & 3 deletions src/sail-codecs/wal/helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,7 @@ sail_status_t wal_private_assign_meta_data(const struct WalFileHeader *wal_heade
struct sail_meta_data_node *meta_data_node_local;
SAIL_TRY(sail_alloc_meta_data_node(&meta_data_node_local));

SAIL_TRY_OR_CLEANUP(sail_alloc_meta_data_from_known_key(SAIL_META_DATA_NAME, &meta_data_node_local->meta_data),
/* cleanup */ sail_destroy_meta_data_node(meta_data_node_local));
SAIL_TRY_OR_CLEANUP(sail_alloc_variant(&meta_data_node_local->meta_data->value),
SAIL_TRY_OR_CLEANUP(sail_alloc_meta_data_and_value_from_known_key(SAIL_META_DATA_NAME, &meta_data_node_local->meta_data),
/* cleanup */ sail_destroy_meta_data_node(meta_data_node_local));
SAIL_TRY_OR_CLEANUP(sail_set_variant_string(meta_data_node_local->meta_data->value, wal_header->name),
/* cleanup */ sail_destroy_meta_data_node(meta_data_node_local));
Expand Down
10 changes: 2 additions & 8 deletions src/sail-codecs/webp/helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,7 @@ sail_status_t webp_private_fetch_meta_data(WebPDemuxer *webp_demux, struct sail_

SAIL_TRY_OR_CLEANUP(sail_alloc_meta_data_node(&meta_data_node),
/* cleanup */ WebPDemuxReleaseChunkIterator(&chunk_iterator));
SAIL_TRY_OR_CLEANUP(sail_alloc_meta_data_from_known_key(SAIL_META_DATA_XMP, &meta_data_node->meta_data),
/* cleanup */ sail_destroy_meta_data_node(meta_data_node),
WebPDemuxReleaseChunkIterator(&chunk_iterator));
SAIL_TRY_OR_CLEANUP(sail_alloc_variant(&meta_data_node->meta_data->value),
SAIL_TRY_OR_CLEANUP(sail_alloc_meta_data_and_value_from_known_key(SAIL_META_DATA_XMP, &meta_data_node->meta_data),
/* cleanup */ sail_destroy_meta_data_node(meta_data_node),
WebPDemuxReleaseChunkIterator(&chunk_iterator));
SAIL_TRY_OR_CLEANUP(sail_set_variant_substring(meta_data_node->meta_data->value,
Expand All @@ -128,10 +125,7 @@ sail_status_t webp_private_fetch_meta_data(WebPDemuxer *webp_demux, struct sail_

SAIL_TRY_OR_CLEANUP(sail_alloc_meta_data_node(&meta_data_node),
/* cleanup */ WebPDemuxReleaseChunkIterator(&chunk_iterator));
SAIL_TRY_OR_CLEANUP(sail_alloc_meta_data_from_known_key(SAIL_META_DATA_EXIF, &meta_data_node->meta_data),
/* cleanup */ sail_destroy_meta_data_node(meta_data_node),
WebPDemuxReleaseChunkIterator(&chunk_iterator));
SAIL_TRY_OR_CLEANUP(sail_alloc_variant(&meta_data_node->meta_data->value),
SAIL_TRY_OR_CLEANUP(sail_alloc_meta_data_and_value_from_known_key(SAIL_META_DATA_EXIF, &meta_data_node->meta_data),
/* cleanup */ sail_destroy_meta_data_node(meta_data_node),
WebPDemuxReleaseChunkIterator(&chunk_iterator));
SAIL_TRY_OR_CLEANUP(sail_set_variant_data(meta_data_node->meta_data->value,
Expand Down
Loading

0 comments on commit 2164f9a

Please sign in to comment.