sfeed

Simple RSS and Atom feed parser
git clone https://git.sinitax.com/codemadness/sfeed
Log | Files | Refs | README | LICENSE | Upstream | sfeed.txt

commit 8df3ab3b50d6ca61859f6c8f1d49913276a4d32d
parent 4a7f419877607b735b26c56dacd566023c3d7da7
Author: Hiltjo Posthuma <hiltjo@codemadness.org>
Date:   Sun, 10 Apr 2016 20:19:29 +0200

absuri, encodeuri: make encodeuri static, change argument order

Diffstat:
Msfeed.c | 4++--
Msfeed_web.c | 2+-
Mutil.c | 62++++++++++++++++++++++++++++++--------------------------------
Mutil.h | 3+--
4 files changed, 34 insertions(+), 37 deletions(-)

diff --git a/sfeed.c b/sfeed.c @@ -535,8 +535,8 @@ printfields(void) putchar(FieldSeparator); /* always print absolute urls */ if (ctx.fields[FeedFieldLink].str.data && - absuri(ctx.fields[FeedFieldLink].str.data, baseurl, link, - sizeof(link)) != -1) + absuri(link, sizeof(link), ctx.fields[FeedFieldLink].str.data, + baseurl) != -1) fputs(link, stdout); putchar(FieldSeparator); string_print_encoded(&ctx.fields[FeedFieldContent].str); diff --git a/sfeed_web.c b/sfeed_web.c @@ -44,7 +44,7 @@ xmltagstartparsed(XMLParser *p, const char *tag, size_t taglen, int isshort) if (!isfeedlink) return; - if (absuri(feedlink, basehref, abslink, sizeof(abslink)) != -1) + if (absuri(abslink, sizeof(abslink), feedlink, basehref) != -1) fputs(abslink, stdout); fputc('\t', stdout); printfeedtype(feedtype, stdout); diff --git a/util.c b/util.c @@ -100,10 +100,38 @@ readpath: return 0; } +static int +encodeuri(char *buf, size_t bufsiz, const char *s) +{ + static const char *table = "0123456789ABCDEF"; + size_t i, b; + + for (i = 0, b = 0; s[i]; i++) { + if ((int)s[i] == ' ' || + (unsigned char)s[i] > 127 || + iscntrl((int)s[i])) { + if (b + 3 >= bufsiz) + return -1; + buf[b++] = '%'; + buf[b++] = table[((uint8_t)s[i] >> 4) & 15]; + buf[b++] = table[(uint8_t)s[i] & 15]; + } else if (b < bufsiz) { + buf[b++] = s[i]; + } else { + return -1; + } + } + if (b >= bufsiz) + return -1; + buf[b] = '\0'; + + return 0; +} + /* Get absolute uri; if `link` is relative use `base` to make it absolute. * the returned string in `buf` is uri encoded, see: encodeuri(). */ int -absuri(const char *link, const char *base, char *buf, size_t bufsiz) +absuri(char *buf, size_t bufsiz, const char *link, const char *base) { struct uri ulink, ubase; char tmp[4096], *host, *p, *port; @@ -154,37 +182,7 @@ absuri(const char *link, const char *base, char *buf, size_t bufsiz) if (strlcat(tmp, ulink.path, sizeof(tmp)) >= sizeof(tmp)) return -1; - return encodeuri(tmp, buf, bufsiz); -} - -int -encodeuri(const char *s, char *buf, size_t bufsiz) -{ - static const char *table = "0123456789ABCDEF"; - size_t i, b; - - if (!bufsiz) - return -1; - for (i = 0, b = 0; s[i]; i++) { - if ((int)s[i] == ' ' || - (unsigned char)s[i] > 127 || - iscntrl((int)s[i])) { - if (b + 3 >= bufsiz) - return -1; - buf[b++] = '%'; - buf[b++] = table[((uint8_t)s[i] >> 4) & 15]; - buf[b++] = table[(uint8_t)s[i] & 15]; - } else { - if (b >= bufsiz) - return -1; - buf[b++] = s[i]; - } - } - if (b >= bufsiz) - return -1; - buf[b] = '\0'; - - return 0; + return encodeuri(buf, bufsiz, tmp); } /* Read a field-separated line from 'fp', diff --git a/util.h b/util.h @@ -31,8 +31,7 @@ enum { FieldFeedType, FieldLast }; -int absuri(const char *, const char *, char *, size_t); -int encodeuri(const char *, char *, size_t); +int absuri(char *, size_t, const char *, const char *); size_t parseline(char *, char *[FieldLast]); int parseuri(const char *, struct uri *, int); void printutf8pad(FILE *, const char *, size_t, int);