Skip to content

Commit

Permalink
Store NULL-Terminated text frames in ID3v2.4.0 tags
Browse files Browse the repository at this point in the history
  • Loading branch information
rstemmer committed Aug 15, 2018
1 parent 0819cf0 commit 64c596f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
2.0.2 - Fixed a possible bug: For ID3v2.4.0 tags all text frames store NULL-terminated strings
2.0.1 - Fixed a critical bug: The frame size en/decoding was not ID3v2.4.0 compatible
2.0.0 - Lots of new features and fixes:
+- Added --get-artwork to extract an artwork from a mp3 file
Expand Down
24 changes: 15 additions & 9 deletions id3v2frame.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,15 @@ int ID3V2_SetTextFrame(ID3V2 *id3v2, unsigned int ID, const char *utf8text, unsi
void *rawtext;
size_t rawtextsize;
size_t textbufferlimit = textlength * 4; // 4 time the uft-8 encoded size is enough
unsigned char version = id3v2->header.version_major;

// Track and update version number
if(encoding == ID3V2TEXTENCODING_UTF16_BE || encoding == ID3V2TEXTENCODING_UTF8)
{
version = 4; // the used encoding is only allows in ID3v2.4.0
}

// Allocate memory for encoded text
rawtext = malloc(textlength * 4);
if(rawtext == NULL)
{
Expand All @@ -63,8 +71,12 @@ int ID3V2_SetTextFrame(ID3V2 *id3v2, unsigned int ID, const char *utf8text, unsi
}

// Encode text
// -1 to not encode '\0' because the ID3 standard does not need it in text frames.
error = Encode(encoding, utf8text, textlength - 1, rawtext, textbufferlimit, &rawtextsize);
// -1 to not encode '\0' because the ID3v2.3.0 standard does not need it in text frames.
if(version == 3)
error = Encode(encoding, utf8text, textlength - 1, rawtext, textbufferlimit, &rawtextsize);
else
error = Encode(encoding, utf8text, textlength , rawtext, textbufferlimit, &rawtextsize);

if(error)
{
free(rawtext);
Expand Down Expand Up @@ -94,13 +106,7 @@ int ID3V2_SetTextFrame(ID3V2 *id3v2, unsigned int ID, const char *utf8text, unsi
}

// Update ID3 version
if(encoding == ID3V2TEXTENCODING_UTF16_BE || encoding == ID3V2TEXTENCODING_UTF8)
{
if(id3v2->header.version_major == 3)
{
id3v2->header.version_major = 4; // the used encoding is only allows in ID3v2.4.0
}
}
id3v2->header.version_major = version; // the used encoding is only allows in ID3v2.4.0

// done
free(rawtext);
Expand Down
5 changes: 3 additions & 2 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <printhex.h>
#include <stdbool.h>

#define VERSION "2.0.1"
#define VERSION "2.0.2"

int CopyArgument(char **dst, char *src);
int ProcessSetArgument(ID3V2 *id3v2, unsigned int ID, const char *argument, unsigned char encoding);
Expand Down Expand Up @@ -78,7 +78,7 @@ void PrintUsage()
printf("\t\e[1;36m --strip \e[1;34m Remove whole ID3 Tag \e[1;30m(leaves a bare audio file) \n");
printf("\t\e[1;36m --showheader \e[1;34m Print details of the headers while reading \n");
printf("\t\e[1;36m --force230 \e[1;34m Force ID3 v 2.3.0 when writing \e[1;31m³\n");
printf("\t\e[1;36m --force240 \e[1;34m Force ID3 v 2.4.0 when writing \n");
printf("\t\e[1;36m --force240 \e[1;34m Force ID3 v 2.4.0 when writing \e[1;31m³\n");
printf("\n");

// Comments / footnotes
Expand Down Expand Up @@ -106,6 +106,7 @@ void PrintUsage()
printf("\e[1;31m ³ \e[1;33mIt is up to you to make sure all frames are conform to that version of the standard!\e[0m\n");
printf("\e[1;30m ID3v2.3.0 only allows UTF-16+BOM or ISO8859-1 encoded text.\e[0m\n");
printf("\e[1;30m Some frame IDs are different! (use --get-framelist to check the new file)\e[0m\n");
printf("\e[1;33m There are many differences in the details of ID3v2.3.0 and ID3v2.4.0!\e[0m\n");
printf("\n");

// Some warnings
Expand Down
10 changes: 5 additions & 5 deletions test/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,15 @@ CheckResult "b094b92ff7873618aebcc819d8b36355"

PrintTest "Set UTF-16BE encoded Text Frame"
./id3edit --encoding UTF-16BE --set-name "This is a test 😈" $DST
CheckResult "24985f60193e2c8e9395d39dd4df48e3"
CheckResult "2b7c1017135093c1ff5254892f168fe3"

PrintTest "Set UTF-8 encoded Text Frame"
./id3edit --encoding UTF-8 --set-name "This is a test 😈" $DST
CheckResult "bbc09094226d8b7229af13d3d5ff1933"
CheckResult "f0828dbcabae5e872e9a288679983c30"

PrintTest "Set ISO 8859-1 encoded Text Frame"
./id3edit --encoding ISO8859-1 --set-name "This is ä test" $DST
CheckResult "bc742aadbef2587ced763374a838d3c5"
CheckResult "ea6b1c47b63774e7249b523f038afcad"

PrintTest "Set release year for ID3v2.3.0"
CreateTestMP3
Expand All @@ -179,9 +179,9 @@ CheckResult "a2b6e5f4f3bbc04e3fb063d3d59eb7d2"

PrintTest "Set release year for ID3v2.4.0"
CreateTestMP3
./id3edit --create --force240 --set-name "Release Test" --outfile $DST $SRC
./id3edit --create --force240 --outfile $DST $SRC
./id3edit --set-release 2018 $DST
CheckResult "f232bccc746f8cc5ea7b9756104835f5"
CheckResult "1c46724c696900e093516b01574b0349"


PrintHeader "Creating and editing artwork"
Expand Down

0 comments on commit 64c596f

Please sign in to comment.