| 28 | } |
| 29 | |
| 30 | int encode(const uint8_t* __restrict plaintextIn, int lengthIn, uint8_t* __restrict codeOut) noexcept { |
| 31 | const uint8_t* plainchar = plaintextIn; |
| 32 | const uint8_t* const plaintextEnd = plaintextIn + lengthIn; |
| 33 | uint8_t* codechar = codeOut; |
| 34 | uint8_t result = 0; |
| 35 | uint8_t fragment = 0; |
| 36 | |
| 37 | while (1) { |
| 38 | if (plainchar == plaintextEnd) { |
| 39 | return codechar - codeOut; |
| 40 | } |
| 41 | // byte 1 of 3 |
| 42 | fragment = *plainchar++; |
| 43 | result = (fragment & 0x0fc) >> 2; |
| 44 | *codechar++ = encodeValue(result); |
| 45 | result = (fragment & 0x003) << 4; |
| 46 | if (plainchar == plaintextEnd) { |
| 47 | *codechar++ = encodeValue(result); |
| 48 | return codechar - codeOut; |
| 49 | } |
| 50 | // byte 2 of 3 |
| 51 | fragment = *plainchar++; |
| 52 | result |= (fragment & 0x0f0) >> 4; |
| 53 | *codechar++ = encodeValue(result); |
| 54 | result = (fragment & 0x00f) << 2; |
| 55 | if (plainchar == plaintextEnd) { |
| 56 | *codechar++ = encodeValue(result); |
| 57 | return codechar - codeOut; |
| 58 | } |
| 59 | // byte 3 of 3 |
| 60 | fragment = *plainchar++; |
| 61 | result |= (fragment & 0x0c0) >> 6; |
| 62 | *codechar++ = encodeValue(result); |
| 63 | result = (fragment & 0x03f) >> 0; |
| 64 | *codechar++ = encodeValue(result); |
| 65 | } |
| 66 | /* control should not reach here */ |
| 67 | return codechar - codeOut; |
| 68 | } |
| 69 | |
| 70 | int encodedLength(int dataLength) noexcept { |
| 71 | auto r = dataLength % 3; |
no test coverage detected