summaryrefslogtreecommitdiffstatshomepage
path: root/src/tools/imgtool/stream.h
blob: 73d0dcb494da863d6261169d77f4d8663f849fae (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// license:BSD-3-Clause
// copyright-holders:Nathan Woods
/***************************************************************************

    stream.h

    Code for implementing Imgtool streams

***************************************************************************/

#ifndef STREAM_H
#define STREAM_H

#include "imgterrs.h"
#include "corefile.h"
#include "osdcomm.h"

namespace imgtool
{
	class stream
	{
	public:
		typedef std::unique_ptr<stream> ptr;

		~stream();

		static imgtool::stream::ptr open(const std::string &filename, int read_or_write);  /* similar params to mame_fopen */
		static imgtool::stream::ptr open_write_stream(int filesize);
		static imgtool::stream::ptr open_mem(void *buf, size_t sz);

		util::core_file *core_file();
		uint32_t read(void *buf, uint32_t sz);
		uint32_t write(const void *buf, uint32_t sz);
		uint64_t size() const;
		int seek(int64_t pos, int where);
		uint64_t tell();
		void *getptr();
		uint32_t putc(char c);
		uint32_t puts(const char *s);
		uint32_t printf(const char *fmt, ...) ATTR_PRINTF(2, 3);

		// transfers sz bytes from source to dest
		static uint64_t transfer(imgtool::stream &dest, imgtool::stream &source, uint64_t sz);
		static uint64_t transfer_all(imgtool::stream &dest, imgtool::stream &source);

		// fills sz bytes with b
		uint64_t fill(unsigned char b, uint64_t sz);

		// returns the CRC of a file
		int crc(unsigned long *result);
		static int file_crc(const char *fname, unsigned long *result);

		// returns whether a stream is read only or not
		bool is_read_only();

	private:
		enum imgtype_t
		{
			IMG_FILE,
			IMG_MEM
		};

		imgtype_t       imgtype;
		bool            write_protect;
		std::uint64_t   position;
		std::uint64_t   filesize;

		util::core_file::ptr file;
		std::uint8_t *buffer;

		// ctors
		stream(bool wp);
		stream(bool wp, util::core_file::ptr &&f);
		stream(bool wp, std::size_t size);
		stream(bool wp, std::size_t size, void *buf);

		// private methods
		static stream::ptr open_zip(const std::string &zipname, const char *subname, int read_or_write);
	};
}



#endif /* STREAM_H */