• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 *******************************************************************************
5 *
6 *   Copyright (C) 2005-2014, International Business Machines
7 *   Corporation and others.  All Rights Reserved.
8 *
9 *******************************************************************************
10 *   file name:  package.h
11 *   encoding:   UTF-8
12 *   tab size:   8 (not used)
13 *   indentation:4
14 *
15 *   created on: 2005aug25
16 *   created by: Markus W. Scherer
17 *
18 *   Read, modify, and write ICU .dat data package files.
19 */
20 
21 #ifndef __PACKAGE_H__
22 #define __PACKAGE_H__
23 
24 #include "unicode/utypes.h"
25 
26 #include <stdio.h>
27 
28 // .dat package file representation ---------------------------------------- ***
29 
30 #define STRING_STORE_SIZE 100000
31 #define MAX_PKG_NAME_LENGTH 64
32 
33 typedef void CheckDependency(void *context, const char *itemName, const char *targetName);
34 
35 U_NAMESPACE_BEGIN
36 
37 struct Item {
38     char *name;
39     uint8_t *data;
40     int32_t length;
41     UBool isDataOwned;
42     char type;
43 };
44 
45 class U_TOOLUTIL_API Package {
46 public:
47     /*
48      * Constructor.
49      * Prepare this object for a new, empty package.
50      */
51     Package();
52 
53     /* Destructor. */
54     ~Package();
55 
56     /**
57      * Uses the prefix of the first entry of the package in readPackage(),
58      * rather than the package basename.
59      */
setAutoPrefix()60     void setAutoPrefix() { doAutoPrefix=true; }
61     /**
62      * Same as setAutoPrefix(), plus the prefix must end with the platform type letter.
63      */
setAutoPrefixWithType()64     void setAutoPrefixWithType() {
65         doAutoPrefix=true;
66         prefixEndsWithType=true;
67     }
68     void setPrefix(const char *p);
69 
70     /*
71      * Read an existing .dat package file.
72      * The header and item name strings are swapped into this object,
73      * but the items are left unswapped.
74      */
75     void readPackage(const char *filename);
76     /*
77      * Write a .dat package file with the items in this object.
78      * Swap all pieces to the desired output platform properties.
79      * The package becomes unusable:
80      * The item names are swapped and sorted in the outCharset rather than the local one.
81      * Also, the items themselves are swapped in-place
82      */
83     void writePackage(const char *filename, char outType, const char *comment);
84 
85     /*
86      * Return the input data type letter (l, b, or e).
87      */
88     char getInType();
89 
90     // find the item in items[], return the non-negative index if found, else the binary-not of the insertion point
91     int32_t findItem(const char *name, int32_t length=-1) const;
92 
93     /*
94      * Set internal state for following calls to findNextItem() which will return
95      * indexes for items whose names match the pattern.
96      */
97     void findItems(const char *pattern);
98     int32_t findNextItem();
99     /*
100      * Set the match mode for findItems() & findNextItem().
101      * @param mode 0=default
102      *             MATCH_NOSLASH * does not match a '/'
103      */
104     void setMatchMode(uint32_t mode);
105 
106     enum {
107         MATCH_NOSLASH=1
108     };
109 
110     void addItem(const char *name);
111     void addItem(const char *name, uint8_t *data, int32_t length, UBool isDataOwned, char type);
112     void addFile(const char *filesPath, const char *name);
113     void addItems(const Package &listPkg);
114 
115     void removeItem(int32_t itemIndex);
116     void removeItems(const char *pattern);
117     void removeItems(const Package &listPkg);
118 
119     /* The extractItem() functions accept outputType=0 to mean "don't swap the item". */
120     void extractItem(const char *filesPath, int32_t itemIndex, char outType);
121     void extractItems(const char *filesPath, const char *pattern, char outType);
122     void extractItems(const char *filesPath, const Package &listPkg, char outType);
123 
124     /* This variant extracts an item to a specific filename. */
125     void extractItem(const char *filesPath, const char *outName, int32_t itemIndex, char outType);
126 
127     int32_t getItemCount() const;
128     const Item *getItem(int32_t idx) const;
129 
130     /*
131      * Check dependencies and return true if all dependencies are fulfilled.
132      */
133     UBool checkDependencies();
134 
135     /*
136      * Enumerate all the dependencies and give the results to context and call CheckDependency callback
137      * @param context user context (will be passed to check function)
138      * @param check will be called with context and any missing items
139      */
140     void enumDependencies(void *context, CheckDependency check);
141 
142 private:
143     void enumDependencies(Item *pItem, void *context, CheckDependency check);
144 
145     /**
146      * Default CheckDependency function used by checkDependencies()
147      */
148     static void checkDependency(void *context, const char *itemName, const char *targetName);
149 
150     /*
151      * Allocate a string in inStrings or outStrings.
152      * The length does not include the terminating NUL.
153      */
154     char *allocString(UBool in, int32_t length);
155 
156     void sortItems();
157 
158     // data fields
159     char inPkgName[MAX_PKG_NAME_LENGTH];
160     char pkgPrefix[MAX_PKG_NAME_LENGTH];
161 
162     uint8_t *inData;
163     uint8_t header[1024];
164     int32_t inLength, headerLength;
165     uint8_t inCharset;
166     UBool inIsBigEndian;
167     UBool doAutoPrefix;
168     UBool prefixEndsWithType;
169 
170     int32_t itemCount;
171     int32_t itemMax;
172     Item   *items;
173 
174     int32_t inStringTop, outStringTop;
175     char inStrings[STRING_STORE_SIZE], outStrings[STRING_STORE_SIZE];
176 
177     // match mode for findItems(pattern) and findNextItem()
178     uint32_t matchMode;
179 
180     // state for findItems(pattern) and findNextItem()
181     const char *findPrefix, *findSuffix;
182     int32_t findPrefixLength, findSuffixLength;
183     int32_t findNextIndex;
184 
185     // state for checkDependencies()
186     UBool isMissingItems;
187 
188     /**
189      * Grow itemMax to new value
190      */
191     void setItemCapacity(int32_t max);
192 
193     /**
194      * Grow itemMax to at least itemCount+1
195      */
196     void ensureItemCapacity();
197 };
198 
199 U_NAMESPACE_END
200 
201 #endif
202 
203 
204