-
Notifications
You must be signed in to change notification settings - Fork 4
/
junklib.c
3781 lines (3469 loc) · 115 KB
/
junklib.c
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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
DeaDBeeF - ultimate music player for GNU/Linux systems with X11
Copyright (C) 2009-2011 Alexey Yakovenko <waker@users.sourceforge.net>
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, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "junklib.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if HAVE_ICONV
#define LIBICONV_PLUG
#include <iconv.h>
#elif HAVE_ICU
#warning icu
#include <unicode/utypes.h>
#include <unicode/ucnv.h>
#else
#define DDB_RECODE
#include "ConvertUTF/ConvertUTF.h"
uint16_t sj_to_unicode[] = {
#include "sj_to_unicode.h"
};
#endif
#include <limits.h>
#include <errno.h>
#include <ctype.h>
#include <unistd.h>
#include <assert.h>
#include "playlist.h"
#include "utf8.h"
#include "plugins.h"
#define MAX_TEXT_FRAME_SIZE 1024
#define MAX_CUESHEET_FRAME_SIZE 10000
#define MAX_APEV2_FRAME_SIZE 2000000
#define MAX_ID3V2_FRAME_SIZE 100000
#define MAX_ID3V2_APIC_FRAME_SIZE 2000000
#define UTF8_STR "utf-8"
//#define trace(...) { fprintf(stderr, __VA_ARGS__); }
#define trace(fmt,...)
#define min(x,y) ((x)<(y)?(x):(y))
#define max(x,y) ((x)>(y)?(x):(y))
// mapping between ddb metadata names and id3v2/apev2 names
#define FRAME_MAPPINGS 5
enum {
MAP_DDB = 0,
MAP_ID3V23 = 1,
MAP_ID3V24 = 2,
MAP_ID3V22 = 3,
MAP_APEV2 = 4
};
// map of known id3v2 and apev2 text tags
// order: ddb, id3-2.3, 2.4, 2.2, apev2
static const char *frame_mapping[] = {
// these tags will be displayed and edited uniformly for all tag types
"artist", "TPE1", "TPE1", "TP1", "Artist",
"disc", "TPOS", "TPOS", "TPA", "Disc",
"title", "TIT2", "TIT2", "TT2", "Title",
"album", "TALB", "TALB", "TAL", "Album",
"copyright", "TCOP", "TCOP", "TCO", "Copyright",
"genre", "TCON", "TCON", "TCO", "Genre",
"composer", "TCOM", "TCOM", "TCM", "Composer",
"year", "TYER", "TDRC", "TYE", "Year", // NOTE: TDRC and TYER are slightly different, and are converted on read/write
"track", "TRCK", "TRCK", "TRK", "Track", // NOTE: this is special case when writing id3v2
// misc id3v2 fields
// these might or might not have appropriate fields in every tag type
"BAND", "TPE2", "TPE2", "TP2", NULL,
"ENCODER", "TENC", "TENC", "TEN", NULL,
"BEATS_PER_MINUTE", "TBPM", "TBPM", "TBP", NULL,
"PLAYLIST_DELAY", "TDLY", "TDLY", "TDY", NULL,
"TEXT_WRITERS", "TEXT", "TEXT", "TXT", NULL,
"FILE_TYPE", "TFLT", "TFLT", "TFT", NULL,
"CONTENT_GROUP_DESCRIPTION", "TIT1", "TIT1", "TT1", NULL,
"SUBTITLE", "TIT3", "TIT3", "TT3", NULL,
"INITIAL_KEY", "TKEY", "TKEY", "TKE", NULL,
"LANGUAGES", "TLAN", "TLAN", "TLA", NULL,
"LENGTH", "TLEN", "TLEN", "TLE", NULL,
"MEDIA_TYPE", "TMED", "TMED", "TMT", NULL,
"ORIGINAL_ALBUM_TITLE", "TOAL", "TOAL", "TOT", NULL,
"ORIGINAL_FILENAME", "TOFN", "TOFN", "TOF", NULL,
"ORIGINAL_TEXT_WRITERS", "TOLY", "TOLY", "TOL", NULL,
"ORIGINAL_ARTISTS", "TOPE", "TOPE", "TOA", NULL,
"FILE_OWNER", "TOWN", "TOWN", NULL, NULL,
"PERFORMER_REFINEMENT", "TPE3", "TPE3", "TP3", NULL,
"MODIFIED_BY", "TPE4", "TPE4", "TP4", NULL,
"PUBLISHER", "TPUB", "TPUB", "TPB", NULL,
"INTERNET_RADIO_STATION_NAME", "TRSN", "TRSN", NULL, NULL,
"INTERNET_RADIO_STATION_OWNER", "TRSO", "TRSO", NULL, NULL,
"ISRC", "TSRC", "TSRC", NULL, NULL,
"ENCODING_SOFTWARE_HARDWARE", "TSSE", "TSSE", "TSS", NULL,
"RECORDING_TIME", NULL, "TDRC", NULL, NULL,
"RELEASE_TIME", NULL, "TDRL", NULL, NULL,
"TAGGING_TIME", NULL, "TDTG", NULL, NULL,
"ALBUM_SORT_ORDER", NULL, "TSOA", NULL, NULL,
"PERFORMER_SORT_ORDER", NULL, "TSOP", NULL, NULL,
"TITLE_SORT_ORDER", NULL, "TSOT", NULL, NULL,
"SIZE", "TSIZ", NULL, "TSI", NULL,
"RECORDING_DATES", "TRDA", NULL, "TRD", NULL,
"INVOLVED_PEOPLE_LIST", NULL, "TIPL", NULL, NULL,
"MUSICIAN_CREDITS_LIST", NULL, "TMCL", NULL, NULL,
"ENCODING_TIME", NULL, "TDEN", NULL, NULL,
"ORIGINAL_RELEASE_TIME", NULL, "TDOR", NULL, NULL,
"MOOD", NULL, "TMOO", NULL, NULL,
"PRODUCED_NOTICE", NULL, "TPRO", NULL, NULL,
NULL
};
static const char *txx_mapping[] = {
"replaygain_album_gain", NULL,
"replaygain_album_peak", NULL,
"replaygain_track_gain", NULL,
"replaygain_track_peak", NULL,
NULL
};
static uint32_t
extract_i32 (const uint8_t *buf)
{
uint32_t x;
// big endian extract
x = buf[0];
x <<= 8;
x |= buf[1];
x <<= 8;
x |= buf[2];
x <<= 8;
x |= buf[3];
return x;
}
static inline uint32_t
extract_i32_le (unsigned char *buf)
{
uint32_t x;
// little endian extract
x = buf[3];
x <<= 8;
x |= buf[2];
x <<= 8;
x |= buf[1];
x <<= 8;
x |= buf[0];
return x;
}
static inline uint16_t
extract_i16 (const uint8_t *buf)
{
uint16_t x;
// big endian extract
x = buf[0];
x <<= 8;
x |= buf[1];
x <<= 8;
return x;
}
static inline float
extract_f32 (unsigned char *buf) {
float f;
uint32_t *x = (uint32_t *)&f;
*x = buf[0];
*x <<= 8;
*x |= buf[1];
*x <<= 8;
*x |= buf[2];
*x <<= 8;
*x |= buf[3];
return f;
}
#ifdef DDB_RECODE
static int
cp1251_to_utf8(const uint8_t *in, int inlen, uint8_t *out, int outlen){
int len = 0;
while (inlen > 0 && outlen-len > 2) {
uint8_t c=*in;
if (c>=192 && c<=239) {
*out++ = 208;
*out++ = c - 48;
len += 2;
}
else if (c>239) {
*out++ = 209;
*out++ = c - 112;
len += 2;
}
else if (c==184) {
*out++ = 0xd1;
*out++ = 0x91;
len += 2;
}
else if (c==168) {
*out++ = 208;
*out++ = 129;
len += 2;
}
else {
*out++ = c;
len++;
}
in++;
inlen--;
}
*out = 0;
return len;
}
static int
iso8859_1_to_utf8(const uint8_t *in, int inlen, uint8_t *out, int outlen){
int len = 0;
while (inlen > 0 && outlen-len > 2) {
uint8_t c=*in;
switch (c) {
case 192 ... 255:
*out++ = 195;
*out++ = c - 64;
len += 2;
break;
case 160 ... 191:
*out++ = 0xc2;
*out++ = c;
len += 2;
break;
#define CONV2(x,y,z) case x:\
*out++ = y;\
*out++ = z;\
len += 2;\
break;
#define CONV3(x,y,z,w) case x:\
*out++ = y;\
*out++ = z;\
*out++ = w;\
len += 3;\
break;
CONV2(0x9f,0xc5,0xb8);
CONV2(0x9e,0xc5,0xbe);
CONV3(0x9d,0xef,0xbf,0xbd);
CONV3(0x80,0xe2,0x82,0xac);
CONV3(0x81,0xef,0xbf,0xbd);
CONV3(0x82,0xe2,0x80,0x9a);
CONV2(0x83,0xc6,0x92);
CONV3(0x84,0xe2,0x80,0x9e);
CONV3(0x85,0xe2,0x80,0xa6);
CONV3(0x86,0xe2,0x80,0xa0);
CONV3(0x87,0xe2,0x80,0xa1);
CONV2(0x88,0xcb,0x86);
CONV3(0x89,0xe2,0x80,0xb0);
CONV2(0x8a,0xc5,0xa0);
CONV3(0x8b,0xe2,0x80,0xb9);
CONV2(0x8c,0xc5,0x92);
CONV3(0x8d,0xef,0xbf,0xbd);
CONV2(0x8e,0xc5,0xbd);
CONV3(0x8f,0xef,0xbf,0xbd);
CONV3(0x90,0xef,0xbf,0xbd);
CONV3(0x91,0xe2,0x80,0x98);
CONV3(0x92,0xe2,0x80,0x99);
CONV3(0x93,0xe2,0x80,0x9c);
CONV3(0x94,0xe2,0x80,0x9d);
CONV3(0x95,0xe2,0x80,0xa2);
CONV3(0x96,0xe2,0x80,0x93);
CONV3(0x97,0xe2,0x80,0x94);
CONV2(0x98,0xcb,0x9c);
CONV3(0x99,0xe2,0x84,0xa2);
CONV2(0x9a,0xc5,0xa1);
CONV3(0x9b,0xe2,0x80,0xba);
CONV2(0x9c,0xc5,0x93);
#undef CONV2
#undef CONV3
default:
if (c >= 127) {
trace ("iso8859 char: %d\n", c);
}
*out++ = c;
len++;
break;
}
in++;
inlen--;
}
*out = 0;
return len;
}
int ddb_iconv (const char *cs_out, const char *cs_in, char *out, int outlen, const char *in, int inlen) {
int len = -1;
*out = 0;
if (inlen==0) {
return 0;
}
// to utf8 branch
if (!strcmp (cs_out, UTF8_STR)) {
if (!strcmp (cs_in, UTF8_STR)) {
memcpy (out, in, inlen);
out[inlen] = 0;
int valid = u8_valid (out, inlen, NULL);
if (valid) {
len = inlen;
}
}
else if (!strcasecmp (cs_in, "cp1251")) {
len = cp1251_to_utf8 (in, inlen, out, outlen);
}
else if (!strcasecmp (cs_in, "iso8859-1")) {
len = iso8859_1_to_utf8 (in, inlen, out, outlen);
}
else if (!strcasecmp (cs_in, "UTF-16LE") || !strcasecmp (cs_in, "UCS-2LE")) {
char *target = out;
ConversionResult result = ConvertUTF16toUTF8 ((const UTF16**)&in, (const UTF16*)(in + inlen), (UTF8**)&target, (UTF8*)(out + outlen), strictConversion);
if (result == conversionOK) {
*target = 0;
len = target - out;
}
}
else if (!strcasecmp (cs_in, "UTF-16BE") || !strcasecmp (cs_in, "UCS-2BE")) {
assert (0);
// convert to big endian
char temp[inlen];
for (int i = 0; i < inlen; i += 2) {
temp[i] = in[i+1];
temp[i+1] = in[i];
}
char *target = out;
char *src = temp;
ConversionResult result = ConvertUTF16toUTF8 ((const UTF16**)&src, (const UTF16*)(temp + inlen), (UTF8**)&target, (UTF8*)(out + outlen), strictConversion);
if (result == conversionOK) {
*target = 0;
len = target - out;
}
}
else if (!strcasecmp (cs_in, "SHIFT-JIS")) {
int len = 0;
while (inlen > 0 && len < outlen) {
if (*in > 0) {
*out++ = *in++;
inlen--;
len++;
}
else if (inlen < 2) {
return -1;
}
else {
// find character in table
uint16_t c = (((uint8_t*)in)[0] << 8) | ((uint8_t*)in)[1];
int i;
for (i = 0; sj_to_unicode[i]; i += 2) {
if (c == sj_to_unicode[i]) {
break;
}
}
if (sj_to_unicode[i]) {
// slow conversion!
char unicode_val[2] = { (sj_to_unicode[i+1] & 0xff00) >> 8, sj_to_unicode[i+1] & 0xff };
char utf8_val[5];
char *src = unicode_val, *dst = utf8_val;
ConversionResult res = ConvertUTF16toUTF8 ((const UTF16**)&src, (const UTF16 *)(src+2), (UTF8**)&dst, dst+5, strictConversion);
if (res == conversionOK) {
if (src - utf8_val < outlen-len) {
memcpy (out, utf8_val, src - utf8_val);
out += src - utf8_val;
len += src - utf8_val;
inlen -= 2;
in += 2;
}
else {
return -1;
}
}
else {
return -1;
}
}
else {
return -1; // error
}
}
}
}
else {
fprintf (stderr, "invalid conversion request: %s -> %s\n", cs_in, cs_out);
}
}
else if (!strcmp (cs_in, UTF8_STR)) {
if (!strcasecmp (cs_out, "UTF-16LE") || !strcasecmp (cs_out, "UCS-2LE")) {
char *target = out;
ConversionResult result = ConvertUTF8toUTF16 ((const UTF8**)&in, (const UTF8*)(in + inlen), (UTF16**)&target, (UTF16*)(out + outlen), strictConversion);
if (result == conversionOK) {
*target = 0;
*(target+1) = 0;
len = target - out;
}
}
else if (!strcasecmp (cs_out, "UTF-16BE") || !strcasecmp (cs_out, "UCS-2BE")) {
assert (0);
char temp[outlen];
char *target = temp;
const char *src = in;
ConversionResult result = ConvertUTF16toUTF8 ((const UTF16**)&src, (const UTF16*)(src + inlen), (UTF8**)&target, (UTF8*)(target + outlen), strictConversion);
if (result == conversionOK) {
len = target - temp;
// convert to big endian
for (int i = 0; i < len; i += 2) {
out[i] = temp[i+1];
out[i+1] = temp[i];
}
}
}
else {
fprintf (stderr, "invalid conversion request: %s -> %s\n", cs_in, cs_out);
}
}
else {
fprintf (stderr, "invalid conversion request: %s -> %s\n", cs_in, cs_out);
}
trace ("\033[0;31mddb_iconv: %s -> %s, in: %s, out: %s\033[37;0m\n", cs_in, cs_out, in, out);
return len;
}
#endif
int
junk_iconv (const char *in, int inlen, char *out, int outlen, const char *cs_in, const char *cs_out) {
// NOTE: this function must support utf8->utf8 conversion, used for validation
#if HAVE_ICONV
iconv_t cd = iconv_open (cs_out, cs_in);
if (cd == (iconv_t)-1) {
return -1;
}
#ifdef __linux__
char *pin = (char*)in;
#else
const char *pin = in;
#endif
size_t inbytesleft = inlen;
size_t outbytesleft = outlen;
char *pout = out;
memset (out, 0, outbytesleft);
size_t res = iconv (cd, &pin, &inbytesleft, &pout, &outbytesleft);
int err = errno;
iconv_close (cd);
//trace ("iconv -f %s -t %s '%s': returned %d, inbytes %d/%d, outbytes %d/%d, errno=%d\n", cs_in, cs_out, in, res, inlen, inbytesleft, outlen, outbytesleft, err);
if (res == -1) {
return -1;
}
//trace ("iconv out: %s (len=%d)\n", out, pout - out);
return pout - out;
#elif defined(HAVE_ICU)
int status = 0;
trace ("ICU convert from %s to %s input %s\n", cs_in, cs_out, in);
int32_t len = ucnv_convert (cs_out, cs_in, out, outlen, in, inlen, &status);
out[len] = 0;
trace ("ICU out: %s\n", out);
return len;
#else
int len = ddb_iconv (cs_out, cs_in, out, outlen, in, inlen);
return len;
#endif
}
static const char *junk_genretbl[] = {
"Blues",
"Classic Rock",
"Country",
"Dance",
"Disco",
"Funk",
"Grunge",
"Hip-Hop",
"Jazz",
"Metal",
"New Age",
"Oldies",
"Other",
"Pop",
"R&B",
"Rap",
"Reggae",
"Rock",
"Techno",
"Industrial",
"Alternative",
"Ska",
"Death Metal",
"Pranks",
"Soundtrack",
"Euro-Techno",
"Ambient",
"Trip-Hop",
"Vocal",
"Jazz+Funk",
"Fusion",
"Trance",
"Classical",
"Instrumental",
"Acid",
"House",
"Game",
"Sound Clip",
"Gospel",
"Noise",
"AlternRock",
"Bass",
"Soul",
"Punk",
"Space",
"Meditative",
"Instrumental Pop",
"Instrumental Rock",
"Ethnic",
"Gothic",
"Darkwave",
"Techno-Industrial",
"Electronic",
"Pop-Folk",
"Eurodance",
"Dream",
"Southern Rock",
"Comedy",
"Cult",
"Gangsta",
"Top 40",
"Christian Rap",
"Pop/Funk",
"Jungle",
"Native American",
"Cabaret",
"New Wave",
"Psychadelic",
"Rave",
"Showtunes",
"Trailer",
"Lo-Fi",
"Tribal",
"Acid Punk",
"Acid Jazz",
"Polka",
"Retro",
"Musical",
"Rock & Roll",
"Hard Rock",
"Folk",
"Folk-Rock",
"National Folk",
"Swing",
"Fast Fusion",
"Bebob",
"Latin",
"Revival",
"Celtic",
"Bluegrass",
"Avantgarde",
"Gothic Rock",
"Progressive Rock",
"Psychedelic Rock",
"Symphonic Rock",
"Slow Rock",
"Big Band",
"Chorus",
"Easy Listening",
"Acoustic",
"Humour",
"Speech",
"Chanson",
"Opera",
"Chamber Music",
"Sonata",
"Symphony",
"Booty Bass",
"Primus",
"Porn Groove",
"Satire",
"Slow Jam",
"Club",
"Tango",
"Samba",
"Folklore",
"Ballad",
"Power Ballad",
"Rhythmic Soul",
"Freestyle",
"Duet",
"Punk Rock",
"Drum Solo",
"Acapella",
"Euro-House",
"Dance Hall",
"Goa",
"Drum & Bass",
"Club-House",
"Hardcore",
"Terror",
"Indie",
"BritPop",
"Negerpunk",
"Polsk Punk",
"Beat",
"Christian Gangsta",
"Heavy Metal",
"Black Metal",
"Crossover",
"Contemporary C",
"Christian Rock",
"Merengue",
"Salsa",
"Thrash Metal",
"Anime",
"JPop",
"SynthPop",
NULL
};
static int
can_be_russian (const signed char *str) {
int latin = 0;
int rus = 0;
int rus_in_row = 0;
int max_rus_row = 0;
for (; *str; str++) {
if ((*str >= 'A' && *str <= 'Z')
|| *str >= 'a' && *str <= 'z') {
if (rus_in_row > max_rus_row) {
max_rus_row = rus_in_row;
}
rus_in_row = 0;
latin++;
}
else if (*str < 0) {
rus_in_row++;
rus++;
}
}
if (rus > latin/2 || (max_rus_row > 4)) {
return 1;
}
return 0;
}
static char *
convstr_id3v2 (int version, uint8_t encoding, const unsigned char* str, int sz) {
char out[2048] = "";
const char *enc = NULL;
// detect encoding
if (version == 4 && encoding == 2) {
enc = "UTF-16BE";
}
else if (version == 4 && encoding == 3) {
enc = UTF8_STR;
}
else if (encoding == 0) {
// hack to add limited cp1251 recoding support
if (can_be_russian (str)) {
enc = "cp1251";
}
else {
enc = "iso8859-1";
}
}
else if (encoding != 1 && !(version == 4 && encoding == 3)){
return NULL; // invalid encoding
}
if (encoding == 1) { // detect kind of unicode used
if (sz < 2) {
return NULL;
}
if (version < 4) {
if (str[0] == 0xff && str[1] == 0xfe) {
enc = "UCS-2LE";
str += 2;
sz -= 2;
}
else if (str[1] == 0xff && str[0] == 0xfe) {
enc = "UCS-2BE";
str += 2;
sz -= 2;
}
else {
trace ("invalid ucs-2 signature %x %x\n", (int)str[0], (int)str[1]);
return NULL;
}
}
else {
enc = "UTF-16";
}
}
trace ("encoding: %s\n", enc);
int converted_sz = 0;
if ((converted_sz = junk_iconv (str, sz, out, sizeof (out), enc, UTF8_STR)) < 0) {
return NULL;
}
// trace ("%s -> %s\n", str, out);
int n;
for (n = 0; n < converted_sz; n++) {
if (out[n] == 0 && n != converted_sz-1) {
out[n] = '\n';
}
}
// trim trailing linebreaks
for (n = converted_sz-1; n >= 0; n--) {
if (out[n] == '\n') {
out[n] = 0;
}
else {
break;
}
}
return strdup (out);
}
static const char *
convstr_id3v1 (const char* str, int sz) {
static char out[2048];
int i;
for (i = 0; i < sz; i++) {
if (str[i] != ' ') {
break;
}
}
if (i == sz) {
out[0] = 0;
return out;
}
// check for valid utf8
int valid = u8_valid (str, sz, NULL);
if (valid) {
return str;
}
const char *enc = "iso8859-1";
if (can_be_russian (str)) {
enc = "cp1251";
}
int len = junk_iconv (str, sz, out, sizeof (out), enc, UTF8_STR);
if (len >= 0) {
return out;
}
return NULL;
}
static void
str_trim_right (uint8_t *str, int len) {
uint8_t *p = str + len - 1;
while (p >= str && *p <= 0x20) {
p--;
}
p++;
*p = 0;
}
// should read both id3v1 and id3v1.1
int
junk_id3v1_read (playItem_t *it, DB_FILE *fp) {
if (!it || !fp) {
trace ("bad call to junk_id3v1_read!\n");
return -1;
}
uint8_t buffer[128];
// try reading from end
if (deadbeef->fseek (fp, -128, SEEK_END) == -1) {
return -1;
}
if (deadbeef->fread (buffer, 1, 128, fp) != 128) {
return -1;
}
if (memcmp (buffer, "TAG", 3)) {
return -1; // no tag
}
char title[31];
char artist[31];
char album[31];
char year[5];
char comment[31];
uint8_t genreid;
uint8_t tracknum;
const char *genre = NULL;
memset (title, 0, 31);
memset (artist, 0, 31);
memset (album, 0, 31);
memset (year, 0, 5);
memset (comment, 0, 31);
memcpy (title, &buffer[3], 30);
str_trim_right (title, 30);
memcpy (artist, &buffer[3+30], 30);
str_trim_right (artist, 30);
memcpy (album, &buffer[3+60], 30);
str_trim_right (album, 30);
memcpy (year, &buffer[3+90], 4);
str_trim_right (year, 4);
memcpy (comment, &buffer[3+94], 30);
str_trim_right (comment, 30);
genreid = buffer[3+124];
tracknum = 0;
if (comment[28] == 0 && comment[29] != 0) {
tracknum = comment[29];
}
// 255 = "None",
// "CR" = "Cover" (id3v2)
// "RX" = "Remix" (id3v2)
if (genreid == 0xff) {
//genre = "None";
}
else if (genreid <= 147) {
genre = junk_genretbl[genreid];
}
// add meta
// trace ("%s - %s - %s - %s - %s - %s\n", title, artist, album, year, comment, genre);
if (*title) {
pl_add_meta (it, "title", convstr_id3v1 (title, strlen (title)));
}
if (*artist) {
pl_add_meta (it, "artist", convstr_id3v1 (artist, strlen (artist)));
}
if (*album) {
pl_add_meta (it, "album", convstr_id3v1 (album, strlen (album)));
}
if (*year) {
pl_add_meta (it, "year", year);
}
if (*comment) {
pl_add_meta (it, "comment", convstr_id3v1 (comment, strlen (comment)));
}
if (genre && *genre) {
pl_add_meta (it, "genre", convstr_id3v1 (genre, strlen (genre)));
}
if (tracknum != 0) {
char s[4];
snprintf (s, 4, "%d", tracknum);
pl_add_meta (it, "track", s);
}
uint32_t f = pl_get_item_flags (it);
f |= DDB_TAG_ID3V1;
pl_set_item_flags (it, f);
return 0;
}
int
junk_id3v1_write (FILE *fp, playItem_t *it, const char *enc) {
char title[30] = "";
char artist[30] = "";
char album[30] = "";
char year[4] = "";
char comment[28] = "";
uint8_t genreid = 0xff;
uint8_t tracknum = 0;
const char *meta;
#define conv(name, store) {\
memset (store, 0x20, sizeof (store));\
meta = pl_find_meta (it, name);\
if (meta) {\
char temp[1000];\
int l = junk_iconv (meta, strlen (meta), temp, sizeof (temp), UTF8_STR, enc);\
if (l == -1) {\
memset (store, 0, sizeof (store));\
}\
else {\
strncpy (store, temp, sizeof (store));\
}\
char *cr = strchr (store, '\n');\
if (cr) {\
*cr = 0;\
}\
}\
}
conv ("title", title);
conv ("artist", artist);
conv ("album", album);
conv ("year", year);
conv ("comment", comment);
#undef conv
// tracknum
meta = pl_find_meta (it, "track");
if (meta) {
tracknum = atoi (meta);
}
// find genre
meta = pl_find_meta (it, "genre");
if (meta) {
for (int i = 0; junk_genretbl[i]; i++) {
if (!strcasecmp (meta, junk_genretbl[i])) {
genreid = i;
break;
}
}
}
if (fwrite ("TAG", 1, 3, fp) != 3) {
trace ("junk_id3v1_write: failed to write signature\n");
return -1;
}
if (fwrite (title, 1, sizeof (title), fp) != sizeof (title)) {
trace ("junk_id3v1_write: failed to write title\n");
return -1;
}
if (fwrite (artist, 1, sizeof (artist), fp) != sizeof (artist)) {
trace ("junk_id3v1_write: failed to write artist\n");
return -1;
}
if (fwrite (album, 1, sizeof (album), fp) != sizeof (album)) {
trace ("junk_id3v1_write: failed to write album\n");
return -1;
}
if (fwrite (year, 1, sizeof (year), fp) != sizeof (year)) {
trace ("junk_id3v1_write: failed to write year\n");
return -1;
}
if (fwrite (comment, 1, sizeof (comment), fp) != sizeof (comment)) {
trace ("junk_id3v1_write: failed to write comment\n");
return -1;
}
uint8_t zero = 0;
if (fwrite (&zero, 1, 1, fp) != 1) {
trace ("junk_id3v1_write: failed to write id3v1.1 marker\n");
return -1;
}
if (fwrite (&tracknum, 1, 1, fp) != 1) {
trace ("junk_id3v1_write: failed to write track\n");
return -1;
}
if (fwrite (&genreid, 1, 1, fp) != 1) {
trace ("junk_id3v1_write: failed to write genre\n");
return -1;
}
return 0;
}
int
junk_id3v1_find (DB_FILE *fp) {
uint8_t buffer[3];
if (deadbeef->fseek (fp, -128, SEEK_END) == -1) {
return -1;
}
if (deadbeef->fread (buffer, 1, 3, fp) != 3) {
return -1;
}
if (memcmp (buffer, "TAG", 3)) {
return -1; // no tag
}
return deadbeef->ftell (fp) - 3;
}
int
junk_apev2_find (DB_FILE *fp, int32_t *psize, uint32_t *pflags, uint32_t *pnumitems) {
uint8_t header[32];
if (deadbeef->fseek (fp, -32, SEEK_END) == -1) {
return -1; // something bad happened
}
if (deadbeef->fread (header, 1, 32, fp) != 32) {
return -1; // something bad happened
}
if (strncmp (header, "APETAGEX", 8)) {