Compare commits

...

41 Commits

Author SHA1 Message Date
cae1791eba Add a9 file submissions 2023-12-05 13:33:01 -04:00
6ff99deff7 Add a8 file submissions 2023-12-05 13:32:04 -04:00
c6dd294bc0 Remove unused import a9 2023-12-05 13:29:57 -04:00
6a6fd9c37c Tests in a9 2023-12-05 13:28:08 -04:00
112535e8e7 Use LF in a9 2023-12-05 13:28:08 -04:00
93635bc1d6 Remove unneeded function 2023-12-05 12:57:16 -04:00
530ab9a135 Add comments and error checking to A9 2023-12-05 12:47:36 -04:00
bea609e01c Use exit define 2023-12-04 20:55:45 -04:00
136a4fe960 Code reformatting 2023-12-04 20:55:07 -04:00
123e0f56bc Add test shell scripts 2023-12-04 20:53:40 -04:00
e79158e3f1 Make Tests pass 2023-12-04 20:53:31 -04:00
73234d0764 Make operation reversable
Before didn't write the final IEND chunk
2023-12-04 20:29:04 -04:00
0a18d16eb0 For submission 2023-12-04 20:24:05 -04:00
862af7192c Minor code cleanup 2023-12-04 20:19:20 -04:00
a62d97f575 Reformat stuff 2023-12-04 20:09:46 -04:00
d81bbe423e WORKS ON TEST1 2023-12-04 20:02:21 -04:00
9f9d7a1925 Fix 2023-12-04 19:44:06 -04:00
2769651fe1 Just need to bugfix now 2023-12-04 19:35:12 -04:00
3aa01cdc2b Working reader 2023-12-04 18:54:29 -04:00
3aad46748a Add an implementation, still just trying to read 2023-12-04 18:39:21 -04:00
18669ebf1c Add skeleton of a9 2023-12-04 16:17:13 -04:00
b95fd534fb Add a9 docs 2023-12-04 16:08:35 -04:00
f049e8aeff Cleanup 2023-12-02 23:16:56 -04:00
025b2640fc Pass tests 2023-12-02 22:41:41 -04:00
64f9b63215 Fix memory leaks and code cleanup 2023-12-02 21:55:24 -04:00
8a5b7ce7fc Clion shit 2023-12-02 21:31:27 -04:00
06b028bfb2 Fixes test 4 2023-12-02 21:31:15 -04:00
7d4ecdaba6 Add comments and change implementation slightly 2023-12-02 21:30:51 -04:00
4494f47a32 fuuuuuuck this 2023-11-28 21:42:42 -04:00
5ebb6a96cc fuck this shit 2023-11-28 21:31:01 -04:00
6189cf6a3b Move to linked list implementation 2023-11-28 21:02:49 -04:00
fb6ec1dc98 Separate data structures into own file 2023-11-28 20:33:23 -04:00
ca2d32dc34 Better implemenation? 2023-11-28 20:12:47 -04:00
9a170e3334 Fix bug 2023-11-28 19:44:53 -04:00
0668095e82 Update makefile for clion highlighting 2023-11-28 19:03:30 -04:00
4cf7a4e698 Fix time calculation 2023-11-28 17:21:49 -04:00
531c063830 Initial implementation with bugs 2023-11-28 13:19:22 -04:00
131210cbab Add gitignore to a8 2023-11-28 10:50:08 -04:00
152e31b38d Configs and makefile fixes 2023-11-28 10:48:55 -04:00
9f1f82ad43 Add phony 2023-11-28 10:43:57 -04:00
0bff959f1b Modify makefile 2023-11-28 10:42:46 -04:00
55 changed files with 1540 additions and 36 deletions

2
Assignment8/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
a.out
student_out

8
Assignment8/.idea/.gitignore generated vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

86
Assignment8/.idea/editor.xml generated Normal file
View File

