All pastes #479146 Raw Edit

Anonymous

public cpp v1 · immutable
·published 2007-05-09 15:53 UTC
rendered paste body
void putint(ucharbuf &p, int n){    if(n<128 && n>-127) p.put(n);    else if(n<0x8000 && n>=-0x8000) { p.put(0x80); p.put(n); p.put(n>>8); }    else { p.put(0x81); p.put(n); p.put(n>>8); p.put(n>>16); p.put(n>>24); }}int getint(ucharbuf &p){    int c = (char)p.get();    if(c==-128) { int n = p.get(); n |= char(p.get())<<8; return n; }    else if(c==-127) { int n = p.get(); n |= p.get()<<8; n |= p.get()<<16; return n|(p.get()<<24); }    else return c;}// much smaller encoding for unsigned integers up to 28 bits, but can handle signedvoid putuint(ucharbuf &p, int n){    if(n < 0 || n >= (1<<21))    {        p.put(0x80 | (n & 0x7F));        p.put(0x80 | ((n >> 7) & 0x7F));        p.put(0x80 | ((n >> 14) & 0x7F));        p.put(n >> 21);    }    else if(n < (1<<7)) p.put(n);    else if(n < (1<<14))    {        p.put(0x80 | (n & 0x7F));        p.put(n >> 7);    }    else    {        p.put(0x80 | (n & 0x7F));        p.put(0x80 | ((n >> 7) & 0x7F));        p.put(n >> 14);    }}int getuint(ucharbuf &p){    int n = p.get();    if(n & 0x80)    {        n += (p.get() << 7) - 0x80;        if(n & (1<<14)) n += (p.get() << 14) - (1<<14);        if(n & (1<<21)) n += (p.get() << 21) - (1<<21);        if(n & (1<<28)) n |= 0xF0000000;    }    return n;}