1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
/*
* basE91 command line front-end
*
* Copyright (c) 2000-2006 Joachim Henke
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* - Neither the name of Joachim Henke nor the names of his contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
import java.io.*;
public class b91cli
{
private static void encode(InputStream is, OutputStream os)
{
int s;
byte[] ibuf = new byte[53248];
byte[] obuf = new byte[65536];
basE91 b91 = new basE91();
try {
while ((s = is.read(ibuf)) > 0) {
s = b91.encode(ibuf, s, obuf);
os.write(obuf, 0, s);
}
s = b91.encEnd(obuf);
os.write(obuf, 0, s);
} catch (Exception e) {
System.err.println(e);
}
}
private static void encodeWrap(InputStream is, OutputStream os)
{
int i, s;
int n = 0;
byte[] ibuf = new byte[53248];
byte[] obuf = new byte[65536];
char[] line = new char[76];
basE91 b91 = new basE91();
try {
PrintStream ps = new PrintStream(os, false, "US-ASCII");
while ((s = is.read(ibuf)) > 0) {
s = b91.encode(ibuf, s, obuf);
for (i = 0; i < s; ++i) {
line[n++] = (char) obuf[i];
if (n == 76) {
ps.println(line);
n = 0;
}
}
}
s = b91.encEnd(obuf);
for (i = 0; i < s; ++i) {
line[n++] = (char) obuf[i];
if (n == 76) {
ps.println(line);
n = 0;
}
}
if (n > 0)
ps.println(new String(line, 0, n));
} catch (Exception e) {
System.err.println(e);
}
}
private static void decode(InputStream is, OutputStream os)
{
int s;
byte[] ibuf = new byte[65536];
byte[] obuf = new byte[57344];
basE91 b91 = new basE91();
try {
while ((s = is.read(ibuf)) > 0) {
s = b91.decode(ibuf, s, obuf);
os.write(obuf, 0, s);
}
s = b91.decEnd(obuf);
os.write(obuf, 0, s);
} catch (Exception e) {
System.err.println(e);
}
}
private static void errExit(String msg)
{
System.err.println("syntax error - " + msg + "\nTry `-h' option for more information.");
System.exit(3);
}
public static void main(String[] args)
{
int i;
boolean enc = true;
boolean lbr = true;
String ifn = null;
String ofn = null;
for (i = 0; i < args.length; ++i)
if (args[i].length() == 2 && args[i].charAt(0) == '-')
switch (args[i].charAt(1)) {
case 'd':
enc = false;
break;
case 'u':
lbr = false;
break;
case 'h':
System.out.println("Usage: base91 [OPTION] infile [outfile]\n\n -d\tdecode a basE91 encoded file\n -u\tleave encoder output unformatted (disable line wrapping)\n -h\tdisplay this help and exit\n -V\toutput version information and exit");
return;
case 'V':
System.out.println("base91 0.6.0\nCopyright (c) 2000-2006 Joachim Henke");
return;
default:
errExit("invalid option: " + args[i]);
}
else if (ifn == null)
ifn = args[i];
else if (ofn == null)
ofn = args[i];
else
errExit("too many arguments: " + args[i]);
if (ifn == null)
errExit("file name missing");
if (ofn == null)
if (enc)
ofn = ifn + (lbr ? "_b91.txt" : ".b91");
else {
String lifn = ifn.toLowerCase();
if (ifn.length() > 4 && lifn.endsWith(".b91"))
ofn = ifn.substring(0, ifn.length() - 4);
else if (ifn.length() > 8 && lifn.endsWith("_b91.txt"))
ofn = ifn.substring(0, ifn.length() - 8);
else
ofn = ifn + ".bin";
}
try {
FileInputStream ifs = new FileInputStream(ifn);
FileOutputStream ofs = new FileOutputStream(ofn);
if (enc)
if (lbr)
encodeWrap(ifs, ofs);
else
encode(ifs, ofs);
else
decode(ifs, ofs);
ifs.close();
ofs.close();
} catch (Exception e) {
System.err.println(e);
}
}
}
|