@ -0,0 +1,86 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="BackendCodeEditorSettings">
<option name="/Default/CodeStyle/CodeFormatting/CppClangFormat/EnableClangFormatSupport/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/EditorConfig/EnableClangFormatSupport/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_MULTILINE_BINARY_EXPRESSIONS_CHAIN/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_MULTILINE_CALLS_CHAIN/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_MULTILINE_EXPRESSION/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_MULTILINE_FOR_STMT/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_MULTIPLE_DECLARATION/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_TERNARY/@EntryValue" value="ALIGN_ALL" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/BLANK_LINES_AROUND_CLASS_DEFINITION/@EntryValue" value="1" type="int" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/KEEP_BLANK_LINES_IN_DECLARATIONS/@EntryValue" value="2" type="int" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/KEEP_BLANK_LINES_IN_CODE/@EntryValue" value="2" type="int" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/KEEP_USER_LINEBREAKS/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/INDENT_CASE_FROM_SWITCH/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/INDENT_COMMENT/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/INT_ALIGN_EQ/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SIMPLE_BLOCK_STYLE/@EntryValue" value="DO_NOT_CHANGE" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_COMMA_IN_TEMPLATE_ARGS/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_COMMA_IN_TEMPLATE_PARAMS/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_FOR_SEMICOLON/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_FOR_SEMICOLON/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_UNARY_OPERATOR/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_WITHIN_ARRAY_ACCESS_BRACKETS/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_WITHIN_CAST_EXPRESSION_PARENTHESES/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_WITHIN_EMPTY_INITIALIZER_BRACES/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_WITHIN_EMPTY_METHOD_PARENTHESES/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_WITHIN_INITIALIZER_BRACES/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPECIAL_ELSE_IF_TREATMENT/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/WRAP_AFTER_BINARY_OPSIGN/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/WRAP_BEFORE_TERNARY_OPSIGNS/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/TYPE_DECLARATION_BRACES/@EntryValue" value="END_OF_LINE" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/OTHER_BRACES/@EntryValue" value="END_OF_LINE" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/CASE_BLOCK_BRACES/@EntryValue" value="END_OF_LINE" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/BLANK_LINES_AROUND_FUNCTION_DECLARATION/@EntryValue" value="1" type="int" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/BLANK_LINES_AROUND_FUNCTION_DEFINITION/@EntryValue" value="1" type="int" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/NAMESPACE_INDENTATION/@EntryValue" value="All" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_MULTILINE_ARGUMENT/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_MULTILINE_EXTENDS_LIST/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_MULTILINE_PARAMETER/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_MULTILINE_TYPE_ARGUMENT/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ALIGN_MULTILINE_TYPE_PARAMETER/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/BLANK_LINES_AROUND_DECLARATIONS/@EntryValue" value="0" type="int" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/INDENT_ACCESS_SPECIFIERS_FROM_CLASS/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/INDENT_CLASS_MEMBERS_FROM_ACCESS_SPECIFIERS/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/LINE_BREAK_AFTER_COLON_IN_MEMBER_INITIALIZER_LISTS/@EntryValue" value="ON_SINGLE_LINE" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/MEMBER_INITIALIZER_LIST_STYLE/@EntryValue" value="DO_NOT_CHANGE" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/PLACE_NAMESPACE_DEFINITIONS_ON_SAME_LINE/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_COLON_IN_BITFIELD_DECLARATOR/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_COLON_IN_BITFIELD_DECLARATOR/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_EXTENDS_COLON/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_EXTENDS_COLON/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_FOR_COLON/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_FOR_COLON/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_AFTER_REF_IN_DATA_MEMBER/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_REF_IN_DATA_MEMBERS/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_PTR_IN_ABSTRACT_DECL/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_REF_IN_ABSTRACT_DECL/@EntryValue" value="true" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_TEMPLATE_ARGS/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_BEFORE_TEMPLATE_PARAMS/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_WITHIN_DECLARATION_PARENTHESES/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_WITHIN_EMPTY_BLOCKS/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_WITHIN_EMPTY_TEMPLATE_PARAMS/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_WITHIN_TEMPLATE_ARGS/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/SPACE_WITHIN_TEMPLATE_PARAMS/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/WRAP_BEFORE_INVOCATION_LPAR/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/WRAP_AFTER_INVOCATION_LPAR/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/WRAP_BEFORE_INVOCATION_RPAR/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/WRAP_BEFORE_DECLARATION_LPAR/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/WRAP_AFTER_DECLARATION_LPAR/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/WRAP_BEFORE_DECLARATION_RPAR/@EntryValue" value="false" type="bool" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/WRAP_ARGUMENTS_STYLE/@EntryValue" value="WRAP_IF_LONG" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/WRAP_PARAMETERS_STYLE/@EntryValue" value="WRAP_IF_LONG" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/BREAK_TEMPLATE_DECLARATION/@EntryValue" value="LINE_BREAK" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/NAMESPACE_DECLARATION_BRACES/@EntryValue" value="END_OF_LINE" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/FREE_BLOCK_BRACES/@EntryValue" value="END_OF_LINE" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/INVOCABLE_DECLARATION_BRACES/@EntryValue" value="END_OF_LINE" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/ANONYMOUS_METHOD_DECLARATION_BRACES/@EntryValue" value="END_OF_LINE" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/INITIALIZER_BRACES/@EntryValue" value="END_OF_LINE_NO_SPACE" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/INDENT_STYLE/@EntryValue" value="Tab" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/INDENT_SIZE/@EntryValue" value="4" type="int" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/CONTINUOUS_LINE_INDENT/@EntryValue" value="Double" type="string" />
<option name="/Default/CodeStyle/CodeFormatting/CppFormatting/TAB_WIDTH/@EntryValue" value="4" type="int" />
</component>
</project>

19
Assignment8/.idea/misc.xml generated Normal file
View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MakefileSettings">
<option name="linkedExternalProjectsSettings">
<MakefileProjectSettings>
<option name="buildTarget" value="build" />
<option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="modules">
<set>
<option value="$PROJECT_DIR$" />
</set>
</option>
<option name="version" value="2" />
</MakefileProjectSettings>
</option>
</component>
<component name="MakefileWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
</project>

View File

@ -0,0 +1,7 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="1" type="CLionNativeAppRunConfigurationType" PROGRAM_PARAMS="F" REDIRECT_INPUT="true" REDIRECT_INPUT_PATH="$PROJECT_DIR$/in/all_known_in_advance_spaces.in" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" PASS_PARENT_ENVS_2="true" PROJECT_NAME="Assignment8" TARGET_NAME="build" CONFIG_NAME="build" version="1" RUN_PATH="a.out">
<method v="2">
<option name="CLION.COMPOUND.BUILD" enabled="true" />
</method>
</configuration>
</component>

View File

@ -0,0 +1,8 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="2" type="MAKEFILE_TARGET_RUN_CONFIGURATION" factoryName="Makefile">
<makefile filename="C:\Users\Isaac\Documents\CS3413\Assignment8\makefile" target="2" workingDirectory="" arguments="">
<envs />
</makefile>
<method v="2" />
</configuration>
</component>

View File

@ -0,0 +1,8 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="3" type="MAKEFILE_TARGET_RUN_CONFIGURATION" factoryName="Makefile">
<makefile filename="C:\Users\Isaac\Documents\CS3413\Assignment8\makefile" target="3" workingDirectory="" arguments="">
<envs />
</makefile>
<method v="2" />
</configuration>
</component>

View File

