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