Skip to content

Commit

Permalink
Updated to cmark 0.20.0
Browse files Browse the repository at this point in the history
  • Loading branch information
rhinoman committed Jun 8, 2015
1 parent 0d388ea commit 9acde66
Show file tree
Hide file tree
Showing 28 changed files with 899 additions and 743 deletions.
318 changes: 182 additions & 136 deletions blocks.c

Large diffs are not rendered by default.

217 changes: 110 additions & 107 deletions buffer.c

Large diffs are not rendered by default.

94 changes: 38 additions & 56 deletions buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,74 +3,48 @@

#include <stddef.h>
#include <stdarg.h>
#include <string.h>
#include <limits.h>
#include "config.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef int bufsize_t;

typedef struct {
unsigned char *ptr;
int asize, size;
bufsize_t asize, size;
} cmark_strbuf;

extern unsigned char cmark_strbuf__initbuf[];

extern unsigned char cmark_strbuf__oom[];

#define GH_BUF_INIT { cmark_strbuf__initbuf, 0, 0 }
#define BUFSIZE_MAX INT_MAX

/**
* Initialize a cmark_strbuf structure.
*
* For the cases where GH_BUF_INIT cannot be used to do static
* initialization.
*/
void cmark_strbuf_init(cmark_strbuf *buf, int initial_size);

/**
* Attempt to grow the buffer to hold at least `target_size` bytes.
*
* If the allocation fails, this will return an error. If mark_oom is true,
* this will mark the buffer as invalid for future operations; if false,
* existing buffer content will be preserved, but calling code must handle
* that buffer was not expanded.
*/
int cmark_strbuf_try_grow(cmark_strbuf *buf, int target_size, bool mark_oom);
void cmark_strbuf_init(cmark_strbuf *buf, bufsize_t initial_size);

/**
* Grow the buffer to hold at least `target_size` bytes.
*
* If the allocation fails, this will return an error and the buffer will be
* marked as invalid for future operations, invaliding contents.
*
* @return 0 on success or -1 on failure
*/
int cmark_strbuf_grow(cmark_strbuf *buf, int target_size);
void cmark_strbuf_grow(cmark_strbuf *buf, bufsize_t target_size);

void cmark_strbuf_free(cmark_strbuf *buf);
void cmark_strbuf_swap(cmark_strbuf *buf_a, cmark_strbuf *buf_b);

/**
* Test if there have been any reallocation failures with this cmark_strbuf.
*
* Any function that writes to a cmark_strbuf can fail due to memory allocation
* issues. If one fails, the cmark_strbuf will be marked with an OOM error and
* further calls to modify the buffer will fail. Check cmark_strbuf_oom() at the
* end of your sequence and it will be true if you ran out of memory at any
* point with that buffer.
*
* @return false if no error, true if allocation error
*/
bool cmark_strbuf_oom(const cmark_strbuf *buf);

size_t cmark_strbuf_len(const cmark_strbuf *buf);
bufsize_t cmark_strbuf_len(const cmark_strbuf *buf);

int cmark_strbuf_cmp(const cmark_strbuf *a, const cmark_strbuf *b);

void cmark_strbuf_attach(cmark_strbuf *buf, unsigned char *ptr, int asize);
unsigned char *cmark_strbuf_detach(cmark_strbuf *buf);
void cmark_strbuf_copy_cstr(char *data, int datasize, const cmark_strbuf *buf);
void cmark_strbuf_copy_cstr(char *data, bufsize_t datasize, const cmark_strbuf *buf);

