File: songid.c

package info (click to toggle)
libnjb1 1.1-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,400 kB
  • ctags: 594
  • sloc: ansic: 10,288; sh: 6,541; makefile: 175
file content (590 lines) | stat: -rw-r--r-- 14,850 bytes parent folder | download | duplicates (2)
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
#include "../config.h"
#include <stdlib.h>
#include <string.h>
#include "libnjb.h"
#include "njb_error.h"
#include "defs.h"
#include "protocol3.h"
#include "byteorder.h"
#include "unicode.h"

extern int njb_error;
extern int __sub_depth;
extern int njb_unicode_flag;

songid_t *songid_new (void)
{
	__dsub= "songid_new";
	songid_t *song;

	song= (songid_t *) malloc (sizeof(songid_t));
	if ( song == NULL ) {
		njb_error_add(__sub, EO_NOMEM);
		return NULL;
	}

	memset(song, 0, sizeof(songid_t));
	song->next = NULL;

	return song;
}

songid_t *songid_unpack (void *data, size_t nbytes)
{
	__dsub= "songid_unpack";
	unsigned char *dp= (unsigned char *) data;
	size_t index;
	songid_t *song;
	u_int16_t i, nframes;

	song= songid_new();
	if ( song == NULL ) return NULL;

	nframes = (dp[1] << 8) + dp[0];

	index = 2;
	for (i= 0; i<nframes; i++) {
		u_int16_t lsize, vsize, type;
		songid_frame_t *frame = NULL;

		type = njb1_bytes_to_16bit(&dp[index]);
		lsize = njb1_bytes_to_16bit(&dp[index+2]);
		vsize = njb1_bytes_to_16bit(&dp[index+4]);
		index += 8;

		/* Handle Unicode conversion on ASCII fields, or default
		 * to ISO 8859-1 */
		if (type == ID_DATA_ASCII && njb_unicode_flag == NJB_UC_UTF8) {
		  char *utf8str = NULL;
		  int utf8len;

		  utf8str= strtoutf8(&dp[index+lsize]);
		  if (utf8str == NULL) {
		    songid_destroy(song);
		    njb_error_add(__sub, EO_NOMEM);
		    return NULL;
		  }
		  utf8len= strlen(utf8str)+1;
		  frame= songid_frame_new(lsize, &dp[index], type, utf8len,
					  utf8str);
		  free(utf8str);
		} else {
		  frame= songid_frame_new(lsize, &dp[index], type, vsize,
					  &dp[index+lsize]);
		}
		if ( frame == NULL ) {
			songid_destroy(song);
			njb_error_add(__sub, EO_NOMEM);
			return NULL;
		}
		songid_addframe(song, frame);
		index += (lsize+vsize);

		if ( index > nbytes ) {
			songid_destroy(song);
			njb_error_add(__sub, EO_BADDATA);
			return NULL;
		}
	}

	return song;
}

