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