commonmarker 0.9.1 → 0.9.2
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of commonmarker might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/README.md +1 -0
- data/Rakefile +9 -9
- data/ext/commonmarker/cmark/api_test/main.c +5 -0
- data/ext/commonmarker/cmark/build/CMakeCache.txt +459 -0
- data/ext/commonmarker/cmark/build/CMakeFiles/3.5.2/CMakeCCompiler.cmake +67 -0
- data/ext/commonmarker/cmark/build/CMakeFiles/3.5.2/CMakeCXXCompiler.cmake +68 -0
- data/ext/commonmarker/cmark/build/CMakeFiles/3.5.2/CMakeDetermineCompilerABI_C.bin +0 -0
- data/ext/commonmarker/cmark/build/CMakeFiles/3.5.2/CMakeDetermineCompilerABI_CXX.bin +0 -0
- data/ext/commonmarker/cmark/build/CMakeFiles/3.5.2/CMakeSystem.cmake +15 -0
- data/ext/commonmarker/cmark/build/CMakeFiles/3.5.2/CompilerIdC/CMakeCCompilerId.c +544 -0
- data/ext/commonmarker/cmark/build/CMakeFiles/3.5.2/CompilerIdC/a.out +0 -0
- data/ext/commonmarker/cmark/build/CMakeFiles/3.5.2/CompilerIdCXX/CMakeCXXCompilerId.cpp +533 -0
- data/ext/commonmarker/cmark/build/CMakeFiles/3.5.2/CompilerIdCXX/a.out +0 -0
- data/ext/commonmarker/cmark/build/CMakeFiles/CMakeDirectoryInformation.cmake +16 -0
- data/ext/commonmarker/cmark/build/CMakeFiles/CMakeError.log +14 -0
- data/ext/commonmarker/cmark/build/CMakeFiles/CMakeOutput.log +562 -0
- data/ext/commonmarker/cmark/build/CMakeFiles/Makefile.cmake +150 -0
- data/ext/commonmarker/cmark/build/CMakeFiles/Makefile2 +295 -0
- data/ext/commonmarker/cmark/build/CMakeFiles/TargetDirectories.txt +39 -0
- data/ext/commonmarker/cmark/build/CMakeFiles/cmake.check_cache +1 -0
- data/ext/commonmarker/cmark/build/CMakeFiles/feature_tests.bin +0 -0
- data/ext/commonmarker/cmark/build/CMakeFiles/feature_tests.c +34 -0
- data/ext/commonmarker/cmark/build/CMakeFiles/feature_tests.cxx +405 -0
- data/ext/commonmarker/cmark/build/CMakeFiles/progress.marks +1 -0
- data/ext/commonmarker/cmark/build/CTestTestfile.cmake +10 -0
- data/ext/commonmarker/cmark/build/Makefile +250 -0
- data/ext/commonmarker/cmark/build/api_test/CMakeFiles/CMakeDirectoryInformation.cmake +16 -0
- data/ext/commonmarker/cmark/build/api_test/CMakeFiles/api_test.dir/DependInfo.cmake +35 -0
- data/ext/commonmarker/cmark/build/api_test/CMakeFiles/api_test.dir/build.make +168 -0
- data/ext/commonmarker/cmark/build/api_test/CMakeFiles/api_test.dir/cmake_clean.cmake +12 -0
- data/ext/commonmarker/cmark/build/api_test/CMakeFiles/api_test.dir/depend.make +2 -0
- data/ext/commonmarker/cmark/build/api_test/CMakeFiles/api_test.dir/flags.make +17 -0
- data/ext/commonmarker/cmark/build/api_test/CMakeFiles/api_test.dir/link.txt +1 -0
- data/ext/commonmarker/cmark/build/api_test/CMakeFiles/api_test.dir/progress.make +5 -0
- data/ext/commonmarker/cmark/build/api_test/CMakeFiles/progress.marks +1 -0
- data/ext/commonmarker/cmark/build/api_test/Makefile +300 -0
- data/ext/commonmarker/cmark/build/api_test/cmake_install.cmake +29 -0
- data/ext/commonmarker/cmark/build/cmake_install.cmake +48 -0
- data/ext/commonmarker/cmark/build/man/CMakeFiles/CMakeDirectoryInformation.cmake +16 -0
- data/ext/commonmarker/cmark/build/man/CMakeFiles/progress.marks +1 -0
- data/ext/commonmarker/cmark/build/man/Makefile +194 -0
- data/ext/commonmarker/cmark/build/man/cmake_install.cmake +37 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/CMakeDirectoryInformation.cmake +16 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/cmark.dir/DependInfo.cmake +41 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/cmark.dir/build.make +626 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/cmark.dir/cmake_clean.cmake +29 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/cmark.dir/depend.make +2 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/cmark.dir/flags.make +10 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/cmark.dir/link.txt +1 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/cmark.dir/progress.make +22 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark.dir/DependInfo.cmake +46 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark.dir/build.make +603 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark.dir/cmake_clean.cmake +29 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark.dir/depend.make +2 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark.dir/flags.make +10 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark.dir/link.txt +1 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark.dir/progress.make +21 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/C.includecache +468 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/DependInfo.cmake +40 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/blocks.c.o +0 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/buffer.c.o +0 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/build.make +600 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/cmake_clean.cmake +28 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/cmake_clean_target.cmake +3 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/cmark.c.o +0 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/cmark_ctype.c.o +0 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/commonmark.c.o +0 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/depend.internal +211 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/depend.make +211 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/flags.make +10 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/houdini_href_e.c.o +0 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/houdini_html_e.c.o +0 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/houdini_html_u.c.o +0 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/html.c.o +0 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/inlines.c.o +0 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/iterator.c.o +0 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/latex.c.o +0 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/link.txt +2 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/man.c.o +0 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/node.c.o +0 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/progress.make +21 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/references.c.o +0 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/render.c.o +0 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/scanners.c.o +0 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/utf8.c.o +0 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/xml.c.o +0 -0
- data/ext/commonmarker/cmark/build/src/CMakeFiles/progress.marks +1 -0
- data/ext/commonmarker/cmark/build/src/Makefile +956 -0
- data/ext/commonmarker/cmark/build/src/cmake_install.cmake +77 -0
- data/ext/commonmarker/cmark/build/src/cmark_export.h +41 -0
- data/ext/commonmarker/cmark/build/src/cmark_version.h +7 -0
- data/ext/commonmarker/cmark/build/src/config.h +84 -0
- data/ext/commonmarker/cmark/build/src/libcmark.a +0 -0
- data/ext/commonmarker/cmark/build/src/libcmark.pc +10 -0
- data/ext/commonmarker/cmark/build/testdir/CMakeFiles/CMakeDirectoryInformation.cmake +16 -0
- data/ext/commonmarker/cmark/build/testdir/CMakeFiles/progress.marks +1 -0
- data/ext/commonmarker/cmark/build/testdir/CTestTestfile.cmake +14 -0
- data/ext/commonmarker/cmark/build/testdir/Makefile +194 -0
- data/ext/commonmarker/cmark/build/testdir/cmake_install.cmake +29 -0
- data/ext/commonmarker/cmark/man/man1/cmark.1 +11 -4
- data/ext/commonmarker/cmark/man/man3/cmark.3 +66 -11
- data/ext/commonmarker/cmark/src/blocks.c +232 -235
- data/ext/commonmarker/cmark/src/buffer.c +19 -56
- data/ext/commonmarker/cmark/src/buffer.h +7 -20
- data/ext/commonmarker/cmark/src/chunk.h +19 -19
- data/ext/commonmarker/cmark/src/cmark.c +14 -0
- data/ext/commonmarker/cmark/src/cmark.h +45 -6
- data/ext/commonmarker/cmark/src/cmark_ctype.c +2 -0
- data/ext/commonmarker/cmark/src/cmark_ctype.h +2 -0
- data/ext/commonmarker/cmark/src/commonmark.c +42 -29
- data/ext/commonmarker/cmark/src/config.h.in +8 -0
- data/ext/commonmarker/cmark/src/html.c +3 -1
- data/ext/commonmarker/cmark/src/inlines.c +111 -150
- data/ext/commonmarker/cmark/src/inlines.h +4 -4
- data/ext/commonmarker/cmark/src/iterator.c +6 -7
- data/ext/commonmarker/cmark/src/iterator.h +2 -0
- data/ext/commonmarker/cmark/src/latex.c +10 -8
- data/ext/commonmarker/cmark/src/main.c +6 -2
- data/ext/commonmarker/cmark/src/man.c +9 -5
- data/ext/commonmarker/cmark/src/node.c +38 -29
- data/ext/commonmarker/cmark/src/node.h +15 -11
- data/ext/commonmarker/cmark/src/parser.h +4 -2
- data/ext/commonmarker/cmark/src/references.c +23 -22
- data/ext/commonmarker/cmark/src/references.h +3 -1
- data/ext/commonmarker/cmark/src/render.c +9 -7
- data/ext/commonmarker/cmark/src/render.h +3 -1
- data/ext/commonmarker/cmark/src/scanners.c +30 -22
- data/ext/commonmarker/cmark/src/xml.c +1 -1
- data/ext/commonmarker/cmark/test/CMakeLists.txt +8 -8
- data/ext/commonmarker/cmark/test/cmark.py +34 -14
- data/ext/commonmarker/cmark/test/roundtrip_tests.py +47 -0
- data/ext/commonmarker/cmark/test/spec.txt +96 -6
- data/ext/commonmarker/cmark/test/spec_tests.py +5 -8
- data/ext/commonmarker/commonmarker.c +14 -8
- data/lib/commonmarker/config.rb +2 -2
- data/lib/commonmarker/renderer.rb +17 -7
- data/lib/commonmarker/renderer/html_renderer.rb +16 -21
- data/lib/commonmarker/version.rb +1 -1
- data/test/test_pathological_inputs.rb +11 -11
- metadata +99 -4
- data/ext/commonmarker/cmark/test/roundtrip.bat +0 -1
- data/ext/commonmarker/cmark/test/roundtrip.sh +0 -2
@@ -5,10 +5,12 @@
|
|
5
5
|
#include <stdio.h>
|
6
6
|
#include <stdlib.h>
|
7
7
|
#include <stdint.h>
|
8
|
+
#include <limits.h>
|
8
9
|
|
9
10
|
#include "config.h"
|
10
11
|
#include "cmark_ctype.h"
|
11
12
|
#include "buffer.h"
|
13
|
+
#include "memory.h"
|
12
14
|
|
13
15
|
/* Used as default value for cmark_strbuf->ptr so that people can always
|
14
16
|
* assume ptr is non-NULL and zero terminated even for new cmark_strbufs.
|
@@ -19,7 +21,8 @@ unsigned char cmark_strbuf__initbuf[1];
|
|
19
21
|
#define MIN(x, y) ((x < y) ? x : y)
|
20
22
|
#endif
|
21
23
|
|
22
|
-
void cmark_strbuf_init(cmark_strbuf *buf, bufsize_t initial_size) {
|
24
|
+
void cmark_strbuf_init(cmark_mem *mem, cmark_strbuf *buf, bufsize_t initial_size) {
|
25
|
+
buf->mem = mem;
|
23
26
|
buf->asize = 0;
|
24
27
|
buf->size = 0;
|
25
28
|
buf->ptr = cmark_strbuf__initbuf;
|
@@ -28,68 +31,28 @@ void cmark_strbuf_init(cmark_strbuf *buf, bufsize_t initial_size) {
|
|
28
31
|
cmark_strbuf_grow(buf, initial_size);
|
29
32
|
}
|
30
33
|
|
31
|
-
void
|
32
|
-
|
33
|
-
abort();
|
34
|
-
}
|
35
|
-
|
36
|
-
static CMARK_INLINE void S_strbuf_grow_by(cmark_strbuf *buf, size_t add) {
|
37
|
-
size_t target_size = (size_t)buf->size + add;
|
38
|
-
|
39
|
-
if (target_size < add /* Integer overflow. */
|
40
|
-
|| target_size > BUFSIZE_MAX /* Truncation overflow. */
|
41
|
-
) {
|
42
|
-
cmark_strbuf_overflow_err();
|
43
|
-
return; /* unreachable */
|
44
|
-
}
|
45
|
-
|
46
|
-
if ((bufsize_t)target_size >= buf->asize)
|
47
|
-
cmark_strbuf_grow(buf, (bufsize_t)target_size);
|
34
|
+
static CMARK_INLINE void S_strbuf_grow_by(cmark_strbuf *buf, bufsize_t add) {
|
35
|
+
cmark_strbuf_grow(buf, buf->size + add);
|
48
36
|
}
|
49
37
|
|
50
38
|
void cmark_strbuf_grow(cmark_strbuf *buf, bufsize_t target_size) {
|
51
|
-
|
39
|
+
assert(target_size > 0);
|
52
40
|
|
53
41
|
if (target_size < buf->asize)
|
54
42
|
return;
|
55
43
|
|
56
|
-
if (
|
57
|
-
|
58
|
-
} else {
|
59
|
-
new_ptr = buf->ptr;
|
60
|
-
}
|
44
|
+
if (target_size > (bufsize_t)(INT32_MAX / 2))
|
45
|
+
abort();
|
61
46
|
|
62
47
|
/* Oversize the buffer by 50% to guarantee amortized linear time
|
63
48
|
* complexity on append operations. */
|
64
|
-
|
65
|
-
|
66
|
-
/* Account for terminating null byte. */
|
49
|
+
bufsize_t new_size = target_size + target_size / 2;
|
67
50
|
new_size += 1;
|
68
|
-
|
69
|
-
/* round allocation up to multiple of 8 */
|
70
51
|
new_size = (new_size + 7) & ~7;
|
71
52
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
if (target_size >= BUFSIZE_MAX) {
|
76
|
-
/* No space for terminating null byte. */
|
77
|
-
cmark_strbuf_overflow_err();
|
78
|
-
return; /* unreachable */
|
79
|
-
}
|
80
|
-
/* Oversize by the maximum possible amount. */
|
81
|
-
new_size = BUFSIZE_MAX;
|
82
|
-
}
|
83
|
-
|
84
|
-
new_ptr = (unsigned char *)realloc(new_ptr, new_size);
|
85
|
-
|
86
|
-
if (!new_ptr) {
|
87
|
-
perror("realloc in cmark_strbuf_grow");
|
88
|
-
abort();
|
89
|
-
}
|
90
|
-
|
91
|
-
buf->asize = (bufsize_t)new_size;
|
92
|
-
buf->ptr = new_ptr;
|
53
|
+
buf->ptr = (unsigned char *)buf->mem->realloc(
|
54
|
+
buf->asize ? buf->ptr : NULL, new_size);
|
55
|
+
buf->asize = new_size;
|
93
56
|
}
|
94
57
|
|
95
58
|
bufsize_t cmark_strbuf_len(const cmark_strbuf *buf) { return buf->size; }
|
@@ -99,9 +62,9 @@ void cmark_strbuf_free(cmark_strbuf *buf) {
|
|
99
62
|
return;
|
100
63
|
|
101
64
|
if (buf->ptr != cmark_strbuf__initbuf)
|
102
|
-
free(buf->ptr);
|
65
|
+
buf->mem->free(buf->ptr);
|
103
66
|
|
104
|
-
cmark_strbuf_init(buf, 0);
|
67
|
+
cmark_strbuf_init(buf->mem, buf, 0);
|
105
68
|
}
|
106
69
|
|
107
70
|
void cmark_strbuf_clear(cmark_strbuf *buf) {
|
@@ -128,7 +91,7 @@ void cmark_strbuf_set(cmark_strbuf *buf, const unsigned char *data,
|
|
128
91
|
|
129
92
|
void cmark_strbuf_sets(cmark_strbuf *buf, const char *string) {
|
130
93
|
cmark_strbuf_set(buf, (const unsigned char *)string,
|
131
|
-
string ?
|
94
|
+
string ? strlen(string) : 0);
|
132
95
|
}
|
133
96
|
|
134
97
|
void cmark_strbuf_putc(cmark_strbuf *buf, int c) {
|
@@ -150,7 +113,7 @@ void cmark_strbuf_put(cmark_strbuf *buf, const unsigned char *data,
|
|
150
113
|
|
151
114
|
void cmark_strbuf_puts(cmark_strbuf *buf, const char *string) {
|
152
115
|
cmark_strbuf_put(buf, (const unsigned char *)string,
|
153
|
-
|
116
|
+
strlen(string));
|
154
117
|
}
|
155
118
|
|
156
119
|
void cmark_strbuf_copy_cstr(char *data, bufsize_t datasize,
|
@@ -184,10 +147,10 @@ unsigned char *cmark_strbuf_detach(cmark_strbuf *buf) {
|
|
184
147
|
|
185
148
|
if (buf->asize == 0) {
|
186
149
|
/* return an empty string */
|
187
|
-
return (unsigned char *)calloc(1, 1);
|
150
|
+
return (unsigned char *)buf->mem->calloc(1, 1);
|
188
151
|
}
|
189
152
|
|
190
|
-
cmark_strbuf_init(buf, 0);
|
153
|
+
cmark_strbuf_init(buf->mem, buf, 0);
|
191
154
|
return data;
|
192
155
|
}
|
193
156
|
|
@@ -5,32 +5,33 @@
|
|
5
5
|
#include <stdarg.h>
|
6
6
|
#include <string.h>
|
7
7
|
#include <limits.h>
|
8
|
+
#include <stdint.h>
|
8
9
|
#include "config.h"
|
10
|
+
#include "cmark.h"
|
9
11
|
|
10
12
|
#ifdef __cplusplus
|
11
13
|
extern "C" {
|
12
14
|
#endif
|
13
15
|
|
14
|
-
typedef
|
16
|
+
typedef int32_t bufsize_t;
|
15
17
|
|
16
18
|
typedef struct {
|
19
|
+
cmark_mem *mem;
|
17
20
|
unsigned char *ptr;
|
18
21
|
bufsize_t asize, size;
|
19
22
|
} cmark_strbuf;
|
20
23
|
|
21
24
|
extern unsigned char cmark_strbuf__initbuf[];
|
22
25
|
|
23
|
-
#define
|
24
|
-
{ cmark_strbuf__initbuf, 0, 0 }
|
25
|
-
#define BUFSIZE_MAX INT_MAX
|
26
|
+
#define CMARK_BUF_INIT(mem) {mem, cmark_strbuf__initbuf, 0, 0}
|
26
27
|
|
27
28
|
/**
|
28
29
|
* Initialize a cmark_strbuf structure.
|
29
30
|
*
|
30
|
-
* For the cases where
|
31
|
+
* For the cases where CMARK_BUF_INIT cannot be used to do static
|
31
32
|
* initialization.
|
32
33
|
*/
|
33
|
-
void cmark_strbuf_init(cmark_strbuf *buf, bufsize_t initial_size);
|
34
|
+
void cmark_strbuf_init(cmark_mem *mem, cmark_strbuf *buf, bufsize_t initial_size);
|
34
35
|
|
35
36
|
/**
|
36
37
|
* Grow the buffer to hold at least `target_size` bytes.
|
@@ -72,20 +73,6 @@ void cmark_strbuf_trim(cmark_strbuf *buf);
|
|
72
73
|
void cmark_strbuf_normalize_whitespace(cmark_strbuf *s);
|
73
74
|
void cmark_strbuf_unescape(cmark_strbuf *s);
|
74
75
|
|
75
|
-
/* Print error and abort. */
|
76
|
-
void cmark_strbuf_overflow_err(void);
|
77
|
-
|
78
|
-
static CMARK_INLINE bufsize_t cmark_strbuf_check_bufsize(size_t size) {
|
79
|
-
if (size > BUFSIZE_MAX) {
|
80
|
-
cmark_strbuf_overflow_err();
|
81
|
-
}
|
82
|
-
return (bufsize_t)size;
|
83
|
-
}
|
84
|
-
|
85
|
-
static CMARK_INLINE bufsize_t cmark_strbuf_safe_strlen(const char *str) {
|
86
|
-
return cmark_strbuf_check_bufsize(strlen(str));
|
87
|
-
}
|
88
|
-
|
89
76
|
#ifdef __cplusplus
|
90
77
|
}
|
91
78
|
#endif
|
@@ -4,8 +4,10 @@
|
|
4
4
|
#include <string.h>
|
5
5
|
#include <stdlib.h>
|
6
6
|
#include <assert.h>
|
7
|
-
#include "
|
7
|
+
#include "cmark.h"
|
8
8
|
#include "buffer.h"
|
9
|
+
#include "memory.h"
|
10
|
+
#include "cmark_ctype.h"
|
9
11
|
|
10
12
|
#define CMARK_CHUNK_EMPTY \
|
11
13
|
{ NULL, 0, 0 }
|
@@ -16,9 +18,9 @@ typedef struct {
|
|
16
18
|
bufsize_t alloc; // also implies a NULL-terminated string
|
17
19
|
} cmark_chunk;
|
18
20
|
|
19
|
-
static CMARK_INLINE void cmark_chunk_free(cmark_chunk *c) {
|
21
|
+
static CMARK_INLINE void cmark_chunk_free(cmark_mem *mem, cmark_chunk *c) {
|
20
22
|
if (c->alloc)
|
21
|
-
free(c->data);
|
23
|
+
mem->free(c->data);
|
22
24
|
|
23
25
|
c->data = NULL;
|
24
26
|
c->alloc = 0;
|
@@ -48,56 +50,54 @@ static CMARK_INLINE void cmark_chunk_trim(cmark_chunk *c) {
|
|
48
50
|
cmark_chunk_rtrim(c);
|
49
51
|
}
|
50
52
|
|
51
|
-
static CMARK_INLINE bufsize_t
|
52
|
-
|
53
|
+
static CMARK_INLINE bufsize_t
|
54
|
+
cmark_chunk_strchr(cmark_chunk *ch, int c, bufsize_t offset) {
|
53
55
|
const unsigned char *p =
|
54
56
|
(unsigned char *)memchr(ch->data + offset, c, ch->len - offset);
|
55
57
|
return p ? (bufsize_t)(p - ch->data) : ch->len;
|
56
58
|
}
|
57
59
|
|
58
|
-
static CMARK_INLINE const char *cmark_chunk_to_cstr(cmark_chunk *c) {
|
60
|
+
static CMARK_INLINE const char *cmark_chunk_to_cstr(cmark_mem *mem, cmark_chunk *c) {
|
59
61
|
unsigned char *str;
|
60
62
|
|
61
63
|
if (c->alloc) {
|
62
64
|
return (char *)c->data;
|
63
65
|
}
|
64
|
-
str = (unsigned char *)
|
65
|
-
if (
|
66
|
-
|
67
|
-
memcpy(str, c->data, c->len);
|
68
|
-
}
|
69
|
-
str[c->len] = 0;
|
66
|
+
str = (unsigned char *)mem->calloc(c->len + 1, 1);
|
67
|
+
if (c->len > 0) {
|
68
|
+
memcpy(str, c->data, c->len);
|
70
69
|
}
|
70
|
+
str[c->len] = 0;
|
71
71
|
c->data = str;
|
72
72
|
c->alloc = 1;
|
73
73
|
|
74
74
|
return (char *)str;
|
75
75
|
}
|
76
76
|
|
77
|
-
static CMARK_INLINE void cmark_chunk_set_cstr(cmark_chunk *c, const char *str) {
|
77
|
+
static CMARK_INLINE void cmark_chunk_set_cstr(cmark_mem *mem, cmark_chunk *c, const char *str) {
|
78
78
|
if (c->alloc) {
|
79
|
-
free(c->data);
|
79
|
+
mem->free(c->data);
|
80
80
|
}
|
81
81
|
if (str == NULL) {
|
82
82
|
c->len = 0;
|
83
83
|
c->data = NULL;
|
84
84
|
c->alloc = 0;
|
85
85
|
} else {
|
86
|
-
c->len =
|
87
|
-
c->data = (unsigned char *)
|
86
|
+
c->len = (bufsize_t)strlen(str);
|
87
|
+
c->data = (unsigned char *)mem->calloc(c->len + 1, 1);
|
88
88
|
c->alloc = 1;
|
89
89
|
memcpy(c->data, str, c->len + 1);
|
90
90
|
}
|
91
91
|
}
|
92
92
|
|
93
93
|
static CMARK_INLINE cmark_chunk cmark_chunk_literal(const char *data) {
|
94
|
-
bufsize_t len = data ?
|
94
|
+
bufsize_t len = data ? (bufsize_t)strlen(data) : 0;
|
95
95
|
cmark_chunk c = {(unsigned char *)data, len, 0};
|
96
96
|
return c;
|
97
97
|
}
|
98
98
|
|
99
|
-
static CMARK_INLINE cmark_chunk
|
100
|
-
|
99
|
+
static CMARK_INLINE cmark_chunk
|
100
|
+
cmark_chunk_dup(const cmark_chunk *ch, bufsize_t pos, bufsize_t len) {
|
101
101
|
cmark_chunk c = {ch->data + pos, len, 0};
|
102
102
|
return c;
|
103
103
|
}
|
@@ -10,6 +10,20 @@ int cmark_version() { return CMARK_VERSION; }
|
|
10
10
|
|
11
11
|
const char *cmark_version_string() { return CMARK_VERSION_STRING; }
|
12
12
|
|
13
|
+
static void *xcalloc(size_t nmem, size_t size) {
|
14
|
+
void *ptr = calloc(nmem, size);
|
15
|
+
if (!ptr) abort();
|
16
|
+
return ptr;
|
17
|
+
}
|
18
|
+
|
19
|
+
static void *xrealloc(void *ptr, size_t size) {
|
20
|
+
void *new_ptr = realloc(ptr, size);
|
21
|
+
if (!new_ptr) abort();
|
22
|
+
return new_ptr;
|
23
|
+
}
|
24
|
+
|
25
|
+
cmark_mem DEFAULT_MEM_ALLOCATOR = { xcalloc, xrealloc, free };
|
26
|
+
|
13
27
|
char *cmark_markdown_to_html(const char *text, size_t len, int options) {
|
14
28
|
cmark_node *doc;
|
15
29
|
char *result;
|
@@ -21,7 +21,8 @@ extern "C" {
|
|
21
21
|
|
22
22
|
/** Convert 'text' (assumed to be a UTF-8 encoded string with length
|
23
23
|
* 'len') from CommonMark Markdown to HTML, returning a null-terminated,
|
24
|
-
* UTF-8-encoded string.
|
24
|
+
* UTF-8-encoded string. It is the caller's responsibility
|
25
|
+
* to free the returned buffer.
|
25
26
|
*/
|
26
27
|
CMARK_EXPORT
|
27
28
|
char *cmark_markdown_to_html(const char *text, size_t len, int options);
|
@@ -86,6 +87,20 @@ typedef struct cmark_node cmark_node;
|
|
86
87
|
typedef struct cmark_parser cmark_parser;
|
87
88
|
typedef struct cmark_iter cmark_iter;
|
88
89
|
|
90
|
+
/**
|
91
|
+
* ## Custom memory allocator support
|
92
|
+
*/
|
93
|
+
|
94
|
+
/** Defines the memory allocation functions to be used by CMark
|
95
|
+
* when parsing and allocating a document tree
|
96
|
+
*/
|
97
|
+
typedef struct cmark_mem {
|
98
|
+
void *(*calloc)(size_t, size_t);
|
99
|
+
void *(*realloc)(void *, size_t);
|
100
|
+
void (*free)(void *);
|
101
|
+
} cmark_mem;
|
102
|
+
|
103
|
+
|
89
104
|
/**
|
90
105
|
* ## Creating and Destroying Nodes
|
91
106
|
*/
|
@@ -96,6 +111,11 @@ typedef struct cmark_iter cmark_iter;
|
|
96
111
|
*/
|
97
112
|
CMARK_EXPORT cmark_node *cmark_node_new(cmark_node_type type);
|
98
113
|
|
114
|
+
/** Same as `cmark_node_new`, but explicitly listing the memory
|
115
|
+
* allocator used to allocate the node
|
116
|
+
*/
|
117
|
+
CMARK_EXPORT cmark_node *cmark_node_new_with_mem(cmark_node_type type, cmark_mem *mem);
|
118
|
+
|
99
119
|
/** Frees the memory allocated for a node and any children.
|
100
120
|
*/
|
101
121
|
CMARK_EXPORT void cmark_node_free(cmark_node *node);
|
@@ -179,7 +199,9 @@ typedef enum {
|
|
179
199
|
} cmark_event_type;
|
180
200
|
|
181
201
|
/** Creates a new iterator starting at 'root'. The current node and event
|
182
|
-
* type are undefined until
|
202
|
+
* type are undefined until 'cmark_iter_next' is called for the first time.
|
203
|
+
* The memory allocated for the iterator should be released using
|
204
|
+
* 'cmark_iter_free' when it is no longer needed.
|
183
205
|
*/
|
184
206
|
CMARK_EXPORT
|
185
207
|
cmark_iter *cmark_iter_new(cmark_node *root);
|
@@ -434,6 +456,11 @@ CMARK_EXPORT void cmark_consolidate_text_nodes(cmark_node *root);
|
|
434
456
|
CMARK_EXPORT
|
435
457
|
cmark_parser *cmark_parser_new(int options);
|
436
458
|
|
459
|
+
/** Creates a new parser object with the given memory allocator
|
460
|
+
*/
|
461
|
+
CMARK_EXPORT
|
462
|
+
cmark_parser *cmark_parser_new_with_mem(int options, cmark_mem *mem);
|
463
|
+
|
437
464
|
/** Frees memory allocated for a parser object.
|
438
465
|
*/
|
439
466
|
CMARK_EXPORT
|
@@ -450,13 +477,16 @@ CMARK_EXPORT
|
|
450
477
|
cmark_node *cmark_parser_finish(cmark_parser *parser);
|
451
478
|
|
452
479
|
/** Parse a CommonMark document in 'buffer' of length 'len'.
|
453
|
-
* Returns a pointer to a tree of nodes.
|
480
|
+
* Returns a pointer to a tree of nodes. The memory allocated for
|
481
|
+
* the node tree should be released using 'cmark_node_free'
|
482
|
+
* when it is no longer needed.
|
454
483
|
*/
|
455
484
|
CMARK_EXPORT
|
456
485
|
cmark_node *cmark_parse_document(const char *buffer, size_t len, int options);
|
457
486
|
|
458
487
|
/** Parse a CommonMark document in file 'f', returning a pointer to
|
459
|
-
* a tree of nodes.
|
488
|
+
* a tree of nodes. The memory allocated for the node tree should be
|
489
|
+
* released using 'cmark_node_free' when it is no longer needed.
|
460
490
|
*/
|
461
491
|
CMARK_EXPORT
|
462
492
|
cmark_node *cmark_parse_file(FILE *f, int options);
|
@@ -465,28 +495,33 @@ cmark_node *cmark_parse_file(FILE *f, int options);
|
|
465
495
|
* ## Rendering
|
466
496
|
*/
|
467
497
|
|
468
|
-
/** Render a 'node' tree as XML.
|
498
|
+
/** Render a 'node' tree as XML. It is the caller's responsibility
|
499
|
+
* to free the returned buffer.
|
469
500
|
*/
|
470
501
|
CMARK_EXPORT
|
471
502
|
char *cmark_render_xml(cmark_node *root, int options);
|
472
503
|
|
473
504
|
/** Render a 'node' tree as an HTML fragment. It is up to the user
|
474
|
-
* to add an appropriate header and footer.
|
505
|
+
* to add an appropriate header and footer. It is the caller's
|
506
|
+
* responsibility to free the returned buffer.
|
475
507
|
*/
|
476
508
|
CMARK_EXPORT
|
477
509
|
char *cmark_render_html(cmark_node *root, int options);
|
478
510
|
|
479
511
|
/** Render a 'node' tree as a groff man page, without the header.
|
512
|
+
* It is the caller's responsibility to free the returned buffer.
|
480
513
|
*/
|
481
514
|
CMARK_EXPORT
|
482
515
|
char *cmark_render_man(cmark_node *root, int options, int width);
|
483
516
|
|
484
517
|
/** Render a 'node' tree as a commonmark document.
|
518
|
+
* It is the caller's responsibility to free the returned buffer.
|
485
519
|
*/
|
486
520
|
CMARK_EXPORT
|
487
521
|
char *cmark_render_commonmark(cmark_node *root, int options, int width);
|
488
522
|
|
489
523
|
/** Render a 'node' tree as a LaTeX document.
|
524
|
+
* It is the caller's responsibility to free the returned buffer.
|
490
525
|
*/
|
491
526
|
CMARK_EXPORT
|
492
527
|
char *cmark_render_latex(cmark_node *root, int options, int width);
|
@@ -519,6 +554,10 @@ char *cmark_render_latex(cmark_node *root, int options, int width);
|
|
519
554
|
*/
|
520
555
|
#define CMARK_OPT_SAFE (1 << 3)
|
521
556
|
|
557
|
+
/** Render `softbreak` elements as spaces.
|
558
|
+
*/
|
559
|
+
#define CMARK_OPT_NOBREAKS (1 << 4)
|
560
|
+
|
522
561
|
/**
|
523
562
|
* ### Options affecting parsing
|
524
563
|
*/
|