unsigned char *songid_pack (songid_t *song, u_int32_t *tagsize)
{
	__dsub= "songid_pack";
	songid_frame_t *frame;
	unsigned char tagbuffer[1024];
	unsigned char *data;
	size_t index;

	*tagsize = 0;

	if ( !song->nframes ) {
	        /* The song has to have frames */
		njb_error_add(__sub, EO_BADDATA);
		return NULL;
	}

	from_16bit_to_njb1_bytes(song->nframes, &tagbuffer[0]);
	index = 2;

	songid_reset_getframe(song);
	while ((frame= songid_getframe(song))) {
	        /* Add type and size for this frame */
		from_16bit_to_njb1_bytes(frame->type, &tagbuffer[index]);
		from_16bit_to_njb1_bytes(frame->labelsz, &tagbuffer[index+2]);
		index += 4;

		/* ASCII frames such as filename, artist, album... */
		if (frame->type ==  ID_DATA_ASCII) {
		  char *frame_ascii_content = NULL;
		  u_int16_t frame_ascii_len = 0;

		  /* Convert ASCII fields to ISO 8859-1 as is used on NJB 1 */
		  if(njb_unicode_flag == NJB_UC_UTF8) {
		    frame_ascii_content = utf8tostr(frame->data);
		  } else {
		    frame_ascii_content = strdup(frame->data);
		  }
		  if (frame_ascii_content == NULL) {
		    njb_error_add(__sub, EO_NOMEM);
		    return NULL;
		  }

		  frame_ascii_len = strlen(frame_ascii_content)+1;
		  from_16bit_to_njb1_bytes(frame_ascii_len, &tagbuffer[index]);
		  tagbuffer[index+2] = 0x00;
		  tagbuffer[index+3] = 0x00;
		  index += 4;
		  memcpy(&tagbuffer[index], frame->label, frame->labelsz);
		  index += frame->labelsz;
		  memcpy(&tagbuffer[index], frame_ascii_content, frame_ascii_len);
		  free(frame_ascii_content);
		  index += frame_ascii_len;

		} else {
		  /* Numerical tags such as year or length */
		  from_16bit_to_njb1_bytes(frame->datasz, &tagbuffer[index]);
		  tagbuffer[index+2] = 0x00;
		  tagbuffer[index+3] = 0x00;
		  index += 4;
		  memcpy(&tagbuffer[index], frame->label, frame->labelsz);
		  index += frame->labelsz;
		  if ( frame->datasz == 1 ) {
		    unsigned char *dp = (unsigned char *) frame->data;

		    tagbuffer[index] = *dp;
		  } else if ( frame->datasz == 2 ) {
		    from_16bit_to_njb1_bytes(*(u_int16_t *)frame->data, 
					     &tagbuffer[index]);
		  } else if ( frame->datasz == 4 ) {
		    from_32bit_to_njb1_bytes(*(u_int32_t *)frame->data,
					     &tagbuffer[index]);
		  } else if ( frame->datasz == 8) {
		    from_32bit_to_njb1_bytes(*(u_int64_t *)frame->data,
					     &tagbuffer[index]);
		  } else {
		    /* Only numerical frames of size 1,2,4 and 8 are allowed. */
		    njb_error_add(__sub, EO_BADDATA);
		    return NULL;
		  }
		  index += frame->datasz;
		}
	}

	*tagsize = index;
	if (*tagsize == 0) {
		njb_error_add(__sub, EO_BADDATA);
		return NULL;
	}
	/* Duplicate the buffer and return */
	data = (unsigned char *) malloc (*tagsize);
	if ( data == NULL ) {
		njb_error_add(__sub, EO_NOMEM);
		return NULL;
	}
	memcpy(data, tagbuffer, *tagsize);

	return data;
}

/* These functions deal with putting data in packed NJB3 tags */
static u_int32_t valtoint32(unsigned char *data, int valsz)
{
  u_int32_t retval = 0;

  if (valsz == 2) {
    retval = (data[1]<<8) + data[0];
  }
  else if (valsz == 4) {
    retval = (data[3]<<24) + (data[2]<<16) + (data[1]<<8) + data[0];
  }
  return retval;
}

static void add_bin_unistr(unsigned char *data, u_int32_t *datap, u_int32_t tagtype, unsigned char *unistr)
{
  u_int32_t binlen;

  binlen = ucs2strlen(unistr) * 2 + 2;
  from_16bit_to_njb3_bytes(binlen+2, &data[*datap]);
  *datap += 2;
  from_16bit_to_njb3_bytes(tagtype, &data[*datap]);
  *datap += 2;
  memcpy(data+(*datap), unistr, binlen);
  *datap += binlen;
}

