Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(475)

Side by Side Diff: tools/data_pack/data_pack.py

Issue 7744017: Updated *.pak file format to support both UTF8 and UTF16 (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Updated. Created 9 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 #!/usr/bin/python 1 #!/usr/bin/python
2 # Copyright (c) 2011 The Chromium Authors. All rights reserved. 2 # Copyright (c) 2011 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be 3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file. 4 # found in the LICENSE file.
5 5
6 """A simple utility function to produce data pack files. 6 """A simple utility function to produce data pack files.
7 See base/pack_file* for details. 7 See base/pack_file* for details.
8 """ 8 """
9 9
10 import struct 10 import struct
11 11
12 FILE_FORMAT_VERSION = 3 12 FILE_FORMAT_VERSION = 4
13 HEADER_LENGTH = 2 * 4 # Two uint32s. (file version and number of entries) 13 HEADER_LENGTH = 2 * 4 + 1 # Two uint32s. (file version, number of entries) and
14 # one uint8 (encoding of text resources)
15 BINARY, UTF8, UTF16 = range(3)
14 16
15 class WrongFileVersion(Exception): 17 class WrongFileVersion(Exception):
16 pass 18 pass
17 19
20 class DataPackContents:
21 def __init__(self, resources, encoding):
22 self.resources = resources
23 self.encoding = encoding
24
18 def ReadDataPack(input_file): 25 def ReadDataPack(input_file):
19 """Reads a data pack file and returns a dictionary.""" 26 """Reads a data pack file and returns a dictionary."""
20 data = open(input_file, "rb").read() 27 data = open(input_file, "rb").read()
21 original_data = data 28 original_data = data
22 29
23 # Read the header. 30 # Read the header.
24 version, num_entries = struct.unpack("<II", data[:HEADER_LENGTH]) 31 version, num_entries, encoding = struct.unpack("<IIB", data[:HEADER_LENGTH])
25 if version != FILE_FORMAT_VERSION: 32 if version != FILE_FORMAT_VERSION:
33 print "Wrong file version in ", input_file
26 raise WrongFileVersion 34 raise WrongFileVersion
27 35
28 resources = {} 36 resources = {}
29 if num_entries == 0: 37 if num_entries == 0:
30 return resources 38 return DataPackContents(resources, encoding)
31 39
32 # Read the index and data. 40 # Read the index and data.
33 data = data[HEADER_LENGTH:] 41 data = data[HEADER_LENGTH:]
34 kIndexEntrySize = 2 + 4 # Each entry is a uint16 and a uint32. 42 kIndexEntrySize = 2 + 4 # Each entry is a uint16 and a uint32.
35 for _ in range(num_entries): 43 for _ in range(num_entries):
36 id, offset = struct.unpack("<HI", data[:kIndexEntrySize]) 44 id, offset = struct.unpack("<HI", data[:kIndexEntrySize])
37 data = data[kIndexEntrySize:] 45 data = data[kIndexEntrySize:]
38 next_id, next_offset = struct.unpack("<HI", data[:kIndexEntrySize]) 46 next_id, next_offset = struct.unpack("<HI", data[:kIndexEntrySize])
39 resources[id] = original_data[offset:next_offset] 47 resources[id] = original_data[offset:next_offset]
40 48
41 return resources 49 return DataPackContents(resources, encoding)
42 50
43 def WriteDataPack(resources, output_file): 51 def WriteDataPack(resources, output_file, encoding):
44 """Write a map of id=>data into output_file as a data pack.""" 52 """Write a map of id=>data into output_file as a data pack."""
45 ids = sorted(resources.keys()) 53 ids = sorted(resources.keys())
46 file = open(output_file, "wb") 54 file = open(output_file, "wb")
47 55
48 # Write file header. 56 # Write file header.
49 file.write(struct.pack("<II", FILE_FORMAT_VERSION, len(ids))) 57 file.write(struct.pack("<IIB", FILE_FORMAT_VERSION, len(ids), encoding))
50 58
51 # Each entry is a uint16 and a uint32. We have one extra entry for the last 59 # Each entry is a uint16 and a uint32. We have one extra entry for the last
52 # item. 60 # item.
53 index_length = (len(ids) + 1) * (2 + 4) 61 index_length = (len(ids) + 1) * (2 + 4)
54 62
55 # Write index. 63 # Write index.
56 data_offset = HEADER_LENGTH + index_length 64 data_offset = HEADER_LENGTH + index_length
57 for id in ids: 65 for id in ids:
58 file.write(struct.pack("<HI", id, data_offset)) 66 file.write(struct.pack("<HI", id, data_offset))
59 data_offset += len(resources[id]) 67 data_offset += len(resources[id])
60 68
61 file.write(struct.pack("<HI", 0, data_offset)) 69 file.write(struct.pack("<HI", 0, data_offset))
62 70
63 # Write data. 71 # Write data.
64 for id in ids: 72 for id in ids:
65 file.write(resources[id]) 73 file.write(resources[id])
66 74
67 def main(): 75 def main():
68 # Just write a simple file. 76 # Just write a simple file.
69 data = { 1: "", 4: "this is id 4", 6: "this is id 6", 10: "" } 77 data = { 1: "", 4: "this is id 4", 6: "this is id 6", 10: "" }
70 WriteDataPack(data, "datapack1.pak") 78 WriteDataPack(data, "datapack1.pak", UTF8)
71 data2 = { 1000: "test", 5: "five" } 79 data2 = { 1000: "test", 5: "five" }
72 WriteDataPack(data2, "datapack2.pak") 80 WriteDataPack(data2, "datapack2.pak", UTF8)
73 print "wrote datapack1 and datapack2 to current directory." 81 print "wrote datapack1 and datapack2 to current directory."
74 82
75 if __name__ == '__main__': 83 if __name__ == '__main__':
76 main() 84 main()
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698