| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
package com.drew.metadata.plist; |
| 22 |
|
| 23 |
import java.io.IOException; |
| 24 |
import java.util.*; |
| 25 |
|
| 26 |
import com.drew.lang.SequentialByteArrayReader; |
| 27 |
import com.drew.lang.annotations.Nullable; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public class BplistReader |
| 40 |
{ |
| 41 |
private static final String PLIST_DTD = "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">"; |
| 42 |
|
| 43 |
private static final byte[] BPLIST_HEADER = {'b', 'p', 'l', 'i', 's', 't', '0', '0'}; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public static boolean isValid(byte[] bplist) |
| 49 |
{ |
| 50 |
if (bplist.length < BPLIST_HEADER.length) { |
| 51 |
return false; |
| 52 |
} |
| 53 |
|
| 54 |
boolean valid = true; |
| 55 |
for (int i = 0; i < BPLIST_HEADER.length; i++) { |
| 56 |
if (bplist[i] != BPLIST_HEADER[i]) { |
| 57 |
valid = false; |
| 58 |
break; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
return valid; |
| 63 |
} |
| 64 |
|
| 65 |
public static PropertyListResults parse(byte[] bplist) throws IOException |
| 66 |
{ |
| 67 |
if (!isValid(bplist)) { |
| 68 |
throw new IllegalArgumentException("Input is not a bplist"); |
| 69 |
} |
| 70 |
|
| 71 |
final ArrayList<Object> objects = new ArrayList<Object>(); |
| 72 |
final Trailer trailer = readTrailer(bplist); |
| 73 |
|
| 74 |
|
| 75 |
SequentialByteArrayReader reader = new SequentialByteArrayReader(bplist, (int)(trailer.offsetTableOffset + trailer.topObject)); |
| 76 |
int[] offsets = new int[(int)trailer.numObjects]; |
| 77 |
for (long i = 0; i < trailer.numObjects; i++) { |
| 78 |
if (trailer.offsetIntSize == 1) { |
| 79 |
offsets[(int)i] = reader.getByte(); |
| 80 |
} else if (trailer.offsetIntSize == 2) { |
| 81 |
offsets[(int)i] = reader.getUInt16(); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
for (int i = 0; i < offsets.length; i++) { |
| 86 |
reader = new SequentialByteArrayReader(bplist, offsets[i]); |
| 87 |
byte marker = reader.getByte(); |
| 88 |
int objectFormat = marker >> 4 & 0x0F; |
| 89 |
switch (objectFormat) { |
| 90 |
case 0x0D: |
| 91 |
handleDict(i, marker, reader, objects); |
| 92 |
break; |
| 93 |
case 0x05: |
| 94 |
int charCount = marker & 0x0F; |
| 95 |
objects.add(i, reader.getString(charCount)); |
| 96 |
break; |
| 97 |
case 0x04: |
| 98 |
handleData(i, marker, reader, objects); |
| 99 |
break; |
| 100 |
case 0x01: |
| 101 |
handleInt(i, marker, reader, objects); |
| 102 |
break; |
| 103 |
default: |
| 104 |
throw new IOException("Un-handled objectFormat encountered"); |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
return new PropertyListResults(objects, trailer); |
| 109 |
} |
| 110 |
|
| 111 |
private static void handleInt(final int objectIndex, final byte marker, final SequentialByteArrayReader reader, final ArrayList<Object> objects) throws IOException |
| 112 |
{ |
| 113 |
int objectSize = (int)Math.pow(2, (marker & 0x0F)); |
| 114 |
if (objectSize == 1) { |
| 115 |
objects.add(objectIndex, reader.getByte()); |
| 116 |
} else if (objectSize == 2) { |
| 117 |
objects.add(objectIndex, reader.getUInt16()); |
| 118 |
} else if (objectSize == 4) { |
| 119 |
objects.add(objectIndex, reader.getUInt32()); |
| 120 |
} else if (objectSize == 8) { |
| 121 |
objects.add(objectIndex, reader.getInt64()); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
private static void handleDict(final int objectIndex, final byte marker, final SequentialByteArrayReader reader, final ArrayList<Object> objects) throws IOException |
| 126 |
{ |
| 127 |
|
| 128 |
LinkedHashMap<Byte, Byte> map = new LinkedHashMap<Byte, Byte>(); |
| 129 |
int dictEntries = marker & 0x0F; |
| 130 |
byte[] keyRefs = new byte[dictEntries]; |
| 131 |
|
| 132 |
for (int j = 0; j < dictEntries; j++) { |
| 133 |
keyRefs[j] = reader.getByte(); |
| 134 |
} |
| 135 |
for (int j = 0; j < dictEntries; j++) { |
| 136 |
map.put(keyRefs[j], reader.getByte()); |
| 137 |
} |
| 138 |
|
| 139 |
objects.add(objectIndex, map); |
| 140 |
} |
| 141 |
|
| 142 |
private static void handleData(final int objectIndex, final byte marker, final SequentialByteArrayReader reader, final ArrayList<Object> objects) throws IOException |
| 143 |
{ |
| 144 |
int byteCount = marker & 0x0F; |
| 145 |
if (byteCount == 0x0F) { |
| 146 |
byte sizeMarker = reader.getByte(); |
| 147 |
if ((sizeMarker >> 4 & 0x0F) != 1) { |
| 148 |
throw new IllegalArgumentException("Invalid size marker"); |
| 149 |
} |
| 150 |
|
| 151 |
int objectSizeWidth = (int)Math.pow(2, sizeMarker & 0x0F); |
| 152 |
if (objectSizeWidth == 1) { |
| 153 |
byteCount = reader.getInt8(); |
| 154 |
} else if (objectSizeWidth == 2) { |
| 155 |
byteCount = reader.getUInt16(); |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
objects.add(objectIndex, reader.getBytes(byteCount)); |
| 160 |
} |
| 161 |
|
| 162 |
public static class PropertyListResults |
| 163 |
{ |
| 164 |
private final List<Object> objects; |
| 165 |
private final Trailer trailer; |
| 166 |
|
| 167 |
public PropertyListResults(List<Object> objects, Trailer trailer) |
| 168 |
{ |
| 169 |
this.objects = objects; |
| 170 |
this.trailer = trailer; |
| 171 |
} |
| 172 |
|
| 173 |
public List<Object> getObjects() |
| 174 |
{ |
| 175 |
return objects; |
| 176 |
} |
| 177 |
|
| 178 |
public Trailer getTrailer() |
| 179 |
{ |
| 180 |
return trailer; |
| 181 |
} |
| 182 |
|
| 183 |
@Nullable |
| 184 |
public Set<Map.Entry<Byte, Byte>> getEntrySet() |
| 185 |
{ |
| 186 |
final Object topObject = this.getObjects().get((int)this.getTrailer().topObject); |
| 187 |
|
| 188 |
if (topObject instanceof Map) { |
| 189 |
@SuppressWarnings("unchecked") |
| 190 |
Map<Byte, Byte> dict = (Map<Byte, Byte>)topObject; |
| 191 |
return dict.entrySet(); |
| 192 |
} |
| 193 |
|
| 194 |
return null; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
public String toXML() |
| 201 |
{ |
| 202 |
final StringBuilder xml = new StringBuilder() |
| 203 |
.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>") |
| 204 |
.append(PLIST_DTD) |
| 205 |
.append("<plist version=\"1.0\">"); |
| 206 |
|
| 207 |
final Set<Map.Entry<Byte, Byte>> entrySet = getEntrySet(); |
| 208 |
|
| 209 |
if (entrySet != null) { |
| 210 |
xml.append("<dict>"); |
| 211 |
|
| 212 |
for (Map.Entry<Byte, Byte> entry : entrySet) { |
| 213 |
xml.append("<key>") |
| 214 |
.append((String)this.getObjects().get(entry.getKey())) |
| 215 |
.append("</key>"); |
| 216 |
xml.append("<integer>") |
| 217 |
.append(this.getObjects().get(entry.getValue()).toString()) |
| 218 |
.append("</integer>"); |
| 219 |
} |
| 220 |
|
| 221 |
xml.append("</dict>"); |
| 222 |
} |
| 223 |
|
| 224 |
xml.append("</plist>"); |
| 225 |
|
| 226 |
return xml.toString(); |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
private static Trailer readTrailer(byte[] bplist) throws IOException |
| 239 |
{ |
| 240 |
SequentialByteArrayReader reader = new SequentialByteArrayReader(bplist, bplist.length - Trailer.STRUCT_SIZE); |
| 241 |
reader.skip(5L); |
| 242 |
reader.skip(1L); |
| 243 |
|
| 244 |
final Trailer trailer = new Trailer(); |
| 245 |
trailer.offsetIntSize = reader.getByte(); |
| 246 |
trailer.objectRefSize = reader.getByte(); |
| 247 |
trailer.numObjects = reader.getInt64(); |
| 248 |
trailer.topObject = reader.getInt64(); |
| 249 |
trailer.offsetTableOffset = reader.getInt64(); |
| 250 |
|
| 251 |
return trailer; |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
private static class Trailer |
| 259 |
{ |
| 260 |
public static final int STRUCT_SIZE = 32; |
| 261 |
|
| 262 |
byte offsetIntSize; |
| 263 |
byte objectRefSize; |
| 264 |
long numObjects; |
| 265 |
long topObject; |
| 266 |
long offsetTableOffset; |
| 267 |
} |
| 268 |
} |