/* Same thing but for NJB3 */
unsigned char *songid_pack3 (songid_t *song, u_int32_t *tagsize)
{
	__dsub= "songid_pack3";
	songid_frame_t *frame;
	unsigned char *data = NULL;
	u_int32_t datap = 0;
	u_int32_t track_size = 0;
	u_int16_t track_year = 0;
	u_int32_t track_number = 0;
	u_int32_t track_length = 0;
	u_int32_t track_protected = 0;
	u_int32_t track_codec = NJB3_CODEC_MP3_ID;
	unsigned char *track_title = NULL;
	unsigned char *track_album = NULL;
	unsigned char *track_artist = NULL;
	unsigned char *track_genre = NULL;
	unsigned char *track_fname = NULL;

	/* Two bytes padding at the end plus
	 * ever-present tags (year, track number, codec) */
	*tagsize = 20;

	if ( !song->nframes ) {
		njb_error_add(__sub, EO_BADDATA);
		return NULL;
	}

	songid_reset_getframe(song);

	while ( (frame= songid_getframe(song)) ) {
	  if (!strcmp(frame->label, FR_SIZE)) {
	    /* 8 bytes, 2 length 2 tagid 4 bytes size */
	    track_size = valtoint32(frame->data, frame->datasz);
	    *tagsize+= 8;
	  }
	  else if (!strcmp(frame->label, FR_YEAR)) {
	    /* 6 bytes, 2 length 2 tagid 2bytes year */
	    /* This is a string so first convert it to an integer */
	    /* The interface uses a string, but PROTOCOL3 series
	     * uses an u_int32 value, so convert it */
	    char *dummy;
	    track_year = (u_int16_t) strtoul(frame->data, &dummy, 10);
	  }
	  else if (!strcmp(frame->label, FR_TRACK)) {
	    /* 6 bytes, 2 length 2 tagid 2bytes trackno */
	    track_number = valtoint32(frame->data, frame->datasz);
	  }
	  else if (!strcmp(frame->label, FR_LENGTH)) {
	    /* 6 bytes, 2 length 2 tagid 2bytes length */
	    track_length = valtoint32(frame->data, frame->datasz);
	    *tagsize+= 6;
	  }
	  /* For strings, stringlengh * 2 + 4 bytes */
	  else if (!strcmp(frame->label, FR_TITLE)) {
	    track_title = strtoucs2(frame->data);
	    *tagsize+= (6 + 2*ucs2strlen(track_title));
	  }
	  else if (!strcmp(frame->label, FR_ALBUM)) {
	    track_album = strtoucs2(frame->data);
	    *tagsize+= (6 + 2*ucs2strlen(track_album));
	  }
	  else if (!strcmp(frame->label, FR_ARTIST)) {
	    track_artist = strtoucs2(frame->data);
	    *tagsize+= (6 + 2*ucs2strlen(track_artist));
	  }
	  else if (!strcmp(frame->label, FR_GENRE)) {
	    track_genre = strtoucs2(frame->data);
	    *tagsize+= (6 + 2*ucs2strlen(track_genre));
	  }
	  else if (!strcmp(frame->label, FR_FNAME)) {
	    track_fname = strtoucs2(frame->data);
	    *tagsize+= (6 + 2*ucs2strlen(track_fname));
	  }
	  else if (!strcmp(frame->label, FR_CODEC)) {
	    /* 6 bytes, 2 length 2 tagid 2bytes year */
	    if (!strcmp(frame->data, NJB_CODEC_MP3)) {
	      /* Use old codec name NJB3_CODEC_MP3_ID_OLD on old firmware? */
	      track_codec = NJB3_CODEC_MP3_ID;
	    } else if (!strcmp(frame->data, NJB_CODEC_WAV)) {
	      track_codec = NJB3_CODEC_WAV_ID;
	    } else if (!strcmp(frame->data, NJB_CODEC_WMA)) {
	      track_codec = NJB3_CODEC_WMA_ID;
	    }
	  }
	  else if (!strcmp(frame->label, FR_PROTECTED)) {
	    track_protected = 1;
	  }
	}

	/* No extra information at all? Bad. */
	if ( *tagsize == 20) {
		njb_error_add(__sub, EO_BADDATA);
		return NULL;
	}

	data= (unsigned char *) malloc (*tagsize);
	if ( data == NULL ) {
		njb_error_add(__sub, EO_NOMEM);
		return NULL;
	}
	memset(data, 0, *tagsize);

	/* Then add the album strings */
	if (track_title != NULL)
	  add_bin_unistr(data, &datap, NJB3_TITLE_FRAME_ID, track_title);
	if (track_album != NULL)
	  add_bin_unistr(data, &datap, NJB3_ALBUM_FRAME_ID, track_album);
	if (track_artist != NULL)
	  add_bin_unistr(data, &datap, NJB3_ARTIST_FRAME_ID, track_artist);
	if (track_genre != NULL)
	  add_bin_unistr(data, &datap, NJB3_GENRE_FRAME_ID, track_genre);
	if (track_size != 0) {
	  from_16bit_to_njb3_bytes(6, &data[datap]);
	  datap += 2;
	  from_16bit_to_njb3_bytes(NJB3_FILESIZE_FRAME_ID, &data[datap]);
	  datap += 2;
	  from_32bit_to_njb3_bytes(track_size, &data[datap]);
	  datap += 4;
	}
	/* Default to unlocked files if == 0 */
	if (track_protected != 0) {
	  from_16bit_to_njb3_bytes(4, &data[datap]);
	  datap += 2;
	  from_16bit_to_njb3_bytes(NJB3_FILESIZE_FRAME_ID, &data[datap]);
	  datap += 2;
	  from_16bit_to_njb3_bytes(1, &data[datap]);
	  datap += 2;
	}
	/* These three are always stored */
	from_16bit_to_njb3_bytes(4, &data[datap]);
	datap += 2;
	from_16bit_to_njb3_bytes(NJB3_CODEC_FRAME_ID, &data[datap]);
	datap += 2;
	from_16bit_to_njb3_bytes(track_codec, &data[datap]);
	datap += 2;
	from_16bit_to_njb3_bytes(4, &data[datap]);
	datap += 2;
	from_16bit_to_njb3_bytes(NJB3_YEAR_FRAME_ID, &data[datap]);
	datap += 2;
	from_16bit_to_njb3_bytes(track_year, &data[datap]);
	datap += 2;
	from_16bit_to_njb3_bytes(4, &data[datap]);
	datap += 2;
	from_16bit_to_njb3_bytes(NJB3_TRACKNO_FRAME_ID, &data[datap]);
	datap += 2;
	from_16bit_to_njb3_bytes(track_number, &data[datap]);
	datap += 2;
	if (track_length != 0) {
	  from_16bit_to_njb3_bytes(4, &data[datap]);
	  datap += 2;
	  from_16bit_to_njb3_bytes(NJB3_LENGTH_FRAME_ID, &data[datap]);
	  datap += 2;
	  from_16bit_to_njb3_bytes(track_length, &data[datap]);
	  datap += 2;
	}
	if (track_fname != NULL)
	  add_bin_unistr(data, &datap, NJB3_FNAME_FRAME_ID, track_fname);

	if (track_title != NULL)
	  free(track_title);
	if (track_album != NULL)
	  free(track_album);
	if (track_artist != NULL)
	  free(track_artist);
	if (track_genre != NULL)
	  free(track_genre);
	if (track_fname != NULL)
	  free(track_fname);
	return data;
}


