summaryrefslogtreecommitdiff
path: root/components/music/id3_frame_text.c
blob: 566c4ae9b9636d1caba8c7c2ef17b6ae0c87b30b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/*********************************************************************
 * 
 *    Copyright (C) 1999,  Espen Skoglund
 *    Department of Computer Science, University of Tromsų
 * 
 * Filename:      id3_frame_text.c
 * Description:   Code for handling ID3 text frames.
 * Author:        Espen Skoglund <espensk@stud.cs.uit.no>
 * Created at:    Fri Feb  5 23:50:33 1999
 *                
 * $Id$
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *                
 ********************************************************************/
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "id3.h"
#include "id3_header.h"


/*
 * Function id3_get_encoding (frame)
 *
 *    Return text encoding for frame, or -1 if frame does not have any
 *    text encoding.
 *
 */
gint8 id3_get_encoding(id3_frame_t *frame)
{
    /* Type check */
    if ( frame->fr_desc->fd_idstr[0] != 'T' &&
	 frame->fr_desc->fd_id != ID3_WXXX &&
	 frame->fr_desc->fd_id != ID3_IPLS &&
	 frame->fr_desc->fd_id != ID3_USLT &&
	 frame->fr_desc->fd_id != ID3_SYLT &&
	 frame->fr_desc->fd_id != ID3_COMM &&
	 frame->fr_desc->fd_id != ID3_APIC &&
	 frame->fr_desc->fd_id != ID3_GEOB &&
	 frame->fr_desc->fd_id != ID3_USER &&
	 frame->fr_desc->fd_id != ID3_OWNE &&
	 frame->fr_desc->fd_id != ID3_COMR )
	return -1;

    /* Check if frame is compressed */
    if ( frame->fr_data_z && !frame->fr_data )
	if ( id3_decompress_frame( frame ) == -1 )
	    return -1;

    return *(gint8 *) frame->fr_data;
}


/*
 * Function id3_set_encoding (frame, encoding)
 *
 *    Set text encoding for frame.  Return 0 upon success, or -1 if an
 *    error occured. 
 *
 */
int id3_set_encoding(id3_frame_t *frame, gint8 encoding)
{
    /* Type check */
    if ( frame->fr_desc->fd_idstr[0] != 'T' &&
	 frame->fr_desc->fd_id != ID3_WXXX &&
	 frame->fr_desc->fd_id != ID3_IPLS &&
	 frame->fr_desc->fd_id != ID3_USLT &&
	 frame->fr_desc->fd_id != ID3_SYLT &&
	 frame->fr_desc->fd_id != ID3_COMM &&
	 frame->fr_desc->fd_id != ID3_APIC &&
	 frame->fr_desc->fd_id != ID3_GEOB &&
	 frame->fr_desc->fd_id != ID3_USER &&
	 frame->fr_desc->fd_id != ID3_OWNE &&
	 frame->fr_desc->fd_id != ID3_COMR )
	return -1;

    /* Check if frame is compressed */
    if ( frame->fr_data_z && !frame->fr_data )
	if ( id3_decompress_frame( frame ) == -1 )
	    return -1;

    /* Changing the encoding of frames is not supported yet */
    if ( *(gint8 *) frame->fr_data != encoding )
	return -1;

    /* Set encoding */
    *(gint8 *) frame->fr_data = encoding;
    return 0;
}


/*
 * Function id3_get_text (frame)
 *
 *    Return string contents of frame.
 *
 */
char *id3_get_text(id3_frame_t *frame)
{
    /* Type check */
    if ( frame->fr_desc->fd_idstr[0] != 'T' )
	return NULL;

    /* Check if frame is compressed */
    if ( frame->fr_data_z && !frame->fr_data )
	if ( id3_decompress_frame( frame ) == -1 )
	    return NULL;

    if ( frame->fr_desc->fd_id == ID3_TXXX ) {
	/*
	 * This is a user defined text frame.  Skip the description.
	 */
	switch ( *(guint8 *) frame->fr_data ) {
	case ID3_ENCODING_ISO_8859_1:
	{
	    char *text = (char *) frame->fr_data + 1;

	    while ( *text != 0 )
		text++;

	    return ++text;
	}
	case ID3_ENCODING_UNICODE:
	{
	    gint16 *text16 = GINT_TO_POINTER ((GPOINTER_TO_INT (frame->fr_data) + 1));

	    while ( *text16 != 0 )
		text16++;

	    return (char *) ++text16;
	}
	default:
	    return NULL;
	}
    }

    return (char *) frame->fr_data + 1;
}


