Debian Codegraph @

src/cddb/cddb.cpp

raw symbols SCIP
1 /* fre:ac - free audio converter
2 * Copyright (C) 2001-2022 Robert Kausch <[email protected]>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation, either version 2 of
7 * the License, or (at your option) any later version.
8 *
9 * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
10 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
11 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. */
12
13 #include <cddb/cddb.h>
14 #include <freac.h>
15 #include <config.h>
16
17 #include <boca.h>
18
19 using namespace BoCA;
20
21 static int cddb_sum(int n)
22 {
23 int ret = 0;
24
25 while (n > 0)
26 {
27 ret = ret + (n % 10);
28 n = n / 10;
29 }
30
31 return ret;
32 }
33
34 freac::CDDB::CDDB()
35 {
36 }
37
38 freac::CDDB::~CDDB()
39 {
40 ids.RemoveAll();
41 titles.RemoveAll();
42 categories.RemoveAll();
43 }
44
45 String freac::CDDB::DiscIDToString(UnsignedInt32 discID)
46 {
47 String discIDString = Number((Int64) discID).ToHexString(8);
48
49 return discIDString;
50 }
51
52 UnsignedInt32 freac::CDDB::StringToDiscID(const String &string)
53 {
54 return (Int64) Number::FromHexString(string);
55 }
56
57 UnsignedInt32 freac::CDDB::DiscIDFromMCDI(const MCDI &mcdi)
58 {
59 Int numTocEntries = mcdi.GetNumberOfEntries();
60 Int n = 0;
61
62 for (Int i = 0; i < numTocEntries; i++)
63 {
64 Int offset = mcdi.GetNthEntryOffset(i) + 150;
65
66 n += cddb_sum(offset / 75);
67 }
68
69 Int t = mcdi.GetNthEntryOffset(numTocEntries) / 75 - mcdi.GetNthEntryOffset(0) / 75;
70
71 return ((n % 0xff) << 24 | t << 8 | numTocEntries);
72 }
73
74 String freac::CDDB::QueryStringFromMCDI(const MCDI &mcdi)
75 {
76 Int numTocEntries = mcdi.GetNumberOfEntries();
77 String str = String("cddb query ").Append(DiscIDToString(DiscIDFromMCDI(mcdi)));
78
79 str.Append(" ").Append(String::FromInt(numTocEntries));
80
81 for (Int i = 0; i < numTocEntries; i++)
82 {
83 str.Append(" ").Append(String::FromInt(mcdi.GetNthEntryOffset(i) + 150));
84 }
85
86 str.Append(" ").Append(String::FromInt(mcdi.GetNthEntryOffset(numTocEntries) / 75 + 2));
87
88 return str;
89 }
90
91 UnsignedInt32 freac::CDDB::DiscIDFromOffsets(const String &offsets)
92 {
93 const Array<String> &values = offsets.Explode("+");
94
95 Int n = 0;
96
97 for (Int i = 1; i < values.Length() - 1; i++)
98 {
99 Int offset = (Int64) Number::FromHexString(values.GetNth(i));
100
101 n += cddb_sum(offset / 75);
102 }
103
104 Int t = (Int64) Number::FromHexString(values.GetLast()) / 75 - (Int64) Number::FromHexString(values.GetNth(1)) / 75;
105
106 return ((n % 0xff) << 24 | t << 8 | (values.Length() - 2));
107 }
108
109 String freac::CDDB::QueryStringFromOffsets(const String &offsets)
110 {
111 const Array<String> &values = offsets.Explode("+");
112
113 String str = String("cddb query ").Append(DiscIDToString(DiscIDFromOffsets(offsets)));
114
115 str.Append(" ").Append(String::FromInt(values.Length() - 2));
116
117 for (Int i = 1; i < values.Length() - 1; i++)
118 {
119 str.Append(" ").Append(String::FromInt((Int64) Number::FromHexString(values.GetNth(i))));
120 }
121
122 str.Append(" ").Append(String::FromInt((Int64) Number::FromHexString(values.GetLast()) / 75));
123
124 return str;
125 }
126
127 Bool freac::CDDB::UpdateEntry(CDDBInfo &cddbInfo)
128 {
129 if (ConnectToServer() == False) return False;
130
131 Int query = Query(cddbInfo.GetQueryString());
132
133 if (query == QUERY_RESULT_ERROR)
134 {
135 CloseConnection();
136
137 return False;
138 }
139
140 if (cddbInfo.revision == 0)
141 {
142 /* This is a new entry; see if it already exists in another category.
143 */
144 if (query == QUERY_RESULT_SINGLE || query == QUERY_RESULT_MULTIPLE)
145 {
146 cddbInfo.category = GetNthCategory(0);
147 }
148 }
149
150 if (query != QUERY_RESULT_FUZZY)
151 {
152 CDDBInfo revisionInfo;
153
154 while (Read(cddbInfo.category, cddbInfo.discID, revisionInfo))
155 {
156 Bool foundCollision = False;
157
158 for (Int i = 0; i < cddbInfo.trackOffsets.Length(); i++)
159 {
160 if (cddbInfo.trackOffsets.GetNth(i) != revisionInfo.trackOffsets.GetNth(i))
161 {
162 /* Found disc ID collision. Try next category.
163 */
164 if (cddbInfo.category == "rock") cddbInfo.category = "misc";
165 else if (cddbInfo.category == "misc") cddbInfo.category = "newage";
166 else if (cddbInfo.category == "newage") cddbInfo.category = "soundtrack";
167 else if (cddbInfo.category == "soundtrack") cddbInfo.category = "blues";
168 else if (cddbInfo.category == "blues") cddbInfo.category = "jazz";
169 else if (cddbInfo.category == "jazz") cddbInfo.category = "folk";
170 else if (cddbInfo.category == "folk") cddbInfo.category = "country";
171 else if (cddbInfo.category == "country") cddbInfo.category = "reggae";
172 else if (cddbInfo.category == "reggae") cddbInfo.category = "classical";
173 else if (cddbInfo.category == "classical") cddbInfo.category = "data";
174 else if (cddbInfo.category == "data") cddbInfo.category = "unknown";
175
176 foundCollision = True;
177
178 break;
179 }
180 }
181
182 if (!foundCollision)
183 {
184 /* Increment revision of old entry.
185 */
186 cddbInfo.revision = revisionInfo.revision + 1;
187
188 break;
189 }
190 }
191 }
192
193 CloseConnection();
194
195 return True;
196 }
197
198 /* Format a complete CDDB record according
199 * to the rules from the freedb how-to.
200 */
201 String freac::CDDB::FormatCDDBRecord(const CDDBInfo &cddbInfo)
202 {
203 String content;
204
205 content.Append("# xmcd").Append("\n");
206 content.Append("# ").Append("\n");
207 content.Append("# Track frame offsets:").Append("\n");
208
209 foreach (Int trackOffset, cddbInfo.trackOffsets)
210 {
211 content.Append("# ").Append(String::FromInt(trackOffset)).Append("\n");
212 }
213
214 content.Append("# ").Append("\n");
215 content.Append("# Disc length: ").Append(String::FromInt(cddbInfo.discLength)).Append("\n");
216 content.Append("# ").Append("\n");
217 content.Append("# Revision: ").Append(String::FromInt(cddbInfo.revision)).Append("\n");
218 content.Append("# Submitted via: ").Append(freac::appName).Append(" ").Append(freac::cddbVersion).Append("\n");
219 content.Append("# ").Append("\n");
220
221 content.Append(FormatCDDBEntry("DISCID", DiscIDToString(cddbInfo.discID)));
222 content.Append(FormatCDDBEntry("DTITLE", String(cddbInfo.dArtist.Replace("\n", " ").Trim()).Append(" / ").Append(cddbInfo.dTitle.Replace("\n", " ").Trim())));
223 content.Append(FormatCDDBEntry("DYEAR", String::FromInt(cddbInfo.dYear)));
224 content.Append(FormatCDDBEntry("DGENRE", cddbInfo.dGenre.Replace("\n", " ").Trim()));
225
226 for (Int i = 0; i < cddbInfo.trackTitles.Length(); i++)
227 {
228 if (cddbInfo.dArtist == CDDBInfo::VariousArtistsID) content.Append(FormatCDDBEntry(String("TTITLE").Append(String::FromInt(i)), cddbInfo.trackArtists.GetNth(i).Replace("\n", " ").Trim().Append(" / ").Append(cddbInfo.trackTitles.GetNth(i).Replace("\n", " ").Trim())));
229 else content.Append(FormatCDDBEntry(String("TTITLE").Append(String::FromInt(i)), cddbInfo.trackTitles.GetNth(i).Replace("\n", " ").Trim()));
230 }
231
232 content.Append(FormatCDDBEntry("EXTD", cddbInfo.comment));
233
234 for (Int i = 0; i < cddbInfo.trackComments.Length(); i++)
235 {
236 content.Append(FormatCDDBEntry(String("EXTT").Append(String::FromInt(i)), cddbInfo.trackComments.GetNth(i)));
237 }
238
239 content.Append(FormatCDDBEntry("PLAYORDER", cddbInfo.playOrder.Replace("\n", " ").Trim()));
240
241 return content;
242 }
243
244 /* Parse a complete CDDB record and fill the
245 * given CDDBInfo structure.
246 */
247 Bool freac::CDDB::ParseCDDBRecord(const String &record, CDDBInfo &cddbInfo)
248 {
249 Bool sampler = True;
250 Int index = 0;
251
252 while (index < record.Length())
253 {
254 String line = ParseCDDBEntry(record, index);
255
256 if (line.StartsWith("DISCID"))
257 {
258 String discIDString;
259
260 for (Int l = 7; l < line.Length(); l++) discIDString[l - 7] = line[l];
261
262 cddbInfo.discID = StringToDiscID(discIDString);
263 }
264 else if (line.StartsWith("DTITLE"))
265 {
266 Int k;
267
268 for (k = 7; k < line.Length(); k++)
269 {
270 if (line[k] == ' ' && line[k + 1] == '/' && line[k + 2] == ' ') break;
271
272 cddbInfo.dArtist[k - 7] = line[k];
273 }
274
275 for (Int l = k + 3; l < line.Length(); l++) cddbInfo.dTitle[l - k - 3] = line[l];
276
277 if (cddbInfo.dTitle == NIL) cddbInfo.dTitle = cddbInfo.dArtist;
278
279 cddbInfo.dArtist = cddbInfo.dArtist.Replace("\n", " ").Trim();
280 cddbInfo.dTitle = cddbInfo.dTitle.Replace("\n", " ").Trim();
281
282 if (cddbInfo.dArtist.ToLower().Contains("various")) cddbInfo.dArtist = CDDBInfo::VariousArtistsID;
283
284 cddbInfo.oDArtist = cddbInfo.dArtist;
285 cddbInfo.oDTitle = cddbInfo.dTitle;
286 }
287 else if (line.StartsWith("DGENRE"))
288 {
289 for (Int l = 7; l < line.Length(); l++) cddbInfo.dGenre[l - 7] = line[l];
290
291 cddbInfo.dGenre = cddbInfo.dGenre.Replace("\n", " ").Trim();
292
293 cddbInfo.oDGenre = cddbInfo.dGenre;
294 }
295 else if (line.StartsWith("DYEAR"))
296 {
297 String year;
298
299 for (Int l = 6; l < line.Length(); l++) year[l - 6] = line[l];
300
301 cddbInfo.dYear = year.ToInt();
302 }
303 else if (line.StartsWith("TTITLE"))
304 {
305 Int k = line.Find("=");
306 Int track = line.SubString(6, k - 6).ToInt();
307
308 String artist;
309 String title;
310
311 if (line.Contains(" / "))
312 {
313 Int l;
314
315 for (l = k + 1; l < line.Length(); l++)
316 {
317 if (line[l] == ' ' && line[l + 1] == '/' && line[l + 2] == ' ') break;
318
319 artist[l - k - 1] = line[l];
320 }
321
322 for (Int m = l + 3; m < line.Length(); m++) title[m - l - 3] = line[m];
323
324 if (title == NIL) { title = artist; artist = NIL; }
325 }
326 else
327 {
328 for (Int l = k + 1; l < line.Length(); l++) title[l - k - 1] = line[l];
329
330 if (!title.ToLower().Contains("data")) sampler = False;
331 }
332
333 artist = artist.Replace("\n", " ").Trim();
334 title = title.Replace("\n", " ").Trim();
335
336 cddbInfo.trackArtists.Add(artist, track);
337 cddbInfo.trackTitles.Add(title, track);
338
339 cddbInfo.oTrackArtists.Add(artist, track);
340 cddbInfo.oTrackTitles.Add(title, track);
341 }
342 else if (line.StartsWith("EXTD"))
343 {
344 for (Int k = 5; k < line.Length(); k++) cddbInfo.comment[k - 5] = line[k];
345
346 cddbInfo.oComment = cddbInfo.comment;
347 }
348 else if (line.StartsWith("EXTT"))
349 {
350 Int k = line.Find("=");
351 Int track = line.SubString(4, k - 4).ToInt();
352
353 String comment;
354
355 for (Int l = k + 1; l < line.Length(); l++) comment[l - k - 1] = line[l];
356
357 cddbInfo.trackComments.Add(comment, track);
358
359 cddbInfo.oTrackComments.Add(comment, track);
360 }
361 else if (line.StartsWith("PLAYORDER"))
362 {
363 for (Int k = 10; k < line.Length(); k++) cddbInfo.playOrder[k - 10] = line[k];
364
365 cddbInfo.playOrder = cddbInfo.playOrder.Replace("\n", " ").Trim();
366 }
367 else if (line.StartsWith("# Revision: "))
368 {
369 String revision;
370
371 for (Int l = 12; l < line.Length(); l++) revision[l - 12] = line[l];
372
373 cddbInfo.revision = revision.ToInt();
374 }
375 else if (line.StartsWith("# Track frame offsets:"))
376 {
377 Int track = 0;
378
379 do
380 {
381 Int oldIndex = index;
382
383 line = ParseCDDBEntry(record, index);
384
385 if (line[0] == '#' && line.Length() <= 2) break;
386
387 Int firstDigit = 0;
388 String offset;
389
390 for (Int n = 2; n < line.Length(); n++)
391 {
392 if (line[n] != ' ' && line[n] != '\t')
393 {
394 firstDigit = n;
395
396 break;
397 }
398 }
399
400 for (Int l = firstDigit; l < line.Length(); l++) offset[l - firstDigit] = line[l];
401
402 if (offset.ToInt() == 0)
403 {
404 index = oldIndex;
405
406 break;
407 }
408
409 cddbInfo.trackOffsets.Add(offset.ToInt(), track++);
410 }
411 while (True);
412 }
413 else if (line.StartsWith("# Disc length: "))
414 {
415 String discLength;
416
417 for (Int l = 15; l < line.Length(); l++) discLength[l - 15] = line[l];
418
419 cddbInfo.discLength = discLength.ToInt();
420 }
421 else if (line.StartsWith("210 "))
422 {
423 String category;
424
425 for (Int l = 4; l < line.Length(); l++)
426 {
427 if (line[l] == ' ') break;
428
429 category[l - 4] = line[l];
430 }
431
432 cddbInfo.category = category;
433 }
434 }
435
436 /* Set disc artist to "Various" for sampler CDs.
437 */
438 if (sampler) cddbInfo.dArtist = CDDBInfo::VariousArtistsID;
439
440 return True;
441 }
442
443 /* Format a CDDB entry according to the rules
444 * from the freedb how-to. Replace special
445 * characters and split lines at 256 chars
446 * minus two to account for line breaks.
447 */
448 String freac::CDDB::FormatCDDBEntry(const String &entry, const String &value)
449 {
450 if (value == NIL) return String(entry).Append("=\n");
451
452 String result;
453 Int totalChars = value.Length();
454 Int lineChars = 256 - entry.Length() - 1 - 2;
455
456 for (Int i = 0; i < totalChars; )
457 {
458 String line = String(entry).Append("=");
459
460 for (Int c = 0; c < lineChars && i < totalChars; c++, i++)
461 {
462 if (value[i] == '\n' || value[i] == '\t' || value[i] == '\\')
463 {
464 if (c >= lineChars - 1) break;
465
466 if (value[i] == '\n') line.Append("\\n");
467 if (value[i] == '\t') line.Append("\\t");
468 if (value[i] == '\\') line.Append("\\\\");
469
470 c++;
471
472 continue;
473 }
474
475 line[line.Length()] = value[i];
476 }
477
478 result.Append(line).Append("\n");
479 }
480
481 return result;
482 }
483
484 /* Parse the next CDDB entry according to the
485 * rules from the freedb how-to. Substitute
486 * special character sequences and concatenate
487 * multiline entries.
488 */
489 String freac::CDDB::ParseCDDBEntry(const String &cddb, Int &index)
490 {
491 String result;
492 String entry;
493
494 while (index < cddb.Length())
495 {
496 if (cddb[index] == '=') break;
497 if (cddb[index] == '\n') { index++; return entry; }
498
499 entry[entry.Length()] = cddb[index++];
500 }
501
502 result.Append(entry).Append("=");
503
504 index -= entry.Length();
505
506 while (index < cddb.Length())
507 {
508 String line;
509 Int oldIndex = index;
510
511 while (index < cddb.Length())
512 {
513 if (cddb[index] == '\n') break;
514
515 if (cddb[index] == '\\')
516 {
517 if (cddb[index + 1] == 'n') line[line.Length()] = '\n';
518 if (cddb[index + 1] == 't') line[line.Length()] = '\t';
519 if (cddb[index + 1] == '\\') line[line.Length()] = '\\';
520
521 index += 2;
522 }
523 else
524 {
525 line[line.Length()] = cddb[index++];
526 }
527 }
528
529 if (!line.StartsWith(String(entry).Append("="))) { index = oldIndex; break; }
530
531 for (Int i = entry.Length() + 1; i < line.Length(); i++) result[result.Length()] = line[i];
532
533 index++;
534 }
535
536 return result;
537 }