Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need some help making these codes as simple(basic) as possible for decoding.c and encoding.c. If you rewrite this C code to make it easily

I need some help making these codes as simple(basic) as possible for decoding.c and encoding.c. If you rewrite this C code to make it easily readable for C beginners.

Here is what this code is supposed to do:

Objective:

The purpose of this assignment is to learn and use bitwise operations. In this homework, as an option, you can find a partner in your class so you can work together to finish the homework. You don't necessarily need to meet each other in face.

Description:

Given an arbitrary text file, your program will pack the ascii code characters into integers. Specfically, 4 characters will be encoded into one unsigned integer. Then, you modify your integer shifting it k bits in circle as an "encryption", where k is the shared key between you and your partner for the encryption and decryption. After that, you save your integer into a binary file. Your partner's program will read the binary file, and decode the contents into the original text, and save the text in another text file.

If you don't want to work with a partner, that is fine. You can use my object file and header file to do either encoding or decoding.

In "encoding.c", you have a function "encoding" which opens the text fiile "HW8.txt" for reading and the binary file "HW8.bin" for wrting. Then, read 4 characters at a time, pack them into an integer by a function "pack", manipulate the integer by "encryption", and write the encrypted integer into "HW8.bin" until the end of "HW8.txt". At the end of file, you may save some NULL ('\0') characters to make it 4 characters for the last integer. Close all open files.

In "decoding.c", you have a function "decoding" which opens "HW8.bin" for reading and a text file "HW8c.txt" for writing. Then, read an integer at a time, use the key from your partner to manipulate the integer by a function "decryption", unpack it into 4 characters by a function called "unpack", and save the 4 characters into "HW8c.txt". Close all open files.

The "encryption" function will shift an integer k bits to the left (or right) in circle in "encoding.c". Here k is the secrete key you share with your partner. The "decryption" will restore the encryption process in "decoding.c". That is, shifting k bits to the right (or left) in circle.

If you work in team, one person implement "encoding.c" and the other implement "decoding.c". You can give your header file and object file to your partner (encoding.h and encoding.o, or decoding.h and decoding.o), but not the source code. Your key and direction of circlic shift should be described and shared in the header file.

Presumably, you don't have your partner's "c" file, but you link the object file your partner provides, so your Makefile will be just using an existing object file for linking. Of course, you may just link with my object file. Then, you don't have a partner.

Your "main.c" is simple: display the content of "HW8.txt", call encoding, call decoding, and display the contents of "HW8c.txt". You can display the content of a file by a system call as follows, for example: system ("cat HW8.txt");

You should have the corresponding header file(s) for compilation. Again, you don't share your source code, but you share the header file(s) and the object files. The key is described in the header file(s).

My codes:

main.c

#include // for system #include "encoding.h" // for encoding #include "decoding.h" // for decoding int main() { char itxt[] = "HW8.txt"; char bin[] = "HW8.bin"; char otxt[] = "HW8c.txt"; system("cat HW8.txt"); encoding(itxt, bin); decoding(otxt, bin); system("cat HW8c.txt"); return 1; } 

decoding.h

// input binary file: ibin[] // output text bile: otxt[] // k = -3: cyclicly shift the integer to the right 3 bits. // 1) read an integer from the input binary file // 2) cyclic shift the integer 3 bits to the right; // 2) unpack the 4 characters from the shifted integer; // 4) write the characters into the output textfile otxt; void decoding(char otxt[], char ibin[]); 

decoding.c

#include //FILE etc #include //exit() etc #include //isascii() etc #include "decoding.h" void decoding(char otxt[], char ibin[]) { int c, MASK=0xFF; char a; FILE *otp, *ibp; otp = fopen(otxt, "w"); if (!otp) {printf("cannot open: %s ", otxt); exit(1);} ibp = fopen(ibin, "rb"); if (!ibp) {printf("cannot open: %s ", ibin); exit(1);} while (fread(&c, sizeof(int), 1, ibp) == 1) { c = (0x1FFFFFFF & c>>3) | c<<29; a = (c >> 8*3) & MASK; fputc(a, otp); a = (c >> 8*2) & MASK; if (isascii(a)) fputc(a, otp); a = (c >> 8*1) & MASK; if (isascii(a)) fputc(a, otp); a = (c >> 8*0) & MASK; if (isascii(a)) fputc(a, otp); } fclose(otp); fclose(ibp); printf("Decoding completed! "); } 

encoding.h

// k equal 3: shift 3 bits to the left cyclicly. // input text file name: itxt[] // output binary file name: obin[] // 1) read 4 characters from input text file // (if not enough characters, use EOF or whatever) // 2) pack the 4 characters into an integer; // 3) cyclic shift the integer 3 bits to the left; // 4) write the shifted integer into obin (binary); void encoding(char itxt[], char obin[]); 

encoding.c

#include #include #include #include "decoding.h"

void decoding(char otxt[], char ibin[]) {

int c, MASK=0xFF; char a; FILE *otp, *ibp;

otp = fopen(otxt, "w");

ibp = fopen(ibin, "rb");

while (fread(&c, sizeof(int), 1, ibp) == 1) {

c = (0x1FFFFFFF & c>>3) | c<<29;

a = (c >> 8*3) & MASK; fputc(a, otp);

a = (c >> 8*2) & MASK; if (isascii(a)) fputc(a, otp);

a = (c >> 8*1) & MASK; if (isascii(a)) fputc(a, otp);

a = (c >> 8*0) & MASK; if (isascii(a)) fputc(a, otp); }

fclose(otp); fclose(ibp);

printf("Decoding completed! "); }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing

Authors: David Kroenke

11th Edition

0132302675, 9780132302678

More Books

Students also viewed these Databases questions

Question

Provide examples of Dimensional Tables.

Answered: 1 week ago