Coverage Report

Created: 2025-11-16 09:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/workdir/UnpackedTarball/md4c/src/md4c.c
Line
Count
Source
1
/*
2
 * MD4C: Markdown parser for C
3
 * (http://github.com/mity/md4c)
4
 *
5
 * Copyright (c) 2016-2024 Martin Mitáš
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a
8
 * copy of this software and associated documentation files (the "Software"),
9
 * to deal in the Software without restriction, including without limitation
10
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11
 * and/or sell copies of the Software, and to permit persons to whom the
12
 * Software is furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in
15
 * all copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23
 * IN THE SOFTWARE.
24
 */
25
26
#include "md4c.h"
27
28
#include <limits.h>
29
#include <stdio.h>
30
#include <stdlib.h>
31
#include <string.h>
32
33
34
/*****************************
35
 ***  Miscellaneous Stuff  ***
36
 *****************************/
37
38
#if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199409L
39
    /* C89/90 or old compilers in general may not understand "inline". */
40
    #if defined __GNUC__
41
        #define inline __inline__
42
    #elif defined _MSC_VER
43
        #define inline __inline
44
    #else
45
        #define inline
46
    #endif
47
#endif
48
49
/* Make the UTF-8 support the default. */
50
#if !defined MD4C_USE_ASCII && !defined MD4C_USE_UTF8 && !defined MD4C_USE_UTF16
51
    #define MD4C_USE_UTF8
52
#endif
53
54
/* Magic for making wide literals with MD4C_USE_UTF16. */
55
#ifdef _T
56
    #undef _T
57
#endif
58
#if defined MD4C_USE_UTF16
59
    #define _T(x)           L##x
60
#else
61
0
    #define _T(x)           x
62
#endif
63
64
/* Misc. macros. */
65
0
#define SIZEOF_ARRAY(a)     (sizeof(a) / sizeof(a[0]))
66
67
#define STRINGIZE_(x)       #x
68
#define STRINGIZE(x)        STRINGIZE_(x)
69
70
#define MAX(a,b)            ((a) > (b) ? (a) : (b))
71
0
#define MIN(a,b)            ((a) < (b) ? (a) : (b))
72
73
#ifndef TRUE
74
0
    #define TRUE            1
75
0
    #define FALSE           0
76
#endif
77
78
#define MD_LOG(msg)                                                     \
79
0
    do {                                                                \
80
0
        if(ctx->parser.debug_log != NULL)                               \
81
0
            ctx->parser.debug_log((msg), ctx->userdata);                \
82
0
    } while(0)
83
84
#ifdef DEBUG
85
    #define MD_ASSERT(cond)                                             \
86
            do {                                                        \
87
                if(!(cond)) {                                           \
88
                    MD_LOG(__FILE__ ":" STRINGIZE(__LINE__) ": "        \
89
                           "Assertion '" STRINGIZE(cond) "' failed.");  \
90
                    exit(1);                                            \
91
                }                                                       \
92
            } while(0)
93
94
    #define MD_UNREACHABLE()        MD_ASSERT(1 == 0)
95
#else
96
    #ifdef __GNUC__
97
0
        #define MD_ASSERT(cond)     do { if(!(cond)) __builtin_unreachable(); } while(0)
98
0
        #define MD_UNREACHABLE()    do { __builtin_unreachable(); } while(0)
99
    #elif defined _MSC_VER  &&  _MSC_VER > 120
100
        #define MD_ASSERT(cond)     do { __assume(cond); } while(0)
101
        #define MD_UNREACHABLE()    do { __assume(0); } while(0)
102
    #else
103
        #define MD_ASSERT(cond)     do {} while(0)
104
        #define MD_UNREACHABLE()    do {} while(0)
105
    #endif
106
#endif
107
108
/* For falling through case labels in switch statements. */
109
#if defined __clang__ && __clang_major__ >= 12
110
0
    #define MD_FALLTHROUGH()        __attribute__((fallthrough))
111
#elif defined __GNUC__ && __GNUC__ >= 7
112
    #define MD_FALLTHROUGH()        __attribute__((fallthrough))
113
#else
114
    #define MD_FALLTHROUGH()        ((void)0)
115
#endif
116
117
/* Suppress "unused parameter" warnings. */
118
0
#define MD_UNUSED(x)                ((void)x)
119
120
121
/******************************
122
 ***  Some internal limits  ***
123
 ******************************/
124
125
/* We limit code span marks to lower than 32 backticks. This solves the
126
 * pathologic case of too many openers, each of different length: Their
127
 * resolving would be then O(n^2). */
128
0
#define CODESPAN_MARK_MAXLEN    32
129
130
/* We limit column count of tables to prevent quadratic explosion of output
131
 * from pathological input of a table thousands of columns and thousands
132
 * of rows where rows are requested with as little as single character
133
 * per-line, relying on us to "helpfully" fill all the missing "<td></td>". */
134
0
#define TABLE_MAXCOLCOUNT       128
135
136
137
/************************
138
 ***  Internal Types  ***
139
 ************************/
140
141
/* These are omnipresent so lets save some typing. */
142
0
#define CHAR    MD_CHAR
143
0
#define SZ      MD_SIZE
144
0
#define OFF     MD_OFFSET
145
146
typedef struct MD_MARK_tag MD_MARK;
147
typedef struct MD_BLOCK_tag MD_BLOCK;
148
typedef struct MD_CONTAINER_tag MD_CONTAINER;
149
typedef struct MD_REF_DEF_tag MD_REF_DEF;
150
151
152
/* During analyzes of inline marks, we need to manage stacks of unresolved
153
 * openers of the given type.
154
 * The stack connects the marks via MD_MARK::next;
155
 */
156
typedef struct MD_MARKSTACK_tag MD_MARKSTACK;
157
struct MD_MARKSTACK_tag {
158
    int top;        /* -1 if empty. */
159
};
160
161
/* Context propagated through all the parsing. */
162
typedef struct MD_CTX_tag MD_CTX;
163
struct MD_CTX_tag {
164
    /* Immutable stuff (parameters of md_parse()). */
165
    const CHAR* text;
166
    SZ size;
167
    MD_PARSER parser;
168
    void* userdata;
169
170
    /* When this is true, it allows some optimizations. */
171
    int doc_ends_with_newline;
172
173
    /* Helper temporary growing buffer. */
174
    CHAR* buffer;
175
    unsigned alloc_buffer;
176
177
    /* Reference definitions. */
178
    MD_REF_DEF* ref_defs;
179
    int n_ref_defs;
180
    int alloc_ref_defs;
181
    void** ref_def_hashtable;
182
    int ref_def_hashtable_size;
183
184
    /* Stack of inline/span markers.
185
     * This is only used for parsing a single block contents but by storing it
186
     * here we may reuse the stack for subsequent blocks; i.e. we have fewer
187
     * (re)allocations. */
188
    MD_MARK* marks;
189
    int n_marks;
190
    int alloc_marks;
191
192
#if defined MD4C_USE_UTF16
193
    char mark_char_map[128];
194
#else
195
    char mark_char_map[256];
196
#endif
197
198
    /* For resolving of inline spans. */
199
    MD_MARKSTACK opener_stacks[16];
200
0
#define ASTERISK_OPENERS_oo_mod3_0      (ctx->opener_stacks[0])     /* Opener-only */
201
#define ASTERISK_OPENERS_oo_mod3_1      (ctx->opener_stacks[1])
202
#define ASTERISK_OPENERS_oo_mod3_2      (ctx->opener_stacks[2])
203
#define ASTERISK_OPENERS_oc_mod3_0      (ctx->opener_stacks[3])     /* Both opener and closer candidate */
204
#define ASTERISK_OPENERS_oc_mod3_1      (ctx->opener_stacks[4])
205
#define ASTERISK_OPENERS_oc_mod3_2      (ctx->opener_stacks[5])
206
0
#define UNDERSCORE_OPENERS_oo_mod3_0    (ctx->opener_stacks[6])     /* Opener-only */
207
#define UNDERSCORE_OPENERS_oo_mod3_1    (ctx->opener_stacks[7])
208
#define UNDERSCORE_OPENERS_oo_mod3_2    (ctx->opener_stacks[8])
209
#define UNDERSCORE_OPENERS_oc_mod3_0    (ctx->opener_stacks[9])     /* Both opener and closer candidate */
210
#define UNDERSCORE_OPENERS_oc_mod3_1    (ctx->opener_stacks[10])
211
#define UNDERSCORE_OPENERS_oc_mod3_2    (ctx->opener_stacks[11])
212
0
#define TILDE_OPENERS_1                 (ctx->opener_stacks[12])
213
0
#define TILDE_OPENERS_2                 (ctx->opener_stacks[13])
214
0
#define BRACKET_OPENERS                 (ctx->opener_stacks[14])
215
0
#define DOLLAR_OPENERS                  (ctx->opener_stacks[15])
216
217
    /* Stack of dummies which need to call free() for pointers stored in them.
218
     * These are constructed during inline parsing and freed after all the block
219
     * is processed (i.e. all callbacks referring those strings are called). */
220
    MD_MARKSTACK ptr_stack;
221
222
    /* For resolving table rows. */
223
    int n_table_cell_boundaries;
224
    int table_cell_boundaries_head;
225
    int table_cell_boundaries_tail;
226
227
    /* For resolving links. */
228
    int unresolved_link_head;
229
    int unresolved_link_tail;
230
231
    /* For resolving raw HTML. */
232
    OFF html_comment_horizon;
233
    OFF html_proc_instr_horizon;
234
    OFF html_decl_horizon;
235
    OFF html_cdata_horizon;
236
237
    /* For block analysis.
238
     * Notes:
239
     *   -- It holds MD_BLOCK as well as MD_LINE structures. After each
240
     *      MD_BLOCK, its (multiple) MD_LINE(s) follow.
241
     *   -- For MD_BLOCK_HTML and MD_BLOCK_CODE, MD_VERBATIMLINE(s) are used
242
     *      instead of MD_LINE(s).
243
     */
244
    void* block_bytes;
245
    MD_BLOCK* current_block;
246
    int n_block_bytes;
247
    int alloc_block_bytes;
248
249
    /* For container block analysis. */
250
    MD_CONTAINER* containers;
251
    int n_containers;
252
    int alloc_containers;
253
254
    /* Minimal indentation to call the block "indented code block". */
255
    unsigned code_indent_offset;
256
257
    /* Contextual info for line analysis. */
258
    SZ code_fence_length;   /* For checking closing fence length. */
259
    int html_block_type;    /* For checking closing raw HTML condition. */
260
    int last_line_has_list_loosening_effect;
261
    int last_list_item_starts_with_two_blank_lines;
262
};
263
264
enum MD_LINETYPE_tag {
265
    MD_LINE_BLANK,
266
    MD_LINE_HR,
267
    MD_LINE_ATXHEADER,
268
    MD_LINE_SETEXTHEADER,
269
    MD_LINE_SETEXTUNDERLINE,
270
    MD_LINE_INDENTEDCODE,
271
    MD_LINE_FENCEDCODE,
272
    MD_LINE_HTML,
273
    MD_LINE_TEXT,
274
    MD_LINE_TABLE,
275
    MD_LINE_TABLEUNDERLINE
276
};
277
typedef enum MD_LINETYPE_tag MD_LINETYPE;
278
279
typedef struct MD_LINE_ANALYSIS_tag MD_LINE_ANALYSIS;
280
struct MD_LINE_ANALYSIS_tag {
281
    MD_LINETYPE type;
282
    unsigned data;
283
    int enforce_new_block;
284
    OFF beg;
285
    OFF end;
286
    unsigned indent;        /* Indentation level. */
287
};
288
289
typedef struct MD_LINE_tag MD_LINE;
290
struct MD_LINE_tag {
291
    OFF beg;
292
    OFF end;
293
};
294
295
typedef struct MD_VERBATIMLINE_tag MD_VERBATIMLINE;
296
struct MD_VERBATIMLINE_tag {
297
    OFF beg;
298
    OFF end;
299
    OFF indent;
300
};
301
302
303
/*****************
304
 ***  Helpers  ***
305
 *****************/
306
307
/* Character accessors. */
308
0
#define CH(off)                 (ctx->text[(off)])
309
0
#define STR(off)                (ctx->text + (off))
310
311
/* Character classification.
312
 * Note we assume ASCII compatibility of code points < 128 here. */
313
0
#define ISIN_(ch, ch_min, ch_max)       ((ch_min) <= (unsigned)(ch) && (unsigned)(ch) <= (ch_max))
314
0
#define ISANYOF_(ch, palette)           ((ch) != _T('\0')  &&  md_strchr((palette), (ch)) != NULL)
315
0
#define ISANYOF2_(ch, ch1, ch2)         ((ch) == (ch1) || (ch) == (ch2))
316
0
#define ISANYOF3_(ch, ch1, ch2, ch3)    ((ch) == (ch1) || (ch) == (ch2) || (ch) == (ch3))
317
0
#define ISASCII_(ch)                    ((unsigned)(ch) <= 127)
318
0
#define ISBLANK_(ch)                    (ISANYOF2_((ch), _T(' '), _T('\t')))
319
0
#define ISNEWLINE_(ch)                  (ISANYOF2_((ch), _T('\r'), _T('\n')))
320
0
#define ISWHITESPACE_(ch)               (ISBLANK_(ch) || ISANYOF2_((ch), _T('\v'), _T('\f')))
321
0
#define ISCNTRL_(ch)                    ((unsigned)(ch) <= 31 || (unsigned)(ch) == 127)
322
0
#define ISPUNCT_(ch)                    (ISIN_(ch, 33, 47) || ISIN_(ch, 58, 64) || ISIN_(ch, 91, 96) || ISIN_(ch, 123, 126))
323
0
#define ISUPPER_(ch)                    (ISIN_(ch, _T('A'), _T('Z')))
324
0
#define ISLOWER_(ch)                    (ISIN_(ch, _T('a'), _T('z')))
325
0
#define ISALPHA_(ch)                    (ISUPPER_(ch) || ISLOWER_(ch))
326
0
#define ISDIGIT_(ch)                    (ISIN_(ch, _T('0'), _T('9')))
327
0
#define ISXDIGIT_(ch)                   (ISDIGIT_(ch) || ISIN_(ch, _T('A'), _T('F')) || ISIN_(ch, _T('a'), _T('f')))
328
0
#define ISALNUM_(ch)                    (ISALPHA_(ch) || ISDIGIT_(ch))
329
330
0
#define ISANYOF(off, palette)           ISANYOF_(CH(off), (palette))
331
0
#define ISANYOF2(off, ch1, ch2)         ISANYOF2_(CH(off), (ch1), (ch2))
332
0
#define ISANYOF3(off, ch1, ch2, ch3)    ISANYOF3_(CH(off), (ch1), (ch2), (ch3))
333
0
#define ISASCII(off)                    ISASCII_(CH(off))
334
0
#define ISBLANK(off)                    ISBLANK_(CH(off))
335
0
#define ISNEWLINE(off)                  ISNEWLINE_(CH(off))
336
0
#define ISWHITESPACE(off)               ISWHITESPACE_(CH(off))
337
0
#define ISCNTRL(off)                    ISCNTRL_(CH(off))
338
0
#define ISPUNCT(off)                    ISPUNCT_(CH(off))
339
0
#define ISUPPER(off)                    ISUPPER_(CH(off))
340
#define ISLOWER(off)                    ISLOWER_(CH(off))
341
0
#define ISALPHA(off)                    ISALPHA_(CH(off))
342
0
#define ISDIGIT(off)                    ISDIGIT_(CH(off))
343
#define ISXDIGIT(off)                   ISXDIGIT_(CH(off))
344
0
#define ISALNUM(off)                    ISALNUM_(CH(off))
345
346
347
#if defined MD4C_USE_UTF16
348
    #define md_strchr wcschr
349
#else
350
0
    #define md_strchr strchr
351
#endif
352
353
354
/* Case insensitive check of string equality. */
355
static inline int
356
md_ascii_case_eq(const CHAR* s1, const CHAR* s2, SZ n)
357
0
{
358
0
    OFF i;
359
0
    for(i = 0; i < n; i++) {
360
0
        CHAR ch1 = s1[i];
361
0
        CHAR ch2 = s2[i];
362
363
0
        if(ISLOWER_(ch1))
364
0
            ch1 += ('A'-'a');
365
0
        if(ISLOWER_(ch2))
366
0
            ch2 += ('A'-'a');
367
0
        if(ch1 != ch2)
368
0
            return FALSE;
369
0
    }
370
0
    return TRUE;
371
0
}
372
373
static inline int
374
md_ascii_eq(const CHAR* s1, const CHAR* s2, SZ n)
375
0
{
376
0
    return memcmp(s1, s2, n * sizeof(CHAR)) == 0;
377
0
}
378
379
static int
380
md_text_with_null_replacement(MD_CTX* ctx, MD_TEXTTYPE type, const CHAR* str, SZ size)
381
0
{
382
0
    OFF off = 0;
383
0
    int ret = 0;
384
385
0
    while(1) {
386
0
        while(off < size  &&  str[off] != _T('\0'))
387
0
            off++;
388
389
0
        if(off > 0) {
390
0
            ret = ctx->parser.text(type, str, off, ctx->userdata);
391
0
            if(ret != 0)
392
0
                return ret;
393
394
0
            str += off;
395
0
            size -= off;
396
0
            off = 0;
397
0
        }
398
399
0
        if(off >= size)
400
0
            return 0;
401
402
0
        ret = ctx->parser.text(MD_TEXT_NULLCHAR, _T(""), 1, ctx->userdata);
403
0
        if(ret != 0)
404
0
            return ret;
405
0
        off++;
406
0
    }
407
0
}
408
409
410
#define MD_CHECK(func)                                                      \
411
0
    do {                                                                    \
412
0
        ret = (func);                                                       \
413
0
        if(ret < 0)                                                         \
414
0
            goto abort;                                                     \
415
0
    } while(0)
416
417
418
#define MD_TEMP_BUFFER(sz)                                                  \
419
0
    do {                                                                    \
420
0
        if(sz > ctx->alloc_buffer) {                                        \
421
0
            CHAR* new_buffer;                                               \
422
0
            SZ new_size = ((sz) + (sz) / 2 + 128) & ~127;                   \
423
0
                                                                            \
424
0
            new_buffer = realloc(ctx->buffer, new_size);                    \
425
0
            if(new_buffer == NULL) {                                        \
426
0
                MD_LOG("realloc() failed.");                                \
427
0
                ret = -1;                                                   \
428
0
                goto abort;                                                 \
429
0
            }                                                               \
430
0
                                                                            \
431
0
            ctx->buffer = new_buffer;                                       \
432
0
            ctx->alloc_buffer = new_size;                                   \
433
0
        }                                                                   \
434
0
    } while(0)
435
436
437
#define MD_ENTER_BLOCK(type, arg)                                           \
438
0
    do {                                                                    \
439
0
        ret = ctx->parser.enter_block((type), (arg), ctx->userdata);        \
440
0
        if(ret != 0) {                                                      \
441
0
            MD_LOG("Aborted from enter_block() callback.");                 \
442
0
            goto abort;                                                     \
443
0
        }                                                                   \
444
0
    } while(0)
445
446
#define MD_LEAVE_BLOCK(type, arg)                                           \
447
0
    do {                                                                    \
448
0
        ret = ctx->parser.leave_block((type), (arg), ctx->userdata);        \
449
0
        if(ret != 0) {                                                      \
450
0
            MD_LOG("Aborted from leave_block() callback.");                 \
451
0
            goto abort;                                                     \
452
0
        }                                                                   \
453
0
    } while(0)
454
455
#define MD_ENTER_SPAN(type, arg)                                            \
456
0
    do {                                                                    \
457
0
        ret = ctx->parser.enter_span((type), (arg), ctx->userdata);         \
458
0
        if(ret != 0) {                                                      \
459
0
            MD_LOG("Aborted from enter_span() callback.");                  \
460
0
            goto abort;                                                     \
461
0
        }                                                                   \
462
0
    } while(0)
463
464
#define MD_LEAVE_SPAN(type, arg)                                            \
465
0
    do {                                                                    \
466
0
        ret = ctx->parser.leave_span((type), (arg), ctx->userdata);         \
467
0
        if(ret != 0) {                                                      \
468
0
            MD_LOG("Aborted from leave_span() callback.");                  \
469
0
            goto abort;                                                     \
470
0
        }                                                                   \
471
0
    } while(0)
472
473
#define MD_TEXT(type, str, size)                                            \
474
0
    do {                                                                    \
475
0
        if(size > 0) {                                                      \
476
0
            ret = ctx->parser.text((type), (str), (size), ctx->userdata);   \
477
0
            if(ret != 0) {                                                  \
478
0
                MD_LOG("Aborted from text() callback.");                    \
479
0
                goto abort;                                                 \
480
0
            }                                                               \
481
0
        }                                                                   \
482
0
    } while(0)
483
484
#define MD_TEXT_INSECURE(type, str, size)                                   \
485
0
    do {                                                                    \
486
0
        if(size > 0) {                                                      \
487
0
            ret = md_text_with_null_replacement(ctx, type, str, size);      \
488
0
            if(ret != 0) {                                                  \
489
0
                MD_LOG("Aborted from text() callback.");                    \
490
0
                goto abort;                                                 \
491
0
            }                                                               \
492
0
        }                                                                   \
493
0
    } while(0)
494
495
496
/* If the offset falls into a gap between line, we return the following
497
 * line. */
498
static const MD_LINE*
499
md_lookup_line(OFF off, const MD_LINE* lines, MD_SIZE n_lines, MD_SIZE* p_line_index)
500
0
{
501
0
    MD_SIZE lo, hi;
502
0
    MD_SIZE pivot;
503
0
    const MD_LINE* line;
504
505
0
    lo = 0;
506
0
    hi = n_lines - 1;
507
0
    while(lo <= hi) {
508
0
        pivot = (lo + hi) / 2;
509
0
        line = &lines[pivot];
510
511
0
        if(off < line->beg) {
512
0
            if(hi == 0  ||  lines[hi-1].end < off) {
513
0
                if(p_line_index != NULL)
514
0
                    *p_line_index = pivot;
515
0
                return line;
516
0
            }
517
0
            hi = pivot - 1;
518
0
        } else if(off > line->end) {
519
0
            lo = pivot + 1;
520
0
        } else {
521
0
            if(p_line_index != NULL)
522
0
                *p_line_index = pivot;
523
0
            return line;
524
0
        }
525
0
    }
526
527
0
    return NULL;
528
0
}
529
530
531
/*************************
532
 ***  Unicode Support  ***
533
 *************************/
534
535
typedef struct MD_UNICODE_FOLD_INFO_tag MD_UNICODE_FOLD_INFO;
536
struct MD_UNICODE_FOLD_INFO_tag {
537
    unsigned codepoints[3];
538
    unsigned n_codepoints;
539
};
540
541
542
#if defined MD4C_USE_UTF16 || defined MD4C_USE_UTF8
543
    /* Binary search over sorted "map" of codepoints. Consecutive sequences
544
     * of codepoints may be encoded in the map by just using the
545
     * (MIN_CODEPOINT | 0x40000000) and (MAX_CODEPOINT | 0x80000000).
546
     *
547
     * Returns index of the found record in the map (in the case of ranges,
548
     * the minimal value is used); or -1 on failure. */
549
    static int
550
    md_unicode_bsearch__(unsigned codepoint, const unsigned* map, size_t map_size)
551
0
    {
552
0
        int beg, end;
553
0
        int pivot_beg, pivot_end;
554
555
0
        beg = 0;
556
0
        end = (int) map_size-1;
557
0
        while(beg <= end) {
558
            /* Pivot may be a range, not just a single value. */
559
0
            pivot_beg = pivot_end = (beg + end) / 2;
560
0
            if(map[pivot_end] & 0x40000000)
561
0
                pivot_end++;
562
0
            if(map[pivot_beg] & 0x80000000)
563
0
                pivot_beg--;
564
565
0
            if(codepoint < (map[pivot_beg] & 0x00ffffff))
566
0
                end = pivot_beg - 1;
567
0
            else if(codepoint > (map[pivot_end] & 0x00ffffff))
568
0
                beg = pivot_end + 1;
569
0
            else
570
0
                return pivot_beg;
571
0
        }
572
573
0
        return -1;
574
0
    }
575
576
    static int
577
    md_is_unicode_whitespace__(unsigned codepoint)
578
0
    {
579
0
#define R(cp_min, cp_max)   ((cp_min) | 0x40000000), ((cp_max) | 0x80000000)
580
0
#define S(cp)               (cp)
581
        /* Unicode "Zs" category.
582
         * (generated by scripts/build_whitespace_map.py) */
583
0
        static const unsigned WHITESPACE_MAP[] = {
584
0
            S(0x0020), S(0x00a0), S(0x1680), R(0x2000,0x200a), S(0x202f), S(0x205f), S(0x3000)
585
0
        };
586
0
#undef R
587
0
#undef S
588
589
        /* The ASCII ones are the most frequently used ones, also CommonMark
590
         * specification requests few more in this range. */
591
0
        if(codepoint <= 0x7f)
592
0
            return ISWHITESPACE_(codepoint);
593
594
0
        return (md_unicode_bsearch__(codepoint, WHITESPACE_MAP, SIZEOF_ARRAY(WHITESPACE_MAP)) >= 0);
595
0
    }
596
597
    static int
598
    md_is_unicode_punct__(unsigned codepoint)
599
0
    {
600
0
#define R(cp_min, cp_max)   ((cp_min) | 0x40000000), ((cp_max) | 0x80000000)
601
0
#define S(cp)               (cp)
602
        /* Unicode general "P" and "S" categories.
603
         * (generated by scripts/build_punct_map.py) */
604
0
        static const unsigned PUNCT_MAP[] = {
605
0
            R(0x0021,0x002f), R(0x003a,0x0040), R(0x005b,0x0060), R(0x007b,0x007e), R(0x00a1,0x00a9),
606
0
            R(0x00ab,0x00ac), R(0x00ae,0x00b1), S(0x00b4), R(0x00b6,0x00b8), S(0x00bb), S(0x00bf), S(0x00d7),
607
0
            S(0x00f7), R(0x02c2,0x02c5), R(0x02d2,0x02df), R(0x02e5,0x02eb), S(0x02ed), R(0x02ef,0x02ff), S(0x0375),
608
0
            S(0x037e), R(0x0384,0x0385), S(0x0387), S(0x03f6), S(0x0482), R(0x055a,0x055f), R(0x0589,0x058a),
609
0
            R(0x058d,0x058f), S(0x05be), S(0x05c0), S(0x05c3), S(0x05c6), R(0x05f3,0x05f4), R(0x0606,0x060f),
610
0
            S(0x061b), R(0x061d,0x061f), R(0x066a,0x066d), S(0x06d4), S(0x06de), S(0x06e9), R(0x06fd,0x06fe),
611
0
            R(0x0700,0x070d), R(0x07f6,0x07f9), R(0x07fe,0x07ff), R(0x0830,0x083e), S(0x085e), S(0x0888),
612
0
            R(0x0964,0x0965), S(0x0970), R(0x09f2,0x09f3), R(0x09fa,0x09fb), S(0x09fd), S(0x0a76), R(0x0af0,0x0af1),
613
0
            S(0x0b70), R(0x0bf3,0x0bfa), S(0x0c77), S(0x0c7f), S(0x0c84), S(0x0d4f), S(0x0d79), S(0x0df4), S(0x0e3f),
614
0
            S(0x0e4f), R(0x0e5a,0x0e5b), R(0x0f01,0x0f17), R(0x0f1a,0x0f1f), S(0x0f34), S(0x0f36), S(0x0f38),
615
0
            R(0x0f3a,0x0f3d), S(0x0f85), R(0x0fbe,0x0fc5), R(0x0fc7,0x0fcc), R(0x0fce,0x0fda), R(0x104a,0x104f),
616
0
            R(0x109e,0x109f), S(0x10fb), R(0x1360,0x1368), R(0x1390,0x1399), S(0x1400), R(0x166d,0x166e),
617
0
            R(0x169b,0x169c), R(0x16eb,0x16ed), R(0x1735,0x1736), R(0x17d4,0x17d6), R(0x17d8,0x17db),
618
0
            R(0x1800,0x180a), S(0x1940), R(0x1944,0x1945), R(0x19de,0x19ff), R(0x1a1e,0x1a1f), R(0x1aa0,0x1aa6),
619
0
            R(0x1aa8,0x1aad), R(0x1b5a,0x1b6a), R(0x1b74,0x1b7e), R(0x1bfc,0x1bff), R(0x1c3b,0x1c3f),
620
0
            R(0x1c7e,0x1c7f), R(0x1cc0,0x1cc7), S(0x1cd3), S(0x1fbd), R(0x1fbf,0x1fc1), R(0x1fcd,0x1fcf),
621
0
            R(0x1fdd,0x1fdf), R(0x1fed,0x1fef), R(0x1ffd,0x1ffe), R(0x2010,0x2027), R(0x2030,0x205e),
622
0
            R(0x207a,0x207e), R(0x208a,0x208e), R(0x20a0,0x20c0), R(0x2100,0x2101), R(0x2103,0x2106),
623
0
            R(0x2108,0x2109), S(0x2114), R(0x2116,0x2118), R(0x211e,0x2123), S(0x2125), S(0x2127), S(0x2129),
624
0
            S(0x212e), R(0x213a,0x213b), R(0x2140,0x2144), R(0x214a,0x214d), S(0x214f), R(0x218a,0x218b),
625
0
            R(0x2190,0x2426), R(0x2440,0x244a), R(0x249c,0x24e9), R(0x2500,0x2775), R(0x2794,0x2b73),
626
0
            R(0x2b76,0x2b95), R(0x2b97,0x2bff), R(0x2ce5,0x2cea), R(0x2cf9,0x2cfc), R(0x2cfe,0x2cff), S(0x2d70),
627
0
            R(0x2e00,0x2e2e), R(0x2e30,0x2e5d), R(0x2e80,0x2e99), R(0x2e9b,0x2ef3), R(0x2f00,0x2fd5),
628
0
            R(0x2ff0,0x2fff), R(0x3001,0x3004), R(0x3008,0x3020), S(0x3030), R(0x3036,0x3037), R(0x303d,0x303f),
629
0
            R(0x309b,0x309c), S(0x30a0), S(0x30fb), R(0x3190,0x3191), R(0x3196,0x319f), R(0x31c0,0x31e3), S(0x31ef),
630
0
            R(0x3200,0x321e), R(0x322a,0x3247), S(0x3250), R(0x3260,0x327f), R(0x328a,0x32b0), R(0x32c0,0x33ff),
631
0
            R(0x4dc0,0x4dff), R(0xa490,0xa4c6), R(0xa4fe,0xa4ff), R(0xa60d,0xa60f), S(0xa673), S(0xa67e),
632
0
            R(0xa6f2,0xa6f7), R(0xa700,0xa716), R(0xa720,0xa721), R(0xa789,0xa78a), R(0xa828,0xa82b),
633
0
            R(0xa836,0xa839), R(0xa874,0xa877), R(0xa8ce,0xa8cf), R(0xa8f8,0xa8fa), S(0xa8fc), R(0xa92e,0xa92f),
634
0
            S(0xa95f), R(0xa9c1,0xa9cd), R(0xa9de,0xa9df), R(0xaa5c,0xaa5f), R(0xaa77,0xaa79), R(0xaade,0xaadf),
635
0
            R(0xaaf0,0xaaf1), S(0xab5b), R(0xab6a,0xab6b), S(0xabeb), S(0xfb29), R(0xfbb2,0xfbc2), R(0xfd3e,0xfd4f),
636
0
            S(0xfdcf), R(0xfdfc,0xfdff), R(0xfe10,0xfe19), R(0xfe30,0xfe52), R(0xfe54,0xfe66), R(0xfe68,0xfe6b),
637
0
            R(0xff01,0xff0f), R(0xff1a,0xff20), R(0xff3b,0xff40), R(0xff5b,0xff65), R(0xffe0,0xffe6),
638
0
            R(0xffe8,0xffee), R(0xfffc,0xfffd), R(0x10100,0x10102), R(0x10137,0x1013f), R(0x10179,0x10189),
639
0
            R(0x1018c,0x1018e), R(0x10190,0x1019c), S(0x101a0), R(0x101d0,0x101fc), S(0x1039f), S(0x103d0),
640
0
            S(0x1056f), S(0x10857), R(0x10877,0x10878), S(0x1091f), S(0x1093f), R(0x10a50,0x10a58), S(0x10a7f),
641
0
            S(0x10ac8), R(0x10af0,0x10af6), R(0x10b39,0x10b3f), R(0x10b99,0x10b9c), S(0x10ead), R(0x10f55,0x10f59),
642
0
            R(0x10f86,0x10f89), R(0x11047,0x1104d), R(0x110bb,0x110bc), R(0x110be,0x110c1), R(0x11140,0x11143),
643
0
            R(0x11174,0x11175), R(0x111c5,0x111c8), S(0x111cd), S(0x111db), R(0x111dd,0x111df), R(0x11238,0x1123d),
644
0
            S(0x112a9), R(0x1144b,0x1144f), R(0x1145a,0x1145b), S(0x1145d), S(0x114c6), R(0x115c1,0x115d7),
645
0
            R(0x11641,0x11643), R(0x11660,0x1166c), S(0x116b9), R(0x1173c,0x1173f), S(0x1183b), R(0x11944,0x11946),
646
0
            S(0x119e2), R(0x11a3f,0x11a46), R(0x11a9a,0x11a9c), R(0x11a9e,0x11aa2), R(0x11b00,0x11b09),
647
0
            R(0x11c41,0x11c45), R(0x11c70,0x11c71), R(0x11ef7,0x11ef8), R(0x11f43,0x11f4f), R(0x11fd5,0x11ff1),
648
0
            S(0x11fff), R(0x12470,0x12474), R(0x12ff1,0x12ff2), R(0x16a6e,0x16a6f), S(0x16af5), R(0x16b37,0x16b3f),
649
0
            R(0x16b44,0x16b45), R(0x16e97,0x16e9a), S(0x16fe2), S(0x1bc9c), S(0x1bc9f), R(0x1cf50,0x1cfc3),
650
0
            R(0x1d000,0x1d0f5), R(0x1d100,0x1d126), R(0x1d129,0x1d164), R(0x1d16a,0x1d16c), R(0x1d183,0x1d184),
651
0
            R(0x1d18c,0x1d1a9), R(0x1d1ae,0x1d1ea), R(0x1d200,0x1d241), S(0x1d245), R(0x1d300,0x1d356), S(0x1d6c1),
652
0
            S(0x1d6db), S(0x1d6fb), S(0x1d715), S(0x1d735), S(0x1d74f), S(0x1d76f), S(0x1d789), S(0x1d7a9),
653
0
            S(0x1d7c3), R(0x1d800,0x1d9ff), R(0x1da37,0x1da3a), R(0x1da6d,0x1da74), R(0x1da76,0x1da83),
654
0
            R(0x1da85,0x1da8b), S(0x1e14f), S(0x1e2ff), R(0x1e95e,0x1e95f), S(0x1ecac), S(0x1ecb0), S(0x1ed2e),
655
0
            R(0x1eef0,0x1eef1), R(0x1f000,0x1f02b), R(0x1f030,0x1f093), R(0x1f0a0,0x1f0ae), R(0x1f0b1,0x1f0bf),
656
0
            R(0x1f0c1,0x1f0cf), R(0x1f0d1,0x1f0f5), R(0x1f10d,0x1f1ad), R(0x1f1e6,0x1f202), R(0x1f210,0x1f23b),
657
0
            R(0x1f240,0x1f248), R(0x1f250,0x1f251), R(0x1f260,0x1f265), R(0x1f300,0x1f6d7), R(0x1f6dc,0x1f6ec),
658
0
            R(0x1f6f0,0x1f6fc), R(0x1f700,0x1f776), R(0x1f77b,0x1f7d9), R(0x1f7e0,0x1f7eb), S(0x1f7f0),
659
0
            R(0x1f800,0x1f80b), R(0x1f810,0x1f847), R(0x1f850,0x1f859), R(0x1f860,0x1f887), R(0x1f890,0x1f8ad),
660
0
            R(0x1f8b0,0x1f8b1), R(0x1f900,0x1fa53), R(0x1fa60,0x1fa6d), R(0x1fa70,0x1fa7c), R(0x1fa80,0x1fa88),
661
0
            R(0x1fa90,0x1fabd), R(0x1fabf,0x1fac5), R(0x1face,0x1fadb), R(0x1fae0,0x1fae8), R(0x1faf0,0x1faf8),
662
0
            R(0x1fb00,0x1fb92), R(0x1fb94,0x1fbca)
663
0
        };
664
0
#undef R
665
0
#undef S
666
667
        /* The ASCII ones are the most frequently used ones, also CommonMark
668
         * specification requests few more in this range. */
669
0
        if(codepoint <= 0x7f)
670
0
            return ISPUNCT_(codepoint);
671
672
0
        return (md_unicode_bsearch__(codepoint, PUNCT_MAP, SIZEOF_ARRAY(PUNCT_MAP)) >= 0);
673
0
    }
674
675
    static void
676
    md_get_unicode_fold_info(unsigned codepoint, MD_UNICODE_FOLD_INFO* info)
