Utilities.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. //
  2. // Utilities.cs
  3. //
  4. // Author:
  5. // Jb Evain (jbevain@gmail.com)
  6. //
  7. // Copyright (c) 2008 - 2011 Jb Evain
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using Mono.Cecil.Metadata;
  30. namespace Mono.Cecil
  31. {
  32. static partial class Mixin
  33. {
  34. public static uint ReadCompressedUInt32(byte[] data, ref int position)
  35. {
  36. uint integer;
  37. if ((data[position] & 0x80) == 0)
  38. {
  39. integer = data[position];
  40. position++;
  41. }
  42. else if ((data[position] & 0x40) == 0)
  43. {
  44. integer = (uint)(data[position] & ~0x80) << 8;
  45. integer |= data[position + 1];
  46. position += 2;
  47. }
  48. else
  49. {
  50. integer = (uint)(data[position] & ~0xc0) << 24;
  51. integer |= (uint)data[position + 1] << 16;
  52. integer |= (uint)data[position + 2] << 8;
  53. integer |= (uint)data[position + 3];
  54. position += 4;
  55. }
  56. return integer;
  57. }
  58. public static MetadataToken GetMetadataToken(CodedIndex self, uint data)
  59. {
  60. uint rid;
  61. TokenType token_type;
  62. switch (self)
  63. {
  64. case CodedIndex.TypeDefOrRef:
  65. rid = data >> 2;
  66. switch (data & 3)
  67. {
  68. case 0:
  69. token_type = TokenType.TypeDef; goto ret;
  70. case 1:
  71. token_type = TokenType.TypeRef; goto ret;
  72. case 2:
  73. token_type = TokenType.TypeSpec; goto ret;
  74. default:
  75. goto exit;
  76. }
  77. case CodedIndex.HasConstant:
  78. rid = data >> 2;
  79. switch (data & 3)
  80. {
  81. case 0:
  82. token_type = TokenType.Field; goto ret;
  83. case 1:
  84. token_type = TokenType.Param; goto ret;
  85. case 2:
  86. token_type = TokenType.Property; goto ret;
  87. default:
  88. goto exit;
  89. }
  90. case CodedIndex.HasCustomAttribute:
  91. rid = data >> 5;
  92. switch (data & 31)
  93. {
  94. case 0:
  95. token_type = TokenType.Method; goto ret;
  96. case 1:
  97. token_type = TokenType.Field; goto ret;
  98. case 2:
  99. token_type = TokenType.TypeRef; goto ret;
  100. case 3:
  101. token_type = TokenType.TypeDef; goto ret;
  102. case 4:
  103. token_type = TokenType.Param; goto ret;
  104. case 5:
  105. token_type = TokenType.InterfaceImpl; goto ret;
  106. case 6:
  107. token_type = TokenType.MemberRef; goto ret;
  108. case 7:
  109. token_type = TokenType.Module; goto ret;
  110. case 8:
  111. token_type = TokenType.Permission; goto ret;
  112. case 9:
  113. token_type = TokenType.Property; goto ret;
  114. case 10:
  115. token_type = TokenType.Event; goto ret;
  116. case 11:
  117. token_type = TokenType.Signature; goto ret;
  118. case 12:
  119. token_type = TokenType.ModuleRef; goto ret;
  120. case 13:
  121. token_type = TokenType.TypeSpec; goto ret;
  122. case 14:
  123. token_type = TokenType.Assembly; goto ret;
  124. case 15:
  125. token_type = TokenType.AssemblyRef; goto ret;
  126. case 16:
  127. token_type = TokenType.File; goto ret;
  128. case 17:
  129. token_type = TokenType.ExportedType; goto ret;
  130. case 18:
  131. token_type = TokenType.ManifestResource; goto ret;
  132. case 19:
  133. token_type = TokenType.GenericParam; goto ret;
  134. default:
  135. goto exit;
  136. }
  137. case CodedIndex.HasFieldMarshal:
  138. rid = data >> 1;
  139. switch (data & 1)
  140. {
  141. case 0:
  142. token_type = TokenType.Field; goto ret;
  143. case 1:
  144. token_type = TokenType.Param; goto ret;
  145. default:
  146. goto exit;
  147. }
  148. case CodedIndex.HasDeclSecurity:
  149. rid = data >> 2;
  150. switch (data & 3)
  151. {
  152. case 0:
  153. token_type = TokenType.TypeDef; goto ret;
  154. case 1:
  155. token_type = TokenType.Method; goto ret;
  156. case 2:
  157. token_type = TokenType.Assembly; goto ret;
  158. default:
  159. goto exit;
  160. }
  161. case CodedIndex.MemberRefParent:
  162. rid = data >> 3;
  163. switch (data & 7)
  164. {
  165. case 0:
  166. token_type = TokenType.TypeDef; goto ret;
  167. case 1:
  168. token_type = TokenType.TypeRef; goto ret;
  169. case 2:
  170. token_type = TokenType.ModuleRef; goto ret;
  171. case 3:
  172. token_type = TokenType.Method; goto ret;
  173. case 4:
  174. token_type = TokenType.TypeSpec; goto ret;
  175. default:
  176. goto exit;
  177. }
  178. case CodedIndex.HasSemantics:
  179. rid = data >> 1;
  180. switch (data & 1)
  181. {
  182. case 0:
  183. token_type = TokenType.Event; goto ret;
  184. case 1:
  185. token_type = TokenType.Property; goto ret;
  186. default:
  187. goto exit;
  188. }
  189. case CodedIndex.MethodDefOrRef:
  190. rid = data >> 1;
  191. switch (data & 1)
  192. {
  193. case 0:
  194. token_type = TokenType.Method; goto ret;
  195. case 1:
  196. token_type = TokenType.MemberRef; goto ret;
  197. default:
  198. goto exit;
  199. }
  200. case CodedIndex.MemberForwarded:
  201. rid = data >> 1;
  202. switch (data & 1)
  203. {
  204. case 0:
  205. token_type = TokenType.Field; goto ret;
  206. case 1:
  207. token_type = TokenType.Method; goto ret;
  208. default:
  209. goto exit;
  210. }
  211. case CodedIndex.Implementation:
  212. rid = data >> 2;
  213. switch (data & 3)
  214. {
  215. case 0:
  216. token_type = TokenType.File; goto ret;
  217. case 1:
  218. token_type = TokenType.AssemblyRef; goto ret;
  219. case 2:
  220. token_type = TokenType.ExportedType; goto ret;
  221. default:
  222. goto exit;
  223. }
  224. case CodedIndex.CustomAttributeType:
  225. rid = data >> 3;
  226. switch (data & 7)
  227. {
  228. case 2:
  229. token_type = TokenType.Method; goto ret;
  230. case 3:
  231. token_type = TokenType.MemberRef; goto ret;
  232. default:
  233. goto exit;
  234. }
  235. case CodedIndex.ResolutionScope:
  236. rid = data >> 2;
  237. switch (data & 3)
  238. {
  239. case 0:
  240. token_type = TokenType.Module; goto ret;
  241. case 1:
  242. token_type = TokenType.ModuleRef; goto ret;
  243. case 2:
  244. token_type = TokenType.AssemblyRef; goto ret;
  245. case 3:
  246. token_type = TokenType.TypeRef; goto ret;
  247. default:
  248. goto exit;
  249. }
  250. case CodedIndex.TypeOrMethodDef:
  251. rid = data >> 1;
  252. switch (data & 1)
  253. {
  254. case 0:
  255. token_type = TokenType.TypeDef; goto ret;
  256. case 1:
  257. token_type = TokenType.Method; goto ret;
  258. default: goto exit;
  259. }
  260. default:
  261. goto exit;
  262. }
  263. ret:
  264. return new MetadataToken(token_type, rid);
  265. exit:
  266. return MetadataToken.Zero;
  267. }
  268. public static int GetSize(CodedIndex self, Func<Table, int> counter)
  269. {
  270. int bits;
  271. Table[] tables;
  272. switch (self)
  273. {
  274. case CodedIndex.TypeDefOrRef:
  275. bits = 2;
  276. tables = new[] { Table.TypeDef, Table.TypeRef, Table.TypeSpec };
  277. break;
  278. case CodedIndex.HasConstant:
  279. bits = 2;
  280. tables = new[] { Table.Field, Table.Param, Table.Property };
  281. break;
  282. case CodedIndex.HasCustomAttribute:
  283. bits = 5;
  284. tables = new[] {
  285. Table.Method, Table.Field, Table.TypeRef, Table.TypeDef, Table.Param, Table.InterfaceImpl, Table.MemberRef,
  286. Table.Module, Table.DeclSecurity, Table.Property, Table.Event, Table.StandAloneSig, Table.ModuleRef,
  287. Table.TypeSpec, Table.Assembly, Table.AssemblyRef, Table.File, Table.ExportedType,
  288. Table.ManifestResource, Table.GenericParam
  289. };
  290. break;
  291. case CodedIndex.HasFieldMarshal:
  292. bits = 1;
  293. tables = new[] { Table.Field, Table.Param };
  294. break;
  295. case CodedIndex.HasDeclSecurity:
  296. bits = 2;
  297. tables = new[] { Table.TypeDef, Table.Method, Table.Assembly };
  298. break;
  299. case CodedIndex.MemberRefParent:
  300. bits = 3;
  301. tables = new[] { Table.TypeDef, Table.TypeRef, Table.ModuleRef, Table.Method, Table.TypeSpec };
  302. break;
  303. case CodedIndex.HasSemantics:
  304. bits = 1;
  305. tables = new[] { Table.Event, Table.Property };
  306. break;
  307. case CodedIndex.MethodDefOrRef:
  308. bits = 1;
  309. tables = new[] { Table.Method, Table.MemberRef };
  310. break;
  311. case CodedIndex.MemberForwarded:
  312. bits = 1;
  313. tables = new[] { Table.Field, Table.Method };
  314. break;
  315. case CodedIndex.Implementation:
  316. bits = 2;
  317. tables = new[] { Table.File, Table.AssemblyRef, Table.ExportedType };
  318. break;
  319. case CodedIndex.CustomAttributeType:
  320. bits = 3;
  321. tables = new[] { Table.Method, Table.MemberRef };
  322. break;
  323. case CodedIndex.ResolutionScope:
  324. bits = 2;
  325. tables = new[] { Table.Module, Table.ModuleRef, Table.AssemblyRef, Table.TypeRef };
  326. break;
  327. case CodedIndex.TypeOrMethodDef:
  328. bits = 1;
  329. tables = new[] { Table.TypeDef, Table.Method };
  330. break;
  331. default:
  332. throw new ArgumentException();
  333. }
  334. int max = 0;
  335. for (int i = 0; i < tables.Length; i++)
  336. {
  337. max = System.Math.Max(counter(tables[i]), max);
  338. }
  339. return max < (1 << (16 - bits)) ? 2 : 4;
  340. }
  341. }
  342. }