|
|
c m s c 311
s u m m e r 2 0 0 3 |
Hex Dump Specs
In a sense, all files are "binary". Files still store information as bytes. However, in ASCII files, those bytes were probably created using a text editor, and can be read using a text editor or some UNIX utility like more or less. Viewing an executable doesn't display well in a text editor.
A hex dump utility displays each byte as a two hexadecimal digit value. It usually gives you some idea of how many bytes have been displayed as well.
The class cluster already has a hex dump utility called
"xxd". To try it, type:
xxd -g 1 <filename>
where <filename> can be any file.
Here are the specifications:
The program may take the following command line switches.
00 00 00 00 00 00 01 00 2E 64 79 6E 73 79 6D 00 00 00 00 00 00 00 04 00 2E 72 65 6C 2E 64 79 6E Total bytes: 32
0000000: 83 01 10 00 AB 47 F0 35 00 40 00 00 00 00 00 00 0000010: 90 00 00 00 50 00 07 30 0B 01 0B 03 0A 00 00 00 Total bytes: 32
You should print the addresses to 7 hex digits. You can assume that the total number of bytes in a file will never exceed FFFFFFF (which is the largest 7 digit hex calue).
If you run "xxd", you will see that, on the side, there are characters that are printed out.
0000060: 80 80 00 40 01 00 00 00 2e 64 79 6e 61 6d 69 63 ...@.....dynamic 0000070: 70 04 00 20 01 00 00 00 70 04 00 20 01 00 00 00 p.. ....p.. ....There are 16 characters at the end of each line. Each character matches up with the byte on that line. If the byte has an ASCII code for a non-printable character (or is something like newline or tab), it's printed as a dot. If the ASCII code for the byte is a digit, a letter, or printable symbols like $, %, ^, etc., then the character associated with the ASCII code is printed. For example, if a byte had the value 61 (in hex), this would print out as lowercase 'a' (since the ASCII code for 'a' is 61, in hex).
If the ASCII code for a blank space appears, print a blank space on the right (there's an example of that above).
This feature is useful to recognize any ASCII text that might appear in a binary file. Many times, a binary file contains quite a few printable ASCII characters, and it's useful to see it that way, in addition to hex values. In the example above, you see the word "dynamic" in the binary file. Other times (as in 'p' shown above), the byte value just happens to coincide with the ASCII code for a letter, though it is typically just coincidence that it prints a letter.
hexdump error: File <filename> does not existwhere <filename> is replaced by the name of the actual file.
If this option does NOT exist, assume that the input file comes in via input redirection. BE ABLE TO HANDLE INPUT REDIRECTION, as this shall be the main way we test your code!
If there is no -o option, then assume the output is sent to STANDARD OUTPUT. In general, we will test WITHOUT the -o option except in a limited number of test cases.
Unknown extensions should be ignored (as if they don't exist).
Some more information
00 00 00 00 00 00 01 00 2E 64 79 6E 73 79 6D 00 00 00 00 00 00 00 04 00 2E 72 65 6C 2E 64 79 6EYou should print letters 'A'...'F' in uppercase, should they appear.
00 00 00 00 00 00 01 00 2E 64 79 6E 73 79 6D 00 2E 72 65 6C 2E 64 79 6EOnly the last line may contain fewer than 16 entries (and it should, provided the number of bytes is not evenly divisible by 16).
00 00 00 00 00 00 01 00 2E 64 79 6E 73 79 6D 00 2E 72 65 6C 2E 64 79 6E Total bytes: 24
void byteToHex( char byte, char * hexArray ) ;This prototype should appear in a header file called byteToHex.h.
Although this is a C-like function, it can still be implemented using a C++ compiler.
For example, suppose you determine that the byte contains 1E. Then, hexArray[0] = '1' and hexArray[1] = 'E'. hexArray[2] = '\0', which is the NULL character. This makes hexArray store a valid C string.
makean executable called "hexdump" should be created that behaves as described in the earlier parts of the specifications.
The executable must be NAMED exactly "hexdump" (not "hexDump", "HEXDUMP", "xxd", "myHedDump"), all in lowercase (without the double quotes);
Here are some restrictions and assumptions you can make.
hexdump -w <num> -i <filename>You should allow the user to type in -w followed by a space, followed by a positive number, which will determine how many hex pairs are printed per row (the default value is 16). You must update the addresses correctly to compensate for the number of hex pairs per row.
This command line switch can appear before or after the filename (if the filename exists).
This will be worth about 15 points extra credit (out of 100).
In order to receive credit for this, you must print out the addresses and the text information on the right (i.e., standard OPTION).
Here is an example, where -w 7 is used (7 hex pairs per line.
0000000: 11 11 11 11 11 11 11 ....... 0000007: 11 11 11 11 11 11 11 ....... 000000E: 11 11 08 08 08 08 08 ....... 0000015: 08 08 08 08 08 08 08 ....... 000001C: 08 08 08 08 48 65 6C ....Hel 0000023: 6C 6F 20 57 6F 72 6C lo Worl 000002A: 64 0A d. Total bytes: 44In particular, notice the addresses on the left increase by 7 (in hexadecimal).
This will be worth 10 points.
hexdump -W < <filename>A typical input file would look like:
00 00 00 00 00 00 01 00 2E 64 79 6E 73 79 6D 00 00 00 00 00 00 00 04 00 2E 72 65 6C 2E 64 79 6EYou may only assume the hex pairs are separated by one or more blank spaces or newlines. You MAY NOT assume that there are exactly sixteen hex pairs per line. There may be any number of hex pairs per line (including 0).
Basically, with this -W option, your program will output a byte for each hex pair. Each byte will have the binary pattern given by the hex pair. Thus, if the hex pair is 2E, your program will output a byte 0010 1110 (which is output as a single char).
This output can be output redirected to a result file.
If you run hexdump -p -i <filename> on the result file, it will produce a result that mimics the input file.
00 00 00 00 00 00 01 00 2E 64 79 6E 73 79 6D 00 00 00 00 00 00 00 04 00 2E 72 65 6C 2E 64 79 6E Total bytes: 32In this case, there will be 16 hex pairs per line, because that's what your hexdump program normally does.
This is basically "hexWrite". It takes an ASCII representation of a file, and generates a binary version of that.
This will be worth 20 points of extra credit.
You may assume no other command line options are used when you use -W (thus, no -p, no -i, etc).
Also, you need to read about command line arguments. Again, a decent book on C/C++ should provide this information. To get practice with command line arguments, I would suggest writing a very simple C/C++ program which prints out argc (the number of arguments), as well as the contents of the array in argv. Recall that argv stores strings using C-style strings, not using C++-style strings.
You may want to see if C++ or C has built-in printing options for hexadecimal values. However, more than likely, it will do so in lowercase ('a' through 'f'), instead of uppercase.
If this is so, you should convert it to uppercase. Here's a hint to convert it to uppercase: try to figure out how to write the output to a string instead of the screen (hint: try sprintf in C and ostringstream in C++). Once it's in a string, you can convert the character to uppercase (for example, using the toupper() function).
You may advise other students where to look for this information, if you want, but it's strongly suggested that they do the reading on their own. You should not be passing along code to other students. However, you may point them to webpages that exist in the general public domain. You should not provide code on the Web or other means.
Furthermore, try to learn the material you read as well as you can. I realize all you need to do is look it up enough to get it done, but by learning to play with other features, even though the project doesn't require it, you'll learn it much better, and hopefully recall it in the future.
You may consult other students about the features in the C/C++ language, but you should not be showing your code to other students nor should they be showing it to you. The goal is to learn, NOT to copy from another student, just so you can turn in your project.
|
See the class syllabus for policies concerning email Last Modified: Fri Feb 14 09:13:38 EST 2003 |
|
|
|
|
|