677
0
    {
678
0
#define R(cp_min, cp_max)   ((cp_min) | 0x40000000), ((cp_max) | 0x80000000)
679
0
#define S(cp)               (cp)
680
        /* Unicode "Pc", "Pd", "Pe", "Pf", "Pi", "Po", "Ps" categories.
681
         * (generated by scripts/build_folding_map.py) */
682
0
        static const unsigned FOLD_MAP_1[] = {
683
0
            R(0x0041,0x005a), S(0x00b5), R(0x00c0,0x00d6), R(0x00d8,0x00de), R(0x0100,0x012e), R(0x0132,0x0136),
684
0
            R(0x0139,0x0147), R(0x014a,0x0176), S(0x0178), R(0x0179,0x017d), S(0x017f), S(0x0181), S(0x0182),
685
0
            S(0x0184), S(0x0186), S(0x0187), S(0x0189), S(0x018a), S(0x018b), S(0x018e), S(0x018f), S(0x0190),
686
0
            S(0x0191), S(0x0193), S(0x0194), S(0x0196), S(0x0197), S(0x0198), S(0x019c), S(0x019d), S(0x019f),
687
0
            R(0x01a0,0x01a4), S(0x01a6), S(0x01a7), S(0x01a9), S(0x01ac), S(0x01ae), S(0x01af), S(0x01b1), S(0x01b2),
688
0
            S(0x01b3), S(0x01b5), S(0x01b7), S(0x01b8), S(0x01bc), S(0x01c4), S(0x01c5), S(0x01c7), S(0x01c8),
689
0
            S(0x01ca), R(0x01cb,0x01db), R(0x01de,0x01ee), S(0x01f1), S(0x01f2), S(0x01f4), S(0x01f6), S(0x01f7),
690
0
            R(0x01f8,0x021e), S(0x0220), R(0x0222,0x0232), S(0x023a), S(0x023b), S(0x023d), S(0x023e), S(0x0241),
691
0
            S(0x0243), S(0x0244), S(0x0245), R(0x0246,0x024e), S(0x0345), S(0x0370), S(0x0372), S(0x0376), S(0x037f),
692
0
            S(0x0386), R(0x0388,0x038a), S(0x038c), S(0x038e), S(0x038f), R(0x0391,0x03a1), R(0x03a3,0x03ab),
693
0
            S(0x03c2), S(0x03cf), S(0x03d0), S(0x03d1), S(0x03d5), S(0x03d6), R(0x03d8,0x03ee), S(0x03f0), S(0x03f1),
694
0
            S(0x03f4), S(0x03f5), S(0x03f7), S(0x03f9), S(0x03fa), R(0x03fd,0x03ff), R(0x0400,0x040f),
695
0
            R(0x0410,0x042f), R(0x0460,0x0480), R(0x048a,0x04be), S(0x04c0), R(0x04c1,0x04cd), R(0x04d0,0x052e),
696
0
            R(0x0531,0x0556), R(0x10a0,0x10c5), S(0x10c7), S(0x10cd), R(0x13f8,0x13fd), S(0x1c80), S(0x1c81),
697
0
            S(0x1c82), S(0x1c83), S(0x1c84), S(0x1c85), S(0x1c86), S(0x1c87), S(0x1c88), R(0x1c90,0x1cba),
698
0
            R(0x1cbd,0x1cbf), R(0x1e00,0x1e94), S(0x1e9b), R(0x1ea0,0x1efe), R(0x1f08,0x1f0f), R(0x1f18,0x1f1d),
699
0
            R(0x1f28,0x1f2f), R(0x1f38,0x1f3f), R(0x1f48,0x1f4d), S(0x1f59), S(0x1f5b), S(0x1f5d), S(0x1f5f),
700
0
            R(0x1f68,0x1f6f), S(0x1fb8), S(0x1fb9), S(0x1fba), S(0x1fbb), S(0x1fbe), R(0x1fc8,0x1fcb), S(0x1fd8),
701
0
            S(0x1fd9), S(0x1fda), S(0x1fdb), S(0x1fe8), S(0x1fe9), S(0x1fea), S(0x1feb), S(0x1fec), S(0x1ff8),
702
0
            S(0x1ff9), S(0x1ffa), S(0x1ffb), S(0x2126), S(0x212a), S(0x212b), S(0x2132), R(0x2160,0x216f), S(0x2183),
703
0
            R(0x24b6,0x24cf), R(0x2c00,0x2c2f), S(0x2c60), S(0x2c62), S(0x2c63), S(0x2c64), R(0x2c67,0x2c6b),
704
0
            S(0x2c6d), S(0x2c6e), S(0x2c6f), S(0x2c70), S(0x2c72), S(0x2c75), S(0x2c7e), S(0x2c7f), R(0x2c80,0x2ce2),
705
0
            S(0x2ceb), S(0x2ced), S(0x2cf2), R(0xa640,0xa66c), R(0xa680,0xa69a), R(0xa722,0xa72e), R(0xa732,0xa76e),
706
0
            S(0xa779), S(0xa77b), S(0xa77d), R(0xa77e,0xa786), S(0xa78b), S(0xa78d), S(0xa790), S(0xa792),
707
0
            R(0xa796,0xa7a8), S(0xa7aa), S(0xa7ab), S(0xa7ac), S(0xa7ad), S(0xa7ae), S(0xa7b0), S(0xa7b1), S(0xa7b2),
708
0
            S(0xa7b3), R(0xa7b4,0xa7c2), S(0xa7c4), S(0xa7c5), S(0xa7c6), S(0xa7c7), S(0xa7c9), S(0xa7d0), S(0xa7d6),
709
0
            S(0xa7d8), S(0xa7f5), R(0xab70,0xabbf), R(0xff21,0xff3a), R(0x10400,0x10427), R(0x104b0,0x104d3),
710
0
            R(0x10570,0x1057a), R(0x1057c,0x1058a), R(0x1058c,0x10592), S(0x10594), S(0x10595), R(0x10c80,0x10cb2),
711
0
            R(0x118a0,0x118bf), R(0x16e40,0x16e5f), R(0x1e900,0x1e921)
712
0
        };
713
0
        static const unsigned FOLD_MAP_1_DATA[] = {
714
0
            0x0061, 0x007a, 0x03bc, 0x00e0, 0x00f6, 0x00f8, 0x00fe, 0x0101, 0x012f, 0x0133, 0x0137, 0x013a, 0x0148,
715
0
            0x014b, 0x0177, 0x00ff, 0x017a, 0x017e, 0x0073, 0x0253, 0x0183, 0x0185, 0x0254, 0x0188, 0x0256, 0x0257,
716
0
            0x018c, 0x01dd, 0x0259, 0x025b, 0x0192, 0x0260, 0x0263, 0x0269, 0x0268, 0x0199, 0x026f, 0x0272, 0x0275,
717
0
            0x01a1, 0x01a5, 0x0280, 0x01a8, 0x0283, 0x01ad, 0x0288, 0x01b0, 0x028a, 0x028b, 0x01b4, 0x01b6, 0x0292,
718
0
            0x01b9, 0x01bd, 0x01c6, 0x01c6, 0x01c9, 0x01c9, 0x01cc, 0x01cc, 0x01dc, 0x01df, 0x01ef, 0x01f3, 0x01f3,
719
0
            0x01f5, 0x0195, 0x01bf, 0x01f9, 0x021f, 0x019e, 0x0223, 0x0233, 0x2c65, 0x023c, 0x019a, 0x2c66, 0x0242,
720
0
            0x0180, 0x0289, 0x028c, 0x0247, 0x024f, 0x03b9, 0x0371, 0x0373, 0x0377, 0x03f3, 0x03ac, 0x03ad, 0x03af,
721
0
            0x03cc, 0x03cd, 0x03ce, 0x03b1, 0x03c1, 0x03c3, 0x03cb, 0x03c3, 0x03d7, 0x03b2, 0x03b8, 0x03c6, 0x03c0,
722
0
            0x03d9, 0x03ef, 0x03ba, 0x03c1, 0x03b8, 0x03b5, 0x03f8, 0x03f2, 0x03fb, 0x037b, 0x037d, 0x0450, 0x045f,
723
0
            0x0430, 0x044f, 0x0461, 0x0481, 0x048b, 0x04bf, 0x04cf, 0x04c2, 0x04ce, 0x04d1, 0x052f, 0x0561, 0x0586,
724
0
            0x2d00, 0x2d25, 0x2d27, 0x2d2d, 0x13f0, 0x13f5, 0x0432, 0x0434, 0x043e, 0x0441, 0x0442, 0x0442, 0x044a,
725
0
            0x0463, 0xa64b, 0x10d0, 0x10fa, 0x10fd, 0x10ff, 0x1e01, 0x1e95, 0x1e61, 0x1ea1, 0x1eff, 0x1f00, 0x1f07,
726
0
            0x1f10, 0x1f15, 0x1f20, 0x1f27, 0x1f30, 0x1f37, 0x1f40, 0x1f45, 0x1f51, 0x1f53, 0x1f55, 0x1f57, 0x1f60,
727
0
            0x1f67, 0x1fb0, 0x1fb1, 0x1f70, 0x1f71, 0x03b9, 0x1f72, 0x1f75, 0x1fd0, 0x1fd1, 0x1f76, 0x1f77, 0x1fe0,
728
0
            0x1fe1, 0x1f7a, 0x1f7b, 0x1fe5, 0x1f78, 0x1f79, 0x1f7c, 0x1f7d, 0x03c9, 0x006b, 0x00e5, 0x214e, 0x2170,
729
0
            0x217f, 0x2184, 0x24d0, 0x24e9, 0x2c30, 0x2c5f, 0x2c61, 0x026b, 0x1d7d, 0x027d, 0x2c68, 0x2c6c, 0x0251,
730
0
            0x0271, 0x0250, 0x0252, 0x2c73, 0x2c76, 0x023f, 0x0240, 0x2c81, 0x2ce3, 0x2cec, 0x2cee, 0x2cf3, 0xa641,
731
0
            0xa66d, 0xa681, 0xa69b, 0xa723, 0xa72f, 0xa733, 0xa76f, 0xa77a, 0xa77c, 0x1d79, 0xa77f, 0xa787, 0xa78c,
732
0
            0x0265, 0xa791, 0xa793, 0xa797, 0xa7a9, 0x0266, 0x025c, 0x0261, 0x026c, 0x026a, 0x029e, 0x0287, 0x029d,
733
0
            0xab53, 0xa7b5, 0xa7c3, 0xa794, 0x0282, 0x1d8e, 0xa7c8, 0xa7ca, 0xa7d1, 0xa7d7, 0xa7d9, 0xa7f6, 0x13a0,
734
0
            0x13ef, 0xff41, 0xff5a, 0x10428, 0x1044f, 0x104d8, 0x104fb, 0x10597, 0x105a1, 0x105a3, 0x105b1, 0x105b3,
735
0
            0x105b9, 0x105bb, 0x105bc, 0x10cc0, 0x10cf2, 0x118c0, 0x118df, 0x16e60, 0x16e7f, 0x1e922, 0x1e943
736
0
        };
737
0
        static const unsigned FOLD_MAP_2[] = {
738
0
            S(0x00df), S(0x0130), S(0x0149), S(0x01f0), S(0x0587), S(0x1e96), S(0x1e97), S(0x1e98), S(0x1e99),
739
0
            S(0x1e9a), S(0x1e9e), S(0x1f50), R(0x1f80,0x1f87), R(0x1f88,0x1f8f), R(0x1f90,0x1f97), R(0x1f98,0x1f9f),
740
0
            R(0x1fa0,0x1fa7), R(0x1fa8,0x1faf), S(0x1fb2), S(0x1fb3), S(0x1fb4), S(0x1fb6), S(0x1fbc), S(0x1fc2),
741
0
            S(0x1fc3), S(0x1fc4), S(0x1fc6), S(0x1fcc), S(0x1fd6), S(0x1fe4), S(0x1fe6), S(0x1ff2), S(0x1ff3),
742
0
            S(0x1ff4), S(0x1ff6), S(0x1ffc), S(0xfb00), S(0xfb01), S(0xfb02), S(0xfb05), S(0xfb06), S(0xfb13),
743
0
            S(0xfb14), S(0xfb15), S(0xfb16), S(0xfb17)
744
0
        };
745
0
        static const unsigned FOLD_MAP_2_DATA[] = {
746
0
            0x0073,0x0073, 0x0069,0x0307, 0x02bc,0x006e, 0x006a,0x030c, 0x0565,0x0582, 0x0068,0x0331, 0x0074,0x0308,
747
0
            0x0077,0x030a, 0x0079,0x030a, 0x0061,0x02be, 0x0073,0x0073, 0x03c5,0x0313, 0x1f00,0x03b9, 0x1f07,0x03b9,
748
0
            0x1f00,0x03b9, 0x1f07,0x03b9, 0x1f20,0x03b9, 0x1f27,0x03b9, 0x1f20,0x03b9, 0x1f27,0x03b9, 0x1f60,0x03b9,
749
0
            0x1f67,0x03b9, 0x1f60,0x03b9, 0x1f67,0x03b9, 0x1f70,0x03b9, 0x03b1,0x03b9, 0x03ac,0x03b9, 0x03b1,0x0342,
750
0
            0x03b1,0x03b9, 0x1f74,0x03b9, 0x03b7,0x03b9, 0x03ae,0x03b9, 0x03b7,0x0342, 0x03b7,0x03b9, 0x03b9,0x0342,
751
0
            0x03c1,0x0313, 0x03c5,0x0342, 0x1f7c,0x03b9, 0x03c9,0x03b9, 0x03ce,0x03b9, 0x03c9,0x0342, 0x03c9,0x03b9,
752
0
            0x0066,0x0066, 0x0066,0x0069, 0x0066,0x006c, 0x0073,0x0074, 0x0073,0x0074, 0x0574,0x0576, 0x0574,0x0565,
753
0
            0x0574,0x056b, 0x057e,0x0576, 0x0574,0x056d
754
0
        };
755
0
        static const unsigned FOLD_MAP_3[] = {
756
0
            S(0x0390), S(0x03b0), S(0x1f52), S(0x1f54), S(0x1f56), S(0x1fb7), S(0x1fc7), S(0x1fd2), S(0x1fd3),
757
0
            S(0x1fd7), S(0x1fe2), S(0x1fe3), S(0x1fe7), S(0x1ff7), S(0xfb03), S(0xfb04)
758
0
        };
759
0
        static const unsigned FOLD_MAP_3_DATA[] = {
760
0
            0x03b9,0x0308,0x0301, 0x03c5,0x0308,0x0301, 0x03c5,0x0313,0x0300, 0x03c5,0x0313,0x0301,
761
0
            0x03c5,0x0313,0x0342, 0x03b1,0x0342,0x03b9, 0x03b7,0x0342,0x03b9, 0x03b9,0x0308,0x0300,
762
0
            0x03b9,0x0308,0x0301, 0x03b9,0x0308,0x0342, 0x03c5,0x0308,0x0300, 0x03c5,0x0308,0x0301,
763
0
            0x03c5,0x0308,0x0342, 0x03c9,0x0342,0x03b9, 0x0066,0x0066,0x0069, 0x0066,0x0066,0x006c
764
0
        };
765
0
#undef R
766
0
#undef S
767
0
        static const struct {
768
0
            const unsigned* map;
769
0
            const unsigned* data;
770
0
            size_t map_size;
771
0
            unsigned n_codepoints;
772
0
        } FOLD_MAP_LIST[] = {
773
0
            { FOLD_MAP_1, FOLD_MAP_1_DATA, SIZEOF_ARRAY(FOLD_MAP_1), 1 },
774
0
            { FOLD_MAP_2, FOLD_MAP_2_DATA, SIZEOF_ARRAY(FOLD_MAP_2), 2 },
775
0
            { FOLD_MAP_3, FOLD_MAP_3_DATA, SIZEOF_ARRAY(FOLD_MAP_3), 3 }
776
0
        };
777
778
0
        int i;
779
780
        /* Fast path for ASCII characters. */
781
0
        if(codepoint <= 0x7f) {
782
0
            info->codepoints[0] = codepoint;
783
0
            if(ISUPPER_(codepoint))
784
0
                info->codepoints[0] += 'a' - 'A';
785
0
            info->n_codepoints = 1;
786
0
            return;
787
0
        }
788
789
        /* Try to locate the codepoint in any of the maps. */
790
0
        for(i = 0; i < (int) SIZEOF_ARRAY(FOLD_MAP_LIST); i++) {
791
0
            int index;
792
793
0
            index = md_unicode_bsearch__(codepoint, FOLD_MAP_LIST[i].map, FOLD_MAP_LIST[i].map_size);
794
0
            if(index >= 0) {
795
                /* Found the mapping. */
796
0
                unsigned n_codepoints = FOLD_MAP_LIST[i].n_codepoints;
797
0
                const unsigned* map = FOLD_MAP_LIST[i].map;
798
0
                const unsigned* codepoints = FOLD_MAP_LIST[i].data + (index * n_codepoints);
799
800
0
                memcpy(info->codepoints, codepoints, sizeof(unsigned) * n_codepoints);
801
0
                info->n_codepoints = n_codepoints;
802
803
0
                if(FOLD_MAP_LIST[i].map[index] != codepoint) {
804
                    /* The found mapping maps whole range of codepoints,
805
                     * i.e. we have to offset info->codepoints[0] accordingly. */
806
0
                    if((map[index] & 0x00ffffff)+1 == codepoints[0]) {
807
                        /* Alternating type of the range. */
808
0
                        info->codepoints[0] = codepoint + ((codepoint & 0x1) == (map[index] & 0x1) ? 1 : 0);
809
0
                    } else {
810
                        /* Range to range kind of mapping. */
811
0
                        info->codepoints[0] += (codepoint - (map[index] & 0x00ffffff));
812
0
                    }
813
0
                }
814
815
0
                return;
816
0
            }
817
0
        }
818
819
        /* No mapping found. Map the codepoint to itself. */
820
0
        info->codepoints[0] = codepoint;
821
0
        info->n_codepoints = 1;
822
0
    }
823
#endif
824
825
826
#if defined MD4C_USE_UTF16
827
    #define IS_UTF16_SURROGATE_HI(word)     (((WORD)(word) & 0xfc00) == 0xd800)
828
    #define IS_UTF16_SURROGATE_LO(word)     (((WORD)(word) & 0xfc00) == 0xdc00)
829
    #define UTF16_DECODE_SURROGATE(hi, lo)  (0x10000 + ((((unsigned)(hi) & 0x3ff) << 10) | (((unsigned)(lo) & 0x3ff) << 0)))
830
831
    static unsigned
832
    md_decode_utf16le__(const CHAR* str, SZ str_size, SZ* p_size)
833
    {
834
        if(IS_UTF16_SURROGATE_HI(str[0])) {
835
            if(1 < str_size && IS_UTF16_SURROGATE_LO(str[1])) {
836
                if(p_size != NULL)
837
                    *p_size = 2;
838
                return UTF16_DECODE_SURROGATE(str[0], str[1]);
839
            }
840
        }
841
842
        if(p_size != NULL)
843
            *p_size = 1;
844
        return str[0];
845
    }
846
847
    static unsigned
848
    md_decode_utf16le_before__(MD_CTX* ctx, OFF off)
849
    {
850
        if(off > 2 && IS_UTF16_SURROGATE_HI(CH(off-2)) && IS_UTF16_SURROGATE_LO(CH(off-1)))
851
            return UTF16_DECODE_SURROGATE(CH(off-2), CH(off-1));
852
853
        return CH(off);
854
    }
855
856
    /* No whitespace uses surrogates, so no decoding needed here. */
857
    #define ISUNICODEWHITESPACE_(codepoint) md_is_unicode_whitespace__(codepoint)
858
    #define ISUNICODEWHITESPACE(off)        md_is_unicode_whitespace__(CH(off))
859
    #define ISUNICODEWHITESPACEBEFORE(off)  md_is_unicode_whitespace__(CH((off)-1))
860
861
    #define ISUNICODEPUNCT(off)             md_is_unicode_punct__(md_decode_utf16le__(STR(off), ctx->size - (off), NULL))
862
    #define ISUNICODEPUNCTBEFORE(off)       md_is_unicode_punct__(md_decode_utf16le_before__(ctx, off))
863
864
    static inline int
865
    md_decode_unicode(const CHAR* str, OFF off, SZ str_size, SZ* p_char_size)
866
    {
867
        return md_decode_utf16le__(str+off, str_size-off, p_char_size);
868
    }
869
#elif defined MD4C_USE_UTF8
870
0
    #define IS_UTF8_LEAD1(byte)     ((unsigned char)(byte) <= 0x7f)
871
0
    #define IS_UTF8_LEAD2(byte)     (((unsigned char)(byte) & 0xe0) == 0xc0)
872
0
    #define IS_UTF8_LEAD3(byte)     (((unsigned char)(byte) & 0xf0) == 0xe0)
873
0
    #define IS_UTF8_LEAD4(byte)     (((unsigned char)(byte) & 0xf8) == 0xf0)
874
0
    #define IS_UTF8_TAIL(byte)      (((unsigned char)(byte) & 0xc0) == 0x80)
875
876
    static unsigned
877
    md_decode_utf8__(const CHAR* str, SZ str_size, SZ* p_size)
878
0
    {
879
0
        if(!IS_UTF8_LEAD1(str[0])) {
880
0
            if(IS_UTF8_LEAD2(str[0])) {
881
0
                if(1 < str_size && IS_UTF8_TAIL(str[1])) {
882
0
                    if(p_size != NULL)
883
0
                        *p_size = 2;
884
885
0
                    return (((unsigned int)str[0] & 0x1f) << 6) |
886
0
                           (((unsigned int)str[1] & 0x3f) << 0);
887
0
                }
888
0
            } else if(IS_UTF8_LEAD3(str[0])) {
889
0
                if(2 < str_size && IS_UTF8_TAIL(str[1]) && IS_UTF8_TAIL(str[2])) {
890
0
                    if(p_size != NULL)
891
0
                        *p_size = 3;
892
893
0
                    return (((unsigned int)str[0] & 0x0f) << 12) |
894
0
                           (((unsigned int)str[1] & 0x3f) << 6) |
895
0
                           (((unsigned int)str[2] & 0x3f) << 0);
896
0
                }
897
0
            } else if(IS_UTF8_LEAD4(str[0])) {
898
0
                if(3 < str_size && IS_UTF8_TAIL(str[1]) && IS_UTF8_TAIL(str[2]) && IS_UTF8_TAIL(str[3])) {
899
0
                    if(p_size != NULL)
900
0
                        *p_size = 4;
901
902
0
                    return (((unsigned int)str[0] & 0x07) << 18) |
903
0
                           (((unsigned int)str[1] & 0x3f) << 12) |
904
0
                           (((unsigned int)str[2] & 0x3f) << 6) |
905
0
                           (((unsigned int)str[3] & 0x3f) << 0);
906
0
                }
907
0
            }
908
0
        }
909
910
0
        if(p_size != NULL)
911
0
            *p_size = 1;
912
0
        return (unsigned) str[0];
913
0
    }
914
915
    static unsigned
916
    md_decode_utf8_before__(MD_CTX* ctx, OFF off)
917
0
    {
918
0
        if(!IS_UTF8_LEAD1(CH(off-1))) {
919
0
            if(off > 1 && IS_UTF8_LEAD2(CH(off-2)) && IS_UTF8_TAIL(CH(off-1)))
920
0
                return (((unsigned int)CH(off-2) & 0x1f) << 6) |
921
0
                       (((unsigned int)CH(off-1) & 0x3f) << 0);
922
923
0
            if(off > 2 && IS_UTF8_LEAD3(CH(off-3)) && IS_UTF8_TAIL(CH(off-2)) && IS_UTF8_TAIL(CH(off-1)))
924
0
                return (((unsigned int)CH(off-3) & 0x0f) << 12) |
925
0
                       (((unsigned int)CH(off-2) & 0x3f) << 6) |
926
0
                       (((unsigned int)CH(off-1) & 0x3f) << 0);
927
928
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)))
929
0
                return (((unsigned int)CH(off-4) & 0x07) << 18) |
930
0
                       (((unsigned int)CH(off-3) & 0x3f) << 12) |
931
0
                       (((unsigned int)CH(off-2) & 0x3f) << 6) |
932
0
                       (((unsigned int)CH(off-1) & 0x3f) << 0);
933
0
        }
934
935
0
        return (unsigned) CH(off-1);
936
0
    }
937
938
0
    #define ISUNICODEWHITESPACE_(codepoint) md_is_unicode_whitespace__(codepoint)
939
0
    #define ISUNICODEWHITESPACE(off)        md_is_unicode_whitespace__(md_decode_utf8__(STR(off), ctx->size - (off), NULL))
940
0
    #define ISUNICODEWHITESPACEBEFORE(off)  md_is_unicode_whitespace__(md_decode_utf8_before__(ctx, off))
941
942
0
    #define ISUNICODEPUNCT(off)             md_is_unicode_punct__(md_decode_utf8__(STR(off), ctx->size - (off), NULL))
943
0
    #define ISUNICODEPUNCTBEFORE(off)       md_is_unicode_punct__(md_decode_utf8_before__(ctx, off))
944
945
    static inline unsigned
946
    md_decode_unicode(const CHAR* str, OFF off, SZ str_size, SZ* p_char_size)
947
0
    {
948
0
        return md_decode_utf8__(str+off, str_size-off, p_char_size);
949
0
    }
950
#else
951
    #define ISUNICODEWHITESPACE_(codepoint) ISWHITESPACE_(codepoint)
952
    #define ISUNICODEWHITESPACE(off)        ISWHITESPACE(off)
953
    #define ISUNICODEWHITESPACEBEFORE(off)  ISWHITESPACE((off)-1)
954
955
    #define ISUNICODEPUNCT(off)             ISPUNCT(off)
956
    #define ISUNICODEPUNCTBEFORE(off)       ISPUNCT((off)-1)
957
958
    static inline void
959
    md_get_unicode_fold_info(unsigned codepoint, MD_UNICODE_FOLD_INFO* info)
960
    {
961
        info->codepoints[0] = codepoint;
962
        if(ISUPPER_(codepoint))
963
            info->codepoints[0] += 'a' - 'A';
964
        info->n_codepoints = 1;
965
    }
966
967
    static inline unsigned
968
    md_decode_unicode(const CHAR* str, OFF off, SZ str_size, SZ* p_size)
969
    {
970
        *p_size = 1;
971
        return (unsigned) str[off];
972
    }
973
#endif
974
975
976
/*************************************
977
 ***  Helper string manipulations  ***
978
 *************************************/
979
980
/* Fill buffer with copy of the string between 'beg' and 'end' but replace any
981
 * line breaks with given replacement character.
982
 *
983
 * NOTE: Caller is responsible to make sure the buffer is large enough.
984
 * (Given the output is always shorter then input, (end - beg) is good idea
985
 * what the caller should allocate.)
986
 */
987
static void
988
md_merge_lines(MD_CTX* ctx, OFF beg, OFF end, const MD_LINE* lines, MD_SIZE n_lines,
989
               CHAR line_break_replacement_char, CHAR* buffer, SZ* p_size)
990
0
{
991
0
    CHAR* ptr = buffer;
992
0
    int line_index = 0;
993
0
    OFF off = beg;
994
995
0
    MD_UNUSED(n_lines);
996
997
0
    while(1) {
998
0
        const MD_LINE* line = &lines[line_index];
999
0
        OFF line_end = line->end;
1000
0
        if(end < line_end)
1001
0
            line_end = end;
1002
1003
0
        while(off < line_end) {
1004
0
            *ptr = CH(off);
1005
0
            ptr++;
1006
0
            off++;
1007
0
        }
1008
1009
0
        if(off >= end) {
1010
0
            *p_size = (MD_SIZE)(ptr - buffer);
1011
0
            return;
1012
0
        }
1013
1014
0
        *ptr = line_break_replacement_char;
1015
0
        ptr++;
1016
1017
0
        line_index++;
1018
0
        off = lines[line_index].beg;
1019
0
    }
1020
0
}
1021
1022
/* Wrapper of md_merge_lines() which allocates new buffer for the output string.
1023
 */
1024
static int
1025
md_merge_lines_alloc(MD_CTX* ctx, OFF beg, OFF end, const MD_LINE* lines, MD_SIZE n_lines,
1026
                    CHAR line_break_replacement_char, CHAR** p_str, SZ* p_size)
1027
0
{
1028
0
    CHAR* buffer;
1029
1030
0
    buffer = (CHAR*) malloc(sizeof(CHAR) * (end - beg));
1031
0
    if(buffer == NULL) {
1032
0
        MD_LOG("malloc() failed.");
1033
0
        return -1;
1034
0
    }
1035
1036
0
    md_merge_lines(ctx, beg, end, lines, n_lines,
1037
0
                line_break_replacement_char, buffer, p_size);
1038
1039
0
    *p_str = buffer;
1040
0
    return 0;
1041
0
}
1042
1043
static OFF
1044
md_skip_unicode_whitespace(const CHAR* label, OFF off, SZ size)
1045
0
{
1046
0
    SZ char_size;
1047
0
    unsigned codepoint;
1048
1049
0
    while(off < size) {
1050
0
        codepoint = md_decode_unicode(label, off, size, &char_size);
1051
0
        if(!ISUNICODEWHITESPACE_(codepoint)  &&  !ISNEWLINE_(label[off]))
1052
0
            break;
1053
0
        off += char_size;
1054
0
    }
1055
1056
0
    return off;
1057
0
}
1058
1059
1060
/******************************
1061
 ***  Recognizing raw HTML  ***
1062
 ******************************/
1063
1064
/* md_is_html_tag() may be called when processing inlines (inline raw HTML)
1065
 * or when breaking document to blocks (checking for start of HTML block type 7).
1066
 *
1067
 * When breaking document to blocks, we do not yet know line boundaries, but
1068
 * in that case the whole tag has to live on a single line. We distinguish this
1069
 * by n_lines == 0.
1070
 */
1071
static int
1072
md_is_html_tag(MD_CTX* ctx, const MD_LINE* lines, MD_SIZE n_lines, OFF beg, OFF max_end, OFF* p_end)
1073
0
{
1074
0
    int attr_state;
1075
0
    OFF off = beg;
1076
0
    OFF line_end = (n_lines > 0) ? lines[0].end : ctx->size;
1077
0
    MD_SIZE line_index = 0;
1078
1079
0
    MD_ASSERT(CH(beg) == _T('<'));
1080
1081
0
    if(off + 1 >= line_end)
1082
0
        return FALSE;
1083
0
    off++;
1084
1085
    /* For parsing attributes, we need a little state automaton below.
1086
     * State -1: no attributes are allowed.
1087
     * State 0: attribute could follow after some whitespace.
1088
     * State 1: after a whitespace (attribute name may follow).
1089
     * State 2: after attribute name ('=' MAY follow).
1090
     * State 3: after '=' (value specification MUST follow).
1091
     * State 41: in middle of unquoted attribute value.
1092
     * State 42: in middle of single-quoted attribute value.
1093
     * State 43: in middle of double-quoted attribute value.
1094
     */
1095
0
    attr_state = 0;
1096
1097
0
    if(CH(off) == _T('/')) {
1098
        /* Closer tag "</ ... >". No attributes may be present. */
1099
0
        attr_state = -1;
1100
0
        off++;
1101
0
    }
1102
1103
    /* Tag name */
1104
0
    if(off >= line_end  ||  !ISALPHA(off))
1105
0
        return FALSE;
1106
0
    off++;
1107
0
    while(off < line_end  &&  (ISALNUM(off)  ||  CH(off) == _T('-')))
1108
0
        off++;
1109
1110
    /* (Optional) attributes (if not closer), (optional) '/' (if not closer)
1111
     * and final '>'. */
1112
0
    while(1) {
1113
0
        while(off < line_end  &&  !ISNEWLINE(off)) {
1114
0
            if(attr_state > 40) {
1115
0
                if(attr_state == 41 && (ISBLANK(off) || ISANYOF(off, _T("\"'=<>`")))) {
1116
0
                    attr_state = 0;
1117
0
                    off--;  /* Put the char back for re-inspection in the new state. */
1118
0
                } else if(attr_state == 42 && CH(off) == _T('\'')) {
1119
0
                    attr_state = 0;
1120
0
                } else if(attr_state == 43 && CH(off) == _T('"')) {
1121
0
                    attr_state = 0;
1122
0
                }
1123
0
                off++;
1124
0
            } else if(ISWHITESPACE(off)) {
1125
0
                if(attr_state == 0)
1126
0
                    attr_state = 1;
1127
0
                off++;
1128
0
            } else if(attr_state <= 2 && CH(off) == _T('>')) {
1129
                /* End. */
1130
0
                goto done;
1131
0
            } else if(attr_state <= 2 && CH(off) == _T('/') && off+1 < line_end && CH(off+1) == _T('>')) {
1132
                /* End with digraph '/>' */
1133
0
                off++;
1134
0
                goto done;
1135
0
            } else if((attr_state == 1 || attr_state == 2) && (ISALPHA(off) || CH(off) == _T('_') || CH(off) == _T(':'))) {
1136
0
                off++;
1137
                /* Attribute name */
1138
0
                while(off < line_end && (ISALNUM(off) || ISANYOF(off, _T("_.:-"))))
1139
0
                    off++;
1140
0
                attr_state = 2;
1141
0
            } else if(attr_state == 2 && CH(off) == _T('=')) {
1142
                /* Attribute assignment sign */
1143
0
                off++;
1144
0
                attr_state = 3;
1145
0
            } else if(attr_state == 3) {
1146
                /* Expecting start of attribute value. */
1147
0
                if(CH(off) == _T('"'))
1148
0
                    attr_state = 43;
1149
0
                else if(CH(off) == _T('\''))
1150
0
                    attr_state = 42;
1151
0
                else if(!ISANYOF(off, _T("\"'=<>`"))  &&  !ISNEWLINE(off))
1152
0
                    attr_state = 41;
1153
0
                else
1154
0
                    return FALSE;
1155
0
                off++;
1156
0
            } else {
1157
                /* Anything unexpected. */
1158
0
                return FALSE;
1159
0
            }
1160
0
        }
1161
1162
        /* We have to be on a single line. See definition of start condition
1163
         * of HTML block, type 7. */
1164
0
        if(n_lines == 0)
1165
0
            return FALSE;
1166
1167
0
        line_index++;
1168
0
        if(line_index >= n_lines)
1169
0
            return FALSE;
1170
1171
0
        off = lines[line_index].beg;
1172
0
        line_end = lines[line_index].end;
1173
1174
0
        if(attr_state == 0  ||  attr_state == 41)
1175
0
            attr_state = 1;
1176
1177
0
        if(off >= max_end)
1178
0
            return FALSE;
1179
0
    }
1180
1181
0
done:
1182
0
    if(off >= max_end)
1183
0
        return FALSE;
1184
1185
0
    *p_end = off+1;
1186
0
    return TRUE;
1187
0
}
1188
1189
static int
1190
md_scan_for_html_closer(MD_CTX* ctx, const MD_CHAR* str, MD_SIZE len,
1191
                        const MD_LINE* lines, MD_SIZE n_lines,
1192
                        OFF beg, OFF max_end, OFF* p_end,
1193
                        OFF* p_scan_horizon)
1194
0
{
1195
0
    OFF off = beg;
1196
0
    MD_SIZE line_index = 0;
1197
1198
0
    if(off < *p_scan_horizon  &&  *p_scan_horizon >= max_end - len) {
1199
        /* We have already scanned the range up to the max_end so we know
1200
         * there is nothing to see. */
1201
0
        return FALSE;
1202
0
    }
1203
1204
0
    while(TRUE) {
1205
0
        while(off + len <= lines[line_index].end  &&  off + len <= max_end) {
1206
0
            if(md_ascii_eq(STR(off), str, len)) {
1207
                /* Success. */
1208
0
                *p_end = off + len;
1209
0
                return TRUE;
1210
0
            }
1211
0
            off++;
1212
0
        }
1213
1214
0
        line_index++;
1215
0
        if(off >= max_end  ||  line_index >= n_lines) {
1216
            /* Failure. */
1217
0
            *p_scan_horizon = off;
1218
0
            return FALSE;
1219
0
        }
1220
1221
0
        off = lines[line_index].beg;
1222
0
    }
1223
0
}
1224
1225
static int
1226
md_is_html_comment(MD_CTX* ctx, const MD_LINE* lines, MD_SIZE n_lines, OFF beg, OFF max_end, OFF* p_end)
1227
0
{
1228
0
    OFF off = beg;
1229
1230
0
    MD_ASSERT(CH(beg) == _T('<'));
1231
1232
0
    if(off + 4 >= lines[0].end)
1233
0
        return FALSE;
1234
0
    if(CH(off+1) != _T('!')  ||  CH(off+2) != _T('-')  ||  CH(off+3) != _T('-'))
1235
0
        return FALSE;
1236
1237
    /* Skip only "<!" so that we accept also "<!-->" or "<!--->" */
1238
0
    off += 2;
1239
1240
    /* Scan for ordinary comment closer "-->". */
1241
0
    return md_scan_for_html_closer(ctx, _T("-->"), 3,
1242
0
                lines, n_lines, off, max_end, p_end, &ctx->html_comment_horizon);
1243
0
}
1244
1245
static int
1246
md_is_html_processing_instruction(MD_CTX* ctx, const MD_LINE* lines, MD_SIZE n_lines, OFF beg, OFF max_end, OFF* p_end)
1247
0
{
1248
0
    OFF off = beg;
1249
1250
0
    if(off + 2 >= lines[0].end)
1251
0
        return FALSE;
1252
0
    if(CH(off+1) != _T('?'))
1253
0
        return FALSE;
1254
0
    off += 2;
1255
1256
0
    return md_scan_for_html_closer(ctx, _T("?>"), 2,
1257
0
                lines, n_lines, off, max_end, p_end, &ctx->html_proc_instr_horizon);
1258
0
}
1259
1260
static int
1261
md_is_html_declaration(MD_CTX* ctx, const MD_LINE* lines, MD_SIZE n_lines, OFF beg, OFF max_end, OFF* p_end)
1262
0
{
1263
0
    OFF off = beg;
1264
1265
0
    if(off + 2 >= lines[0].end)
1266
0
        return FALSE;
1267
0
    if(CH(off+1) != _T('!'))
1268
0
        return FALSE;
1269
0
    off += 2;
1270
1271
    /* Declaration name. */
1272
0
    if(off >= lines[0].end  ||  !ISALPHA(off))
1273
0
        return FALSE;
1274
0
    off++;
1275
0
    while(off < lines[0].end  &&  ISALPHA(off))
1276
0
        off++;
1277
1278
0
    return md_scan_for_html_closer(ctx, _T(">"), 1,
1279
0
                lines, n_lines, off, max_end, p_end, &ctx->html_decl_horizon);
1280
0
}
1281
1282
static int
1283
md_is_html_cdata(MD_CTX* ctx, const MD_LINE* lines, MD_SIZE n_lines, OFF beg, OFF max_end, OFF* p_end)
1284
0
{
1285
0
    static const CHAR open_str[] = _T("<![CDATA[");
1286
0
    static const SZ open_size = SIZEOF_ARRAY(open_str) - 1;
1287
1288
0
    OFF off = beg;
1289
1290
0
    if(off + open_size >= lines[0].end)
1291
0
        return FALSE;
1292
0
    if(memcmp(STR(off), open_str, open_size) != 0)
1293
0
        return FALSE;
1294
0
    off += open_size;
1295
1296
0
    return md_scan_for_html_closer(ctx, _T("]]>"), 3,
1297
0
                lines, n_lines, off, max_end, p_end, &ctx->html_cdata_horizon);
1298
0
}
1299
1300
static int
1301
md_is_html_any(MD_CTX* ctx, const MD_LINE* lines, MD_SIZE n_lines, OFF beg, OFF max_end, OFF* p_end)
1302
0
{
1303
0
    MD_ASSERT(CH(beg) == _T('<'));
1304
0
    return (md_is_html_tag(ctx, lines, n_lines, beg, max_end, p_end)  ||
1305
0
            md_is_html_comment(ctx, lines, n_lines, beg, max_end, p_end)  ||
1306
0
            md_is_html_processing_instruction(ctx, lines, n_lines, beg, max_end, p_end)  ||
1307
0
            md_is_html_declaration(ctx, lines, n_lines, beg, max_end, p_end)  ||
1308
0
            md_is_html_cdata(ctx, lines, n_lines, beg, max_end, p_end));
1309
0
}
1310
1311
1312
/****************************
1313
 ***  Recognizing Entity  ***
1314
 ****************************/
1315
1316
static int
1317
md_is_hex_entity_contents(MD_CTX* ctx, const CHAR* text, OFF beg, OFF max_end, OFF* p_end)
1318
0
{
1319
0
    OFF off = beg;
1320
0
    MD_UNUSED(ctx);
1321
1322
0
    while(off < max_end  &&  ISXDIGIT_(text[off])  &&  off - beg <= 8)
1323
0
        off++;
1324
1325
0
    if(1 <= off - beg  &&  off - beg <= 6) {
1326
0
        *p_end = off;
1327
0
        return TRUE;
1328
0
    } else {
1329
0
        return FALSE;
1330
0
    }
1331
0
}
1332
1333
static int
1334
md_is_dec_entity_contents(MD_CTX* ctx, const CHAR* text, OFF beg, OFF max_end, OFF* p_end)
1335
0
{
1336
0
    OFF off = beg;
1337
0
    MD_UNUSED(ctx);
1338
1339
0
    while(off < max_end  &&  ISDIGIT_(text[off])  &&  off - beg <= 8)
1340
0
        off++;
1341
1342
0
    if(1 <= off - beg  &&  off - beg <= 7) {
1343
0
        *p_end = off;
1344
0
        return TRUE;
1345
0
    } else {
1346
0
        return FALSE;
1347
0
    }
1348
0
}
1349
1350
static int
1351
md_is_named_entity_contents(MD_CTX* ctx, const CHAR* text, OFF beg, OFF max_end, OFF* p_end)
1352
0
{
1353
0
    OFF off = beg;
1354
0
    MD_UNUSED(ctx);
1355
1356
0
    if(off < max_end  &&  ISALPHA_(text[off]))
1357
0
        off++;
1358
0
    else
1359
0
        return FALSE;
1360
1361
0
    while(off < max_end  &&  ISALNUM_(text[off])  &&  off - beg <= 48)
1362
0
        off++;
1363
1364
0
    if(2 <= off - beg  &&  off - beg <= 48) {
1365
0
        *p_end = off;
1366
0
        return TRUE;
1367
0
    } else {
1368
0
        return FALSE;
1369
0
    }
1370
0
}
1371
1372
static int
1373
md_is_entity_str(MD_CTX* ctx, const CHAR* text, OFF beg, OFF max_end, OFF* p_end)
1374
0
{
1375
0
    int is_contents;
1376
0
    OFF off = beg;
1377
1378
0
    MD_ASSERT(text[off] == _T('&'));
1379
0
    off++;
1380
1381
0
    if(off+2 < max_end  &&  text[off] == _T('#')  &&  (text[off+1] == _T('x') || text[off+1] == _T('X')))
1382
0
        is_contents = md_is_hex_entity_contents(ctx, text, off+2, max_end, &off);
1383
0
    else if(off+1 < max_end  &&  text[off] == _T('#'))
1384
0
        is_contents = md_is_dec_entity_contents(ctx, text, off+1, max_end, &off);
1385
0
    else
1386
0
        is_contents = md_is_named_entity_contents(ctx, text, off, max_end, &off);
1387
1388
0
    if(is_contents  &&  off < max_end  &&  text[off] == _T(';')) {
1389
0
        *p_end = off+1;
1390
0
        return TRUE;
1391
0
    } else {
1392
0
        return FALSE;
1393
0
    }
1394
0
}
1395
1396
static inline int
1397
md_is_entity(MD_CTX* ctx, OFF beg, OFF max_end, OFF* p_end)
1398
0
{
1399
0
    return md_is_entity_str(ctx, ctx->text, beg, max_end, p_end);
1400
0
}
1401
1402
1403
/******************************
1404
 ***  Attribute Management  ***
1405
 ******************************/
1406
1407
typedef struct MD_ATTRIBUTE_BUILD_tag MD_ATTRIBUTE_BUILD;
1408
struct MD_ATTRIBUTE_BUILD_tag {
1409
    CHAR* text;
1410
    MD_TEXTTYPE* substr_types;
1411
    OFF* substr_offsets;
1412
    int substr_count;
1413
    int substr_alloc;
1414
    MD_TEXTTYPE trivial_types[1];
1415
    OFF trivial_offsets[2];
1416
};
1417
1418
1419
0
#define MD_BUILD_ATTR_NO_ESCAPES    0x0001
1420
1421
static int
1422
md_build_attr_append_substr(MD_CTX* ctx, MD_ATTRIBUTE_BUILD* build,
1423
                            MD_TEXTTYPE type, OFF off)
1424
0
{
1425
0
    if(build->substr_count >= build->substr_alloc) {
1426
0
        MD_TEXTTYPE* new_substr_types;
1427
0
        OFF* new_substr_offsets;
1428
1429
0
        build->substr_alloc = (build->substr_alloc > 0
1430
0
                ? build->substr_alloc + build->substr_alloc / 2
1431
0
                : 8);
1432
0
        new_substr_types = (MD_TEXTTYPE*) realloc(build->substr_types,
1433
0
                                    build->substr_alloc * sizeof(MD_TEXTTYPE));
1434
0
        if(new_substr_types == NULL) {
1435
0
            MD_LOG("realloc() failed.");
1436
0
            return -1;
1437
0
        }
1438
        /* Note +1 to reserve space for final offset (== raw_size). */
1439
0
        new_substr_offsets = (OFF*) realloc(build->substr_offsets,
1440
0
                                    (build->substr_alloc+1) * sizeof(OFF));
1441
0
        if(new_substr_offsets == NULL) {
1442
0
            MD_LOG("realloc() failed.");
1443
0
            free(new_substr_types);
1444
0
            return -1;
1445
0
        }
1446
1447
0
        build->substr_types = new_substr_types;
1448
0
        build->substr_offsets = new_substr_offsets;
1449
0
    }
1450
1451
0
    build->substr_types[build->substr_count] = type;
1452
0
    build->substr_offsets[build->substr_count] = off;
1453
0
    build->substr_count++;
1454
0
    return 0;
1455
0
}
1456
1457
static void
1458
md_free_attribute(MD_CTX* ctx, MD_ATTRIBUTE_BUILD* build)
1459
0
{
1460
0
    MD_UNUSED(ctx);
1461
1462
0
    if(build->substr_alloc > 0) {
1463
0
        free(build->text);
1464
0
        free(build->substr_types);
1465
0
        free(build->substr_offsets);
1466
0
    }
1467
0
}
1468
1469
static int
1470
md_build_attribute(MD_CTX* ctx, const CHAR* raw_text, SZ raw_size,
1471
                   unsigned flags, MD_ATTRIBUTE* attr, MD_ATTRIBUTE_BUILD* build)
1472
0
{
1473
0
    OFF raw_off, off;
1474
0
    int is_trivial;
1475
0
    int ret = 0;
1476
1477
0
    memset(build, 0, sizeof(MD_ATTRIBUTE_BUILD));
1478
1479
    /* If there is no backslash and no ampersand, build trivial attribute
1480
     * without any malloc(). */
1481
0
    is_trivial = TRUE;
1482
0
    for(raw_off = 0; raw_off < raw_size; raw_off++) {
1483
0
        if(ISANYOF3_(raw_text[raw_off], _T('\\'), _T('&'), _T('\0'))) {
1484
0
            is_trivial = FALSE;
1485
0
            break;
1486
0
        }
1487
0
    }
1488
1489
0
    if(is_trivial) {
1490
0
        build->text = (CHAR*) (raw_size ? raw_text : NULL);
1491
0
        build->substr_types = build->trivial_types;
1492
0
        build->substr_offsets = build->trivial_offsets;
1493
0
        build->substr_count = 1;
1494
0
        build->substr_alloc = 0;
1495
0
        build->trivial_types[0] = MD_TEXT_NORMAL;
1496
0
        build->trivial_offsets[0] = 0;
1497
0
        build->trivial_offsets[1] = raw_size;
1498
0
        off = raw_size;
1499
0
    } else {
1500
0
        build->text = (CHAR*) malloc(raw_size * sizeof(CHAR));
1501
0
        if(build->text == NULL) {
1502
0
            MD_LOG("malloc() failed.");
1503
0
            goto abort;
1504
0
        }
1505
1506
0
        raw_off = 0;
1507
0
        off = 0;
1508
1509
0
        while(raw_off < raw_size) {
1510
0
            if(raw_text[raw_off] == _T('\0')) {
1511
0
                MD_CHECK(md_build_attr_append_substr(ctx, build, MD_TEXT_NULLCHAR, off));
1512
0
                memcpy(build->text + off, raw_text + raw_off, 1);
1513
0
                off++;
1514
0
                raw_off++;
1515
0
                continue;
1516
0
            }
1517
1518
0
            if(raw_text[raw_off] == _T('&')) {
1519
0
                OFF ent_end;
1520
1521
0
                if(md_is_entity_str(ctx, raw_text, raw_off, raw_size, &ent_end)) {
1522
0
                    MD_CHECK(md_build_attr_append_substr(ctx, build, MD_TEXT_ENTITY, off));
1523
0
                    memcpy(build->text + off, raw_text + raw_off, ent_end - raw_off);
1524
0
                    off += ent_end - raw_off;
1525
0
                    raw_off = ent_end;
1526
0
                    continue;
1527
0
                }
1528
0
            }
1529
1530
0
            if(build->substr_count == 0  ||  build->substr_types[build->substr_count-1] != MD_TEXT_NORMAL)
1531
0
                MD_CHECK(md_build_attr_append_substr(ctx, build, MD_TEXT_NORMAL, off));
1532
1533
0
            if(!(flags & MD_BUILD_ATTR_NO_ESCAPES)  &&
1534
0
               raw_text[raw_off] == _T('\\')  &&  raw_off+1 < raw_size  &&
1535
0
               (ISPUNCT_(raw_text[raw_off+1]) || ISNEWLINE_(raw_text[raw_off+1])))
1536
0
                raw_off++;
1537
1538
0
            build->text[off++] = raw_text[raw_off++];
1539
0
        }
1540
0
        build->substr_offsets[build->substr_count] = off;
1541
0
    }
1542
1543
0
    attr->text = build->text;
1544
0
    attr->size = off;
1545
0
    attr->substr_offsets = build->substr_offsets;
1546
0
    attr->substr_types = build->substr_types;
1547
0
    return 0;
1548
1549
0
abort:
1550
0
    md_free_attribute(ctx, build);
1551
0
    return -1;
1552
0
}
1553
1554
1555
/*********************************************
1556
 ***  Dictionary of Reference Definitions  ***
1557
 *********************************************/
1558
1559
0
#define MD_FNV1A_BASE       2166136261U
1560
0
#define MD_FNV1A_PRIME      16777619U
1561
1562
static inline unsigned
1563
md_fnv1a(unsigned base, const void* data, size_t n)
1564
0
{
1565
0
    const unsigned char* buf = (const unsigned char*) data;
1566
0
    unsigned hash = base;
1567
0
    size_t i;
1568
1569
0
    for(i = 0; i < n; i++) {
1570
0
        hash ^= buf[i];
1571
0
        hash *= MD_FNV1A_PRIME;
1572
0
    }
1573
1574
0
    return hash;
1575
0
}
1576
1577
1578
struct MD_REF_DEF_tag {
1579
    CHAR* label;
1580
    CHAR* title;
1581
    unsigned hash;
1582
    SZ label_size;
1583
    SZ title_size;
1584
    OFF dest_beg;
1585
    OFF dest_end;
1586
    unsigned char label_needs_free : 1;
1587
    unsigned char title_needs_free : 1;
1588
};
1589
1590
/* Label equivalence is quite complicated with regards to whitespace and case
1591
 * folding. This complicates computing a hash of it as well as direct comparison
1592
 * of two labels. */
1593
1594
static unsigned
1595
md_link_label_hash(const CHAR* label, SZ size)
1596
0
{
1597
0
    unsigned hash = MD_FNV1A_BASE;
1598
0
    OFF off;
1599
0
    unsigned codepoint;
1600
0
    int is_whitespace = FALSE;
1601
1602
0
    off = md_skip_unicode_whitespace(label, 0, size);
1603
0
    while(off < size) {
1604
0
        SZ char_size;
1605
1606
0
        codepoint = md_decode_unicode(label, off, size, &char_size);
1607
0
        is_whitespace = ISUNICODEWHITESPACE_(codepoint) || ISNEWLINE_(label[off]);
1608
1609
0
        if(is_whitespace) {
1610
0
            codepoint = ' ';
1611
0
            hash = md_fnv1a(hash, &codepoint, sizeof(unsigned));
1612
0
            off = md_skip_unicode_whitespace(label, off, size);
1613
0
        } else {
1614
0
            MD_UNICODE_FOLD_INFO fold_info;
1615
1616
0
            md_get_unicode_fold_info(codepoint, &fold_info);
1617
0
            hash = md_fnv1a(hash, fold_info.codepoints, fold_info.n_codepoints * sizeof(unsigned));
1618
0
            off += char_size;
1619
0
        }
1620
0
    }
1621
1622
0
    return hash;
1623
0
}
1624
1625
static OFF
1626
md_link_label_cmp_load_fold_info(const CHAR* label, OFF off, SZ size,
1627
                                 MD_UNICODE_FOLD_INFO* fold_info)
1628
0
{
1629
0
    unsigned codepoint;
1630
0
    SZ char_size;
1631
1632
0
    if(off >= size) {
1633
        /* Treat end of a link label as a whitespace. */
1634
0
        goto whitespace;
1635
0
    }
1636
1637
0
    codepoint = md_decode_unicode(label, off, size, &char_size);
1638
0
    off += char_size;
1639
0
    if(ISUNICODEWHITESPACE_(codepoint)) {
1640
        /* Treat all whitespace as equivalent */
1641
0
        goto whitespace;
1642
0
    }
1643
1644
    /* Get real folding info. */
1645
0
    md_get_unicode_fold_info(codepoint, fold_info);
1646
0
    return off;
1647
1648
0
whitespace:
1649
0
    fold_info->codepoints[0] = _T(' ');
1650
0
    fold_info->n_codepoints = 1;
1651
0
    return md_skip_unicode_whitespace(label, off, size);
1652
0
}
1653
1654
static int
1655
md_link_label_cmp(const CHAR* a_label, SZ a_size, const CHAR* b_label, SZ b_size)
1656
0
{
1657
0
    OFF a_off;
1658
0
    OFF b_off;
1659
0
    MD_UNICODE_FOLD_INFO a_fi = { { 0 }, 0 };
1660
0
    MD_UNICODE_FOLD_INFO b_fi = { { 0 }, 0 };
1661
0
    OFF a_fi_off = 0;
1662
0
    OFF b_fi_off = 0;
1663
0
    int cmp;
1664
1665
0
    a_off = md_skip_unicode_whitespace(a_label, 0, a_size);
1666
0
    b_off = md_skip_unicode_whitespace(b_label, 0, b_size);
1667
0
    while(a_off < a_size || a_fi_off < a_fi.n_codepoints ||
1668
0
          b_off < b_size || b_fi_off < b_fi.n_codepoints)
1669
0
    {
1670
        /* If needed, load fold info for next char. */
1671
0
        if(a_fi_off >= a_fi.n_codepoints) {
1672
0
            a_fi_off = 0;
1673
0
            a_off = md_link_label_cmp_load_fold_info(a_label, a_off, a_size, &a_fi);
1674
0
        }
1675
0
        if(b_fi_off >= b_fi.n_codepoints) {
1676
0
            b_fi_off = 0;
1677
0
            b_off = md_link_label_cmp_load_fold_info(b_label, b_off, b_size, &b_fi);
1678
0
        }
1679
1680
0
        cmp = b_fi.codepoints[b_fi_off] - a_fi.codepoints[a_fi_off];
1681
0
        if(cmp != 0)
1682
0
            return cmp;
1683
1684
0
        a_fi_off++;
1685
0
        b_fi_off++;
1686
0
    }
1687
1688
0
    return 0;
1689
0
}
1690
1691
typedef struct MD_REF_DEF_LIST_tag MD_REF_DEF_LIST;
1692
struct MD_REF_DEF_LIST_tag {
1693
    int n_ref_defs;
1694
    int alloc_ref_defs;
1695
    MD_REF_DEF* ref_defs[];  /* Valid items always  point into ctx->ref_defs[] */
1696
};
1697
1698
static int
1699
md_ref_def_cmp(const void* a, const void* b)
1700
0
{
1701
0
    const MD_REF_DEF* a_ref = *(const MD_REF_DEF**)a;
1702
0
    const MD_REF_DEF* b_ref = *(const MD_REF_DEF**)b;
1703
1704
0
    if(a_ref->hash < b_ref->hash)
1705
0
        return -1;
1706
0
    else if(a_ref->hash > b_ref->hash)
1707
0
        return +1;
1708
0
    else
1709
0
        return md_link_label_cmp(a_ref->label, a_ref->label_size, b_ref->label, b_ref->label_size);
1710
0
}
1711
1712
static int
1713
md_ref_def_cmp_for_sort(const void* a, const void* b)
1714
0
{
1715
0
    int cmp;
1716
1717
0
    cmp = md_ref_def_cmp(a, b);
1718
1719
    /* Ensure stability of the sorting. */
1720
0
    if(cmp == 0) {
1721
0
        const MD_REF_DEF* a_ref = *(const MD_REF_DEF**)a;
1722
0
        const MD_REF_DEF* b_ref = *(const MD_REF_DEF**)b;
1723
1724
0
        if(a_ref < b_ref)
1725
0
            cmp = -1;
1726
0
        else if(a_ref > b_ref)
1727
0
            cmp = +1;
1728
0
        else
1729
0
            cmp = 0;
1730
0
    }
1731
1732
0
    return cmp;
1733
0
}
1734
1735
static int
1736
md_build_ref_def_hashtable(MD_CTX* ctx)
1737
0
{
1738
0
    int i, j;
1739
1740
0
    if(ctx->n_ref_defs == 0)
1741
0
        return 0;
1742
1743
0
    ctx->ref_def_hashtable_size = (ctx->n_ref_defs * 5) / 4;
1744
0
    ctx->ref_def_hashtable = malloc(ctx->ref_def_hashtable_size * sizeof(void*));
1745
0
    if(ctx->ref_def_hashtable == NULL) {
1746
0
        MD_LOG("malloc() failed.");
1747
0
        goto abort;
1748
0
    }
1749
0
    memset(ctx->ref_def_hashtable, 0, ctx->ref_def_hashtable_size * sizeof(void*));
1750
1751
    /* Each member of ctx->ref_def_hashtable[] can be:
1752
     *  -- NULL,
1753
     *  -- pointer to the MD_REF_DEF in ctx->ref_defs[], or
1754
     *  -- pointer to a MD_REF_DEF_LIST, which holds multiple pointers to
1755
     *     such MD_REF_DEFs.
1756
     */
1757
0
    for(i = 0; i < ctx->n_ref_defs; i++) {
1758
0
        MD_REF_DEF* def = &ctx->ref_defs[i];
1759
0
        void* bucket;
1760
0
        MD_REF_DEF_LIST* list;
1761
1762
0
        def->hash = md_link_label_hash(def->label, def->label_size);
1763
0
        bucket = ctx->ref_def_hashtable[def->hash % ctx->ref_def_hashtable_size];
1764
1765
0
        if(bucket == NULL) {
1766
            /* The bucket is empty. Make it just point to the def. */
1767
0
            ctx->ref_def_hashtable[def->hash % ctx->ref_def_hashtable_size] = def;
1768
0
            continue;
1769
0
        }
1770
1771
0
        if(ctx->ref_defs <= (MD_REF_DEF*) bucket  &&  (MD_REF_DEF*) bucket < ctx->ref_defs + ctx->n_ref_defs) {
1772
            /* The bucket already contains one ref. def. Lets see whether it
1773
             * is the same label (ref. def. duplicate) or different one
1774
             * (hash conflict). */
1775
0
            MD_REF_DEF* old_def = (MD_REF_DEF*) bucket;
1776
1777
0
            if(md_link_label_cmp(def->label, def->label_size, old_def->label, old_def->label_size) == 0) {
1778
                /* Duplicate label: Ignore this ref. def. */
1779
0
                continue;
1780
0
            }
1781
1782
            /* Make the bucket complex, i.e. able to hold more ref. defs. */
1783
0
            list = (MD_REF_DEF_LIST*) malloc(sizeof(MD_REF_DEF_LIST) + 2 * sizeof(MD_REF_DEF*));
1784
0
            if(list == NULL) {
1785
0
                MD_LOG("malloc() failed.");
1786
0
                goto abort;
1787
0
            }
1788
0
            list->ref_defs[0] = old_def;
1789
0
            list->ref_defs[1] = def;
1790
0
            list->n_ref_defs = 2;
1791
0
            list->alloc_ref_defs = 2;
1792
0
            ctx->ref_def_hashtable[def->hash % ctx->ref_def_hashtable_size] = list;
1793
0
            continue;
1794
0
        }
1795
1796
        /* Append the def to the complex bucket list.
1797
         *
1798
         * Note in this case we ignore potential duplicates to avoid expensive
1799
         * iterating over the complex bucket. Below, we revisit all the complex
1800
         * buckets and handle it more cheaply after the complex bucket contents
1801
         * is sorted. */
1802
0
        list = (MD_REF_DEF_LIST*) bucket;
1803
0
        if(list->n_ref_defs >= list->alloc_ref_defs) {
1804
0
            int alloc_ref_defs = list->alloc_ref_defs + list->alloc_ref_defs / 2;
1805
0
            MD_REF_DEF_LIST* list_tmp = (MD_REF_DEF_LIST*) realloc(list,
1806
0
                        sizeof(MD_REF_DEF_LIST) + alloc_ref_defs * sizeof(MD_REF_DEF*));
1807
0
            if(list_tmp == NULL) {
1808
0
                MD_LOG("realloc() failed.");
1809
0
                goto abort;
1810
0
            }
1811
0
            list = list_tmp;
1812
0
            list->alloc_ref_defs = alloc_ref_defs;
1813
0
            ctx->ref_def_hashtable[def->hash % ctx->ref_def_hashtable_size] = list;
1814
0
        }
1815
1816
0
        list->ref_defs[list->n_ref_defs] = def;
1817
0
        list->n_ref_defs++;
1818
0
    }
1819
1820
    /* Sort the complex buckets so we can use bsearch() with them. */
1821
0
    for(i = 0; i < ctx->ref_def_hashtable_size; i++) {
1822
0
        void* bucket = ctx->ref_def_hashtable[i];
1823
0
        MD_REF_DEF_LIST* list;
1824
1825
0
        if(bucket == NULL)
1826
0
            continue;
1827
0
        if(ctx->ref_defs <= (MD_REF_DEF*) bucket  &&  (MD_REF_DEF*) bucket < ctx->ref_defs + ctx->n_ref_defs)
1828
0
            continue;
1829
1830
0
        list = (MD_REF_DEF_LIST*) bucket;
1831
0
        qsort(list->ref_defs, list->n_ref_defs, sizeof(MD_REF_DEF*), md_ref_def_cmp_for_sort);
1832
1833
        /* Disable all duplicates in the complex bucket by forcing all such
1834
         * records to point to the 1st such ref. def. I.e. no matter which
1835
         * record is found during the lookup, it will always point to the right
1836
         * ref. def. in ctx->ref_defs[]. */
1837
0
        for(j = 1; j < list->n_ref_defs; j++) {
1838
0
            if(md_ref_def_cmp(&list->ref_defs[j-1], &list->ref_defs[j]) == 0)
1839
0
                list->ref_defs[j] = list->ref_defs[j-1];
1840
0
        }
1841
0
    }
1842
1843
0
    return 0;
1844
1845
0
abort:
1846
0
    return -1;
1847
0
}
1848
1849
static void
1850
md_free_ref_def_hashtable(MD_CTX* ctx)
1851
0
{
1852
0
    if(ctx->ref_def_hashtable != NULL) {
1853
0
        int i;
1854
1855
0
        for(i = 0; i < ctx->ref_def_hashtable_size; i++) {
1856
0
            void* bucket = ctx->ref_def_hashtable[i];
1857
0
            if(bucket == NULL)
1858
0
                continue;
1859
0
            if(ctx->ref_defs <= (MD_REF_DEF*) bucket  &&  (MD_REF_DEF*) bucket < ctx->ref_defs + ctx->n_ref_defs)
1860
0
                continue;
1861
0
            free(bucket);
1862
0
        }
1863
1864
0
        free(ctx->ref_def_hashtable);
1865
0
    }
1866
0
}
1867
1868
static const MD_REF_DEF*
1869
md_lookup_ref_def(MD_CTX* ctx, const CHAR* label, SZ label_size)
1870
0
{
1871
0
    unsigned hash;
1872
0
    void* bucket;
1873
1874
0
    if(ctx->ref_def_hashtable_size == 0)
1875
0
        return NULL;
1876
1877
0
    hash = md_link_label_hash(label, label_size);
1878
0
    bucket = ctx->ref_def_hashtable[hash % ctx->ref_def_hashtable_size];
1879
1880
0
    if(bucket == NULL) {
1881
0
        return NULL;
1882
0
    } else if(ctx->ref_defs <= (MD_REF_DEF*) bucket  &&  (MD_REF_DEF*) bucket < ctx->ref_defs + ctx->n_ref_defs) {
1883
0
        const MD_REF_DEF* def = (MD_REF_DEF*) bucket;
1884
1885
0
        if(md_link_label_cmp(def->label, def->label_size, label, label_size) == 0)
1886
0
            return def;
1887
0
        else
1888
0
            return NULL;
1889
0
    } else {
1890
0
        MD_REF_DEF_LIST* list = (MD_REF_DEF_LIST*) bucket;
1891
0
        MD_REF_DEF key_buf;
1892
0
        const MD_REF_DEF* key = &key_buf;
1893
0
        const MD_REF_DEF** ret;
1894
1895
0
        key_buf.label = (CHAR*) label;
1896
0
        key_buf.label_size = label_size;
1897
0
        key_buf.hash = md_link_label_hash(key_buf.label, key_buf.label_size);
1898
1899
0
        ret = (const MD_REF_DEF**) bsearch(&key, list->ref_defs,
1900
0
                    list->n_ref_defs, sizeof(MD_REF_DEF*), md_ref_def_cmp);
1901
0
        if(ret != NULL)
1902
0
            return *ret;
1903
0
        else
1904
0
            return NULL;
1905
0
    }
1906
0
}
1907
1908
1909
/***************************
1910
 ***  Recognizing Links  ***
1911
 ***************************/
1912
1913
/* Note this code is partially shared between processing inlines and blocks
1914
 * as reference definitions and links share some helper parser functions.
1915
 */
1916
1917
typedef struct MD_LINK_ATTR_tag MD_LINK_ATTR;
1918
struct MD_LINK_ATTR_tag {
1919
    OFF dest_beg;
1920
    OFF dest_end;
1921
1922
    CHAR* title;
1923
    SZ title_size;
1924
    int title_needs_free;
1925
};
1926
1927
1928
static int
1929
md_is_link_label(MD_CTX* ctx, const MD_LINE* lines, MD_SIZE n_lines, OFF beg,
1930
                 OFF* p_end, MD_SIZE* p_beg_line_index, MD_SIZE* p_end_line_index,
1931
                 OFF* p_contents_beg, OFF* p_contents_end)
1932
0
{
1933
0
    OFF off = beg;
1934
0
    OFF contents_beg = 0;
1935
0
    OFF contents_end = 0;
1936
0
    MD_SIZE line_index = 0;
1937
0
    int len = 0;
1938
1939
0
    if(CH(off) != _T('['))
1940
0
        return FALSE;
1941
0
    off++;
1942
1943
0
    while(1) {
1944
0
        OFF line_end = lines[line_index].end;
1945
1946
0
        while(off < line_end) {
1947
0
            if(CH(off) == _T('\\')  &&  off+1 < ctx->size  &&  (ISPUNCT(off+1) || ISNEWLINE(off+1))) {
1948
0
                if(contents_end == 0) {
1949
0
                    contents_beg = off;
1950
0
                    *p_beg_line_index = line_index;
1951
0
                }
1952
0
                contents_end = off + 2;
1953
0
                off += 2;
1954
0
            } else if(CH(off) == _T('[')) {
1955
0
                return FALSE;
1956
0
            } else if(CH(off) == _T(']')) {
1957
0
                if(contents_beg < contents_end) {
1958
                    /* Success. */
1959
0
                    *p_contents_beg = contents_beg;
1960
0
                    *p_contents_end = contents_end;
1961
0
                    *p_end = off+1;
1962
0
                    *p_end_line_index = line_index;
1963
0
                    return TRUE;
1964
0
                } else {
1965
                    /* Link label must have some non-whitespace contents. */
1966
0
                    return FALSE;
1967
0
                }
1968
0
            } else {
1969
0
                unsigned codepoint;
1970
0
                SZ char_size;
1971
1972
0
                codepoint = md_decode_unicode(ctx->text, off, ctx->size, &char_size);
1973
0
                if(!ISUNICODEWHITESPACE_(codepoint)) {
1974
0
                    if(contents_end == 0) {
1975
0
                        contents_beg = off;
1976
0
                        *p_beg_line_index = line_index;
1977
0
                    }
1978
0
                    contents_end = off + char_size;
1979
0
                }
1980
1981
0
                off += char_size;
1982
0
            }
1983
1984
0
            len++;
1985
0
            if(len > 999)
1986
0
                return FALSE;
1987
0
        }
1988
1989
0
        line_index++;
1990
0
        len++;
1991
0
        if(line_index < n_lines)
1992
0
            off = lines[line_index].beg;
1993
0
        else
1994
0
            break;
1995
0
    }
1996
1997
0
    return FALSE;
1998
0
}
1999
2000
static int
2001
md_is_link_destination_A(MD_CTX* ctx, OFF beg, OFF max_end, OFF* p_end,
2002
                         OFF* p_contents_beg, OFF* p_contents_end)
2003
0
{
2004
0
    OFF off = beg;
2005
2006
0
    if(off >= max_end  ||  CH(off) != _T('<'))
2007
0
        return FALSE;
2008
0
    off++;
2009
2010
0
    while(off < max_end) {
2011
0
        if(CH(off) == _T('\\')  &&  off+1 < max_end  &&  ISPUNCT(off+1)) {
2012
0
            off += 2;
2013
0
            continue;
2014
0
        }
2015
2016
0
        if(ISNEWLINE(off)  ||  CH(off) == _T('<'))
2017
0
            return FALSE;
2018
2019
0
        if(CH(off) == _T('>')) {
2020
            /* Success. */
2021
0
            *p_contents_beg = beg+1;
2022
0
            *p_contents_end = off;
2023
0
            *p_end = off+1;
2024
0
            return TRUE;
2025
0
        }
2026
2027
0
        off++;
2028
0
    }
2029
2030
0
    return FALSE;
2031
0
}
2032
2033
static int
2034
md_is_link_destination_B(MD_CTX* ctx, OFF beg, OFF max_end, OFF* p_end,
2035
                         OFF* p_contents_beg, OFF* p_contents_end)
2036
0
{
2037
0
    OFF off = beg;
2038
0
    int parenthesis_level = 0;
2039
2040
0
    while(off < max_end) {
2041
0
        if(CH(off) == _T('\\')  &&  off+1 < max_end  &&  ISPUNCT(off+1)) {
2042
0
            off += 2;
2043
0
            continue;
2044
0
        }
2045
2046
0
        if(ISWHITESPACE(off) || ISCNTRL(off))
2047
0
            break;
2048
2049
        /* Link destination may include balanced pairs of unescaped '(' ')'.
2050
         * Note we limit the maximal nesting level by 32 to protect us from
2051
         * https://github.com/jgm/cmark/issues/214 */
2052
0
        if(CH(off) == _T('(')) {
2053
0
            parenthesis_level++;
2054
0
            if(parenthesis_level > 32)
2055
0
                return FALSE;
2056
0
        } else if(CH(off) == _T(')')) {
2057
0
            if(parenthesis_level == 0)
2058
0
                break;
2059
0
            parenthesis_level--;
2060
0
        }
2061
2062
0
        off++;
2063
0
    }
2064
2065
0
    if(parenthesis_level != 0  ||  off == beg)
2066
0
        return FALSE;
2067
2068
    /* Success. */
2069
0
    *p_contents_beg = beg;
2070
0
    *p_contents_end = off;
2071
0
    *p_end = off;
2072
0
    return TRUE;
2073
0
}
2074
2075
static inline int
2076
md_is_link_destination(MD_CTX* ctx, OFF beg, OFF max_end, OFF* p_end,
2077
                       OFF* p_contents_beg, OFF* p_contents_end)
2078
0
{
2079
0
    if(CH(beg) == _T('<'))
2080
0
        return md_is_link_destination_A(ctx, beg, max_end, p_end, p_contents_beg, p_contents_end);
2081
0
    else
2082
0
        return md_is_link_destination_B(ctx, beg, max_end, p_end, p_contents_beg, p_contents_end);
2083
0
}
2084
2085
static int
2086
md_is_link_title(MD_CTX* ctx, const MD_LINE* lines, MD_SIZE n_lines, OFF beg,
2087
                 OFF* p_end, MD_SIZE* p_beg_line_index, MD_SIZE* p_end_line_index,
2088
                 OFF* p_contents_beg, OFF* p_contents_end)
2089
0
{
2090
0
    OFF off = beg;
2091
0
    CHAR closer_char;
2092
0
    MD_SIZE line_index = 0;
2093
2094
    /* White space with up to one line break. */
2095
0
    while(off < lines[line_index].end  &&  ISWHITESPACE(off))
2096
0
        off++;
2097
0
    if(off >= lines[line_index].end) {
2098
0
        line_index++;
2099
0
        if(line_index >= n_lines)
2100
0
            return FALSE;
2101
0
        off = lines[line_index].beg;
2102
0
    }
2103
0
    if(off == beg)
2104
0
        return FALSE;
2105
2106
0
    *p_beg_line_index = line_index;
2107
2108
    /* First char determines how to detect end of it. */
2109
0
    switch(CH(off)) {
2110
0
        case _T('"'):   closer_char = _T('"'); break;
2111
0
        case _T('\''):  closer_char = _T('\''); break;
2112
0
        case _T('('):   closer_char = _T(')'); break;
2113
0
        default:        return FALSE;
2114
0
    }
2115
0
    off++;
2116
2117
0
    *p_contents_beg = off;
2118
2119
0
    while(line_index < n_lines) {
2120
0
        OFF line_end = lines[line_index].end;
2121
2122
0
        while(off < line_end) {
2123
0
            if(CH(off) == _T('\\')  &&  off+1 < ctx->size  &&  (ISPUNCT(off+1) || ISNEWLINE(off+1))) {
2124
0
                off++;
2125
0
            } else if(CH(off) == closer_char) {
2126
                /* Success. */
2127
0
                *p_contents_end = off;
2128
0
                *p_end = off+1;
2129
0
                *p_end_line_index = line_index;
2130
0
                return TRUE;
2131
0
            } else if(closer_char == _T(')')  &&  CH(off) == _T('(')) {
2132
                /* ()-style title cannot contain (unescaped '(')) */
2133
0
                return FALSE;
2134
0
            }
2135
2136
0
            off++;
2137
0
        }
2138
2139
0
        line_index++;
2140
0
    }
2141
2142
0
    return FALSE;
2143
0
}
2144
2145
/* Returns 0 if it is not a reference definition.
2146
 *
2147
 * Returns N > 0 if it is a reference definition. N then corresponds to the
2148
 * number of lines forming it). In this case the definition is stored for
2149
 * resolving any links referring to it.
2150
 *
2151
 * Returns -1 in case of an error (out of memory).
2152
 */
2153
static int
2154
md_is_link_reference_definition(MD_CTX* ctx, const MD_LINE* lines, MD_SIZE n_lines)
2155
0
{
2156
0
    OFF label_contents_beg;
2157
0
    OFF label_contents_end;
2158
0
    MD_SIZE label_contents_line_index;
2159
0
    int label_is_multiline = FALSE;
2160
0
    OFF dest_contents_beg;
2161
0
    OFF dest_contents_end;
2162
0
    OFF title_contents_beg;
2163
0
    OFF title_contents_end;
2164
0
    MD_SIZE title_contents_line_index;
2165
0
    int title_is_multiline = FALSE;
2166
0
    OFF off;
2167
0
    MD_SIZE line_index = 0;
2168
0
    MD_SIZE tmp_line_index;
2169
0
    MD_REF_DEF* def = NULL;
2170
0
    int ret = 0;
2171
2172
    /* Link label. */
2173
0
    if(!md_is_link_label(ctx, lines, n_lines, lines[0].beg,
2174
0
                &off, &label_contents_line_index, &line_index,
2175
0
                &label_contents_beg, &label_contents_end))
2176
0
        return FALSE;
2177
0
    label_is_multiline = (label_contents_line_index != line_index);
2178
2179
    /* Colon. */
2180
0
    if(off >= lines[line_index].end  ||  CH(off) != _T(':'))
2181
0
        return FALSE;
2182
0
    off++;
2183
2184
    /* Optional white space with up to one line break. */
2185
0
    while(off < lines[line_index].end  &&  ISWHITESPACE(off))
2186
0
        off++;
2187
0
    if(off >= lines[line_index].end) {
2188
0
        line_index++;
2189
0
        if(line_index >= n_lines)
2190
0
            return FALSE;
2191
0
        off = lines[line_index].beg;
2192
0
    }
2193
2194
    /* Link destination. */
2195
0
    if(!md_is_link_destination(ctx, off, lines[line_index].end,
2196
0
                &off, &dest_contents_beg, &dest_contents_end))
2197
0
        return FALSE;
2198
2199
    /* (Optional) title. Note we interpret it as an title only if nothing
2200
     * more follows on its last line. */
2201
0
    if(md_is_link_title(ctx, lines + line_index, n_lines - line_index, off,
2202
0
                &off, &title_contents_line_index, &tmp_line_index,
2203
0
                &title_contents_beg, &title_contents_end)
2204
0
        &&  off >= lines[line_index + tmp_line_index].end)
2205
0
    {
2206
0
        title_is_multiline = (tmp_line_index != title_contents_line_index);
2207
0
        title_contents_line_index += line_index;
2208
0
        line_index += tmp_line_index;
2209
0
    } else {
2210
        /* Not a title. */
2211
0
        title_is_multiline = FALSE;
2212
0
        title_contents_beg = off;
2213
0
        title_contents_end = off;
2214
0
        title_contents_line_index = 0;
2215
0
    }
2216
2217
    /* Nothing more can follow on the last line. */
2218
0
    if(off < lines[line_index].end)
2219
0
        return FALSE;
2220
2221
    /* So, it _is_ a reference definition. Remember it. */
2222
0
    if(ctx->n_ref_defs >= ctx->alloc_ref_defs) {
2223
0
        MD_REF_DEF* new_defs;
2224
2225
0
        ctx->alloc_ref_defs = (ctx->alloc_ref_defs > 0
2226
0
                ? ctx->alloc_ref_defs + ctx->alloc_ref_defs / 2
2227
0
                : 16);
2228
0
        new_defs = (MD_REF_DEF*) realloc(ctx->ref_defs, ctx->alloc_ref_defs * sizeof(MD_REF_DEF));
2229
0
        if(new_defs == NULL) {
2230
0
            MD_LOG("realloc() failed.");
2231
0
            goto abort;
2232
0
        }
2233
2234
0
        ctx->ref_defs = new_defs;
2235
0
    }
2236
0
    def = &ctx->ref_defs[ctx->n_ref_defs];
2237
0
    memset(def, 0, sizeof(MD_REF_DEF));
2238
2239
0
    if(label_is_multiline) {
2240
0
        MD_CHECK(md_merge_lines_alloc(ctx, label_contents_beg, label_contents_end,
2241
0
                    lines + label_contents_line_index, n_lines - label_contents_line_index,
2242
0
                    _T(' '), &def->label, &def->label_size));
2243
0
        def->label_needs_free = TRUE;
2244
0
    } else {
2245
0
        def->label = (CHAR*) STR(label_contents_beg);
2246
0
        def->label_size = label_contents_end - label_contents_beg;
2247
0
    }
2248
2249
0
    if(title_is_multiline) {
2250
0
        MD_CHECK(md_merge_lines_alloc(ctx, title_contents_beg, title_contents_end,
2251
0
                    lines + title_contents_line_index, n_lines - title_contents_line_index,
2252
0
                    _T('\n'), &def->title, &def->title_size));
2253
0
        def->title_needs_free = TRUE;
2254
0
    } else {
2255
0
        def->title = (CHAR*) STR(title_contents_beg);
2256
0
        def->title_size = title_contents_end - title_contents_beg;
2257
0
    }
2258
2259
0
    def->dest_beg = dest_contents_beg;
2260
0
    def->dest_end = dest_contents_end;
2261
2262
    /* Success. */
2263
0
    ctx->n_ref_defs++;
2264
0
    return line_index + 1;
2265
2266
0
abort:
2267
    /* Failure. */
2268
0
    if(def != NULL  &&  def->label_needs_free)
2269
0
        free(def->label);
2270
0
    if(def != NULL  &&  def->title_needs_free)
2271
0
        free(def->title);
2272
0
    return ret;
2273
0
}
2274
2275
static int
2276
md_is_link_reference(MD_CTX* ctx, const MD_LINE* lines, MD_SIZE n_lines,
2277
                     OFF beg, OFF end, MD_LINK_ATTR* attr)
