-
Notifications
You must be signed in to change notification settings - Fork 317
/
Copy pathTag.cs
2557 lines (2255 loc) · 78.5 KB
/
Tag.cs
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
//
// Tag.cs: Provide support for reading and writing ID3v2 tags.
//
// Authors:
// Brian Nickel ([email protected])
// Gabriel BUrt ([email protected])
//
// Original Source:
// id3v2tag.cpp from TagLib
//
// Copyright (C) 2010 Novell, Inc.
// Copyright (C) 2005-2007 Brian Nickel
// Copyright (C) 2002,2003 Scott Wheeler (Original Implementation)
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License version
// 2.1 as published by the Free Software Foundation.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
// USA
//
using System;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
namespace TagLib.Id3v2
{
/// <summary>
/// This class extends <see cref="TagLib.Tag" /> and implements <see
/// cref="T:System.Collections.Generic.IEnumerable`1" /> to provide support for reading and
/// writing ID3v2 tags.
/// </summary>
public class Tag : TagLib.Tag, IEnumerable<Frame>, ICloneable
{
#region Private Static Fields
/// <summary>
/// Contains the language to use for language specific
/// fields.
/// </summary>
static string language = CultureInfo.CurrentCulture.ThreeLetterISOLanguageName;
/// <summary>
/// Contains the field to use for new tags.
/// </summary>
static byte default_version = 3;
#endregion
#region Private Fields
/// <summary>
/// Contains the tag's header.
/// </summary>
Header header;
/// <summary>
/// Contains the tag's extended header.
/// </summary>
ExtendedHeader extended_header;
/// <summary>
/// Contains the tag's frames.
/// </summary>
readonly List<Frame> frame_list = new List<Frame> ();
/// <summary>
/// Store the PerformersRole property
/// </summary>
string[] performers_role;
#endregion
#region Constructors
/// <summary>
/// Constructs and initializes a new instance of <see
/// cref="Tag" /> with no contents.
/// </summary>
public Tag ()
{
}
/// <summary>
/// Constructs and initializes a new instance of <see
/// cref="Tag" /> by reading the contents from a specified
/// position in a specified file.
/// </summary>
/// <param name="file">
/// A <see cref="File" /> object containing the file from
/// which the contents of the new instance is to be read.
/// </param>
/// <param name="position">
/// A <see cref="long" /> value specify at what position to
/// read the tag.
/// </param>
/// <param name="style">
/// A <see cref="ReadStyle"/> value specifying how the media
/// data is to be read into the current instance.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="file" /> is <see langword="null" />.
/// </exception>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="position" /> is less than zero or greater
/// than the size of the file.
/// </exception>
public Tag (File file, long position, ReadStyle style)
{
if (file == null)
throw new ArgumentNullException (nameof (file));
file.Mode = File.AccessMode.Read;
if (position < 0 || position > file.Length - Header.Size)
throw new ArgumentOutOfRangeException (nameof (position));
Read (file, position, style);
}
/// <summary>
/// Constructs and initializes a new instance of <see
/// cref="Tag" /> by reading the contents from a specified
/// <see cref="ByteVector" /> object.
/// </summary>
/// <param name="data">
/// A <see cref="ByteVector" /> object to read the tag from.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="data" /> is <see langword="null" />.
/// </exception>
/// <exception cref="CorruptFileException">
/// <paramref name="data" /> does not contain enough data.
/// </exception>
public Tag (ByteVector data)
{
if (data == null)
throw new ArgumentNullException (nameof (data));
if (data.Count < Header.Size)
throw new CorruptFileException ("Does not contain enough header data.");
header = new Header (data);
// If the tag size is 0, then this is an invalid tag.
// Tags must contain at least one frame.
if (header.TagSize == 0)
return;
if (data.Count - Header.Size < header.TagSize)
throw new CorruptFileException ("Does not contain enough tag data.");
Parse (data.Mid ((int)Header.Size, (int)header.TagSize), null, 0, ReadStyle.None);
}
#endregion
#region Public Methods
/// <summary>
/// Gets the text value from a specified Text Information
/// Frame.
/// </summary>
/// <param name="ident">
/// A <see cref="ByteVector" /> object containing the frame
/// identifier of the Text Information Frame to get the value
/// from.
/// </param>
/// <returns>
/// A <see cref="string" /> object containing the text of the
/// specified frame, or <see langword="null" /> if no value
/// was found.
/// </returns>
public string GetTextAsString (ByteVector ident)
{
Frame frame;
// Handle URL LInk frames differently
if (ident[0] == 'W')
frame = UrlLinkFrame.Get (this, ident, false);
else
frame = TextInformationFrame.Get (this, ident, false);
string result = frame?.ToString ();
return string.IsNullOrEmpty (result) ? null : result;
}
/// <summary>
/// Gets all frames contained in the current instance.
/// </summary>
/// <returns>
/// A <see cref="T:System.Collections.Generic.IEnumerable`1" /> object enumerating
/// through the frames.
/// </returns>
public IEnumerable<Frame> GetFrames ()
{
return frame_list;
}
/// <summary>
/// Gets all frames with a specified identifier contained in
/// the current instance.
/// </summary>
/// <param name="ident">
/// A <see cref="ByteVector" /> object containing the
/// identifier of the frames to return.
/// </param>
/// <returns>
/// A <see cref="T:System.Collections.Generic.IEnumerable`1" /> object enumerating
/// through the frames.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="ident" /> is <see langword="null" />.
/// </exception>
/// <exception cref="ArgumentException">
/// <paramref name="ident" /> is not exactly four bytes long.
/// </exception>
public IEnumerable<Frame> GetFrames (ByteVector ident)
{
if (ident == null)
throw new ArgumentNullException (nameof (ident));
if (ident.Count != 4)
throw new ArgumentException ("Identifier must be four bytes long.", nameof (ident));
foreach (Frame f in frame_list)
if (f.FrameId.Equals (ident))
yield return f;
}
/// <summary>
/// Gets all frames with of a specified type contained in
/// the current instance.
/// </summary>
/// <typeparam name="T">
/// The type of object, derived from <see cref="Frame" />,
/// to return from in the current instance.
/// </typeparam>
/// <returns>
/// A <see cref="T:System.Collections.Generic.IEnumerable`1" /> object enumerating
/// through the frames.
/// </returns>
public IEnumerable<T> GetFrames<T> () where T : Frame
{
foreach (Frame f in frame_list) {
if (f is T tf)
yield return tf;
}
}
/// <summary>
/// Gets all frames with a of type <typeparamref name="T" />
/// with a specified identifier contained in the current
/// instance.
/// </summary>
/// <typeparam name="T">
/// The type of object, derived from <see cref="Frame" />,
/// to return from in the current instance.
/// </typeparam>
/// <param name="ident">
/// A <see cref="ByteVector" /> object containing the
/// identifier of the frames to return.
/// </param>
/// <returns>
/// A <see cref="T:System.Collections.Generic.IEnumerable`1" /> object enumerating
/// through the frames.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="ident" /> is <see langword="null" />.
/// </exception>
/// <exception cref="ArgumentException">
/// <paramref name="ident" /> is not exactly four bytes long.
/// </exception>
public IEnumerable<T> GetFrames<T> (ByteVector ident)
where T : Frame
{
if (ident == null)
throw new ArgumentNullException (nameof (ident));
if (ident.Count != 4)
throw new ArgumentException ("Identifier must be four bytes long.", nameof (ident));
foreach (Frame f in frame_list) {
if (f is T tf && f.FrameId.Equals (ident))
yield return tf;
}
}
/// <summary>
/// Adds a frame to the current instance.
/// </summary>
/// <param name="frame">
/// A <see cref="Frame" /> object to add to the current
/// instance.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="frame" /> is <see langword="null" />.
/// </exception>
public void AddFrame (Frame frame)
{
if (frame == null)
throw new ArgumentNullException (nameof (frame));
frame_list.Add (frame);
}
/// <summary>
/// Replaces an existing frame with a new one in the list
/// contained in the current instance, or adds a new one if
/// the existing one is not contained.
/// </summary>
/// <param name="oldFrame">
/// A <see cref="Frame" /> object to be replaced.
/// </param>
/// <param name="newFrame">
/// A <see cref="Frame" /> object to add to the current
/// instance.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="oldFrame" /> or <paramref name="newFrame"
/// /> is <see langword="null" />.
/// </exception>
public void ReplaceFrame (Frame oldFrame, Frame newFrame)
{
if (oldFrame == null)
throw new ArgumentNullException (nameof (oldFrame));
if (newFrame == null)
throw new ArgumentNullException (nameof (newFrame));
if (oldFrame == newFrame)
return;
int i = frame_list.IndexOf (oldFrame);
if (i >= 0)
frame_list[i] = newFrame;
else
frame_list.Add (newFrame);
}
/// <summary>
/// Removes a specified frame from the current instance.
/// </summary>
/// <param name="frame">
/// A <see cref="Frame" /> object to remove from the current
/// instance.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="frame" /> is <see langword="null" />.
/// </exception>
public void RemoveFrame (Frame frame)
{
if (frame == null)
throw new ArgumentNullException (nameof (frame));
if (frame_list.Contains (frame))
frame_list.Remove (frame);
}
/// <summary>
/// Removes all frames with a specified identifier from the
/// current instance.
/// </summary>
/// <param name="ident">
/// A <see cref="ByteVector" /> object containing the
/// identifier of the frames to remove.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="ident" /> is <see langword="null" />.
/// </exception>
/// <exception cref="ArgumentException">
/// <paramref name="ident" /> is not exactly four bytes long.
/// </exception>
public void RemoveFrames (ByteVector ident)
{
if (ident == null)
throw new ArgumentNullException (nameof (ident));
if (ident.Count != 4)
throw new ArgumentException ("Identifier must be four bytes long.", nameof (ident));
for (int i = frame_list.Count - 1; i >= 0; i--)
if (frame_list[i].FrameId.Equals (ident))
frame_list.RemoveAt (i);
}
/// <summary>
/// Sets the text for a specified Text Information Frame.
/// </summary>
/// <param name="ident">
/// A <see cref="ByteVector" /> object containing the
/// identifier of the frame to set the data for.
/// </param>
/// <param name="text">
/// A <see cref="T:string[]" /> containing the text to set for
/// the specified frame, or <see langword="null" /> to unset
/// the value.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="ident" /> is <see langword="null" />.
/// </exception>
/// <exception cref="ArgumentException">
/// <paramref name="ident" /> is not exactly four bytes long.
/// </exception>
public void SetTextFrame (ByteVector ident, params string[] text)
{
if (ident == null)
throw new ArgumentNullException (nameof (ident));
if (ident.Count != 4)
throw new ArgumentException ("Identifier must be four bytes long.",
nameof (ident));
bool empty = true;
if (text != null)
for (int i = 0; empty && i < text.Length; i++)
if (!string.IsNullOrEmpty (text[i]))
empty = false;
if (empty) {
RemoveFrames (ident);
return;
}
// Handle URL Link frames differently
if (ident[0] == 'W') {
var urlFrame = UrlLinkFrame.Get (this, ident, true);
urlFrame.Text = text;
urlFrame.TextEncoding = DefaultEncoding;
return;
}
var frame = TextInformationFrame.Get (this, ident, true);
frame.Text = text;
frame.TextEncoding = DefaultEncoding;
}
/// <summary>
/// Sets the text for a specified Text Information Frame.
/// </summary>
/// <param name="ident">
/// A <see cref="ByteVector" /> object containing the
/// identifier of the frame to set the data for.
/// </param>
/// <param name="text">
/// A <see cref="StringCollection" /> object containing the
/// text to set for the specified frame, or <see
/// langword="null" /> to unset the value.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="ident" /> is <see langword="null" />.
/// </exception>
/// <exception cref="ArgumentException">
/// <paramref name="ident" /> is not exactly four bytes long.
/// </exception>
[Obsolete ("Use SetTextFrame(ByteVector,String[])")]
public void SetTextFrame (ByteVector ident, StringCollection text)
{
if (text == null || text.Count == 0)
RemoveFrames (ident);
else
SetTextFrame (ident, text.ToArray ());
}
/// <summary>
/// Sets the numeric values for a specified Text Information
/// Frame.
/// </summary>
/// <param name="ident">
/// A <see cref="ByteVector" /> object containing the
/// identifier of the frame to set the data for.
/// </param>
/// <param name="number">
/// A <see cref="uint" /> value containing the number to
/// store.
/// </param>
/// <param name="count">
/// A <see cref="uint" /> value representing a total which
/// <paramref name="number" /> is a part of, or zero if
/// <paramref name="number" /> is not part of a set.
/// </param>
/// <param name="format">
/// A <see cref="string" /> value representing the format
/// to be used to repreesent the <paramref name="number"/>.
/// Default: simple decimal number ("0").
/// </param>
/// <remarks>
/// If both <paramref name="number" /> and <paramref
/// name="count" /> are equal to zero, the value will be
/// cleared. If <paramref name="count" /> is zero, <paramref
/// name="number" /> by itself will be stored. Otherwise, the
/// values will be stored as "<paramref name="number"
/// />/<paramref name="count" />".
/// </remarks>
/// <exception cref="ArgumentNullException">
/// <paramref name="ident" /> is <see langword="null" />.
/// </exception>
/// <exception cref="ArgumentException">
/// <paramref name="ident" /> is not exactly four bytes long.
/// </exception>
public void SetNumberFrame (ByteVector ident, uint number, uint count, string format = "0")
{
if (ident == null)
throw new ArgumentNullException (nameof (ident));
if (ident.Count != 4)
throw new ArgumentException ("Identifier must be four bytes long.", nameof (ident));
if (number == 0 && count == 0) {
RemoveFrames (ident);
} else if (count != 0) {
SetTextFrame (ident, string.Format (
CultureInfo.InvariantCulture, "{0:" + format + "}/{1}",
number, count));
} else {
SetTextFrame (ident, number.ToString (format, CultureInfo.InvariantCulture));
}
}
/// <summary>
/// Renders the current instance as a raw ID3v2 tag.
/// </summary>
/// <returns>
/// A <see cref="ByteVector" /> object containing the
/// rendered tag.
/// </returns>
/// <remarks>
/// By default, tags will be rendered in the version they
/// were loaded in, and new tags using the version specified
/// by <see cref="DefaultVersion" />. If <see
/// cref="ForceDefaultVersion" /> is <see langword="true" />,
/// all tags will be rendered in using the version specified
/// by <see cref="DefaultVersion" />, except for tags with
/// footers, which must be in version 4.
/// </remarks>
public ByteVector Render ()
{
// Convert the PerformersRole to the TMCL Tag
string[] ret = null;
if (performers_role != null) {
var map = new Dictionary<string, string> ();
for (int i = 0; i < performers_role.Length; i++) {
var insts = performers_role[i];
if (string.IsNullOrEmpty (insts))
continue;
var instlist = insts.Split (';');
foreach (var iinst in instlist) {
var inst = iinst.Trim ();
if (i < Performers.Length) {
var perf = Performers[i];
if (map.ContainsKey (inst)) {
map[inst] += ", " + perf;
} else {
map.Add (inst, perf);
}
}
}
}
// Convert dictionary to array
ret = new string[map.Count * 2];
int j = 0;
foreach (var dict in map) {
ret[j++] = dict.Key;
ret[j++] = dict.Value;
}
}
SetTextFrame (FrameType.TMCL, ret);
// We need to render the "tag data" first so that we
// have to correct size to render in the tag's header.
// The "tag data" (everything that is included in
// Header.TagSize) includes the extended header, frames
// and padding, but does not include the tag's header or
// footer.
bool has_footer = (header.Flags &
HeaderFlags.FooterPresent) != 0;
bool unsynchAtFrameLevel = (header.Flags & HeaderFlags.Unsynchronisation) != 0 && Version >= 4;
bool unsynchAtTagLevel = (header.Flags & HeaderFlags.Unsynchronisation) != 0 && Version < 4;
header.MajorVersion = has_footer ? (byte)4 : Version;
var tag_data = new ByteVector ();
// TODO: Render the extended header.
header.Flags &= ~HeaderFlags.ExtendedHeader;
// Loop through the frames rendering them and adding
// them to the tag_data.
foreach (Frame frame in frame_list) {
if (unsynchAtFrameLevel)
frame.Flags |= FrameFlags.Unsynchronisation;
if ((frame.Flags &
FrameFlags.TagAlterPreservation) != 0)
continue;
try {
tag_data.Add (frame.Render (header.MajorVersion));
} catch (NotImplementedException) {
}
}
// Add unsyncronization bytes if necessary.
if (unsynchAtTagLevel)
SynchData.UnsynchByteVector (tag_data);
// Compute the amount of padding, and append that to
// tag_data.
if (!has_footer)
tag_data.Add (new ByteVector ((int)
((tag_data.Count < header.TagSize) ?
(header.TagSize - tag_data.Count) :
1024)));
// Set the tag size.
header.TagSize = (uint)tag_data.Count;
tag_data.Insert (0, header.Render ());
if (has_footer)
tag_data.Add (new Footer (header).Render ());
return tag_data;
}
#endregion
#region Public Properties
/// <summary>
/// Gets and sets the header flags applied to the current
/// instance.
/// </summary>
/// <value>
/// A bitwise combined <see cref="HeaderFlags" /> value
/// containing flags applied to the current instance.
/// </value>
public HeaderFlags Flags {
get { return header.Flags; }
set { header.Flags = value; }
}
/// <summary>
/// Gets and sets the ID3v2 version of the current instance.
/// </summary>
/// <value>
/// A <see cref="byte" /> value specifying the ID3v2 version
/// of the current instance.
/// </value>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="value" /> is less than 2 or more than 4.
/// </exception>
public byte Version {
get {
return ForceDefaultVersion ? DefaultVersion : header.MajorVersion;
}
set {
if (value < 2 || value > 4)
throw new ArgumentOutOfRangeException (nameof (value), "Version must be 2, 3, or 4");
header.MajorVersion = value;
}
}
#endregion
#region Public Static Properties
/// <summary>
/// Gets and sets the ISO-639-2 language code to use when
/// searching for and storing language specific values.
/// </summary>
/// <value>
/// A <see cref="string" /> object containing an ISO-639-2
/// language code fto use when searching for and storing
/// language specific values.
/// </value>
/// <remarks>
/// If the language is unknown, " " is the appropriate
/// filler.
/// </remarks>
public static string Language {
get { return language; }
set {
language = (value == null || value.Length < 3) ? " " : value.Substring (0, 3);
}
}
/// <summary>
/// Gets and sets the the default version to use when
/// creating new tags.
/// </summary>
/// <value>
/// A <see cref="byte" /> value specifying the default ID3v2
/// version. The default version for this library is 3.
/// </value>
/// <remarks>
/// If <see cref="ForceDefaultVersion" /> is <see
/// langword="true" />, all tags will be rendered with this
/// version.
/// </remarks>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="value" /> is less than 2 or more than 4.
/// </exception>
public static byte DefaultVersion {
get { return default_version; }
set {
if (value < 2 || value > 4)
throw new ArgumentOutOfRangeException (nameof (value), "Version must be 2, 3, or 4");
default_version = value;
}
}
/// <summary>
/// Gets and sets whether or not to save all tags in the
/// default version rather than their original version.
/// </summary>
/// <value>
/// If <see langword="true"/>, tags will be saved in
/// <see cref="DefaultVersion" /> rather than their original
/// format, with the exception of tags with footers, which
/// will be saved in version 4.
/// </value>
public static bool ForceDefaultVersion { get; set; } = false;
/// <summary>
/// Gets and sets the encoding to use when creating new
/// frames.
/// </summary>
/// <value>
/// A <see cref="StringType" /> value specifying the encoding
/// to use when creating new frames.
/// </value>
public static StringType DefaultEncoding { get; set; } = StringType.UTF8;
/// <summary>
/// Gets and sets whether or not to render all frames with
/// the default encoding rather than their original encoding.
/// </summary>
/// <value>
/// If <see langword="true"/>, fames will be rendered in
/// <see cref="DefaultEncoding" /> rather than their original
/// encoding.
/// </value>
public static bool ForceDefaultEncoding { get; set; } = false;
/// <summary>
/// Gets and sets whether or not to use ID3v1 style numeric
/// genres when possible.
/// </summary>
/// <value>
/// A <see cref="bool" /> value specifying whether or not to
/// use genres with numeric values when possible.
/// </value>
/// <remarks>
/// If <see langword="true" />, TagLib# will try looking up
/// the numeric genre code when storing the value. For
/// ID3v2.2 and ID3v2.3, "Rock" would be stored as "(17)" and
/// for ID3v2.4 it would be stored as "17".
/// </remarks>
public static bool UseNumericGenres { get; set; } = true;
#endregion
#region Protected Methods
/// <summary>
/// Populates the current instance be reading in a tag from
/// a specified position in a specified file.
/// </summary>
/// <param name="file">
/// A <see cref="File" /> object to read the tag from.
/// </param>
/// <param name="position">
/// A <see cref="long" /> value specifying the seek position
/// at which to read the tag.
/// </param>
/// <param name="style">
/// A <see cref="ReadStyle"/> value specifying how the media
/// data is to be read into the current instance.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="file" /> is <see langword="null" />.
/// </exception>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="position" /> is less than 0 or greater
/// than the size of the file.
/// </exception>
protected void Read (File file, long position, ReadStyle style)
{
if (file == null)
throw new ArgumentNullException (nameof (file));
file.Mode = File.AccessMode.Read;
if (position < 0 || position > file.Length - Header.Size)
throw new ArgumentOutOfRangeException (nameof (position));
file.Seek (position);
header = new Header (file.ReadBlock ((int)Header.Size));
// If the tag size is 0, then this is an invalid tag.
// Tags must contain at least one frame.
if (header.TagSize == 0)
return;
position += Header.Size;
Parse (null, file, position, style);
}
/// <summary>
/// Populates the current instance by parsing the contents of
/// a raw ID3v2 tag, minus the header.
/// </summary>
/// <param name="data">
/// A <see cref="ByteVector" /> object containing the content
/// of an ID3v2 tag, minus the header.
/// </param>
/// <param name="file">
/// A <see cref="File"/> object containing
/// abstraction of the file to read.
/// Ignored if <paramref name="data"/> is not null.
/// </param>
/// <param name="position">
/// A <see cref="int" /> value reference specifying at what
/// index in <paramref name="file" />
/// at which the frame begins.
/// </param>
/// <param name="style">
/// A <see cref="ReadStyle"/> value specifying how the media
/// data is to be read into the current instance.
/// </param>
/// <remarks>
/// This method must only be called after the internal
/// header has been read from the file, otherwise the data
/// cannot be parsed correctly.
/// </remarks>
protected void Parse (ByteVector data, File file, long position, ReadStyle style)
{
// If the entire tag is marked as unsynchronized, and this tag
// is version id3v2.3 or lower, resynchronize it.
bool fullTagUnsynch = (header.MajorVersion < 4) &&
(header.Flags & HeaderFlags.Unsynchronisation) != 0;
// Avoid to load all the ID3 tag if PictureLazy enabled and size is
// significant enough (ID3v4 and later only)
if (data == null &&
(fullTagUnsynch ||
header.TagSize < 1024 ||
(style & ReadStyle.PictureLazy) == 0 ||
(header.Flags & HeaderFlags.ExtendedHeader) != 0)) {
file.Seek (position);
data = file.ReadBlock ((int)header.TagSize);
}
if (fullTagUnsynch)
SynchData.ResynchByteVector (data);
int frame_data_position = data != null ? 0 : (int)position;
int frame_data_endposition = (data != null ? data.Count : (int)header.TagSize)
+ frame_data_position - (int)FrameHeader.Size (header.MajorVersion);
// Check for the extended header (ID3v2 only)
if ((header.Flags & HeaderFlags.ExtendedHeader) != 0) {
extended_header = new ExtendedHeader (data, header.MajorVersion);
if (extended_header.Size <= data.Count) {
frame_data_position += (int)extended_header.Size;
frame_data_endposition -= (int)extended_header.Size;
}
}
// Parse the frames. TDRC, TDAT, and TIME will be needed
// for post-processing, so check for them as they are
// loaded.
TextInformationFrame tdrc = null;
TextInformationFrame tyer = null;
TextInformationFrame tdat = null;
TextInformationFrame time = null;
while (frame_data_position < frame_data_endposition) {
Frame frame;
try {
frame = FrameFactory.CreateFrame (data, file, ref frame_data_position, header.MajorVersion, fullTagUnsynch);
} catch (NotImplementedException) {
continue;
} catch (CorruptFileException) {
throw;
}
if (frame == null)
break;
// Only add frames that contain data.
if (frame.Size == 0)
continue;
AddFrame (frame);
// If the tag is version 4, no post-processing
// is needed.
if (header.MajorVersion == 4)
continue;
// Load up the first instance of each, for
// post-processing.
if (tdrc == null &&
frame.FrameId.Equals (FrameType.TDRC)) {
tdrc = frame as TextInformationFrame;
} else if (tyer == null &&
frame.FrameId.Equals (FrameType.TYER)) {
tyer = frame as TextInformationFrame;
} else if (tdat == null &&
frame.FrameId.Equals (FrameType.TDAT)) {
tdat = frame as TextInformationFrame;
} else if (time == null &&
frame.FrameId.Equals (FrameType.TIME)) {
time = frame as TextInformationFrame;
}
}
// Try to fill out the date/time of the TDRC frame. Can't do that if no TDRC
// frame exists, or if there is no TDAT frame, or if TDRC already has the date.
if (tdrc == null || tdat == null || tdrc.ToString ().Length > 4) {
return;
}
string year = tdrc.ToString ();
if (year.Length != 4)
return;
// Start with the year already in TDRC, then add the TDAT and TIME if available
var tdrc_text = new StringBuilder ();
tdrc_text.Append (year);
// Add the date
if (tdat != null) {
string tdat_text = tdat.ToString ();
if (tdat_text.Length == 4) {
tdrc_text.Append ("-").Append (tdat_text, 0, 2).Append ("-").Append (tdat_text, 2, 2);
// Add the time
if (time != null) {
string time_text = time.ToString ();
if (time_text.Length == 4)
tdrc_text.Append ("T").Append (time_text, 0, 2).Append (":").Append (time_text, 2, 2);
RemoveFrames (FrameType.TIME);
}
}
RemoveFrames (FrameType.TDAT);
}
tdrc.Text = new [] { tdrc_text.ToString () };
}