Click here to Skip to main content
15,897,518 members
Articles / Programming Languages / C#

Accessing alternative data-streams of files on an NTFS volume

Rate me:
Please Sign up or sign in to vote.
4.85/5 (53 votes)
15 Aug 2016CPOL4 min read 504.4K   5.4K   132  
A pair of classes to encapsulate access to NTFS alternative data streams.
/*
  * Trinet.Core.IO.Ntfs - Utilities for working with alternate data streams on NTFS file systems.
  * Copyright (C) 2002-2010 Richard Deeming
  * 
  * This code is free software: you can redistribute it and/or modify it under the terms of either
  * - the Code Project Open License (CPOL) version 1 or later; or
  * - the GNU General Public License as published by the Free Software Foundation, version 3 or later; or
  * - the BSD 2-Clause License;
  * 
  * This code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
  * See the license files for details.
  * 
  * You should have received a copy of the licenses along with this code. 
  * If not, see <http://www.codeproject.com/info/cpol10.aspx>, <http://www.gnu.org/licenses/> 
  * and <http://opensource.org/licenses/bsd-license.php>.
*/

using System;
using System.Runtime.InteropServices;

namespace Trinet.Core.IO.Ntfs
{
	internal sealed class StreamName : IDisposable
	{
		#region Private Data

		private static readonly SafeHGlobalHandle _invalidBlock = SafeHGlobalHandle.Invalid();
		private SafeHGlobalHandle _memoryBlock = _invalidBlock;

		#endregion

		#region Properties

		/// <summary>
		/// Returns the handle to the block of memory.
		/// </summary>
		/// <value>
		/// The <see cref="SafeHGlobalHandle"/> representing the block of memory.
		/// </value>
		public SafeHGlobalHandle MemoryBlock
		{
			get { return _memoryBlock; }
		}

		#endregion

		#region Methods

		/// <summary>
		/// Performs application-defined tasks associated with freeing, 
		/// releasing, or resetting unmanaged resources.
		/// </summary>
		public void Dispose()
		{
			if (!_memoryBlock.IsInvalid)
			{
				_memoryBlock.Dispose();
				_memoryBlock = _invalidBlock;
			}
		}

		/// <summary>
		/// Ensures that there is sufficient memory allocated.
		/// </summary>
		/// <param name="capacity">
		/// The required capacity of the block, in bytes.
		/// </param>
		/// <exception cref="OutOfMemoryException">
		/// There is insufficient memory to satisfy the request.
		/// </exception>
		public void EnsureCapacity(int capacity)
		{
			int currentSize = _memoryBlock.IsInvalid ? 0 : _memoryBlock.Size;
			if (capacity > currentSize)
			{
				if (0 != currentSize) currentSize <<= 1;
				if (capacity > currentSize) currentSize = capacity;

				if (!_memoryBlock.IsInvalid) _memoryBlock.Dispose();
				_memoryBlock = SafeHGlobalHandle.Allocate(currentSize);
			}
		}

		/// <summary>
		/// Reads the Unicode string from the memory block.
		/// </summary>
		/// <param name="length">
		/// The length of the string to read, in characters.
		/// </param>
		/// <returns>
		/// The string read from the memory block.
		/// </returns>
		public string ReadString(int length)
		{
			if (0 >= length || _memoryBlock.IsInvalid) return null;
			if (length > _memoryBlock.Size) length = _memoryBlock.Size;
			return Marshal.PtrToStringUni(_memoryBlock.DangerousGetHandle(), length);
		}

		/// <summary>
		/// Reads the string, and extracts the stream name.
		/// </summary>
		/// <param name="length">
		/// The length of the string to read, in characters.
		/// </param>
		/// <returns>
		/// The stream name.
		/// </returns>
		public string ReadStreamName(int length)
		{
			string name = ReadString(length);
			if (!string.IsNullOrEmpty(name))
			{
				// Name is of the format ":NAME:$DATA\0"
				int separatorIndex = name.IndexOf(SafeNativeMethods.StreamSeparator, 1);
				if (-1 != separatorIndex)
				{
					name = name.Substring(1, separatorIndex - 1);
				}
				else
				{
					// Should never happen!
					separatorIndex = name.IndexOf('\0');
					if (1 < separatorIndex)
					{
						name = name.Substring(1, separatorIndex - 1);
					}
					else
					{
						name = null;
					}
				}
			}

			return name;
		}

		#endregion
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer CodeProject
United Kingdom United Kingdom
I started writing code when I was 8, with my trusty ZX Spectrum and a subscription to "Input" magazine. Spent many a happy hour in the school's computer labs with the BBC Micros and our two DOS PCs.

After a brief detour into the world of Maths, I found my way back into programming during my degree via free copies of Delphi and Visual C++ given away with computing magazines.

I went straight from my degree into my first programming job, at Trinet Ltd. Eleven years later, the company merged to become ArcomIT. Three years after that, our project manager left to set up Nevalee Business Solutions, and took me with him. Since then, we've taken on four more members of staff, and more work than you can shake a stick at. Smile | :)

Between writing custom code to integrate with Visma Business, developing web portals to streamline operations for a large multi-national customer, and maintaining RedAtlas, our general aviation airport management system, there's certainly never a dull day in the office!

Outside of work, I enjoy real ale and decent books, and when I get the chance I "tinkle the ivories" on my Technics organ.

Comments and Discussions