2278
0
{
2279
0
    const MD_REF_DEF* def;
2280
0
    const MD_LINE* beg_line;
2281
0
    int is_multiline;
2282
0
    CHAR* label;
2283
0
    SZ label_size;
2284
0
    int ret;
2285
2286
0
    MD_ASSERT(CH(beg) == _T('[') || CH(beg) == _T('!'));
2287
0
    MD_ASSERT(CH(end-1) == _T(']'));
2288
2289
0
    beg += (CH(beg) == _T('!') ? 2 : 1);
2290
0
    end--;
2291
2292
    /* Find lines corresponding to the beg and end positions. */
2293
0
    beg_line = md_lookup_line(beg, lines, n_lines, NULL);
2294
0
    is_multiline = (end > beg_line->end);
2295
2296
0
    if(is_multiline) {
2297
0
        MD_CHECK(md_merge_lines_alloc(ctx, beg, end, beg_line,
2298
0
                 (int)(n_lines - (beg_line - lines)), _T(' '), &label, &label_size));
2299
0
    } else {
2300
0
        label = (CHAR*) STR(beg);
2301
0
        label_size = end - beg;
2302
0
    }
2303
2304
0
    def = md_lookup_ref_def(ctx, label, label_size);
2305
0
    if(def != NULL) {
2306
0
        attr->dest_beg = def->dest_beg;
2307
0
        attr->dest_end = def->dest_end;
2308
0
        attr->title = def->title;
2309
0
        attr->title_size = def->title_size;
2310
0
        attr->title_needs_free = FALSE;
2311
0
    }
2312
2313
0
    if(is_multiline)
2314
0
        free(label);
2315
2316
0
    ret = (def != NULL);
2317
2318
0
abort:
2319
0
    return ret;
2320
0
}
2321
2322
static int
2323
md_is_inline_link_spec(MD_CTX* ctx, const MD_LINE* lines, MD_SIZE n_lines,
2324
                       OFF beg, OFF* p_end, MD_LINK_ATTR* attr)
2325
0
{
2326
0
    MD_SIZE line_index = 0;
2327
0
    MD_SIZE tmp_line_index;
2328
0
    OFF title_contents_beg;
2329
0
    OFF title_contents_end;
2330
0
    MD_SIZE title_contents_line_index;
2331
0
    int title_is_multiline;
2332
0
    OFF off = beg;
2333
0
    int ret = FALSE;
2334
2335
0
    while(off >= lines[line_index].end)
2336
0
        line_index++;
2337
2338
0
    MD_ASSERT(CH(off) == _T('('));
2339
0
    off++;
2340
2341
    /* Optional white space with up to one line break. */
2342
0
    while(off < lines[line_index].end  &&  ISWHITESPACE(off))
2343
0
        off++;
2344
0
    if(off >= lines[line_index].end  &&  (off >= ctx->size  ||  ISNEWLINE(off))) {
2345
0
        line_index++;
2346
0
        if(line_index >= n_lines)
2347
0
            return FALSE;
2348
0
        off = lines[line_index].beg;
2349
0
    }
2350
2351
    /* Link destination may be omitted, but only when not also having a title. */
2352
0
    if(off < ctx->size  &&  CH(off) == _T(')')) {
2353
0
        attr->dest_beg = off;
2354
0
        attr->dest_end = off;
2355
0
        attr->title = NULL;
2356
0
        attr->title_size = 0;
2357
0
        attr->title_needs_free = FALSE;
2358
0
        off++;
2359
0
        *p_end = off;
2360
0
        return TRUE;
2361
0
    }
2362
2363
    /* Link destination. */
2364
0
    if(!md_is_link_destination(ctx, off, lines[line_index].end,
2365
0
                        &off, &attr->dest_beg, &attr->dest_end))
2366
0
        return FALSE;
2367
2368
    /* (Optional) title. */
2369
0
    if(md_is_link_title(ctx, lines + line_index, n_lines - line_index, off,
2370
0
                &off, &title_contents_line_index, &tmp_line_index,
2371
0
                &title_contents_beg, &title_contents_end))
2372
0
    {
2373
0
        title_is_multiline = (tmp_line_index != title_contents_line_index);
2374
0
        title_contents_line_index += line_index;
2375
0
        line_index += tmp_line_index;
2376
0
    } else {
2377
        /* Not a title. */
2378
0
        title_is_multiline = FALSE;
2379
0
        title_contents_beg = off;
2380
0
        title_contents_end = off;
2381
0
        title_contents_line_index = 0;
2382
0
    }
2383
2384
    /* Optional whitespace followed with final ')'. */
2385
0
    while(off < lines[line_index].end  &&  ISWHITESPACE(off))
2386
0
        off++;
2387
0
    if(off >= lines[line_index].end) {
2388
0
        line_index++;
2389
0
        if(line_index >= n_lines)
2390
0
            return FALSE;
2391
0
        off = lines[line_index].beg;
2392
0
    }
2393
0
    if(CH(off) != _T(')'))
2394
0
        goto abort;
2395
0
    off++;
2396
2397
0
    if(title_contents_beg >= title_contents_end) {
2398
0
        attr->title = NULL;
2399
0
        attr->title_size = 0;
2400
0
        attr->title_needs_free = FALSE;
2401
0
    } else if(!title_is_multiline) {
2402
0
        attr->title = (CHAR*) STR(title_contents_beg);
2403
0
        attr->title_size = title_contents_end - title_contents_beg;
2404
0
        attr->title_needs_free = FALSE;
2405
0
    } else {
2406
0
        MD_CHECK(md_merge_lines_alloc(ctx, title_contents_beg, title_contents_end,
2407
0
                    lines + title_contents_line_index, n_lines - title_contents_line_index,
2408
0
                    _T('\n'), &attr->title, &attr->title_size));
2409
0
        attr->title_needs_free = TRUE;
2410
0
    }
2411
2412
0
    *p_end = off;
2413
0
    ret = TRUE;
2414
2415
0
abort:
2416
0
    return ret;
2417
0
}
2418
2419
static void
2420
md_free_ref_defs(MD_CTX* ctx)
2421
0
{
2422
0
    int i;
2423
2424
0
    for(i = 0; i < ctx->n_ref_defs; i++) {
2425
0
        MD_REF_DEF* def = &ctx->ref_defs[i];
2426
2427
0
        if(def->label_needs_free)
2428
0
            free(def->label);
2429
0
        if(def->title_needs_free)
2430
0
            free(def->title);
2431
0
    }
2432
2433
0
    free(ctx->ref_defs);
2434
0
}
2435
2436
2437
/******************************************
2438
 ***  Processing Inlines (a.k.a Spans)  ***
2439
 ******************************************/
2440
2441
/* We process inlines in few phases:
2442
 *
2443
 * (1) We go through the block text and collect all significant characters
2444
 *     which may start/end a span or some other significant position into
2445
 *     ctx->marks[]. Core of this is what md_collect_marks() does.
2446
 *
2447
 *     We also do some very brief preliminary context-less analysis, whether
2448
 *     it might be opener or closer (e.g. of an emphasis span).
2449
 *
2450
 *     This speeds the other steps as we do not need to re-iterate over all
2451
 *     characters anymore.
2452
 *
2453
 * (2) We analyze each potential mark types, in order by their precedence.
2454
 *
2455
 *     In each md_analyze_XXX() function, we re-iterate list of the marks,
2456
 *     skipping already resolved regions (in preceding precedences) and try to
2457
 *     resolve them.
2458
 *
2459
 * (2.1) For trivial marks, which are single (e.g. HTML entity), we just mark
2460
 *       them as resolved.
2461
 *
2462
 * (2.2) For range-type marks, we analyze whether the mark could be closer
2463
 *       and, if yes, whether there is some preceding opener it could satisfy.
2464
 *
2465
 *       If not we check whether it could be really an opener and if yes, we
2466
 *       remember it so subsequent closers may resolve it.
2467
 *
2468
 * (3) Finally, when all marks were analyzed, we render the block contents
2469
 *     by calling MD_RENDERER::text() callback, interrupting by ::enter_span()
2470
 *     or ::close_span() whenever we reach a resolved mark.
2471
 */
2472
2473
2474
/* The mark structure.
2475
 *
2476
 * '\\': Maybe escape sequence.
2477
 * '\0': NULL char.
2478
 *  '*': Maybe (strong) emphasis start/end.
2479
 *  '_': Maybe (strong) emphasis start/end.
2480
 *  '~': Maybe strikethrough start/end (needs MD_FLAG_STRIKETHROUGH).
2481
 *  '`': Maybe code span start/end.
2482
 *  '&': Maybe start of entity.
2483
 *  ';': Maybe end of entity.
2484
 *  '<': Maybe start of raw HTML or autolink.
2485
 *  '>': Maybe end of raw HTML or autolink.
2486
 *  '[': Maybe start of link label or link text.
2487
 *  '!': Equivalent of '[' for image.
2488
 *  ']': Maybe end of link label or link text.
2489
 *  '@': Maybe permissive e-mail auto-link (needs MD_FLAG_PERMISSIVEEMAILAUTOLINKS).
2490
 *  ':': Maybe permissive URL auto-link (needs MD_FLAG_PERMISSIVEURLAUTOLINKS).
2491
 *  '.': Maybe permissive WWW auto-link (needs MD_FLAG_PERMISSIVEWWWAUTOLINKS).
2492
 *  'D': Dummy mark, it reserves a space for splitting a previous mark
2493
 *       (e.g. emphasis) or to make more space for storing some special data
2494
 *       related to the preceding mark (e.g. link).
2495
 *
2496
 * Note that not all instances of these chars in the text imply creation of the
2497
 * structure. Only those which have (or may have, after we see more context)
2498
 * the special meaning.
2499
 *
2500
 * (Keep this struct as small as possible to fit as much of them into CPU
2501
 * cache line.)
2502
 */
2503
struct MD_MARK_tag {
2504
    OFF beg;
2505
    OFF end;
2506
2507
    /* For unresolved openers, 'next' may be used to form a stack of
2508
     * unresolved open openers.
2509
     *
2510
     * When resolved with MD_MARK_OPENER/CLOSER flag, next/prev is index of the
2511
     * respective closer/opener.
2512
     */
2513
    int prev;
2514
    int next;
2515
    CHAR ch;
2516
    unsigned char flags;
2517
};
2518
2519
/* Mark flags (these apply to ALL mark types). */
2520
0
#define MD_MARK_POTENTIAL_OPENER            0x01  /* Maybe opener. */
2521
0
#define MD_MARK_POTENTIAL_CLOSER            0x02  /* Maybe closer. */
2522
0
#define MD_MARK_OPENER                      0x04  /* Definitely opener. */
2523
0
#define MD_MARK_CLOSER                      0x08  /* Definitely closer. */
2524
0
#define MD_MARK_RESOLVED                    0x10  /* Resolved in any definite way. */
2525
2526
/* Mark flags specific for various mark types (so they can share bits). */
2527
0
#define MD_MARK_EMPH_OC                     0x20  /* Opener/closer mixed candidate. Helper for the "rule of 3". */
2528
0
#define MD_MARK_EMPH_MOD3_0                 0x40
2529
0
#define MD_MARK_EMPH_MOD3_1                 0x80
2530
0
#define MD_MARK_EMPH_MOD3_2                 (0x40 | 0x80)
2531
0
#define MD_MARK_EMPH_MOD3_MASK              (0x40 | 0x80)
2532
0
#define MD_MARK_AUTOLINK                    0x20  /* Distinguisher for '<', '>'. */
2533
0
#define MD_MARK_AUTOLINK_MISSING_MAILTO     0x40
2534
0
#define MD_MARK_VALIDPERMISSIVEAUTOLINK     0x20  /* For permissive autolinks. */
2535
0
#define MD_MARK_HASNESTEDBRACKETS           0x20  /* For '[' to rule out invalid link labels early */
2536
2537
static MD_MARKSTACK*
2538
md_emph_stack(MD_CTX* ctx, MD_CHAR ch, unsigned flags)
2539
0
{
2540
0
    MD_MARKSTACK* stack;
2541
2542
0
    switch(ch) {
2543
0
        case '*':   stack = &ASTERISK_OPENERS_oo_mod3_0; break;
2544
0
        case '_':   stack = &UNDERSCORE_OPENERS_oo_mod3_0; break;
2545
0
        default:    MD_UNREACHABLE();
2546
0
    }
2547
2548
0
    if(flags & MD_MARK_EMPH_OC)
2549
0
        stack += 3;
2550
2551
0
    switch(flags & MD_MARK_EMPH_MOD3_MASK) {
2552
0
        case MD_MARK_EMPH_MOD3_0:   stack += 0; break;
2553
0
        case MD_MARK_EMPH_MOD3_1:   stack += 1; break;
2554
0
        case MD_MARK_EMPH_MOD3_2:   stack += 2; break;
2555
0
        default:                    MD_UNREACHABLE();
2556
0
    }
2557
2558
0
    return stack;
2559
0
}
2560
2561
static MD_MARKSTACK*
2562
md_opener_stack(MD_CTX* ctx, int mark_index)
2563
0
{
2564
0
    MD_MARK* mark = &ctx->marks[mark_index];
2565
2566
0
    switch(mark->ch) {
2567
0
        case _T('*'):
2568
0
        case _T('_'):   return md_emph_stack(ctx, mark->ch, mark->flags);
2569
2570
0
        case _T('~'):   return (mark->end - mark->beg == 1) ? &TILDE_OPENERS_1 : &TILDE_OPENERS_2;
2571
2572
0
        case _T('!'):
2573
0
        case _T('['):   return &BRACKET_OPENERS;
2574
2575
0
        default:        MD_UNREACHABLE();
2576
0
    }
2577
0
}
2578
2579
static MD_MARK*
2580
md_add_mark(MD_CTX* ctx)
2581
0
{
2582
0
    if(ctx->n_marks >= ctx->alloc_marks) {
2583
0
        MD_MARK* new_marks;
2584
2585
0
        ctx->alloc_marks = (ctx->alloc_marks > 0
2586
0
                ? ctx->alloc_marks + ctx->alloc_marks / 2
2587
0
                : 64);
2588
0
        new_marks = realloc(ctx->marks, ctx->alloc_marks * sizeof(MD_MARK));
2589
0
        if(new_marks == NULL) {
2590
0
            MD_LOG("realloc() failed.");
2591
0
            return NULL;
2592
0
        }
2593
2594
0
        ctx->marks = new_marks;
2595
0
    }
2596
2597
0
    return &ctx->marks[ctx->n_marks++];
2598
0
}
2599
2600
#define ADD_MARK_()                                                     \
2601
0
        do {                                                            \
2602
0
            mark = md_add_mark(ctx);                                    \
2603
0
            if(mark == NULL) {                                          \
2604
0
                ret = -1;                                               \
2605
0
                goto abort;                                             \
2606
0
            }                                                           \
2607
0
        } while(0)
2608
2609
#define ADD_MARK(ch_, beg_, end_, flags_)                               \
2610
0
        do {                                                            \
2611
0
            ADD_MARK_();                                                \
2612
0
            mark->beg = (beg_);                                         \
2613
0
            mark->end = (end_);                                         \
2614
0
            mark->prev = -1;                                            \
2615
0
            mark->next = -1;                                            \
2616
0
            mark->ch = (char)(ch_);                                     \
2617
0
            mark->flags = (flags_);                                     \
2618
0
        } while(0)
2619
2620
2621
static inline void
2622
md_mark_stack_push(MD_CTX* ctx, MD_MARKSTACK* stack, int mark_index)
2623
0
{
2624
0
    ctx->marks[mark_index].next = stack->top;
2625
0
    stack->top = mark_index;
2626
0
}
2627
2628
static inline int
2629
md_mark_stack_pop(MD_CTX* ctx, MD_MARKSTACK* stack)
2630
0
{
2631
0
    int top = stack->top;
2632
0
    if(top >= 0)
2633
0
        stack->top = ctx->marks[top].next;
2634
0
    return top;
2635
0
}
2636
2637
/* Sometimes, we need to store a pointer into the mark. It is quite rare
2638
 * so we do not bother to make MD_MARK use union, and it can only happen
2639
 * for dummy marks. */
2640
static inline void
2641
md_mark_store_ptr(MD_CTX* ctx, int mark_index, void* ptr)
2642
0
{
2643
0
    MD_MARK* mark = &ctx->marks[mark_index];
2644
0
    MD_ASSERT(mark->ch == 'D');
2645
2646
    /* Check only members beg and end are misused for this. */
2647
0
    MD_ASSERT(sizeof(void*) <= 2 * sizeof(OFF));
2648
0
    memcpy(mark, &ptr, sizeof(void*));
2649
0
}
2650
2651
static inline void*
2652
md_mark_get_ptr(MD_CTX* ctx, int mark_index)
2653
0
{
2654
0
    void* ptr;
2655
0
    MD_MARK* mark = &ctx->marks[mark_index];
2656
0
    MD_ASSERT(mark->ch == 'D');
2657
0
    memcpy(&ptr, mark, sizeof(void*));
2658
0
    return ptr;
2659
0
}
2660
2661
static inline void
2662
md_resolve_range(MD_CTX* ctx, int opener_index, int closer_index)
2663
0
{
2664
0
    MD_MARK* opener = &ctx->marks[opener_index];
2665
0
    MD_MARK* closer = &ctx->marks[closer_index];
2666
2667
    /* Interconnect opener and closer and mark both as resolved. */
2668
0
    opener->next = closer_index;
2669
0
    closer->prev = opener_index;
2670
2671
0
    opener->flags |= MD_MARK_OPENER | MD_MARK_RESOLVED;
2672
0
    closer->flags |= MD_MARK_CLOSER | MD_MARK_RESOLVED;
2673
0
}
2674
2675
2676
0
#define MD_ROLLBACK_CROSSING    0
2677
0
#define MD_ROLLBACK_ALL         1
2678
2679
/* In the range ctx->marks[opener_index] ... [closer_index], undo some or all
2680
 * resolvings accordingly to these rules:
2681
 *
2682
 * (1) All stacks of openers are cut so that any pending potential openers
2683
 *     are discarded from future consideration.
2684
 *
2685
 * (2) If 'how' is MD_ROLLBACK_ALL, then ALL resolved marks inside the range
2686
 *     are thrown away and turned into dummy marks ('D').
2687
 *
2688
 * WARNING: Do not call for arbitrary range of opener and closer.
2689
 * This must form (potentially) valid range not crossing nesting boundaries
2690
 * of already resolved ranges.
2691
 */
2692
static void
2693
md_rollback(MD_CTX* ctx, int opener_index, int closer_index, int how)
2694
0
{
2695
0
    int i;
2696
2697
0
    for(i = 0; i < (int) SIZEOF_ARRAY(ctx->opener_stacks); i++) {
2698
0
        MD_MARKSTACK* stack = &ctx->opener_stacks[i];
2699
0
        while(stack->top >= opener_index)
2700
0
            md_mark_stack_pop(ctx, stack);
2701
0
    }
2702
2703
0
    if(how == MD_ROLLBACK_ALL) {
2704
0
        for(i = opener_index + 1; i < closer_index; i++) {
2705
0
            ctx->marks[i].ch = 'D';
2706
0
            ctx->marks[i].flags = 0;
2707
0
        }
2708
0
    }
2709
0
}
2710
2711
static void
2712
md_build_mark_char_map(MD_CTX* ctx)
2713
0
{
2714
0
    memset(ctx->mark_char_map, 0, sizeof(ctx->mark_char_map));
2715
2716
0
    ctx->mark_char_map['\\'] = 1;
2717
0
    ctx->mark_char_map['*'] = 1;
2718
0
    ctx->mark_char_map['_'] = 1;
2719
0
    ctx->mark_char_map['`'] = 1;
2720
0
    ctx->mark_char_map['&'] = 1;
2721
0
    ctx->mark_char_map[';'] = 1;
2722
0
    ctx->mark_char_map['<'] = 1;
2723
0
    ctx->mark_char_map['>'] = 1;
2724
0
    ctx->mark_char_map['['] = 1;
2725
0
    ctx->mark_char_map['!'] = 1;
2726
0
    ctx->mark_char_map[']'] = 1;
2727
0
    ctx->mark_char_map['\0'] = 1;
2728
2729
0
    if(ctx->parser.flags & MD_FLAG_STRIKETHROUGH)
2730
0
        ctx->mark_char_map['~'] = 1;
2731
2732
0
    if(ctx->parser.flags & MD_FLAG_LATEXMATHSPANS)
2733
0
        ctx->mark_char_map['$'] = 1;
2734
2735
0
    if(ctx->parser.flags & MD_FLAG_PERMISSIVEEMAILAUTOLINKS)
2736
0
        ctx->mark_char_map['@'] = 1;
2737
2738
0
    if(ctx->parser.flags & MD_FLAG_PERMISSIVEURLAUTOLINKS)
2739
0
        ctx->mark_char_map[':'] = 1;
2740
2741
0
    if(ctx->parser.flags & MD_FLAG_PERMISSIVEWWWAUTOLINKS)
2742
0
        ctx->mark_char_map['.'] = 1;
2743
2744
0
    if((ctx->parser.flags & MD_FLAG_TABLES) || (ctx->parser.flags & MD_FLAG_WIKILINKS))
2745
0
        ctx->mark_char_map['|'] = 1;
2746
2747
0
    if(ctx->parser.flags & MD_FLAG_COLLAPSEWHITESPACE) {
2748
0
        int i;
2749
2750
0
        for(i = 0; i < (int) sizeof(ctx->mark_char_map); i++) {
2751
0
            if(ISWHITESPACE_(i))
2752
0
                ctx->mark_char_map[i] = 1;
2753
0
        }
2754
0
    }
2755
0
}
2756
2757
static int
2758
md_is_code_span(MD_CTX* ctx, const MD_LINE* lines, MD_SIZE n_lines, OFF beg,
2759
                MD_MARK* opener, MD_MARK* closer,
2760
                OFF last_potential_closers[CODESPAN_MARK_MAXLEN],
2761
                int* p_reached_paragraph_end)
2762
0
{
2763
0
    OFF opener_beg = beg;
2764
0
    OFF opener_end;
2765
0
    OFF closer_beg;
2766
0
    OFF closer_end;
2767
0
    SZ mark_len;
2768
0
    OFF line_end;
2769
0
    int has_space_after_opener = FALSE;
2770
0
    int has_eol_after_opener = FALSE;
2771
0
    int has_space_before_closer = FALSE;
2772
0
    int has_eol_before_closer = FALSE;
2773
0
    int has_only_space = TRUE;
2774
0
    MD_SIZE line_index = 0;
2775
2776
0
    line_end = lines[0].end;
2777
0
    opener_end = opener_beg;
2778
0
    while(opener_end < line_end  &&  CH(opener_end) == _T('`'))
2779
0
        opener_end++;
2780
0
    has_space_after_opener = (opener_end < line_end && CH(opener_end) == _T(' '));
2781
0
    has_eol_after_opener = (opener_end == line_end);
2782
2783
    /* The caller needs to know end of the opening mark even if we fail. */
2784
0
    opener->end = opener_end;
2785
2786
0
    mark_len = opener_end - opener_beg;
2787
0
    if(mark_len > CODESPAN_MARK_MAXLEN)
2788
0
        return FALSE;
2789
2790
    /* Check whether we already know there is no closer of this length.
2791
     * If so, re-scan does no sense. This fixes issue #59. */
2792
0
    if(last_potential_closers[mark_len-1] >= lines[n_lines-1].end  ||
2793
0
       (*p_reached_paragraph_end  &&  last_potential_closers[mark_len-1] < opener_end))
2794
0
        return FALSE;
2795
2796
0
    closer_beg = opener_end;
2797
0
    closer_end = opener_end;
2798
2799
    /* Find closer mark. */
2800
0
    while(TRUE) {
2801
0
        while(closer_beg < line_end  &&  CH(closer_beg) != _T('`')) {
2802
0
            if(CH(closer_beg) != _T(' '))
2803
0
                has_only_space = FALSE;
2804
0
            closer_beg++;
2805
0
        }
2806
0
        closer_end = closer_beg;
2807
0
        while(closer_end < line_end  &&  CH(closer_end) == _T('`'))
2808
0
            closer_end++;
2809
2810
0
        if(closer_end - closer_beg == mark_len) {
2811
            /* Success. */
2812
0
            has_space_before_closer = (closer_beg > lines[line_index].beg && CH(closer_beg-1) == _T(' '));
2813
0
            has_eol_before_closer = (closer_beg == lines[line_index].beg);
2814
0
            break;
2815
0
        }
2816
2817
0
        if(closer_end - closer_beg > 0) {
2818
            /* We have found a back-tick which is not part of the closer. */
2819
0
            has_only_space = FALSE;
2820
2821
            /* But if we eventually fail, remember it as a potential closer
2822
             * of its own length for future attempts. This mitigates needs for
2823
             * rescans. */
2824
0
            if(closer_end - closer_beg < CODESPAN_MARK_MAXLEN) {
2825
0
                if(closer_beg > last_potential_closers[closer_end - closer_beg - 1])
2826
0
                    last_potential_closers[closer_end - closer_beg - 1] = closer_beg;
2827
0
            }
2828
0
        }
2829
2830
0
        if(closer_end >= line_end) {
2831
0
            line_index++;
2832
0
            if(line_index >= n_lines) {
2833
                /* Reached end of the paragraph and still nothing. */
2834
0
                *p_reached_paragraph_end = TRUE;
2835
0
                return FALSE;
2836
0
            }
2837
            /* Try on the next line. */
2838
0
            line_end = lines[line_index].end;
2839
0
            closer_beg = lines[line_index].beg;
2840
0
        } else {
2841
0
            closer_beg = closer_end;
2842
0
        }
2843
0
    }
2844
2845
    /* If there is a space or a new line both after and before the opener
2846
     * (and if the code span is not made of spaces only), consume one initial
2847
     * and one trailing space as part of the marks. */
2848
0
    if(!has_only_space  &&
2849
0
       (has_space_after_opener || has_eol_after_opener)  &&
2850
0
       (has_space_before_closer || has_eol_before_closer))
2851
0
    {
2852
0
        if(has_space_after_opener)
2853
0
            opener_end++;
2854
0
        else
2855
0
            opener_end = lines[1].beg;
2856
2857
0
        if(has_space_before_closer)
2858
0
            closer_beg--;
2859
0
        else {
2860
            /* Go back to the end of prev line */
2861
0
            closer_beg = lines[line_index-1].end;
2862
            /* But restore any trailing whitespace */
2863
0
            while(closer_beg < ctx->size  &&  ISBLANK(closer_beg))
2864
0
                closer_beg++;
2865
0
        }
2866
0
    }
2867
2868
0
    opener->ch = _T('`');
2869
0
    opener->beg = opener_beg;
2870
0
    opener->end = opener_end;
2871
0
    opener->flags = MD_MARK_POTENTIAL_OPENER;
2872
0
    closer->ch = _T('`');
2873
0
    closer->beg = closer_beg;
2874
0
    closer->end = closer_end;
2875
0
    closer->flags = MD_MARK_POTENTIAL_CLOSER;
2876
0
    return TRUE;
2877
0
}
2878
2879
static int
2880
md_is_autolink_uri(MD_CTX* ctx, OFF beg, OFF max_end, OFF* p_end)
2881
0
{
2882
0
    OFF off = beg+1;
2883
2884
0
    MD_ASSERT(CH(beg) == _T('<'));
2885
2886
    /* Check for scheme. */
2887
0
    if(off >= max_end  ||  !ISASCII(off))
2888
0
        return FALSE;
2889
0
    off++;
2890
0
    while(1) {
2891
0
        if(off >= max_end)
2892
0
            return FALSE;
2893
0
        if(off - beg > 32)
2894
0
            return FALSE;
2895
0
        if(CH(off) == _T(':')  &&  off - beg >= 3)
2896
0
            break;
2897
0
        if(!ISALNUM(off) && CH(off) != _T('+') && CH(off) != _T('-') && CH(off) != _T('.'))
2898
0
            return FALSE;
2899
0
        off++;
2900
0
    }
2901
2902
    /* Check the path after the scheme. */
2903
0
    while(off < max_end  &&  CH(off) != _T('>')) {
2904
0
        if(ISWHITESPACE(off) || ISCNTRL(off) || CH(off) == _T('<'))
2905
0
            return FALSE;
2906
0
        off++;
2907
0
    }
2908
2909
0
    if(off >= max_end)
2910
0
        return FALSE;
2911
2912
0
    MD_ASSERT(CH(off) == _T('>'));
2913
0
    *p_end = off+1;
2914
0
    return TRUE;
2915
0
}
2916
2917
static int
2918
md_is_autolink_email(MD_CTX* ctx, OFF beg, OFF max_end, OFF* p_end)
2919
0
{
2920
0
    OFF off = beg + 1;
2921
0
    int label_len;
2922
2923
0
    MD_ASSERT(CH(beg) == _T('<'));
2924
2925
    /* The code should correspond to this regexp:
2926
            /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+
2927
            @[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?
2928
            (?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/
2929
     */
2930
2931
    /* Username (before '@'). */
2932
0
    while(off < max_end  &&  (ISALNUM(off) || ISANYOF(off, _T(".!#$%&'*+/=?^_`{|}~-"))))
2933
0
        off++;
2934
0
    if(off <= beg+1)
2935
0
        return FALSE;
2936
2937
    /* '@' */
2938
0
    if(off >= max_end  ||  CH(off) != _T('@'))
2939
0
        return FALSE;
2940
0
    off++;
2941
2942
    /* Labels delimited with '.'; each label is sequence of 1 - 63 alnum
2943
     * characters or '-', but '-' is not allowed as first or last char. */
2944
0
    label_len = 0;
2945
0
    while(off < max_end) {
2946
0
        if(ISALNUM(off))
2947
0
            label_len++;
2948
0
        else if(CH(off) == _T('-')  &&  label_len > 0)
2949
0
            label_len++;
2950
0
        else if(CH(off) == _T('.')  &&  label_len > 0  &&  CH(off-1) != _T('-'))
2951
0
            label_len = 0;
2952
0
        else
2953
0
            break;
2954
2955
0
        if(label_len > 63)
2956
0
            return FALSE;
2957
2958
0
        off++;
2959
0
    }
2960
2961
0
    if(label_len <= 0  || off >= max_end  ||  CH(off) != _T('>') ||  CH(off-1) == _T('-'))
2962
0
        return FALSE;
2963
2964
0
    *p_end = off+1;
2965
0
    return TRUE;
2966
0
}
2967
2968
static int
2969
md_is_autolink(MD_CTX* ctx, OFF beg, OFF max_end, OFF* p_end, int* p_missing_mailto)
2970
0
{
2971
0
    if(md_is_autolink_uri(ctx, beg, max_end, p_end)) {
2972
0
        *p_missing_mailto = FALSE;
2973
0
        return TRUE;
2974
0
    }
2975
2976
0
    if(md_is_autolink_email(ctx, beg, max_end, p_end)) {
2977
0
        *p_missing_mailto = TRUE;
2978
0
        return TRUE;
2979
0
    }
2980
2981
0
    return FALSE;
2982
0
}
2983
2984
static int
2985
md_collect_marks(MD_CTX* ctx, const MD_LINE* lines, MD_SIZE n_lines, int table_mode)
2986
0
{
2987
0
    MD_SIZE line_index;
2988
0
    int ret = 0;
2989
0
    MD_MARK* mark;
2990
0
    OFF codespan_last_potential_closers[CODESPAN_MARK_MAXLEN] = { 0 };
2991
0
    int codespan_scanned_till_paragraph_end = FALSE;
2992
2993
0
    for(line_index = 0; line_index < n_lines; line_index++) {
2994
0
        const MD_LINE* line = &lines[line_index];
2995
0
        OFF off = line->beg;
2996
2997
0
        while(TRUE) {
2998
0
            CHAR ch;
2999
3000
#ifdef MD4C_USE_UTF16
3001
    /* For UTF-16, mark_char_map[] covers only ASCII. */
3002
    #define IS_MARK_CHAR(off)   ((CH(off) < SIZEOF_ARRAY(ctx->mark_char_map))  &&  \
3003
                                (ctx->mark_char_map[(unsigned char) CH(off)]))
3004
#else
3005
    /* For 8-bit encodings, mark_char_map[] covers all 256 elements. */
3006
0
    #define IS_MARK_CHAR(off)   (ctx->mark_char_map[(unsigned char) CH(off)])
3007
0
#endif
3008
3009
            /* Optimization: Use some loop unrolling. */
3010
0
            while(off + 3 < line->end  &&  !IS_MARK_CHAR(off+0)  &&  !IS_MARK_CHAR(off+1)
3011
0
                                       &&  !IS_MARK_CHAR(off+2)  &&  !IS_MARK_CHAR(off+3))
3012
0
                off += 4;
3013
0
            while(off < line->end  &&  !IS_MARK_CHAR(off+0))
3014
0
                off++;
3015
3016
0
            if(off >= line->end)
3017
0
                break;
3018
3019
0
            ch = CH(off);
3020
3021
            /* A backslash escape.
3022
             * It can go beyond line->end as it may involve escaped new
3023
             * line to form a hard break. */
3024
0
            if(ch == _T('\\')  &&  off+1 < ctx->size  &&  (ISPUNCT(off+1) || ISNEWLINE(off+1))) {
3025
                /* Hard-break cannot be on the last line of the block. */
3026
0
                if(!ISNEWLINE(off+1)  ||  line_index+1 < n_lines)
3027
0
                    ADD_MARK(ch, off, off+2, MD_MARK_RESOLVED);
3028
0
                off += 2;
3029
0
                continue;
3030
0
            }
3031
3032
            /* A potential (string) emphasis start/end. */
3033
0
            if(ch == _T('*')  ||  ch == _T('_')) {
3034
0
                OFF tmp = off+1;
3035
0
                int left_level;     /* What precedes: 0 = whitespace; 1 = punctuation; 2 = other char. */
3036
0
                int right_level;    /* What follows: 0 = whitespace; 1 = punctuation; 2 = other char. */
3037
3038
0
                while(tmp < line->end  &&  CH(tmp) == ch)
3039
0
                    tmp++;
3040
3041
0
                if(off == line->beg  ||  ISUNICODEWHITESPACEBEFORE(off))
3042
0
                    left_level = 0;
3043
0
                else if(ISUNICODEPUNCTBEFORE(off))
3044
0
                    left_level = 1;
3045
0
                else
3046
0
                    left_level = 2;
3047
3048
0
                if(tmp == line->end  ||  ISUNICODEWHITESPACE(tmp))
3049
0
                    right_level = 0;
3050
0
                else if(ISUNICODEPUNCT(tmp))
3051
0
                    right_level = 1;
3052
0
                else
3053
0
                    right_level = 2;
3054
3055
                /* Intra-word underscore doesn't have special meaning. */
3056
0
                if(ch == _T('_')  &&  left_level == 2  &&  right_level == 2) {
3057
0
                    left_level = 0;
3058
0
                    right_level = 0;
3059
0
                }
3060
3061
0
                if(left_level != 0  ||  right_level != 0) {
3062
0
                    unsigned flags = 0;
3063
3064
0
                    if(left_level > 0  &&  left_level >= right_level)
3065
0
                        flags |= MD_MARK_POTENTIAL_CLOSER;
3066
0
                    if(right_level > 0  &&  right_level >= left_level)
3067
0
                        flags |= MD_MARK_POTENTIAL_OPENER;
3068
0
                    if(flags == (MD_MARK_POTENTIAL_OPENER | MD_MARK_POTENTIAL_CLOSER))
3069
0
                        flags |= MD_MARK_EMPH_OC;
3070
3071
                    /* For "the rule of three" we need to remember the original
3072
                     * size of the mark (modulo three), before we potentially
3073
                     * split the mark when being later resolved partially by some
3074
                     * shorter closer. */
3075
0
                    switch((tmp - off) % 3) {
3076
0
                        case 0: flags |= MD_MARK_EMPH_MOD3_0; break;
3077
0
                        case 1: flags |= MD_MARK_EMPH_MOD3_1; break;
3078
0
                        case 2: flags |= MD_MARK_EMPH_MOD3_2; break;
3079
0
                    }
3080
3081
0
                    ADD_MARK(ch, off, tmp, flags);
3082
3083
                    /* During resolving, multiple asterisks may have to be
3084
                     * split into independent span start/ends. Consider e.g.
3085
                     * "**foo* bar*". Therefore we push also some empty dummy
3086
                     * marks to have enough space for that. */
3087
0
                    off++;
3088
0
                    while(off < tmp) {
3089
0
                        ADD_MARK('D', off, off, 0);
3090
0
                        off++;
3091
0
                    }
3092
0
                    continue;
3093
0
                }
3094
3095
0
                off = tmp;
3096
0
                continue;
3097
0
            }
3098
3099
            /* A potential code span start/end. */
3100
0
            if(ch == _T('`')) {
3101
0
                MD_MARK opener;
3102
0
                MD_MARK closer;
3103
0
                int is_code_span;
3104
3105
0
                is_code_span = md_is_code_span(ctx, line, n_lines - line_index, off,
3106
0
                            &opener, &closer, codespan_last_potential_closers,
3107
0
                            &codespan_scanned_till_paragraph_end);
3108
0
                if(is_code_span) {
3109
0
                    ADD_MARK(opener.ch, opener.beg, opener.end, opener.flags);
3110
0
                    ADD_MARK(closer.ch, closer.beg, closer.end, closer.flags);
3111
0
                    md_resolve_range(ctx, ctx->n_marks-2, ctx->n_marks-1);
3112
0
                    off = closer.end;
3113
3114
                    /* Advance the current line accordingly. */
3115
0
                    if(off > line->end)
3116
0
                        line = md_lookup_line(off, lines, n_lines, &line_index);
3117
0
                    continue;
3118
0
                }
3119
3120
0
                off = opener.end;
3121
0
                continue;
3122
0
            }
3123
3124
            /* A potential entity start. */
3125
0
            if(ch == _T('&')) {
3126
0
                ADD_MARK(ch, off, off+1, MD_MARK_POTENTIAL_OPENER);
3127
0
                off++;
3128
0
                continue;
3129
0
            }
3130
3131
            /* A potential entity end. */
3132
0
            if(ch == _T(';')) {
3133
                /* We surely cannot be entity unless the previous mark is '&'. */
3134
0
                if(ctx->n_marks > 0  &&  ctx->marks[ctx->n_marks-1].ch == _T('&'))
3135
0
                    ADD_MARK(ch, off, off+1, MD_MARK_POTENTIAL_CLOSER);
3136
3137
0
                off++;
3138
0
                continue;
3139
0
            }
3140
3141
            /* A potential autolink or raw HTML start/end. */
3142
0
            if(ch == _T('<')) {
3143
0
                int is_autolink;
3144
0
                OFF autolink_end;
3145
0
                int missing_mailto;
3146
3147
0
                if(!(ctx->parser.flags & MD_FLAG_NOHTMLSPANS)) {
3148
0
                    int is_html;
3149
0
                    OFF html_end;
3150
3151
                    /* Given the nature of the raw HTML, we have to recognize
3152
                     * it here. Doing so later in md_analyze_lt_gt() could
3153
                     * open can of worms of quadratic complexity. */
3154
0
                    is_html = md_is_html_any(ctx, line, n_lines - line_index, off,
3155
0
                                    lines[n_lines-1].end, &html_end);
3156
0
                    if(is_html) {
3157
0
                        ADD_MARK(_T('<'), off, off, MD_MARK_OPENER | MD_MARK_RESOLVED);
3158
0
                        ADD_MARK(_T('>'), html_end, html_end, MD_MARK_CLOSER | MD_MARK_RESOLVED);
3159
0
                        ctx->marks[ctx->n_marks-2].next = ctx->n_marks-1;
3160
0
                        ctx->marks[ctx->n_marks-1].prev = ctx->n_marks-2;
3161
0
                        off = html_end;
3162
3163
                        /* Advance the current line accordingly. */
3164
0
                        if(off > line->end)
3165
0
                            line = md_lookup_line(off, lines, n_lines, &line_index);
3166
0
                        continue;
3167
0
                    }
3168
0
                }
3169
3170
0
                is_autolink = md_is_autolink(ctx, off, lines[n_lines-1].end,
3171
0
                                    &autolink_end, &missing_mailto);
3172
0
                if(is_autolink) {
3173
0
                    unsigned flags = MD_MARK_RESOLVED | MD_MARK_AUTOLINK;
3174
0
                    if(missing_mailto)
3175
0
                        flags |= MD_MARK_AUTOLINK_MISSING_MAILTO;
3176
3177
0
                    ADD_MARK(_T('<'), off, off+1, MD_MARK_OPENER | flags);
3178
0
                    ADD_MARK(_T('>'), autolink_end-1, autolink_end, MD_MARK_CLOSER | flags);
3179
0
                    ctx->marks[ctx->n_marks-2].next = ctx->n_marks-1;
3180
0
                    ctx->marks[ctx->n_marks-1].prev = ctx->n_marks-2;
3181
0
                    off = autolink_end;
3182
0
                    continue;
3183
0
                }
3184
3185
0
                off++;
3186
0
                continue;
3187
0
            }
3188
3189
            /* A potential link or its part. */
3190
0
            if(ch == _T('[')  ||  (ch == _T('!') && off+1 < line->end && CH(off+1) == _T('['))) {
3191
0
                OFF tmp = (ch == _T('[') ? off+1 : off+2);
3192
0
                ADD_MARK(ch, off, tmp, MD_MARK_POTENTIAL_OPENER);
3193
0
                off = tmp;
3194
                /* Two dummies to make enough place for data we need if it is
3195
                 * a link. */
3196
0
                ADD_MARK('D', off, off, 0);
3197
0
                ADD_MARK('D', off, off, 0);
3198
0
                continue;
3199
0
            }
3200
0
            if(ch == _T(']')) {
3201
0
                ADD_MARK(ch, off, off+1, MD_MARK_POTENTIAL_CLOSER);
3202
0
                off++;
3203
0
                continue;
3204
0
            }
3205
3206
            /* A potential permissive e-mail autolink. */
3207
0
            if(ch == _T('@')) {
3208
0
                if(line->beg + 1 <= off  &&  ISALNUM(off-1)  &&
3209
0
                    off + 3 < line->end  &&  ISALNUM(off+1))
3210
0
                {
3211
0
                    ADD_MARK(ch, off, off+1, MD_MARK_POTENTIAL_OPENER);
3212
                    /* Push a dummy as a reserve for a closer. */
3213
0
                    ADD_MARK('D', line->beg, line->end, 0);
3214
0
                }
3215
3216
0
                off++;
3217
0
                continue;
3218
0
            }
3219
3220
            /* A potential permissive URL autolink. */
3221
0
            if(ch == _T(':')) {
3222
0
                static const struct {
3223
0
                    const CHAR* scheme;
3224
0
                    SZ scheme_size;
3225
0
                    const CHAR* suffix;
3226
0
                    SZ suffix_size;
3227
0
                } scheme_map[] = {
3228
                    /* In the order from the most frequently used, arguably. */
3229
0
                    { _T("http"), 4,    _T("//"), 2 },
3230
0
                    { _T("https"), 5,   _T("//"), 2 },
3231
0
                    { _T("ftp"), 3,     _T("//"), 2 }
3232
0
                };
3233
0
                int scheme_index;
3234
3235
0
                for(scheme_index = 0; scheme_index < (int) SIZEOF_ARRAY(scheme_map); scheme_index++) {
3236
0
                    const CHAR* scheme = scheme_map[scheme_index].scheme;
3237
0
                    const SZ scheme_size = scheme_map[scheme_index].scheme_size;
3238
0
                    const CHAR* suffix = scheme_map[scheme_index].suffix;
3239
0
                    const SZ suffix_size = scheme_map[scheme_index].suffix_size;
3240
3241
0
                    if(line->beg + scheme_size <= off  &&  md_ascii_eq(STR(off-scheme_size), scheme, scheme_size)  &&
3242
0
                        off + 1 + suffix_size < line->end  &&  md_ascii_eq(STR(off+1), suffix, suffix_size))
3243
0
                    {
3244
0
                        ADD_MARK(ch, off-scheme_size, off+1+suffix_size, MD_MARK_POTENTIAL_OPENER);
3245
                        /* Push a dummy as a reserve for a closer. */
3246
0
                        ADD_MARK('D', line->beg, line->end, 0);
3247
0
                        off += 1 + suffix_size;
3248
0
                        break;
3249
0
                    }
3250
0
                }
3251
3252
0
                off++;
3253
0
                continue;
3254
0
            }
3255
3256
            /* A potential permissive WWW autolink. */
3257
0
            if(ch == _T('.')) {
3258
0
                if(line->beg + 3 <= off  &&  md_ascii_eq(STR(off-3), _T("www"), 3)  &&
3259
0
                   (off-3 == line->beg || ISUNICODEWHITESPACEBEFORE(off-3) || ISUNICODEPUNCTBEFORE(off-3)))
3260
0
                {
3261
0
                    ADD_MARK(ch, off-3, off+1, MD_MARK_POTENTIAL_OPENER);
3262
                    /* Push a dummy as a reserve for a closer. */
3263
0
                    ADD_MARK('D', line->beg, line->end, 0);
3264
0
                    off++;
3265
0
                    continue;
3266
0
                }
3267
3268
0
                off++;
3269
0
                continue;
3270
0
            }
3271
3272
            /* A potential table cell boundary or wiki link label delimiter. */
3273
0
            if((table_mode || ctx->parser.flags & MD_FLAG_WIKILINKS) && ch == _T('|')) {
3274
0
                ADD_MARK(ch, off, off+1, 0);
3275
0
                off++;
3276
0
                continue;
3277
0
            }
3278
3279
            /* A potential strikethrough start/end. */
3280
0
            if(ch == _T('~')) {
3281
0
                OFF tmp = off+1;
3282
3283
0
                while(tmp < line->end  &&  CH(tmp) == _T('~'))
3284
0
                    tmp++;
3285
3286
0
                if(tmp - off < 3) {
3287
0
                    unsigned flags = 0;
3288
3289
0
                    if(tmp < line->end  &&  !ISUNICODEWHITESPACE(tmp))
3290
0
                        flags |= MD_MARK_POTENTIAL_OPENER;
3291
0
                    if(off > line->beg  &&  !ISUNICODEWHITESPACEBEFORE(off))
3292
0
                        flags |= MD_MARK_POTENTIAL_CLOSER;
3293
0
                    if(flags != 0)
3294
0
                        ADD_MARK(ch, off, tmp, flags);
3295
0
                }
3296
3297
0
                off = tmp;
3298
0
                continue;
3299
0
            }
3300
3301
            /* A potential equation start/end */
3302
0
            if(ch == _T('$')) {
3303
                /* We can have at most two consecutive $ signs,
3304
                 * where two dollar signs signify a display equation. */
3305
0
                OFF tmp = off+1;
3306
3307
0
                while(tmp < line->end && CH(tmp) == _T('$'))
3308
0
                    tmp++;
3309
3310
0
                if(tmp - off <= 2) {
3311
0
                    unsigned flags = MD_MARK_POTENTIAL_OPENER | MD_MARK_POTENTIAL_CLOSER;
3312
3313
0
                    if(off > line->beg  &&  !ISUNICODEWHITESPACEBEFORE(off)  &&  !ISUNICODEPUNCTBEFORE(off))
3314
0
                        flags &= ~MD_MARK_POTENTIAL_OPENER;
3315
0
                    if(tmp < line->end  &&  !ISUNICODEWHITESPACE(tmp)  &&  !ISUNICODEPUNCT(tmp))
3316
0
                        flags &= ~MD_MARK_POTENTIAL_CLOSER;
3317
0
                    if(flags != 0)
3318
0
                        ADD_MARK(ch, off, tmp, flags);
3319
0
                }
3320
3321
0
                off = tmp;
3322
0
                continue;
3323
0
            }
3324
3325
            /* Turn non-trivial whitespace into single space. */
3326
0
            if(ISWHITESPACE_(ch)) {
3327
0
                OFF tmp = off+1;
3328
3329
0
                while(tmp < line->end  &&  ISWHITESPACE(tmp))
3330
0
                    tmp++;
3331
3332
0
                if(tmp - off > 1  ||  ch != _T(' '))
3333
0
                    ADD_MARK(ch, off, tmp, MD_MARK_RESOLVED);
3334
3335
0
                off = tmp;
3336
0
                continue;
3337
0
            }
3338
3339
            /* NULL character. */
3340
0
            if(ch == _T('\0')) {
3341
0
                ADD_MARK(ch, off, off+1, MD_MARK_RESOLVED);
3342
0
                off++;
3343
0
                continue;
3344
0
            }
3345
3346
0
            off++;
3347
0
        }
3348
0
    }
