context: //
  // Utilities.cs
  //
  // Author:
  //   Jb Evain (jbevain@gmail.com)
  //
  // Generated by /CodeGen/cecil-gen.rb do not edit
  // Sat Feb 16 23:23:29 +0100 2008
  //
  // (C) 2005 Jb Evain
  //
  // Permission is hereby granted, free of charge, to any person obtaining
  // a copy of this software and associated documentation files (the
  // "Software"), to deal in the Software without restriction, including
  // without limitation the rights to use, copy, modify, merge, publish,
  // distribute, sublicense, and/or sell copies of the Software, and to
  // permit persons to whom the Software is furnished to do so, subject to
  // the following conditions:
  //
  // The above copyright notice and this permission notice shall be
  // included in all copies or substantial portions of the Software.
  //
  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  //
  
  namespace Mono.Cecil.Metadata {
  
  	using System;
  	using System.Collections;
  	using System.IO;
  
  	sealed class Utilities {
  
  		Utilities ()
  		{
  		}
  
  		public static int ReadCompressedInteger (byte [] data, int pos, out int start)
  		{
  			int integer;
  			start = pos;
  			if ((data [pos] & 0x80) == 0) {
  				integer = data [pos];
  				start++;
  			} else if ((data [pos] & 0x40) == 0) {
  				integer = (data [start] & ~0x80) << 8;
  				integer |= data [pos + 1];
  				start += 2;
  			} else {
  				integer = (data [start] & ~0xc0) << 24;
  				integer |= data [pos + 1] << 16;
  				integer |= data [pos + 2] << 8;
  				integer |= data [pos + 3];
  				start += 4;
  			}
  			return integer;
  		}
  
  		public static int ReadCompressedSignedInteger (byte [] data, int pos, out int start)
  		{
  			int integer = ReadCompressedInteger (data, pos, out start) >> 1;
  			if ((integer & 1) == 0)
  				return integer;
  
  			if (integer < 0x40)
  				return integer - 0x40;
  
  			if (integer < 0x2000)
  				return integer - 0x2000;
  
  			if (integer < 0x10000000)
  				return integer - 0x10000000;
  
  			return integer - 0x20000000;
  		}
  
  		public static int WriteCompressedInteger (BinaryWriter writer, int value)
  		{
  			if (value < 0x80)
  				writer.Write ((byte) value);
  			else if (value < 0x4000) {
  				writer.Write ((byte) (0x80 | (value >> 8)));
  				writer.Write ((byte) (value & 0xff));
  			} else {
  				writer.Write ((byte) ((value >> 24) | 0xc0));
  				writer.Write ((byte) ((value >> 16) & 0xff));
  				writer.Write ((byte) ((value >> 8) & 0xff));
  				writer.Write ((byte) (value & 0xff));
  			}
  			return (int) writer.BaseStream.Position;
  		}
  
  		public static MetadataToken GetMetadataToken (CodedIndex cidx, uint data)
  		{
  			uint rid = 0;
  			switch (cidx) {
  			case CodedIndex.TypeDefOrRef :
  				rid = data >> 2;
  				switch (data & 3) {
  				case 0 :
  					return new MetadataToken (TokenType.TypeDef, rid);
  				case 1 :
  					return new MetadataToken (TokenType.TypeRef, rid);
  				case 2 :
  					return new MetadataToken (TokenType.TypeSpec, rid);
  				default :
  					return MetadataToken.Zero;
  				}
  			case CodedIndex.HasConstant :
  				rid = data >> 2;
  				switch (data & 3) {
  				case 0 :
  					return new MetadataToken (TokenType.Field, rid);
  				case 1 :
  					return new MetadataToken (TokenType.Param, rid);
  				case 2 :
  					return new MetadataToken (TokenType.Property, rid);
  				default :
  					return MetadataToken.Zero;
  				}
  			case CodedIndex.HasCustomAttribute :
  				rid = data >> 5;
  				switch (data & 31) {
  				case 0 :
  					return new MetadataToken (TokenType.Method, rid);
  				case 1 :
  					return new MetadataToken (TokenType.Field, rid);
  				case 2 :
  					return new MetadataToken (TokenType.TypeRef, rid);
  				case 3 :
  					return new MetadataToken (TokenType.TypeDef, rid);
  				case 4 :
  					return new MetadataToken (TokenType.Param, rid);
  				case 5 :
  					return new MetadataToken (TokenType.InterfaceImpl, rid);
  				case 6 :
  					return new MetadataToken (TokenType.MemberRef, rid);
  				case 7 :
  					return new MetadataToken (TokenType.Module, rid);
  				case 8 :
  					return new MetadataToken (TokenType.Permission, rid);
  				case 9 :
  					return new MetadataToken (TokenType.Property, rid);
  				case 10 :
  					return new MetadataToken (TokenType.Event, rid);
  				case 11 :
  					return new MetadataToken (TokenType.Signature, rid);
  				case 12 :
  					return new MetadataToken (TokenType.ModuleRef, rid);
  				case 13 :
  					return new MetadataToken (TokenType.TypeSpec, rid);
  				case 14 :
  					return new MetadataToken (TokenType.Assembly, rid);
  				case 15 :
  					return new MetadataToken (TokenType.AssemblyRef, rid);
  				case 16 :
  					return new MetadataToken (TokenType.File, rid);
  				case 17 :
  					return new MetadataToken (TokenType.ExportedType, rid);
  				case 18 :
  					return new MetadataToken (TokenType.ManifestResource, rid);
  				case 19 :
  					return new MetadataToken (TokenType.GenericParam, rid);
  				default :
  					return MetadataToken.Zero;
  				}
  			case CodedIndex.HasFieldMarshal :
  				rid = data >> 1;
  				switch (data & 1) {
  				case 0 :
  					return new MetadataToken (TokenType.Field, rid);
  				case 1 :
  					return new MetadataToken (TokenType.Param, rid);
  				default :
  					return MetadataToken.Zero;
  				}
  			case CodedIndex.HasDeclSecurity :
  				rid = data >> 2;
  				switch (data & 3) {
  				case 0 :
  					return new MetadataToken (TokenType.TypeDef, rid);
  				case 1 :
  					return new MetadataToken (TokenType.Method, rid);
  				case 2 :
  					return new MetadataToken (TokenType.Assembly, rid);
  				default :
  					return MetadataToken.Zero;
  				}
  			case CodedIndex.MemberRefParent :
  				rid = data >> 3;
  				switch (data & 7) {
  				case 0 :
  					return new MetadataToken (TokenType.TypeDef, rid);
  				case 1 :
  					return new MetadataToken (TokenType.TypeRef, rid);
  				case 2 :
  					return new MetadataToken (TokenType.ModuleRef, rid);
  				case 3 :
  					return new MetadataToken (TokenType.Method, rid);
  				case 4 :
  					return new MetadataToken (TokenType.TypeSpec, rid);
  				default :
  					return MetadataToken.Zero;
  				}
  			case CodedIndex.HasSemantics :
  				rid = data >> 1;
  				switch (data & 1) {
  				case 0 :
  					return new MetadataToken (TokenType.Event, rid);
  				case 1 :
  					return new MetadataToken (TokenType.Property, rid);
  				default :
  					return MetadataToken.Zero;
  				}
  			case CodedIndex.MethodDefOrRef :
  				rid = data >> 1;
  				switch (data & 1) {
  				case 0 :
  					return new MetadataToken (TokenType.Method, rid);
  				case 1 :
  					return new MetadataToken (TokenType.MemberRef, rid);
  				default :
  					return MetadataToken.Zero;
  				}
  			case CodedIndex.MemberForwarded :
  
gt: 				rid = data >> 1;