Debian Codegraph @

contrib/encodings/tools.lisp

raw
1 ;;; -*- Mode: Lisp; Syntax: Common-Lisp; -*-
2 ;;;
3 ;;; Copyright (c) 2009, Juan Jose Garcia-Ripoll
4 ;;;
5 ;;; This program is free software; you can redistribute it and/or
6 ;;; modify it under the terms of the GNU Library General Public
7 ;;; License as published by the Free Software Foundation; either
8 ;;; version 2 of the License, or (at your option) any later version.
9 ;;;
10 ;;; See file '../Copyright' for full details.
11
12 (defconstant +sequence-type+ '(unsigned-byte 16))
13
14 (defconstant +source-pathname+
15 (make-pathname :name nil :type nil
16 :directory (append (pathname-directory *load-pathname*)
17 (list "sources"))
18 :host (pathname-host *load-pathname*)
19 :device (pathname-device *load-pathname*)))
20
21 (defconstant +all-mappings+
22 '(("ATARIST" "http://unicode.org/Public/MAPPINGS/VENDORS/MISC/ATARIST.TXT")
23
24 ("ISO-8859-1" "http://unicode.org/Public/MAPPINGS/ISO8859/8859-1.TXT")
25 ("ISO-8859-2" "http://unicode.org/Public/MAPPINGS/ISO8859/8859-2.TXT")
26 ("ISO-8859-3" "http://unicode.org/Public/MAPPINGS/ISO8859/8859-3.TXT")
27 ("ISO-8859-4" "http://unicode.org/Public/MAPPINGS/ISO8859/8859-4.TXT")
28 ("ISO-8859-5" "http://unicode.org/Public/MAPPINGS/ISO8859/8859-5.TXT")
29 ("ISO-8859-6" "http://unicode.org/Public/MAPPINGS/ISO8859/8859-6.TXT")
30 ("ISO-8859-7" "http://unicode.org/Public/MAPPINGS/ISO8859/8859-7.TXT")
31 ("ISO-8859-8" "http://unicode.org/Public/MAPPINGS/ISO8859/8859-8.TXT")
32 ("ISO-8859-9" "http://unicode.org/Public/MAPPINGS/ISO8859/8859-9.TXT")
33 ("ISO-8859-10" "http://unicode.org/Public/MAPPINGS/ISO8859/8859-10.TXT")
34 ("ISO-8859-11" "http://unicode.org/Public/MAPPINGS/ISO8859/8859-11.TXT")
35 ("ISO-8859-13" "http://unicode.org/Public/MAPPINGS/ISO8859/8859-13.TXT")
36 ("ISO-8859-14" "http://unicode.org/Public/MAPPINGS/ISO8859/8859-14.TXT")
37 ("ISO-8859-15" "http://unicode.org/Public/MAPPINGS/ISO8859/8859-15.TXT")
38 ("ISO-8859-16" "http://unicode.org/Public/MAPPINGS/ISO8859/8859-16.TXT")
39 ("KOI8-R" "http://unicode.org/Public/MAPPINGS/VENDORS/MISC/KOI8-R.TXT")
40 ("KOI8-U" "http://unicode.org/Public/MAPPINGS/VENDORS/MISC/KOI8-U.TXT")
41 ("CP-856" "http://unicode.org/Public/MAPPINGS/VENDORS/MISC/CP856.TXT")
42 ("CP-856" "http://unicode.org/Public/MAPPINGS/VENDORS/MISC/CP856.TXT")
43
44 ("DOS-CP437" "http://unicode.org/Public/MAPPINGS/VENDORS/MICSFT/PC/CP437.TXT")
45 ("DOS-CP737" "http://unicode.org/Public/MAPPINGS/VENDORS/MICSFT/PC/CP737.TXT")
46 ("DOS-CP775" "http://unicode.org/Public/MAPPINGS/VENDORS/MICSFT/PC/CP775.TXT")
47 ("DOS-CP850" "http://unicode.org/Public/MAPPINGS/VENDORS/MICSFT/PC/CP850.TXT")
48 ("DOS-CP852" "http://unicode.org/Public/MAPPINGS/VENDORS/MICSFT/PC/CP852.TXT")
49 ("DOS-CP855" "http://unicode.org/Public/MAPPINGS/VENDORS/MICSFT/PC/CP855.TXT")
50 ("DOS-CP857" "http://unicode.org/Public/MAPPINGS/VENDORS/MICSFT/PC/CP857.TXT")
51 ("DOS-CP860" "http://unicode.org/Public/MAPPINGS/VENDORS/MICSFT/PC/CP860.TXT")
52 ("DOS-CP861" "http://unicode.org/Public/MAPPINGS/VENDORS/MICSFT/PC/CP861.TXT")
53 ("DOS-CP862" "http://unicode.org/Public/MAPPINGS/VENDORS/MICSFT/PC/CP862.TXT")
54 ("DOS-CP863" "http://unicode.org/Public/MAPPINGS/VENDORS/MICSFT/PC/CP863.TXT")
55 ("DOS-CP864" "http://unicode.org/Public/MAPPINGS/VENDORS/MICSFT/PC/CP864.TXT")
56 ("DOS-CP865" "http://unicode.org/Public/MAPPINGS/VENDORS/MICSFT/PC/CP865.TXT")
57 ("DOS-CP866" "http://unicode.org/Public/MAPPINGS/VENDORS/MICSFT/PC/CP866.TXT")
58 ("DOS-CP869" "http://unicode.org/Public/MAPPINGS/VENDORS/MICSFT/PC/CP869.TXT")
59 ("DOS-CP874" "http://unicode.org/Public/MAPPINGS/VENDORS/MICSFT/PC/CP874.TXT")
60
61 ; Redundant WINDOWS-CP874 DOS-CP874
62 ;("WINDOWS-CP874" "http://unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP874.TXT")
63
64 ("WINDOWS-CP932" "http://unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP932.TXT")
65 ("WINDOWS-CP936" "http://unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP936.TXT")
66 ("WINDOWS-CP949" "http://unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP949.TXT")
67 ("WINDOWS-CP950" "http://unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP950.TXT")
68
69 ("WINDOWS-CP1250" "http://unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP1250.TXT")
70 ("WINDOWS-CP1251" "http://unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP1251.TXT")
71 ("WINDOWS-CP1252" "http://unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP1252.TXT")
72 ("WINDOWS-CP1253" "http://unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP1253.TXT")
73 ("WINDOWS-CP1254" "http://unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP1254.TXT")
74 ("WINDOWS-CP1255" "http://unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP1255.TXT")
75 ("WINDOWS-CP1256" "http://unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP1256.TXT")
76 ("WINDOWS-CP1257" "http://unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP1257.TXT")
77 ("WINDOWS-CP1258" "http://unicode.org/Public/MAPPINGS/VENDORS/MICSFT/WINDOWS/CP1258.TXT")
78
79 ("JISX0201" "http://unicode.org/Public/MAPPINGS/OBSOLETE/EASTASIA/JIS/JIS0201.TXT")
80 ("JISX0208" "http://unicode.org/Public/MAPPINGS/OBSOLETE/EASTASIA/JIS/JIS0208.TXT"
81 ;; Fixes compatible with libiconv: we replace a reverse solidus with a
82 ;; fullwidth reverse solidus, so that JISX0208 does not contain characters
83 ;; in the ASCII range (Needed by ISO-2022-JP-1)
84 ((#x815F #x2140 #xff3c)))
85
86 ("JISX0212" "http://unicode.org/Public/MAPPINGS/OBSOLETE/EASTASIA/JIS/JIS0212.TXT"
87 ;; Fixes compatible with libiconv: we replace a tilde with a
88 ;; fullwidth tilde, so that JISX0212 does not contain characters
89 ;; in the ASCII range (Needed by ISO-2022-JP-1)
90 ((#x2237 #xff5e)))
91
92 ("SHIFT-JIS" "http://unicode.org/Public/MAPPINGS/OBSOLETE/EASTASIA/JIS/SHIFTJIS.TXT")
93
94 ;Unable to parse because they output more than one Unicode character
95 ;("SJIS-0213" "http://x0213.org/codetable/sjis-0213-2004-std.txt")
96 ;("EUC-JISX0213" "http://x0213.org/codetable/euc-jis")
97 ))
98
99 (defun download (filename url)
100 (unless (probe-file filename)
101 (let ((command (format nil "curl \"~A\" > ~A" url filename)))
102 (unless (zerop (si::system command))
103 (error "Unable to retrieve file ~A" url)))))
104
105 (defun reformat (line)
106 (loop with l = (length line)
107 for i from 0 below l
108 for c = (char line i)
109 do (cond ((eql c #\#)
110 (return (if (zerop i) "" (subseq line 0 (1- i)))))
111 ((not (standard-char-p c))
112 (setf (char line i) #\space))
113 ((and (eql c #\0)
114 (let ((j (1+ i)))
115 (and (< j l) (member (char line j) '(#\x #\X)))))
116 (setf (char line i) #\#)))
117 finally (return line)))
118
119 (defun read-mapping (name &optional (n 2))
120 (let* ((source-file (make-pathname :name name :defaults +source-pathname+))
121 (record (find name +all-mappings+ :key #'first :test #'equalp))
122 (fixes (third record))
123 (source-url (fourth record)))
124 (unless (probe-file source-file)
125 (unless source-url
126 (error "Unknown encoding ~A" name))
127 (download file source-url))
128 (with-open-file (in source-file :direction :input)
129 (loop with output = '()
130 for line = (reformat (read-line in nil nil))
131 while line
132 unless (zerop (length line))
133 do (with-input-from-string (aux line)
134 (let ((byte-list (loop for byte = (read aux nil nil)
135 while byte
136 collect byte)))
137 (unless (/= (length byte-list) n)
138 (loop for i in fixes
139 when (= (first i) (first byte-list))
140 do (progn (setf byte-list i) (return)))
141 (push byte-list output))))
142 finally (return (nreverse output))))))
143
144 (defun mapping-hash-table (mapping)
145 (loop with hash = (make-hash-table :size (floor (* 1.5 (length mapping)))
146 :test 'eq)
147 for (multibyte codepoint) in mapping
148 for unicode-char = (code-char codepoint)
149 do (progn
150 (setf (gethash multibyte hash) unicode-char)
151 (setf (gethash unicode-char hash) multibyte)
152 (when (> multibyte #xFF)
153 (setf (gethash (ash multibyte -8) hash) t)))
154 finally (return hash)))
155
156 (defun dump-mapping-array (mapping-assoc output-file)
157 (let* ((mapping-list (reduce #'nconc mapping-assoc))
158 (mapping-array (make-array (length mapping-list) :element-type +sequence-type+
159 :initial-contents mapping-list)))
160 (format t "~%;;; Generating ~A" output-file)
161 (force-output t)
162 (with-open-file (s output-file :direction :output :if-exists :supersede
163 :element-type +sequence-type+ :external-format :big-endian)
164 (write-byte (length mapping-array) s)
165 (write-sequence mapping-array s))))
166
167 (defun copy-encoding-file (in out)
168 (let ((buffer (make-array 8192 :element-type '(unsigned-byte 8))))
169 (format t "~%;;; Copying ~A to ~A" in out)
170 (with-open-file (sin in :direction :input :element-type '(unsigned-byte 8))
171 (with-open-file (sout out :direction :output :element-type '(unsigned-byte 8)
172 :if-exists :supersede :if-does-not-exist :create)
173 (loop for nbytes = (read-sequence buffer sin)
174 until (zerop nbytes)
175 do (write-sequence buffer sout :end nbytes))))))
176
177 (defun all-valid-unicode-chars (mapping)
178 (cond ((consp mapping)
179 (loop for sublist on mapping
180 for i from 0 below 10
181 until (and (eq sublist mapping) (plusp i))
182 collect (all-valid-unicode-chars (first sublist))))
183 ((hash-table-p mapping)
184 (concatenate 'string (loop for key being the hash-key in mapping
185 when (characterp key)
186 collect key)))
187 ((eq mapping :iso-8859-1)
188 (coerce 'string (loop for i from 0 to 255 collect (code-char i))))
189 (t
190 (error "Unknown encoding"))))
191
192 (defun compare-hashes (h1 h2)
193 (flet ((h1-in-h2 (h1 h2)
194 (loop for k being the hash-key in h1 using (hash-value v)
195 for v2 = (gethash k h2 nil)
196 unless (or (consp v2) (consp v) (equal v v2))
197 do (progn (print (list h1 k v h2 k v2))
198 (error)
199 (return nil))
200 finally (return t))))
201 (and (h1-in-h2 h1 h2)
202 (h1-in-h2 h2 h1))))