Minor fixes

Signed-off-by: Balazs Toldi <balazs@toldi.eu>
This commit is contained in:
Balazs Toldi 2023-05-25 06:55:52 +02:00
parent 0cf51dea87
commit 053ba8d04f
Signed by: Bazsalanszky
GPG key ID: 6C7D440036F99D58
3 changed files with 22 additions and 15 deletions

16
caff.c
View file

@ -40,10 +40,14 @@ CAFF* caff_parse_file(const char* filename) {
}
CAFF* caff = malloc(sizeof(CAFF));
if(!caff){
printf("Failed to allocate memory for CAFF!\n");
return NULL;
}
caff->credits = NULL;
caff->header = NULL;
caff_init_animation_list(&caff->animations);
// Check for malloc failure here.
size_t anim_count = 0;
while (!feof(file)) {
uint8_t id;
@ -53,10 +57,12 @@ CAFF* caff_parse_file(const char* filename) {
switch (id) {
case 0x1:
// Do not replace the header after it is initialized
if(!caff->header)
caff->header = read_header(file);
break;
case 0x2:
// Do not replace the credits after it is initialized
if(!caff->credits)
caff->credits = read_credits(file);
print_credits(caff->credits );
@ -66,19 +72,18 @@ CAFF* caff_parse_file(const char* filename) {
if(caff->header && caff->header->num_anim == anim_count){
goto end;
}
printf("Here!\n");
// You might want to append to a list of animations here.
CAFF_Animation* anim = read_animation(file);
caff_add_animation_list(&caff->animations,*anim);
free(anim);
printf("There!\n");
// We only need the first image now...
goto end;
break;
default:
// Random bytes should not cause an error
printf("Unknown block ID: %u\n", id);
goto end;
// Handle unknown block ID here.
}
}
end:
@ -190,7 +195,6 @@ CAFF_Animation* read_animation(FILE* file) {
}
// Read the CIFF image.
// Note: The read_ciff function needs to be defined to read a CIFF image from a file.
animation->ciff = read_ciff(file);
if (!animation->ciff) {
printf("Failed to read CIFF image from CAFF animation\n");
@ -213,7 +217,7 @@ void caff_free(CAFF* caff) {
}
free(caff->credits);
caff->credits = NULL;
// Free the animations
for (size_t i = 0; i < caff->animations.length; i++) {
free_ciff(caff->animations.array[i].ciff);

19
ciff.c
View file

@ -52,7 +52,8 @@ CIFF* read_ciff(FILE* file) {
ciff->caption[i] = '\0'; // null-terminate the string
size_t caption_lenth= i;
// Read the tags
ciff->tags = malloc(ciff->header_size-sizeof(char)*4-sizeof(uint64_t)*4-caption_lenth);
size_t tags_size = ciff->header_size-sizeof(char)*4-sizeof(uint64_t)*4-caption_lenth; // Size for the rest of the header
ciff->tags = malloc(tags_size*sizeof(char));
if (!ciff->tags) {
printf("Failed to allocate memory for tags\n");
free(ciff->caption);
@ -60,15 +61,15 @@ CIFF* read_ciff(FILE* file) {
fclose(file);
return NULL;
}
if(fread(ciff->tags, tags_size, 1, file) != 1) {
printf("Failed to read tags!\n");
free(ciff->caption);
free(ciff);
fclose(file);
return NULL;
}
i = 0;
size_t header_loaded = sizeof(char)*4+sizeof(uint64_t)*4+caption_lenth;
printf("Header loaded: %ld/%ld\n",header_loaded,ciff->header_size);
while(header_loaded < ciff->header_size) {
fread(&c, 1, 1, file);
ciff->tags[i++] = c;
header_loaded += sizeof(char);
};
// Read the pixels
ciff->pixels = malloc(ciff->content_size);
if (!ciff->pixels) {

2
main.c
View file

@ -35,6 +35,7 @@ int main(int argc, char **argv)
{
FILE *ciff_file = fopen(input_path, "rb");
ciff = read_ciff(ciff_file);
fclose(ciff_file);
}
if (!ciff)
{
@ -49,6 +50,7 @@ int main(int argc, char **argv)
}
else
{
free_ciff(ciff);
}
return 0;