| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 3 |
* or more contributor license agreements. See the NOTICE file |
| 4 |
* distributed with this work for additional information |
| 5 |
* regarding copyright ownership. The ASF licenses this file |
| 6 |
* to you under the Apache License, Version 2.0 (the |
| 7 |
* "License"); you may not use this file except in compliance |
| 8 |
* with the License. You may obtain a copy of the License at |
| 9 |
* |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, |
| 13 |
* software distributed under the License is distributed on an |
| 14 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 |
* KIND, either express or implied. See the License for the |
| 16 |
* specific language governing permissions and limitations |
| 17 |
* under the License. |
| 18 |
*/ |
| 19 |
|
| 20 |
package org.apache.tools.zip; |
| 21 |
|
| 22 |
import java.io.IOException; |
| 23 |
import java.nio.ByteBuffer; |
| 24 |
import java.util.ArrayList; |
| 25 |
import java.util.Collections; |
| 26 |
import java.util.List; |
| 27 |
|
| 28 |
/** |
| 29 |
* This ZipEncoding implementation implements a simple 8bit character |
| 30 |
* set, which mets the following restrictions: |
| 31 |
* |
| 32 |
* <ul> |
| 33 |
* <li>Characters 0x0000 to 0x007f are encoded as the corresponding |
| 34 |
* byte values 0x00 to 0x7f.</li> |
| 35 |
* <li>All byte codes from 0x80 to 0xff are mapped to a unique unicode |
| 36 |
* character in the range 0x0080 to 0x7fff. (No support for |
| 37 |
* UTF-16 surrogates) |
| 38 |
* </ul> |
| 39 |
* |
| 40 |
* <p>These restrictions most notably apply to the most prominent |
| 41 |
* omissions of java-1.4's {@link java.nio.charset.Charset Charset} |
| 42 |
* implementation, Cp437 and Cp850.</p> |
| 43 |
* |
| 44 |
* <p>The methods of this class are reentrant.</p> |
| 45 |
*/ |
| 46 |
@SuppressWarnings("unchecked") |
| 47 |
class Simple8BitZipEncoding implements ZipEncoding { |
| 48 |
|
| 49 |
/** |
| 50 |
* A character entity, which is put to the reverse mapping table |
| 51 |
* of a simple encoding. |
| 52 |
*/ |
| 53 |
@SuppressWarnings("rawtypes") |
| 54 |
private static final class Simple8BitChar implements Comparable { |
| 55 |
public final char unicode; |
| 56 |
public final byte code; |
| 57 |
|
| 58 |
Simple8BitChar(byte code, char unicode) { |
| 59 |
this.code = code; |
| 60 |
this.unicode = unicode; |
| 61 |
} |
| 62 |
|
| 63 |
public int compareTo(Object o) { |
| 64 |
Simple8BitChar a = (Simple8BitChar) o; |
| 65 |
|
| 66 |
return this.unicode - a.unicode; |
| 67 |
} |
| 68 |
|
| 69 |
public String toString() { |
| 70 |
return "0x" + Integer.toHexString(0xffff & (int) unicode) |
| 71 |
+ "->0x" + Integer.toHexString(0xff & (int) code); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* The characters for byte values of 128 to 255 stored as an array of |
| 77 |
* 128 chars. |
| 78 |
*/ |
| 79 |
private final char[] highChars; |
| 80 |
|
| 81 |
/** |
| 82 |
* A list of {@link Simple8BitChar} objects sorted by the unicode |
| 83 |
* field. This list is used to binary search reverse mapping of |
| 84 |
* unicode characters with a character code greater than 127. |
| 85 |
*/ |
| 86 |
@SuppressWarnings("rawtypes") |
| 87 |
private final List reverseMapping; |
| 88 |
|
| 89 |
/** |
| 90 |
* @param highChars The characters for byte values of 128 to 255 |
| 91 |
* stored as an array of 128 chars. |
| 92 |
*/ |
| 93 |
@SuppressWarnings("rawtypes") |
| 94 |
public Simple8BitZipEncoding(char[] highChars) { |
| 95 |
this.highChars = highChars; |
| 96 |
this.reverseMapping = new ArrayList(this.highChars.length); |
| 97 |
|
| 98 |
byte code = 127; |
| 99 |
|
| 100 |
for (int i = 0; i < this.highChars.length; ++i) { |
| 101 |
this.reverseMapping.add(new Simple8BitChar(++code, |
| 102 |
this.highChars[i])); |
| 103 |
} |
| 104 |
|
| 105 |
Collections.sort(this.reverseMapping); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Return the character code for a given encoded byte. |
| 110 |
* |
| 111 |
* @param b The byte to decode. |
| 112 |
* @return The associated character value. |
| 113 |
*/ |
| 114 |
public char decodeByte(byte b) { |
| 115 |
// code 0-127 |
| 116 |
if (b >= 0) { |
| 117 |
return (char) b; |
| 118 |
} |
| 119 |
|
| 120 |
// byte is signed, so 128 == -128 and 255 == -1 |
| 121 |
return this.highChars[128 + (int) b]; |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* @param c The character to encode. |
| 126 |
* @return Whether the given unicode character is covered by this encoding. |
| 127 |
*/ |
| 128 |
public boolean canEncodeChar(char c) { |
| 129 |
|
| 130 |
if (c >= 0 && c < 128) { |
| 131 |
return true; |
| 132 |
} |
| 133 |
|
| 134 |
Simple8BitChar r = this.encodeHighChar(c); |
| 135 |
return r != null; |
| 136 |
} |
| 137 |
|
| 138 |
/** |
| 139 |
* Pushes the encoded form of the given character to the given byte buffer. |
| 140 |
* |
| 141 |
* @param bb The byte buffer to write to. |
| 142 |
* @param c The character to encode. |
| 143 |
* @return Whether the given unicode character is covered by this encoding. |
| 144 |
* If <code>false</code> is returned, nothing is pushed to the |
| 145 |
* byte buffer. |
| 146 |
*/ |
| 147 |
public boolean pushEncodedChar(ByteBuffer bb, char c) { |
| 148 |
|
| 149 |
if (c >= 0 && c < 128) { |
| 150 |
bb.put((byte) c); |
| 151 |
return true; |
| 152 |
} |
| 153 |
|
| 154 |
Simple8BitChar r = this.encodeHighChar(c); |
| 155 |
if (r == null) { |
| 156 |
return false; |
| 157 |
} |
| 158 |
bb.put(r.code); |
| 159 |
return true; |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* @param c A unicode character in the range from 0x0080 to 0x7f00 |
| 164 |
* @return A Simple8BitChar, if this character is covered by this encoding. |
| 165 |
* A <code>null</code> value is returned, if this character is not |
| 166 |
* covered by this encoding. |
| 167 |
*/ |
| 168 |
private Simple8BitChar encodeHighChar(char c) { |
| 169 |
// for performance an simplicity, yet another reincarnation of |
| 170 |
// binary search... |
| 171 |
int i0 = 0; |
| 172 |
int i1 = this.reverseMapping.size(); |
| 173 |
|
| 174 |
while (i1 > i0) { |
| 175 |
|
| 176 |
int i = i0 + (i1 - i0) / 2; |
| 177 |
|
| 178 |
Simple8BitChar m = (Simple8BitChar) this.reverseMapping.get(i); |
| 179 |
|
| 180 |
if (m.unicode == c) { |
| 181 |
return m; |
| 182 |
} |
| 183 |
|
| 184 |
if (m.unicode < c) { |
| 185 |
i0 = i + 1; |
| 186 |
} else { |
| 187 |
i1 = i; |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
if (i0 >= this.reverseMapping.size()) { |
| 192 |
return null; |
| 193 |
} |
| 194 |
|
| 195 |
Simple8BitChar r = (Simple8BitChar) this.reverseMapping.get(i0); |
| 196 |
|
| 197 |
if (r.unicode != c) { |
| 198 |
return null; |
| 199 |
} |
| 200 |
|
| 201 |
return r; |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* @see |
| 206 |
* org.apache.tools.zip.ZipEncoding#canEncode(java.lang.String) |
| 207 |
*/ |
| 208 |
public boolean canEncode(String name) { |
| 209 |
|
| 210 |
for (int i=0;i<name.length();++i) { |
| 211 |
|
| 212 |
char c = name.charAt(i); |
| 213 |
|
| 214 |
if (!this.canEncodeChar(c)) { |
| 215 |
return false; |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
return true; |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* @see |
| 224 |
* org.apache.tools.zip.ZipEncoding#encode(java.lang.String) |
| 225 |
*/ |
| 226 |
public ByteBuffer encode(String name) { |
| 227 |
ByteBuffer out = ByteBuffer.allocate(name.length() |
| 228 |
+ 6 + (name.length() + 1) / 2); |
| 229 |
|
| 230 |
for (int i=0;i<name.length();++i) { |
| 231 |
|
| 232 |
char c = name.charAt(i); |
| 233 |
|
| 234 |
if (out.remaining() < 6) { |
| 235 |
out = ZipEncodingHelper.growBuffer(out,out.position() + 6); |
| 236 |
} |
| 237 |
|
| 238 |
if (!this.pushEncodedChar(out,c)) { |
| 239 |
|
| 240 |
ZipEncodingHelper.appendSurrogate(out,c); |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
out.limit(out.position()); |
| 245 |
out.rewind(); |
| 246 |
return out; |
| 247 |
} |
| 248 |
|
| 249 |
/** |
| 250 |
* @see |
| 251 |
* org.apache.tools.zip.ZipEncoding#decode(byte[]) |
| 252 |
*/ |
| 253 |
public String decode(byte[] data) throws IOException { |
| 254 |
char [] ret = new char[data.length]; |
| 255 |
|
| 256 |
for (int i=0;i<data.length;++i) { |
| 257 |
ret[i] = this.decodeByte(data[i]); |
| 258 |
} |
| 259 |
|
| 260 |
return new String(ret); |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
} |