Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiler compatibility and unicode termination fixes #55

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/modules/frames/apic_frame.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,25 +51,28 @@ ID3v2_ApicFrame* ApicFrame_parse(CharStream* frame_cs, const int id3_major_versi
CharStream_seek(frame_cs, ID3v2_FRAME_ENCODING_LENGTH, SEEK_CUR); // skip encoding

const int mime_type_size = ID3v2_strlent(CharStream_get_cur(frame_cs));
char mime_type[mime_type_size];
char* mime_type = malloc(mime_type_size * sizeof(char));
CharStream_read(frame_cs, mime_type, mime_type_size);

const char picture_type = CharStream_getc(frame_cs);

const int description_size = ID3v2_strlent(CharStream_get_cur(frame_cs));
char description[description_size];
char* description = malloc(description_size * sizeof(char));
CharStream_read(frame_cs, description, description_size);

const int pic_size = header->size - ID3v2_FRAME_ENCODING_LENGTH - mime_type_size -
ID3v2_APIC_FRAME_PICTURE_TYPE_LENGTH - description_size;
char pic_data[pic_size];
char* pic_data = malloc(pic_size * sizeof(char));
CharStream_read(frame_cs, pic_data, pic_size);

ID3v2_ApicFrame* frame =
ApicFrame_new(header->flags, description, picture_type, mime_type, pic_size, pic_data);

FrameHeader_free(header); // we only needed the header to parse the data

free(mime_type);
free(description);
free(pic_data);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
free(pic_data);
free(pic_data);

return frame;
}

Expand Down
6 changes: 4 additions & 2 deletions src/modules/frames/comment_frame.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,19 @@ ID3v2_CommentFrame* CommentFrame_parse(CharStream* frame_cs, const int id3_major
CharStream_read(frame_cs, lang, ID3v2_COMMENT_FRAME_LANGUAGE_LENGTH);

const int short_desc_size = ID3v2_strlent(CharStream_get_cur(frame_cs));
char short_desc[short_desc_size];
char* short_desc = malloc(short_desc_size * sizeof (char));
CharStream_read(frame_cs, short_desc, short_desc_size);

const int comment_size = ID3v2_strlent(CharStream_get_cur(frame_cs));
char comment[comment_size];
char* comment = malloc(comment_size * sizeof(char));
CharStream_read(frame_cs, comment, comment_size);

ID3v2_CommentFrame* frame = CommentFrame_new(header->flags, lang, short_desc, comment);

FrameHeader_free(header); // we only needed the header to parse the data

free(comment);
free(short_desc);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
free(short_desc);
free(short_desc);

return frame;
}

Expand Down
8 changes: 7 additions & 1 deletion src/modules/frames/text_frame.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,19 @@ ID3v2_TextFrame* TextFrame_parse(CharStream* frame_cs, const int id3_major_versi
CharStream_seek(frame_cs, ID3v2_FRAME_ENCODING_LENGTH, SEEK_CUR); // skip encoding

const int text_size = header->size - ID3v2_FRAME_ENCODING_LENGTH;
char text[text_size];
const size_t string_termination_bytes = 2;
char* text = malloc((string_termination_bytes + text_size) * sizeof(char));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's been a while since I've worked on this, but, doesn't text_size already include the string_termination_bytes length?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on mp3's I've worked with, there's no termination, so it's not included

CharStream_read(frame_cs, text, text_size);

// Adding string termination bytes in case the stored string doesn't have those
for (int i = 0; i < string_termination_bytes; ++i)
text[text_size + i] = 0x00;
Comment on lines +44 to +46
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this due to a malformed id3tag embedded in the mp3? or how can we reach that scenario?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, why do we need a for loop here instead of just adding the string termination bytes? It's only gonna run for two iterations.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that it's not malformed, but intended - there's no zeroes after the strings in actual mp3 files, I've checked multiple mp3 files I've had. Probably because storing the string + its length is sufficient for them. So we want to put those zeros into the RAM ourselves, if we want to rely on strlen-like functions.

Regarding for loop - I was actually debating between this and two normal assignments, neither looks better than the other to me. Looks like you favor the other way, so I'll switch to that :)


ID3v2_TextFrame* frame = TextFrame_new(header->id, header->flags, text);

FrameHeader_free(header); // we only needed the header to parse the data

free(text);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
free(text);
free(text);

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also think we can group those free's with FrameHeader_free (by deleting line 51) if it makes sense to you

return frame;
}

Expand Down