{"id":1628,"date":"2015-11-14T00:01:15","date_gmt":"2015-11-14T08:01:15","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1628"},"modified":"2015-11-21T07:51:22","modified_gmt":"2015-11-21T15:51:22","slug":"encoding-and-decoding-part-iv","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1628","title":{"rendered":"Encoding and Decoding, Part IV"},"content":{"rendered":"<p>It would be a grand thing to set out and craft the entire decoding program in one sitting. That&#8217;s ambition in action, but it doesn&#8217;t demonstrate much programming experience.<br \/>\n<!--more--><br \/>\nInstead of facing multiple setbacks, my approach is to tackle the decoding process one step at a time.<\/p>\n<p>The first step, conquered in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1618\">last week&#8217;s Lesson<\/a>, was to figure out how to translate the hexadecimal character string into a value. That accomplishment can be set aside for the next few steps, which are:<\/p>\n<p>2. Process standard input one line at a time.<br \/>\n3. Confirm that the first line is the hexcode header and check the version number.<br \/>\n4. Decode each line of text and display the output.<br \/>\n5. Check for the end of the formatted hexcode data.<\/p>\n<p>Step 2 is to write code that chews standard input one line a time. To accomplish this task, and because this program is a filter, you could use the <em>fgets()<\/em> function to read in a line of text from standard input. Instead, I&#8217;ve opted for more control by using the <em>getchar()<\/em> function. That way I can monitor input one character at a time.<\/p>\n<p>For decoding hexcode output, you know that every line is formatted: 24 hex values on a line, each prefixed by a space, evaluates to 72 characters. Add one for the null character, and you need a 73 byte buffer to hold each line of input. Any line longer than 72 characters indicates some type of error, either a file read booboo or data that&#8217;s simple not in the hexcode format.<\/p>\n<p>Here&#8217;s my solution:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\n#define LINE_LENGTH 73\r\n\r\nint main()\r\n{\r\n    char line_buffer[LINE_LENGTH];\r\n    char c;\r\n    int buffer_index = 0;\r\n\r\n    while(1)\r\n    {\r\n        c = getchar();\r\n        if( c == EOF)\r\n            break;\r\n        line_buffer[buffer_index] = c;\r\n        buffer_index++;\r\n        if( buffer_index &gt; LINE_LENGTH)\r\n        {\r\n            <span class=\"comments\">\/* overflow condition *\/<\/span>\r\n            puts(\"\\nInvalid hexcode line format\");\r\n            return(1);\r\n        }\r\n        if( c == '\\n')\r\n        {\r\n            <span class=\"comments\">\/* terminate string *\/<\/span>\r\n            line_buffer[buffer_index] = '\\0';\r\n            <span class=\"comments\">\/* display contents *\/<\/span>\r\n            printf(\"%s\",line_buffer);\r\n            <span class=\"comments\">\/* reset index *\/<\/span>\r\n            buffer_index = 0;\r\n        }\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>The filter runs from a endless <em>while<\/em> loop (Lines 11 through 33). Line 13 fetches a character from standard input. If the end of file (EOF in Line 14) is encountered, the loop terminates and the program stops. Otherwise, the character <code>c<\/code> is stored in the <code>line_buffer<\/code> <em>char<\/em> array at offset <code>buffer_index<\/code> (Line 16).<\/p>\n<p>Line 18 checks for buffer overflow, which happens when the <code>buffer_index<\/code> variable is greater than the line length. This step not only prevents hackers from abusing the code, but it confirms that the input isn&#8217;t formatting for hexcode; the program spews out an error message (Line 21) and quits (Line 22).<\/p>\n<p>When the newline is encountered (Line 24), the string is terminated (Line 27) and then displayed (Line 29). This is the point where processing would normally take place, but at this stage the string is output instead. Line 31 resets the <code>buffer_index<\/code> variable, and the endless <em>while<\/em> loop continues.<\/p>\n<p>I used the following redirection command to create a hexcode formatted file, <code>gettysburg.hexc<\/code>:<\/p>\n<pre><code>hexencode < gettysburg.txt > gettysburg.hexc<\/code><\/pre>\n<p>Running this Lesson&#8217;s code on the file <code>gettysburg.hexc<\/code> generates this output:<\/p>\n<pre><code>a.out < gettysburg.hexc\r\nSTART HEX CODE v1.0\r\n EC C5 DF D8 8A D9 C9 C5 D8 CF 8A CB C4 CE 8A D9 CF DC CF C4 8A D3 CF CB\r\n D8 D9 8A CB CD C5 8A C5 DF D8 8A CC CB DE C2 CF D8 D9 8A C8 D8 C5 DF CD\r\n C2 DE 8A CC C5 D8 DE C2 8A C5 C4 8A DE C2 C3 D9 8A C9 C5 C4 DE C3 C4 CF\r\n C4 DE 8A CB 8A C4 CF DD 8A C4 CB DE C3 C5 C4 86 8A C9 C5 C4 C9 CF C3 DC\r\n CF CE 8A C3 C4 8A C6 C3 C8 CF D8 DE D3 86 8A CB C4 CE 8A CE CF CE C3 C9\r\n CB DE CF CE 8A DE C5 8A DE C2 CF 8A DA D8 C5 DA C5 D9 C3 DE C3 C5 C4 8A\r\n DE C2 CB DE 8A CB C6 C6 8A C7 CF C4 8A CB D8 CF 8A C9 D8 CF CB DE CF CE\r\n 8A CF DB DF CB C6 84 A0 A0\r\nEND HEX CODE<\/code><\/pre>\n<p>No processing takes place; the code simply spits out the formatted input. If you tried the code with a non-formatted file, such as the original <code>gettysburg.txt<\/code> file, you'd see this output:<\/p>\n<pre><code>a.out < gettysburg.txt \r\n\r\nInvalid hexcode line format<\/code><\/pre>\n<p>This solution confirms that hexcode output can be read and that long lines are rejected. Step 3 is to confirm the hexcode header, which I cover in <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1644\">the next Lesson<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The first step is to process input one line at a time. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1628\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-1628","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1628","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1628"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1628\/revisions"}],"predecessor-version":[{"id":1661,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1628\/revisions\/1661"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1628"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1628"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1628"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}