User Tools

Site Tools


Sidebar

projects

cci0 (due 20170826)
wcp1 (due 20170826)
dtr0 (due 20170830)
wcp2 (due 20170902)
sof0 (due 20170906)
wcp3 (due 20170909)
dow0 (due 20170913)
wcp4 (due 20170916)
mbe0 (due 20170920)
wcp5 (due 20170923)
cbf0 (due 20170927)
wcp6 (due 20170930)
cos0 (due 20171004)
wcp7 (due 20171007)
pnc0 [metrics] (due 20171018)
mbe1 (BONUS – due 20171018)
wcp8 (due 20171021)
pnc1 [metrics] (due 20171025)
wcp9 (due 20171028)
gfo0 (due 20171101)
wcpA (due 20171104)
gfo1 (due 20171108)
wcpB (due 20171111)
gfo2 (due 20171115)
wcpC (due 20171118)
haas:fall2017:cprog:projects:cbf0

This is an old revision of the document!


Corning Community College

CSCS1320 C/C++ Programming

~~TOC~~

Project: C Binary Fun (cbf0)

Objective

To practice manipulating binary data in a C program (for fun and glory).

Background

We've had a newfound exposure to data this semester, and how programming languages like C interpret different forms of data; we look at things like data type and corresponding storage allocated, and we use these as ingredients in our programmatic solutions.

Yet- we've also inserted a layer of abstraction between us and the computer: integers and floating point values and ASCII characters… each with its own unique ways of accessing and manipulating.

The thing is: to the computer all data is largely the same: sequences of 1s and 0s, accessed in units of bytes.

This project will expose us to some of the underlying aspects of the realm of data that lies closer to the computer, that of “binary data”, where we often come in contact with hexadecimal values to aid us in the interaction.

So, as we are here to learn more about the computer, it only makes sense to steer some of our activities towards the manipulation of binary data as well- one cannot effectively solve a whole domain of problems if they have no idea how to work with it.

This project aims to ameliorate that.

Binary data merely refers to data as the computer stores it. The computer is a binary device, so its raw data (as it exists on various forms of storage and media) is often referred to as binary data, to reflect the 1s and 0s being represented.

The data we have become familiar with is textual data. We read from and write to files (even with those files commonly being the keyboard and screen) with the express purpose of retrieving or storing text with them. And with the use of various text processing tools, we can easily manipulate these text files.

But: did you know that all text data is also binary data?

The trick to remember is that its opposite is not always true: not all binary data is text. In fact most of it isn't. Text represents is a very narrow range of possible data values, and then only within a certain context. You may “see” random letters when viewing binary data, but there is no continuity. The data values that we utilize when interacting with text are also valid combinations of binary values. Which can mean almost anything.

So, text is really ONE (of many) possible representations of binary data. We need to gain a wider perspective and get more familiar with this more expansive and general notion of binary data.

The computer works in units of bytes, which these days means groups of 8 bits. C has the ability to arbitrarily read and write individual bytes of data, and we will want to make use of that to aid us in our current task.

Task

Your task is to write a hex viewer, along the lines of the xxd(1) tool found on the system.

Grabit

I have prepared some files to assist in our endeavors, which can be obtained through the use of the special grabit tool found on lab46:

Experiencing xxd

If we don't know what it is we are implementing, we won't be all that successful. So, here's a quick overview of the xxd(1) tool we will be simulating aspects of; first up, a plain text look at a data file we will be processing:

lab46:~/src/cprog/cbf0$ cat sample0.txt
>ABCDEFGHIJKLMNOPQRSTUVWXYZ<
[abcdefghijklmnopqrstuvwxyz]
01:              BINARY
01234567:        OCTAL
0123456789:      DECIMAL
0123456789ABCDEF:HEXADECIMAL
)!@#$%^&*(
.
lab46:~/src/cprog/cbf0$ 

Note how it is filled with ASCII text- many of our recognizable symbols we use when using a text editor.

But, to illustrate how text is just a form of binary, witness what we are shown when we peel away a layer, and view the binary data (represented in hex for convenience) of that same file:

lab46:~/src/cprog/cbf0$ xxd sample0.txt
0000000: 3e41 4243 4445 4647 4849 4a4b 4c4d 4e4f  >ABCDEFGHIJKLMNO
0000010: 5051 5253 5455 5657 5859 5a3c 0a5b 6162  PQRSTUVWXYZ<.[ab
0000020: 6364 6566 6768 696a 6b6c 6d6e 6f70 7172  cdefghijklmnopqr
0000030: 7374 7576 7778 797a 5d0a 3031 3a09 0920  stuvwxyz].01:..
0000040: 4249 4e41 5259 0a30 3132 3334 3536 373a  BINARY.01234567:
0000050: 0920 4f43 5441 4c0a 3031 3233 3435 3637  . OCTAL.01234567
0000060: 3839 3a09 2044 4543 494d 414c 0a30 3132  89:. DECIMAL.012
0000070: 3334 3536 3738 3941 4243 4445 463a 4845  3456789ABCDEF:HE
0000080: 5841 4445 4349 4d41 4c0a 2921 4023 2425  XADECIMAL.)!@#$%
0000090: 5e26 2a28 0a2e 0a                        ^&*(...
lab46:~/src/cprog/cbf0$ 

The EXACT same file, with the EXACT same arrangement of data, only represented more as the computer looks at it (sequentially, one byte immediately following the next).

The output of xxd(1) has 3 distinct sections:

  1. the address or offset (from the start of file). This is a hexadecimal address, starting at 0 (beginning of the file), and increments according to the number of bytes displayed. You'll notice that there are (at maximum) the same number of bytes on each line, so the offset increments by that amount with each new line it displays.
  2. the actual data (represented in hex); here we see 8 columns of hex values, grouped together in pairs of two bytes (other hex viewers may separate into 16 columns, isolating each byte for better viewing).
  3. the ASCII rendering (far right field); if we are viewing an ASCII file, we will easily see the ASCII contents of this file. If we are viewing a non-ASCII file, we may still see random ASCII values, but that is just that the value stored in the particular byte maps to that ASCII value, and should NOT be considered actual ASCII data.

This is one of those conceptual roadblocks many develop- they think that binary is somehow more complicated than it is, and create all sorts of obstacles to effective access. Here we will try to break down some of those walls, because this is really important stuff to know.

Your task is to write a C program that takes a file name as a command-line argument, opens that file, reads its contents, and displays that data to the screen in the manner that the xxd(1) tool does in the above example (note that while the xxd(1) tool has other features, we are not looking to implement them; only this simple rendering view).

Your program must:

  • Require the user supply a file via the command-line
    • if the file specified does not exist/cannot be opened, display an error message and exit.
      • error message should be of the form: Error: Could not open 'filename' for reading!
        • Where filename is the name of the file specified on the command-line (make sure the quotes surround it in the output).
    • no further processing should be done if the file is not able to be accessed.
  • Detect the current size of the terminal (see “Detecting Terminal Size” section below), and record the lines and columns into variables for use in your program.
    • If the terminal your program is being run in is less than 80 columns, display an error message and exit.
      • error message should be of the form: Error: Terminal width is less than 80 columns!
      • Your program will only be displaying to an area up to 80 characters wide, so a wider terminal will not influence program output.
    • Similarly, if the number of lines in the terminal is less than 20, display a similar error message and exit.
      • error message should be of the form: Error: Terminal height is less than 20 lines!
      • Unlike the width, the height can impact program output (taller terminals, if not otherwise throttled by a second command-line argument, can auto-expand if there is more room and data to display).
  • The second command-line argument is a sizing throttle (controlling the number of lines your program will display). If no argument, or a 0 is given, assume autosize (use the detected height to be your maximum in your calculations).
  • Each row will display:
    • a 7-digit hex offset (referring to the first data byte on a given line)
    • followed by a colon and a single space
    • then eight space separated groups of two bytes
    • however you arrive at it: two total spaces following the hex bytes (again, see output example)
    • a 16-character ASCII representation field (no separating spaces between the values)
      • all printable characters should be displayed.
      • all non-printable (and various whitespace) characters should be substituted with a '.'
    • A newline will be the last character on each line.
  • The hex values and rendered ASCII displayed will be sourced from the file specified on the command-line. While the target files for this project are less than 512 bytes, your program should be able to handle larger and smaller files, and update its display accordingly.
  • If a line throttle is given, your program is to stop output of data and ASCII rendering at that line, once it completes.
  • Once the data in the file has been exhausted, you need to wrap up as appropriate; finish the current line (even if you have to pad spaces), display the corresponding ascii field (padding spaces as appropriate), and display the closing footer.
  • Don't forget to fclose() any open file pointers! And free() any malloc()'ed or calloc()'ed memory.

Detecting Terminal Size

To detect the current size of your terminal, you may make use of the following code, provided in the form of a complete program for you to test, and then adapt into your code as appropriate.

It makes use of a structure, which we have not extensively covered yet, but the example shows you how you can make use of an existing struct, which is all you have to do in the program (we're just using it to retrieve information to help us on our program).

#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
 
int main (void)
{
    struct winsize terminal;
    ioctl  (0, TIOCGWINSZ, &terminal);
 
    printf ("lines:   %d\n", terminal.ws_row);
    printf ("columns: %d\n", terminal.ws_col);
    return (0);
}

An ioctl(2) is a method (and system/library call) for manipulating underlying device parameters of special files (for the UNIX people: everything is a file, including your keyboard, and terminal screen). We are basically querying the screen (or accessing lower level information made possible by communicating with the driver of the device) to obtain some useful information.

Here we are accessing the information on our terminal file, retrieving the width and height so that we can make use of them productively in our programs.

Compile and run the above code to see how it works. Try it in different size terminals. Then incorporate the logic into your hex viewer for this project.

Bonus Opportunities

The following can be considered a bonus point opportunity:

  • Enhance the program to accept up to 6 pairs of additional values (offset followed by its length), where each offset through length will be colored using ANSI text escape sequences.
  • For any line containing this colorized text, highlight the address in bold white.

Sample output

As an example, running the program with the following arguments could produce results like this:

lab46:~/src/cprog/cbf0$ ./cbf0 sample0.txt 0 0x1be 1 0x1c2 1 0x1c6 4 0x1ca 4 0x1fe 2

ANSI escape sequences for color

This probably isn't very portable, and depending on the terminal, it may not work for some people.

It may be most convenient to set up preprocessor #define statements near the top of your code, as follows:

#define  ANSI_RESET             "\x1b[0m"
#define  ANSI_BOLD              "\x1b[1m"
#define  ANSI_FG_BLACK          "\x1b[30m"
#define  ANSI_FG_RED            "\x1b[31m"
#define  ANSI_FG_GREEN          "\x1b[32m"
#define  ANSI_FG_YELLOW         "\x1b[33m"
#define  ANSI_FG_BLUE           "\x1b[34m"
#define  ANSI_FG_MAGENTA        "\x1b[35m"
#define  ANSI_FG_CYAN           "\x1b[36m"
#define  ANSI_FG_WHITE          "\x1b[37m"
#define  ANSI_BG_BLACK          "\x1b[40m"
#define  ANSI_BG_RED            "\x1b[41m"
#define  ANSI_BG_GREEN          "\x1b[42m"
#define  ANSI_BG_YELLOW         "\x1b[43m"
#define  ANSI_BG_BLUE           "\x1b[44m"
#define  ANSI_BG_MAGENTA        "\x1b[45m"
#define  ANSI_BG_CYAN           "\x1b[46m"
#define  ANSI_BG_WHITE          "\x1b[47m"

To use, you output them:

fprintf(stdout, ANSI_FG_GREEN);
fprintf(stdout, "This text is green\n");
fprintf(stdout, ANSI_RESET);

You have to remember to turn the color or setting off (resetting it) to revert back to the original color.

You can mix and match as well:

fprintf(stdout, ANSI_FG_YELLOW);
fprintf(stdout, ANSI_BG_BLUE);
fprintf(stdout, ANSI_BOLD);
fprintf(stdout, "This text is bold yellow on blue\n");
fprintf(stdout, ANSI_RESET);

While there are 8 available foreground colors, bolding can double that range to 16.

Submission

To successfully complete this project, the following criteria must be met:

  • Code must compile cleanly (no warnings or errors)
    • Use the -Wall and –std=c99 flags when compiling.
  • Code must be nicely and consistently indented (you may use the indent tool)
  • Code must utilize the algorithm/approach presented above
  • Output must match the specifications presented above (when given the same inputs)
  • Code must be commented
    • be sure your comments reflect the how and why of what you are doing, not merely the what.
  • Track/version the source code in a repository
  • Submit a copy of your source code to me using the submit tool.

To submit this program to me using the submit tool, run the following command at your lab46 prompt:

$ submit cprog cbf0 cbf0.c
Submitting cprog project "cbf0":
    -> cbf0.c(OK)

SUCCESSFULLY SUBMITTED

You should get some sort of confirmation indicating successful submission if all went according to plan. If not, check for typos and or locational mismatches.

haas/fall2017/cprog/projects/cbf0.1504999488.txt.gz · Last modified: 2017/09/09 23:24 by wedge