Differences From
Artifact [f7f2775d1b]:
- File
clib/iaia.c
— part of check-in
[6a14de1811]
at
2019-07-20 23:37:03
on branch trunk
— udpate xpriv to use sysv shmem by default; give iaia an option to generate its own types, and allow selecting between 7-bit and 8-bit encodings for ascii (defaulting to 8-bit); update mkpw to work with new iaia; update ord to add flag controlling byte length (7 or 8) for iaia's ascii mode
(user:
lexi,
size: 4370)
[annotate]
[blame]
[check-ins using]
1 1 /* [ʞ] iaia.c
2 2 * ~ lexi hale <lexi@hale.su>
3 3 * # include "iaia.c"
4 4 * # define _IAIA_FN_{ATOI,ITOA,ASCTOI,ITOASC}
5 + * _IAIA_EXP_ASCFORM
5 6 * : typedef iaia_word_type,
6 7 * iaia_error_type
7 8 * : enum iaia_e_domain,
8 9 * iaia_e_base,
9 10 * iaia_e_overflow
10 11 * ? arbitrary-base integer conversion functions
11 12 */
................................................................................
18 19 #endif
19 20 #ifndef _IAIA_FN_ASCTOI
20 21 # define _IAIA_FN_ASCTOI asctoi
21 22 #endif
22 23 #ifndef _IAIA_FN_ITOASC
23 24 # define _IAIA_FN_ITOASC itoasc
24 25 #endif
26 +#ifndef _IAIA_EXP_ASCFORM
27 +/* this is an expression that will be evaluated to
28 + * determine whether to compress ascii to a 7-bit
29 + * representation or to store it in 8-bit space.
30 + * 1 = 7-bit (compressed)
31 + * 0 = 8-bit (uncompressed) */
32 +# define _IAIA_EXP_ASCFORM (0)
33 +#endif
25 34
26 35 enum /* constants */ {
36 + base7bit = 128,
27 37
28 38 /* ascii address space */
29 39 numspace = (0x39 - 0x30) + 1, /* 10 */
30 40 alphaspace = (0x5a - 0x41) + 1, /* 26 */
31 41 smallalphaspace = (0x7a - 0x61) + 1, /* 26 */
32 42
33 43 /* base representations */
34 44 imaxbase = numspace + alphaspace, /* 36 */
35 45 maxbase = imaxbase + smallalphaspace /* 62 */
36 46 };
47 +#ifndef _IAIA_EXTERNAL_TYPES
48 + typedef enum iaia_error_type {
49 + iaia_e_ok,
50 + iaia_e_domain,
51 + iaia_e_base,
52 + iaia_e_overflow,
53 + } iaia_error_type;
54 + typedef unsigned long long iaia_word_type;
55 +#endif
37 56
38 57 /* -- string to integer converters -- */
39 58
40 59 iaia_error_type _IAIA_FN_ASCTOI(const char* s, iaia_word_type* ret) {
41 60 iaia_word_type val = 0;
42 - enum { base = 128 };
43 61
44 62 for (;*s!=null;++s) {
45 63 uint8_t v = *s;
46 - if (v > base) return iaia_e_domain;
64 + if (v > base7bit) return iaia_e_domain;
47 65
48 - val *= base;
66 + if (_IAIA_EXP_ASCFORM)
67 + val *= base7bit;
68 + else val <<= 8;
69 +
49 70 val += v;
50 71 }
51 72
52 73 *ret = val;
53 74 return ok;
54 75 }
55 76
................................................................................
106 127 iaia_error_type _IAIA_FN_ITOASC(iaia_word_type val, const char* buf_start, char* buf_end, char** newbuf) {
107 128 char* ptr = buf_end;
108 129
109 130 *ptr-- = 0;
110 131 while(val > 0) {
111 132 if (ptr < buf_start) return iaia_e_overflow;
112 133 iaia_word_type rem = val % 128;
113 - val /= 128;
134 + if (_IAIA_EXP_ASCFORM)
135 + val /= base7bit;
136 + else val >>= 8;
114 137 *ptr-- = (char)rem;
115 138 }
116 139
117 140 if (newbuf != null) *newbuf = ptr + 1;
118 141 return ok;
119 142 }
120 143