From c911e393d5729aca2b0d57fa3184178d6e66bfca Mon Sep 17 00:00:00 2001 From: Alex Baines Date: Sat, 8 May 2021 23:30:59 +0100 Subject: [PATCH] hmmlib2: v2.0.2 fixing issues with code204/284/440 also allow / in markers --- hmmlib2/hmmlib.h | 63 ++++++++++++++++++++++++------------------------ 1 file changed, 32 insertions(+), 31 deletions(-) diff --git a/hmmlib2/hmmlib.h b/hmmlib2/hmmlib.h index b3079eb..9594dae 100644 --- a/hmmlib2/hmmlib.h +++ b/hmmlib2/hmmlib.h @@ -298,7 +298,7 @@ static char* _hmml_read_attr(struct _hmml_parser* p, char* mem, size_t mem_size, p->cursor = src+1; } else { const char* breaks = break_on_punct - ? " ]\r\n\t:,'-./#=[" + ? " ]\r\n\t:,'-.#=[\\" : " ]\r\n\t" ; @@ -511,6 +511,25 @@ static void _hmml_parse_timecode(struct _hmml_parser* p, HMML_Annotation* anno) anno->s = s; } +static void _hmml_store_marker(struct _hmml_parser* p, HMML_Annotation* anno, char** out, char* text_mem, size_t text_mem_size) +{ + HMML_Marker m = _hmml_parse_marker(p); + m.offset = (*out) - text_mem; + _hmml_persist_array(p, &anno->markers, &anno->marker_count, m); + + const char* marker_text = m.parameter + ? m.parameter + : m.marker + ; + + size_t text_len = strlen(marker_text); + if((*out) + text_len > text_mem + text_mem_size) {\ + _hmml_err(p, "Not enough text memory");\ + } + memcpy(*out, marker_text, text_len); + *out += text_len; +} + static size_t _hmml_parse_text(struct _hmml_parser* p, HMML_Annotation* anno) { static char text_mem[4096]; @@ -518,16 +537,13 @@ static size_t _hmml_parse_text(struct _hmml_parser* p, HMML_Annotation* anno) memset(text_mem, 0, sizeof(text_mem)); -#define CHECKSIZE(__n) \ - if(out + (__n) > text_mem + sizeof(text_mem)) {\ - _hmml_err(p, "Not enough text memory");\ - } - for(;;) { - size_t n = strcspn(p->cursor, "\\\n[]:@~"); + size_t n = strcspn(p->cursor, "\\\n\r[]:@~"); char c = p->cursor[n]; - CHECKSIZE(n); + if(out + n > text_mem + sizeof(text_mem)) {\ + _hmml_err(p, "Not enough text memory");\ + } memcpy(out, p->cursor, n); p->cursor += n; @@ -553,7 +569,7 @@ static size_t _hmml_parse_text(struct _hmml_parser* p, HMML_Annotation* anno) } } - else if(c == '\n') { + else if(c == '\n' || c == '\r') { ++p->cursor; } @@ -564,32 +580,18 @@ static size_t _hmml_parse_text(struct _hmml_parser* p, HMML_Annotation* anno) ref.offset = out - text_mem; _hmml_persist_array(p, &anno->references, &anno->reference_count, ref); } else { - HMML_Marker m = _hmml_parse_marker(p); - m.offset = out - text_mem; - _hmml_persist_array(p, &anno->markers, &anno->marker_count, m); - - size_t text_len = strlen(m.marker); - CHECKSIZE(text_len); - memcpy(out, m.marker, text_len); - out += text_len; + _hmml_store_marker(p, anno, &out, text_mem, sizeof(text_mem)); } } // it is a @ ~ or : marker without parameters else { - // if next char is a space, it can't be a marker - if(strchr(" \t\r\n", p->cursor[1])) { + // if next char is a space, or prev char is not a space, then it can't be a marker + if(strchr(" \t\r\n", p->cursor[1]) || !strchr(" \t\r\n", p->cursor[-1])) { *out++ = c; ++p->cursor; } else { - HMML_Marker m = _hmml_parse_marker(p); - m.offset = out - text_mem; - _hmml_persist_array(p, &anno->markers, &anno->marker_count, m); - - size_t text_len = strlen(m.marker); - CHECKSIZE(text_len); - memcpy(out, m.marker, text_len); - out += text_len; + _hmml_store_marker(p, anno, &out, text_mem, sizeof(text_mem)); } } @@ -607,8 +609,6 @@ static size_t _hmml_parse_text(struct _hmml_parser* p, HMML_Annotation* anno) size_t text_size = out - text_mem; anno->text = _hmml_persist_str(p, (struct _hmml_str){ text_mem, text_size }); -#undef CHECKSIZE - return text_size; } @@ -765,11 +765,12 @@ HMML_Output hmml_parse(const char* string) if(setjmp(p.err_buf) == 1) { // if it returns 1, an error happened + p.out.free_list = p.free_list; return p.out; } static const struct _hmml_str prefix = HSTR("[video"); - if(strncasecmp(p.cursor, prefix.ptr, prefix.len)) { + if(strncmp(p.cursor, prefix.ptr, prefix.len)) { _hmml_err(&p, "Missing initial video tag."); } else { p.cursor += prefix.len; @@ -795,7 +796,7 @@ void hmml_free(HMML_Output* out) } const struct HMML_Version hmml_version = { - 2, 0, 1 + 2, 0, 2 }; #undef HSTX