Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / cdf / src / WCF / Serialization / System / Runtime / Serialization / BitFlagsGenerator.cs / 1305376 / BitFlagsGenerator.cs
//------------------------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- namespace System.Runtime.Serialization { using System; using System.Reflection; using System.Reflection.Emit; using System.Security; [Fx.Tag.SecurityNote(Miscellaneous = "RequiresReview (Critical) - works on CodeGenerator objects, which require Critical access.")] class BitFlagsGenerator { int bitCount; CodeGenerator ilg; LocalBuilder[] locals; public BitFlagsGenerator(int bitCount, CodeGenerator ilg, string localName) { this.ilg = ilg; this.bitCount = bitCount; int localCount = (bitCount + 7) / 8; locals = new LocalBuilder[localCount]; for (int i = 0; i < locals.Length; i++) { locals[i] = ilg.DeclareLocal(typeof(byte), localName + i, (byte) 0); } } public static bool IsBitSet(byte[] bytes, int bitIndex) { int byteIndex = GetByteIndex(bitIndex); byte bitValue = GetBitValue(bitIndex); return (bytes[byteIndex] & bitValue) == bitValue; } public static void SetBit(byte[] bytes, int bitIndex) { int byteIndex = GetByteIndex(bitIndex); byte bitValue = GetBitValue(bitIndex); bytes[byteIndex] |= bitValue; } public int GetBitCount() { return bitCount; } public LocalBuilder GetLocal(int i) { return locals[i]; } public int GetLocalCount() { return locals.Length; } public void Load(int bitIndex) { LocalBuilder local = locals[GetByteIndex(bitIndex)]; byte bitValue = GetBitValue(bitIndex); ilg.Load(local); ilg.Load(bitValue); ilg.And(); ilg.Load(bitValue); ilg.Ceq(); } public void LoadArray() { LocalBuilder localArray = ilg.DeclareLocal(Globals.TypeOfByteArray, "localArray"); ilg.NewArray(typeof(byte), locals.Length); ilg.Store(localArray); for (int i = 0; i < locals.Length; i++) { ilg.StoreArrayElement(localArray, i, locals[i]); } ilg.Load(localArray); } public void Store(int bitIndex, bool value) { LocalBuilder local = locals[GetByteIndex(bitIndex)]; byte bitValue = GetBitValue(bitIndex); if (value) { ilg.Load(local); ilg.Load(bitValue); ilg.Or(); ilg.Stloc(local); } else { ilg.Load(local); ilg.Load(bitValue); ilg.Not(); ilg.And(); ilg.Stloc(local); } } static byte GetBitValue(int bitIndex) { return (byte)(1 << (bitIndex & 7)); } static int GetByteIndex(int bitIndex) { return bitIndex >> 3; } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. //------------------------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- namespace System.Runtime.Serialization { using System; using System.Reflection; using System.Reflection.Emit; using System.Security; [Fx.Tag.SecurityNote(Miscellaneous = "RequiresReview (Critical) - works on CodeGenerator objects, which require Critical access.")] class BitFlagsGenerator { int bitCount; CodeGenerator ilg; LocalBuilder[] locals; public BitFlagsGenerator(int bitCount, CodeGenerator ilg, string localName) { this.ilg = ilg; this.bitCount = bitCount; int localCount = (bitCount + 7) / 8; locals = new LocalBuilder[localCount]; for (int i = 0; i < locals.Length; i++) { locals[i] = ilg.DeclareLocal(typeof(byte), localName + i, (byte) 0); } } public static bool IsBitSet(byte[] bytes, int bitIndex) { int byteIndex = GetByteIndex(bitIndex); byte bitValue = GetBitValue(bitIndex); return (bytes[byteIndex] & bitValue) == bitValue; } public static void SetBit(byte[] bytes, int bitIndex) { int byteIndex = GetByteIndex(bitIndex); byte bitValue = GetBitValue(bitIndex); bytes[byteIndex] |= bitValue; } public int GetBitCount() { return bitCount; } public LocalBuilder GetLocal(int i) { return locals[i]; } public int GetLocalCount() { return locals.Length; } public void Load(int bitIndex) { LocalBuilder local = locals[GetByteIndex(bitIndex)]; byte bitValue = GetBitValue(bitIndex); ilg.Load(local); ilg.Load(bitValue); ilg.And(); ilg.Load(bitValue); ilg.Ceq(); } public void LoadArray() { LocalBuilder localArray = ilg.DeclareLocal(Globals.TypeOfByteArray, "localArray"); ilg.NewArray(typeof(byte), locals.Length); ilg.Store(localArray); for (int i = 0; i < locals.Length; i++) { ilg.StoreArrayElement(localArray, i, locals[i]); } ilg.Load(localArray); } public void Store(int bitIndex, bool value) { LocalBuilder local = locals[GetByteIndex(bitIndex)]; byte bitValue = GetBitValue(bitIndex); if (value) { ilg.Load(local); ilg.Load(bitValue); ilg.Or(); ilg.Stloc(local); } else { ilg.Load(local); ilg.Load(bitValue); ilg.Not(); ilg.And(); ilg.Stloc(local); } } static byte GetBitValue(int bitIndex) { return (byte)(1 << (bitIndex & 7)); } static int GetByteIndex(int bitIndex) { return bitIndex >> 3; } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- TypeEnumerableViewSchema.cs
- ServicesSection.cs
- SHA256.cs
- SqlDataSourceFilteringEventArgs.cs
- FrameworkTextComposition.cs
- GenericIdentity.cs
- SerializationHelper.cs
- ExtractorMetadata.cs
- SimpleWebHandlerParser.cs
- Rect3D.cs
- ColorContextHelper.cs
- DataColumn.cs
- DNS.cs
- EventLog.cs
- XmlSchemaParticle.cs
- Stackframe.cs
- SkewTransform.cs
- BitmapEncoder.cs
- COM2EnumConverter.cs
- WebPartConnectionsConnectVerb.cs
- BitmapEffectDrawingContextState.cs
- TreeView.cs
- CompilationUtil.cs
- WorkflowInstanceAbortedRecord.cs
- ServerIdentity.cs
- SendingRequestEventArgs.cs
- Activity.cs
- RuleSet.cs
- ButtonStandardAdapter.cs
- securestring.cs
- TextRange.cs
- HMACMD5.cs
- LiteralControl.cs
- UpdatePanel.cs
- SharedPerformanceCounter.cs
- FunctionCommandText.cs
- SizeFConverter.cs
- InternalControlCollection.cs
- DataServiceHostFactory.cs
- ToolStripPanelSelectionGlyph.cs
- Knowncolors.cs
- TextParagraph.cs
- KeyGestureConverter.cs
- SqlProfileProvider.cs
- RegularExpressionValidator.cs
- WindowsContainer.cs
- EntityContainerEmitter.cs
- ObjectListShowCommandsEventArgs.cs
- HitTestResult.cs
- WindowsGraphics2.cs
- FacetEnabledSchemaElement.cs
- SimpleBitVector32.cs
- ServiceProviders.cs
- PeerNameRecordCollection.cs
- Utils.cs
- SocketException.cs
- ModelItemCollection.cs
- UIElementParagraph.cs
- DataGridViewCellValueEventArgs.cs
- NavigateEvent.cs
- GlyphElement.cs
- WeakHashtable.cs
- PrintDialogException.cs
- AppDomain.cs
- ReadOnlyDataSourceView.cs
- AutomationElementIdentifiers.cs
- DependencyPropertyChangedEventArgs.cs
- DesignerVerbCollection.cs
- CustomCategoryAttribute.cs
- KeyValueInternalCollection.cs
- ObfuscateAssemblyAttribute.cs
- UriScheme.cs
- HttpRequestTraceRecord.cs
- BitmapDecoder.cs
- RegexFCD.cs
- HttpListenerPrefixCollection.cs
- AssociatedControlConverter.cs
- KeyGesture.cs
- Registry.cs
- FieldDescriptor.cs
- Atom10FeedFormatter.cs
- ThaiBuddhistCalendar.cs
- RuleRef.cs
- XmlSchemaValidationException.cs
- WindowsListViewGroupHelper.cs
- FrameworkReadOnlyPropertyMetadata.cs
- StrokeNodeOperations.cs
- VScrollProperties.cs
- LightweightCodeGenerator.cs
- Compiler.cs
- PathHelper.cs
- StringUtil.cs
- ToolStripPanelRow.cs
- AnonymousIdentificationModule.cs
- SafeUserTokenHandle.cs
- FixedBufferAttribute.cs
- HyperLinkColumn.cs
- OleDbCommandBuilder.cs
- OuterGlowBitmapEffect.cs
- AspNetRouteServiceHttpHandler.cs