xref: /haiku/src/apps/haikudepot/tar/TarArchiveService.h (revision 2db0fbd87ec3eeed8ab9b08ecfd1c41891c7e3a6)
1 /*
2  * Copyright 2017-2020, Andrew Lindesay <apl@lindesay.co.nz>.
3  * All rights reserved. Distributed under the terms of the MIT License.
4  */
5 #ifndef TAR_ARCHIVE_SERVICE_H
6 #define TAR_ARCHIVE_SERVICE_H
7 
8 #include "Stoppable.h"
9 #include "TarArchiveHeader.h"
10 
11 #include <String.h>
12 #include <Path.h>
13 
14 
15 class TarEntryListener {
16 public:
17 	virtual status_t			Handle(
18 									const TarArchiveHeader& header,
19 									size_t offset,
20 									BDataIO* data) = 0;
21 };
22 
23 
24 class TarArchiveService {
25 public:
26 	static	status_t			ForEachEntry(BPositionIO& tarIo,
27 									TarEntryListener* listener);
28 	static	status_t			GetEntry(BPositionIO& tarIo,
29 									TarArchiveHeader& header);
30 
31 private:
32 	static	status_t			_ValidatePathComponent(
33 									const BString& component);
34 
35 	static	off_t				_BytesRoundedToBlocks(off_t value);
36 	static	uint32				_CalculateBlockChecksum(
37 									const unsigned char* data);
38 
39 	static	status_t			_ReadHeader(const uint8* data,
40 										TarArchiveHeader& header);
41 	static	int32				_ReadHeaderStringLength(const uint8* data,
42 									size_t maxStringLength);
43 	static	void				_ReadHeaderString(const uint8* data,
44 									size_t dataLength, BString& result);
45 	static uint32				_ReadHeaderNumeric(const uint8* data,
46 									size_t dataLength);
47 	static tar_file_type		_ReadHeaderFileType(uint8 data);
48 
49 };
50 
51 #endif // TAR_ARCHIVE_SERVICE_H
52