3349
3350
    /* Add a dummy mark at the end of the mark vector to simplify
3351
     * process_inlines(). */
3352
0
    ADD_MARK(127, ctx->size, ctx->size, MD_MARK_RESOLVED);
3353
3354
0
abort:
3355
0
    return ret;
3356
0
}
3357
3358
static void
3359
md_analyze_bracket(MD_CTX* ctx, int mark_index)
3360
0
{
3361
    /* We cannot really resolve links here as for that we would need
3362
     * more context. E.g. a following pair of brackets (reference link),
3363
     * or enclosing pair of brackets (if the inner is the link, the outer
3364
     * one cannot be.)
3365
     *
3366
     * Therefore we here only construct a list of '[' ']' pairs ordered by
3367
     * position of the closer. This allows us to analyze what is or is not
3368
     * link in the right order, from inside to outside in case of nested
3369
     * brackets.
3370
     *
3371
     * The resolving itself is deferred to md_resolve_links().
3372
     */
3373
3374
0
    MD_MARK* mark = &ctx->marks[mark_index];
3375
3376
0
    if(mark->flags & MD_MARK_POTENTIAL_OPENER) {
3377
0
        if(BRACKET_OPENERS.top >= 0)
3378
0
            ctx->marks[BRACKET_OPENERS.top].flags |= MD_MARK_HASNESTEDBRACKETS;
3379
3380
0
        md_mark_stack_push(ctx, &BRACKET_OPENERS, mark_index);
3381
0
        return;
3382
0
    }
3383
3384
0
    if(BRACKET_OPENERS.top >= 0) {
3385
0
        int opener_index = md_mark_stack_pop(ctx, &BRACKET_OPENERS);
3386
0
        MD_MARK* opener = &ctx->marks[opener_index];
3387
3388
        /* Interconnect the opener and closer. */
3389
0
        opener->next = mark_index;
3390
0
        mark->prev = opener_index;
3391
3392
        /* Add the pair into a list of potential links for md_resolve_links().
3393
         * Note we misuse opener->prev for this as opener->next points to its
3394
         * closer. */
3395
0
        if(ctx->unresolved_link_tail >= 0)
3396
0
            ctx->marks[ctx->unresolved_link_tail].prev = opener_index;
3397
0
        else
3398
0
            ctx->unresolved_link_head = opener_index;
3399
0
        ctx->unresolved_link_tail = opener_index;
3400
0
        opener->prev = -1;
3401
0
    }
3402
0
}
3403
3404
/* Forward declaration. */
3405
static void md_analyze_link_contents(MD_CTX* ctx, const MD_LINE* lines, MD_SIZE n_lines,
3406
                                     int mark_beg, int mark_end);
3407
3408
static int
3409
md_resolve_links(MD_CTX* ctx, const MD_LINE* lines, MD_SIZE n_lines)
3410
0
{
3411
0
    int opener_index = ctx->unresolved_link_head;
3412
0
    OFF last_link_beg = 0;
3413
0
    OFF last_link_end = 0;
3414
0
    OFF last_img_beg = 0;
3415
0
    OFF last_img_end = 0;
3416
3417
0
    while(opener_index >= 0) {
3418
0
        MD_MARK* opener = &ctx->marks[opener_index];
3419
0
        int closer_index = opener->next;
3420
0
        MD_MARK* closer = &ctx->marks[closer_index];
3421
0
        int next_index = opener->prev;
3422
0
        MD_MARK* next_opener;
3423
0
        MD_MARK* next_closer;
3424
0
        MD_LINK_ATTR attr;
3425
0
        int is_link = FALSE;
3426
3427
0
        if(next_index >= 0) {
3428
0
            next_opener = &ctx->marks[next_index];
3429
0
            next_closer = &ctx->marks[next_opener->next];
3430
0
        } else {
3431
0
            next_opener = NULL;
3432
0
            next_closer = NULL;
3433
0
        }
3434
3435
        /* If nested ("[ [ ] ]"), we need to make sure that:
3436
         *   - The outer does not end inside of (...) belonging to the inner.
3437
         *   - The outer cannot be link if the inner is link (i.e. not image).
3438
         *
3439
         * (Note we here analyze from inner to outer as the marks are ordered
3440
         * by closer->beg.)
3441
         */
3442
0
        if((opener->beg < last_link_beg  &&  closer->end < last_link_end)  ||
3443
0
           (opener->beg < last_img_beg  &&  closer->end < last_img_end)  ||
3444
0
           (opener->beg < last_link_end  &&  opener->ch == '['))
3445
0
        {
3446
0
            opener_index = next_index;
3447
0
            continue;
3448
0
        }
3449
3450
        /* Recognize and resolve wiki links.
3451
         * Wiki-links maybe '[[destination]]' or '[[destination|label]]'.
3452
         */
3453
0
        if ((ctx->parser.flags & MD_FLAG_WIKILINKS) &&
3454
0
            (opener->end - opener->beg == 1) &&         /* not image */
3455
0
            next_opener != NULL &&                      /* double '[' opener */
3456
0
            next_opener->ch == '[' &&
3457
0
            (next_opener->beg == opener->beg - 1) &&
3458
0
            (next_opener->end - next_opener->beg == 1) &&
3459
0
            next_closer != NULL &&                      /* double ']' closer */
3460
0
            next_closer->ch == ']' &&
3461
0
            (next_closer->beg == closer->beg + 1) &&
3462
0
            (next_closer->end - next_closer->beg == 1))
3463
0
        {
3464
0
            MD_MARK* delim = NULL;
3465
0
            int delim_index;
3466
0
            OFF dest_beg, dest_end;
3467
3468
0
            is_link = TRUE;
3469
3470
            /* We don't allow destination to be longer than 100 characters.
3471
             * Lets scan to see whether there is '|'. (If not then the whole
3472
             * wiki-link has to be below the 100 characters.) */
3473
0
            delim_index = opener_index + 1;
3474
0
            while(delim_index < closer_index) {
3475
0
                MD_MARK* m = &ctx->marks[delim_index];
3476
0
                if(m->ch == '|') {
3477
0
                    delim = m;
3478
0
                    break;
3479
0
                }
3480
0
                if(m->ch != 'D') {
3481
0
                    if(m->beg - opener->end > 100)
3482
0
                        break;
3483
0
                    if(m->ch != 'D'  &&  (m->flags & MD_MARK_OPENER))
3484
0
                        delim_index = m->next;
3485
0
                }
3486
0
                delim_index++;
3487
0
            }
3488
3489
0
            dest_beg = opener->end;
3490
0
            dest_end = (delim != NULL) ? delim->beg : closer->beg;
3491
0
            if(dest_end - dest_beg == 0 || dest_end - dest_beg > 100)
3492
0
                is_link = FALSE;
3493
3494
            /* There may not be any new line in the destination. */
3495
0
            if(is_link) {
3496
0
                OFF off;
3497
0
                for(off = dest_beg; off < dest_end; off++) {
3498
0
                    if(ISNEWLINE(off)) {
3499
0
                        is_link = FALSE;
3500
0
                        break;
3501
0
                    }
3502
0
                }
3503
0
            }
3504
3505
0
            if(is_link) {
3506
0
                if(delim != NULL) {
3507
0
                    if(delim->end < closer->beg) {
3508
0
                        md_rollback(ctx, opener_index, delim_index, MD_ROLLBACK_ALL);
3509
0
                        md_rollback(ctx, delim_index, closer_index, MD_ROLLBACK_CROSSING);
3510
0
                        delim->flags |= MD_MARK_RESOLVED;
3511
0
                        opener->end = delim->beg;
3512
0
                    } else {
3513
                        /* The pipe is just before the closer: [[foo|]] */
3514
0
                        md_rollback(ctx, opener_index, closer_index, MD_ROLLBACK_ALL);
3515
0
                        closer->beg = delim->beg;
3516
0
                        delim = NULL;
3517
0
                    }
3518
0
                }
3519
3520
0
                opener->beg = next_opener->beg;
3521
0
                opener->next = closer_index;
3522
0
                opener->flags |= MD_MARK_OPENER | MD_MARK_RESOLVED;
3523
3524
0
                closer->end = next_closer->end;
3525
0
                closer->prev = opener_index;
3526
0
                closer->flags |= MD_MARK_CLOSER | MD_MARK_RESOLVED;
3527
3528
0
                last_link_beg = opener->beg;
3529
0
                last_link_end = closer->end;
3530
3531
0
                if(delim != NULL)
3532
0
                    md_analyze_link_contents(ctx, lines, n_lines, delim_index+1, closer_index);
3533
3534
0
                opener_index = next_opener->prev;
3535
0
                continue;
3536
0
            }
3537
0
        }
3538
3539
0
        if(next_opener != NULL  &&  next_opener->beg == closer->end) {
3540
0
            if(next_closer->beg > closer->end + 1) {
3541
                /* Might be full reference link. */
3542
0
                if(!(next_opener->flags & MD_MARK_HASNESTEDBRACKETS))
3543
0
                    is_link = md_is_link_reference(ctx, lines, n_lines, next_opener->beg, next_closer->end, &attr);
3544
0
            } else {
3545
                /* Might be shortcut reference link. */
3546
0
                if(!(opener->flags & MD_MARK_HASNESTEDBRACKETS))
3547
0
                    is_link = md_is_link_reference(ctx, lines, n_lines, opener->beg, closer->end, &attr);
3548
0
            }
3549
3550
0
            if(is_link < 0)
3551
0
                return -1;
3552
3553
0
            if(is_link) {
3554
                /* Eat the 2nd "[...]". */
3555
0
                closer->end = next_closer->end;
3556
3557
                /* Do not analyze the label as a standalone link in the next
3558
                 * iteration. */
3559
0
                next_index = ctx->marks[next_index].prev;
3560
0
            }
3561
0
        } else {
3562
0
            if(closer->end < ctx->size  &&  CH(closer->end) == _T('(')) {
3563
                /* Might be inline link. */
3564
0
                OFF inline_link_end = UINT_MAX;
3565
3566
0
                is_link = md_is_inline_link_spec(ctx, lines, n_lines, closer->end, &inline_link_end, &attr);
3567
0
                if(is_link < 0)
3568
0
                    return -1;
3569
3570
                /* Check the closing ')' is not inside an already resolved range
3571
                 * (i.e. a range with a higher priority), e.g. a code span. */
3572
0
                if(is_link) {
3573
0
                    int i = closer_index + 1;
3574
3575
0
                    while(i < ctx->n_marks) {
3576
0
                        MD_MARK* mark = &ctx->marks[i];
3577
3578
0
                        if(mark->beg >= inline_link_end)
3579
0
                            break;
3580
0
                        if((mark->flags & (MD_MARK_OPENER | MD_MARK_RESOLVED)) == (MD_MARK_OPENER | MD_MARK_RESOLVED)) {
3581
0
                            if(ctx->marks[mark->next].beg >= inline_link_end) {
3582
                                /* Cancel the link status. */
3583
0
                                if(attr.title_needs_free)
3584
0
                                    free(attr.title);
3585
0
                                is_link = FALSE;
3586
0
                                break;
3587
0
                            }
3588
3589
0
                            i = mark->next + 1;
3590
0
                        } else {
3591
0
                            i++;
3592
0
                        }
3593
0
                    }
3594
0
                }
3595
3596
0
                if(is_link) {
3597
                    /* Eat the "(...)" */
3598
0
                    closer->end = inline_link_end;
3599
0
                }
3600
0
            }
3601
3602
0
            if(!is_link) {
3603
                /* Might be collapsed reference link. */
3604
0
                if(!(opener->flags & MD_MARK_HASNESTEDBRACKETS))
3605
0
                    is_link = md_is_link_reference(ctx, lines, n_lines, opener->beg, closer->end, &attr);
3606
0
                if(is_link < 0)
3607
0
                    return -1;
3608
0
            }
3609
0
        }
3610
3611
0
        if(is_link) {
3612
            /* Resolve the brackets as a link. */
3613
0
            opener->flags |= MD_MARK_OPENER | MD_MARK_RESOLVED;
3614
0
            closer->flags |= MD_MARK_CLOSER | MD_MARK_RESOLVED;
3615
3616
            /* If it is a link, we store the destination and title in the two
3617
             * dummy marks after the opener. */
3618
0
            MD_ASSERT(ctx->marks[opener_index+1].ch == 'D');
3619
0
            ctx->marks[opener_index+1].beg = attr.dest_beg;
3620
0
            ctx->marks[opener_index+1].end = attr.dest_end;
3621
3622
0
            MD_ASSERT(ctx->marks[opener_index+2].ch == 'D');
3623
0
            md_mark_store_ptr(ctx, opener_index+2, attr.title);
3624
            /* The title might or might not have been allocated for us. */
3625
0
            if(attr.title_needs_free)
3626
0
                md_mark_stack_push(ctx, &ctx->ptr_stack, opener_index+2);
3627
0
            ctx->marks[opener_index+2].prev = attr.title_size;
3628
3629
0
            if(opener->ch == '[') {
3630
0
                last_link_beg = opener->beg;
3631
0
                last_link_end = closer->end;
3632
0
            } else {
3633
0
                last_img_beg = opener->beg;
3634
0
                last_img_end = closer->end;
3635
0
            }
3636
3637
0
            md_analyze_link_contents(ctx, lines, n_lines, opener_index+1, closer_index);
3638
3639
            /* If the link text is formed by nothing but permissive autolink,
3640
             * suppress the autolink.
3641
             * See https://github.com/mity/md4c/issues/152 for more info. */
3642
0
            if(ctx->parser.flags & MD_FLAG_PERMISSIVEAUTOLINKS) {
3643
0
                MD_MARK* first_nested;
3644
0
                MD_MARK* last_nested;
3645
3646
0
                first_nested = opener + 1;
3647
0
                while(first_nested->ch == _T('D')  &&  first_nested < closer)
3648
0
                    first_nested++;
3649
3650
0
                last_nested = closer - 1;
3651
0
                while(first_nested->ch == _T('D')  &&  last_nested > opener)
3652
0
                    last_nested--;
3653
3654
0
                if((first_nested->flags & MD_MARK_RESOLVED)  &&
3655
0
                   first_nested->beg == opener->end  &&
3656
0
                   ISANYOF_(first_nested->ch, _T("@:."))  &&
3657
0
                   first_nested->next == (last_nested - ctx->marks)  &&
3658
0
                   last_nested->end == closer->beg)
3659
0
                {
3660
0
                    first_nested->ch = _T('D');
3661
0
                    first_nested->flags &= ~MD_MARK_RESOLVED;
3662
0
                    last_nested->ch = _T('D');
3663
0
                    last_nested->flags &= ~MD_MARK_RESOLVED;
3664
0
                }
3665
0
            }
3666
0
        }
3667
3668
0
        opener_index = next_index;
3669
0
    }
3670
3671
0
    return 0;
3672
0
}
3673
3674
/* Analyze whether the mark '&' starts a HTML entity.
3675
 * If so, update its flags as well as flags of corresponding closer ';'. */
3676
static void
3677
md_analyze_entity(MD_CTX* ctx, int mark_index)
3678
0
{
3679
0
    MD_MARK* opener = &ctx->marks[mark_index];
3680
0
    MD_MARK* closer;
3681
0
    OFF off;
3682
3683
    /* Cannot be entity if there is no closer as the next mark.
3684
     * (Any other mark between would mean strange character which cannot be
3685
     * part of the entity.
3686
     *
3687
     * So we can do all the work on '&' and do not call this later for the
3688
     * closing mark ';'.
3689
     */
3690
0
    if(mark_index + 1 >= ctx->n_marks)
3691
0
        return;
3692
0
    closer = &ctx->marks[mark_index+1];
3693
0
    if(closer->ch != ';')
3694
0
        return;
3695
3696
0
    if(md_is_entity(ctx, opener->beg, closer->end, &off)) {
3697
0
        MD_ASSERT(off == closer->end);
3698
3699
0
        md_resolve_range(ctx, mark_index, mark_index+1);
3700
0
        opener->end = closer->end;
3701
0
    }
3702
0
}
3703
3704
static void
3705
md_analyze_table_cell_boundary(MD_CTX* ctx, int mark_index)
3706
0
{
3707
0
    MD_MARK* mark = &ctx->marks[mark_index];
3708
0
    mark->flags |= MD_MARK_RESOLVED;
3709
0
    mark->next = -1;
3710
3711
0
    if(ctx->table_cell_boundaries_head < 0)
3712
0
        ctx->table_cell_boundaries_head = mark_index;
3713
0
    else
3714
0
        ctx->marks[ctx->table_cell_boundaries_tail].next = mark_index;
3715
0
    ctx->table_cell_boundaries_tail = mark_index;
3716
0
    ctx->n_table_cell_boundaries++;
3717
0
}
3718
3719
/* Split a longer mark into two. The new mark takes the given count of
3720
 * characters. May only be called if an adequate number of dummy 'D' marks
3721
 * follows.
3722
 */
3723
static int
3724
md_split_emph_mark(MD_CTX* ctx, int mark_index, SZ n)
3725
0
{
3726
0
    MD_MARK* mark = &ctx->marks[mark_index];
3727
0
    int new_mark_index = mark_index + (mark->end - mark->beg - n);
3728
0
    MD_MARK* dummy = &ctx->marks[new_mark_index];
3729
3730
0
    MD_ASSERT(mark->end - mark->beg > n);
3731
0
    MD_ASSERT(dummy->ch == 'D');
3732
3733
0
    memcpy(dummy, mark, sizeof(MD_MARK));
3734
0
    mark->end -= n;
3735
0
    dummy->beg = mark->end;
3736
3737
0
    return new_mark_index;
3738
0
}
3739
3740
static void
3741
md_analyze_emph(MD_CTX* ctx, int mark_index)
3742
0
{
3743
0
    MD_MARK* mark = &ctx->marks[mark_index];
3744
3745
    /* If we can be a closer, try to resolve with the preceding opener. */
3746
0
    if(mark->flags & MD_MARK_POTENTIAL_CLOSER) {
3747
0
        MD_MARK* opener = NULL;
3748
0
        int opener_index = 0;
3749
0
        MD_MARKSTACK* opener_stacks[6];
3750
0
        int i, n_opener_stacks;
3751
0
        unsigned flags = mark->flags;
3752
3753
0
        n_opener_stacks = 0;
3754
3755
        /* Apply the rule of 3 */
3756
0
        opener_stacks[n_opener_stacks++] = md_emph_stack(ctx, mark->ch, MD_MARK_EMPH_MOD3_0 | MD_MARK_EMPH_OC);
3757
0
        if((flags & MD_MARK_EMPH_MOD3_MASK) != MD_MARK_EMPH_MOD3_2)
3758
0
            opener_stacks[n_opener_stacks++] = md_emph_stack(ctx, mark->ch, MD_MARK_EMPH_MOD3_1 | MD_MARK_EMPH_OC);
3759
0
        if((flags & MD_MARK_EMPH_MOD3_MASK) != MD_MARK_EMPH_MOD3_1)
3760
0
            opener_stacks[n_opener_stacks++] = md_emph_stack(ctx, mark->ch, MD_MARK_EMPH_MOD3_2 | MD_MARK_EMPH_OC);
3761
0
        opener_stacks[n_opener_stacks++] = md_emph_stack(ctx, mark->ch, MD_MARK_EMPH_MOD3_0);
3762
0
        if(!(flags & MD_MARK_EMPH_OC)  ||  (flags & MD_MARK_EMPH_MOD3_MASK) != MD_MARK_EMPH_MOD3_2)
3763
0
            opener_stacks[n_opener_stacks++] = md_emph_stack(ctx, mark->ch, MD_MARK_EMPH_MOD3_1);
3764
0
        if(!(flags & MD_MARK_EMPH_OC)  ||  (flags & MD_MARK_EMPH_MOD3_MASK) != MD_MARK_EMPH_MOD3_1)
3765
0
            opener_stacks[n_opener_stacks++] = md_emph_stack(ctx, mark->ch, MD_MARK_EMPH_MOD3_2);
3766
3767
        /* Opener is the most recent mark from the allowed stacks. */
3768
0
        for(i = 0; i < n_opener_stacks; i++) {
3769
0
            if(opener_stacks[i]->top >= 0) {
3770
0
                int m_index = opener_stacks[i]->top;
3771
0
                MD_MARK* m = &ctx->marks[m_index];
3772
3773
0
                if(opener == NULL  ||  m->end > opener->end) {
3774
0
                    opener_index = m_index;
3775
0
                    opener = m;
3776
0
                }
3777
0
            }
3778
0
        }
3779
3780
        /* Resolve, if we have found matching opener. */
3781
0
        if(opener != NULL) {
3782
0
            SZ opener_size = opener->end - opener->beg;
3783
0
            SZ closer_size = mark->end - mark->beg;
3784
0
            MD_MARKSTACK* stack = md_opener_stack(ctx, opener_index);
3785
3786
0
            if(opener_size > closer_size) {
3787
0
                opener_index = md_split_emph_mark(ctx, opener_index, closer_size);
3788
0
                md_mark_stack_push(ctx, stack, opener_index);
3789
0
            } else if(opener_size < closer_size) {
3790
0
                md_split_emph_mark(ctx, mark_index, closer_size - opener_size);
3791
0
            }
3792
3793
            /* Above we were only peeking. */
3794
0
            md_mark_stack_pop(ctx, stack);
3795
3796
0
            md_rollback(ctx, opener_index, mark_index, MD_ROLLBACK_CROSSING);
3797
0
            md_resolve_range(ctx, opener_index, mark_index);
3798
0
            return;
3799
0
        }
3800
0
    }
3801
3802
    /* If we could not resolve as closer, we may be yet be an opener. */
3803
0
    if(mark->flags & MD_MARK_POTENTIAL_OPENER)
3804
0
        md_mark_stack_push(ctx, md_emph_stack(ctx, mark->ch, mark->flags), mark_index);
3805
0
}
3806
3807
static void
3808
md_analyze_tilde(MD_CTX* ctx, int mark_index)
3809
0
{
3810
0
    MD_MARK* mark = &ctx->marks[mark_index];
3811
0
    MD_MARKSTACK* stack = md_opener_stack(ctx, mark_index);
3812
3813
    /* We attempt to be Github Flavored Markdown compatible here. GFM accepts
3814
     * only tildes sequences of length 1 and 2, and the length of the opener
3815
     * and closer has to match. */
3816
3817
0
    if((mark->flags & MD_MARK_POTENTIAL_CLOSER)  &&  stack->top >= 0) {
3818
0
        int opener_index = stack->top;
3819
3820
0
        md_mark_stack_pop(ctx, stack);
3821
0
        md_rollback(ctx, opener_index, mark_index, MD_ROLLBACK_CROSSING);
3822
0
        md_resolve_range(ctx, opener_index, mark_index);
3823
0
        return;
3824
0
    }
3825
3826
0
    if(mark->flags & MD_MARK_POTENTIAL_OPENER)
3827
0
        md_mark_stack_push(ctx, stack, mark_index);
3828
0
}
3829
3830
static void
3831
md_analyze_dollar(MD_CTX* ctx, int mark_index)
3832
0
{
3833
0
    MD_MARK* mark = &ctx->marks[mark_index];
3834
3835
0
    if((mark->flags & MD_MARK_POTENTIAL_CLOSER)  &&  DOLLAR_OPENERS.top >= 0) {
3836
        /* If the potential closer has a non-matching number of $, discard */
3837
0
        MD_MARK* opener = &ctx->marks[DOLLAR_OPENERS.top];
3838
0
        int opener_index = DOLLAR_OPENERS.top;
3839
0
        MD_MARK* closer = mark;
3840
0
        int closer_index = mark_index;
3841
3842
0
        if(opener->end - opener->beg == closer->end - closer->beg) {
3843
            /* We are the matching closer */
3844
0
            md_mark_stack_pop(ctx, &DOLLAR_OPENERS);
3845
0
            md_rollback(ctx, opener_index, closer_index, MD_ROLLBACK_ALL);
3846
0
            md_resolve_range(ctx, opener_index, closer_index);
3847
3848
            /* Discard all pending openers: Latex math span do not allow
3849
             * nesting. */
3850
0
            DOLLAR_OPENERS.top = -1;
3851
0
            return;
3852
0
        }
3853
0
    }
3854
3855
0
    if(mark->flags & MD_MARK_POTENTIAL_OPENER)
3856
0
        md_mark_stack_push(ctx, &DOLLAR_OPENERS, mark_index);
3857
0
}
3858
3859
static MD_MARK*
3860
md_scan_left_for_resolved_mark(MD_CTX* ctx, MD_MARK* mark_from, OFF off, MD_MARK** p_cursor)
3861
0
{
3862
0
    MD_MARK* mark;
3863
3864
0
    for(mark = mark_from; mark >= ctx->marks; mark--) {
3865
0
        if(mark->ch == 'D'  ||  mark->beg > off)
3866
0
            continue;
3867
0
        if(mark->beg <= off  &&  off < mark->end  &&  (mark->flags & MD_MARK_RESOLVED)) {
3868
0
            if(p_cursor != NULL)
3869
0
                *p_cursor = mark;
3870
0
            return mark;
3871
0
        }
3872
0
        if(mark->end <= off)
3873
0
            break;
3874
0
    }
3875
3876
0
    if(p_cursor != NULL)
3877
0
        *p_cursor = mark;
3878
0
    return NULL;
3879
0
}
3880
3881
static MD_MARK*
3882
md_scan_right_for_resolved_mark(MD_CTX* ctx, MD_MARK* mark_from, OFF off, MD_MARK** p_cursor)
3883
0
{
3884
0
    MD_MARK* mark;
3885
3886
0
    for(mark = mark_from; mark < ctx->marks + ctx->n_marks; mark++) {
3887
0
        if(mark->ch == 'D'  ||  mark->end <= off)
3888
0
            continue;
3889
0
        if(mark->beg <= off  &&  off < mark->end  &&  (mark->flags & MD_MARK_RESOLVED)) {
3890
0
            if(p_cursor != NULL)
3891
0
                *p_cursor = mark;
3892
0
            return mark;
3893
0
        }
3894
0
        if(mark->beg > off)
3895
0
            break;
3896
0
    }
3897
3898
0
    if(p_cursor != NULL)
3899
0
        *p_cursor = mark;
3900
0
    return NULL;
3901
0
}
3902
3903
static void
3904
md_analyze_permissive_autolink(MD_CTX* ctx, int mark_index)
3905
0
{
3906
0
    static const struct {
3907
0
        const MD_CHAR start_char;
3908
0
        const MD_CHAR delim_char;
3909
0
        const MD_CHAR* allowed_nonalnum_chars;
3910
0
        int min_components;
3911
0
        const MD_CHAR optional_end_char;
3912
0
    } URL_MAP[] = {
3913
0
        { _T('\0'), _T('.'),  _T(".-_"),      2, _T('\0') },    /* host, mandatory */
3914
0
        { _T('/'),  _T('/'),  _T("/.-_"),     0, _T('/') },     /* path */
3915
0
        { _T('?'),  _T('&'),  _T("&.-+_=()"), 1, _T('\0') },    /* query */
3916
0
        { _T('#'),  _T('\0'), _T(".-+_") ,    1, _T('\0') }     /* fragment */
3917
0
    };
3918
3919
0
    MD_MARK* opener = &ctx->marks[mark_index];
3920
0
    MD_MARK* closer = &ctx->marks[mark_index + 1];  /* The dummy. */
3921
0
    OFF line_beg = closer->beg;     /* md_collect_mark() set this for us */
3922
0
    OFF line_end = closer->end;     /* ditto */
3923
0
    OFF beg = opener->beg;
3924
0
    OFF end = opener->end;
3925
0
    MD_MARK* left_cursor = opener;
3926
0
    int left_boundary_ok = FALSE;
3927
0
    MD_MARK* right_cursor = opener;
3928
0
    int right_boundary_ok = FALSE;
3929
0
    unsigned i;
3930
3931
0
    MD_ASSERT(closer->ch == 'D');
3932
3933
0
    if(opener->ch == '@') {
3934
0
        MD_ASSERT(CH(opener->beg) == _T('@'));
3935
3936
        /* Scan backwards for the user name (before '@'). */
3937
0
        while(beg > line_beg) {
3938
0
            if(ISALNUM(beg-1))
3939
0
                beg--;
3940
0
            else if(beg >= line_beg+2  &&  ISALNUM(beg-2)  &&
3941
0
                        ISANYOF(beg-1, _T(".-_+"))  &&
3942
0
                        md_scan_left_for_resolved_mark(ctx, left_cursor, beg-1, &left_cursor) == NULL  &&
3943
0
                        ISALNUM(beg))
3944
0
                beg--;
3945
0
            else
3946
0
                break;
3947
0
        }
3948
0
        if(beg == opener->beg)      /* empty user name */
3949
0
            return;
3950
0
    }
3951
3952
    /* Verify there's line boundary, whitespace, allowed punctuation or
3953
     * resolved emphasis mark just before the suspected autolink. */
3954
0
    if(beg == line_beg  ||  ISUNICODEWHITESPACEBEFORE(beg)  ||  ISANYOF(beg-1, _T("({["))) {
3955
0
        left_boundary_ok = TRUE;
3956
0
    } else if(ISANYOF(beg-1, _T("*_~"))) {
3957
0
        MD_MARK* left_mark;
3958
3959
0
        left_mark = md_scan_left_for_resolved_mark(ctx, left_cursor, beg-1, &left_cursor);
3960
0
        if(left_mark != NULL  &&  (left_mark->flags & MD_MARK_OPENER))
3961
0
            left_boundary_ok = TRUE;
3962
0
    }
3963
0
    if(!left_boundary_ok)
3964
0
        return;
3965
3966
0
    for(i = 0; i < SIZEOF_ARRAY(URL_MAP); i++) {
3967
0
        int n_components = 0;
3968
0
        int n_open_brackets = 0;
3969
3970
0
        if(URL_MAP[i].start_char != _T('\0')) {
3971
0
            if(end >= line_end  ||  CH(end) != URL_MAP[i].start_char)
3972
0
                continue;
3973
0
            if(URL_MAP[i].min_components > 0  &&  (end+1 >= line_end  ||  !ISALNUM(end+1)))
3974
0
                continue;
3975
0
            end++;
3976
0
        }
3977
3978
0
        while(end < line_end) {
3979
0
            if(ISALNUM(end)) {
3980
0
                if(n_components == 0)
3981
0
                    n_components++;
3982
0
                end++;
3983
0
            } else if(end < line_end  &&
3984
0
                        ISANYOF(end, URL_MAP[i].allowed_nonalnum_chars)  &&
3985
0
                        md_scan_right_for_resolved_mark(ctx, right_cursor, end, &right_cursor) == NULL  &&
3986
0
                        ((end > line_beg && (ISALNUM(end-1) || CH(end-1) == _T(')')))  ||  CH(end) == _T('('))  &&
3987
0
                        ((end+1 < line_end && (ISALNUM(end+1) || CH(end+1) == _T('(')))  ||  CH(end) == _T(')')))
3988
0
            {
3989
0
                if(CH(end) == URL_MAP[i].delim_char)
3990
0
                    n_components++;
3991
3992
                /* brackets have to be balanced. */
3993
0
                if(CH(end) == _T('(')) {
3994
0
                    n_open_brackets++;
3995
0
                } else if(CH(end) == _T(')')) {
3996
0
                    if(n_open_brackets <= 0)
3997
0
                        break;
3998
0
                    n_open_brackets--;
3999
0
                }
4000
4001
0
                end++;
4002
0
            } else {
4003
0
                break;
4004
0
            }
4005
0
        }
4006
4007
0
        if(end < line_end  &&  URL_MAP[i].optional_end_char != _T('\0')  &&
4008
0
                CH(end) == URL_MAP[i].optional_end_char)
4009
0
            end++;
4010
4011
0
        if(n_components < URL_MAP[i].min_components  ||  n_open_brackets != 0)
4012
0
            return;
4013
4014
0
        if(opener->ch == '@')   /* E-mail autolinks wants only the host. */
4015
0
            break;
4016
0
    }
4017
4018
    /* Verify there's line boundary, whitespace, allowed punctuation or
4019
     * resolved emphasis mark just after the suspected autolink. */
4020
0
    if(end == line_end  ||  ISUNICODEWHITESPACE(end)  ||  ISANYOF(end, _T(")}].!?,;"))) {
4021
0
        right_boundary_ok = TRUE;
4022
0
    } else {
4023
0
        MD_MARK* right_mark;
4024
4025
0
        right_mark = md_scan_right_for_resolved_mark(ctx, right_cursor, end, &right_cursor);
4026
0
        if(right_mark != NULL  &&  (right_mark->flags & MD_MARK_CLOSER))
4027
0
            right_boundary_ok = TRUE;
4028
0
    }
4029
0
    if(!right_boundary_ok)
4030
0
        return;
4031
4032
    /* Success, we are an autolink. */
4033
0
    opener->beg = beg;
4034
0
    opener->end = beg;
4035
0
    closer->beg = end;
4036
0
    closer->end = end;
4037
0
    closer->ch = opener->ch;
4038
0
    md_resolve_range(ctx, mark_index, mark_index + 1);
4039
0
}
4040
4041
0
#define MD_ANALYZE_NOSKIP_EMPH  0x01
4042
4043
static inline void
4044
md_analyze_marks(MD_CTX* ctx, const MD_LINE* lines, MD_SIZE n_lines,
4045
                 int mark_beg, int mark_end, const CHAR* mark_chars, unsigned flags)
