1 | /* $Id: EBMLWriter.h 76553 2019-01-01 01:45:53Z 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 ____H_EBMLWRITER
|
---|
19 | #define ____H_EBMLWRITER
|
---|
20 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
21 | # pragma once
|
---|
22 | #endif
|
---|
23 |
|
---|
24 | #include <iprt/file.h>
|
---|
25 | #include <VBox/com/string.h> /* For Utf8Str. */
|
---|
26 |
|
---|
27 | using namespace com;
|
---|
28 |
|
---|
29 | #include <list>
|
---|
30 | #include <map>
|
---|
31 | #include <queue>
|
---|
32 | #include <stack>
|
---|
33 |
|
---|
34 | #include <math.h> /* For lround.h. */
|
---|
35 |
|
---|
36 | #include <iprt/asm.h>
|
---|
37 | #include <iprt/buildconfig.h>
|
---|
38 | #include <iprt/cdefs.h>
|
---|
39 | #include <iprt/critsect.h>
|
---|
40 | #include <iprt/err.h>
|
---|
41 | #include <iprt/file.h>
|
---|
42 | #include <iprt/rand.h>
|
---|
43 | #include <iprt/string.h>
|
---|
44 |
|
---|
45 | #include <VBox/log.h>
|
---|
46 | #include <VBox/version.h>
|
---|
47 |
|
---|
48 | /** No flags set. */
|
---|
49 | #define VBOX_EBMLWRITER_FLAG_NONE 0
|
---|
50 | /** The file handle was inherited. */
|
---|
51 | #define VBOX_EBMLWRITER_FLAG_HANDLE_INHERITED RT_BIT(0)
|
---|
52 |
|
---|
53 | class EBMLWriter
|
---|
54 | {
|
---|
55 | public:
|
---|
56 | typedef uint32_t EbmlClassId;
|
---|
57 |
|
---|
58 | private:
|
---|
59 |
|
---|
60 | struct EbmlSubElement
|
---|
61 | {
|
---|
62 | uint64_t offset;
|
---|
63 | EbmlClassId classId;
|
---|
64 | EbmlSubElement(uint64_t offs, EbmlClassId cid) : offset(offs), classId(cid) {}
|
---|
65 | };
|
---|
66 |
|
---|
67 | /** Stack of EBML sub elements. */
|
---|
68 | std::stack<EbmlSubElement> m_Elements;
|
---|
69 | /** The file's handle. */
|
---|
70 | RTFILE m_hFile;
|
---|
71 | /** The file's name (path). */
|
---|
72 | Utf8Str m_strFile;
|
---|
73 | /** Flags. */
|
---|
74 | uint32_t m_fFlags;
|
---|
75 |
|
---|
76 | public:
|
---|
77 |
|
---|
78 | EBMLWriter(void)
|
---|
79 | : m_hFile(NIL_RTFILE)
|
---|
80 | , m_fFlags(VBOX_EBMLWRITER_FLAG_NONE) { }
|
---|
81 |
|
---|
82 | virtual ~EBMLWriter(void) { close(); }
|
---|
83 |
|
---|
84 | public:
|
---|
85 |
|
---|
86 | int createEx(const char *a_pszFile, PRTFILE phFile);
|
---|
87 |
|
---|
88 | int create(const char *a_pszFile, uint64_t fOpen);
|
---|
89 |
|
---|
90 | void close(void);
|
---|
91 |
|
---|
92 | /** Returns the file name. */
|
---|
93 | const Utf8Str& getFileName(void) { return m_strFile; }
|
---|
94 |
|
---|
95 | /** Returns file size. */
|
---|
96 | uint64_t getFileSize(void) { return RTFileTell(m_hFile); }
|
---|
97 |
|
---|
98 | /** Get reference to file descriptor */
|
---|
99 | inline const RTFILE &getFile(void) { return m_hFile; }
|
---|
100 |
|
---|
101 | /** Returns available space on storage. */
|
---|
102 | uint64_t getAvailableSpace(void);
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * Returns whether the file is open or not.
|
---|
106 | *
|
---|
107 | * @returns True if open, false if not.
|
---|
108 | */
|
---|
109 | bool isOpen(void) { return RTFileIsValid(m_hFile); }
|
---|
110 |
|
---|
111 | public:
|
---|
112 |
|
---|
113 | EBMLWriter &subStart(EbmlClassId classId);
|
---|
114 |
|
---|
115 | EBMLWriter &subEnd(EbmlClassId classId);
|
---|
116 |
|
---|
117 | EBMLWriter &serializeString(EbmlClassId classId, const char *str);
|
---|
118 |
|
---|
119 | EBMLWriter &serializeUnsignedInteger(EbmlClassId classId, uint64_t parm, size_t size = 0);
|
---|
120 |
|
---|
121 | EBMLWriter &serializeFloat(EbmlClassId classId, float value);
|
---|
122 |
|
---|
123 | EBMLWriter &serializeData(EbmlClassId classId, const void *pvData, size_t cbData);
|
---|
124 |
|
---|
125 | int write(const void *data, size_t size);
|
---|
126 |
|
---|
127 | void writeUnsignedInteger(uint64_t value, size_t size = sizeof(uint64_t));
|
---|
128 |
|
---|
129 | void writeClassId(EbmlClassId parm);
|
---|
130 |
|
---|
131 | void writeSize(uint64_t parm);
|
---|
132 |
|
---|
133 | static inline size_t getSizeOfUInt(uint64_t arg);
|
---|
134 |
|
---|
135 | private:
|
---|
136 |
|
---|
137 | void operator=(const EBMLWriter &);
|
---|
138 | };
|
---|
139 |
|
---|
140 | #endif
|
---|
141 |
|
---|