<?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>CE&#039;s Blog</title>
	<atom:link href="http://www.christian-etter.de/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://www.christian-etter.de</link>
	<description>早起的鸟儿有虫吃 - 早起的虫儿被鸟吃</description>
	<lastBuildDate>Sun, 07 Apr 2013 00:08:02 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Custom Memory Destination Manager for libjpeg</title>
		<link>http://www.christian-etter.de/?p=842</link>
		<comments>http://www.christian-etter.de/?p=842#comments</comments>
		<pubDate>Sat, 06 Apr 2013 23:55:54 +0000</pubDate>
		<dc:creator>Christian Etter</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[destination manager]]></category>
		<category><![CDATA[jpeg_destination_mgr]]></category>
		<category><![CDATA[libjpeg]]></category>
		<category><![CDATA[libjpeg-turbo]]></category>
		<category><![CDATA[memory]]></category>

		<guid isPermaLink="false">http://www.christian-etter.de/?p=842</guid>
		<description><![CDATA[Similar to creating an in memory source manager for libjpeg decompression, we can also create a destination manager which will store the compressed data in memory. FOr this purpose, the destination manager contains a byte vector: typedef struct _jpeg_destination_mem_mgr &#123; jpeg_destination_mgr mgr; std::vector&#60;unsigned char&#62; data; &#125; jpeg_destination_mem_mgr; Initialization takes place in the callback mem_init_destination(): static [...]]]></description>
				<content:encoded><![CDATA[<p>Similar to creating an in <a href="/?p=828" title="Memory Source Manager">memory source manager</a> for libjpeg decompression, we can also create a destination manager which will store the compressed data in memory.<br />
FOr this purpose, the destination manager contains a byte vector:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">typedef</span> <span style="color: #993333;">struct</span> _jpeg_destination_mem_mgr
<span style="color: #009900;">&#123;</span>
    jpeg_destination_mgr mgr<span style="color: #339933;">;</span>
    std<span style="color: #339933;">::</span><span style="color: #202020;">vector</span><span style="color: #339933;">&lt;</span><span style="color: #993333;">unsigned</span> char<span style="color: #339933;">&gt;</span> data<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> jpeg_destination_mem_mgr<span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Initialization takes place in the callback <em>mem_init_destination()</em>:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">static</span> <span style="color: #993333;">void</span> mem_init_destination<span style="color: #009900;">&#40;</span> j_compress_ptr cinfo <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    jpeg_destination_mem_mgr<span style="color: #339933;">*</span> dst <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>jpeg_destination_mem_mgr<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>cinfo<span style="color: #339933;">-&gt;</span>dest<span style="color: #339933;">;</span>
    dst<span style="color: #339933;">-&gt;</span>data.<span style="color: #202020;">resize</span><span style="color: #009900;">&#40;</span> JPEG_MEM_DST_MGR_BUFFER_SIZE <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    cinfo<span style="color: #339933;">-&gt;</span>dest<span style="color: #339933;">-&gt;</span>next_output_byte <span style="color: #339933;">=</span> dst<span style="color: #339933;">-&gt;</span>data.<span style="color: #202020;">data</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    cinfo<span style="color: #339933;">-&gt;</span>dest<span style="color: #339933;">-&gt;</span>free_in_buffer <span style="color: #339933;">=</span> dst<span style="color: #339933;">-&gt;</span>data.<span style="color: #202020;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>When the compression has finished, we need to resize the buffer to the actual size:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">static</span> <span style="color: #993333;">void</span> mem_term_destination<span style="color: #009900;">&#40;</span> j_compress_ptr cinfo <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    jpeg_destination_mem_mgr<span style="color: #339933;">*</span> dst <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>jpeg_destination_mem_mgr<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>cinfo<span style="color: #339933;">-&gt;</span>dest<span style="color: #339933;">;</span>
    dst<span style="color: #339933;">-&gt;</span>data.<span style="color: #202020;">resize</span><span style="color: #009900;">&#40;</span> dst<span style="color: #339933;">-&gt;</span>data.<span style="color: #202020;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> cinfo<span style="color: #339933;">-&gt;</span>dest<span style="color: #339933;">-&gt;</span>free_in_buffer <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>When the buffer size is not large enough, the library requests more data in the following callback:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">static</span> boolean mem_empty_output_buffer<span style="color: #009900;">&#40;</span> j_compress_ptr cinfo <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    jpeg_destination_mem_mgr<span style="color: #339933;">*</span> dst <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>jpeg_destination_mem_mgr<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>cinfo<span style="color: #339933;">-&gt;</span>dest<span style="color: #339933;">;</span>
    <span style="color: #993333;">size_t</span> oldsize <span style="color: #339933;">=</span> dst<span style="color: #339933;">-&gt;</span>data.<span style="color: #202020;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    dst<span style="color: #339933;">-&gt;</span>data.<span style="color: #202020;">resize</span><span style="color: #009900;">&#40;</span> oldsize <span style="color: #339933;">+</span> JPEG_MEM_DST_MGR_BUFFER_SIZE <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    cinfo<span style="color: #339933;">-&gt;</span>dest<span style="color: #339933;">-&gt;</span>next_output_byte <span style="color: #339933;">=</span> dst<span style="color: #339933;">-&gt;</span>data.<span style="color: #202020;">data</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> oldsize<span style="color: #339933;">;</span>
    cinfo<span style="color: #339933;">-&gt;</span>dest<span style="color: #339933;">-&gt;</span>free_in_buffer <span style="color: #339933;">=</span> JPEG_MEM_DST_MGR_BUFFER_SIZE<span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>All callbacks are configured in <em>jpeg_mem_dest()</em>:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">static</span> <span style="color: #993333;">void</span> jpeg_mem_dest<span style="color: #009900;">&#40;</span> j_compress_ptr cinfo<span style="color: #339933;">,</span> jpeg_destination_mem_mgr <span style="color: #339933;">*</span> dst <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    cinfo<span style="color: #339933;">-&gt;</span>dest <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>jpeg_destination_mgr<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>dst<span style="color: #339933;">;</span>
    cinfo<span style="color: #339933;">-&gt;</span>dest<span style="color: #339933;">-&gt;</span>init_destination <span style="color: #339933;">=</span> mem_init_destination<span style="color: #339933;">;</span>
    cinfo<span style="color: #339933;">-&gt;</span>dest<span style="color: #339933;">-&gt;</span>term_destination <span style="color: #339933;">=</span> mem_term_destination<span style="color: #339933;">;</span>
    cinfo<span style="color: #339933;">-&gt;</span>dest<span style="color: #339933;">-&gt;</span>empty_output_buffer <span style="color: #339933;">=</span> mem_empty_output_buffer<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Usage:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="c" style="font-family:monospace;">jpeg_destination_mem_mgr dst_mem<span style="color: #339933;">;</span>
jpeg_compress_struct_wrapper cinfo<span style="color: #339933;">;</span>
j_compress_ptr pcinfo <span style="color: #339933;">=</span> cinfo<span style="color: #339933;">;</span>
jpeg_mem_dest<span style="color: #009900;">&#40;</span> cinfo<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>dst_mem<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>After encoding has finished, data can be retrieved from the <em>data</em> member of the destination manager.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span> pjpeg_data <span style="color: #339933;">=</span> dst_mem.<span style="color: #202020;">data</span>.<span style="color: #202020;">data</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #993333;">size_t</span> data_size <span style="color: #339933;">=</span> dst_mem.<span style="color: #202020;">data</span>.<span style="color: #202020;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Here is a link to the entire code: <a href="/wp-content/uploads/jpeg_mem_dst.zip">jpeg_mem_dst.h</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.christian-etter.de/?feed=rss2&#038;p=842</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Custom Memory Source Manager for libjpeg</title>
		<link>http://www.christian-etter.de/?p=828</link>
		<comments>http://www.christian-etter.de/?p=828#comments</comments>
		<pubDate>Sat, 06 Apr 2013 16:35:49 +0000</pubDate>
		<dc:creator>Christian Etter</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[JPEG]]></category>
		<category><![CDATA[jpeg_mem_src]]></category>
		<category><![CDATA[libjpeg]]></category>
		<category><![CDATA[libjpeg-turbo]]></category>
		<category><![CDATA[source manager]]></category>

		<guid isPermaLink="false">http://www.christian-etter.de/?p=828</guid>
		<description><![CDATA[libjpeg-turbo is an optimized plug-in replacement for libjpeg, which delivers 2-4 times the performance of the original library. However currently the current libjpeg-turbo release version have a memory source manager included when running in libjpeg v6b compatibility mode. The following implementation fills the gap: The callback mem_fill_input_buffer() is invoked when the library expects more data [...]]]></description>
				<content:encoded><![CDATA[<p><a href="http://libjpeg-turbo.virtualgl.org/" title="libjpeg-turbo">libjpeg-turbo</a> is an optimized plug-in replacement for libjpeg, which delivers 2-4 times the performance of the original library. However currently the current libjpeg-turbo release version have a memory source manager included when running in libjpeg v6b compatibility mode.</p>
<p>The following implementation fills the gap:</p>
<p>The callback mem_fill_input_buffer() is invoked when the library expects more data than supplied in the buffer. Since we already hold all data in the memory buffer, this can only be due to the image being prematurely terminated. There are two different ways of handling such a circumstance, either allow the library to finish processing and display the partial data, or abort processing by raising an error. The first option is activated by defining <em>PROCESS_TRUNCATED_IMAGES</em>.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">static</span> boolean mem_fill_input_buffer<span style="color: #009900;">&#40;</span> j_decompress_ptr cinfo <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
<span style="color: #339933;">#ifdef PROCESS_TRUNCATED_IMAGES</span>
    jpeg_source_mgr<span style="color: #339933;">*</span> src <span style="color: #339933;">=</span> cinfo<span style="color: #339933;">-&gt;</span>src<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #993333;">static</span> <span style="color: #993333;">const</span> JOCTET EOI_BUFFER<span style="color: #009900;">&#91;</span> <span style="color: #0000dd;">2</span> <span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#40;</span>JOCTET<span style="color: #009900;">&#41;</span><span style="color: #208080;">0xFF</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span>JOCTET<span style="color: #009900;">&#41;</span>JPEG_EOI <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    src<span style="color: #339933;">-&gt;</span>next_input_byte <span style="color: #339933;">=</span> EOI_BUFFER<span style="color: #339933;">;</span>
    src<span style="color: #339933;">-&gt;</span>bytes_in_buffer <span style="color: #339933;">=</span> <span style="color: #993333;">sizeof</span><span style="color: #009900;">&#40;</span> EOI_BUFFER <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">#else</span>
    ERREXIT<span style="color: #009900;">&#40;</span> cinfo<span style="color: #339933;">,</span> JERR_INPUT_EOF <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">#endif</span>
    <span style="color: #b1b100;">return</span> TRUE<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>mem_skip_input_data() is called when the library wants to skip over a certain part of the data. If the data to be skipped is less than the remaining bytes in the buffer, we simple anjust the buffer pointer. If there is not enough input data, we either raise an exception or set the input data length to zero, which will result in a call to mem_fill_input_buffer(), where an EOI marker is returned.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">static</span> <span style="color: #993333;">void</span> mem_skip_input_data<span style="color: #009900;">&#40;</span> j_decompress_ptr cinfo<span style="color: #339933;">,</span> <span style="color: #993333;">long</span> num_bytes <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    jpeg_source_mgr<span style="color: #339933;">*</span> src <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>jpeg_source_mgr<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>cinfo<span style="color: #339933;">-&gt;</span>src<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000dd;">1</span> <span style="color: #339933;">&gt;</span> num_bytes <span style="color: #009900;">&#41;</span>
        <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> num_bytes <span style="color: #339933;">&lt;</span> src<span style="color: #339933;">-&gt;</span>bytes_in_buffer <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        src<span style="color: #339933;">-&gt;</span>next_input_byte <span style="color: #339933;">+=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">size_t</span><span style="color: #009900;">&#41;</span>num_bytes<span style="color: #339933;">;</span>
        src<span style="color: #339933;">-&gt;</span>bytes_in_buffer <span style="color: #339933;">-=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">size_t</span><span style="color: #009900;">&#41;</span>num_bytes<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span>
    <span style="color: #009900;">&#123;</span>
<span style="color: #339933;">#ifdef PROCESS_TRUNCATED_IMAGES</span>
        src<span style="color: #339933;">-&gt;</span>bytes_in_buffer <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #339933;">#else</span>
        ERREXIT<span style="color: #009900;">&#40;</span> cinfo<span style="color: #339933;">,</span> JERR_INPUT_EOF <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #339933;">#endif</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>In our case, there is no need to perform initialization or termination, since the buffer which is used is managed by the caller.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">static</span> <span style="color: #993333;">void</span> mem_init_source<span style="color: #009900;">&#40;</span> j_decompress_ptr cinfo <span style="color: #009900;">&#41;</span> 
<span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">static</span> <span style="color: #993333;">void</span> mem_term_source<span style="color: #009900;">&#40;</span> j_decompress_ptr cinfo <span style="color: #009900;">&#41;</span> 
<span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The following function performs initialization of the memory buffer. It is important to note that the bytes supplied are not being copied, and therefore the buffer must not be freed before the image processing has finished.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">static</span> <span style="color: #993333;">void</span> jpeg_mem_src<span style="color: #009900;">&#40;</span> j_decompress_ptr cinfo<span style="color: #339933;">,</span> jpeg_source_mgr<span style="color: #339933;">*</span> src<span style="color: #339933;">,</span> <span style="color: #993333;">void</span><span style="color: #339933;">*</span> buffer<span style="color: #339933;">,</span> <span style="color: #993333;">long</span> nbytes <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    src<span style="color: #339933;">-&gt;</span>init_source <span style="color: #339933;">=</span> mem_init_source<span style="color: #339933;">;</span>
    src<span style="color: #339933;">-&gt;</span>fill_input_buffer <span style="color: #339933;">=</span> mem_fill_input_buffer<span style="color: #339933;">;</span>
    src<span style="color: #339933;">-&gt;</span>skip_input_data <span style="color: #339933;">=</span> mem_skip_input_data<span style="color: #339933;">;</span>
    src<span style="color: #339933;">-&gt;</span>resync_to_restart <span style="color: #339933;">=</span> jpeg_resync_to_restart<span style="color: #339933;">;</span>
    src<span style="color: #339933;">-&gt;</span>term_source <span style="color: #339933;">=</span> mem_term_source<span style="color: #339933;">;</span>
    src<span style="color: #339933;">-&gt;</span>bytes_in_buffer <span style="color: #339933;">=</span> nbytes<span style="color: #339933;">;</span>
    src<span style="color: #339933;">-&gt;</span>next_input_byte <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>JOCTET<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>buffer<span style="color: #339933;">;</span>
    cinfo<span style="color: #339933;">-&gt;</span>src <span style="color: #339933;">=</span> src<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>With the above manager definition, we can easily initialize and work with the memory source manager.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="c" style="font-family:monospace;">jpeg_decompress_struct cinfo<span style="color: #339933;">;</span>
jpeg_source_mgr src_mem<span style="color: #339933;">;</span>
jpeg_create_decompress<span style="color: #009900;">&#40;</span> <span style="color: #339933;">&amp;</span>cinfo <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
jpeg_mem_src<span style="color: #009900;">&#40;</span> <span style="color: #339933;">&amp;</span>cinfo<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>src_mem<span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span>PVOID<span style="color: #009900;">&#41;</span>content<span style="color: #339933;">,</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">long</span><span style="color: #009900;">&#41;</span>dwSize <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
jpeg_read_header<span style="color: #009900;">&#40;</span> <span style="color: #339933;">&amp;</span>cinfo<span style="color: #339933;">,</span> TRUE <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Here is the complete header file: <a href="/wp-content/uploads/jpeg_mem_src.zip">jpeg_mem_src.h</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christian-etter.de/?feed=rss2&#038;p=828</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>RAII Wrapper for libjpeg Compression/Decompression</title>
		<link>http://www.christian-etter.de/?p=834</link>
		<comments>http://www.christian-etter.de/?p=834#comments</comments>
		<pubDate>Sun, 06 Jan 2013 22:39:14 +0000</pubDate>
		<dc:creator>Christian Etter</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[cinfo]]></category>
		<category><![CDATA[JPEG]]></category>
		<category><![CDATA[jpeg_compress_struct]]></category>
		<category><![CDATA[jpeg_decompress_struct]]></category>
		<category><![CDATA[libjpeg]]></category>
		<category><![CDATA[libjpeg-turbo]]></category>
		<category><![CDATA[RAII]]></category>
		<category><![CDATA[Wrapper]]></category>

		<guid isPermaLink="false">http://www.christian-etter.de/?p=834</guid>
		<description><![CDATA[To facilitate resource cleanup within libjpeg encoding/decoding functions, the following classes implement RAII wrappers for the jpeg_decompress_struct and jpeg_compress_struct structures. RAII is a C++ concept which helps to ensure that all resources are correctly deinitialized/released even in case of error/exceptions. Usage example: jpeg_decompress_struct_wrapper cinfo; jpeg_decompress_struct* pcinfo = cinfo; jpeg_win32_src_mgr src_data; &#160; jpeg_win32_src&#40; cinfo, &#38;src_data, szFilename [...]]]></description>
				<content:encoded><![CDATA[<p>To facilitate resource cleanup within libjpeg encoding/decoding functions, the following classes implement RAII wrappers for the jpeg_decompress_struct and jpeg_compress_struct structures.</p>
<p><a href="http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization" title="Wikipedia RAII">RAII</a> is a C++ concept which helps to ensure that all resources are correctly deinitialized/released even in case of error/exceptions.</p>
<p>Usage example:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="c" style="font-family:monospace;">jpeg_decompress_struct_wrapper cinfo<span style="color: #339933;">;</span>
jpeg_decompress_struct<span style="color: #339933;">*</span> pcinfo <span style="color: #339933;">=</span> cinfo<span style="color: #339933;">;</span>
jpeg_win32_src_mgr src_data<span style="color: #339933;">;</span>
&nbsp;
jpeg_win32_src<span style="color: #009900;">&#40;</span> cinfo<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>src_data<span style="color: #339933;">,</span> szFilename <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
jpeg_read_header<span style="color: #009900;">&#40;</span> cinfo<span style="color: #339933;">,</span> TRUE <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
pcinfo<span style="color: #339933;">-&gt;</span>dct_method <span style="color: #339933;">=</span> JDCT_IFAST<span style="color: #339933;">;</span>
<span style="color: #808080; font-style: italic;">/* ... */</span></pre></td></tr></table></div>

<p>No calls to <em>jpeg_destroy_decompress()</em> are necessary, stack unwinding will ensure that the <em>jpeg_decompress_struct</em> will be automatically destroyed, even if the encoding/decoding routine has several exit/return paths.<br />
Since the wrapper class implements an operator for <em>jpeg_decompress_struct*</em>, the instance can be passed as a parameter instead of &#8220;&#038;cinfo&#8221;.</p>
<p>This is the wrapper for <em>jpeg_decompress_struct</em>:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="c" style="font-family:monospace;">class jpeg_decompress_struct_wrapper
<span style="color: #009900;">&#123;</span>
public<span style="color: #339933;">:</span>
    jpeg_decompress_struct_wrapper<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        jpeg_create_decompress<span style="color: #009900;">&#40;</span> <span style="color: #339933;">&amp;</span>this<span style="color: #339933;">-&gt;</span>cinfo <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    ~jpeg_decompress_struct_wrapper<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        jpeg_destroy_decompress<span style="color: #009900;">&#40;</span> <span style="color: #339933;">&amp;</span>this<span style="color: #339933;">-&gt;</span>cinfo <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    operator jpeg_decompress_struct<span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #339933;">&amp;</span>this<span style="color: #339933;">-&gt;</span>cinfo<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
private<span style="color: #339933;">:</span>
    jpeg_decompress_struct cinfo<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Wrapper for <em>jpeg_compress_struct</em>:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="c" style="font-family:monospace;">class jpeg_compress_struct_wrapper
<span style="color: #009900;">&#123;</span>
public<span style="color: #339933;">:</span>
    jpeg_compress_struct_wrapper<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        jpeg_create_compress<span style="color: #009900;">&#40;</span> <span style="color: #339933;">&amp;</span>this<span style="color: #339933;">-&gt;</span>cinfo <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    ~jpeg_compress_struct_wrapper<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        jpeg_destroy_compress<span style="color: #009900;">&#40;</span> <span style="color: #339933;">&amp;</span>this<span style="color: #339933;">-&gt;</span>cinfo <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    operator jpeg_compress_struct<span style="color: #339933;">*</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #339933;">&amp;</span>this<span style="color: #339933;">-&gt;</span>cinfo<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
private<span style="color: #339933;">:</span>
    jpeg_compress_struct cinfo<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.christian-etter.de/?feed=rss2&#038;p=834</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Win32 libjpeg Source Manager</title>
		<link>http://www.christian-etter.de/?p=811</link>
		<comments>http://www.christian-etter.de/?p=811#comments</comments>
		<pubDate>Thu, 06 Dec 2012 14:37:34 +0000</pubDate>
		<dc:creator>Christian Etter</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[JPEG]]></category>
		<category><![CDATA[jpeg_source_mgr]]></category>
		<category><![CDATA[jpeg_stdio_src]]></category>
		<category><![CDATA[libjpeg]]></category>
		<category><![CDATA[libjpeg-turbo]]></category>
		<category><![CDATA[source manager]]></category>
		<category><![CDATA[stdio]]></category>
		<category><![CDATA[Win32]]></category>

		<guid isPermaLink="false">http://www.christian-etter.de/?p=811</guid>
		<description><![CDATA[I was recently running into problems using libjpeg-turbo together with the stdio FILE* API. So instead of using _wfopen_s, _tfopen_s etc. I decided to write my own source manager which does not rely on stdlib functionality and uses the Win32 CreateFile API instead. First, we need to extend a jpeg_src_mgr structure to hold additional data. [...]]]></description>
				<content:encoded><![CDATA[<p>I was recently running into problems using libjpeg-turbo together with the stdio FILE* API. So instead of using _wfopen_s, _tfopen_s etc. I decided to write my own source manager which does not rely on stdlib functionality and uses the Win32 CreateFile API instead. </p>
<p>First, we need to extend a jpeg_src_mgr structure to hold additional data.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#define JPEG_WIN32_SRC_MGR_BUFFER_SIZE ( 1 &lt;&lt; 10 )</span>
&nbsp;
<span style="color: #993333;">typedef</span> <span style="color: #993333;">struct</span> _jpeg_win32_src_mgr
<span style="color: #009900;">&#123;</span>
    jpeg_source_mgr mgr<span style="color: #339933;">;</span>
    <span style="color: #993333;">unsigned</span> <span style="color: #993333;">int</span> bytes_read<span style="color: #339933;">;</span>
    HANDLE file<span style="color: #339933;">;</span>
    <span style="color: #993333;">unsigned</span> <span style="color: #993333;">char</span> buffer<span style="color: #009900;">&#91;</span> JPEG_WIN32_SRC_MGR_BUFFER_SIZE <span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> jpeg_win32_src_mgr<span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Then we define several callback functions which are used by the libjpeg API to retrieve data from our data source.</p>
<p>The win32_init_source() callback is a no-op since we perform initialization before handing over the source manager to the library.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">static</span> <span style="color: #993333;">void</span> win32_init_source<span style="color: #009900;">&#40;</span> j_decompress_ptr cinfo <span style="color: #009900;">&#41;</span> 
<span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Function win32_fill_input_buffer() is called when the library has consumed the entire data in the buffer. The return value should always be TRUE if no I/O suspension is needed. There is one peculiarity when handling truncated image files. To prevent the library to request further data on EOF, the recommendation is to insert an EOI marker to signal end of file.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">static</span> boolean win32_fill_input_buffer<span style="color: #009900;">&#40;</span> j_decompress_ptr cinfo <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    jpeg_win32_src_mgr<span style="color: #339933;">*</span> src <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>jpeg_win32_src_mgr<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>cinfo<span style="color: #339933;">-&gt;</span>src<span style="color: #339933;">;</span>
    DWORD bytes_read <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> TRUE <span style="color: #339933;">!=</span> ReadFile<span style="color: #009900;">&#40;</span> src<span style="color: #339933;">-&gt;</span>file<span style="color: #339933;">,</span> src<span style="color: #339933;">-&gt;</span>buffer<span style="color: #339933;">,</span> JPEG_WIN32_SRC_MGR_BUFFER_SIZE<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>bytes_read<span style="color: #339933;">,</span> NULL <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
        ERREXIT<span style="color: #009900;">&#40;</span> cinfo<span style="color: #339933;">,</span> JERR_FILE_READ <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    src<span style="color: #339933;">-&gt;</span>mgr.<span style="color: #202020;">next_input_byte</span> <span style="color: #339933;">=</span> src<span style="color: #339933;">-&gt;</span>buffer<span style="color: #339933;">;</span>
    src<span style="color: #339933;">-&gt;</span>mgr.<span style="color: #202020;">bytes_in_buffer</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">size_t</span><span style="color: #009900;">&#41;</span>bytes_read<span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000dd;">0</span> <span style="color: #339933;">==</span> src<span style="color: #339933;">-&gt;</span>mgr.<span style="color: #202020;">bytes_in_buffer</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #808080; font-style: italic;">/* The image file is truncated. We insert EOI marker to tell the library to stop processing. */</span>
        src<span style="color: #339933;">-&gt;</span>buffer<span style="color: #009900;">&#91;</span> <span style="color: #0000dd;">0</span> <span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>JOCTET<span style="color: #009900;">&#41;</span><span style="color: #208080;">0xFF</span><span style="color: #339933;">;</span>
        src<span style="color: #339933;">-&gt;</span>buffer<span style="color: #009900;">&#91;</span> <span style="color: #0000dd;">1</span> <span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>JOCTET<span style="color: #009900;">&#41;</span>JPEG_EOI<span style="color: #339933;">;</span>
        src<span style="color: #339933;">-&gt;</span>mgr.<span style="color: #202020;">bytes_in_buffer</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> TRUE<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The win32_skip_input_data() callback is called when the library wants to skip over some unnecessary data such as APP markers.<br />
It is valid for the function to leave 0 bytes in the input buffer, in which case win32_fill_input_buffer() will be called.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">static</span> <span style="color: #993333;">void</span> win32_skip_input_data<span style="color: #009900;">&#40;</span> j_decompress_ptr cinfo<span style="color: #339933;">,</span> <span style="color: #993333;">long</span> num_bytes <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    jpeg_win32_src_mgr<span style="color: #339933;">*</span> src <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>jpeg_win32_src_mgr<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>cinfo<span style="color: #339933;">-&gt;</span>src<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #0000dd;">1</span> <span style="color: #339933;">&gt;</span> num_bytes <span style="color: #009900;">&#41;</span>
        <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> num_bytes <span style="color: #339933;">&lt;=</span> src<span style="color: #339933;">-&gt;</span>mgr.<span style="color: #202020;">bytes_in_buffer</span> <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        src<span style="color: #339933;">-&gt;</span>mgr.<span style="color: #202020;">next_input_byte</span> <span style="color: #339933;">+=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">size_t</span><span style="color: #009900;">&#41;</span>num_bytes<span style="color: #339933;">;</span>
        src<span style="color: #339933;">-&gt;</span>mgr.<span style="color: #202020;">bytes_in_buffer</span> <span style="color: #339933;">-=</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">size_t</span><span style="color: #009900;">&#41;</span>num_bytes<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span>
    <span style="color: #009900;">&#123;</span>
        LARGE_INTEGER offset<span style="color: #339933;">;</span>
        offset.<span style="color: #202020;">QuadPart</span> <span style="color: #339933;">=</span> num_bytes <span style="color: #339933;">-</span> <span style="color: #009900;">&#40;</span><span style="color: #993333;">long</span><span style="color: #009900;">&#41;</span>src<span style="color: #339933;">-&gt;</span>mgr.<span style="color: #202020;">bytes_in_buffer</span><span style="color: #339933;">;</span>
        src<span style="color: #339933;">-&gt;</span>mgr.<span style="color: #202020;">bytes_in_buffer</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
        DWORD new_pos <span style="color: #339933;">=</span> SetFilePointer<span style="color: #009900;">&#40;</span> src<span style="color: #339933;">-&gt;</span>file<span style="color: #339933;">,</span> offset.<span style="color: #202020;">LowPart</span><span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>offset.<span style="color: #202020;">HighPart</span><span style="color: #339933;">,</span> FILE_CURRENT <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> INVALID_SET_FILE_POINTER <span style="color: #339933;">==</span> new_pos <span style="color: #009900;">&#41;</span>
            ERREXIT<span style="color: #009900;">&#40;</span> cinfo<span style="color: #339933;">,</span> JERR_FILE_READ <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>The win32_term_source() callback ensures there are no open handles left when the library is done processing.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">static</span> <span style="color: #993333;">void</span> win32_term_source<span style="color: #009900;">&#40;</span> j_decompress_ptr cinfo <span style="color: #009900;">&#41;</span> 
<span style="color: #009900;">&#123;</span>
    jpeg_win32_src_mgr<span style="color: #339933;">*</span> src <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>jpeg_win32_src_mgr<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>cinfo<span style="color: #339933;">-&gt;</span>src<span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> INVALID_HANDLE_VALUE <span style="color: #339933;">!=</span> src<span style="color: #339933;">-&gt;</span>file  <span style="color: #339933;">&amp;&amp;</span> NULL <span style="color: #339933;">!=</span> src<span style="color: #339933;">-&gt;</span>file <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        CloseHandle<span style="color: #009900;">&#40;</span> src<span style="color: #339933;">-&gt;</span>file <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        src<span style="color: #339933;">-&gt;</span>file <span style="color: #339933;">=</span> NULL<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Our new source manager is initialized via a call to jpeg_win32_src(), which will open the file and provide references to the callbacks defined above.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">static</span> <span style="color: #993333;">void</span> jpeg_win32_src<span style="color: #009900;">&#40;</span> j_decompress_ptr cinfo<span style="color: #339933;">,</span> jpeg_win32_src_mgr <span style="color: #339933;">*</span> src<span style="color: #339933;">,</span> PCTSTR filename <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    src<span style="color: #339933;">-&gt;</span>file <span style="color: #339933;">=</span> CreateFile<span style="color: #009900;">&#40;</span> filename<span style="color: #339933;">,</span> GENERIC_READ<span style="color: #339933;">,</span> FILE_SHARE_READ<span style="color: #339933;">,</span> NULL<span style="color: #339933;">,</span> OPEN_EXISTING<span style="color: #339933;">,</span> FILE_ATTRIBUTE_NORMAL<span style="color: #339933;">,</span> NULL <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> INVALID_HANDLE_VALUE <span style="color: #339933;">==</span> src<span style="color: #339933;">-&gt;</span>file <span style="color: #009900;">&#41;</span>
        ERREXIT<span style="color: #009900;">&#40;</span> cinfo<span style="color: #339933;">,</span> JERR_FILE_READ <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    src<span style="color: #339933;">-&gt;</span>bytes_read <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
    src<span style="color: #339933;">-&gt;</span>mgr.<span style="color: #202020;">init_source</span> <span style="color: #339933;">=</span> win32_init_source<span style="color: #339933;">;</span>
    src<span style="color: #339933;">-&gt;</span>mgr.<span style="color: #202020;">fill_input_buffer</span> <span style="color: #339933;">=</span> win32_fill_input_buffer<span style="color: #339933;">;</span>
    src<span style="color: #339933;">-&gt;</span>mgr.<span style="color: #202020;">skip_input_data</span> <span style="color: #339933;">=</span> win32_skip_input_data<span style="color: #339933;">;</span>
    src<span style="color: #339933;">-&gt;</span>mgr.<span style="color: #202020;">resync_to_restart</span> <span style="color: #339933;">=</span> jpeg_resync_to_restart<span style="color: #339933;">;</span>
    src<span style="color: #339933;">-&gt;</span>mgr.<span style="color: #202020;">term_source</span> <span style="color: #339933;">=</span> win32_term_source<span style="color: #339933;">;</span>
    src<span style="color: #339933;">-&gt;</span>mgr.<span style="color: #202020;">bytes_in_buffer</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
    src<span style="color: #339933;">-&gt;</span>mgr.<span style="color: #202020;">next_input_byte</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>JOCTET<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>src<span style="color: #339933;">-&gt;</span>buffer<span style="color: #339933;">;</span>
    cinfo<span style="color: #339933;">-&gt;</span>src <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>jpeg_source_mgr<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>src<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Usage of the new Win32 source manager is simple:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="c" style="font-family:monospace;">jpeg_decompress_struct cinfo<span style="color: #339933;">;</span>
jpeg_win32_src_mgr src_data<span style="color: #339933;">;</span>
&nbsp;
jpeg_create_decompress<span style="color: #009900;">&#40;</span> <span style="color: #339933;">&amp;</span>cinfo <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
jpeg_win32_src<span style="color: #009900;">&#40;</span> <span style="color: #339933;">&amp;</span>cinfo<span style="color: #339933;">,</span> <span style="color: #339933;">&amp;</span>src_data<span style="color: #339933;">,</span> szFilename <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
jpeg_read_header<span style="color: #009900;">&#40;</span> <span style="color: #339933;">&amp;</span>cinfo<span style="color: #339933;">,</span> TRUE <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
<span style="color: #808080; font-style: italic;">/* ... */</span></pre></td></tr></table></div>

<p>It would be quite easy to modify the source manager code listed above to support reading from other Win32 sources such as named pipes, console buffer or I/O devices.</p>
<p>The entire file can be downloaded here: <a href="/wp-content/uploads/jpeg_win32_src.zip">jpeg_win32_src.h</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.christian-etter.de/?feed=rss2&#038;p=811</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Visual Studio 2012 Entity Data Model Wizard Annoyance</title>
		<link>http://www.christian-etter.de/?p=755</link>
		<comments>http://www.christian-etter.de/?p=755#comments</comments>
		<pubDate>Tue, 13 Nov 2012 19:02:14 +0000</pubDate>
		<dc:creator>Christian Etter</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Bug]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Entity Model]]></category>
		<category><![CDATA[Visual Studio 2012]]></category>

		<guid isPermaLink="false">http://www.christian-etter.de/?p=755</guid>
		<description><![CDATA[Just stumbled upon an annoyance in VS 2012 Entity data model wizard. I had implemented a class and then decided to add an Entity Model to the project. Since the model was supposed to implement storage functionality for the previously written class, it was named the same as the .cs file of that class (except [...]]]></description>
				<content:encoded><![CDATA[<p>Just stumbled upon an annoyance in VS 2012 Entity data model wizard. I had implemented a class and then decided to add an Entity Model to the project. Since the model was supposed to implement storage functionality for the previously written class, it was named the same as the .cs file of that class (except the .cs extension).<br />
It turned out that this setting will silently overrwrite the .cs file with an entity framework generated class. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.christian-etter.de/?feed=rss2&#038;p=755</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>.NET RSACryptoServiceProvider PEM + DER Support</title>
		<link>http://www.christian-etter.de/?p=771</link>
		<comments>http://www.christian-etter.de/?p=771#comments</comments>
		<pubDate>Wed, 03 Oct 2012 22:14:10 +0000</pubDate>
		<dc:creator>Christian Etter</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[CAPI]]></category>
		<category><![CDATA[CERT_PUBLIC_KEY_INFO]]></category>
		<category><![CDATA[CryptDecodeObjectEx]]></category>
		<category><![CDATA[CryptoAPI]]></category>
		<category><![CDATA[CryptStringToBinary]]></category>
		<category><![CDATA[CRYPT_STRING_BASE64HEADER]]></category>
		<category><![CDATA[DER]]></category>
		<category><![CDATA[ImportCspBlob]]></category>
		<category><![CDATA[P/Invoke]]></category>
		<category><![CDATA[PEM]]></category>
		<category><![CDATA[PKCS_7_ASN_ENCODING]]></category>
		<category><![CDATA[PKCS_RSA_PRIVATE_KEY]]></category>
		<category><![CDATA[Private Key]]></category>
		<category><![CDATA[Public Key]]></category>
		<category><![CDATA[RSACryptoServiceProvider]]></category>
		<category><![CDATA[RSA_CSP_PUBLICKEYBLOB]]></category>
		<category><![CDATA[System.Security.Cryptography]]></category>
		<category><![CDATA[Win32]]></category>
		<category><![CDATA[X509_ASN_ENCODING]]></category>
		<category><![CDATA[X509_PUBLIC_KEY_INFO]]></category>

		<guid isPermaLink="false">http://www.christian-etter.de/?p=771</guid>
		<description><![CDATA[In .NET, RSACryptoServiceProvider greatly simplifies common tasks associated with public/private keys, such as signing of data and verifying a signature. Unfortunately, there is only a single format (proprietary XML) available for importing and exporting public/private key data. The two widely spread formats for key exchange, PEM and DER are not supported, which limits the usability [...]]]></description>
				<content:encoded><![CDATA[<p>In .NET, RSACryptoServiceProvider greatly simplifies common tasks associated with public/private keys, such as signing of data and verifying a signature.<br />
Unfortunately, there is only a single format (proprietary XML) available for importing and exporting public/private key data.<br />
The two widely spread formats for key exchange, PEM and DER are not supported, which limits the usability of the class when working with different kinds of public key APIs.</p>
<p>There are a few workarounds though:</p>
<ul>
<li>Write your own .NET code to support the desired formats, which might not be that simple, as the following <a href="http://pumka.net/2009/12/19/reading-writing-and-converting-rsa-keys-in-pem-der-publickeyblob-and-privatekeyblob-formats/" title="Reading, writing and converting RSA keys in PEM, DER, PUBLICKEYBLOB and PRIVATEKEYBLOB formats">blog post</a> suggests.</li>
<li>Use a third party component, such as the <a href="http://www.bouncycastle.org/docs/pkixdocs1.5on/org/bouncycastle/openssl/PEMReader.html" title="PEMReader (Bouncy Castle Library 1.47 API Specification):" target="_blank">PEMReader</a> class of the Bouncy Castle Library (Java/C# port).</li>
<li>Leverage Microsoft Crypto API (CAPI), which provides support for opening and converting a variety of different public/private key file formats. Here I would like to demonstrate  how this can be achieved by means of simple extension methods to the <a href="http://msdn.microsoft.com/en-us/library/system.security.cryptography.rsacryptoserviceprovider.aspx" title="MSDN: RSACryptoServiceProvider Class (System.Security.Cryptography)" target="_blank">RSACryptoServiceProvider</a> class.</li>
</ul>
<p>Here is a sample RSA 1024 bit private key in PEM format:</p>
<pre>
-----BEGIN RSA PRIVATE KEY-----
MIICXgIBAAKBgQDwIqfvxEjqHu8048x4wJ5EId6ASAbWdH5fzgHxvew5kXqECMNc
XzRqDVnDVPQT41UeZs8HxouBE+ZA8DfnVlHwP4EIeigOUaqy0sseKpO71tupFU+2
LjpcF6O7cVuLjt6476iYfSyrssK4hnmzVYGZNz16OSR9z/SuTd8BhohG4QIDAQAB
AoGBAOmEmhEUrN9XU8D4IVfv4DhbQ1c2M8gKovYhjEx8J6LX8O9C4lAKmRrkfrzv
+Sb59EVLLtrd3b2ZD1lpAMQrciMwC5PAa8da/J++lR1VjM5GbzqKjGtfx3WQlzNE
1ZaZ2FSY8lAPMM4uLczyD79PJQBsGCcx3KDJRR5ENp6an5cRAkEA/m1FEqol/KKh
xOyGsK4GVuansBXhrAgpwMlYLT+vF0gy1jzYQDNNQXzeQFYH6gZY66RTYFl3JPNL
8KXLyhwDLQJBAPGew6xkLBoYi4IO9I+NP/gIHzSiQeEl2OxZsgZiz0Yh5E9ndwMr
87jTX/4ZBwNlDC0E+MXsJpMSvTFNpw4rcwUCQQC5FU5JLKOjq79YnOPChWYxM2vL
Ka/YULvm9dGCYTCDFE9/EBYUZf2OZULctHjfYqyvBwRsM8j7hU26CzI7nbMlAkAA
kVjwXMPlw80AHzzf4XsXAB3ip8bz2nzqAUPz0+OczJOWxC15am8GLij5leF4VpJy
wKI9BNMKYW7kYMRVujBpAkEA7gQ8MGqjjrCAfOzrrC9ZuVdGRfEjUEdHMqiF+js7
XNBvnT5lBznUOd+eta6CGo7S5hjU7D3CEzmVGQfxUsRZ1w==
-----END RSA PRIVATE KEY-----
</pre>
<p>This is the corresponding public key in textual PEM representation:</p>
<pre>
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDwIqfvxEjqHu8048x4wJ5EId6A
SAbWdH5fzgHxvew5kXqECMNcXzRqDVnDVPQT41UeZs8HxouBE+ZA8DfnVlHwP4EI
eigOUaqy0sseKpO71tupFU+2LjpcF6O7cVuLjt6476iYfSyrssK4hnmzVYGZNz16
OSR9z/SuTd8BhohG4QIDAQAB
-----END PUBLIC KEY-----
</pre>
<p>We cannot load these keys into the RSACryptoServiceProvider directly. However the class supports an import method which is compatible with CryptoAPI: <a href="http://msdn.microsoft.com/en-us/library/system.security.cryptography.rsacryptoserviceprovider.importcspblob.aspx" title="MSDN: RSACryptoServiceProvider.ImportCspBlob Method (System.Security.Cryptography)" target="_blank">ImportCspBlob()</a>.<br />
With CAPI in return, we can do all the heavy lifting including format conversion which the .NET Framework does not support.<br />
For public keys, the conversion process for PEM (string) key data requires the following steps:</p>
<ol>
<li>Converting a public key in PEM string format into DER representation.

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;">CryptStringToBinaryA<span style="color: #008000;">&#40;</span> sPEM, <span style="color: #008000;">&#40;</span>UInt32<span style="color: #008000;">&#41;</span>sPEM<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span>, CRYPT_STRING_FLAGS<span style="color: #008000;">.</span><span style="color: #0000FF;">CRYPT_STRING_BASE64HEADER</span>, IntPtr<span style="color: #008000;">.</span><span style="color: #0000FF;">Zero</span>, <span style="color: #0600FF; font-weight: bold;">ref</span> dwBinarySize, <span style="color: #0600FF; font-weight: bold;">out</span> dwSkip, <span style="color: #0600FF; font-weight: bold;">out</span> dwFlags <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span></pre></td></tr></table></div>

</li>
<li>Retrieve the key through a <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa377463%28v=vs.85%29.aspx" title="MSDN: CERT_PUBLIC_KEY_INFO structure (Windows)" target="_blank">CERT_PUBLIC_KEY_INFO</a> struct.

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;">CryptDecodeObjectEx<span style="color: #008000;">&#40;</span> CRYPT_ENCODING_FLAGS<span style="color: #008000;">.</span><span style="color: #0000FF;">X509_ASN_ENCODING</span> <span style="color: #008000;">|</span> CRYPT_ENCODING_FLAGS<span style="color: #008000;">.</span><span style="color: #0000FF;">PKCS_7_ASN_ENCODING</span>, <span style="color: #008000;">new</span> IntPtr<span style="color: #008000;">&#40;</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&#41;</span>CRYPT_OUTPUT_TYPES<span style="color: #008000;">.</span><span style="color: #0000FF;">X509_PUBLIC_KEY_INFO</span> <span style="color: #008000;">&#41;</span>, DERData, <span style="color: #008000;">&#40;</span>UInt32<span style="color: #008000;">&#41;</span>DERData<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span>, CRYPT_DECODE_FLAGS<span style="color: #008000;">.</span><span style="color: #0000FF;">CRYPT_DECODE_ALLOC_FLAG</span>, IntPtr<span style="color: #008000;">.</span><span style="color: #0000FF;">Zero</span>, <span style="color: #0600FF; font-weight: bold;">ref</span> pCertPublicKeyInfo, <span style="color: #0600FF; font-weight: bold;">out</span> dwCertPublicKeyInfoSize <span style="color: #008000;">&#41;</span></pre></td></tr></table></div>

</li>
<li>Convert the returned RSA key into a <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa378145%28v=vs.85%29.aspx" title="MSDN: Constants for CryptEncodeObject and CryptDecodeObject (Windows)" target="_blank">Diffie-Hellman Version 3 Public Key BLOBs or DSS Version 3 Public Key BLOBs</a> struct:

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;">CryptDecodeObjectEx<span style="color: #008000;">&#40;</span> CRYPT_ENCODING_FLAGS<span style="color: #008000;">.</span><span style="color: #0000FF;">X509_ASN_ENCODING</span> <span style="color: #008000;">|</span> CRYPT_ENCODING_FLAGS<span style="color: #008000;">.</span><span style="color: #0000FF;">PKCS_7_ASN_ENCODING</span>, <span style="color: #008000;">new</span> IntPtr<span style="color: #008000;">&#40;</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&#41;</span>CRYPT_OUTPUT_TYPES<span style="color: #008000;">.</span><span style="color: #0000FF;">RSA_CSP_PUBLICKEYBLOB</span> <span style="color: #008000;">&#41;</span>, RSAData, <span style="color: #008000;">&#40;</span>UInt32<span style="color: #008000;">&#41;</span>RSAData<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span>, CRYPT_DECODE_FLAGS<span style="color: #008000;">.</span><span style="color: #0000FF;">CRYPT_DECODE_ALLOC_FLAG</span>, IntPtr<span style="color: #008000;">.</span><span style="color: #0000FF;">Zero</span>, <span style="color: #0600FF; font-weight: bold;">ref</span> pCertPublicKeyBlob, <span style="color: #0600FF; font-weight: bold;">out</span> dwCertPublicKeyBlobSize <span style="color: #008000;">&#41;</span></pre></td></tr></table></div>

</li>
<li>Call RSACryptoServiceProvider.ImportCspBlob() for the resulting binary data.</li>
</ol>
<p>For private keys, the following steps are required:</p>
<ol>
<li>Converting a private key in PEM string format into DER representation.

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;">CryptStringToBinaryA<span style="color: #008000;">&#40;</span> sPEM, <span style="color: #008000;">&#40;</span>UInt32<span style="color: #008000;">&#41;</span>sPEM<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span>, CRYPT_STRING_FLAGS<span style="color: #008000;">.</span><span style="color: #0000FF;">CRYPT_STRING_BASE64HEADER</span>, IntPtr<span style="color: #008000;">.</span><span style="color: #0000FF;">Zero</span>, <span style="color: #0600FF; font-weight: bold;">ref</span> dwBinarySize, <span style="color: #0600FF; font-weight: bold;">out</span> dwSkip, <span style="color: #0600FF; font-weight: bold;">out</span> dwFlags <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span></pre></td></tr></table></div>

</li>
<li>For private keys, retrieve a pointer to an <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa378145%28v=vs.85%29.aspx" title="Constants for CryptEncodeObject and CryptDecodeObject (Windows)" target="_blank">RSA private key BLOB</a> directly:

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;">CryptDecodeObjectEx<span style="color: #008000;">&#40;</span> CRYPT_ENCODING_FLAGS<span style="color: #008000;">.</span><span style="color: #0000FF;">X509_ASN_ENCODING</span> <span style="color: #008000;">|</span> CRYPT_ENCODING_FLAGS<span style="color: #008000;">.</span><span style="color: #0000FF;">PKCS_7_ASN_ENCODING</span>, <span style="color: #008000;">new</span> IntPtr<span style="color: #008000;">&#40;</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&#41;</span>CRYPT_OUTPUT_TYPES<span style="color: #008000;">.</span><span style="color: #0000FF;">PKCS_RSA_PRIVATE_KEY</span> <span style="color: #008000;">&#41;</span>, DERData, <span style="color: #008000;">&#40;</span>UInt32<span style="color: #008000;">&#41;</span>DERData<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span>, CRYPT_DECODE_FLAGS<span style="color: #008000;">.</span><span style="color: #0000FF;">CRYPT_DECODE_ALLOC_FLAG</span>, IntPtr<span style="color: #008000;">.</span><span style="color: #0000FF;">Zero</span>, <span style="color: #0600FF; font-weight: bold;">ref</span> pRSAPrivateKeyBlob, <span style="color: #0600FF; font-weight: bold;">out</span> pRSAPrivateKeyBlobSize <span style="color: #008000;">&#41;</span></pre></td></tr></table></div>

</li>
<li>Call RSACryptoServiceProvider.ImportCspBlob() for the resulting binary data.</li>
</ol>
<p>If you wrap the above logic into extension methods for RSACryptoServiceProvider, importing keys in PEM format and signing data can be done with a few lines of code:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// -----BEGIN RSA PRIVATE KEY-----...-----END RSA PRIVATE KEY-----</span>
<span style="color: #6666cc; font-weight: bold;">string</span> sPrivateKeyPEM <span style="color: #008000;">=</span> File<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadAllText</span><span style="color: #008000;">&#40;</span> <span style="color: #666666;">&quot;PrivateKey.pem&quot;</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span> RSACryptoServiceProvider rsa <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> RSACryptoServiceProvider<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    rsa<span style="color: #008000;">.</span><span style="color: #0000FF;">PersistKeyInCsp</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
    rsa<span style="color: #008000;">.</span><span style="color: #0000FF;">LoadPrivateKeyPEM</span><span style="color: #008000;">&#40;</span> sPrivateKeyPEM <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span> SHA1CryptoServiceProvider sha1 <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SHA1CryptoServiceProvider<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span>
        signature <span style="color: #008000;">=</span> rsa<span style="color: #008000;">.</span><span style="color: #0000FF;">SignData</span><span style="color: #008000;">&#40;</span> dataToSign, sha1 <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Verifying the same signature also becomes easy:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// -----BEGIN PUBLIC KEY-----...-----END PUBLIC KEY-----</span>
<span style="color: #6666cc; font-weight: bold;">string</span> sPublicKeyPEM <span style="color: #008000;">=</span> File<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadAllText</span><span style="color: #008000;">&#40;</span> <span style="color: #666666;">&quot;PublicKey.pem&quot;</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span> RSACryptoServiceProvider rsa <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> RSACryptoServiceProvider<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    rsa<span style="color: #008000;">.</span><span style="color: #0000FF;">PersistKeyInCsp</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
    rsa<span style="color: #008000;">.</span><span style="color: #0000FF;">LoadPublicKeyPEM</span><span style="color: #008000;">&#40;</span> sPublicKeyPEM <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span> SHA1CryptoServiceProvider sha1 <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SHA1CryptoServiceProvider<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span>
        bVerifyResultOriginal <span style="color: #008000;">=</span> rsa<span style="color: #008000;">.</span><span style="color: #0000FF;">VerifyData</span><span style="color: #008000;">&#40;</span> dataToSign, sha1, signature <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Here is a cut-and-paste solution based on an extension class to RSACryptoServiceProvider:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/*********************************************************************************
 * Copyright (c) 2013, Christian Etter info at christian-etter dot de
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer. 
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS &quot;AS IS&quot; AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *********************************************************************************/</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.ComponentModel</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Runtime.InteropServices</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Security.Cryptography</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;Extension method for initializing a RSACryptoServiceProvider from PEM data string.&lt;/summary&gt;</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">class</span> RSACryptoServiceProviderExtension
<span style="color: #008000;">&#123;</span>
    <span style="color: #008080;">#region Methods</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;Extension method which initializes an RSACryptoServiceProvider from a DER public key blob.&lt;/summary&gt;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> LoadPublicKeyDER<span style="color: #008000;">&#40;</span> <span style="color: #0600FF; font-weight: bold;">this</span> RSACryptoServiceProvider provider, <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> DERData <span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> RSAData <span style="color: #008000;">=</span> RSACryptoServiceProviderExtension<span style="color: #008000;">.</span><span style="color: #0000FF;">GetRSAFromDER</span><span style="color: #008000;">&#40;</span> DERData <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> publicKeyBlob <span style="color: #008000;">=</span> RSACryptoServiceProviderExtension<span style="color: #008000;">.</span><span style="color: #0000FF;">GetPublicKeyBlobFromRSA</span><span style="color: #008000;">&#40;</span> RSAData <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        provider<span style="color: #008000;">.</span><span style="color: #0000FF;">ImportCspBlob</span><span style="color: #008000;">&#40;</span> publicKeyBlob <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;Extension method which initializes an RSACryptoServiceProvider from a DER private key blob.&lt;/summary&gt;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> LoadPrivateKeyDER<span style="color: #008000;">&#40;</span> <span style="color: #0600FF; font-weight: bold;">this</span> RSACryptoServiceProvider provider, <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> DERData <span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> privateKeyBlob <span style="color: #008000;">=</span> RSACryptoServiceProviderExtension<span style="color: #008000;">.</span><span style="color: #0000FF;">GetPrivateKeyDER</span><span style="color: #008000;">&#40;</span> DERData <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        provider<span style="color: #008000;">.</span><span style="color: #0000FF;">ImportCspBlob</span><span style="color: #008000;">&#40;</span> privateKeyBlob <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;Extension method which initializes an RSACryptoServiceProvider from a PEM public key string.&lt;/summary&gt;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> LoadPublicKeyPEM<span style="color: #008000;">&#40;</span> <span style="color: #0600FF; font-weight: bold;">this</span> RSACryptoServiceProvider provider, <span style="color: #6666cc; font-weight: bold;">string</span> sPEM <span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> DERData <span style="color: #008000;">=</span> RSACryptoServiceProviderExtension<span style="color: #008000;">.</span><span style="color: #0000FF;">GetDERFromPEM</span><span style="color: #008000;">&#40;</span> sPEM <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        RSACryptoServiceProviderExtension<span style="color: #008000;">.</span><span style="color: #0000FF;">LoadPublicKeyDER</span><span style="color: #008000;">&#40;</span> provider, DERData <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;Extension method which initializes an RSACryptoServiceProvider from a PEM private key string.&lt;/summary&gt;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> LoadPrivateKeyPEM<span style="color: #008000;">&#40;</span> <span style="color: #0600FF; font-weight: bold;">this</span> RSACryptoServiceProvider provider, <span style="color: #6666cc; font-weight: bold;">string</span> sPEM <span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> DERData <span style="color: #008000;">=</span> RSACryptoServiceProviderExtension<span style="color: #008000;">.</span><span style="color: #0000FF;">GetDERFromPEM</span><span style="color: #008000;">&#40;</span> sPEM <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        RSACryptoServiceProviderExtension<span style="color: #008000;">.</span><span style="color: #0000FF;">LoadPrivateKeyDER</span><span style="color: #008000;">&#40;</span> provider, DERData <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;Returns a public key blob from an RSA public key.&lt;/summary&gt;</span>
    <span style="color: #0600FF; font-weight: bold;">internal</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> GetPublicKeyBlobFromRSA<span style="color: #008000;">&#40;</span> <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> RSAData <span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> data <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
        UInt32 dwCertPublicKeyBlobSize <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span> RSACryptoServiceProviderExtension<span style="color: #008000;">.</span><span style="color: #0000FF;">CryptDecodeObject</span><span style="color: #008000;">&#40;</span> CRYPT_ENCODING_FLAGS<span style="color: #008000;">.</span><span style="color: #0000FF;">X509_ASN_ENCODING</span> <span style="color: #008000;">|</span> CRYPT_ENCODING_FLAGS<span style="color: #008000;">.</span><span style="color: #0000FF;">PKCS_7_ASN_ENCODING</span>,
            <span style="color: #008000;">new</span> IntPtr<span style="color: #008000;">&#40;</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&#41;</span>CRYPT_OUTPUT_TYPES<span style="color: #008000;">.</span><span style="color: #0000FF;">RSA_CSP_PUBLICKEYBLOB</span> <span style="color: #008000;">&#41;</span>, RSAData, <span style="color: #008000;">&#40;</span>UInt32<span style="color: #008000;">&#41;</span>RSAData<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span>, CRYPT_DECODE_FLAGS<span style="color: #008000;">.</span><span style="color: #0000FF;">NONE</span>,
            data, <span style="color: #0600FF; font-weight: bold;">ref</span> dwCertPublicKeyBlobSize <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            data <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span> dwCertPublicKeyBlobSize <span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span> <span style="color: #008000;">!</span>RSACryptoServiceProviderExtension<span style="color: #008000;">.</span><span style="color: #0000FF;">CryptDecodeObject</span><span style="color: #008000;">&#40;</span> CRYPT_ENCODING_FLAGS<span style="color: #008000;">.</span><span style="color: #0000FF;">X509_ASN_ENCODING</span> <span style="color: #008000;">|</span> CRYPT_ENCODING_FLAGS<span style="color: #008000;">.</span><span style="color: #0000FF;">PKCS_7_ASN_ENCODING</span>,
                <span style="color: #008000;">new</span> IntPtr<span style="color: #008000;">&#40;</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&#41;</span>CRYPT_OUTPUT_TYPES<span style="color: #008000;">.</span><span style="color: #0000FF;">RSA_CSP_PUBLICKEYBLOB</span> <span style="color: #008000;">&#41;</span>, RSAData, <span style="color: #008000;">&#40;</span>UInt32<span style="color: #008000;">&#41;</span>RSAData<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span>, CRYPT_DECODE_FLAGS<span style="color: #008000;">.</span><span style="color: #0000FF;">NONE</span>,
                data, <span style="color: #0600FF; font-weight: bold;">ref</span> dwCertPublicKeyBlobSize <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span>
                <span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> Win32Exception<span style="color: #008000;">&#40;</span> Marshal<span style="color: #008000;">.</span><span style="color: #0000FF;">GetLastWin32Error</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">else</span>
            <span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> Win32Exception<span style="color: #008000;">&#40;</span> Marshal<span style="color: #008000;">.</span><span style="color: #0000FF;">GetLastWin32Error</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> data<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;Converts DER binary format to a CAPI CRYPT_PRIVATE_KEY_INFO structure.&lt;/summary&gt;</span>
    <span style="color: #0600FF; font-weight: bold;">internal</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> GetPrivateKeyDER<span style="color: #008000;">&#40;</span> <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> DERData <span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> data <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
        UInt32 dwRSAPrivateKeyBlobSize <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
        IntPtr pRSAPrivateKeyBlob <span style="color: #008000;">=</span> IntPtr<span style="color: #008000;">.</span><span style="color: #0000FF;">Zero</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span> RSACryptoServiceProviderExtension<span style="color: #008000;">.</span><span style="color: #0000FF;">CryptDecodeObject</span><span style="color: #008000;">&#40;</span> CRYPT_ENCODING_FLAGS<span style="color: #008000;">.</span><span style="color: #0000FF;">X509_ASN_ENCODING</span> <span style="color: #008000;">|</span> CRYPT_ENCODING_FLAGS<span style="color: #008000;">.</span><span style="color: #0000FF;">PKCS_7_ASN_ENCODING</span>, <span style="color: #008000;">new</span> IntPtr<span style="color: #008000;">&#40;</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&#41;</span>CRYPT_OUTPUT_TYPES<span style="color: #008000;">.</span><span style="color: #0000FF;">PKCS_RSA_PRIVATE_KEY</span> <span style="color: #008000;">&#41;</span>,
            DERData, <span style="color: #008000;">&#40;</span>UInt32<span style="color: #008000;">&#41;</span>DERData<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span>, CRYPT_DECODE_FLAGS<span style="color: #008000;">.</span><span style="color: #0000FF;">NONE</span>, data, <span style="color: #0600FF; font-weight: bold;">ref</span> dwRSAPrivateKeyBlobSize <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            data <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span> dwRSAPrivateKeyBlobSize <span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span> <span style="color: #008000;">!</span>RSACryptoServiceProviderExtension<span style="color: #008000;">.</span><span style="color: #0000FF;">CryptDecodeObject</span><span style="color: #008000;">&#40;</span> CRYPT_ENCODING_FLAGS<span style="color: #008000;">.</span><span style="color: #0000FF;">X509_ASN_ENCODING</span> <span style="color: #008000;">|</span> CRYPT_ENCODING_FLAGS<span style="color: #008000;">.</span><span style="color: #0000FF;">PKCS_7_ASN_ENCODING</span>, <span style="color: #008000;">new</span> IntPtr<span style="color: #008000;">&#40;</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&#41;</span>CRYPT_OUTPUT_TYPES<span style="color: #008000;">.</span><span style="color: #0000FF;">PKCS_RSA_PRIVATE_KEY</span> <span style="color: #008000;">&#41;</span>,
                DERData, <span style="color: #008000;">&#40;</span>UInt32<span style="color: #008000;">&#41;</span>DERData<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span>, CRYPT_DECODE_FLAGS<span style="color: #008000;">.</span><span style="color: #0000FF;">NONE</span>, data, <span style="color: #0600FF; font-weight: bold;">ref</span> dwRSAPrivateKeyBlobSize <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span>
                <span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> Win32Exception<span style="color: #008000;">&#40;</span> Marshal<span style="color: #008000;">.</span><span style="color: #0000FF;">GetLastWin32Error</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">else</span>
            <span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> Win32Exception<span style="color: #008000;">&#40;</span> Marshal<span style="color: #008000;">.</span><span style="color: #0000FF;">GetLastWin32Error</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> data<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;Converts DER binary format to a CAPI CERT_PUBLIC_KEY_INFO structure containing an RSA key.&lt;/summary&gt;</span>
    <span style="color: #0600FF; font-weight: bold;">internal</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> GetRSAFromDER<span style="color: #008000;">&#40;</span> <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> DERData <span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> data <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
        <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> publicKey <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">;</span>
        CERT_PUBLIC_KEY_INFO info<span style="color: #008000;">;</span>
        UInt32 dwCertPublicKeyInfoSize <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
        IntPtr pCertPublicKeyInfo <span style="color: #008000;">=</span> IntPtr<span style="color: #008000;">.</span><span style="color: #0000FF;">Zero</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span> RSACryptoServiceProviderExtension<span style="color: #008000;">.</span><span style="color: #0000FF;">CryptDecodeObject</span><span style="color: #008000;">&#40;</span> CRYPT_ENCODING_FLAGS<span style="color: #008000;">.</span><span style="color: #0000FF;">X509_ASN_ENCODING</span> <span style="color: #008000;">|</span> CRYPT_ENCODING_FLAGS<span style="color: #008000;">.</span><span style="color: #0000FF;">PKCS_7_ASN_ENCODING</span>, <span style="color: #008000;">new</span> IntPtr<span style="color: #008000;">&#40;</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&#41;</span>CRYPT_OUTPUT_TYPES<span style="color: #008000;">.</span><span style="color: #0000FF;">X509_PUBLIC_KEY_INFO</span> <span style="color: #008000;">&#41;</span>,
            DERData, <span style="color: #008000;">&#40;</span>UInt32<span style="color: #008000;">&#41;</span>DERData<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span>, CRYPT_DECODE_FLAGS<span style="color: #008000;">.</span><span style="color: #0000FF;">NONE</span>, data, <span style="color: #0600FF; font-weight: bold;">ref</span> dwCertPublicKeyInfoSize <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            data <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span> dwCertPublicKeyInfoSize <span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span> RSACryptoServiceProviderExtension<span style="color: #008000;">.</span><span style="color: #0000FF;">CryptDecodeObject</span><span style="color: #008000;">&#40;</span> CRYPT_ENCODING_FLAGS<span style="color: #008000;">.</span><span style="color: #0000FF;">X509_ASN_ENCODING</span> <span style="color: #008000;">|</span> CRYPT_ENCODING_FLAGS<span style="color: #008000;">.</span><span style="color: #0000FF;">PKCS_7_ASN_ENCODING</span>, <span style="color: #008000;">new</span> IntPtr<span style="color: #008000;">&#40;</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&#41;</span>CRYPT_OUTPUT_TYPES<span style="color: #008000;">.</span><span style="color: #0000FF;">X509_PUBLIC_KEY_INFO</span> <span style="color: #008000;">&#41;</span>,
                DERData, <span style="color: #008000;">&#40;</span>UInt32<span style="color: #008000;">&#41;</span>DERData<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span>, CRYPT_DECODE_FLAGS<span style="color: #008000;">.</span><span style="color: #0000FF;">NONE</span>, data, <span style="color: #0600FF; font-weight: bold;">ref</span> dwCertPublicKeyInfoSize <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                GCHandle handle <span style="color: #008000;">=</span> GCHandle<span style="color: #008000;">.</span><span style="color: #0000FF;">Alloc</span><span style="color: #008000;">&#40;</span> data, GCHandleType<span style="color: #008000;">.</span><span style="color: #0000FF;">Pinned</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #0600FF; font-weight: bold;">try</span>
                <span style="color: #008000;">&#123;</span>
                    info <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span>CERT_PUBLIC_KEY_INFO<span style="color: #008000;">&#41;</span>Marshal<span style="color: #008000;">.</span><span style="color: #0000FF;">PtrToStructure</span><span style="color: #008000;">&#40;</span> handle<span style="color: #008000;">.</span><span style="color: #0000FF;">AddrOfPinnedObject</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, <span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span> CERT_PUBLIC_KEY_INFO <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                    publicKey <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span> info<span style="color: #008000;">.</span><span style="color: #0000FF;">PublicKey</span><span style="color: #008000;">.</span><span style="color: #0000FF;">cbData</span> <span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
                    Marshal<span style="color: #008000;">.</span><span style="color: #0000FF;">Copy</span><span style="color: #008000;">&#40;</span> info<span style="color: #008000;">.</span><span style="color: #0000FF;">PublicKey</span><span style="color: #008000;">.</span><span style="color: #0000FF;">pbData</span>, publicKey, <span style="color: #FF0000;">0</span>, publicKey<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #008000;">&#125;</span>
                <span style="color: #0600FF; font-weight: bold;">finally</span>
                <span style="color: #008000;">&#123;</span>
                    handle<span style="color: #008000;">.</span><span style="color: #0000FF;">Free</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                <span style="color: #008000;">&#125;</span>
            <span style="color: #008000;">&#125;</span>
            <span style="color: #0600FF; font-weight: bold;">else</span>
                <span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> Win32Exception<span style="color: #008000;">&#40;</span> Marshal<span style="color: #008000;">.</span><span style="color: #0000FF;">GetLastWin32Error</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #0600FF; font-weight: bold;">else</span>
            <span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> Win32Exception<span style="color: #008000;">&#40;</span> Marshal<span style="color: #008000;">.</span><span style="color: #0000FF;">GetLastWin32Error</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> publicKey<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;Extracts the binary data from a PEM file.&lt;/summary&gt;</span>
    <span style="color: #0600FF; font-weight: bold;">internal</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> GetDERFromPEM<span style="color: #008000;">&#40;</span> <span style="color: #6666cc; font-weight: bold;">string</span> sPEM <span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        UInt32 dwSkip, dwFlags<span style="color: #008000;">;</span>
        UInt32 dwBinarySize <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span> <span style="color: #008000;">!</span>RSACryptoServiceProviderExtension<span style="color: #008000;">.</span><span style="color: #0000FF;">CryptStringToBinary</span><span style="color: #008000;">&#40;</span> sPEM, <span style="color: #008000;">&#40;</span>UInt32<span style="color: #008000;">&#41;</span>sPEM<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span>, CRYPT_STRING_FLAGS<span style="color: #008000;">.</span><span style="color: #0000FF;">CRYPT_STRING_BASE64HEADER</span>, <span style="color: #0600FF; font-weight: bold;">null</span>, <span style="color: #0600FF; font-weight: bold;">ref</span> dwBinarySize, <span style="color: #0600FF; font-weight: bold;">out</span> dwSkip, <span style="color: #0600FF; font-weight: bold;">out</span> dwFlags <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span>
            <span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> Win32Exception<span style="color: #008000;">&#40;</span> Marshal<span style="color: #008000;">.</span><span style="color: #0000FF;">GetLastWin32Error</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> decodedData <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span> dwBinarySize <span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span> <span style="color: #008000;">!</span>RSACryptoServiceProviderExtension<span style="color: #008000;">.</span><span style="color: #0000FF;">CryptStringToBinary</span><span style="color: #008000;">&#40;</span> sPEM, <span style="color: #008000;">&#40;</span>UInt32<span style="color: #008000;">&#41;</span>sPEM<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span>, CRYPT_STRING_FLAGS<span style="color: #008000;">.</span><span style="color: #0000FF;">CRYPT_STRING_BASE64HEADER</span>, decodedData, <span style="color: #0600FF; font-weight: bold;">ref</span> dwBinarySize, <span style="color: #0600FF; font-weight: bold;">out</span> dwSkip, <span style="color: #0600FF; font-weight: bold;">out</span> dwFlags <span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span>
            <span style="color: #0600FF; font-weight: bold;">throw</span> <span style="color: #008000;">new</span> Win32Exception<span style="color: #008000;">&#40;</span> Marshal<span style="color: #008000;">.</span><span style="color: #0000FF;">GetLastWin32Error</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> decodedData<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008080;">#endregion Methods</span>
&nbsp;
    <span style="color: #008080;">#region P/Invoke Constants</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;Enumeration derived from Crypto API.&lt;/summary&gt;</span>
    <span style="color: #0600FF; font-weight: bold;">internal</span> <span style="color: #6666cc; font-weight: bold;">enum</span> CRYPT_ACQUIRE_CONTEXT_FLAGS <span style="color: #008000;">:</span> <span style="color: #6666cc; font-weight: bold;">uint</span>
    <span style="color: #008000;">&#123;</span>
        CRYPT_NEWKEYSET <span style="color: #008000;">=</span> 0x8,
        CRYPT_DELETEKEYSET <span style="color: #008000;">=</span> 0x10,
        CRYPT_MACHINE_KEYSET <span style="color: #008000;">=</span> 0x20,
        CRYPT_SILENT <span style="color: #008000;">=</span> 0x40,
        CRYPT_DEFAULT_CONTAINER_OPTIONAL <span style="color: #008000;">=</span> 0x80,
        CRYPT_VERIFYCONTEXT <span style="color: #008000;">=</span> 0xF0000000
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;Enumeration derived from Crypto API.&lt;/summary&gt;</span>
    <span style="color: #0600FF; font-weight: bold;">internal</span> <span style="color: #6666cc; font-weight: bold;">enum</span> CRYPT_PROVIDER_TYPE <span style="color: #008000;">:</span> <span style="color: #6666cc; font-weight: bold;">uint</span>
    <span style="color: #008000;">&#123;</span>
        PROV_RSA_FULL <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;Enumeration derived from Crypto API.&lt;/summary&gt;</span>
    <span style="color: #0600FF; font-weight: bold;">internal</span> <span style="color: #6666cc; font-weight: bold;">enum</span> CRYPT_DECODE_FLAGS <span style="color: #008000;">:</span> <span style="color: #6666cc; font-weight: bold;">uint</span>
    <span style="color: #008000;">&#123;</span>
        NONE <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span>,
        CRYPT_DECODE_ALLOC_FLAG <span style="color: #008000;">=</span> 0x8000
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;Enumeration derived from Crypto API.&lt;/summary&gt;</span>
    <span style="color: #0600FF; font-weight: bold;">internal</span> <span style="color: #6666cc; font-weight: bold;">enum</span> CRYPT_ENCODING_FLAGS <span style="color: #008000;">:</span> <span style="color: #6666cc; font-weight: bold;">uint</span>
    <span style="color: #008000;">&#123;</span>
        PKCS_7_ASN_ENCODING <span style="color: #008000;">=</span> 0x00010000,
        X509_ASN_ENCODING <span style="color: #008000;">=</span> 0x00000001,
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;Enumeration derived from Crypto API.&lt;/summary&gt;</span>
    <span style="color: #0600FF; font-weight: bold;">internal</span> <span style="color: #6666cc; font-weight: bold;">enum</span> CRYPT_OUTPUT_TYPES <span style="color: #008000;">:</span> <span style="color: #6666cc; font-weight: bold;">int</span>
    <span style="color: #008000;">&#123;</span>
        X509_PUBLIC_KEY_INFO <span style="color: #008000;">=</span> <span style="color: #FF0000;">8</span>,
        RSA_CSP_PUBLICKEYBLOB <span style="color: #008000;">=</span> <span style="color: #FF0000;">19</span>,
        PKCS_RSA_PRIVATE_KEY <span style="color: #008000;">=</span> <span style="color: #FF0000;">43</span>,
        PKCS_PRIVATE_KEY_INFO <span style="color: #008000;">=</span> <span style="color: #FF0000;">44</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;Enumeration derived from Crypto API.&lt;/summary&gt;</span>
    <span style="color: #0600FF; font-weight: bold;">internal</span> <span style="color: #6666cc; font-weight: bold;">enum</span> CRYPT_STRING_FLAGS <span style="color: #008000;">:</span> <span style="color: #6666cc; font-weight: bold;">uint</span>
    <span style="color: #008000;">&#123;</span>
        CRYPT_STRING_BASE64HEADER <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span>,
        CRYPT_STRING_BASE64 <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span>,
        CRYPT_STRING_BINARY <span style="color: #008000;">=</span> <span style="color: #FF0000;">2</span>,
        CRYPT_STRING_BASE64REQUESTHEADER <span style="color: #008000;">=</span> <span style="color: #FF0000;">3</span>,
        CRYPT_STRING_HEX <span style="color: #008000;">=</span> <span style="color: #FF0000;">4</span>,
        CRYPT_STRING_HEXASCII <span style="color: #008000;">=</span> <span style="color: #FF0000;">5</span>,
        CRYPT_STRING_BASE64_ANY <span style="color: #008000;">=</span> <span style="color: #FF0000;">6</span>,
        CRYPT_STRING_ANY <span style="color: #008000;">=</span> <span style="color: #FF0000;">7</span>,
        CRYPT_STRING_HEX_ANY <span style="color: #008000;">=</span> <span style="color: #FF0000;">8</span>,
        CRYPT_STRING_BASE64X509CRLHEADER <span style="color: #008000;">=</span> <span style="color: #FF0000;">9</span>,
        CRYPT_STRING_HEXADDR <span style="color: #008000;">=</span> <span style="color: #FF0000;">10</span>,
        CRYPT_STRING_HEXASCIIADDR <span style="color: #008000;">=</span> <span style="color: #FF0000;">11</span>,
        CRYPT_STRING_HEXRAW <span style="color: #008000;">=</span> <span style="color: #FF0000;">12</span>,
        CRYPT_STRING_NOCRLF <span style="color: #008000;">=</span> 0x40000000,
        CRYPT_STRING_NOCR <span style="color: #008000;">=</span> 0x80000000
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008080;">#endregion P/Invoke Constants</span>
&nbsp;
    <span style="color: #008080;">#region P/Invoke Structures</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;Structure from Crypto API.&lt;/summary&gt;</span>
    <span style="color: #008000;">&#91;</span>StructLayout<span style="color: #008000;">&#40;</span> LayoutKind<span style="color: #008000;">.</span><span style="color: #0000FF;">Sequential</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">internal</span> <span style="color: #6666cc; font-weight: bold;">struct</span> CRYPT_OBJID_BLOB
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">internal</span> UInt32 cbData<span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">internal</span> IntPtr pbData<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;Structure from Crypto API.&lt;/summary&gt;</span>
    <span style="color: #008000;">&#91;</span>StructLayout<span style="color: #008000;">&#40;</span> LayoutKind<span style="color: #008000;">.</span><span style="color: #0000FF;">Sequential</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">internal</span> <span style="color: #6666cc; font-weight: bold;">struct</span> CRYPT_ALGORITHM_IDENTIFIER
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">internal</span> IntPtr pszObjId<span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">internal</span> CRYPT_OBJID_BLOB Parameters<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;Structure from Crypto API.&lt;/summary&gt;</span>
    <span style="color: #008000;">&#91;</span>StructLayout<span style="color: #008000;">&#40;</span> LayoutKind<span style="color: #008000;">.</span><span style="color: #0000FF;">Sequential</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
    <span style="color: #6666cc; font-weight: bold;">struct</span> CRYPT_BIT_BLOB
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">internal</span> UInt32 cbData<span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">internal</span> IntPtr pbData<span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">internal</span> UInt32 cUnusedBits<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;Structure from Crypto API.&lt;/summary&gt;</span>
    <span style="color: #008000;">&#91;</span>StructLayout<span style="color: #008000;">&#40;</span> LayoutKind<span style="color: #008000;">.</span><span style="color: #0000FF;">Sequential</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
    <span style="color: #6666cc; font-weight: bold;">struct</span> CERT_PUBLIC_KEY_INFO
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">internal</span> CRYPT_ALGORITHM_IDENTIFIER Algorithm<span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">internal</span> CRYPT_BIT_BLOB PublicKey<span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #008080;">#endregion P/Invoke Structures</span>
&nbsp;
    <span style="color: #008080;">#region P/Invoke Functions</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;Function for Crypto API.&lt;/summary&gt;</span>
    <span style="color: #008000;">&#91;</span>DllImport<span style="color: #008000;">&#40;</span> <span style="color: #666666;">&quot;advapi32.dll&quot;</span>, SetLastError <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
    <span style="color: #008000;">&#91;</span><span style="color: #0600FF; font-weight: bold;">return</span><span style="color: #008000;">:</span> MarshalAs<span style="color: #008000;">&#40;</span> UnmanagedType<span style="color: #008000;">.</span><span style="color: #6666cc; font-weight: bold;">Bool</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">internal</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">extern</span> <span style="color: #6666cc; font-weight: bold;">bool</span> CryptDestroyKey<span style="color: #008000;">&#40;</span> IntPtr hKey <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;Function for Crypto API.&lt;/summary&gt;</span>
    <span style="color: #008000;">&#91;</span>DllImport<span style="color: #008000;">&#40;</span> <span style="color: #666666;">&quot;advapi32.dll&quot;</span>, SetLastError <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
    <span style="color: #008000;">&#91;</span><span style="color: #0600FF; font-weight: bold;">return</span><span style="color: #008000;">:</span> MarshalAs<span style="color: #008000;">&#40;</span> UnmanagedType<span style="color: #008000;">.</span><span style="color: #6666cc; font-weight: bold;">Bool</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">internal</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">extern</span> <span style="color: #6666cc; font-weight: bold;">bool</span> CryptImportKey<span style="color: #008000;">&#40;</span> IntPtr hProv, <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> pbKeyData, UInt32 dwDataLen, IntPtr hPubKey, UInt32 dwFlags, <span style="color: #0600FF; font-weight: bold;">ref</span> IntPtr hKey <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;Function for Crypto API.&lt;/summary&gt;</span>
    <span style="color: #008000;">&#91;</span>DllImport<span style="color: #008000;">&#40;</span> <span style="color: #666666;">&quot;advapi32.dll&quot;</span>, SetLastError <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
    <span style="color: #008000;">&#91;</span><span style="color: #0600FF; font-weight: bold;">return</span><span style="color: #008000;">:</span> MarshalAs<span style="color: #008000;">&#40;</span> UnmanagedType<span style="color: #008000;">.</span><span style="color: #6666cc; font-weight: bold;">Bool</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">internal</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">extern</span> <span style="color: #6666cc; font-weight: bold;">bool</span> CryptReleaseContext<span style="color: #008000;">&#40;</span> IntPtr hProv, Int32 dwFlags <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;Function for Crypto API.&lt;/summary&gt;</span>
    <span style="color: #008000;">&#91;</span>DllImport<span style="color: #008000;">&#40;</span> <span style="color: #666666;">&quot;advapi32.dll&quot;</span>, CharSet <span style="color: #008000;">=</span> CharSet<span style="color: #008000;">.</span><span style="color: #0000FF;">Auto</span>, SetLastError <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
    <span style="color: #008000;">&#91;</span><span style="color: #0600FF; font-weight: bold;">return</span><span style="color: #008000;">:</span> MarshalAs<span style="color: #008000;">&#40;</span> UnmanagedType<span style="color: #008000;">.</span><span style="color: #6666cc; font-weight: bold;">Bool</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">internal</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">extern</span> <span style="color: #6666cc; font-weight: bold;">bool</span> CryptAcquireContext<span style="color: #008000;">&#40;</span> <span style="color: #0600FF; font-weight: bold;">ref</span> IntPtr hProv, <span style="color: #6666cc; font-weight: bold;">string</span> pszContainer, <span style="color: #6666cc; font-weight: bold;">string</span> pszProvider, CRYPT_PROVIDER_TYPE dwProvType, CRYPT_ACQUIRE_CONTEXT_FLAGS dwFlags <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;Function from Crypto API.&lt;/summary&gt;</span>
    <span style="color: #008000;">&#91;</span>DllImport<span style="color: #008000;">&#40;</span> <span style="color: #666666;">&quot;crypt32.dll&quot;</span>, SetLastError <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span>, CharSet <span style="color: #008000;">=</span> CharSet<span style="color: #008000;">.</span><span style="color: #0000FF;">Auto</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
    <span style="color: #008000;">&#91;</span><span style="color: #0600FF; font-weight: bold;">return</span><span style="color: #008000;">:</span> MarshalAs<span style="color: #008000;">&#40;</span> UnmanagedType<span style="color: #008000;">.</span><span style="color: #6666cc; font-weight: bold;">Bool</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">internal</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">extern</span> <span style="color: #6666cc; font-weight: bold;">bool</span> CryptStringToBinary<span style="color: #008000;">&#40;</span> <span style="color: #6666cc; font-weight: bold;">string</span> sPEM, UInt32 sPEMLength, CRYPT_STRING_FLAGS dwFlags, <span style="color: #008000;">&#91;</span><span style="color: #0600FF; font-weight: bold;">Out</span><span style="color: #008000;">&#93;</span> <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> pbBinary, <span style="color: #0600FF; font-weight: bold;">ref</span> UInt32 pcbBinary, <span style="color: #0600FF; font-weight: bold;">out</span> UInt32 pdwSkip, <span style="color: #0600FF; font-weight: bold;">out</span> UInt32 pdwFlags <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;Function from Crypto API.&lt;/summary&gt;</span>
    <span style="color: #008000;">&#91;</span>DllImport<span style="color: #008000;">&#40;</span> <span style="color: #666666;">&quot;crypt32.dll&quot;</span>, SetLastError <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
    <span style="color: #008000;">&#91;</span><span style="color: #0600FF; font-weight: bold;">return</span><span style="color: #008000;">:</span> MarshalAs<span style="color: #008000;">&#40;</span> UnmanagedType<span style="color: #008000;">.</span><span style="color: #6666cc; font-weight: bold;">Bool</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">internal</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">extern</span> <span style="color: #6666cc; font-weight: bold;">bool</span> CryptDecodeObjectEx<span style="color: #008000;">&#40;</span> CRYPT_ENCODING_FLAGS dwCertEncodingType, IntPtr lpszStructType, <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> pbEncoded, UInt32 cbEncoded, CRYPT_DECODE_FLAGS dwFlags, IntPtr pDecodePara, <span style="color: #0600FF; font-weight: bold;">ref</span> <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> pvStructInfo, <span style="color: #0600FF; font-weight: bold;">ref</span> UInt32 pcbStructInfo <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// &lt;summary&gt;Function from Crypto API.&lt;/summary&gt;</span>
    <span style="color: #008000;">&#91;</span>DllImport<span style="color: #008000;">&#40;</span> <span style="color: #666666;">&quot;crypt32.dll&quot;</span>, SetLastError <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
    <span style="color: #008000;">&#91;</span><span style="color: #0600FF; font-weight: bold;">return</span><span style="color: #008000;">:</span> MarshalAs<span style="color: #008000;">&#40;</span> UnmanagedType<span style="color: #008000;">.</span><span style="color: #6666cc; font-weight: bold;">Bool</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
    <span style="color: #0600FF; font-weight: bold;">internal</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">extern</span> <span style="color: #6666cc; font-weight: bold;">bool</span> CryptDecodeObject<span style="color: #008000;">&#40;</span> CRYPT_ENCODING_FLAGS dwCertEncodingType, IntPtr lpszStructType, <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> pbEncoded, UInt32 cbEncoded, CRYPT_DECODE_FLAGS flags, <span style="color: #008000;">&#91;</span><span style="color: #0600FF; font-weight: bold;">In</span>, <span style="color: #0600FF; font-weight: bold;">Out</span><span style="color: #008000;">&#93;</span> <span style="color: #6666cc; font-weight: bold;">byte</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> pvStructInfo, <span style="color: #0600FF; font-weight: bold;">ref</span> UInt32 cbStructInfo <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080;">#endregion P/Invoke Functions</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>I am attaching a Visual Studio 2012 solution containing a sample usage for PEM and DER encoded files. <a href="/wp-content/uploads/RSACryptoServiceProviderExtension.zip" title="RSACryptoServiceProviderExtension.zip">Click here to download</a>.</p>
<p>Solution containing sample keys:<br />
<img src="/wp-content/uploads/RSACryptoServiceProviderExtensionSolution.png" title="RSACryptoServiceProviderExtension - Solution" /><br />
Test program output:<br />
<img src="/wp-content/uploads/RSACryptoServiceProviderExtensionConsole.png" title="RSACryptoServiceProviderExtension - Console" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.christian-etter.de/?feed=rss2&#038;p=771</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Visual Studio 2012 Upper Case Menu</title>
		<link>http://www.christian-etter.de/?p=724</link>
		<comments>http://www.christian-etter.de/?p=724#comments</comments>
		<pubDate>Sun, 30 Sep 2012 11:57:08 +0000</pubDate>
		<dc:creator>Christian Etter</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Capitalization]]></category>
		<category><![CDATA[Mixed case]]></category>
		<category><![CDATA[Uppercase]]></category>
		<category><![CDATA[Visual Studio 2012]]></category>

		<guid isPermaLink="false">http://www.christian-etter.de/?p=724</guid>
		<description><![CDATA[Visual Studio 2012 is out for two weeks now. The first thing that catches the eye is the new 2D flat chroma-reduced UI. Second is probably the all-uppercase menu. For those who don&#8217;t like it, there is a registry tweak to revert to mixed case writing. Fortunately this is easy to achieve with a single [...]]]></description>
				<content:encoded><![CDATA[<p>Visual Studio 2012 is out for two weeks now. The first thing that catches the eye is the new 2D flat chroma-reduced UI. Second is probably the all-uppercase menu. For those who don&#8217;t like it, there is a registry tweak to revert to mixed case writing.</p>
<p><img title="VS2012Uppercase" src="/wp-content/uploads/VS2012Uppercase.png" alt="" width="466" height="66" /></p>
<p>Fortunately this is easy to achieve with a single value to be added to <strong>HKCU\Software\Microsoft\VisualStudio\11.0\General</strong>. Open the location in regedit.exe and add a DWORD value called <strong>SuppressUppercaseConversion</strong> which must be set to <strong>1</strong>.</p>
<p><img title="VS2012MixedCaseRegistry" src="/wp-content/uploads/VS2012MixedCaseRegistry.png" alt="" /></p>
<p>The result should look familiar again.</p>
<p><img title="VS2012MixedCase" src="/wp-content/uploads/VS2012MixedCase.png" alt="" width="434" height="66" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.christian-etter.de/?feed=rss2&#038;p=724</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>T-SQL Capitalize First Character in Each Word</title>
		<link>http://www.christian-etter.de/?p=634</link>
		<comments>http://www.christian-etter.de/?p=634#comments</comments>
		<pubDate>Thu, 26 Jul 2012 05:55:52 +0000</pubDate>
		<dc:creator>Christian Etter</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.christian-etter.de/?p=634</guid>
		<description><![CDATA[This is a small exercise in T-SQL for a common situation: It happens quite frequently that data is entered manually in slightly different ways regarding upper and lower case. Examples are &#8220;james smith&#8221;, &#8220;Gerald BLACK&#8221;, &#8220;MIKE O&#8217;NEILL&#8221; etc. Normalizing such input can be done in a procedural way by iterating over each record with a [...]]]></description>
				<content:encoded><![CDATA[<p>This is a small exercise in T-SQL for a common situation: It happens quite frequently that data is entered manually in slightly different ways regarding upper and lower case. Examples are &#8220;james smith&#8221;, &#8220;Gerald BLACK&#8221;, &#8220;MIKE O&#8217;NEILL&#8221; etc.</p>
<p>Normalizing such input can be done in a procedural way by iterating over each record with a couple of variables and two loops.</p>
<p>Is it possible to normalize such input using T-SQL in a non-procedural manner? It is, though I would leave it up to the reader to judge whether or not it is more efficient.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">WITH</span> CTE1 <span style="color: #993333; font-weight: bold;">AS</span><span style="color: #66cc66;">&#40;</span>
	<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #cc66cc;">0</span> <span style="color: #993333; font-weight: bold;">AS</span> ID<span style="color: #66cc66;">,</span> N<span style="color: #ff0000;">'章子怡'</span> <span style="color: #993333; font-weight: bold;">AS</span> A <span style="color: #993333; font-weight: bold;">UNION</span> <span style="color: #993333; font-weight: bold;">ALL</span>
	<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'new york'</span> <span style="color: #993333; font-weight: bold;">UNION</span> <span style="color: #993333; font-weight: bold;">ALL</span>
	<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'michael BOND'</span> <span style="color: #993333; font-weight: bold;">UNION</span> <span style="color: #993333; font-weight: bold;">ALL</span>
	<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'DAN john o'</span><span style="color: #ff0000;">'keele'</span> <span style="color: #993333; font-weight: bold;">UNION</span> <span style="color: #993333; font-weight: bold;">ALL</span> 
	<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #cc66cc;">4</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'michaela dÜmpel-gerümpel'</span>	<span style="color: #993333; font-weight: bold;">UNION</span> <span style="color: #993333; font-weight: bold;">ALL</span>
	<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #cc66cc;">5</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'mieze katze luftMatratze'</span> <span style="color: #993333; font-weight: bold;">UNION</span> <span style="color: #993333; font-weight: bold;">ALL</span>
	<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #cc66cc;">6</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'gute grütze gedankenStütze'</span>
<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> CTE2 <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#40;</span>
	<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">ROW_NUMBER</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">OVER</span><span style="color: #66cc66;">&#40;</span> <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> ID <span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> ID1<span style="color: #66cc66;">,</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> CTE1
<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> CTE3 <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#40;</span>
	<span style="color: #993333; font-weight: bold;">SELECT</span> ID1<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">AS</span> ID2<span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">SUBSTRING</span><span style="color: #66cc66;">&#40;</span> A<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> C<span style="color: #66cc66;">,</span> A <span style="color: #993333; font-weight: bold;">FROM</span> CTE2
	<span style="color: #993333; font-weight: bold;">UNION</span> <span style="color: #993333; font-weight: bold;">ALL</span> 
	<span style="color: #993333; font-weight: bold;">SELECT</span> ID1<span style="color: #66cc66;">,</span> ID2 <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">SUBSTRING</span><span style="color: #66cc66;">&#40;</span> A<span style="color: #66cc66;">,</span> ID2 <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> A <span style="color: #993333; font-weight: bold;">FROM</span> CTE3 <span style="color: #993333; font-weight: bold;">WHERE</span> ID2 &amp;lt; LEN<span style="color: #66cc66;">&#40;</span> A <span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> CTE4 <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#40;</span>
	<span style="color: #993333; font-weight: bold;">SELECT</span> A<span style="color: #66cc66;">.</span>ID1<span style="color: #66cc66;">,</span> A<span style="color: #66cc66;">.</span>ID2<span style="color: #66cc66;">,</span> A<span style="color: #66cc66;">.</span>A<span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">CASE</span> <span style="color: #993333; font-weight: bold;">WHEN</span> B<span style="color: #66cc66;">.</span>C <span style="color: #993333; font-weight: bold;">IS</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">THEN</span> <span style="color: #993333; font-weight: bold;">UPPER</span><span style="color: #66cc66;">&#40;</span> A<span style="color: #66cc66;">.</span>C <span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">ELSE</span> <span style="color: #993333; font-weight: bold;">LOWER</span><span style="color: #66cc66;">&#40;</span> A<span style="color: #66cc66;">.</span>C <span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">END</span> <span style="color: #993333; font-weight: bold;">AS</span> C <span style="color: #993333; font-weight: bold;">FROM</span> CTE3 <span style="color: #993333; font-weight: bold;">AS</span> A
		<span style="color: #993333; font-weight: bold;">LEFT</span> <span style="color: #993333; font-weight: bold;">JOIN</span> CTE3 <span style="color: #993333; font-weight: bold;">AS</span> B <span style="color: #993333; font-weight: bold;">ON</span> A<span style="color: #66cc66;">.</span>ID1 <span style="color: #66cc66;">=</span> B<span style="color: #66cc66;">.</span>ID1 <span style="color: #993333; font-weight: bold;">AND</span> A<span style="color: #66cc66;">.</span>ID2 <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">=</span> B<span style="color: #66cc66;">.</span>ID2 <span style="color: #993333; font-weight: bold;">AND</span> B<span style="color: #66cc66;">.</span>C <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">' '</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'.'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">','</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'['</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'('</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">''</span><span style="color: #ff0000;">''</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'-'</span> <span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> CTE5 <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#40;</span>
	<span style="color: #993333; font-weight: bold;">SELECT</span> CTE2<span style="color: #66cc66;">.</span>ID1<span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> CTE4<span style="color: #66cc66;">.</span>C <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">''</span> <span style="color: #993333; font-weight: bold;">FROM</span> CTE4 <span style="color: #993333; font-weight: bold;">WHERE</span> CTE4<span style="color: #66cc66;">.</span>ID1 <span style="color: #66cc66;">=</span> CTE2<span style="color: #66cc66;">.</span>ID1 <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> CTE4<span style="color: #66cc66;">.</span>ID2 <span style="color: #993333; font-weight: bold;">FOR</span> XML PATH<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">TYPE</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">VALUE</span><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'text()[1]'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'NVARCHAR(MAX)'</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> A
		<span style="color: #993333; font-weight: bold;">FROM</span> CTE2
<span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> CTE5 <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> ID1 <span style="color: #993333; font-weight: bold;">ASC</span> <span style="color: #993333; font-weight: bold;">OPTION</span><span style="color: #66cc66;">&#40;</span> MAXRECURSION <span style="color: #cc66cc;">32767</span> <span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>Here is the input:</p>
<table>
<tr>
<td>0</td>
<td>章子怡</td>
</tr>
<tr>
<td>1</td>
<td>new york</td>
</tr>
<tr>
<td>2</td>
<td>michael BOND</td>
</tr>
<tr>
<td>3</td>
<td>DAN john o&#8217;keele</td>
</tr>
<tr>
<td>4</td>
<td>michaela dÜmpel-gerümpel</td>
</tr>
<tr>
<td>5</td>
<td>mieze katze luftMatratze</td>
</tr>
<tr>
<td>6</td>
<td>gute grütze gedankenStütze</td>
</tr>
</table>
<p>This becomes:</p>
<table>
<tr>
<td>1</td>
<td>章子怡</td>
</tr>
<tr>
<td>2</td>
<td>New York</td>
</tr>
<tr>
<td>3</td>
<td>Michael Bond</td>
</tr>
<tr>
<td>4</td>
<td>Dan John O&#8217;Keele</td>
</tr>
<tr>
<td>5</td>
<td>Michaela Dümpel-Gerümpel</td>
</tr>
<tr>
<td>6</td>
<td>Mieze Katze Luftmatratze</td>
</tr>
<tr>
<td>7</td>
<td>Gute Grütze Gedankenstütze</td>
</tr>
</table>
<p>Obviously the above code won&#8217;t work if your name is MacGuyver, DiRosa or McDonalds. But it would not take much more than another CTE to fix such names as well as long as they adhere to a common capitalization scheme.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christian-etter.de/?feed=rss2&#038;p=634</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Most Popular Photograph I Ever Took</title>
		<link>http://www.christian-etter.de/?p=665</link>
		<comments>http://www.christian-etter.de/?p=665#comments</comments>
		<pubDate>Sun, 03 Jun 2012 21:18:26 +0000</pubDate>
		<dc:creator>Christian Etter</dc:creator>
				<category><![CDATA[Photography]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Asia]]></category>
		<category><![CDATA[Baumhaus]]></category>
		<category><![CDATA[Japan]]></category>
		<category><![CDATA[Naha]]></category>
		<category><![CDATA[Okinawa]]></category>
		<category><![CDATA[Photo]]></category>
		<category><![CDATA[Picture]]></category>
		<category><![CDATA[Ryukyu Islands]]></category>
		<category><![CDATA[Tree House]]></category>
		<category><![CDATA[アジア]]></category>
		<category><![CDATA[南西諸島]]></category>
		<category><![CDATA[日本]]></category>
		<category><![CDATA[日本国]]></category>
		<category><![CDATA[沖縄県]]></category>
		<category><![CDATA[那覇市]]></category>

		<guid isPermaLink="false">http://www.christian-etter.de/?p=665</guid>
		<description><![CDATA[While cycling through Okinawa south of Naha, I came across a red traffic light. Looking to the left, there was a huge house built on an artificial tree. It did not strike me as that spectacular, but I figured since I had to wait I might as well take a snapshot. I would have never [...]]]></description>
				<content:encoded><![CDATA[<p>While cycling through Okinawa south of Naha, I came across a red traffic light. Looking to the left, there was a huge house built on an artificial tree. It did not strike me as that spectacular, but I figured since I had to wait I might as well take a snapshot.</p>
<div id="www" class="wp-caption aligncenter" style="width: 510px"><br />
<a href="wp-content/uploads/TreeHouseOkinawa.jpg"><img src="/wp-content/uploads/TreeHouseOkinawa450.jpg" alt="" title="TreeHouseOkinawa" width="500" height="335" class="aligncenter size-medium" /></a><br />
<p class="wp-caption-text">This gigantic tree house restaurant can be found south of Naha on Okinawa, Japan. It is a restaurant called アジア (Asia). Shot with NIKON D80</p></div>
<p>I would have never expected this would turn out to be one of the most popular shots I ever took. It has been printed on 2 magazines. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.christian-etter.de/?feed=rss2&#038;p=665</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>T-SQL Word Randomizer &#8211; Does the Order of Letters in a Word Matter?</title>
		<link>http://www.christian-etter.de/?p=676</link>
		<comments>http://www.christian-etter.de/?p=676#comments</comments>
		<pubDate>Sat, 19 May 2012 21:37:14 +0000</pubDate>
		<dc:creator>Christian Etter</dc:creator>
				<category><![CDATA[Software Development]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[Randomize]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">http://www.christian-etter.de/?p=676</guid>
		<description><![CDATA[Perhaps you have come across this paragraph before: Acdrocing to a rcersaheer at Cdmiabrge Uterniisvy, it dens&#8217;ot mttear in waht odrer the letters in a word are, the only inapmtrot tinhg is that the frist and last lteetr be at the rghit plcae. The rset can be a ttoal mess and you can siltl raed [...]]]></description>
				<content:encoded><![CDATA[<p>Perhaps you have come across this paragraph before:</p>
<p style="padding: 10px; color: grey; background-color: white; border: black 2px solid">
Acdrocing to a rcersaheer at Cdmiabrge Uterniisvy, it dens&#8217;ot mttear in waht odrer the letters in a word are, the only inapmtrot tinhg is that the frist and last lteetr be at the rghit plcae. The rset can be a ttoal mess and you can siltl raed it wtihuot plreobm. Tihs is baesuce the hmuan mind does not raed ervey lteetr by ilestf but the wrod as a wlohe.
</p>
<p>When I saw this, I thought about how to write such a randomizer in T-SQL without using loops and variables:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">WITH</span> CTE1 <span style="color: #993333; font-weight: bold;">AS</span><span style="color: #66cc66;">&#40;</span>
	<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">'According to a researcher at Cambridge University, it doesn'</span><span style="color: #ff0000;">'t matter in what order the letters in a word are, the only important thing is that the first and last letter be at the right place. The rest can be a total mess and you can still read it without problem. This is because the human mind does not read every letter by itself but the word as a whole.'</span> <span style="color: #993333; font-weight: bold;">AS</span> A
	<span style="color: #993333; font-weight: bold;">UNION</span> <span style="color: #993333; font-weight: bold;">ALL</span>
	<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">'Manche Leute sagen es spielt keine Rolle, in welcher Reihenfolge die Buchstaben in einem Wort angeordnet sind, solange der erste und der letzte Buchstabe nicht verändert wird.'</span>
<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> CTE2 <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#40;</span>
	<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">ROW_NUMBER</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">OVER</span><span style="color: #66cc66;">&#40;</span> <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #993333; font-weight: bold;">LEFT</span><span style="color: #66cc66;">&#40;</span> A<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">0</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> ID1<span style="color: #66cc66;">,</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> CTE1
<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> CTE3 <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#40;</span>
	<span style="color: #993333; font-weight: bold;">SELECT</span> ID1<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">AS</span> ID2<span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">SUBSTRING</span><span style="color: #66cc66;">&#40;</span> A<span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> C<span style="color: #66cc66;">,</span> A <span style="color: #993333; font-weight: bold;">FROM</span> CTE2
	<span style="color: #993333; font-weight: bold;">UNION</span> <span style="color: #993333; font-weight: bold;">ALL</span> 
	<span style="color: #993333; font-weight: bold;">SELECT</span> ID1<span style="color: #66cc66;">,</span> ID2 <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">SUBSTRING</span><span style="color: #66cc66;">&#40;</span> A<span style="color: #66cc66;">,</span> ID2 <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> A <span style="color: #993333; font-weight: bold;">FROM</span> CTE3 <span style="color: #993333; font-weight: bold;">WHERE</span> ID2 <span style="color: #66cc66;">&lt;</span> LEN<span style="color: #66cc66;">&#40;</span> A <span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> CTE4 <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#40;</span>
	<span style="color: #993333; font-weight: bold;">SELECT</span> A<span style="color: #66cc66;">.</span>ID1<span style="color: #66cc66;">,</span> A<span style="color: #66cc66;">.</span>ID2<span style="color: #66cc66;">,</span> A<span style="color: #66cc66;">.</span>A<span style="color: #66cc66;">,</span> A<span style="color: #66cc66;">.</span>C<span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">CASE</span> <span style="color: #993333; font-weight: bold;">WHEN</span> B<span style="color: #66cc66;">.</span>C <span style="color: #993333; font-weight: bold;">IS</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AND</span> C<span style="color: #66cc66;">.</span>C <span style="color: #993333; font-weight: bold;">IS</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">THEN</span> <span style="color: #cc66cc;">0</span> <span style="color: #993333; font-weight: bold;">ELSE</span> <span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">END</span> <span style="color: #993333; font-weight: bold;">AS</span> ID3 <span style="color: #993333; font-weight: bold;">FROM</span> CTE3 <span style="color: #993333; font-weight: bold;">AS</span> A
		<span style="color: #993333; font-weight: bold;">LEFT</span> <span style="color: #993333; font-weight: bold;">JOIN</span> CTE3 <span style="color: #993333; font-weight: bold;">AS</span> B <span style="color: #993333; font-weight: bold;">ON</span> A<span style="color: #66cc66;">.</span>ID1 <span style="color: #66cc66;">=</span> B<span style="color: #66cc66;">.</span>ID1 <span style="color: #993333; font-weight: bold;">AND</span> A<span style="color: #66cc66;">.</span>ID2 <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">=</span> B<span style="color: #66cc66;">.</span>ID2 <span style="color: #993333; font-weight: bold;">AND</span> B<span style="color: #66cc66;">.</span>C <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">' '</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'.'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">','</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'['</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'('</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">']'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">')'</span> <span style="color: #66cc66;">&#41;</span>
		<span style="color: #993333; font-weight: bold;">LEFT</span> <span style="color: #993333; font-weight: bold;">JOIN</span> CTE3 <span style="color: #993333; font-weight: bold;">AS</span> C <span style="color: #993333; font-weight: bold;">ON</span> A<span style="color: #66cc66;">.</span>ID1 <span style="color: #66cc66;">=</span> C<span style="color: #66cc66;">.</span>ID1 <span style="color: #993333; font-weight: bold;">AND</span> A<span style="color: #66cc66;">.</span>ID2 <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">=</span> C<span style="color: #66cc66;">.</span>ID2 <span style="color: #993333; font-weight: bold;">AND</span> C<span style="color: #66cc66;">.</span>C <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">' '</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'.'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">','</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'['</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'('</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">']'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">')'</span> <span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> CTE5 <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#40;</span>
	<span style="color: #993333; font-weight: bold;">SELECT</span> A<span style="color: #66cc66;">.</span>ID1<span style="color: #66cc66;">,</span> A<span style="color: #66cc66;">.</span>ID2<span style="color: #66cc66;">,</span> A<span style="color: #66cc66;">.</span>A<span style="color: #66cc66;">,</span> A<span style="color: #66cc66;">.</span>C<span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">CASE</span> A<span style="color: #66cc66;">.</span>ID3 <span style="color: #993333; font-weight: bold;">WHEN</span> <span style="color: #cc66cc;">0</span> <span style="color: #993333; font-weight: bold;">THEN</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">MAX</span><span style="color: #66cc66;">&#40;</span> B<span style="color: #66cc66;">.</span>ID2 <span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span> <span style="color: #993333; font-weight: bold;">FROM</span> CTE4 <span style="color: #993333; font-weight: bold;">AS</span> B <span style="color: #993333; font-weight: bold;">WHERE</span> A<span style="color: #66cc66;">.</span>ID1 <span style="color: #66cc66;">=</span> B<span style="color: #66cc66;">.</span>ID1 <span style="color: #993333; font-weight: bold;">AND</span> B<span style="color: #66cc66;">.</span>ID2 <span style="color: #66cc66;">&lt;</span> A<span style="color: #66cc66;">.</span>ID2 <span style="color: #993333; font-weight: bold;">AND</span> B<span style="color: #66cc66;">.</span>ID3 <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">ELSE</span> ID2 <span style="color: #993333; font-weight: bold;">END</span> <span style="color: #993333; font-weight: bold;">AS</span> ID3 <span style="color: #993333; font-weight: bold;">FROM</span> CTE4 <span style="color: #993333; font-weight: bold;">AS</span> A
<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> CTE6 <span style="color: #993333; font-weight: bold;">AS</span> <span style="color: #66cc66;">&#40;</span>
	<span style="color: #993333; font-weight: bold;">SELECT</span> CTE2<span style="color: #66cc66;">.</span>ID1<span style="color: #66cc66;">,</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #993333; font-weight: bold;">SELECT</span> CTE5<span style="color: #66cc66;">.</span>C <span style="color: #66cc66;">+</span> <span style="color: #ff0000;">''</span> <span style="color: #993333; font-weight: bold;">FROM</span> CTE5 <span style="color: #993333; font-weight: bold;">WHERE</span> CTE5<span style="color: #66cc66;">.</span>ID1 <span style="color: #66cc66;">=</span> CTE2<span style="color: #66cc66;">.</span>ID1 <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> CTE5<span style="color: #66cc66;">.</span>ID3<span style="color: #66cc66;">,</span> NEWID<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">FOR</span> XML PATH<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">''</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">TYPE</span> <span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">.</span><span style="color: #993333; font-weight: bold;">VALUE</span><span style="color: #66cc66;">&#40;</span> <span style="color: #ff0000;">'text()[1]'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'NVARCHAR(MAX)'</span> <span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AS</span> A
		<span style="color: #993333; font-weight: bold;">FROM</span> CTE2
<span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> CTE6 <span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> ID1 <span style="color: #993333; font-weight: bold;">ASC</span> <span style="color: #993333; font-weight: bold;">OPTION</span><span style="color: #66cc66;">&#40;</span> MAXRECURSION <span style="color: #cc66cc;">32767</span> <span style="color: #66cc66;">&#41;</span></pre></td></tr></table></div>

<p>Here is a German example:</p>
<p style="padding: 10px; color: grey; background-color: white; border: black 2px solid">
Mahnce Letue seagn es speilt kenie Rolle, in wcelher Renfhioglee die Bbetschuan in eniem Wort aegdnonret snid, solnage der erste und der ltteze Bsbctauhe nihct vedrreänt wrid.
</p>
<p>Ok, this is not terribly efficient, but it proves that recursive CTEs have their right of existence at last ;-)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christian-etter.de/?feed=rss2&#038;p=676</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic page generated in 1.066 seconds. -->
<!-- Cached page generated by WP-Super-Cache on 2013-05-18 20:14:51 -->