4046
0
{
4047
0
    int i = mark_beg;
4048
0
    OFF last_end = lines[0].beg;
4049
4050
0
    MD_UNUSED(lines);
4051
0
    MD_UNUSED(n_lines);
4052
4053
0
    while(i < mark_end) {
4054
0
        MD_MARK* mark = &ctx->marks[i];
4055
4056
        /* Skip resolved spans. */
4057
0
        if(mark->flags & MD_MARK_RESOLVED) {
4058
0
            if((mark->flags & MD_MARK_OPENER)  &&
4059
0
               !((flags & MD_ANALYZE_NOSKIP_EMPH) && ISANYOF_(mark->ch, "*_~")))
4060
0
            {
4061
0
                MD_ASSERT(i < mark->next);
4062
0
                i = mark->next + 1;
4063
0
            } else {
4064
0
                i++;
4065
0
            }
4066
0
            continue;
4067
0
        }
4068
4069
        /* Skip marks we do not want to deal with. */
4070
0
        if(!ISANYOF_(mark->ch, mark_chars)) {
4071
0
            i++;
4072
0
            continue;
4073
0
        }
4074
4075
        /* The resolving in previous step could have expanded a mark. */
4076
0
        if(mark->beg < last_end) {
4077
0
            i++;
4078
0
            continue;
4079
0
        }
4080
4081
        /* Analyze the mark. */
4082
0
        switch(mark->ch) {
4083
0
            case '[':   /* Pass through. */
4084
0
            case '!':   /* Pass through. */
4085
0
            case ']':   md_analyze_bracket(ctx, i); break;
4086
0
            case '&':   md_analyze_entity(ctx, i); break;
4087
0
            case '|':   md_analyze_table_cell_boundary(ctx, i); break;
4088
0
            case '_':   /* Pass through. */
4089
0
            case '*':   md_analyze_emph(ctx, i); break;
4090
0
            case '~':   md_analyze_tilde(ctx, i); break;
4091
0
            case '$':   md_analyze_dollar(ctx, i); break;
4092
0
            case '.':   /* Pass through. */
4093
0
            case ':':   /* Pass through. */
4094
0
            case '@':   md_analyze_permissive_autolink(ctx, i); break;
4095
0
        }
4096
4097
0
        if(mark->flags & MD_MARK_RESOLVED) {
4098
0
            if(mark->flags & MD_MARK_OPENER)
4099
0
                last_end = ctx->marks[mark->next].end;
4100
0
            else
4101
0
                last_end = mark->end;
4102
0
        }
4103
4104
0
        i++;
4105
0
    }
4106
0
}
4107
4108
/* Analyze marks (build ctx->marks). */
4109
static int
4110
md_analyze_inlines(MD_CTX* ctx, const MD_LINE* lines, MD_SIZE n_lines, int table_mode)
4111
0
{
4112
0
    int ret;
4113
4114
    /* Reset the previously collected stack of marks. */
4115
0
    ctx->n_marks = 0;
4116
4117
    /* Collect all marks. */
4118
0
    MD_CHECK(md_collect_marks(ctx, lines, n_lines, table_mode));
4119
4120
    /* (1) Links. */
4121
0
    md_analyze_marks(ctx, lines, n_lines, 0, ctx->n_marks, _T("[]!"), 0);
4122
0
    MD_CHECK(md_resolve_links(ctx, lines, n_lines));
4123
0
    BRACKET_OPENERS.top = -1;
4124
0
    ctx->unresolved_link_head = -1;
4125
0
    ctx->unresolved_link_tail = -1;
4126
4127
0
    if(table_mode) {
4128
        /* (2) Analyze table cell boundaries. */
4129
0
        MD_ASSERT(n_lines == 1);
4130
0
        ctx->n_table_cell_boundaries = 0;
4131
0
        md_analyze_marks(ctx, lines, n_lines, 0, ctx->n_marks, _T("|"), 0);
4132
0
        return ret;
4133
0
    }
4134
4135
    /* (3) Emphasis and strong emphasis; permissive autolinks. */
4136
0
    md_analyze_link_contents(ctx, lines, n_lines, 0, ctx->n_marks);
4137
4138
0
abort:
4139
0
    return ret;
4140
0
}
4141
4142
static void
4143
md_analyze_link_contents(MD_CTX* ctx, const MD_LINE* lines, MD_SIZE n_lines,
4144
                         int mark_beg, int mark_end)
4145
0
{
4146
0
    int i;
4147
4148
0
    md_analyze_marks(ctx, lines, n_lines, mark_beg, mark_end, _T("&"), 0);
4149
0
    md_analyze_marks(ctx, lines, n_lines, mark_beg, mark_end, _T("*_~$"), 0);
4150
4151
0
    if((ctx->parser.flags & MD_FLAG_PERMISSIVEAUTOLINKS) != 0) {
4152
        /* These have to be processed last, as they may be greedy and expand
4153
         * from their original mark. Also their implementation must be careful
4154
         * not to cross any (previously) resolved marks when doing so. */
4155
0
        md_analyze_marks(ctx, lines, n_lines, mark_beg, mark_end, _T("@:."), MD_ANALYZE_NOSKIP_EMPH);
4156
0
    }
4157
4158
0
    for(i = 0; i < (int) SIZEOF_ARRAY(ctx->opener_stacks); i++)
4159
0
        ctx->opener_stacks[i].top = -1;
4160
0
}
4161
4162
static int
4163
md_enter_leave_span_a(MD_CTX* ctx, int enter, MD_SPANTYPE type,
4164
                      const CHAR* dest, SZ dest_size, int is_autolink,
4165
                      const CHAR* title, SZ title_size)
4166
0
{
4167
0
    MD_ATTRIBUTE_BUILD href_build = { 0 };
4168
0
    MD_ATTRIBUTE_BUILD title_build = { 0 };
4169
0
    MD_SPAN_A_DETAIL det;
4170
0
    int ret = 0;
4171
4172
    /* Note we here rely on fact that MD_SPAN_A_DETAIL and
4173
     * MD_SPAN_IMG_DETAIL are binary-compatible. */
4174
0
    memset(&det, 0, sizeof(MD_SPAN_A_DETAIL));
4175
0
    MD_CHECK(md_build_attribute(ctx, dest, dest_size,
4176
0
                    (is_autolink ? MD_BUILD_ATTR_NO_ESCAPES : 0),
4177
0
                    &det.href, &href_build));
4178
0
    MD_CHECK(md_build_attribute(ctx, title, title_size, 0, &det.title, &title_build));
4179
0
    det.is_autolink = is_autolink;
4180
0
    if(enter)
4181
0
        MD_ENTER_SPAN(type, &det);
4182
0
    else
4183
0
        MD_LEAVE_SPAN(type, &det);
4184
4185
0
abort:
4186
0
    md_free_attribute(ctx, &href_build);
4187
0
    md_free_attribute(ctx, &title_build);
4188
0
    return ret;
4189
0
}
4190
4191
static int
4192
md_enter_leave_span_wikilink(MD_CTX* ctx, int enter, const CHAR* target, SZ target_size)
4193
0
{
4194
0
    MD_ATTRIBUTE_BUILD target_build = { 0 };
4195
0
    MD_SPAN_WIKILINK_DETAIL det;
4196
0
    int ret = 0;
4197
4198
0
    memset(&det, 0, sizeof(MD_SPAN_WIKILINK_DETAIL));
4199
0
    MD_CHECK(md_build_attribute(ctx, target, target_size, 0, &det.target, &target_build));
4200
4201
0
    if (enter)
4202
0
        MD_ENTER_SPAN(MD_SPAN_WIKILINK, &det);
4203
0
    else
4204
0
        MD_LEAVE_SPAN(MD_SPAN_WIKILINK, &det);
4205
4206
0
abort:
4207
0
    md_free_attribute(ctx, &target_build);
4208
0
    return ret;
4209
0
}
4210
4211
4212
/* Render the output, accordingly to the analyzed ctx->marks. */
4213
static int
4214
md_process_inlines(MD_CTX* ctx, const MD_LINE* lines, MD_SIZE n_lines)
4215
0
{
4216
0
    MD_TEXTTYPE text_type;
4217
0
    const MD_LINE* line = lines;
4218
0
    MD_MARK* prev_mark = NULL;
4219
0
    MD_MARK* mark;
4220
0
    OFF off = lines[0].beg;
4221
0
    OFF end = lines[n_lines-1].end;
4222
0
    OFF tmp;
4223
0
    int enforce_hardbreak = 0;
4224
0
    int ret = 0;
4225
4226
    /* Find first resolved mark. Note there is always at least one resolved
4227
     * mark,  the dummy last one after the end of the latest line we actually
4228
     * never really reach. This saves us of a lot of special checks and cases
4229
     * in this function. */
4230
0
    mark = ctx->marks;
4231
0
    while(!(mark->flags & MD_MARK_RESOLVED))
4232
0
        mark++;
4233
4234
0
    text_type = MD_TEXT_NORMAL;
4235
4236
0
    while(1) {
4237
        /* Process the text up to the next mark or end-of-line. */
4238
0
        tmp = (line->end < mark->beg ? line->end : mark->beg);
4239
0
        if(tmp > off) {
4240
0
            MD_TEXT(text_type, STR(off), tmp - off);
4241
0
            off = tmp;
4242
0
        }
4243
4244
        /* If reached the mark, process it and move to next one. */
4245
0
        if(off >= mark->beg) {
4246
0
            switch(mark->ch) {
4247
0
                case '\\':      /* Backslash escape. */
4248
0
                    if(ISNEWLINE(mark->beg+1))
4249
0
                        enforce_hardbreak = 1;
4250
0
                    else
4251
0
                        MD_TEXT(text_type, STR(mark->beg+1), 1);
4252
0
                    break;
4253
4254
0
                case ' ':       /* Non-trivial space. */
4255
0
                    MD_TEXT(text_type, _T(" "), 1);
4256
0
                    break;
4257
4258
0
                case '`':       /* Code span. */
4259
0
                    if(mark->flags & MD_MARK_OPENER) {
4260
0
                        MD_ENTER_SPAN(MD_SPAN_CODE, NULL);
4261
0
                        text_type = MD_TEXT_CODE;
4262
0
                    } else {
4263
0
                        MD_LEAVE_SPAN(MD_SPAN_CODE, NULL);
4264
0
                        text_type = MD_TEXT_NORMAL;
4265
0
                    }
4266
0
                    break;
4267
4268
0
                case '_':       /* Underline (or emphasis if we fall through). */
4269
0
                    if(ctx->parser.flags & MD_FLAG_UNDERLINE) {
4270
0
                        if(mark->flags & MD_MARK_OPENER) {
4271
0
                            while(off < mark->end) {
4272
0
                                MD_ENTER_SPAN(MD_SPAN_U, NULL);
4273
0
                                off++;
4274
0
                            }
4275
0
                        } else {
4276
0
                            while(off < mark->end) {
4277
0
                                MD_LEAVE_SPAN(MD_SPAN_U, NULL);
4278
0
                                off++;
4279
0
                            }
4280
0
                        }
4281
0
                        break;
4282
0
                    }
4283
0
                    MD_FALLTHROUGH();
4284
4285
0
                case '*':       /* Emphasis, strong emphasis. */
4286
0
                    if(mark->flags & MD_MARK_OPENER) {
4287
0
                        if((mark->end - off) % 2) {
4288
0
                            MD_ENTER_SPAN(MD_SPAN_EM, NULL);
4289
0
                            off++;
4290
0
                        }
4291
0
                        while(off + 1 < mark->end) {
4292
0
                            MD_ENTER_SPAN(MD_SPAN_STRONG, NULL);
4293
0
                            off += 2;
4294
0
                        }
4295
0
                    } else {
4296
0
                        while(off + 1 < mark->end) {
4297
0
                            MD_LEAVE_SPAN(MD_SPAN_STRONG, NULL);
4298
0
                            off += 2;
4299
0
                        }
4300
0
                        if((mark->end - off) % 2) {
4301
0
                            MD_LEAVE_SPAN(MD_SPAN_EM, NULL);
4302
0
                            off++;
4303
0
                        }
4304
0
                    }
4305
0
                    break;
4306
4307
0
                case '~':
4308
0
                    if(mark->flags & MD_MARK_OPENER)
4309
0
                        MD_ENTER_SPAN(MD_SPAN_DEL, NULL);
4310
0
                    else
4311
0
                        MD_LEAVE_SPAN(MD_SPAN_DEL, NULL);
4312
0
                    break;
4313
4314
0
                case '$':
4315
0
                    if(mark->flags & MD_MARK_OPENER) {
4316
0
                        MD_ENTER_SPAN((mark->end - off) % 2 ? MD_SPAN_LATEXMATH : MD_SPAN_LATEXMATH_DISPLAY, NULL);
4317
0
                        text_type = MD_TEXT_LATEXMATH;
4318
0
                    } else {
4319
0
                        MD_LEAVE_SPAN((mark->end - off) % 2 ? MD_SPAN_LATEXMATH : MD_SPAN_LATEXMATH_DISPLAY, NULL);
4320
0
                        text_type = MD_TEXT_NORMAL;
4321
0
                    }
4322
0
                    break;
4323
4324
0
                case '[':       /* Link, wiki link, image. */
4325
0
                case '!':
4326
0
                case ']':
4327
0
                {
4328
0
                    const MD_MARK* opener = (mark->ch != ']' ? mark : &ctx->marks[mark->prev]);
4329
0
                    const MD_MARK* closer = &ctx->marks[opener->next];
4330
0
                    const MD_MARK* dest_mark;
4331
0
                    const MD_MARK* title_mark;
4332
4333
0
                    if ((opener->ch == '[' && closer->ch == ']') &&
4334
0
                        opener->end - opener->beg >= 2 &&
4335
0
                        closer->end - closer->beg >= 2)
4336
0
                    {
4337
0
                        int has_label = (opener->end - opener->beg > 2);
4338
0
                        SZ target_sz;
4339
4340
0
                        if(has_label)
4341
0
                            target_sz = opener->end - (opener->beg+2);
4342
0
                        else
4343
0
                            target_sz = closer->beg - opener->end;
4344
4345
0
                        MD_CHECK(md_enter_leave_span_wikilink(ctx, (mark->ch != ']'),
4346
0
                                 has_label ? STR(opener->beg+2) : STR(opener->end),
4347
0
                                 target_sz));
4348
4349
0
                        break;
4350
0
                    }
4351
4352
0
                    dest_mark = opener+1;
4353
0
                    MD_ASSERT(dest_mark->ch == 'D');
4354
0
                    title_mark = opener+2;
4355
0
                    MD_ASSERT(title_mark->ch == 'D');
4356
4357
0
                    MD_CHECK(md_enter_leave_span_a(ctx, (mark->ch != ']'),
4358
0
                                (opener->ch == '!' ? MD_SPAN_IMG : MD_SPAN_A),
4359
0
                                STR(dest_mark->beg), dest_mark->end - dest_mark->beg, FALSE,
4360
0
                                md_mark_get_ptr(ctx, (int)(title_mark - ctx->marks)),
4361
0
                title_mark->prev));
4362
4363
                    /* link/image closer may span multiple lines. */
4364
0
                    if(mark->ch == ']') {
4365
0
                        while(mark->end > line->end)
4366
0
                            line++;
4367
0
                    }
4368
4369
0
                    break;
4370
0
                }
4371
4372
0
                case '<':
4373
0
                case '>':       /* Autolink or raw HTML. */
4374
0
                    if(!(mark->flags & MD_MARK_AUTOLINK)) {
4375
                        /* Raw HTML. */
4376
0
                        if(mark->flags & MD_MARK_OPENER)
4377
0
                            text_type = MD_TEXT_HTML;
4378
0
                        else
4379
0
                            text_type = MD_TEXT_NORMAL;
4380
0
                        break;
4381
0
                    }
4382
                    /* Pass through, if auto-link. */
4383
0
                    MD_FALLTHROUGH();
4384
4385
0
                case '@':       /* Permissive e-mail autolink. */
4386
0
                case ':':       /* Permissive URL autolink. */
4387
0
                case '.':       /* Permissive WWW autolink. */
4388
0
                {
4389
0
                    MD_MARK* opener = ((mark->flags & MD_MARK_OPENER) ? mark : &ctx->marks[mark->prev]);
4390
0
                    MD_MARK* closer = &ctx->marks[opener->next];
4391
0
                    const CHAR* dest = STR(opener->end);
4392
0
                    SZ dest_size = closer->beg - opener->end;
4393
4394
                    /* For permissive auto-links we do not know closer mark
4395
                     * position at the time of md_collect_marks(), therefore
4396
                     * it can be out-of-order in ctx->marks[].
4397
                     *
4398
                     * With this flag, we make sure that we output the closer
4399
                     * only if we processed the opener. */
4400
0
                    if(mark->flags & MD_MARK_OPENER)
4401
0
                        closer->flags |= MD_MARK_VALIDPERMISSIVEAUTOLINK;
4402
4403
0
                    if(opener->ch == '@' || opener->ch == '.' ||
4404
0
                        (opener->ch == '<' && (opener->flags & MD_MARK_AUTOLINK_MISSING_MAILTO)))
4405
0
                    {
4406
0
                        dest_size += 7;
4407
0
                        MD_TEMP_BUFFER(dest_size * sizeof(CHAR));
4408
0
                        memcpy(ctx->buffer,
4409
0
                                (opener->ch == '.' ? _T("http://") : _T("mailto:")),
4410
0
                                7 * sizeof(CHAR));
4411
0
                        memcpy(ctx->buffer + 7, dest, (dest_size-7) * sizeof(CHAR));
4412
0
                        dest = ctx->buffer;
4413
0
                    }
4414
4415
0
                    if(closer->flags & MD_MARK_VALIDPERMISSIVEAUTOLINK)
4416
0
                        MD_CHECK(md_enter_leave_span_a(ctx, (mark->flags & MD_MARK_OPENER),
4417
0
                                    MD_SPAN_A, dest, dest_size, TRUE, NULL, 0));
4418
0
                    break;
4419
0
                }
4420
4421
0
                case '&':       /* Entity. */
4422
0
                    MD_TEXT(MD_TEXT_ENTITY, STR(mark->beg), mark->end - mark->beg);
4423
0
                    break;
4424
4425
0
                case '\0':
4426
0
                    MD_TEXT(MD_TEXT_NULLCHAR, _T(""), 1);
4427
0
                    break;
4428
4429
0
                case 127:
4430
0
                    goto abort;
4431
0
            }
4432
4433
0
            off = mark->end;
4434
4435
            /* Move to next resolved mark. */
4436
0
            prev_mark = mark;
4437
0
            mark++;
4438
0
            while(!(mark->flags & MD_MARK_RESOLVED)  ||  mark->beg < off)
4439
0
                mark++;
4440
0
        }
4441
4442
        /* If reached end of line, move to next one. */
4443
0
        if(off >= line->end) {
4444
            /* If it is the last line, we are done. */
4445
0
            if(off >= end)
4446
0
                break;
4447
4448
0
            if(text_type == MD_TEXT_CODE || text_type == MD_TEXT_LATEXMATH) {
4449
0
                MD_ASSERT(prev_mark != NULL);
4450
0
                MD_ASSERT(ISANYOF2_(prev_mark->ch, '`', '$')  &&  (prev_mark->flags & MD_MARK_OPENER));
4451
0
                MD_ASSERT(ISANYOF2_(mark->ch, '`', '$')  &&  (mark->flags & MD_MARK_CLOSER));
4452
4453
                /* Inside a code span, trailing line whitespace has to be
4454
                 * outputted. */
4455
0
                tmp = off;
4456
0
                while(off < ctx->size  &&  ISBLANK(off))
4457
0
                    off++;
4458
0
                if(off > tmp)
4459
0
                    MD_TEXT(text_type, STR(tmp), off-tmp);
4460
4461
                /* and new lines are transformed into single spaces. */
4462
0
                if(off == line->end)
4463
0
                    MD_TEXT(text_type, _T(" "), 1);
4464
0
            } else if(text_type == MD_TEXT_HTML) {
4465
                /* Inside raw HTML, we output the new line verbatim, including
4466
                 * any trailing spaces. */
4467
0
                tmp = off;
4468
0
                while(tmp < end  &&  ISBLANK(tmp))
4469
0
                    tmp++;
4470
0
                if(tmp > off)
4471
0
                    MD_TEXT(MD_TEXT_HTML, STR(off), tmp - off);
4472
0
                MD_TEXT(MD_TEXT_HTML, _T("\n"), 1);
4473
0
            } else {
4474
                /* Output soft or hard line break. */
4475
0
                MD_TEXTTYPE break_type = MD_TEXT_SOFTBR;
4476
4477
0
                if(text_type == MD_TEXT_NORMAL) {
4478
0
                    if(ctx->parser.flags & MD_FLAG_HARD_SOFT_BREAKS)
4479
0
                        break_type = MD_TEXT_BR;
4480
0
                    else if(enforce_hardbreak)
4481
0
                        break_type = MD_TEXT_BR;
4482
0
                    else if((CH(line->end) == _T(' ') && CH(line->end+1) == _T(' ')))
4483
0
                        break_type = MD_TEXT_BR;
4484
0
                }
4485
4486
0
                MD_TEXT(break_type, _T("\n"), 1);
4487
0
            }
4488
4489
            /* Move to the next line. */
4490
0
            line++;
4491
0
            off = line->beg;
4492
4493
0
            enforce_hardbreak = 0;
4494
0
        }
4495
0
    }
4496
4497
0
abort:
4498
0
    return ret;
4499
0
}
4500
4501
4502
/***************************
4503
 ***  Processing Tables  ***
4504
 ***************************/
4505
4506
static void
4507
md_analyze_table_alignment(MD_CTX* ctx, OFF beg, OFF end, MD_ALIGN* align, int n_align)
4508
0
{
4509
0
    static const MD_ALIGN align_map[] = { MD_ALIGN_DEFAULT, MD_ALIGN_LEFT, MD_ALIGN_RIGHT, MD_ALIGN_CENTER };
4510
0
    OFF off = beg;
4511
4512
0
    while(n_align > 0) {
4513
0
        int index = 0;  /* index into align_map[] */
4514
4515
0
        while(CH(off) != _T('-'))
4516
0
            off++;
4517
0
        if(off > beg  &&  CH(off-1) == _T(':'))
4518
0
            index |= 1;
4519
0
        while(off < end  &&  CH(off) == _T('-'))
4520
0
            off++;
4521
0
        if(off < end  &&  CH(off) == _T(':'))
4522
0
            index |= 2;
4523
4524
0
        *align = align_map[index];
4525
0
        align++;
4526
0
        n_align--;
4527
0
    }
4528
4529
0
}
4530
4531
/* Forward declaration. */
4532
static int md_process_normal_block_contents(MD_CTX* ctx, const MD_LINE* lines, MD_SIZE n_lines);
4533
4534
static int
4535
md_process_table_cell(MD_CTX* ctx, MD_BLOCKTYPE cell_type, MD_ALIGN align, OFF beg, OFF end)
4536
0
{
4537
0
    MD_LINE line;
4538
0
    MD_BLOCK_TD_DETAIL det;
4539
0
    int ret = 0;
4540
4541
0
    while(beg < end  &&  ISWHITESPACE(beg))
4542
0
        beg++;
4543
0
    while(end > beg  &&  ISWHITESPACE(end-1))
4544
0
        end--;
4545
4546
0
    det.align = align;
4547
0
    line.beg = beg;
4548
0
    line.end = end;
4549
4550
0
    MD_ENTER_BLOCK(cell_type, &det);
4551
0
    MD_CHECK(md_process_normal_block_contents(ctx, &line, 1));
4552
0
    MD_LEAVE_BLOCK(cell_type, &det);
4553
4554
0
abort:
4555
0
    return ret;
4556
0
}
4557
4558
static int
4559
md_process_table_row(MD_CTX* ctx, MD_BLOCKTYPE cell_type, OFF beg, OFF end,
4560
                     const MD_ALIGN* align, int col_count)
4561
0
{
4562
0
    MD_LINE line;
4563
0
    OFF* pipe_offs = NULL;
4564
0
    int i, j, k, n;
4565
0
    int ret = 0;
4566
4567
0
    line.beg = beg;
4568
0
    line.end = end;
4569
4570
    /* Break the line into table cells by identifying pipe characters who
4571
     * form the cell boundary. */
4572
0
    MD_CHECK(md_analyze_inlines(ctx, &line, 1, TRUE));
4573
4574
    /* We have to remember the cell boundaries in local buffer because
4575
     * ctx->marks[] shall be reused during cell contents processing. */
4576
0
    n = ctx->n_table_cell_boundaries + 2;
4577
0
    pipe_offs = (OFF*) malloc(n * sizeof(OFF));
4578
0
    if(pipe_offs == NULL) {
4579
0
        MD_LOG("malloc() failed.");
4580
0
        ret = -1;
4581
0
        goto abort;
4582
0
    }
4583
0
    j = 0;
4584
0
    pipe_offs[j++] = beg;
4585
0
    for(i = ctx->table_cell_boundaries_head; i >= 0; i = ctx->marks[i].next) {
4586
0
        MD_MARK* mark = &ctx->marks[i];
4587
0
        pipe_offs[j++] = mark->end;
4588
0
    }
4589
0
    pipe_offs[j++] = end+1;
4590
4591
    /* Process cells. */
4592
0
    MD_ENTER_BLOCK(MD_BLOCK_TR, NULL);
4593
0
    k = 0;
4594
0
    for(i = 0; i < j-1  &&  k < col_count; i++) {
4595
0
        if(pipe_offs[i] < pipe_offs[i+1]-1)
4596
0
            MD_CHECK(md_process_table_cell(ctx, cell_type, align[k++], pipe_offs[i], pipe_offs[i+1]-1));
4597
0
    }
4598
    /* Make sure we call enough table cells even if the current table contains
4599
     * too few of them. */
4600
0
    while(k < col_count)
4601
0
        MD_CHECK(md_process_table_cell(ctx, cell_type, align[k++], 0, 0));
4602
0
    MD_LEAVE_BLOCK(MD_BLOCK_TR, NULL);
4603
4604
0
abort:
4605
0
    free(pipe_offs);
4606
4607
0
    ctx->table_cell_boundaries_head = -1;
4608
0
    ctx->table_cell_boundaries_tail = -1;
4609
4610
0
    return ret;
4611
0
}
4612
4613
static int
4614
md_process_table_block_contents(MD_CTX* ctx, int col_count, const MD_LINE* lines, MD_SIZE n_lines)
4615
0
{
4616
0
    MD_ALIGN* align;
4617
0
    MD_SIZE line_index;
4618
0
    int ret = 0;
4619
4620
    /* At least two lines have to be present: The column headers and the line
4621
     * with the underlines. */
4622
0
    MD_ASSERT(n_lines >= 2);
4623
4624
0
    align = malloc(col_count * sizeof(MD_ALIGN));
4625
0
    if(align == NULL) {
4626
0
        MD_LOG("malloc() failed.");
4627
0
        ret = -1;
4628
0
        goto abort;
4629
0
    }
4630
4631
0
    md_analyze_table_alignment(ctx, lines[1].beg, lines[1].end, align, col_count);
4632
4633
0
    MD_ENTER_BLOCK(MD_BLOCK_THEAD, NULL);
4634
0
    MD_CHECK(md_process_table_row(ctx, MD_BLOCK_TH,
4635
0
                        lines[0].beg, lines[0].end, align, col_count));
4636
0
    MD_LEAVE_BLOCK(MD_BLOCK_THEAD, NULL);
4637
4638
0
    if(n_lines > 2) {
4639
0
        MD_ENTER_BLOCK(MD_BLOCK_TBODY, NULL);
4640
0
        for(line_index = 2; line_index < n_lines; line_index++) {
4641
0
            MD_CHECK(md_process_table_row(ctx, MD_BLOCK_TD,
4642
0
                     lines[line_index].beg, lines[line_index].end, align, col_count));
4643
0
        }
4644
0
        MD_LEAVE_BLOCK(MD_BLOCK_TBODY, NULL);
4645
0
    }
4646
4647
0
abort:
4648
0
    free(align);
4649
0
    return ret;
4650
0
}
4651
4652
4653
/**************************
4654
 ***  Processing Block  ***
4655
 **************************/
4656
4657
0
#define MD_BLOCK_CONTAINER_OPENER   0x01
4658
0
#define MD_BLOCK_CONTAINER_CLOSER   0x02
4659
0
#define MD_BLOCK_CONTAINER          (MD_BLOCK_CONTAINER_OPENER | MD_BLOCK_CONTAINER_CLOSER)
4660
0
#define MD_BLOCK_LOOSE_LIST         0x04
4661
0
#define MD_BLOCK_SETEXT_HEADER      0x08
4662
4663
struct MD_BLOCK_tag {
4664
    MD_BLOCKTYPE type  :  8;
4665
    unsigned flags     :  8;
4666
4667
    /* MD_BLOCK_H:      Header level (1 - 6)
4668
     * MD_BLOCK_CODE:   Non-zero if fenced, zero if indented.
4669
     * MD_BLOCK_LI:     Task mark character (0 if not task list item, 'x', 'X' or ' ').
4670
     * MD_BLOCK_TABLE:  Column count (as determined by the table underline).
4671
     */
4672
    unsigned data      : 16;
4673
4674
    /* Leaf blocks:     Count of lines (MD_LINE or MD_VERBATIMLINE) on the block.
4675
     * MD_BLOCK_LI:     Task mark offset in the input doc.
4676
     * MD_BLOCK_OL:     Start item number.
4677
     */
4678
    MD_SIZE n_lines;
4679
};
4680
4681
struct MD_CONTAINER_tag {
4682
    CHAR ch;
4683
    unsigned is_loose    : 8;
4684
    unsigned is_task     : 8;
4685
    unsigned start;
4686
    unsigned mark_indent;
4687
    unsigned contents_indent;
4688
    OFF block_byte_off;
4689
    OFF task_mark_off;
4690
};
4691
4692
4693
static int
4694
md_process_normal_block_contents(MD_CTX* ctx, const MD_LINE* lines, MD_SIZE n_lines)
4695
0
{
4696
0
    int i;
4697
0
    int ret;
4698
4699
0
    MD_CHECK(md_analyze_inlines(ctx, lines, n_lines, FALSE));
4700
0
    MD_CHECK(md_process_inlines(ctx, lines, n_lines));
4701
4702
0
abort:
4703
    /* Free any temporary memory blocks stored within some dummy marks. */
4704
0
    for(i = ctx->ptr_stack.top; i >= 0; i = ctx->marks[i].next)
4705
0
        free(md_mark_get_ptr(ctx, i));
4706
0
    ctx->ptr_stack.top = -1;
4707
4708
0
    return ret;
4709
0
}
4710
4711
static int
4712
md_process_verbatim_block_contents(MD_CTX* ctx, MD_TEXTTYPE text_type, const MD_VERBATIMLINE* lines, MD_SIZE n_lines)
4713
0
{
4714
0
    static const CHAR indent_chunk_str[] = _T("                ");
4715
0
    static const SZ indent_chunk_size = SIZEOF_ARRAY(indent_chunk_str) - 1;
4716
4717
0
    MD_SIZE line_index;
4718
0
    int ret = 0;
4719
4720
0
    for(line_index = 0; line_index < n_lines; line_index++) {
4721
0
        const MD_VERBATIMLINE* line = &lines[line_index];
4722
0
        int indent = line->indent;
4723
4724
0
        MD_ASSERT(indent >= 0);
4725
4726
        /* Output code indentation. */
4727
0
        while(indent > (int) indent_chunk_size) {
4728
0
            MD_TEXT(text_type, indent_chunk_str, indent_chunk_size);
4729
0
            indent -= indent_chunk_size;
4730
0
        }
4731
0
        if(indent > 0)
4732
0
            MD_TEXT(text_type, indent_chunk_str, indent);
4733
4734
        /* Output the code line itself. */
4735
0
        MD_TEXT_INSECURE(text_type, STR(line->beg), line->end - line->beg);
4736
4737
        /* Enforce end-of-line. */
4738
0
        MD_TEXT(text_type, _T("\n"), 1);
4739
0
    }
4740
4741
0
abort:
4742
0
    return ret;
4743
0
}
4744
4745
static int
4746
md_process_code_block_contents(MD_CTX* ctx, int is_fenced, const MD_VERBATIMLINE* lines, MD_SIZE n_lines)
4747
0
{
4748
0
    if(is_fenced) {
4749
        /* Skip the first line in case of fenced code: It is the fence.
4750
         * (Only the starting fence is present due to logic in md_analyze_line().) */
4751
0
        lines++;
4752
0
        n_lines--;
4753
0
    } else {
4754
        /* Ignore blank lines at start/end of indented code block. */
4755
0
        while(n_lines > 0  &&  lines[0].beg == lines[0].end) {
4756
0
            lines++;
4757
0
            n_lines--;
4758
0
        }
4759
0
        while(n_lines > 0  &&  lines[n_lines-1].beg == lines[n_lines-1].end) {
4760
0
            n_lines--;
4761
0
        }
4762
0
    }
4763
4764
0
    if(n_lines == 0)
4765
0
        return 0;
4766
4767
0
    return md_process_verbatim_block_contents(ctx, MD_TEXT_CODE, lines, n_lines);
4768
0
}
4769
4770
static int
4771
md_setup_fenced_code_detail(MD_CTX* ctx, const MD_BLOCK* block, MD_BLOCK_CODE_DETAIL* det,
4772
                            MD_ATTRIBUTE_BUILD* info_build, MD_ATTRIBUTE_BUILD* lang_build)
