1 | /* $Id: EBMLWriter.h 82422 2019-12-05 15:59:34Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * EBMLWriter.h - EBML writer.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2013-2019 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 | /** No flags set. */
|
---|
30 | #define VBOX_EBMLWRITER_FLAG_NONE 0
|
---|
31 | /** The file handle was inherited. */
|
---|
32 | #define VBOX_EBMLWRITER_FLAG_HANDLE_INHERITED RT_BIT(0)
|
---|
33 |
|
---|
34 | class EBMLWriter
|
---|
35 | {
|
---|
36 | public:
|
---|
37 | typedef uint32_t EbmlClassId;
|
---|
38 |
|
---|
39 | private:
|
---|
40 |
|
---|
41 | struct EbmlSubElement
|
---|
42 | {
|
---|
43 | uint64_t offset;
|
---|
44 | EbmlClassId classId;
|
---|
45 | EbmlSubElement(uint64_t offs, EbmlClassId cid) : offset(offs), classId(cid) {}
|
---|
46 | };
|
---|
47 |
|
---|
48 | /** Stack of EBML sub elements. */
|
---|
49 | std::stack<EbmlSubElement> m_Elements;
|
---|
50 | /** The file's handle. */
|
---|
51 | RTFILE m_hFile;
|
---|
52 | /** The file's name (path). */
|
---|
53 | com::Utf8Str m_strFile;
|
---|
54 | /** Flags. */
|
---|
55 | uint32_t m_fFlags;
|
---|
56 |
|
---|
57 | public:
|
---|
58 |
|
---|
59 | EBMLWriter(void)
|
---|
60 | : m_hFile(NIL_RTFILE)
|
---|
61 | , m_fFlags(VBOX_EBMLWRITER_FLAG_NONE) { }
|
---|
62 |
|
---|
63 | virtual ~EBMLWriter(void) { close(); }
|
---|
64 |
|
---|
65 | public:
|
---|
66 |
|
---|
67 | int createEx(const char *a_pszFile, PRTFILE phFile);
|
---|
68 |
|
---|
69 | int create(const char *a_pszFile, uint64_t fOpen);
|
---|
70 |
|
---|
71 | void close(void);
|
---|
72 |
|
---|
73 | /** Returns the file name. */
|
---|
74 | const com::Utf8Str& getFileName(void) { return m_strFile; }
|
---|
75 |
|
---|
76 | /** Returns file size. */
|
---|
77 | uint64_t getFileSize(void) { return RTFileTell(m_hFile); }
|
---|
78 |
|
---|
79 | /** Get reference to file descriptor */
|
---|
80 | inline const RTFILE &getFile(void) { return m_hFile; }
|
---|
81 |
|
---|
82 | /** Returns available space on storage. */
|
---|
83 | uint64_t getAvailableSpace(void);
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Returns whether the file is open or not.
|
---|
87 | *
|
---|
88 | * @returns True if open, false if not.
|
---|
89 | */
|
---|
90 | bool isOpen(void) { return RTFileIsValid(m_hFile); }
|
---|
91 |
|
---|
92 | public:
|
---|
93 |
|
---|
94 | EBMLWriter &subStart(EbmlClassId classId);
|
---|
95 |
|
---|
96 | EBMLWriter &subEnd(EbmlClassId classId);
|
---|
97 |
|
---|
98 | EBMLWriter &serializeString(EbmlClassId classId, const char *str);
|
---|
99 |
|
---|
100 | EBMLWriter &serializeUnsignedInteger(EbmlClassId classId, uint64_t parm, size_t size = 0);
|
---|
101 |
|
---|
102 | EBMLWriter &serializeFloat(EbmlClassId classId, float value);
|
---|
103 |
|
---|
104 | EBMLWriter &serializeData(EbmlClassId classId, const void *pvData, size_t cbData);
|
---|
105 |
|
---|
106 | int write(const void *data, size_t size);
|
---|
107 |
|
---|
108 | void writeUnsignedInteger(uint64_t value, size_t size = sizeof(uint64_t));
|
---|
109 |
|
---|
110 | void writeClassId(EbmlClassId parm);
|
---|
111 |
|
---|
112 | void writeSize(uint64_t parm);
|
---|
113 |
|
---|
114 | static inline size_t getSizeOfUInt(uint64_t arg);
|
---|
115 |
|
---|
116 | private:
|
---|
117 |
|
---|
118 | void operator=(const EBMLWriter &);
|
---|
119 | };
|
---|
120 |
|
---|
121 | #endif /* !MAIN_INCLUDED_EBMLWriter_h */
|
---|
122 |
|
---|