blob: a8b72e6948d5cece0df3600b1bdf289a50802c52 [file] [log] [blame]
Neil Fuller0480f462019-04-09 19:23:35 +01001/*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import java.io.File;
18import java.io.IOException;
19import java.io.RandomAccessFile;
20import java.nio.MappedByteBuffer;
21import java.nio.channels.FileChannel;
22import java.nio.charset.StandardCharsets;
23import java.nio.file.Files;
24import java.nio.file.StandardOpenOption;
25import java.util.Arrays;
26
27/**
28 * Reverses the ZoneCompactor process to extract information and zic output files from Android's
29 * tzdata file. This enables easier debugging / inspection of Android's tzdata file with standard
30 * tools like zdump or Android tools like TzFileDumper.
31 *
Neil Fuller92f87e62020-01-28 13:47:49 +000032 * <p>This class contains a copy of logic found in Android's ZoneInfoDb.
Neil Fuller0480f462019-04-09 19:23:35 +010033 */
34public class ZoneSplitter {
35
36 public static void main(String[] args) throws Exception {
37 if (args.length != 2) {
38 System.err.println("usage: java ZoneSplitter <tzdata file> <output directory>");
39 System.exit(0);
40 }
41 new ZoneSplitter(args[0], args[1]).execute();
42 }
43
44 private final File tzData;
45 private final File outputDir;
46
47 private ZoneSplitter(String tzData, String outputDir) {
48 this.tzData = new File(tzData);
49 this.outputDir = new File(outputDir);
50 }
51
52 private void execute() throws IOException {
53 if (!(tzData.exists() && tzData.isFile() && tzData.canRead())) {
54 throw new IOException(tzData + " not found or is not readable");
55 }
56 if (!(outputDir.exists() && outputDir.isDirectory())) {
57 throw new IOException(outputDir + " not found or is not a directory");
58 }
59
60 MappedByteBuffer mappedFile = createMappedByteBuffer(tzData);
61
62 // byte[12] tzdata_version -- "tzdata2012f\0"
63 // int index_offset
64 // int data_offset
65 // int zonetab_offset
66 writeVersionFile(mappedFile, outputDir);
67
68 final int fileSize = (int) tzData.length();
69 int index_offset = mappedFile.getInt();
70 validateOffset(index_offset, fileSize);
71 int data_offset = mappedFile.getInt();
72 validateOffset(data_offset, fileSize);
73 int zonetab_offset = mappedFile.getInt();
74 validateOffset(zonetab_offset, fileSize);
75
76 if (index_offset >= data_offset || data_offset >= zonetab_offset) {
77 throw new IOException("Invalid offset: index_offset=" + index_offset
78 + ", data_offset=" + data_offset + ", zonetab_offset=" + zonetab_offset
79 + ", fileSize=" + fileSize);
80 }
81
82 File zicFilesDir = new File(outputDir, "zones");
83 zicFilesDir.mkdir();
84 extractZicFiles(mappedFile, index_offset, data_offset, zicFilesDir);
85
86 writeZoneTabFile(mappedFile, zonetab_offset, fileSize - zonetab_offset, outputDir);
87 }
88
89 static MappedByteBuffer createMappedByteBuffer(File tzData) throws IOException {
90 MappedByteBuffer mappedFile;
91 RandomAccessFile file = new RandomAccessFile(tzData, "r");
92 try (FileChannel fileChannel = file.getChannel()) {
93 mappedFile = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, file.length());
94 }
95 mappedFile.load();
96 return mappedFile;
97 }
98
99 private static void validateOffset(int offset, int size) throws IOException {
100 if (offset < 0 || offset >= size) {
101 throw new IOException("Invalid offset=" + offset + ", size=" + size);
102 }
103 }
104
105 private static void writeVersionFile(MappedByteBuffer mappedFile, File targetDir)
106 throws IOException {
107
108 byte[] tzdata_version = new byte[12];
109 mappedFile.get(tzdata_version);
110
111 String magic = new String(tzdata_version, 0, 6, StandardCharsets.US_ASCII);
112 if (!magic.startsWith("tzdata") || tzdata_version[11] != 0) {
113 throw new IOException("bad tzdata magic: " + Arrays.toString(tzdata_version));
114 }
115 writeStringUtf8ToFile(new File(targetDir, "version"),
116 new String(tzdata_version, 6, 5, StandardCharsets.US_ASCII));
117 }
118
119 private static void extractZicFiles(MappedByteBuffer mappedFile, int indexOffset,
120 int dataOffset, File outputDir) throws IOException {
121
122 mappedFile.position(indexOffset);
123
124 // The index of the tzdata file is made up of entries for each time zone ID which describe
125 // the location of the associated zic data in the data section of the file. The index
126 // section has no padding so we can determine the number of entries from the size.
127 //
128 // Each index entry consists of:
129 // byte[MAXNAME] idBytes - the id string, \0 terminated. e.g. "America/New_York\0"
130 // int32 byteOffset - the offset of the start of the zic data relative to the start of
131 // the tzdata data section
132 // int32 length - the length of the of the zic data
133 // int32 unused - no longer used
134 final int MAXNAME = 40;
135 final int SIZEOF_OFFSET = 4;
136 final int SIZEOF_INDEX_ENTRY = MAXNAME + 3 * SIZEOF_OFFSET;
137
138 int indexSize = (dataOffset - indexOffset);
139 if (indexSize % SIZEOF_INDEX_ENTRY != 0) {
140 throw new IOException("Index size is not divisible by " + SIZEOF_INDEX_ENTRY
141 + ", indexSize=" + indexSize);
142 }
143
144 byte[] idBytes = new byte[MAXNAME];
145 int entryCount = indexSize / SIZEOF_INDEX_ENTRY;
146 int[] byteOffsets = new int[entryCount];
147 int[] lengths = new int[entryCount];
148 String[] ids = new String[entryCount];
149
150 for (int i = 0; i < entryCount; i++) {
151 // Read the fixed length timezone ID.
152 mappedFile.get(idBytes, 0, idBytes.length);
153
154 // Read the offset into the file where the data for ID can be found.
155 byteOffsets[i] = mappedFile.getInt();
156 byteOffsets[i] += dataOffset;
157
158 lengths[i] = mappedFile.getInt();
159 if (lengths[i] < 44) {
160 throw new IOException("length in index file < sizeof(tzhead)");
161 }
162 mappedFile.getInt(); // Skip the unused 4 bytes that used to be the raw offset.
163
164 // Calculate the true length of the ID.
165 int len = 0;
166 while (len < idBytes.length && idBytes[len] != 0) {
167 len++;
168 }
169 if (len == 0) {
170 throw new IOException("Invalid ID at index=" + i);
171 }
172 ids[i] = new String(idBytes, 0, len, StandardCharsets.US_ASCII);
173 if (i > 0) {
174 if (ids[i].compareTo(ids[i - 1]) <= 0) {
175 throw new IOException(
176 "Index not sorted or contains multiple entries with the same ID"
177 + ", index=" + i + ", ids[i]=" + ids[i] + ", ids[i - 1]=" + ids[i - 1]);
178 }
179 }
180 }
181 for (int i = 0; i < entryCount; i++) {
182 String id = ids[i];
183 int byteOffset = byteOffsets[i];
184 int length = lengths[i];
185
186 File subFile = new File(outputDir, id.replace('/', '_'));
187 mappedFile.position(byteOffset);
188 byte[] bytes = new byte[length];
189 mappedFile.get(bytes, 0, length);
190
191 writeBytesToFile(subFile, bytes);
192 }
193 }
194
195 private static void writeZoneTabFile(MappedByteBuffer mappedFile,
196 int zoneTabOffset, int zoneTabSize, File outputDir) throws IOException {
197 byte[] bytes = new byte[zoneTabSize];
198 mappedFile.position(zoneTabOffset);
199 mappedFile.get(bytes, 0, bytes.length);
200 writeBytesToFile(new File(outputDir, "zone.tab"), bytes);
201 }
202
203 private static void writeStringUtf8ToFile(File file, String string) throws IOException {
204 writeBytesToFile(file, string.getBytes(StandardCharsets.UTF_8));
205 }
206
207 private static void writeBytesToFile(File file, byte[] bytes) throws IOException {
208 System.out.println("Writing: " + file);
209 Files.write(file.toPath(), bytes, StandardOpenOption.CREATE);
210 }
211}