static inline const char *cmark_strbuf_cstr(const cmark_strbuf *buf)
{
Expand All @@ -79,33 +53,41 @@ static inline const char *cmark_strbuf_cstr(const cmark_strbuf *buf)

#define cmark_strbuf_at(buf, n) ((buf)->ptr[n])

/*
* Functions below that return int value error codes will return 0 on
* success or -1 on failure (which generally means an allocation failed).
* Using a cmark_strbuf where the allocation has failed with result in -1 from
* all further calls using that buffer. As a result, you can ignore the
* return code of these functions and call them in a series then just call
* cmark_strbuf_oom at the end.
*/
int cmark_strbuf_set(cmark_strbuf *buf, const unsigned char *data, int len);
int cmark_strbuf_sets(cmark_strbuf *buf, const char *string);
int cmark_strbuf_putc(cmark_strbuf *buf, int c);
int cmark_strbuf_put(cmark_strbuf *buf, const unsigned char *data, int len);
int cmark_strbuf_puts(cmark_strbuf *buf, const char *string);
int cmark_strbuf_printf(cmark_strbuf *buf, const char *format, ...)
CMARK_ATTRIBUTE((format (printf, 2, 3)));
int cmark_strbuf_vprintf(cmark_strbuf *buf, const char *format, va_list ap);
void cmark_strbuf_set(cmark_strbuf *buf, const unsigned char *data, bufsize_t len);
void cmark_strbuf_sets(cmark_strbuf *buf, const char *string);
void cmark_strbuf_putc(cmark_strbuf *buf, int c);
void cmark_strbuf_put(cmark_strbuf *buf, const unsigned char *data, bufsize_t len);
void cmark_strbuf_puts(cmark_strbuf *buf, const char *string);
void cmark_strbuf_printf(cmark_strbuf *buf, const char *format, ...)
CMARK_ATTRIBUTE((format (printf, 2, 3)));
void cmark_strbuf_vprintf(cmark_strbuf *buf, const char *format, va_list ap);
void cmark_strbuf_clear(cmark_strbuf *buf);

int cmark_strbuf_strchr(const cmark_strbuf *buf, int c, int pos);
int cmark_strbuf_strrchr(const cmark_strbuf *buf, int c, int pos);
void cmark_strbuf_drop(cmark_strbuf *buf, int n);
void cmark_strbuf_truncate(cmark_strbuf *buf, int len);
bufsize_t cmark_strbuf_strchr(const cmark_strbuf *buf, int c, bufsize_t pos);
bufsize_t cmark_strbuf_strrchr(const cmark_strbuf *buf, int c, bufsize_t pos);
void cmark_strbuf_drop(cmark_strbuf *buf, bufsize_t n);
void cmark_strbuf_truncate(cmark_strbuf *buf, bufsize_t len);
void cmark_strbuf_rtrim(cmark_strbuf *buf);
void cmark_strbuf_trim(cmark_strbuf *buf);
void cmark_strbuf_normalize_whitespace(cmark_strbuf *s);
void cmark_strbuf_unescape(cmark_strbuf *s);

/* Print error and abort. */
void cmark_strbuf_overflow_err(void);

static inline bufsize_t
cmark_strbuf_check_bufsize(size_t size) {
if (size > BUFSIZE_MAX) {
cmark_strbuf_overflow_err();
}
return (bufsize_t)size;
}

static inline bufsize_t
cmark_strbuf_safe_strlen(const char *str) {
return cmark_strbuf_check_bufsize(strlen(str));
}

#ifdef __cplusplus
}
#endif
Expand Down
33 changes: 22 additions & 11 deletions chunk.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
#include "cmark_ctype.h"
#include "buffer.h"

#define CMARK_CHUNK_EMPTY { NULL, 0, 0 }

typedef struct {
unsigned char *data;
int len;
int alloc; // also implies a NULL-terminated string
bufsize_t len;
bufsize_t alloc; // also implies a NULL-terminated string
} cmark_chunk;

static inline void cmark_chunk_free(cmark_chunk *c)
Expand Down Expand Up @@ -49,10 +51,10 @@ static inline void cmark_chunk_trim(cmark_chunk *c)
cmark_chunk_rtrim(c);
}

static inline int cmark_chunk_strchr(cmark_chunk *ch, int c, int offset)
static inline bufsize_t cmark_chunk_strchr(cmark_chunk *ch, int c, bufsize_t offset)
{
const unsigned char *p = (unsigned char *)memchr(ch->data + offset, c, ch->len - offset);
return p ? (int)(p - ch->data) : ch->len;
return p ? (bufsize_t)(p - ch->data) : ch->len;
}

static inline const char *cmark_chunk_to_cstr(cmark_chunk *c)
Expand All @@ -64,7 +66,9 @@ static inline const char *cmark_chunk_to_cstr(cmark_chunk *c)
}
str = (unsigned char *)malloc(c->len + 1);
if(str != NULL) {
memcpy(str, c->data, c->len);
if(c->len > 0) {
memcpy(str, c->data, c->len);
}
str[c->len] = 0;
}
c->data = str;
Expand All @@ -78,19 +82,26 @@ static inline void cmark_chunk_set_cstr(cmark_chunk *c, const char *str)
if (c->alloc) {
free(c->data);
}
c->len = strlen(str);
c->data = (unsigned char *)malloc(c->len + 1);
c->alloc = 1;
memcpy(c->data, str, c->len + 1);
if (str == NULL) {
c->len = 0;
c->data = NULL;
c->alloc = 0;
} else {
c->len = cmark_strbuf_safe_strlen(str);
c->data = (unsigned char *)malloc(c->len + 1);
c->alloc = 1;
memcpy(c->data, str, c->len + 1);
}
}

