From aafeedbbac97a90d86ee2665b4a081fac1234198 Mon Sep 17 00:00:00 2001 From: Ethan Lee Date: Wed, 1 Apr 2020 17:11:09 -0400 Subject: [PATCH] Move buffer_find to assembler.c, it isn't actually common --- mojoshader_assembler.c | 96 +++++++++++++++++++++++++++++++++++ mojoshader_common.c | 112 ----------------------------------------- mojoshader_internal.h | 19 +++++-- 3 files changed, 112 insertions(+), 115 deletions(-) diff --git a/mojoshader_assembler.c b/mojoshader_assembler.c index c9148410..abb85821 100644 --- a/mojoshader_assembler.c +++ b/mojoshader_assembler.c @@ -1492,6 +1492,102 @@ static const MOJOSHADER_parseData *build_failed_assembly(Context *ctx) } // build_failed_assembly +static int blockscmp(BufferBlock *item, const uint8 *data, size_t len) +{ + if (len == 0) + return 1; // "match" + + while (item != NULL) + { + const size_t itemremain = item->bytes; + const size_t avail = len < itemremain ? len : itemremain; + if (memcmp(item->data, data, avail) != 0) + return 0; // not a match. + + if (len == avail) + return 1; // complete match! + + len -= avail; + data += avail; + item = item->next; + } // while + + return 0; // not a complete match. +} // blockscmp + + +ssize_t buffer_find(Buffer *buffer, const size_t start, + const void *_data, const size_t len) +{ + if (len == 0) + return 0; // I guess that's right. + + if (start >= buffer->total_bytes) + return -1; // definitely can't match. + + if (len > (buffer->total_bytes - start)) + return -1; // definitely can't match. + + // Find the start point somewhere in the center of a buffer. + BufferBlock *item = buffer->head; + const uint8 *ptr = item->data; + size_t pos = 0; + if (start > 0) + { + while (1) + { + assert(item != NULL); + if ((pos + item->bytes) > start) // start is in this block. + { + ptr = item->data + (start - pos); + break; + } // if + + pos += item->bytes; + item = item->next; + } // while + } // if + + // okay, we're at the origin of the search. + assert(item != NULL); + assert(ptr != NULL); + + const uint8 *data = (const uint8 *) _data; + const uint8 first = *data; + while (item != NULL) + { + const size_t itemremain = item->bytes - ((size_t)(ptr-item->data)); + ptr = (uint8 *) memchr(ptr, first, itemremain); + while (ptr != NULL) + { + const size_t retval = pos + ((size_t) (ptr - item->data)); + if (len == 1) + return retval; // we're done, here it is! + + const size_t itemremain = item->bytes - ((size_t)(ptr-item->data)); + const size_t avail = len < itemremain ? len : itemremain; + if ((avail == 0) || (memcmp(ptr, data, avail) == 0)) + { + // okay, we've got a (sub)string match! Move to the next block. + // check all blocks until we get a complete match or a failure. + if (blockscmp(item->next, data+avail, len-avail)) + return (ssize_t) retval; + } // if + + // try again, further in this block. + ptr = (uint8 *) memchr(ptr + 1, first, itemremain - 1); + } // while + + pos += item->bytes; + item = item->next; + if (item != NULL) + ptr = item->data; + } // while + + return -1; // no match found. +} // buffer_find + + static uint32 add_ctab_bytes(Context *ctx, const uint8 *bytes, const size_t len) { if (isfail(ctx)) diff --git a/mojoshader_common.c b/mojoshader_common.c index 9c84697d..25a3cc71 100644 --- a/mojoshader_common.c +++ b/mojoshader_common.c @@ -674,24 +674,6 @@ void errorlist_destroy(ErrorList *list) } // errorlist_destroy -typedef struct BufferBlock -{ - uint8 *data; - size_t bytes; - struct BufferBlock *next; -} BufferBlock; - -struct Buffer -{ - size_t total_bytes; - BufferBlock *head; - BufferBlock *tail; - size_t block_size; - MOJOSHADER_malloc m; - MOJOSHADER_free f; - void *d; -}; - Buffer *buffer_create(size_t blksz, MOJOSHADER_malloc m, MOJOSHADER_free f, void *d) { @@ -942,100 +924,6 @@ void buffer_destroy(Buffer *buffer) } // if } // buffer_destroy -static int blockscmp(BufferBlock *item, const uint8 *data, size_t len) -{ - if (len == 0) - return 1; // "match" - - while (item != NULL) - { - const size_t itemremain = item->bytes; - const size_t avail = len < itemremain ? len : itemremain; - if (memcmp(item->data, data, avail) != 0) - return 0; // not a match. - - if (len == avail) - return 1; // complete match! - - len -= avail; - data += avail; - item = item->next; - } // while - - return 0; // not a complete match. -} // blockscmp - -ssize_t buffer_find(Buffer *buffer, const size_t start, - const void *_data, const size_t len) -{ - if (len == 0) - return 0; // I guess that's right. - - if (start >= buffer->total_bytes) - return -1; // definitely can't match. - - if (len > (buffer->total_bytes - start)) - return -1; // definitely can't match. - - // Find the start point somewhere in the center of a buffer. - BufferBlock *item = buffer->head; - const uint8 *ptr = item->data; - size_t pos = 0; - if (start > 0) - { - while (1) - { - assert(item != NULL); - if ((pos + item->bytes) > start) // start is in this block. - { - ptr = item->data + (start - pos); - break; - } // if - - pos += item->bytes; - item = item->next; - } // while - } // if - - // okay, we're at the origin of the search. - assert(item != NULL); - assert(ptr != NULL); - - const uint8 *data = (const uint8 *) _data; - const uint8 first = *data; - while (item != NULL) - { - const size_t itemremain = item->bytes - ((size_t)(ptr-item->data)); - ptr = (uint8 *) memchr(ptr, first, itemremain); - while (ptr != NULL) - { - const size_t retval = pos + ((size_t) (ptr - item->data)); - if (len == 1) - return retval; // we're done, here it is! - - const size_t itemremain = item->bytes - ((size_t)(ptr-item->data)); - const size_t avail = len < itemremain ? len : itemremain; - if ((avail == 0) || (memcmp(ptr, data, avail) == 0)) - { - // okay, we've got a (sub)string match! Move to the next block. - // check all blocks until we get a complete match or a failure. - if (blockscmp(item->next, data+avail, len-avail)) - return (ssize_t) retval; - } // if - - // try again, further in this block. - ptr = (uint8 *) memchr(ptr + 1, first, itemremain - 1); - } // while - - pos += item->bytes; - item = item->next; - if (item != NULL) - ptr = item->data; - } // while - - return -1; // no match found. -} // buffer_find - void buffer_patch(Buffer *buffer, const size_t start, const void *_data, const size_t len) { diff --git a/mojoshader_internal.h b/mojoshader_internal.h index 4d761ae1..d46e85a1 100644 --- a/mojoshader_internal.h +++ b/mojoshader_internal.h @@ -264,7 +264,22 @@ void errorlist_destroy(ErrorList *list); // Dynamic buffers... -typedef struct Buffer Buffer; +typedef struct BufferBlock +{ + uint8 *data; + size_t bytes; + struct BufferBlock *next; +} BufferBlock; +typedef struct Buffer +{ + size_t total_bytes; + BufferBlock *head; + BufferBlock *tail; + size_t block_size; + MOJOSHADER_malloc m; + MOJOSHADER_free f; + void *d; +} Buffer; Buffer *buffer_create(size_t blksz,MOJOSHADER_malloc m,MOJOSHADER_free f,void *d); char *buffer_reserve(Buffer *buffer, const size_t len); int buffer_append(Buffer *buffer, const void *_data, size_t len); @@ -275,8 +290,6 @@ void buffer_empty(Buffer *buffer); char *buffer_flatten(Buffer *buffer); char *buffer_merge(Buffer **buffers, const size_t n, size_t *_len); void buffer_destroy(Buffer *buffer); -ssize_t buffer_find(Buffer *buffer, const size_t start, - const void *data, const size_t len); void buffer_patch(Buffer *buffer, const size_t start, const void *data, const size_t len);