[45913] | 1 | /* $Id: EBMLWriter.h 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
[69683] | 3 | * EBMLWriter.h - EBML writer.
|
---|
[45913] | 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[106061] | 7 | * Copyright (C) 2013-2024 Oracle and/or its affiliates.
|
---|
[45913] | 8 | *
|
---|
[96407] | 9 | * This file is part of VirtualBox base platform packages, as
|
---|
| 10 | * available from https://www.virtualbox.org.
|
---|
| 11 | *
|
---|
| 12 | * This program is free software; you can redistribute it and/or
|
---|
| 13 | * modify it under the terms of the GNU General Public License
|
---|
| 14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
| 15 | * License.
|
---|
| 16 | *
|
---|
| 17 | * This program is distributed in the hope that it will be useful, but
|
---|
| 18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 20 | * General Public License for more details.
|
---|
| 21 | *
|
---|
| 22 | * You should have received a copy of the GNU General Public License
|
---|
| 23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
| 24 | *
|
---|
| 25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
[45913] | 26 | */
|
---|
| 27 |
|
---|
[76562] | 28 | #ifndef MAIN_INCLUDED_EBMLWriter_h
|
---|
| 29 | #define MAIN_INCLUDED_EBMLWriter_h
|
---|
[76487] | 30 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
| 31 | # pragma once
|
---|
| 32 | #endif
|
---|
[45913] | 33 |
|
---|
[69683] | 34 | #include <stack>
|
---|
| 35 |
|
---|
[82422] | 36 | #include <iprt/critsect.h>
|
---|
[69683] | 37 | #include <iprt/file.h>
|
---|
| 38 |
|
---|
[82427] | 39 | #include <VBox/com/string.h>
|
---|
| 40 |
|
---|
| 41 |
|
---|
[69683] | 42 | /** No flags set. */
|
---|
| 43 | #define VBOX_EBMLWRITER_FLAG_NONE 0
|
---|
| 44 | /** The file handle was inherited. */
|
---|
| 45 | #define VBOX_EBMLWRITER_FLAG_HANDLE_INHERITED RT_BIT(0)
|
---|
| 46 |
|
---|
| 47 | class EBMLWriter
|
---|
[45913] | 48 | {
|
---|
[52312] | 49 | public:
|
---|
[69683] | 50 | typedef uint32_t EbmlClassId;
|
---|
[65197] | 51 |
|
---|
[69683] | 52 | private:
|
---|
[65212] | 53 |
|
---|
[69683] | 54 | struct EbmlSubElement
|
---|
[65212] | 55 | {
|
---|
[69683] | 56 | uint64_t offset;
|
---|
| 57 | EbmlClassId classId;
|
---|
| 58 | EbmlSubElement(uint64_t offs, EbmlClassId cid) : offset(offs), classId(cid) {}
|
---|
[65212] | 59 | };
|
---|
| 60 |
|
---|
[69683] | 61 | /** Stack of EBML sub elements. */
|
---|
| 62 | std::stack<EbmlSubElement> m_Elements;
|
---|
| 63 | /** The file's handle. */
|
---|
| 64 | RTFILE m_hFile;
|
---|
| 65 | /** The file's name (path). */
|
---|
[76760] | 66 | com::Utf8Str m_strFile;
|
---|
[69683] | 67 | /** Flags. */
|
---|
| 68 | uint32_t m_fFlags;
|
---|
[65212] | 69 |
|
---|
[69683] | 70 | public:
|
---|
[65197] | 71 |
|
---|
[69683] | 72 | EBMLWriter(void)
|
---|
| 73 | : m_hFile(NIL_RTFILE)
|
---|
| 74 | , m_fFlags(VBOX_EBMLWRITER_FLAG_NONE) { }
|
---|
| 75 |
|
---|
| 76 | virtual ~EBMLWriter(void) { close(); }
|
---|
| 77 |
|
---|
[65197] | 78 | public:
|
---|
| 79 |
|
---|
[69683] | 80 | int createEx(const char *a_pszFile, PRTFILE phFile);
|
---|
[52901] | 81 |
|
---|
[69683] | 82 | int create(const char *a_pszFile, uint64_t fOpen);
|
---|
[68796] | 83 |
|
---|
[69683] | 84 | void close(void);
|
---|
[45913] | 85 |
|
---|
[69683] | 86 | /** Returns the file name. */
|
---|
[76760] | 87 | const com::Utf8Str& getFileName(void) { return m_strFile; }
|
---|
[52312] | 88 |
|
---|
[69683] | 89 | /** Returns file size. */
|
---|
| 90 | uint64_t getFileSize(void) { return RTFileTell(m_hFile); }
|
---|
[52312] | 91 |
|
---|
[69683] | 92 | /** Get reference to file descriptor */
|
---|
| 93 | inline const RTFILE &getFile(void) { return m_hFile; }
|
---|
[65256] | 94 |
|
---|
[69683] | 95 | /** Returns available space on storage. */
|
---|
| 96 | uint64_t getAvailableSpace(void);
|
---|
[52312] | 97 |
|
---|
[65197] | 98 | /**
|
---|
[69683] | 99 | * Returns whether the file is open or not.
|
---|
[68332] | 100 | *
|
---|
[69683] | 101 | * @returns True if open, false if not.
|
---|
[68332] | 102 | */
|
---|
[69683] | 103 | bool isOpen(void) { return RTFileIsValid(m_hFile); }
|
---|
[68332] | 104 |
|
---|
[69683] | 105 | public:
|
---|
[52791] | 106 |
|
---|
[69683] | 107 | EBMLWriter &subStart(EbmlClassId classId);
|
---|
[52312] | 108 |
|
---|
[69683] | 109 | EBMLWriter &subEnd(EbmlClassId classId);
|
---|
| 110 |
|
---|
| 111 | EBMLWriter &serializeString(EbmlClassId classId, const char *str);
|
---|
| 112 |
|
---|
| 113 | EBMLWriter &serializeUnsignedInteger(EbmlClassId classId, uint64_t parm, size_t size = 0);
|
---|
| 114 |
|
---|
| 115 | EBMLWriter &serializeFloat(EbmlClassId classId, float value);
|
---|
| 116 |
|
---|
| 117 | EBMLWriter &serializeData(EbmlClassId classId, const void *pvData, size_t cbData);
|
---|
| 118 |
|
---|
| 119 | int write(const void *data, size_t size);
|
---|
| 120 |
|
---|
| 121 | void writeUnsignedInteger(uint64_t value, size_t size = sizeof(uint64_t));
|
---|
| 122 |
|
---|
| 123 | void writeClassId(EbmlClassId parm);
|
---|
| 124 |
|
---|
| 125 | void writeSize(uint64_t parm);
|
---|
| 126 |
|
---|
| 127 | static inline size_t getSizeOfUInt(uint64_t arg);
|
---|
| 128 |
|
---|
[52312] | 129 | private:
|
---|
[65197] | 130 |
|
---|
[69683] | 131 | void operator=(const EBMLWriter &);
|
---|
[45913] | 132 | };
|
---|
| 133 |
|
---|
[76562] | 134 | #endif /* !MAIN_INCLUDED_EBMLWriter_h */
|
---|
[76487] | 135 |
|
---|