media/libstagefright/binding/include/mp4_demuxer/ByteReader.h
author Anthony Jones <ajones@mozilla.com>
Fri, 18 Jul 2014 14:37:43 +1200
changeset 216747 daa82439f77ff7bac1b577e98b7773ce04bcb3c4
parent 216468 93e0cd8e8cb78c39720c920eb69a000c76262091
child 216751 a9d2ca71f904b9fbd8afaec44e8f662cd121f124
permissions -rw-r--r--
Bug 1022434 - Extract crypto information from MP4 demuxer; r=cpearce * * * Bug 1022434 - "[EME] Expose decryption data in MP4Samples" []

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef BYTE_READER_H_
#define BYTE_READER_H_

#include "mozilla/Vector.h"
#include "nsTArray.h"

namespace mp4_demuxer
{

class ByteReader
{
public:
  ByteReader(const mozilla::Vector<uint8_t>& aData)
    : mPtr(&aData[0]), mRemaining(aData.length())
  {
  }
  ByteReader(const uint8_t* aData, size_t aSize)
    : mPtr(aData), mRemaining(aSize)
  {
  }

  ~ByteReader()
  {
    MOZ_ASSERT(!mRemaining);
  }

  // Make it explicit if we're not using the extra bytes.
  void DiscardRemaining() {
    mRemaining = 0;
  }

  size_t Remaining() const { return mRemaining; }

  bool CanRead8() { return mRemaining >= 1; }

  uint8_t ReadU8()
  {
    auto ptr = Read(1);
    if (!ptr) {
      MOZ_ASSERT(false);
      return 0;
    }
    return *ptr;
  }

  bool CanRead16() { return mRemaining >= 2; }

  uint16_t ReadU16()
  {
    auto ptr = Read(2);
    if (!ptr) {
      MOZ_ASSERT(false);
      return 0;
    }
    return ptr[0] << 8 | ptr[1];
  }

  const uint8_t* Read(size_t aCount)
  {
    if (aCount > mRemaining) {
      mRemaining = 0;
      return nullptr;
    }
    mRemaining -= aCount;

    const uint8_t* result = mPtr;
    mPtr += aCount;

    return result;
  }

  template <typename T> bool CanReadType() { return mRemaining >= sizeof(T); }

  template <typename T> T ReadType()
  {
    auto ptr = Read(sizeof(T));
    if (!ptr) {
      MOZ_ASSERT(false);
      return 0;
    }
    return *reinterpret_cast<const T*>(ptr);
  }

  template <typename T>
  bool ReadArray(nsTArray<T>& aDest, size_t aLength)
  {
    auto ptr = Read(aLength * sizeof(T));
    if (!ptr) {
      return false;
    }

    aDest.Clear();
    aDest.AppendElements(reinterpret_cast<const T*>(ptr), aLength);
    return true;
  }

private:
  const uint8_t* mPtr;
  size_t mRemaining;
};
}

#endif