UBBase32.cpp 2.92 KB
Newer Older
Claudio Valerio's avatar
Claudio Valerio committed
1
/*
Claudio Valerio's avatar
Claudio Valerio committed
2 3
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
Claudio Valerio's avatar
Claudio Valerio committed
4
 * the Free Software Foundation, either version 2 of the License, or
Claudio Valerio's avatar
Claudio Valerio committed
5
 * (at your option) any later version.
Claudio Valerio's avatar
Claudio Valerio committed
6
 *
Claudio Valerio's avatar
Claudio Valerio committed
7 8 9 10 11 12 13
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
Claudio Valerio's avatar
Claudio Valerio committed
14 15 16 17
 */

#include "UBBase32.h"

18 19
#include "core/memcheck.h"

Claudio Valerio's avatar
Claudio Valerio committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
QString UBBase32::sBase32Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";

int UBBase32::sBase32Lookup[] =
    { 0xFF,0xFF,0x1A,0x1B,0x1C,0x1D,0x1E,0x1F, // '0', '1', '2', '3', '4', '5', '6', '7'
      0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, // '8', '9', ':', ';', '<', '=', '>', '?'
      0xFF,0x00,0x01,0x02,0x03,0x04,0x05,0x06, // '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G'
      0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E, // 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O'
      0x0F,0x10,0x11,0x12,0x13,0x14,0x15,0x16, // 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W'
      0x17,0x18,0x19,0xFF,0xFF,0xFF,0xFF,0xFF, // 'X', 'Y', 'Z', '[', '\', ']', '^', '_'
      0xFF,0x00,0x01,0x02,0x03,0x04,0x05,0x06, // '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g'
      0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E, // 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o'
      0x0F,0x10,0x11,0x12,0x13,0x14,0x15,0x16, // 'p', 'q', 'r', 's', 't', 'u', 'v', 'w'
      0x17,0x18,0x19,0xFF,0xFF,0xFF,0xFF,0xFF  // 'x', 'y', 'z', '{', '|', '}', '~', 'DEL'
    };


QByteArray UBBase32::decode(const QString& base32String)
{
    int i, index, offset;
    QByteArray bytes(base32String.length() * 5/8, '\0');

    for (i = 0, index = 0, offset = 0; i < base32String.length(); i++)
    {
        QChar ch = base32String.at(i);
        char lookup = ch.toAscii() - '0';

        /* Skip chars outside the lookup table */
        if (lookup < 0 || lookup >= 80)
            continue;

Claudio Valerio's avatar
Claudio Valerio committed
50
        int digit = sBase32Lookup[(int)lookup];
Claudio Valerio's avatar
Claudio Valerio committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

        /* If this digit is not in the table, ignore it */
        if (digit == 0xFF)
            continue;

        if (index <= 3)
        {
            index = (index + 5) % 8;
            if (index == 0)
            {
               bytes[offset] = bytes[offset] | digit;
               offset++;

               if(offset >= bytes.length())
                   break;
            }
            else
               bytes[offset] = bytes[offset] | (digit << (8 - index));
        }
        else
        {
            index = (index + 5) % 8;
            bytes[offset] = bytes[offset] | ((unsigned int)digit >> index);
            offset++;

            if(offset >= bytes.length())
                break;

            bytes[offset] = bytes[offset] | (digit << (8 - index));
        }
    }
    return bytes;

}