tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

Box.h (3010B)


      1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
      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 #ifndef BOX_H_
      8 #define BOX_H_
      9 
     10 #include <stdint.h>
     11 
     12 #include "AtomType.h"
     13 #include "BufferReader.h"
     14 #include "MediaResource.h"
     15 #include "nsTArray.h"
     16 
     17 namespace mozilla {
     18 class ByteStream;
     19 
     20 class BumpAllocator {
     21 public:
     22  uint8_t* Allocate(size_t aNumBytes);
     23 
     24 private:
     25  nsTArray<nsTArray<uint8_t>> mBuffers;
     26 };
     27 
     28 class BoxContext {
     29 public:
     30  BoxContext(ByteStream* aSource, const MediaByteRangeSet& aByteRanges)
     31      : mSource(aSource), mByteRanges(aByteRanges) {}
     32 
     33  RefPtr<ByteStream> mSource;
     34  const MediaByteRangeSet& mByteRanges;
     35  BumpAllocator mAllocator;
     36 };
     37 
     38 struct ByteSlice {
     39  const uint8_t* mBytes;
     40  size_t mSize;
     41 };
     42 
     43 class Box {
     44 public:
     45  Box(BoxContext* aContext, uint64_t aOffset, const Box* aParent = nullptr);
     46  Box();
     47  nsresult InitStatus() const { return mInitStatus; }
     48 
     49  bool IsAvailable() const { return !mRange.IsEmpty(); }
     50  uint64_t Offset() const { return mRange.mStart; }
     51  uint64_t Length() const { return mRange.mEnd - mRange.mStart; }
     52  uint64_t NextOffset() const { return mRange.mEnd; }
     53  const MediaByteRange& Range() const { return mRange; }
     54  const Box* Parent() const { return mParent; }
     55  bool IsType(const char* aType) const { return mType == AtomType(aType); }
     56 
     57  Box Next() const;
     58  Box FirstChild() const;
     59  // Reads the box contents, excluding the header.
     60  nsTArray<uint8_t> Read() const;
     61 
     62  // Reads the complete box; its header and body.
     63  nsTArray<uint8_t> ReadCompleteBox() const;
     64 
     65  // Reads from the content of the box, excluding header.
     66  bool Read(nsTArray<uint8_t>* aDest, const MediaByteRange& aRange) const;
     67 
     68  static const uint64_t kMAX_BOX_READ;
     69 
     70  // Returns a slice, pointing to the data of this box. The lifetime of
     71  // the memory this slice points to matches the box's context's lifetime.
     72  ByteSlice ReadAsSlice() const;
     73 
     74 private:
     75  bool Contains(MediaByteRange aRange) const;
     76  BoxContext* mContext;
     77  mozilla::MediaByteRange mRange;
     78  uint64_t mBodyOffset;
     79  uint64_t mChildOffset;
     80  AtomType mType;
     81  const Box* mParent;
     82  nsresult mInitStatus = NS_ERROR_NOT_INITIALIZED;
     83 };
     84 
     85 // BoxReader serves box data through an AutoByteReader. The box data is
     86 // stored either in the box's context's bump allocator, or in the ByteStream
     87 // itself if the ByteStream implements the Access() method.
     88 // NOTE: The data the BoxReader reads may be stored in the Box's BoxContext.
     89 // Ensure that the BoxReader doesn't outlive the BoxContext!
     90 class MOZ_RAII BoxReader {
     91 public:
     92  explicit BoxReader(const Box& aBox)
     93      : mData(aBox.ReadAsSlice()), mReader(mData.mBytes, mData.mSize) {}
     94  BufferReader* operator->() { return &mReader; }
     95 
     96 private:
     97  ByteSlice mData;
     98  BufferReader mReader;
     99 };
    100 }  // namespace mozilla
    101 
    102 #endif