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/. */
7 #include "XiphExtradata.h"
11 bool XiphHeadersToExtradata(MediaByteBuffer
* aCodecSpecificConfig
,
12 const nsTArray
<const unsigned char*>& aHeaders
,
13 const nsTArray
<size_t>& aHeaderLens
) {
14 size_t nheaders
= aHeaders
.Length();
15 if (!nheaders
|| nheaders
> 255) return false;
16 aCodecSpecificConfig
->AppendElement(nheaders
- 1);
17 for (size_t i
= 0; i
< nheaders
- 1; i
++) {
19 for (headerLen
= aHeaderLens
[i
]; headerLen
>= 255; headerLen
-= 255) {
20 aCodecSpecificConfig
->AppendElement(255);
22 aCodecSpecificConfig
->AppendElement(headerLen
);
24 for (size_t i
= 0; i
< nheaders
; i
++) {
25 aCodecSpecificConfig
->AppendElements(aHeaders
[i
], aHeaderLens
[i
]);
30 bool XiphExtradataToHeaders(nsTArray
<unsigned char*>& aHeaders
,
31 nsTArray
<size_t>& aHeaderLens
, unsigned char* aData
,
38 int nHeaders
= *aData
++ + 1;
39 for (int i
= 0; i
< nHeaders
- 1; i
++) {
42 // After this test, we know that (aAvailable - total > headerLen) and
43 // (headerLen >= 0) so (aAvailable - total > 0). The loop decrements
44 // aAvailable by 1 and total remains fixed, so we know that in the next
45 // iteration (aAvailable - total >= 0). Thus (aAvailable - total) can
47 if (aAvailable
- total
<= headerLen
) {
50 // Since we know (aAvailable > total + headerLen), this can't overflow
51 // unless total is near 0 and both aAvailable and headerLen are within
52 // 255 bytes of the maximum representable size. However, that is
53 // impossible, since we would have had to have gone through this loop
54 // more than 255 times to make headerLen that large, and thus decremented
55 // aAvailable more than 255 times.
58 if (*aData
++ != 255) break;
60 // And this check ensures updating total won't cause (aAvailable - total)
62 if (aAvailable
- total
< headerLen
) {
65 aHeaderLens
.AppendElement(headerLen
);
66 // Since we know aAvailable >= total + headerLen, this can't overflow.
69 aHeaderLens
.AppendElement(aAvailable
- total
);
70 for (int i
= 0; i
< nHeaders
; i
++) {
71 aHeaders
.AppendElement(aData
);
72 aData
+= aHeaderLens
[i
];
77 } // namespace mozilla