blaze822: api refactoring

pull/1/merge
Christian Neukirchen 8 years ago
parent 7c0663e0e5
commit 17355dbe63

@ -337,8 +337,7 @@ blaze822(char *file)
mesg->msg = buf; mesg->msg = buf;
mesg->end = end; mesg->end = end;
mesg->body = 0; mesg->body = mesg->bodyend = mesg->bodychunk = 0;
mesg->bodyend = 0;
return mesg; return mesg;
} }
@ -355,41 +354,37 @@ blaze822_mem(char *src, size_t len)
return 0; return 0;
end = memmem(src, len, "\n\n", 2); end = memmem(src, len, "\n\n", 2);
if (!end) end = memmem(src, len, "\r\n\r\n", 4); if (!end)
if (!end) end = src + len; end = memmem(src, len, "\r\n\r\n", 4);
if (!end)
end = src + len;
len = end - src; size_t hlen = end - src;
buf = malloc(len+1); buf = malloc(hlen+1);
if (!buf) if (!buf)
return 0; return 0;
memcpy(buf, src, len); memcpy(buf, src, hlen);
end = buf+len+1; end = buf+hlen+1;
*end = 0; // dereferencing *end is safe *end = 0; // dereferencing *end is safe
unfold_hdr(buf, end); unfold_hdr(buf, end);
mesg->msg = buf; mesg->msg = buf;
mesg->end = end; mesg->end = end;
mesg->body = 0; mesg->body = src + hlen;
mesg->bodyend = 0; mesg->bodyend = src + len;
mesg->bodychunk = 0; // src is not ours
return mesg; return mesg;
} }
void
blaze822_mem_body(struct message *mesg, char *buf, size_t len)
{
mesg->body = buf + (mesg->end - mesg->msg - 2);
mesg->bodyend = buf + len - (mesg->end - mesg->msg - 2);
}
void void
blaze822_free(struct message *mesg) blaze822_free(struct message *mesg)
{ {
free(mesg->msg); free(mesg->msg);
// XXX body? keep track who malloced it? free(mesg->bodychunk);
free(mesg); free(mesg);
} }
@ -434,24 +429,8 @@ blaze822_loop(int argc, char *argv[], void (*cb)(char *))
return i; return i;
} }
int struct message *
blaze822_body(struct message *mesg, char *file) blaze822_file(char *file)
{
int fd = open(file, O_RDONLY);
if (fd < 0)
return fd;
if (lseek(fd, mesg->end - mesg->msg - 2, SEEK_SET) < 0) {
perror("lseek");
close(fd);
return -1;
}
return fd;
}
int
blaze822_file_body(struct message *mesg, char *file)
{ {
int fd = open(file, O_RDONLY); int fd = open(file, O_RDONLY);
if (fd < 0) if (fd < 0)
@ -461,29 +440,39 @@ blaze822_file_body(struct message *mesg, char *file)
if (fstat(fd, &st) < 0) if (fstat(fd, &st) < 0)
goto error; goto error;
if (lseek(fd, mesg->end - mesg->msg - 2, SEEK_SET) < 0) { size_t s = st.st_size;
perror("lseek");
goto error;
}
size_t s = st.st_size - (mesg->end - mesg->msg - 2);
char *body = malloc(s+1); char *buf = malloc(s+1);
if (read(fd, body, s) < 0) { if (read(fd, buf, s) < 0) {
// XXX handle short reads? // XXX handle short reads?
perror("read"); perror("read");
goto error; goto error;
} }
body[s] = 0; close(fd);
mesg->body = body; buf[s] = 0;
mesg->bodyend = body+s;
close(fd); // XXX duplicate header in ram...
return 1; struct message *mesg = blaze822_mem(buf, s);
if (mesg)
mesg->bodychunk = buf;
return mesg;
error: error:
close(fd); close(fd);
return -1; return 0;
} }
char *
blaze822_body(struct message *mesg)
{
return mesg->body;
}
size_t
blaze822_bodylen(struct message *mesg)
{
if (!mesg->body || !mesg->bodyend)
return 0;
return mesg->bodyend - mesg->body;
}

@ -1,20 +1,29 @@
#include <stdint.h> #include <stdint.h>
#include <sys/types.h>
struct message; struct message;
struct message *blaze822(char *file); // blaze822.c
struct message *blaze822_mem(char *buf, size_t len);
void blaze822_free(struct message *mesg); struct message *blaze822(char *file); // just header
struct message *blaze822_file(char *file); // header + body
struct message *blaze822_mem(char *buf, size_t len); // header + body
char *blaze822_hdr_(struct message *mesg, const char *hdr, size_t len); char *blaze822_hdr_(struct message *mesg, const char *hdr, size_t len);
#define blaze822_hdr(mesg, hdr) blaze822_hdr_(mesg, "\0" hdr ":", 2+strlen((hdr))) #define blaze822_hdr(mesg, hdr) blaze822_hdr_(mesg, "\0" hdr ":", 2+strlen((hdr)))
int blaze822_body(struct message *mesg, char *file);
void blaze822_mem_body(struct message *mesg, char *buf, size_t len);
int blaze822_loop(int, char **, void (*)(char *)); void blaze822_free(struct message *mesg);
time_t blaze822_date(char *); time_t blaze822_date(char *);
char *blaze822_addr(char *, char **, char **); char *blaze822_addr(char *, char **, char **);
char *blaze822_body(struct message *mesg);
size_t blaze822_bodylen(struct message *mesg);
int blaze822_loop(int, char **, void (*)(char *));
// rfc2047.c
int blaze822_decode_rfc2047(char *, char *, size_t, char *); int blaze822_decode_rfc2047(char *, char *, size_t, char *);
int blaze822_decode_qp(char *start, char *stop, char **deco, size_t *decleno);
int blaze822_decode_b64(char *start, char *stop, char **deco, size_t *decleno);

@ -3,4 +3,7 @@ struct message {
char *end; char *end;
char *body; char *body;
char *bodyend; char *bodyend;
char *bodychunk;
}; };
#define iswsp(c) (((c) == ' ' || (c) == '\t'))

@ -14,7 +14,7 @@
// XXX keep trying bytewise on invalid iconv // XXX keep trying bytewise on invalid iconv
int int
decode_qp(char *start, char *stop, char **deco, size_t *decleno) blaze822_decode_qp(char *start, char *stop, char **deco, size_t *decleno)
{ {
static signed char hex[] = { static signed char hex[] = {
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
@ -55,8 +55,9 @@ decode_qp(char *start, char *stop, char **deco, size_t *decleno)
*decleno = buf - *deco; *decleno = buf - *deco;
return 1; return 1;
} }
int int
decode_b64(char *s, char *e, char **deco, size_t *decleno) blaze822_decode_b64(char *s, char *e, char **deco, size_t *decleno)
{ {
static signed char b64[128] = { static signed char b64[128] = {
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
@ -153,9 +154,9 @@ blaze822_decode_rfc2047(char *dst, char *src, size_t dlen, char *tgtenc)
char *dec; char *dec;
size_t declen; size_t declen;
if (enc == 'q') if (enc == 'q')
decode_qp(start, stop, &dec, &declen); blaze822_decode_qp(start, stop, &dec, &declen);
else if (enc == 'b') else if (enc == 'b')
decode_b64(start, stop, &dec, &declen); blaze822_decode_b64(start, stop, &dec, &declen);
else else
goto nocode; goto nocode;

Loading…
Cancel
Save