4773
0
{
4774
0
    const MD_VERBATIMLINE* fence_line = (const MD_VERBATIMLINE*)(block + 1);
4775
0
    OFF beg = fence_line->beg;
4776
0
    OFF end = fence_line->end;
4777
0
    OFF lang_end;
4778
0
    CHAR fence_ch = CH(fence_line->beg);
4779
0
    int ret = 0;
4780
4781
    /* Skip the fence itself. */
4782
0
    while(beg < ctx->size  &&  CH(beg) == fence_ch)
4783
0
        beg++;
4784
    /* Trim initial spaces. */
4785
0
    while(beg < ctx->size  &&  CH(beg) == _T(' '))
4786
0
        beg++;
4787
4788
    /* Trim trailing spaces. */
4789
0
    while(end > beg  &&  CH(end-1) == _T(' '))
4790
0
        end--;
4791
4792
    /* Build info string attribute. */
4793
0
    MD_CHECK(md_build_attribute(ctx, STR(beg), end - beg, 0, &det->info, info_build));
4794
4795
    /* Build info string attribute. */
4796
0
    lang_end = beg;
4797
0
    while(lang_end < end  &&  !ISWHITESPACE(lang_end))
4798
0
        lang_end++;
4799
0
    MD_CHECK(md_build_attribute(ctx, STR(beg), lang_end - beg, 0, &det->lang, lang_build));
4800
4801
0
    det->fence_char = fence_ch;
4802
4803
0
abort:
4804
0
    return ret;
4805
0
}
4806
4807
static int
4808
md_process_leaf_block(MD_CTX* ctx, const MD_BLOCK* block)
4809
0
{
4810
0
    union {
4811
0
        MD_BLOCK_H_DETAIL header;
4812
0
        MD_BLOCK_CODE_DETAIL code;
4813
0
        MD_BLOCK_TABLE_DETAIL table;
4814
0
    } det;
4815
0
    MD_ATTRIBUTE_BUILD info_build;
4816
0
    MD_ATTRIBUTE_BUILD lang_build;
4817
0
    int is_in_tight_list;
4818
0
    int clean_fence_code_detail = FALSE;
4819
0
    int ret = 0;
4820
4821
0
    memset(&det, 0, sizeof(det));
4822
4823
0
    if(ctx->n_containers == 0)
4824
0
        is_in_tight_list = FALSE;
4825
0
    else
4826
0
        is_in_tight_list = !ctx->containers[ctx->n_containers-1].is_loose;
4827
4828
0
    switch(block->type) {
4829
0
        case MD_BLOCK_H:
4830
0
            det.header.level = block->data;
4831
0
            break;
4832
4833
0
        case MD_BLOCK_CODE:
4834
            /* For fenced code block, we may need to set the info string. */
4835
0
            if(block->data != 0) {
4836
0
                memset(&det.code, 0, sizeof(MD_BLOCK_CODE_DETAIL));
4837
0
                clean_fence_code_detail = TRUE;
4838
0
                MD_CHECK(md_setup_fenced_code_detail(ctx, block, &det.code, &info_build, &lang_build));
4839
0
            }
4840
0
            break;
4841
4842
0
        case MD_BLOCK_TABLE:
4843
0
            det.table.col_count = block->data;
4844
0
            det.table.head_row_count = 1;
4845
0
            det.table.body_row_count = block->n_lines - 2;
4846
0
            break;
4847
4848
0
        default:
4849
            /* Noop. */
4850
0
            break;
4851
0
    }
4852
4853
0
    if(!is_in_tight_list  ||  block->type != MD_BLOCK_P)
4854
0
        MD_ENTER_BLOCK(block->type, (void*) &det);
4855
4856
    /* Process the block contents accordingly to is type. */
4857
0
    switch(block->type) {
4858
0
        case MD_BLOCK_HR:
4859
            /* noop */
4860
0
            break;
4861
4862
0
        case MD_BLOCK_CODE:
4863
0
            MD_CHECK(md_process_code_block_contents(ctx, (block->data != 0),
4864
0
                            (const MD_VERBATIMLINE*)(block + 1), block->n_lines));
4865
0
            break;
4866
4867
0
        case MD_BLOCK_HTML:
4868
0
            MD_CHECK(md_process_verbatim_block_contents(ctx, MD_TEXT_HTML,
4869
0
                            (const MD_VERBATIMLINE*)(block + 1), block->n_lines));
4870
0
            break;
4871
4872
0
        case MD_BLOCK_TABLE:
4873
0
            MD_CHECK(md_process_table_block_contents(ctx, block->data,
4874
0
                            (const MD_LINE*)(block + 1), block->n_lines));
4875
0
            break;
4876
4877
0
        default:
4878
0
            MD_CHECK(md_process_normal_block_contents(ctx,
4879
0
                            (const MD_LINE*)(block + 1), block->n_lines));
4880
0
            break;
4881
0
    }
4882
4883
0
    if(!is_in_tight_list  ||  block->type != MD_BLOCK_P)
4884
0
        MD_LEAVE_BLOCK(block->type, (void*) &det);
4885
4886
0
abort:
4887
0
    if(clean_fence_code_detail) {
4888
0
        md_free_attribute(ctx, &info_build);
4889
0
        md_free_attribute(ctx, &lang_build);
4890
0
    }
4891
0
    return ret;
4892
0
}
4893
4894
static int
4895
md_process_all_blocks(MD_CTX* ctx)
4896
0
{
4897
0
    int byte_off = 0;
4898
0
    int ret = 0;
4899
4900
    /* ctx->containers now is not needed for detection of lists and list items
4901
     * so we reuse it for tracking what lists are loose or tight. We rely
4902
     * on the fact the vector is large enough to hold the deepest nesting
4903
     * level of lists. */
4904
0
    ctx->n_containers = 0;
4905
4906
0
    while(byte_off < ctx->n_block_bytes) {
4907
0
        MD_BLOCK* block = (MD_BLOCK*)((char*)ctx->block_bytes + byte_off);
4908
0
        union {
4909
0
            MD_BLOCK_UL_DETAIL ul;
4910
0
            MD_BLOCK_OL_DETAIL ol;
4911
0
            MD_BLOCK_LI_DETAIL li;
4912
0
        } det;
4913
4914
0
        switch(block->type) {
4915
0
            case MD_BLOCK_UL:
4916
0
                det.ul.is_tight = (block->flags & MD_BLOCK_LOOSE_LIST) ? FALSE : TRUE;
4917
0
                det.ul.mark = (CHAR) block->data;
4918
0
                break;
4919
4920
0
            case MD_BLOCK_OL:
4921
0
                det.ol.start = block->n_lines;
4922
0
                det.ol.is_tight =  (block->flags & MD_BLOCK_LOOSE_LIST) ? FALSE : TRUE;
4923
0
                det.ol.mark_delimiter = (CHAR) block->data;
4924
0
                break;
4925
4926
0
            case MD_BLOCK_LI:
4927
0
                det.li.is_task = (block->data != 0);
4928
0
                det.li.task_mark = (CHAR) block->data;
4929
0
                det.li.task_mark_offset = (OFF) block->n_lines;
4930
0
                break;
4931
4932
0
            default:
4933
                /* noop */
4934
0
                break;
4935
0
        }
4936
4937
0
        if(block->flags & MD_BLOCK_CONTAINER) {
4938
0
            if(block->flags & MD_BLOCK_CONTAINER_CLOSER) {
4939
0
                MD_LEAVE_BLOCK(block->type, &det);
4940
4941
0
                if(block->type == MD_BLOCK_UL || block->type == MD_BLOCK_OL || block->type == MD_BLOCK_QUOTE)
4942
0
                    ctx->n_containers--;
4943
0
            }
4944
4945
0
            if(block->flags & MD_BLOCK_CONTAINER_OPENER) {
4946
0
                MD_ENTER_BLOCK(block->type, &det);
4947
4948
0
                if(block->type == MD_BLOCK_UL || block->type == MD_BLOCK_OL) {
4949
0
                    ctx->containers[ctx->n_containers].is_loose = (block->flags & MD_BLOCK_LOOSE_LIST);
4950
0
                    ctx->n_containers++;
4951
0
                } else if(block->type == MD_BLOCK_QUOTE) {
4952
                    /* This causes that any text in a block quote, even if
4953
                     * nested inside a tight list item, is wrapped with
4954
                     * <p>...</p>. */
4955
0
                    ctx->containers[ctx->n_containers].is_loose = TRUE;
4956
0
                    ctx->n_containers++;
4957
0
                }
4958
0
            }
4959
0
        } else {
4960
0
            MD_CHECK(md_process_leaf_block(ctx, block));
4961
4962
0
            if(block->type == MD_BLOCK_CODE || block->type == MD_BLOCK_HTML)
4963
0
                byte_off += block->n_lines * sizeof(MD_VERBATIMLINE);
4964
0
            else
4965
0
                byte_off += block->n_lines * sizeof(MD_LINE);
4966
0
        }
4967
4968
0
        byte_off += sizeof(MD_BLOCK);
4969
0
    }
4970
4971
0
    ctx->n_block_bytes = 0;
4972
4973
0
abort:
4974
0
    return ret;
4975
0
}
4976
4977
4978
/************************************
4979
 ***  Grouping Lines into Blocks  ***
4980
 ************************************/
4981
4982
static void*
4983
md_push_block_bytes(MD_CTX* ctx, int n_bytes)
4984
0
{
4985
0
    void* ptr;
4986
4987
0
    if(ctx->n_block_bytes + n_bytes > ctx->alloc_block_bytes) {
4988
0
        void* new_block_bytes;
4989
4990
0
        ctx->alloc_block_bytes = (ctx->alloc_block_bytes > 0
4991
0
                ? ctx->alloc_block_bytes + ctx->alloc_block_bytes / 2
4992
0
                : 512);
4993
0
        new_block_bytes = realloc(ctx->block_bytes, ctx->alloc_block_bytes);
4994
0
        if(new_block_bytes == NULL) {
4995
0
            MD_LOG("realloc() failed.");
4996
0
            return NULL;
4997
0
        }
4998
4999
        /* Fix the ->current_block after the reallocation. */
5000
0
        if(ctx->current_block != NULL) {
5001
0
            OFF off_current_block = (OFF) ((char*) ctx->current_block - (char*) ctx->block_bytes);
5002
0
            ctx->current_block = (MD_BLOCK*) ((char*) new_block_bytes + off_current_block);
5003
0
        }
5004
5005
0
        ctx->block_bytes = new_block_bytes;
5006
0
    }
5007
5008
0
    ptr = (char*)ctx->block_bytes + ctx->n_block_bytes;
5009
0
    ctx->n_block_bytes += n_bytes;
5010
0
    return ptr;
5011
0
}
5012
5013
static int
5014
md_start_new_block(MD_CTX* ctx, const MD_LINE_ANALYSIS* line)
5015
0
{
5016
0
    MD_BLOCK* block;
5017
5018
0
    MD_ASSERT(ctx->current_block == NULL);
5019
5020
0
    block = (MD_BLOCK*) md_push_block_bytes(ctx, sizeof(MD_BLOCK));
5021
0
    if(block == NULL)
5022
0
        return -1;
5023
5024
0
    switch(line->type) {
5025
0
        case MD_LINE_HR:
5026
0
            block->type = MD_BLOCK_HR;
5027
0
            break;
5028
5029
0
        case MD_LINE_ATXHEADER:
5030
0
        case MD_LINE_SETEXTHEADER:
5031
0
            block->type = MD_BLOCK_H;
5032
0
            break;
5033
5034
0
        case MD_LINE_FENCEDCODE:
5035
0
        case MD_LINE_INDENTEDCODE:
5036
0
            block->type = MD_BLOCK_CODE;
5037
0
            break;
5038
5039
0
        case MD_LINE_TEXT:
5040
0
            block->type = MD_BLOCK_P;
5041
0
            break;
5042
5043
0
        case MD_LINE_HTML:
5044
0
            block->type = MD_BLOCK_HTML;
5045
0
            break;
5046
5047
0
        case MD_LINE_BLANK:
5048
0
        case MD_LINE_SETEXTUNDERLINE:
5049
0
        case MD_LINE_TABLEUNDERLINE:
5050
0
        default:
5051
0
            MD_UNREACHABLE();
5052
0
            break;
5053
0
    }
5054
5055
0
    block->flags = 0;
5056
0
    block->data = line->data;
5057
0
    block->n_lines = 0;
5058
5059
0
    ctx->current_block = block;
5060
0
    return 0;
5061
0
}
5062
5063
/* Eat from start of current (textual) block any reference definitions and
5064
 * remember them so we can resolve any links referring to them.
5065
 *
5066
 * (Reference definitions can only be at start of it as they cannot break
5067
 * a paragraph.)
5068
 */
5069
static int
5070
md_consume_link_reference_definitions(MD_CTX* ctx)
5071
0
{
5072
0
    MD_LINE* lines = (MD_LINE*) (ctx->current_block + 1);
5073
0
    MD_SIZE n_lines = ctx->current_block->n_lines;
5074
0
    MD_SIZE n = 0;
5075
5076
    /* Compute how many lines at the start of the block form one or more
5077
     * reference definitions. */
5078
0
    while(n < n_lines) {
5079
0
        int n_link_ref_lines;
5080
5081
0
        n_link_ref_lines = md_is_link_reference_definition(ctx,
5082
0
                                    lines + n, n_lines - n);
5083
        /* Not a reference definition? */
5084
0
        if(n_link_ref_lines == 0)
5085
0
            break;
5086
5087
        /* We fail if it is the ref. def. but it could not be stored due
5088
         * a memory allocation error. */
5089
0
        if(n_link_ref_lines < 0)
5090
0
            return -1;
5091
5092
0
        n += n_link_ref_lines;
5093
0
    }
5094
5095
    /* If there was at least one reference definition, we need to remove
5096
     * its lines from the block, or perhaps even the whole block. */
5097
0
    if(n > 0) {
5098
0
        if(n == n_lines) {
5099
            /* Remove complete block. */
5100
0
            ctx->n_block_bytes -= n * sizeof(MD_LINE);
5101
0
            ctx->n_block_bytes -= sizeof(MD_BLOCK);
5102
0
            ctx->current_block = NULL;
5103
0
        } else {
5104
            /* Remove just some initial lines from the block. */
5105
0
            memmove(lines, lines + n, (n_lines - n) * sizeof(MD_LINE));
5106
0
            ctx->current_block->n_lines -= n;
5107
0
            ctx->n_block_bytes -= n * sizeof(MD_LINE);
5108
0
        }
5109
0
    }
5110
5111
0
    return 0;
5112
0
}
5113
5114
static int
5115
md_end_current_block(MD_CTX* ctx)
5116
0
{
5117
0
    int ret = 0;
5118
5119
0
    if(ctx->current_block == NULL)
5120
0
        return ret;
5121
5122
    /* Check whether there is a reference definition. (We do this here instead
5123
     * of in md_analyze_line() because reference definition can take multiple
5124
     * lines.) */
5125
0
    if(ctx->current_block->type == MD_BLOCK_P  ||
5126
0
       (ctx->current_block->type == MD_BLOCK_H  &&  (ctx->current_block->flags & MD_BLOCK_SETEXT_HEADER)))
5127
0
    {
5128
0
        MD_LINE* lines = (MD_LINE*) (ctx->current_block + 1);
5129
0
        if(lines[0].beg < ctx->size  &&  CH(lines[0].beg) == _T('[')) {
5130
0
            MD_CHECK(md_consume_link_reference_definitions(ctx));
5131
0
            if(ctx->current_block == NULL)
5132
0
                return ret;
5133
0
        }
5134
0
    }
5135
5136
0
    if(ctx->current_block->type == MD_BLOCK_H  &&  (ctx->current_block->flags & MD_BLOCK_SETEXT_HEADER)) {
5137
0
        MD_SIZE n_lines = ctx->current_block->n_lines;
5138
5139
0
        if(n_lines > 1) {
5140
            /* Get rid of the underline. */
5141
0
            ctx->current_block->n_lines--;
5142
0
            ctx->n_block_bytes -= sizeof(MD_LINE);
5143
0
        } else {
5144
            /* Only the underline has left after eating the ref. defs.
5145
             * Keep the line as beginning of a new ordinary paragraph. */
5146
0
            ctx->current_block->type = MD_BLOCK_P;
5147
0
            return 0;
5148
0
        }
5149
0
    }
5150
5151
    /* Mark we are not building any block anymore. */
5152
0
    ctx->current_block = NULL;
5153
5154
0
abort:
5155
0
    return ret;
5156
0
}
5157
5158
static int
5159
md_add_line_into_current_block(MD_CTX* ctx, const MD_LINE_ANALYSIS* analysis)
5160
0
{
5161
0
    MD_ASSERT(ctx->current_block != NULL);
5162
5163
0
    if(ctx->current_block->type == MD_BLOCK_CODE || ctx->current_block->type == MD_BLOCK_HTML) {
5164
0
        MD_VERBATIMLINE* line;
5165
5166
0
        line = (MD_VERBATIMLINE*) md_push_block_bytes(ctx, sizeof(MD_VERBATIMLINE));
5167
0
        if(line == NULL)
5168
0
            return -1;
5169
5170
0
        line->indent = analysis->indent;
5171
0
        line->beg = analysis->beg;
5172
0
        line->end = analysis->end;
5173
0
    } else {
5174
0
        MD_LINE* line;
5175
5176
0
        line = (MD_LINE*) md_push_block_bytes(ctx, sizeof(MD_LINE));
5177
0
        if(line == NULL)
5178
0
            return -1;
5179
5180
0
        line->beg = analysis->beg;
5181
0
        line->end = analysis->end;
5182
0
    }
5183
0
    ctx->current_block->n_lines++;
5184
5185
0
    return 0;
5186
0
}
5187
5188
static int
5189
md_push_container_bytes(MD_CTX* ctx, MD_BLOCKTYPE type, unsigned start,
5190
                        unsigned data, unsigned flags)
5191
0
{
5192
0
    MD_BLOCK* block;
5193
0
    int ret = 0;
5194
5195
0
    MD_CHECK(md_end_current_block(ctx));
5196
5197
0
    block = (MD_BLOCK*) md_push_block_bytes(ctx, sizeof(MD_BLOCK));
5198
0
    if(block == NULL)
5199
0
        return -1;
5200
5201
0
    block->type = type;
5202
0
    block->flags = flags;
5203
0
    block->data = data;
5204
0
    block->n_lines = start;
5205
5206
0
abort:
5207
0
    return ret;
5208
0
}
5209
5210
5211
5212
/***********************
5213
 ***  Line Analysis  ***
5214
 ***********************/
5215
5216
static int
5217
md_is_hr_line(MD_CTX* ctx, OFF beg, OFF* p_end, OFF* p_killer)
5218
0
{
5219
0
    OFF off = beg + 1;
5220
0
    int n = 1;
5221
5222
0
    while(off < ctx->size  &&  (CH(off) == CH(beg) || CH(off) == _T(' ') || CH(off) == _T('\t'))) {
5223
0
        if(CH(off) == CH(beg))
5224
0
            n++;
5225
0
        off++;
5226
0
    }
5227
5228
0
    if(n < 3) {
5229
0
        *p_killer = off;
5230
0
        return FALSE;
5231
0
    }
5232
5233
    /* Nothing else can be present on the line. */
5234
0
    if(off < ctx->size  &&  !ISNEWLINE(off)) {
5235
0
        *p_killer = off;
5236
0
        return FALSE;
5237
0
    }
5238
5239
0
    *p_end = off;
5240
0
    return TRUE;
5241
0
}
5242
5243
static int
5244
md_is_atxheader_line(MD_CTX* ctx, OFF beg, OFF* p_beg, OFF* p_end, unsigned* p_level)
5245
0
{
5246
0
    int n;
5247
0
    OFF off = beg + 1;
5248
5249
0
    while(off < ctx->size  &&  CH(off) == _T('#')  &&  off - beg < 7)
5250
0
        off++;
5251
0
    n = off - beg;
5252
5253
0
    if(n > 6)
5254
0
        return FALSE;
5255
0
    *p_level = n;
5256
5257
0
    if(!(ctx->parser.flags & MD_FLAG_PERMISSIVEATXHEADERS)  &&  off < ctx->size  &&
5258
0
       CH(off) != _T(' ')  &&  CH(off) != _T('\t')  &&  !ISNEWLINE(off))
5259
0
        return FALSE;
5260
5261
0
    while(off < ctx->size  &&  CH(off) == _T(' '))
5262
0
        off++;
5263
0
    *p_beg = off;
5264
0
    *p_end = off;
5265
0
    return TRUE;
5266
0
}
5267
5268
static int
5269
md_is_setext_underline(MD_CTX* ctx, OFF beg, OFF* p_end, unsigned* p_level)
5270
0
{
5271
0
    OFF off = beg + 1;
5272
5273
0
    while(off < ctx->size  &&  CH(off) == CH(beg))
5274
0
        off++;
5275
5276
    /* Optionally, space(s) or tabs can follow. */
5277
0
    while(off < ctx->size  &&  ISBLANK(off))
5278
0
        off++;
5279
5280
    /* But nothing more is allowed on the line. */
5281
0
    if(off < ctx->size  &&  !ISNEWLINE(off))
5282
0
        return FALSE;
5283
5284
0
    *p_level = (CH(beg) == _T('=') ? 1 : 2);
5285
0
    *p_end = off;
5286
0
    return TRUE;
5287
0
}
5288
5289
static int
5290
md_is_table_underline(MD_CTX* ctx, OFF beg, OFF* p_end, unsigned* p_col_count)
5291
0
{
5292
0
    OFF off = beg;
5293
0
    int found_pipe = FALSE;
5294
0
    unsigned col_count = 0;
5295
5296
0
    if(off < ctx->size  &&  CH(off) == _T('|')) {
5297
0
        found_pipe = TRUE;
5298
0
        off++;
5299
0
        while(off < ctx->size  &&  ISWHITESPACE(off))
5300
0
            off++;
5301
0
    }
5302
5303
0
    while(1) {
5304
0
        int delimited = FALSE;
5305
5306
        /* Cell underline ("-----", ":----", "----:" or ":----:") */
5307
0
        if(off < ctx->size  &&  CH(off) == _T(':'))
5308
0
            off++;
5309
0
        if(off >= ctx->size  ||  CH(off) != _T('-'))
5310
0
            return FALSE;
5311
0
        while(off < ctx->size  &&  CH(off) == _T('-'))
5312
0
            off++;
5313
0
        if(off < ctx->size  &&  CH(off) == _T(':'))
5314
0
            off++;
5315
5316
0
        col_count++;
5317
0
        if(col_count > TABLE_MAXCOLCOUNT) {
5318
0
            MD_LOG("Suppressing table (column_count >" STRINGIZE(TABLE_MAXCOLCOUNT) ")");
5319
0
            return FALSE;
5320
0
        }
5321
5322
        /* Pipe delimiter (optional at the end of line). */
5323
0
        while(off < ctx->size  &&  ISWHITESPACE(off))
5324
0
            off++;
5325
0
        if(off < ctx->size  &&  CH(off) == _T('|')) {
5326
0
            delimited = TRUE;
5327
0
            found_pipe =  TRUE;
5328
0
            off++;
5329
0
            while(off < ctx->size  &&  ISWHITESPACE(off))
5330
0
                off++;
5331
0
        }
5332
5333
        /* Success, if we reach end of line. */
5334
0
        if(off >= ctx->size  ||  ISNEWLINE(off))
5335
0
            break;
5336
5337
0
        if(!delimited)
5338
0
            return FALSE;
5339
0
    }
5340
5341
0
    if(!found_pipe)
5342
0
        return FALSE;
5343
5344
0
    *p_end = off;
5345
0
    *p_col_count = col_count;
5346
0
    return TRUE;
5347
0
}
5348
5349
static int
5350
md_is_opening_code_fence(MD_CTX* ctx, OFF beg, OFF* p_end)
5351
0
{
5352
0
    OFF off = beg;
5353
5354
0
    while(off < ctx->size && CH(off) == CH(beg))
5355
0
        off++;
5356
5357
    /* Fence must have at least three characters. */
5358
0
    if(off - beg < 3)
5359
0
        return FALSE;
5360
5361
0
    ctx->code_fence_length = off - beg;
5362
5363
    /* Optionally, space(s) can follow. */
5364
0
    while(off < ctx->size  &&  CH(off) == _T(' '))
5365
0
        off++;
5366
5367
    /* Optionally, an info string can follow. */
5368
0
    while(off < ctx->size  &&  !ISNEWLINE(off)) {
5369
        /* Backtick-based fence must not contain '`' in the info string. */
5370
0
        if(CH(beg) == _T('`')  &&  CH(off) == _T('`'))
5371
0
            return FALSE;
5372
0
        off++;
5373
0
    }
5374
5375
0
    *p_end = off;
5376
0
    return TRUE;
5377
0
}
5378
5379
static int
5380
md_is_closing_code_fence(MD_CTX* ctx, CHAR ch, OFF beg, OFF* p_end)
5381
0
{
5382
0
    OFF off = beg;
5383
0
    int ret = FALSE;
5384
5385
    /* Closing fence must have at least the same length and use same char as
5386
     * opening one. */
5387
0
    while(off < ctx->size  &&  CH(off) == ch)
5388
0
        off++;
5389
0
    if(off - beg < ctx->code_fence_length)
5390
0
        goto out;
5391
5392
    /* Optionally, space(s) can follow */
5393
0
    while(off < ctx->size  &&  CH(off) == _T(' '))
5394
0
        off++;
5395
5396
    /* But nothing more is allowed on the line. */
5397
0
    if(off < ctx->size  &&  !ISNEWLINE(off))
5398
0
        goto out;
5399
5400
0
    ret = TRUE;
5401
5402
0
out:
5403
    /* Note we set *p_end even on failure: If we are not closing fence, caller
5404
     * would eat the line anyway without any parsing. */
5405
0
    *p_end = off;
5406
0
    return ret;
5407
0
}
5408
5409
5410
/* Helper data for md_is_html_block_start_condition() and
5411
 * md_is_html_block_end_condition() */
5412
typedef struct TAG_tag TAG;
5413
struct TAG_tag {
5414
    const CHAR* name;
5415
    unsigned len    : 8;
5416
};
5417
5418
#ifdef X
5419
    #undef X
5420
#endif
5421
#define X(name)     { _T(name), (sizeof(name)-1) / sizeof(CHAR) }
5422
#define Xend        { NULL, 0 }
5423
5424
static const TAG t1[] = { X("pre"), X("script"), X("style"), X("textarea"), Xend };
5425
5426
static const TAG a6[] = { X("address"), X("article"), X("aside"), Xend };
5427
static const TAG b6[] = { X("base"), X("basefont"), X("blockquote"), X("body"), Xend };
5428
static const TAG c6[] = { X("caption"), X("center"), X("col"), X("colgroup"), Xend };
5429
static const TAG d6[] = { X("dd"), X("details"), X("dialog"), X("dir"),
5430
                          X("div"), X("dl"), X("dt"), Xend };
5431
static const TAG f6[] = { X("fieldset"), X("figcaption"), X("figure"), X("footer"),
5432
                          X("form"), X("frame"), X("frameset"), Xend };
5433
static const TAG h6[] = { X("h1"), X("h2"), X("h3"), X("h4"), X("h5"), X("h6"),
5434
                          X("head"), X("header"), X("hr"), X("html"), Xend };
5435
static const TAG i6[] = { X("iframe"), Xend };
5436
static const TAG l6[] = { X("legend"), X("li"), X("link"), Xend };
5437
static const TAG m6[] = { X("main"), X("menu"), X("menuitem"), Xend };
5438
static const TAG n6[] = { X("nav"), X("noframes"), Xend };
5439
static const TAG o6[] = { X("ol"), X("optgroup"), X("option"), Xend };
5440
static const TAG p6[] = { X("p"), X("param"), Xend };
5441
static const TAG s6[] = { X("search"), X("section"), X("summary"), Xend };
5442
static const TAG t6[] = { X("table"), X("tbody"), X("td"), X("tfoot"), X("th"),
5443
                          X("thead"), X("title"), X("tr"), X("track"), Xend };
5444
static const TAG u6[] = { X("ul"), Xend };
5445
static const TAG xx[] = { Xend };
5446
5447
#undef X
5448
#undef Xend
5449
5450
/* Returns type of the raw HTML block, or FALSE if it is not HTML block.
5451
 * (Refer to CommonMark specification for details about the types.)
5452
 */
5453
static int
5454
md_is_html_block_start_condition(MD_CTX* ctx, OFF beg)
5455
0
{
5456
    /* Type 6 is started by a long list of allowed tags. We use two-level
5457
     * tree to speed-up the search. */
5458
0
    static const TAG* map6[26] = {
5459
0
        a6, b6, c6, d6, xx, f6, xx, h6, i6, xx, xx, l6, m6,
5460
0
        n6, o6, p6, xx, xx, s6, t6, u6, xx, xx, xx, xx, xx
5461
0
    };
5462
0
    OFF off = beg + 1;
5463
0
    int i;
5464
5465
    /* Check for type 1: <script, <pre, or <style */
5466
0
    for(i = 0; t1[i].name != NULL; i++) {
5467
0
        if(off + t1[i].len <= ctx->size) {
5468
0
            if(md_ascii_case_eq(STR(off), t1[i].name, t1[i].len))
5469
0
                return 1;
5470
0
        }
5471
0
    }
5472
5473
    /* Check for type 2: <!-- */
5474
0
    if(off + 3 < ctx->size  &&  CH(off) == _T('!')  &&  CH(off+1) == _T('-')  &&  CH(off+2) == _T('-'))
5475
0
        return 2;
5476
5477
    /* Check for type 3: <? */
5478
0
    if(off < ctx->size  &&  CH(off) == _T('?'))
5479
0
        return 3;
5480
5481
    /* Check for type 4 or 5: <! */
5482
0
    if(off < ctx->size  &&  CH(off) == _T('!')) {
5483
        /* Check for type 4: <! followed by uppercase letter. */
5484
0
        if(off + 1 < ctx->size  &&  ISASCII(off+1))
5485
0
            return 4;
5486
5487
        /* Check for type 5: <![CDATA[ */
5488
0
        if(off + 8 < ctx->size) {
5489
0
            if(md_ascii_eq(STR(off), _T("![CDATA["), 8))
5490
0
                return 5;
5491
0
        }
5492
0
    }
5493
5494
    /* Check for type 6: Many possible starting tags listed above. */
5495
0
    if(off + 1 < ctx->size  &&  (ISALPHA(off) || (CH(off) == _T('/') && ISALPHA(off+1)))) {
5496
0
        int slot;
5497
0
        const TAG* tags;
5498
5499
0
        if(CH(off) == _T('/'))
5500
0
            off++;
5501
5502
0
        slot = (ISUPPER(off) ? CH(off) - 'A' : CH(off) - 'a');
5503
0
        tags = map6[slot];
5504
5505
0
        for(i = 0; tags[i].name != NULL; i++) {
5506
0
            if(off + tags[i].len <= ctx->size) {
5507
0
                if(md_ascii_case_eq(STR(off), tags[i].name, tags[i].len)) {
5508
0
                    OFF tmp = off + tags[i].len;
5509
0
                    if(tmp >= ctx->size)
5510
0
                        return 6;
5511
0
                    if(ISBLANK(tmp) || ISNEWLINE(tmp) || CH(tmp) == _T('>'))
5512
0
                        return 6;
5513
0
                    if(tmp+1 < ctx->size && CH(tmp) == _T('/') && CH(tmp+1) == _T('>'))
5514
0
                        return 6;
5515
0
                    break;
5516
0
                }
5517
0
            }
5518
0
        }
5519
0
    }
5520
5521
    /* Check for type 7: any COMPLETE other opening or closing tag. */
5522
0
    if(off + 1 < ctx->size) {
5523
0
        OFF end;
5524
5525
0
        if(md_is_html_tag(ctx, NULL, 0, beg, ctx->size, &end)) {
5526
            /* Only optional whitespace and new line may follow. */
5527
0
            while(end < ctx->size  &&  ISWHITESPACE(end))
5528
0
                end++;
5529
0
            if(end >= ctx->size  ||  ISNEWLINE(end))
5530
0
                return 7;
5531
0
        }
5532
0
    }
5533
5534
0
    return FALSE;
5535
0
}
5536
5537
/* Case sensitive check whether there is a substring 'what' between 'beg'
5538
 * and end of line. */
5539
static int
5540
md_line_contains(MD_CTX* ctx, OFF beg, const CHAR* what, SZ what_len, OFF* p_end)
5541
0
{
5542
0
    OFF i;
5543
0
    for(i = beg; i + what_len < ctx->size; i++) {
5544
0
        if(ISNEWLINE(i))
5545
0
            break;
5546
0
        if(memcmp(STR(i), what, what_len * sizeof(CHAR)) == 0) {
5547
0
            *p_end = i + what_len;
5548
0
            return TRUE;
5549
0
        }
5550
0
    }
5551
5552
0
    *p_end = i;
5553
0
    return FALSE;
5554
0
}
5555
5556
/* Returns type of HTML block end condition or FALSE if not an end condition.
5557
 *
5558
 * Note it fills p_end even when it is not end condition as the caller
5559
 * does not need to analyze contents of a raw HTML block.
5560
 */
