| 53 | } |
| 54 | |
| 55 | int decode(const uint8_t* __restrict codeIn, const int lengthIn, uint8_t* __restrict plaintextOut) noexcept { |
| 56 | const uint8_t* codechar = codeIn; |
| 57 | const uint8_t* const codeEnd = codeIn + lengthIn; |
| 58 | uint8_t* plainchar = plaintextOut; |
| 59 | uint8_t fragment = 0; |
| 60 | |
| 61 | while (1) { |
| 62 | // code 1 of 4 |
| 63 | if (codechar == codeEnd) { |
| 64 | return plainchar - plaintextOut; |
| 65 | } |
| 66 | fragment = decodeValue(*codechar++); |
| 67 | if (fragment == _X) |
| 68 | return -1; |
| 69 | *plainchar = (fragment & 0x03f) << 2; |
| 70 | if (codechar == codeEnd) { |
| 71 | return -1; // requires at least 2 chars to decode 1 plain byte |
| 72 | } |
| 73 | // code 2 of 4 |
| 74 | fragment = decodeValue(*codechar++); |
| 75 | if (fragment == _X) |
| 76 | return -1; |
| 77 | *plainchar++ |= (fragment & 0x030) >> 4; |
| 78 | if (codechar == codeEnd) { |
| 79 | return plainchar - plaintextOut; |
| 80 | } |
| 81 | *plainchar = (fragment & 0x00f) << 4; |
| 82 | // code 3 of 4 |
| 83 | fragment = decodeValue(*codechar++); |
| 84 | if (fragment == _X) |
| 85 | return -1; |
| 86 | *plainchar++ |= (fragment >> 2); |
| 87 | if (codechar == codeEnd) { |
| 88 | return plainchar - plaintextOut; |
| 89 | } |
| 90 | *plainchar = (fragment & 0x003) << 6; |
| 91 | // code 4 of 4 |
| 92 | fragment = decodeValue(*codechar++); |
| 93 | if (fragment == _X) |
| 94 | return -1; |
| 95 | *plainchar++ |= (fragment & 0x03f); |
| 96 | } |
| 97 | /* control should not reach here */ |
| 98 | return plainchar - plaintextOut; |
| 99 | } |
| 100 | |
| 101 | int decodedLength(int codeLength) noexcept { |
| 102 | const auto r = (codeLength & 3); |
no test coverage detected