--- rockbox-devel/apps/metadata.c.old 2006-01-13 16:50:40.000000000 +0100 +++ rockbox-devel/apps/metadata.c 2006-01-13 17:30:17.000000000 +0100 @@ -551,7 +551,7 @@ long comment_size; long remaining = 0; long last_serial = 0; - long serial; + long serial, r; int segments; int i; bool eof = false; @@ -647,7 +647,7 @@ while (!eof) { - long r = read(fd, &buf[remaining], MAX_PATH - remaining); + r = read(fd, &buf[remaining], MAX_PATH - remaining); if (r <= 0) { @@ -661,7 +661,7 @@ /* Inefficient (but simple) search */ i = 0; - while (i < (remaining - 5)) + while (i < (remaining - 3)) { if ((buf[i] == 'O') && (memcmp(&buf[i], "OggS", 4) == 0)) { @@ -672,8 +672,11 @@ */ id3->samples = get_long(&buf[i + 6]); last_serial = get_long(&buf[i + 14]); - /* We can discard the rest of the buffer */ - remaining = 0; + + /* If this page is very small the beginning of the next + * header could be in buffer. Jump near end of this header + * and continue */ + i += 27; } else { @@ -686,16 +689,20 @@ } } - if (i < (remaining - 5)) + if (i < remaining) { - /* Move OggS to start of buffer. */ - while (i >0) + /* Move the remaining bytes to start of buffer. + * Reuse var 'segments' as it is no longer needed */ + segments = 0; + while (i < remaining) { - buf[i--] = buf[remaining--]; + buf[segments++] = buf[i++]; } + remaining = segments; } else { + /* Discard the rest of the buffer */ remaining = 0; } }