@ -0,0 +1,8 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="4" type="MAKEFILE_TARGET_RUN_CONFIGURATION" factoryName="Makefile">
<makefile filename="C:\Users\Isaac\Documents\CS3413\Assignment8\makefile" target="4" workingDirectory="" arguments="">
<envs />
</makefile>
<method v="2" />
</configuration>
</component>

View File

@ -0,0 +1,7 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="5" type="CLionNativeAppRunConfigurationType" PROGRAM_PARAMS="F" REDIRECT_INPUT="true" REDIRECT_INPUT_PATH="$PROJECT_DIR$/in/time_dependent_second_spaces.in" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" PASS_PARENT_ENVS_2="true" PROJECT_NAME="Assignment8" TARGET_NAME="build" CONFIG_NAME="build" version="1" RUN_PATH="a.out">
<method v="2">
<option name="CLION.COMPOUND.BUILD" enabled="true" />
</method>
</configuration>
</component>

View File

@ -0,0 +1,7 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="6" type="CLionNativeAppRunConfigurationType" PROGRAM_PARAMS="C" REDIRECT_INPUT="true" REDIRECT_INPUT_PATH="$PROJECT_DIR$/in/time_dependent_second_spaces.in" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" PASS_PARENT_ENVS_2="true" PROJECT_NAME="Assignment8" TARGET_NAME="build" CONFIG_NAME="build" version="1" RUN_PATH="a.out">
<method v="2">
<option name="CLION.COMPOUND.BUILD" enabled="true" />
</method>
</configuration>
</component>

View File

@ -0,0 +1,8 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="all" type="MAKEFILE_TARGET_RUN_CONFIGURATION" factoryName="Makefile">
<makefile filename="C:\Users\Isaac\Documents\CS3413\Assignment8\makefile" target="all" workingDirectory="" arguments="">
<envs />
</makefile>
<method v="2" />
</configuration>
</component>

View File

@ -0,0 +1,8 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="build" type="MAKEFILE_TARGET_RUN_CONFIGURATION" factoryName="Makefile">
<makefile filename="C:\Users\Isaac\Documents\CS3413\Assignment8\makefile" target="build" workingDirectory="" arguments="">
<envs />
</makefile>
<method v="2" />
</configuration>
</component>

View File

@ -0,0 +1,8 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="clean" type="MAKEFILE_TARGET_RUN_CONFIGURATION" factoryName="Makefile">
<makefile filename="C:\Users\Isaac\Documents\CS3413\Assignment8\makefile" target="clean" workingDirectory="" arguments="">
<envs />
</makefile>
<method v="2" />
</configuration>
</component>

View File

@ -0,0 +1,8 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="test" type="MAKEFILE_TARGET_RUN_CONFIGURATION" factoryName="Makefile">
<makefile filename="C:\Users\Isaac\Documents\CS3413\Assignment8\makefile" target="test" workingDirectory="" arguments="">
<envs />
</makefile>
<method v="2" />
</configuration>
</component>

6
Assignment8/.idea/vcs.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>

104
Assignment8/code/disk.c Normal file
View File

@ -0,0 +1,104 @@
#include <stdlib.h>
#include <stdio.h>
#include "disk.h"
DiskQueue *createDiskQueue() {
DiskQueue *queue = malloc(sizeof(DiskQueue));
if (queue == NULL) {
printf("Error allocating memory for queue\n");
exit(EXIT_FAILURE);
}
queue->head = NULL;
queue->size = 0;
return queue;
}
void enqueue(DiskQueue *queue, int time, int position) {
DiskRequest *request = malloc(sizeof(DiskRequest));
if (request == NULL) {
printf("Error allocating memory for request\n");
exit(EXIT_FAILURE);
}
request->time = time;
request->position = position;
request->next = NULL;
if (queue->head == NULL) {
queue->head = request;
} else {
DiskRequest *current = queue->head;
while (current->next != NULL) {
current = current->next;
}
current->next = request;
}
queue->size++;
}
void delete(DiskQueue *queue, DiskRequest request) {
DiskRequest *current = queue->head;
DiskRequest *previous = NULL;
while (current != NULL) {
if (current->position == request.position && current->time == request.time) {
if (previous == NULL) {
queue->head = current->next;
} else {
previous->next = current->next;
}
queue->size--;
free(current);
return;
}
previous = current;
current = current->next;
}
printf("Error: Request not found\n");
exit(EXIT_FAILURE);
}
DiskRequest *dequeue(DiskQueue *queue) {
DiskRequest *head = queue->head;
queue->head = head->next;
queue->size--;
return head;
}
void destroyDiskQueue(DiskQueue *queue) {
DiskRequest *current = queue->head;
DiskRequest *next = NULL;
while (current != NULL) {
next = current->next;
free(current);
current = next;
}
free(queue);
}
int calcDistance(int position, int destination) {
if (position > destination) {
return destination + (9999 + 1 - position);
} else {
return destination - position;
}
}
DiskRequest* findClosest(DiskQueue *queue, int position, double time) {
DiskRequest *current = queue->head;
DiskRequest *closest = NULL;
while (current != NULL) {
if (current->time <= time) { // Filter out requests that haven't come in yet
if (closest == NULL) { // If there is no closest yet, set it to the current
closest = current;
} else {
int closestDistance = calcDistance(position, closest->position); // Distance from the closest to the current position, negative means down (bad)
int currentDistance = calcDistance(position, current->position); // Distance from the current to the current position, negative means down (bad
if (currentDistance < closestDistance) { // If the current is closer than the closest
closest = current;
}
}
}
current = current->next;
}
return closest;
}

30
Assignment8/code/disk.h Normal file
View File

