#include <config.h>
#include <ctype.h>
#include <string.h>
#include <limits.h>
#include "prot.h"
#include "xmalloc.h"
#include "global.h"
#include "exitcodes.h"
enum {
MAXQUOTED = 32768,
MAXWORD = 32768,
MAXLITERAL = INT_MAX / 20
};
void freebuf(struct buf *buf)
{
if (buf->s) {
free(buf->s);
buf->s = NULL;
}
buf->len = 0;
buf->alloc = 0;
}
#define BUFGROWSIZE 100
int getword(struct protstream *in, struct buf *buf)
{
int c;
int len = 0;
if (buf->alloc == 0) {
buf->alloc = BUFGROWSIZE;
buf->s = xmalloc(buf->alloc+1);
}
for (;;) {
c = prot_getc(in);
if (c == EOF || isspace(c) || c == '(' || c == ')' || c == '\"') {
buf->s[len] = '\0';
buf->len = len;
return c;
}
if (len == buf->alloc) {
buf->alloc += BUFGROWSIZE;
buf->s = xrealloc(buf->s, buf->alloc+1);
if (len > MAXWORD) {
fatal("word too long", EC_IOERR);
}
}
buf->s[len++] = c;
}
}
int getxstring(struct protstream *pin, struct protstream *pout,
struct buf *buf, int type)
{
int c;
int i;
int len = 0;
int sawdigit = 0;
int isnowait;
if (buf->alloc == 0) {
buf->alloc = BUFGROWSIZE;
buf->s = xmalloc(buf->alloc+1);
}
c = prot_getc(pin);
switch (c) {
case EOF:
case ' ':
case '(':
case ')':
case '\r':
case '\n':
buf->s[0] = '\0';
buf->len = 0;
if (c != EOF) prot_ungetc(c, pin);
return EOF;
case '\"':
for (;;) {
c = prot_getc(pin);
if (c == '\\') {
c = prot_getc(pin);
}
else if (c == '\"') {
buf->s[len] = '\0';
buf->len = len;
return prot_getc(pin);
}
else if (c == EOF || c == '\r' || c == '\n') {
buf->s[len] = '\0';
buf->len = len;
if (c != EOF) prot_ungetc(c, pin);
return EOF;
}
if (len == buf->alloc) {
buf->alloc += BUFGROWSIZE;
buf->s = xrealloc(buf->s, buf->alloc+1);
if (len > MAXQUOTED) {
fatal("word too long", EC_IOERR);
}
}
buf->s[len++] = c;
}
case '{':
if (type == IMAP_QSTRING) {
buf->s[0] = '\0';
buf->len = 0;
if (c != EOF) prot_ungetc(c, pin);
return EOF;
}
isnowait = 0;
buf->s[0] = '\0';
while ((c = prot_getc(pin)) != EOF && isdigit(c)) {
sawdigit = 1;
len = len*10 + c - '0';
if (len > MAXLITERAL || len < 0) {
fatal("literal too big", EC_IOERR);
}
}
if (c == '+') {
isnowait++;
c = prot_getc(pin);
}
if (!sawdigit || c != '}') {
if (c != EOF) prot_ungetc(c, pin);
return EOF;
}
c = prot_getc(pin);
if (c != '\r') {
if (c != EOF) prot_ungetc(c, pin);
return EOF;
}
c = prot_getc(pin);
if (c != '\n') {
if (c != EOF) prot_ungetc(c, pin);
return EOF;
}
if (len >= buf->alloc) {
buf->alloc = len+1;
buf->s = xrealloc(buf->s, buf->alloc+1);
}
if (!isnowait) {
prot_printf(pout, "+ go ahead\r\n");
prot_flush(pout);
}
for (i = 0; i < len; i++) {
c = prot_getc(pin);
if (c == EOF) {
buf->s[len] = '\0';
buf->len = len;
return EOF;
}
buf->s[i] = c;
}
buf->s[len] = '\0';
buf->len = len;
if (type != IMAP_BIN_ASTRING && strlen(buf->s) != len)
return EOF;
return prot_getc(pin);
default:
switch (type) {
case IMAP_BIN_ASTRING:
case IMAP_ASTRING:
for (;;) {
if (c == EOF || isspace(c) || c == '(' ||
c == ')' || c == '\"') {
buf->s[len] = '\0';
buf->len = len;
return c;
}
if (len == buf->alloc) {
buf->alloc += BUFGROWSIZE;
buf->s = xrealloc(buf->s, buf->alloc+1);
}
buf->s[len++] = c;
c = prot_getc(pin);
}
break;
case IMAP_NSTRING:
if (c == 'N') {
prot_ungetc(c, pin);
c = getword(pin, buf);
if (!strcmp(buf->s, "NIL"))
return c;
}
if (c != EOF) prot_ungetc(c, pin);
return EOF;
break;
case IMAP_QSTRING:
case IMAP_STRING:
if (c != EOF) prot_ungetc(c, pin);
return EOF;
break;
}
}
return EOF;
}
void eatline(struct protstream *pin, int c)
{
int state = 0;
char *statediagram = " {+}\r";
int size = -1;
for (;;) {
if (c == '\n') return;
if (c == statediagram[state+1]) {
state++;
if (state == 1) size = 0;
else if (c == '\r') {
c = prot_getc(pin);
while (size--) {
c = prot_getc(pin);
}
state = 0;
}
}
else if (state == 1 && isdigit(c)) {
size = size * 10 + c - '0';
}
else state = 0;
c = prot_getc(pin);
if (c == EOF) return;
}
}