md2teach/md2teach/md4c.c

6382 lines
218 KiB
C

/*
* MD4C: Markdown parser for C
* (http://github.com/mity/md4c)
*
* Copyright (c) 2016-2020 Martin Mitas
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "md4c.h"
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// GS_SPECIFIC - This is only required if > 64K arrays are created statically or
// dynamically. I think with large documents, it is very likely that we need to
// have a memory allocation greater than 64K. So, we take this penalty here.
#pragma memorymodel 1
// GS_SPECIFIC - There is > 64K of code here so it must be split into multiple
// segments.
segment "md4c1";
/*****************************
*** Miscellaneous Stuff ***
*****************************/
#if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199409L
/* C89/90 or old compilers in general may not understand "inline". */
#if defined __GNUC__
#define inline __inline__
#elif defined _MSC_VER
#define inline __inline
#else
#define inline
#endif
#endif
// GS_SPECIFIC - Force ascii only mode.
#define MD4C_USE_ASCII
/* Make the UTF-8 support the default. */
#if !defined MD4C_USE_ASCII && !defined MD4C_USE_UTF8 && !defined MD4C_USE_UTF16
#define MD4C_USE_UTF8
#endif
/* Magic for making wide literals with MD4C_USE_UTF16. */
#ifdef _T
#undef _T
#endif
#if defined MD4C_USE_UTF16
#define _T(x) L##x
#else
#define _T(x) x
#endif
/* Misc. macros. */
#define SIZEOF_ARRAY(a) (sizeof(a) / sizeof(a[0]))
#define STRINGIZE_(x) #x
#define STRINGIZE(x) STRINGIZE_(x)
#ifndef TRUE
#define TRUE 1
#define FALSE 0
#endif
#define MD_LOG(msg) \
do { \
if(ctx->parser.debug_log != NULL) \
ctx->parser.debug_log((msg), ctx->userdata); \
} while(0)
#ifdef DEBUG
#define MD_ASSERT(cond) \
do { \
if(!(cond)) { \
MD_LOG(__FILE__ ":" STRINGIZE(__LINE__) ": " \
"Assertion '" STRINGIZE(cond) "' failed."); \
exit(1); \
} \
} while(0)
#define MD_UNREACHABLE() MD_ASSERT(1 == 0)
#else
#ifdef __GNUC__
#define MD_ASSERT(cond) do { if(!(cond)) __builtin_unreachable(); } while(0)
#define MD_UNREACHABLE() do { __builtin_unreachable(); } while(0)
#elif defined _MSC_VER && _MSC_VER > 120
#define MD_ASSERT(cond) do { __assume(cond); } while(0)
#define MD_UNREACHABLE() do { __assume(0); } while(0)
#else
#define MD_ASSERT(cond) do {} while(0)
#define MD_UNREACHABLE() do {} while(0)
#endif
#endif
/* For falling through case labels in switch statements. */
#if defined __clang__ && __clang_major__ >= 12
#define MD_FALLTHROUGH() __attribute__((fallthrough))
#elif defined __GNUC__ && __GNUC__ >= 7
#define MD_FALLTHROUGH() __attribute__((fallthrough))
#else
#define MD_FALLTHROUGH() ((void)0)
#endif
/* Suppress "unused parameter" warnings. */
#define MD_UNUSED(x) ((void)x)
/************************
*** Internal Types ***
************************/
/* These are omnipresent so lets save some typing. */
#define CHAR MD_CHAR
#define SZ MD_SIZE
#define OFF MD_OFFSET
typedef struct MD_MARK_tag MD_MARK;
typedef struct MD_BLOCK_tag MD_BLOCK;
typedef struct MD_CONTAINER_tag MD_CONTAINER;
typedef struct MD_REF_DEF_tag MD_REF_DEF;
/* During analyzes of inline marks, we need to manage some "mark chains",
* of (yet unresolved) openers. This structure holds start/end of the chain.
* The chain internals are then realized through MD_MARK::prev and ::next.
*/
typedef struct MD_MARKCHAIN_tag MD_MARKCHAIN;
struct MD_MARKCHAIN_tag {
int32_t head; /* Index of first mark in the chain, or -1 if empty. */
int32_t tail; /* Index of last mark in the chain, or -1 if empty. */
};
/* Context propagated through all the parsing. */
typedef struct MD_CTX_tag MD_CTX;
struct MD_CTX_tag {
/* Immutable stuff (parameters of md_parse()). */
const CHAR* text;
SZ size;
MD_PARSER parser;
void* userdata;
/* When this is true, it allows some optimizations. */
int doc_ends_with_newline;
/* Helper temporary growing buffer. */
CHAR* buffer;
uint32_t alloc_buffer;
/* Reference definitions. */
MD_REF_DEF* ref_defs;
int32_t n_ref_defs;
int32_t alloc_ref_defs;
void** ref_def_hashtable;
int32_t ref_def_hashtable_size;
/* Stack of inline/span markers.
* This is only used for parsing a single block contents but by storing it
* here we may reuse the stack for subsequent blocks; i.e. we have fewer
* (re)allocations. */
MD_MARK* marks;
int32_t n_marks;
int32_t alloc_marks;
#if defined MD4C_USE_UTF16
char mark_char_map[128];
#else
char mark_char_map[256];
#endif
/* For resolving of inline spans. */
MD_MARKCHAIN mark_chains[13];
#define PTR_CHAIN (ctx->mark_chains[0])
#define TABLECELLBOUNDARIES (ctx->mark_chains[1])
#define ASTERISK_OPENERS_extraword_mod3_0 (ctx->mark_chains[2])
#define ASTERISK_OPENERS_extraword_mod3_1 (ctx->mark_chains[3])
#define ASTERISK_OPENERS_extraword_mod3_2 (ctx->mark_chains[4])
#define ASTERISK_OPENERS_intraword_mod3_0 (ctx->mark_chains[5])
#define ASTERISK_OPENERS_intraword_mod3_1 (ctx->mark_chains[6])
#define ASTERISK_OPENERS_intraword_mod3_2 (ctx->mark_chains[7])
#define UNDERSCORE_OPENERS (ctx->mark_chains[8])
#define TILDE_OPENERS_1 (ctx->mark_chains[9])
#define TILDE_OPENERS_2 (ctx->mark_chains[10])
#define BRACKET_OPENERS (ctx->mark_chains[11])
#define DOLLAR_OPENERS (ctx->mark_chains[12])
#define OPENERS_CHAIN_FIRST 2
#define OPENERS_CHAIN_LAST 12
int n_table_cell_boundaries;
/* For resolving links. */
int32_t unresolved_link_head;
int32_t unresolved_link_tail;
/* For resolving raw HTML. */
OFF html_comment_horizon;
OFF html_proc_instr_horizon;
OFF html_decl_horizon;
OFF html_cdata_horizon;
/* For block analysis.
* Notes:
* -- It holds MD_BLOCK as well as MD_LINE structures. After each
* MD_BLOCK, its (multiple) MD_LINE(s) follow.
* -- For MD_BLOCK_HTML and MD_BLOCK_CODE, MD_VERBATIMLINE(s) are used
* instead of MD_LINE(s).
*/
void* block_bytes;
MD_BLOCK* current_block;
int32_t n_block_bytes;
int32_t alloc_block_bytes;
/* For container block analysis. */
MD_CONTAINER* containers;
int32_t n_containers;
int32_t alloc_containers;
/* Minimal indentation to call the block "indented code block". */
unsigned code_indent_offset;
/* Contextual info for line analysis. */
SZ code_fence_length; /* For checking closing fence length. */
int html_block_type; /* For checking closing raw HTML condition. */
int last_line_has_list_loosening_effect;
int last_list_item_starts_with_two_blank_lines;
};
enum MD_LINETYPE_tag {
MD_LINE_BLANK,
MD_LINE_HR,
MD_LINE_ATXHEADER,
MD_LINE_SETEXTHEADER,
MD_LINE_SETEXTUNDERLINE,
MD_LINE_INDENTEDCODE,
MD_LINE_FENCEDCODE,
MD_LINE_HTML,
MD_LINE_TEXT,
MD_LINE_TABLE,
MD_LINE_TABLEUNDERLINE
};
typedef enum MD_LINETYPE_tag MD_LINETYPE;
typedef struct MD_LINE_ANALYSIS_tag MD_LINE_ANALYSIS;
struct MD_LINE_ANALYSIS_tag {
MD_LINETYPE type : 16;
unsigned data : 16;
OFF beg;
OFF end;
unsigned indent; /* Indentation level. */
};
typedef struct MD_LINE_tag MD_LINE;
struct MD_LINE_tag {
OFF beg;
OFF end;
};
typedef struct MD_VERBATIMLINE_tag MD_VERBATIMLINE;
struct MD_VERBATIMLINE_tag {
OFF beg;
OFF end;
OFF indent;
};
/*****************
*** Helpers ***
*****************/
/* Character accessors. */
#define CH(off) (ctx->text[(off)])
#define STR(off) (ctx->text + (off))
/* Character classification.
* Note we assume ASCII compatibility of code points < 128 here. */
#define ISIN_(ch, ch_min, ch_max) ((ch_min) <= (unsigned)(ch) && (unsigned)(ch) <= (ch_max))
#define ISANYOF_(ch, palette) ((ch) != _T('\0') && md_strchr((palette), (ch)) != NULL)
#define ISANYOF2_(ch, ch1, ch2) ((ch) == (ch1) || (ch) == (ch2))
#define ISANYOF3_(ch, ch1, ch2, ch3) ((ch) == (ch1) || (ch) == (ch2) || (ch) == (ch3))
#define ISASCII_(ch) ((unsigned)(ch) <= 127)
#define ISBLANK_(ch) (ISANYOF2_((ch), _T(' '), _T('\t')))
#define ISNEWLINE_(ch) (ISANYOF2_((ch), _T('\r'), _T('\n')))
#define ISWHITESPACE_(ch) (ISBLANK_(ch) || ISANYOF2_((ch), _T('\v'), _T('\f')))
#define ISCNTRL_(ch) ((unsigned)(ch) <= 31 || (unsigned)(ch) == 127)
#define ISPUNCT_(ch) (ISIN_(ch, 33, 47) || ISIN_(ch, 58, 64) || ISIN_(ch, 91, 96) || ISIN_(ch, 123, 126))
#define ISUPPER_(ch) (ISIN_(ch, _T('A'), _T('Z')))
#define ISLOWER_(ch) (ISIN_(ch, _T('a'), _T('z')))
#define ISALPHA_(ch) (ISUPPER_(ch) || ISLOWER_(ch))
#define ISDIGIT_(ch) (ISIN_(ch, _T('0'), _T('9')))
#define ISXDIGIT_(ch) (ISDIGIT_(ch) || ISIN_(ch, _T('A'), _T('F')) || ISIN_(ch, _T('a'), _T('f')))
#define ISALNUM_(ch) (ISALPHA_(ch) || ISDIGIT_(ch))
#define ISANYOF(off, palette) ISANYOF_(CH(off), (palette))
#define ISANYOF2(off, ch1, ch2) ISANYOF2_(CH(off), (ch1), (ch2))
#define ISANYOF3(off, ch1, ch2, ch3) ISANYOF3_(CH(off), (ch1), (ch2), (ch3))
#define ISASCII(off) ISASCII_(CH(off))
#define ISBLANK(off) ISBLANK_(CH(off))
#define ISNEWLINE(off) ISNEWLINE_(CH(off))
#define ISWHITESPACE(off) ISWHITESPACE_(CH(off))
#define ISCNTRL(off) ISCNTRL_(CH(off))
#define ISPUNCT(off) ISPUNCT_(CH(off))
#define ISUPPER(off) ISUPPER_(CH(off))
#define ISLOWER(off) ISLOWER_(CH(off))
#define ISALPHA(off) ISALPHA_(CH(off))
#define ISDIGIT(off) ISDIGIT_(CH(off))
#define ISXDIGIT(off) ISXDIGIT_(CH(off))
#define ISALNUM(off) ISALNUM_(CH(off))
#if defined MD4C_USE_UTF16
#define md_strchr wcschr
#else
#define md_strchr strchr
#endif
/* Case insensitive check of string equality. */
static inline int
md_ascii_case_eq(const CHAR* s1, const CHAR* s2, SZ n)
{
OFF i;
for(i = 0; i < n; i++) {
CHAR ch1 = s1[i];
CHAR ch2 = s2[i];
if(ISLOWER_(ch1))
ch1 += ('A'-'a');
if(ISLOWER_(ch2))
ch2 += ('A'-'a');
if(ch1 != ch2)
return FALSE;
}
return TRUE;
}
static inline int
md_ascii_eq(const CHAR* s1, const CHAR* s2, SZ n)
{
return memcmp(s1, s2, n * sizeof(CHAR)) == 0;
}
static int
md_text_with_null_replacement(MD_CTX* ctx, MD_TEXTTYPE type, const CHAR* str, SZ size)
{
OFF off = 0;
int ret = 0;
while(1) {
while(off < size && str[off] != _T('\0'))
off++;
if(off > 0) {
ret = ctx->parser.text(type, str, off, ctx->userdata);
if(ret != 0)
return ret;
str += off;
size -= off;
off = 0;
}
if(off >= size)
return 0;
ret = ctx->parser.text(MD_TEXT_NULLCHAR, _T(""), 1, ctx->userdata);
if(ret != 0)
return ret;
off++;
}
}
#define MD_CHECK(func) \
do { \
ret = (func); \
if(ret < 0) \
goto abort; \
} while(0)
#define MD_TEMP_BUFFER(sz) \
do { \
if(sz > ctx->alloc_buffer) { \
CHAR* new_buffer; \
SZ new_size = ((sz) + (sz) / 2 + 128) & ~127; \
\
new_buffer = realloc(ctx->buffer, new_size); \
if(new_buffer == NULL) { \
MD_LOG("realloc() failed."); \
ret = -1; \
goto abort; \
} \
\
ctx->buffer = new_buffer; \
ctx->alloc_buffer = new_size; \
} \
} while(0)
#define MD_ENTER_BLOCK(type, arg) \
do { \
ret = ctx->parser.enter_block((type), (arg), ctx->userdata); \
if(ret != 0) { \
MD_LOG("Aborted from enter_block() callback."); \
goto abort; \
} \
} while(0)
#define MD_LEAVE_BLOCK(type, arg) \
do { \
ret = ctx->parser.leave_block((type), (arg), ctx->userdata); \
if(ret != 0) { \
MD_LOG("Aborted from leave_block() callback."); \
goto abort; \
} \
} while(0)
#define MD_ENTER_SPAN(type, arg) \
do { \
ret = ctx->parser.enter_span((type), (arg), ctx->userdata); \
if(ret != 0) { \
MD_LOG("Aborted from enter_span() callback."); \
goto abort; \
} \
} while(0)
#define MD_LEAVE_SPAN(type, arg) \
do { \
ret = ctx->parser.leave_span((type), (arg), ctx->userdata); \
if(ret != 0) { \
MD_LOG("Aborted from leave_span() callback."); \
goto abort; \
} \
} while(0)
#define MD_TEXT(type, str, size) \
do { \
if(size > 0) { \
ret = ctx->parser.text((type), (str), (size), ctx->userdata); \
if(ret != 0) { \
MD_LOG("Aborted from text() callback."); \
goto abort; \
} \
} \
} while(0)
#define MD_TEXT_INSECURE(type, str, size) \
do { \
if(size > 0) { \
ret = md_text_with_null_replacement(ctx, type, str, size); \
if(ret != 0) { \
MD_LOG("Aborted from text() callback."); \
goto abort; \
} \
} \
} while(0)
/*************************
*** Unicode Support ***
*************************/
typedef struct MD_UNICODE_FOLD_INFO_tag MD_UNICODE_FOLD_INFO;
struct MD_UNICODE_FOLD_INFO_tag {
unsigned codepoints[3];
unsigned n_codepoints;
};
#if defined MD4C_USE_UTF16 || defined MD4C_USE_UTF8
/* Binary search over sorted "map" of codepoints. Consecutive sequences
* of codepoints may be encoded in the map by just using the
* (MIN_CODEPOINT | 0x40000000) and (MAX_CODEPOINT | 0x80000000).
*
* Returns index of the found record in the map (in the case of ranges,
* the minimal value is used); or -1 on failure. */
static int
md_unicode_bsearch__(unsigned codepoint, const unsigned* map, size_t map_size)
{
int beg, end;
int pivot_beg, pivot_end;
beg = 0;
end = (int) map_size-1;
while(beg <= end) {
/* Pivot may be a range, not just a single value. */
pivot_beg = pivot_end = (beg + end) / 2;
if(map[pivot_end] & 0x40000000)
pivot_end++;
if(map[pivot_beg] & 0x80000000)
pivot_beg--;
if(codepoint < (map[pivot_beg] & 0x00ffffff))
end = pivot_beg - 1;
else if(codepoint > (map[pivot_end] & 0x00ffffff))
beg = pivot_end + 1;
else
return pivot_beg;
}
return -1;
}
static int
md_is_unicode_whitespace__(unsigned codepoint)
{
#define R(cp_min, cp_max) ((cp_min) | 0x40000000), ((cp_max) | 0x80000000)
#define S(cp) (cp)
/* Unicode "Zs" category.
* (generated by scripts/build_whitespace_map.py) */
static const unsigned WHITESPACE_MAP[] = {
S(0x0020), S(0x00a0), S(0x1680), R(0x2000,0x200a), S(0x202f), S(0x205f), S(0x3000)
};
#undef R
#undef S
/* The ASCII ones are the most frequently used ones, also CommonMark
* specification requests few more in this range. */
if(codepoint <= 0x7f)
return ISWHITESPACE_(codepoint);
return (md_unicode_bsearch__(codepoint, WHITESPACE_MAP, SIZEOF_ARRAY(WHITESPACE_MAP)) >= 0);
}
static int
md_is_unicode_punct__(unsigned codepoint)
{
#define R(cp_min, cp_max) ((cp_min) | 0x40000000), ((cp_max) | 0x80000000)
#define S(cp) (cp)
/* Unicode "Pc", "Pd", "Pe", "Pf", "Pi", "Po", "Ps" categories.
* (generated by scripts/build_punct_map.py) */
static const unsigned PUNCT_MAP[] = {
R(0x0021,0x0023), R(0x0025,0x002a), R(0x002c,0x002f), R(0x003a,0x003b), R(0x003f,0x0040),
R(0x005b,0x005d), S(0x005f), S(0x007b), S(0x007d), S(0x00a1), S(0x00a7), S(0x00ab), R(0x00b6,0x00b7),
S(0x00bb), S(0x00bf), S(0x037e), S(0x0387), R(0x055a,0x055f), R(0x0589,0x058a), S(0x05be), S(0x05c0),
S(0x05c3), S(0x05c6), R(0x05f3,0x05f4), R(0x0609,0x060a), R(0x060c,0x060d), S(0x061b), R(0x061e,0x061f),
R(0x066a,0x066d), S(0x06d4), R(0x0700,0x070d), R(0x07f7,0x07f9), R(0x0830,0x083e), S(0x085e),
R(0x0964,0x0965), S(0x0970), S(0x09fd), S(0x0a76), S(0x0af0), S(0x0c77), S(0x0c84), S(0x0df4), S(0x0e4f),
R(0x0e5a,0x0e5b), R(0x0f04,0x0f12), S(0x0f14), R(0x0f3a,0x0f3d), S(0x0f85), R(0x0fd0,0x0fd4),
R(0x0fd9,0x0fda), R(0x104a,0x104f), S(0x10fb), R(0x1360,0x1368), S(0x1400), S(0x166e), R(0x169b,0x169c),
R(0x16eb,0x16ed), R(0x1735,0x1736), R(0x17d4,0x17d6), R(0x17d8,0x17da), R(0x1800,0x180a),
R(0x1944,0x1945), R(0x1a1e,0x1a1f), R(0x1aa0,0x1aa6), R(0x1aa8,0x1aad), R(0x1b5a,0x1b60),
R(0x1bfc,0x1bff), R(0x1c3b,0x1c3f), R(0x1c7e,0x1c7f), R(0x1cc0,0x1cc7), S(0x1cd3), R(0x2010,0x2027),
R(0x2030,0x2043), R(0x2045,0x2051), R(0x2053,0x205e), R(0x207d,0x207e), R(0x208d,0x208e),
R(0x2308,0x230b), R(0x2329,0x232a), R(0x2768,0x2775), R(0x27c5,0x27c6), R(0x27e6,0x27ef),
R(0x2983,0x2998), R(0x29d8,0x29db), R(0x29fc,0x29fd), R(0x2cf9,0x2cfc), R(0x2cfe,0x2cff), S(0x2d70),
R(0x2e00,0x2e2e), R(0x2e30,0x2e4f), S(0x2e52), R(0x3001,0x3003), R(0x3008,0x3011), R(0x3014,0x301f),
S(0x3030), S(0x303d), S(0x30a0), S(0x30fb), R(0xa4fe,0xa4ff), R(0xa60d,0xa60f), S(0xa673), S(0xa67e),
R(0xa6f2,0xa6f7), R(0xa874,0xa877), R(0xa8ce,0xa8cf), R(0xa8f8,0xa8fa), S(0xa8fc), R(0xa92e,0xa92f),
S(0xa95f), R(0xa9c1,0xa9cd), R(0xa9de,0xa9df), R(0xaa5c,0xaa5f), R(0xaade,0xaadf), R(0xaaf0,0xaaf1),
S(0xabeb), R(0xfd3e,0xfd3f), R(0xfe10,0xfe19), R(0xfe30,0xfe52), R(0xfe54,0xfe61), S(0xfe63), S(0xfe68),
R(0xfe6a,0xfe6b), R(0xff01,0xff03), R(0xff05,0xff0a), R(0xff0c,0xff0f), R(0xff1a,0xff1b),
R(0xff1f,0xff20), R(0xff3b,0xff3d), S(0xff3f), S(0xff5b), S(0xff5d), R(0xff5f,0xff65), R(0x10100,0x10102),
S(0x1039f), S(0x103d0), S(0x1056f), S(0x10857), S(0x1091f), S(0x1093f), R(0x10a50,0x10a58), S(0x10a7f),
R(0x10af0,0x10af6), R(0x10b39,0x10b3f), R(0x10b99,0x10b9c), S(0x10ead), R(0x10f55,0x10f59),
R(0x11047,0x1104d), R(0x110bb,0x110bc), R(0x110be,0x110c1), R(0x11140,0x11143), R(0x11174,0x11175),
R(0x111c5,0x111c8), S(0x111cd), S(0x111db), R(0x111dd,0x111df), R(0x11238,0x1123d), S(0x112a9),
R(0x1144b,0x1144f), R(0x1145a,0x1145b), S(0x1145d), S(0x114c6), R(0x115c1,0x115d7), R(0x11641,0x11643),
R(0x11660,0x1166c), R(0x1173c,0x1173e), S(0x1183b), R(0x11944,0x11946), S(0x119e2), R(0x11a3f,0x11a46),
R(0x11a9a,0x11a9c), R(0x11a9e,0x11aa2), R(0x11c41,0x11c45), R(0x11c70,0x11c71), R(0x11ef7,0x11ef8),
S(0x11fff), R(0x12470,0x12474), R(0x16a6e,0x16a6f), S(0x16af5), R(0x16b37,0x16b3b), S(0x16b44),
R(0x16e97,0x16e9a), S(0x16fe2), S(0x1bc9f), R(0x1da87,0x1da8b), R(0x1e95e,0x1e95f)
};
#undef R
#undef S
/* The ASCII ones are the most frequently used ones, also CommonMark
* specification requests few more in this range. */
if(codepoint <= 0x7f)
return ISPUNCT_(codepoint);
return (md_unicode_bsearch__(codepoint, PUNCT_MAP, SIZEOF_ARRAY(PUNCT_MAP)) >= 0);
}
static void
md_get_unicode_fold_info(unsigned codepoint, MD_UNICODE_FOLD_INFO* info)
{
#define R(cp_min, cp_max) ((cp_min) | 0x40000000), ((cp_max) | 0x80000000)
#define S(cp) (cp)
/* Unicode "Pc", "Pd", "Pe", "Pf", "Pi", "Po", "Ps" categories.
* (generated by scripts/build_folding_map.py) */
static const unsigned FOLD_MAP_1[] = {
R(0x0041,0x005a), S(0x00b5), R(0x00c0,0x00d6), R(0x00d8,0x00de), R(0x0100,0x012e), R(0x0132,0x0136),
R(0x0139,0x0147), R(0x014a,0x0176), S(0x0178), R(0x0179,0x017d), S(0x017f), S(0x0181), S(0x0182),
S(0x0184), S(0x0186), S(0x0187), S(0x0189), S(0x018a), S(0x018b), S(0x018e), S(0x018f), S(0x0190),
S(0x0191), S(0x0193), S(0x0194), S(0x0196), S(0x0197), S(0x0198), S(0x019c), S(0x019d), S(0x019f),
R(0x01a0,0x01a4), S(0x01a6), S(0x01a7), S(0x01a9), S(0x01ac), S(0x01ae), S(0x01af), S(0x01b1), S(0x01b2),
S(0x01b3), S(0x01b5), S(0x01b7), S(0x01b8), S(0x01bc), S(0x01c4), S(0x01c5), S(0x01c7), S(0x01c8),
S(0x01ca), R(0x01cb,0x01db), R(0x01de,0x01ee), S(0x01f1), S(0x01f2), S(0x01f4), S(0x01f6), S(0x01f7),
R(0x01f8,0x021e), S(0x0220), R(0x0222,0x0232), S(0x023a), S(0x023b), S(0x023d), S(0x023e), S(0x0241),
S(0x0243), S(0x0244), S(0x0245), R(0x0246,0x024e), S(0x0345), S(0x0370), S(0x0372), S(0x0376), S(0x037f),
S(0x0386), R(0x0388,0x038a), S(0x038c), S(0x038e), S(0x038f), R(0x0391,0x03a1), R(0x03a3,0x03ab),
S(0x03c2), S(0x03cf), S(0x03d0), S(0x03d1), S(0x03d5), S(0x03d6), R(0x03d8,0x03ee), S(0x03f0), S(0x03f1),
S(0x03f4), S(0x03f5), S(0x03f7), S(0x03f9), S(0x03fa), R(0x03fd,0x03ff), R(0x0400,0x040f),
R(0x0410,0x042f), R(0x0460,0x0480), R(0x048a,0x04be), S(0x04c0), R(0x04c1,0x04cd), R(0x04d0,0x052e),
R(0x0531,0x0556), R(0x10a0,0x10c5), S(0x10c7), S(0x10cd), R(0x13f8,0x13fd), S(0x1c80), S(0x1c81),
S(0x1c82), S(0x1c83), S(0x1c84), S(0x1c85), S(0x1c86), S(0x1c87), S(0x1c88), R(0x1c90,0x1cba),
R(0x1cbd,0x1cbf), R(0x1e00,0x1e94), S(0x1e9b), R(0x1ea0,0x1efe), R(0x1f08,0x1f0f), R(0x1f18,0x1f1d),
R(0x1f28,0x1f2f), R(0x1f38,0x1f3f), R(0x1f48,0x1f4d), S(0x1f59), S(0x1f5b), S(0x1f5d), S(0x1f5f),
R(0x1f68,0x1f6f), S(0x1fb8), S(0x1fb9), S(0x1fba), S(0x1fbb), S(0x1fbe), R(0x1fc8,0x1fcb), S(0x1fd8),
S(0x1fd9), S(0x1fda), S(0x1fdb), S(0x1fe8), S(0x1fe9), S(0x1fea), S(0x1feb), S(0x1fec), S(0x1ff8),
S(0x1ff9), S(0x1ffa), S(0x1ffb), S(0x2126), S(0x212a), S(0x212b), S(0x2132), R(0x2160,0x216f), S(0x2183),
R(0x24b6,0x24cf), R(0x2c00,0x2c2e), S(0x2c60), S(0x2c62), S(0x2c63), S(0x2c64), R(0x2c67,0x2c6b),
S(0x2c6d), S(0x2c6e), S(0x2c6f), S(0x2c70), S(0x2c72), S(0x2c75), S(0x2c7e), S(0x2c7f), R(0x2c80,0x2ce2),
S(0x2ceb), S(0x2ced), S(0x2cf2), R(0xa640,0xa66c), R(0xa680,0xa69a), R(0xa722,0xa72e), R(0xa732,0xa76e),
S(0xa779), S(0xa77b), S(0xa77d), R(0xa77e,0xa786), S(0xa78b), S(0xa78d), S(0xa790), S(0xa792),
R(0xa796,0xa7a8), S(0xa7aa), S(0xa7ab), S(0xa7ac), S(0xa7ad), S(0xa7ae), S(0xa7b0), S(0xa7b1), S(0xa7b2),
S(0xa7b3), R(0xa7b4,0xa7be), S(0xa7c2), S(0xa7c4), S(0xa7c5), S(0xa7c6), S(0xa7c7), S(0xa7c9), S(0xa7f5),
R(0xab70,0xabbf), R(0xff21,0xff3a), R(0x10400,0x10427), R(0x104b0,0x104d3), R(0x10c80,0x10cb2),
R(0x118a0,0x118bf), R(0x16e40,0x16e5f), R(0x1e900,0x1e921)
};
static const unsigned FOLD_MAP_1_DATA[] = {
0x0061, 0x007a, 0x03bc, 0x00e0, 0x00f6, 0x00f8, 0x00fe, 0x0101, 0x012f, 0x0133, 0x0137, 0x013a, 0x0148,
0x014b, 0x0177, 0x00ff, 0x017a, 0x017e, 0x0073, 0x0253, 0x0183, 0x0185, 0x0254, 0x0188, 0x0256, 0x0257,
0x018c, 0x01dd, 0x0259, 0x025b, 0x0192, 0x0260, 0x0263, 0x0269, 0x0268, 0x0199, 0x026f, 0x0272, 0x0275,
0x01a1, 0x01a5, 0x0280, 0x01a8, 0x0283, 0x01ad, 0x0288, 0x01b0, 0x028a, 0x028b, 0x01b4, 0x01b6, 0x0292,
0x01b9, 0x01bd, 0x01c6, 0x01c6, 0x01c9, 0x01c9, 0x01cc, 0x01cc, 0x01dc, 0x01df, 0x01ef, 0x01f3, 0x01f3,
0x01f5, 0x0195, 0x01bf, 0x01f9, 0x021f, 0x019e, 0x0223, 0x0233, 0x2c65, 0x023c, 0x019a, 0x2c66, 0x0242,
0x0180, 0x0289, 0x028c, 0x0247, 0x024f, 0x03b9, 0x0371, 0x0373, 0x0377, 0x03f3, 0x03ac, 0x03ad, 0x03af,
0x03cc, 0x03cd, 0x03ce, 0x03b1, 0x03c1, 0x03c3, 0x03cb, 0x03c3, 0x03d7, 0x03b2, 0x03b8, 0x03c6, 0x03c0,
0x03d9, 0x03ef, 0x03ba, 0x03c1, 0x03b8, 0x03b5, 0x03f8, 0x03f2, 0x03fb, 0x037b, 0x037d, 0x0450, 0x045f,
0x0430, 0x044f, 0x0461, 0x0481, 0x048b, 0x04bf, 0x04cf, 0x04c2, 0x04ce, 0x04d1, 0x052f, 0x0561, 0x0586,
0x2d00, 0x2d25, 0x2d27, 0x2d2d, 0x13f0, 0x13f5, 0x0432, 0x0434, 0x043e, 0x0441, 0x0442, 0x0442, 0x044a,
0x0463, 0xa64b, 0x10d0, 0x10fa, 0x10fd, 0x10ff, 0x1e01, 0x1e95, 0x1e61, 0x1ea1, 0x1eff, 0x1f00, 0x1f07,
0x1f10, 0x1f15, 0x1f20, 0x1f27, 0x1f30, 0x1f37, 0x1f40, 0x1f45, 0x1f51, 0x1f53, 0x1f55, 0x1f57, 0x1f60,
0x1f67, 0x1fb0, 0x1fb1, 0x1f70, 0x1f71, 0x03b9, 0x1f72, 0x1f75, 0x1fd0, 0x1fd1, 0x1f76, 0x1f77, 0x1fe0,
0x1fe1, 0x1f7a, 0x1f7b, 0x1fe5, 0x1f78, 0x1f79, 0x1f7c, 0x1f7d, 0x03c9, 0x006b, 0x00e5, 0x214e, 0x2170,
0x217f, 0x2184, 0x24d0, 0x24e9, 0x2c30, 0x2c5e, 0x2c61, 0x026b, 0x1d7d, 0x027d, 0x2c68, 0x2c6c, 0x0251,
0x0271, 0x0250, 0x0252, 0x2c73, 0x2c76, 0x023f, 0x0240, 0x2c81, 0x2ce3, 0x2cec, 0x2cee, 0x2cf3, 0xa641,
0xa66d, 0xa681, 0xa69b, 0xa723, 0xa72f, 0xa733, 0xa76f, 0xa77a, 0xa77c, 0x1d79, 0xa77f, 0xa787, 0xa78c,
0x0265, 0xa791, 0xa793, 0xa797, 0xa7a9, 0x0266, 0x025c, 0x0261, 0x026c, 0x026a, 0x029e, 0x0287, 0x029d,
0xab53, 0xa7b5, 0xa7bf, 0xa7c3, 0xa794, 0x0282, 0x1d8e, 0xa7c8, 0xa7ca, 0xa7f6, 0x13a0, 0x13ef, 0xff41,
0xff5a, 0x10428, 0x1044f, 0x104d8, 0x104fb, 0x10cc0, 0x10cf2, 0x118c0, 0x118df, 0x16e60, 0x16e7f, 0x1e922,
0x1e943
};
static const unsigned FOLD_MAP_2[] = {
S(0x00df), S(0x0130), S(0x0149), S(0x01f0), S(0x0587), S(0x1e96), S(0x1e97), S(0x1e98), S(0x1e99),
S(0x1e9a), S(0x1e9e), S(0x1f50), R(0x1f80,0x1f87), R(0x1f88,0x1f8f), R(0x1f90,0x1f97), R(0x1f98,0x1f9f),
R(0x1fa0,0x1fa7), R(0x1fa8,0x1faf), S(0x1fb2), S(0x1fb3), S(0x1fb4), S(0x1fb6), S(0x1fbc), S(0x1fc2),
S(0x1fc3), S(0x1fc4), S(0x1fc6), S(0x1fcc), S(0x1fd6), S(0x1fe4), S(0x1fe6), S(0x1ff2), S(0x1ff3),
S(0x1ff4), S(0x1ff6), S(0x1ffc), S(0xfb00), S(0xfb01), S(0xfb02), S(0xfb05), S(0xfb06), S(0xfb13),
S(0xfb14), S(0xfb15), S(0xfb16), S(0xfb17)
};
static const unsigned FOLD_MAP_2_DATA[] = {
0x0073,0x0073, 0x0069,0x0307, 0x02bc,0x006e, 0x006a,0x030c, 0x0565,0x0582, 0x0068,0x0331, 0x0074,0x0308,
0x0077,0x030a, 0x0079,0x030a, 0x0061,0x02be, 0x0073,0x0073, 0x03c5,0x0313, 0x1f00,0x03b9, 0x1f07,0x03b9,
0x1f00,0x03b9, 0x1f07,0x03b9, 0x1f20,0x03b9, 0x1f27,0x03b9, 0x1f20,0x03b9, 0x1f27,0x03b9, 0x1f60,0x03b9,
0x1f67,0x03b9, 0x1f60,0x03b9, 0x1f67,0x03b9, 0x1f70,0x03b9, 0x03b1,0x03b9, 0x03ac,0x03b9, 0x03b1,0x0342,
0x03b1,0x03b9, 0x1f74,0x03b9, 0x03b7,0x03b9, 0x03ae,0x03b9, 0x03b7,0x0342, 0x03b7,0x03b9, 0x03b9,0x0342,
0x03c1,0x0313, 0x03c5,0x0342, 0x1f7c,0x03b9, 0x03c9,0x03b9, 0x03ce,0x03b9, 0x03c9,0x0342, 0x03c9,0x03b9,
0x0066,0x0066, 0x0066,0x0069, 0x0066,0x006c, 0x0073,0x0074, 0x0073,0x0074, 0x0574,0x0576, 0x0574,0x0565,
0x0574,0x056b, 0x057e,0x0576, 0x0574,0x056d
};
static const unsigned FOLD_MAP_3[] = {
S(0x0390), S(0x03b0), S(0x1f52), S(0x1f54), S(0x1f56), S(0x1fb7), S(0x1fc7), S(0x1fd2), S(0x1fd3),
S(0x1fd7), S(0x1fe2), S(0x1fe3), S(0x1fe7), S(0x1ff7), S(0xfb03), S(0xfb04)
};
static const unsigned FOLD_MAP_3_DATA[] = {
0x03b9,0x0308,0x0301, 0x03c5,0x0308,0x0301, 0x03c5,0x0313,0x0300, 0x03c5,0x0313,0x0301,
0x03c5,0x0313,0x0342, 0x03b1,0x0342,0x03b9, 0x03b7,0x0342,0x03b9, 0x03b9,0x0308,0x0300,
0x03b9,0x0308,0x0301, 0x03b9,0x0308,0x0342, 0x03c5,0x0308,0x0300, 0x03c5,0x0308,0x0301,
0x03c5,0x0308,0x0342, 0x03c9,0x0342,0x03b9, 0x0066,0x0066,0x0069, 0x0066,0x0066,0x006c
};
#undef R
#undef S
static const struct {
const unsigned* map;
const unsigned* data;
size_t map_size;
unsigned n_codepoints;
} FOLD_MAP_LIST[] = {
{ FOLD_MAP_1, FOLD_MAP_1_DATA, SIZEOF_ARRAY(FOLD_MAP_1), 1 },
{ FOLD_MAP_2, FOLD_MAP_2_DATA, SIZEOF_ARRAY(FOLD_MAP_2), 2 },
{ FOLD_MAP_3, FOLD_MAP_3_DATA, SIZEOF_ARRAY(FOLD_MAP_3), 3 }
};
int i;
/* Fast path for ASCII characters. */
if(codepoint <= 0x7f) {
info->codepoints[0] = codepoint;
if(ISUPPER_(codepoint))
info->codepoints[0] += 'a' - 'A';
info->n_codepoints = 1;
return;
}
/* Try to locate the codepoint in any of the maps. */
for(i = 0; i < (int) SIZEOF_ARRAY(FOLD_MAP_LIST); i++) {
int index;
index = md_unicode_bsearch__(codepoint, FOLD_MAP_LIST[i].map, FOLD_MAP_LIST[i].map_size);
if(index >= 0) {
/* Found the mapping. */
unsigned n_codepoints = FOLD_MAP_LIST[i].n_codepoints;
const unsigned* map = FOLD_MAP_LIST[i].map;
const unsigned* codepoints = FOLD_MAP_LIST[i].data + (index * n_codepoints);
memcpy(info->codepoints, codepoints, sizeof(unsigned) * n_codepoints);
info->n_codepoints = n_codepoints;
if(FOLD_MAP_LIST[i].map[index] != codepoint) {
/* The found mapping maps whole range of codepoints,
* i.e. we have to offset info->codepoints[0] accordingly. */
if((map[index] & 0x00ffffff)+1 == codepoints[0]) {
/* Alternating type of the range. */
info->codepoints[0] = codepoint + ((codepoint & 0x1) == (map[index] & 0x1) ? 1 : 0);
} else {
/* Range to range kind of mapping. */
info->codepoints[0] += (codepoint - (map[index] & 0x00ffffff));
}
}
return;
}
}
/* No mapping found. Map the codepoint to itself. */
info->codepoints[0] = codepoint;
info->n_codepoints = 1;
}
#endif
#if defined MD4C_USE_UTF16
#define IS_UTF16_SURROGATE_HI(word) (((WORD)(word) & 0xfc00) == 0xd800)
#define IS_UTF16_SURROGATE_LO(word) (((WORD)(word) & 0xfc00) == 0xdc00)
#define UTF16_DECODE_SURROGATE(hi, lo) (0x10000 + ((((unsigned)(hi) & 0x3ff) << 10) | (((unsigned)(lo) & 0x3ff) << 0)))
static unsigned
md_decode_utf16le__(const CHAR* str, SZ str_size, SZ* p_size)
{
if(IS_UTF16_SURROGATE_HI(str[0])) {
if(1 < str_size && IS_UTF16_SURROGATE_LO(str[1])) {
if(p_size != NULL)
*p_size = 2;
return UTF16_DECODE_SURROGATE(str[0], str[1]);
}
}
if(p_size != NULL)
*p_size = 1;
return str[0];
}
static unsigned
md_decode_utf16le_before__(MD_CTX* ctx, OFF off)
{
if(off > 2 && IS_UTF16_SURROGATE_HI(CH(off-2)) && IS_UTF16_SURROGATE_LO(CH(off-1)))
return UTF16_DECODE_SURROGATE(CH(off-2), CH(off-1));
return CH(off);
}
/* No whitespace uses surrogates, so no decoding needed here. */
#define ISUNICODEWHITESPACE_(codepoint) md_is_unicode_whitespace__(codepoint)
#define ISUNICODEWHITESPACE(off) md_is_unicode_whitespace__(CH(off))
#define ISUNICODEWHITESPACEBEFORE(off) md_is_unicode_whitespace__(CH((off)-1))
#define ISUNICODEPUNCT(off) md_is_unicode_punct__(md_decode_utf16le__(STR(off), ctx->size - (off), NULL))
#define ISUNICODEPUNCTBEFORE(off) md_is_unicode_punct__(md_decode_utf16le_before__(ctx, off))
static inline int
md_decode_unicode(const CHAR* str, OFF off, SZ str_size, SZ* p_char_size)
{
return md_decode_utf16le__(str+off, str_size-off, p_char_size);
}
#elif defined MD4C_USE_UTF8
#define IS_UTF8_LEAD1(byte) ((unsigned char)(byte) <= 0x7f)
#define IS_UTF8_LEAD2(byte) (((unsigned char)(byte) & 0xe0) == 0xc0)
#define IS_UTF8_LEAD3(byte) (((unsigned char)(byte) & 0xf0) == 0xe0)
#define IS_UTF8_LEAD4(byte) (((unsigned char)(byte) & 0xf8) == 0xf0)
#define IS_UTF8_TAIL(byte) (((unsigned char)(byte) & 0xc0) == 0x80)
static unsigned
md_decode_utf8__(const CHAR* str, SZ str_size, SZ* p_size)
{
if(!IS_UTF8_LEAD1(str[0])) {
if(IS_UTF8_LEAD2(str[0])) {
if(1 < str_size && IS_UTF8_TAIL(str[1])) {
if(p_size != NULL)
*p_size = 2;
return (((unsigned int)str[0] & 0x1f) << 6) |
(((unsigned int)str[1] & 0x3f) << 0);
}
} else if(IS_UTF8_LEAD3(str[0])) {
if(2 < str_size && IS_UTF8_TAIL(str[1]) && IS_UTF8_TAIL(str[2])) {
if(p_size != NULL)
*p_size = 3;
return (((unsigned int)str[0] & 0x0f) << 12) |
(((unsigned int)str[1] & 0x3f) << 6) |
(((unsigned int)str[2] & 0x3f) << 0);
}
} else if(IS_UTF8_LEAD4(str[0])) {
if(3 < str_size && IS_UTF8_TAIL(str[1]) && IS_UTF8_TAIL(str[2]) && IS_UTF8_TAIL(str[3])) {
if(p_size != NULL)
*p_size = 4;
return (((unsigned int)str[0] & 0x07) << 18) |
(((unsigned int)str[1] & 0x3f) << 12) |
(((unsigned int)str[2] & 0x3f) << 6) |
(((unsigned int)str[3] & 0x3f) << 0);
}
}
}
if(p_size != NULL)
*p_size = 1;
return (unsigned) str[0];
}
static unsigned
md_decode_utf8_before__(MD_CTX* ctx, OFF off)
{
if(!IS_UTF8_LEAD1(CH(off-1))) {
if(off > 1 && IS_UTF8_LEAD2(CH(off-2)) && IS_UTF8_TAIL(CH(off-1)))
return (((unsigned int)CH(off-2) & 0x1f) << 6) |
(((unsigned int)CH(off-1) & 0x3f) << 0);
if(off > 2 && IS_UTF8_LEAD3(CH(off-3)) && IS_UTF8_TAIL(CH(off-2)) && IS_UTF8_TAIL(CH(off-1)))
return (((unsigned int)CH(off-3) & 0x0f) << 12) |
(((unsigned int)CH(off-2) & 0x3f) << 6) |
(((unsigned int)CH(off-1) & 0x3f) << 0);
if(off > 3 && IS_UTF8_LEAD4(CH(off-4)) && IS_UTF8_TAIL(CH(off-3)) && IS_UTF8_TAIL(CH(off-2)) && IS_UTF8_TAIL(CH(off-1)))
return (((unsigned int)CH(off-4) & 0x07) << 18) |
(((unsigned int)CH(off-3) & 0x3f) << 12) |
(((unsigned int)CH(off-2) & 0x3f) << 6) |
(((unsigned int)CH(off-1) & 0x3f) << 0);
}
return (unsigned) CH(off-1);
}
#define ISUNICODEWHITESPACE_(codepoint) md_is_unicode_whitespace__(codepoint)
#define ISUNICODEWHITESPACE(off) md_is_unicode_whitespace__(md_decode_utf8__(STR(off), ctx->size - (off), NULL))
#define ISUNICODEWHITESPACEBEFORE(off) md_is_unicode_whitespace__(md_decode_utf8_before__(ctx, off))
#define ISUNICODEPUNCT(off) md_is_unicode_punct__(md_decode_utf8__(STR(off), ctx->size - (off), NULL))
#define ISUNICODEPUNCTBEFORE(off) md_is_unicode_punct__(md_decode_utf8_before__(ctx, off))
static inline unsigned
md_decode_unicode(const CHAR* str, OFF off, SZ str_size, SZ* p_char_size)
{
return md_decode_utf8__(str+off, str_size-off, p_char_size);
}
#else
#define ISUNICODEWHITESPACE_(codepoint) ISWHITESPACE_(codepoint)
#define ISUNICODEWHITESPACE(off) ISWHITESPACE(off)
#define ISUNICODEWHITESPACEBEFORE(off) ISWHITESPACE((off)-1)
#define ISUNICODEPUNCT(off) ISPUNCT(off)
#define ISUNICODEPUNCTBEFORE(off) ISPUNCT((off)-1)
static inline void
md_get_unicode_fold_info(unsigned codepoint, MD_UNICODE_FOLD_INFO* info)
{
info->codepoints[0] = codepoint;
if(ISUPPER_(codepoint))
info->codepoints[0] += 'a' - 'A';
info->n_codepoints = 1;
}
static inline unsigned
md_decode_unicode(const CHAR* str, OFF off, SZ str_size, SZ* p_size)
{
*p_size = 1;
return (unsigned) str[off];
}
#endif
/*************************************
*** Helper string manipulations ***
*************************************/
/* Fill buffer with copy of the string between 'beg' and 'end' but replace any
* line breaks with given replacement character.
*
* NOTE: Caller is responsible to make sure the buffer is large enough.
* (Given the output is always shorter then input, (end - beg) is good idea
* what the caller should allocate.)
*/
static void
md_merge_lines(MD_CTX* ctx, OFF beg, OFF end, const MD_LINE* lines, int32_t n_lines,
CHAR line_break_replacement_char, CHAR* buffer, SZ* p_size)
{
CHAR* ptr = buffer;
int32_t line_index = 0;
OFF off = beg;
MD_UNUSED(n_lines);
while(1) {
const MD_LINE* line = &lines[line_index];
OFF line_end = line->end;
if(end < line_end)
line_end = end;
while(off < line_end) {
*ptr = CH(off);
ptr++;
off++;
}
if(off >= end) {
*p_size = ptr - buffer;
return;
}
*ptr = line_break_replacement_char;
ptr++;
line_index++;
off = lines[line_index].beg;
}
}
/* Wrapper of md_merge_lines() which allocates new buffer for the output string.
*/
static int
md_merge_lines_alloc(MD_CTX* ctx, OFF beg, OFF end, const MD_LINE* lines, int32_t n_lines,
CHAR line_break_replacement_char, CHAR** p_str, SZ* p_size)
{
CHAR* buffer;
buffer = (CHAR*) malloc(sizeof(CHAR) * (end - beg));
if(buffer == NULL) {
MD_LOG("malloc() failed.");
return -1;
}
md_merge_lines(ctx, beg, end, lines, n_lines,
line_break_replacement_char, buffer, p_size);
*p_str = buffer;
return 0;
}
static OFF
md_skip_unicode_whitespace(const CHAR* label, OFF off, SZ size)
{
SZ char_size;
unsigned codepoint;
while(off < size) {
codepoint = md_decode_unicode(label, off, size, &char_size);
if(!ISUNICODEWHITESPACE_(codepoint) && !ISNEWLINE_(label[off]))
break;
off += char_size;
}
return off;
}
/******************************
*** Recognizing raw HTML ***
******************************/
/* md_is_html_tag() may be called when processing inlines (inline raw HTML)
* or when breaking document to blocks (checking for start of HTML block type 7).
*
* When breaking document to blocks, we do not yet know line boundaries, but
* in that case the whole tag has to live on a single line. We distinguish this
* by n_lines == 0.
*/
static int
md_is_html_tag(MD_CTX* ctx, const MD_LINE* lines, int32_t n_lines, OFF beg, OFF max_end, OFF* p_end)
{
int attr_state;
OFF off = beg;
OFF line_end = (n_lines > 0) ? lines[0].end : ctx->size;
int32_t i = 0;
MD_ASSERT(CH(beg) == _T('<'));
if(off + 1 >= line_end)
return FALSE;
off++;
/* For parsing attributes, we need a little state automaton below.
* State -1: no attributes are allowed.
* State 0: attribute could follow after some whitespace.
* State 1: after a whitespace (attribute name may follow).
* State 2: after attribute name ('=' MAY follow).
* State 3: after '=' (value specification MUST follow).
* State 41: in middle of unquoted attribute value.
* State 42: in middle of single-quoted attribute value.
* State 43: in middle of double-quoted attribute value.
*/
attr_state = 0;
if(CH(off) == _T('/')) {
/* Closer tag "</ ... >". No attributes may be present. */
attr_state = -1;
off++;
}
/* Tag name */
if(off >= line_end || !ISALPHA(off))
return FALSE;
off++;
while(off < line_end && (ISALNUM(off) || CH(off) == _T('-')))
off++;
/* (Optional) attributes (if not closer), (optional) '/' (if not closer)
* and final '>'. */
while(1) {
while(off < line_end && !ISNEWLINE(off)) {
if(attr_state > 40) {
if(attr_state == 41 && (ISBLANK(off) || ISANYOF(off, _T("\"'=<>`")))) {
attr_state = 0;
off--; /* Put the char back for re-inspection in the new state. */
} else if(attr_state == 42 && CH(off) == _T('\'')) {
attr_state = 0;
} else if(attr_state == 43 && CH(off) == _T('"')) {
attr_state = 0;
}
off++;
} else if(ISWHITESPACE(off)) {
if(attr_state == 0)
attr_state = 1;
off++;
} else if(attr_state <= 2 && CH(off) == _T('>')) {
/* End. */
goto done;
} else if(attr_state <= 2 && CH(off) == _T('/') && off+1 < line_end && CH(off+1) == _T('>')) {
/* End with digraph '/>' */
off++;
goto done;
} else if((attr_state == 1 || attr_state == 2) && (ISALPHA(off) || CH(off) == _T('_') || CH(off) == _T(':'))) {
off++;
/* Attribute name */
while(off < line_end && (ISALNUM(off) || ISANYOF(off, _T("_.:-"))))
off++;
attr_state = 2;
} else if(attr_state == 2 && CH(off) == _T('=')) {
/* Attribute assignment sign */
off++;
attr_state = 3;
} else if(attr_state == 3) {
/* Expecting start of attribute value. */
if(CH(off) == _T('"'))
attr_state = 43;
else if(CH(off) == _T('\''))
attr_state = 42;
else if(!ISANYOF(off, _T("\"'=<>`")) && !ISNEWLINE(off))
attr_state = 41;
else
return FALSE;
off++;
} else {
/* Anything unexpected. */
return FALSE;
}
}
/* We have to be on a single line. See definition of start condition
* of HTML block, type 7. */
if(n_lines == 0)
return FALSE;
i++;
if(i >= n_lines)
return FALSE;
off = lines[i].beg;
line_end = lines[i].end;
if(attr_state == 0 || attr_state == 41)
attr_state = 1;
if(off >= max_end)
return FALSE;
}
done:
if(off >= max_end)
return FALSE;
*p_end = off+1;
return TRUE;
}
static int
md_scan_for_html_closer(MD_CTX* ctx, const MD_CHAR* str, MD_SIZE len,
const MD_LINE* lines, int32_t n_lines,
OFF beg, OFF max_end, OFF* p_end,
OFF* p_scan_horizon)
{
OFF off = beg;
int32_t i = 0;
if(off < *p_scan_horizon && *p_scan_horizon >= max_end - len) {
/* We have already scanned the range up to the max_end so we know
* there is nothing to see. */
return FALSE;
}
while(TRUE) {
while(off + len <= lines[i].end && off + len <= max_end) {
if(md_ascii_eq(STR(off), str, len)) {
/* Success. */
*p_end = off + len;
return TRUE;
}
off++;
}
i++;
if(off >= max_end || i >= n_lines) {
/* Failure. */
*p_scan_horizon = off;
return FALSE;
}
off = lines[i].beg;
}
}
static int
md_is_html_comment(MD_CTX* ctx, const MD_LINE* lines, int32_t n_lines, OFF beg, OFF max_end, OFF* p_end)
{
OFF off = beg;
MD_ASSERT(CH(beg) == _T('<'));
if(off + 4 >= lines[0].end)
return FALSE;
if(CH(off+1) != _T('!') || CH(off+2) != _T('-') || CH(off+3) != _T('-'))
return FALSE;
off += 4;
/* ">" and "->" must not follow the opening. */
if(off < lines[0].end && CH(off) == _T('>'))
return FALSE;
if(off+1 < lines[0].end && CH(off) == _T('-') && CH(off+1) == _T('>'))
return FALSE;
/* HTML comment must not contain "--", so we scan just for "--" instead
* of "-->" and verify manually that '>' follows. */
if(md_scan_for_html_closer(ctx, _T("--"), 2,
lines, n_lines, off, max_end, p_end, &ctx->html_comment_horizon))
{
if(*p_end < max_end && CH(*p_end) == _T('>')) {
*p_end = *p_end + 1;
return TRUE;
}
}
return FALSE;
}
static int
md_is_html_processing_instruction(MD_CTX* ctx, const MD_LINE* lines, int32_t n_lines, OFF beg, OFF max_end, OFF* p_end)
{
OFF off = beg;
if(off + 2 >= lines[0].end)
return FALSE;
if(CH(off+1) != _T('?'))
return FALSE;
off += 2;
return md_scan_for_html_closer(ctx, _T("?>"), 2,
lines, n_lines, off, max_end, p_end, &ctx->html_proc_instr_horizon);
}
static int
md_is_html_declaration(MD_CTX* ctx, const MD_LINE* lines, int32_t n_lines, OFF beg, OFF max_end, OFF* p_end)
{
OFF off = beg;
if(off + 2 >= lines[0].end)
return FALSE;
if(CH(off+1) != _T('!'))
return FALSE;
off += 2;
/* Declaration name. */
if(off >= lines[0].end || !ISALPHA(off))
return FALSE;
off++;
while(off < lines[0].end && ISALPHA(off))
off++;
if(off < lines[0].end && !ISWHITESPACE(off))
return FALSE;
return md_scan_for_html_closer(ctx, _T(">"), 1,
lines, n_lines, off, max_end, p_end, &ctx->html_decl_horizon);
}
static int
md_is_html_cdata(MD_CTX* ctx, const MD_LINE* lines, int32_t n_lines, OFF beg, OFF max_end, OFF* p_end)
{
static const CHAR open_str[] = _T("<![CDATA[");
static const SZ open_size = SIZEOF_ARRAY(open_str) - 1;
OFF off = beg;
if(off + open_size >= lines[0].end)
return FALSE;
if(memcmp(STR(off), open_str, open_size) != 0)
return FALSE;
off += open_size;
if(lines[n_lines-1].end < max_end)
max_end = lines[n_lines-1].end - 2;
return md_scan_for_html_closer(ctx, _T("]]>"), 3,
lines, n_lines, off, max_end, p_end, &ctx->html_cdata_horizon);
}
static int
md_is_html_any(MD_CTX* ctx, const MD_LINE* lines, int32_t n_lines, OFF beg, OFF max_end, OFF* p_end)
{
MD_ASSERT(CH(beg) == _T('<'));
return (md_is_html_tag(ctx, lines, n_lines, beg, max_end, p_end) ||
md_is_html_comment(ctx, lines, n_lines, beg, max_end, p_end) ||
md_is_html_processing_instruction(ctx, lines, n_lines, beg, max_end, p_end) ||
md_is_html_declaration(ctx, lines, n_lines, beg, max_end, p_end) ||
md_is_html_cdata(ctx, lines, n_lines, beg, max_end, p_end));
}
/****************************
*** Recognizing Entity ***
****************************/
static int
md_is_hex_entity_contents(MD_CTX* ctx, const CHAR* text, OFF beg, OFF max_end, OFF* p_end)
{
OFF off = beg;
MD_UNUSED(ctx);
while(off < max_end && ISXDIGIT_(text[off]) && off - beg <= 8)
off++;
if(1 <= off - beg && off - beg <= 6) {
*p_end = off;
return TRUE;
} else {
return FALSE;
}
}
static int
md_is_dec_entity_contents(MD_CTX* ctx, const CHAR* text, OFF beg, OFF max_end, OFF* p_end)
{
OFF off = beg;
MD_UNUSED(ctx);
while(off < max_end && ISDIGIT_(text[off]) && off - beg <= 8)
off++;
if(1 <= off - beg && off - beg <= 7) {
*p_end = off;
return TRUE;
} else {
return FALSE;
}
}
static int
md_is_named_entity_contents(MD_CTX* ctx, const CHAR* text, OFF beg, OFF max_end, OFF* p_end)
{
OFF off = beg;
MD_UNUSED(ctx);
if(off < max_end && ISALPHA_(text[off]))
off++;
else
return FALSE;
while(off < max_end && ISALNUM_(text[off]) && off - beg <= 48)
off++;
if(2 <= off - beg && off - beg <= 48) {
*p_end = off;
return TRUE;
} else {
return FALSE;
}
}
static int
md_is_entity_str(MD_CTX* ctx, const CHAR* text, OFF beg, OFF max_end, OFF* p_end)
{
int is_contents;
OFF off = beg;
MD_ASSERT(text[off] == _T('&'));
off++;
if(off+2 < max_end && text[off] == _T('#') && (text[off+1] == _T('x') || text[off+1] == _T('X')))
is_contents = md_is_hex_entity_contents(ctx, text, off+2, max_end, &off);
else if(off+1 < max_end && text[off] == _T('#'))
is_contents = md_is_dec_entity_contents(ctx, text, off+1, max_end, &off);
else
is_contents = md_is_named_entity_contents(ctx, text, off, max_end, &off);
if(is_contents && off < max_end && text[off] == _T(';')) {
*p_end = off+1;
return TRUE;
} else {
return FALSE;
}
}
static inline int
md_is_entity(MD_CTX* ctx, OFF beg, OFF max_end, OFF* p_end)
{
return md_is_entity_str(ctx, ctx->text, beg, max_end, p_end);
}
/******************************
*** Attribute Management ***
******************************/
typedef struct MD_ATTRIBUTE_BUILD_tag MD_ATTRIBUTE_BUILD;
struct MD_ATTRIBUTE_BUILD_tag {
CHAR* text;
MD_TEXTTYPE* substr_types;
OFF* substr_offsets;
int32_t substr_count;
int32_t substr_alloc;
MD_TEXTTYPE trivial_types[1];
OFF trivial_offsets[2];
};
#define MD_BUILD_ATTR_NO_ESCAPES 0x0001
static int
md_build_attr_append_substr(MD_CTX* ctx, MD_ATTRIBUTE_BUILD* build,
MD_TEXTTYPE type, OFF off)
{
if(build->substr_count >= build->substr_alloc) {
MD_TEXTTYPE* new_substr_types;
OFF* new_substr_offsets;
build->substr_alloc = (build->substr_alloc > 0
? build->substr_alloc + build->substr_alloc / 2
: 8);
new_substr_types = (MD_TEXTTYPE*) realloc(build->substr_types,
build->substr_alloc * sizeof(MD_TEXTTYPE));
if(new_substr_types == NULL) {
MD_LOG("realloc() failed.");
return -1;
}
/* Note +1 to reserve space for final offset (== raw_size). */
new_substr_offsets = (OFF*) realloc(build->substr_offsets,
(build->substr_alloc+1) * sizeof(OFF));
if(new_substr_offsets == NULL) {
MD_LOG("realloc() failed.");
free(new_substr_types);
return -1;
}
build->substr_types = new_substr_types;
build->substr_offsets = new_substr_offsets;
}
build->substr_types[build->substr_count] = type;
build->substr_offsets[build->substr_count] = off;
build->substr_count++;
return 0;
}
static void
md_free_attribute(MD_CTX* ctx, MD_ATTRIBUTE_BUILD* build)
{
MD_UNUSED(ctx);
if(build->substr_alloc > 0) {
free(build->text);
free(build->substr_types);
free(build->substr_offsets);
}
}
static int
md_build_attribute(MD_CTX* ctx, const CHAR* raw_text, SZ raw_size,
unsigned flags, MD_ATTRIBUTE* attr, MD_ATTRIBUTE_BUILD* build)
{
OFF raw_off, off;
int is_trivial;
int ret = 0;
memset(build, 0, sizeof(MD_ATTRIBUTE_BUILD));
/* If there is no backslash and no ampersand, build trivial attribute
* without any malloc(). */
is_trivial = TRUE;
for(raw_off = 0; raw_off < raw_size; raw_off++) {
if(ISANYOF3_(raw_text[raw_off], _T('\\'), _T('&'), _T('\0'))) {
is_trivial = FALSE;
break;
}
}
if(is_trivial) {
build->text = (CHAR*) (raw_size ? raw_text : NULL);
build->substr_types = build->trivial_types;
build->substr_offsets = build->trivial_offsets;
build->substr_count = 1;
build->substr_alloc = 0;
build->trivial_types[0] = MD_TEXT_NORMAL;
build->trivial_offsets[0] = 0;
build->trivial_offsets[1] = raw_size;
off = raw_size;
} else {
build->text = (CHAR*) malloc(raw_size * sizeof(CHAR));
if(build->text == NULL) {
MD_LOG("malloc() failed.");
goto abort;
}
raw_off = 0;
off = 0;
while(raw_off < raw_size) {
if(raw_text[raw_off] == _T('\0')) {
MD_CHECK(md_build_attr_append_substr(ctx, build, MD_TEXT_NULLCHAR, off));
memcpy(build->text + off, raw_text + raw_off, 1);
off++;
raw_off++;
continue;
}
if(raw_text[raw_off] == _T('&')) {
OFF ent_end;
if(md_is_entity_str(ctx, raw_text, raw_off, raw_size, &ent_end)) {
MD_CHECK(md_build_attr_append_substr(ctx, build, MD_TEXT_ENTITY, off));
memcpy(build->text + off, raw_text + raw_off, ent_end - raw_off);
off += ent_end - raw_off;
raw_off = ent_end;
continue;
}
}
if(build->substr_count == 0 || build->substr_types[build->substr_count-1] != MD_TEXT_NORMAL)
MD_CHECK(md_build_attr_append_substr(ctx, build, MD_TEXT_NORMAL, off));
if(!(flags & MD_BUILD_ATTR_NO_ESCAPES) &&
raw_text[raw_off] == _T('\\') && raw_off+1 < raw_size &&
(ISPUNCT_(raw_text[raw_off+1]) || ISNEWLINE_(raw_text[raw_off+1])))
raw_off++;
build->text[off++] = raw_text[raw_off++];
}
build->substr_offsets[build->substr_count] = off;
}
attr->text = build->text;
attr->size = off;
attr->substr_offsets = build->substr_offsets;
attr->substr_types = build->substr_types;
return 0;
abort:
md_free_attribute(ctx, build);
return -1;
}
/*********************************************
*** Dictionary of Reference Definitions ***
*********************************************/
// GS_SPECIFIC - Lots of unsigned changed to unsigned longs in this hash code.
#define MD_FNV1A_BASE 2166136261UL
#define MD_FNV1A_PRIME 16777619UL
static inline uint32_t
md_fnv1a(uint32_t base, const void* data, size_t n)
{
const unsigned char* buf = (const unsigned char*) data;
uint32_t hash = base;
size_t i;
for(i = 0; i < n; i++) {
hash ^= buf[i];
hash *= MD_FNV1A_PRIME;
}
return hash;
}
struct MD_REF_DEF_tag {
CHAR* label;
CHAR* title;
uint32_t hash;
SZ label_size;
SZ title_size;
OFF dest_beg;
OFF dest_end;
unsigned char label_needs_free : 1;
unsigned char title_needs_free : 1;
};
/* Label equivalence is quite complicated with regards to whitespace and case
* folding. This complicates computing a hash of it as well as direct comparison
* of two labels. */
static uint32_t
md_link_label_hash(const CHAR* label, SZ size)
{
uint32_t hash = MD_FNV1A_BASE;
OFF off;
unsigned codepoint;
int is_whitespace = FALSE;
off = md_skip_unicode_whitespace(label, 0, size);
while(off < size) {
SZ char_size;
codepoint = md_decode_unicode(label, off, size, &char_size);
is_whitespace = ISUNICODEWHITESPACE_(codepoint) || ISNEWLINE_(label[off]);
if(is_whitespace) {
codepoint = ' ';
hash = md_fnv1a(hash, &codepoint, sizeof(unsigned));
off = md_skip_unicode_whitespace(label, off, size);
} else {
MD_UNICODE_FOLD_INFO fold_info;
md_get_unicode_fold_info(codepoint, &fold_info);
hash = md_fnv1a(hash, fold_info.codepoints, fold_info.n_codepoints * sizeof(unsigned));
off += char_size;
}
}
return hash;
}
static OFF
md_link_label_cmp_load_fold_info(const CHAR* label, OFF off, SZ size,
MD_UNICODE_FOLD_INFO* fold_info)
{
unsigned codepoint;
SZ char_size;
if(off >= size) {
/* Treat end of a link label as a whitespace. */
goto whitespace;
}
codepoint = md_decode_unicode(label, off, size, &char_size);
off += char_size;
if(ISUNICODEWHITESPACE_(codepoint)) {
/* Treat all whitespace as equivalent */
goto whitespace;
}
/* Get real folding info. */
md_get_unicode_fold_info(codepoint, fold_info);
return off;
whitespace:
fold_info->codepoints[0] = _T(' ');
fold_info->n_codepoints = 1;
return md_skip_unicode_whitespace(label, off, size);
}
static int
md_link_label_cmp(const CHAR* a_label, SZ a_size, const CHAR* b_label, SZ b_size)
{
OFF a_off;
OFF b_off;
MD_UNICODE_FOLD_INFO a_fi = { { 0 }, 0 };
MD_UNICODE_FOLD_INFO b_fi = { { 0 }, 0 };
OFF a_fi_off = 0;
OFF b_fi_off = 0;
int cmp;
a_off = md_skip_unicode_whitespace(a_label, 0, a_size);
b_off = md_skip_unicode_whitespace(b_label, 0, b_size);
while(a_off < a_size || a_fi_off < a_fi.n_codepoints ||
b_off < b_size || b_fi_off < b_fi.n_codepoints)
{
/* If needed, load fold info for next char. */
if(a_fi_off >= a_fi.n_codepoints) {
a_fi_off = 0;
a_off = md_link_label_cmp_load_fold_info(a_label, a_off, a_size, &a_fi);
}
if(b_fi_off >= b_fi.n_codepoints) {
b_fi_off = 0;
b_off = md_link_label_cmp_load_fold_info(b_label, b_off, b_size, &b_fi);
}
cmp = b_fi.codepoints[b_fi_off] - a_fi.codepoints[a_fi_off];
if(cmp != 0)
return cmp;
a_fi_off++;
b_fi_off++;
}
return 0;
}
typedef struct MD_REF_DEF_LIST_tag MD_REF_DEF_LIST;
struct MD_REF_DEF_LIST_tag {
int32_t n_ref_defs;
int32_t alloc_ref_defs;
MD_REF_DEF* ref_defs[]; /* Valid items always point into ctx->ref_defs[] */
};
static int
md_ref_def_cmp(const void* a, const void* b)
{
const MD_REF_DEF* a_ref = *(const MD_REF_DEF**)a;
const MD_REF_DEF* b_ref = *(const MD_REF_DEF**)b;
if(a_ref->hash < b_ref->hash)
return -1;
else if(a_ref->hash > b_ref->hash)
return +1;
else
return md_link_label_cmp(a_ref->label, a_ref->label_size, b_ref->label, b_ref->label_size);
}
static int
md_ref_def_cmp_for_sort(const void* a, const void* b)
{
int cmp;
cmp = md_ref_def_cmp(a, b);
/* Ensure stability of the sorting. */
if(cmp == 0) {
const MD_REF_DEF* a_ref = *(const MD_REF_DEF**)a;
const MD_REF_DEF* b_ref = *(const MD_REF_DEF**)b;
if(a_ref < b_ref)
cmp = -1;
else if(a_ref > b_ref)
cmp = +1;
else
cmp = 0;
}
return cmp;
}
static int
md_build_ref_def_hashtable(MD_CTX* ctx)
{
int32_t i, j;
if(ctx->n_ref_defs == 0)
return 0;
ctx->ref_def_hashtable_size = (ctx->n_ref_defs * 5) / 4;
ctx->ref_def_hashtable = malloc(ctx->ref_def_hashtable_size * sizeof(void*));
if(ctx->ref_def_hashtable == NULL) {
MD_LOG("malloc() failed.");
goto abort;
}
memset(ctx->ref_def_hashtable, 0, ctx->ref_def_hashtable_size * sizeof(void*));
/* Each member of ctx->ref_def_hashtable[] can be:
* -- NULL,
* -- pointer to the MD_REF_DEF in ctx->ref_defs[], or
* -- pointer to a MD_REF_DEF_LIST, which holds multiple pointers to
* such MD_REF_DEFs.
*/
for(i = 0; i < ctx->n_ref_defs; i++) {
MD_REF_DEF* def = &ctx->ref_defs[i];
void* bucket;
MD_REF_DEF_LIST* list;
def->hash = md_link_label_hash(def->label, def->label_size);
bucket = ctx->ref_def_hashtable[def->hash % ctx->ref_def_hashtable_size];
if(bucket == NULL) {
/* The bucket is empty. Make it just point to the def. */
ctx->ref_def_hashtable[def->hash % ctx->ref_def_hashtable_size] = def;
continue;
}
if(ctx->ref_defs <= (MD_REF_DEF*) bucket && (MD_REF_DEF*) bucket < ctx->ref_defs + ctx->n_ref_defs) {
/* The bucket already contains one ref. def. Lets see whether it
* is the same label (ref. def. duplicate) or different one
* (hash conflict). */
MD_REF_DEF* old_def = (MD_REF_DEF*) bucket;
if(md_link_label_cmp(def->label, def->label_size, old_def->label, old_def->label_size) == 0) {
/* Duplicate label: Ignore this ref. def. */
continue;
}
/* Make the bucket complex, i.e. able to hold more ref. defs. */
list = (MD_REF_DEF_LIST*) malloc(sizeof(MD_REF_DEF_LIST) + 2 * sizeof(MD_REF_DEF*));
if(list == NULL) {
MD_LOG("malloc() failed.");
goto abort;
}
list->ref_defs[0] = old_def;
list->ref_defs[1] = def;
list->n_ref_defs = 2;
list->alloc_ref_defs = 2;
ctx->ref_def_hashtable[def->hash % ctx->ref_def_hashtable_size] = list;
continue;
}
/* Append the def to the complex bucket list.
*
* Note in this case we ignore potential duplicates to avoid expensive
* iterating over the complex bucket. Below, we revisit all the complex
* buckets and handle it more cheaply after the complex bucket contents
* is sorted. */
list = (MD_REF_DEF_LIST*) bucket;
if(list->n_ref_defs >= list->alloc_ref_defs) {
int32_t alloc_ref_defs = list->alloc_ref_defs + list->alloc_ref_defs / 2;
MD_REF_DEF_LIST* list_tmp = (MD_REF_DEF_LIST*) realloc(list,
sizeof(MD_REF_DEF_LIST) + alloc_ref_defs * sizeof(MD_REF_DEF*));
if(list_tmp == NULL) {
MD_LOG("realloc() failed.");
goto abort;
}
list = list_tmp;
list->alloc_ref_defs = alloc_ref_defs;
ctx->ref_def_hashtable[def->hash % ctx->ref_def_hashtable_size] = list;
}
list->ref_defs[list->n_ref_defs] = def;
list->n_ref_defs++;
}
/* Sort the complex buckets so we can use bsearch() with them. */
for(i = 0; i < ctx->ref_def_hashtable_size; i++) {
void* bucket = ctx->ref_def_hashtable[i];
MD_REF_DEF_LIST* list;
if(bucket == NULL)
continue;
if(ctx->ref_defs <= (MD_REF_DEF*) bucket && (MD_REF_DEF*) bucket < ctx->ref_defs + ctx->n_ref_defs)
continue;
list = (MD_REF_DEF_LIST*) bucket;
qsort(list->ref_defs, list->n_ref_defs, sizeof(MD_REF_DEF*), md_ref_def_cmp_for_sort);
/* Disable all duplicates in the complex bucket by forcing all such
* records to point to the 1st such ref. def. I.e. no matter which
* record is found during the lookup, it will always point to the right
* ref. def. in ctx->ref_defs[]. */
for(j = 1; j < list->n_ref_defs; j++) {
if(md_ref_def_cmp(&list->ref_defs[j-1], &list->ref_defs[j]) == 0)
list->ref_defs[j] = list->ref_defs[j-1];
}
}
return 0;
abort:
return -1;
}
static void
md_free_ref_def_hashtable(MD_CTX* ctx)
{
if(ctx->ref_def_hashtable != NULL) {
int32_t i;
for(i = 0; i < ctx->ref_def_hashtable_size; i++) {
void* bucket = ctx->ref_def_hashtable[i];
if(bucket == NULL)
continue;
if(ctx->ref_defs <= (MD_REF_DEF*) bucket && (MD_REF_DEF*) bucket < ctx->ref_defs + ctx->n_ref_defs)
continue;
free(bucket);
}
free(ctx->ref_def_hashtable);
}
}
static const MD_REF_DEF*
md_lookup_ref_def(MD_CTX* ctx, const CHAR* label, SZ label_size)
{
uint32_t hash;
void* bucket;
if(ctx->ref_def_hashtable_size == 0)
return NULL;
hash = md_link_label_hash(label, label_size);
bucket = ctx->ref_def_hashtable[hash % ctx->ref_def_hashtable_size];
if(bucket == NULL) {
return NULL;
} else if(ctx->ref_defs <= (MD_REF_DEF*) bucket && (MD_REF_DEF*) bucket < ctx->ref_defs + ctx->n_ref_defs) {
const MD_REF_DEF* def = (MD_REF_DEF*) bucket;
if(md_link_label_cmp(def->label, def->label_size, label, label_size) == 0)
return def;
else
return NULL;
} else {
MD_REF_DEF_LIST* list = (MD_REF_DEF_LIST*) bucket;
MD_REF_DEF key_buf;
const MD_REF_DEF* key = &key_buf;
const MD_REF_DEF** ret;
key_buf.label = (CHAR*) label;
key_buf.label_size = label_size;
key_buf.hash = md_link_label_hash(key_buf.label, key_buf.label_size);
ret = (const MD_REF_DEF**) bsearch(&key, list->ref_defs,
list->n_ref_defs, sizeof(MD_REF_DEF*), md_ref_def_cmp);
if(ret != NULL)
return *ret;
else
return NULL;
}
}
/***************************
*** Recognizing Links ***
***************************/
/* Note this code is partially shared between processing inlines and blocks
* as reference definitions and links share some helper parser functions.
*/
typedef struct MD_LINK_ATTR_tag MD_LINK_ATTR;
struct MD_LINK_ATTR_tag {
OFF dest_beg;
OFF dest_end;
CHAR* title;
SZ title_size;
int title_needs_free;
};
static int
md_is_link_label(MD_CTX* ctx, const MD_LINE* lines, int32_t n_lines, OFF beg,
OFF* p_end, int32_t* p_beg_line_index, int32_t* p_end_line_index,
OFF* p_contents_beg, OFF* p_contents_end)
{
OFF off = beg;
OFF contents_beg = 0;
OFF contents_end = 0;
int32_t line_index = 0;
int32_t len = 0;
if(CH(off) != _T('['))
return FALSE;
off++;
while(1) {
OFF line_end = lines[line_index].end;
while(off < line_end) {
if(CH(off) == _T('\\') && off+1 < ctx->size && (ISPUNCT(off+1) || ISNEWLINE(off+1))) {
if(contents_end == 0) {
contents_beg = off;
*p_beg_line_index = line_index;
}
contents_end = off + 2;
off += 2;
} else if(CH(off) == _T('[')) {
return FALSE;
} else if(CH(off) == _T(']')) {
if(contents_beg < contents_end) {
/* Success. */
*p_contents_beg = contents_beg;
*p_contents_end = contents_end;
*p_end = off+1;
*p_end_line_index = line_index;
return TRUE;
} else {
/* Link label must have some non-whitespace contents. */
return FALSE;
}
} else {
unsigned codepoint;
SZ char_size;
codepoint = md_decode_unicode(ctx->text, off, ctx->size, &char_size);
if(!ISUNICODEWHITESPACE_(codepoint)) {
if(contents_end == 0) {
contents_beg = off;
*p_beg_line_index = line_index;
}
contents_end = off + char_size;
}
off += char_size;
}
len++;
if(len > 999)
return FALSE;
}
line_index++;
len++;
if(line_index < n_lines)
off = lines[line_index].beg;
else
break;
}
return FALSE;
}
static int
md_is_link_destination_A(MD_CTX* ctx, OFF beg, OFF max_end, OFF* p_end,
OFF* p_contents_beg, OFF* p_contents_end)
{
OFF off = beg;
if(off >= max_end || CH(off) != _T('<'))
return FALSE;
off++;
while(off < max_end) {
if(CH(off) == _T('\\') && off+1 < max_end && ISPUNCT(off+1)) {
off += 2;
continue;
}
if(ISNEWLINE(off) || CH(off) == _T('<'))
return FALSE;
if(CH(off) == _T('>')) {
/* Success. */
*p_contents_beg = beg+1;
*p_contents_end = off;
*p_end = off+1;
return TRUE;
}
off++;
}
return FALSE;
}
static int
md_is_link_destination_B(MD_CTX* ctx, OFF beg, OFF max_end, OFF* p_end,
OFF* p_contents_beg, OFF* p_contents_end)
{
OFF off = beg;
int parenthesis_level = 0;
while(off < max_end) {
if(CH(off) == _T('\\') && off+1 < max_end && ISPUNCT(off+1)) {
off += 2;
continue;
}
if(ISWHITESPACE(off) || ISCNTRL(off))
break;
/* Link destination may include balanced pairs of unescaped '(' ')'.
* Note we limit the maximal nesting level by 32 to protect us from
* https://github.com/jgm/cmark/issues/214 */
if(CH(off) == _T('(')) {
parenthesis_level++;
if(parenthesis_level > 32)
return FALSE;
} else if(CH(off) == _T(')')) {
if(parenthesis_level == 0)
break;
parenthesis_level--;
}
off++;
}
if(parenthesis_level != 0 || off == beg)
return FALSE;
/* Success. */
*p_contents_beg = beg;
*p_contents_end = off;
*p_end = off;
return TRUE;
}
static inline int
md_is_link_destination(MD_CTX* ctx, OFF beg, OFF max_end, OFF* p_end,
OFF* p_contents_beg, OFF* p_contents_end)
{
if(CH(beg) == _T('<'))
return md_is_link_destination_A(ctx, beg, max_end, p_end, p_contents_beg, p_contents_end);
else
return md_is_link_destination_B(ctx, beg, max_end, p_end, p_contents_beg, p_contents_end);
}
static int
md_is_link_title(MD_CTX* ctx, const MD_LINE* lines, int32_t n_lines, OFF beg,
OFF* p_end, int32_t* p_beg_line_index, int32_t* p_end_line_index,
OFF* p_contents_beg, OFF* p_contents_end)
{
OFF off = beg;
CHAR closer_char;
int32_t line_index = 0;
/* White space with up to one line break. */
while(off < lines[line_index].end && ISWHITESPACE(off))
off++;
if(off >= lines[line_index].end) {
line_index++;
if(line_index >= n_lines)
return FALSE;
off = lines[line_index].beg;
}
if(off == beg)
return FALSE;
*p_beg_line_index = line_index;
/* First char determines how to detect end of it. */
switch(CH(off)) {
case _T('"'): closer_char = _T('"'); break;
case _T('\''): closer_char = _T('\''); break;
case _T('('): closer_char = _T(')'); break;
default: return FALSE;
}
off++;
*p_contents_beg = off;
while(line_index < n_lines) {
OFF line_end = lines[line_index].end;
while(off < line_end) {
if(CH(off) == _T('\\') && off+1 < ctx->size && (ISPUNCT(off+1) || ISNEWLINE(off+1))) {
off++;
} else if(CH(off) == closer_char) {
/* Success. */
*p_contents_end = off;
*p_end = off+1;
*p_end_line_index = line_index;
return TRUE;
} else if(closer_char == _T(')') && CH(off) == _T('(')) {
/* ()-style title cannot contain (unescaped '(')) */
return FALSE;
}
off++;
}
line_index++;
}
return FALSE;
}
/* Returns 0 if it is not a reference definition.
*
* Returns N > 0 if it is a reference definition. N then corresponds to the
* number of lines forming it). In this case the definition is stored for
* resolving any links referring to it.
*
* Returns -1 in case of an error (out of memory).
*/
static int32_t
md_is_link_reference_definition(MD_CTX* ctx, const MD_LINE* lines, int32_t n_lines)
{
OFF label_contents_beg;
OFF label_contents_end;
int32_t label_contents_line_index = -1;
int label_is_multiline = FALSE;
OFF dest_contents_beg;
OFF dest_contents_end;
OFF title_contents_beg;
OFF title_contents_end;
int32_t title_contents_line_index;
int title_is_multiline = FALSE;
OFF off;
int32_t line_index = 0;
int32_t tmp_line_index;
MD_REF_DEF* def = NULL;
int ret = 0;
/* Link label. */
if(!md_is_link_label(ctx, lines, n_lines, lines[0].beg,
&off, &label_contents_line_index, &line_index,
&label_contents_beg, &label_contents_end))
return FALSE;
label_is_multiline = (label_contents_line_index != line_index);
/* Colon. */
if(off >= lines[line_index].end || CH(off) != _T(':'))
return FALSE;
off++;
/* Optional white space with up to one line break. */
while(off < lines[line_index].end && ISWHITESPACE(off))
off++;
if(off >= lines[line_index].end) {
line_index++;
if(line_index >= n_lines)
return FALSE;
off = lines[line_index].beg;
}
/* Link destination. */
if(!md_is_link_destination(ctx, off, lines[line_index].end,
&off, &dest_contents_beg, &dest_contents_end))
return FALSE;
/* (Optional) title. Note we interpret it as an title only if nothing
* more follows on its last line. */
if(md_is_link_title(ctx, lines + line_index, n_lines - line_index, off,
&off, &title_contents_line_index, &tmp_line_index,
&title_contents_beg, &title_contents_end)
&& off >= lines[line_index + tmp_line_index].end)
{
title_is_multiline = (tmp_line_index != title_contents_line_index);
title_contents_line_index += line_index;
line_index += tmp_line_index;
} else {
/* Not a title. */
title_is_multiline = FALSE;
title_contents_beg = off;
title_contents_end = off;
title_contents_line_index = 0;
}
/* Nothing more can follow on the last line. */
if(off < lines[line_index].end)
return FALSE;
/* So, it _is_ a reference definition. Remember it. */
if(ctx->n_ref_defs >= ctx->alloc_ref_defs) {
MD_REF_DEF* new_defs;
ctx->alloc_ref_defs = (ctx->alloc_ref_defs > 0
? ctx->alloc_ref_defs + ctx->alloc_ref_defs / 2
: 16);
new_defs = (MD_REF_DEF*) realloc(ctx->ref_defs, ctx->alloc_ref_defs * sizeof(MD_REF_DEF));
if(new_defs == NULL) {
MD_LOG("realloc() failed.");
goto abort;
}
ctx->ref_defs = new_defs;
}
def = &ctx->ref_defs[ctx->n_ref_defs];
memset(def, 0, sizeof(MD_REF_DEF));
if(label_is_multiline) {
MD_CHECK(md_merge_lines_alloc(ctx, label_contents_beg, label_contents_end,
lines + label_contents_line_index, n_lines - label_contents_line_index,
_T(' '), &def->label, &def->label_size));
def->label_needs_free = TRUE;
} else {
def->label = (CHAR*) STR(label_contents_beg);
def->label_size = label_contents_end - label_contents_beg;
}
if(title_is_multiline) {
MD_CHECK(md_merge_lines_alloc(ctx, title_contents_beg, title_contents_end,
lines + title_contents_line_index, n_lines - title_contents_line_index,
_T('\n'), &def->title, &def->title_size));
def->title_needs_free = TRUE;
} else {
def->title = (CHAR*) STR(title_contents_beg);
def->title_size = title_contents_end - title_contents_beg;
}
def->dest_beg = dest_contents_beg;
def->dest_end = dest_contents_end;
/* Success. */
ctx->n_ref_defs++;
return line_index + 1;
abort:
/* Failure. */
if(def != NULL && def->label_needs_free)
free(def->label);
if(def != NULL && def->title_needs_free)
free(def->title);
return ret;
}
static int
md_is_link_reference(MD_CTX* ctx, const MD_LINE* lines, int32_t n_lines,
OFF beg, OFF end, MD_LINK_ATTR* attr)
{
const MD_REF_DEF* def;
const MD_LINE* beg_line;
const MD_LINE* end_line;
CHAR* label;
SZ label_size;
int ret;
MD_ASSERT(CH(beg) == _T('[') || CH(beg) == _T('!'));
MD_ASSERT(CH(end-1) == _T(']'));
beg += (CH(beg) == _T('!') ? 2 : 1);
end--;
/* Find lines corresponding to the beg and end positions. */
MD_ASSERT(lines[0].beg <= beg);
beg_line = lines;
while(beg >= beg_line->end)
beg_line++;
MD_ASSERT(end <= lines[n_lines-1].end);
end_line = beg_line;
while(end >= end_line->end)
end_line++;
if(beg_line != end_line) {
MD_CHECK(md_merge_lines_alloc(ctx, beg, end, beg_line,
n_lines - (beg_line - lines), _T(' '), &label, &label_size));
} else {
label = (CHAR*) STR(beg);
label_size = end - beg;
}
def = md_lookup_ref_def(ctx, label, label_size);
if(def != NULL) {
attr->dest_beg = def->dest_beg;
attr->dest_end = def->dest_end;
attr->title = def->title;
attr->title_size = def->title_size;
attr->title_needs_free = FALSE;
}
if(beg_line != end_line)
free(label);
ret = (def != NULL);
abort:
return ret;
}
static int
md_is_inline_link_spec(MD_CTX* ctx, const MD_LINE* lines, int32_t n_lines,
OFF beg, OFF* p_end, MD_LINK_ATTR* attr)
{
int32_t line_index = 0;
int32_t tmp_line_index;
OFF title_contents_beg;
OFF title_contents_end;
int32_t title_contents_line_index;
int title_is_multiline;
OFF off = beg;
int ret = FALSE;
while(off >= lines[line_index].end)
line_index++;
MD_ASSERT(CH(off) == _T('('));
off++;
/* Optional white space with up to one line break. */
while(off < lines[line_index].end && ISWHITESPACE(off))
off++;
if(off >= lines[line_index].end && ISNEWLINE(off)) {
line_index++;
if(line_index >= n_lines)
return FALSE;
off = lines[line_index].beg;
}
/* Link destination may be omitted, but only when not also having a title. */
if(off < ctx->size && CH(off) == _T(')')) {
attr->dest_beg = off;
attr->dest_end = off;
attr->title = NULL;
attr->title_size = 0;
attr->title_needs_free = FALSE;
off++;
*p_end = off;
return TRUE;
}
/* Link destination. */
if(!md_is_link_destination(ctx, off, lines[line_index].end,
&off, &attr->dest_beg, &attr->dest_end))
return FALSE;
/* (Optional) title. */
if(md_is_link_title(ctx, lines + line_index, n_lines - line_index, off,
&off, &title_contents_line_index, &tmp_line_index,
&title_contents_beg, &title_contents_end))
{
title_is_multiline = (tmp_line_index != title_contents_line_index);
title_contents_line_index += line_index;
line_index += tmp_line_index;
} else {
/* Not a title. */
title_is_multiline = FALSE;
title_contents_beg = off;
title_contents_end = off;
title_contents_line_index = 0;
}
/* Optional whitespace followed with final ')'. */
while(off < lines[line_index].end && ISWHITESPACE(off))
off++;
if(off >= lines[line_index].end && ISNEWLINE(off)) {
line_index++;
if(line_index >= n_lines)
return FALSE;
off = lines[line_index].beg;
}
if(CH(off) != _T(')'))
goto abort;
off++;
if(title_contents_beg >= title_contents_end) {
attr->title = NULL;
attr->title_size = 0;
attr->title_needs_free = FALSE;
} else if(!title_is_multiline) {
attr->title = (CHAR*) STR(title_contents_beg);
attr->title_size = title_contents_end - title_contents_beg;
attr->title_needs_free = FALSE;
} else {
MD_CHECK(md_merge_lines_alloc(ctx, title_contents_beg, title_contents_end,
lines + title_contents_line_index, n_lines - title_contents_line_index,
_T('\n'), &attr->title, &attr->title_size));
attr->title_needs_free = TRUE;
}
*p_end = off;
ret = TRUE;
abort:
return ret;
}
static void
md_free_ref_defs(MD_CTX* ctx)
{
int32_t i;
for(i = 0; i < ctx->n_ref_defs; i++) {
MD_REF_DEF* def = &ctx->ref_defs[i];
if(def->label_needs_free)
free(def->label);
if(def->title_needs_free)
free(def->title);
}
free(ctx->ref_defs);
}
/******************************************
*** Processing Inlines (a.k.a Spans) ***
******************************************/
/* We process inlines in few phases:
*
* (1) We go through the block text and collect all significant characters
* which may start/end a span or some other significant position into
* ctx->marks[]. Core of this is what md_collect_marks() does.
*
* We also do some very brief preliminary context-less analysis, whether
* it might be opener or closer (e.g. of an emphasis span).
*
* This speeds the other steps as we do not need to re-iterate over all
* characters anymore.
*
* (2) We analyze each potential mark types, in order by their precedence.
*
* In each md_analyze_XXX() function, we re-iterate list of the marks,
* skipping already resolved regions (in preceding precedences) and try to
* resolve them.
*
* (2.1) For trivial marks, which are single (e.g. HTML entity), we just mark
* them as resolved.
*
* (2.2) For range-type marks, we analyze whether the mark could be closer
* and, if yes, whether there is some preceding opener it could satisfy.
*
* If not we check whether it could be really an opener and if yes, we
* remember it so subsequent closers may resolve it.
*
* (3) Finally, when all marks were analyzed, we render the block contents
* by calling MD_RENDERER::text() callback, interrupting by ::enter_span()
* or ::close_span() whenever we reach a resolved mark.
*/
/* The mark structure.
*
* '\\': Maybe escape sequence.
* '\0': NULL char.
* '*': Maybe (strong) emphasis start/end.
* '_': Maybe (strong) emphasis start/end.
* '~': Maybe strikethrough start/end (needs MD_FLAG_STRIKETHROUGH).
* '`': Maybe code span start/end.
* '&': Maybe start of entity.
* ';': Maybe end of entity.
* '<': Maybe start of raw HTML or autolink.
* '>': Maybe end of raw HTML or autolink.
* '[': Maybe start of link label or link text.
* '!': Equivalent of '[' for image.
* ']': Maybe end of link label or link text.
* '@': Maybe permissive e-mail auto-link (needs MD_FLAG_PERMISSIVEEMAILAUTOLINKS).
* ':': Maybe permissive URL auto-link (needs MD_FLAG_PERMISSIVEURLAUTOLINKS).
* '.': Maybe permissive WWW auto-link (needs MD_FLAG_PERMISSIVEWWWAUTOLINKS).
* 'D': Dummy mark, it reserves a space for splitting a previous mark
* (e.g. emphasis) or to make more space for storing some special data
* related to the preceding mark (e.g. link).
*
* Note that not all instances of these chars in the text imply creation of the
* structure. Only those which have (or may have, after we see more context)
* the special meaning.
*
* (Keep this struct as small as possible to fit as much of them into CPU
* cache line.)
*/
struct MD_MARK_tag {
OFF beg;
OFF end;
/* For unresolved openers, 'prev' and 'next' form the chain of open openers
* of given type 'ch'.
*
* During resolving, we disconnect from the chain and point to the
* corresponding counterpart so opener points to its closer and vice versa.
*/
int32_t prev;
int32_t next;
CHAR ch;
unsigned char flags;
};
/* Mark flags (these apply to ALL mark types). */
#define MD_MARK_POTENTIAL_OPENER 0x01 /* Maybe opener. */
#define MD_MARK_POTENTIAL_CLOSER 0x02 /* Maybe closer. */
#define MD_MARK_OPENER 0x04 /* Definitely opener. */
#define MD_MARK_CLOSER 0x08 /* Definitely closer. */
#define MD_MARK_RESOLVED 0x10 /* Resolved in any definite way. */
/* Mark flags specific for various mark types (so they can share bits). */
#define MD_MARK_EMPH_INTRAWORD 0x20 /* Helper for the "rule of 3". */
#define MD_MARK_EMPH_MOD3_0 0x40
#define MD_MARK_EMPH_MOD3_1 0x80
#define MD_MARK_EMPH_MOD3_2 (0x40 | 0x80)
#define MD_MARK_EMPH_MOD3_MASK (0x40 | 0x80)
#define MD_MARK_AUTOLINK 0x20 /* Distinguisher for '<', '>'. */
#define MD_MARK_VALIDPERMISSIVEAUTOLINK 0x20 /* For permissive autolinks. */
static MD_MARKCHAIN*
md_asterisk_chain(MD_CTX* ctx, unsigned flags)
{
switch(flags & (MD_MARK_EMPH_INTRAWORD | MD_MARK_EMPH_MOD3_MASK)) {
case MD_MARK_EMPH_INTRAWORD | MD_MARK_EMPH_MOD3_0: return &ASTERISK_OPENERS_intraword_mod3_0;
case MD_MARK_EMPH_INTRAWORD | MD_MARK_EMPH_MOD3_1: return &ASTERISK_OPENERS_intraword_mod3_1;
case MD_MARK_EMPH_INTRAWORD | MD_MARK_EMPH_MOD3_2: return &ASTERISK_OPENERS_intraword_mod3_2;
case MD_MARK_EMPH_MOD3_0: return &ASTERISK_OPENERS_extraword_mod3_0;
case MD_MARK_EMPH_MOD3_1: return &ASTERISK_OPENERS_extraword_mod3_1;
case MD_MARK_EMPH_MOD3_2: return &ASTERISK_OPENERS_extraword_mod3_2;
default: MD_UNREACHABLE();
}
return NULL;
}
static MD_MARKCHAIN*
md_mark_chain(MD_CTX* ctx, int32_t mark_index)
{
MD_MARK* mark = &ctx->marks[mark_index];
switch(mark->ch) {
case _T('*'): return md_asterisk_chain(ctx, mark->flags);
case _T('_'): return &UNDERSCORE_OPENERS;
case _T('~'): return (mark->end - mark->beg == 1) ? &TILDE_OPENERS_1 : &TILDE_OPENERS_2;
case _T('['): return &BRACKET_OPENERS;
case _T('|'): return &TABLECELLBOUNDARIES;
default: return NULL;
}
}
static MD_MARK*
md_push_mark(