@ -0,0 +1,30 @@
#pragma once
typedef struct DiskRequest {
struct DiskRequest *next;
int position;
int time;
} DiskRequest;
typedef struct DiskQueue {
DiskRequest *head;
int size;
} DiskQueue;
typedef enum DiskDirection {
UP, DOWN
} DiskDirection;
DiskQueue *createDiskQueue();
void enqueue(DiskQueue *queue, int time, int position);
void delete(DiskQueue *queue, DiskRequest request);
DiskRequest *dequeue(DiskQueue *queue);
DiskRequest *findClosest(DiskQueue *queue, int position, double time);
void destroyDiskQueue(DiskQueue *queue);
int calcDistance(int position, int destination);

95
Assignment8/code/main.c Normal file
View File

@ -0,0 +1,95 @@
#include <stdio.h>
#include <stdlib.h>
#include "disk.h"
// Global tracking variables
DiskDirection currentDirection = UP;
int movement = 0;
double seekTime = 0;
// Array for storing the requests
DiskQueue *queue;
double timeToProcessRequest(int position, int destination) {
//The time (a floating point number) required to process a request is computed by distance the head travels divided by 5
//Plus additional 15 milliseconds penalty if the direction has to change (for FCFS)
double time = 0;
// Calculated this way so that positive means up and negative means down
int distance = destination - position;
DiskDirection direction = (distance > 0) ? UP : DOWN;
time += abs(distance) / 5.0;
if (direction != currentDirection) {
time += 15;
currentDirection = direction;
}
return time;
}
int diskMovement(int position, int destination) {
int distance = destination - position;
return abs(distance);
}
// Your simulated disk is of size 10000, numbered from 0 to 9999.
// In first come first the time the request comes in is irrelevant
void fcfs(int start) {
int position = start;
while (queue->size > 0) {
DiskRequest *request = dequeue(queue);
while (seekTime < request->time) { seekTime++; }
seekTime += timeToProcessRequest(position, request->position);
movement += diskMovement(position, request->position);
position = request->position;
free(request);
}
}
void cscan(int start) {
int position = start;
while (queue->size > 0) {
DiskRequest *request = findClosest(queue, position, seekTime);
if (request == NULL) {
seekTime++;
} else {
if (request->position < position) {
// + 1 simulates the time it takes to move to the end of the disk (it is a circular disk)
seekTime += timeToProcessRequest(position, 9999 + 1);
movement += diskMovement(position, 9999 + 1);
position = 0;
// Rescan for closest, so we can efficiently handle requests
request = findClosest(queue, position, seekTime);
}
seekTime += timeToProcessRequest(position, request->position);
movement += diskMovement(position, request->position);
position = request->position;
}
if (request != NULL) {
delete(queue, *request);
}
}
}
int main(int argc, char **argv) {
int position, time;
char algorithm = argv[1][0];
int start = 0;
queue = createDiskQueue();
while (EOF != (scanf("%i %i\n", &position, &time))) {
enqueue(queue, time, position);
}
if (algorithm == 'F') {
fcfs(start);
printf("Movement:%i Time:%.1lf\n", movement, seekTime);
} else if (algorithm == 'C') {
cscan(start);
// Stupid printf difference to pass tests, could call once after if statement
printf("Movement: %i Time:%.1lf\n", movement, seekTime);
}
destroyDiskQueue(queue);
return 0;
}

View File

@ -1,34 +0,0 @@
#include <stdio.h>
char currentDirection;
void fcfs(int start){
int movement = 0;
double time = 0;
printf("Movement: %i Time:%.1lf\n", movement, time);
}
void cscan(int start){
int movement = 0;
double time = 0;
printf("Movement: %i Time:%.1lf\n", movement, time);
}
int main (int argc, char** argv){
int position, time;
char algorithm = argv[1][0];
int start = 0;
currentDirection = 'a';
while ( EOF!=(scanf("%i %i\n",&position,&time)))
{
printf("Delete me: position %i, Delete me: time %i\n",position,time);
}
if (algorithm == 'F'){
fcfs(start);
}else if ( algorithm == 'C'){
cscan(start);
}
return 0;
}

View File

@ -0,0 +1,14 @@
| Initial position | Next Position | Distance | Time From Distance | Time from Change Direction | Total Time |
|------------------|---------------|----------|--------------------|----------------------------|------------|
| 0 | 93 | 93 | 18.6 | 0 | 18.6 |
| 93 | 183 | 90 | 18 | 0 | 18 |
| 183 | 37 | 146 | 29.2 | 15 | 44.2 |
| 37 | 122 | 85 | 17 | 0 | 17 |
| 122 | 14 | 108 | 21.6 | 15 | 36.6 |
| 14 | 124 | 110 | 22 | 0 | 22 |
| 124 | 65 | 59 | 11.8 | 15 | 26.8 |
| 65 | 67 | 2 | 0.4 | 0 | 0.4 |
| Total Distance | Total Time |
|----------------|------------|
| 693 | 183 |

View File

