XiphExtradata.cpp (2931B)
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 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++) { 18 size_t headerLen; 19 for (headerLen = aHeaderLens[i]; headerLen >= 255; headerLen -= 255) { 20 aCodecSpecificConfig->AppendElement(255); 21 } 22 aCodecSpecificConfig->AppendElement(headerLen); 23 } 24 for (size_t i = 0; i < nheaders; i++) { 25 aCodecSpecificConfig->AppendElements(aHeaders[i], aHeaderLens[i]); 26 } 27 return true; 28 } 29 30 bool XiphExtradataToHeaders(nsTArray<unsigned char*>& aHeaders, 31 nsTArray<size_t>& aHeaderLens, unsigned char* aData, 32 size_t aAvailable) { 33 size_t total = 0; 34 if (aAvailable < 1) { 35 return false; 36 } 37 aAvailable--; 38 int nHeaders = *aData++ + 1; 39 for (int i = 0; i < nHeaders - 1; i++) { 40 size_t headerLen = 0; 41 for (;;) { 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 46 // never underflow. 47 if (aAvailable - total <= headerLen) { 48 return false; 49 } 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. 56 headerLen += *aData; 57 aAvailable--; 58 if (*aData++ != 255) break; 59 } 60 // And this check ensures updating total won't cause (aAvailable - total) 61 // to underflow. 62 if (aAvailable - total < headerLen) { 63 return false; 64 } 65 aHeaderLens.AppendElement(headerLen); 66 // Since we know aAvailable >= total + headerLen, this can't overflow. 67 total += headerLen; 68 } 69 aHeaderLens.AppendElement(aAvailable - total); 70 for (int i = 0; i < nHeaders; i++) { 71 aHeaders.AppendElement(aData); 72 aData += aHeaderLens[i]; 73 } 74 return true; 75 } 76 77 } // namespace mozilla