Autumn 2020 CSE 2421 LAB 2
DUE: Tuesday, September 15th, 2020, 11:30:00 p.m.
10 Bonus Points (i.e.10% bonus) awarded if correctly submitted by Monday, September 14th at noon.
Objectives/Skills:
Standard character-based I/O
Preliminary work with scanf/printf
Arithmetic/bitwisestatements
while, for, do-while statements
ASCII character representation
#ifdef preprocessor statements
using the make command
REMINDERS:
Thislabisanindividualassignment.
Waitinguntilthedaythislabisduetostartworkingonitwouldbeaverybadidea.
EverylabrequiresaREADMEfile.Forthislab,createyourfileonstdlinuxinatexteditorandit should be named LAB2README (exactly this name (case matters), with no “extensions”; Linux can identify the file as a text file without any “extension,” such as txt, doc, etc., so do not use these). This file should include the following:
1. Required Header:
BY SUBMITTING THIS FILE TO CARMEN, I CERTIFY THAT I HAVE STRICTLY ADHERED TO THE TENURES OF THE OHIO STATE UNIVERSITY’S ACADEMIC INTEGRITY POLICY WITH RESPECT TO THIS ASSIGNMENT.
THIS IS THE README FILE FOR LAB 2.
Student name:
2. Total amount of time in hours (approximate) to complete the entire lab;
3. Short description of any concerns, interesting problems or discoveries encountered, or comments in general about the contents of the lab;
4. Run gdb on your bit_decode2 executable. Enter data here that describes the interim values you calculated as you created your 8-bit key as you read in each separate digit of the 4 digit key. Show how the values were printed in gdb. For example, what is the value of key after you have read and processed the first digit of the key, then after you have processed the second digit of the key, etc.
5. If you run gdb for any reason on bit_decode1, then describe why you chose to do so, what you did, what you saw.
Youshouldaimtoalwayshandassignmentsinontime.Ifyouarelate(evenbylessthana minute), you will receive 75% of your earned points for the designated grade as long as the assignment is submitted by 11:30:00 pm the following day (NOTE: This is not necessarily the same as the following class day!), based on the due date listed at the top of this document. If you are more than 24 hours late, you will receive a zero for the assignment and your assignment will not be graded at all (so you will get no feedback on your work). Start early, and work steadily on the lab, and you should be able to hand the assignment in on time without a problem. If you are unable to finish on time, you will need to consider whether it is better to submit on time and get full credit for what you have done, or if it would be better totrytofinishandsubmitonedaylate withthe25%penalty.
Any lab submitted that does not build to an executable using the required gcc command (see below) and run without crashing or freezing WILL RECEIVE AN AUTOMATIC GRADE OF ZERO. No exceptions will be made for this rule – to achieve even a single point on a lab your code must minimally compile (without errors or warnings) and execute without crashing or freezing.
It is your responsibility to ensure that your program compiles without errors or warnings and runs on valid input as described without crashing or freezing.
–
We will be taking advantage of a feature of Unix/Linux called make to compile executables and to create the .zip file needed to upload to Carmen.
You are responsible for making sure that your lab submits correctly. Make yourself aware of the CODING_STYLE_IN_C file posted on: https://piazza.com/osu/autumn2020/cse2421jones/resources
in the General Resources section.
GRADING CRITERIA (approximate percentages listed)
Thecodeandalgorithmarewelldocumented,includinganexplanatorycommentfor each function, and comments in the code.
A comment should be included in the main function of the program including the programmer name(s) as well as explaining the nature of the problem and an overall method of the solution (what you are doing, not how).
A comment should be included before each function documenting what the function does (but not details on how it does it).
A short comment should be included for each logical or syntactic block of statements.
(10%)Theprogramshouldbeappropriatetotheassignment,well-structuredand easy to understand without complicated and confusing flow of control. We will not usually deduct points for the efficiency of your code, but if you do something in a way which is clearly significantly less efficient, we may deduct some points.
(10%) There is a description in LAB2README of the gdb results asked for both
bit_decode1 and bit_decode2.
(80%)Theresultsarecorrect,verifiable,andwell-formatted.Theprogram correctly performs as assigned with both the given input and one other (unknown) input file designed to test boundary conditions within your program.
If the grader cannot compile your code using the supplied Makefile with no error or warning messages, you will receive no points.
REVIEW
Recall that Homework 1 had you working with binary values and bitwise operations. Recall that within those bitwise operations, operand 1 and operand 2 could be variables or could be a constant value. For example, if there were declared unsigned char var1=0, then you could set bit 6 and bit 1 to 1, by bitwise OR’ing var1 with 0x42. 0x42==0b 0100 0010. Recall that bit positions start at 0 and increase as you move to the left. 8 bits would have bit positions 0-7, 32 bits would have bit positions 0-31, etc.
LAB DESCRIPTION
PART 1:
From a window on stdlinux bring up a web browser and navigate to the Piazza page for this class. Go to the Resources->Labs link and find a file call Makefile.Lab2. Click on the filename. A window should pop up that says something like:
You have chosen to open:
Makefile.Lab2
which is: Lab2 File from: https://piazza.com
Would you like to save this file?
Click the Save File button.
You can now exit out of the web browser.
From your
1. 2. 3. 4.
5.
stdlinux window, maneuver to the directory $HOME/cse2421.
Create a lab2 directory.
Enter the lab2 directory.
Execute the following command: cp $HOME/Downloads/Makefile.Lab2 Makefile Use the ls command to verify that you now have a file in your current working directory called Makefile.
Use a text editor or the command cat Makefile to inspect the contents of the file. Do not make any changes/edits to this file. We will talk about the contents of this file and how it works during the week of 9/8/20. For the time being, consider it magic.
This file will allow you to create both executables required for this lab and also the .zip file required for this lab. Once you have created a bit_decode.c file,
A. To compile the executable for PART 2, enter the command make bit_decode2 on the command line.
B. To compile the executable for PART 3, enter the
PART 2 (50%):
command make bit_decode1 on the command line. C. To create the .zip file to submit to Carmen, enter the
command make lab2.zip on the command line.
D. If you want to do all three of these things at the same time, enter the command make on the command line.
There is nothing wrong with using make without a parameter almost all the time. The command just does a little extra work. The benefit is that you will know that all of your programs/zip file are up to date.
E. If you use the command make clean, all executable files and the current .zip file will be deleted from your directory. Your .c files will not be touched.
Mandatory file name: bit_decode.c Mandatory executable name: bit_decode2
Write a program that implements an elementary bit stream cipher decoder. An elementary level bit stream cipher is an encryption algorithm that – given a key – encrypts 1 byte of ASCII text into two hexadecimal digits. You will be writing a program that decrypts the two hexadecimal digits into an ASCII character. This program will prompt for a 4-bit pattern to create an 8-bit key. The size of the encrypted message that we want to be able to decode has no limit.
Here is an example:
If we have the ASCII value for the letter ‘t’, then that equates to the hex value 0x74, which represents the binary value 0111 0100. If we bitwise XOR that value with a key, say 0101 0101, then
0111 0100
0101 0101
0010 0001
If we then XOR the encrypted value with the key, we get our ASCII value back.
0010 0001
0101 0101
0111 0100
-> encrypted value 0x21 -> XOR with key
-> ‘t’
-> ‘t’
-> XOR with key
-> encrypted value 0x21
In this program:
1. You must use printf() to prompt the user for the 4-bit key to decrypt the data (e.g.
0110, 1010, etc.)
2. You must use getchar() to read in each ASCII digit of the 4-digit key
3. You must create an 8-bit key using the bitwise OR instruction based on the 4 ASCII values read in. For example, if the user specifies 0110, then your internal key must be 0110 0110. An alternative way to create the key would be to use a bitwise OR instruction and a shift instruction with each ASCII digit.
4. You must use printf() to prompt the user to input the encrypted text message.
5. You must use scanf() to read each 2-digit hexadecimal cipher value. Each 2-digit hexadecimal number will be separated by a single space and a ‘\n’ character (rather than a space after the number) will indicate that the message is complete. Ensure that your format statement in the call to scanf() specifies that only 2 hex digits should be read at a time.
6. You must bitwise XOR the 8-bit key you created with the hex value. This should convert the value to a valid ASCII character.
7. You must use putchar() to output the ASCII character to the screen.
8. When a ‘\n’ character is detected after a 2-digit hex number in read, this indicates that the last hex value read in (not the ‘\n’) was the last encrypted value. You must end your program when this occurs.
Output might look similar to the following: [jones.5684@cse-fl1 lab2]$ bit_decode2
enter 4-bit key: 1011
enter encoded text: cf d3 d2 c8 9b d2 c8 9b da 9b d6 de c8 c8 da dc de this is a message
[jones.5684@cse-fl1 lab2]$
NOTE!! The input above is not the only input that can be entered in to the program that will be expected to work correctly. It is only an example of input. Both lowercase and uppercase hex digits are valid input.
9. You may declare only two variables: the first holds the ASCII value returned from getchar() and later in your program a single hex value from scanf(), the second one holds the cipher key.
PART 3. (50%). Mandatory file name: bit_decode.c
(note that this is the SAME filename as PART 2)
Mandatory executable name: bit_decode1
After you get bit_decode2 working, introduce #ifdef PROMPT and #endif preprocessor statements into your code (you must use the same file, bit_decode.c) that modify your program such that it will not print any prompts. Input will be coming from a redirected input source from the command line. It will help you understand how to do this if you inspect the Makefile supplied with this lab and look at the comments and observe the options used in the gcc statements ( such as -D PROMPT). If you chose not to read Chapter 14 of the Reek book, you may want to reconsider at this point.
Sample output:
[jones.5684@cse-fac1 lab2] bit_decode1 < decode.input this is a new message
[jones.5684@cse-fac1 lab2]
In this example, decode.input contains these two lines:
0110
12 0E 0F 15 46 0F 15 46 07 46 08 03 11 46 0B 03 15 15 07 01 03
CONSTRAINTS:
All source code files (.c files/.h files) and your Makefile that are submitted to Carmen as a part of this lab must include the following at the top of each file:
BY SUBMITTING THIS FILE TO CARMEN, I CERTIFY THAT I HAVE STRICTLY ADHERED TO THE TENURES OF THE OHIO STATE UNIVERSITY’S ACADEMIC INTEGRITY POLICY WITH RESPECT TO THIS ASSIGNMENT.
If you choose not to put the above comment in your file, you will receive no points for the lab.
Youmustcommentyourcode
YoumustputyounameinacommentatthetopofallsourcecodefilesandyourMakefile
All requirements in items 1-9 above must be followed
All of the code you write must be in main(). This program is small enough (it should only take up about half a screen) that you do not need to create other functions.
You must correctly use preprocessor directives #ifdef PROMPT and #endif to omit requirement #1 and #4 to complete Part 3.
LAB SUBMISSION
Always be sure your linux prompt reflects the correct directory or folder where all of your files to be submitted reside. If you are not in the directory with your files, the following will not work correctly.
You must submit all your lab assignments electronically to Carmen in .zip file format. The .zip file will be created by using the make command (See PART 1.) Once you execute the command, you should find a file in your lab2 directory called lab2.zip; this is the file that must be uploaded to Carmen.
NOTE:
• It is YOUR responsibility to make sure your code can compile and run on CSE department server stdlinux.cse.ohio-state.edu, using gcc -ansi –pedantic –g without generating any errors or warnings or segmentation faults, etc. Any program that generates errors or warnings when compile or does not run without system errors will receive 0 points. No exceptions! This means you may want to go back to the lab1 instructions and re-read the section on how to test that your .zip file is complete.