FlacIOHandle.cxx 2.96 KB
Newer Older
1
/*
Max Kellermann's avatar
Max Kellermann committed
2
 * Copyright 2003-2019 The Music Player Daemon Project
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 * 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.
 */

20
#include "FlacIOHandle.hxx"
21
#include "Log.hxx"
22
#include "util/Compiler.h"
23
#include "system/Error.hxx"
24

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

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

	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) {
40 41 42
		try {
			size_t nbytes = is->LockRead(p, end - p);
			if (nbytes == 0)
43 44 45
				/* end of file */
				break;

46 47
			p += nbytes;

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

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

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

75
	offset_type offset = _offset;
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
	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;
	}

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

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

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

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

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

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

	return 0;
}

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

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