Mercurial > hg > sidinfo
annotate sidlib.c @ 163:179ffe97599c
Implement support for the "new" MD5 format of SLDB.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Fri, 16 Feb 2018 22:07:19 +0200 |
parents | 7359170fbb7f |
children | e6c8a235ce2f |
rev | line source |
---|---|
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
1 /* |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
2 * SIDInfoLib - Way too simplistic PSID/RSID file library |
93 | 3 * Written by Matti 'ccr' Hämäläinen <ccr@tnsp.org> |
131 | 4 * (C) Copyright 2014-2017 Tecnic Software productions (TNSP) |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
5 */ |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
6 #include "sidlib.h" |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
7 #include "th_endian.h" |
85
4c0ecb078591
Rename various variables and functions and change relevant places to use the
Matti Hamalainen <ccr@tnsp.org>
parents:
77
diff
changeset
|
8 #include "th_string.h" |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
9 |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
10 |
103
ff6d1a9a2fde
Move definitions to start of the file.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
11 #define SIDLIB_DB_MAGIC "SIDLibDB" |
ff6d1a9a2fde
Move definitions to start of the file.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
12 #define SIDLIB_DB_VERSION 0x0100 |
ff6d1a9a2fde
Move definitions to start of the file.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
13 |
ff6d1a9a2fde
Move definitions to start of the file.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
14 |
ff6d1a9a2fde
Move definitions to start of the file.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
15 typedef struct |
ff6d1a9a2fde
Move definitions to start of the file.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
16 { |
ff6d1a9a2fde
Move definitions to start of the file.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
17 char magic[8]; // File magic ID |
ff6d1a9a2fde
Move definitions to start of the file.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
18 uint16_t version; // Version |
ff6d1a9a2fde
Move definitions to start of the file.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
19 uint32_t nnodes; // Number of song nodes |
ff6d1a9a2fde
Move definitions to start of the file.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
20 // struct { |
ff6d1a9a2fde
Move definitions to start of the file.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
21 // th_md5hash_t hash; // Hash for this SID file |
ff6d1a9a2fde
Move definitions to start of the file.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
22 // uint16_t nlengths; // Number of song (lengths) |
ff6d1a9a2fde
Move definitions to start of the file.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
23 // uint16_t length[nlengths]; // Length in seconds |
ff6d1a9a2fde
Move definitions to start of the file.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
24 // } * nnodes; |
ff6d1a9a2fde
Move definitions to start of the file.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
25 th_md5hash_t hash; |
ff6d1a9a2fde
Move definitions to start of the file.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
26 } PSIDLibHdr; |
ff6d1a9a2fde
Move definitions to start of the file.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
27 |
ff6d1a9a2fde
Move definitions to start of the file.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
28 |
128
2636185649c6
Add si_fread_str() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
126
diff
changeset
|
29 BOOL si_fread_str(th_ioctx *ctx, char **str, const size_t len) |
2636185649c6
Add si_fread_str() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
126
diff
changeset
|
30 { |
2636185649c6
Add si_fread_str() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
126
diff
changeset
|
31 char *tmp = th_malloc(len + 1); |
2636185649c6
Add si_fread_str() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
126
diff
changeset
|
32 if (tmp == NULL) |
2636185649c6
Add si_fread_str() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
126
diff
changeset
|
33 goto err; |
2636185649c6
Add si_fread_str() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
126
diff
changeset
|
34 |
2636185649c6
Add si_fread_str() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
126
diff
changeset
|
35 if (!thfread_str(ctx, tmp, len)) |
2636185649c6
Add si_fread_str() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
126
diff
changeset
|
36 goto err; |
2636185649c6
Add si_fread_str() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
126
diff
changeset
|
37 |
2636185649c6
Add si_fread_str() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
126
diff
changeset
|
38 tmp[len] = 0; |
2636185649c6
Add si_fread_str() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
126
diff
changeset
|
39 |
2636185649c6
Add si_fread_str() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
126
diff
changeset
|
40 *str = tmp; |
2636185649c6
Add si_fread_str() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
126
diff
changeset
|
41 |
2636185649c6
Add si_fread_str() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
126
diff
changeset
|
42 return TRUE; |
2636185649c6
Add si_fread_str() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
126
diff
changeset
|
43 |
2636185649c6
Add si_fread_str() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
126
diff
changeset
|
44 err: |
2636185649c6
Add si_fread_str() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
126
diff
changeset
|
45 th_free(tmp); |
2636185649c6
Add si_fread_str() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
126
diff
changeset
|
46 return FALSE; |
2636185649c6
Add si_fread_str() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
126
diff
changeset
|
47 } |
2636185649c6
Add si_fread_str() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
126
diff
changeset
|
48 |
2636185649c6
Add si_fread_str() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
126
diff
changeset
|
49 |
163
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
50 static BOOL si_read_hash_data(th_ioctx *ctx, PSIDHeader *psid, |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
51 th_md5state_t *state, BOOL first) |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
52 { |
163
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
53 uint8_t *data = NULL; |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
54 BOOL ret = FALSE; |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
55 size_t read; |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
56 |
85
4c0ecb078591
Rename various variables and functions and change relevant places to use the
Matti Hamalainen <ccr@tnsp.org>
parents:
77
diff
changeset
|
57 if ((data = (uint8_t *) th_malloc(PSID_BUFFER_SIZE)) == NULL) |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
58 { |
85
4c0ecb078591
Rename various variables and functions and change relevant places to use the
Matti Hamalainen <ccr@tnsp.org>
parents:
77
diff
changeset
|
59 th_io_error(ctx, THERR_MALLOC, |
4c0ecb078591
Rename various variables and functions and change relevant places to use the
Matti Hamalainen <ccr@tnsp.org>
parents:
77
diff
changeset
|
60 "Error allocating temporary data buffer of %d bytes.\n", |
77 | 61 PSID_BUFFER_SIZE); |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
62 goto error; |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
63 } |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
64 |
163
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
65 psid->dataSize = 0; |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
66 do |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
67 { |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
68 read = thfread(data, sizeof(uint8_t), PSID_BUFFER_SIZE, ctx); |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
69 psid->dataSize += read; |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
70 |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
71 // If load address is 0 in header and we have the first block, grab it |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
72 if (first && psid->loadAddress == 0) |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
73 { |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
74 if (read < 4) |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
75 { |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
76 th_io_error(ctx, THERR_FREAD, |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
77 "Error reading song data, unexpectedly small file.\n"); |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
78 goto error; |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
79 } |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
80 |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
81 // Grab the load address |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
82 psid->loadAddress = TH_LE16_TO_NATIVE(*(uint16_t *) data); |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
83 |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
84 // .. do not include the load address to the hash |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
85 th_md5_append(state, &data[2], read - 2); |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
86 first = FALSE; |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
87 } |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
88 else |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
89 if (read > 0) |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
90 { |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
91 // Append data "as is" |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
92 th_md5_append(state, data, read); |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
93 } |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
94 } while (read > 0 && !thfeof(ctx)); |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
95 |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
96 ret = TRUE; |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
97 |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
98 error: |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
99 th_free(data); |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
100 return ret; |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
101 } |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
102 |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
103 |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
104 BOOL si_read_sid_file(th_ioctx *ctx, PSIDHeader **ppsid, const BOOL newSLDB) |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
105 { |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
106 PSIDHeader *psid = NULL; |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
107 th_md5state_t state; |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
108 BOOL ret = FALSE; |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
109 off_t hdrStart, hdrEnd; |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
110 |
126
c1462b7880e8
Make si_read_sid_file() to allocate the PSIDHeader struct itself, adjust
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
111 if ((psid = *ppsid = th_malloc0(sizeof(PSIDHeader))) == NULL) |
c1462b7880e8
Make si_read_sid_file() to allocate the PSIDHeader struct itself, adjust
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
112 { |
c1462b7880e8
Make si_read_sid_file() to allocate the PSIDHeader struct itself, adjust
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
113 th_io_error(ctx, THERR_MALLOC, |
c1462b7880e8
Make si_read_sid_file() to allocate the PSIDHeader struct itself, adjust
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
114 "Error PSID context struct.\n"); |
c1462b7880e8
Make si_read_sid_file() to allocate the PSIDHeader struct itself, adjust
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
115 goto error; |
c1462b7880e8
Make si_read_sid_file() to allocate the PSIDHeader struct itself, adjust
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
116 } |
c1462b7880e8
Make si_read_sid_file() to allocate the PSIDHeader struct itself, adjust
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
117 |
163
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
118 hdrStart = thftell(ctx); |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
119 |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
120 // Read PSID header in |
85
4c0ecb078591
Rename various variables and functions and change relevant places to use the
Matti Hamalainen <ccr@tnsp.org>
parents:
77
diff
changeset
|
121 if (!thfread_str(ctx, (uint8_t *) psid->magic, PSID_MAGIC_LEN) || |
4c0ecb078591
Rename various variables and functions and change relevant places to use the
Matti Hamalainen <ccr@tnsp.org>
parents:
77
diff
changeset
|
122 !thfread_be16(ctx, &psid->version) || |
4c0ecb078591
Rename various variables and functions and change relevant places to use the
Matti Hamalainen <ccr@tnsp.org>
parents:
77
diff
changeset
|
123 !thfread_be16(ctx, &psid->dataOffset) || |
4c0ecb078591
Rename various variables and functions and change relevant places to use the
Matti Hamalainen <ccr@tnsp.org>
parents:
77
diff
changeset
|
124 !thfread_be16(ctx, &psid->loadAddress) || |
4c0ecb078591
Rename various variables and functions and change relevant places to use the
Matti Hamalainen <ccr@tnsp.org>
parents:
77
diff
changeset
|
125 !thfread_be16(ctx, &psid->initAddress) || |
4c0ecb078591
Rename various variables and functions and change relevant places to use the
Matti Hamalainen <ccr@tnsp.org>
parents:
77
diff
changeset
|
126 !thfread_be16(ctx, &psid->playAddress) || |
4c0ecb078591
Rename various variables and functions and change relevant places to use the
Matti Hamalainen <ccr@tnsp.org>
parents:
77
diff
changeset
|
127 !thfread_be16(ctx, &psid->nSongs) || |
4c0ecb078591
Rename various variables and functions and change relevant places to use the
Matti Hamalainen <ccr@tnsp.org>
parents:
77
diff
changeset
|
128 !thfread_be16(ctx, &psid->startSong) || |
4c0ecb078591
Rename various variables and functions and change relevant places to use the
Matti Hamalainen <ccr@tnsp.org>
parents:
77
diff
changeset
|
129 !thfread_be32(ctx, &psid->speed)) |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
130 { |
104
09ca9d9a8710
Change to match changed th_ioctx API.
Matti Hamalainen <ccr@tnsp.org>
parents:
103
diff
changeset
|
131 th_io_error(ctx, ctx->status, |
85
4c0ecb078591
Rename various variables and functions and change relevant places to use the
Matti Hamalainen <ccr@tnsp.org>
parents:
77
diff
changeset
|
132 "Could not read PSID/RSID header: %s.\n", |
104
09ca9d9a8710
Change to match changed th_ioctx API.
Matti Hamalainen <ccr@tnsp.org>
parents:
103
diff
changeset
|
133 th_error_str(ctx->status)); |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
134 goto error; |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
135 } |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
136 |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
137 psid->magic[PSID_MAGIC_LEN] = 0; |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
138 |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
139 if ((psid->magic[0] != 'R' && psid->magic[0] != 'P') || |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
140 psid->magic[1] != 'S' || psid->magic[2] != 'I' || psid->magic[3] != 'D' || |
133
9cfa0553e7f9
Add support for reading PSID/RSID v4 in sidlib.
Matti Hamalainen <ccr@tnsp.org>
parents:
132
diff
changeset
|
141 psid->version < 1 || psid->version > 4) |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
142 { |
85
4c0ecb078591
Rename various variables and functions and change relevant places to use the
Matti Hamalainen <ccr@tnsp.org>
parents:
77
diff
changeset
|
143 th_io_error(ctx, THERR_NOT_SUPPORTED, |
4c0ecb078591
Rename various variables and functions and change relevant places to use the
Matti Hamalainen <ccr@tnsp.org>
parents:
77
diff
changeset
|
144 "Not a supported PSID or RSID file.\n"); |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
145 goto error; |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
146 } |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
147 |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
148 psid->isRSID = psid->magic[0] == 'R'; |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
149 |
128
2636185649c6
Add si_fread_str() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
126
diff
changeset
|
150 if (!si_fread_str(ctx, &psid->sidName, PSID_STR_LEN) || |
2636185649c6
Add si_fread_str() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
126
diff
changeset
|
151 !si_fread_str(ctx, &psid->sidAuthor, PSID_STR_LEN) || |
2636185649c6
Add si_fread_str() helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
126
diff
changeset
|
152 !si_fread_str(ctx, &psid->sidCopyright, PSID_STR_LEN)) |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
153 { |
104
09ca9d9a8710
Change to match changed th_ioctx API.
Matti Hamalainen <ccr@tnsp.org>
parents:
103
diff
changeset
|
154 th_io_error(ctx, ctx->status, |
85
4c0ecb078591
Rename various variables and functions and change relevant places to use the
Matti Hamalainen <ccr@tnsp.org>
parents:
77
diff
changeset
|
155 "Error reading SID file header: %s.\n", |
104
09ca9d9a8710
Change to match changed th_ioctx API.
Matti Hamalainen <ccr@tnsp.org>
parents:
103
diff
changeset
|
156 th_error_str(ctx->status)); |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
157 goto error; |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
158 } |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
159 |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
160 // Check if we need to load PSIDv2NG header ... |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
161 if (psid->version >= 2) |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
162 { |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
163 // Yes, we need to |
85
4c0ecb078591
Rename various variables and functions and change relevant places to use the
Matti Hamalainen <ccr@tnsp.org>
parents:
77
diff
changeset
|
164 if (!thfread_be16(ctx, &psid->flags) || |
98
d9f38d657433
Match with th-libs API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
165 !thfread_u8(ctx, &psid->startPage) || |
d9f38d657433
Match with th-libs API change.
Matti Hamalainen <ccr@tnsp.org>
parents:
95
diff
changeset
|
166 !thfread_u8(ctx, &psid->pageLength) || |
133
9cfa0553e7f9
Add support for reading PSID/RSID v4 in sidlib.
Matti Hamalainen <ccr@tnsp.org>
parents:
132
diff
changeset
|
167 !thfread_u8(ctx, &psid->sid2Addr) || |
9cfa0553e7f9
Add support for reading PSID/RSID v4 in sidlib.
Matti Hamalainen <ccr@tnsp.org>
parents:
132
diff
changeset
|
168 !thfread_u8(ctx, &psid->sid3Addr)) |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
169 { |
104
09ca9d9a8710
Change to match changed th_ioctx API.
Matti Hamalainen <ccr@tnsp.org>
parents:
103
diff
changeset
|
170 th_io_error(ctx, ctx->status, |
85
4c0ecb078591
Rename various variables and functions and change relevant places to use the
Matti Hamalainen <ccr@tnsp.org>
parents:
77
diff
changeset
|
171 "Error reading PSID/RSID v2+ extra header data: %s.\n", |
104
09ca9d9a8710
Change to match changed th_ioctx API.
Matti Hamalainen <ccr@tnsp.org>
parents:
103
diff
changeset
|
172 th_error_str(ctx->status)); |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
173 goto error; |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
174 } |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
175 } |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
176 |
163
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
177 hdrEnd = thftell(ctx); |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
178 |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
179 // Initialize MD5-hash calculation |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
180 th_md5_init(&state); |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
181 |
163
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
182 if (newSLDB) |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
183 { |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
184 // New Songlengths.md5 style hash calculation: |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
185 // We just hash the whole file, so seek back to beginning .. |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
186 thfseek(ctx, hdrStart, SEEK_SET); |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
187 |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
188 if (!si_read_hash_data(ctx, psid, &state, FALSE)) |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
189 goto error; |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
190 |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
191 psid->dataSize -= hdrEnd - hdrStart; |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
192 } |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
193 else |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
194 { |
163
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
195 // "Old" Songlengths.txt style MD5 hash calculation |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
196 // We need to separately hash data etc. |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
197 if (!si_read_hash_data(ctx, psid, &state, TRUE)) |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
198 goto error; |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
199 |
163
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
200 // Append header data to hash |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
201 th_md5_append_le16(&state, psid->initAddress); |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
202 th_md5_append_le16(&state, psid->playAddress); |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
203 th_md5_append_le16(&state, psid->nSongs); |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
204 |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
205 // Append song speed data to hash |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
206 uint8_t tmp8 = psid->isRSID ? 60 : 0; |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
207 for (int index = 0; index < psid->nSongs && index < 32; index++) |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
208 { |
163
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
209 if (psid->isRSID) |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
210 tmp8 = 60; |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
211 else |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
212 tmp8 = (psid->speed & (1 << index)) ? 60 : 0; |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
213 |
163
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
214 th_md5_append(&state, &tmp8, sizeof(tmp8)); |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
215 } |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
216 |
163
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
217 // Rest of songs (more than 32) |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
218 for (int index = 32; index < psid->nSongs; index++) |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
219 th_md5_append(&state, &tmp8, sizeof(tmp8)); |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
220 |
163
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
221 // PSIDv2NG specific |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
222 if (psid->version >= 2) |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
223 { |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
224 // REFER TO SIDPLAY HEADERS FOR MORE INFORMATION |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
225 tmp8 = (psid->flags >> 2) & 3; |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
226 if (tmp8 == 2) |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
227 th_md5_append(&state, &tmp8, sizeof(tmp8)); |
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
228 } |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
229 } |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
230 |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
231 // Calculate the hash |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
232 th_md5_finish(&state, psid->hash); |
163
179ffe97599c
Implement support for the "new" MD5 format of SLDB.
Matti Hamalainen <ccr@tnsp.org>
parents:
162
diff
changeset
|
233 ret = TRUE; |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
234 |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
235 error: |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
236 // Free buffer |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
237 return ret; |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
238 } |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
239 |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
240 |
126
c1462b7880e8
Make si_read_sid_file() to allocate the PSIDHeader struct itself, adjust
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
241 void si_free_sid_file(PSIDHeader *psid) |
c1462b7880e8
Make si_read_sid_file() to allocate the PSIDHeader struct itself, adjust
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
242 { |
c1462b7880e8
Make si_read_sid_file() to allocate the PSIDHeader struct itself, adjust
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
243 if (psid != NULL) |
c1462b7880e8
Make si_read_sid_file() to allocate the PSIDHeader struct itself, adjust
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
244 { |
c1462b7880e8
Make si_read_sid_file() to allocate the PSIDHeader struct itself, adjust
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
245 th_free(psid->sidName); |
c1462b7880e8
Make si_read_sid_file() to allocate the PSIDHeader struct itself, adjust
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
246 th_free(psid->sidAuthor); |
c1462b7880e8
Make si_read_sid_file() to allocate the PSIDHeader struct itself, adjust
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
247 th_free(psid->sidCopyright); |
c1462b7880e8
Make si_read_sid_file() to allocate the PSIDHeader struct itself, adjust
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
248 } |
c1462b7880e8
Make si_read_sid_file() to allocate the PSIDHeader struct itself, adjust
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
249 } |
c1462b7880e8
Make si_read_sid_file() to allocate the PSIDHeader struct itself, adjust
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
250 |
c1462b7880e8
Make si_read_sid_file() to allocate the PSIDHeader struct itself, adjust
Matti Hamalainen <ccr@tnsp.org>
parents:
104
diff
changeset
|
251 |
85
4c0ecb078591
Rename various variables and functions and change relevant places to use the
Matti Hamalainen <ccr@tnsp.org>
parents:
77
diff
changeset
|
252 const char *si_get_sid_clock_str(const int flags) |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
253 { |
132
b3e034b8c4b9
Change how certain flags (for SID model and clock) are handled.
Matti Hamalainen <ccr@tnsp.org>
parents:
131
diff
changeset
|
254 switch (flags) |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
255 { |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
256 case PSF_CLOCK_UNKNOWN : return "Unknown"; |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
257 case PSF_CLOCK_PAL : return "PAL 50Hz"; |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
258 case PSF_CLOCK_NTSC : return "NTSC 60Hz"; |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
259 case PSF_CLOCK_ANY : return "PAL / NTSC"; |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
260 default : return "?"; |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
261 } |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
262 } |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
263 |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
264 |
85
4c0ecb078591
Rename various variables and functions and change relevant places to use the
Matti Hamalainen <ccr@tnsp.org>
parents:
77
diff
changeset
|
265 const char *si_get_sid_model_str(const int flags) |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
266 { |
132
b3e034b8c4b9
Change how certain flags (for SID model and clock) are handled.
Matti Hamalainen <ccr@tnsp.org>
parents:
131
diff
changeset
|
267 switch (flags) |
70
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
268 { |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
269 case PSF_MODEL_UNKNOWN : return "Unknown"; |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
270 case PSF_MODEL_MOS6581 : return "MOS6581"; |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
271 case PSF_MODEL_MOS8580 : return "MOS8580"; |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
272 case PSF_MODEL_ANY : return "MOS6581 / MOS8580"; |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
273 default : return "?"; |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
274 } |
4779bbec2f28
Split some functionality into sidlib.[ch].
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
275 } |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
276 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
277 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
278 // Free memory allocated for given SLDB node |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
279 // |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
280 static void si_sldb_node_free(SIDLibSLDBNode *node) |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
281 { |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
282 if (node) |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
283 { |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
284 th_free_r(&node->lengths); |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
285 th_free_r(&node); |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
286 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
287 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
288 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
289 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
290 // Insert given node to db linked list |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
291 // |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
292 static void si_sldb_node_insert(SIDLibSLDB *dbh, SIDLibSLDBNode *node) |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
293 { |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
294 if (dbh->nodes) |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
295 { |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
296 node->prev = dbh->nodes->prev; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
297 dbh->nodes->prev->next = node; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
298 dbh->nodes->prev = node; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
299 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
300 else |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
301 { |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
302 dbh->nodes = node; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
303 node->prev = node; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
304 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
305 node->next = NULL; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
306 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
307 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
308 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
309 // Parse a time-entry in SLDB format |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
310 // |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
311 static int si_sldb_get_value(const char *str, size_t *pos) |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
312 { |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
313 int result = 0; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
314 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
315 while (isdigit(str[*pos])) |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
316 result = (result * 10) + (str[(*pos)++] - '0'); |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
317 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
318 return result; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
319 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
320 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
321 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
322 static int si_sldb_gettime(const char *str, size_t *pos) |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
323 { |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
324 int result; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
325 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
326 // Check if it starts with a digit |
88
c5ff71d64e53
More work on sidlib SLDB code.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
327 if (th_isdigit(str[*pos])) |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
328 { |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
329 // Get minutes-field |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
330 result = si_sldb_get_value(str, pos) * 60; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
331 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
332 // Check the field separator char |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
333 if (str[*pos] == ':') |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
334 { |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
335 // Get seconds-field |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
336 (*pos)++; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
337 result += si_sldb_get_value(str, pos); |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
338 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
339 else |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
340 result = -2; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
341 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
342 else |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
343 result = -1; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
344 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
345 // Ignore and skip the possible attributes |
88
c5ff71d64e53
More work on sidlib SLDB code.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
346 while (str[*pos] && !th_isspace(str[*pos])) |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
347 (*pos)++; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
348 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
349 return result; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
350 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
351 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
352 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
353 // Parse one SLDB definition line, return SLDB node |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
354 // |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
355 SIDLibSLDBNode *si_sldb_parse_entry(th_ioctx *ctx, const char *line) |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
356 { |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
357 SIDLibSLDBNode *node = NULL; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
358 size_t pos, tmpLen, savePos; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
359 BOOL isOK; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
360 int i; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
361 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
362 // Allocate new node |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
363 node = (SIDLibSLDBNode *) th_malloc0(sizeof(SIDLibSLDBNode)); |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
364 if (node == NULL) |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
365 { |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
366 th_io_error(ctx, THERR_MALLOC, |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
367 "Error allocating new node.\n"); |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
368 return NULL; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
369 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
370 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
371 // Get hash value |
160 | 372 for (pos = 0, i = 0; i < TH_MD5HASH_LENGTH; i++, pos += 2) |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
373 { |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
374 unsigned int tmpu; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
375 sscanf(&line[pos], "%2x", &tmpu); |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
376 node->hash[i] = tmpu; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
377 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
378 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
379 // Get playtimes |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
380 th_findnext(line, &pos); |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
381 if (line[pos] != '=') |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
382 { |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
383 th_io_error(ctx, THERR_INVALID_DATA, |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
384 "'=' expected on column #%d.\n", pos); |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
385 goto error; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
386 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
387 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
388 // First playtime is after '=' |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
389 savePos = ++pos; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
390 tmpLen = strlen(line); |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
391 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
392 // Get number of sub-tune lengths |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
393 isOK = TRUE; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
394 while (pos < tmpLen && isOK) |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
395 { |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
396 th_findnext(line, &pos); |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
397 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
398 if (si_sldb_gettime(line, &pos) >= 0) |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
399 node->nlengths++; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
400 else |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
401 isOK = FALSE; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
402 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
403 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
404 // Allocate memory for lengths |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
405 if (node->nlengths == 0) |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
406 goto error; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
407 |
99 | 408 node->lengths = (int *) th_calloc(node->nlengths, sizeof(int)); |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
409 if (node->lengths == NULL) |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
410 { |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
411 th_io_error(ctx, THERR_MALLOC, |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
412 "Could not allocate memory for node.\n"); |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
413 goto error; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
414 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
415 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
416 // Read lengths in |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
417 for (i = 0, pos = savePos, isOK = TRUE; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
418 pos < tmpLen && i < node->nlengths && isOK; i++) |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
419 { |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
420 int l; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
421 th_findnext(line, &pos); |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
422 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
423 l = si_sldb_gettime(line, &pos); |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
424 if (l >= 0) |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
425 node->lengths[i] = l; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
426 else |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
427 isOK = FALSE; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
428 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
429 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
430 return node; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
431 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
432 error: |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
433 si_sldb_node_free(node); |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
434 return NULL; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
435 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
436 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
437 |
88
c5ff71d64e53
More work on sidlib SLDB code.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
438 SIDLibSLDB * si_sldb_new(void) |
c5ff71d64e53
More work on sidlib SLDB code.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
439 { |
c5ff71d64e53
More work on sidlib SLDB code.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
440 return (SIDLibSLDB *) th_malloc0(sizeof(SIDLibSLDB)); |
c5ff71d64e53
More work on sidlib SLDB code.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
441 } |
c5ff71d64e53
More work on sidlib SLDB code.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
442 |
c5ff71d64e53
More work on sidlib SLDB code.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
443 |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
444 // Read SLDB database to memory |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
445 // |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
446 int si_sldb_read(th_ioctx *ctx, SIDLibSLDB *dbh) |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
447 { |
88
c5ff71d64e53
More work on sidlib SLDB code.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
448 char *line = NULL; |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
449 |
88
c5ff71d64e53
More work on sidlib SLDB code.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
450 if ((line = th_malloc(PSID_BUFFER_SIZE)) == NULL) |
c5ff71d64e53
More work on sidlib SLDB code.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
451 { |
c5ff71d64e53
More work on sidlib SLDB code.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
452 th_io_error(ctx, THERR_MALLOC, |
c5ff71d64e53
More work on sidlib SLDB code.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
453 "Error allocating temporary data buffer of %d bytes.\n", |
c5ff71d64e53
More work on sidlib SLDB code.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
454 PSID_BUFFER_SIZE); |
104
09ca9d9a8710
Change to match changed th_ioctx API.
Matti Hamalainen <ccr@tnsp.org>
parents:
103
diff
changeset
|
455 return ctx->status; |
88
c5ff71d64e53
More work on sidlib SLDB code.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
456 } |
c5ff71d64e53
More work on sidlib SLDB code.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
457 |
c5ff71d64e53
More work on sidlib SLDB code.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
458 while (thfgets(line, PSID_BUFFER_SIZE, ctx) != NULL) |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
459 { |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
460 SIDLibSLDBNode *tmnode; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
461 size_t pos = 0; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
462 ctx->line++; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
463 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
464 th_findnext(line, &pos); |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
465 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
466 // Check if it is datafield |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
467 if (th_isxdigit(line[pos])) |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
468 { |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
469 // Check the length of the hash |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
470 int hashLen; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
471 for (hashLen = 0; line[pos] && th_isxdigit(line[pos]); hashLen++, pos++); |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
472 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
473 if (hashLen != TH_MD5HASH_LENGTH_CH) |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
474 { |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
475 th_io_error(ctx, THERR_INVALID_DATA, |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
476 "Invalid MD5-hash in SongLengthDB file '%s' line #%d:\n%s\n", |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
477 ctx->filename, ctx->line, line); |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
478 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
479 else |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
480 { |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
481 // Parse and add node to db |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
482 if ((tmnode = si_sldb_parse_entry(ctx, line)) != NULL) |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
483 { |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
484 si_sldb_node_insert(dbh, tmnode); |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
485 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
486 else |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
487 { |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
488 th_io_error(ctx, THERR_INVALID_DATA, |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
489 "Invalid entry in SongLengthDB file '%s' line #%d:\n%s\n", |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
490 ctx->filename, ctx->line, line); |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
491 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
492 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
493 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
494 else |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
495 if (line[pos] != ';' && line[pos] != '[' && line[pos] != 0) |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
496 { |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
497 th_io_error(ctx, THERR_INVALID_DATA, |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
498 "Invalid line in SongLengthDB file '%s' line #%d:\n%s\n", |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
499 ctx->filename, ctx->line, line); |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
500 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
501 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
502 |
88
c5ff71d64e53
More work on sidlib SLDB code.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
503 th_free(line); |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
504 return THERR_OK; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
505 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
506 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
507 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
508 // Compare two given MD5-hashes. |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
509 // Return: 0 if equal |
102 | 510 // negative if hash1 < hash2 |
511 // positive if hash1 > hash2 | |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
512 // |
102 | 513 static int si_sldb_compare_hash(th_md5hash_t hash1, th_md5hash_t hash2) |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
514 { |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
515 int i, delta; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
516 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
517 for (i = delta = 0; i < TH_MD5HASH_LENGTH && !delta; i++) |
102 | 518 delta = hash1[i] - hash2[i]; |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
519 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
520 return delta; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
521 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
522 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
523 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
524 // Compare two nodes. |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
525 // We assume here that we never ever get NULL-pointers. |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
526 static int si_sldb_compare_nodes(const void *node1, const void *node2) |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
527 { |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
528 return si_sldb_compare_hash( |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
529 (*(SIDLibSLDBNode **) node1)->hash, |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
530 (*(SIDLibSLDBNode **) node2)->hash); |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
531 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
532 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
533 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
534 // (Re)create index |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
535 // |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
536 int si_sldb_build_index(SIDLibSLDB * dbh) |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
537 { |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
538 SIDLibSLDBNode *node; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
539 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
540 // Free old index |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
541 th_free_r(&dbh->pindex); |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
542 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
543 // Get size of db |
100 | 544 for (node = dbh->nodes, dbh->nnodes = 0; node != NULL; node = node->next) |
545 dbh->nnodes++; | |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
546 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
547 // Check number of nodes |
100 | 548 if (dbh->nnodes > 0) |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
549 { |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
550 size_t i; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
551 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
552 // Allocate memory for index-table |
100 | 553 dbh->pindex = (SIDLibSLDBNode **) th_malloc(sizeof(SIDLibSLDBNode *) * dbh->nnodes); |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
554 if (dbh->pindex == NULL) |
88
c5ff71d64e53
More work on sidlib SLDB code.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
555 return THERR_MALLOC; |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
556 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
557 // Get node-pointers to table |
100 | 558 for (i = 0, node = dbh->nodes; node && i < dbh->nnodes; node = node->next) |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
559 dbh->pindex[i++] = node; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
560 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
561 // Sort the indexes |
100 | 562 qsort(dbh->pindex, dbh->nnodes, sizeof(SIDLibSLDBNode *), si_sldb_compare_nodes); |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
563 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
564 |
88
c5ff71d64e53
More work on sidlib SLDB code.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
565 return THERR_OK; |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
566 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
567 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
568 |
103
ff6d1a9a2fde
Move definitions to start of the file.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
569 // |
ff6d1a9a2fde
Move definitions to start of the file.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
570 // Read binary format SLDB |
ff6d1a9a2fde
Move definitions to start of the file.
Matti Hamalainen <ccr@tnsp.org>
parents:
102
diff
changeset
|
571 // |
101
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
572 int si_sldb_read_bin(th_ioctx *ctx, SIDLibSLDB *dbh) |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
573 { |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
574 PSIDLibHdr hdr; |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
575 th_md5state_t state; |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
576 th_md5hash_t hash; |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
577 size_t n; |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
578 |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
579 // Check pointers |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
580 if (ctx == NULL || dbh == NULL) |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
581 return THERR_NULLPTR; |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
582 |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
583 if (!thfread_str(ctx, &hdr.magic, sizeof(hdr.magic)) || |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
584 !thfread_le16(ctx, &hdr.version) || |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
585 !thfread_le32(ctx, &hdr.nnodes)) |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
586 return THERR_FREAD; |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
587 |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
588 // Check magic and version |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
589 if (memcmp(hdr.magic, SIDLIB_DB_MAGIC, sizeof(hdr.magic)) != 0) |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
590 return THERR_NOT_SUPPORTED; |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
591 |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
592 if (hdr.version != SIDLIB_DB_VERSION) |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
593 return THERR_NOT_SUPPORTED; |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
594 |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
595 // Make some reasonable assumptions about validity |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
596 if (hdr.nnodes == 0 || hdr.nnodes > 256*1024) |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
597 return THERR_INVALID_DATA; |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
598 |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
599 th_md5_init(&state); |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
600 th_md5_append_ne16(&state, hdr.version); |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
601 th_md5_append_ne32(&state, hdr.nnodes); |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
602 |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
603 // Allocate index |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
604 dbh->nnodes = hdr.nnodes; |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
605 dbh->pindex = (SIDLibSLDBNode **) th_malloc(sizeof(SIDLibSLDBNode *) * dbh->nnodes); |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
606 if (dbh->pindex == NULL) |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
607 return THERR_MALLOC; |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
608 |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
609 // Read nodes |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
610 for (n = 0; n < dbh->nnodes; n++) |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
611 { |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
612 SIDLibSLDBNode *node; |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
613 th_md5hash_t mhash; |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
614 uint16_t tmpn; |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
615 int index; |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
616 |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
617 // Read node hash and nlengths |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
618 if (!thfread_str(ctx, mhash, TH_MD5HASH_LENGTH) || |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
619 !thfread_le16(ctx, &tmpn)) |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
620 return THERR_FREAD; |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
621 |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
622 // Sanity check |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
623 if (tmpn == 0 || tmpn > 2048) |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
624 return THERR_INVALID_DATA; |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
625 |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
626 // Append to hash |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
627 th_md5_append(&state, mhash, TH_MD5HASH_LENGTH); |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
628 th_md5_append_ne16(&state, tmpn); |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
629 |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
630 // Allocate node |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
631 node = (SIDLibSLDBNode *) th_malloc0(sizeof(SIDLibSLDBNode)); |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
632 if (node == NULL) |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
633 return THERR_MALLOC; |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
634 |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
635 node->nlengths = tmpn; |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
636 memcpy(node->hash, mhash, sizeof(mhash)); |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
637 |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
638 node->lengths = (int *) th_calloc(node->nlengths, sizeof(int)); |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
639 if (node->lengths == NULL) |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
640 { |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
641 th_free(node); |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
642 th_io_error(ctx, THERR_MALLOC, |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
643 "Could not allocate memory for node.\n"); |
104
09ca9d9a8710
Change to match changed th_ioctx API.
Matti Hamalainen <ccr@tnsp.org>
parents:
103
diff
changeset
|
644 return ctx->status; |
101
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
645 } |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
646 |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
647 // Read node lenghts |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
648 for (index = 0; index < node->nlengths; index++) |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
649 { |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
650 uint16_t tmpl; |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
651 if (!thfread_le16(ctx, &tmpl)) |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
652 return THERR_FREAD; |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
653 |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
654 th_md5_append_ne16(&state, tmpl); |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
655 node->lengths[index] = tmpl; |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
656 } |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
657 |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
658 si_sldb_node_insert(dbh, node); |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
659 dbh->pindex[n] = node; |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
660 } |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
661 |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
662 // Read stored checksum hash |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
663 if (!thfread_str(ctx, hdr.hash, sizeof(hdr.hash))) |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
664 return THERR_FREAD; |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
665 |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
666 // Compare to what we get |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
667 th_md5_finish(&state, hash); |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
668 if (memcmp(hash, hdr.hash, sizeof(hdr.hash)) != 0) |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
669 return THERR_INVALID_DATA; |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
670 |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
671 return THERR_OK; |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
672 } |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
673 |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
674 |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
675 int si_sldb_write_bin(th_ioctx *ctx, SIDLibSLDB *dbh) |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
676 { |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
677 th_md5state_t state; |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
678 th_md5hash_t hash; |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
679 size_t n; |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
680 |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
681 // Check pointers |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
682 if (ctx == NULL || dbh == NULL) |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
683 return THERR_NULLPTR; |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
684 |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
685 // Write header |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
686 if (!thfwrite_str(ctx, SIDLIB_DB_MAGIC, 8) || |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
687 !thfwrite_le16(ctx, SIDLIB_DB_VERSION) || |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
688 !thfwrite_le32(ctx, dbh->nnodes)) |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
689 return THERR_FWRITE; |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
690 |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
691 th_md5_init(&state); |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
692 th_md5_append_ne16(&state, SIDLIB_DB_VERSION); |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
693 th_md5_append_ne32(&state, dbh->nnodes); |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
694 |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
695 // Write nodes |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
696 for (n = 0; n < dbh->nnodes; n++) |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
697 { |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
698 SIDLibSLDBNode *node = dbh->pindex[n]; |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
699 uint16_t tmpn; |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
700 int index; |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
701 |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
702 // Sanity check |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
703 if (node->nlengths <= 0 || node->nlengths > 2048) |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
704 return THERR_INVALID_DATA; |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
705 |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
706 tmpn = node->nlengths; |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
707 th_md5_append(&state, node->hash, TH_MD5HASH_LENGTH); |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
708 th_md5_append_ne16(&state, tmpn); |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
709 |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
710 // Write node data |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
711 if (!thfwrite_str(ctx, node->hash, TH_MD5HASH_LENGTH) || |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
712 !thfwrite_le16(ctx, tmpn)) |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
713 return THERR_FWRITE; |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
714 |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
715 for (index = 0; index < node->nlengths; index++) |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
716 { |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
717 if (node->lengths[index] > 16378) |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
718 return THERR_INVALID_DATA; |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
719 |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
720 if (!thfwrite_le16(ctx, node->lengths[index])) |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
721 return THERR_FWRITE; |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
722 |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
723 th_md5_append_ne16(&state, node->lengths[index]); |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
724 } |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
725 } |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
726 |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
727 th_md5_finish(&state, hash); |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
728 if (!thfwrite_str(ctx, hash, sizeof(hash))) |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
729 return THERR_FWRITE; |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
730 |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
731 return THERR_OK; |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
732 } |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
733 |
9d4d1783800b
Add si_sldb_{read,write}_bin() functions to write and read a binary format
Matti Hamalainen <ccr@tnsp.org>
parents:
100
diff
changeset
|
734 |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
735 // Free a given song-length database |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
736 // |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
737 void si_sldb_free(SIDLibSLDB *dbh) |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
738 { |
88
c5ff71d64e53
More work on sidlib SLDB code.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
739 if (dbh != NULL) |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
740 { |
88
c5ff71d64e53
More work on sidlib SLDB code.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
741 SIDLibSLDBNode *node = dbh->nodes; |
c5ff71d64e53
More work on sidlib SLDB code.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
742 while (node != NULL) |
c5ff71d64e53
More work on sidlib SLDB code.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
743 { |
c5ff71d64e53
More work on sidlib SLDB code.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
744 SIDLibSLDBNode *next = node->next; |
c5ff71d64e53
More work on sidlib SLDB code.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
745 si_sldb_node_free(node); |
c5ff71d64e53
More work on sidlib SLDB code.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
746 node = next; |
c5ff71d64e53
More work on sidlib SLDB code.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
747 } |
c5ff71d64e53
More work on sidlib SLDB code.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
748 |
c5ff71d64e53
More work on sidlib SLDB code.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
749 dbh->nodes = NULL; |
100 | 750 dbh->nnodes = 0; |
88
c5ff71d64e53
More work on sidlib SLDB code.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
751 |
c5ff71d64e53
More work on sidlib SLDB code.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
752 th_free_r(&dbh->pindex); |
c5ff71d64e53
More work on sidlib SLDB code.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
753 th_free(dbh); |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
754 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
755 } |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
756 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
757 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
758 SIDLibSLDBNode *si_sldb_get_by_hash(SIDLibSLDB *dbh, th_md5hash_t hash) |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
759 { |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
760 SIDLibSLDBNode keyItem, *key, **item; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
761 |
88
c5ff71d64e53
More work on sidlib SLDB code.
Matti Hamalainen <ccr@tnsp.org>
parents:
86
diff
changeset
|
762 memcpy(&keyItem.hash, hash, sizeof(th_md5hash_t)); |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
763 key = &keyItem; |
100 | 764 item = bsearch(&key, dbh->pindex, dbh->nnodes, sizeof(dbh->pindex[0]), si_sldb_compare_nodes); |
86
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
765 |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
766 return (item != NULL) ? *item : NULL; |
e1ff9cd27a84
Initial port of songlength database (SLDB) handling code from XMMS-SID to here.
Matti Hamalainen <ccr@tnsp.org>
parents:
85
diff
changeset
|
767 } |