Compare commits

...

2 Commits

Author SHA1 Message Date
4f59d64848 Tests in a9 2023-12-05 13:28:00 -04:00
f33662aeaa Use LF 2023-12-05 13:04:47 -04:00
7 changed files with 314 additions and 286 deletions

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,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>

View File

@ -3,4 +3,8 @@ project(Assignment9 C)
set(CMAKE_C_STANDARD 99) set(CMAKE_C_STANDARD 99)
add_executable(Assignment9 src/main.c add_executable(Assignment9 src/main.c
src/png.c src/png.c
src/png.h) 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)

View File

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

View File

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

View File

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