Chris@0
|
1 /*
|
Chris@0
|
2 * libid3tag - ID3 tag manipulation library
|
Chris@0
|
3 * Copyright (C) 2000-2004 Underbit Technologies, Inc.
|
Chris@0
|
4 *
|
Chris@0
|
5 * This program is free software; you can redistribute it and/or modify
|
Chris@0
|
6 * it under the terms of the GNU General Public License as published by
|
Chris@0
|
7 * the Free Software Foundation; either version 2 of the License, or
|
Chris@0
|
8 * (at your option) any later version.
|
Chris@0
|
9 *
|
Chris@0
|
10 * This program is distributed in the hope that it will be useful,
|
Chris@0
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Chris@0
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Chris@0
|
13 * GNU General Public License for more details.
|
Chris@0
|
14 *
|
Chris@0
|
15 * You should have received a copy of the GNU General Public License
|
Chris@0
|
16 * along with this program; if not, write to the Free Software
|
Chris@0
|
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
Chris@0
|
18 *
|
Chris@0
|
19 * $Id: util.c,v 1.9 2004/01/23 09:41:32 rob Exp $
|
Chris@0
|
20 */
|
Chris@0
|
21
|
Chris@0
|
22 # ifdef HAVE_CONFIG_H
|
Chris@0
|
23 # include "config.h"
|
Chris@0
|
24 # endif
|
Chris@0
|
25
|
Chris@0
|
26 # include "global.h"
|
Chris@0
|
27
|
Chris@0
|
28 # include <stdlib.h>
|
Chris@0
|
29 # include <zlib.h>
|
Chris@0
|
30
|
Chris@0
|
31 # include "id3tag.h"
|
Chris@0
|
32 # include "util.h"
|
Chris@0
|
33
|
Chris@0
|
34 /*
|
Chris@0
|
35 * NAME: util->unsynchronise()
|
Chris@0
|
36 * DESCRIPTION: perform (in-place) unsynchronisation
|
Chris@0
|
37 */
|
Chris@0
|
38 id3_length_t id3_util_unsynchronise(id3_byte_t *data, id3_length_t length)
|
Chris@0
|
39 {
|
Chris@0
|
40 id3_length_t bytes = 0, count;
|
Chris@0
|
41 id3_byte_t *end = data + length;
|
Chris@0
|
42 id3_byte_t const *ptr;
|
Chris@0
|
43
|
Chris@0
|
44 if (length == 0)
|
Chris@0
|
45 return 0;
|
Chris@0
|
46
|
Chris@0
|
47 for (ptr = data; ptr < end - 1; ++ptr) {
|
Chris@0
|
48 if (ptr[0] == 0xff && (ptr[1] == 0x00 || (ptr[1] & 0xe0) == 0xe0))
|
Chris@0
|
49 ++bytes;
|
Chris@0
|
50 }
|
Chris@0
|
51
|
Chris@0
|
52 if (bytes) {
|
Chris@0
|
53 ptr = end;
|
Chris@0
|
54 end += bytes;
|
Chris@0
|
55
|
Chris@0
|
56 *--end = *--ptr;
|
Chris@0
|
57
|
Chris@0
|
58 for (count = bytes; count; *--end = *--ptr) {
|
Chris@0
|
59 if (ptr[-1] == 0xff && (ptr[0] == 0x00 || (ptr[0] & 0xe0) == 0xe0)) {
|
Chris@0
|
60 *--end = 0x00;
|
Chris@0
|
61 --count;
|
Chris@0
|
62 }
|
Chris@0
|
63 }
|
Chris@0
|
64 }
|
Chris@0
|
65
|
Chris@0
|
66 return length + bytes;
|
Chris@0
|
67 }
|
Chris@0
|
68
|
Chris@0
|
69 /*
|
Chris@0
|
70 * NAME: util->deunsynchronise()
|
Chris@0
|
71 * DESCRIPTION: undo unsynchronisation (in-place)
|
Chris@0
|
72 */
|
Chris@0
|
73 id3_length_t id3_util_deunsynchronise(id3_byte_t *data, id3_length_t length)
|
Chris@0
|
74 {
|
Chris@0
|
75 id3_byte_t const *old, *end = data + length;
|
Chris@0
|
76 id3_byte_t *new;
|
Chris@0
|
77
|
Chris@0
|
78 if (length == 0)
|
Chris@0
|
79 return 0;
|
Chris@0
|
80
|
Chris@0
|
81 for (old = new = data; old < end - 1; ++old) {
|
Chris@0
|
82 *new++ = *old;
|
Chris@0
|
83 if (old[0] == 0xff && old[1] == 0x00)
|
Chris@0
|
84 ++old;
|
Chris@0
|
85 }
|
Chris@0
|
86
|
Chris@0
|
87 *new++ = *old;
|
Chris@0
|
88
|
Chris@0
|
89 return new - data;
|
Chris@0
|
90 }
|
Chris@0
|
91
|
Chris@0
|
92 /*
|
Chris@0
|
93 * NAME: util->compress()
|
Chris@0
|
94 * DESCRIPTION: perform zlib deflate method compression
|
Chris@0
|
95 */
|
Chris@0
|
96 id3_byte_t *id3_util_compress(id3_byte_t const *data, id3_length_t length,
|
Chris@0
|
97 id3_length_t *newlength)
|
Chris@0
|
98 {
|
Chris@0
|
99 id3_byte_t *compressed;
|
Chris@0
|
100
|
Chris@0
|
101 *newlength = length + 12;
|
Chris@0
|
102 *newlength += *newlength / 1000;
|
Chris@0
|
103
|
Chris@0
|
104 compressed = malloc(*newlength);
|
Chris@0
|
105 if (compressed) {
|
Chris@0
|
106 if (compress2(compressed, newlength, data, length,
|
Chris@0
|
107 Z_BEST_COMPRESSION) != Z_OK ||
|
Chris@0
|
108 *newlength >= length) {
|
Chris@0
|
109 free(compressed);
|
Chris@0
|
110 compressed = 0;
|
Chris@0
|
111 }
|
Chris@0
|
112 else {
|
Chris@0
|
113 id3_byte_t *resized;
|
Chris@0
|
114
|
Chris@0
|
115 resized = realloc(compressed, *newlength ? *newlength : 1);
|
Chris@0
|
116 if (resized)
|
Chris@0
|
117 compressed = resized;
|
Chris@0
|
118 }
|
Chris@0
|
119 }
|
Chris@0
|
120
|
Chris@0
|
121 return compressed;
|
Chris@0
|
122 }
|
Chris@0
|
123
|
Chris@0
|
124 /*
|
Chris@0
|
125 * NAME: util->decompress()
|
Chris@0
|
126 * DESCRIPTION: undo zlib deflate method compression
|
Chris@0
|
127 */
|
Chris@0
|
128 id3_byte_t *id3_util_decompress(id3_byte_t const *data, id3_length_t length,
|
Chris@0
|
129 id3_length_t newlength)
|
Chris@0
|
130 {
|
Chris@0
|
131 id3_byte_t *decompressed;
|
Chris@0
|
132
|
Chris@0
|
133 decompressed = malloc(newlength ? newlength : 1);
|
Chris@0
|
134 if (decompressed) {
|
Chris@0
|
135 id3_length_t size;
|
Chris@0
|
136
|
Chris@0
|
137 size = newlength;
|
Chris@0
|
138
|
Chris@0
|
139 if (uncompress(decompressed, &size, data, length) != Z_OK ||
|
Chris@0
|
140 size != newlength) {
|
Chris@0
|
141 free(decompressed);
|
Chris@0
|
142 decompressed = 0;
|
Chris@0
|
143 }
|
Chris@0
|
144 }
|
Chris@0
|
145
|
Chris@0
|
146 return decompressed;
|
Chris@0
|
147 }
|