kmdcodec.cpp

来自「将konqueror浏览器移植到ARM9 2410中」· C++ 代码 · 共 1,213 行 · 第 1/3 页

CPP
1,213
字号
}QCString KBase64::base64Decode( const QByteArray& in ){    QByteArray out;    base64Decode( in, out );    return QCString( out.data(), out.size()+1 );}void KBase64::base64Decode( const QByteArray& in, QByteArray& out ){    out.resize(0);    if ( in.isEmpty() )        return;    unsigned int count = 0;    unsigned int len = in.size(), tail = len;    const char* in_buf = in.data();    // Deal with possible *nix "BEGIN" marker!!    while ( count < len && (in_buf[count] == '\n' || in_buf[count] == '\r' ||            in_buf[count] == '\t' || in_buf[count] == ' ') )        count++;    if ( strncasecmp(in_buf+count, "begin", 5) == 0 )    {        count += 5;        while ( count < len && in_buf[count] != '\n' && in_buf[count] != '\r' )            count++;        while ( count < len && (in_buf[count] == '\n' || in_buf[count] == '\r') )            count ++;        in_buf += count;        tail = (len -= count);    }    // Find the tail end of the actual encoded data even if    // there is/are trailing CR and/or LF.    while ( in_buf[tail-1] == '=' || in_buf[tail-1] == '\n' ||            in_buf[tail-1] == '\r' )        if ( in_buf[--tail] != '=' ) len = tail;    unsigned int outIdx = 0;    out.resize( (count=len) );    for (unsigned int idx = 0; idx < count; idx++)    {        // Adhere to RFC 2045 and ignore characters        // that are not part of the encoding table.        char ch = in_buf[idx];        if ((ch > 47 && ch < 58) || (ch > 64 && ch < 91) ||            (ch > 96 && ch < 123) || ch == '+' || ch == '/' || ch == '=')        {            out[outIdx++] = Base64DecMap[ch];        }        else        {            len--;            tail--;        }    }    // kdDebug() << "Tail size = " << tail << ", Length size = " << len << endl;    // 4-byte to 3-byte conversion    len = (tail>(len/4)) ? tail-(len/4) : 0;    unsigned int sidx = 0, didx = 0;    if ( len > 1 )    {      while (didx < len-2)      {          out[didx] = (((out[sidx] << 2) & 255) | ((out[sidx+1] >> 4) & 003));          out[didx+1] = (((out[sidx+1] << 4) & 255) | ((out[sidx+2] >> 2) & 017));          out[didx+2] = (((out[sidx+2] << 6) & 255) | (out[sidx+3] & 077));          sidx += 4;          didx += 3;      }    }    if (didx < len)        out[didx] = (((out[sidx] << 2) & 255) | ((out[sidx+1] >> 4) & 003));    if (++didx < len )        out[didx] = (((out[sidx+1] << 4) & 255) | ((out[sidx+2] >> 2) & 017));    // Resize the output buffer    if ( len == 0 || len < out.size() )      out.resize(len);}QCString KCodecs::uuencode( const QCString& str ){    if ( str.isEmpty() )        return "";    QByteArray in;    in.resize( str.length() );    memcpy( in.data(), str.data(), str.length() );    return uuencode( in );}QCString KCodecs::uuencode( const QByteArray& in ){    QByteArray out;    uuencode( in, out );    return QCString( out.data(), out.size()+1 );}void KCodecs::uuencode( const QByteArray& in, QByteArray& out ){    out.resize( 0 );    if( in.isEmpty() )        return;    unsigned int sidx = 0;    unsigned int didx = 0;    unsigned int line_len = 45;    const char nl[] = "\n";    const char* buf = in.data();    const unsigned int nl_len = strlen(nl);    const unsigned int len = in.size();    out.resize( (len+2)/3*4 + ((len+line_len-1)/line_len)*(nl_len+1) );    // split into lines, adding line-length and line terminator    while (sidx+line_len < len)    {        // line length        out[didx++] = UUEncMap[line_len];        // 3-byte to 4-byte conversion + 0-63 to ascii printable conversion        for (unsigned int end = sidx+line_len; sidx < end; sidx += 3)        {            out[didx++] = UUEncMap[(buf[sidx] >> 2) & 077];            out[didx++] = UUEncMap[(buf[sidx+1] >> 4) & 017 |                                   (buf[sidx] << 4) & 077];            out[didx++] = UUEncMap[(buf[sidx+2] >> 6) & 003 |                                (buf[sidx+1] << 2) & 077];            out[didx++] = UUEncMap[buf[sidx+2] & 077];        }        // line terminator        //for (unsigned int idx=0; idx < nl_len; idx++)        //out[didx++] = nl[idx];        memcpy(out.data()+didx, nl, nl_len);        didx += nl_len;    }    // line length    out[didx++] = UUEncMap[len-sidx];    // 3-byte to 4-byte conversion + 0-63 to ascii printable conversion    while (sidx+2 < len)    {        out[didx++] = UUEncMap[(buf[sidx] >> 2) & 077];        out[didx++] = UUEncMap[(buf[sidx+1] >> 4) & 017 |                               (buf[sidx] << 4) & 077];        out[didx++] = UUEncMap[(buf[sidx+2] >> 6) & 003 |                               (buf[sidx+1] << 2) & 077];        out[didx++] = UUEncMap[buf[sidx+2] & 077];        sidx += 3;    }    if (sidx < len-1)    {        out[didx++] = UUEncMap[(buf[sidx] >> 2) & 077];        out[didx++] = UUEncMap[(buf[sidx+1] >> 4) & 017 |                               (buf[sidx] << 4) & 077];        out[didx++] = UUEncMap[(buf[sidx+1] << 2) & 077];        out[didx++] = UUEncMap[0];    }    else if (sidx < len)    {        out[didx++] = UUEncMap[(buf[sidx] >> 2) & 077];        out[didx++] = UUEncMap[(buf[sidx] << 4) & 077];        out[didx++] = UUEncMap[0];        out[didx++] = UUEncMap[0];    }    // line terminator    memcpy(out.data()+didx, nl, nl_len);    didx += nl_len;    // sanity check    if ( didx != out.size() )        out.resize( 0 );}QCString KCodecs::uudecode( const QCString& str ){    if ( str.isEmpty() )        return "";    QByteArray in;    in.resize( str.length() );    memcpy( in.data(), str.data(), str.length() );    return uudecode( in );}QCString KCodecs::uudecode( const QByteArray& in ){    QByteArray out;    uudecode( in, out );    return QCString( out.data(), out.size()+1 );}void KCodecs::uudecode( const QByteArray& in, QByteArray& out ){    out.resize( 0 );    if( in.isEmpty() )        return;    unsigned int sidx = 0;    unsigned int didx = 0;    unsigned int len = in.size();    unsigned int line_len, end;    const char* in_buf = in.data();    // Deal with *nix "BEGIN"/"END" separators!!    unsigned int count = 0;    while ( count < len && (in_buf[count] == '\n' || in_buf[count] == '\r' ||            in_buf[count] == '\t' || in_buf[count] == ' ') )        count ++;    bool hasLF = false;    if ( strncasecmp( in_buf+count, "begin", 5) == 0 )    {        count += 5;        while ( count < len && in_buf[count] != '\n' && in_buf[count] != '\r' )            count ++;        while ( count < len && (in_buf[count] == '\n' || in_buf[count] == '\r') )            count ++;        in_buf += count;        len -= count;        hasLF = true;    }    out.resize( len/4*3 );    while ( sidx < len )    {        // get line length (in number of encoded octets)        line_len = UUDecMap[in_buf[sidx++]];        // ascii printable to 0-63 and 4-byte to 3-byte conversion        end = didx+line_len;        char A, B, C, D;        if (end > 2) {          while (didx < end-2)          {             A = UUDecMap[in_buf[sidx]];             B = UUDecMap[in_buf[sidx+1]];             C = UUDecMap[in_buf[sidx+2]];             D = UUDecMap[in_buf[sidx+3]];             out[didx++] = ( ((A << 2) & 255) | ((B >> 4) & 003) );             out[didx++] = ( ((B << 4) & 255) | ((C >> 2) & 017) );             out[didx++] = ( ((C << 6) & 255) | (D & 077) );             sidx += 4;          }        }        if (didx < end)        {            A = UUDecMap[in_buf[sidx]];            B = UUDecMap[in_buf[sidx+1]];            out[didx++] = ( ((A << 2) & 255) | ((B >> 4) & 003) );        }        if (didx < end)        {            B = UUDecMap[in_buf[sidx+1]];            C = UUDecMap[in_buf[sidx+2]];            out[didx++] = ( ((B << 4) & 255) | ((C >> 2) & 017) );        }        // skip padding        while (sidx < len  && in_buf[sidx] != '\n' && in_buf[sidx] != '\r')            sidx++;        // skip end of line        while (sidx < len  && (in_buf[sidx] == '\n' || in_buf[sidx] == '\r'))            sidx++;        // skip the "END" separator when present.        if ( hasLF && strncasecmp( in_buf+sidx, "end", 3) == 0 )            break;    }    if ( didx < out.size()  )        out.resize( didx );}/**** Functions provided for backwards compatibility ****/QString KCodecs::base64Encode( const QString& str ){    if ( str.isEmpty() )        return QString::fromLatin1("");    QByteArray in, out;    const unsigned int len = str.length();    in.resize( len );    memcpy( in.data(), str.latin1(), len );    base64Encode( in, out );    return QString( out );}QString KCodecs::base64Decode( const QString& str ){    if ( str.isEmpty() )        return QString::fromLatin1("");    QByteArray in, out;    const unsigned int len = str.length();    in.resize( str.length() );    memcpy( in.data(), str.latin1(), len );    base64Decode( in, out );    return QString( out );}QString KCodecs::uuencode( const QString& str ){    if ( str.isEmpty() )        return QString::fromLatin1("");    QByteArray in, out;    const unsigned int len = str.length();    in.resize( len );    memcpy( in.data(), str.latin1(), len );    uuencode( in, out );    return QString( out );}QString KCodecs::uudecode( const QString& str ){    if ( str.isEmpty() )        return QString::fromLatin1("");    QByteArray in, out;    const unsigned int len = str.length();    in.resize( len );    memcpy( in.data(), str.latin1(), len );    uudecode( in, out );    return QString( out );}QString KCodecs::encodeString( const QString& data ){    return base64Encode(data);}QString KCodecs::decodeString( const QString& data ){    return base64Decode(data);}/******************************** KMD5 ********************************/KMD5::KMD5(){    init();}KMD5::KMD5( Q_UINT8 *in ){    init();    update( in, qstrlen(reinterpret_cast<char *>(in)) );    finalize();}KMD5::KMD5( const QCString& in ){    init();    update( in );    finalize();}KMD5::KMD5( const QString& in ){    init();    update( in );    finalize();}KMD5::KMD5( const QByteArray& in ){    init();    update( in );    finalize();}KMD5::KMD5(FILE *f){    init();    update( f, true );    finalize ();}void KMD5::update ( const QString& str ){    QByteArray in;    in.setRawData( str.latin1(), str.length() );    update( in );    in.resetRawData( str.latin1(), str.length() );}void KMD5::update ( const QCString& in ){    update( reinterpret_cast<Q_UINT8 *>(in.data()) );

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?