Path: blob/main/contrib/libcbor/src/cbor/strings.c
39536 views
/*1* Copyright (c) 2014-2020 Pavel Kalvoda <[email protected]>2*3* libcbor is free software; you can redistribute it and/or modify4* it under the terms of the MIT license. See LICENSE for details.5*/67#include "strings.h"8#include <string.h>9#include "internal/memory_utils.h"10#include "internal/unicode.h"1112cbor_item_t *cbor_new_definite_string(void) {13cbor_item_t *item = _cbor_malloc(sizeof(cbor_item_t));14_CBOR_NOTNULL(item);15*item = (cbor_item_t){16.refcount = 1,17.type = CBOR_TYPE_STRING,18.metadata = {.string_metadata = {_CBOR_METADATA_DEFINITE, 0}}};19return item;20}2122cbor_item_t *cbor_new_indefinite_string(void) {23cbor_item_t *item = _cbor_malloc(sizeof(cbor_item_t));24_CBOR_NOTNULL(item);25*item = (cbor_item_t){26.refcount = 1,27.type = CBOR_TYPE_STRING,28.metadata = {.string_metadata = {.type = _CBOR_METADATA_INDEFINITE,29.length = 0}},30.data = _cbor_malloc(sizeof(struct cbor_indefinite_string_data))};31_CBOR_DEPENDENT_NOTNULL(item, item->data);32*((struct cbor_indefinite_string_data *)item->data) =33(struct cbor_indefinite_string_data){34.chunk_count = 0,35.chunk_capacity = 0,36.chunks = NULL,37};38return item;39}4041cbor_item_t *cbor_build_string(const char *val) {42cbor_item_t *item = cbor_new_definite_string();43_CBOR_NOTNULL(item);44size_t len = strlen(val);45unsigned char *handle = _cbor_malloc(len);46_CBOR_DEPENDENT_NOTNULL(item, handle);47memcpy(handle, val, len);48cbor_string_set_handle(item, handle, len);49return item;50}5152cbor_item_t *cbor_build_stringn(const char *val, size_t length) {53cbor_item_t *item = cbor_new_definite_string();54_CBOR_NOTNULL(item);55unsigned char *handle = _cbor_malloc(length);56_CBOR_DEPENDENT_NOTNULL(item, handle);57memcpy(handle, val, length);58cbor_string_set_handle(item, handle, length);59return item;60}6162void cbor_string_set_handle(cbor_item_t *item,63cbor_mutable_data CBOR_RESTRICT_POINTER data,64size_t length) {65CBOR_ASSERT(cbor_isa_string(item));66CBOR_ASSERT(cbor_string_is_definite(item));67item->data = data;68item->metadata.string_metadata.length = length;69struct _cbor_unicode_status unicode_status;70size_t codepoint_count =71_cbor_unicode_codepoint_count(data, length, &unicode_status);72CBOR_ASSERT(codepoint_count <= length);73if (unicode_status.status == _CBOR_UNICODE_OK) {74item->metadata.string_metadata.codepoint_count = codepoint_count;75} else {76item->metadata.string_metadata.codepoint_count = 0;77}78}7980cbor_item_t **cbor_string_chunks_handle(const cbor_item_t *item) {81CBOR_ASSERT(cbor_isa_string(item));82CBOR_ASSERT(cbor_string_is_indefinite(item));83return ((struct cbor_indefinite_string_data *)item->data)->chunks;84}8586size_t cbor_string_chunk_count(const cbor_item_t *item) {87CBOR_ASSERT(cbor_isa_string(item));88CBOR_ASSERT(cbor_string_is_indefinite(item));89return ((struct cbor_indefinite_string_data *)item->data)->chunk_count;90}9192bool cbor_string_add_chunk(cbor_item_t *item, cbor_item_t *chunk) {93CBOR_ASSERT(cbor_isa_string(item));94CBOR_ASSERT(cbor_string_is_indefinite(item));95struct cbor_indefinite_string_data *data =96(struct cbor_indefinite_string_data *)item->data;97if (data->chunk_count == data->chunk_capacity) {98if (!_cbor_safe_to_multiply(CBOR_BUFFER_GROWTH, data->chunk_capacity)) {99return false;100}101102size_t new_chunk_capacity =103data->chunk_capacity == 0 ? 1104: CBOR_BUFFER_GROWTH * (data->chunk_capacity);105cbor_item_t **new_chunks_data = _cbor_realloc_multiple(106data->chunks, sizeof(cbor_item_t *), new_chunk_capacity);107108if (new_chunks_data == NULL) {109return false;110}111112data->chunk_capacity = new_chunk_capacity;113data->chunks = new_chunks_data;114}115data->chunks[data->chunk_count++] = cbor_incref(chunk);116return true;117}118119size_t cbor_string_length(const cbor_item_t *item) {120CBOR_ASSERT(cbor_isa_string(item));121return item->metadata.string_metadata.length;122}123124unsigned char *cbor_string_handle(const cbor_item_t *item) {125CBOR_ASSERT(cbor_isa_string(item));126return item->data;127}128129size_t cbor_string_codepoint_count(const cbor_item_t *item) {130CBOR_ASSERT(cbor_isa_string(item));131return item->metadata.string_metadata.codepoint_count;132}133134bool cbor_string_is_definite(const cbor_item_t *item) {135CBOR_ASSERT(cbor_isa_string(item));136return item->metadata.string_metadata.type == _CBOR_METADATA_DEFINITE;137}138139bool cbor_string_is_indefinite(const cbor_item_t *item) {140return !cbor_string_is_definite(item);141}142143144