| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
package android.util.jar; |
| 19 |
|
| 20 |
import android.annotation.Nullable; |
| 21 |
|
| 22 |
import libcore.io.Streams; |
| 23 |
|
| 24 |
import java.io.IOException; |
| 25 |
import java.io.InputStream; |
| 26 |
import java.io.OutputStream; |
| 27 |
import java.nio.ByteBuffer; |
| 28 |
import java.nio.CharBuffer; |
| 29 |
import java.nio.charset.CharsetEncoder; |
| 30 |
import java.nio.charset.CoderResult; |
| 31 |
import java.nio.charset.StandardCharsets; |
| 32 |
import java.util.HashMap; |
| 33 |
import java.util.Iterator; |
| 34 |
import java.util.Map; |
| 35 |
import java.util.jar.Attributes; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public class StrictJarManifest implements Cloneable { |
| 44 |
static final int LINE_LENGTH_LIMIT = 72; |
| 45 |
|
| 46 |
private static final byte[] LINE_SEPARATOR = new byte[] { '\r', '\n' }; |
| 47 |
|
| 48 |
private static final byte[] VALUE_SEPARATOR = new byte[] { ':', ' ' }; |
| 49 |
|
| 50 |
|
| 51 |
static final Attributes.Name ATTRIBUTE_NAME_NAME = new Attributes.Name("Name"); |
| 52 |
|
| 53 |
private final Attributes mainAttributes; |
| 54 |
private final HashMap<String, Attributes> entries; |
| 55 |
|
| 56 |
static final class Chunk { |
| 57 |
final int start; |
| 58 |
final int end; |
| 59 |
|
| 60 |
Chunk(int start, int end) { |
| 61 |
this.start = start; |
| 62 |
this.end = end; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
private HashMap<String, Chunk> chunks; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
private int mainEnd; |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public StrictJarManifest() { |
| 78 |
entries = new HashMap<String, Attributes>(); |
| 79 |
mainAttributes = new Attributes(); |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public StrictJarManifest(InputStream is) throws IOException { |
| 92 |
this(); |
| 93 |
read(Streams.readFully(is)); |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
@SuppressWarnings("unchecked") |
| 104 |
public StrictJarManifest(StrictJarManifest man) { |
| 105 |
mainAttributes = (Attributes) man.mainAttributes.clone(); |
| 106 |
entries = (HashMap<String, Attributes>) ((HashMap<String, Attributes>) man |
| 107 |
.getEntries()).clone(); |
| 108 |
} |
| 109 |
|
| 110 |
StrictJarManifest(byte[] manifestBytes, boolean readChunks) throws IOException { |
| 111 |
this(); |
| 112 |
if (readChunks) { |
| 113 |
chunks = new HashMap<String, Chunk>(); |
| 114 |
} |
| 115 |
read(manifestBytes); |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public void clear() { |
| 123 |
entries.clear(); |
| 124 |
mainAttributes.clear(); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
public Attributes getAttributes(String name) { |
| 137 |
return getEntries().get(name); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public Map<String, Attributes> getEntries() { |
| 147 |
return entries; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
public Attributes getMainAttributes() { |
| 157 |
return mainAttributes; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
@Override |
| 167 |
public Object clone() { |
| 168 |
return new StrictJarManifest(this); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
public void write(OutputStream os) throws IOException { |
| 180 |
write(this, os); |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
public void read(InputStream is) throws IOException { |
| 192 |
read(Streams.readFullyNoClose(is)); |
| 193 |
} |
| 194 |
|
| 195 |
private void read(byte[] buf) throws IOException { |
| 196 |
if (buf.length == 0) { |
| 197 |
return; |
| 198 |
} |
| 199 |
|
| 200 |
StrictJarManifestReader im = new StrictJarManifestReader(buf, mainAttributes); |
| 201 |
mainEnd = im.getEndOfMainSection(); |
| 202 |
im.readEntries(entries, chunks); |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
@Override |
| 211 |
public int hashCode() { |
| 212 |
return mainAttributes.hashCode() ^ getEntries().hashCode(); |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
@Override |
| 225 |
public boolean equals(@Nullable Object o) { |
| 226 |
if (o == null) { |
| 227 |
return false; |
| 228 |
} |
| 229 |
if (o.getClass() != this.getClass()) { |
| 230 |
return false; |
| 231 |
} |
| 232 |
if (!mainAttributes.equals(((StrictJarManifest) o).mainAttributes)) { |
| 233 |
return false; |
| 234 |
} |
| 235 |
return getEntries().equals(((StrictJarManifest) o).getEntries()); |
| 236 |
} |
| 237 |
|
| 238 |
Chunk getChunk(String name) { |
| 239 |
return chunks.get(name); |
| 240 |
} |
| 241 |
|
| 242 |
void removeChunks() { |
| 243 |
chunks = null; |
| 244 |
} |
| 245 |
|
| 246 |
int getMainAttributesEnd() { |
| 247 |
return mainEnd; |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
static void write(StrictJarManifest manifest, OutputStream out) throws IOException { |
| 262 |
CharsetEncoder encoder = StandardCharsets.UTF_8.newEncoder(); |
| 263 |
ByteBuffer buffer = ByteBuffer.allocate(LINE_LENGTH_LIMIT); |
| 264 |
|
| 265 |
Attributes.Name versionName = Attributes.Name.MANIFEST_VERSION; |
| 266 |
String version = manifest.mainAttributes.getValue(versionName); |
| 267 |
if (version == null) { |
| 268 |
versionName = Attributes.Name.SIGNATURE_VERSION; |
| 269 |
version = manifest.mainAttributes.getValue(versionName); |
| 270 |
} |
| 271 |
if (version != null) { |
| 272 |
writeEntry(out, versionName, version, encoder, buffer); |
| 273 |
Iterator<?> entries = manifest.mainAttributes.keySet().iterator(); |
| 274 |
while (entries.hasNext()) { |
| 275 |
Attributes.Name name = (Attributes.Name) entries.next(); |
| 276 |
if (!name.equals(versionName)) { |
| 277 |
writeEntry(out, name, manifest.mainAttributes.getValue(name), encoder, buffer); |
| 278 |
} |
| 279 |
} |
| 280 |
} |
| 281 |
out.write(LINE_SEPARATOR); |
| 282 |
Iterator<String> i = manifest.getEntries().keySet().iterator(); |
| 283 |
while (i.hasNext()) { |
| 284 |
String key = i.next(); |
| 285 |
writeEntry(out, ATTRIBUTE_NAME_NAME, key, encoder, buffer); |
| 286 |
Attributes attributes = manifest.entries.get(key); |
| 287 |
Iterator<?> entries = attributes.keySet().iterator(); |
| 288 |
while (entries.hasNext()) { |
| 289 |
Attributes.Name name = (Attributes.Name) entries.next(); |
| 290 |
writeEntry(out, name, attributes.getValue(name), encoder, buffer); |
| 291 |
} |
| 292 |
out.write(LINE_SEPARATOR); |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
private static void writeEntry(OutputStream os, Attributes.Name name, |
| 297 |
String value, CharsetEncoder encoder, ByteBuffer bBuf) throws IOException { |
| 298 |
String nameString = name.toString(); |
| 299 |
os.write(nameString.getBytes(StandardCharsets.US_ASCII)); |
| 300 |
os.write(VALUE_SEPARATOR); |
| 301 |
|
| 302 |
encoder.reset(); |
| 303 |
bBuf.clear().limit(LINE_LENGTH_LIMIT - nameString.length() - 2); |
| 304 |
|
| 305 |
CharBuffer cBuf = CharBuffer.wrap(value); |
| 306 |
|
| 307 |
while (true) { |
| 308 |
CoderResult r = encoder.encode(cBuf, bBuf, true); |
| 309 |
if (CoderResult.UNDERFLOW == r) { |
| 310 |
r = encoder.flush(bBuf); |
| 311 |
} |
| 312 |
os.write(bBuf.array(), bBuf.arrayOffset(), bBuf.position()); |
| 313 |
os.write(LINE_SEPARATOR); |
| 314 |
if (CoderResult.UNDERFLOW == r) { |
| 315 |
break; |
| 316 |
} |
| 317 |
os.write(' '); |
| 318 |
bBuf.clear().limit(LINE_LENGTH_LIMIT - 1); |
| 319 |
} |
| 320 |
} |
| 321 |
} |