Mplib.xs

filter: Statementstmt 61.5% 262 / 426 Branchbran 49.5% 110 / 222 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 57.4% 372 / 648 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/MP3/Mplib.pm ↑ Summary
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4 #include <sys/time.h>
5 #include <time.h>
6  
7 #include "./mplib/mplib.h"
8  
9 static int
10 0 not_here(char *s)
11 {
12 0 croak("%s not implemented on this architecture", s);
13 return -1;
14 }
15  
16 static double
17 0 constant__(char *name, int len, int arg)
18 {
19 0 ▶ if (1 + 1 >= len ) {
Branch: 1 branch
BranchTrueFalseTotal
if (1 + 1 >= len ) { 0 0 0
20 0 errno = EINVAL;
21 0 return 0;
22 }
23 0 switch (name[1 + 1]) {
24 0 case 'B':
25 0 ▶ if (strEQ(name + 1, "_BEGIN_DECLS")) { /* _ removed */
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name + 1, "_BEGIN_DECLS")) { /* _ removed */ 0 0 0
26 #ifdef __BEGIN_DECLS
27 0 return __BEGIN_DECLS;
28 #else
29 goto not_there;
30 #endif
31 }
32 case 'E':
33 0 ▶ if (strEQ(name + 1, "_END_DECLS")) { /* _ removed */
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name + 1, "_END_DECLS")) { /* _ removed */ 0 0 0
34 #ifdef __END_DECLS
35 0 return __END_DECLS;
36 #else
37 goto not_there;
38 #endif
39 }
40 }
41 0 errno = EINVAL;
42 0 return 0;
43  
44 not_there:
45 errno = ENOENT;
46 return 0;
47 }
48  
49 static double
50 2 constant_UTF1(char *name, int len, int arg)
51 {
52 2 ▶ if (3 + 1 >= len ) {
Branch: 1 branch
BranchTrueFalseTotal
if (3 + 1 >= len ) { 0 2 2
53 0 errno = EINVAL;
54 0 return 0;
55 }
56 2 switch (name[4 + 1]) {
57 1 case '\0':
58 1 ▶ if (strEQ(name + 4, "6")) { /* UTF1 removed */
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name + 4, "6")) { /* UTF1 removed */ 1 0 1
59 #ifdef UTF16
60 1 return UTF16;
61 #else
62 goto not_there;
63 #endif
64 }
65 case 'B':
66 1 ▶ if (strEQ(name + 4, "6BE")) { /* UTF1 removed */
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name + 4, "6BE")) { /* UTF1 removed */ 1 0 1
67 #ifdef UTF16BE
68 1 return UTF16BE;
69 #else
70 goto not_there;
71 #endif
72 }
73 }
74 0 errno = EINVAL;
75 0 return 0;
76  
77 not_there:
78 errno = ENOENT;
79 return 0;
80 }
81  
82 static double
83 3 constant_U(char *name, int len, int arg)
84 {
85 3 ▶ if (1 + 2 >= len ) {
Branch: 1 branch
BranchTrueFalseTotal
if (1 + 2 >= len ) { 0 3 3
86 0 errno = EINVAL;
87 0 return 0;
88 }
89 3 switch (name[1 + 2]) {
90 2 case '1':
91 2 ▶ if (!strnEQ(name + 1,"TF", 2))
Branch: 1 branch
BranchTrueFalseTotal
if (!strnEQ(name + 1,"TF", 2)) 0 2 2
92 0 break;
93 2 return constant_UTF1(name, len, arg);
94 1 case '8':
95 1 ▶ if (strEQ(name + 1, "TF8")) { /* U removed */
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name + 1, "TF8")) { /* U removed */ 1 0 1
96 #ifdef UTF8
97 1 return UTF8;
98 #else
99 goto not_there;
100 #endif
101 }
102 }
103 0 errno = EINVAL;
104 0 return 0;
105  
106 not_there:
107 errno = ENOENT;
108 return 0;
109 }
110  
111 static double
112 2 constant_MP_A(char *name, int len, int arg)
113 {
114 2 switch (name[4 + 0]) {
115 1 case 'L':
116 1 ▶ if (strEQ(name + 4, "LBUM")) { /* MP_A removed */
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name + 4, "LBUM")) { /* MP_A removed */ 1 0 1
117 #ifdef MP_ALBUM
118 1 return MP_ALBUM;
119 #else
120 goto not_there;
121 #endif
122 }
123 case 'R':
124 1 ▶ if (strEQ(name + 4, "RTIST")) { /* MP_A removed */
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name + 4, "RTIST")) { /* MP_A removed */ 1 0 1
125 #ifdef MP_ARTIST
126 1 return MP_ARTIST;
127 #else
128 goto not_there;
129 #endif
130 }
131 }
132 0 errno = EINVAL;
133 0 return 0;
134  
135 not_there:
136 errno = ENOENT;
137 return 0;
138 }
139  
140 static double
141 2 constant_MP_T(char *name, int len, int arg)
142 {
143 2 switch (name[4 + 0]) {
144 1 case 'I':
145 1 ▶ if (strEQ(name + 4, "ITLE")) { /* MP_T removed */
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name + 4, "ITLE")) { /* MP_T removed */ 1 0 1
146 #ifdef MP_TITLE
147 1 return MP_TITLE;
148 #else
149 goto not_there;
150 #endif
151 }
152 case 'R':
153 1 ▶ if (strEQ(name + 4, "RACK")) { /* MP_T removed */
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name + 4, "RACK")) { /* MP_T removed */ 1 0 1
154 #ifdef MP_TRACK
155 1 return MP_TRACK;
156 #else
157 goto not_there;
158 #endif
159 }
160 }
161 0 errno = EINVAL;
162 0 return 0;
163  
164 not_there:
165 errno = ENOENT;
166 return 0;
167 }
168  
169 static double
170 4 constant_MP_EF(char *name, int len, int arg)
171 {
172 4 switch (name[5 + 0]) {
173 1 case 'C':
174 1 ▶ if (strEQ(name + 5, "COMPR")) { /* MP_EF removed */
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name + 5, "COMPR")) { /* MP_EF removed */ 1 0 1
175 #ifdef MP_EFCOMPR
176 1 return MP_EFCOMPR;
177 #else
178 goto not_there;
179 #endif
180 }
181 case 'E':
182 1 ▶ if (strEQ(name + 5, "ENCR")) { /* MP_EF removed */
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name + 5, "ENCR")) { /* MP_EF removed */ 1 0 1
183 #ifdef MP_EFENCR
184 1 return MP_EFENCR;
185 #else
186 goto not_there;
187 #endif
188 }
189 case 'N':
190 2 ▶ if (strEQ(name + 5, "NF")) { /* MP_EF removed */
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name + 5, "NF")) { /* MP_EF removed */ 2 0 2
191 #ifdef MP_EFNF
192 2 return MP_EFNF;
193 #else
194 goto not_there;
195 #endif
196 }
197 }
198 0 errno = EINVAL;
199 0 return 0;
200  
201 not_there:
202 errno = ENOENT;
203 return 0;
204 }
205  
206 static double
207 7 constant_MP_E(char *name, int len, int arg)
208 {
209 7 switch (name[4 + 0]) {
210 2 case 'E':
211 2 ▶ if (strEQ(name + 4, "ERROR")) { /* MP_E removed */
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name + 4, "ERROR")) { /* MP_E removed */ 2 0 2
212 #ifdef MP_EERROR
213 2 return MP_EERROR;
214 #else
215 goto not_there;
216 #endif
217 }
218 case 'F':
219 4 return constant_MP_EF(name, len, arg);
220 1 case 'V':
221 1 ▶ if (strEQ(name + 4, "VERSION")) { /* MP_E removed */
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name + 4, "VERSION")) { /* MP_E removed */ 1 0 1
222 #ifdef MP_EVERSION
223 1 return MP_EVERSION;
224 #else
225 goto not_there;
226 #endif
227 }
228 }
229 0 errno = EINVAL;
230 0 return 0;
231  
232 not_there:
233 errno = ENOENT;
234 return 0;
235 }
236  
237 static double
238 14 constant_M(char *name, int len, int arg)
239 {
240 14 ▶ if (1 + 2 >= len ) {
Branch: 1 branch
BranchTrueFalseTotal
if (1 + 2 >= len ) { 0 14 14
241 0 errno = EINVAL;
242 0 return 0;
243 }
244 14 switch (name[1 + 2]) {
245 2 case 'A':
246 2 ▶ if (!strnEQ(name + 1,"P_", 2))
Branch: 1 branch
BranchTrueFalseTotal
if (!strnEQ(name + 1,"P_", 2)) 0 2 2
247 0 break;
248 2 return constant_MP_A(name, len, arg);
249 1 case 'C':
250 1 ▶ if (strEQ(name + 1, "P_COMMENT")) { /* M removed */
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name + 1, "P_COMMENT")) { /* M removed */ 1 0 1
251 #ifdef MP_COMMENT
252 1 return MP_COMMENT;
253 #else
254 goto not_there;
255 #endif
256 }
257 case 'E':
258 7 ▶ if (!strnEQ(name + 1,"P_", 2))
Branch: 1 branch
BranchTrueFalseTotal
if (!strnEQ(name + 1,"P_", 2)) 0 7 7
259 0 break;
260 7 return constant_MP_E(name, len, arg);
261 1 case 'G':
262 1 ▶ if (strEQ(name + 1, "P_GENRE")) { /* M removed */
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name + 1, "P_GENRE")) { /* M removed */ 1 0 1
263 #ifdef MP_GENRE
264 1 return MP_GENRE;
265 #else
266 goto not_there;
267 #endif
268 }
269 case 'T':
270 2 ▶ if (!strnEQ(name + 1,"P_", 2))
Branch: 1 branch
BranchTrueFalseTotal
if (!strnEQ(name + 1,"P_", 2)) 0 2 2
271 0 break;
272 2 return constant_MP_T(name, len, arg);
273 1 case 'Y':
274 1 ▶ if (strEQ(name + 1, "P_YEAR")) { /* M removed */
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name + 1, "P_YEAR")) { /* M removed */ 1 0 1
275 #ifdef MP_YEAR
276 1 return MP_YEAR;
277 #else
278 goto not_there;
279 #endif
280 }
281 }
282 0 errno = EINVAL;
283 0 return 0;
284  
285 not_there:
286 errno = ENOENT;
287 return 0;
288 }
289  
290 static double
291 24 constant(char *name, int len, int arg)
292 {
293 24 errno = 0;
294 24 switch (name[0 + 0]) {
295 3 case 'I':
296 3 ▶ if (strEQ(name + 0, "ISO_8859_1")) { /* removed */
Branch: 1 branch
BranchTrueFalseTotal
if (strEQ(name + 0, "ISO_8859_1")) { /* removed */ 3 0 3
297 #ifdef ISO_8859_1
298 3 return ISO_8859_1;
299 #else
300 goto not_there;
301 #endif
302 }
303 case 'M':
304 14 return constant_M(name, len, arg);
305 3 case 'U':
306 3 return constant_U(name, len, arg);
307 0 case '_':
308 0 return constant__(name, len, arg);
309 }
310 4 errno = EINVAL;
311 4 return 0;
312  
313 not_there:
314 errno = ENOENT;
315 return 0;
316 }
317  
318 /* maintain backwards compatibility to at least 5.00503 */
319  
320 #ifndef SvPV_nolen
321 # define SvPV_nolen(sv) SvPV(sv, PL_na)
322 #endif
323  
324 #ifndef newSVuv
325 # define newSVuv(i) newSViv(i)
326 #endif
327  
328 #ifdef FIX_ANTIQUE_PERL
329 # define hv_store_fx(hv,str,strlen,sv,lval) \
330 hv_store(hv,(char*)str,strlen,(SV*)sv,lval)
331 # define get_sv(name,create) \
332 perl_get_sv(name,create)
333 #else
334 # define hv_store_fx(hv,str,strlen,sv,lval) \
335 hv_store(hv,str,strlen,sv,lval)
336 #endif
337  
338 #define isHaSHref(sv) (SvROK(sv)) && (SvTYPE(SvRV(sv)) == SVt_PVHV)
339  
340 /* the id3v2-tag fields we check for.
341 * I don't have the slightest idea what all these are four.
342 * So check the standard. :-) */
343  
344 #define V2FRAMES 74
345 const char * fields[V2FRAMES] = {
346 "COMM", "WXXX", "AENC", "APIC", "COMR", "ENCR", "EQUA", "ETCO", "GEOB",
347 "GRID", "IPLS", "LINK", "MCDI", "MLLT", "OWNE", "PRIV", "PCNT", "POPM",
348 "POSS", "RBUF", "RVAD", "RVRB", "SYLT", "SYTC", "TALB", "TBPM", "TCOM",
349 "TCON", "TCOP", "TDAT", "TDLY", "TENC", "TEXT", "TFLT", "TIME", "TIT1",
350 "TIT2", "TIT3", "TKEY", "TLAN", "TLEN", "TMED", "TOAL", "TOFN", "TOLY",
351 "TOPE", "TORY", "TOWN", "TPE1", "TPE2", "TPE3", "TPE4", "TPOS", "TPUB",
352 "TRCK", "TRDA", "TRSN", "TRSO", "TSIZ", "TSRC", "TSSE", "TYER", "TXXX",
353 "UFID", "USER", "USLT", "WCOM", "WCOP", "WOAF", "WOAR", "WOAS", "WORS",
354 "WPAY", "WPUB", };
355  
356 const char * fieldsv1[MP_TRACK] = {
357 "ARTIST", "TITLE", "ALBUM", "GENRE", "COMMENT", "YEAR", "TRACK",
358 };
359  
360 /* MP3::Mplib::Error */
361 SV * mp3_lib_err;
362  
363 /* turn mpeg_header into hash-ref */
364 0 SV * map_mpeg_header_to_sv(mpeg_header * header) {
365 0 HV * hash = newHV();
366  
367 /* so sucky: perhaps we should have used a typemap, eh? */
368 0 hv_store(hash, "syncword", 8, newSVuv(header->syncword), 0);
369 0 hv_store(hash, "version", 7, newSVpv(mp_get_str_version(header), 0), 0);
370 0 hv_store(hash, "layer", 5, newSVpv(mp_get_str_layer(header), 0), 0);
371 0 hv_store(hash, "protbit", 7, newSVuv(header->protbit), 0);
372 0 hv_store(hash, "bitrate", 7, newSVpv(mp_get_str_bitrate(header), 0), 0);
373 0 hv_store(hash, "samplingfreq", 12,
374 newSVpv(mp_get_str_samplingfreq(header), 0), 0);
375 0 hv_store(hash, "padbit", 6, newSVuv(header->padbit), 0);
376 0 hv_store(hash, "privbit", 7, newSVuv(header->privbit), 0);
377 0 hv_store(hash, "mode", 4, newSVpv(mp_get_str_mode(header), 0), 0);
378 0 hv_store(hash, "mode_ext", 8, newSVuv(header->mode_ext), 0);
379 0 hv_store(hash, "copyright", 9, newSVuv(header->copyright), 0);
380 0 hv_store(hash, "originalhome", 12, newSVuv(header->originalhome), 0);
381 0 hv_store(hash, "emphasis", 8, newSVuv(header->emphasis), 0);
382  
383 0 return newRV_noinc((SV *)hash);
384 }
385  
386 /* turn id3v2 header into hash-ref */
387 0 SV * map_id3v2_header_to_sv (id3v2_header * header) {
388 0 HV * hash = newHV();
389  
390 id3v2_extended_header * extheader;
391
392 0 hv_store(hash, "ver_minor", 9, newSVuv(header->version_minor), 0);
393 0 hv_store(hash, "ver_revision", 12, newSVuv(header->version_revision), 0);
394 0 hv_store(hash, "unsync", 6, newSVuv(header->unsyncronization), 0);
395 0 hv_store(hash, "experimental", 12, newSVuv(header->is_experimental), 0);
396 0 hv_store(hash, "footer", 6, newSVuv(header->has_footer), 0);
397 0 hv_store(hash, "total_tag_size", 14, newSVuv(header->total_tag_size), 0);
398
399 /* make extended header */
400 0 extheader = header->extended_header;
401 0 ▶ if (extheader != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (extheader != NULL) { 0 0 0
402 0 HV * ext = newHV();
403
404 0 hv_store(ext, "size", 4, newSVuv(extheader->size), 0);
405 0 hv_store(ext, "flag_bytes", 10, newSVpv(extheader->flag_bytes, 0), 0);
406 0 hv_store(ext, "no_flag_bytes", 13,
407 newSVuv(extheader->no_flag_bytes), 0);
408 0 hv_store(ext, "is_update", 9, newSVuv(extheader->is_update), 0);
409 0 hv_store(ext, "crc_data_present", 9,
410 newSVuv(extheader->crc_data_present), 0);
411 0 hv_store(ext, "crc_data_length", 15,
412 newSVuv(extheader->crc_data_length), 0);
413 0 hv_store(ext, "crc_data", 8, newSVpv(extheader->crc_data, 0), 0);
414 0 hv_store(ext, "restrictions", 12,
415 newSVuv(extheader->restrictions), 0);
416 0 hv_store(ext, "restrictions_data_length", 24,
417 newSVuv(extheader->restrictions_data_length), 0);
418 0 hv_store(ext, "restrictions_data", 17,
419 newSVpv(extheader->restrictions_data, 0), 0);
420
421 /* nest ext as hash-ref in previous hash */
422 0 hv_store(hash, "extended_header", 15, newRV_noinc((SV *) ext), 0);
423 }
424 0 return newRV_noinc((SV *)hash);
425 }
426  
427 /* turn id3_tag into hash-ref */
428 6 SV * map_id3_tag_to_sv(id3_tag * tag) {
429 6 HV * hash = newHV();
430  
431 6 ▶ if (tag->version == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (tag->version == 1) 3 3 6
432 3 fill_sv_v1tag(hash, tag);
433 6 ▶ if (tag->version == 2)
Branch: 1 branch
BranchTrueFalseTotal
if (tag->version == 2) 3 3 6
434 3 fill_sv_v2tag(hash, tag);
435 6 ▶ if (tag->version == -1)
Branch: 1 branch
BranchTrueFalseTotal
if (tag->version == -1) 0 6 6
436 0 return NULL;
437
438 6 return newRV_noinc((SV *)hash);
439 }
440  
441 /* returns to the tag of the specified version from the tag list
442 * returns NULL if no such tag has been found */
443 7 id3_tag * get_tag (id3_tag_list * tag_list, int ver) {
444  
445 7 id3_tag * tag = NULL;
446  
447 7 ▶ if(tag_list == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if(tag_list == NULL) 7 0 7
448 0 return NULL;
449  
450 while (1) {
451 10 ▶ if (tag_list->tag->version == ver)
Branch: 1 branch
BranchTrueFalseTotal
if (tag_list->tag->version == ver) 6 4 10
452 6 return tag_list->tag;
453 4 ▶ if (tag_list->next == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (tag_list->next == NULL) 1 3 4
454 1 return NULL;
455 3 tag_list = tag_list->next;
456 }
457 }
458  
459 /* add a new tag to the taglist: this happens when a tag is set that is not
460 * yet there */
461 4 id3_tag_list * add_tag (id3_tag_list * tag_list, id3_tag * tag) {
462
463 4 id3_tag_list *new = malloc(sizeof(id3_tag_list));
464 4 new->tag = tag;
465 4 new->first = NULL;
466 4 new->next = NULL;
467
468 /* no tag_list: return our newly created one */
469 4 ▶ if (tag_list == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (tag_list == NULL) { 3 1 4
470 1 tag_list = new;
471 1 tag_list->first = tag_list;
472 1 return tag_list;
473 }
474
475 /* tag_list present: replace existing tag with new one */
476 else {
477 while (1) {
478 4 ▶ if (tag_list->tag->version == tag->version) {
Branch: 1 branch
BranchTrueFalseTotal
if (tag_list->tag->version == tag->version) { 3 1 4
479 3 new->first = tag_list;
480 3 new->next = tag_list->next;
481 3 tag_list = new;
482 3 break;
483 }
484 1 ▶ if (tag_list->next)
Branch: 1 branch
BranchTrueFalseTotal
if (tag_list->next) 1 0 1
485 1 tag_list = tag_list->next;
486 else {
487 0 tag_list->next = new;
488 0 new->next = NULL;
489 0 new->first = tag_list;
490 0 break;
491 }
492 }
493 3 return tag_list;
494 }
495 }
496  
497 /* populates hv with content of id3_tag (v1!) structure */
498 3 int fill_sv_v1tag(HV * hv, id3_tag * t) {
499 id3_content * content;
500
501 /* kill me */
502 3 content = mp_get_content(t, MP_ARTIST);
503 3 ▶ hv_store(hv, "ARTIST", 6,
Branch: 1 branch
BranchTrueFalseTotal
hv_store(hv, "ARTIST", 6, 3 0 3
504 newSVpv(content ? mp_parse_artist(content)->text : "", 0), 0);
505
506 3 content = mp_get_content(t, MP_TITLE);
507 3 ▶ hv_store(hv, "TITLE", 5,
Branch: 1 branch
BranchTrueFalseTotal
hv_store(hv, "TITLE", 5, 3 0 3
508 newSVpv(content ? mp_parse_title(content)->text : "", 0), 0);
509  
510 3 content = mp_get_content(t, MP_ALBUM);
511 3 ▶ hv_store(hv, "ALBUM", 5,
Branch: 1 branch
BranchTrueFalseTotal
hv_store(hv, "ALBUM", 5, 3 0 3
512 newSVpv(content ? mp_parse_album(content)->text : "", 0), 0);
513  
514 3 content = mp_get_content(t, MP_GENRE);
515 3 ▶ hv_store(hv, "GENRE", 5,
Branch: 1 branch
BranchTrueFalseTotal
hv_store(hv, "GENRE", 5, 3 0 3
516 newSVpv(content ? mp_parse_genre(content)->text : "", 0), 0);
517
518 3 content = mp_get_content(t, MP_TRACK);
519 3 ▶ hv_store(hv, "TRACK", 5,
Branch: 1 branch
BranchTrueFalseTotal
hv_store(hv, "TRACK", 5, 2 1 3
520 newSVpv(content ? mp_parse_track(content)->text : "", 0), 0);
521
522 3 content = mp_get_content(t, MP_YEAR);
523 3 ▶ hv_store(hv, "YEAR", 4,
Branch: 1 branch
BranchTrueFalseTotal
hv_store(hv, "YEAR", 4, 3 0 3
524 newSVpv(content ? mp_parse_year(content)->text : "", 0), 0);
525  
526 3 content = mp_get_content(t, MP_COMMENT);
527 3 ▶ hv_store(hv, "COMMENT", 7,
Branch: 1 branch
BranchTrueFalseTotal
hv_store(hv, "COMMENT", 7, 3 0 3
528 newSVpv(content ? mp_parse_comment(content)->text : "", 0), 0);
529  
530 3 free(content);
531
532 3 }
533  
534 /* populates hv with content of id3_tag (v2!) structure */
535 3 int fill_sv_v2tag(HV * hv, id3_tag * t) {
536 id3_content * content;
537 int i;
538
539 /* COMM frame is special */
540 3 content = mp_get_content_custom (t, "COMM");
541 3 ▶ if (content != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (content != NULL) { 3 0 3
542 3 HV * comhsh = newHV();
543 3 id3_comment_content * comment = mp_parse_comment(content);
544 3 ▶ if (comment != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (comment != NULL) { 3 0 3
545 3 ▶ hv_store_fx(comhsh, "text", 4,
Branch: 1 branch
BranchTrueFalseTotal
hv_store_fx(comhsh, "text", 4, 3 0 3
546 newSVpv(comment->text != NULL
547 ? comment->text : "", 0), 0);
548 3 ▶ hv_store_fx(comhsh, "lang", 4,
Branch: 1 branch
BranchTrueFalseTotal
hv_store_fx(comhsh, "lang", 4, 3 0 3
549 newSVpv(comment->language != NULL
550 ? comment->language : "", 0), 0);
551 3 ▶ hv_store_fx(comhsh, "short", 5,
Branch: 1 branch
BranchTrueFalseTotal
hv_store_fx(comhsh, "short", 5, 2 1 3
552 newSVpv(comment->short_descr != NULL
553 ? comment->short_descr : "", 0), 0);
554 }
555 3 hv_store_fx(hv, "COMM", 4, newRV_inc((SV*)comhsh), 0);
556 3 mp_free_comment_content(comment);
557 }
558  
559 /* WXXX frame:
560 * I have to rely on my own patches to mplib here :-( */
561 3 content = mp_get_content_custom (t, "WXXX");
562 3 ▶ if (content != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (content != NULL) { 3 0 3
563 3 HV* wxhsh = newHV();
564 3 id3_wxxx_content *wxxx = mp_parse_wxxx(content);
565  
566 3 ▶ hv_store(wxhsh, "description", 11,
Branch: 1 branch
BranchTrueFalseTotal
hv_store(wxhsh, "description", 11, 3 0 3
567 newSVpv(wxxx->description ? wxxx->description : "", 0), 0);
568 3 ▶ hv_store(wxhsh, "url", 3,
Branch: 1 branch
BranchTrueFalseTotal
hv_store(wxhsh, "url", 3, 3 0 3
569 newSVpv(wxxx->url ? wxxx->url : "", 0), 0);
570 3 hv_store_fx(hv, "WXXX", 4, newRV_noinc((SV*)wxhsh), 0);
571 3 mp_free_wxxx_content(wxxx);
572 }
573  
574 /* quick and painless */
575 219 ▶ for (i = 2; i < 74; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 2; i < 74; i++) { 216 3 219
576 216 content = mp_get_content_custom (t, fields[i]);
577 216 ▶ if (content == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (content == NULL) 203 13 216
578 203 continue;
579 13 hv_store_fx(hv, fields[i], 4,
580 newSVpv(mp_parse_text(content)->text, 0), 0);
581 }
582 3 free(content);
583 3 }
584  
585 3 int set_v1_tag (const char * filename, HV * t, int enc) {
586  
587 id3_tag_list *newlist;
588  
589 /* need those to iterate over the hash t */
590 HE *iter_struct;
591 I32 key_len;
592 char *key;
593 char *val;
594 int ret;
595
596 /* construct new tag */
597 3 id3_tag_list *taglist = mp_get_tag_list_from_file(filename);
598 3 id3_tag *new_tag = mp_alloc_tag_with_version(1);
599 id3_content *content;
600
601  
602 3 hv_iterinit(t);
603 3 sv_setpv(mp3_lib_err, "");
604 10 ▶ while ((iter_struct = hv_iternext(t)) != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
while ((iter_struct = hv_iternext(t)) != NULL) { 8 2 10
605 int err;
606 8 key = hv_iterkey(iter_struct, &key_len);
607 8 val = SvPV_nolen(hv_iterval(t, iter_struct));
608
609 8 ▶ if (strcmp(key, "TRACK") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(key, "TRACK") == 0) { 0 8 8
610 0 content = mp_assemble_text_content(val, enc);
611 0 ▶ if ((err = mp_set_content(new_tag, MP_TRACK, content)) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((err = mp_set_content(new_tag, MP_TRACK, content)) != 0) 0 0 0
612 0 sv_catpvf(mp3_lib_err, "%s\034%i", "TRACK", err);
613 0 continue;
614 }
615 8 ▶ if (strcmp(key, "ARTIST") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(key, "ARTIST") == 0) { 1 7 8
616 1 content = mp_assemble_text_content(val, enc);
617 1 ▶ if ((err = mp_set_content(new_tag, MP_ARTIST, content)) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((err = mp_set_content(new_tag, MP_ARTIST, content)) != 0) 0 1 1
618 0 sv_catpvf(mp3_lib_err, "%s\034%i", "ARTIST", err);
619 1 continue;
620 }
621 7 ▶ if (strcmp(key, "COMMENT") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(key, "COMMENT") == 0) { 1 6 7
622 1 content = mp_assemble_comment_content(val, NULL, enc, NULL);
623 1 ▶ if ((err = mp_set_content(new_tag, MP_COMMENT, content)) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((err = mp_set_content(new_tag, MP_COMMENT, content)) != 0) 0 1 1
624 0 sv_catpvf(mp3_lib_err, "%s\034%i", "COMMENT", err);
625 1 continue;
626 }
627 6 ▶ if (strcmp(key, "GENRE") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(key, "GENRE") == 0) { 1 5 6
628 1 content = mp_assemble_text_content(val, enc);
629 1 ▶ if ((err = mp_set_content(new_tag, MP_GENRE, content)) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((err = mp_set_content(new_tag, MP_GENRE, content)) != 0) 0 1 1
630 0 sv_catpvf(mp3_lib_err, "%s\034%i", "GENRE", err);
631 1 continue;
632 }
633 5 ▶ if (strcmp(key, "ALBUM") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(key, "ALBUM") == 0) { 1 4 5
634 1 content = mp_assemble_text_content(val, enc);
635 1 ▶ if ((err = mp_set_content(new_tag, MP_ALBUM, content)) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((err = mp_set_content(new_tag, MP_ALBUM, content)) != 0) 0 1 1
636 0 sv_catpvf(mp3_lib_err, "%s\034%i", "ALBUM", err);
637 1 continue;
638 }
639 4 ▶ if (strcmp(key, "TITLE") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(key, "TITLE") == 0) { 2 2 4
640 2 content = mp_assemble_text_content(val, enc);
641 2 ▶ if ((err = mp_set_content(new_tag, MP_TITLE, content)) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((err = mp_set_content(new_tag, MP_TITLE, content)) != 0) 0 2 2
642 0 sv_catpvf(mp3_lib_err, "%s\034%i", "TITLE", err);
643 2 continue;
644 }
645 2 ▶ if (strcmp(key, "YEAR") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(key, "YEAR") == 0) { 1 1 2
646 1 content = mp_assemble_text_content(val, enc);
647 1 ▶ if ((err = mp_set_content(new_tag, MP_YEAR, content)) != 0)
Branch: 1 branch
BranchTrueFalseTotal
if ((err = mp_set_content(new_tag, MP_YEAR, content)) != 0) 0 1 1
648 0 sv_catpvf(mp3_lib_err, "%s\034%i", "YEAR", err);
649 1 continue;
650 }
651
652 /* we made it that far so there was an invalid tag */
653 1 sv_setpv(mp3_lib_err, "");
654 1 sv_catpvf(mp3_lib_err, "%s%c%i", key, '\034', MP_EFNF);
655  
656 /* we don't overwrite existing tag in this case */
657 1 mp_free_list(taglist);
658 1 return -1;
659 }
660
661 2 newlist = add_tag(taglist, new_tag);
662 2 ret = mp_write_to_file(newlist, filename);
663
664 2 mp_free_list(newlist);
665 2 mp_free_content(content);
666  
667 2 return ret;
668 }
669  
670 2 int set_v2_tag (const char * filename, HV * t, int enc) {
671
672 id3_tag_list *newlist;
673  
674 /* need those to iterate over the hash t */
675 HE * iter_struct;
676 I32 key_len;
677 char *key;
678 char *val;
679 int ret;
680  
681 /* construct new tag */
682 2 id3_tag_list *taglist = mp_get_tag_list_from_file(filename);
683 2 id3_tag *new_tag = mp_alloc_tag_with_version(2);
684
685 2 hv_iterinit(t);
686 9 ▶ while ((iter_struct = hv_iternext(t)) != NULL) {
Branch: 1 branch
BranchTrueFalseTotal
while ((iter_struct = hv_iternext(t)) != NULL) { 7 2 9
687
688 id3_content *content;
689 7 key = hv_iterkey(iter_struct, &key_len);
690  
691 /* handle comments */
692 7 ▶ if (strcmp(key, "COMM") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(key, "COMM") == 0) { 2 5 7
693 2 SV *comval = hv_iterval(t, iter_struct);
694 /* hash-ref? */
695 4 ▶ if (isHaSHref(comval)) {
Branch: 2 branches
BranchTrueFalseTotal
if (isHaSHref(comval)) { 2 0 2
if (isHaSHref(comval)) { 2 0 2
696 SV **comtext, **comshort, **comlang;
697 2 HV *comhsh = (HV *) SvRV(comval);
698 2 comtext = hv_fetch(comhsh, "text", 4, FALSE);
699 2 comshort = hv_fetch(comhsh, "short", 5, FALSE);
700 2 comlang = hv_fetch(comhsh, "lang", 4, FALSE);
701 8 ▶ content = mp_assemble_comment_content(
Branch: 3 branches
BranchTrueFalseTotal
content = mp_assemble_comment_content( 2 0 2
content = mp_assemble_comment_content( 2 0 2
content = mp_assemble_comment_content( 2 0 2
702 4 comtext != NULL ? SvPV_nolen(*comtext) : "",
703 4 comshort != NULL ? SvPV_nolen(*comshort) : "",
704 enc,
705 4 comlang != NULL ? SvPV_nolen(*comlang) : "ENG");
706 } else {
707 0 val = SvPV_nolen(hv_iterval(t, iter_struct));
708 0 ▶ content = mp_assemble_comment_content(
Branch: 1 branch
BranchTrueFalseTotal
content = mp_assemble_comment_content( 0 0 0
709 val != NULL ? val : "", "", enc, "ENG");
710 }
711 2 mp_set_custom_content(new_tag, "COMM", content);
712 2 continue;
713 } /* COMM */
714
715 5 ▶ if (strcmp(key, "WXXX") == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(key, "WXXX") == 0) { 2 3 5
716 2 SV *wxval = hv_iterval(t, iter_struct);
717 4 ▶ if (isHaSHref(wxval)) {
Branch: 2 branches
BranchTrueFalseTotal
if (isHaSHref(wxval)) { 2 0 2
if (isHaSHref(wxval)) { 2 0 2
718 SV **wxurl, **wxdesc;
719 2 HV *wxhsh = (HV *) SvRV(wxval);
720 2 wxurl = hv_fetch(wxhsh, "url", 3, FALSE);
721 2 wxdesc = hv_fetch(wxhsh, "description", 11, FALSE);
722 6 ▶ content = mp_assemble_wxxx_content(
Branch: 2 branches
BranchTrueFalseTotal
content = mp_assemble_wxxx_content( 2 0 2
content = mp_assemble_wxxx_content( 2 0 2
723 4 wxurl != NULL ? SvPV_nolen(*wxurl) : "",
724 4 wxdesc != NULL ? SvPV_nolen(*wxdesc) : "",
725 enc);
726 } else {
727 0 val = SvPV_nolen(hv_iterval(t, iter_struct));
728 0 ▶ content = mp_assemble_wxxx_content(
Branch: 1 branch
BranchTrueFalseTotal
content = mp_assemble_wxxx_content( 0 0 0
729 val != NULL ? val : "", "", enc);
730 }
731 2 mp_set_custom_content_at_pos(new_tag, "WXXX", content, 0);
732 2 continue;
733 } /* WXXX */
734  
735 /* all other frames */
736 3 val = SvPV_nolen(hv_iterval(t, iter_struct));
737 3 content = mp_assemble_text_content(val, enc);
738 3 ret = mp_set_custom_content_at_pos(new_tag, key, content, 0);
739 3 mp_free_content(content);
740  
741 3 ▶ if (ret != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (ret != 0) { 0 3 3
742 0 sv_setpv(mp3_lib_err, "");
743 0 sv_catpvf(mp3_lib_err, "%s%c%i", key, '\034', MP_EFNF);
744 }
745 }
746 2 newlist = add_tag(taglist, new_tag);
747 2 ret = mp_write_to_file(newlist, filename);
748 2 mp_free_list(newlist);
749
750 2 return ret;
751 }
752  
753 MODULE = MP3::Mplib PACKAGE = MP3::Mplib
754  
755 double
756 constant(sv,arg)
757 PREINIT:
758 STRLEN len;
759 INPUT:
760 SV * sv
761 char * s = SvPV(sv, len);
762 int arg
763 CODE:
764 24 RETVAL = constant(s,len,arg);
765 OUTPUT:
766 RETVAL
767  
768 void
769 get_header(filename)
770 char * filename;
771 PROTOTYPE: $
772 INIT:
773 mpeg_header * header;
774 PPCODE:
775 0 header = mp_get_mpeg_header_from_file(filename);
776 0 ▶ if (header == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (header == NULL) 0 0 0
777 0 XSRETURN_UNDEF;
778
779 0 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 0 0
780 0 PUSHs(sv_2mortal(map_mpeg_header_to_sv(header)));
781  
782 void
783 get_tag(filename, ver)
784 char * filename;
785 int ver;
786 PROTOTYPE: $$
787 INIT:
788 id3_tag_list * taglist;
789 id3_tag * tag;
790 PPCODE:
791 8 taglist = mp_get_tag_list_from_file(filename);
792 8 ▶ if (taglist == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (taglist == NULL) 1 7 8
793 1 XSRETURN_UNDEF;
794
795 7 tag = get_tag(taglist, ver);
796 7 ▶ if (tag == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (tag == NULL) 1 6 7
797 1 XSRETURN_UNDEF;
798
799 6 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 6 6
800 6 PUSHs(sv_2mortal(map_id3_tag_to_sv(tag)));
801 6 mp_free_list(taglist);
802  
803 void
804 get_id3v2_header(filename)
805 char * filename;
806 PROTOTYPE: $
807 INIT:
808 id3_tag_list * taglist;
809 id3_tag * tag;
810 PPCODE:
811 0 taglist = mp_get_tag_list_from_file(filename);
812 0 ▶ if (taglist == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (taglist == NULL) 0 0 0
813 0 XSRETURN_UNDEF;
814 0 tag = get_tag(taglist, 2);
815 0 ▶ if (tag == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (tag == NULL) 0 0 0
816 0 XSRETURN_UNDEF;
817 0 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 0 0
818 0 PUSHs(sv_2mortal(map_id3v2_header_to_sv(((id3v2_tag*)tag->tag)->header)));
819 0 mp_free_list(taglist);
820
821 void
822 set_tag (filename, ver, tag, enc = ISO_8859_1)
823 char * filename;
824 int ver;
825 SV * tag;
826 int enc;
827 PREINIT:
828 int ret;
829 PROTOTYPE: $$$;$
830 PPCODE:
831 5 ▶ if ( (!SvROK(tag)) ||
Branch: 1 branch
BranchTrueFalseTotal
if ( (!SvROK(tag)) || 5 0 5
832 5 ▶ (SvTYPE(SvRV(tag)) != SVt_PVHV) )
Branch: 1 branch
BranchTrueFalseTotal
(SvTYPE(SvRV(tag)) != SVt_PVHV) ) 0 5 5
833 0 croak("MP3::Mplib::set_tag expects a hash-ref as third arg");
834  
835 5 ▶ if (ver == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (ver == 1) 3 2 5
836 3 ret = set_v1_tag(filename, (HV *) SvRV(tag), enc);
837 2 ▶ else if (ver == 2)
Branch: 1 branch
BranchTrueFalseTotal
else if (ver == 2) 2 0 2
838 2 ret = set_v2_tag(filename, (HV *) SvRV(tag), enc);
839 else
840 0 croak("Unsupported tag version (v%i) in MP3::Mplib::set_header",
841 ver);
842  
843 5 ▶ if (ret == MP_EERROR)
Branch: 1 branch
BranchTrueFalseTotal
if (ret == MP_EERROR) 1 4 5
844 1 sv_setpvf(mp3_lib_err, "mp_file\034%i", MP_EERROR);
845  
846 5 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 5 5
847 5 PUSHs(sv_2mortal(newSViv(ret == 0 ? 1 : 0)));
848  
849 void
850 delete_tags(filename, ver)
851 char * filename;
852 int ver;
853 PROTOTYPE: $$
854 PPCODE:
855 2 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 2 2
856 2 switch(mp_del_tags_by_ver_from_file(filename, ver)) {
857 2 case 0:
858 2 PUSHs(sv_2mortal(newSVuv(1)));
859 2 break;
860 0 case 1:
861 0 PUSHs(sv_2mortal(newSVuv(0)));
862 0 break;
863 }
864  
865 void
866 _clean_up(filename)
867 char * filename;
868 PROTOTYPE: $
869 PPCODE:
870 id3_tag_list *taglist;
871 id3_tag * tag;
872 int i, j;
873  
874 0 taglist = mp_get_tag_list_from_file(filename);
875 0 tag = get_tag(taglist, 2);
876
877 0 ▶ if (tag == NULL)
Branch: 1 branch
BranchTrueFalseTotal
if (tag == NULL) 0 0 0
878 0 XSRETURN_UNDEF;
879  
880 0 ▶ for (i = 0; i < V2FRAMES; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < V2FRAMES; i++) { 0 0 0
881 0 j = 1;
882 while (1) {
883 0 ▶ if (mp_get_content_custom_at_pos(tag, fields[i], j))
Branch: 1 branch
BranchTrueFalseTotal
if (mp_get_content_custom_at_pos(tag, fields[i], j)) 0 0 0
884 0 mp_set_custom_content_at_pos(tag, (char*) fields[i],
885 NULL, j);
886 else
887 0 break;
888 0 j++;
889 }
890 }
891 0 ▶ if (mp_write_to_file(taglist, filename) == MP_EERROR)
Branch: 1 branch
BranchTrueFalseTotal
if (mp_write_to_file(taglist, filename) == MP_EERROR) 0 0 0
892 0 sv_setpvf(mp3_lib_err, "mp_file\034%i", MP_EERROR);
893 0 mp_free_list(taglist->first);
894  
895 void
896 _dump_structure(filename)
897 char * filename;
898 INIT:
899 id3_tag_list * taglist, *iter;
900 0 int c = 0;
901 PROTOTYPE: $
902 PPCODE:
903 0 taglist = mp_get_tag_list_from_file(filename);
904 0 ▶ if (taglist == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (taglist == NULL) { 0 0 0
905 0 printf("No tags found\n");
906 0 XSRETURN_UNDEF;
907 }
908 0 iter = taglist;
909 0 ▶ while (iter) {
Branch: 1 branch
BranchTrueFalseTotal
while (iter) { 0 0 0
910 0 id3_tag *tag = iter->tag;
911 0 printf("Tag at position %i:\n", ++c);
912 0 printf("Version: %i", tag->version);
913 0 ▶ if (tag->version == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (tag->version == 2) { 0 0 0
914 int i, j;
915 0 id3v2_header* head = ((id3v2_tag*)tag->tag)->header;
916 0 printf(" (id3v2.%i.%i)\n", head->version_minor,
917 head->version_revision);
918 0 printf("Fields set:\n");
919 0 ▶ for (i = 0; i < V2FRAMES; i++, j = 0) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < V2FRAMES; i++, j = 0) { 0 0 0
920 0 ▶ while (mp_get_content_custom_at_pos(tag, fields[i], j)) {
Branch: 1 branch
BranchTrueFalseTotal
while (mp_get_content_custom_at_pos(tag, fields[i], j)) { 0 0 0
921 int k;
922 0 char ind[4*(j+1)+1];
923 0 ▶ for (k = 0; k < j+1; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < j+1; k++) { 0 0 0
924 0 strcpy(&ind[4*k], " ");
925 }
926 0 ind[4*k+1] = 0;
927 0 printf("%s+%s at pos %i\n", ind, fields[i], j++);
928 }
929 }
930 0 printf("\n");
931 }
932 0 ▶ if (tag->version == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (tag->version == 1) { 0 0 0
933 int i;
934 0 printf(" (id3v1.1)\n");
935 0 printf("Fields set:\n");
936 0 ▶ for (i = MP_ARTIST; i <= MP_TRACK; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = MP_ARTIST; i <= MP_TRACK; i++) { 0 0 0
937 0 ▶ if (mp_get_content(tag, i))
Branch: 1 branch
BranchTrueFalseTotal
if (mp_get_content(tag, i)) 0 0 0
938 0 printf("\t+%s\n", fieldsv1[i - 1]);
939 }
940 0 printf("\n");
941 }
942 0 iter = iter->next;
943 }
944 0 mp_free_list(taglist);
945  
946 BOOT:
947 5 mp3_lib_err = get_sv("MP3::Mplib::Error", TRUE);
« blib/lib/MP3/Mplib.pm ↑ Summary