@ -1,5 +1,12 @@
all:
gcc -g -O0 ./code/*.c -lm
.PHONY: clean build test all
clean:
mkdir -p ./student_out
rm -f ./student_out/*
rm -f ./a.out
build:
gcc -g -O0 ./code/main.c ./code/disk.c -lm
1:
./a.out F < in/all_known_in_advance_spaces.in > student_out/f-in-advance.out && diff student_out/f-in-advance.out out/f-in-advance.out
2:
@ -13,3 +20,6 @@ all:
6:
./a.out C < in/time_dependent_second_spaces.in > student_out/c-time_dependent_second.out && diff student_out/c-time_dependent_second.out out/c-time_dependent_second.out
test: build 1 2 3 4 5 6
all: clean build test

Binary file not shown.

8
Assignment9/.idea/.gitignore generated vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

2
Assignment9/.idea/Assignment9.iml generated Normal file
View File

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<module classpath="CMake" type="CPP_MODULE" version="4" />

8
Assignment9/.idea/cmake.xml generated Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CMakeSharedSettings">
<configurations>
<configuration PROFILE_NAME="Debug" ENABLED="true" GENERATION_DIR="build" CONFIG_NAME="Debug" />
</configurations>
</component>
</project>

4
Assignment9/.idea/misc.xml generated Normal file
View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
</project>

8
Assignment9/.idea/modules.xml generated Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/Assignment9.iml" filepath="$PROJECT_DIR$/.idea/Assignment9.iml" />
</modules>
</component>
</project>

View File

@ -0,0 +1,8 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="All CTest" type="CTestRunConfiguration" factoryName="CTestRun" PROGRAM_PARAMS="--extra-verbose" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" WORKING_DIR="file://$CMakeCurrentLocalGenerationDir$" PASS_PARENT_ENVS_2="true" CONFIG_NAME="Debug" RUN_PATH="$CTestCurrentExecutableName$" EXPLICIT_BUILD_TARGET_NAME="all" TEST_MODE="PATTERN">
<method v="2">
<option name="com.jetbrains.cidr.execution.CidrBuildBeforeRunTaskProvider$BuildBeforeRunTask" enabled="true" />
<option name="BeforeTestRunTask" enabled="true" />
</method>
</configuration>
</component>

View File

@ -0,0 +1,7 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Assignment9" type="CMakeRunConfiguration" factoryName="Application" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" PASS_PARENT_ENVS_2="true" PROJECT_NAME="Assignment9" TARGET_NAME="Assignment9" CONFIG_NAME="Debug" RUN_TARGET_PROJECT_NAME="Assignment9" RUN_TARGET_NAME="Assignment9">
<method v="2">
<option name="com.jetbrains.cidr.execution.CidrBuildBeforeRunTaskProvider$BuildBeforeRunTask" enabled="true" />
</method>
</configuration>
</component>

View File

@ -0,0 +1,7 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Test1" type="CMakeRunConfiguration" factoryName="Application" PROGRAM_PARAMS="../tests/test1.png" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" PASS_PARENT_ENVS_2="true" PROJECT_NAME="Assignment9" TARGET_NAME="Assignment9" CONFIG_NAME="Debug" RUN_TARGET_PROJECT_NAME="Assignment9" RUN_TARGET_NAME="Assignment9">
<method v="2">
<option name="com.jetbrains.cidr.execution.CidrBuildBeforeRunTaskProvider$BuildBeforeRunTask" enabled="true" />
</method>
</configuration>
</component>

View File

@ -0,0 +1,7 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Test2" type="CMakeRunConfiguration" factoryName="Application" PROGRAM_PARAMS="../tests/test2.png" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" PASS_PARENT_ENVS_2="true" PROJECT_NAME="Assignment9" TARGET_NAME="Assignment9" CONFIG_NAME="Debug" RUN_TARGET_PROJECT_NAME="Assignment9" RUN_TARGET_NAME="Assignment9">
<method v="2">
<option name="com.jetbrains.cidr.execution.CidrBuildBeforeRunTaskProvider$BuildBeforeRunTask" enabled="true" />
</method>
</configuration>
</component>

View File

@ -0,0 +1,7 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Test3" type="CMakeRunConfiguration" factoryName="Application" PROGRAM_PARAMS="../tests/test3.png" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" PASS_PARENT_ENVS_2="true" PROJECT_NAME="Assignment9" TARGET_NAME="Assignment9" CONFIG_NAME="Debug" RUN_TARGET_PROJECT_NAME="Assignment9" RUN_TARGET_NAME="Assignment9">
<method v="2">
<option name="com.jetbrains.cidr.execution.CidrBuildBeforeRunTaskProvider$BuildBeforeRunTask" enabled="true" />
</method>
</configuration>
</component>

View File

@ -0,0 +1,7 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Test4" type="CMakeRunConfiguration" factoryName="Application" PROGRAM_PARAMS="../tests/test4.png" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" PASS_PARENT_ENVS_2="true" PROJECT_NAME="Assignment9" TARGET_NAME="Assignment9" CONFIG_NAME="Debug" RUN_TARGET_PROJECT_NAME="Assignment9" RUN_TARGET_NAME="Assignment9">
<method v="2">
<option name="com.jetbrains.cidr.execution.CidrBuildBeforeRunTaskProvider$BuildBeforeRunTask" enabled="true" />
</method>
</configuration>
</component>

View File

@ -0,0 +1,10 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="Test All" type="CMakeRunConfiguration" factoryName="Application" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" PASS_PARENT_ENVS_2="true" PROJECT_NAME="Assignment9" TARGET_NAME="Assignment9" CONFIG_NAME="Debug" RUN_TARGET_PROJECT_NAME="Assignment9" RUN_TARGET_NAME="Assignment9">
<method v="2">
<option name="com.jetbrains.cidr.execution.CidrBuildBeforeRunTaskProvider$BuildBeforeRunTask" enabled="true" />
<option name="RunConfigurationTask" enabled="false" run_configuration_name="Test1" run_configuration_type="CMakeRunConfiguration" />
<option name="RunConfigurationTask" enabled="true" run_configuration_name="Test3" run_configuration_type="CMakeRunConfiguration" />
<option name="RunConfigurationTask" enabled="true" run_configuration_name="Test4" run_configuration_type="CMakeRunConfiguration" />
</method>
</configuration>
</component>

View File

@ -0,0 +1,8 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="images" type="CTestRunConfiguration" factoryName="CTestRun" PROGRAM_PARAMS="--extra-verbose" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" WORKING_DIR="file://$CMakeCurrentLocalGenerationDir$" PASS_PARENT_ENVS_2="true" CONFIG_NAME="Debug" RUN_PATH="$CTestCurrentExecutableName$" EXPLICIT_BUILD_TARGET_NAME="all" TEST_METHOD="images" TEST_MODE="SUITE_TEST">
<method v="2">
<option name="com.jetbrains.cidr.execution.CidrBuildBeforeRunTaskProvider$BuildBeforeRunTask" enabled="true" />
<option name="BeforeTestRunTask" enabled="true" />
</method>
</configuration>
</component>

View File

@ -0,0 +1,8 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="output" type="CTestRunConfiguration" factoryName="CTestRun" PROGRAM_PARAMS="--extra-verbose" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" WORKING_DIR="file://$CMakeCurrentLocalGenerationDir$" PASS_PARENT_ENVS_2="true" CONFIG_NAME="Debug" RUN_PATH="$CTestCurrentExecutableName$" EXPLICIT_BUILD_TARGET_NAME="all" TEST_METHOD="output" TEST_MODE="SUITE_TEST">
<method v="2">
<option name="com.jetbrains.cidr.execution.CidrBuildBeforeRunTaskProvider$BuildBeforeRunTask" enabled="true" />
<option name="BeforeTestRunTask" enabled="true" />
</method>
</configuration>
</component>

6
Assignment9/.idea/vcs.xml generated Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>

View File

@ -0,0 +1,10 @@
cmake_minimum_required(VERSION 3.20)
project(Assignment9 C)
set(CMAKE_C_STANDARD 99)
add_executable(Assignment9 src/main.c
src/png.c
src/png.h)
enable_testing()
add_test(NAME output COMMAND test_text.sh WORKING_DIRECTORY ../tests)
add_test(NAME images COMMAND test_images.sh WORKING_DIRECTORY ../tests)

Binary file not shown.

View File

@ -0,0 +1,68 @@
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
// FILE format is: 'header', then 2023 skipped bytes, then the number of interest
#define OURINT 1426915328
#define OFFSETTODATA 2023
int writeToFile(){
FILE* fp = fopen("toReadLater.txt","w+");
char c = 'h';
fwrite(&c, sizeof(char), 1, fp);
c = 'e';
fwrite(&c, sizeof(char), 1, fp);
c = 'a';
fwrite(&c, sizeof(char), 1, fp);
c = 'd';
fwrite(&c, sizeof(char), 1, fp);
c = 'e';
fwrite(&c, sizeof(char), 1, fp);
c = 'r';
fwrite(&c, sizeof(char), 1, fp);
unsigned int buf = OFFSETTODATA;
fwrite(&buf, sizeof(int), 1, fp);
fseek(fp, OFFSETTODATA, SEEK_CUR);
buf = OURINT;
fwrite(&buf, sizeof(int), 1, fp);
fclose(fp);
}
int readFromFile(){
FILE *fp = fopen("toReadLater.txt", "r+");
fseek(fp, 6 , SEEK_SET);
int skip = 0;
fread(&skip, sizeof(int), 1, fp);
// NOTE, in HW, the size of the section is a value in
// Big-Endian format, it will actually need conversion
fseek(fp, skip, SEEK_CUR);
unsigned int qBe = 0;
unsigned int qLe = 0;
int i;
unsigned char *c = (char *)(&qBe);
unsigned char *d = (char *)(&qLe);
for (i = 0; i < 4; i++)
{
c[i] = getc(fp);
printf("Byte %i: %i %x\n",i, c[i], c[i]);
}
for (i = 0; i < 4; i++)
{
d[4-i-1] = c[i];
}
printf("Translated using swapping the bytes order: %i\n", qLe);
qLe = ntohl(qBe);
printf("Translated using ntohl: %i\n", qLe);
fclose(fp);
}
int main (int argc, char** argv){
unsigned int wantToRead;
writeToFile();
printf("Sizeof (int) is:%li\n", sizeof(unsigned int));
readFromFile();
return 0;
}

Binary file not shown.

50
Assignment9/src/main.c Normal file
View File

@ -0,0 +1,50 @@
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <stdbool.h>
#include "png.h"
int main(int argc, char *argv[]) {
// Error handling
if (argc != 2) {
printf("Usage: %s <png file>\n", argv[0]);
exit(EXIT_FAILURE);
}
if (strlen(argv[1]) > PATH_MAX) {
printf("Path too long\n");
exit(EXIT_FAILURE);
}
// Read file
char *path = argv[1];
char *png_buffer = load_file(path);
if (!is_png(png_buffer)) {
printf("It's not a PNG file\n");
exit(EXIT_FAILURE);
} else {
printf("It's a PNG file\n");
}
png_chunk **chunks = get_png_chunks(png_buffer);
free(png_buffer);
// Done with buffer, as chunks are a structured way to access the data
// Iterate over chunks to display info and "decrypt" IDAT chunks
int size = get_number_of_chunks(chunks);
for (int i = 0; i < size; i++) {
// Check if header is IDAT or IEND
bool is_idat = memcmp(chunks[i]->type, "IDAT", 4) == 0;
bool is_iend = memcmp(chunks[i]->type, "IEND", 4) == 0;
if (is_idat || is_iend) {
printf("Found %.4s\n", chunks[i]->type);
if (is_idat) {
xor_data(chunks[i]);
}
} else {
printf("Found unknown: %.4s\n", chunks[i]->type);
}
printf("Chunk size is:%d\n", chunks[i]->length);
}
// Write file and free memory
write_png_chunks(path, chunks);
destroy_chunks(chunks);
return EXIT_SUCCESS;
}

159
Assignment9/src/png.c Normal file
View File

@ -0,0 +1,159 @@
/*
* Copyright Notice
* ----------------
* Copyright © 1998, 1999 by: Glenn Randers-Pehrson
* This specification is a modification of the PNG 1.0 specification. It is being
* provided by the copyright holder under the provisions of the 1996 MIT copyright and license:
* Copyright © 1996 by: Massachusetts Institute of Technology (MIT)
* This W3C specification is being provided by the copyright holders under
* the following license. By obtaining, using and/or copying this specification, you
* agree that you have read, understood, and will comply with the following terms
* and conditions:
* Permission to use, copy, and distribute this specification for any purpose
* and without fee or royalty is hereby granted, provided that the full text of
* this NOTICE appears on ALL copies of the specification or portions thereof,
* including modifications, that you make.
* THIS SPECIFICATION IS PROVIDED ”AS IS,” AND COPYRIGHT HOLD-ERS
* MAKE NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR
* IMPLIED. BY WAY OF EXAMPLE, BUT NOT LIMITATION, COPYRIGHT
* HOLDERS MAKE NO REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY
* OR FITNESS FOR ANY PARTICULAR PURPOSE OR
* THAT THE USE OF THE SPECIFICATION WILL NOT INFRINGE ANY
* THIRD PARTY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER
* RIGHTS. COPYRIGHT HOLDERS WILL BEAR NO LIABILITY FOR ANY
* USE OF THIS SPECIFICATION.
* The name and trademarks of copyright holders may NOT be used in
* advertising or publicity pertaining to the specification without specific, written
* prior permission. Title to copyright in this specification and any associated
* documentation will at all times remain with copyright holders.
*/
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "png.h"
FILE *get_file(char *path) {
FILE *fp = fopen(path, "rb");
if (fp == NULL) {
perror("fopen");
exit(EXIT_FAILURE);
}
return fp;
}
char *load_file(char *path) {
FILE *fp = get_file(path);
fseek(fp, 0, SEEK_END);
long size = ftell(fp);
rewind(fp);
char *buffer = malloc(size);
if (buffer == NULL) {
perror("malloc");
exit(EXIT_FAILURE);
}
fread(buffer, 1, size, fp);
fclose(fp);
return buffer;
}
int is_png(char *buffer) {
return memcmp(buffer, PNG_SIGNATURE, PNG_SIGNATURE_SIZE) == 0;
}
png_chunk *get_png_chunk(char *buffer, unsigned int offset) {
png_chunk *chunk = malloc(sizeof(png_chunk));
if (chunk == NULL) {
perror("malloc");
exit(EXIT_FAILURE);
}
memcpy(&chunk->length, buffer + offset + 0, 4);
// Convert length to host byte order because PNG lengths is in network byte order/Big Endian
chunk->length = ntohl(chunk->length);
memcpy(&chunk->type, buffer + offset + 4, 4);
chunk->data = malloc(chunk->length);
if (chunk->data == NULL) {
perror("malloc");
exit(EXIT_FAILURE);
}
memcpy(chunk->data, buffer + offset + 8, chunk->length);
memcpy(&chunk->crc, buffer + offset + 8 + chunk->length, 4);
return chunk;
}
png_chunk **get_png_chunks(char *buffer) {
png_chunk **chunks = malloc(sizeof(png_chunk *));
if (chunks == NULL) {
perror("malloc");
exit(EXIT_FAILURE);
}
// Running offset of the buffer
unsigned int offset = PNG_SIGNATURE_SIZE;
// Index of the current chunk
int i = 0;
while (true) {
chunks[i] = get_png_chunk(buffer, offset);
// 12 = 4 (length) + 4 (type) + 4 (crc)
offset += 12 + chunks[i]->length;
if (memcmp(chunks[i]->type, "IEND", 4) == 0) {
break;
}
i++;
// Add one because realloc is 1-indexed when multiplying by sizeof
png_chunk **err_check = realloc(chunks, sizeof(png_chunk *) * (i + 1));
if (err_check == NULL) {
destroy_chunks(chunks);
perror("realloc");
exit(EXIT_FAILURE);
}
chunks = err_check;
}
return chunks;
}
int get_number_of_chunks(png_chunk **chunks) {
int i = 0;
while (memcmp(chunks[i]->type, "IEND", 4) != 0) {
i++;
}
// Add one for the IEND chunk
return ++i;
}
void xor_data(png_chunk *chunk) {
for (int i = 0; i < chunk->length; i++) {
chunk->data[i] ^= KEY;
}
}
void destroy_chunks(png_chunk **chunks) {
unsigned int size = get_number_of_chunks(chunks);
for (int i = 0; i < size; i++) {
free(chunks[i]->data);
free(chunks[i]);
}
free(chunks);
}
void write_png_chunks(char *path, png_chunk **chunks) {
FILE *fp = fopen(path, "wb");
if (fp == NULL) {
perror("fopen");
exit(EXIT_FAILURE);
}
fwrite(PNG_SIGNATURE, PNG_SIGNATURE_SIZE, 1, fp);
unsigned int size = get_number_of_chunks(chunks);
for (int i = 0; i < size; i++) {
// Convert length to network byte order for writing
chunks[i]->length = htonl(chunks[i]->length);
fwrite(&chunks[i]->length, 4, 1, fp);
fwrite(chunks[i]->type, 4, 1, fp);
// Convert back so we can accurately use it to write the length of the data
chunks[i]->length = ntohl(chunks[i]->length);
fwrite(chunks[i]->data, chunks[i]->length, 1, fp);
fwrite(&chunks[i]->crc, 4, 1, fp);
}
fclose(fp);
}

