FlacIOHandle.cxx 2.98 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2017 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 "config.h"
21
#include "FlacIOHandle.hxx"
22
#include "Log.hxx"
23
#include "Compiler.h"
24
#include "system/Error.hxx"
25

26
#include <errno.h>
27
#include <stdio.h>
28 29

static size_t
30
FlacIORead(void *ptr, size_t size, size_t nmemb, FLAC__IOHandle handle)
31
{
32
	InputStream *is = (InputStream *)handle;
33 34 35 36 37 38 39 40

	uint8_t *const p0 = (uint8_t *)ptr, *p = p0,
		*const end = p0 + size * nmemb;

	/* libFLAC is very picky about short reads, and expects the IO
	   callback to fill the whole buffer (undocumented!) */

	while (p < end) {
41 42 43
		try {
			size_t nbytes = is->LockRead(p, end - p);
			if (nbytes == 0)
44 45 46
				/* end of file */
				break;

47 48
			p += nbytes;

49
#ifndef _WIN32
50
		} catch (const std::system_error &e) {
51
			errno = e.code().category() == ErrnoCategory()
52 53 54 55 56 57
				? e.code().value()
				/* just some random non-zero errno
				   value */
				: EINVAL;
			return 0;
#endif
58
		} catch (...) {
59 60
			/* just some random non-zero errno value */
			errno = EINVAL;
61 62 63 64 65 66 67 68 69 70 71
			return 0;
		}
	}

	/* libFLAC expects a clean errno after returning from the IO
	   callbacks (undocumented!) */
	errno = 0;
	return (p - p0) / size;
}

static int
72
FlacIOSeek(FLAC__IOHandle handle, FLAC__int64 _offset, int whence)
73
{
74
	InputStream *is = (InputStream *)handle;
75

76
	offset_type offset = _offset;
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
	switch (whence) {
	case SEEK_SET:
		break;

	case SEEK_CUR:
		offset += is->GetOffset();
		break;

	case SEEK_END:
		if (!is->KnownSize())
			return -1;

		offset += is->GetSize();
		break;

	default:
		return -1;
	}

96 97 98
	try {
		is->LockSeek(offset);
		return 0;
99 100
	} catch (...) {
		LogError(std::current_exception());
101 102
		return -1;
	}
103 104 105
}

static FLAC__int64
106
FlacIOTell(FLAC__IOHandle handle)
107
{
108
	InputStream *is = (InputStream *)handle;
109

110
	return is->GetOffset();
111 112 113
}

static int
114
FlacIOEof(FLAC__IOHandle handle)
115
{
116
	InputStream *is = (InputStream *)handle;
117

118
	return is->LockIsEOF();
119 120 121
}

static int
122
FlacIOClose(gcc_unused FLAC__IOHandle handle)
123
{
124
	/* no-op because the libFLAC caller is responsible for closing
125
	   the #InputStream */
126 127 128 129 130

	return 0;
}

const FLAC__IOCallbacks flac_io_callbacks = {
131
	FlacIORead,
132 133 134
	nullptr,
	nullptr,
	nullptr,
135 136
	FlacIOEof,
	FlacIOClose,
137 138 139
};

const FLAC__IOCallbacks flac_io_callbacks_seekable = {
140
	FlacIORead,
141
	nullptr,
142 143 144 145
	FlacIOSeek,
	FlacIOTell,
	FlacIOEof,
	FlacIOClose,
146
};