BitFieldParser.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * Copyright 2015, The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef ANDROID_AUDIO_BIT_FIELD_PARSER_H
  17. #define ANDROID_AUDIO_BIT_FIELD_PARSER_H
  18. #include <stdint.h>
  19. namespace android {
  20. /**
  21. * Extract bit fields from a byte array.
  22. */
  23. class BitFieldParser {
  24. public:
  25. explicit BitFieldParser(uint8_t *data);
  26. virtual ~BitFieldParser();
  27. /**
  28. * Read numBits bits from the data array.
  29. * Fields may span byte boundaries but may not exceed 32-bits.
  30. * Note that the caller must ensure that there is suffcient data.
  31. * Assume data is organized as BigEndian format.
  32. */
  33. uint32_t readBits(uint32_t numBits);
  34. /*
  35. * When the cursor is zero it points to a position right before
  36. * the most significant bit.
  37. * When the cursor is seven it points to a position right before
  38. * the least significant bit.
  39. */
  40. uint32_t getBitCursor() const { return mBitCursor; }
  41. private:
  42. uint8_t *mData;
  43. uint32_t mBitCursor;
  44. };
  45. } // namespace android
  46. #endif // ANDROID_AUDIO_BIT_FIELD_PARSER_H