5561
static int
5562
md_is_html_block_end_condition(MD_CTX* ctx, OFF beg, OFF* p_end)
5563
0
{
5564
0
    switch(ctx->html_block_type) {
5565
0
        case 1:
5566
0
        {
5567
0
            OFF off = beg;
5568
0
            int i;
5569
5570
0
            while(off+1 < ctx->size  &&  !ISNEWLINE(off)) {
5571
0
                if(CH(off) == _T('<')  &&  CH(off+1) == _T('/')) {
5572
0
                    for(i = 0; t1[i].name != NULL; i++) {
5573
0
                        if(off + 2 + t1[i].len < ctx->size) {
5574
0
                            if(md_ascii_case_eq(STR(off+2), t1[i].name, t1[i].len)  &&
5575
0
                               CH(off+2+t1[i].len) == _T('>'))
5576
0
                            {
5577
0
                                *p_end = off+2+t1[i].len+1;
5578
0
                                return TRUE;
5579
0
                            }
5580
0
                        }
5581
0
                    }
5582
0
                }
5583
0
                off++;
5584
0
            }
5585
0
            *p_end = off;
5586
0
            return FALSE;
5587
0
        }
5588
5589
0
        case 2:
5590
0
            return (md_line_contains(ctx, beg, _T("-->"), 3, p_end) ? 2 : FALSE);
5591
5592
0
        case 3:
5593
0
            return (md_line_contains(ctx, beg, _T("?>"), 2, p_end) ? 3 : FALSE);
5594
5595
0
        case 4:
5596
0
            return (md_line_contains(ctx, beg, _T(">"), 1, p_end) ? 4 : FALSE);
5597
5598
0
        case 5:
5599
0
            return (md_line_contains(ctx, beg, _T("]]>"), 3, p_end) ? 5 : FALSE);
5600
5601
0
        case 6:     /* Pass through */
5602
0
        case 7:
5603
0
            if(beg >= ctx->size  ||  ISNEWLINE(beg)) {
5604
                /* Blank line ends types 6 and 7. */
5605
0
                *p_end = beg;
5606
0
                return ctx->html_block_type;
5607
0
            }
5608
0
            return FALSE;
5609
5610
0
        default:
5611
0
            MD_UNREACHABLE();
5612
0
    }
5613
0
    return FALSE;
5614
0
}
5615
5616
5617
static int
5618
md_is_container_compatible(const MD_CONTAINER* pivot, const MD_CONTAINER* container)
5619
0
{
5620
    /* Block quote has no "items" like lists. */
5621
0
    if(container->ch == _T('>'))
5622
0
        return FALSE;
5623
5624
0
    if(container->ch != pivot->ch)
5625
0
        return FALSE;
5626
0
    if(container->mark_indent > pivot->contents_indent)
5627
0
        return FALSE;
5628
5629
0
    return TRUE;
5630
0
}
5631
5632
static int
5633
md_push_container(MD_CTX* ctx, const MD_CONTAINER* container)
5634
0
{
5635
0
    if(ctx->n_containers >= ctx->alloc_containers) {
5636
0
        MD_CONTAINER* new_containers;
5637
5638
0
        ctx->alloc_containers = (ctx->alloc_containers > 0
5639
0
                ? ctx->alloc_containers + ctx->alloc_containers / 2
5640
0
                : 16);
5641
0
        new_containers = realloc(ctx->containers, ctx->alloc_containers * sizeof(MD_CONTAINER));
5642
0
        if(new_containers == NULL) {
5643
0
            MD_LOG("realloc() failed.");
5644
0
            return -1;
5645
0
        }
5646
5647
0
        ctx->containers = new_containers;
5648
0
    }
5649
5650
0
    memcpy(&ctx->containers[ctx->n_containers++], container, sizeof(MD_CONTAINER));
5651
0
    return 0;
5652
0
}
5653
5654
static int
5655
md_enter_child_containers(MD_CTX* ctx, int n_children)
5656
0
{
5657
0
    int i;
5658
0
    int ret = 0;
5659
5660
0
    for(i = ctx->n_containers - n_children; i < ctx->n_containers; i++) {
5661
0
        MD_CONTAINER* c = &ctx->containers[i];
5662
0
        int is_ordered_list = FALSE;
5663
5664
0
        switch(c->ch) {
5665
0
            case _T(')'):
5666
0
            case _T('.'):
5667
0
                is_ordered_list = TRUE;
5668
0
                MD_FALLTHROUGH();
5669
5670
0
            case _T('-'):
5671
0
            case _T('+'):
5672
0
            case _T('*'):
5673
                /* Remember offset in ctx->block_bytes so we can revisit the
5674
                 * block if we detect it is a loose list. */
5675
0
                md_end_current_block(ctx);
5676
0
                c->block_byte_off = ctx->n_block_bytes;
5677
5678
0
                MD_CHECK(md_push_container_bytes(ctx,
5679
0
                                (is_ordered_list ? MD_BLOCK_OL : MD_BLOCK_UL),
5680
0
                                c->start, c->ch, MD_BLOCK_CONTAINER_OPENER));
5681
0
                MD_CHECK(md_push_container_bytes(ctx, MD_BLOCK_LI,
5682
0
                                c->task_mark_off,
5683
0
                                (c->is_task ? CH(c->task_mark_off) : 0),
5684
0
                                MD_BLOCK_CONTAINER_OPENER));
5685
0
                break;
5686
5687
0
            case _T('>'):
5688
0
                MD_CHECK(md_push_container_bytes(ctx, MD_BLOCK_QUOTE, 0, 0, MD_BLOCK_CONTAINER_OPENER));
5689
0
                break;
5690
5691
0
            default:
5692
0
                MD_UNREACHABLE();
5693
0
                break;
5694
0
        }
5695
0
    }
5696
5697
0
abort:
5698
0
    return ret;
5699
0
}
5700
5701
static int
5702
md_leave_child_containers(MD_CTX* ctx, int n_keep)
5703
0
{
5704
0
    int ret = 0;
5705
5706
0
    while(ctx->n_containers > n_keep) {
5707
0
        MD_CONTAINER* c = &ctx->containers[ctx->n_containers-1];
5708
0
        int is_ordered_list = FALSE;
5709
5710
0
        switch(c->ch) {
5711
0
            case _T(')'):
5712
0
            case _T('.'):
5713
0
                is_ordered_list = TRUE;
5714
0
                MD_FALLTHROUGH();
5715
5716
0
            case _T('-'):
5717
0
            case _T('+'):
5718
0
            case _T('*'):
5719
0
                MD_CHECK(md_push_container_bytes(ctx, MD_BLOCK_LI,
5720
0
                                c->task_mark_off, (c->is_task ? CH(c->task_mark_off) : 0),
5721
0
                                MD_BLOCK_CONTAINER_CLOSER));
5722
0
                MD_CHECK(md_push_container_bytes(ctx,
5723
0
                                (is_ordered_list ? MD_BLOCK_OL : MD_BLOCK_UL), 0,
5724
0
                                c->ch, MD_BLOCK_CONTAINER_CLOSER));
5725
0
                break;
5726
5727
0
            case _T('>'):
5728
0
                MD_CHECK(md_push_container_bytes(ctx, MD_BLOCK_QUOTE, 0,
5729
0
                                0, MD_BLOCK_CONTAINER_CLOSER));
5730
0
                break;
5731
5732
0
            default:
5733
0
                MD_UNREACHABLE();
5734
0
                break;
5735
0
        }
5736
5737
0
        ctx->n_containers--;
5738
0
    }
5739
5740
0
abort:
5741
0
    return ret;
5742
0
}
5743
5744
static int
5745
md_is_container_mark(MD_CTX* ctx, unsigned indent, OFF beg, OFF* p_end, MD_CONTAINER* p_container)
5746
0
{
5747
0
    OFF off = beg;
5748
0
    OFF max_end;
5749
5750
0
    if(off >= ctx->size  ||  indent >= ctx->code_indent_offset)
5751
0
        return FALSE;
5752
5753
    /* Check for block quote mark. */
5754
0
    if(CH(off) == _T('>')) {
5755
0
        off++;
5756
0
        p_container->ch = _T('>');
5757
0
        p_container->is_loose = FALSE;
5758
0
        p_container->is_task = FALSE;
5759
0
        p_container->mark_indent = indent;
5760
0
        p_container->contents_indent = indent + 1;
5761
0
        *p_end = off;
5762
0
        return TRUE;
5763
0
    }
5764
5765
    /* Check for list item bullet mark. */
5766
0
    if(ISANYOF(off, _T("-+*"))  &&  (off+1 >= ctx->size || ISBLANK(off+1) || ISNEWLINE(off+1))) {
5767
0
        p_container->ch = CH(off);
5768
0
        p_container->is_loose = FALSE;
5769
0
        p_container->is_task = FALSE;
5770
0
        p_container->mark_indent = indent;
5771
0
        p_container->contents_indent = indent + 1;
5772
0
        *p_end = off+1;
5773
0
        return TRUE;
5774
0
    }
5775
5776
    /* Check for ordered list item marks. */
5777
0
    max_end = off + 9;
5778
0
    if(max_end > ctx->size)
5779
0
        max_end = ctx->size;
5780
0
    p_container->start = 0;
5781
0
    while(off < max_end  &&  ISDIGIT(off)) {
5782
0
        p_container->start = p_container->start * 10 + CH(off) - _T('0');
5783
0
        off++;
5784
0
    }
5785
0
    if(off > beg  &&
5786
0
       off < ctx->size  &&
5787
0
       (CH(off) == _T('.') || CH(off) == _T(')'))  &&
5788
0
       (off+1 >= ctx->size || ISBLANK(off+1) || ISNEWLINE(off+1)))
5789
0
    {
5790
0
        p_container->ch = CH(off);
5791
0
        p_container->is_loose = FALSE;
5792
0
        p_container->is_task = FALSE;
5793
0
        p_container->mark_indent = indent;
5794
0
        p_container->contents_indent = indent + off - beg + 1;
5795
0
        *p_end = off+1;
5796
0
        return TRUE;
5797
0
    }
5798
5799
0
    return FALSE;
5800
0
}
5801
5802
static unsigned
5803
md_line_indentation(MD_CTX* ctx, unsigned total_indent, OFF beg, OFF* p_end)
5804
0
{
5805
0
    OFF off = beg;
5806
0
    unsigned indent = total_indent;
5807
5808
0
    while(off < ctx->size  &&  ISBLANK(off)) {
5809
0
        if(CH(off) == _T('\t'))
5810
0
            indent = (indent + 4) & ~3;
5811
0
        else
5812
0
            indent++;
5813
0
        off++;
5814
0
    }
5815
5816
0
    *p_end = off;
5817
0
    return indent - total_indent;
5818
0
}
5819
5820
static const MD_LINE_ANALYSIS md_dummy_blank_line = { MD_LINE_BLANK, 0, 0, 0, 0, 0 };
5821
5822
/* Analyze type of the line and find some its properties. This serves as a
5823
 * main input for determining type and boundaries of a block. */
5824
static int
5825
md_analyze_line(MD_CTX* ctx, OFF beg, OFF* p_end,
5826
                const MD_LINE_ANALYSIS* pivot_line, MD_LINE_ANALYSIS* line)
5827
0
{
5828
0
    unsigned total_indent = 0;
5829
0
    int n_parents = 0;
5830
0
    int n_brothers = 0;
5831
0
    int n_children = 0;
5832
0
    MD_CONTAINER container = { 0 };
5833
0
    int prev_line_has_list_loosening_effect = ctx->last_line_has_list_loosening_effect;
5834
0
    OFF off = beg;
5835
0
    OFF hr_killer = 0;
5836
0
    int ret = 0;
5837
5838
0
    line->indent = md_line_indentation(ctx, total_indent, off, &off);
5839
0
    total_indent += line->indent;
5840
0
    line->beg = off;
5841
0
    line->enforce_new_block = FALSE;
5842
5843
    /* Given the indentation and block quote marks '>', determine how many of
5844
     * the current containers are our parents. */
5845
0
    while(n_parents < ctx->n_containers) {
5846
0
        MD_CONTAINER* c = &ctx->containers[n_parents];
5847
5848
0
        if(c->ch == _T('>')  &&  line->indent < ctx->code_indent_offset  &&
5849
0
            off < ctx->size  &&  CH(off) == _T('>'))
5850
0
        {
5851
            /* Block quote mark. */
5852
0
            off++;
5853
0
            total_indent++;
5854
0
            line->indent = md_line_indentation(ctx, total_indent, off, &off);
5855
0
            total_indent += line->indent;
5856
5857
            /* The optional 1st space after '>' is part of the block quote mark. */
5858
0
            if(line->indent > 0)
5859
0
                line->indent--;
5860
5861
0
            line->beg = off;
5862
5863
0
        } else if(c->ch != _T('>')  &&  line->indent >= c->contents_indent) {
5864
            /* List. */
5865
0
            line->indent -= c->contents_indent;
5866
0
        } else {
5867
0
            break;
5868
0
        }
5869
5870
0
        n_parents++;
5871
0
    }
5872
5873
0
    if(off >= ctx->size  ||  ISNEWLINE(off)) {
5874
        /* Blank line does not need any real indentation to be nested inside
5875
         * a list. */
5876
0
        if(n_brothers + n_children == 0) {
5877
0
            while(n_parents < ctx->n_containers  &&  ctx->containers[n_parents].ch != _T('>'))
5878
0
                n_parents++;
5879
0
        }
5880
0
    }
5881
5882
0
    while(TRUE) {
5883
        /* Check whether we are fenced code continuation. */
5884
0
        if(pivot_line->type == MD_LINE_FENCEDCODE) {
5885
0
            line->beg = off;
5886
5887
            /* We are another MD_LINE_FENCEDCODE unless we are closing fence
5888
             * which we transform into MD_LINE_BLANK. */
5889
0
            if(line->indent < ctx->code_indent_offset) {
5890
0
                if(md_is_closing_code_fence(ctx, CH(pivot_line->beg), off, &off)) {
5891
0
                    line->type = MD_LINE_BLANK;
5892
0
                    ctx->last_line_has_list_loosening_effect = FALSE;
5893
0
                    break;
5894
0
                }
5895
0
            }
5896
5897
            /* Change indentation accordingly to the initial code fence. */
5898
0
            if(n_parents == ctx->n_containers) {
5899
0
                if(line->indent > pivot_line->indent)
5900
0
                    line->indent -= pivot_line->indent;
5901
0
                else
5902
0
                    line->indent = 0;
5903
5904
0
                line->type = MD_LINE_FENCEDCODE;
5905
0
                break;
5906
0
            }
5907
0
        }
5908
5909
        /* Check whether we are HTML block continuation. */
5910
0
        if(pivot_line->type == MD_LINE_HTML  &&  ctx->html_block_type > 0) {
5911
0
            if(n_parents < ctx->n_containers) {
5912
                /* HTML block is implicitly ended if the enclosing container
5913
                 * block ends. */
5914
0
                ctx->html_block_type = 0;
5915
0
            } else {
5916
0
                int html_block_type;
5917
5918
0
                html_block_type = md_is_html_block_end_condition(ctx, off, &off);
5919
0
                if(html_block_type > 0) {
5920
0
                    MD_ASSERT(html_block_type == ctx->html_block_type);
5921
5922
                    /* Make sure this is the last line of the block. */
5923
0
                    ctx->html_block_type = 0;
5924
5925
                    /* Some end conditions serve as blank lines at the same time. */
5926
0
                    if(html_block_type == 6 || html_block_type == 7) {
5927
0
                        line->type = MD_LINE_BLANK;
5928
0
                        line->indent = 0;
5929
0
                        break;
5930
0
                    }
5931
0
                }
5932
5933
0
                line->type = MD_LINE_HTML;
5934
0
                n_parents = ctx->n_containers;
5935
0
                break;
5936
0
            }
5937
0
        }
5938
5939
        /* Check for blank line. */
5940
0
        if(off >= ctx->size  ||  ISNEWLINE(off)) {
5941
0
            if(pivot_line->type == MD_LINE_INDENTEDCODE  &&  n_parents == ctx->n_containers) {
5942
0
                line->type = MD_LINE_INDENTEDCODE;
5943
0
                if(line->indent > ctx->code_indent_offset)
5944
0
                    line->indent -= ctx->code_indent_offset;
5945
0
                else
5946
0
                    line->indent = 0;
5947
0
                ctx->last_line_has_list_loosening_effect = FALSE;
5948
0
            } else {
5949
0
                line->type = MD_LINE_BLANK;
5950
0
                ctx->last_line_has_list_loosening_effect = (n_parents > 0  &&
5951
0
                        n_brothers + n_children == 0  &&
5952
0
                        ctx->containers[n_parents-1].ch != _T('>'));
5953
5954
0
    #if 1
5955
                /* See https://github.com/mity/md4c/issues/6
5956
                 *
5957
                 * This ugly checking tests we are in (yet empty) list item but
5958
                 * not its very first line (i.e. not the line with the list
5959
                 * item mark).
5960
                 *
5961
                 * If we are such a blank line, then any following non-blank
5962
                 * line which would be part of the list item actually has to
5963
                 * end the list because according to the specification, "a list
5964
                 * item can begin with at most one blank line."
5965
                 */
5966
0
                if(n_parents > 0  &&  ctx->containers[n_parents-1].ch != _T('>')  &&
5967
0
                   n_brothers + n_children == 0  &&  ctx->current_block == NULL  &&
5968
0
                   ctx->n_block_bytes > (int) sizeof(MD_BLOCK))
5969
0
                {
5970
0
                    MD_BLOCK* top_block = (MD_BLOCK*) ((char*)ctx->block_bytes + ctx->n_block_bytes - sizeof(MD_BLOCK));
5971
0
                    if(top_block->type == MD_BLOCK_LI)
5972
0
                        ctx->last_list_item_starts_with_two_blank_lines = TRUE;
5973
0
                }
5974
0
    #endif
5975
0
            }
5976
0
            break;
5977
0
        } else {
5978
0
    #if 1
5979
            /* This is the 2nd half of the hack. If the flag is set (i.e. there
5980
             * was a 2nd blank line at the beginning of the list item) and if
5981
             * we would otherwise still belong to the list item, we enforce
5982
             * the end of the list. */
5983
0
            if(ctx->last_list_item_starts_with_two_blank_lines) {
5984
0
                if(n_parents > 0  &&  n_parents == ctx->n_containers  &&
5985
0
                   ctx->containers[n_parents-1].ch != _T('>')  &&
5986
0
                   n_brothers + n_children == 0  &&  ctx->current_block == NULL  &&
5987
0
                   ctx->n_block_bytes > (int) sizeof(MD_BLOCK))
5988
0
                {
5989
0
                    MD_BLOCK* top_block = (MD_BLOCK*) ((char*)ctx->block_bytes + ctx->n_block_bytes - sizeof(MD_BLOCK));
5990
0
                    if(top_block->type == MD_BLOCK_LI) {
5991
0
                        n_parents--;
5992
5993
0
                        line->indent = total_indent;
5994
0
                        if(n_parents > 0)
5995
0
                            line->indent -= MIN(line->indent, ctx->containers[n_parents-1].contents_indent);
5996
0
                    }
5997
0
                }
5998
5999
0
                ctx->last_list_item_starts_with_two_blank_lines = FALSE;
6000
0
            }
6001
0
    #endif
6002
0
            ctx->last_line_has_list_loosening_effect = FALSE;
6003
0
        }
6004
6005
        /* Check whether we are Setext underline. */
6006
0
        if(line->indent < ctx->code_indent_offset  &&  pivot_line->type == MD_LINE_TEXT
6007
0
            &&  off < ctx->size  &&  ISANYOF2(off, _T('='), _T('-'))
6008
0
            &&  (n_parents == ctx->n_containers))
6009
0
        {
6010
0
            unsigned level;
6011
6012
0
            if(md_is_setext_underline(ctx, off, &off, &level)) {
6013
0
                line->type = MD_LINE_SETEXTUNDERLINE;
6014
0
                line->data = level;
6015
0
                break;
6016
0
            }
6017
0
        }
6018
6019
        /* Check for thematic break line. */
6020
0
        if(line->indent < ctx->code_indent_offset
6021
0
            &&  off < ctx->size  &&  off >= hr_killer
6022
0
            &&  ISANYOF(off, _T("-_*")))
6023
0
        {
6024
0
            if(md_is_hr_line(ctx, off, &off, &hr_killer)) {
6025
0
                line->type = MD_LINE_HR;
6026
0
                break;
6027
0
            }
6028
0
        }
6029
6030
        /* Check for "brother" container. I.e. whether we are another list item
6031
         * in already started list. */
6032
0
        if(n_parents < ctx->n_containers  &&  n_brothers + n_children == 0) {
6033
0
            OFF tmp;
6034
6035
0
            if(md_is_container_mark(ctx, line->indent, off, &tmp, &container)  &&
6036
0
               md_is_container_compatible(&ctx->containers[n_parents], &container))
6037
0
            {
6038
0
                pivot_line = &md_dummy_blank_line;
6039
6040
0
                off = tmp;
6041
6042
0
                total_indent += container.contents_indent - container.mark_indent;
6043
0
                line->indent = md_line_indentation(ctx, total_indent, off, &off);
6044
0
                total_indent += line->indent;
6045
0
                line->beg = off;
6046
6047
                /* Some of the following whitespace actually still belongs to the mark. */
6048
0
                if(off >= ctx->size || ISNEWLINE(off)) {
6049
0
                    container.contents_indent++;
6050
0
                } else if(line->indent <= ctx->code_indent_offset) {
6051
0
                    container.contents_indent += line->indent;
6052
0
                    line->indent = 0;
6053
0
                } else {
6054
0
                    container.contents_indent += 1;
6055
0
                    line->indent--;
6056
0
                }
6057
6058
0
                ctx->containers[n_parents].mark_indent = container.mark_indent;
6059
0
                ctx->containers[n_parents].contents_indent = container.contents_indent;
6060
6061
0
                n_brothers++;
6062
0
                continue;
6063
0
            }
6064
0
        }
6065
6066
        /* Check for indented code.
6067
         * Note indented code block cannot interrupt a paragraph. */
6068
0
        if(line->indent >= ctx->code_indent_offset  &&  (pivot_line->type != MD_LINE_TEXT)) {
6069
0
            line->type = MD_LINE_INDENTEDCODE;
6070
0
            line->indent -= ctx->code_indent_offset;
6071
0
            line->data = 0;
6072
0
            break;
6073
0
        }
6074
6075
        /* Check for start of a new container block. */
6076
0
        if(line->indent < ctx->code_indent_offset  &&
6077
0
           md_is_container_mark(ctx, line->indent, off, &off, &container))
6078
0
        {
6079
0
            if(pivot_line->type == MD_LINE_TEXT  &&  n_parents == ctx->n_containers  &&
6080
0
                        (off >= ctx->size || ISNEWLINE(off))  &&  container.ch != _T('>'))
6081
0
            {
6082
                /* Noop. List mark followed by a blank line cannot interrupt a paragraph. */
6083
0
            } else if(pivot_line->type == MD_LINE_TEXT  &&  n_parents == ctx->n_containers  &&
6084
0
                        ISANYOF2_(container.ch, _T('.'), _T(')'))  &&  container.start != 1)
6085
0
            {
6086
                /* Noop. Ordered list cannot interrupt a paragraph unless the start index is 1. */
6087
0
            } else {
6088
0
                total_indent += container.contents_indent - container.mark_indent;
6089
0
                line->indent = md_line_indentation(ctx, total_indent, off, &off);
6090
0
                total_indent += line->indent;
6091
6092
0
                line->beg = off;
6093
0
                line->data = container.ch;
6094
6095
                /* Some of the following whitespace actually still belongs to the mark. */
6096
0
                if(off >= ctx->size || ISNEWLINE(off)) {
6097
0
                    container.contents_indent++;
6098
0
                } else if(line->indent <= ctx->code_indent_offset) {
6099
0
                    container.contents_indent += line->indent;
6100
0
                    line->indent = 0;
6101
0
                } else {
6102
0
                    container.contents_indent += 1;
6103
0
                    line->indent--;
6104
0
                }
6105
6106
0
                if(n_brothers + n_children == 0)
6107
0
                    pivot_line = &md_dummy_blank_line;
6108
6109
0
                if(n_children == 0)
6110
0
                    MD_CHECK(md_leave_child_containers(ctx, n_parents + n_brothers));
6111
6112
0
                n_children++;
6113
0
                MD_CHECK(md_push_container(ctx, &container));
6114
0
                continue;
6115
0
            }
6116
0
        }
6117
6118
        /* Check whether we are table continuation. */
6119
0
        if(pivot_line->type == MD_LINE_TABLE  &&  n_parents == ctx->n_containers) {
6120
0
            line->type = MD_LINE_TABLE;
6121
0
            break;
6122
0
        }
6123
6124
        /* Check for ATX header. */
6125
0
        if(line->indent < ctx->code_indent_offset  &&
6126
0
                off < ctx->size  &&  CH(off) == _T('#'))
6127
0
        {
6128
0
            unsigned level;
6129
6130
0
            if(md_is_atxheader_line(ctx, off, &line->beg, &off, &level)) {
6131
0
                line->type = MD_LINE_ATXHEADER;
6132
0
                line->data = level;
6133
0
                break;
6134
0
            }
6135
0
        }
6136
6137
        /* Check whether we are starting code fence. */
6138
0
        if(line->indent < ctx->code_indent_offset  &&
6139
0
                off < ctx->size  &&  ISANYOF2(off, _T('`'), _T('~')))
6140
0
        {
6141
0
            if(md_is_opening_code_fence(ctx, off, &off)) {
6142
0
                line->type = MD_LINE_FENCEDCODE;
6143
0
                line->data = 1;
6144
0
                line->enforce_new_block = TRUE;
6145
0
                break;
6146
0
            }
6147
0
        }
6148
6149
        /* Check for start of raw HTML block. */
6150
0
        if(off < ctx->size  &&  CH(off) == _T('<')
6151
0
            &&  !(ctx->parser.flags & MD_FLAG_NOHTMLBLOCKS))
6152
0
        {
6153
0
            ctx->html_block_type = md_is_html_block_start_condition(ctx, off);
6154
6155
            /* HTML block type 7 cannot interrupt paragraph. */
6156
0
            if(ctx->html_block_type == 7  &&  pivot_line->type == MD_LINE_TEXT)
6157
0
                ctx->html_block_type = 0;
6158
6159
0
            if(ctx->html_block_type > 0) {
6160
                /* The line itself also may immediately close the block. */
6161
0
                if(md_is_html_block_end_condition(ctx, off, &off) == ctx->html_block_type) {
6162
                    /* Make sure this is the last line of the block. */
6163
0
                    ctx->html_block_type = 0;
6164
0
                }
6165
6166
0
                line->enforce_new_block = TRUE;
6167
0
                line->type = MD_LINE_HTML;
6168
0
                break;
6169
0
            }
6170
0
        }
6171
6172
        /* Check for table underline. */
6173
0
        if((ctx->parser.flags & MD_FLAG_TABLES)  &&  pivot_line->type == MD_LINE_TEXT
6174
0
            &&  off < ctx->size  &&  ISANYOF3(off, _T('|'), _T('-'), _T(':'))
6175
0
            &&  n_parents == ctx->n_containers)
6176
0
        {
6177
0
            unsigned col_count;
6178
6179
0
            if(ctx->current_block != NULL  &&  ctx->current_block->n_lines == 1  &&
6180
0
                md_is_table_underline(ctx, off, &off, &col_count))
6181
0
            {
6182
0
                line->data = col_count;
6183
0
                line->type = MD_LINE_TABLEUNDERLINE;
6184
0
                break;
6185
0
            }
6186
0
        }
6187
6188
        /* By default, we are normal text line. */
6189
0
        line->type = MD_LINE_TEXT;
6190
0
        if(pivot_line->type == MD_LINE_TEXT  &&  n_brothers + n_children == 0) {
6191
            /* Lazy continuation. */
6192
0
            n_parents = ctx->n_containers;
6193
0
        }
6194
6195
        /* Check for task mark. */
6196
0
        if((ctx->parser.flags & MD_FLAG_TASKLISTS)  &&  n_brothers + n_children > 0  &&
6197
0
           ISANYOF_(ctx->containers[ctx->n_containers-1].ch, _T("-+*.)")))
6198
0
        {
6199
0
            OFF tmp = off;
6200
6201
0
            while(tmp < ctx->size  &&  tmp < off + 3  &&  ISBLANK(tmp))
6202
0
                tmp++;
6203
0
            if(tmp + 2 < ctx->size  &&  CH(tmp) == _T('[')  &&
6204
0
               ISANYOF(tmp+1, _T("xX "))  &&  CH(tmp+2) == _T(']')  &&
6205
0
               (tmp + 3 == ctx->size  ||  ISBLANK(tmp+3)  ||  ISNEWLINE(tmp+3)))
6206
0
            {
6207
0
                MD_CONTAINER* task_container = (n_children > 0 ? &ctx->containers[ctx->n_containers-1] : &container);
6208
0
                task_container->is_task = TRUE;
6209
0
                task_container->task_mark_off = tmp + 1;
6210
0
                off = tmp + 3;
6211
0
                while(off < ctx->size  &&  ISWHITESPACE(off))
6212
0
                    off++;
6213
0
                line->beg = off;
6214
0
            }
6215
0
        }
6216
6217
0
        break;
6218
0
    }
6219
6220
    /* Scan for end of the line.
6221
     *
6222
     * Note this is quite a bottleneck of the parsing as we here iterate almost
6223
     * over compete document.
6224
     */
6225
0
#if defined __linux__ && !defined MD4C_USE_UTF16
6226
    /* Recent glibc versions have superbly optimized strcspn(), even using
6227
     * vectorization if available. */
6228
0
    if(ctx->doc_ends_with_newline  &&  off < ctx->size) {
6229
0
        while(TRUE) {
6230
0
            off += (OFF) strcspn(STR(off), "\r\n");
6231
6232
            /* strcspn() can stop on zero terminator; but that can appear
6233
             * anywhere in the Markfown input... */
6234
0
            if(CH(off) == _T('\0'))
6235
0
                off++;
6236
0
            else
6237
0
                break;
6238
0
        }
6239
0
    } else
6240
0
#endif
6241
0
    {
6242
        /* Optimization: Use some loop unrolling. */
6243
0
        while(off + 3 < ctx->size  &&  !ISNEWLINE(off+0)  &&  !ISNEWLINE(off+1)
6244
0
                                   &&  !ISNEWLINE(off+2)  &&  !ISNEWLINE(off+3))
6245
0
            off += 4;
6246
0
        while(off < ctx->size  &&  !ISNEWLINE(off))
6247
0
            off++;
6248
0
    }
6249
6250
    /* Set end of the line. */
6251
0
    line->end = off;
6252
6253
    /* But for ATX header, we should exclude the optional trailing mark. */
6254
0
    if(line->type == MD_LINE_ATXHEADER) {
6255
0
        OFF tmp = line->end;
6256
0
        while(tmp > line->beg && CH(tmp-1) == _T(' '))
6257
0
            tmp--;
6258
0
        while(tmp > line->beg && CH(tmp-1) == _T('#'))
6259
0
            tmp--;
6260
0
        if(tmp == line->beg || CH(tmp-1) == _T(' ') || (ctx->parser.flags & MD_FLAG_PERMISSIVEATXHEADERS))
6261
0
            line->end = tmp;
6262
0
    }
6263
6264
    /* Trim trailing spaces. */
6265
0
    if(line->type != MD_LINE_INDENTEDCODE  &&  line->type != MD_LINE_FENCEDCODE  && line->type != MD_LINE_HTML) {
6266
0
        while(line->end > line->beg && CH(line->end-1) == _T(' '))
6267
0
            line->end--;
6268
0
    }
6269
6270
    /* Eat also the new line. */
6271
0
    if(off < ctx->size && CH(off) == _T('\r'))
6272
0
        off++;
6273
0
    if(off < ctx->size && CH(off) == _T('\n'))
6274
0
        off++;
6275
6276
0
    *p_end = off;
6277
6278
    /* If we belong to a list after seeing a blank line, the list is loose. */
6279
0
    if(prev_line_has_list_loosening_effect  &&  line->type != MD_LINE_BLANK  &&  n_parents + n_brothers > 0) {
6280
0
        MD_CONTAINER* c = &ctx->containers[n_parents + n_brothers - 1];
6281
0
        if(c->ch != _T('>')) {
6282
0
            MD_BLOCK* block = (MD_BLOCK*) (((char*)ctx->block_bytes) + c->block_byte_off);
6283
0
            block->flags |= MD_BLOCK_LOOSE_LIST;
6284
0
        }
6285
0
    }
6286
6287
    /* Leave any containers we are not part of anymore. */
6288
0
    if(n_children == 0  &&  n_parents + n_brothers < ctx->n_containers)
6289
0
        MD_CHECK(md_leave_child_containers(ctx, n_parents + n_brothers));
6290
6291
    /* Enter any container we found a mark for. */
6292
0
    if(n_brothers > 0) {
6293
0
        MD_ASSERT(n_brothers == 1);
6294
0
        MD_CHECK(md_push_container_bytes(ctx, MD_BLOCK_LI,
6295
0
                    ctx->containers[n_parents].task_mark_off,
6296
0
                    (ctx->containers[n_parents].is_task ? CH(ctx->containers[n_parents].task_mark_off) : 0),
6297
0
                    MD_BLOCK_CONTAINER_CLOSER));
6298
0
        MD_CHECK(md_push_container_bytes(ctx, MD_BLOCK_LI,
6299
0
                    container.task_mark_off,
6300
0
                    (container.is_task ? CH(container.task_mark_off) : 0),
6301
0
                    MD_BLOCK_CONTAINER_OPENER));
6302
0
        ctx->containers[n_parents].is_task = container.is_task;
6303
0
        ctx->containers[n_parents].task_mark_off = container.task_mark_off;
6304
0
    }
6305
6306
0
    if(n_children > 0)
6307
0
        MD_CHECK(md_enter_child_containers(ctx, n_children));
6308
6309
0
abort:
6310
0
    return ret;
6311
0
}
6312
6313
static int
6314
md_process_line(MD_CTX* ctx, const MD_LINE_ANALYSIS** p_pivot_line, MD_LINE_ANALYSIS* line)
6315
0
{
6316
0
    const MD_LINE_ANALYSIS* pivot_line = *p_pivot_line;
6317
0
    int ret = 0;
6318
6319
    /* Blank line ends current leaf block. */
6320
0
    if(line->type == MD_LINE_BLANK) {
6321
0
        MD_CHECK(md_end_current_block(ctx));
6322
0
        *p_pivot_line = &md_dummy_blank_line;
6323
0
        return 0;
6324
0
    }
6325
6326
0
    if(line->enforce_new_block)
6327
0
        MD_CHECK(md_end_current_block(ctx));
6328
6329
    /* Some line types form block on their own. */
6330
0
    if(line->type == MD_LINE_HR || line->type == MD_LINE_ATXHEADER) {
6331
0
        MD_CHECK(md_end_current_block(ctx));
6332
6333
        /* Add our single-line block. */
6334
0
        MD_CHECK(md_start_new_block(ctx, line));
6335
0
        MD_CHECK(md_add_line_into_current_block(ctx, line));
6336
0
        MD_CHECK(md_end_current_block(ctx));
6337
0
        *p_pivot_line = &md_dummy_blank_line;
6338
0
        return 0;
6339
0
    }
6340
6341
    /* MD_LINE_SETEXTUNDERLINE changes meaning of the current block and ends it. */
6342
0
    if(line->type == MD_LINE_SETEXTUNDERLINE) {
6343
0
        MD_ASSERT(ctx->current_block != NULL);
6344
0
        ctx->current_block->type = MD_BLOCK_H;
6345
0
        ctx->current_block->data = line->data;
6346
0
        ctx->current_block->flags |= MD_BLOCK_SETEXT_HEADER;
6347
0
        MD_CHECK(md_add_line_into_current_block(ctx, line));
6348
0
        MD_CHECK(md_end_current_block(ctx));
6349
0
        if(ctx->current_block == NULL) {
6350
0
            *p_pivot_line = &md_dummy_blank_line;
6351
0
        } else {
6352
            /* This happens if we have consumed all the body as link ref. defs.
6353
             * and downgraded the underline into start of a new paragraph block. */
6354
0
            line->type = MD_LINE_TEXT;
6355
0
            *p_pivot_line = line;
6356
0
        }
6357
0
        return 0;
6358
0
    }
6359
6360
    /* MD_LINE_TABLEUNDERLINE changes meaning of the current block. */
6361
0
    if(line->type == MD_LINE_TABLEUNDERLINE) {
6362
0
        MD_ASSERT(ctx->current_block != NULL);
6363
0
        MD_ASSERT(ctx->current_block->n_lines == 1);
6364
0
        ctx->current_block->type = MD_BLOCK_TABLE;
6365
0
        ctx->current_block->data = line->data;
6366
0
        MD_ASSERT(pivot_line != &md_dummy_blank_line);
6367
0
        ((MD_LINE_ANALYSIS*)pivot_line)->type = MD_LINE_TABLE;
6368
0
        MD_CHECK(md_add_line_into_current_block(ctx, line));
6369
0
        return 0;
6370
0
    }
6371
6372
    /* The current block also ends if the line has different type. */
6373
0
    if(line->type != pivot_line->type)
6374
0
        MD_CHECK(md_end_current_block(ctx));
6375
6376
    /* The current line may start a new block. */
6377
0
    if(ctx->current_block == NULL) {
6378
0
        MD_CHECK(md_start_new_block(ctx, line));
6379
0
        *p_pivot_line = line;
6380
0
    }
6381
6382
    /* In all other cases the line is just a continuation of the current block. */
6383
0
    MD_CHECK(md_add_line_into_current_block(ctx, line));
6384
6385
0
abort:
6386
0
    return ret;
6387
0
}
6388
6389
static int
6390
md_process_doc(MD_CTX *ctx)
6391
0
{
6392
0
    const MD_LINE_ANALYSIS* pivot_line = &md_dummy_blank_line;
6393
0
    MD_LINE_ANALYSIS line_buf[2];
6394
0
    MD_LINE_ANALYSIS* line = &line_buf[0];
6395
0
    OFF off = 0;
6396
0
    int ret = 0;
6397
6398
0
    MD_ENTER_BLOCK(MD_BLOCK_DOC, NULL);
6399
6400
0
    while(off < ctx->size) {
6401
0
        if(line == pivot_line)
6402
0
            line = (line == &line_buf[0] ? &line_buf[1] : &line_buf[0]);
6403
6404
0
        MD_CHECK(md_analyze_line(ctx, off, &off, pivot_line, line));
6405
0
        MD_CHECK(md_process_line(ctx, &pivot_line, line));
6406
0
    }
6407
6408
0
    md_end_current_block(ctx);
6409
6410
0
    MD_CHECK(md_build_ref_def_hashtable(ctx));
6411
6412
    /* Process all blocks. */
6413
0
    MD_CHECK(md_leave_child_containers(ctx, 0));
6414
0
    MD_CHECK(md_process_all_blocks(ctx));
6415
6416
0
    MD_LEAVE_BLOCK(MD_BLOCK_DOC, NULL);
6417
6418
0
abort:
6419
6420
#if 0
6421
    /* Output some memory consumption statistics. */
6422
    {
6423
        char buffer[256];
6424
        sprintf(buffer, "Alloced %u bytes for block buffer.",
6425
                    (unsigned)(ctx->alloc_block_bytes));
6426
        MD_LOG(buffer);
6427
6428
        sprintf(buffer, "Alloced %u bytes for containers buffer.",
6429
                    (unsigned)(ctx->alloc_containers * sizeof(MD_CONTAINER)));
6430
        MD_LOG(buffer);
6431
6432
        sprintf(buffer, "Alloced %u bytes for marks buffer.",
6433
                    (unsigned)(ctx->alloc_marks * sizeof(MD_MARK)));
6434
        MD_LOG(buffer);
6435
6436
        sprintf(buffer, "Alloced %u bytes for aux. buffer.",
6437
                    (unsigned)(ctx->alloc_buffer * sizeof(MD_CHAR)));
6438
        MD_LOG(buffer);
6439
    }
6440
#endif
6441
6442
0
    return ret;
6443
0
}
6444
6445
6446
/********************
6447
 ***  Public API  ***
6448
 ********************/
6449
6450
int
6451
md_parse(const MD_CHAR* text, MD_SIZE size, const MD_PARSER* parser, void* userdata)
6452
0
{
6453
0
    MD_CTX ctx;
6454
0
    int i;
6455
0
    int ret;
6456
6457
0
    if(parser->abi_version != 0) {
6458
0
        if(parser->debug_log != NULL)
6459
0
            parser->debug_log("Unsupported abi_version.", userdata);
6460
0
        return -1;
6461
0
    }
6462
6463
    /* Setup context structure. */
6464
0
    memset(&ctx, 0, sizeof(MD_CTX));
6465
0
    ctx.text = text;
6466
0
    ctx.size = size;
6467
0
    memcpy(&ctx.parser, parser, sizeof(MD_PARSER));
6468
0
    ctx.userdata = userdata;
6469
0
    ctx.code_indent_offset = (ctx.parser.flags & MD_FLAG_NOINDENTEDCODEBLOCKS) ? (OFF)(-1) : 4;
6470
0
    md_build_mark_char_map(&ctx);
6471
0
    ctx.doc_ends_with_newline = (size > 0  &&  ISNEWLINE_(text[size-1]));
6472
6473
    /* Reset all mark stacks and lists. */
6474
0
    for(i = 0; i < (int) SIZEOF_ARRAY(ctx.opener_stacks); i++)
6475
0
        ctx.opener_stacks[i].top = -1;
6476
0
    ctx.ptr_stack.top = -1;
6477
0
    ctx.unresolved_link_head = -1;
6478
0
    ctx.unresolved_link_tail = -1;
6479
0
    ctx.table_cell_boundaries_head = -1;
6480
0
    ctx.table_cell_boundaries_tail = -1;
6481
6482
    /* All the work. */
6483
0
    ret = md_process_doc(&ctx);
6484
6485
    /* Clean-up. */
6486
0
    md_free_ref_defs(&ctx);
6487
0
    md_free_ref_def_hashtable(&ctx);
6488
0
    free(ctx.buffer);
6489
0
    free(ctx.marks);
6490
0
    free(ctx.block_bytes);
6491
0
    free(ctx.containers);
6492
6493
0
    return ret;
6494
0
}