util  Diff

Differences From Artifact [f7f2775d1b]:

To Artifact [1d0d9f385e]:

  • 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
2
3
4

5
6
7
8
9
10
11
..
18
19
20
21
22
23
24








25
26

27
28
29
30
31
32
33
34
35
36









37
38
39
40
41
42
43
44
45
46
47

48


49
50
51
52
53
54
55
...
106
107
108
109
110
111
112

113

114
115
116
117
118
119
120
/* [ʞ] iaia.c
 *  ~ lexi hale <lexi@hale.su>
 *  # include "iaia.c"
 *  # define _IAIA_FN_{ATOI,ITOA,ASCTOI,ITOASC}

 *  : typedef iaia_word_type,
 *            iaia_error_type
 *  : enum iaia_e_domain,
 *         iaia_e_base,
 *         iaia_e_overflow
 *  ? arbitrary-base integer conversion functions
 */
................................................................................
#endif
#ifndef _IAIA_FN_ASCTOI
#	define _IAIA_FN_ASCTOI asctoi
#endif
#ifndef _IAIA_FN_ITOASC
#	define _IAIA_FN_ITOASC itoasc
#endif









enum /* constants */ {


	/* ascii address space */
	numspace        = (0x39 - 0x30) + 1, /* 10 */
	alphaspace      = (0x5a - 0x41) + 1, /* 26 */
	smallalphaspace = (0x7a - 0x61) + 1, /* 26 */

	/* base representations */
	imaxbase = numspace + alphaspace,    /* 36 */
	maxbase = imaxbase + smallalphaspace /* 62 */
};










/* -- string to integer converters -- */

iaia_error_type _IAIA_FN_ASCTOI(const char* s, iaia_word_type* ret) {
	iaia_word_type val = 0;
	enum { base = 128 };

	for (;*s!=null;++s) {
		uint8_t v = *s;
		if (v > base) return iaia_e_domain;


		val *= base;


		val += v;
	}

	*ret = val;
	return ok;
}

................................................................................
iaia_error_type _IAIA_FN_ITOASC(iaia_word_type val, const char* buf_start, char* buf_end, char** newbuf) {
	char* ptr = buf_end;

	*ptr-- = 0;
	while(val > 0) {
		if (ptr < buf_start) return iaia_e_overflow;
		iaia_word_type rem = val % 128;

		val /= 128;

		*ptr-- = (char)rem;
	}

	if (newbuf != null) *newbuf = ptr + 1;
	return ok;
}





>







 







>
>
>
>
>
>
>
>


>










>
>
>
>
>
>
>
>
>





<



|

>
|
>
>







 







>
|
>







1
2
3
4
5
6
7
8
9
10
11
12
..
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
...
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* [ʞ] iaia.c
 *  ~ lexi hale <lexi@hale.su>
 *  # include "iaia.c"
 *  # define _IAIA_FN_{ATOI,ITOA,ASCTOI,ITOASC}
 *           _IAIA_EXP_ASCFORM
 *  : typedef iaia_word_type,
 *            iaia_error_type
 *  : enum iaia_e_domain,
 *         iaia_e_base,
 *         iaia_e_overflow
 *  ? arbitrary-base integer conversion functions
 */
................................................................................
#endif
#ifndef _IAIA_FN_ASCTOI
#	define _IAIA_FN_ASCTOI asctoi
#endif
#ifndef _IAIA_FN_ITOASC
#	define _IAIA_FN_ITOASC itoasc
#endif
#ifndef _IAIA_EXP_ASCFORM
/* this is an expression that will be evaluated to
 * determine whether to compress ascii to a 7-bit
 * representation or to store it in 8-bit space.
 *  1 = 7-bit (compressed)
 *  0 = 8-bit (uncompressed) */
#	define _IAIA_EXP_ASCFORM (0)
#endif

enum /* constants */ {
	base7bit = 128,

	/* ascii address space */
	numspace        = (0x39 - 0x30) + 1, /* 10 */
	alphaspace      = (0x5a - 0x41) + 1, /* 26 */
	smallalphaspace = (0x7a - 0x61) + 1, /* 26 */

	/* base representations */
	imaxbase = numspace + alphaspace,    /* 36 */
	maxbase = imaxbase + smallalphaspace /* 62 */
};
#ifndef _IAIA_EXTERNAL_TYPES
	typedef enum iaia_error_type {
		iaia_e_ok,
		iaia_e_domain,
		iaia_e_base,
		iaia_e_overflow,
	} iaia_error_type;
	typedef unsigned long long iaia_word_type;
#endif

/* -- string to integer converters -- */

iaia_error_type _IAIA_FN_ASCTOI(const char* s, iaia_word_type* ret) {
	iaia_word_type val = 0;


	for (;*s!=null;++s) {
		uint8_t v = *s;
		if (v > base7bit) return iaia_e_domain;

		if (_IAIA_EXP_ASCFORM)
			val *= base7bit;
			else val <<= 8;

		val += v;
	}

	*ret = val;
	return ok;
}

................................................................................
iaia_error_type _IAIA_FN_ITOASC(iaia_word_type val, const char* buf_start, char* buf_end, char** newbuf) {
	char* ptr = buf_end;

	*ptr-- = 0;
	while(val > 0) {
		if (ptr < buf_start) return iaia_e_overflow;
		iaia_word_type rem = val % 128;
		if (_IAIA_EXP_ASCFORM)
			val /= base7bit;
			else val >>= 8;
		*ptr-- = (char)rem;
	}

	if (newbuf != null) *newbuf = ptr + 1;
	return ok;
}