void* halloc(HConnect *c, ulong n) { void *p; p = binalloc(&c->bin, n, 1); if(p == nil) sysfatal("out of memory"); return p; }
NList* mkNList(uint32_t n, NList *next) { NList *nl; nl = binalloc(&parseBin, sizeof(NList), 0); if(nl == nil) parseErr("out of memory"); nl->n = n; nl->next = next; return nl; }
Fetch * mkFetch(int op, Fetch *next) { Fetch *f; f = binalloc(&parseBin, sizeof(Fetch), 1); if(f == nil) parseErr("out of memory"); f->op = op; f->next = next; return f; }
SList* mkSList(char *s, SList *next) { SList *sl; sl = binalloc(&parseBin, sizeof(SList), 0); if(sl == nil) parseErr("out of memory"); sl->s = s; sl->next = next; return sl; }
static void markvisited(uint8_t score[VtScoreSize], int type) { ScoreTree *a; Avl *old; if(scoretree == nil) return; a = binalloc(&scorebin, sizeof *a, 1); memmove(a->score, score, VtScoreSize); a->type = type; insertavl(scoretree, &a->avl, &old); }
Store * mkStore(int sign, int op, int flags) { Store *st; st = binalloc(&parseBin, sizeof(Store), 1); if(st == nil) parseErr("out of memory"); st->sign = sign; st->op = op; st->flags = flags; return st; }
/* * memory allocators: * h routines call canalloc; they should be used by everything else * note this memory is wiped out at the start of each new request * note: these routines probably shouldn't fatal. */ char* hstrdup(HConnect *c, char *s) { char *t; int n; n = strlen(s) + 1; t = binalloc(&c->bin, n, 0); if(t == nil) sysfatal("out of memory"); memmove(t, s, n); return t; }
char * strmutf7(char *s) { char *m; int n; n = strlen(s) * MUtf7Max + 1; m = binalloc(&parseBin, n, 0); if(m == nil) return nil; if(encmutf7(m, n, s) < 0) return nil; return m; }
char* impName(char *name) { char *s; int n; if(cistrcmp(name, "inbox") == 0) name = "mbox"; n = strlen(name) + STRLEN(".imp") + 1; s = binalloc(&parseBin, n, 0); if(s == nil) return nil; snprint(s, n, "%s.imp", name); return s; }
char * mutf7str(char *s) { char *m; int n; /* * n = strlen(s) * UTFmax / (2.67) + 1 * UTFMax / 2.67 == 3 / (8/3) == 9 / 8 */ n = strlen(s); n = (n * 9 + 7) / 8 + 1; m = binalloc(&parseBin, n, 0); if(m == nil) return nil; if(decmutf7(m, n, s) < 0) return nil; return m; }
void splitr(char *s, int c, char **left, char **right) { char *d; int n; n = strlen(s); d = binalloc(&parseBin, n + 1, 0); if(d == nil) parseErr("out of memory"); strcpy(d, s); s = strrchr(d, c); if(s != nil){ *left = d; *s++ = '\0'; *right = s; }else{ *right = d; *left = d + n; } }
void* bingrow(Bin **bin, void *op, ulong osize, ulong size, int zero) { Bin *b; void *np; uintptr p; p = (uintptr)op; b = *bin; if(b != nil && p == b->v && p + size <= b->end){ b->pos = p + size; if(zero) memset((char*)p + osize, 0, size - osize); return op; } np = binalloc(bin, size, zero); if(np == nil) return nil; memmove(np, op, osize); return np; }
/* * rfc 2195 cram-md5 authentication */ char* cramauth(void) { AuthInfo *ai; Chalstate *cs; char *s, *t; int n; if((cs = auth_challenge("proto=cram role=server")) == nil) return "couldn't get cram challenge"; n = cs->nchal; s = binalloc(&parseBin, n * 2, 0); n = enc64(s, n * 2, (uint8_t*)cs->chal, n); Bprint(&bout, "+ "); Bwrite(&bout, s, n); Bprint(&bout, "\r\n"); if(Bflush(&bout) < 0) writeErr(); s = authresp(); if(s == nil) return "client cancelled authentication"; t = strchr(s, ' '); if(t == nil) bye("bad auth response"); *t++ = '\0'; strncpy(username, s, UserNameLen); username[UserNameLen-1] = '\0'; cs->user = username; cs->resp = t; cs->nresp = strlen(t); if((ai = auth_response(cs)) == nil) return "login failed"; auth_freechal(cs); setupuser(ai); return nil; }
static char* authresp(void) { char *s, *t; int n; t = Brdline(&bin, '\n'); n = Blinelen(&bin); if(n < 2) return nil; n--; if(t[n-1] == '\r') n--; t[n] = '\0'; if(n == 0 || strcmp(t, "*") == 0) return nil; s = binalloc(&parseBin, n + 1, 0); n = dec64((uint8_t*)s, n, t, n); s[n] = '\0'; return s; }