static inline cmark_chunk cmark_chunk_literal(const char *data)
{
cmark_chunk c = {(unsigned char *)data, data ? strlen(data) : 0, 0};
bufsize_t len = data ? cmark_strbuf_safe_strlen(data) : 0;
cmark_chunk c = {(unsigned char *)data, len, 0};
return c;
}

static inline cmark_chunk cmark_chunk_dup(const cmark_chunk *ch, int pos, int len)
static inline cmark_chunk cmark_chunk_dup(const cmark_chunk *ch, bufsize_t pos, bufsize_t len)
{
cmark_chunk c = {ch->data + pos, len, 0};
return c;
Expand Down
2 changes: 1 addition & 1 deletion cmark.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
const int cmark_version = CMARK_VERSION;
const char cmark_version_string[] = CMARK_VERSION_STRING;

char *cmark_markdown_to_html(const char *text, int len, int options)
char *cmark_markdown_to_html(const char *text, size_t len, int options)
{
cmark_node *doc;
char *result;
Expand Down
2 changes: 1 addition & 1 deletion cmark.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ extern "C" {
* UTF-8-encoded string.
*/
CMARK_EXPORT
char *cmark_markdown_to_html(const char *text, int len, int options);
char *cmark_markdown_to_html(const char *text, size_t len, int options);

/** ## Node Structure
*/
Expand Down
52 changes: 27 additions & 25 deletions commonmark.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ struct render_state {
int column;
int width;
int need_cr;
int last_breakable;
bufsize_t last_breakable;
bool begin_line;
bool no_wrap;
bool in_tight_list_item;
Expand Down Expand Up @@ -237,28 +237,31 @@ shortest_unused_backtick_sequence(cmark_chunk *code)
static bool
is_autolink(cmark_node *node)
{
const char *title;
const char *url;
cmark_chunk *title;
cmark_chunk *url;
cmark_node *link_text;

if (node->type != CMARK_NODE_LINK) {
return false;
}

url = cmark_node_get_url(node);
if (url == NULL ||
_scan_scheme((unsigned char *)url) == 0) {
url = &node->as.link.url;
if (url->len == 0 || scan_scheme(url, 0) == 0) {
return false;
}

title = cmark_node_get_title(node);
title = &node->as.link.title;
// if it has a title, we can't treat it as an autolink:
if (title != NULL && strlen(title) > 0) {
if (title->len > 0) {
return false;
}
cmark_consolidate_text_nodes(node);
return (strncmp(url,
(char*)node->as.literal.data,
node->as.literal.len) == 0);

link_text = node->first_child;
cmark_consolidate_text_nodes(link_text);
return (url->len == link_text->as.literal.len &&
strncmp((char*)url->data,
(char*)link_text->as.literal.data,
link_text->as.literal.len) == 0);
}

// if node is a block node, returns node.
Expand All @@ -285,11 +288,11 @@ S_render_node(cmark_node *node, cmark_event_type ev_type,
int numticks;
int i;
bool entering = (ev_type == CMARK_EVENT_ENTER);
const char *info;
const char *title;
cmark_chunk *info;
cmark_chunk *title;
cmark_strbuf listmarker = GH_BUF_INIT;
char *emph_delim;
int marker_width;
bufsize_t marker_width;

// Don't adjust tight list status til we've started the list.
// Otherwise we loose the blank line between a paragraph and
Expand Down Expand Up @@ -392,12 +395,12 @@ S_render_node(cmark_node *node, cmark_event_type ev_type,

case CMARK_NODE_CODE_BLOCK:
blankline(state);
info = cmark_node_get_fence_info(node);
info = &node->as.code.info;
code = &node->as.code.literal;
// use indented form if no info, and code doesn't
// begin or end with a blank line, and code isn't
// first thing in a list item
if ((info == NULL || strlen(info) == 0) &&
if (info->len == 0 &&
(code->len > 2 &&
!isspace(code->data[0]) &&
!(isspace(code->data[code->len - 1]) &&
Expand All @@ -418,7 +421,7 @@ S_render_node(cmark_node *node, cmark_event_type ev_type,
lit(state, "`", false);
}
lit(state, " ", false);
out(state, cmark_chunk_literal(info), false, LITERAL);
out(state, *info, false, LITERAL);
cr(state);
out(state, node->as.code.literal, false, LITERAL);
cr(state);
Expand Down Expand Up @@ -538,11 +541,10 @@ S_render_node(cmark_node *node, cmark_event_type ev_type,
out(state,
cmark_chunk_literal(cmark_node_get_url(node)),
false, URL);
title = cmark_node_get_title(node);
if (title && strlen(title) > 0) {
title = &node->as.link.title;
if (title->len > 0) {
lit(state, " \"", true);
out(state, cmark_chunk_literal(title),
false, TITLE);
out(state, *title, false, TITLE);
lit(state, "\"", false);
}
lit(state, ")", false);
Expand All @@ -556,10 +558,10 @@ S_render_node(cmark_node *node, cmark_event_type ev_type,
} else {
lit(state, "](", false);
out(state, cmark_chunk_literal(cmark_node_get_url(node)), false, URL);
title = cmark_node_get_title(node);
if (title && strlen(title) > 0) {
title = &node->as.link.title;
if (title->len > 0) {
lit(state, " \"", true);
out(state, cmark_chunk_literal(title), false, TITLE);
out(state, *title, false, TITLE);
lit(state, "\"", false);
}
lit(state, ")", false);
Expand Down
2 changes: 1 addition & 1 deletion commonmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func Md2Html(mdtext string, options int) string {
mdtext += "\n"
}
mdCstr := C.CString(mdtext)
strLen := C.int(len(mdtext))
strLen := C.size_t(len(mdtext))
defer C.free(unsafe.Pointer(mdCstr))
htmlString := C.cmark_markdown_to_html(mdCstr, strLen, C.int(options))
defer C.free(unsafe.Pointer(htmlString))
Expand Down
2 changes: 2 additions & 0 deletions config.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@
#ifndef HAVE_VA_COPY
#define va_copy(dest, src) ((dest) = (src))
#endif

#define HAVE_C99_SNPRINTF
19 changes: 6 additions & 13 deletions houdini.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,12 @@ extern "C" {
#define HOUDINI_ESCAPED_SIZE(x) (((x) * 12) / 10)
#define HOUDINI_UNESCAPED_SIZE(x) (x)

extern size_t houdini_unescape_ent(cmark_strbuf *ob, const uint8_t *src, size_t size);
extern int houdini_escape_html(cmark_strbuf *ob, const uint8_t *src, size_t size);
extern int houdini_escape_html0(cmark_strbuf *ob, const uint8_t *src, size_t size, int secure);
extern int houdini_unescape_html(cmark_strbuf *ob, const uint8_t *src, size_t size);
extern void houdini_unescape_html_f(cmark_strbuf *ob, const uint8_t *src, size_t size);
extern int houdini_escape_xml(cmark_strbuf *ob, const uint8_t *src, size_t size);
extern int houdini_escape_uri(cmark_strbuf *ob, const uint8_t *src, size_t size);
extern int houdini_escape_url(cmark_strbuf *ob, const uint8_t *src, size_t size);
extern int houdini_escape_href(cmark_strbuf *ob, const uint8_t *src, size_t size);
extern int houdini_unescape_uri(cmark_strbuf *ob, const uint8_t *src, size_t size);
extern int houdini_unescape_url(cmark_strbuf *ob, const uint8_t *src, size_t size);
extern int houdini_escape_js(cmark_strbuf *ob, const uint8_t *src, size_t size);
extern int houdini_unescape_js(cmark_strbuf *ob, const uint8_t *src, size_t size);
extern bufsize_t houdini_unescape_ent(cmark_strbuf *ob, const uint8_t *src, bufsize_t size);
extern int houdini_escape_html(cmark_strbuf *ob, const uint8_t *src, bufsize_t size);
extern int houdini_escape_html0(cmark_strbuf *ob, const uint8_t *src, bufsize_t size, int secure);
extern int houdini_unescape_html(cmark_strbuf *ob, const uint8_t *src, bufsize_t size);
extern void houdini_unescape_html_f(cmark_strbuf *ob, const uint8_t *src, bufsize_t size);
extern int houdini_escape_href(cmark_strbuf *ob, const uint8_t *src, bufsize_t size);

#ifdef __cplusplus
}
Expand Down
Loading

0 comments on commit 9acde66

Please sign in to comment.