summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--sfeed.c4
-rw-r--r--sfeed_web.c2
-rw-r--r--util.c62
-rw-r--r--util.h3
4 files changed, 34 insertions, 37 deletions
diff --git a/sfeed.c b/sfeed.c
index f289461..e52207c 100644
--- 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
index 3b2b43c..525bd84 100644
--- 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
index 399bb78..c867a09 100644
--- 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
index ef874e7..3def3c7 100644
--- 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);