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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
|
/* libFLAC - Free Lossless Audio Codec library
* Copyright (C) 2001-2009 Josh Coalson
* Copyright (C) 2011-2016 Xiph.Org Foundation
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* - Neither the name of the Xiph.org Foundation nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <sys/stat.h> /* for stat(), maybe chmod() */
#include "private/metadata.h"
#include "FLAC/assert.h"
#include "FLAC/stream_decoder.h"
#include "share/alloc.h"
#include "share/compat.h"
#include "share/macros.h"
#include "share/safe_str.h"
#include "private/macros.h"
#include "private/memory.h"
/* Alias the first (in share/alloc.h) to the second (in src/libFLAC/memory.c). */
#define safe_malloc_mul_2op_ safe_malloc_mul_2op_p
/****************************************************************************
*
* Local function declarations
*
***************************************************************************/
static void pack_uint32_(FLAC__uint32 val, FLAC__byte *b, unsigned bytes);
static void pack_uint32_little_endian_(FLAC__uint32 val, FLAC__byte *b, unsigned bytes);
static void pack_uint64_(FLAC__uint64 val, FLAC__byte *b, unsigned bytes);
static FLAC__uint32 unpack_uint32_(FLAC__byte *b, unsigned bytes);
static FLAC__uint32 unpack_uint32_little_endian_(FLAC__byte *b, unsigned bytes);
static FLAC__uint64 unpack_uint64_(FLAC__byte *b, unsigned bytes);
static FLAC__bool read_metadata_block_header_(FLAC__Metadata_SimpleIterator *iterator);
static FLAC__bool read_metadata_block_data_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block);
static FLAC__bool read_metadata_block_header_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__bool *is_last, FLAC__MetadataType *type, unsigned *length);
static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Seek seek_cb, FLAC__StreamMetadata *block);
static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_streaminfo_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_StreamInfo *block);
static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_padding_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Seek seek_cb, FLAC__StreamMetadata_Padding *block, unsigned block_length);
static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_application_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_Application *block, unsigned block_length);
static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_seektable_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_SeekTable *block, unsigned block_length);
static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_vorbis_comment_entry_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_VorbisComment_Entry *entry, unsigned max_length);
static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_vorbis_comment_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Seek seek_cb, FLAC__StreamMetadata_VorbisComment *block, unsigned block_length);
static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_cuesheet_track_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_CueSheet_Track *track);
static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_cuesheet_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_CueSheet *block);
static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_picture_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_Picture *block);
static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_unknown_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_Unknown *block, unsigned block_length);
static FLAC__bool write_metadata_block_header_(FILE *file, FLAC__Metadata_SimpleIteratorStatus *status, const FLAC__StreamMetadata *block);
static FLAC__bool write_metadata_block_data_(FILE *file, FLAC__Metadata_SimpleIteratorStatus *status, const FLAC__StreamMetadata *block);
static FLAC__bool write_metadata_block_header_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata *block);
static FLAC__bool write_metadata_block_data_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata *block);
static FLAC__bool write_metadata_block_data_streaminfo_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_StreamInfo *block);
static FLAC__bool write_metadata_block_data_padding_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Padding *block, unsigned block_length);
static FLAC__bool write_metadata_block_data_application_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Application *block, unsigned block_length);
static FLAC__bool write_metadata_block_data_seektable_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_SeekTable *block);
static FLAC__bool write_metadata_block_data_vorbis_comment_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_VorbisComment *block);
static FLAC__bool write_metadata_block_data_cuesheet_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_CueSheet *block);
static FLAC__bool write_metadata_block_data_picture_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Picture *block);
static FLAC__bool write_metadata_block_data_unknown_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Unknown *block, unsigned block_length);
static FLAC__bool write_metadata_block_stationary_(FLAC__Metadata_SimpleIterator *iterator, const FLAC__StreamMetadata *block);
static FLAC__bool write_metadata_block_stationary_with_padding_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, unsigned padding_length, FLAC__bool padding_is_last);
static FLAC__bool rewrite_whole_file_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, FLAC__bool append);
static void simple_iterator_push_(FLAC__Metadata_SimpleIterator *iterator);
static FLAC__bool simple_iterator_pop_(FLAC__Metadata_SimpleIterator *iterator);
static unsigned seek_to_first_metadata_block_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Seek seek_cb);
static unsigned seek_to_first_metadata_block_(FILE *f);
static FLAC__bool simple_iterator_copy_file_prefix_(FLAC__Metadata_SimpleIterator *iterator, FILE **tempfile, char **tempfilename, FLAC__bool append);
static FLAC__bool simple_iterator_copy_file_postfix_(FLAC__Metadata_SimpleIterator *iterator, FILE **tempfile, char **tempfilename, int fixup_is_last_code, FLAC__off_t fixup_is_last_flag_offset, FLAC__bool backup);
static FLAC__bool copy_n_bytes_from_file_(FILE *file, FILE *tempfile, FLAC__off_t bytes, FLAC__Metadata_SimpleIteratorStatus *status);
static FLAC__bool copy_n_bytes_from_file_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOHandle temp_handle, FLAC__IOCallback_Write temp_write_cb, FLAC__off_t bytes, FLAC__Metadata_SimpleIteratorStatus *status);
static FLAC__bool copy_remaining_bytes_from_file_(FILE *file, FILE *tempfile, FLAC__Metadata_SimpleIteratorStatus *status);
static FLAC__bool copy_remaining_bytes_from_file_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Eof eof_cb, FLAC__IOHandle temp_handle, FLAC__IOCallback_Write temp_write_cb, FLAC__Metadata_SimpleIteratorStatus *status);
static FLAC__bool open_tempfile_(const char *filename, const char *tempfile_path_prefix, FILE **tempfile, char **tempfilename, FLAC__Metadata_SimpleIteratorStatus *status);
static FLAC__bool transport_tempfile_(const char *filename, FILE **tempfile, char **tempfilename, FLAC__Metadata_SimpleIteratorStatus *status);
static void cleanup_tempfile_(FILE **tempfile, char **tempfilename);
static FLAC__bool get_file_stats_(const char *filename, struct flac_stat_s *stats);
static void set_file_stats_(const char *filename, struct flac_stat_s *stats);
static int fseek_wrapper_(FLAC__IOHandle handle, FLAC__int64 offset, int whence);
static FLAC__int64 ftell_wrapper_(FLAC__IOHandle handle);
static FLAC__Metadata_ChainStatus get_equivalent_status_(FLAC__Metadata_SimpleIteratorStatus status);
#ifdef FLAC__VALGRIND_TESTING
static size_t local__fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
{
size_t ret = fwrite(ptr, size, nmemb, stream);
if(!ferror(stream))
fflush(stream);
return ret;
}
#else
#define local__fwrite fwrite
#endif
/****************************************************************************
*
* Level 0 implementation
*
***************************************************************************/
static FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data);
static void metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data);
static void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data);
typedef struct {
FLAC__bool got_error;
FLAC__StreamMetadata *object;
} level0_client_data;
static FLAC__StreamMetadata *get_one_metadata_block_(const char *filename, FLAC__MetadataType type)
{
level0_client_data cd;
FLAC__StreamDecoder *decoder;
FLAC__ASSERT(0 != filename);
cd.got_error = false;
cd.object = 0;
decoder = FLAC__stream_decoder_new();
if(0 == decoder)
return 0;
FLAC__stream_decoder_set_md5_checking(decoder, false);
FLAC__stream_decoder_set_metadata_ignore_all(decoder);
FLAC__stream_decoder_set_metadata_respond(decoder, type);
if(FLAC__stream_decoder_init_file(decoder, filename, write_callback_, metadata_callback_, error_callback_, &cd) != FLAC__STREAM_DECODER_INIT_STATUS_OK || cd.got_error) {
(void)FLAC__stream_decoder_finish(decoder);
FLAC__stream_decoder_delete(decoder);
return 0;
}
if(!FLAC__stream_decoder_process_until_end_of_metadata(decoder) || cd.got_error) {
(void)FLAC__stream_decoder_finish(decoder);
FLAC__stream_decoder_delete(decoder);
if(0 != cd.object)
FLAC__metadata_object_delete(cd.object);
return 0;
}
(void)FLAC__stream_decoder_finish(decoder);
FLAC__stream_decoder_delete(decoder);
return cd.object;
}
FLAC_API FLAC__bool FLAC__metadata_get_streaminfo(const char *filename, FLAC__StreamMetadata *streaminfo)
{
FLAC__StreamMetadata *object;
FLAC__ASSERT(0 != filename);
FLAC__ASSERT(0 != streaminfo);
object = get_one_metadata_block_(filename, FLAC__METADATA_TYPE_STREAMINFO);
if (object) {
/* can just copy the contents since STREAMINFO has no internal structure */
*streaminfo = *object;
FLAC__metadata_object_delete(object);
return true;
}
else {
return false;
}
}
FLAC_API FLAC__bool FLAC__metadata_get_tags(const char *filename, FLAC__StreamMetadata **tags)
{
FLAC__ASSERT(0 != filename);
FLAC__ASSERT(0 != tags);
*tags = get_one_metadata_block_(filename, FLAC__METADATA_TYPE_VORBIS_COMMENT);
return 0 != *tags;
}
FLAC_API FLAC__bool FLAC__metadata_get_cuesheet(const char *filename, FLAC__StreamMetadata **cuesheet)
{
FLAC__ASSERT(0 != filename);
FLAC__ASSERT(0 != cuesheet);
*cuesheet = get_one_metadata_block_(filename, FLAC__METADATA_TYPE_CUESHEET);
return 0 != *cuesheet;
}
FLAC__StreamDecoderWriteStatus write_callback_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
{
(void)decoder, (void)frame, (void)buffer, (void)client_data;
return FLAC__STREAM_DECODER_WRITE_STATUS_CONTINUE;
}
void metadata_callback_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
{
level0_client_data *cd = (level0_client_data *)client_data;
(void)decoder;
/*
* we assume we only get here when the one metadata block we were
* looking for was passed to us
*/
if(!cd->got_error && 0 == cd->object) {
if(0 == (cd->object = FLAC__metadata_object_clone(metadata)))
cd->got_error = true;
}
}
void error_callback_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
{
level0_client_data *cd = (level0_client_data *)client_data;
(void)decoder;
if(status != FLAC__STREAM_DECODER_ERROR_STATUS_LOST_SYNC)
cd->got_error = true;
}
FLAC_API FLAC__bool FLAC__metadata_get_picture(const char *filename, FLAC__StreamMetadata **picture, FLAC__StreamMetadata_Picture_Type type, const char *mime_type, const FLAC__byte *description, unsigned max_width, unsigned max_height, unsigned max_depth, unsigned max_colors)
{
FLAC__Metadata_SimpleIterator *it;
FLAC__uint64 max_area_seen = 0;
FLAC__uint64 max_depth_seen = 0;
FLAC__ASSERT(0 != filename);
FLAC__ASSERT(0 != picture);
*picture = 0;
it = FLAC__metadata_simple_iterator_new();
if(0 == it)
return false;
if(!FLAC__metadata_simple_iterator_init(it, filename, /*read_only=*/true, /*preserve_file_stats=*/true)) {
FLAC__metadata_simple_iterator_delete(it);
return false;
}
do {
if(FLAC__metadata_simple_iterator_get_block_type(it) == FLAC__METADATA_TYPE_PICTURE) {
FLAC__StreamMetadata *obj = FLAC__metadata_simple_iterator_get_block(it);
FLAC__uint64 area = (FLAC__uint64)obj->data.picture.width * (FLAC__uint64)obj->data.picture.height;
/* check constraints */
if(
(type == (FLAC__StreamMetadata_Picture_Type)(-1) || type == obj->data.picture.type) &&
(mime_type == 0 || !strcmp(mime_type, obj->data.picture.mime_type)) &&
(description == 0 || !strcmp((const char *)description, (const char *)obj->data.picture.description)) &&
obj->data.picture.width <= max_width &&
obj->data.picture.height <= max_height &&
obj->data.picture.depth <= max_depth &&
obj->data.picture.colors <= max_colors &&
(area > max_area_seen || (area == max_area_seen && obj->data.picture.depth > max_depth_seen))
) {
if(*picture)
FLAC__metadata_object_delete(*picture);
*picture = obj;
max_area_seen = area;
max_depth_seen = obj->data.picture.depth;
}
else {
FLAC__metadata_object_delete(obj);
}
}
} while(FLAC__metadata_simple_iterator_next(it));
FLAC__metadata_simple_iterator_delete(it);
return (0 != *picture);
}
/****************************************************************************
*
* Level 1 implementation
*
***************************************************************************/
#define SIMPLE_ITERATOR_MAX_PUSH_DEPTH (1+4)
/* 1 for initial offset, +4 for our own personal use */
struct FLAC__Metadata_SimpleIterator {
FILE *file;
char *filename, *tempfile_path_prefix;
struct flac_stat_s stats;
FLAC__bool has_stats;
FLAC__bool is_writable;
FLAC__Metadata_SimpleIteratorStatus status;
FLAC__off_t offset[SIMPLE_ITERATOR_MAX_PUSH_DEPTH];
FLAC__off_t first_offset; /* this is the offset to the STREAMINFO block */
unsigned depth;
/* this is the metadata block header of the current block we are pointing to: */
FLAC__bool is_last;
FLAC__MetadataType type;
unsigned length;
};
FLAC_API const char * const FLAC__Metadata_SimpleIteratorStatusString[] = {
"FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK",
"FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT",
"FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE",
"FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_A_FLAC_FILE",
"FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_WRITABLE",
"FLAC__METADATA_SIMPLE_ITERATOR_STATUS_BAD_METADATA",
"FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR",
"FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR",
"FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR",
"FLAC__METADATA_SIMPLE_ITERATOR_STATUS_RENAME_ERROR",
"FLAC__METADATA_SIMPLE_ITERATOR_STATUS_UNLINK_ERROR",
"FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR",
"FLAC__METADATA_SIMPLE_ITERATOR_STATUS_INTERNAL_ERROR"
};
FLAC_API FLAC__Metadata_SimpleIterator *FLAC__metadata_simple_iterator_new(void)
{
FLAC__Metadata_SimpleIterator *iterator = calloc(1, sizeof(FLAC__Metadata_SimpleIterator));
if(0 != iterator) {
iterator->file = 0;
iterator->filename = 0;
iterator->tempfile_path_prefix = 0;
iterator->has_stats = false;
iterator->is_writable = false;
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
iterator->first_offset = iterator->offset[0] = -1;
iterator->depth = 0;
}
return iterator;
}
static void simple_iterator_free_guts_(FLAC__Metadata_SimpleIterator *iterator)
{
FLAC__ASSERT(0 != iterator);
if(0 != iterator->file) {
fclose(iterator->file);
iterator->file = 0;
if(iterator->has_stats)
set_file_stats_(iterator->filename, &iterator->stats);
}
if(0 != iterator->filename) {
free(iterator->filename);
iterator->filename = 0;
}
if(0 != iterator->tempfile_path_prefix) {
free(iterator->tempfile_path_prefix);
iterator->tempfile_path_prefix = 0;
}
}
FLAC_API void FLAC__metadata_simple_iterator_delete(FLAC__Metadata_SimpleIterator *iterator)
{
FLAC__ASSERT(0 != iterator);
simple_iterator_free_guts_(iterator);
free(iterator);
}
FLAC_API FLAC__Metadata_SimpleIteratorStatus FLAC__metadata_simple_iterator_status(FLAC__Metadata_SimpleIterator *iterator)
{
FLAC__Metadata_SimpleIteratorStatus status;
FLAC__ASSERT(0 != iterator);
status = iterator->status;
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
return status;
}
static FLAC__bool simple_iterator_prime_input_(FLAC__Metadata_SimpleIterator *iterator, FLAC__bool read_only)
{
unsigned ret;
FLAC__ASSERT(0 != iterator);
if(read_only || 0 == (iterator->file = flac_fopen(iterator->filename, "r+b"))) {
iterator->is_writable = false;
if(read_only || errno == EACCES) {
if(0 == (iterator->file = flac_fopen(iterator->filename, "rb"))) {
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE;
return false;
}
}
else {
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE;
return false;
}
}
else {
iterator->is_writable = true;
}
ret = seek_to_first_metadata_block_(iterator->file);
switch(ret) {
case 0:
iterator->depth = 0;
iterator->first_offset = iterator->offset[iterator->depth] = ftello(iterator->file);
return read_metadata_block_header_(iterator);
case 1:
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
return false;
case 2:
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
return false;
case 3:
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_A_FLAC_FILE;
return false;
default:
FLAC__ASSERT(0);
return false;
}
}
#if 0
@@@ If we decide to finish implementing this, put this comment back in metadata.h
/*
* The 'tempfile_path_prefix' allows you to specify a directory where
* tempfiles should go. Remember that if your metadata edits cause the
* FLAC file to grow, the entire file will have to be rewritten. If
* 'tempfile_path_prefix' is NULL, the temp file will be written in the
* same directory as the original FLAC file. This makes replacing the
* original with the tempfile fast but requires extra space in the same
* partition for the tempfile. If space is a problem, you can pass a
* directory name belonging to a different partition in
* 'tempfile_path_prefix'. Note that you should use the forward slash
* '/' as the directory separator. A trailing slash is not needed; it
* will be added automatically.
*/
FLAC__bool FLAC__metadata_simple_iterator_init(FLAC__Metadata_SimpleIterator *iterator, const char *filename, FLAC__bool preserve_file_stats, const char *tempfile_path_prefix);
#endif
FLAC_API FLAC__bool FLAC__metadata_simple_iterator_init(FLAC__Metadata_SimpleIterator *iterator, const char *filename, FLAC__bool read_only, FLAC__bool preserve_file_stats)
{
const char *tempfile_path_prefix = 0; /*@@@ search for comments near 'flac_rename(...)' for what it will take to finish implementing this */
FLAC__ASSERT(0 != iterator);
FLAC__ASSERT(0 != filename);
simple_iterator_free_guts_(iterator);
if(!read_only && preserve_file_stats)
iterator->has_stats = get_file_stats_(filename, &iterator->stats);
if(0 == (iterator->filename = strdup(filename))) {
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
return false;
}
if(0 != tempfile_path_prefix && 0 == (iterator->tempfile_path_prefix = strdup(tempfile_path_prefix))) {
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
return false;
}
return simple_iterator_prime_input_(iterator, read_only);
}
FLAC_API FLAC__bool FLAC__metadata_simple_iterator_is_writable(const FLAC__Metadata_SimpleIterator *iterator)
{
FLAC__ASSERT(0 != iterator);
FLAC__ASSERT(0 != iterator->file);
return iterator->is_writable;
}
FLAC_API FLAC__bool FLAC__metadata_simple_iterator_next(FLAC__Metadata_SimpleIterator *iterator)
{
FLAC__ASSERT(0 != iterator);
FLAC__ASSERT(0 != iterator->file);
if(iterator->is_last)
return false;
if(0 != fseeko(iterator->file, iterator->length, SEEK_CUR)) {
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
return false;
}
iterator->offset[iterator->depth] = ftello(iterator->file);
return read_metadata_block_header_(iterator);
}
FLAC_API FLAC__bool FLAC__metadata_simple_iterator_prev(FLAC__Metadata_SimpleIterator *iterator)
{
FLAC__off_t this_offset;
FLAC__ASSERT(0 != iterator);
FLAC__ASSERT(0 != iterator->file);
if(iterator->offset[iterator->depth] == iterator->first_offset)
return false;
if(0 != fseeko(iterator->file, iterator->first_offset, SEEK_SET)) {
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
return false;
}
this_offset = iterator->first_offset;
if(!read_metadata_block_header_(iterator))
return false;
/* we ignore any error from ftello() and catch it in fseeko() */
while(ftello(iterator->file) + (FLAC__off_t)iterator->length < iterator->offset[iterator->depth]) {
if(0 != fseeko(iterator->file, iterator->length, SEEK_CUR)) {
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
return false;
}
this_offset = ftello(iterator->file);
if(!read_metadata_block_header_(iterator))
return false;
}
iterator->offset[iterator->depth] = this_offset;
return true;
}
/*@@@@add to tests*/
FLAC_API FLAC__bool FLAC__metadata_simple_iterator_is_last(const FLAC__Metadata_SimpleIterator *iterator)
{
FLAC__ASSERT(0 != iterator);
FLAC__ASSERT(0 != iterator->file);
return iterator->is_last;
}
/*@@@@add to tests*/
FLAC_API off_t FLAC__metadata_simple_iterator_get_block_offset(const FLAC__Metadata_SimpleIterator *iterator)
{
FLAC__ASSERT(0 != iterator);
FLAC__ASSERT(0 != iterator->file);
return iterator->offset[iterator->depth];
}
FLAC_API FLAC__MetadataType FLAC__metadata_simple_iterator_get_block_type(const FLAC__Metadata_SimpleIterator *iterator)
{
FLAC__ASSERT(0 != iterator);
FLAC__ASSERT(0 != iterator->file);
return iterator->type;
}
/*@@@@add to tests*/
FLAC_API unsigned FLAC__metadata_simple_iterator_get_block_length(const FLAC__Metadata_SimpleIterator *iterator)
{
FLAC__ASSERT(0 != iterator);
FLAC__ASSERT(0 != iterator->file);
return iterator->length;
}
/*@@@@add to tests*/
FLAC_API FLAC__bool FLAC__metadata_simple_iterator_get_application_id(FLAC__Metadata_SimpleIterator *iterator, FLAC__byte *id)
{
const unsigned id_bytes = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8;
FLAC__ASSERT(0 != iterator);
FLAC__ASSERT(0 != iterator->file);
FLAC__ASSERT(0 != id);
if(iterator->type != FLAC__METADATA_TYPE_APPLICATION) {
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT;
return false;
}
if(fread(id, 1, id_bytes, iterator->file) != id_bytes) {
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
return false;
}
/* back up */
if(0 != fseeko(iterator->file, -((int)id_bytes), SEEK_CUR)) {
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
return false;
}
return true;
}
FLAC_API FLAC__StreamMetadata *FLAC__metadata_simple_iterator_get_block(FLAC__Metadata_SimpleIterator *iterator)
{
FLAC__StreamMetadata *block = FLAC__metadata_object_new(iterator->type);
FLAC__ASSERT(0 != iterator);
FLAC__ASSERT(0 != iterator->file);
if(0 != block) {
block->is_last = iterator->is_last;
block->length = iterator->length;
if(!read_metadata_block_data_(iterator, block)) {
FLAC__metadata_object_delete(block);
return 0;
}
/* back up to the beginning of the block data to stay consistent */
if(0 != fseeko(iterator->file, iterator->offset[iterator->depth] + FLAC__STREAM_METADATA_HEADER_LENGTH, SEEK_SET)) {
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
FLAC__metadata_object_delete(block);
return 0;
}
}
else
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
return block;
}
FLAC_API FLAC__bool FLAC__metadata_simple_iterator_set_block(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, FLAC__bool use_padding)
{
FLAC__ASSERT_DECLARATION(FLAC__off_t debug_target_offset = iterator->offset[iterator->depth];)
FLAC__bool ret;
FLAC__ASSERT(0 != iterator);
FLAC__ASSERT(0 != iterator->file);
FLAC__ASSERT(0 != block);
if(!iterator->is_writable) {
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_WRITABLE;
return false;
}
if(iterator->type == FLAC__METADATA_TYPE_STREAMINFO || block->type == FLAC__METADATA_TYPE_STREAMINFO) {
if(iterator->type != block->type) {
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT;
return false;
}
}
block->is_last = iterator->is_last;
if(iterator->length == block->length)
return write_metadata_block_stationary_(iterator, block);
else if(iterator->length > block->length) {
if(use_padding && iterator->length >= FLAC__STREAM_METADATA_HEADER_LENGTH + block->length) {
ret = write_metadata_block_stationary_with_padding_(iterator, block, iterator->length - FLAC__STREAM_METADATA_HEADER_LENGTH - block->length, block->is_last);
FLAC__ASSERT(!ret || iterator->offset[iterator->depth] == debug_target_offset);
FLAC__ASSERT(!ret || ftello(iterator->file) == debug_target_offset + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH);
return ret;
}
else {
ret = rewrite_whole_file_(iterator, block, /*append=*/false);
FLAC__ASSERT(!ret || iterator->offset[iterator->depth] == debug_target_offset);
FLAC__ASSERT(!ret || ftello(iterator->file) == debug_target_offset + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH);
return ret;
}
}
else /* iterator->length < block->length */ {
unsigned padding_leftover = 0;
FLAC__bool padding_is_last = false;
if(use_padding) {
/* first see if we can even use padding */
if(iterator->is_last) {
use_padding = false;
}
else {
const unsigned extra_padding_bytes_required = block->length - iterator->length;
simple_iterator_push_(iterator);
if(!FLAC__metadata_simple_iterator_next(iterator)) {
(void)simple_iterator_pop_(iterator);
return false;
}
if(iterator->type != FLAC__METADATA_TYPE_PADDING) {
use_padding = false;
}
else {
if(FLAC__STREAM_METADATA_HEADER_LENGTH + iterator->length == extra_padding_bytes_required) {
padding_leftover = 0;
block->is_last = iterator->is_last;
}
else if(iterator->length < extra_padding_bytes_required)
use_padding = false;
else {
padding_leftover = FLAC__STREAM_METADATA_HEADER_LENGTH + iterator->length - extra_padding_bytes_required;
padding_is_last = iterator->is_last;
block->is_last = false;
}
}
if(!simple_iterator_pop_(iterator))
return false;
}
}
if(use_padding) {
if(padding_leftover == 0) {
ret = write_metadata_block_stationary_(iterator, block);
FLAC__ASSERT(!ret || iterator->offset[iterator->depth] == debug_target_offset);
FLAC__ASSERT(!ret || ftello(iterator->file) == debug_target_offset + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH);
return ret;
}
else {
FLAC__ASSERT(padding_leftover >= FLAC__STREAM_METADATA_HEADER_LENGTH);
ret = write_metadata_block_stationary_with_padding_(iterator, block, padding_leftover - FLAC__STREAM_METADATA_HEADER_LENGTH, padding_is_last);
FLAC__ASSERT(!ret || iterator->offset[iterator->depth] == debug_target_offset);
FLAC__ASSERT(!ret || ftello(iterator->file) == debug_target_offset + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH);
return ret;
}
}
else {
ret = rewrite_whole_file_(iterator, block, /*append=*/false);
FLAC__ASSERT(!ret || iterator->offset[iterator->depth] == debug_target_offset);
FLAC__ASSERT(!ret || ftello(iterator->file) == debug_target_offset + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH);
return ret;
}
}
}
FLAC_API FLAC__bool FLAC__metadata_simple_iterator_insert_block_after(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, FLAC__bool use_padding)
{
unsigned padding_leftover = 0;
FLAC__bool padding_is_last = false;
FLAC__ASSERT_DECLARATION(FLAC__off_t debug_target_offset = iterator->offset[iterator->depth] + FLAC__STREAM_METADATA_HEADER_LENGTH + iterator->length;)
FLAC__bool ret;
FLAC__ASSERT(0 != iterator);
FLAC__ASSERT(0 != iterator->file);
FLAC__ASSERT(0 != block);
if(!iterator->is_writable) {
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_WRITABLE;
return false;
}
if(block->type == FLAC__METADATA_TYPE_STREAMINFO) {
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT;
return false;
}
block->is_last = iterator->is_last;
if(use_padding) {
/* first see if we can even use padding */
if(iterator->is_last) {
use_padding = false;
}
else {
simple_iterator_push_(iterator);
if(!FLAC__metadata_simple_iterator_next(iterator)) {
(void)simple_iterator_pop_(iterator);
return false;
}
if(iterator->type != FLAC__METADATA_TYPE_PADDING) {
use_padding = false;
}
else {
if(iterator->length == block->length) {
padding_leftover = 0;
block->is_last = iterator->is_last;
}
else if(iterator->length < FLAC__STREAM_METADATA_HEADER_LENGTH + block->length)
use_padding = false;
else {
padding_leftover = iterator->length - block->length;
padding_is_last = iterator->is_last;
block->is_last = false;
}
}
if(!simple_iterator_pop_(iterator))
return false;
}
}
if(use_padding) {
/* move to the next block, which is suitable padding */
if(!FLAC__metadata_simple_iterator_next(iterator))
return false;
if(padding_leftover == 0) {
ret = write_metadata_block_stationary_(iterator, block);
FLAC__ASSERT(iterator->offset[iterator->depth] == debug_target_offset);
FLAC__ASSERT(ftello(iterator->file) == debug_target_offset + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH);
return ret;
}
else {
FLAC__ASSERT(padding_leftover >= FLAC__STREAM_METADATA_HEADER_LENGTH);
ret = write_metadata_block_stationary_with_padding_(iterator, block, padding_leftover - FLAC__STREAM_METADATA_HEADER_LENGTH, padding_is_last);
FLAC__ASSERT(iterator->offset[iterator->depth] == debug_target_offset);
FLAC__ASSERT(ftello(iterator->file) == debug_target_offset + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH);
return ret;
}
}
else {
ret = rewrite_whole_file_(iterator, block, /*append=*/true);
FLAC__ASSERT(iterator->offset[iterator->depth] == debug_target_offset);
FLAC__ASSERT(ftello(iterator->file) == debug_target_offset + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH);
return ret;
}
}
FLAC_API FLAC__bool FLAC__metadata_simple_iterator_delete_block(FLAC__Metadata_SimpleIterator *iterator, FLAC__bool use_padding)
{
FLAC__ASSERT_DECLARATION(FLAC__off_t debug_target_offset = iterator->offset[iterator->depth];)
FLAC__bool ret;
if(!iterator->is_writable) {
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_WRITABLE;
return false;
}
if(iterator->type == FLAC__METADATA_TYPE_STREAMINFO) {
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT;
return false;
}
if(use_padding) {
FLAC__StreamMetadata *padding = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING);
if(0 == padding) {
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
return false;
}
padding->length = iterator->length;
if(!FLAC__metadata_simple_iterator_set_block(iterator, padding, false)) {
FLAC__metadata_object_delete(padding);
return false;
}
FLAC__metadata_object_delete(padding);
if(!FLAC__metadata_simple_iterator_prev(iterator))
return false;
FLAC__ASSERT(iterator->offset[iterator->depth] + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH + (FLAC__off_t)iterator->length == debug_target_offset);
FLAC__ASSERT(ftello(iterator->file) + (FLAC__off_t)iterator->length == debug_target_offset);
return true;
}
else {
ret = rewrite_whole_file_(iterator, 0, /*append=*/false);
FLAC__ASSERT(iterator->offset[iterator->depth] + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH + (FLAC__off_t)iterator->length == debug_target_offset);
FLAC__ASSERT(ftello(iterator->file) + (FLAC__off_t)iterator->length == debug_target_offset);
return ret;
}
}
/****************************************************************************
*
* Level 2 implementation
*
***************************************************************************/
typedef struct FLAC__Metadata_Node {
FLAC__StreamMetadata *data;
struct FLAC__Metadata_Node *prev, *next;
} FLAC__Metadata_Node;
struct FLAC__Metadata_Chain {
char *filename; /* will be NULL if using callbacks */
FLAC__bool is_ogg;
FLAC__Metadata_Node *head;
FLAC__Metadata_Node *tail;
unsigned nodes;
FLAC__Metadata_ChainStatus status;
FLAC__off_t first_offset, last_offset;
/*
* This is the length of the chain initially read from the FLAC file.
* it is used to compare against the current length to decide whether
* or not the whole file has to be rewritten.
*/
FLAC__off_t initial_length;
/* @@@ hacky, these are currently only needed by ogg reader */
FLAC__IOHandle handle;
FLAC__IOCallback_Read read_cb;
};
struct FLAC__Metadata_Iterator {
FLAC__Metadata_Chain *chain;
FLAC__Metadata_Node *current;
};
FLAC_API const char * const FLAC__Metadata_ChainStatusString[] = {
"FLAC__METADATA_CHAIN_STATUS_OK",
"FLAC__METADATA_CHAIN_STATUS_ILLEGAL_INPUT",
"FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE",
"FLAC__METADATA_CHAIN_STATUS_NOT_A_FLAC_FILE",
"FLAC__METADATA_CHAIN_STATUS_NOT_WRITABLE",
"FLAC__METADATA_CHAIN_STATUS_BAD_METADATA",
"FLAC__METADATA_CHAIN_STATUS_READ_ERROR",
"FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR",
"FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR",
"FLAC__METADATA_CHAIN_STATUS_RENAME_ERROR",
"FLAC__METADATA_CHAIN_STATUS_UNLINK_ERROR",
"FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR",
"FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR",
"FLAC__METADATA_CHAIN_STATUS_INVALID_CALLBACKS",
"FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH",
"FLAC__METADATA_CHAIN_STATUS_WRONG_WRITE_CALL"
};
static FLAC__Metadata_Node *node_new_(void)
{
return calloc(1, sizeof(FLAC__Metadata_Node));
}
static void node_delete_(FLAC__Metadata_Node *node)
{
FLAC__ASSERT(0 != node);
if(0 != node->data)
FLAC__metadata_object_delete(node->data);
free(node);
}
static void chain_init_(FLAC__Metadata_Chain *chain)
{
FLAC__ASSERT(0 != chain);
chain->filename = 0;
chain->is_ogg = false;
chain->head = chain->tail = 0;
chain->nodes = 0;
chain->status = FLAC__METADATA_CHAIN_STATUS_OK;
chain->initial_length = 0;
chain->read_cb = 0;
}
static void chain_clear_(FLAC__Metadata_Chain *chain)
{
FLAC__Metadata_Node *node, *next;
FLAC__ASSERT(0 != chain);
for(node = chain->head; node; ) {
next = node->next;
node_delete_(node);
node = next;
}
if(0 != chain->filename)
free(chain->filename);
chain_init_(chain);
}
static void chain_append_node_(FLAC__Metadata_Chain *chain, FLAC__Metadata_Node *node)
{
FLAC__ASSERT(0 != chain);
FLAC__ASSERT(0 != node);
FLAC__ASSERT(0 != node->data);
node->next = node->prev = 0;
node->data->is_last = true;
if(0 != chain->tail)
chain->tail->data->is_last = false;
if(0 == chain->head)
chain->head = node;
else {
FLAC__ASSERT(0 != chain->tail);
chain->tail->next = node;
node->prev = chain->tail;
}
chain->tail = node;
chain->nodes++;
}
static void chain_remove_node_(FLAC__Metadata_Chain *chain, FLAC__Metadata_Node *node)
{
FLAC__ASSERT(0 != chain);
FLAC__ASSERT(0 != node);
if(node == chain->head)
chain->head = node->next;
else
node->prev->next = node->next;
if(node == chain->tail)
chain->tail = node->prev;
else
node->next->prev = node->prev;
if(0 != chain->tail)
chain->tail->data->is_last = true;
chain->nodes--;
}
static void chain_delete_node_(FLAC__Metadata_Chain *chain, FLAC__Metadata_Node *node)
{
chain_remove_node_(chain, node);
node_delete_(node);
}
static FLAC__off_t chain_calculate_length_(FLAC__Metadata_Chain *chain)
{
const FLAC__Metadata_Node *node;
FLAC__off_t length = 0;
for(node = chain->head; node; node = node->next)
length += (FLAC__STREAM_METADATA_HEADER_LENGTH + node->data->length);
return length;
}
static void iterator_insert_node_(FLAC__Metadata_Iterator *iterator, FLAC__Metadata_Node *node)
{
FLAC__ASSERT(0 != node);
FLAC__ASSERT(0 != node->data);
FLAC__ASSERT(0 != iterator);
FLAC__ASSERT(0 != iterator->current);
FLAC__ASSERT(0 != iterator->chain);
FLAC__ASSERT(0 != iterator->chain->head);
FLAC__ASSERT(0 != iterator->chain->tail);
node->data->is_last = false;
node->prev = iterator->current->prev;
node->next = iterator->current;
if(0 == node->prev)
iterator->chain->head = node;
else
node->prev->next = node;
iterator->current->prev = node;
iterator->chain->nodes++;
}
static void iterator_insert_node_after_(FLAC__Metadata_Iterator *iterator, FLAC__Metadata_Node *node)
{
FLAC__ASSERT(0 != node);
FLAC__ASSERT(0 != node->data);
FLAC__ASSERT(0 != iterator);
FLAC__ASSERT(0 != iterator->current);
FLAC__ASSERT(0 != iterator->chain);
FLAC__ASSERT(0 != iterator->chain->head);
FLAC__ASSERT(0 != iterator->chain->tail);
iterator->current->data->is_last = false;
node->prev = iterator->current;
node->next = iterator->current->next;
if(0 == node->next)
iterator->chain->tail = node;
else
node->next->prev = node;
node->prev->next = node;
iterator->chain->tail->data->is_last = true;
iterator->chain->nodes++;
}
/* return true iff node and node->next are both padding */
static FLAC__bool chain_merge_adjacent_padding_(FLAC__Metadata_Chain *chain, FLAC__Metadata_Node *node)
{
if(node->data->type == FLAC__METADATA_TYPE_PADDING && 0 != node->next && node->next->data->type == FLAC__METADATA_TYPE_PADDING) {
const unsigned growth = FLAC__STREAM_METADATA_HEADER_LENGTH + node->next->data->length;
node->data->length += growth; /* new block size can be greater than max metadata block size, but it'll be fixed later in chain_prepare_for_write_() */
chain_delete_node_(chain, node->next);
return true;
}
else
return false;
}
/* Returns the new length of the chain, or 0 if there was an error. */
/* WATCHOUT: This can get called multiple times before a write, so
* it should still work when this happens.
*/
/* WATCHOUT: Make sure to also update the logic in
* FLAC__metadata_chain_check_if_tempfile_needed() if the logic here changes.
*/
static FLAC__off_t chain_prepare_for_write_(FLAC__Metadata_Chain *chain, FLAC__bool use_padding)
{
FLAC__off_t current_length = chain_calculate_length_(chain);
if(use_padding) {
/* if the metadata shrank and the last block is padding, we just extend the last padding block */
if(current_length < chain->initial_length && chain->tail->data->type == FLAC__METADATA_TYPE_PADDING) {
const FLAC__off_t delta = chain->initial_length - current_length;
chain->tail->data->length += delta;
current_length += delta;
FLAC__ASSERT(current_length == chain->initial_length);
}
/* if the metadata shrank more than 4 bytes then there's room to add another padding block */
else if(current_length + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH <= chain->initial_length) {
FLAC__StreamMetadata *padding;
FLAC__Metadata_Node *node;
if(0 == (padding = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING))) {
chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
return 0;
}
padding->length = chain->initial_length - (FLAC__STREAM_METADATA_HEADER_LENGTH + current_length);
if(0 == (node = node_new_())) {
FLAC__metadata_object_delete(padding);
chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
return 0;
}
node->data = padding;
chain_append_node_(chain, node);
current_length = chain_calculate_length_(chain);
FLAC__ASSERT(current_length == chain->initial_length);
}
/* if the metadata grew but the last block is padding, try cutting the padding to restore the original length so we don't have to rewrite the whole file */
else if(current_length > chain->initial_length) {
const FLAC__off_t delta = current_length - chain->initial_length;
if(chain->tail->data->type == FLAC__METADATA_TYPE_PADDING) {
/* if the delta is exactly the size of the last padding block, remove the padding block */
if((FLAC__off_t)chain->tail->data->length + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH == delta) {
chain_delete_node_(chain, chain->tail);
current_length = chain_calculate_length_(chain);
FLAC__ASSERT(current_length == chain->initial_length);
}
/* if there is at least 'delta' bytes of padding, trim the padding down */
else if((FLAC__off_t)chain->tail->data->length >= delta) {
chain->tail->data->length -= delta;
current_length -= delta;
FLAC__ASSERT(current_length == chain->initial_length);
}
}
}
}
/* check sizes of all metadata blocks; reduce padding size if necessary */
{
FLAC__Metadata_Node *node;
for (node = chain->head; node; node = node->next) {
if(node->data->length >= (1u << FLAC__STREAM_METADATA_LENGTH_LEN)) {
if(node->data->type == FLAC__METADATA_TYPE_PADDING) {
node->data->length = (1u << FLAC__STREAM_METADATA_LENGTH_LEN) - 1;
current_length = chain_calculate_length_(chain);
} else {
chain->status = FLAC__METADATA_CHAIN_STATUS_BAD_METADATA;
return 0;
}
}
}
}
return current_length;
}
static FLAC__bool chain_read_cb_(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Seek seek_cb, FLAC__IOCallback_Tell tell_cb)
{
FLAC__Metadata_Node *node;
FLAC__ASSERT(0 != chain);
/* we assume we're already at the beginning of the file */
switch(seek_to_first_metadata_block_cb_(handle, read_cb, seek_cb)) {
case 0:
break;
case 1:
chain->status = FLAC__METADATA_CHAIN_STATUS_READ_ERROR;
return false;
case 2:
chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR;
return false;
case 3:
chain->status = FLAC__METADATA_CHAIN_STATUS_NOT_A_FLAC_FILE;
return false;
default:
FLAC__ASSERT(0);
return false;
}
{
FLAC__int64 pos = tell_cb(handle);
if(pos < 0) {
chain->status = FLAC__METADATA_CHAIN_STATUS_READ_ERROR;
return false;
}
chain->first_offset = (FLAC__off_t)pos;
}
{
FLAC__bool is_last;
FLAC__MetadataType type;
unsigned length;
do {
node = node_new_();
if(0 == node) {
chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
return false;
}
if(!read_metadata_block_header_cb_(handle, read_cb, &is_last, &type, &length)) {
node_delete_(node);
chain->status = FLAC__METADATA_CHAIN_STATUS_READ_ERROR;
return false;
}
node->data = FLAC__metadata_object_new(type);
if(0 == node->data) {
node_delete_(node);
chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
return false;
}
node->data->is_last = is_last;
node->data->length = length;
chain->status = get_equivalent_status_(read_metadata_block_data_cb_(handle, read_cb, seek_cb, node->data));
if(chain->status != FLAC__METADATA_CHAIN_STATUS_OK) {
node_delete_(node);
return false;
}
chain_append_node_(chain, node);
} while(!is_last);
}
{
FLAC__int64 pos = tell_cb(handle);
if(pos < 0) {
chain->status = FLAC__METADATA_CHAIN_STATUS_READ_ERROR;
return false;
}
chain->last_offset = (FLAC__off_t)pos;
}
chain->initial_length = chain_calculate_length_(chain);
return true;
}
static FLAC__StreamDecoderReadStatus chain_read_ogg_read_cb_(const FLAC__StreamDecoder *decoder, FLAC__byte buffer[], size_t *bytes, void *client_data)
{
FLAC__Metadata_Chain *chain = (FLAC__Metadata_Chain*)client_data;
(void)decoder;
if(*bytes > 0 && chain->status == FLAC__METADATA_CHAIN_STATUS_OK) {
*bytes = chain->read_cb(buffer, sizeof(FLAC__byte), *bytes, chain->handle);
if(*bytes == 0)
return FLAC__STREAM_DECODER_READ_STATUS_END_OF_STREAM;
else
return FLAC__STREAM_DECODER_READ_STATUS_CONTINUE;
}
else
return FLAC__STREAM_DECODER_READ_STATUS_ABORT;
}
static FLAC__StreamDecoderWriteStatus chain_read_ogg_write_cb_(const FLAC__StreamDecoder *decoder, const FLAC__Frame *frame, const FLAC__int32 * const buffer[], void *client_data)
{
(void)decoder, (void)frame, (void)buffer, (void)client_data;
return FLAC__STREAM_DECODER_WRITE_STATUS_ABORT;
}
static void chain_read_ogg_metadata_cb_(const FLAC__StreamDecoder *decoder, const FLAC__StreamMetadata *metadata, void *client_data)
{
FLAC__Metadata_Chain *chain = (FLAC__Metadata_Chain*)client_data;
FLAC__Metadata_Node *node;
(void)decoder;
node = node_new_();
if(0 == node) {
chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
return;
}
node->data = FLAC__metadata_object_clone(metadata);
if(0 == node->data) {
node_delete_(node);
chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
return;
}
chain_append_node_(chain, node);
}
static void chain_read_ogg_error_cb_(const FLAC__StreamDecoder *decoder, FLAC__StreamDecoderErrorStatus status, void *client_data)
{
FLAC__Metadata_Chain *chain = (FLAC__Metadata_Chain*)client_data;
(void)decoder, (void)status;
chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR; /*@@@ maybe needs better error code */
}
static FLAC__bool chain_read_ogg_cb_(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb)
{
FLAC__StreamDecoder *decoder;
FLAC__ASSERT(0 != chain);
/* we assume we're already at the beginning of the file */
chain->handle = handle;
chain->read_cb = read_cb;
if(0 == (decoder = FLAC__stream_decoder_new())) {
chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
return false;
}
FLAC__stream_decoder_set_metadata_respond_all(decoder);
if(FLAC__stream_decoder_init_ogg_stream(decoder, chain_read_ogg_read_cb_, /*seek_callback=*/0, /*tell_callback=*/0, /*length_callback=*/0, /*eof_callback=*/0, chain_read_ogg_write_cb_, chain_read_ogg_metadata_cb_, chain_read_ogg_error_cb_, chain) != FLAC__STREAM_DECODER_INIT_STATUS_OK) {
FLAC__stream_decoder_delete(decoder);
chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR; /*@@@ maybe needs better error code */
return false;
}
chain->first_offset = 0; /*@@@ wrong; will need to be set correctly to implement metadata writing for Ogg FLAC */
if(!FLAC__stream_decoder_process_until_end_of_metadata(decoder))
chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR; /*@@@ maybe needs better error code */
if(chain->status != FLAC__METADATA_CHAIN_STATUS_OK) {
FLAC__stream_decoder_delete(decoder);
return false;
}
FLAC__stream_decoder_delete(decoder);
chain->last_offset = 0; /*@@@ wrong; will need to be set correctly to implement metadata writing for Ogg FLAC */
chain->initial_length = chain_calculate_length_(chain);
return true;
}
static FLAC__bool chain_rewrite_metadata_in_place_cb_(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, FLAC__IOCallback_Seek seek_cb)
{
FLAC__Metadata_Node *node;
FLAC__ASSERT(0 != chain);
FLAC__ASSERT(0 != chain->head);
if(0 != seek_cb(handle, chain->first_offset, SEEK_SET)) {
chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR;
return false;
}
for(node = chain->head; node; node = node->next) {
if(!write_metadata_block_header_cb_(handle, write_cb, node->data)) {
chain->status = FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR;
return false;
}
if(!write_metadata_block_data_cb_(handle, write_cb, node->data)) {
chain->status = FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR;
return false;
}
}
/*FLAC__ASSERT(fflush(), ftello() == chain->last_offset);*/
chain->status = FLAC__METADATA_CHAIN_STATUS_OK;
return true;
}
static FLAC__bool chain_rewrite_metadata_in_place_(FLAC__Metadata_Chain *chain)
{
FILE *file;
FLAC__bool ret;
FLAC__ASSERT(0 != chain->filename);
if(0 == (file = flac_fopen(chain->filename, "r+b"))) {
chain->status = FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE;
return false;
}
/* chain_rewrite_metadata_in_place_cb_() sets chain->status for us */
ret = chain_rewrite_metadata_in_place_cb_(chain, (FLAC__IOHandle)file, (FLAC__IOCallback_Write)fwrite, fseek_wrapper_);
fclose(file);
return ret;
}
static FLAC__bool chain_rewrite_file_(FLAC__Metadata_Chain *chain, const char *tempfile_path_prefix)
{
FILE *f, *tempfile = NULL;
char *tempfilename;
FLAC__Metadata_SimpleIteratorStatus status;
const FLAC__Metadata_Node *node;
FLAC__ASSERT(0 != chain);
FLAC__ASSERT(0 != chain->filename);
FLAC__ASSERT(0 != chain->head);
/* copy the file prefix (data up to first metadata block */
if(0 == (f = flac_fopen(chain->filename, "rb"))) {
chain->status = FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE;
return false;
}
if(!open_tempfile_(chain->filename, tempfile_path_prefix, &tempfile, &tempfilename, &status)) {
chain->status = get_equivalent_status_(status);
goto err;
}
if(!copy_n_bytes_from_file_(f, tempfile, chain->first_offset, &status)) {
chain->status = get_equivalent_status_(status);
goto err;
}
/* write the metadata */
for(node = chain->head; node; node = node->next) {
if(!write_metadata_block_header_(tempfile, &status, node->data)) {
chain->status = get_equivalent_status_(status);
goto err;
}
if(!write_metadata_block_data_(tempfile, &status, node->data)) {
chain->status = get_equivalent_status_(status);
goto err;
}
}
/*FLAC__ASSERT(fflush(), ftello() == chain->last_offset);*/
/* copy the file postfix (everything after the metadata) */
if(0 != fseeko(f, chain->last_offset, SEEK_SET)) {
chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR;
goto err;
}
if(!copy_remaining_bytes_from_file_(f, tempfile, &status)) {
chain->status = get_equivalent_status_(status);
goto err;
}
/* move the tempfile on top of the original */
(void)fclose(f);
if(!transport_tempfile_(chain->filename, &tempfile, &tempfilename, &status))
return false;
return true;
err:
(void)fclose(f);
cleanup_tempfile_(&tempfile, &tempfilename);
return false;
}
/* assumes 'handle' is already at beginning of file */
static FLAC__bool chain_rewrite_file_cb_(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Seek seek_cb, FLAC__IOCallback_Eof eof_cb, FLAC__IOHandle temp_handle, FLAC__IOCallback_Write temp_write_cb)
{
FLAC__Metadata_SimpleIteratorStatus status;
const FLAC__Metadata_Node *node;
FLAC__ASSERT(0 != chain);
FLAC__ASSERT(0 == chain->filename);
FLAC__ASSERT(0 != chain->head);
/* copy the file prefix (data up to first metadata block */
if(!copy_n_bytes_from_file_cb_(handle, read_cb, temp_handle, temp_write_cb, chain->first_offset, &status)) {
chain->status = get_equivalent_status_(status);
return false;
}
/* write the metadata */
for(node = chain->head; node; node = node->next) {
if(!write_metadata_block_header_cb_(temp_handle, temp_write_cb, node->data)) {
chain->status = FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR;
return false;
}
if(!write_metadata_block_data_cb_(temp_handle, temp_write_cb, node->data)) {
chain->status = FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR;
return false;
}
}
/*FLAC__ASSERT(fflush(), ftello() == chain->last_offset);*/
/* copy the file postfix (everything after the metadata) */
if(0 != seek_cb(handle, chain->last_offset, SEEK_SET)) {
chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR;
return false;
}
if(!copy_remaining_bytes_from_file_cb_(handle, read_cb, eof_cb, temp_handle, temp_write_cb, &status)) {
chain->status = get_equivalent_status_(status);
return false;
}
return true;
}
FLAC_API FLAC__Metadata_Chain *FLAC__metadata_chain_new(void)
{
FLAC__Metadata_Chain *chain = calloc(1, sizeof(FLAC__Metadata_Chain));
if(0 != chain)
chain_init_(chain);
return chain;
}
FLAC_API void FLAC__metadata_chain_delete(FLAC__Metadata_Chain *chain)
{
FLAC__ASSERT(0 != chain);
chain_clear_(chain);
free(chain);
}
FLAC_API FLAC__Metadata_ChainStatus FLAC__metadata_chain_status(FLAC__Metadata_Chain *chain)
{
FLAC__Metadata_ChainStatus status;
FLAC__ASSERT(0 != chain);
status = chain->status;
chain->status = FLAC__METADATA_CHAIN_STATUS_OK;
return status;
}
static FLAC__bool chain_read_(FLAC__Metadata_Chain *chain, const char *filename, FLAC__bool is_ogg)
{
FILE *file;
FLAC__bool ret;
FLAC__ASSERT(0 != chain);
FLAC__ASSERT(0 != filename);
chain_clear_(chain);
if(0 == (chain->filename = strdup(filename))) {
chain->status = FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
return false;
}
chain->is_ogg = is_ogg;
if(0 == (file = flac_fopen(filename, "rb"))) {
chain->status = FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE;
return false;
}
/* the function also sets chain->status for us */
ret = is_ogg?
chain_read_ogg_cb_(chain, file, (FLAC__IOCallback_Read)fread) :
chain_read_cb_(chain, file, (FLAC__IOCallback_Read)fread, fseek_wrapper_, ftell_wrapper_)
;
fclose(file);
return ret;
}
FLAC_API FLAC__bool FLAC__metadata_chain_read(FLAC__Metadata_Chain *chain, const char *filename)
{
return chain_read_(chain, filename, /*is_ogg=*/false);
}
/*@@@@add to tests*/
FLAC_API FLAC__bool FLAC__metadata_chain_read_ogg(FLAC__Metadata_Chain *chain, const char *filename)
{
return chain_read_(chain, filename, /*is_ogg=*/true);
}
static FLAC__bool chain_read_with_callbacks_(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks, FLAC__bool is_ogg)
{
FLAC__bool ret;
FLAC__ASSERT(0 != chain);
chain_clear_(chain);
if (0 == callbacks.read || 0 == callbacks.seek || 0 == callbacks.tell) {
chain->status = FLAC__METADATA_CHAIN_STATUS_INVALID_CALLBACKS;
return false;
}
chain->is_ogg = is_ogg;
/* rewind */
if(0 != callbacks.seek(handle, 0, SEEK_SET)) {
chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR;
return false;
}
/* the function also sets chain->status for us */
ret = is_ogg?
chain_read_ogg_cb_(chain, handle, callbacks.read) :
chain_read_cb_(chain, handle, callbacks.read, callbacks.seek, callbacks.tell)
;
return ret;
}
FLAC_API FLAC__bool FLAC__metadata_chain_read_with_callbacks(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks)
{
return chain_read_with_callbacks_(chain, handle, callbacks, /*is_ogg=*/false);
}
/*@@@@add to tests*/
FLAC_API FLAC__bool FLAC__metadata_chain_read_ogg_with_callbacks(FLAC__Metadata_Chain *chain, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks)
{
return chain_read_with_callbacks_(chain, handle, callbacks, /*is_ogg=*/true);
}
typedef enum {
LBS_NONE = 0,
LBS_SIZE_CHANGED,
LBS_BLOCK_ADDED,
LBS_BLOCK_REMOVED
} LastBlockState;
FLAC_API FLAC__bool FLAC__metadata_chain_check_if_tempfile_needed(FLAC__Metadata_Chain *chain, FLAC__bool use_padding)
{
/* This does all the same checks that are in chain_prepare_for_write_()
* but doesn't actually alter the chain. Make sure to update the logic
* here if chain_prepare_for_write_() changes.
*/
FLAC__off_t current_length;
LastBlockState lbs_state = LBS_NONE;
unsigned lbs_size = 0;
FLAC__ASSERT(0 != chain);
current_length = chain_calculate_length_(chain);
if(use_padding) {
const FLAC__Metadata_Node * const node = chain->tail;
/* if the metadata shrank and the last block is padding, we just extend the last padding block */
if(current_length < chain->initial_length && node->data->type == FLAC__METADATA_TYPE_PADDING) {
lbs_state = LBS_SIZE_CHANGED;
lbs_size = node->data->length + (chain->initial_length - current_length);
}
/* if the metadata shrank more than 4 bytes then there's room to add another padding block */
else if(current_length + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH <= chain->initial_length) {
lbs_state = LBS_BLOCK_ADDED;
lbs_size = chain->initial_length - (current_length + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH);
}
/* if the metadata grew but the last block is padding, try cutting the padding to restore the original length so we don't have to rewrite the whole file */
else if(current_length > chain->initial_length) {
const FLAC__off_t delta = current_length - chain->initial_length;
if(node->data->type == FLAC__METADATA_TYPE_PADDING) {
/* if the delta is exactly the size of the last padding block, remove the padding block */
if((FLAC__off_t)node->data->length + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH == delta) {
lbs_state = LBS_BLOCK_REMOVED;
lbs_size = 0;
}
/* if there is at least 'delta' bytes of padding, trim the padding down */
else if((FLAC__off_t)node->data->length >= delta) {
lbs_state = LBS_SIZE_CHANGED;
lbs_size = node->data->length - delta;
}
}
}
}
current_length = 0;
/* check sizes of all metadata blocks; reduce padding size if necessary */
{
const FLAC__Metadata_Node *node;
for(node = chain->head; node; node = node->next) {
unsigned block_len = node->data->length;
if(node == chain->tail) {
if(lbs_state == LBS_BLOCK_REMOVED)
continue;
else if(lbs_state == LBS_SIZE_CHANGED)
block_len = lbs_size;
}
if(block_len >= (1u << FLAC__STREAM_METADATA_LENGTH_LEN)) {
if(node->data->type == FLAC__METADATA_TYPE_PADDING)
block_len = (1u << FLAC__STREAM_METADATA_LENGTH_LEN) - 1;
else
return false /* the return value doesn't matter */;
}
current_length += (FLAC__STREAM_METADATA_HEADER_LENGTH + block_len);
}
if(lbs_state == LBS_BLOCK_ADDED) {
/* test added padding block */
unsigned block_len = lbs_size;
if(block_len >= (1u << FLAC__STREAM_METADATA_LENGTH_LEN))
block_len = (1u << FLAC__STREAM_METADATA_LENGTH_LEN) - 1;
current_length += (FLAC__STREAM_METADATA_HEADER_LENGTH + block_len);
}
}
return (current_length != chain->initial_length);
}
FLAC_API FLAC__bool FLAC__metadata_chain_write(FLAC__Metadata_Chain *chain, FLAC__bool use_padding, FLAC__bool preserve_file_stats)
{
struct flac_stat_s stats;
const char *tempfile_path_prefix = 0;
FLAC__off_t current_length;
FLAC__ASSERT(0 != chain);
if (chain->is_ogg) { /* cannot write back to Ogg FLAC yet */
chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR;
return false;
}
if (0 == chain->filename) {
chain->status = FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH;
return false;
}
current_length = chain_prepare_for_write_(chain, use_padding);
/* a return value of 0 means there was an error; chain->status is already set */
if (0 == current_length)
return false;
if(preserve_file_stats)
get_file_stats_(chain->filename, &stats);
if(current_length == chain->initial_length) {
if(!chain_rewrite_metadata_in_place_(chain))
return false;
}
else {
if(!chain_rewrite_file_(chain, tempfile_path_prefix))
return false;
/* recompute lengths and offsets */
{
const FLAC__Metadata_Node *node;
chain->initial_length = current_length;
chain->last_offset = chain->first_offset;
for(node = chain->head; node; node = node->next)
chain->last_offset += (FLAC__STREAM_METADATA_HEADER_LENGTH + node->data->length);
}
}
if(preserve_file_stats)
set_file_stats_(chain->filename, &stats);
return true;
}
FLAC_API FLAC__bool FLAC__metadata_chain_write_with_callbacks(FLAC__Metadata_Chain *chain, FLAC__bool use_padding, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks)
{
FLAC__off_t current_length;
FLAC__ASSERT(0 != chain);
if (chain->is_ogg) { /* cannot write back to Ogg FLAC yet */
chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR;
return false;
}
if (0 != chain->filename) {
chain->status = FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH;
return false;
}
if (0 == callbacks.write || 0 == callbacks.seek) {
chain->status = FLAC__METADATA_CHAIN_STATUS_INVALID_CALLBACKS;
return false;
}
if (FLAC__metadata_chain_check_if_tempfile_needed(chain, use_padding)) {
chain->status = FLAC__METADATA_CHAIN_STATUS_WRONG_WRITE_CALL;
return false;
}
current_length = chain_prepare_for_write_(chain, use_padding);
/* a return value of 0 means there was an error; chain->status is already set */
if (0 == current_length)
return false;
FLAC__ASSERT(current_length == chain->initial_length);
return chain_rewrite_metadata_in_place_cb_(chain, handle, callbacks.write, callbacks.seek);
}
FLAC_API FLAC__bool FLAC__metadata_chain_write_with_callbacks_and_tempfile(FLAC__Metadata_Chain *chain, FLAC__bool use_padding, FLAC__IOHandle handle, FLAC__IOCallbacks callbacks, FLAC__IOHandle temp_handle, FLAC__IOCallbacks temp_callbacks)
{
FLAC__off_t current_length;
FLAC__ASSERT(0 != chain);
if (chain->is_ogg) { /* cannot write back to Ogg FLAC yet */
chain->status = FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR;
return false;
}
if (0 != chain->filename) {
chain->status = FLAC__METADATA_CHAIN_STATUS_READ_WRITE_MISMATCH;
return false;
}
if (0 == callbacks.read || 0 == callbacks.seek || 0 == callbacks.eof) {
chain->status = FLAC__METADATA_CHAIN_STATUS_INVALID_CALLBACKS;
return false;
}
if (0 == temp_callbacks.write) {
chain->status = FLAC__METADATA_CHAIN_STATUS_INVALID_CALLBACKS;
return false;
}
if (!FLAC__metadata_chain_check_if_tempfile_needed(chain, use_padding)) {
chain->status = FLAC__METADATA_CHAIN_STATUS_WRONG_WRITE_CALL;
return false;
}
current_length = chain_prepare_for_write_(chain, use_padding);
/* a return value of 0 means there was an error; chain->status is already set */
if (0 == current_length)
return false;
FLAC__ASSERT(current_length != chain->initial_length);
/* rewind */
if(0 != callbacks.seek(handle, 0, SEEK_SET)) {
chain->status = FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR;
return false;
}
if(!chain_rewrite_file_cb_(chain, handle, callbacks.read, callbacks.seek, callbacks.eof, temp_handle, temp_callbacks.write))
return false;
/* recompute lengths and offsets */
{
const FLAC__Metadata_Node *node;
chain->initial_length = current_length;
chain->last_offset = chain->first_offset;
for(node = chain->head; node; node = node->next)
chain->last_offset += (FLAC__STREAM_METADATA_HEADER_LENGTH + node->data->length);
}
return true;
}
FLAC_API void FLAC__metadata_chain_merge_padding(FLAC__Metadata_Chain *chain)
{
FLAC__Metadata_Node *node;
FLAC__ASSERT(0 != chain);
for(node = chain->head; node; ) {
if(!chain_merge_adjacent_padding_(chain, node))
node = node->next;
}
}
FLAC_API void FLAC__metadata_chain_sort_padding(FLAC__Metadata_Chain *chain)
{
FLAC__Metadata_Node *node, *save;
unsigned i;
FLAC__ASSERT(0 != chain);
/*
* Don't try and be too smart... this simple algo is good enough for
* the small number of nodes that we deal with.
*/
for(i = 0, node = chain->head; i < chain->nodes; i++) {
if(node->data->type == FLAC__METADATA_TYPE_PADDING) {
save = node->next;
chain_remove_node_(chain, node);
chain_append_node_(chain, node);
node = save;
}
else {
node = node->next;
}
}
FLAC__metadata_chain_merge_padding(chain);
}
FLAC_API FLAC__Metadata_Iterator *FLAC__metadata_iterator_new(void)
{
FLAC__Metadata_Iterator *iterator = calloc(1, sizeof(FLAC__Metadata_Iterator));
/* calloc() implies:
iterator->current = 0;
iterator->chain = 0;
*/
return iterator;
}
FLAC_API void FLAC__metadata_iterator_delete(FLAC__Metadata_Iterator *iterator)
{
FLAC__ASSERT(0 != iterator);
free(iterator);
}
FLAC_API void FLAC__metadata_iterator_init(FLAC__Metadata_Iterator *iterator, FLAC__Metadata_Chain *chain)
{
FLAC__ASSERT(0 != iterator);
FLAC__ASSERT(0 != chain);
FLAC__ASSERT(0 != chain->head);
iterator->chain = chain;
iterator->current = chain->head;
}
FLAC_API FLAC__bool FLAC__metadata_iterator_next(FLAC__Metadata_Iterator *iterator)
{
FLAC__ASSERT(0 != iterator);
if(0 == iterator->current || 0 == iterator->current->next)
return false;
iterator->current = iterator->current->next;
return true;
}
FLAC_API FLAC__bool FLAC__metadata_iterator_prev(FLAC__Metadata_Iterator *iterator)
{
FLAC__ASSERT(0 != iterator);
if(0 == iterator->current || 0 == iterator->current->prev)
return false;
iterator->current = iterator->current->prev;
return true;
}
FLAC_API FLAC__MetadataType FLAC__metadata_iterator_get_block_type(const FLAC__Metadata_Iterator *iterator)
{
FLAC__ASSERT(0 != iterator);
FLAC__ASSERT(0 != iterator->current);
FLAC__ASSERT(0 != iterator->current->data);
return iterator->current->data->type;
}
FLAC_API FLAC__StreamMetadata *FLAC__metadata_iterator_get_block(FLAC__Metadata_Iterator *iterator)
{
FLAC__ASSERT(0 != iterator);
FLAC__ASSERT(0 != iterator->current);
return iterator->current->data;
}
FLAC_API FLAC__bool FLAC__metadata_iterator_set_block(FLAC__Metadata_Iterator *iterator, FLAC__StreamMetadata *block)
{
FLAC__ASSERT(0 != iterator);
FLAC__ASSERT(0 != block);
return FLAC__metadata_iterator_delete_block(iterator, false) && FLAC__metadata_iterator_insert_block_after(iterator, block);
}
FLAC_API FLAC__bool FLAC__metadata_iterator_delete_block(FLAC__Metadata_Iterator *iterator, FLAC__bool replace_with_padding)
{
FLAC__Metadata_Node *save;
FLAC__ASSERT(0 != iterator);
FLAC__ASSERT(0 != iterator->current);
if(0 == iterator->current->prev) {
FLAC__ASSERT(iterator->current->data->type == FLAC__METADATA_TYPE_STREAMINFO);
return false;
}
save = iterator->current->prev;
if(replace_with_padding) {
FLAC__metadata_object_delete_data(iterator->current->data);
iterator->current->data->type = FLAC__METADATA_TYPE_PADDING;
}
else {
chain_delete_node_(iterator->chain, iterator->current);
}
iterator->current = save;
return true;
}
FLAC_API FLAC__bool FLAC__metadata_iterator_insert_block_before(FLAC__Metadata_Iterator *iterator, FLAC__StreamMetadata *block)
{
FLAC__Metadata_Node *node;
FLAC__ASSERT(0 != iterator);
FLAC__ASSERT(0 != iterator->current);
FLAC__ASSERT(0 != block);
if(block->type == FLAC__METADATA_TYPE_STREAMINFO)
return false;
if(0 == iterator->current->prev) {
FLAC__ASSERT(iterator->current->data->type == FLAC__METADATA_TYPE_STREAMINFO);
return false;
}
if(0 == (node = node_new_()))
return false;
node->data = block;
iterator_insert_node_(iterator, node);
iterator->current = node;
return true;
}
FLAC_API FLAC__bool FLAC__metadata_iterator_insert_block_after(FLAC__Metadata_Iterator *iterator, FLAC__StreamMetadata *block)
{
FLAC__Metadata_Node *node;
FLAC__ASSERT(0 != iterator);
FLAC__ASSERT(0 != iterator->current);
FLAC__ASSERT(0 != block);
if(block->type == FLAC__METADATA_TYPE_STREAMINFO)
return false;
if(0 == (node = node_new_()))
return false;
node->data = block;
iterator_insert_node_after_(iterator, node);
iterator->current = node;
return true;
}
/****************************************************************************
*
* Local function definitions
*
***************************************************************************/
void pack_uint32_(FLAC__uint32 val, FLAC__byte *b, unsigned bytes)
{
unsigned i;
b += bytes;
for(i = 0; i < bytes; i++) {
*(--b) = (FLAC__byte)(val & 0xff);
val >>= 8;
}
}
void pack_uint32_little_endian_(FLAC__uint32 val, FLAC__byte *b, unsigned bytes)
{
unsigned i;
for(i = 0; i < bytes; i++) {
*(b++) = (FLAC__byte)(val & 0xff);
val >>= 8;
}
}
void pack_uint64_(FLAC__uint64 val, FLAC__byte *b, unsigned bytes)
{
unsigned i;
b += bytes;
for(i = 0; i < bytes; i++) {
*(--b) = (FLAC__byte)(val & 0xff);
val >>= 8;
}
}
FLAC__uint32 unpack_uint32_(FLAC__byte *b, unsigned bytes)
{
FLAC__uint32 ret = 0;
unsigned i;
for(i = 0; i < bytes; i++)
ret = (ret << 8) | (FLAC__uint32)(*b++);
return ret;
}
FLAC__uint32 unpack_uint32_little_endian_(FLAC__byte *b, unsigned bytes)
{
FLAC__uint32 ret = 0;
unsigned i;
b += bytes;
for(i = 0; i < bytes; i++)
ret = (ret << 8) | (FLAC__uint32)(*--b);
return ret;
}
FLAC__uint64 unpack_uint64_(FLAC__byte *b, unsigned bytes)
{
FLAC__uint64 ret = 0;
unsigned i;
for(i = 0; i < bytes; i++)
ret = (ret << 8) | (FLAC__uint64)(*b++);
return ret;
}
FLAC__bool read_metadata_block_header_(FLAC__Metadata_SimpleIterator *iterator)
{
FLAC__ASSERT(0 != iterator);
FLAC__ASSERT(0 != iterator->file);
if(!read_metadata_block_header_cb_((FLAC__IOHandle)iterator->file, (FLAC__IOCallback_Read)fread, &iterator->is_last, &iterator->type, &iterator->length)) {
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
return false;
}
return true;
}
FLAC__bool read_metadata_block_data_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block)
{
FLAC__ASSERT(0 != iterator);
FLAC__ASSERT(0 != iterator->file);
iterator->status = read_metadata_block_data_cb_((FLAC__IOHandle)iterator->file, (FLAC__IOCallback_Read)fread, fseek_wrapper_, block);
return (iterator->status == FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK);
}
FLAC__bool read_metadata_block_header_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__bool *is_last, FLAC__MetadataType *type, unsigned *length)
{
FLAC__byte raw_header[FLAC__STREAM_METADATA_HEADER_LENGTH];
if(read_cb(raw_header, 1, FLAC__STREAM_METADATA_HEADER_LENGTH, handle) != FLAC__STREAM_METADATA_HEADER_LENGTH)
return false;
*is_last = raw_header[0] & 0x80? true : false;
*type = (FLAC__MetadataType)(raw_header[0] & 0x7f);
*length = unpack_uint32_(raw_header + 1, 3);
/* Note that we don't check:
* if(iterator->type >= FLAC__METADATA_TYPE_UNDEFINED)
* we just will read in an opaque block
*/
return true;
}
FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Seek seek_cb, FLAC__StreamMetadata *block)
{
switch(block->type) {
case FLAC__METADATA_TYPE_STREAMINFO:
return read_metadata_block_data_streaminfo_cb_(handle, read_cb, &block->data.stream_info);
case FLAC__METADATA_TYPE_PADDING:
return read_metadata_block_data_padding_cb_(handle, seek_cb, &block->data.padding, block->length);
case FLAC__METADATA_TYPE_APPLICATION:
return read_metadata_block_data_application_cb_(handle, read_cb, &block->data.application, block->length);
case FLAC__METADATA_TYPE_SEEKTABLE:
return read_metadata_block_data_seektable_cb_(handle, read_cb, &block->data.seek_table, block->length);
case FLAC__METADATA_TYPE_VORBIS_COMMENT:
return read_metadata_block_data_vorbis_comment_cb_(handle, read_cb, seek_cb, &block->data.vorbis_comment, block->length);
case FLAC__METADATA_TYPE_CUESHEET:
return read_metadata_block_data_cuesheet_cb_(handle, read_cb, &block->data.cue_sheet);
case FLAC__METADATA_TYPE_PICTURE:
return read_metadata_block_data_picture_cb_(handle, read_cb, &block->data.picture);
default:
return read_metadata_block_data_unknown_cb_(handle, read_cb, &block->data.unknown, block->length);
}
}
FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_streaminfo_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_StreamInfo *block)
{
FLAC__byte buffer[FLAC__STREAM_METADATA_STREAMINFO_LENGTH], *b;
if(read_cb(buffer, 1, FLAC__STREAM_METADATA_STREAMINFO_LENGTH, handle) != FLAC__STREAM_METADATA_STREAMINFO_LENGTH)
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
b = buffer;
/* we are using hardcoded numbers for simplicity but we should
* probably eventually write a bit-level unpacker and use the
* _STREAMINFO_ constants.
*/
block->min_blocksize = unpack_uint32_(b, 2); b += 2;
block->max_blocksize = unpack_uint32_(b, 2); b += 2;
block->min_framesize = unpack_uint32_(b, 3); b += 3;
block->max_framesize = unpack_uint32_(b, 3); b += 3;
block->sample_rate = (unpack_uint32_(b, 2) << 4) | ((unsigned)(b[2] & 0xf0) >> 4);
block->channels = (unsigned)((b[2] & 0x0e) >> 1) + 1;
block->bits_per_sample = ((((unsigned)(b[2] & 0x01)) << 4) | (((unsigned)(b[3] & 0xf0)) >> 4)) + 1;
block->total_samples = (((FLAC__uint64)(b[3] & 0x0f)) << 32) | unpack_uint64_(b+4, 4);
memcpy(block->md5sum, b+8, 16);
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
}
FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_padding_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Seek seek_cb, FLAC__StreamMetadata_Padding *block, unsigned block_length)
{
(void)block; /* nothing to do; we don't care about reading the padding bytes */
if(0 != seek_cb(handle, block_length, SEEK_CUR))
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
}
FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_application_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_Application *block, unsigned block_length)
{
const unsigned id_bytes = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8;
if(read_cb(block->id, 1, id_bytes, handle) != id_bytes)
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
if(block_length < id_bytes)
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
block_length -= id_bytes;
if(block_length == 0) {
block->data = 0;
}
else {
if(0 == (block->data = malloc(block_length)))
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
if(read_cb(block->data, 1, block_length, handle) != block_length)
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
}
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
}
FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_seektable_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_SeekTable *block, unsigned block_length)
{
unsigned i;
FLAC__byte buffer[FLAC__STREAM_METADATA_SEEKPOINT_LENGTH];
FLAC__ASSERT(block_length % FLAC__STREAM_METADATA_SEEKPOINT_LENGTH == 0);
block->num_points = block_length / FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
if(block->num_points == 0)
block->points = 0;
else if(0 == (block->points = safe_malloc_mul_2op_p(block->num_points, /*times*/sizeof(FLAC__StreamMetadata_SeekPoint))))
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
for(i = 0; i < block->num_points; i++) {
if(read_cb(buffer, 1, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH, handle) != FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
/* some MAGIC NUMBERs here */
block->points[i].sample_number = unpack_uint64_(buffer, 8);
block->points[i].stream_offset = unpack_uint64_(buffer+8, 8);
block->points[i].frame_samples = unpack_uint32_(buffer+16, 2);
}
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
}
FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_vorbis_comment_entry_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_VorbisComment_Entry *entry, unsigned max_length)
{
const unsigned entry_length_len = FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN / 8;
FLAC__byte buffer[4]; /* magic number is asserted below */
FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN / 8 == sizeof(buffer));
if(max_length < entry_length_len)
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_BAD_METADATA;
max_length -= entry_length_len;
if(read_cb(buffer, 1, entry_length_len, handle) != entry_length_len)
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
entry->length = unpack_uint32_little_endian_(buffer, entry_length_len);
if(max_length < entry->length) {
entry->length = 0;
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_BAD_METADATA;
} else max_length -= entry->length;
if(0 != entry->entry)
free(entry->entry);
if(entry->length == 0) {
entry->entry = 0;
}
else {
if(0 == (entry->entry = safe_malloc_add_2op_(entry->length, /*+*/1)))
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
if(read_cb(entry->entry, 1, entry->length, handle) != entry->length)
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
entry->entry[entry->length] = '\0';
}
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
}
FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_vorbis_comment_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Seek seek_cb, FLAC__StreamMetadata_VorbisComment *block, unsigned block_length)
{
unsigned i;
FLAC__Metadata_SimpleIteratorStatus status;
const unsigned num_comments_len = FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN / 8;
FLAC__byte buffer[4]; /* magic number is asserted below */
FLAC__ASSERT(FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN / 8 == sizeof(buffer));
status = read_metadata_block_data_vorbis_comment_entry_cb_(handle, read_cb, &(block->vendor_string), block_length);
if(block_length >= 4)
block_length -= 4;
if(status == FLAC__METADATA_SIMPLE_ITERATOR_STATUS_BAD_METADATA)
goto skip;
else if(status != FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK)
return status;
block_length -= block->vendor_string.length;
if(block_length < num_comments_len) goto skip; else block_length -= num_comments_len;
if(read_cb(buffer, 1, num_comments_len, handle) != num_comments_len)
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
block->num_comments = unpack_uint32_little_endian_(buffer, num_comments_len);
if(block->num_comments == 0) {
block->comments = 0;
}
else if(0 == (block->comments = calloc(block->num_comments, sizeof(FLAC__StreamMetadata_VorbisComment_Entry)))) {
block->num_comments = 0;
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
}
for(i = 0; i < block->num_comments; i++) {
status = read_metadata_block_data_vorbis_comment_entry_cb_(handle, read_cb, block->comments + i, block_length);
if(block_length >= 4) block_length -= 4;
if(status == FLAC__METADATA_SIMPLE_ITERATOR_STATUS_BAD_METADATA) {
block->num_comments = i;
goto skip;
}
else if(status != FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK) return status;
block_length -= block->comments[i].length;
}
skip:
if(block_length > 0) {
/* bad metadata */
if(0 != seek_cb(handle, block_length, SEEK_CUR))
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
}
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
}
FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_cuesheet_track_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_CueSheet_Track *track)
{
unsigned i, len;
FLAC__byte buffer[32]; /* asserted below that this is big enough */
FLAC__ASSERT(sizeof(buffer) >= sizeof(FLAC__uint64));
FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN/8);
FLAC__ASSERT(sizeof(buffer) >= (FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN) / 8);
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN % 8 == 0);
len = FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN / 8;
if(read_cb(buffer, 1, len, handle) != len)
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
track->offset = unpack_uint64_(buffer, len);
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN % 8 == 0);
len = FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN / 8;
if(read_cb(buffer, 1, len, handle) != len)
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
track->number = (FLAC__byte)unpack_uint32_(buffer, len);
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN % 8 == 0);
len = FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN / 8;
if(read_cb(track->isrc, 1, len, handle) != len)
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
FLAC__ASSERT((FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN) % 8 == 0);
len = (FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN) / 8;
if(read_cb(buffer, 1, len, handle) != len)
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN == 1);
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN == 1);
track->type = buffer[0] >> 7;
track->pre_emphasis = (buffer[0] >> 6) & 1;
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN % 8 == 0);
len = FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN / 8;
if(read_cb(buffer, 1, len, handle) != len)
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
track->num_indices = (FLAC__byte)unpack_uint32_(buffer, len);
if(track->num_indices == 0) {
track->indices = 0;
}
else if(0 == (track->indices = calloc(track->num_indices, sizeof(FLAC__StreamMetadata_CueSheet_Index))))
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
for(i = 0; i < track->num_indices; i++) {
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN % 8 == 0);
len = FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN / 8;
if(read_cb(buffer, 1, len, handle) != len)
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
track->indices[i].offset = unpack_uint64_(buffer, len);
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN % 8 == 0);
len = FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN / 8;
if(read_cb(buffer, 1, len, handle) != len)
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
track->indices[i].number = (FLAC__byte)unpack_uint32_(buffer, len);
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN % 8 == 0);
len = FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN / 8;
if(read_cb(buffer, 1, len, handle) != len)
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
}
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
}
FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_cuesheet_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_CueSheet *block)
{
unsigned i, len;
FLAC__Metadata_SimpleIteratorStatus status;
FLAC__byte buffer[1024]; /* MSVC needs a constant expression so we put a magic number and assert */
FLAC__ASSERT((FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN)/8 <= sizeof(buffer));
FLAC__ASSERT(sizeof(FLAC__uint64) <= sizeof(buffer));
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN % 8 == 0);
len = FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN / 8;
if(read_cb(block->media_catalog_number, 1, len, handle) != len)
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN % 8 == 0);
len = FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN / 8;
if(read_cb(buffer, 1, len, handle) != len)
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
block->lead_in = unpack_uint64_(buffer, len);
FLAC__ASSERT((FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN) % 8 == 0);
len = (FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN) / 8;
if(read_cb(buffer, 1, len, handle) != len)
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
block->is_cd = buffer[0]&0x80? true : false;
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN % 8 == 0);
len = FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN / 8;
if(read_cb(buffer, 1, len, handle) != len)
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
block->num_tracks = unpack_uint32_(buffer, len);
if(block->num_tracks == 0) {
block->tracks = 0;
}
else if(0 == (block->tracks = calloc(block->num_tracks, sizeof(FLAC__StreamMetadata_CueSheet_Track))))
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
for(i = 0; i < block->num_tracks; i++) {
if(FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK != (status = read_metadata_block_data_cuesheet_track_cb_(handle, read_cb, block->tracks + i)))
return status;
}
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
}
static FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_picture_cstring_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__byte **data, FLAC__uint32 *length, FLAC__uint32 length_len)
{
FLAC__byte buffer[sizeof(FLAC__uint32)];
FLAC__ASSERT(0 != data);
FLAC__ASSERT(length_len%8 == 0);
length_len /= 8; /* convert to bytes */
FLAC__ASSERT(sizeof(buffer) >= length_len);
if(read_cb(buffer, 1, length_len, handle) != length_len)
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
*length = unpack_uint32_(buffer, length_len);
if(0 != *data)
free(*data);
if(0 == (*data = safe_malloc_add_2op_(*length, /*+*/1)))
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
if(*length > 0) {
if(read_cb(*data, 1, *length, handle) != *length)
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
}
(*data)[*length] = '\0';
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
}
FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_picture_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_Picture *block)
{
FLAC__Metadata_SimpleIteratorStatus status;
FLAC__byte buffer[4]; /* asserted below that this is big enough */
FLAC__uint32 len;
FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_TYPE_LEN/8);
FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN/8);
FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN/8);
FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN/8);
FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_COLORS_LEN/8);
FLAC__ASSERT(FLAC__STREAM_METADATA_PICTURE_TYPE_LEN % 8 == 0);
len = FLAC__STREAM_METADATA_PICTURE_TYPE_LEN / 8;
if(read_cb(buffer, 1, len, handle) != len)
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
block->type = (FLAC__StreamMetadata_Picture_Type)unpack_uint32_(buffer, len);
if((status = read_metadata_block_data_picture_cstring_cb_(handle, read_cb, (FLAC__byte**)(&(block->mime_type)), &len, FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN)) != FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK)
return status;
if((status = read_metadata_block_data_picture_cstring_cb_(handle, read_cb, &(block->description), &len, FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN)) != FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK)
return status;
FLAC__ASSERT(FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN % 8 == 0);
len = FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN / 8;
if(read_cb(buffer, 1, len, handle) != len)
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
block->width = unpack_uint32_(buffer, len);
FLAC__ASSERT(FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN % 8 == 0);
len = FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN / 8;
if(read_cb(buffer, 1, len, handle) != len)
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
block->height = unpack_uint32_(buffer, len);
FLAC__ASSERT(FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN % 8 == 0);
len = FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN / 8;
if(read_cb(buffer, 1, len, handle) != len)
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
block->depth = unpack_uint32_(buffer, len);
FLAC__ASSERT(FLAC__STREAM_METADATA_PICTURE_COLORS_LEN % 8 == 0);
len = FLAC__STREAM_METADATA_PICTURE_COLORS_LEN / 8;
if(read_cb(buffer, 1, len, handle) != len)
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
block->colors = unpack_uint32_(buffer, len);
/* for convenience we use read_metadata_block_data_picture_cstring_cb_() even though it adds an extra terminating NUL we don't use */
if((status = read_metadata_block_data_picture_cstring_cb_(handle, read_cb, &(block->data), &(block->data_length), FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN)) != FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK)
return status;
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
}
FLAC__Metadata_SimpleIteratorStatus read_metadata_block_data_unknown_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__StreamMetadata_Unknown *block, unsigned block_length)
{
if(block_length == 0) {
block->data = 0;
}
else {
if(0 == (block->data = malloc(block_length)))
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
if(read_cb(block->data, 1, block_length, handle) != block_length)
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
}
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
}
FLAC__bool write_metadata_block_header_(FILE *file, FLAC__Metadata_SimpleIteratorStatus *status, const FLAC__StreamMetadata *block)
{
FLAC__ASSERT(0 != file);
FLAC__ASSERT(0 != status);
if(!write_metadata_block_header_cb_((FLAC__IOHandle)file, (FLAC__IOCallback_Write)fwrite, block)) {
*status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
return false;
}
return true;
}
FLAC__bool write_metadata_block_data_(FILE *file, FLAC__Metadata_SimpleIteratorStatus *status, const FLAC__StreamMetadata *block)
{
FLAC__ASSERT(0 != file);
FLAC__ASSERT(0 != status);
if (write_metadata_block_data_cb_((FLAC__IOHandle)file, (FLAC__IOCallback_Write)fwrite, block)) {
*status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK;
return true;
}
else {
*status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
return false;
}
}
FLAC__bool write_metadata_block_header_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata *block)
{
FLAC__byte buffer[FLAC__STREAM_METADATA_HEADER_LENGTH];
FLAC__ASSERT(block->length < (1u << FLAC__STREAM_METADATA_LENGTH_LEN));
/* double protection */
if(block->length >= (1u << FLAC__STREAM_METADATA_LENGTH_LEN))
return false;
buffer[0] = (block->is_last? 0x80 : 0) | (FLAC__byte)block->type;
pack_uint32_(block->length, buffer + 1, 3);
if(write_cb(buffer, 1, FLAC__STREAM_METADATA_HEADER_LENGTH, handle) != FLAC__STREAM_METADATA_HEADER_LENGTH)
return false;
return true;
}
FLAC__bool write_metadata_block_data_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata *block)
{
FLAC__ASSERT(0 != block);
switch(block->type) {
case FLAC__METADATA_TYPE_STREAMINFO:
return write_metadata_block_data_streaminfo_cb_(handle, write_cb, &block->data.stream_info);
case FLAC__METADATA_TYPE_PADDING:
return write_metadata_block_data_padding_cb_(handle, write_cb, &block->data.padding, block->length);
case FLAC__METADATA_TYPE_APPLICATION:
return write_metadata_block_data_application_cb_(handle, write_cb, &block->data.application, block->length);
case FLAC__METADATA_TYPE_SEEKTABLE:
return write_metadata_block_data_seektable_cb_(handle, write_cb, &block->data.seek_table);
case FLAC__METADATA_TYPE_VORBIS_COMMENT:
return write_metadata_block_data_vorbis_comment_cb_(handle, write_cb, &block->data.vorbis_comment);
case FLAC__METADATA_TYPE_CUESHEET:
return write_metadata_block_data_cuesheet_cb_(handle, write_cb, &block->data.cue_sheet);
case FLAC__METADATA_TYPE_PICTURE:
return write_metadata_block_data_picture_cb_(handle, write_cb, &block->data.picture);
default:
return write_metadata_block_data_unknown_cb_(handle, write_cb, &block->data.unknown, block->length);
}
}
FLAC__bool write_metadata_block_data_streaminfo_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_StreamInfo *block)
{
FLAC__byte buffer[FLAC__STREAM_METADATA_STREAMINFO_LENGTH];
const unsigned channels1 = block->channels - 1;
const unsigned bps1 = block->bits_per_sample - 1;
/* we are using hardcoded numbers for simplicity but we should
* probably eventually write a bit-level packer and use the
* _STREAMINFO_ constants.
*/
pack_uint32_(block->min_blocksize, buffer, 2);
pack_uint32_(block->max_blocksize, buffer+2, 2);
pack_uint32_(block->min_framesize, buffer+4, 3);
pack_uint32_(block->max_framesize, buffer+7, 3);
buffer[10] = (block->sample_rate >> 12) & 0xff;
buffer[11] = (block->sample_rate >> 4) & 0xff;
buffer[12] = ((block->sample_rate & 0x0f) << 4) | (channels1 << 1) | (bps1 >> 4);
buffer[13] = (FLAC__byte)(((bps1 & 0x0f) << 4) | ((block->total_samples >> 32) & 0x0f));
pack_uint32_((FLAC__uint32)block->total_samples, buffer+14, 4);
memcpy(buffer+18, block->md5sum, 16);
if(write_cb(buffer, 1, FLAC__STREAM_METADATA_STREAMINFO_LENGTH, handle) != FLAC__STREAM_METADATA_STREAMINFO_LENGTH)
return false;
return true;
}
FLAC__bool write_metadata_block_data_padding_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Padding *block, unsigned block_length)
{
unsigned i, n = block_length;
FLAC__byte buffer[1024];
(void)block;
memset(buffer, 0, 1024);
for(i = 0; i < n/1024; i++)
if(write_cb(buffer, 1, 1024, handle) != 1024)
return false;
n %= 1024;
if(write_cb(buffer, 1, n, handle) != n)
return false;
return true;
}
FLAC__bool write_metadata_block_data_application_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Application *block, unsigned block_length)
{
const unsigned id_bytes = FLAC__STREAM_METADATA_APPLICATION_ID_LEN / 8;
if(write_cb(block->id, 1, id_bytes, handle) != id_bytes)
return false;
block_length -= id_bytes;
if(write_cb(block->data, 1, block_length, handle) != block_length)
return false;
return true;
}
FLAC__bool write_metadata_block_data_seektable_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_SeekTable *block)
{
unsigned i;
FLAC__byte buffer[FLAC__STREAM_METADATA_SEEKPOINT_LENGTH];
for(i = 0; i < block->num_points; i++) {
/* some MAGIC NUMBERs here */
pack_uint64_(block->points[i].sample_number, buffer, 8);
pack_uint64_(block->points[i].stream_offset, buffer+8, 8);
pack_uint32_(block->points[i].frame_samples, buffer+16, 2);
if(write_cb(buffer, 1, FLAC__STREAM_METADATA_SEEKPOINT_LENGTH, handle) != FLAC__STREAM_METADATA_SEEKPOINT_LENGTH)
return false;
}
return true;
}
FLAC__bool write_metadata_block_data_vorbis_comment_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_VorbisComment *block)
{
unsigned i;
const unsigned entry_length_len = FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN / 8;
const unsigned num_comments_len = FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN / 8;
FLAC__byte buffer[4]; /* magic number is asserted below */
FLAC__ASSERT(flac_max(FLAC__STREAM_METADATA_VORBIS_COMMENT_ENTRY_LENGTH_LEN, FLAC__STREAM_METADATA_VORBIS_COMMENT_NUM_COMMENTS_LEN) / 8 == sizeof(buffer));
pack_uint32_little_endian_(block->vendor_string.length, buffer, entry_length_len);
if(write_cb(buffer, 1, entry_length_len, handle) != entry_length_len)
return false;
if(write_cb(block->vendor_string.entry, 1, block->vendor_string.length, handle) != block->vendor_string.length)
return false;
pack_uint32_little_endian_(block->num_comments, buffer, num_comments_len);
if(write_cb(buffer, 1, num_comments_len, handle) != num_comments_len)
return false;
for(i = 0; i < block->num_comments; i++) {
pack_uint32_little_endian_(block->comments[i].length, buffer, entry_length_len);
if(write_cb(buffer, 1, entry_length_len, handle) != entry_length_len)
return false;
if(write_cb(block->comments[i].entry, 1, block->comments[i].length, handle) != block->comments[i].length)
return false;
}
return true;
}
FLAC__bool write_metadata_block_data_cuesheet_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_CueSheet *block)
{
unsigned i, j, len;
FLAC__byte buffer[1024]; /* asserted below that this is big enough */
FLAC__ASSERT(sizeof(buffer) >= sizeof(FLAC__uint64));
FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN/8);
FLAC__ASSERT(sizeof(buffer) >= (FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN + FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN)/8);
FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN/8);
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN % 8 == 0);
len = FLAC__STREAM_METADATA_CUESHEET_MEDIA_CATALOG_NUMBER_LEN / 8;
if(write_cb(block->media_catalog_number, 1, len, handle) != len)
return false;
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN % 8 == 0);
len = FLAC__STREAM_METADATA_CUESHEET_LEAD_IN_LEN / 8;
pack_uint64_(block->lead_in, buffer, len);
if(write_cb(buffer, 1, len, handle) != len)
return false;
FLAC__ASSERT((FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN) % 8 == 0);
len = (FLAC__STREAM_METADATA_CUESHEET_IS_CD_LEN + FLAC__STREAM_METADATA_CUESHEET_RESERVED_LEN) / 8;
memset(buffer, 0, len);
if(block->is_cd)
buffer[0] |= 0x80;
if(write_cb(buffer, 1, len, handle) != len)
return false;
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN % 8 == 0);
len = FLAC__STREAM_METADATA_CUESHEET_NUM_TRACKS_LEN / 8;
pack_uint32_(block->num_tracks, buffer, len);
if(write_cb(buffer, 1, len, handle) != len)
return false;
for(i = 0; i < block->num_tracks; i++) {
FLAC__StreamMetadata_CueSheet_Track *track = block->tracks + i;
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN % 8 == 0);
len = FLAC__STREAM_METADATA_CUESHEET_TRACK_OFFSET_LEN / 8;
pack_uint64_(track->offset, buffer, len);
if(write_cb(buffer, 1, len, handle) != len)
return false;
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN % 8 == 0);
len = FLAC__STREAM_METADATA_CUESHEET_TRACK_NUMBER_LEN / 8;
pack_uint32_(track->number, buffer, len);
if(write_cb(buffer, 1, len, handle) != len)
return false;
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN % 8 == 0);
len = FLAC__STREAM_METADATA_CUESHEET_TRACK_ISRC_LEN / 8;
if(write_cb(track->isrc, 1, len, handle) != len)
return false;
FLAC__ASSERT((FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN) % 8 == 0);
len = (FLAC__STREAM_METADATA_CUESHEET_TRACK_TYPE_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_PRE_EMPHASIS_LEN + FLAC__STREAM_METADATA_CUESHEET_TRACK_RESERVED_LEN) / 8;
memset(buffer, 0, len);
buffer[0] = (track->type << 7) | (track->pre_emphasis << 6);
if(write_cb(buffer, 1, len, handle) != len)
return false;
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN % 8 == 0);
len = FLAC__STREAM_METADATA_CUESHEET_TRACK_NUM_INDICES_LEN / 8;
pack_uint32_(track->num_indices, buffer, len);
if(write_cb(buffer, 1, len, handle) != len)
return false;
for(j = 0; j < track->num_indices; j++) {
FLAC__StreamMetadata_CueSheet_Index *indx = track->indices + j;
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN % 8 == 0);
len = FLAC__STREAM_METADATA_CUESHEET_INDEX_OFFSET_LEN / 8;
pack_uint64_(indx->offset, buffer, len);
if(write_cb(buffer, 1, len, handle) != len)
return false;
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN % 8 == 0);
len = FLAC__STREAM_METADATA_CUESHEET_INDEX_NUMBER_LEN / 8;
pack_uint32_(indx->number, buffer, len);
if(write_cb(buffer, 1, len, handle) != len)
return false;
FLAC__ASSERT(FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN % 8 == 0);
len = FLAC__STREAM_METADATA_CUESHEET_INDEX_RESERVED_LEN / 8;
memset(buffer, 0, len);
if(write_cb(buffer, 1, len, handle) != len)
return false;
}
}
return true;
}
FLAC__bool write_metadata_block_data_picture_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Picture *block)
{
unsigned len;
size_t slen;
FLAC__byte buffer[4]; /* magic number is asserted below */
FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_TYPE_LEN%8);
FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN%8);
FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN%8);
FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN%8);
FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN%8);
FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN%8);
FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_COLORS_LEN%8);
FLAC__ASSERT(0 == FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN%8);
FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_TYPE_LEN/8);
FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN/8);
FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN/8);
FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN/8);
FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN/8);
FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN/8);
FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_COLORS_LEN/8);
FLAC__ASSERT(sizeof(buffer) >= FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN/8);
len = FLAC__STREAM_METADATA_PICTURE_TYPE_LEN/8;
pack_uint32_(block->type, buffer, len);
if(write_cb(buffer, 1, len, handle) != len)
return false;
len = FLAC__STREAM_METADATA_PICTURE_MIME_TYPE_LENGTH_LEN/8;
slen = strlen(block->mime_type);
pack_uint32_(slen, buffer, len);
if(write_cb(buffer, 1, len, handle) != len)
return false;
if(write_cb(block->mime_type, 1, slen, handle) != slen)
return false;
len = FLAC__STREAM_METADATA_PICTURE_DESCRIPTION_LENGTH_LEN/8;
slen = strlen((const char *)block->description);
pack_uint32_(slen, buffer, len);
if(write_cb(buffer, 1, len, handle) != len)
return false;
if(write_cb(block->description, 1, slen, handle) != slen)
return false;
len = FLAC__STREAM_METADATA_PICTURE_WIDTH_LEN/8;
pack_uint32_(block->width, buffer, len);
if(write_cb(buffer, 1, len, handle) != len)
return false;
len = FLAC__STREAM_METADATA_PICTURE_HEIGHT_LEN/8;
pack_uint32_(block->height, buffer, len);
if(write_cb(buffer, 1, len, handle) != len)
return false;
len = FLAC__STREAM_METADATA_PICTURE_DEPTH_LEN/8;
pack_uint32_(block->depth, buffer, len);
if(write_cb(buffer, 1, len, handle) != len)
return false;
len = FLAC__STREAM_METADATA_PICTURE_COLORS_LEN/8;
pack_uint32_(block->colors, buffer, len);
if(write_cb(buffer, 1, len, handle) != len)
return false;
len = FLAC__STREAM_METADATA_PICTURE_DATA_LENGTH_LEN/8;
pack_uint32_(block->data_length, buffer, len);
if(write_cb(buffer, 1, len, handle) != len)
return false;
if(write_cb(block->data, 1, block->data_length, handle) != block->data_length)
return false;
return true;
}
FLAC__bool write_metadata_block_data_unknown_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Write write_cb, const FLAC__StreamMetadata_Unknown *block, unsigned block_length)
{
if(write_cb(block->data, 1, block_length, handle) != block_length)
return false;
return true;
}
FLAC__bool write_metadata_block_stationary_(FLAC__Metadata_SimpleIterator *iterator, const FLAC__StreamMetadata *block)
{
if(0 != fseeko(iterator->file, iterator->offset[iterator->depth], SEEK_SET)) {
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
return false;
}
if(!write_metadata_block_header_(iterator->file, &iterator->status, block))
return false;
if(!write_metadata_block_data_(iterator->file, &iterator->status, block))
return false;
if(0 != fseeko(iterator->file, iterator->offset[iterator->depth], SEEK_SET)) {
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
return false;
}
return read_metadata_block_header_(iterator);
}
FLAC__bool write_metadata_block_stationary_with_padding_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, unsigned padding_length, FLAC__bool padding_is_last)
{
FLAC__StreamMetadata *padding;
if(0 != fseeko(iterator->file, iterator->offset[iterator->depth], SEEK_SET)) {
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
return false;
}
block->is_last = false;
if(!write_metadata_block_header_(iterator->file, &iterator->status, block))
return false;
if(!write_metadata_block_data_(iterator->file, &iterator->status, block))
return false;
if(0 == (padding = FLAC__metadata_object_new(FLAC__METADATA_TYPE_PADDING)))
return FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
padding->is_last = padding_is_last;
padding->length = padding_length;
if(!write_metadata_block_header_(iterator->file, &iterator->status, padding)) {
FLAC__metadata_object_delete(padding);
return false;
}
if(!write_metadata_block_data_(iterator->file, &iterator->status, padding)) {
FLAC__metadata_object_delete(padding);
return false;
}
FLAC__metadata_object_delete(padding);
if(0 != fseeko(iterator->file, iterator->offset[iterator->depth], SEEK_SET)) {
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
return false;
}
return read_metadata_block_header_(iterator);
}
FLAC__bool rewrite_whole_file_(FLAC__Metadata_SimpleIterator *iterator, FLAC__StreamMetadata *block, FLAC__bool append)
{
FILE *tempfile = NULL;
char *tempfilename = NULL;
int fixup_is_last_code = 0; /* 0 => no need to change any is_last flags */
FLAC__off_t fixup_is_last_flag_offset = -1;
FLAC__ASSERT(0 != block || append == false);
if(iterator->is_last) {
if(append) {
fixup_is_last_code = 1; /* 1 => clear the is_last flag at the following offset */
fixup_is_last_flag_offset = iterator->offset[iterator->depth];
}
else if(0 == block) {
simple_iterator_push_(iterator);
if(!FLAC__metadata_simple_iterator_prev(iterator)) {
(void)simple_iterator_pop_(iterator);
return false;
}
fixup_is_last_code = -1; /* -1 => set the is_last the flag at the following offset */
fixup_is_last_flag_offset = iterator->offset[iterator->depth];
if(!simple_iterator_pop_(iterator))
return false;
}
}
if(!simple_iterator_copy_file_prefix_(iterator, &tempfile, &tempfilename, append))
return false;
if(0 != block) {
if(!write_metadata_block_header_(tempfile, &iterator->status, block)) {
cleanup_tempfile_(&tempfile, &tempfilename);
return false;
}
if(!write_metadata_block_data_(tempfile, &iterator->status, block)) {
cleanup_tempfile_(&tempfile, &tempfilename);
return false;
}
}
if(!simple_iterator_copy_file_postfix_(iterator, &tempfile, &tempfilename, fixup_is_last_code, fixup_is_last_flag_offset, block==0))
return false;
if(append)
return FLAC__metadata_simple_iterator_next(iterator);
return true;
}
void simple_iterator_push_(FLAC__Metadata_SimpleIterator *iterator)
{
FLAC__ASSERT(iterator->depth+1 < SIMPLE_ITERATOR_MAX_PUSH_DEPTH);
iterator->offset[iterator->depth+1] = iterator->offset[iterator->depth];
iterator->depth++;
}
FLAC__bool simple_iterator_pop_(FLAC__Metadata_SimpleIterator *iterator)
{
FLAC__ASSERT(iterator->depth > 0);
iterator->depth--;
if(0 != fseeko(iterator->file, iterator->offset[iterator->depth], SEEK_SET)) {
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
return false;
}
return read_metadata_block_header_(iterator);
}
/* return meanings:
* 0: ok
* 1: read error
* 2: seek error
* 3: not a FLAC file
*/
unsigned seek_to_first_metadata_block_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Seek seek_cb)
{
FLAC__byte buffer[4];
size_t n;
unsigned i;
FLAC__ASSERT(FLAC__STREAM_SYNC_LENGTH == sizeof(buffer));
/* skip any id3v2 tag */
errno = 0;
n = read_cb(buffer, 1, 4, handle);
if(errno)
return 1;
else if(n != 4)
return 3;
else if(0 == memcmp(buffer, "ID3", 3)) {
unsigned tag_length = 0;
/* skip to the tag length */
if(seek_cb(handle, 2, SEEK_CUR) < 0)
return 2;
/* read the length */
for(i = 0; i < 4; i++) {
if(read_cb(buffer, 1, 1, handle) < 1 || buffer[0] & 0x80)
return 1;
tag_length <<= 7;
tag_length |= (buffer[0] & 0x7f);
}
/* skip the rest of the tag */
if(seek_cb(handle, tag_length, SEEK_CUR) < 0)
return 2;
/* read the stream sync code */
errno = 0;
n = read_cb(buffer, 1, 4, handle);
if(errno)
return 1;
else if(n != 4)
return 3;
}
/* check for the fLaC signature */
if(0 == memcmp(FLAC__STREAM_SYNC_STRING, buffer, FLAC__STREAM_SYNC_LENGTH))
return 0;
else
return 3;
}
unsigned seek_to_first_metadata_block_(FILE *f)
{
return seek_to_first_metadata_block_cb_((FLAC__IOHandle)f, (FLAC__IOCallback_Read)fread, fseek_wrapper_);
}
FLAC__bool simple_iterator_copy_file_prefix_(FLAC__Metadata_SimpleIterator *iterator, FILE **tempfile, char **tempfilename, FLAC__bool append)
{
const FLAC__off_t offset_end = append? iterator->offset[iterator->depth] + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH + (FLAC__off_t)iterator->length : iterator->offset[iterator->depth];
if(0 != fseeko(iterator->file, 0, SEEK_SET)) {
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
return false;
}
if(!open_tempfile_(iterator->filename, iterator->tempfile_path_prefix, tempfile, tempfilename, &iterator->status)) {
cleanup_tempfile_(tempfile, tempfilename);
return false;
}
if(!copy_n_bytes_from_file_(iterator->file, *tempfile, offset_end, &iterator->status)) {
cleanup_tempfile_(tempfile, tempfilename);
return false;
}
return true;
}
FLAC__bool simple_iterator_copy_file_postfix_(FLAC__Metadata_SimpleIterator *iterator, FILE **tempfile, char **tempfilename, int fixup_is_last_code, FLAC__off_t fixup_is_last_flag_offset, FLAC__bool backup)
{
FLAC__off_t save_offset = iterator->offset[iterator->depth];
FLAC__ASSERT(0 != *tempfile);
if(0 != fseeko(iterator->file, save_offset + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH + (FLAC__off_t)iterator->length, SEEK_SET)) {
cleanup_tempfile_(tempfile, tempfilename);
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
return false;
}
if(!copy_remaining_bytes_from_file_(iterator->file, *tempfile, &iterator->status)) {
cleanup_tempfile_(tempfile, tempfilename);
return false;
}
if(fixup_is_last_code != 0) {
/*
* if code == 1, it means a block was appended to the end so
* we have to clear the is_last flag of the previous block
* if code == -1, it means the last block was deleted so
* we have to set the is_last flag of the previous block
*/
/* MAGIC NUMBERs here; we know the is_last flag is the high bit of the byte at this location */
FLAC__byte x;
if(0 != fseeko(*tempfile, fixup_is_last_flag_offset, SEEK_SET)) {
cleanup_tempfile_(tempfile, tempfilename);
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
return false;
}
if(fread(&x, 1, 1, *tempfile) != 1) {
cleanup_tempfile_(tempfile, tempfilename);
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
return false;
}
if(fixup_is_last_code > 0) {
FLAC__ASSERT(x & 0x80);
x &= 0x7f;
}
else {
FLAC__ASSERT(!(x & 0x80));
x |= 0x80;
}
if(0 != fseeko(*tempfile, fixup_is_last_flag_offset, SEEK_SET)) {
cleanup_tempfile_(tempfile, tempfilename);
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR;
return false;
}
if(local__fwrite(&x, 1, 1, *tempfile) != 1) {
cleanup_tempfile_(tempfile, tempfilename);
iterator->status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
return false;
}
}
(void)fclose(iterator->file);
if(!transport_tempfile_(iterator->filename, tempfile, tempfilename, &iterator->status))
return false;
if(iterator->has_stats)
set_file_stats_(iterator->filename, &iterator->stats);
if(!simple_iterator_prime_input_(iterator, !iterator->is_writable))
return false;
if(backup) {
while(iterator->offset[iterator->depth] + (FLAC__off_t)FLAC__STREAM_METADATA_HEADER_LENGTH + (FLAC__off_t)iterator->length < save_offset)
if(!FLAC__metadata_simple_iterator_next(iterator))
return false;
return true;
}
else {
/* move the iterator to it's original block faster by faking a push, then doing a pop_ */
FLAC__ASSERT(iterator->depth == 0);
iterator->offset[0] = save_offset;
iterator->depth++;
return simple_iterator_pop_(iterator);
}
}
FLAC__bool copy_n_bytes_from_file_(FILE *file, FILE *tempfile, FLAC__off_t bytes, FLAC__Metadata_SimpleIteratorStatus *status)
{
FLAC__byte buffer[8192];
size_t n;
FLAC__ASSERT(bytes >= 0);
while(bytes > 0) {
n = flac_min(sizeof(buffer), (size_t)bytes);
if(fread(buffer, 1, n, file) != n) {
*status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
return false;
}
if(local__fwrite(buffer, 1, n, tempfile) != n) {
*status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
return false;
}
bytes -= n;
}
return true;
}
FLAC__bool copy_n_bytes_from_file_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOHandle temp_handle, FLAC__IOCallback_Write temp_write_cb, FLAC__off_t bytes, FLAC__Metadata_SimpleIteratorStatus *status)
{
FLAC__byte buffer[8192];
size_t n;
FLAC__ASSERT(bytes >= 0);
while(bytes > 0) {
n = flac_min(sizeof(buffer), (size_t)bytes);
if(read_cb(buffer, 1, n, handle) != n) {
*status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
return false;
}
if(temp_write_cb(buffer, 1, n, temp_handle) != n) {
*status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
return false;
}
bytes -= n;
}
return true;
}
FLAC__bool copy_remaining_bytes_from_file_(FILE *file, FILE *tempfile, FLAC__Metadata_SimpleIteratorStatus *status)
{
FLAC__byte buffer[8192];
size_t n;
while(!feof(file)) {
n = fread(buffer, 1, sizeof(buffer), file);
if(n == 0 && !feof(file)) {
*status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
return false;
}
if(n > 0 && local__fwrite(buffer, 1, n, tempfile) != n) {
*status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
return false;
}
}
return true;
}
FLAC__bool copy_remaining_bytes_from_file_cb_(FLAC__IOHandle handle, FLAC__IOCallback_Read read_cb, FLAC__IOCallback_Eof eof_cb, FLAC__IOHandle temp_handle, FLAC__IOCallback_Write temp_write_cb, FLAC__Metadata_SimpleIteratorStatus *status)
{
FLAC__byte buffer[8192];
size_t n;
while(!eof_cb(handle)) {
n = read_cb(buffer, 1, sizeof(buffer), handle);
if(n == 0 && !eof_cb(handle)) {
*status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR;
return false;
}
if(n > 0 && temp_write_cb(buffer, 1, n, temp_handle) != n) {
*status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR;
return false;
}
}
return true;
}
static int
local_snprintf(char *str, size_t size, const char *fmt, ...)
{
va_list va;
int rc;
#if defined _MSC_VER
if (size == 0)
return 1024;
#endif
va_start (va, fmt);
#if defined _MSC_VER
rc = vsnprintf_s (str, size, _TRUNCATE, fmt, va);
if (rc < 0)
rc = size - 1;
#elif defined __MINGW32__
rc = __mingw_vsnprintf (str, size, fmt, va);
#else
rc = vsnprintf (str, size, fmt, va);
#endif
va_end (va);
return rc;
}
FLAC__bool open_tempfile_(const char *filename, const char *tempfile_path_prefix, FILE **tempfile, char **tempfilename, FLAC__Metadata_SimpleIteratorStatus *status)
{
static const char *tempfile_suffix = ".metadata_edit";
if(0 == tempfile_path_prefix) {
size_t dest_len = strlen(filename) + strlen(tempfile_suffix) + 1;
if(0 == (*tempfilename = safe_malloc_(dest_len))) {
*status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
return false;
}
local_snprintf(*tempfilename, dest_len, "%s%s", filename, tempfile_suffix);
}
else {
const char *p = strrchr(filename, '/');
size_t dest_len;
if(0 == p)
p = filename;
else
p++;
dest_len = strlen(tempfile_path_prefix) + strlen(p) + strlen(tempfile_suffix) + 2;
if(0 == (*tempfilename = safe_malloc_(dest_len))) {
*status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR;
return false;
}
local_snprintf(*tempfilename, dest_len, "%s/%s%s", tempfile_path_prefix, p, tempfile_suffix);
}
if(0 == (*tempfile = flac_fopen(*tempfilename, "w+b"))) {
*status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE;
return false;
}
return true;
}
FLAC__bool transport_tempfile_(const char *filename, FILE **tempfile, char **tempfilename, FLAC__Metadata_SimpleIteratorStatus *status)
{
FLAC__ASSERT(0 != filename);
FLAC__ASSERT(0 != tempfile);
FLAC__ASSERT(0 != *tempfile);
FLAC__ASSERT(0 != tempfilename);
FLAC__ASSERT(0 != *tempfilename);
FLAC__ASSERT(0 != status);
(void)fclose(*tempfile);
*tempfile = 0;
#if defined _MSC_VER || defined __BORLANDC__ || defined __MINGW32__ || defined __EMX__
/* on some flavors of windows, flac_rename() will fail if the destination already exists */
if(flac_unlink(filename) < 0) {
cleanup_tempfile_(tempfile, tempfilename);
*status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_UNLINK_ERROR;
return false;
}
#endif
/*@@@ to fully support the tempfile_path_prefix we need to update this piece to actually copy across filesystems instead of just flac_rename(): */
if(0 != flac_rename(*tempfilename, filename)) {
cleanup_tempfile_(tempfile, tempfilename);
*status = FLAC__METADATA_SIMPLE_ITERATOR_STATUS_RENAME_ERROR;
return false;
}
cleanup_tempfile_(tempfile, tempfilename);
return true;
}
void cleanup_tempfile_(FILE **tempfile, char **tempfilename)
{
if(0 != *tempfile) {
(void)fclose(*tempfile);
*tempfile = 0;
}
if(0 != *tempfilename) {
(void)flac_unlink(*tempfilename);
free(*tempfilename);
*tempfilename = 0;
}
}
FLAC__bool get_file_stats_(const char *filename, struct flac_stat_s *stats)
{
FLAC__ASSERT(0 != filename);
FLAC__ASSERT(0 != stats);
return (0 == flac_stat(filename, stats));
}
void set_file_stats_(const char *filename, struct flac_stat_s *stats)
{
struct utimbuf srctime;
FLAC__ASSERT(0 != filename);
FLAC__ASSERT(0 != stats);
srctime.actime = stats->st_atime;
srctime.modtime = stats->st_mtime;
(void)flac_chmod(filename, stats->st_mode);
(void)flac_utime(filename, &srctime);
#if !defined _MSC_VER && !defined __BORLANDC__ && !defined __MINGW32__
FLAC_CHECK_RETURN(chown(filename, stats->st_uid, -1));
FLAC_CHECK_RETURN(chown(filename, -1, stats->st_gid));
#endif
}
int fseek_wrapper_(FLAC__IOHandle handle, FLAC__int64 offset, int whence)
{
return fseeko((FILE*)handle, (FLAC__off_t)offset, whence);
}
FLAC__int64 ftell_wrapper_(FLAC__IOHandle handle)
{
return ftello((FILE*)handle);
}
FLAC__Metadata_ChainStatus get_equivalent_status_(FLAC__Metadata_SimpleIteratorStatus status)
{
switch(status) {
case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_OK:
return FLAC__METADATA_CHAIN_STATUS_OK;
case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ILLEGAL_INPUT:
return FLAC__METADATA_CHAIN_STATUS_ILLEGAL_INPUT;
case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_ERROR_OPENING_FILE:
return FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE;
case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_A_FLAC_FILE:
return FLAC__METADATA_CHAIN_STATUS_NOT_A_FLAC_FILE;
case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_NOT_WRITABLE:
return FLAC__METADATA_CHAIN_STATUS_NOT_WRITABLE;
case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_BAD_METADATA:
return FLAC__METADATA_CHAIN_STATUS_BAD_METADATA;
case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_READ_ERROR:
return FLAC__METADATA_CHAIN_STATUS_READ_ERROR;
case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_SEEK_ERROR:
return FLAC__METADATA_CHAIN_STATUS_SEEK_ERROR;
case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_WRITE_ERROR:
return FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR;
case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_RENAME_ERROR:
return FLAC__METADATA_CHAIN_STATUS_RENAME_ERROR;
case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_UNLINK_ERROR:
return FLAC__METADATA_CHAIN_STATUS_UNLINK_ERROR;
case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_MEMORY_ALLOCATION_ERROR:
return FLAC__METADATA_CHAIN_STATUS_MEMORY_ALLOCATION_ERROR;
case FLAC__METADATA_SIMPLE_ITERATOR_STATUS_INTERNAL_ERROR:
default:
return FLAC__METADATA_CHAIN_STATUS_INTERNAL_ERROR;
}
}
|