chiark / gitweb /
Add 'base91-c/' from commit '664054e8f603f09badb98ef09ee1bd1e58d93659'
[secnet.git] / base91-c / Java / basE91.java
1 /*
2  * basE91 encoding/decoding routines
3  *
4  * Copyright (c) 2000-2006 Joachim Henke
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 met:
9  *
10  *  - Redistributions of source code must retain the above copyright notice,
11  *    this list of conditions and the following disclaimer.
12  *  - Redistributions in binary form must reproduce the above copyright notice,
13  *    this list of conditions and the following disclaimer in the documentation
14  *    and/or other materials provided with the distribution.
15  *  - Neither the name of Joachim Henke nor the names of his contributors may
16  *    be used to endorse or promote products derived from this software without
17  *    specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 public class basE91
33 {
34         private int ebq, en, dbq, dn, dv;
35         public final byte[] enctab;
36         private final byte[] dectab;
37
38         public int encode(byte[] ib, int n, byte[] ob)
39         {
40                 int i, c = 0;
41
42                 for (i = 0; i < n; ++i) {
43                         ebq |= (ib[i] & 255) << en;
44                         en += 8;
45                         if (en > 13) {
46                                 int ev = ebq & 8191;
47
48                                 if (ev > 88) {
49                                         ebq >>= 13;
50                                         en -= 13;
51                                 } else {
52                                         ev = ebq & 16383;
53                                         ebq >>= 14;
54                                         en -= 14;
55                                 }
56                                 ob[c++] = enctab[ev % 91];
57                                 ob[c++] = enctab[ev / 91];
58                         }
59                 }
60                 return c;
61         }
62
63         public int encEnd(byte[] ob)
64         {
65                 int c = 0;
66
67                 if (en > 0) {
68                         ob[c++] = enctab[ebq % 91];
69                         if (en > 7 || ebq > 90)
70                                 ob[c++] = enctab[ebq / 91];
71                 }
72                 encReset();
73                 return c;
74         }
75
76         public void encReset()
77         {
78                 ebq = 0;
79                 en = 0;
80         }
81
82         public int decode(byte[] ib, int n, byte[] ob)
83         {
84                 int i, c = 0;
85
86                 for (i = 0; i < n; ++i) {
87                         if (dectab[ib[i]] == -1)
88                                 continue;
89                         if (dv == -1)
90                                 dv = dectab[ib[i]];
91                         else {
92                                 dv += dectab[ib[i]] * 91;
93                                 dbq |= dv << dn;
94                                 dn += (dv & 8191) > 88 ? 13 : 14;
95                                 do {
96                                         ob[c++] = (byte) dbq;
97                                         dbq >>= 8;
98                                         dn -= 8;
99                                 } while (dn > 7);
100                                 dv = -1;
101                         }
102                 }
103                 return c;
104         }
105
106         public int decEnd(byte[] ob)
107         {
108                 int c = 0;
109
110                 if (dv != -1)
111                         ob[c++] = (byte) (dbq | dv << dn);
112                 decReset();
113                 return c;
114         }
115
116         public void decReset()
117         {
118                 dbq = 0;
119                 dn = 0;
120                 dv = -1;
121         }
122
123         public basE91()
124         {
125                 int i;
126                 String ts = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~\"";
127
128                 enctab = ts.getBytes();
129                 dectab = new byte[256];
130                 for (i = 0; i < 256; ++i)
131                         dectab[i] = -1;
132                 for (i = 0; i < 91; ++i)
133                         dectab[enctab[i]] = (byte) i;
134                 encReset();
135                 decReset();
136         }
137 }