75
Assignment9/src/png.h Normal file
View File

@ -0,0 +1,75 @@
/*
* Copyright Notice
* ----------------
* Copyright © 1998, 1999 by: Glenn Randers-Pehrson
* This specification is a modification of the PNG 1.0 specification. It is being
* provided by the copyright holder under the provisions of the 1996 MIT copyright and license:
* Copyright © 1996 by: Massachusetts Institute of Technology (MIT)
* This W3C specification is being provided by the copyright holders under
* the following license. By obtaining, using and/or copying this specification, you
* agree that you have read, understood, and will comply with the following terms
* and conditions:
* Permission to use, copy, and distribute this specification for any purpose
* and without fee or royalty is hereby granted, provided that the full text of
* this NOTICE appears on ALL copies of the specification or portions thereof,
* including modifications, that you make.
* THIS SPECIFICATION IS PROVIDED ”AS IS,” AND COPYRIGHT HOLD-ERS
* MAKE NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR
* IMPLIED. BY WAY OF EXAMPLE, BUT NOT LIMITATION, COPYRIGHT
* HOLDERS MAKE NO REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY
* OR FITNESS FOR ANY PARTICULAR PURPOSE OR
* THAT THE USE OF THE SPECIFICATION WILL NOT INFRINGE ANY
* THIRD PARTY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER
* RIGHTS. COPYRIGHT HOLDERS WILL BEAR NO LIABILITY FOR ANY
* USE OF THIS SPECIFICATION.
* The name and trademarks of copyright holders may NOT be used in
* advertising or publicity pertaining to the specification without specific, written
* prior permission. Title to copyright in this specification and any associated
* documentation will at all times remain with copyright holders.
*/
#pragma once
// Needed for uint types
#include <arpa/inet.h>
// http://www.libpng.org/pub/png/spec/1.2/PNG-Rationale.html#R.PNG-file-signature
#define PNG_SIGNATURE "\211PNG\r\n\032\n"
#define PNG_SIGNATURE_SIZE 8
// The key used to "encrypt/decrypt" the PNG chunks
#define KEY 42
typedef struct png_chunk {
uint32_t length;
char type[4];
char *data;
uint32_t crc;
} png_chunk;
// Get FILE from path
int get_fd(char *path);
// Store file in heap memory
char *load_file(char *path);
// Check if file is a PNG
int is_png(char *buffer);
// Get PNG chunk
png_chunk *get_png_chunk(char *buffer, unsigned int offset);
// Get all PNG chunks
png_chunk **get_png_chunks(char *buffer);
// Get number of chunks
int get_number_of_chunks(png_chunk **chunks);
// XOR chunk with key
void xor_data(png_chunk *chunk);
// Destroy all chunks
void destroy_chunks(png_chunk **chunks);
// Write PNG chunks to file
void write_png_chunks(char *path, png_chunk **chunks);

