cvs: pecl /id3 id3.c php_id3.h

From: Date: Mon, 23 Aug 2004 19:12:34 +0000
Subject: cvs: pecl /id3 id3.c php_id3.h
Groups: php.pecl.cvs 
Request: Send a blank email to pecl-cvs+get-1710@lists.php.net to get a copy of this message
luckec Mon Aug 23 15:12:34 2004 EDT Modified files: /pecl/id3 id3.c php_id3.h Log: added function to parse id3v2 header and extHeader + Flags

http://cvs.php.net/diff.php/pecl/id3/id3.c?r1=1.4&r2=1.5&ty=u Index: pecl/id3/id3.c diff -u pecl/id3/id3.c:1.4 pecl/id3/id3.c:1.5 --- pecl/id3/id3.c:1.4 Sat Jul 31 14:31:23 2004 +++ pecl/id3/id3.c Mon Aug 23 15:12:34 2004 @@ -17,7 +17,7 @@ +----------------------------------------------------------------------+ */ -/* $Id: id3.c,v 1.4 2004/07/31 18:31:23 wez Exp $ */ +/* $Id: id3.c,v 1.5 2004/08/23 19:12:34 luckec Exp $ */ #ifdef HAVE_CONFIG_H #include "config.h" @@ -30,13 +30,6 @@ #include "ext/standard/flock_compat.h" /** - * version constants - */ -const int ID3_V1_0 = 1; -const int ID3_V1_1 = 3; -const int ID3_V2 = 4; - -/** * fseek positions */ const int ID3_SEEK_V1_TAG = -128; @@ -121,7 +114,10 @@ { REGISTER_LONG_CONSTANT("ID3_V1_0", ID3_V1_0, CONST_CS|CONST_PERSISTENT); REGISTER_LONG_CONSTANT("ID3_V1_1", ID3_V1_1, CONST_CS|CONST_PERSISTENT); - REGISTER_LONG_CONSTANT("ID3_V2", ID3_V2, CONST_CS|CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("ID3_V2_1", ID3_V2_1, CONST_CS|CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("ID3_V2_2", ID3_V2_2, CONST_CS|CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("ID3_V2_3", ID3_V2_3, CONST_CS|CONST_PERSISTENT); + REGISTER_LONG_CONSTANT("ID3_V2_4", ID3_V2_4, CONST_CS|CONST_PERSISTENT); return SUCCESS; } /* }}} */ @@ -280,6 +276,14 @@ /* {{{ proto boolean id3_set_tag(string file, array tag [, int version]) Set an array containg all information from the id3 tag */ +zval* _php_id3v1_get_tag(php_stream *stream TSRMLS_DC) +{ + +} +/* }}} */ + +/* {{{ proto boolean id3_set_tag(string file, array tag [, int version]) + Set an array containg all information from the id3 tag */ PHP_FUNCTION(id3_set_tag) { zval *arg; @@ -552,23 +556,34 @@ return; } - /* get the filedescriptor */ - /* XXX: AVOID DOING THIS!!! */ - if (FAILURE == php_stream_cast(stream, PHP_STREAM_AS_FD, (void **) &fd, REPORT_ERRORS)) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "id3_remove_tag() was unable to remove the existing id3-tag"); - if (opened == 1) { - php_stream_close(stream); + /* using streams api for php5, otherwise a stream-cast with following ftruncate() */ + #if PHP_MAJOR_VERSION >= 5 + /* cut the stream right before TAG */ + if(php_stream_truncate_set_size(stream, cutPos) == -1) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "id3_remove_tag() was unable to remove the existing id3-tag"); + if (opened == 1) { + php_stream_close(stream); + } + return; } - return; - } - /* cut the stream right before TAG */ - if(ftruncate(fd, cutPos) == -1) { - php_error_docref(NULL TSRMLS_CC, E_WARNING, "id3_remove_tag() was unable to remove the existing id3-tag"); - if (opened == 1) { - php_stream_close(stream); + #else + /* get the filedescriptor */ + if (FAILURE == php_stream_cast(stream, PHP_STREAM_AS_FD, (void **) &fd, REPORT_ERRORS)) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "id3_remove_tag() was unable to remove the existing id3-tag"); + if (opened == 1) { + php_stream_close(stream); + } + return; } - return; - } + /* cut the stream right before TAG */ + if(ftruncate(fd, cutPos) == -1) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "id3_remove_tag() was unable to remove the existing id3-tag"); + if (opened == 1) { + php_stream_close(stream); + } + return; + } + #endif if(opened == 1) { php_stream_close(stream); @@ -620,11 +635,129 @@ } /* }}} */ +/* {{{ proto struct id3v2HdrFlags _php_id3v2_get_hdrFlags(php_stream *stream TSRMLS_DC) + Returns a structure that contains all flags of the tag-header */ +struct id3v2HdrFlags _php_id3v2_get_hdrFlags(php_stream *stream TSRMLS_DC) +{ + struct id3v2HdrFlags sFlags; + + char version, + revision; + + unsigned char flags; + + int iFlags = 0, + unsynch = 0, + hasExtHdr = 0, + isExperimental = 0, + hasFooter = 0; + + php_stream_seek(stream, ID3V2_IDENTIFIER_LENGTH, SEEK_SET); + php_stream_read(stream, &version, 1); + php_stream_read(stream, &revision, 1); + php_stream_read(stream, &flags, 1); + + switch (version) { + case 2: + /* %ab000000 in v2.2 + * a - Unsynchronization + * b - Compression + */ + sFlags.unsynch = BIT7((int)flags) > 0 ? 1 : 0; + sFlags.compression = BIT6((int)flags) > 0 ? 1 : 0; + break; + + case 3: + /* + * %abc00000 in v2.3 + * a - Unsynchronisation + * b - Extended header + * c - Experimental indicator + */ + sFlags.unsynch = BIT7((int)flags) > 0 ? 1 : 0; + sFlags.extHdr = BIT6((int)flags) > 0 ? 1 : 0; + sFlags.experimental = BIT5((int)flags) > 0 ? 1 : 0; + break; + + case 4: + /* %abcd0000 in v2.4 + * a - Unsynchronisation + * b - Extended header + * c - Experimental indicator + * d - Footer present + */ + sFlags.unsynch = BIT7((int)flags) > 0 ? 1 : 0; + sFlags.extHdr = BIT6((int)flags) > 0 ? 1 : 0; + sFlags.experimental = BIT5((int)flags) > 0 ? 1 : 0; + sFlags.footer = BIT4((int)flags) > 0 ? 1 : 0; + break; + } + + return sFlags; +} + +/* {{{ proto int _php_id3v2_get_tagLength(php_stream *stream TSRMLS_DC) + Returns the length in bytes of the id3v2-tag */ +int _php_id3v2_get_tagLength(php_stream *stream TSRMLS_DC) +{ +/* + Overall tag structure: + + +-----------------------------+ + | Header (10 bytes) | + +-----------------------------+ + | Extended Header | + | (variable length, OPTIONAL) | + +-----------------------------+ + | Frames (variable length) | + +-----------------------------+ + | Padding | + | (variable length, OPTIONAL) | + +-----------------------------+ + | Footer (10 bytes, OPTIONAL) | + +-----------------------------+ + + In general, padding and footer are mutually exclusive. +*/ + char size[5]; + int stdHdr = 10, + footer = 0; + + struct id3v2HdrFlags sFlags = _php_id3v2_get_hdrFlags(stream); + + if(sFlags.footer) { + footer = 10; + } + + php_stream_seek(stream, ID3V2_IDENTIFIER_LENGTH + 3, SEEK_SET); + return stdHdr + _php_bigEndian_to_Int(size, 4, sFlags.unsynch) + footer; +} +/* }}} */ + +/* {{{ proto int _php_bigEndian_to_Int(char* byteword, int bytewordlen, int synchsafe) + Converts a big-endian byte-stream into an integer */ +int _php_bigEndian_to_Int(char* byteword, int bytewordlen, int synchsafe) +{ + int intvalue = 0, + i; + + for (i = 0; i < bytewordlen; i++) { + if (synchsafe) { /* disregard MSB, effectively 7-bit bytes */ + intvalue = intvalue | (byteword[i] & 0x7F) << ((bytewordlen - 1 - i) * 7); + } else { + intvalue += byteword[i] * pow(256, (bytewordlen - 1 - i)); + } + } + return intvalue + 10; +} +/* }}} */ int _php_id3_get_version(php_stream *stream TSRMLS_DC) { - int version = 0; - char buf[4]; + int version = 0; + char buf[4], + majorVersion = 0, + revision = 0; /** * check for v1 @@ -650,7 +783,23 @@ php_stream_seek(stream, 0, SEEK_SET); php_stream_read(stream, buf, 3); if (strncmp("ID3", buf, 3) == 0) { - version = version | ID3_V2; + php_stream_read(stream, &majorVersion, 1); + php_stream_read(stream, &revision, 1); + + switch ((int)majorVersion) { + case 1: + version = version | ID3_V2_1; + break; + case 2: + version = version | ID3_V2_2; + break; + case 3: + version = version | ID3_V2_3; + break; + case 4: + version = version | ID3_V2_4; + break; + } } return version; } http://cvs.php.net/diff.php/pecl/id3/php_id3.h?r1=1.1&r2=1.2&ty=u Index: pecl/id3/php_id3.h diff -u pecl/id3/php_id3.h:1.1 pecl/id3/php_id3.h:1.2 --- pecl/id3/php_id3.h:1.1 Mon Jun 21 03:57:43 2004 +++ pecl/id3/php_id3.h Mon Aug 23 15:12:34 2004 @@ -13,11 +13,11 @@ | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Authors: Stephan Schmidt <schst@php.net> | - | Carsten Lucke <c.lucke@tool-garage.de> | + | Carsten Lucke <luckec@php.net> | +----------------------------------------------------------------------+ */ -/* $Id: php_id3.h,v 1.1 2004/06/21 07:57:43 schst Exp $ */ +/* $Id: php_id3.h,v 1.2 2004/08/23 19:12:34 luckec Exp $ */ #ifndef PHP_ID3_H #define PHP_ID3_H @@ -46,6 +46,47 @@ PHP_FUNCTION(id3_get_genre_list); PHP_FUNCTION(id3_get_genre_name); PHP_FUNCTION(id3_get_genre_id); + +/* macros */ +#define BIT0(a) (a & 1) +#define BIT1(a) (a & 2) +#define BIT2(a) (a & 4) +#define BIT3(a) (a & 8) +#define BIT4(a) (a & 16) +#define BIT5(a) (a & 32) +#define BIT6(a) (a & 64) +#define BIT7(a) (a & 128) + +/* constants */ +const int ID3V2_BASEHEADER_LENGTH = 10; +const int ID3V2_IDENTIFIER_LENGTH = 3; +/* version constants */ +const int ID3_V1_0 = 1; +const int ID3_V1_1 = 3; +const int ID3_V2_1 = 4; +const int ID3_V2_2 = 12; +const int ID3_V2_3 = 28; +const int ID3_V2_4 = 60; + +/* id3v2x flags + * + * 1 = flag is set + * 0 = flag isn't set + * -1 = id3-version doesn't know about this flag + */ +struct id3v2HdrFlags { + int unsynch; + int extHdr; + int experimental; + int footer; + int compression; +}; + +/* prototypes */ +struct id3v2HdrFlags _php_id3v2_get_hdrFlags(php_stream *stream TSRMLS_DC); +int _php_id3v2_get_tagLength(php_stream *stream TSRMLS_DC); +int _php_bigEndian_to_Int(char* byteword, int bytewordlen, int synchsafe); +zval* _php_id3v1_get_tag(php_stream *stream TSRMLS_DC); /* Declare any global variables you may need between the BEGIN
« previous php.pecl.cvs (#1710) next »