lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | |
| 2 | static char map64[] = { |
| 3 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 4 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, |
| 5 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 6 | 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, |
| 7 | -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, |
| 8 | 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, |
| 9 | -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, |
| 10 | 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, |
| 11 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 12 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 13 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 14 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 15 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 16 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 17 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 18 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 19 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 20 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 21 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 22 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
| 23 | }; |
| 24 | |
| 25 | static char alphabet64[] = { |
| 26 | 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', |
| 27 | 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', |
| 28 | 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', |
| 29 | 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', |
| 30 | 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', |
| 31 | 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', |
| 32 | 'w', 'x', 'y', 'z', '0', '1', '2', '3', |
| 33 | '4', '5', '6', '7', '8', '9', '+', '/', |
| 34 | }; |
| 35 | |
| 36 | int websDecode64(char *outbuf, char *string, int outlen) |
| 37 | { |
| 38 | unsigned long shiftbuf; |
| 39 | char *cp, *op; |
| 40 | int c, i, j, shift; |
| 41 | |
| 42 | op = outbuf; |
| 43 | *op = '\0'; |
| 44 | cp = string; |
| 45 | while (*cp && *cp != '=') { |
| 46 | shiftbuf = 0; |
| 47 | shift = 18; |
| 48 | for (i = 0; i < 4 && *cp && *cp != '='; i++, cp++) { |
| 49 | c = map64[*cp & 0xff]; |
| 50 | if (c == -1) { |
| 51 | return -1; |
| 52 | } |
| 53 | shiftbuf = shiftbuf | (c << shift); |
| 54 | shift -= 6; |
| 55 | } |
| 56 | --i; |
| 57 | if ((op + i) >= &outbuf[outlen]) { |
| 58 | strcpy(outbuf, "String too big"); |
| 59 | return -1; |
| 60 | } |
| 61 | for (j = 0; j < i; j++) { |
| 62 | *op++ = (char) ((shiftbuf >> (8 * (2 - j))) & 0xff); |
| 63 | } |
| 64 | *op = '\0'; |
| 65 | } |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | void websEncode64(char *outbuf, char *string, int outlen) |
| 70 | { |
| 71 | unsigned long shiftbuf; |
| 72 | char *cp, *op; |
| 73 | int x, i, j, shift; |
| 74 | |
| 75 | op = outbuf; |
| 76 | *op = '\0'; |
| 77 | cp = string; |
| 78 | while (*cp) { |
| 79 | shiftbuf = 0; |
| 80 | for (j = 2; j >= 0 && *cp; j--, cp++) { |
| 81 | shiftbuf |= ((*cp & 0xff) << (j * 8)); |
| 82 | } |
| 83 | shift = 18; |
| 84 | for (i = ++j; i < 4 && op < &outbuf[outlen] ; i++) { |
| 85 | x = (shiftbuf >> shift) & 0x3f; |
| 86 | *op++ = alphabet64[(shiftbuf >> shift) & 0x3f]; |
| 87 | shift -= 6; |
| 88 | } |
| 89 | while (j-- > 0) { |
| 90 | *op++ = '='; |
| 91 | } |
| 92 | *op = '\0'; |
| 93 | } |
| 94 | } |
| 95 | |