Skip to content

Commit

Permalink
use a single constructor for the EbmlId
Browse files Browse the repository at this point in the history
When we have a buffer we can convert it to an Id value.
  • Loading branch information
robUx4 committed Jan 27, 2024
1 parent ccd2697 commit 18d05d4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 19 deletions.
26 changes: 10 additions & 16 deletions ebml/EbmlId.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,6 @@ namespace libebml {
*/
class EBML_DLL_API EbmlId {
public:
constexpr EbmlId(const binary aValue[4], const unsigned int aLength)
:Value(FromBuffer(aValue, aLength))
,Length(LengthFromValue(Value))
{
}

constexpr EbmlId(const std::uint32_t aValue)
:Value(aValue), Length(LengthFromValue(aValue)) {}

Expand Down Expand Up @@ -60,6 +54,16 @@ class EBML_DLL_API EbmlId {
return Value >= 0x10000000;
}

static constexpr std::uint32_t FromBuffer(const binary aValue[4], const unsigned int aLength)
{
std::uint32_t Value = 0;
for (unsigned int i=0; i<aLength; i++) {
Value <<= 8;
Value += aValue[i];
}
return Value;
}

private:
std::uint32_t Value;
std::size_t Length;
Expand All @@ -73,16 +77,6 @@ class EBML_DLL_API EbmlId {
return 3;
return 4;
}

static constexpr std::uint32_t FromBuffer(const binary aValue[4], const unsigned int aLength)
{
std::uint32_t Value = 0;
for (unsigned int i=0; i<aLength; i++) {
Value <<= 8;
Value += aValue[i];
}
return Value;
}
};

} // namespace libebml
Expand Down
4 changes: 2 additions & 2 deletions src/EbmlElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ EbmlElement * EbmlElement::FindNextID(IOCallback & DataStream, const EbmlCallbac
} while (_SizeLength == 0);
}

const auto PossibleID = EbmlId(PossibleId.data(), PossibleID_Length);
const auto PossibleID = EbmlId(EbmlId::FromBuffer(PossibleId.data(), PossibleID_Length));
auto Result = [=] {
if (PossibleID != EBML_INFO_ID(ClassInfos))
{
Expand Down Expand Up @@ -327,7 +327,7 @@ EbmlElement * EbmlElement::FindNextElement(IOCallback & DataStream, const EbmlSe

if (bFound) {
// find the element in the context and use the correct creator
const auto PossibleID = EbmlId(PossibleIdNSize.data(), PossibleID_Length);
const auto PossibleID = EbmlId(EbmlId::FromBuffer(PossibleIdNSize.data(), PossibleID_Length));
EbmlElement * Result = CreateElementUsingContext(PossibleID, Context, UpperLevel, false, SizeFound == SizeUnknown, AllowDummyElt, MaxLowerLevel);
///< \todo continue is misplaced
if (Result != nullptr) {
Expand Down
2 changes: 1 addition & 1 deletion test/test_id.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class KaxTracks {
int main(void)
{
constexpr binary buf[4] = { 0x12, 0x34, 0x56, 0x78 };
EbmlId frombuf(buf, 4);
EbmlId frombuf(EbmlId::FromBuffer(buf, 4));

EbmlId fromint(0x12345678);

Expand Down

0 comments on commit 18d05d4

Please sign in to comment.