]> code.delx.au - refind/blob - filesystems/mk_fsw_strfunc.py
Close to finished with debian/copyright revisions.
[refind] / filesystems / mk_fsw_strfunc.py
1 #!/usr/bin/env python
2
3 #
4 # Copyright (c) 2006 Christoph Pfisterer
5 # All rights reserved.
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions are
9 # met:
10 #
11 # * Redistributions of source code must retain the above copyright
12 # notice, this list of conditions and the following disclaimer.
13 #
14 # * Redistributions in binary form must reproduce the above copyright
15 # notice, this list of conditions and the following disclaimer in the
16 # documentation and/or other materials provided with the
17 # distribution.
18 #
19 # * Neither the name of Christoph Pfisterer nor the names of the
20 # contributors may be used to endorse or promote products derived
21 # from this software without specific prior written permission.
22 #
23 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #
35
36 #
37 # mk_fsw_strfunc.py
38 #
39
40 # definitions
41
42 types = {
43 'ISO88591': 'fsw_u8',
44 'UTF8': 'fsw_u8',
45 'UTF16': 'fsw_u16',
46 'UTF16_SWAPPED': 'fsw_u16',
47 }
48 getnext = {
49 'ISO88591': 'VARC = *VARP++;',
50 'UTF8': """VARC = *VARP++;
51 if ((VARC & 0xe0) == 0xc0) {
52 VARC = ((VARC & 0x1f) << 6) | (*VARP++ & 0x3f);
53 } else if ((VARC & 0xf0) == 0xe0) {
54 VARC = ((VARC & 0x0f) << 12) | ((*VARP++ & 0x3f) << 6);
55 VARC |= (*VARP++ & 0x3f);
56 } else if ((VARC & 0xf8) == 0xf0) {
57 VARC = ((VARC & 0x07) << 18) | ((*VARP++ & 0x3f) << 12);
58 VARC |= ((*VARP++ & 0x3f) << 6);
59 VARC |= (*VARP++ & 0x3f);
60 }""",
61 'UTF16': 'VARC = *VARP++;',
62 'UTF16_SWAPPED': 'VARC = *VARP++; VARC = FSW_SWAPVALUE_U16(VARC);',
63 }
64
65 combos = ( ('ISO88591', 'UTF8'), ('ISO88591', 'UTF16'), ('ISO88591', 'UTF16_SWAPPED'),
66 ('UTF8', 'UTF16'), ('UTF8', 'UTF16_SWAPPED'),
67 ('UTF16', 'UTF16_SWAPPED') )
68
69 coerce_combos = {}
70 for combo in combos:
71 coerce_combos.setdefault(combo[0], []).append(combo[1])
72 coerce_combos.setdefault(combo[1], []).append(combo[0])
73
74 # generate functions
75
76 output = """/* fsw_strfunc.h generated by mk_fsw_strfunc.py */
77 """
78
79 # generate streq functions (symmetric)
80
81 for combo in combos:
82 (enc1, enc2) = combo
83 type1 = types[enc1]
84 type2 = types[enc2]
85 getnext1 = getnext[enc1].replace('VARC', 'c1').replace('VARP', 'p1').replace("\n", "\n ")
86 getnext2 = getnext[enc2].replace('VARC', 'c2').replace('VARP', 'p2').replace("\n", "\n ")
87
88 output += """
89 static int fsw_streq_%(enc1)s_%(enc2)s(void *s1data, void *s2data, int len)
90 {
91 int i;
92 %(type1)s *p1 = (%(type1)s *)s1data;
93 %(type2)s *p2 = (%(type2)s *)s2data;
94 fsw_u32 c1, c2;
95
96 for (i = 0; i < len; i++) {
97 %(getnext1)s
98 %(getnext2)s
99 if (c1 != c2)
100 return 0;
101 }
102 return 1;
103 }
104 """ % locals()
105
106 # generate strcoerce functions (asymmetric, destination-specific)
107
108 for enc2 in ('ISO88591', 'UTF16'):
109 for enc1 in coerce_combos[enc2]:
110 type1 = types[enc1]
111 type2 = types[enc2]
112 getnext1 = getnext[enc1].replace('VARC', 'c').replace('VARP', 'sp').replace("\n", "\n ")
113 output += """
114 static fsw_status_t fsw_strcoerce_%(enc1)s_%(enc2)s(void *srcdata, int srclen, struct fsw_string *dest)
115 {
116 fsw_status_t status;
117 int i;
118 %(type1)s *sp;
119 %(type2)s *dp;
120 fsw_u32 c;
121
122 dest->type = FSW_STRING_TYPE_%(enc2)s;
123 dest->len = srclen;
124 dest->size = srclen * sizeof(%(type2)s);
125 status = fsw_alloc(dest->size, &dest->data);
126 if (status)
127 return status;
128
129 sp = (%(type1)s *)srcdata;
130 dp = (%(type2)s *)dest->data;
131 for (i = 0; i < srclen; i++) {
132 %(getnext1)s
133 *dp++ = c;
134 }
135 return FSW_SUCCESS;
136 }
137 """ % locals()
138
139 for enc2 in ('UTF8',):
140 for enc1 in coerce_combos[enc2]:
141 type1 = types[enc1]
142 type2 = types[enc2]
143 getnext1 = getnext[enc1].replace('VARC', 'c').replace('VARP', 'sp').replace("\n", "\n ")
144 output += """
145 static fsw_status_t fsw_strcoerce_%(enc1)s_%(enc2)s(void *srcdata, int srclen, struct fsw_string *dest)
146 {
147 fsw_status_t status;
148 int i, destsize;
149 %(type1)s *sp;
150 %(type2)s *dp;
151 fsw_u32 c;
152
153 sp = (%(type1)s *)srcdata;
154 destsize = 0;
155 for (i = 0; i < srclen; i++) {
156 %(getnext1)s
157
158 if (c < 0x000080)
159 destsize++;
160 else if (c < 0x000800)
161 destsize += 2;
162 else if (c < 0x010000)
163 destsize += 3;
164 else
165 destsize += 4;
166 }
167
168 dest->type = FSW_STRING_TYPE_%(enc2)s;
169 dest->len = srclen;
170 dest->size = destsize;
171 status = fsw_alloc(dest->size, &dest->data);
172 if (status)
173 return status;
174
175 sp = (%(type1)s *)srcdata;
176 dp = (%(type2)s *)dest->data;
177 for (i = 0; i < srclen; i++) {
178 %(getnext1)s
179
180 if (c < 0x000080) {
181 *dp++ = c;
182 } else if (c < 0x000800) {
183 *dp++ = 0xc0 | ((c >> 6) & 0x1f);
184 *dp++ = 0x80 | (c & 0x3f);
185 } else if (c < 0x010000) {
186 *dp++ = 0xe0 | ((c >> 12) & 0x0f);
187 *dp++ = 0x80 | ((c >> 6) & 0x3f);
188 *dp++ = 0x80 | (c & 0x3f);
189 } else {
190 *dp++ = 0xf0 | ((c >> 18) & 0x07);
191 *dp++ = 0x80 | ((c >> 12) & 0x3f);
192 *dp++ = 0x80 | ((c >> 6) & 0x3f);
193 *dp++ = 0x80 | (c & 0x3f);
194 }
195 }
196 return FSW_SUCCESS;
197 }
198 """ % locals()
199
200 # coerce functions with destination UFT16_SWAPPED missing by design
201
202 # write output file
203
204 f = file("fsw_strfunc.h", "w")
205 f.write(output)
206 f.close()
207
208 # EOF