X-Git-Url: https://gitweb.ps.run/ps-cgit/blobdiff_plain/e550440233875b298f8574e9273457516791010f..c0dfaf1c281d0697ce43131343d7a9f170a61ff9:/parsing.c diff --git a/parsing.c b/parsing.c index f3f3b15..658621d 100644 --- a/parsing.c +++ b/parsing.c @@ -52,17 +52,19 @@ void cgit_parse_url(const char *url) } } -char *substr(const char *head, const char *tail) +static char *substr(const char *head, const char *tail) { char *buf; + if (tail < head) + return xstrdup(""); buf = xmalloc(tail - head + 1); strncpy(buf, head, tail - head); buf[tail - head] = '\0'; return buf; } -char *parse_user(char *t, char **name, char **email, unsigned long *date) +static char *parse_user(char *t, char **name, char **email, unsigned long *date) { char *p = t; int mode = 1; @@ -99,14 +101,21 @@ char *parse_user(char *t, char **name, char **email, unsigned long *date) #ifdef NO_ICONV #define reencode(a, b, c) #else -const char *reencode(char **txt, const char *src_enc, const char *dst_enc) +static const char *reencode(char **txt, const char *src_enc, const char *dst_enc) { char *tmp; - if (!txt || !*txt || !src_enc || !dst_enc) + if (!txt) + return NULL; + + if (!*txt || !src_enc || !dst_enc) + return *txt; + + /* no encoding needed if src_enc equals dst_enc */ + if (!strcasecmp(src_enc, dst_enc)) return *txt; - tmp = reencode_string(*txt, src_enc, dst_enc); + tmp = reencode_string(*txt, dst_enc, src_enc); if (tmp) { free(*txt); *txt = tmp; @@ -118,7 +127,7 @@ const char *reencode(char **txt, const char *src_enc, const char *dst_enc) struct commitinfo *cgit_parse_commit(struct commit *commit) { struct commitinfo *ret; - char *p = commit->buffer, *t = commit->buffer; + char *p = commit->buffer, *t; ret = xmalloc(sizeof(*ret)); ret->commit = commit; @@ -160,6 +169,10 @@ struct commitinfo *cgit_parse_commit(struct commit *commit) } } + /* if no special encoding is found, assume UTF-8 */ + if (!ret->msg_encoding) + ret->msg_encoding = xstrdup("UTF-8"); + // skip unknown header fields while (p && *p && (*p != '\n')) { p = strchr(p, '\n'); @@ -189,10 +202,12 @@ struct commitinfo *cgit_parse_commit(struct commit *commit) } else ret->subject = xstrdup(p); - if (ret->msg_encoding) { - reencode(&ret->subject, PAGE_ENCODING, ret->msg_encoding); - reencode(&ret->msg, PAGE_ENCODING, ret->msg_encoding); - } + reencode(&ret->author, ret->msg_encoding, PAGE_ENCODING); + reencode(&ret->author_email, ret->msg_encoding, PAGE_ENCODING); + reencode(&ret->committer, ret->msg_encoding, PAGE_ENCODING); + reencode(&ret->committer_email, ret->msg_encoding, PAGE_ENCODING); + reencode(&ret->subject, ret->msg_encoding, PAGE_ENCODING); + reencode(&ret->msg, ret->msg_encoding, PAGE_ENCODING); return ret; }