Working reader

This commit is contained in:
2023-12-04 18:54:29 -04:00
parent 3aad46748a
commit 3aa01cdc2b
3 changed files with 37 additions and 24 deletions

View File

@@ -69,11 +69,6 @@ char *load_file(int fd) {
exit(1);
}
fread(buffer, 1, size, fp);
// Iterate over buffer and convert from network byte order to host byte order
//for (int i = 0; i < size; i += 4) {
// uint32_t *ptr = (uint32_t *)(buffer + i);
// *ptr = ntohl(*ptr);
//}
fclose(fp);
return buffer;
}
@@ -111,7 +106,8 @@ png_chunk **get_png_chunks(char *buffer) {
int i = 0;
while (1) {
chunks[i] = get_png_chunk(buffer, offset);
offset += chunks[i]->length;
// 12 = 4 (length) + 4 (type) + 4 (crc)
offset += 12 + chunks[i]->length;
if (memcmp(chunks[i]->type, "IEND", 4) == 0) {
break;
}
@@ -125,18 +121,6 @@ png_chunk **get_png_chunks(char *buffer) {
return chunks;
}
int is_chunk_valid(png_chunk *chunk) {
uint32_t crc = htonl(chunk->crc);
//uint32_t calculated_crc = crc32(0, chunk->type, 4);
//calculated_crc = crc32(calculated_crc, chunk->data, chunk->length);
//return crc == calculated_crc;
return 1;
}
int is_chunk_critical(png_chunk *chunk) {
return chunk->type[0] >= 'A' && chunk->type[0] <= 'Z';
}
int get_number_of_chunks(png_chunk **chunks) {
int i = 0;
while (chunks[i] != NULL) {
@@ -144,3 +128,25 @@ int get_number_of_chunks(png_chunk **chunks) {
}
return i;
}
void xor_data(png_chunk *chunk) {
for (int i = 0; i < chunk->length; i++) {
chunk->data[i] ^= KEY;
}
}
void write_png_chunks(char *path, png_chunk **chunks) {
FILE *fp = fopen(path, "wb");
if (fp == NULL) {
perror("fopen");
exit(1);
}
fwrite(PNG_SIGNATURE, PNG_SIGNATURE_SIZE, 1, fp);
for (int i = 0; chunks[i] != NULL; i++) {
fwrite(&chunks[i]->length, 4, 1, fp);
fwrite(chunks[i]->type, 4, 1, fp);
fwrite(chunks[i]->data, chunks[i]->length, 1, fp);
fwrite(&chunks[i]->crc, 4, 1, fp);
}
fclose(fp);
}