]> pd.if.org Git - pdclib.old/blob - opt/basecodecs/_PDCLIB_ascii.c
ca70a667af84069938925c81605a836c35883317
[pdclib.old] / opt / basecodecs / _PDCLIB_ascii.c
1 /* ASCII codec
2
3    This file is part of the Public Domain C Library (PDCLib).
4    Permission is granted to use, modify, and / or redistribute at will.
5 */
6
7 #include <stdbool.h>
8 #ifndef REGTEST
9 #include <uchar.h>
10
11 static bool asciitoc32(
12     char32_t       **restrict   p_outbuf,
13     size_t          *restrict   p_outsz,
14     const char     **restrict   p_inbuf,
15     size_t          *restrict   p_insz,
16     mbstate_t       *restrict   p_ps
17 )
18 {
19     while(*p_outsz && *p_insz) {
20         unsigned char c = **p_inbuf;
21         if(c > 127)
22             return false;
23         **p_outbuf = c;
24
25         (*p_inbuf)++;
26         (*p_outbuf)++; 
27         (*p_insz)--; 
28         (*p_outsz)--;
29     }
30     return true;
31 }
32
33 static bool c32toascii(
34     char           **restrict  p_outbuf,
35     size_t          *restrict  p_outsz,
36     const char32_t **restrict  p_inbuf,
37     size_t          *restrict  p_insz,
38     mbstate_t       *restrict  p_ps
39 )
40 {
41     while(*p_outsz && *p_insz) {
42         char32_t c = **p_inbuf;
43         if(c > 127)
44             return false;
45         **p_outbuf = c;
46
47         (*p_inbuf)++;
48         (*p_outbuf)++; 
49         (*p_insz)--; 
50         (*p_outsz)--;        
51     }
52     return true;
53 }
54 #endif
55
56 #ifdef TEST
57 #include <_PDCLIB_test.h>
58
59 int main( void )
60 {
61 #ifndef REGTEST
62     // Valid conversion & back
63
64     char32_t c32out[5];
65
66     char32_t *c32ptr = &c32out[0];
67     size_t    c32rem = 5;
68     char     *chrptr = (char*) &abcde[0];
69     size_t    chrrem = 5;
70     mbstate_t mbs = { 0 };
71
72     TESTCASE(asciitoc32(&c32ptr, &c32rem, &chrptr, &chrrem, &mbs) == true);
73     TESTCASE(c32rem == 0);
74     TESTCASE(chrrem == 0);
75     TESTCASE(c32ptr == &c32out[5]);
76     TESTCASE(chrptr == &abcde[5]);
77     TESTCASE(c32out[0] == 'a' && c32out[1] == 'b' && c32out[2] == 'c' && \
78              c32out[3] == 'd' && c32out[4] == 'e');
79
80     char chrout[5];
81     c32ptr = &c32out[0];
82     c32rem = 5;
83     chrptr = &chrout[0];
84     chrrem = 5;
85
86
87     TESTCASE(c32toascii(&chrptr, &chrrem, &c32ptr, &c32rem, &mbs) == true);
88     TESTCASE(c32rem == 0);
89     TESTCASE(chrrem == 0);
90     TESTCASE(c32ptr == &c32out[5]);
91     TESTCASE(chrptr == &chrout[5]);
92     TESTCASE(memcmp(chrout, abcde, 5) == 0);
93
94     // Invalid conversions
95     char badascii = '\xC0';
96     c32ptr = &c32out[0];
97     c32rem = 5;
98     chrptr = &badascii;
99     chrrem = 1;
100     TESTCASE(asciitoc32(&c32ptr, &c32rem, &chrptr, &chrrem, &mbs) == false);
101     TESTCASE(c32ptr == &c32out[0]);
102     TESTCASE(c32rem == 5);
103     TESTCASE(chrptr == &badascii);
104     TESTCASE(chrrem == 1);
105
106     char32_t baduni = 0xC0;
107     c32ptr = &baduni;
108     c32rem = 1;
109     chrptr = &chrout[0];
110     chrrem = 5;
111     TESTCASE(c32toascii(&chrptr, &chrrem, &c32ptr, &c32rem, &mbs) == false);
112     TESTCASE(c32ptr == &baduni);
113     TESTCASE(c32rem == 1);
114     TESTCASE(chrptr == &chrout[0]);
115     TESTCASE(chrrem == 5);
116 #endif
117     return TEST_RESULTS;
118 }
119
120 #endif
121