Imported Upstream version 3.23.2
[platform/upstream/cmake.git] / Utilities / cmlibarchive / build / utils / gen_archive_string_composition_h.sh
1 #!/bin/sh
2 #
3 # This needs http://unicode.org/Public/6.0.0/ucd/UnicodeData.txt
4 #
5 inputfile="$1"  # Expect UnicodeData.txt
6 outfile=archive_string_composition.h
7 pickout=/tmp/mk_unicode_composition_tbl$$.awk
8 pickout2=/tmp/mk_unicode_composition_tbl2$$.awk
9 #nfdtmp=/tmp/mk_unicode_decomposition_tmp$$.txt
10 nfdtmp="nfdtmpx"
11 #################################################################################
12 #
13 # Append the file header of "archive_string_composition.h"
14 #
15 #################################################################################
16 append_copyright()
17 {
18 cat > ${outfile} <<CR_END
19 /*-
20  * Copyright (c) 2011-2012 libarchive Project
21  * All rights reserved.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  * 1. Redistributions of source code must retain the above copyright
27  *    notice, this list of conditions and the following disclaimer.
28  * 2. Redistributions in binary form must reproduce the above copyright
29  *    notice, this list of conditions and the following disclaimer in the
30  *    documentation and/or other materials provided with the distribution.
31  *
32  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
33  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
34  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
35  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
36  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
37  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
38  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
39  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
41  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42  *
43  * \$FreeBSD\$
44  *
45  */
46
47 /*
48  * ATTENTION!
49  *  This file is generated by build/utils/gen_archive_string_composition_h.sh
50  *  from http://unicode.org/Public/6.0.0/ucd/UnicodeData.txt
51  *
52  *  See also http://unicode.org/report/tr15/
53  */
54
55 #ifndef __LIBARCHIVE_BUILD
56 #error This header is only to be used internally to libarchive.
57 #endif
58
59 #ifndef ARCHIVE_STRING_COMPOSITION_H_INCLUDED
60 #define ARCHIVE_STRING_COMPOSITION_H_INCLUDED
61
62 struct unicode_composition_table {
63         uint32_t cp1;
64         uint32_t cp2;
65         uint32_t nfc;
66 };
67
68 CR_END
69 }
70 #################################################################################
71 #
72 # awk script
73 #
74 #################################################################################
75 cat > ${pickout} <<AWK_END
76 #
77 BEGIN {
78   FS = ";"
79   min = "";
80   max = "";
81   cmd="sort | awk -F ' ' '{printf \"\\\\t{ 0x%s , 0x%s , 0x%s },\\\\n\",\$1,\$2,\$3}'"
82   nfdtbl="${nfdtmp}"
83   print "static const struct unicode_composition_table u_composition_table[] = {"
84 }
85 END {
86   close(cmd)
87   print "};"
88   print ""
89   #
90   # Output Canonical Combining Class tables used for translating NFD to NFC.
91   #
92   printf "#define CANONICAL_CLASS_MIN\\t0x%s\\n", min
93   printf "#define CANONICAL_CLASS_MAX\\t0x%s\\n", max
94   print ""
95   printf "#define IS_DECOMPOSABLE_BLOCK(uc)\\t\\\\\n"
96   printf "\\t(((uc)>>8) <= 0x%X && u_decomposable_blocks[(uc)>>8])\\n", highnum
97   printf "static const char u_decomposable_blocks[0x%X+1] = {\\n\\t", highnum
98   #
99   # Output blockmap
100   for (i = 0; i <= highnum; i++) {
101     if (i != 0 && i % 32 == 0)
102       printf "\\n\\t"
103     # Additionally Hangul[11XX(17), AC00(172) - D7FF(215)] is decomposable.
104     if (blockmap[i] || i == 17 || (i >= 172 && i <= 215))
105         printf "1,"
106     else
107         printf "0,"
108   }
109   printf "\\n};\\n\\n"
110   #
111   # Output a macro to get a canonical combining class.
112   #
113   print "/* Get Canonical Combining Class(CCC). */"
114   printf "#define CCC(uc)\\t\\\\\n"
115   printf "\\t(((uc) > 0x%s)?0:\\\\\\n", max
116   printf "\\tccc_val[ccc_val_index[ccc_index[(uc)>>8]][((uc)>>4)&0x0F]][(uc)&0x0F])\\n"
117   print ""
118   #
119   # Output a canonical combining class value table.
120   #
121   midcnt = 0
122   printf "/* The table of the value of Canonical Cimbining Class */\\n"
123   print "static const unsigned char ccc_val[][16] = {"
124   print " /* idx=0: XXXX0 - XXXXF */"
125   print " { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },"
126   for (h = 0; h <= highnum; h++) {
127     if (!blockmap[h])
128       continue;
129     for (m = 0; m < 16; m++) {
130       if (!xx_blockmap[h, m])
131         continue;
132       midcnt++
133       printf " /* idx=%d: %03X%1X0 - %03X%1XF */\\n {", midcnt, h, m, h, m
134       for (l = 0; l < 15; l++) {
135         printf "%d, ", xxx_blockmap[h, m, l]
136       }
137       printf "%d },\n", xxx_blockmap[h, m, 15]
138     }
139   }
140   printf "};\n"
141   #
142   # Output the index table of the canonical combining class value table.
143   #
144   cnt = 0
145   midcnt = 0
146   printf "\\n/* The index table to ccc_val[*][16] */\\n"
147   print "static const unsigned char ccc_val_index[][16] = {"
148   print " /* idx=0: XXX00 - XXXFF */"
149   print " { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },"
150   for (h = 0; h <= highnum; h++) {
151     if (!blockmap[h])
152       continue;
153     cnt++
154     printf " /* idx=%d: %03X00 - %03XFF */\\n {", cnt, h, h
155     for (m = 0; m < 16; m++) {
156       if (m != 0)
157           printf ","
158       if (xx_blockmap[h, m]) {
159           midcnt++
160           printf "%2d", midcnt
161       } else
162           printf " 0"
163     }
164     printf " },\\n"
165   }
166   printf "};\\n"
167   #
168   # Output the index table to the index table of the canonical combining
169   # class value table.
170   #
171   printf "\\n/* The index table to ccc_val_index[*][16] */\\n"
172   printf "static const unsigned char ccc_index[] = {\\n ", h
173   cnt = 0
174   for (h = 0; h <= highnum; h++) {
175     if (h != 0 && h % 24 == 0)
176       printf "\\n "
177     if (blockmap[h]) {
178       cnt++;
179       printf "%2d,", cnt
180     } else
181       printf " 0,"
182   }
183   print "};"
184   print ""
185 }
186 #
187 #
188 function hextoi(hex)
189 {
190   dec = 0
191   for (i=0; i < length(hex); i++) {
192     x = substr(hex, i+1, 1)
193     if (x ~/[0-9]/)
194         dec = dec * 16 + x;
195     else if (x == "A")
196         dec = dec * 16 + 10;
197     else if (x == "B")
198         dec = dec * 16 + 11;
199     else if (x == "C")
200         dec = dec * 16 + 12;
201     else if (x == "D")
202         dec = dec * 16 + 13;
203     else if (x == "E")
204         dec = dec * 16 + 14;
205     else if (x == "F")
206         dec = dec * 16 + 15;
207   }
208   return dec
209 }
210 #
211 # Collect Canonical Combining Class values.
212 #
213 \$4 ~/^[0-9A-F]+$/ {
214   if (\$4 !~/^0$/) {
215     if (min == "") {
216       min = \$1
217     }
218     max = \$1
219     high = substr(\$1, 1, length(\$1) -2)
220     highnum = hextoi(high)
221     mid = substr(\$1, length(\$1) -1, 1)
222     midnum = hextoi(mid)
223     low = substr(\$1, length(\$1), 1)
224     lownum = hextoi(low)
225     blockmap[highnum] = 1
226     xx_blockmap[highnum, midnum] = 1
227     xxx_blockmap[highnum, midnum, lownum] = \$4
228   }
229 }
230 #
231 # Following code points are not decomposed in MAC OS.
232 #   U+2000  - U+2FFF
233 #   U+F900  - U+FAFF
234 #   U+2F800 - U+2FAFF
235 #
236 #\$1 ~/^2[0-9A-F][0-9A-F][0-9A-F]\$/ {
237 #        next
238 #}
239 #\$1 ~/^F[9A][0-9A-F][0-9A-F]\$/ {
240 #        next
241 #}
242 #\$1 ~/^2F[89A][0-9A-F][0-9A-F]\$/ {
243 #        next
244 #}
245 #
246 # Exclusion code points specified by  
247 # http://unicode.org/Public/6.0.0/ucd/CompositionExclusions.txt
248 ##
249 # 1. Script Specifics
250 ##
251 \$1 ~/^095[89ABCDEF]\$/ {
252     next
253 }
254 \$1 ~/^09D[CDF]\$/ {
255     next
256 }
257 \$1 ~/^0A3[36]\$/ {
258     next
259 }
260 \$1 ~/^0A5[9ABE]\$/ {
261     next
262 }
263 \$1 ~/^0B5[CD]\$/ {
264     next
265 }
266 \$1 ~/^0F4[3D]\$/ {
267     next
268 }
269 \$1 ~/^0F5[27C]\$/ {
270     next
271 }
272 \$1 ~/^0F69\$/ {
273     next
274 }
275 \$1 ~/^0F7[68]\$/ {
276     next
277 }
278 \$1 ~/^0F9[3D]\$/ {
279     next
280 }
281 \$1 ~/^0FA[27C]\$/ {
282     next
283 }
284 \$1 ~/^0FB9\$/ {
285     next
286 }
287 \$1 ~/^FB1[DF]\$/ {
288     next
289 }
290 \$1 ~/^FB2[ABCDEF]\$/ {
291     next
292 }
293 \$1 ~/^FB3[012345689ABCE]\$/ {
294     next
295 }
296 \$1 ~/^FB4[01346789ABCDE]\$/ {
297     next
298 }
299 ##
300 # 2. Post Composition Version precomposed characters
301 ##
302 \$1 ~/^2ADC\$/ {
303     next
304 }
305 \$1 ~/^1D15[EF]\$/ {
306     next
307 }
308 \$1 ~/^1D16[01234]\$/ {
309     next
310 }
311 \$1 ~/^1D1B[BCDEF]\$/ {
312     next
313 }
314 \$1 ~/^1D1C0\$/ {
315     next
316 }
317 ##
318 # 3. Singleton Decompositions
319 ##
320 \$1 ~/^034[01]\$/ {
321     next
322 }
323 \$1 ~/^037[4E]\$/ {
324     next
325 }
326 \$1 ~/^0387\$/ {
327     next
328 }
329 \$1 ~/^1F7[13579BD]\$/ {
330     next
331 }
332 \$1 ~/^1FB[BE]\$/ {
333     next
334 }
335 \$1 ~/^1FC[9B]\$/ {
336     next
337 }
338 \$1 ~/^1FD[3B]\$/ {
339     next
340 }
341 \$1 ~/^1FE[3BEF]\$/ {
342     next
343 }
344 \$1 ~/^1FF[9BD]\$/ {
345     next
346 }
347 \$1 ~/^200[01]\$/ {
348     next
349 }
350 \$1 ~/^212[6AB]\$/ {
351     next
352 }
353 \$1 ~/^232[9A]\$/ {
354     next
355 }
356 \$1 ~/^F9[0-9A-F][0-9A-F]\$/ {
357     next
358 }
359 \$1 ~/^FA0[0-9A-D]\$/ {
360     next
361 }
362 \$1 ~/^FA1[025-9A-E]\$/ {
363     next
364 }
365 \$1 ~/^FA2[0256A-D]\$/ {
366     next
367 }
368 \$1 ~/^FA[3-5][0-9A-F]\$/ {
369     next
370 }
371 \$1 ~/^FA6[0-9A-D]\$/ {
372     next
373 }
374 \$1 ~/^FA[7-9A-C][0-9A-F]\$/ {
375     next
376 }
377 \$1 ~/^FAD[0-9]\$/ {
378     next
379 }
380 \$1 ~/^2F[89][0-9A-F][0-9A-F]\$/ {
381     next
382 }
383 \$1 ~/^2FA0[0-9A-F]\$/ {
384     next
385 }
386 \$1 ~/^2FA1[0-9A-D]\$/ {
387     next
388 }
389 ##
390 # 4. Non-Starter Decompositions
391 ##
392 \$1 ~/^0344\$/ {
393     next
394 }
395 \$1 ~/^0F7[35]\$/ {
396     next
397 }
398 \$1 ~/^0F81\$/ {
399     next
400 }
401 #
402 # Output combinations for NFD ==> NFC.
403 #
404 \$6 ~/^[0-9A-F]+ [0-9A-F]+\$/ {
405     split(\$6, cp, " ")
406     if (length(\$1) == 4)
407         print "0"cp[1], "0"cp[2], "0"\$1 | cmd
408     else
409         print cp[1], cp[2], \$1 | cmd
410     # NFC ==> NFD table.
411     if (length(\$1) == 4)
412         print "0"\$1, "0"cp[1], "0"cp[2] >>nfdtbl
413     else
414         print \$1, cp[1], cp[2] >>nfdtbl
415 }
416 AWK_END
417 #################################################################################
418 # awk script
419 #
420 #################################################################################
421 cat > ${pickout2} <<AWK_END
422 #
423 BEGIN {
424   FS = " "
425   print "struct unicode_decomposition_table {"
426   print "\tuint32_t nfc;"
427   print "\tuint32_t cp1;"
428   print "\tuint32_t cp2;"
429   print "};"
430   print ""
431   print "static const struct unicode_decomposition_table u_decomposition_table[] = {"
432 }
433 END {
434   print "};"
435   print ""
436 }
437 {
438 printf "\t{ 0x%s , 0x%s , 0x%s },\n", \$1, \$2, \$3;
439 }
440 AWK_END
441 #################################################################################
442 #
443 # Run awk a script.
444 #
445 #################################################################################
446 append_copyright
447 awk -f ${pickout} ${inputfile} >> ${outfile}
448 awk -f ${pickout2} ${nfdtmp} >> ${outfile}
449 echo "#endif /* ARCHIVE_STRING_COMPOSITION_H_INCLUDED */" >> ${outfile}
450 echo "" >> ${outfile}
451 #
452 # Remove awk the script.
453 rm ${pickout}
454 rm ${pickout2}
455 rm ${nfdtmp}