Table of Contents

Corning Community College

CSCS2330 Discrete Structures

Project: Binary Data Tool (bdt2)

Objective

To continue our binary data explorations in the creation of a useful tool for aiding us in the debugging of our dcfX endeavors.

Background

With our recent xxd(1) tool implementation in bdt0, and our rudimentary “first point of difference” “binary diff” that was implemented in bdt1, we are going to continue down this rabbit hole further by writing a tool to allow us to explore multiple points of difference (within reason).

One of the things I noticed while helping debug was a frequent loss of place while looking at the hex output of encoded data. Seconds were lost relocating the points of difference, and over time, those lost seconds add up.

It would be uniquely useful if we had a way to highlight the point (byte) of difference in two files, so we can then focus on why/how they are different, vs. devoting far too much time to discovering what is different.

Task

Your task is to write a custom binary difference visualizer, in a format not unlike that of xxd(1), but certainly different from the output format we strove for in bdt0 and bdt1.

The idea is to take 2 files as input, parse through those (ideally similar) files, until the a point of difference is found, at which point your tool will display:

Thought empowerment vs. thought slavery

Something I've noticed with many who are so used to conforming and following authority, is that the thought of “questioning why things are” rarely comes into the picture.

I've certainly seen plenty of examples… of people messing something up, and then proceeding to live with the mistake, maybe bothered by the inconvenience, but seemingly powerless to fix it.

The thing is, we are very much in control, and if the universe doesn't conform to our demands, we must simply realign the universe.

So here, while debugging binary data… instead of just going with the flow and inconveniencing ourselves, losing our place and wasting time elongating our debugging process, we will be writing a specialized tool that should assist us greatly in the dcfX debugging process.

The key is to identify an inconvenience. If we have a tool that helps, but is limited, is that a limitation we can live with, or can we improve our overall process by improving the tool (either by extending it, or by writing a new tool altogether).

We've done this a bit with pipes… xxd(1) doesn't natively support capping its lines of display, so we've been using UNIX pipes to have commands like head(1) and tail(1) greatly enhance the utility of our xxd(1) output (versus haplessly scrolling through hundreds of lines of hex values). Thing is, how many would have done this if I never showed you examples?

So please, be on the lookout for limitations in the process- ANY process. Sometimes there is nothing we can really do, but other times, we definitely can. Don't just go with some mindless flow- constantly evaluate whatever process you are following:

There are constantly opportunities for enhancement of process. It is our job to identify strategic ones that can make significant gains. That's why we automate things with shell scripts, that's why we learn to solve problems, that's why we learn about different approaches to algorithms.

So these bdt# projects are a specific foray into this special case study of writing our own custom tool that can get a certain job done, faster. Reducing OUR particular need to keep tabs on something the computer is very much better at doing.

Implementation Restrictions

As our goal is not only to explore the more subtle concepts of computing but to promote different methods of thinking (and arriving at solutions seemingly in different ways), one of the themes I have been harping on is the stricter adherence to the structured programming philosophy. It isn't just good enough to be able to crank out a solution if you remain blind to the many nuances of the tools we are using, so we will at times be going out of our way to emphasize focus on certain areas that may see less exposure (or avoidance due to it being less familiar).

As such, the following implementation restrictions are also in place:

Basically, I am going loosen my implementation restriction grip for this project: I would like you NOT to disappoint me. Write clean, well-indented, well-commented, effective code… show me that you have learned something from your programming experience.

Program Specifications

Basic functionality

Your program should:

In bdt1, the focus is the FIRST byte of difference. Here in bdt2, we are looking at supporting multiple bytes of difference, perhaps even occurring on the same line.

Access variables in your C program

Setting a variable in your terminal (or even on the command-line prefixing the execution of your bdt2 tool) allows you to communicate information to your program, enabling it to change or alter its default behaviour (just as command-line arguments do)

For this project, you can make use of the getenv(3) function, provided in the C standard library (symbols included via the stdlib.h file).

Excerpted from the getenv(3) manual page:

NAME
       getenv - get an environment variable

SYNOPSIS
       #include <stdlib.h>

       char *getenv (const char *name);

DESCRIPTION
       The getenv() function searches the environment list to find
       the environment variable name, and returns a pointer to the
       corresponding value string.

RETURN VALUE
       The getenv() function returns a pointer to the value in the
       environment, or NULL if there is no match.

Your “const char *name” is the name of the variable set (for the purposes of this project, call it THROTTLE). Check the example execution to see it in use.

You may notice similarity in parameter usage to that of other standard library functions that you've utilized, like atoi(3).

NOTE: You may not want to nest your getenv(3) call as a parameter to atoi(3); in the event of an error (or no such variable being set), a NULL tends to cause a segmentation fault.

Reference Implementations

To assist you in your off-system development efforts, the bin/ directory in the bdt2 project (available on lab46 via grabit) has 2 compiled binaries:

Since the Raspberry Pi uses an ARM processor and lab46 uses an Intel processor, binaries are not compatible across architectures.

If you are not sure what architecture your development system uses, run the uname -m command:

lab46:~$ uname -m
x86_64
yourpi:~$ uname -m
aarch64

Your bdt2 implementation should match the behaviour and appearance of these reference implementations.

Output

An example of expected output:

Submission

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

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

lab46:~/src/desig/bdt2$ submit discrete bdt2 bdt2.c
Submitting discrete project "bdt2":
    -> bdt2.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.

What I will be looking for:

195:bdt2:final tally of results (195/195)
*:bdt2:bdt2.c compiles cleanly, no compiler messages [13/13]
*:bdt2:bdt2.c implements only specified algorithm [26/26]
*:bdt2:bdt2.c code conforms to project specifications [26/26]
*:bdt2:bdt2.c implementation free from restrictions [26/26]
*:bdt2:bdt2.c implements and utilizes THROTTLE usage [13/13]
*:bdt2:bdt2.c implements and utilizes a flag in logic [13/13]
*:bdt2:bdt2 runtime output conforms to reference [26/26]
*:bdt2:bdt2 runtime output matches reference [26/26]
*:bdt2:bdt2 committed, pushed to lab46 repository [26/26]

Additionally: