/src/mozilla-central/dom/media/XiphExtradata.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #include "XiphExtradata.h" |
8 | | |
9 | | namespace mozilla { |
10 | | |
11 | | bool XiphHeadersToExtradata(MediaByteBuffer* aCodecSpecificConfig, |
12 | | const nsTArray<const unsigned char*>& aHeaders, |
13 | | const nsTArray<size_t>& aHeaderLens) |
14 | 0 | { |
15 | 0 | size_t nheaders = aHeaders.Length(); |
16 | 0 | if (!nheaders || nheaders > 255) return false; |
17 | 0 | aCodecSpecificConfig->AppendElement(nheaders - 1); |
18 | 0 | for (size_t i = 0; i < nheaders - 1; i++) { |
19 | 0 | size_t headerLen; |
20 | 0 | for (headerLen = aHeaderLens[i]; headerLen >= 255; headerLen -= 255) { |
21 | 0 | aCodecSpecificConfig->AppendElement(255); |
22 | 0 | } |
23 | 0 | aCodecSpecificConfig->AppendElement(headerLen); |
24 | 0 | } |
25 | 0 | for (size_t i = 0; i < nheaders; i++) { |
26 | 0 | aCodecSpecificConfig->AppendElements(aHeaders[i], aHeaderLens[i]); |
27 | 0 | } |
28 | 0 | return true; |
29 | 0 | } |
30 | | |
31 | | bool XiphExtradataToHeaders(nsTArray<unsigned char*>& aHeaders, |
32 | | nsTArray<size_t>& aHeaderLens, |
33 | | unsigned char* aData, |
34 | | size_t aAvailable) |
35 | 0 | { |
36 | 0 | size_t total = 0; |
37 | 0 | if (aAvailable < 1) { |
38 | 0 | return false; |
39 | 0 | } |
40 | 0 | aAvailable--; |
41 | 0 | int nHeaders = *aData++ + 1; |
42 | 0 | for (int i = 0; i < nHeaders - 1; i++) { |
43 | 0 | size_t headerLen = 0; |
44 | 0 | for (;;) { |
45 | 0 | // After this test, we know that (aAvailable - total > headerLen) and |
46 | 0 | // (headerLen >= 0) so (aAvailable - total > 0). The loop decrements |
47 | 0 | // aAvailable by 1 and total remains fixed, so we know that in the next |
48 | 0 | // iteration (aAvailable - total >= 0). Thus (aAvailable - total) can |
49 | 0 | // never underflow. |
50 | 0 | if (aAvailable - total <= headerLen) { |
51 | 0 | return false; |
52 | 0 | } |
53 | 0 | // Since we know (aAvailable > total + headerLen), this can't overflow |
54 | 0 | // unless total is near 0 and both aAvailable and headerLen are within |
55 | 0 | // 255 bytes of the maximum representable size. However, that is |
56 | 0 | // impossible, since we would have had to have gone through this loop |
57 | 0 | // more than 255 times to make headerLen that large, and thus decremented |
58 | 0 | // aAvailable more than 255 times. |
59 | 0 | headerLen += *aData; |
60 | 0 | aAvailable--; |
61 | 0 | if (*aData++ != 255) break; |
62 | 0 | } |
63 | 0 | // And this check ensures updating total won't cause (aAvailable - total) |
64 | 0 | // to underflow. |
65 | 0 | if (aAvailable - total < headerLen) { |
66 | 0 | return false; |
67 | 0 | } |
68 | 0 | aHeaderLens.AppendElement(headerLen); |
69 | 0 | // Since we know aAvailable >= total + headerLen, this can't overflow. |
70 | 0 | total += headerLen; |
71 | 0 | } |
72 | 0 | aHeaderLens.AppendElement(aAvailable - total); |
73 | 0 | for (int i = 0; i < nHeaders; i++) { |
74 | 0 | aHeaders.AppendElement(aData); |
75 | 0 | aData += aHeaderLens[i]; |
76 | 0 | } |
77 | 0 | return true; |
78 | 0 | } |
79 | | |
80 | | } // namespace mozilla |