unsigned int io_debugstring(int64 s,char* buf,unsigned int bufsize) { struct timeval tv; unsigned int i; io_entry* e; gettimeofday(&tv,NULL); e=iarray_get(&io_fds,s); if (!e) return 0; if (bufsize<100) return 0; i=fmt_str(buf,"first_readable "); i+=fmt_long(buf+i,first_readable); i+=fmt_str(buf+i,", first_writeable "); i+=fmt_long(buf+i,first_writeable); #ifdef HAVE_SIGIO i+=fmt_str(buf+i,", alt_firstread "); i+=fmt_long(buf+i,alt_firstread); i+=fmt_str(buf+i,", alt_firstwrite "); i+=fmt_long(buf+i,alt_firstwrite); #endif i+=fmt_str(buf+i,"\n"); i+=fmt_str(buf+i,"fd # "); i+=fmt_ulong(buf+i,s); i+=fmt_str(buf+i,": "); if (bufsize-i<100) return 0; i+=fmt_str(buf+i,"timeout "); i+=fmt_long(buf+i,e->timeout.sec.x-4611686018427387914ULL-tv.tv_sec); i+=fmt_str(buf+i,"."); i+=fmt_ulong(buf+i,e->timeout.nano); i+=fmt_str(buf+i," "); if (bufsize-i<100) return 0; if (e->wantread) i+=fmt_str(buf+i,"wr "); if (e->wantwrite) i+=fmt_str(buf+i,"ww "); if (e->canread) i+=fmt_str(buf+i,"cr "); if (e->canwrite) i+=fmt_str(buf+i,"cw "); if (e->nonblock) i+=fmt_str(buf+i,"nb "); if (!e->inuse) i+=fmt_str(buf+i,"!inuse "); if (e->kernelwantread) i+=fmt_str(buf+i,"kwr "); if (e->kernelwantwrite) i+=fmt_str(buf+i,"kww "); if (e->epolladded) i+=fmt_str(buf+i,"ea "); if (e->mmapped) { i+=fmt_str(buf+i,"mmap("); i+=fmt_xlong(buf+i,(unsigned long)e->mmapped); i+=fmt_str(buf+i,","); i+=fmt_xlong(buf+i,(unsigned long)e->maplen); i+=fmt_str(buf+i,"@"); i+=fmt_xlonglong(buf+i,(unsigned long)e->mapofs); } if (bufsize-i<100) return 0; i+=fmt_str(buf+i,"next_read "); i+=fmt_long(buf+i,e->next_read); i+=fmt_str(buf+i," next_write "); i+=fmt_long(buf+i,e->next_write); i+=fmt_str(buf+i," cookie "); i+=fmt_xlonglong(buf+i,(unsigned long)e->cookie); buf[i]=0; return i; }
unsigned int ip6_fmt(char *s,char ip[16]) { unsigned int len; unsigned int i; len = 0; i = fmt_xlong(s,((unsigned long) (unsigned char) ip[0] << 8) + (unsigned long) (unsigned char) ip[1]); len += i; if (s) s += i; if (s) *s++ = ':'; ++len; i = fmt_xlong(s,((unsigned long) (unsigned char) ip[2] << 8) + (unsigned long) (unsigned char) ip[3]); len += i; if (s) s += i; if (s) *s++ = ':'; ++len; i = fmt_xlong(s,((unsigned long) (unsigned char) ip[4] << 8) + (unsigned long) (unsigned char) ip[5]); len += i; if (s) s += i; if (s) *s++ = ':'; ++len; i = fmt_xlong(s,((unsigned long) (unsigned char) ip[6] << 8) + (unsigned long) (unsigned char) ip[7]); len += i; if (s) s += i; if (s) *s++ = ':'; ++len; i = fmt_xlong(s,((unsigned long) (unsigned char) ip[8] << 8) + (unsigned long) (unsigned char) ip[9]); len += i; if (s) s += i; if (s) *s++ = ':'; ++len; i = fmt_xlong(s,((unsigned long) (unsigned char) ip[10] << 8) + (unsigned long) (unsigned char) ip[11]); len += i; if (s) s += i; if (s) *s++ = ':'; ++len; i = fmt_xlong(s,((unsigned long) (unsigned char) ip[12] << 8) + (unsigned long) (unsigned char) ip[13]); len += i; if (s) s += i; if (s) *s++ = ':'; ++len; i = fmt_xlong(s,((unsigned long) (unsigned char) ip[14] << 8) + (unsigned long) (unsigned char) ip[15]); len += i; if (s) s += i; if (s) *s=0; return len; }
size_t fmt_escapecharxml(char* dest,uint32_t ch) { char a[FMT_LONG], b[FMT_XLONG]; const char* s; size_t i,j; switch (ch) { case '&': s="&"; goto string; case '<': s="<"; goto string; case '>': s=">"; goto string; case '\'': s="'"; goto string; case '"': s="""; goto string; default: a[i=fmt_ulong(a,ch)]=0; b[0]='x'; b[j=fmt_xlong(b+1,ch)+1]=0; s=a; if (i>j) { s=b; i=j; } if (dest) { dest[0]='&'; dest[1]='#'; byte_copy(dest+2,i,s); dest[i+2]=';'; } return i+3; } string: return fmt_str(dest,s); }
unsigned int ip6_fmt(char *s,const char ip[16]) { unsigned int len; unsigned int i; unsigned int temp; unsigned int compressing; int j; len = 0; compressing = 0; for (j=0; j<16; j+=2) { if (j==12 && ip6_isv4mapped(ip)) { temp=ip4_fmt(s,ip+12); len+=temp; if (s) s+=temp; break; } temp = ((unsigned long) (unsigned char) ip[j] << 8) + (unsigned long) (unsigned char) ip[j+1]; if (temp == 0) { if (!compressing) { compressing=1; if (j==0) { if (s) *s++=':'; ++len; } } } else { if (compressing) { compressing=0; if (s) *s++=':'; ++len; } i = fmt_xlong(s,temp); len += i; if (s) s += i; if (j<14) { if (s) *s++ = ':'; ++len; } } } /* if (s) *s=0; */ return len; }
unsigned int ip6_compactaddr(char *s,char ip[16]) { unsigned long len,temp, k, pos0 = 0,len0 = 0, pos1 = 0, compr = 0; // const unsigned char V4mappedprefix[12] = {0,0,0,0,0,0,0,0,0,0,0xff,0xff}; for (k=0; k<16; k+=2) { if (ip[k] == 0 && ip[k+1] == 0) { if (!compr) { compr=1; pos1=k; } if (k == 14) { k = 16; goto last; } } else if (compr) { last: if ((temp=k-pos1) > len0) { len0 = temp; pos0 = pos1; } compr=0; } } for (len=0,k=0; k<16; k+=2) { if (k == 12 && (byte_equal(ip,12,V4mappedprefix))) { len += ip4_fmt(s,ip+12); break; } if (pos0 == k && len0) { if (k == 0) { ++len; if (s) *s++ = ':'; } ++len; if (s) *s++ = ':'; k += len0-2; continue; } temp = ((unsigned long) (unsigned char) ip[k] << 8) + (unsigned long) (unsigned char) ip[k+1]; temp = fmt_xlong(s,temp); len += temp; if (s) s += temp; if (k<14) { ++len; if (s) *s++ = ':'; } } return len; }
unsigned int fmt_ip6(char *s,const char ip[16]) { unsigned long len,temp, k, pos0=0,len0=0, pos1=0, compr=0; for (k=0; k<16; k+=2) { if (ip[k]==0 && ip[k+1]==0) { if (!compr) { compr=1; pos1=k; } if (k==14) { k=16; goto last; } } else if (compr) { last: if ((temp=k-pos1) > len0) { len0=temp; pos0=pos1; } compr=0; } } for (len=0,k=0; k<16; k+=2) { if (k==12 && ip6_isv4mapped(ip)) { len += ip4_fmt(s,ip+12); break; } if (pos0==k && len0) { if (k==0) { ++len; if (s) *s++ = ':'; } ++len; if (s) *s++ = ':'; k += len0-2; continue; } temp = ((unsigned long) (unsigned char) ip[k] << 8) + (unsigned long) (unsigned char) ip[k+1]; temp = fmt_xlong(s,temp); len += temp; if (s) s += temp; if (k<14) { ++len; if (s) *s++ = ':'; } } return len; }
/* src is UTF-8 encoded */ size_t fmt_jsonescape(char* dest,const char* src,size_t len) { register const unsigned char* s=(const unsigned char*) src; size_t written=0,i; char c; for (i=0; i<len; ++i) { switch (s[i]) { case '\\': case '"': c=s[i]; escape: if (dest) { dest[written]='\\'; dest[written+1]=c; } written+=2; break; case '\n': c='n'; goto escape; case '\r': c='r'; goto escape; case '\b': c='b'; goto escape; case '\t': c='t'; goto escape; case '\f': c='f'; goto escape; default: if (s[i]<' ') { if (dest) { dest[written]='\\'; dest[written+1]='u'; dest[written+2]='0'; dest[written+3]='0'; dest[written+4]=fmt_tohex(s[i]>>4); dest[written+5]=fmt_tohex(s[i]&0xf); } written+=6; } else if (s[i]>0x7f) { /* UTF-8! Convert to surrogate pair if needed. */ uint32_t u; size_t j=scan_utf8_sem((const char*)s+i,len-i,&u); if (j==0) { /* Invalid UTF-8! Try to limp on! */ written+=fmt_utf8(dest?dest+written:0,s[i]); break; } /* It turns out we are not required to escape these. * So we won't. */ #if 0 if (u>0xffff) { if (dest) { dest[written ]='\\'; dest[written+1]='u'; fmt_xlong(dest+written+2,0xd800 + ((u>>10) & 0x3bf)); dest[written+6]='\\'; dest[written+7]='u'; fmt_xlong(dest+written+8,0xdc00 + (u & 0x3ff)); } written+=12; } else #endif { if (dest) memcpy(dest+written,s+i,j); written+=j; } i+=j-1; /* -1 because the for loop will also add 1 */ break; } else {