1 | /* $Id: EBMLWriter.h 82968 2020-02-04 10:35:17Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * EBMLWriter.h - EBML writer.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2013-2020 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #ifndef MAIN_INCLUDED_EBMLWriter_h
|
---|
19 | #define MAIN_INCLUDED_EBMLWriter_h
|
---|
20 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
21 | # pragma once
|
---|
22 | #endif
|
---|
23 |
|
---|
24 | #include <stack>
|
---|
25 |
|
---|
26 | #include <iprt/critsect.h>
|
---|
27 | #include <iprt/file.h>
|
---|
28 |
|
---|
29 | #include <VBox/com/string.h>
|
---|
30 |
|
---|
31 |
|
---|
32 | /** No flags set. */
|
---|
33 | #define VBOX_EBMLWRITER_FLAG_NONE 0
|
---|
34 | /** The file handle was inherited. */
|
---|
35 | #define VBOX_EBMLWRITER_FLAG_HANDLE_INHERITED RT_BIT(0)
|
---|
36 |
|
---|
37 | class EBMLWriter
|
---|
38 | {
|
---|
39 | public:
|
---|
40 | typedef uint32_t EbmlClassId;
|
---|
41 |
|
---|
42 | private:
|
---|
43 |
|
---|
44 | struct EbmlSubElement
|
---|
45 | {
|
---|
46 | uint64_t offset;
|
---|
47 | EbmlClassId classId;
|
---|
48 | EbmlSubElement(uint64_t offs, EbmlClassId cid) : offset(offs), classId(cid) {}
|
---|
49 | };
|
---|
50 |
|
---|
51 | /** Stack of EBML sub elements. */
|
---|
52 | std::stack<EbmlSubElement> m_Elements;
|
---|
53 | /** The file's handle. */
|
---|
54 | RTFILE m_hFile;
|
---|
55 | /** The file's name (path). */
|
---|
56 | com::Utf8Str m_strFile;
|
---|
57 | /** Flags. */
|
---|
58 | uint32_t m_fFlags;
|
---|
59 |
|
---|
60 | public:
|
---|
61 |
|
---|
62 | EBMLWriter(void)
|
---|
63 | : m_hFile(NIL_RTFILE)
|
---|
64 | , m_fFlags(VBOX_EBMLWRITER_FLAG_NONE) { }
|
---|
65 |
|
---|
66 | virtual ~EBMLWriter(void) { close(); }
|
---|
67 |
|
---|
68 | public:
|
---|
69 |
|
---|
70 | int createEx(const char *a_pszFile, PRTFILE phFile);
|
---|
71 |
|
---|
72 | int create(const char *a_pszFile, uint64_t fOpen);
|
---|
73 |
|
---|
74 | void close(void);
|
---|
75 |
|
---|
76 | /** Returns the file name. */
|
---|
77 | const com::Utf8Str& getFileName(void) { return m_strFile; }
|
---|
78 |
|
---|
79 | /** Returns file size. */
|
---|
80 | uint64_t getFileSize(void) { return RTFileTell(m_hFile); }
|
---|
81 |
|
---|
82 | /** Get reference to file descriptor */
|
---|
83 | inline const RTFILE &getFile(void) { return m_hFile; }
|
---|
84 |
|
---|
85 | /** Returns available space on storage. */
|
---|
86 | uint64_t getAvailableSpace(void);
|
---|
87 |
|
---|
88 | /**
|
---|
89 | * Returns whether the file is open or not.
|
---|
90 | *
|
---|
91 | * @returns True if open, false if not.
|
---|
92 | */
|
---|
93 | bool isOpen(void) { return RTFileIsValid(m_hFile); }
|
---|
94 |
|
---|
95 | public:
|
---|
96 |
|
---|
97 | EBMLWriter &subStart(EbmlClassId classId);
|
---|
98 |
|
---|
99 | EBMLWriter &subEnd(EbmlClassId classId);
|
---|
100 |
|
---|
101 | EBMLWriter &serializeString(EbmlClassId classId, const char *str);
|
---|
102 |
|
---|
103 | EBMLWriter &serializeUnsignedInteger(EbmlClassId classId, uint64_t parm, size_t size = 0);
|
---|
104 |
|
---|
105 | EBMLWriter &serializeFloat(EbmlClassId classId, float value);
|
---|
106 |
|
---|
107 | EBMLWriter &serializeData(EbmlClassId classId, const void *pvData, size_t cbData);
|
---|
108 |
|
---|
109 | int write(const void *data, size_t size);
|
---|
110 |
|
---|
111 | void writeUnsignedInteger(uint64_t value, size_t size = sizeof(uint64_t));
|
---|
112 |
|
---|
113 | void writeClassId(EbmlClassId parm);
|
---|
114 |
|
---|
115 | void writeSize(uint64_t parm);
|
---|
116 |
|
---|
117 | static inline size_t getSizeOfUInt(uint64_t arg);
|
---|
118 |
|
---|
119 | private:
|
---|
120 |
|
---|
121 | void operator=(const EBMLWriter &);
|
---|
122 | };
|
---|
123 |
|
---|
124 | #endif /* !MAIN_INCLUDED_EBMLWriter_h */
|
---|
125 |
|
---|