android-xml.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * Copyright (C) 2008 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. #ifndef ANDROID_XML_H
  17. #define ANDROID_XML_H
  18. #include <stdint.h>
  19. extern uint32_t g_lineno;
  20. /**
  21. * Header that appears at the front of every data chunk in a resource.
  22. */
  23. struct ResChunk_header
  24. {
  25. // Type identifier for this chunk. The meaning of this value depends
  26. // on the containing chunk.
  27. uint16_t type;
  28. // Size of the chunk header (in bytes). Adding this value to
  29. // the address of the chunk allows you to find its associated data
  30. // (if any).
  31. uint16_t headerSize;
  32. // Total size of this chunk (in bytes). This is the chunkSize plus
  33. // the size of any data associated with the chunk. Adding this value
  34. // to the chunk allows you to completely skip its contents (including
  35. // any child chunks). If this value is the same as chunkSize, there is
  36. // no data associated with the chunk.
  37. uint32_t size;
  38. };
  39. enum {
  40. RES_NULL_TYPE = 0x0000,
  41. RES_STRING_POOL_TYPE = 0x0001,
  42. RES_TABLE_TYPE = 0x0002,
  43. RES_XML_TYPE = 0x0003,
  44. // Chunk types in RES_XML_TYPE
  45. RES_XML_FIRST_CHUNK_TYPE = 0x0100,
  46. RES_XML_START_NAMESPACE_TYPE= 0x0100,
  47. RES_XML_END_NAMESPACE_TYPE = 0x0101,
  48. RES_XML_START_ELEMENT_TYPE = 0x0102,
  49. RES_XML_END_ELEMENT_TYPE = 0x0103,
  50. RES_XML_CDATA_TYPE = 0x0104,
  51. RES_XML_LAST_CHUNK_TYPE = 0x017f,
  52. // This contains a uint32_t array mapping strings in the string
  53. // pool back to resource identifiers. It is optional.
  54. RES_XML_RESOURCE_MAP_TYPE = 0x0180,
  55. // Chunk types in RES_TABLE_TYPE
  56. RES_TABLE_PACKAGE_TYPE = 0x0200,
  57. RES_TABLE_TYPE_TYPE = 0x0201,
  58. RES_TABLE_TYPE_SPEC_TYPE = 0x0202,
  59. RES_TABLE_LIBRARY_TYPE = 0x0203
  60. };
  61. /**
  62. * Definition for a pool of strings. The data of this chunk is an
  63. * array of uint32_t providing indices into the pool, relative to
  64. * stringsStart. At stringsStart are all of the UTF-16 strings
  65. * concatenated together; each starts with a uint16_t of the string's
  66. * length and each ends with a 0x0000 terminator. If a string is >
  67. * 32767 characters, the high bit of the length is set meaning to take
  68. * those 15 bits as a high word and it will be followed by another
  69. * uint16_t containing the low word.
  70. *
  71. * If styleCount is not zero, then immediately following the array of
  72. * uint32_t indices into the string table is another array of indices
  73. * into a style table starting at stylesStart. Each entry in the
  74. * style table is an array of ResStringPool_span structures.
  75. */
  76. struct ResStringPool_header
  77. {
  78. struct ResChunk_header header;
  79. // Number of strings in this pool (number of uint32_t indices that follow
  80. // in the data).
  81. uint32_t stringCount;
  82. // Number of style span arrays in the pool (number of uint32_t indices
  83. // follow the string indices).
  84. uint32_t styleCount;
  85. // Flags.
  86. enum {
  87. // If set, the string index is sorted by the string values (based
  88. // on strcmp16()).
  89. SORTED_FLAG = 1<<0,
  90. // String pool is encoded in UTF-8
  91. UTF8_FLAG = 1<<8
  92. };
  93. uint32_t flags;
  94. // Index from header of the string data.
  95. uint32_t stringsStart;
  96. // Index from header of the style data.
  97. uint32_t stylesStart;
  98. };
  99. /**
  100. * Reference to a string in a string pool.
  101. */
  102. struct ResStringPool_ref
  103. {
  104. // Index into the string pool table (uint32_t-offset from the indices
  105. // immediately after ResStringPool_header) at which to find the location
  106. // of the string data in the pool.
  107. uint32_t index;
  108. };
  109. /**
  110. * Extended XML tree node for element start/end nodes.
  111. * Appears header.headerSize bytes after a ResXMLTree_node.
  112. */
  113. struct ResXMLTree_endElementExt
  114. {
  115. // String of the full namespace of this element.
  116. struct ResStringPool_ref ns;
  117. // String name of this node if it is an ELEMENT; the raw
  118. // character data if this is a CDATA node.
  119. struct ResStringPool_ref name;
  120. };
  121. /**
  122. * Basic XML tree node. A single item in the XML document. Extended info
  123. * about the node can be found after header.headerSize.
  124. */
  125. struct ResXMLTree_node
  126. {
  127. struct ResChunk_header header;
  128. // Line number in original source file at which this element appeared.
  129. uint32_t lineNumber;
  130. // Optional XML comment that was associated with this element; -1 if none.
  131. struct ResStringPool_ref comment;
  132. };
  133. /**
  134. * Representation of a value in a resource, supplying type
  135. * information.
  136. */
  137. struct Res_value
  138. {
  139. // Number of bytes in this structure.
  140. uint16_t size;
  141. // Always set to 0.
  142. uint8_t res0;
  143. // Type of the data value.
  144. enum : uint8_t {
  145. // The 'data' is either 0 or 1, specifying this resource is either
  146. // undefined or empty, respectively.
  147. TYPE_NULL = 0x00,
  148. // The 'data' holds a ResTable_ref, a reference to another resource
  149. // table entry.
  150. TYPE_REFERENCE = 0x01,
  151. // The 'data' holds an attribute resource identifier.
  152. TYPE_ATTRIBUTE = 0x02,
  153. // The 'data' holds an index into the containing resource table's
  154. // global value string pool.
  155. TYPE_STRING = 0x03,
  156. // The 'data' holds a single-precision floating point number.
  157. TYPE_FLOAT = 0x04,
  158. // The 'data' holds a complex number encoding a dimension value,
  159. // such as "100in".
  160. TYPE_DIMENSION = 0x05,
  161. // The 'data' holds a complex number encoding a fraction of a
  162. // container.
  163. TYPE_FRACTION = 0x06,
  164. // The 'data' holds a dynamic ResTable_ref, which needs to be
  165. // resolved before it can be used like a TYPE_REFERENCE.
  166. TYPE_DYNAMIC_REFERENCE = 0x07,
  167. // The 'data' holds an attribute resource identifier, which needs to be resolved
  168. // before it can be used like a TYPE_ATTRIBUTE.
  169. TYPE_DYNAMIC_ATTRIBUTE = 0x08,
  170. // Beginning of integer flavors...
  171. TYPE_FIRST_INT = 0x10,
  172. // The 'data' is a raw integer value of the form n..n.
  173. TYPE_INT_DEC = 0x10,
  174. // The 'data' is a raw integer value of the form 0xn..n.
  175. TYPE_INT_HEX = 0x11,
  176. // The 'data' is either 0 or 1, for input "false" or "true" respectively.
  177. TYPE_INT_BOOLEAN = 0x12,
  178. // Beginning of color integer flavors...
  179. TYPE_FIRST_COLOR_INT = 0x1c,
  180. // The 'data' is a raw integer value of the form #aarrggbb.
  181. TYPE_INT_COLOR_ARGB8 = 0x1c,
  182. // The 'data' is a raw integer value of the form #rrggbb.
  183. TYPE_INT_COLOR_RGB8 = 0x1d,
  184. // The 'data' is a raw integer value of the form #argb.
  185. TYPE_INT_COLOR_ARGB4 = 0x1e,
  186. // The 'data' is a raw integer value of the form #rgb.
  187. TYPE_INT_COLOR_RGB4 = 0x1f,
  188. // ...end of integer flavors.
  189. TYPE_LAST_COLOR_INT = 0x1f,
  190. // ...end of integer flavors.
  191. TYPE_LAST_INT = 0x1f
  192. };
  193. uint8_t dataType;
  194. // Structure of complex data values (TYPE_UNIT and TYPE_FRACTION)
  195. enum {
  196. // Where the unit type information is. This gives us 16 possible
  197. // types, as defined below.
  198. COMPLEX_UNIT_SHIFT = 0,
  199. COMPLEX_UNIT_MASK = 0xf,
  200. // TYPE_DIMENSION: Value is raw pixels.
  201. COMPLEX_UNIT_PX = 0,
  202. // TYPE_DIMENSION: Value is Device Independent Pixels.
  203. COMPLEX_UNIT_DIP = 1,
  204. // TYPE_DIMENSION: Value is a Scaled device independent Pixels.
  205. COMPLEX_UNIT_SP = 2,
  206. // TYPE_DIMENSION: Value is in points.
  207. COMPLEX_UNIT_PT = 3,
  208. // TYPE_DIMENSION: Value is in inches.
  209. COMPLEX_UNIT_IN = 4,
  210. // TYPE_DIMENSION: Value is in millimeters.
  211. COMPLEX_UNIT_MM = 5,
  212. // TYPE_FRACTION: A basic fraction of the overall size.
  213. COMPLEX_UNIT_FRACTION = 0,
  214. // TYPE_FRACTION: A fraction of the parent size.
  215. COMPLEX_UNIT_FRACTION_PARENT = 1,
  216. // Where the radix information is, telling where the decimal place
  217. // appears in the mantissa. This give us 4 possible fixed point
  218. // representations as defined below.
  219. COMPLEX_RADIX_SHIFT = 4,
  220. COMPLEX_RADIX_MASK = 0x3,
  221. // The mantissa is an integral number -- i.e., 0xnnnnnn.0
  222. COMPLEX_RADIX_23p0 = 0,
  223. // The mantissa magnitude is 16 bits -- i.e, 0xnnnn.nn
  224. COMPLEX_RADIX_16p7 = 1,
  225. // The mantissa magnitude is 8 bits -- i.e, 0xnn.nnnn
  226. COMPLEX_RADIX_8p15 = 2,
  227. // The mantissa magnitude is 0 bits -- i.e, 0x0.nnnnnn
  228. COMPLEX_RADIX_0p23 = 3,
  229. // Where the actual value is. This gives us 23 bits of
  230. // precision. The top bit is the sign.
  231. COMPLEX_MANTISSA_SHIFT = 8,
  232. COMPLEX_MANTISSA_MASK = 0xffffff
  233. };
  234. // Possible data values for TYPE_NULL.
  235. enum {
  236. // The value is not defined.
  237. DATA_NULL_UNDEFINED = 0,
  238. // The value is explicitly defined as empty.
  239. DATA_NULL_EMPTY = 1
  240. };
  241. // The data for this item, as interpreted according to dataType.
  242. typedef uint32_t data_type;
  243. data_type data;
  244. void copyFrom_dtoh(const Res_value& src);
  245. };
  246. /**
  247. * Extended XML tree node for start tags -- includes attribute
  248. * information.
  249. * Appears header.headerSize bytes after a ResXMLTree_node.
  250. */
  251. struct ResXMLTree_attrExt
  252. {
  253. // String of the full namespace of this element.
  254. struct ResStringPool_ref ns;
  255. // String name of this node if it is an ELEMENT; the raw
  256. // character data if this is a CDATA node.
  257. struct ResStringPool_ref name;
  258. // Byte offset from the start of this structure where the attributes start.
  259. uint16_t attributeStart;
  260. // Size of the ResXMLTree_attribute structures that follow.
  261. uint16_t attributeSize;
  262. // Number of attributes associated with an ELEMENT. These are
  263. // available as an array of ResXMLTree_attribute structures
  264. // immediately following this node.
  265. uint16_t attributeCount;
  266. // Index (1-based) of the "id" attribute. 0 if none.
  267. uint16_t idIndex;
  268. // Index (1-based) of the "class" attribute. 0 if none.
  269. uint16_t classIndex;
  270. // Index (1-based) of the "style" attribute. 0 if none.
  271. uint16_t styleIndex;
  272. };
  273. struct ResXMLTree_attribute
  274. {
  275. // Namespace of this attribute.
  276. struct ResStringPool_ref ns;
  277. // Name of this attribute.
  278. struct ResStringPool_ref name;
  279. // The original raw string value of this attribute.
  280. struct ResStringPool_ref rawValue;
  281. // Processesd typed value of this attribute.
  282. struct Res_value typedValue;
  283. };
  284. /**
  285. * Extended XML tree node for namespace start/end nodes.
  286. * Appears header.headerSize bytes after a ResXMLTree_node.
  287. */
  288. struct ResXMLTree_namespaceExt
  289. {
  290. // The prefix of the namespace.
  291. struct ResStringPool_ref prefix;
  292. // The URI of the namespace.
  293. struct ResStringPool_ref uri;
  294. };
  295. #endif