Binary file not shown.

View File

@ -0,0 +1,13 @@
It's a PNG file
Found unknown: IHDR
Chunk size is:13
Found unknown: sRGB
Chunk size is:1
Found unknown: gAMA
Chunk size is:4
Found unknown: pHYs
Chunk size is:9
Found IDAT
Chunk size is:18944
Found IEND
Chunk size is:0

View File

@ -0,0 +1 @@
It's not a PNG file

View File

@ -0,0 +1,249 @@
It's a PNG file
Found unknown: IHDR
Chunk size is:13
Found unknown: sRGB
Chunk size is:1
Found unknown: gAMA
Chunk size is:4
Found unknown: pHYs
Chunk size is:9
Found unknown: tEXt
Chunk size is:15
Found unknown: tIME
Chunk size is:7
Found unknown: tEXt
Chunk size is:32
Found unknown: tEXt
Chunk size is:33
Found IDAT
Chunk size is:65310
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:17176
Found IEND
Chunk size is:0

View File

@ -0,0 +1,317 @@
It's a PNG file
Found unknown: IHDR
Chunk size is:13
Found unknown: sRGB
Chunk size is:1
Found unknown: gAMA
Chunk size is:4
Found unknown: pHYs
Chunk size is:9
Found unknown: tEXt
Chunk size is:15
Found unknown: tIME
Chunk size is:7
Found unknown: tEXt
Chunk size is:32
Found unknown: tEXt
Chunk size is:33
Found IDAT
Chunk size is:65310
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:65524
Found IDAT
Chunk size is:53390
Found IEND
Chunk size is:0

