hmmlib2: v2.0.2 fixing issues with code204/284/440

also allow / in markers
This commit is contained in:
Alex Baines 2021-05-08 23:30:59 +01:00
parent 05b7883033
commit c911e393d5
1 changed files with 32 additions and 31 deletions

View File

@ -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