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.

Files changed (143) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -0
  3. data/Rakefile +9 -9
  4. data/ext/commonmarker/cmark/api_test/main.c +5 -0
  5. data/ext/commonmarker/cmark/build/CMakeCache.txt +459 -0
  6. data/ext/commonmarker/cmark/build/CMakeFiles/3.5.2/CMakeCCompiler.cmake +67 -0
  7. data/ext/commonmarker/cmark/build/CMakeFiles/3.5.2/CMakeCXXCompiler.cmake +68 -0
  8. data/ext/commonmarker/cmark/build/CMakeFiles/3.5.2/CMakeDetermineCompilerABI_C.bin +0 -0
  9. data/ext/commonmarker/cmark/build/CMakeFiles/3.5.2/CMakeDetermineCompilerABI_CXX.bin +0 -0
  10. data/ext/commonmarker/cmark/build/CMakeFiles/3.5.2/CMakeSystem.cmake +15 -0
  11. data/ext/commonmarker/cmark/build/CMakeFiles/3.5.2/CompilerIdC/CMakeCCompilerId.c +544 -0
  12. data/ext/commonmarker/cmark/build/CMakeFiles/3.5.2/CompilerIdC/a.out +0 -0
  13. data/ext/commonmarker/cmark/build/CMakeFiles/3.5.2/CompilerIdCXX/CMakeCXXCompilerId.cpp +533 -0
  14. data/ext/commonmarker/cmark/build/CMakeFiles/3.5.2/CompilerIdCXX/a.out +0 -0
  15. data/ext/commonmarker/cmark/build/CMakeFiles/CMakeDirectoryInformation.cmake +16 -0
  16. data/ext/commonmarker/cmark/build/CMakeFiles/CMakeError.log +14 -0
  17. data/ext/commonmarker/cmark/build/CMakeFiles/CMakeOutput.log +562 -0
  18. data/ext/commonmarker/cmark/build/CMakeFiles/Makefile.cmake +150 -0
  19. data/ext/commonmarker/cmark/build/CMakeFiles/Makefile2 +295 -0
  20. data/ext/commonmarker/cmark/build/CMakeFiles/TargetDirectories.txt +39 -0
  21. data/ext/commonmarker/cmark/build/CMakeFiles/cmake.check_cache +1 -0
  22. data/ext/commonmarker/cmark/build/CMakeFiles/feature_tests.bin +0 -0
  23. data/ext/commonmarker/cmark/build/CMakeFiles/feature_tests.c +34 -0
  24. data/ext/commonmarker/cmark/build/CMakeFiles/feature_tests.cxx +405 -0
  25. data/ext/commonmarker/cmark/build/CMakeFiles/progress.marks +1 -0
  26. data/ext/commonmarker/cmark/build/CTestTestfile.cmake +10 -0
  27. data/ext/commonmarker/cmark/build/Makefile +250 -0
  28. data/ext/commonmarker/cmark/build/api_test/CMakeFiles/CMakeDirectoryInformation.cmake +16 -0
  29. data/ext/commonmarker/cmark/build/api_test/CMakeFiles/api_test.dir/DependInfo.cmake +35 -0
  30. data/ext/commonmarker/cmark/build/api_test/CMakeFiles/api_test.dir/build.make +168 -0
  31. data/ext/commonmarker/cmark/build/api_test/CMakeFiles/api_test.dir/cmake_clean.cmake +12 -0
  32. data/ext/commonmarker/cmark/build/api_test/CMakeFiles/api_test.dir/depend.make +2 -0
  33. data/ext/commonmarker/cmark/build/api_test/CMakeFiles/api_test.dir/flags.make +17 -0
  34. data/ext/commonmarker/cmark/build/api_test/CMakeFiles/api_test.dir/link.txt +1 -0
  35. data/ext/commonmarker/cmark/build/api_test/CMakeFiles/api_test.dir/progress.make +5 -0
  36. data/ext/commonmarker/cmark/build/api_test/CMakeFiles/progress.marks +1 -0
  37. data/ext/commonmarker/cmark/build/api_test/Makefile +300 -0
  38. data/ext/commonmarker/cmark/build/api_test/cmake_install.cmake +29 -0
  39. data/ext/commonmarker/cmark/build/cmake_install.cmake +48 -0
  40. data/ext/commonmarker/cmark/build/man/CMakeFiles/CMakeDirectoryInformation.cmake +16 -0
  41. data/ext/commonmarker/cmark/build/man/CMakeFiles/progress.marks +1 -0
  42. data/ext/commonmarker/cmark/build/man/Makefile +194 -0
  43. data/ext/commonmarker/cmark/build/man/cmake_install.cmake +37 -0
  44. data/ext/commonmarker/cmark/build/src/CMakeFiles/CMakeDirectoryInformation.cmake +16 -0
  45. data/ext/commonmarker/cmark/build/src/CMakeFiles/cmark.dir/DependInfo.cmake +41 -0
  46. data/ext/commonmarker/cmark/build/src/CMakeFiles/cmark.dir/build.make +626 -0
  47. data/ext/commonmarker/cmark/build/src/CMakeFiles/cmark.dir/cmake_clean.cmake +29 -0
  48. data/ext/commonmarker/cmark/build/src/CMakeFiles/cmark.dir/depend.make +2 -0
  49. data/ext/commonmarker/cmark/build/src/CMakeFiles/cmark.dir/flags.make +10 -0
  50. data/ext/commonmarker/cmark/build/src/CMakeFiles/cmark.dir/link.txt +1 -0
  51. data/ext/commonmarker/cmark/build/src/CMakeFiles/cmark.dir/progress.make +22 -0
  52. data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark.dir/DependInfo.cmake +46 -0
  53. data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark.dir/build.make +603 -0
  54. data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark.dir/cmake_clean.cmake +29 -0
  55. data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark.dir/depend.make +2 -0
  56. data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark.dir/flags.make +10 -0
  57. data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark.dir/link.txt +1 -0
  58. data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark.dir/progress.make +21 -0
  59. data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/C.includecache +468 -0
  60. data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/DependInfo.cmake +40 -0
  61. data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/blocks.c.o +0 -0
  62. data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/buffer.c.o +0 -0
  63. data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/build.make +600 -0
  64. data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/cmake_clean.cmake +28 -0
  65. data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/cmake_clean_target.cmake +3 -0
  66. data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/cmark.c.o +0 -0
  67. data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/cmark_ctype.c.o +0 -0
  68. data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/commonmark.c.o +0 -0
  69. data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/depend.internal +211 -0
  70. data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/depend.make +211 -0
  71. data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/flags.make +10 -0
  72. data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/houdini_href_e.c.o +0 -0
  73. data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/houdini_html_e.c.o +0 -0
  74. data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/houdini_html_u.c.o +0 -0
  75. data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/html.c.o +0 -0
  76. data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/inlines.c.o +0 -0
  77. data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/iterator.c.o +0 -0
  78. data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/latex.c.o +0 -0
  79. data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/link.txt +2 -0
  80. data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/man.c.o +0 -0
  81. data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/node.c.o +0 -0
  82. data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/progress.make +21 -0
  83. data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/references.c.o +0 -0
  84. data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/render.c.o +0 -0
  85. data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/scanners.c.o +0 -0
  86. data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/utf8.c.o +0 -0
  87. data/ext/commonmarker/cmark/build/src/CMakeFiles/libcmark_static.dir/xml.c.o +0 -0
  88. data/ext/commonmarker/cmark/build/src/CMakeFiles/progress.marks +1 -0
  89. data/ext/commonmarker/cmark/build/src/Makefile +956 -0
  90. data/ext/commonmarker/cmark/build/src/cmake_install.cmake +77 -0
  91. data/ext/commonmarker/cmark/build/src/cmark_export.h +41 -0
  92. data/ext/commonmarker/cmark/build/src/cmark_version.h +7 -0
  93. data/ext/commonmarker/cmark/build/src/config.h +84 -0
  94. data/ext/commonmarker/cmark/build/src/libcmark.a +0 -0
  95. data/ext/commonmarker/cmark/build/src/libcmark.pc +10 -0
  96. data/ext/commonmarker/cmark/build/testdir/CMakeFiles/CMakeDirectoryInformation.cmake +16 -0
  97. data/ext/commonmarker/cmark/build/testdir/CMakeFiles/progress.marks +1 -0
  98. data/ext/commonmarker/cmark/build/testdir/CTestTestfile.cmake +14 -0
  99. data/ext/commonmarker/cmark/build/testdir/Makefile +194 -0
  100. data/ext/commonmarker/cmark/build/testdir/cmake_install.cmake +29 -0
  101. data/ext/commonmarker/cmark/man/man1/cmark.1 +11 -4
  102. data/ext/commonmarker/cmark/man/man3/cmark.3 +66 -11
  103. data/ext/commonmarker/cmark/src/blocks.c +232 -235
  104. data/ext/commonmarker/cmark/src/buffer.c +19 -56
  105. data/ext/commonmarker/cmark/src/buffer.h +7 -20
  106. data/ext/commonmarker/cmark/src/chunk.h +19 -19
  107. data/ext/commonmarker/cmark/src/cmark.c +14 -0
  108. data/ext/commonmarker/cmark/src/cmark.h +45 -6
  109. data/ext/commonmarker/cmark/src/cmark_ctype.c +2 -0
  110. data/ext/commonmarker/cmark/src/cmark_ctype.h +2 -0
  111. data/ext/commonmarker/cmark/src/commonmark.c +42 -29
  112. data/ext/commonmarker/cmark/src/config.h.in +8 -0
  113. data/ext/commonmarker/cmark/src/html.c +3 -1
  114. data/ext/commonmarker/cmark/src/inlines.c +111 -150
  115. data/ext/commonmarker/cmark/src/inlines.h +4 -4
  116. data/ext/commonmarker/cmark/src/iterator.c +6 -7
  117. data/ext/commonmarker/cmark/src/iterator.h +2 -0
  118. data/ext/commonmarker/cmark/src/latex.c +10 -8
  119. data/ext/commonmarker/cmark/src/main.c +6 -2
  120. data/ext/commonmarker/cmark/src/man.c +9 -5
  121. data/ext/commonmarker/cmark/src/node.c +38 -29
  122. data/ext/commonmarker/cmark/src/node.h +15 -11
  123. data/ext/commonmarker/cmark/src/parser.h +4 -2
  124. data/ext/commonmarker/cmark/src/references.c +23 -22
  125. data/ext/commonmarker/cmark/src/references.h +3 -1
  126. data/ext/commonmarker/cmark/src/render.c +9 -7
  127. data/ext/commonmarker/cmark/src/render.h +3 -1
  128. data/ext/commonmarker/cmark/src/scanners.c +30 -22
  129. data/ext/commonmarker/cmark/src/xml.c +1 -1
  130. data/ext/commonmarker/cmark/test/CMakeLists.txt +8 -8
  131. data/ext/commonmarker/cmark/test/cmark.py +34 -14
  132. data/ext/commonmarker/cmark/test/roundtrip_tests.py +47 -0
  133. data/ext/commonmarker/cmark/test/spec.txt +96 -6
  134. data/ext/commonmarker/cmark/test/spec_tests.py +5 -8
  135. data/ext/commonmarker/commonmarker.c +14 -8
  136. data/lib/commonmarker/config.rb +2 -2
  137. data/lib/commonmarker/renderer.rb +17 -7
  138. data/lib/commonmarker/renderer/html_renderer.rb +16 -21
  139. data/lib/commonmarker/version.rb +1 -1
  140. data/test/test_pathological_inputs.rb +11 -11
  141. metadata +99 -4
  142. data/ext/commonmarker/cmark/test/roundtrip.bat +0 -1
  143. 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 cmark_strbuf_overflow_err() {
32
- fprintf(stderr, "String buffer overflow");
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
- unsigned char *new_ptr;
39
+ assert(target_size > 0);
52
40
 
53
41
  if (target_size < buf->asize)
54
42
  return;
55
43
 
56
- if (buf->asize == 0) {
57
- new_ptr = NULL;
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
- size_t new_size = (size_t)target_size + (size_t)target_size / 2;
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
- if (new_size < (size_t)target_size /* Integer overflow. */
73
- || new_size > BUFSIZE_MAX /* Truncation overflow. */
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 ? cmark_strbuf_safe_strlen(string) : 0);
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
- cmark_strbuf_safe_strlen(string));
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 int bufsize_t;
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 GH_BUF_INIT \
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 GH_BUF_INIT cannot be used to do static
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 "cmark_ctype.h"
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 cmark_chunk_strchr(cmark_chunk *ch, int c,
52
- bufsize_t offset) {
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 *)malloc(c->len + 1);
65
- if (str != NULL) {
66
- if (c->len > 0) {
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 = cmark_strbuf_safe_strlen(str);
87
- c->data = (unsigned char *)malloc(c->len + 1);
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 ? cmark_strbuf_safe_strlen(data) : 0;
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 cmark_chunk_dup(const cmark_chunk *ch,
100
- bufsize_t pos, bufsize_t len) {
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 `cmark_iter_next` is called for the first time.
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
  */
@@ -40,3 +40,5 @@ int cmark_isalnum(char c) {
40
40
  }
41
41
 
42
42
  int cmark_isdigit(char c) { return cmark_ctype_class[(uint8_t)c] == 3; }
43
+
44
+ int cmark_isalpha(char c) { return cmark_ctype_class[(uint8_t)c] == 4; }