Index: media/formats/webm/webm_crypto_helpers.cc |
diff --git a/media/formats/webm/webm_crypto_helpers.cc b/media/formats/webm/webm_crypto_helpers.cc |
index a483df9019cf342cb69856e1a60ab90e9b17b23d..dcdbc6106ae7d5b70deff6dba070bc6d3c28bc88 100644 |
--- a/media/formats/webm/webm_crypto_helpers.cc |
+++ b/media/formats/webm/webm_crypto_helpers.cc |
@@ -24,6 +24,61 @@ std::string GenerateWebMCounterBlock(const uint8_t* iv, int iv_size) { |
return counter_block; |
} |
+uint32_t ReadInteger(const uint8_t* buf, int size) { |
+ // Read in the big-endian integer. |
+ uint32_t value = 0; |
+ for (int i = 0; i < size; ++i) |
+ value = (value << 8) | buf[i]; |
+ return value; |
+} |
+ |
+bool ExtractSubsamples(const uint8_t* buf, |
+ size_t frame_data_size, |
+ size_t num_partitions, |
+ std::vector<SubsampleEntry>* subsample_entries) { |
+ subsample_entries->clear(); |
+ uint32_t partition_offset = 0; |
+ uint32_t clear_bytes = 0; |
+ // N partitions means N+1 sections (alternating between clear and cipher |
+ // sections). |
+ for (size_t i = 0, offset = 0; i <= num_partitions; ++i) { |
+ const uint32_t prev_partition_offset = partition_offset; |
+ partition_offset = |
+ (i == num_partitions) |
+ ? frame_data_size |
chcunningham
2016/07/29 21:32:41
I don't follow this bit. Is the offset for the las
chcunningham
2016/07/29 22:26:22
We chatted, and this works, I was just confused ab
kqyang
2016/08/02 00:07:23
Done.
|
+ : ReadInteger(buf + offset, kWebMEncryptedFramePartitionSize); |
+ offset += kWebMEncryptedFramePartitionSize; |
+ if (partition_offset < prev_partition_offset) { |
+ DVLOG(1) << "Partition should not be decreasing " << prev_partition_offset |
+ << " " << partition_offset; |
+ return false; |
+ } |
+ |
+ uint32_t encrypted_bytes = 0; |
+ bool new_subsample_entry = false; |
+ // Alternating clear and cipher sections. |
+ if ((i % 2) == 0) { |
+ clear_bytes = partition_offset - prev_partition_offset; |
+ // Generate a new subsample when finishing reading partitions. |
+ new_subsample_entry = i == num_partitions; |
+ } else { |
+ encrypted_bytes = partition_offset - prev_partition_offset; |
+ // Generate a new subsample after seeing a cipher section. |
+ new_subsample_entry = true; |
+ } |
+ |
+ if (new_subsample_entry) { |
+ if (clear_bytes == 0 && encrypted_bytes == 0) { |
+ DVLOG(1) << "Not expecting >2 partitions with the same offsets."; |
+ return false; |
+ } |
+ subsample_entries->push_back( |
+ SubsampleEntry(clear_bytes, encrypted_bytes)); |
+ } |
+ } |
+ return true; |
+} |
+ |
} // namespace anonymous |
bool WebMCreateDecryptConfig(const uint8_t* data, |
@@ -37,13 +92,14 @@ bool WebMCreateDecryptConfig(const uint8_t* data, |
return false; |
} |
- uint8_t signal_byte = data[0]; |
+ const uint8_t signal_byte = data[0]; |
int frame_offset = sizeof(signal_byte); |
// Setting the DecryptConfig object of the buffer while leaving the |
// initialization vector empty will tell the decryptor that the frame is |
// unencrypted. |
std::string counter_block; |
+ std::vector<SubsampleEntry> subsample_entries; |
if (signal_byte & kWebMFlagEncryptedFrame) { |
if (data_size < kWebMSignalByteSize + kWebMIvSize) { |
@@ -52,12 +108,38 @@ bool WebMCreateDecryptConfig(const uint8_t* data, |
} |
counter_block = GenerateWebMCounterBlock(data + frame_offset, kWebMIvSize); |
frame_offset += kWebMIvSize; |
+ |
+ if (signal_byte & kWebMFlagEncryptedFramePartitioned) { |
+ if (data_size < frame_offset + kWebMEncryptedFrameNumPartitionSize) { |
+ DVLOG(1) << "Got a partitioned encrypted block with not enough data " |
+ << data_size; |
+ return false; |
+ } |
+ |
+ const size_t num_partitions = data[frame_offset]; |
+ if (num_partitions == 0) { |
+ DVLOG(1) << "Got a partitioned encrypted block with 0 partitions."; |
+ return false; |
+ } |
+ frame_offset += kWebMEncryptedFrameNumPartitionSize; |
+ const uint8_t* partition_data_start = data + frame_offset; |
+ frame_offset += kWebMEncryptedFramePartitionSize * num_partitions; |
chcunningham
2016/07/29 21:32:41
kWebMEncryptedFramePartitionSize ... might be conf
kqyang
2016/08/02 00:07:23
Renamed to kWebMEncryptedFramePartitionOffsetSize
|
+ if (data_size <= frame_offset) { |
+ DVLOG(1) << "Got a partitioned encrypted block with " << num_partitions |
+ << " partitions but not enough data " << data_size; |
+ return false; |
+ } |
+ const size_t frame_data_size = data_size - frame_offset; |
+ if (!ExtractSubsamples(partition_data_start, frame_data_size, |
+ num_partitions, &subsample_entries)) { |
+ return false; |
+ } |
+ } |
} |
decrypt_config->reset(new DecryptConfig( |
std::string(reinterpret_cast<const char*>(key_id), key_id_size), |
- counter_block, |
- std::vector<SubsampleEntry>())); |
+ counter_block, subsample_entries)); |
*data_offset = frame_offset; |
return true; |