| 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 java.io.ByteArrayOutputStream; |
| 21 |
import java.io.IOException; |
| 22 |
import java.nio.charset.StandardCharsets; |
| 23 |
import java.util.HashMap; |
| 24 |
import java.util.Map; |
| 25 |
import java.util.jar.Attributes; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
class StrictJarManifestReader { |
| 32 |
|
| 33 |
|
| 34 |
private final HashMap<String, Attributes.Name> attributeNameCache = new HashMap<String, Attributes.Name>(); |
| 35 |
|
| 36 |
private final ByteArrayOutputStream valueBuffer = new ByteArrayOutputStream(80); |
| 37 |
|
| 38 |
private final byte[] buf; |
| 39 |
|
| 40 |
private final int endOfMainSection; |
| 41 |
|
| 42 |
private int pos; |
| 43 |
|
| 44 |
private Attributes.Name name; |
| 45 |
|
| 46 |
private String value; |
| 47 |
|
| 48 |
private int consecutiveLineBreaks = 0; |
| 49 |
|
| 50 |
public StrictJarManifestReader(byte[] buf, Attributes main) throws IOException { |
| 51 |
this.buf = buf; |
| 52 |
while (readHeader()) { |
| 53 |
main.put(name, value); |
| 54 |
} |
| 55 |
this.endOfMainSection = pos; |
| 56 |
} |
| 57 |
|
| 58 |
public void readEntries(Map<String, Attributes> entries, Map<String, StrictJarManifest.Chunk> chunks) throws IOException { |
| 59 |
int mark = pos; |
| 60 |
while (readHeader()) { |
| 61 |
if (!StrictJarManifest.ATTRIBUTE_NAME_NAME.equals(name)) { |
| 62 |
throw new IOException("Entry is not named"); |
| 63 |
} |
| 64 |
String entryNameValue = value; |
| 65 |
|
| 66 |
Attributes entry = entries.get(entryNameValue); |
| 67 |
if (entry == null) { |
| 68 |
entry = new Attributes(12); |
| 69 |
} |
| 70 |
|
| 71 |
while (readHeader()) { |
| 72 |
entry.put(name, value); |
| 73 |
} |
| 74 |
|
| 75 |
if (chunks != null) { |
| 76 |
if (chunks.get(entryNameValue) != null) { |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
throw new IOException("A jar verifier does not support more than one entry with the same name"); |
| 84 |
} |
| 85 |
chunks.put(entryNameValue, new StrictJarManifest.Chunk(mark, pos)); |
| 86 |
mark = pos; |
| 87 |
} |
| 88 |
|
| 89 |
entries.put(entryNameValue, entry); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
public int getEndOfMainSection() { |
| 94 |
return endOfMainSection; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
private boolean readHeader() throws IOException { |
| 101 |
if (consecutiveLineBreaks > 1) { |
| 102 |
|
| 103 |
consecutiveLineBreaks = 0; |
| 104 |
return false; |
| 105 |
} |
| 106 |
readName(); |
| 107 |
consecutiveLineBreaks = 0; |
| 108 |
readValue(); |
| 109 |
|
| 110 |
|
| 111 |
return consecutiveLineBreaks > 0; |
| 112 |
} |
| 113 |
|
| 114 |
private void readName() throws IOException { |
| 115 |
int mark = pos; |
| 116 |
|
| 117 |
while (pos < buf.length) { |
| 118 |
if (buf[pos++] != ':') { |
| 119 |
continue; |
| 120 |
} |
| 121 |
|
| 122 |
String nameString = new String(buf, mark, pos - mark - 1, StandardCharsets.US_ASCII); |
| 123 |
|
| 124 |
if (buf[pos++] != ' ') { |
| 125 |
throw new IOException(String.format("Invalid value for attribute '%s'", nameString)); |
| 126 |
} |
| 127 |
|
| 128 |
try { |
| 129 |
name = attributeNameCache.get(nameString); |
| 130 |
if (name == null) { |
| 131 |
name = new Attributes.Name(nameString); |
| 132 |
attributeNameCache.put(nameString, name); |
| 133 |
} |
| 134 |
} catch (IllegalArgumentException e) { |
| 135 |
|
| 136 |
throw new IOException(e.getMessage()); |
| 137 |
} |
| 138 |
return; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
private void readValue() throws IOException { |
| 143 |
boolean lastCr = false; |
| 144 |
int mark = pos; |
| 145 |
int last = pos; |
| 146 |
valueBuffer.reset(); |
| 147 |
while (pos < buf.length) { |
| 148 |
byte next = buf[pos++]; |
| 149 |
switch (next) { |
| 150 |
case 0: |
| 151 |
throw new IOException("NUL character in a manifest"); |
| 152 |
case '\n': |
| 153 |
if (lastCr) { |
| 154 |
lastCr = false; |
| 155 |
} else { |
| 156 |
consecutiveLineBreaks++; |
| 157 |
} |
| 158 |
continue; |
| 159 |
case '\r': |
| 160 |
lastCr = true; |
| 161 |
consecutiveLineBreaks++; |
| 162 |
continue; |
| 163 |
case ' ': |
| 164 |
if (consecutiveLineBreaks == 1) { |
| 165 |
valueBuffer.write(buf, mark, last - mark); |
| 166 |
mark = pos; |
| 167 |
consecutiveLineBreaks = 0; |
| 168 |
continue; |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
if (consecutiveLineBreaks >= 1) { |
| 173 |
pos--; |
| 174 |
break; |
| 175 |
} |
| 176 |
last = pos; |
| 177 |
} |
| 178 |
|
| 179 |
valueBuffer.write(buf, mark, last - mark); |
| 180 |
|
| 181 |
|
| 182 |
value = valueBuffer.toString(StandardCharsets.UTF_8.name()); |
| 183 |
} |
| 184 |
} |