Question: Assignment 1 : Base 6 4 In this assignment, you will write a simple base 6 4 encoding utility to familiarize yourself with the basics

Assignment 1: Base64
In this assignment, you will write a simple base64 encoding utility to familiarize yourself with the basics of C programming. You will be using, exclusively, a small subset of the C standard library to accomplish this task. You will practice:
Parsing command-line arguments
Using the standard io library features
Handling errors
Manipulating data and working with arrays
Reading and understanding man-page documentation
Learning Outcomes
How to write a C program to solve a problem? (Module 1 MLO 4)
How can programs invoke OS services using system calls? (Module 1 MLO 5)
How do you interact with the user in C programs? (Module 1 MLO 6)
How are C programs transformed into an executable form? (Module 1 MLO 7)
Specification
You will write a program which meets the following specification, presented in a man-page format,
NAME
base64- Base64 encode data and print to standard output
SYNOPSIS
base64[FILE]
DESCRIPTION
Base64 encode FILE, or standard input, and output to standard output.
With no FILE, or if FILE is -, read from standard input.
Encoded lines are wrapped every 76 characters.
The data are encoded according to the standard algorithm and standard base64 alphabet described in RFC 4648.
STDIN
The standard input shall be used only if no FILE operand is specified, or if the FILE operand is -.
STDOUT
The standard output shall contain the base64 encoded output, wrapped to 76 characters.
STDERR
The standard error shall be used only for error reporting. All errors must be reported to standard error.
EXIT STATUS
0, if successful.
>0, if an error occurs.
Please refer to man(1) for a detailed breakdown of how man pages are structured. If you are not familiar with man pages, they are locally installed operating system documentation, and can be accessed using the man command while logged into os1. The first place to start learning to read man pages is to read the documentation on the man pager, itself: $ man 1 man. Please note, man pages are specific to the system they are onwhile online man pages can be a great quick reference, always double check local man pages for things that may differ. There are often critical differences between the two.
You will write a program named base64, which performs base 64 encoding on its input. Input to be base 64 encoded may be provided either via standard input, or via a file where the name of the file is provided as a command-line argument. As a special case, if the file name is -, standard input will be used. This is a widely held convention with POSIX utilitiesin the event that a user wished to actually provide the file named - as input, they would simply need to disambiguate by providing additional path information, as in ./-.
Some starter code is provided
Selected Topics
Program Arguments
Standard Streams
Exit Status
Understanding Encoding
Constraints
Your program may not use a feature of the C language called Variable Length Arrays (VLAs). There are arrays that are declared with extents that are not compile-time constant expressions; for example,
size_t i = strlen(input);
char buffer[i]; /*<- Prohibited! */
strcpy(buffer, input);
VLAs are generally considered a bad programming practice, and are a common cause of serious bugs and security holesthey can also be a source of difficult-to-diagnose issues in your own program, so we will avoid using them. Additionally, many compilers just dont support them because they are complicated to implement with a lot of edge cases. You can use the compiler option -Werror=vla when compiling your code (the makefile will do this automatically for you) to emit an error if you accidentally use a VLA.
Note that, macros are commonly used to avoid magic numbers in array extents, as in,
#define BUF_SIZE 1024
/*...*/
char buf[BUF_SIZE];
The above is not an example of VLA, because macro replacement happens during preprocessingby the time the C compiler is involved, all it sees is char buf[1024]and 1024 is a compile-time constant expression. The rule of thumb is, if you reference an actual variable (not a macro), or call a function, its not a compile-time constant expression!
Your program must run in constant, O(1), space complexitythis means, regardless of the size of the input provided, your program must use the same amount of memory to process it. This precludes, for example, buffering the entire input into a dynamically resized array and then processing itdont use malloc(), you dont need it!
Your program must compile against the c99 language standard, using the compiler option -std=c99(the makefile will also handle this for you). We are not using the gnu99 standard in this class, as it is more relaxed on constraints and less likely to help you catch common programming mistakes.
When your program encounters an error (such as failure to open a file, input, or output error) it must print an error message to stderr, and exit with a non-zero exit status

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!