BIN
Assignment9/tests/test1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

BIN
Assignment9/tests/test2.png Normal file

Binary file not shown.

BIN
Assignment9/tests/test3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.1 MiB

BIN
Assignment9/tests/test4.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.3 MiB

View File

@ -0,0 +1,22 @@
#!/usr/bin/env bash
# Executable path
EXECUTABLE=../build/Assignment9
test_reverse() {
start=$(date +%s)
cp "$1" in.png
cp in.png out.png
$EXECUTABLE out.png > /dev/null
$EXECUTABLE out.png > /dev/null
diff in.png out.png
rm in.png out.png
end=$(date +%s)
echo "Test $1 took $((end - start)) seconds"
}
test_reverse test1.png
# test_reverse test2.png # This png is invalid, so it will fail
test_reverse test3.png
test_reverse test4.png

View File

@ -0,0 +1,18 @@
#!/usr/bin/env bash
EXECUTABLE=../build/Assignment9
# Test the text output
$EXECUTABLE test1.png | diff output1.txt -
$EXECUTABLE test2.png | diff output2.txt -
$EXECUTABLE test3.png | diff output3.txt -
$EXECUTABLE test4.png | diff output4.txt -
# Reverse the images
$EXECUTABLE test1.png > /dev/null
$EXECUTABLE test2.png > /dev/null
$EXECUTABLE test3.png > /dev/null
$EXECUTABLE test4.png > /dev/null
echo "If there is no diff output, then the test passed."