Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unused JSON functions #140

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 0 additions & 119 deletions src/ocispec/json_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -1600,122 +1600,3 @@ clone_map_string_string (json_map_string_string *src)
}
return move_ptr (ret);
}

int
append_json_map_string_string (json_map_string_string *map, const char *key, const char *val)
{
size_t len, i;
__auto_free char **keys = NULL;
__auto_free char **values = NULL;
__auto_free char *new_key = NULL;
__auto_free char *new_value = NULL;

if (map == NULL)
return -1;

for (i = 0; i < map->len; i++)
{
if (strcmp (map->keys[i], key) == 0)
{
char *v = strdup (val ? val : "");
if (v == NULL)
return -1;
free (map->values[i]);
map->values[i] = v;
return 0;
}
}

if ((SIZE_MAX / sizeof (char *) - 1) < map->len)
return -1;

new_key = strdup (key ? key : "");
if (new_key == NULL)
return -1;

new_value = strdup (val ? val : "");
if (new_value == NULL)
return -1;

len = map->len + 1;
keys = realloc (map->keys, len * sizeof (char *));
if (keys == NULL)
return -1;
map->keys = keys;
keys = NULL;
map->keys[map->len] = NULL;

values = realloc (map->values, len * sizeof (char *));
if (values == NULL)
return -1;

map->keys[map->len] = new_key;
new_key = NULL;
map->values = values;
values = NULL;
map->values[map->len] = new_value;
new_value = NULL;

map->len++;
return 0;
}

static void
cleanup_yajl_gen (yajl_gen g)
{
if (! g)
return;
yajl_gen_clear (g);
yajl_gen_free (g);
}

define_cleaner_function (yajl_gen, cleanup_yajl_gen)

char *
json_marshal_string (const char *str, size_t length, const struct parser_context *ctx, parser_error *err)
{
__auto_cleanup (cleanup_yajl_gen) yajl_gen g = NULL;
struct parser_context tmp_ctx = { 0 };
const unsigned char *gen_buf = NULL;
char *json_buf = NULL;
size_t gen_len = 0;
yajl_gen_status stat;

if (str == NULL || err == NULL)
return NULL;

*err = NULL;
if (ctx == NULL)
ctx = (const struct parser_context *) (&tmp_ctx);

if (! json_gen_init (&g, ctx))
{
*err = strdup ("Json_gen init failed");
return json_buf;
}
stat = yajl_gen_string ((yajl_gen) g, (const unsigned char *) str, length);
if (yajl_gen_status_ok != stat)
{
if (asprintf (err, "error generating json, errcode: %d", (int) stat) < 0)
*err = strdup ("error allocating memory");
return json_buf;
}
yajl_gen_get_buf (g, &gen_buf, &gen_len);
if (gen_buf == NULL)
{
*err = strdup ("Error to get generated json");
return json_buf;
}

json_buf = calloc (1, gen_len + 1);
if (json_buf == NULL)
{
*err = strdup ("error allocating memory");
return json_buf;
}

(void) memcpy (json_buf, gen_buf, gen_len);
json_buf[gen_len] = '\0';

return json_buf;
}
4 changes: 0 additions & 4 deletions src/ocispec/json_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,6 @@ json_map_string_string *make_json_map_string_string (yajl_val src, const struct
yajl_gen_status gen_json_map_string_string (void *ctx, const json_map_string_string *map,
const struct parser_context *ptx, parser_error *err);

int append_json_map_string_string (json_map_string_string *map, const char *key, const char *val);

char *json_marshal_string (const char *str, size_t length, const struct parser_context *ctx, parser_error *err);

#ifdef __cplusplus
}
#endif
Expand Down
Loading