2 * libid3tag - ID3 tag manipulation library
3 * Copyright (C) 2000-2004 Underbit Technologies, Inc.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * $Id: util.c,v 1.9 2004/01/23 09:41:32 rob Exp $
35 * NAME: util->unsynchronise()
36 * DESCRIPTION: perform (in-place) unsynchronisation
38 id3_length_t
id3_util_unsynchronise(id3_byte_t
*data
, id3_length_t length
)
40 id3_length_t bytes
= 0, count
;
41 id3_byte_t
*end
= data
+ length
;
42 id3_byte_t
const *ptr
;
47 for (ptr
= data
; ptr
< end
- 1; ++ptr
) {
48 if (ptr
[0] == 0xff && (ptr
[1] == 0x00 || (ptr
[1] & 0xe0) == 0xe0))
58 for (count
= bytes
; count
; *--end
= *--ptr
) {
59 if (ptr
[-1] == 0xff && (ptr
[0] == 0x00 || (ptr
[0] & 0xe0) == 0xe0)) {
66 return length
+ bytes
;
70 * NAME: util->deunsynchronise()
71 * DESCRIPTION: undo unsynchronisation (in-place)
73 id3_length_t
id3_util_deunsynchronise(id3_byte_t
*data
, id3_length_t length
)
75 id3_byte_t
const *old
, *end
= data
+ length
;
81 for (old
= new = data
; old
< end
- 1; ++old
) {
83 if (old
[0] == 0xff && old
[1] == 0x00)
93 * NAME: util->compress()
94 * DESCRIPTION: perform zlib deflate method compression
96 id3_byte_t
*id3_util_compress(id3_byte_t
const *data
, id3_length_t length
,
97 id3_length_t
*newlength
)
99 id3_byte_t
*compressed
;
101 *newlength
= length
+ 12;
102 *newlength
+= *newlength
/ 1000;
104 compressed
= malloc(*newlength
);
106 if (compress2(compressed
, newlength
, data
, length
,
107 Z_BEST_COMPRESSION
) != Z_OK
||
108 *newlength
>= length
) {
115 resized
= realloc(compressed
, *newlength
? *newlength
: 1);
117 compressed
= resized
;
125 * NAME: util->decompress()
126 * DESCRIPTION: undo zlib deflate method compression
128 id3_byte_t
*id3_util_decompress(id3_byte_t
const *data
, id3_length_t length
,
129 id3_length_t newlength
)
131 id3_byte_t
*decompressed
;
133 decompressed
= malloc(newlength
? newlength
: 1);
139 if (uncompress(decompressed
, &size
, data
, length
) != Z_OK
||