Flylib.com
Secure Programming Cookbook for C and C++: Recipes for Cryptography, Authentication, Input Validation & More
Secure Programming Cookbook for C and C++: Recipes for Cryptography, Authentication, Input Validation & More
ISBN: 0596003943
EAN: 2147483647
Year: 2004
Pages: 424
Authors:
John Viega
,
Matt Messier
BUY ON AMAZON
C Cookbook, 2nd Edition
Table of Contents
Copyright
Dedication
Preface
Who This Book Is For
What You Need to Use This Book
Platform Notes
How This Book Is Organized
What Was Left Out
Conventions Used in This Book
About the Code
Using Code Examples
Comments and Questions
Safari Enabled
Acknowledgments
Chapter 1. Numbers and Enumerations
Introduction
Recipe1.1.Determining Approximate Equality Between a Fraction and Floating-Point Value
Recipe1.2.Converting Degrees to Radians
Recipe1.3.Converting Radians to Degrees
Recipe1.4.Using the Bitwise Complement Operator with Various Data Types
Recipe1.5.Testing for an Even or Odd Value
Recipe1.6.Obtaining the High Word or Low Word of a Number
Recipe1.7.Converting a Number in Another Base to Base10
Recipe1.8.Determining Whether a String Is a Valid Number
Recipe1.9.Rounding a Floating-Point Value
Recipe1.10.Choosing a Rounding Algorithm
Recipe1.11.Converting Celsius to Fahrenheit
Recipe1.12.Converting Fahrenheit to Celsius
Recipe1.13.Safely Performing a Narrowing Numeric Cast
Recipe1.14.Finding the Length of Any Three Sides of a Right Triangle
Recipe1.15.Finding the Angles of a Right Triangle
Recipe1.16.Displaying an Enumeration Value as a String
Recipe1.17.Converting Plain Text to an Equivalent Enumeration Value
Recipe1.18.Testing for a Valid Enumeration Value
Recipe1.19.Testing for a Valid Enumeration of Flags
Recipe1.20.Using Enumerated Members in a Bit Mask
Recipe1.21.Determining if One or More Enumeration Flags Are Set
Recipe1.22.Determining the Integral Part of a Decimal or Double
Chapter 2. Strings and Characters
Introduction
Recipe2.1.Determining the Kind of Character a char Contains
Recipe2.2.Determining Whether a Character Is Within a Specified Range
Recipe2.3.Controlling Case Sensitivity When Comparing Two Characters
Recipe2.4.Finding All Occurrences of a Character Within a String
Recipe2.5.Finding the Location of All Occurrences of a String Within Another String
Recipe2.6.Implementing a Poor Man s Tokenizer to Deconstruct a String
Recipe2.7.Controlling Case Sensitivity When Comparing Two Strings
Recipe2.8.Comparing a String to the Beginning or End of a Second String
Recipe2.9.Inserting Text into a String
Recipe2.10.Removing or Replacing Characters Within a String
Recipe2.11.Encoding Binary Data as Base64
Recipe2.12.Decoding a Base64-Encoded Binary
Recipe2.13.Converting a String Returned as a Byte Back into a String
Recipe2.14.Passing a String to a Method That Accepts only a Byte
Recipe2.15.Converting Strings to Other Types
Recipe2.16.Formatting Data in Strings
Recipe2.17.Creating a Delimited String
Recipe2.18.Extracting Items from a Delimited String
Recipe2.19.Setting the Maximum Number of Characters a StringBuilder Can Contain
Recipe2.20.Iterating over Each Character in a String
Recipe2.21.Improving String Comparison Performance
Recipe2.22.Improving StringBuilder Performance
Recipe2.23.Pruning Characters from the Head andor Tail of a String
Recipe2.24.Testing a String for Null or Empty
Recipe2.25.Appending a Line
Recipe2.26.Encoding Chunks of Data
Chapter 3. Classes and Structures
Introduction
Recipe3.1.Creating Union-Type Structures
Recipe3.2.Allowing a Type to Represent Itself as a String
Recipe3.3.Converting a String Representation of an Object into an Actual Object
Recipe3.4.Implementing Polymorphism with Abstract Base Classes
Recipe3.5.Making a Type Sortable
Recipe3.6.Making a Type Searchable
Recipe3.7.Indirectly Overloading the , -, , and Operators
Recipe3.8.Indirectly Overloading the , and ?: Operators
Recipe3.9.Turning Bits On or Off
Recipe3.10.Making Error-Free Expressions
Recipe3.11.Minimizing (Reducing) Your Boolean Logic
Recipe3.12.Converting Between Simple Types in a Language-Agnostic Manner
Recipe3.13.Determining When to Use the Cast Operator, the as Operator, or the is Operator
Recipe3.14.Casting with the as Operator
Recipe3.15.Determining a Variable s Type with the is Operator
Recipe3.16.Implementing Polymorphism with Interfaces
Recipe3.17.Calling the Same Method on Multiple Object Types
Recipe3.18.Adding a Notification Callback Using an Interface
Recipe3.19.Using Multiple Entry Points to Version an Application
Recipe3.20.Preventing the Creation of an Only Partially Initialized Object
Recipe3.21.Returning Multiple Items from a Method
Recipe3.22.Parsing Command-Line Parameters
Recipe3.23.Retrofitting a Class to Interoperate with COM
Recipe3.24.Initializing a Constant Field at Runtime
Recipe3.25.Writing Code That Is Compatible with the Widest Range of Managed Languages
Recipe3.26.Building Cloneable Classes
Recipe3.27.Assuring an Object s Disposal
Recipe3.28.Releasing a COM Object Through Managed Code
Recipe3.29.Creating an Object Cache
Recipe3.30.Rolling Back Object Changes
Recipe3.31.Disposing of Unmanaged Resources
Recipe3.32.Determining Where Boxing and Unboxing Occur
Chapter 4. Generics
Introduction
Recipe4.1.Deciding When and Where to Use Generics
Recipe4.2.Understanding Generic Types
Recipe4.3.Getting the Type of a Generic Type
Recipe4.4.Replacing the ArrayList with Its Generic Counterpart
Recipe4.5.Replacing the Stack and Queue with Their Generic Counterparts
Recipe4.6.Implementing a Linked List
Recipe4.7.Creating a Value Type That Can Be Initialized to Null
Recipe4.8.Reversing the Contents of a Sorted List
Recipe4.9.Making Read-Only Collections the Generic Way
Recipe4.10.Replacing the Hashtable with Its Generic Counterpart
Recipe4.11.Using foreach with Generic Dictionary Types
Recipe4.12.Constraining Type Arguments
Recipe4.13.Initializing Generic Variables to Their Default Values
Chapter 5. Collections
Introduction
Recipe5.1.Swapping Two Elements in an Array
Recipe5.2.Reversing an Array Quickly
Recipe5.3.Reversing a Two-Dimensional Array
Recipe5.4.Reversing a Jagged Array
Recipe5.5.Writing a More Flexible StackTrace Class
Recipe5.6.Determining the Number of Times an Item Appears in a ListT
Recipe5.7.Retrieving All Instances of a Specific Item in a ListT
Recipe5.8.Inserting and Removing Items from an Array
Recipe5.9.Keeping Your ListT Sorted
Recipe5.10.Sorting a Dictionary s Keys andor Values
Recipe5.11.Creating a Dictionary with Max and Min Value Boundaries
Recipe5.12.Displaying an Array s Data as a Delimited String
Recipe5.13.Storing Snapshots of Lists in an Array
Recipe5.14.Persisting a Collection Between Application Sessions
Recipe5.15.Testing Every Element in an Array or ListT
Recipe5.16.Performing an Action on Each Element in an Array or ListT
Recipe5.17.Creating a Read-Only Array or ListT
Chapter 6. Iterators and Partial Types
Introduction
Recipe6.1.Implementing Nested foreach Functionality in a Class
Recipe6.2.Creating Custom Enumerators
Recipe6.3.Creating an Iterator on a Generic Type
Recipe6.4.Creating an Iterator on a Non-generic Type
Recipe6.5.Creating Iterators That Accept Parameters
Recipe6.6.Adding Multiple Iterators on a Single Type
Recipe6.7.Implementing Iterators as Overloaded Operators
Recipe6.8.Forcing an Iterator to Stop Iterating
Recipe6.9.Dealing with Finally Blocks and Iterators
Recipe6.10.Organizing Your Interface Implementations
Recipe6.11.Generating Code That Is No Longer in Your Main Code Paths
Chapter 7. Exception Handling
Introduction
Recipe7.1.Verifying Critical Parameters
Recipe7.2.Knowing When to Catch and Rethrow Exceptions
Recipe7.3.Identifying Exceptions and Their Usage
Recipe7.4.Handling Derived Exceptions Individually
Recipe7.5.Assuring Exceptions Are Not Lost When Using Finally Blocks
Recipe7.6.Handling Exceptions Thrown from Methods Invoked via Reflection
Recipe7.7.Debugging Problems When Loading an Assembly
Recipe7.8.Mapping Back and Forth Between Managed Exceptions and HRESULTs
Recipe7.9.Handling User-Defined HRESULTs
Recipe7.10.Preventing Unhandled Exceptions
Recipe7.11.Getting Exception Information
Recipe7.12.Getting to the Root of a Problem Quickly
Recipe7.13.Creating a New Exception Type
Recipe7.14.Obtaining a Stack Trace
Recipe7.15.Breaking on a First-Chance Exception
Recipe7.16.Preventing the Nefarious TypeInitializationException
Recipe7.17.Handling Exceptions Thrown from an Asynchronous Delegate
Recipe7.18.Giving Exceptions the Extra Info They Need with Exception.Data
Recipe7.19.Looking at Exceptions in a New Way Using Visualizers
Recipe7.20.Dealing with Unhandled Exceptions in WinForms Applications
Chapter 8. Diagnostics
Introduction
Recipe8.1.Controlling Tracing Output in Production Code
Recipe8.2.Providing Fine-Grained Control over DebuggingTracing Output
Recipe8.3.Creating Your Own Custom Switch Class
Recipe8.4.Compiling Blocks of Code Conditionally
Recipe8.5.Determining Whether a Process Has Stopped Responding
Recipe8.6.Using Event Logs in Your Application
Recipe8.7.Changing the Maximum Size of a Custom Event Log
Recipe8.8.Searching Event Log Entries
Recipe8.9.Watching the Event Log for a Specific Entry
Recipe8.10.Finding All Sources Belonging to a Specific Event Log
Recipe8.11.Implementing a Simple Performance Counter
Recipe8.12.Implementing Performance Counters That Require a Base Counter
Recipe8.13.Enabling and Disabling Complex Tracing Code
Recipe8.14.Capturing Standard Output for a Process
Recipe8.15.Creating Custom Debugging Displays for Your Classes
Recipe8.16.Determining Current appdomain Settings Information
Recipe8.17.Boosting the Priority of a Process Programmatically
Recipe8.18.Looking at Your Runtime Environment and Seeing What You Can Do About It
Chapter 9. Delegates, Events, and Anonymous Methods
Introduction
Recipe9.1.Controlling When and If a Delegate Fires Within a Multicast Delegate
Recipe9.2.Obtaining Return Values from Each Delegate in a Multicast Delegate
Recipe9.3.Handling Exceptions Individually for Each Delegate in a Multicast Delegate
Recipe9.4.Converting Delegate Invocation from Synchronous to Asynchronous
Recipe9.5.Wrapping Sealed Classes to Add Events
Recipe9.6.Passing Specialized Parameters to and from an Event
Recipe9.7.An Advanced Interface Search Mechanism
Recipe9.8.An Advanced Member Search Mechanism
Recipe9.9.Observing Additions and Modifications to a Hashtable
Recipe9.10.Using the Windows Keyboard Hook
Recipe9.11.Tracking and Responding to the Mouse
Recipe9.12.Using Anonymous Methods
Recipe9.13.Set up Event Handlers Without the Mess
Recipe9.14.Using Different Parameter Modifiers in Anonymous Methods
Recipe9.15.Using Closures in C
Recipe9.16.Performing Multiple Operations on a List Using Functors
Chapter 10. Regular Expressions
Introduction
Recipe10.1.Enumerating Matches
Recipe10.2.Extracting Groups from a MatchCollection
Recipe10.3.Verifying the Syntax of a Regular Expression
Recipe10.4.Quickly Finding Only the Last Match in a String
Recipe10.5.Replacing Characters or Words in a String
Recipe10.6.Augmenting the Basic String Replacement Function
Recipe10.7.Implementing a Better Tokenizer
Recipe10.8.Compiling Regular Expressions
Recipe10.9.Counting Lines of Text
Recipe10.10.Returning the Entire Line in Which a Match Is Found
Recipe10.11.Finding a Particular Occurrence of a Match
Recipe10.12.Using Common Patterns
Recipe10.13.Documenting Your Regular Expressions
Recipe10.14.Using Built-in Regular Expressions to Parse ASP. NET Pages
Chapter 11. Data Structures and Algorithms
Introduction
Recipe11.1.Creating a Hash Code for a Data Type
Recipe11.2.Creating a Priority Queue
Recipe11.3.Creating a Double Queue
Recipe11.4.Determining Where Characters or Strings Do Not Balance
Recipe11.5.Creating a One-to-Many Map (MultiMap)
Recipe11.6.Creating a Binary Tree
Recipe11.7.Creating an n-ary Tree
Recipe11.8.Creating a Set Object
Chapter 12. Filesystem IO
Introduction
Recipe12.1.Creating, Copying, Moving, or Deleting a File
Recipe12.2.Manipulating File Attributes
Recipe12.3.Renaming a File
Recipe12.4.Determining Whether a File Exists
Recipe12.5.Choosing a Method of Opening a File or Stream for Reading andor Writing
Recipe12.6.Accessing Part of a File Randomly
Recipe12.7.Outputting a Platform-Independent EOL Character
Recipe12.8.Creating, Writing to, and Reading from a File
Recipe12.9.Determining Whether a Directory Exists
Recipe12.10.Creating, Copying, Moving, or Deleting a Directory
Recipe12.11.Manipulating Directory Attributes
Recipe12.12.Renaming a Directory
Recipe12.13.Searching for Directories or Files Using Wildcards
Recipe12.14.Obtaining the Directory Tree
Recipe12.15.Parsing a Path
Recipe12.16.Parsing Paths in Environment Variables
Recipe12.17.Verifying a Path
Recipe12.18.Using a Temporary File in Your Application
Recipe12.19.Opening a File Stream with Just a File Handle
Recipe12.20.Writing to Multiple Output Files at One Time
Recipe12.21.Launching and Interacting with Console Utilities
Recipe12.22.Locking Subsections of a File
Recipe12.23.Watching the Filesystem for Specific Changes to One or More Files or Directories
Recipe12.24.Waiting for an Action to Occur in the Filesystem
Recipe12.25.Comparing Version Information of Two Executable Modules
Recipe12.26.Querying Information for All Drives on a System
Recipe12.27.EncryptingDecrypting an Existing File
Recipe12.28.Compressing and Decompressing Your Files
Chapter 13. Reflection
Introduction
Recipe13.1.Listing Referenced Assemblies
Recipe13.2.Listing Exported Types
Recipe13.3.Finding Overridden Methods
Recipe13.4.Finding Members in an Assembly
Recipe13.5.Finding Members Within an Interface
Recipe13.6.Determining and Obtaining Nested Types Within an Assembly
Recipe13.7.Displaying the Inheritance Hierarchy for a Type
Recipe13.8.Finding the Subclasses of a Type
Recipe13.9.Finding All Serializable Types Within an Assembly
Recipe13.10.Filtering Output When Obtaining Members
Recipe13.11.Dynamically Invoking Members
Recipe13.12.Providing Guidance to Obfuscators
Recipe13.13.Determining if a Type or Method Is Generic
Recipe13.14.Reading Manifest Resources Programmatically
Recipe13.15.Accessing Local Variable Information
Recipe13.16.Creating a Generic Type
Chapter 14. Web
Introduction
Recipe14.1.Converting an IP Address to a Hostname
Recipe14.2.Converting a Hostname to an IP Address
Recipe14.3.Parsing a URI
Recipe14.4.Forming and Validating an Absolute Uri
Recipe14.5.Handling Web Server Errors
Recipe14.6.Communicating with a Web Server
Recipe14.7.Going Through a Proxy
Recipe14.8.Obtaining the HTML from a URL
Recipe14.9.Using the New Web Browser Control
Recipe14.10.Tying Database Tables to the Cache
Recipe14.11.Caching Data with Multiple Dependencies
Recipe14.12.Prebuilding an ASP.NET Web Site Programmatically
Recipe14.13.Escaping and Unescaping Data for the Web
Recipe14.14.Using the UriBuilder Class
Recipe14.15.Inspect and Change Your Web Application Configuration
Recipe14.16.Working with HTML
Recipe14.17.Using Cached Results When Working with HTTP for Faster Performance
Recipe14.18.Checking out a Web Server s Custom Error Pages
Recipe14.19.Determining the Application Mappings for ASP.NET Set Up on IIS
Chapter 15. XML
Introduction
Recipe15.1.Reading and Accessing XML Data in Document Order
Recipe15.2.Reading XML on the Web
Recipe15.3.Querying the Contents of an XML Document
Recipe15.4.Validating XML
Recipe15.5.Creating an XML Document Programmatically
Recipe15.6.Detecting Changes to an XML Document
Recipe15.7.Handling Invalid Characters in an XML String
Recipe15.8.Transforming XML
Recipe15.9.Tearing Apart an XML Document
Recipe15.10.Putting Together an XML Document
Recipe15.11.Validating Modified XML Documents Without Reloading
Recipe15.12.Extending XSLT Transformations
Recipe15.13.Getting Your Schema in Bulk from Existing XML Files
Recipe15.14.Passing Parameters to XSLT Transformations
Chapter 16. Networking
Introduction
Recipe16.1.Writing a TCP Server
Recipe16.2.Writing a TCP Client
Recipe16.3.Simulating Form Execution
Recipe16.4.Downloading Data from a Server
Recipe16.5.Using Named Pipes to Communicate
Recipe16.6.Pinging Programmatically
Recipe16.7.Send SMTP Mail Using the SMTP Service
Recipe16.8.Check out Your Network Connectivity
Recipe16.9.Use Sockets to Scan the Ports on a Machine
Recipe16.10.Use the Current Internet Connection Settings
Recipe16.11.Download a File Using FTP
Chapter 17. Security
Introduction
Recipe17.1.Controlling Access to Types in a Local Assembly
Recipe17.2.EncryptingDecrypting a String
Recipe17.3.Encrypting and Decrypting a File
Recipe17.4.Cleaning up Cryptography Information
Recipe17.5.Verifying that a String Remains Uncorrupted Following Transmission
Recipe17.6.Wrapping a String Hash for Ease of Use
Recipe17.7.A Better Random Number Generator
Recipe17.8.Storing Data Securely
Recipe17.9.Making a Security Assert Safe
Recipe17.10.Preventing Malicious Modifications to an Assembly
Recipe17.11.Verifying That an Assembly Has Been Granted Specific Permissions
Recipe17.12.Minimizing the Attack Surface of an Assembly
Recipe17.13.Obtaining SecurityAudit Information
Recipe17.14.GrantingRevoking Access to a File or Registry Key
Recipe17.15.Protecting String Data with Secure Strings
Recipe17.16.Securing Stream Data
Recipe17.17.Encrypting web.config Information
Recipe17.18.Obtaining the Full Reason a SecurityException Was Thrown
Recipe17.19.Achieving Secure Unicode Encoding
Recipe17.20.Obtaining a Safer File Handle
Chapter 18. Threading and Synchronization
Introduction
Recipe18.1.Creating Per-Thread Static Fields
Recipe18.2.Providing Thread-Safe Access to Class Members
Recipe18.3.Preventing Silent Thread Termination
Recipe18.4.Polling an Asynchronous Delegate
Recipe18.5.Timing out an Asynchronous Delegate
Recipe18.6.Being Notified of the Completion of an Asynchronous Delegate
Recipe18.7.Determining Whether a Request for a Pooled Thread Will Be Queued
Recipe18.8.Configuring a Timer
Recipe18.9.Storing Thread-Specific Data Privately
Recipe18.10.Granting Multiple Access to Resources with a Semaphore
Recipe18.11.Synchronizing Multiple Processes with the Mutex
Recipe18.12.Using Events to Make Threads Cooperate
Recipe18.13.Get the Naming Rights for Your Events
Recipe18.14.Performing Atomic Operations Among Threads
Chapter 19. Unsafe Code
Introduction
Recipe19.1.Controlling Changes to Pointers Passed to Methods
Recipe19.2.Comparing Pointers
Recipe19.3.Navigating Arrays
Recipe19.4.Manipulating a Pointer to a Fixed Array
Recipe19.5.Returning a Pointer to a Particular Element in an Array
Recipe19.6.Creating and Using an Array of Pointers
Recipe19.7.Switching Unknown Pointer Types
Recipe19.8.Converting a String to a char
Recipe19.9.Declaring a Fixed-Size Structure with an Embedded Array
Chapter 20. Toolbox
Introduction
Recipe20.1.Dealing with Operating System Shutdown, Power Management, or User Session Changes
Recipe20.2.Controlling a Service
Recipe20.3.List What Processes an Assembly Is Loaded In
Recipe20.4.Using Message Queues on a Local Workstation
Recipe20.5.Finding the Path to the Current Framework Version
Recipe20.6.Determining the Versions of an Assembly That Are Registered in the Global Assembly Cache (GAC)
Recipe20.7.Getting the Windows Directory
Recipe20.8.Capturing Output from the Standard Output Stream
Recipe20.9.Running Code in Its Own appdomain
Recipe20.10.Determining the Operating System and Service Pack Version of the Current Operating System
About the Authors
Colophon
Index
SYMBOL
A
B
C
D
E
F
G
H
I
J
K
L
M
N
O
P
Q
R
S
T
U
V
W
X
Y
Secure Programming Cookbook for C and C++: Recipes for Cryptography, Authentication, Input Validation & More
ISBN: 0596003943
EAN: 2147483647
Year: 2004
Pages: 424
Authors:
John Viega
,
Matt Messier
BUY ON AMAZON
ADO.NET 3.5 Cookbook (Cookbooks (OReilly))
Synchronizing a DataSet with an XML Document
Formatting Column Values When Outputting Data as XML
Performing Batch Updates with a DataAdapter
Counting Records
A.2. Classes
Agile Project Management: Creating Innovative Products (2nd Edition)
Practice: Product Vision Box and Elevator Test Statement
Phase: Speculate
Practice: Participatory Decision Making
A Scaled Adaptive Framework
Implementing the Vision
The Complete Cisco VPN Configuration Guide
Authentication Methods
L2TP
Concentrator Tools
Router Site-to-Site Connections
Issues with Site-to-Site Connections
Postfix: The Definitive Guide
Email and the Internet
Email Topics
The Nature of Spam
Content-Checking
SASL Authentication
Visual Studio Tools for Office(c) Using C# with Excel, Word, Outlook, and InfoPath
Conclusion
Events in the Excel Object Model
Introduction
Adding Windows Forms Controls to Your Document
Introduction to XML Schema Creation in Visual Studio
.NET System Management Services
Using the System.Management Namespace
Querying WMI
Handling WMI Events
Instrumenting .NET Applications with WMI
WMI Providers
flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net
Privacy policy
This website uses cookies. Click
here
to find out more.
Accept cookies