Skip to content

Commit

Permalink
Merge branch 'extheader'
Browse files Browse the repository at this point in the history
  • Loading branch information
rstemmer committed Aug 19, 2018
2 parents 0fef57f + b70ce34 commit 3877de0
Show file tree
Hide file tree
Showing 20 changed files with 843 additions and 90 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
2.1.0 - Partial support for extended header: remove (--rm-exthdr) and add checksum (--add-crc)
2.0.3 - Tag version number changes are more obvious now. Only --force2?0 changes it at the beginning of execution.
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
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ I separated id3edit from the [MusicDB Project](https://github.com/rstemmer/music
* It works from command line
* Support for ID3v2.3.0 (most common) and ID3v2.4.0 (latest)
* All encodings supported (ISO 8859-1, UTF-16 with BOM, UTF-16BE, UTF-8)
* Partial support of extended header: CRC feature supported!

**Project State:** Alive

## Limitations

* It does not care about any flags (usually no ID3 Tag or Frame flags are set) (some flags that may appear in the wild will be added in later versions)
* See Issue Tracker

## Name Definitions

Expand Down
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

SOURCE=$(find . -type f ! -path './test/*' -name "*.c")
HEADER="-I."
LIBS="-lprinthex"
LIBS="-lprinthex -lz"

for c in $SOURCE ;
do
Expand Down
62 changes: 62 additions & 0 deletions crc32.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <crc32.h>
#include <zlib.h>


int CRC32FromFile(FILE *file, long from, long to, unsigned long *crc)
{
if(from >= to)
{
fprintf(stderr, "Invalid span of data for CRC: from = %li; to = %li!\n", from, to);
return -1;
}

// Create Buffer
unsigned char *buffer;
size_t buffersize = to-from;

buffer = malloc(buffersize * sizeof(char));
if(buffer == NULL)
{
fprintf(stderr, "%s, %i: ", __FILE__, __LINE__);
fprintf(stderr, "Fatal Error! - malloc returned NULL!\n");
return -1;
}

// Go to begin of relevant data
int error;
error = fseek(file, from, SEEK_SET);
if(error != 0)
{
fprintf(stderr, "%s, %i: ", __FILE__, __LINE__);
fprintf(stderr, "Fatal Error! - Setting correct file position failed!\n");
return -1;
}

// Read File
size_t bytesread;
bytesread = fread(buffer, 1, buffersize, file);
if(bytesread != buffersize)
{
fprintf(stderr, "%s, %i: ", __FILE__, __LINE__);
fprintf(stderr, "Fatal Error! - Only %zu of %zu bytes read from file!\n", bytesread, buffersize);
return -1;
}

// Calculate CRC32
if(crc == NULL)
return 0;
*crc = crc32(0x00000000, buffer, buffersize);

// Clean up
free(buffer);
return 0;
}




// vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4

13 changes: 13 additions & 0 deletions crc32.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef CRC32_H
#define CRC32_H

#include <stdio.h>

int CRC32FromFile(FILE *file, long from, long to, unsigned long *crc);


#endif


// vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4

50 changes: 50 additions & 0 deletions encoding/crc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <stdlib.h>
#include <endian.h>
#include <encoding/crc.h>


void ID3V2_EncodeCRC(unsigned long crc, unsigned char enccrc[5])
{
if(enccrc == NULL)
return;

// 1.: LE -> BE
unsigned long becrc;
//becrc = htobe32(crc);
becrc = crc;

// 1.: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
// -> 0000xxxx0xxxxxxx0xxxxxxx0xxxxxxx0xxxxxxx
unsigned int byte = 0;
for(int i=0; i<5; i++)
{
byte = becrc >> ((4-i)*7);
byte &= 0x7F;
enccrc[i] = byte;
}

return;
}

void ID3V2_DecodeCRC(unsigned long *crc, const unsigned char enccrc[5])
{
if(crc == NULL || enccrc == NULL)
return;

// 1.: 0000xxxx0xxxxxxx0xxxxxxx0xxxxxxx0xxxxxxx
// -> xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
unsigned int byte = 0;
unsigned long tmp = 0;
for(int i=0; i<5; i++)
{
byte = enccrc[i];
byte &= 0x7F;
tmp |= byte << ((4-i)*7);
}

*crc = tmp;
return;
}

// vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4

13 changes: 13 additions & 0 deletions encoding/crc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef ENCODING_CRC_H
#define ENCODING_CRC_H


void ID3V2_EncodeCRC(unsigned long crc, unsigned char enccrc[5]);
void ID3V2_DecodeCRC(unsigned long *crc, const unsigned char enccrc[5]);


#endif


// vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4

47 changes: 47 additions & 0 deletions encoding/size.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <errno.h>
#include <endian.h>
#include <encoding/size.h>


unsigned int ID3V2_EncodeSize(unsigned int size)
{
// 1.: 0000xxxxxxxxxxxxxxxxxxxxxxxxxxxx
// -> 0xxxxxxx0xxxxxxx0xxxxxxx0xxxxxxx
unsigned int byte = 0;
unsigned int tmp = 0;
for(int i=0; i<4; i++)
{
byte = size >> (i*7);
byte &= 0x7F;
tmp |= byte << (i*8);
}

// 2.: LE -> BE
unsigned int encsize;
encsize = htobe32(tmp);

return encsize;
}

unsigned int ID3V2_DecodeSize(unsigned int encsize)
{
// 1.: BE -> LE
unsigned int tmp;
tmp = be32toh(encsize);

// 2.: 0xxxxxxx0xxxxxxx0xxxxxxx0xxxxxxx
// -> 0000xxxxxxxxxxxxxxxxxxxxxxxxxxxx
unsigned int byte = 0;
unsigned int size = 0;
for(int i=0; i<4; i++)
{
byte = tmp >> (i*8);
byte &= 0x7F;
size |= byte << (i*7);
}

return size;
}

// vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4

13 changes: 13 additions & 0 deletions encoding/size.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef ENCODING_SIZE_H
#define ENCODING_SIZE_H


unsigned int ID3V2_EncodeSize(unsigned int size);
unsigned int ID3V2_DecodeSize(unsigned int encsize);


#endif


// vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4

3 changes: 2 additions & 1 deletion encoding.c → encoding/text.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <id3v2.h>
#include <id3v2frame.h>
#include <iconv.h>
#include <encoding.h>
#include <encoding/text.h>

#ifdef DEBUG
#include <printhex.h>
Expand Down Expand Up @@ -121,5 +121,6 @@ int Encode(unsigned char id3v2encoding, const char *utf8text, size_t textlength,
return ID3V2ERROR_NOERROR;
}


// vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4

5 changes: 3 additions & 2 deletions encoding.h → encoding/text.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef ENCODING_H
#define ENCODING_H
#ifndef ENCODING_TEXT_H
#define ENCODING_TEXT_H


#define UTF16BOM_LE 0xFEFF
Expand All @@ -20,6 +20,7 @@ int Transcode(const char *from, const char *to, const void *input, size_t inputb
*/
int Decode(unsigned char id3v2encoding, void *rawdata, size_t rawdatasize, char *utf8text, size_t textlengthlimit, size_t *actualsize);
int Encode(unsigned char id3v2encoding, const char *utf8text, size_t textlength, void *rawdata, size_t rawdatasizelimit, size_t *actualsize);

#endif


Expand Down
Loading

0 comments on commit 3877de0

Please sign in to comment.