encode a buffer using base64 - simple and slow algorithm. null terminates the result. ***************************************************************************/
| 31 | the result. |
| 32 | ***************************************************************************/ |
| 33 | void base64_encode(const char *buf, int len, char *out, int pad) |
| 34 | { |
| 35 | char *b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 36 | int bit_offset, byte_offset, idx, i; |
| 37 | const uchar *d = (const uchar *)buf; |
| 38 | int bytes = (len*8 + 5)/6; |
| 39 | |
| 40 | for (i = 0; i < bytes; i++) { |
| 41 | byte_offset = (i*6)/8; |
| 42 | bit_offset = (i*6)%8; |
| 43 | if (bit_offset < 3) { |
| 44 | idx = (d[byte_offset] >> (2-bit_offset)) & 0x3F; |
| 45 | } else { |
| 46 | idx = (d[byte_offset] << (bit_offset-2)) & 0x3F; |
| 47 | if (byte_offset+1 < len) { |
| 48 | idx |= (d[byte_offset+1] >> (8-(bit_offset-2))); |
| 49 | } |
| 50 | } |
| 51 | out[i] = b64[idx]; |
| 52 | } |
| 53 | |
| 54 | while (pad && (i % 4)) |
| 55 | out[i++] = '='; |
| 56 | |
| 57 | out[i] = '\0'; |
| 58 | } |
| 59 | |
| 60 | /* Generate a challenge buffer and return it base64-encoded. */ |
| 61 | static void gen_challenge(const char *addr, char *challenge) |
no outgoing calls
no test coverage detected