<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>words &#187; C++</title>
	<atom:link href="http://www.curlybrace.com/words/category/technology/cplusplus/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.curlybrace.com/words</link>
	<description>by Jeff Fitzsimons</description>
	<lastBuildDate>Tue, 20 Dec 2011 10:21:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>What Exactly Are ATL&#8217;s BEGIN_COM_MAP, END_COM_MAP, and COM_INTERFACE_ENTRY Macros?</title>
		<link>http://www.curlybrace.com/words/2011/12/20/what-exactly-are-atls-begin_com_map-end_com_map-and-com_interface_entry/</link>
		<comments>http://www.curlybrace.com/words/2011/12/20/what-exactly-are-atls-begin_com_map-end_com_map-and-com_interface_entry/#comments</comments>
		<pubDate>Tue, 20 Dec 2011 10:20:44 +0000</pubDate>
		<dc:creator>Jeff Fitzsimons</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[COM]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://www.curlybrace.com/words/?p=1959</guid>
		<description><![CDATA[There are many places where ATL&#8217;s COM_MAP macros are documented, but I haven&#8217;t seen it plainly stated what they actually mean. Simply put, BEGIN_COM_MAP implements _InternalQueryInterface. COM_INTERFACE_ENTRY indicates that your class supports the specified interface. END_COM_MAP finishes the _InternalQueryInterface implementation. &#8230; <a href="http://www.curlybrace.com/words/2011/12/20/what-exactly-are-atls-begin_com_map-end_com_map-and-com_interface_entry/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>There are many places where ATL&#8217;s COM_MAP macros are documented, but I haven&#8217;t seen it plainly stated what they actually mean.</p>
<p>Simply put, BEGIN_COM_MAP implements _InternalQueryInterface.  COM_INTERFACE_ENTRY indicates that your class supports the specified interface.  END_COM_MAP finishes the _InternalQueryInterface implementation.</p>
<blockquote><p>Note:  A QueryInterface implementation which calls this _InternalQueryInterface method must either be hand-coded, <a href="http://msdn.microsoft.com/en-us/library/c43h4867(v=VS.100).aspx">or supplied by CComObject, CComAggObject, etc</a>.</p></blockquote>
<p>For example:</p>
<blockquote><pre>
class MyClass :
    public CComObjectRoot,
    public ISomeInterface
{
public:
    BEGIN_COM_MAP(MyClass)
        COM_INTERFACE_ENTRY(ISomeInterface)
    END_COM_MAP()
};

CComClass<MyClass> myInstance = new CComClass<MyCLass>;
</pre>
</blockquote>
<p>The COM_INTERFACE_ENTRY line indicates that MyClass implements ISomeInterface.  If QueryInterface is called to requests an ISomeInterface pointer, the call will succeed.  Without the COM_INTERFACE_ENTRY line, the QueryInterface call would fail.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.curlybrace.com/words/2011/12/20/what-exactly-are-atls-begin_com_map-end_com_map-and-com_interface_entry/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Alternate Data Streams (Metadata) on Files in NTFS</title>
		<link>http://www.curlybrace.com/words/2011/01/01/alternate-data-streams/</link>
		<comments>http://www.curlybrace.com/words/2011/01/01/alternate-data-streams/#comments</comments>
		<pubDate>Sun, 02 Jan 2011 07:51:50 +0000</pubDate>
		<dc:creator>Jeff Fitzsimons</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://www.curlybrace.com/words/?p=1120</guid>
		<description><![CDATA[Introduction Alternate Data Streams (ADS) allow arbitrary metadata to be associated with files and directories on Windows NTFS. Alternate data streams are the Windows implementation of forks. The apparent size of the file will be unchanged, and most applications and &#8230; <a href="http://www.curlybrace.com/words/2011/01/01/alternate-data-streams/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<h2>Introduction</h2>
<p>Alternate Data Streams (ADS) allow arbitrary metadata to be associated with files and directories on Windows NTFS.  Alternate data streams are the Windows implementation of <a href="http://en.wikipedia.org/wiki/Fork_%28filesystem%29">forks</a>.  The apparent size of the file will be unchanged, and most applications and users are unaware of their existence.  If a file is moved, any alternate data stream will move along with it, as long as the destination is on an NTFS drive.</p>
<p>The command line can access alternate data streams using redirection operators.  Streams are specified on the command line as <i>filename</i><b>:</b><i>stream name</i>.</p>
<h3>Creating an Alternate Data Stream</h3>
<p>As an example, a string is written into an ADS named <tt>hidden</tt>, which is associated with file <tt>test.txt</tt>:</p>
<blockquote><pre class="DOS">C:\test>echo Hidden text > test.txt:hidden</pre>
</blockquote>
<p>The file appears to be empty, though as detailed below, the metadata is intact and associated with the file:</p>
<blockquote><pre class="DOS">C:\test>dir test.txt

06/24/2010  01:33 PM                 0 test.txt</pre>
</blockquote>
<h3>Viewing an Alternate Data Stream</h3>
<p>The metadata can be viewed by redirecting from it to <tt>more</tt>:</p>
<blockquote><pre class="DOS">C:\test>more < test.txt:hidden
Hidden text</pre>
</blockquote>
<p>The name and content of the ADS can be anything (see 'Details' below for restrictions):</p>
<blockquote><pre class="DOS">C:\test>echo Arbitrary string > test.txt:arbitraryName

C:\test>more < test.txt:arbitraryName
Arbitrary string</pre>
</blockquote>
<h3>Listing Files With Alternate Data Streams</h3>
<p>On Windows Vista and later, a list of alternate data streams can be obtained using <tt>DIR /R</tt>:</p>
<blockquote><pre class="DOS">C:\test>dir test.txt /R

06/24/2010  01:33 PM                 0 test.txt
                                    38 test.txt:arbitraryName:$DATA
                                    28 test.txt:hidden:$DATA</pre>
</blockquote>
<p>On earlier operating systems, the SysInternals utility <a href="http://technet.microsoft.com/en-us/sysinternals/bb897440">Streams</a> can be used:</p>
<blockquote><pre class="DOS">C:\test>c:\tools\SysInternals\streams.exe test.txt

Streams v1.56 - Enumerate alternate NTFS data streams
Copyright (C) 1999-2007 Mark Russinovich
Sysinternals - www.sysinternals.com

C:\test\test.txt:
   :arbitraryName:$DATA 38
          :hidden:$DATA 28</pre>
</blockquote>
<h3>Alternate Data Streams on Directories</h3>
<p>Metadata can be added to directories the same way it's added to files:</p>
<blockquote><pre class="DOS">C:\test>mkdir test2

C:\test>echo ADS on a directory > test2:someText

C:\test>dir /r

06/25/2010  11:27 PM    &lt;DIR&gt;          .
06/25/2010  11:27 PM    &lt;DIR&gt;          ..
06/25/2010  11:27 PM    &lt;DIR&gt;          test2
                                    42 test2:someText:$DATA

C:\test>more < test2:someText
ADS on a directory</pre>
</blockquote>
<h2>Details</h2>
<h3>Stream Naming</h3>
<p>To be more accurate, streams are specified as <i>filename</i><b>:</b><i>stream name</i><b>:</b><i>stream type</i>.  It appears that the only stream type accessible from the command line is $DATA, which is why it's optional.  All of the stream types are listed in the <a href="http://msdn.microsoft.com/en-us/library/aa362667%28v=VS.85%29.aspx">WIN32_STREAM_ID structure documentation</a>.  The default data stream is unnamed, so <i>filename</i>::$DATA will contain the file's data:</p>
<blockquote><pre class="DOS">C:\test>echo This is the file > file.txt

C:\test>echo This is the stream > file.txt:stream

C:\test>more < file.txt::$DATA
This is the file

C:\test>more < file.txt:stream:$DATA
This is the stream</pre>
</blockquote>
<p>Stream names are generally held to the same requirements as any filename.  One interesting difference is that stream names can contain characters whose integer representations are in the range from 1 through 31.  Refer to <a href="http://msdn.microsoft.com/en-us/library/aa365247%28v=VS.85%29.aspx">Naming Files, Paths, and Namespaces</a> (MSDN) for details.</p>
<p>Note that when using streams with files having a single letter name, the filename should be prefixed with a period and backslash.  The reason for this is Windows drive names.  For example, does "<tt>echo hello > c:test</tt>" refer to a stream named <tt>test</tt> on file <tt>c</tt>, or does it refer to a file <tt>test</tt> on drive <tt>c</tt>?</p>
<h3>Executing Streams</h3>
<p>As of Windows Vista, it is no longer possible to execute directly from an alternate data stream.  On Windows XP and earlier, the Start command was used, similar to <tt>start somefile.ext:hiddenExecutable</tt>.</p>
<h3>Editing with Notepad</h3>
<p>Notepad can be used to create and edit alternate data streams.  The File Open dialog doesn't recognize stream syntax, however, so the file must be created and opened using command line parameters.  Notepad will insist on appending <tt>.txt</tt> to the stream name.</p>
<h3>Programmatic Access</h3>
<p>Microsoft provides a <a href="http://support.microsoft.com/kb/105763">sample program</a> in C++, demonstrating how to open and write to an alternate data stream.</p>
<h2>Real-World Applications</h2>
<h3>Downloaded Executables</h3>
<p><a href="http://blogs.msdn.com/b/oldnewthing/archive/2007/08/27/4580767.aspx?PageIndex=4">Since Windows XP SP2</a>, when a file is downloaded from the Internet and executed (assuming a zone-aware browser), this warning is displayed:</p>
<blockquote><p><a href="http://www.curlybrace.com/words/wp-content/uploads/2010/06/UAC_Example_s.png"><img src="http://www.curlybrace.com/words/wp-content/uploads/2010/06/UAC_Example_s.png" alt="" title="UAC_Example_s" width="320" height="192" class="aligncenter size-full wp-image-1127" /></a></p></blockquote>
<p>Windows displays this warning because the web browser tagged the executable with a alternate data stream named <tt>Zone.Identifier</tt>:</p>
<blockquote><pre class="DOS">C:\test>dir /r setup.exe

06/25/2010  12:10 PM           680,467 setup.exe
                                    26 setup.exe:Zone.Identifier:$DATA</pre>
</blockquote>
<p>By redirecting this stream to <tt>more</tt>, we can see its contents:</p>
<blockquote><pre class="DOS">C:\test>more < setup.exe:Zone.Identifier
[ZoneTransfer]
ZoneId=3</pre>
</blockquote>
<p>The <a href="http://blogs.msdn.com/b/powershell/archive/2007/03/07/how-does-the-remotesigned-execution-policy-work.aspx">PowerShell blog</a> has more information on zone identifiers.</p>
<h3>Viruses</h3>
<p>The W2K.Stream virus <a href="http://www.symantec.com/security_response/writeup.jsp?docid=2000-121416-2928-99">used alternate data streams</a>.</p>
<h2>Additional Resources</h2>
<ul>
<li /><a href="http://msdn.microsoft.com/en-us/library/ms810604.aspx"><del>A Programmer's Perspective on NTFS 2000 Part 1: Stream and Hard Link</del></a> (MSDN, article removed)
<li /><a href="http://msdn.microsoft.com/en-us/library/ms810500.aspx"><del>A Programmer's Perspective on NTFS 2000 Part 2: Encryption, Sparseness, and Reparse Points</del></a> (MSDN, article removed)
<li /><a href="http://msdn.microsoft.com/en-us/library/aa364404%28v=VS.85%29.aspx">File Streams</a> (MSDN)
<li /><a href="http://www.codeproject.com/KB/shell/csadsdetectorarticle.aspx">Visual browsing of alternative data-streams in Windows Explorer</a> (CodeProject)
<li /><a href="http://www.alex-ionescu.com/NTFS%20Alternate%20Data%20Streams.pdf">NTFS Alternate Data Streams</a> (Alex Ionescu)
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.curlybrace.com/words/2011/01/01/alternate-data-streams/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Queue Implemented Using Stacks</title>
		<link>http://www.curlybrace.com/words/2009/02/09/queue-implemented-using-stack/</link>
		<comments>http://www.curlybrace.com/words/2009/02/09/queue-implemented-using-stack/#comments</comments>
		<pubDate>Tue, 10 Feb 2009 00:56:07 +0000</pubDate>
		<dc:creator>Jeff Fitzsimons</dc:creator>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[interview question]]></category>

		<guid isPermaLink="false">http://www.curlybrace.com/words/?p=684</guid>
		<description><![CDATA[Here are two solutions for using stacks to emulate a queue. The first always keeps one or both stacks empty, shifts the set of values back and forth as the caller switches between enqueuing and dequeueing. The second maintains an &#8230; <a href="http://www.curlybrace.com/words/2009/02/09/queue-implemented-using-stack/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Here are two solutions for using stacks to emulate a queue.  The first always keeps one or both stacks empty, shifts the set of values back and forth as the caller switches between enqueuing and dequeueing.  The second maintains an input stack and an output stack.  All enqueued data goes directly onto the input stack.  When dequeue is called, if the output stack has anything on it, it takes the topmost value, otherwise it transfers the entire input stack into the output stack.</p>
<p>The latter implementation is more efficient &#8212; transfer between the two stacks occurs only when dequeue is called <b>and</b> the output stack is already empty &#8212; as well as more compact and readable.</p>
<h3>Stack-Swapping Implementation</h3>
<p>This version keeps one stack empty, and swaps all content between the two each time we switch between enqueue and dequeue operations.</p>
<pre class="brush: cpp; title: ; notranslate">
#include &lt;stdio.h&gt;
#include &lt;stack&gt;

// This implementation uses two stacks.  It keeps one empty, and swaps content
// whenever we switch between eprforming enqueues and dequeues.
class FakeQueue
{
    std::stack&lt;int&gt; s1;
    std::stack&lt;int&gt; s2;

	// We need to keep track of the last operation (push vs. pop) so that we can
    // resorder our stack whenever we switch between enqueue and dequeue.
	typedef enum { PUSH = 0, POP } STACK_OPS;
	STACK_OPS last_op;

	void SwapStacks();

public:
	void enqueue(int i);
	bool dequeue(int &amp;i);

	FakeQueue() : last_op(PUSH)
	{ /* empty constructor */ }
};

// SwapStacks swaps the stack between s1 and s2.
void FakeQueue::SwapStacks()
{
	if (s1.empty())
	{
		while (!s2.empty())
		{
			s1.push(s2.top());
			s2.pop();
   		}
	}
	else
	{
        while (!s1.empty())
        {
            s2.push(s1.top());
            s1.pop();
        }
    }
}

void FakeQueue::enqueue(int i)
{
	if (last_op == POP)
	{
		SwapStacks();
		last_op = PUSH;
	}

	if (s1.empty())
        s2.push(i);
	else
        s1.push(i);
}

// Retrieves the oldest item in the virtual queue.  If the queue is empty, sets i to
// -1 and returns false.  Otherwise, it returns true.
bool FakeQueue::dequeue(int &amp;i)
{
    if (last_op == PUSH)
    {
        SwapStacks();
        last_op = POP;
    }

    if (!s1.empty())
    {
        i = s1.top();
        s1.pop();
        return true;
    }
    else if (!s2.empty())
    {
        i = s2.top();
        s2.pop();
        return true;
    }
    else
    {
        // Queue is empty.
        i = -1;
        return false;
    }
}

int main(int /*argc*/, char ** /*argv*/)
{
    FakeQueue queue;
    int i;

    // Perform a variety of queues and dequeues to show that the queue order is
    // always maintained.

    queue.enqueue(1);
    queue.enqueue(2); // contains { 1, 2 }

    queue.dequeue(i);
    printf(&quot;Dequeued %d\n&quot;, i);

    queue.enqueue(3);
    queue.enqueue(4); // contains { 2, 3, 4 }

    queue.dequeue(i);
    printf(&quot;Dequeued %d\n&quot;, i);

    queue.enqueue(5); // contains { 3, 4, 5 }

    while (queue.dequeue(i))
    {
        printf(&quot;Dequeued %d\n&quot;, i);
    }

    return 0;
}
</pre>
<h3>Input-Output Stack Implementation</h3>
<p>This version uses dedicated input and output stacks.  All input goes on the input stack.  When dequeue is called, if the output stack has anything on it, it takes the topmost value, otherwise it transfers the entire input stack into the output stack.</p>
<pre class="brush: cpp; title: ; notranslate">

#include &lt;stdio.h&gt;
#include &lt;stack&gt;

// The second implementation uses dedicated input and output stacks.
class FakeQueue
{
    std::stack&lt;int&gt; in;
    std::stack&lt;int&gt; out;

public:
    void enqueue(int i);
    bool dequeue(int &amp;i);
};

void FakeQueue::enqueue(int i)
{
    in.push(i);
}

bool FakeQueue::dequeue(int &amp;i)
{
    // Abort if our queue is empty.
    if (in.empty() &amp;&amp; out.empty())
    {
        i = -1;
        return false;
    }

    // If the &quot;out&quot; stack is empty, shift the contents of the &quot;in&quot; stack into
    // it...
    if (out.empty())
    {
        while (!in.empty())
        {
            out.push(in.top());
            in.pop();
        }
    }

    // Pop the return value from the &quot;out&quot; stack:
    i = out.top();
    out.pop();

    return true;
}

int main(int /*argc*/, char ** /*argv*/)
{
    FakeQueue queue;
    int i;

    // Perform a variety of queues and dequeues to show that the queue order is
    // always maintained.

    queue.enqueue(1);
    queue.enqueue(2); // contains { 1, 2 }

    queue.dequeue(i);
    printf(&quot;Dequeued %d\n&quot;, i);

    queue.enqueue(3);
    queue.enqueue(4); // contains { 2, 3, 4 }

    queue.dequeue(i);
    printf(&quot;Dequeued %d\n&quot;, i);

    queue.enqueue(5); // contains { 3, 4, 5 }

    while (queue.dequeue(i))
    {
        printf(&quot;Dequeued %d\n&quot;, i);
    }

    return 0;
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.curlybrace.com/words/2009/02/09/queue-implemented-using-stack/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

