1 //-----------------------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 //-----------------------------------------------------------------------------
5 namespace System.Runtime.Serialization
8 using System.Reflection;
9 using System.Reflection.Emit;
10 using System.Security;
12 #if !NO_DYNAMIC_CODEGEN
13 [Fx.Tag.SecurityNote(Miscellaneous = "RequiresReview (Critical) - works on CodeGenerator objects, which require Critical access.")]
14 class BitFlagsGenerator
18 LocalBuilder[] locals;
20 public BitFlagsGenerator(int bitCount, CodeGenerator ilg, string localName)
23 this.bitCount = bitCount;
24 int localCount = (bitCount + 7) / 8;
25 locals = new LocalBuilder[localCount];
26 for (int i = 0; i < locals.Length; i++)
28 locals[i] = ilg.DeclareLocal(typeof(byte), localName + i, (byte) 0);
32 public static bool IsBitSet(byte[] bytes, int bitIndex)
34 int byteIndex = GetByteIndex(bitIndex);
35 byte bitValue = GetBitValue(bitIndex);
36 return (bytes[byteIndex] & bitValue) == bitValue;
39 public static void SetBit(byte[] bytes, int bitIndex)
41 int byteIndex = GetByteIndex(bitIndex);
42 byte bitValue = GetBitValue(bitIndex);
43 bytes[byteIndex] |= bitValue;
46 public int GetBitCount()
51 public LocalBuilder GetLocal(int i)
56 public int GetLocalCount()
61 public void Load(int bitIndex)
63 LocalBuilder local = locals[GetByteIndex(bitIndex)];
64 byte bitValue = GetBitValue(bitIndex);
72 public void LoadArray()
74 LocalBuilder localArray = ilg.DeclareLocal(Globals.TypeOfByteArray, "localArray");
75 ilg.NewArray(typeof(byte), locals.Length);
76 ilg.Store(localArray);
77 for (int i = 0; i < locals.Length; i++)
79 ilg.StoreArrayElement(localArray, i, locals[i]);
84 public void Store(int bitIndex, bool value)
86 LocalBuilder local = locals[GetByteIndex(bitIndex)];
87 byte bitValue = GetBitValue(bitIndex);
105 static byte GetBitValue(int bitIndex)
107 return (byte)(1 << (bitIndex & 7));
110 static int GetByteIndex(int bitIndex)
112 return bitIndex >> 3;