diff options
author | lexborisov <lex.borisov@gmail.com> | 2016-12-14 22:59:03 +0300 |
---|---|---|
committer | lexborisov <lex.borisov@gmail.com> | 2016-12-14 22:59:03 +0300 |
commit | fa03d22cb80b234b60c00522f5e47341bacb0903 (patch) | |
tree | 1c2f65e9b097cbf108457c8e6d75424e3f2024b4 /source/myhtml | |
parent | 33918eafb8a9d80b81413a8c0ad51fc48b6f1743 (diff) |
Fixes after check by the Coverity Scan
Diffstat (limited to 'source/myhtml')
-rw-r--r-- | source/myhtml/Makefile | 53 | ||||
-rwxr-xr-x[-rw-r--r--] | source/myhtml/api.h | 65 | ||||
-rw-r--r-- | source/myhtml/encoding.c | 2 | ||||
-rw-r--r-- | source/myhtml/myhtml.c | 29 | ||||
-rw-r--r-- | source/myhtml/myosi.h | 2 | ||||
-rwxr-xr-x[-rw-r--r--] | source/myhtml/serialization.c | 24 | ||||
-rw-r--r-- | source/myhtml/tree.c | 16 | ||||
-rw-r--r-- | source/myhtml/utils.c | 50 | ||||
-rw-r--r-- | source/myhtml/utils/mcobject_async.c | 13 | ||||
-rw-r--r-- | source/myhtml/utils/mctree.h | 4 |
10 files changed, 177 insertions, 81 deletions
diff --git a/source/myhtml/Makefile b/source/myhtml/Makefile new file mode 100644 index 0000000..f24973c --- /dev/null +++ b/source/myhtml/Makefile @@ -0,0 +1,53 @@ +TARGET := source/myhtml +SRCDIR := source/myhtml + +CC ?= gcc + +LIBPOSTFIX := .so +LIBNAME := myhtml +LIBSTATIC_POSTFIX := _static + +MyHTML_OPTIMIZATION_LEVEL ?= -O2 + +CFLAGS ?= -Wall -Werror +CFLAGS += $(MyHTML_OPTIMIZATION_LEVEL) -fPIC --std=c99 -I.. + +MyHTML_BUILD_WITHOUT_THREADS ?= NO +ifeq ($(MyHTML_BUILD_WITHOUT_THREADS),YES) + $(info Build without POSIX Threads) + CFLAGS += -DMyHTML_BUILD_WITHOUT_THREADS +else + $(info Build with POSIX Threads) + CFLAGS += -pthread +endif + +ifeq ($(OS),Windows_NT) +else + UNAM := $(shell uname -s) + ifeq ($(UNAM),Darwin) + LIBPOSTFIX := .dylib + else + CFLAGS += -D_POSIX_C_SOURCE=199309L + endif +endif + +SRCS := $(wildcard *.c) +SRCS += $(wildcard utils/*.c) +HDRS := $(wildcard *.h) +HDRS += $(wildcard utils/*.h) +OBJS := $(patsubst %.c, %.o, $(SRCS)) + +all: shared static + +shared: $(OBJS) $(HDRS) + $(CC) -shared $(LDFLAGS) $(OBJS) -o lib$(LIBNAME)$(LIBPOSTFIX) + +static: shared + $(AR) crus lib$(LIBNAME)$(LIBSTATIC_POSTFIX).a $(OBJS) + +clean: + rm -rf *.o + rm -rf utils/*.o + rm -rf *lib$(LIBNAME)* + +.PHONY: all clean diff --git a/source/myhtml/api.h b/source/myhtml/api.h index ad94e84..794b9c7 100644..100755 --- a/source/myhtml/api.h +++ b/source/myhtml/api.h @@ -36,7 +36,7 @@ #define MyHTML_VERSION_MAJOR 1 #define MyHTML_VERSION_MINOR 0 -#define MyHTML_VERSION_PATCH 4 +#define MyHTML_VERSION_PATCH 5 #include <stdbool.h> #include <stddef.h> @@ -577,6 +577,7 @@ typedef myhtml_version_t; // callback functions typedef void* (*myhtml_callback_token_f)(myhtml_tree_t* tree, myhtml_token_node_t* token, void* ctx); typedef void (*myhtml_callback_tree_node_f)(myhtml_tree_t* tree, myhtml_tree_node_t* node, void* ctx); +typedef void (*myhtml_callback_serialize_f)(const char* buffer, size_t size, void* ctx); /*********************************************************************************** * @@ -2714,28 +2715,74 @@ myhtml_strncasecmp(const char* str1, const char* str2, size_t size); ***********************************************************************************/ /** - * Tree fragment serialization + * Tree fragment serialization + * The same as myhtml_serialization_tree_buffer function + */ +bool +myhtml_serialization(myhtml_tree_t* tree, myhtml_tree_node_t* scope_node, + myhtml_string_raw_t* str); + +/** + * Only one tree node serialization + * The same as myhtml_serialization_node_buffer function + */ +bool +myhtml_serialization_node(myhtml_tree_t* tree, myhtml_tree_node_t* node, + myhtml_string_raw_t* str); + +/** + * Serialize tree to an output string * * @param[in] myhtml_tree_t* - * @param[in] scope node, myhtml_tree_node_t* - * @param[in] myhtml_string_raw_t* (date to be created if str_raw.data == NULL) + * @param[in] scope node + * @param[in] myhtml_string_raw_t* * * @return true if successful, otherwise false */ bool -myhtml_serialization(myhtml_tree_t* tree, myhtml_tree_node_t* scope_node, myhtml_string_raw_t* str); +myhtml_serialization_tree_buffer(myhtml_tree_t* tree, myhtml_tree_node_t* scope_node, + myhtml_string_raw_t* str); /** - * Only one tree node serialization + * Serialize node to an output string * * @param[in] myhtml_tree_t* - * @param[in] myhtml_tree_node_t* - * @param[in] myhtml_string_raw_t* (date to be created if str_raw.data == NULL) + * @param[in] node + * @param[in] myhtml_string_raw_t* + * + * @return true if successful, otherwise false + */ +bool +myhtml_serialization_node_buffer(myhtml_tree_t* tree, myhtml_tree_node_t* node, + myhtml_string_raw_t* str); + +/** + * The serialize function for an entire tree + * + * @param[in] tree the tree to be serialized + * @param[in] scope_node the scope_node + * @param[in] callback function that will be called for all strings that have to be printed + * @param[in] ptr user-supplied pointer + * + * @return true if successful, otherwise false + */ +bool +myhtml_serialization_tree_callback(myhtml_tree_t* tree, myhtml_tree_node_t* scope_node, + myhtml_callback_serialize_f callback, void* ptr); + +/** + * The serialize function for a single node + * + * @param[in] tree the tree to be serialized + * @param[in] node the node that is going to be serialized + * @param[in] callback function that will be called for all strings that have to be printed + * @param[in] ptr user-supplied pointer * * @return true if successful, otherwise false */ bool -myhtml_serialization_node(myhtml_tree_t* tree, myhtml_tree_node_t* node, myhtml_string_raw_t* str); +myhtml_serialization_node_callback(myhtml_tree_t* tree, myhtml_tree_node_t* node, + myhtml_callback_serialize_f callback, void* ptr); /*********************************************************************************** * diff --git a/source/myhtml/encoding.c b/source/myhtml/encoding.c index 1c7560a..078ac5c 100644 --- a/source/myhtml/encoding.c +++ b/source/myhtml/encoding.c @@ -398,7 +398,7 @@ unsigned long myhtml_encoding_index_gb18030_ranges_code_point(unsigned long poin return 0xe7c7; // step 3 - unsigned long offset = sizeof(myhtml_encoding_map_gb18030_ranges) / sizeof(unsigned long); + unsigned long offset = sizeof(myhtml_encoding_map_gb18030_ranges) / (sizeof(unsigned long) * 2); unsigned long code_point_offset = 0; while (offset) { diff --git a/source/myhtml/myhtml.c b/source/myhtml/myhtml.c index 7edeaf1..6b3bbd2 100644 --- a/source/myhtml/myhtml.c +++ b/source/myhtml/myhtml.c @@ -1434,35 +1434,50 @@ void myhtml_queue_add(myhtml_tree_t *tree, size_t begin, myhtml_token_node_t* to } } - qnode->token = token; - #ifndef MyHTML_BUILD_WITHOUT_THREADS if(tree->flags & MyHTML_TREE_FLAGS_SINGLE_MODE) { - myhtml_parser_worker(0, qnode); - myhtml_parser_stream(0, qnode); + if(qnode) { + qnode->token = token; + + myhtml_parser_worker(0, qnode); + myhtml_parser_stream(0, qnode); + } tree->current_qnode = mythread_queue_node_malloc_limit(tree->myhtml->thread, tree->queue, 4, NULL); } else { + if(qnode) + qnode->token = token; + tree->current_qnode = mythread_queue_node_malloc_round(tree->myhtml->thread, tree->queue_entry, NULL); } #else - myhtml_parser_worker(0, qnode); - myhtml_parser_stream(0, qnode); + f(qnode) { + qnode->token = token; + + myhtml_parser_worker(0, qnode); + myhtml_parser_stream(0, qnode); + } tree->current_qnode = mythread_queue_node_malloc_limit(tree->myhtml->thread, tree->queue, 4, NULL); #endif /* MyHTML_BUILD_WITHOUT_THREADS */ + if(tree->current_qnode == NULL) { + // TODO: add return status + return; + } + tree->current_qnode->tree = tree; tree->current_qnode->prev = qnode; - if(qnode) + if(qnode && token) myhtml_tokenizer_calc_current_namespace(tree, token); + // TODO: add check created node myhtml_token_node_malloc(tree->token, tree->current_token_node, tree->token->mcasync_token_id); tree->current_token_node->raw_begin = tree->current_token_node->element_begin = (tree->global_offset + begin); diff --git a/source/myhtml/myosi.h b/source/myhtml/myosi.h index 530dd72..4c516a8 100644 --- a/source/myhtml/myosi.h +++ b/source/myhtml/myosi.h @@ -31,7 +31,7 @@ #define MyHTML_VERSION_MAJOR 1 #define MyHTML_VERSION_MINOR 0 -#define MyHTML_VERSION_PATCH 4 +#define MyHTML_VERSION_PATCH 5 #if (defined(_WIN32) || defined(_WIN64)) && !defined(__WINPTHREADS_VERSION) #define IS_OS_WINDOWS diff --git a/source/myhtml/serialization.c b/source/myhtml/serialization.c index ef76b94..9685cd0 100644..100755 --- a/source/myhtml/serialization.c +++ b/source/myhtml/serialization.c @@ -269,9 +269,15 @@ void myhtml_serialization_append(const char *data, size_t size, myhtml_callback_ notwritten = 0; break; case 0xA0: - if (notwritten) callback(data + i - notwritten, notwritten, ptr); - callback(" ", 6, ptr); - notwritten = 0; + if(i > 0 && (unsigned char)(data[(i - 1)]) == 0xC2) { + if (notwritten) callback(data + i - notwritten, (notwritten - 1), ptr); + callback(" ", 6, ptr); + notwritten = 0; + } + else { + ++notwritten; + } + break; default: ++notwritten; @@ -308,9 +314,15 @@ void myhtml_serialization_append_attr(const char* data, size_t size, myhtml_call notwritten = 0; break; case 0xA0: - if (notwritten) callback(data + i - notwritten, notwritten, ptr); - callback(" ", 6, ptr); - notwritten = 0; + if(i > 0 && (unsigned char)(data[(i - 1)]) == 0xC2) { + if (notwritten) callback(data + i - notwritten, (notwritten - 1), ptr); + callback(" ", 6, ptr); + notwritten = 0; + } + else { + ++notwritten; + } + break; default: ++notwritten; diff --git a/source/myhtml/tree.c b/source/myhtml/tree.c index cd5e929..6bf7772 100644 --- a/source/myhtml/tree.c +++ b/source/myhtml/tree.c @@ -71,14 +71,6 @@ myhtml_status_t myhtml_tree_init(myhtml_tree_t* tree, myhtml_t* myhtml) if(mcstatus) return MyHTML_STATUS_TREE_ERROR_MCOBJECT_INIT; - if(status) { - myhtml->parse_state_func = NULL; - myhtml->insertion_func = NULL; - myhtml->thread = NULL; - - return status; - } - tree->mchar = mchar_async_create(128, (4096 * 5)); tree->active_formatting = myhtml_tree_active_formatting_init(tree); tree->open_elements = myhtml_tree_open_elements_init(tree); @@ -880,7 +872,7 @@ void myhtml_tree_list_insert_by_index(myhtml_tree_list_t* list, myhtml_tree_node myhtml_tree_node_t** node_list = list->list; - memmove(&node_list[(index + 1)], &node_list[index], sizeof(myhtml_tree_node_t**) * (list->length - index)); + memmove(&node_list[(index + 1)], &node_list[index], sizeof(myhtml_tree_node_t*) * (list->length - index)); list->list[index] = node; list->length++; @@ -961,7 +953,7 @@ void myhtml_tree_open_elements_remove(myhtml_tree_t* tree, myhtml_tree_node_t* n if(list[el_idx] == node) { - memmove(&list[el_idx], &list[el_idx + 1], sizeof(myhtml_tree_node_t**) * (tree->open_elements->length - el_idx)); + memmove(&list[el_idx], &list[el_idx + 1], sizeof(myhtml_tree_node_t*) * (tree->open_elements->length - el_idx)); tree->open_elements->length--; break; @@ -1451,7 +1443,7 @@ void myhtml_tree_active_formatting_remove(myhtml_tree_t* tree, myhtml_tree_node_ if(list[el_idx] == node) { - memmove(&list[el_idx], &list[el_idx + 1], sizeof(myhtml_tree_node_t**) * (tree->active_formatting->length - el_idx)); + memmove(&list[el_idx], &list[el_idx + 1], sizeof(myhtml_tree_node_t*) * (tree->active_formatting->length - el_idx)); tree->active_formatting->length--; break; @@ -1469,7 +1461,7 @@ void myhtml_tree_active_formatting_remove_by_index(myhtml_tree_t* tree, size_t i { myhtml_tree_node_t** list = tree->active_formatting->list; - memmove(&list[idx], &list[idx + 1], sizeof(myhtml_tree_node_t**) * (tree->active_formatting->length - idx)); + memmove(&list[idx], &list[idx + 1], sizeof(myhtml_tree_node_t*) * (tree->active_formatting->length - idx)); tree->active_formatting->length--; #ifdef DEBUG_MODE diff --git a/source/myhtml/utils.c b/source/myhtml/utils.c index 2134270..eb5c741 100644 --- a/source/myhtml/utils.c +++ b/source/myhtml/utils.c @@ -54,14 +54,8 @@ static const unsigned char myhtml_utils_chars_map[] = { size_t myhtml_strncasecmp(const char* str1, const char* str2, size_t size) { - if(str1 == NULL) { - if(str2 == NULL) - return 0; - - return 1; - } - else if(str2 == NULL) { - if(str1 == NULL) + if(str1 == NULL || str2 == NULL) { + if(str1 == str2) return 0; return 1; @@ -86,14 +80,8 @@ size_t myhtml_strncasecmp(const char* str1, const char* str2, size_t size) size_t myhtml_strcasecmp(const char* str1, const char* str2) { - if(str1 == NULL) { - if(str2 == NULL) - return 0; - - return 1; - } - else if(str2 == NULL) { - if(str1 == NULL) + if(str1 == NULL || str2 == NULL) { + if(str1 == str2) return 0; return 1; @@ -119,14 +107,8 @@ size_t myhtml_strcasecmp(const char* str1, const char* str2) size_t myhtml_strncmp(const char* str1, const char* str2, size_t size) { - if(str1 == NULL) { - if(str2 == NULL) - return 0; - - return 1; - } - else if(str2 == NULL) { - if(str1 == NULL) + if(str1 == NULL || str2 == NULL) { + if(str1 == str2) return 0; return 1; @@ -151,14 +133,8 @@ size_t myhtml_strncmp(const char* str1, const char* str2, size_t size) size_t myhtml_strcmp(const char* str1, const char* str2) { - if(str1 == NULL) { - if(str2 == NULL) - return 0; - - return 1; - } - else if(str2 == NULL) { - if(str1 == NULL) + if(str1 == NULL || str2 == NULL) { + if(str1 == str2) return 0; return 1; @@ -184,14 +160,8 @@ size_t myhtml_strcmp(const char* str1, const char* str2) size_t myhtml_strcmp_ws(const char* str1, const char* str2) { - if(str1 == NULL) { - if(str2 == NULL) - return 0; - - return 1; - } - else if(str2 == NULL) { - if(str1 == NULL) + if(str1 == NULL || str2 == NULL) { + if(str1 == str2) return 0; return 1; diff --git a/source/myhtml/utils/mcobject_async.c b/source/myhtml/utils/mcobject_async.c index c62b4d8..21e36e8 100644 --- a/source/myhtml/utils/mcobject_async.c +++ b/source/myhtml/utils/mcobject_async.c @@ -354,13 +354,20 @@ void mcobject_async_node_delete(mcobject_async_t *mcobj_async, size_t node_idx) while (chunk) { if(mcobj_async->chunk_cache_length >= mcobj_async->chunk_cache_size) { - mcobj_async->chunk_cache_size <<= 1; + size_t new_size = mcobj_async->chunk_cache_size << 1; mcobject_async_chunk_t **tmp = (mcobject_async_chunk_t**)myhtml_realloc(mcobj_async->chunk_cache, - sizeof(mcobject_async_chunk_t*) * mcobj_async->chunk_cache_size); + sizeof(mcobject_async_chunk_t*) * new_size); - if(tmp) + if(tmp) { + mcobj_async->chunk_cache_size = new_size; mcobj_async->chunk_cache = tmp; + } + else { + // TODO: add return status + mcsync_unlock(mcobj_async->mcsync); + return; + } } mcobj_async->chunk_cache[ mcobj_async->chunk_cache_length ] = chunk; diff --git a/source/myhtml/utils/mctree.h b/source/myhtml/utils/mctree.h index e3c2b42..bd3054f 100644 --- a/source/myhtml/utils/mctree.h +++ b/source/myhtml/utils/mctree.h @@ -45,8 +45,8 @@ extern "C" { #define mctree_node_add(mctree) \ mctree->nodes_length++; \ - if(mctree->nodes_length == mctree->nodes_size) { \ - mctree->nodes_size += 4096; \ + if(mctree->nodes_length >= mctree->nodes_size) { \ + mctree->nodes_size = mctree->nodes_length + 4096; \ mctree->nodes = (mctree_node_t*)myhtml_realloc(mctree->nodes, \ sizeof(mctree_node_t) * mctree->nodes_size); \ } \ |