/*
 * Function id3_get_text_desc (frame)
 *
 *    Get description part of a text frame.
 *
 */
char *id3_get_text_desc(id3_frame_t *frame)
{
    /* Type check */
    if ( frame->fr_desc->fd_idstr[0] != 'T' )
	return NULL;

    /* If predefined text frame, return description. */
    if ( frame->fr_desc->fd_id != ID3_TXXX )
	return frame->fr_desc->fd_description;

    /* Check if frame is compressed */
    if ( frame->fr_data_z && !frame->fr_data )
	if ( id3_decompress_frame( frame ) == -1 )
	    return NULL;

    return (char *) frame->fr_data + 1;
}


/*
 * Function id3_get_text_number (frame)
 *
 *    Return string contents of frame translated to a positive
 *    integer, or -1 if an error occured.
 *
 */
int id3_get_text_number(id3_frame_t *frame)
{
    int number = 0;

    /* Check if frame is compressed */
    if ( frame->fr_data_z && !frame->fr_data )
	if ( id3_decompress_frame( frame ) == -1 )
	    return -1;

    /*
     * Generate integer according to encoding.
     */
    switch ( *(guint8 *) frame->fr_data ) {
    case ID3_ENCODING_ISO_8859_1:
    {
	char *text = ((char *) frame->fr_data) + 1;

	while ( *text >= '0' && *text <= '9' ) {
	    number *= 10;
	    number += *text - '0';
	    text++;
	}

	return number;
    }
    case ID3_ENCODING_UNICODE:
    {
	gint16 *text = ((gint16 *) frame->fr_data) + 1;

	while ( *text >= '0' && *text <= '9' ) {
	    number *= 10;
	    number += *text - '0';
	    text++;
	}

	return number;
    }

    default:
	return -1;
    }
}


/*
 * Function id3_set_text (frame, text)
 *
 *    Set text for the indicated frame (only ISO-8859-1 is currently
 *    supported).  Return 0 upon success, or -1 if an error occured.
 *
 */
int id3_set_text(id3_frame_t *frame, char *text)
{
    /* Type check */
    if ( frame->fr_desc->fd_idstr[0] != 'T' )
	return -1;

    /*
     * Release memory occupied by previous data.
     */
    if ( frame->fr_data )
	free( frame->fr_data );
    if ( frame->fr_data_z )
	free( frame->fr_data_z );
    frame->fr_data_z = NULL;
    frame->fr_size_z = 0;

    /*
     * Allocate memory for new data.
     */
    frame->fr_size = strlen(text) + 1;
    frame->fr_data = malloc( frame->fr_size + 1 );
    if ( frame->fr_data == NULL )
	return -1;

    /*
     * Copy contents.
     */
    *(gint8 *) frame->fr_data = ID3_ENCODING_ISO_8859_1;
    memcpy( (char *) frame->fr_data + 1, text, frame->fr_size );

    frame->fr_altered = 1;
    frame->fr_owner->id3_altered = 1;

    return 0;
}


/*
 * Function id3_set_text_number (frame, number)
 *
 *    Set number for the indicated frame (only ISO-8859-1 is currently
 *    supported).  Return 0 upon success, or -1 if an error occured.
 *
 */
int id3_set_text_number(id3_frame_t *frame, int number)
{
    char buf[64];
    int pos;
    char *text;

    /* Type check */
    if ( frame->fr_desc->fd_idstr[0] != 'T' )
	return -1;

    /*
     * Release memory occupied by previous data.
     */
    if ( frame->fr_data )
	free( frame->fr_data );
    if ( frame->fr_data_z )
	free( frame->fr_data_z );
    frame->fr_data_z = NULL;
    frame->fr_size_z = 0;

    /*
     * Create a string with a reversed number.
     */
    pos = 0;
    while ( number > 0 && pos < 64 ) {
	buf[pos++] = (number % 10) + '0';
	number /= 10;
    }
    if ( pos == 64 )
	return -1;
    if ( pos == 0 )
	buf[pos++] = '0';

    /*
     * Allocate memory for new data.
     */
    frame->fr_size = pos + 1;
    frame->fr_data = malloc( frame->fr_size + 1 );
    if ( frame->fr_data == NULL )
	return -1;

    /*
     * Insert contents.
     */
    *(gint8 *) frame->fr_data = ID3_ENCODING_ISO_8859_1;
    text = (char *) frame->fr_data + 1;
    while ( --pos >= 0 ) 
	*text++ = buf[pos];
    *text = '\0';

    frame->fr_altered = 1;
    frame->fr_owner->id3_altered = 1;

    return 0;
}