summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick White <git@njw.me.uk>2012-07-13 18:20:13 +0100
committerNick White <git@njw.me.uk>2012-07-13 18:20:13 +0100
commit51c41296bca9e06cfa2d53ce3a0837e1a8697b1c (patch)
treea77825784861c85e8509f25cc9d6de9d619b26f6
parent8f03147275f2522fa9d5dd16ed3e875dfffe515e (diff)
Ensure text downloaded is null terminated
-rw-r--r--getabook.c6
-rw-r--r--getbnbook.c6
-rw-r--r--getgbook.c8
-rw-r--r--util.c25
-rw-r--r--util.h6
5 files changed, 29 insertions, 22 deletions
diff --git a/getabook.c b/getabook.c
index d923029..8892394 100644
--- a/getabook.c
+++ b/getabook.c
@@ -73,7 +73,7 @@ int getpagelist()
snprintf(url, URLMAX, "/gp/search-inside/service-data?method=getBookData&asin=%s", bookid);
- if(!get("www.amazon.com", url, NULL, NULL, &buf))
+ if(!get("www.amazon.com", url, NULL, NULL, &buf, 1))
return 1;
/* amazon have a canonical asin, which is needed to get all available pages */
@@ -114,7 +114,7 @@ int getpageurls(int pagenum) {
strncpy(url, "/gp/search-inside/service-data", URLMAX);
snprintf(query, URLMAX, "method=goToPage&asin=%s&page=%d", bookid, pagenum);
- if(!post("www.amazon.com", url, NULL, NULL, query, &buf))
+ if(!post("www.amazon.com", url, NULL, NULL, query, &buf, 1))
return 1;
fillurls(buf);
@@ -133,7 +133,7 @@ int getpage(Page *page)
return 1;
}
- if(gettofile("sitb-images.amazon.com", page->url, NULL, NULL, path)) {
+ if(gettofile("sitb-images.amazon.com", page->url, NULL, NULL, path, 0)) {
fprintf(stderr, "%d failed\n", page->num);
return 1;
}
diff --git a/getbnbook.c b/getbnbook.c
index d6f06b8..dba1856 100644
--- a/getbnbook.c
+++ b/getbnbook.c
@@ -39,7 +39,7 @@ int getpagelist()
snprintf(url, URLMAX, "/DigBooks/viewer/bookviewmanager.aspx?op=getbookinfo&ean=%s", bookid);
- if(!get("search2.barnesandnoble.com", url, cookies, NULL, &buf))
+ if(!get("search2.barnesandnoble.com", url, cookies, NULL, &buf, 1))
return 1;
/* find page url structure */
@@ -79,7 +79,7 @@ int getpage(int pagenum)
s=strchr(urlpath+7, '/');
snprintf(pageurl, STRMAX, s, pagenum);
- if(gettofile("search2.barnesandnoble.com", pageurl, cookies, NULL, path)) {
+ if(gettofile("search2.barnesandnoble.com", pageurl, cookies, NULL, path, 0)) {
fprintf(stderr, "%d failed\n", pagenum);
return 1;
}
@@ -111,7 +111,7 @@ int main(int argc, char *argv[])
bookdir = argv[argc-1];
/* get cookie */
- if(get("www.barnesandnoble.com", "/", NULL, cookies, &tmp))
+ if(get("www.barnesandnoble.com", "/", NULL, cookies, &tmp, 0))
free(tmp);
if(getpagelist()) {
diff --git a/getgbook.c b/getgbook.c
index 2042a75..b869b7f 100644
--- a/getgbook.c
+++ b/getgbook.c
@@ -44,7 +44,7 @@ int getpagelist()
snprintf(url, URLMAX, "/books?id=%s&printsec=frontcover&redir_esc=y", bookid);
- if(!get("books.google.com", url, NULL, NULL, &buf))
+ if(!get("books.google.com", url, NULL, NULL, &buf, 1))
return 1;
if((s = strstr(buf, "_OC_Run({\"page\":[")) == NULL)
@@ -83,7 +83,7 @@ int getpageurls(char *pagecode, char *cookie)
snprintf(url, URLMAX, "/books?id=%s&pg=%s&jscmd=click3&q=subject:a&redir_esc=y", bookid, pagecode);
- if(!get("books.google.com", url, cookie, NULL, &buf))
+ if(!get("books.google.com", url, cookie, NULL, &buf, 1))
return 1;
c = buf;
@@ -133,7 +133,7 @@ int getpage(Page *page)
return 1;
}
- if(gettofile("books.google.com", page->url, page->cookie, NULL, path)) {
+ if(gettofile("books.google.com", page->url, page->cookie, NULL, path, 0)) {
fprintf(stderr, "%s failed\n", page->name);
return 1;
}
@@ -184,7 +184,7 @@ int main(int argc, char *argv[])
/* get cookies */
for(i=0;i<COOKIENUM;i++) {
- if(get("books.google.com", "/", NULL, cookies[i], &tmp))
+ if(get("books.google.com", "/", NULL, cookies[i], &tmp, 0))
free(tmp);
}
diff --git a/util.c b/util.c
index 2d573b0..e88d8ee 100644
--- a/util.c
+++ b/util.c
@@ -49,13 +49,13 @@ int dial(char *host, char *port) {
return srv;
}
-int request(char *host, char *request, char *savecookie, char **body) {
+int request(char *host, char *request, char *savecookie, char **body, int istext) {
size_t l, res;
int fd, i, p;
char m[256];
char c[COOKIEMAX] = "";
char *headpos;
- size_t headsize;
+ size_t headsize, bodysize;
char headline[BUFSIZ] = "";
char *buf, *cur, *pos;
@@ -76,13 +76,20 @@ int request(char *host, char *request, char *savecookie, char **body) {
}
headpos += 4;
headsize = headpos - buf;
+ bodysize = l - headsize;
+ if(istext) {
+ bodysize++; /* for null terminator */
+ }
/* memcopy from there into a large enough buf */
- if((*body = malloc(sizeof(char *) * (l - headsize))) == NULL) {
+ if((*body = malloc(sizeof(char *) * bodysize)) == NULL) {
free(buf);
return 0;
}
memcpy(*body, headpos, sizeof(char *) * (l - headsize));
+ if(istext) {
+ (*body)[bodysize - 1] = '\0';
+ }
/* parse header as needed */
snprintf(m, 256, "Set-Cookie: %%%ds;", COOKIEMAX-1);
@@ -110,7 +117,7 @@ int request(char *host, char *request, char *savecookie, char **body) {
return l;
}
-int get(char *host, char *path, char *sendcookie, char *savecookie, char **body) {
+int get(char *host, char *path, char *sendcookie, char *savecookie, char **body, int istext) {
char h[BUFSIZ] = "";
char c[COOKIEMAX] = "";
@@ -119,10 +126,10 @@ int get(char *host, char *path, char *sendcookie, char *savecookie, char **body)
snprintf(h, BUFSIZ, "GET %s HTTP/1.0\r\nUser-Agent: getxbook-"VERSION \
" (not mozilla)\r\nHost: %s%s\r\n\r\n", path, host, c);
- return request(host, h, savecookie, body);
+ return request(host, h, savecookie, body, istext);
}
-int post(char *host, char *path, char *sendcookie, char *savecookie, char *data, char **body) {
+int post(char *host, char *path, char *sendcookie, char *savecookie, char *data, char **body, int istext) {
char h[BUFSIZ] = "";
char c[COOKIEMAX] = "";
@@ -134,15 +141,15 @@ int post(char *host, char *path, char *sendcookie, char *savecookie, char *data,
"Host: %s%s\r\n\r\n%s\r\n",
path, (int)strlen(data), host, c, data);
- return request(host, h, savecookie, body);
+ return request(host, h, savecookie, body, istext);
}
-int gettofile(char *host, char *url, char *sendcookie, char *savecookie, char *savepath) {
+int gettofile(char *host, char *url, char *sendcookie, char *savecookie, char *savepath, int istext) {
char *buf = 0;
FILE *f;
size_t i, l;
- if(!(l = get(host, url, sendcookie, savecookie, &buf))) {
+ if(!(l = get(host, url, sendcookie, savecookie, &buf, istext))) {
fprintf(stderr, "Could not download %s\n", url);
return 1;
}
diff --git a/util.h b/util.h
index 35bbd74..6959ddc 100644
--- a/util.h
+++ b/util.h
@@ -1,7 +1,7 @@
/* See COPYING file for copyright and license details. */
#define COOKIEMAX 1024
int dial(char *host, char *port);
-int get(char *host, char *path, char *sendcookie, char *savecookie, char **body);
-int post(char *host, char *path, char *sendcookie, char *savecookie, char *data, char **body);
-int gettofile(char *host, char *url, char *sendcookie, char *savecookie, char *savepath);
+int get(char *host, char *path, char *sendcookie, char *savecookie, char **body, int istext);
+int post(char *host, char *path, char *sendcookie, char *savecookie, char *data, char **body, int istext);
+int gettofile(char *host, char *url, char *sendcookie, char *savecookie, char *savepath, int istext);
int renameifjpg(char *path);