3rdparty/licenseReport: Add seperate LGPL checks
[haiku.git] / src / add-ons / media / plugins / mp4_reader / libMP4 / BitParser.cpp
blob86a9e9a587449c654f78d2449d80aef3789c2e40
1 /*
2 * Copyright (c) 2010, David McPaul
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
18 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
21 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
22 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
23 * OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "BitParser.h"
28 #include <math.h>
30 BitParser::BitParser(uint8 *bits, uint32 bitLength) {
31 this->bits = bits;
32 this->bitLength = bitLength;
33 this->index = 0;
36 BitParser::BitParser() {
37 bits = NULL;
38 bitLength = 0;
39 index = 1;
42 BitParser::~BitParser() {
43 bits = NULL;
46 void
47 BitParser::Init(uint8 *bits, uint32 bitLength) {
48 this->bits = bits;
49 this->bitLength = bitLength;
50 this->index = 1;
53 void
54 BitParser::Skip(uint8 length) {
55 if (bitLength >= length) {
57 while (length-- > 0) {
58 index++;
59 bitLength--;
61 if (index > 8) {
62 index = 1;
63 bits++;
69 uint32
70 BitParser::GetValue(uint8 length) {
71 uint32 result = 0;
73 if (bitLength < length) {
74 return 0;
77 while (length-- > 0) {
78 result = result << 1;
79 result += (*bits) & (uint8)(pow(2, 8-index)) ? 1 : 0;
80 index++;
81 bitLength--;
83 if (index > 8) {
84 index = 1;
85 bits++;
89 return result;