void songid_addframe (songid_t *song, songid_frame_t *frame)
{
	if ( song->nframes ) {
		song->last->next= frame;
		song->last= frame;
	} else {
		song->first= song->last= frame;
		song->cur= song->first;
	}

	frame->next= NULL;
	song->nframes++;
}

void songid_destroy (songid_t *song)
{
	songid_frame_t *frame;

	songid_reset_getframe(song);
	while ( (frame= songid_getframe(song)) ) {
		songid_frame_destroy(frame);
	}

	free(song);
}

void songid_reset_getframe (songid_t *song)
{
	song->cur= song->first;
}

songid_frame_t *songid_getframe (songid_t *song)
{
	songid_frame_t *t;

	if ( song->cur == NULL ) return NULL;
	t= song->cur;
	song->cur= song->cur->next;

	return t;
}

void songid_dump (songid_t *song, FILE *fp)
{
	songid_frame_t *frame;

	songid_reset_getframe(song);
	fprintf(fp, "ID: %u\n", song->trid);
	while( (frame= songid_getframe(song)) ) {
		songid_frame_dump(frame, fp);
	}
}

songid_frame_t *songid_findframe (songid_t *song, const char *label)
{
	songid_frame_t *frame;

	songid_reset_getframe(song);
	while ( (frame= songid_getframe(song)) ) {
		if ( ! strcmp(frame->label, label) ) return frame;
	}

	return NULL;
}

