Line data Source code
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 0 : bool XiphHeadersToExtradata(MediaByteBuffer* aCodecSpecificConfig,
12 : const nsTArray<const unsigned char*>& aHeaders,
13 : const nsTArray<size_t>& aHeaderLens)
14 : {
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 : size_t headerLen;
20 0 : for (headerLen = aHeaderLens[i]; headerLen >= 255; headerLen -= 255) {
21 0 : aCodecSpecificConfig->AppendElement(255);
22 : }
23 0 : aCodecSpecificConfig->AppendElement(headerLen);
24 : }
25 0 : for (size_t i = 0; i < nheaders; i++) {
26 0 : aCodecSpecificConfig->AppendElements(aHeaders[i], aHeaderLens[i]);
27 : }
28 0 : return true;
29 : }
30 :
31 0 : bool XiphExtradataToHeaders(nsTArray<unsigned char*>& aHeaders,
32 : nsTArray<size_t>& aHeaderLens,
33 : unsigned char* aData,
34 : size_t aAvailable)
35 : {
36 0 : size_t total = 0;
37 0 : if (aAvailable < 1) {
38 0 : return false;
39 : }
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 : for (;;) {
45 : // After this test, we know that (aAvailable - total > headerLen) and
46 : // (headerLen >= 0) so (aAvailable - total > 0). The loop decrements
47 : // aAvailable by 1 and total remains fixed, so we know that in the next
48 : // iteration (aAvailable - total >= 0). Thus (aAvailable - total) can
49 : // never underflow.
50 0 : if (aAvailable - total <= headerLen) {
51 0 : return false;
52 : }
53 : // Since we know (aAvailable > total + headerLen), this can't overflow
54 : // unless total is near 0 and both aAvailable and headerLen are within
55 : // 255 bytes of the maximum representable size. However, that is
56 : // impossible, since we would have had to have gone through this loop
57 : // more than 255 times to make headerLen that large, and thus decremented
58 : // aAvailable more than 255 times.
59 0 : headerLen += *aData;
60 0 : aAvailable--;
61 0 : if (*aData++ != 255) break;
62 : }
63 : // And this check ensures updating total won't cause (aAvailable - total)
64 : // to underflow.
65 0 : if (aAvailable - total < headerLen) {
66 0 : return false;
67 : }
68 0 : aHeaderLens.AppendElement(headerLen);
69 : // Since we know aAvailable >= total + headerLen, this can't overflow.
70 0 : total += headerLen;
71 : }
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 : }
77 0 : return true;
78 : }
79 :
80 9 : } // namespace mozilla
|