decrypt_config_marshaller.cc revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
1// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chromecast/media/cma/ipc_streamer/decrypt_config_marshaller.h"
6
7#include "base/logging.h"
8#include "chromecast/media/cma/ipc/media_message.h"
9#include "media/base/decrypt_config.h"
10
11namespace chromecast {
12namespace media {
13
14namespace {
15const size_t kMaxKeyIdSize = 256;
16const size_t kMaxIvSize = 256;
17const size_t kMaxSubsampleCount = 1024;
18}
19
20// static
21void DecryptConfigMarshaller::Write(
22    const ::media::DecryptConfig& config, MediaMessage* msg) {
23  CHECK_GT(config.key_id().size(), 0);
24  CHECK_GT(config.iv().size(), 0);
25  CHECK_GT(config.subsamples().size(), 0);
26
27  CHECK(msg->WritePod(config.key_id().size()));
28  CHECK(msg->WriteBuffer(config.key_id().data(), config.key_id().size()));
29  CHECK(msg->WritePod(config.iv().size()));
30  CHECK(msg->WriteBuffer(config.iv().data(), config.iv().size()));
31  CHECK(msg->WritePod(config.subsamples().size()));
32  for (size_t k = 0; k < config.subsamples().size(); k++) {
33    CHECK(msg->WritePod(config.subsamples()[k].clear_bytes));
34    CHECK(msg->WritePod(config.subsamples()[k].cypher_bytes));
35  }
36}
37
38// static
39scoped_ptr< ::media::DecryptConfig> DecryptConfigMarshaller::Read(
40    MediaMessage* msg) {
41  size_t key_id_size = 0;
42  CHECK(msg->ReadPod(&key_id_size));
43  CHECK_GT(key_id_size, 0);
44  CHECK_LT(key_id_size, kMaxKeyIdSize);
45  scoped_ptr<char[]> key_id(new char[key_id_size]);
46  CHECK(msg->ReadBuffer(key_id.get(), key_id_size));
47
48  size_t iv_size = 0;
49  CHECK(msg->ReadPod(&iv_size));
50  CHECK_GT(iv_size, 0);
51  CHECK_LT(iv_size, kMaxIvSize);
52  scoped_ptr<char[]> iv(new char[iv_size]);
53  CHECK(msg->ReadBuffer(iv.get(), iv_size));
54
55  size_t subsample_count = 0;
56  CHECK(msg->ReadPod(&subsample_count));
57  CHECK_GT(subsample_count, 0);
58  CHECK_LT(subsample_count, kMaxSubsampleCount);
59  std::vector< ::media::SubsampleEntry> subsamples(subsample_count);
60  for (size_t k = 0; k < subsample_count; k++) {
61    subsamples[k].clear_bytes = 0;
62    subsamples[k].cypher_bytes = 0;
63    CHECK(msg->ReadPod(&subsamples[k].clear_bytes));
64    CHECK(msg->ReadPod(&subsamples[k].cypher_bytes));
65  }
66
67  return scoped_ptr< ::media::DecryptConfig>(
68      new ::media::DecryptConfig(
69          std::string(key_id.get(), key_id_size),
70          std::string(iv.get(), iv_size),
71          subsamples));
72}
73
74}  // namespace media
75}  // namespace chromecast
76