/*
 * Song ID frame methods 
 */

songid_frame_t *songid_frame_new (u_int16_t labelsz, const void *label,
	u_int16_t type, u_int16_t datasz, const void *data)
{
	__dsub= "songid_frame_new";
	songid_frame_t *frame;

	frame= (songid_frame_t *) malloc(sizeof(songid_frame_t));
	if ( frame == NULL ) {
		return NULL;
		njb_error_add(__sub, EO_NOMEM);
	}

	frame->label= malloc(labelsz);
	frame->data= malloc(datasz);
	if ( frame->label == NULL || frame->data == NULL ) {
		songid_frame_destroy(frame);
		njb_error_add(__sub, EO_NOMEM);
		return NULL;
	}

	frame->labelsz= labelsz;
	frame->datasz= datasz;
	frame->type= type;
	memcpy(frame->label, label, labelsz);
	memcpy(frame->data, data, datasz);

	return frame;
}

void songid_frame_destroy (songid_frame_t *frame)
{
	if ( frame->label != NULL ) free(frame->label);
	if ( frame->data != NULL ) free(frame->data);
	free(frame);
}

void songid_frame_dump (songid_frame_t *frame, FILE *fp)
{
	char *label;

	label= (char *) malloc (frame->labelsz + 1);
	if ( label == NULL ) {
		fprintf(fp, "(malloc failure)\n");
		return;
	}
	memcpy(label, frame->label, frame->labelsz);

	fprintf(fp, "%s: ", label);
	free(label);

	if ( frame->type == ID_DATA_ASCII ) {
		char *data;
		data= (char *) malloc (frame->datasz + 1);
		if ( data == NULL ) {
			fprintf(fp, "(malloc failure)\n");
			return;
		}
		memcpy(data, frame->data, frame->datasz);
		fprintf(fp, "%s\n", data);
		free(data);
	} else {
		if ( frame->datasz > 4 ) {
			fprintf(fp, "(can't display 64-bit int)\n");
		} else {
			fprintf(fp, "%u\n", songid_frame_data32(frame));
		}
	}
}

u_int32_t songid_frame_data32 (songid_frame_t *frame)
{
	if ( frame->datasz == 1 ) {
		char tval= ((char *) frame->data)[0];
		return (u_int32_t) tval;
	} else if ( frame->datasz == 2 ) {
		u_int16_t tval;
		memcpy(&tval, frame->data, 2);
		return (u_int32_t) tval;
	} else if ( frame->datasz == 4 ) {
		u_int32_t tval;
		memcpy(&tval, frame->data, 4);
		return tval;
	} 

	return 0;
}

/*
 * Info methods
 */

u_int32_t songid_size (songid_t *song)
{
	songid_frame_t *frame;

	frame= songid_findframe(song, FR_SIZE);
	if ( frame == NULL ) return 0;

	return (u_int32_t) songid_frame_data32(frame);
}

u_int32_t songid_length (songid_t *song)
{
	songid_frame_t *frame;

	frame= songid_findframe(song, FR_LENGTH);
	if ( frame == NULL ) return 0;

	return (u_int32_t) songid_frame_data32(frame);
}

u_int32_t songid_track (songid_t *song)
{
	songid_frame_t *frame;

	frame= songid_findframe(song, FR_TRACK);
	if ( frame == NULL ) return 0;

	return (u_int32_t) songid_frame_data32(frame);
}

const char *songid_label (songid_t *song, const char *label)
{
	songid_frame_t *frame;

	frame= songid_findframe(song, label);
	if ( frame == NULL ) return NULL;

	return (const char *) frame->data;
}