Esempio n. 1
0
void toBase64(std::vector<unsigned char>&retval,
              const MemoryReference&firstInput, 
              const MemoryReference&secondInput){
    const MemoryReference*refs[2];
    refs[0]=&firstInput;
    refs[1]=&secondInput;
    unsigned int datalen=0;
    uint8 data[3];
    size_t curPlace=retval.size();
    retval.resize(curPlace+conservativeBase64Size(refs[1]->size()+refs[2]->size()));
    size_t retvalSize=retval.size();
    for (int i=0;i<3;++i) {
        const uint8*dat=(const uint8*)refs[i]->data();
        uint32 size=refs[i]->size();
        for (uint32 j=0;j<size;++j) {
            data[datalen++]=dat[j];
            if (datalen==3) {
                if (retvalSize<=curPlace+5) {
                    retval.resize(curPlace+5);
                }
                curPlace+=translateBase64(&*(retval.begin()+curPlace),data,datalen);
                datalen=0;
            }
        }
    }
    if (datalen) {
        if (retvalSize<=curPlace+5) {
            retval.resize(curPlace+5);
            fprintf(stderr,"conservative size estimate incorrect\n");
        }        
        curPlace+=translateBase64(&*(retval.begin()+curPlace),data,datalen);
    }
    retval.resize(curPlace);
}
Esempio n. 2
0
Chunk* ASIOSocketWrapper::toBase64ZeroDelim(const MemoryReference&a, const MemoryReference&b, const MemoryReference&c, const MemoryReference*rawBytesToPrepend) {
    const MemoryReference*refs[3];
    refs[0]=&a;
    refs[1]=&b;
    refs[2]=&c;
    unsigned int datalen=0;
    uint8 data[3];
    Chunk * retval= new Chunk((rawBytesToPrepend?rawBytesToPrepend->size():0)+conservativeBase64Size(a.size()+b.size()+c.size())+2);
    Chunk::iterator prependStart=retval->begin();
    *(prependStart++)='\0';//frame start
    if (rawBytesToPrepend) {
        memcpy(&*prependStart,rawBytesToPrepend->data(),rawBytesToPrepend->size());
        prependStart+=rawBytesToPrepend->size();
    }
    size_t retvalSize=retval->size();
    unsigned int curPlace=(unsigned int)(prependStart-retval->begin());
    for (int i=0; i<3; ++i) {
        const uint8*dat=(const uint8*)refs[i]->data();
        uint32 size=refs[i]->size();
        for (uint32 j=0; j<size; ++j) {
            data[datalen++]=dat[j];
            if (datalen==3) {
                if (retvalSize<=curPlace+5) {
                    retval->resize(curPlace+5);
                }
                curPlace+=translateBase64(&*(retval->begin()+curPlace),data,datalen);
                datalen=0;
            }
        }
    }
    if (datalen) {
        if (retvalSize<=curPlace+5) {
            retval->resize(curPlace+5);
            SILOG(tcpsst,error,"conservative size estimate incorrect");
        }
        curPlace+=translateBase64(&*(retval->begin()+curPlace),data,datalen);
    }
    (*retval)[curPlace]=0xff;//0xff DELIMITED
    retval->resize(curPlace+1);
    return retval;
}