#include #include #include #include #include <9p.h> #include "mysql.h" vlong gnum(Pkt *p, int *exists) { int n; if(p->end - p->pos < 1) sysfatal("gnum: packet underflow\n"); if(exists) *exists = 1; n = *p->pos++; if(n <= 250) // 8 bit return n; switch(n){ case 251: // no field if(exists) *exists = 0; return 0; case 252: // 16 bit return g16(p); case 253: // 24 bit return g24(p); case 254: return g64(p); default: break; } sysfatal("gnum: %d unknown type\n", n); return 0; // shut up 8c } void gskip(Pkt *p, int n) { if(p->end-p->pos < n) sysfatal("gskip: packet underflow\n"); p->pos += n; } void * gmem(Pkt *p, int n) { char *t; if(p->end - p->pos < n) sysfatal("gmem: packet underflow\n"); t = emalloc9p(n); memcpy(t, p->pos, n); p->pos += n; return t; } /* zero terminated ascii string */ char * gstr(Pkt *p) { int n; char *t; uchar *s; for(n = 0, s = p->pos; *s; s++, n++) if(s >= p->end) sysfatal("gstr: packet underflow\n"); n++; /* ensure we copy the terminator */ t = emalloc9p(n); memcpy(t, p->pos, n); p->pos += n; return t; } /* string to end of packet, no terminating zero */ char * gsall(Pkt *p) { int n; char *t; if(p->pos > p->end) sysfatal("gsall: packet underflow\n"); n = (p->end - p->pos); t = emalloc9p(n+1); // +1 for terminator memcpy(t, p->pos, n); t[n] = 0; p->pos += n; return t; } /* string, with a variable length number specifying its length */ char * gnstr(Pkt *p) { char *t; int n, exists; n = gnum(p, &exists); if(! exists) return nil; if(p->end-p->pos < n) sysfatal("gskip: packet underflow\n"); t = emalloc9p(n+1); // +1 for terminator memcpy(t, p->pos, n); t[n] = 0; p->pos += n; return t; } uvlong g64(Pkt *p) { uvlong n; if(p->end - p->pos <8) sysfatal("g64 - packet underflow\n"); n = (uvlong)*p->pos++; n |= (uvlong)*p->pos++ << 8; n |= (uvlong)*p->pos++ << 16; n |= (uvlong)*p->pos++ << 24; n |= (uvlong)*p->pos++ << 32; n |= (uvlong)*p->pos++ << 40; n |= (uvlong)*p->pos++ << 48; n |= (uvlong)*p->pos++ << 56; return n; } uint g32(Pkt *p) { uint n; if(p->end - p->pos <4) sysfatal("g32 - packet underflow\n"); n = (uint)*p->pos++; n |= (uint)*p->pos++ << 8; n |= (uint)*p->pos++ << 16; n |= (uint)*p->pos++ << 24; return n; } uint g24(Pkt *p) { uint n; if(p->end - p->pos <3) sysfatal("g24 - packet underflow\n"); n = (uint)*p->pos++; n |= (uint)*p->pos++ << 8; n |= (uint)*p->pos++ << 16; return n; } uint g16(Pkt *p) { uint n; if(p->end - p->pos <2) sysfatal("g16 - packet underflow\n"); n = (uint)*p->pos++; n |= (uint)*p->pos++ << 8; return n; } uint g8(Pkt *p) { if(p->end - p->pos <1) sysfatal("g8 - packet underflow\n"); return (uint)*p->pos++; } uint peek8(Pkt *p) { if(p->end - p->pos <1) sysfatal("peek - packet underflow\n"); return (uint)*p->pos; } /*************************************************/ void pnum(Pkt *p, vlong n, int exists) { if(p->end - p->pos < 1) sysfatal("pnum: packet overflow\n"); if(exists == 0){ // field does not exist *p->pos++ = 251; return; } if(n <= 250){ // 8 bit *p->pos++ = n; return; } if(n < 0xffffLL){ *p->pos++ = 252; p16(p, n); return; } if(n < 0xfffFFfLL){ *p->pos++ = 253; p24(p, n); return; } *p->pos++ = 254; p64(p, n); } void pskip(Pkt *p, int n) { if(p->end - p->pos < n) sysfatal("pskip - packet overflow\n"); p->pos += n; } void pmem(Pkt *p, void *s, int n) { if(p->end - p->pos < n) sysfatal("pmem - packet overflow\n"); memcpy(p->pos, s, n); p->pos += n; } void pstr(Pkt *p, char *s) { int n; n = strlen(s)+1; if(p->end - p->pos < n) sysfatal("pstr - packet overflow\n"); memcpy(p->pos, s, n); p->pos += n; } void p64(Pkt *p, uvlong n) { if(p->end - p->pos < 8) sysfatal("p64 - packet overflow\n"); *(p->pos++) = n & 0xff; *(p->pos++) = (n >> 8) & 0xff; *(p->pos++) = (n >> 16) & 0xff; *(p->pos++) = (n >> 24) & 0xff; *(p->pos++) = (n >> 32) & 0xff; *(p->pos++) = (n >> 40) & 0xff; *(p->pos++) = (n >> 48) & 0xff; *(p->pos++) = (n >> 56) & 0xff; } void p32(Pkt *p, uint n) { if(p->end - p->pos < 4) sysfatal("p32 - packet overflow\n"); *(p->pos++) = n & 0xff; *(p->pos++) = (n >> 8) & 0xff; *(p->pos++) = (n >> 16) & 0xff; *(p->pos++) = (n >> 24) & 0xff; } void p24(Pkt *p, uint n) { if(p->end - p->pos < 3) sysfatal("p24 - packet overflow\n"); *(p->pos++) = n & 0xff; *(p->pos++) = (n >> 8) & 0xff; *(p->pos++) = (n >> 16) & 0xff; } void p16(Pkt *p, uint n) { if(p->end - p->pos < 2) sysfatal("p16 - packet overflow\n"); *(p->pos++) = n & 0xff; *(p->pos++) = (n >> 8) & 0xff; } void p8(Pkt *p, uint n) { if(p->end - p->pos < 1) sysfatal("p8 - packet overflow\n"); *(p->pos++) = n & 0xff; } int remain(Pkt *p) { if(p->end < p->pos) sysfatal("remain - packet overflow\n"); return p->end - p->pos; }