summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/rbcodec/metadata/smaf.c53
1 files changed, 27 insertions, 26 deletions
diff --git a/lib/rbcodec/metadata/smaf.c b/lib/rbcodec/metadata/smaf.c
index 78e1fec499..fcd026924c 100644
--- a/lib/rbcodec/metadata/smaf.c
+++ b/lib/rbcodec/metadata/smaf.c
@@ -137,18 +137,19 @@ static void decode2utf8(const unsigned char *src, unsigned char **dst,
137 *dstsize -= utf8size; 137 *dstsize -= utf8size;
138} 138}
139 139
140static int read_audio_track_contets(int fd, int codepage, unsigned char **dst, 140static int read_audio_track_contents(int fd, int codepage, unsigned char **dst,
141 int *dstsize) 141 int *dstsize)
142{ 142{
143 /* value length <= 256 bytes */ 143 /* value length <= 256 bytes */
144 unsigned char buf[256]; 144 unsigned char buf[256];
145 unsigned char *p = buf; 145 unsigned char *p = buf;
146 unsigned char *q = buf; 146 unsigned char *q = buf;
147 int datasize;
148 147
149 read(fd, buf, 256); 148 int datasize = read(fd, buf, 256);
149 if (datasize <= 0)
150 return 0;
150 151
151 while (p - buf < 256 && *p != ',') 152 while (p - buf < datasize && *p != ',')
152 { 153 {
153 /* skip yen mark */ 154 /* skip yen mark */
154 if (codepage != UCS2) 155 if (codepage != UCS2)
@@ -177,6 +178,7 @@ static int read_audio_track_contets(int fd, int codepage, unsigned char **dst,
177 if (codepage == UCS2) 178 if (codepage == UCS2)
178 *q++ = *p++; 179 *q++ = *p++;
179 } 180 }
181
180 datasize = p - buf + 1; 182 datasize = p - buf + 1;
181 lseek(fd, datasize - 256, SEEK_CUR); 183 lseek(fd, datasize - 256, SEEK_CUR);
182 184
@@ -186,12 +188,12 @@ static int read_audio_track_contets(int fd, int codepage, unsigned char **dst,
186 return datasize; 188 return datasize;
187} 189}
188 190
189static void read_score_track_contets(int fd, int codepage, int datasize, 191static void read_score_track_contents(int fd, int codepage, int datasize,
190 unsigned char **dst, int *dstsize) 192 unsigned char **dst, int *dstsize)
191{ 193{
192 unsigned char buf[datasize]; 194 unsigned char buf[datasize];
193 195 if (read(fd, buf, datasize) != datasize)
194 read(fd, buf, datasize); 196 memset(buf, 0, datasize);
195 decode2utf8(buf, dst, datasize, dstsize, codepage); 197 decode2utf8(buf, dst, datasize, dstsize, codepage);
196} 198}
197 199
@@ -202,7 +204,7 @@ static unsigned int search_chunk(int fd, const unsigned char *name, int nlen)
202 unsigned char buf[8]; 204 unsigned char buf[8];
203 unsigned int chunksize; 205 unsigned int chunksize;
204 206
205 while (read(fd, buf, 8) > 0) 207 while (read(fd, buf, 8) == 8)
206 { 208 {
207 chunksize = get_long_be(buf + 4); 209 chunksize = get_long_be(buf + 4);
208 if (memcmp(buf, name, nlen) == 0) 210 if (memcmp(buf, name, nlen) == 0)
@@ -221,15 +223,14 @@ static bool parse_smaf_audio_track(int fd, struct mp3entry *id3, unsigned int da
221 /* contents stored buffer */ 223 /* contents stored buffer */
222 unsigned char *buf = id3->id3v2buf; 224 unsigned char *buf = id3->id3v2buf;
223 int bufsize = sizeof(id3->id3v2buf); 225 int bufsize = sizeof(id3->id3v2buf);
224
225 unsigned int chunksize = datasize;
226 int valsize; 226 int valsize;
227 227 unsigned int chunksize = datasize;
228 int codepage; 228 int codepage = -1;
229 229
230 /* parse contents info */ 230 /* parse contents info */
231 read(fd, tmp, 5); 231 if (read(fd, tmp, 5) == 5)
232 codepage = convert_smaf_codetype(tmp[2]); 232 codepage = convert_smaf_codetype(tmp[2]);
233
233 if (codepage < 0) 234 if (codepage < 0)
234 { 235 {
235 DEBUGF("metadata error: smaf unsupport codetype: %d\n", tmp[2]); 236 DEBUGF("metadata error: smaf unsupport codetype: %d\n", tmp[2]);
@@ -240,7 +241,7 @@ static bool parse_smaf_audio_track(int fd, struct mp3entry *id3, unsigned int da
240 while ((id3->title == NULL || id3->artist == NULL || id3->composer == NULL) 241 while ((id3->title == NULL || id3->artist == NULL || id3->composer == NULL)
241 && (datasize > 0 && bufsize > 0)) 242 && (datasize > 0 && bufsize > 0))
242 { 243 {
243 if (read(fd, tmp, 3) <= 0) 244 if (read(fd, tmp, 3) != 3)
244 return false; 245 return false;
245 246
246 if (tmp[2] != ':') 247 if (tmp[2] != ':')
@@ -252,18 +253,18 @@ static bool parse_smaf_audio_track(int fd, struct mp3entry *id3, unsigned int da
252 { 253 {
253 case TAG_TITLE: 254 case TAG_TITLE:
254 id3->title = buf; 255 id3->title = buf;
255 valsize = read_audio_track_contets(fd, codepage, &buf, &bufsize); 256 valsize = read_audio_track_contents(fd, codepage, &buf, &bufsize);
256 break; 257 break;
257 case TAG_ARTIST: 258 case TAG_ARTIST:
258 id3->artist = buf; 259 id3->artist = buf;
259 valsize = read_audio_track_contets(fd, codepage, &buf, &bufsize); 260 valsize = read_audio_track_contents(fd, codepage, &buf, &bufsize);
260 break; 261 break;
261 case TAG_COMPOSER: 262 case TAG_COMPOSER:
262 id3->composer = buf; 263 id3->composer = buf;
263 valsize = read_audio_track_contets(fd, codepage, &buf, &bufsize); 264 valsize = read_audio_track_contents(fd, codepage, &buf, &bufsize);
264 break; 265 break;
265 default: 266 default:
266 valsize = read_audio_track_contets(fd, codepage, NULL, &bufsize); 267 valsize = read_audio_track_contents(fd, codepage, NULL, &bufsize);
267 break; 268 break;
268 } 269 }
269 datasize -= (valsize + 3); 270 datasize -= (valsize + 3);
@@ -353,7 +354,7 @@ static bool parse_smaf_score_track(int fd, struct mp3entry *id3)
353 while ((id3->title == NULL || id3->artist == NULL || id3->composer == NULL) 354 while ((id3->title == NULL || id3->artist == NULL || id3->composer == NULL)
354 && (datasize > 0 && bufsize > 0)) 355 && (datasize > 0 && bufsize > 0))
355 { 356 {
356 if (read(fd, tmp, 4) <= 0) 357 if (read(fd, tmp, 4) != 4)
357 return false; 358 return false;
358 359
359 valsize = (tmp[2] << 8) | tmp[3]; 360 valsize = (tmp[2] << 8) | tmp[3];
@@ -362,15 +363,15 @@ static bool parse_smaf_score_track(int fd, struct mp3entry *id3)
362 { 363 {
363 case TAG_TITLE: 364 case TAG_TITLE:
364 id3->title = buf; 365 id3->title = buf;
365 read_score_track_contets(fd, codepage, valsize, &buf, &bufsize); 366 read_score_track_contents(fd, codepage, valsize, &buf, &bufsize);
366 break; 367 break;
367 case TAG_ARTIST: 368 case TAG_ARTIST:
368 id3->artist = buf; 369 id3->artist = buf;
369 read_score_track_contets(fd, codepage, valsize, &buf, &bufsize); 370 read_score_track_contents(fd, codepage, valsize, &buf, &bufsize);
370 break; 371 break;
371 case TAG_COMPOSER: 372 case TAG_COMPOSER:
372 id3->composer = buf; 373 id3->composer = buf;
373 read_score_track_contets(fd, codepage, valsize, &buf, &bufsize); 374 read_score_track_contents(fd, codepage, valsize, &buf, &bufsize);
374 break; 375 break;
375 default: 376 default:
376 lseek(fd, valsize, SEEK_CUR); 377 lseek(fd, valsize, SEEK_CUR);
@@ -449,10 +450,10 @@ bool get_smaf_metadata(int fd, struct mp3entry* id3)
449 450
450 /* check File Chunk and Contents Info Chunk */ 451 /* check File Chunk and Contents Info Chunk */
451 lseek(fd, 0, SEEK_SET); 452 lseek(fd, 0, SEEK_SET);
452 read(fd, tmp, 16); 453
453 if ((memcmp(tmp, "MMMD", 4) != 0) || (memcmp(tmp + 8, "CNTI", 4) != 0)) 454 if (read(fd, tmp, 16) != 16 || (memcmp(tmp, "MMMD", 4) != 0) || (memcmp(tmp + 8, "CNTI", 4) != 0))
454 { 455 {
455 DEBUGF("metadata error: does not smaf format\n"); 456 DEBUGF("metadata error: no smaf format\n");
456 return false; 457 return false;
457 } 458 }
458 459