{"id":1656,"date":"2015-11-28T00:01:02","date_gmt":"2015-11-28T08:01:02","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=1656"},"modified":"2015-12-05T08:35:54","modified_gmt":"2015-12-05T16:35:54","slug":"encoding-and-decoding-part-vi","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=1656","title":{"rendered":"Encoding and Decoding, Part VI"},"content":{"rendered":"<p>Data encoded by my hexencode system is formatted. That means that the decoding program must look for both the header and footer in that formatted output. In <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1644\">last week&#8217;s Lesson<\/a>, code was added to confirm the input file&#8217;s first line as the header. This week&#8217;s modification is to search for the end of encoded text.<br \/>\n<!--more--><br \/>\nThe code is getting rather long, but the main modification from last week is the addition of the <em>process_line()<\/em> function. Here&#8217;s the code:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n#include &lt;string.h&gt;\r\n\r\n#define LINE_LENGTH 73\r\n#define TRUE 1\r\n#define FALSE 0\r\n\r\nvoid check_file(char *line);\r\nint process_line(char *line);\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    int first_line = TRUE;\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\">\/* process first line separately *\/<\/span>\r\n            if(first_line)\r\n            {\r\n                check_file(line_buffer);\r\n                first_line = FALSE;\r\n            }\r\n            else\r\n            {\r\n                if(process_line(line_buffer))\r\n                    break;\r\n            }\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}\r\n\r\nvoid check_file(char *line)\r\n{\r\n    if( strncmp(line,\"START HEX CODE\",14) == 0 )\r\n    {\r\n        <span class=\"comments\">\/* check version number here *\/<\/span>\r\n        <span class=\"comments\">\/* offset 17 bytes *\/<\/span>\r\n        return;\r\n    }\r\n    else\r\n    {\r\n        puts(\"Improper hexcode file format\");\r\n        exit(1);\r\n    }\r\n}\r\n\r\nint process_line(char *line)\r\n{\r\n    if( strncmp(line,\"END HEX CODE\",12) == 0 )\r\n    {\r\n        return(TRUE);\r\n    }\r\n    else\r\n    {\r\n        <span class=\"comments\">\/* process the input here *\/<\/span>\r\n        printf(\"%s\",line);\r\n        return(FALSE);\r\n    }\r\n}<\/pre>\n<p>Rather than use a <em>printf()<\/em> statement at Line 42 (in last week&#8217;s code), the <em>else<\/em> part of the decision now calls the <em>process_line()<\/em> function (Line 44). That function returns TRUE or FALSE, so it&#8217;s placed inside an <em>if<\/em> test. When the result is TRUE, meaning the last line in the encoded file is processed, then the loop breaks (Line 45). The decoding is finished.<\/p>\n<p>The <em>process_line()<\/em> function is prototyped at Line 9 and defined at Line 70. For now, it contains an <em>strncmp()<\/em> function to scan for the final line of the encoded file (Line 71). If found, the function returns TRUE to break the main program&#8217;s <em>while<\/em> loop. Otherwise, input is processed, which at this point the text is displayed at Line 79 and the value FALSE is returned (Line 80).<\/p>\n<p><em>Why are you taking so long to do this?<\/em><\/p>\n<p>Glad you asked: I want to ensure that all the program&#8217;s pieces work, one at a time. If you code too much at once, you deal with multiple errors and problems. Solve the puzzle one step at a time and the process runs faster.<\/p>\n<p>The sample output displays the hexadecimal digits from the input file. Here how it looks when run on the <code>gettysburg.hexc<\/code> input file:<\/p>\n<pre><code>blog$ .\/a.out < gettysburg.hexc \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<\/code><\/pre>\n<p>For <a href=\"http:\/\/c-for-dummies.com\/blog\/?p=1665\">next week's Lesson<\/a>, I (finally) finish the decoding program by adding the process that translates the hexadecimal bytes back into values, performs the XOR 0xAA operation, and outputs the original input.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Searching for the end of the decoding block. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=1656\">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-1656","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\/1656","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=1656"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1656\/revisions"}],"predecessor-version":[{"id":1680,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1656\/revisions\/1680"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1656"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1656"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1656"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}