blob: ae97422bd5b358e80686b02a5b9ad2a918401597 [file]
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include "jumbf/internal/parse_embedded_file_description_box.h"
#include <cstdint>
#include "absl/status/status.h"
#include "absl/status/status_macros.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "jumbf/box.h"
#include "jumbf/constants.h"
#include "jumbf/internal/intermediate.h"
#include "jumbf/internal/serialization_utils.h"
namespace jumbf_internal {
absl::StatusOr<jumbf::EmbeddedFileDescriptionBox>
ParseEmbeddedFileDescriptionBox(const IntermediateBox& unwrapped_box) {
if (unwrapped_box.type != jumbf::kEmbeddedFileDescriptionBoxType) {
return absl::InvalidArgumentError(absl::StrCat(
"box is not an embedded file description box; got type 0x",
absl::Hex(unwrapped_box.type), " need 0x",
absl::Hex(jumbf::kEmbeddedFileDescriptionBoxType), " ('bfdb')"));
}
absl::string_view temp_payload = unwrapped_box.payload;
jumbf::EmbeddedFileDescriptionBox efdb;
ABSL_ASSIGN_OR_RETURN(auto toggles, ConsumeInteger<uint8_t>(&temp_payload),
_.SetPrepend() << "could not read toggles: ");
ABSL_ASSIGN_OR_RETURN(efdb.media_type, ConsumeNullTerminated(&temp_payload),
_.SetPrepend() << "could not read media type: ");
if (toggles & jumbf::kEfdbToggleFileNamePresent) {
ABSL_ASSIGN_OR_RETURN(efdb.file_name, ConsumeNullTerminated(&temp_payload),
_.SetPrepend() << "could not read file name: ");
}
if (toggles & jumbf::kEfdbToggleExternal) {
efdb.external = true;
}
if (!temp_payload.empty()) {
return absl::InvalidArgumentError(
absl::StrCat("excess data in embedded file description box payload; "
"after parsing complete, valid payload, ",
temp_payload.length(), " bytes were remaining"));
}
return efdb;
}
} // namespace jumbf_internal