OggFind.cxx 1.86 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2021 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 * http://www.musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "OggFind.hxx"
21
#include "lib/xiph/OggSyncState.hxx"
22
#include "input/InputStream.hxx"
23

24
bool
25
OggFindEOS(OggSyncState &oy, ogg_stream_state &os, ogg_packet &packet)
26 27 28 29
{
	while (true) {
		int r = ogg_stream_packetout(&os, &packet);
		if (r == 0) {
30
			if (!oy.ExpectPageIn(os))
31 32 33 34 35 36 37
				return false;

			continue;
		} else if (r > 0 && packet.e_o_s)
			return true;
	}
}
38

39
bool
40
OggSeekPageAtOffset(OggSyncState &oy, ogg_stream_state &os, InputStream &is,
41
		    offset_type offset)
42 43 44
{
	oy.Reset();

45 46 47 48
	/* reset the stream to clear any previous partial packet
	   data */
	ogg_stream_reset(&os);

49 50
	try {
		is.LockSeek(offset);
51
	} catch (...) {
52 53 54 55
		return false;
	}

	return oy.ExpectPageSeekIn(os);
56 57
}

58 59
bool
OggSeekFindEOS(OggSyncState &oy, ogg_stream_state &os, ogg_packet &packet,
60
	       InputStream &is, bool synced)
61
{
62 63 64 65
	if (!is.KnownSize())
		return false;

	if (is.GetRest() < 65536)
66 67
		return (synced || oy.ExpectPageSeekIn(os)) &&
			OggFindEOS(oy, os, packet);
68 69 70 71

	if (!is.CheapSeeking())
		return false;

72
	return OggSeekPageAtOffset(oy, os, is, is.GetSize() - 65536) &&
73 74
		OggFindEOS(oy, os, packet);
}