2015-12-28 18:21:20 +01:00
|
|
|
;;; puny.el --- translate non-ASCII domain names to ASCII
|
2015-12-28 02:46:50 +01:00
|
|
|
|
|
|
|
;; Copyright (C) 2015 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
|
|
|
|
;; Keywords: mail, net
|
|
|
|
|
|
|
|
;; This file is part of GNU Emacs.
|
|
|
|
|
|
|
|
;; GNU Emacs is free software: you can redistribute it and/or modify
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
|
|
|
|
|
|
|
;; GNU Emacs is distributed in the hope that it will be useful,
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;; Written by looking at
|
|
|
|
;; http://stackoverflow.com/questions/183485/can-anyone-recommend-a-good-free-javascript-for-punycode-to-unicode-conversion
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
2015-12-28 02:55:52 +01:00
|
|
|
(require 'seq)
|
|
|
|
|
2015-12-28 18:41:13 +01:00
|
|
|
(defun puny-encode-domain (domain)
|
|
|
|
"Encode DOMAIN according to the IDNA/punycode algorith.
|
|
|
|
For instance, \"fśf.org\" => \"xn--ff-2sa.org\"."
|
|
|
|
(mapconcat 'puny-encode-string (split-string domain "[.]") "."))
|
|
|
|
|
2015-12-28 18:21:20 +01:00
|
|
|
(defun puny-encode-string (string)
|
2015-12-28 04:15:16 +01:00
|
|
|
"Encode STRING according to the IDNA/punycode algorithm.
|
|
|
|
This is used to encode non-ASCII domain names.
|
|
|
|
For instance, \"bücher\" => \"xn--bcher-kva\"."
|
2015-12-28 02:55:52 +01:00
|
|
|
(let ((ascii (seq-filter (lambda (char)
|
|
|
|
(< char 128))
|
|
|
|
string)))
|
2015-12-28 02:57:40 +01:00
|
|
|
(if (= (length ascii) (length string))
|
|
|
|
string
|
2015-12-28 18:21:20 +01:00
|
|
|
(concat "xn--" ascii "-" (puny-encode-complex (length ascii) string)))))
|
2015-12-28 02:46:50 +01:00
|
|
|
|
2015-12-28 18:41:13 +01:00
|
|
|
(defun puny-decode-domain (domain)
|
|
|
|
"Decode DOMAIN according to the IDNA/punycode algorith.
|
|
|
|
For instance, \"xn--ff-2sa.org\" => \"fśf.org\"."
|
|
|
|
(mapconcat 'puny-decode-string (split-string domain "[.]") "."))
|
|
|
|
|
2015-12-28 18:21:20 +01:00
|
|
|
(defun puny-decode-string (string)
|
2015-12-28 04:15:16 +01:00
|
|
|
"Decode an IDNA/punycode-encoded string.
|
|
|
|
For instance \"xn--bcher-kva\" => \"bücher\"."
|
2015-12-28 18:41:13 +01:00
|
|
|
(if (string-match "\\`xn--" string)
|
2015-12-28 18:21:20 +01:00
|
|
|
(puny-decode-string-internal (substring string 4))
|
2015-12-28 04:15:16 +01:00
|
|
|
string))
|
|
|
|
|
2015-12-28 18:21:20 +01:00
|
|
|
(defconst puny-initial-n 128)
|
|
|
|
(defconst puny-initial-bias 72)
|
|
|
|
(defconst puny-base 36)
|
|
|
|
(defconst puny-damp 700)
|
|
|
|
(defconst puny-tmin 1)
|
|
|
|
(defconst puny-tmax 26)
|
|
|
|
(defconst puny-skew 28)
|
2015-12-28 02:46:50 +01:00
|
|
|
|
|
|
|
;; 0-25 a-z
|
|
|
|
;; 26-36 0-9
|
2015-12-28 18:21:20 +01:00
|
|
|
(defun puny-encode-digit (d)
|
2015-12-28 02:46:50 +01:00
|
|
|
(if (< d 26)
|
|
|
|
(+ ?a d)
|
|
|
|
(+ ?0 (- d 26))))
|
|
|
|
|
2015-12-28 18:21:20 +01:00
|
|
|
(defun puny-adapt (delta num-points first-time)
|
2015-12-28 02:46:50 +01:00
|
|
|
(let ((delta (if first-time
|
2015-12-28 18:21:20 +01:00
|
|
|
(/ delta puny-damp)
|
2015-12-28 02:46:50 +01:00
|
|
|
(/ delta 2)))
|
|
|
|
(k 0))
|
|
|
|
(setq delta (+ delta (/ delta num-points)))
|
2015-12-28 18:21:20 +01:00
|
|
|
(while (> delta (/ (* (- puny-base puny-tmin)
|
|
|
|
puny-tmax)
|
2015-12-28 02:55:52 +01:00
|
|
|
2))
|
2015-12-28 18:21:20 +01:00
|
|
|
(setq delta (/ delta (- puny-base puny-tmin))
|
|
|
|
k (+ k puny-base)))
|
|
|
|
(+ k (/ (* (1+ (- puny-base puny-tmin)) delta)
|
|
|
|
(+ delta puny-skew)))))
|
2015-12-28 02:46:50 +01:00
|
|
|
|
2015-12-28 18:21:20 +01:00
|
|
|
(defun puny-encode-complex (insertion-points string)
|
|
|
|
(let ((n puny-initial-n)
|
2015-12-28 02:46:50 +01:00
|
|
|
(delta 0)
|
2015-12-28 18:21:20 +01:00
|
|
|
(bias puny-initial-bias)
|
2015-12-28 02:46:50 +01:00
|
|
|
(h insertion-points)
|
2015-12-28 02:55:52 +01:00
|
|
|
result m ijv q)
|
2015-12-28 02:46:50 +01:00
|
|
|
(while (< h (length string))
|
|
|
|
(setq ijv (cl-loop for char across string
|
|
|
|
when (>= char n)
|
|
|
|
minimize char))
|
|
|
|
(setq m ijv)
|
|
|
|
(setq delta (+ delta (* (- m n) (+ h 1)))
|
|
|
|
n m)
|
|
|
|
(cl-loop for char across string
|
|
|
|
when (< char n)
|
|
|
|
do (cl-incf delta)
|
|
|
|
when (= char ijv)
|
|
|
|
do (progn
|
|
|
|
(setq q delta)
|
2015-12-28 18:21:20 +01:00
|
|
|
(cl-loop with k = puny-base
|
2015-12-28 02:55:52 +01:00
|
|
|
for t1 = (cond
|
|
|
|
((<= k bias)
|
2015-12-28 18:21:20 +01:00
|
|
|
puny-tmin)
|
|
|
|
((>= k (+ bias puny-tmax))
|
|
|
|
puny-tmax)
|
2015-12-28 02:55:52 +01:00
|
|
|
(t
|
|
|
|
(- k bias)))
|
2015-12-28 02:46:50 +01:00
|
|
|
while (>= q t1)
|
2015-12-28 18:21:20 +01:00
|
|
|
do (push (puny-encode-digit
|
2015-12-28 02:46:50 +01:00
|
|
|
(+ t1 (mod (- q t1)
|
2015-12-28 18:21:20 +01:00
|
|
|
(- puny-base t1))))
|
2015-12-28 02:46:50 +01:00
|
|
|
result)
|
2015-12-28 18:21:20 +01:00
|
|
|
do (setq q (/ (- q t1) (- puny-base t1))
|
|
|
|
k (+ k puny-base)))
|
|
|
|
(push (puny-encode-digit q) result)
|
|
|
|
(setq bias (puny-adapt delta (+ h 1) (= h insertion-points))
|
2015-12-28 02:46:50 +01:00
|
|
|
delta 0
|
|
|
|
h (1+ h))))
|
|
|
|
(cl-incf delta)
|
|
|
|
(cl-incf n))
|
|
|
|
(nreverse result)))
|
|
|
|
|
2015-12-28 18:41:13 +01:00
|
|
|
(defun puny-decode-digit (cp)
|
|
|
|
(cond
|
|
|
|
((<= cp ?9)
|
|
|
|
(+ (- cp ?0) 26))
|
|
|
|
((<= cp ?Z)
|
|
|
|
(- cp ?A))
|
|
|
|
((<= cp ?z)
|
|
|
|
(- cp ?a))
|
|
|
|
(t
|
|
|
|
puny-base)))
|
|
|
|
|
2015-12-28 18:21:20 +01:00
|
|
|
(defun puny-decode-string-internal (string)
|
2015-12-28 04:15:16 +01:00
|
|
|
(with-temp-buffer
|
|
|
|
(insert string)
|
|
|
|
(goto-char (point-max))
|
2015-12-28 18:41:13 +01:00
|
|
|
(search-backward "-" nil (point-min))
|
|
|
|
;; The encoded chars are after the final dash.
|
|
|
|
(let ((encoded (buffer-substring (1+ (point)) (point-max)))
|
|
|
|
(ic 0)
|
|
|
|
(i 0)
|
|
|
|
(bias puny-initial-bias)
|
|
|
|
(n puny-initial-n)
|
|
|
|
out)
|
|
|
|
(delete-region (point) (point-max))
|
|
|
|
(while (< ic (length encoded))
|
|
|
|
(let ((old-i i)
|
|
|
|
(w 1)
|
|
|
|
(k puny-base)
|
|
|
|
digit t1)
|
|
|
|
(cl-loop do (progn
|
|
|
|
(setq digit (puny-decode-digit (aref encoded ic)))
|
|
|
|
(cl-incf ic)
|
|
|
|
(cl-incf i (* digit w))
|
|
|
|
(setq t1 (cond
|
|
|
|
((<= k bias)
|
|
|
|
puny-tmin)
|
|
|
|
((>= k (+ bias puny-tmax))
|
|
|
|
puny-tmax)
|
|
|
|
(t
|
|
|
|
(- k bias)))))
|
|
|
|
while (>= digit t1)
|
|
|
|
do (setq w (* w (- puny-base t1))
|
|
|
|
k (+ k puny-base)))
|
|
|
|
(setq out (1+ (buffer-size)))
|
|
|
|
(setq bias (puny-adapt (- i old-i) out (= old-i 0))))
|
|
|
|
|
|
|
|
(setq n (+ n (/ i out))
|
|
|
|
i (mod i out))
|
|
|
|
(goto-char (point-min))
|
|
|
|
(forward-char i)
|
|
|
|
(insert (format "%c" n))
|
|
|
|
(cl-incf i)))
|
2015-12-28 04:15:16 +01:00
|
|
|
(buffer-string)))
|
|
|
|
|
2015-12-28 18:21:20 +01:00
|
|
|
(provide 'puny)
|
2015-12-28 02:46:50 +01:00
|
|
|
|
2015-12-28 18:21:20 +01:00
|
|
|
;;; puny.el ends here
|