| 63 | } |
| 64 | |
| 65 | QString Base32::encodeBase32Sequence(QByteArray const& source, bool formatString) { |
| 66 | QString result; |
| 67 | size_t usedBits = 0; |
| 68 | uint32_t cache = 0; |
| 69 | unsigned char value = 0; |
| 70 | |
| 71 | const int sourceLength = source.size(); |
| 72 | for (int i = 0; i < sourceLength; ++i) { |
| 73 | char c = source.at(i); |
| 74 | value = *reinterpret_cast<unsigned char*>(&c); |
| 75 | cache = (cache << 8) | value; |
| 76 | usedBits += 8; |
| 77 | |
| 78 | while (usedBits >= 5) { |
| 79 | result.append(convert5BitValueToChar((cache >> (usedBits - 5)) & 0x1F)); |
| 80 | usedBits -= 5; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | // Last iteration to take the last bits |
| 85 | if (usedBits > 0) { |
| 86 | result.append(convert5BitValueToChar((cache << (5 - usedBits)) & 0x1F)); |
| 87 | } |
| 88 | |
| 89 | if (formatString) { |
| 90 | QString formatedResult; |
| 91 | for (int i = 0, length = result.size(); i < length; ++i) { |
| 92 | if ((i % 4 == 0) && (i > 0)) { |
| 93 | formatedResult.append('-'); |
| 94 | } |
| 95 | formatedResult.append(result.at(i)); |
| 96 | } |
| 97 | return formatedResult; |
| 98 | } |
| 99 | |
| 100 | return result; |
| 101 | } |
| 102 | |
| 103 | } |
| 104 | } |