annotate lisp/hex-util.el @ 87126:48d7747f9483

(antlr-keyword, antlr-syntax) (antlr-ruledef, antlr-tokendef, antlr-ruleref-face) (antlr-tokenref, antlr-literal): Inherit from standard font-lock faces in non-light-background case.
author Glenn Morris <rgm@gnu.org>
date Thu, 06 Dec 2007 04:13:02 +0000
parents f7d29ef23ff8
children 107ccd98fa12
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
86919
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
1 ;;; hex-util.el --- Functions to encode/decode hexadecimal string.
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
2
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
3 ;; Copyright (C) 1999, 2001, 2002, 2003, 2004,
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
4 ;; 2005, 2006, 2007 Free Software Foundation, Inc.
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
5
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
6 ;; Author: Shuhei KOBAYASHI <shuhei@aqua.ocn.ne.jp>
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
7 ;; Keywords: data
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
8
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
9 ;; This file is part of GNU Emacs.
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
10
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
12 ;; it under the terms of the GNU General Public License as published by
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
13 ;; the Free Software Foundation; either version 3, or (at your option)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
14 ;; any later version.
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
15
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
16 ;; GNU Emacs is distributed in the hope that it will be useful,
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
19 ;; GNU General Public License for more details.
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
20
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
21 ;; You should have received a copy of the GNU General Public License
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
24 ;; Boston, MA 02110-1301, USA.
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
25
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
26 ;;; Commentary:
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
27
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
28 ;;; Code:
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
29
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
30 (eval-when-compile
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
31 (defmacro hex-char-to-num (chr)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
32 `(let ((chr ,chr))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
33 (cond
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
34 ((and (<= ?a chr)(<= chr ?f)) (+ (- chr ?a) 10))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
35 ((and (<= ?A chr)(<= chr ?F)) (+ (- chr ?A) 10))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
36 ((and (<= ?0 chr)(<= chr ?9)) (- chr ?0))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
37 (t (error "Invalid hexadecimal digit `%c'" chr)))))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
38 (defmacro num-to-hex-char (num)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
39 `(aref "0123456789abcdef" ,num)))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
40
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
41 (defun decode-hex-string (string)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
42 "Decode hexadecimal STRING to octet string."
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
43 (let* ((len (length string))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
44 (dst (make-string (/ len 2) 0))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
45 (idx 0)(pos 0))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
46 (while (< pos len)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
47 ;; logior and lsh are not byte-coded.
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
48 ;; (aset dst idx (logior (lsh (hex-char-to-num (aref string pos)) 4)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
49 ;; (hex-char-to-num (aref string (1+ pos)))))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
50 (aset dst idx (+ (* (hex-char-to-num (aref string pos)) 16)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
51 (hex-char-to-num (aref string (1+ pos)))))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
52 (setq idx (1+ idx)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
53 pos (+ 2 pos)))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
54 dst))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
55
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
56 (defun encode-hex-string (string)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
57 "Encode octet STRING to hexadecimal string."
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
58 (let* ((len (length string))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
59 (dst (make-string (* len 2) 0))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
60 (idx 0)(pos 0))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
61 (while (< pos len)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
62 ;; logand and lsh are not byte-coded.
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
63 ;; (aset dst idx (num-to-hex-char (logand (lsh (aref string pos) -4) 15)))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
64 (aset dst idx (num-to-hex-char (/ (aref string pos) 16)))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
65 (setq idx (1+ idx))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
66 ;; (aset dst idx (num-to-hex-char (logand (aref string pos) 15)))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
67 (aset dst idx (num-to-hex-char (% (aref string pos) 16)))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
68 (setq idx (1+ idx)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
69 pos (1+ pos)))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
70 dst))
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
71
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
72 (provide 'hex-util)
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
73
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
74 ;; arch-tag: fe8aaa79-6c86-400e-813f-5a8cc4cb3859
f7d29ef23ff8 Move here from gnus/.
Glenn Morris <rgm@gnu.org>
parents:
diff changeset
75 ;;; hex-util.el ends here