blob: 9e8d6263a324a43e4dbd87c5c7df050306f39b63 [file] [log] [blame]
xjde81d1d2021-11-25 15:01:52 +08001/* Copyright Statement:
2 *
3 * This software/firmware and related documentation ("MediaTek Software") are
4 * protected under relevant copyright laws. The information contained herein
5 * is confidential and proprietary to MediaTek Inc. and/or its licensors.
6 * Without the prior written permission of MediaTek inc. and/or its licensors,
7 * any reproduction, modification, use or disclosure of MediaTek Software,
8 * and information contained herein, in whole or in part, shall be strictly prohibited.
9 */
10/* MediaTek Inc. (C) 2010. All rights reserved.
11 *
12 * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
13 * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
14 * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
15 * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
18 * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
19 * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
20 * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
21 * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
22 * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
23 * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
24 * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
25 * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
26 * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
27 * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
28 * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
29 * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
30 *
31 * The following software/firmware and/or related documentation ("MediaTek Software")
32 * have been modified by MediaTek Inc. All revisions are subject to any receiver's
33 * applicable license agreements with MediaTek Inc.
34 */
35
36#include "BitwiseInputStream.h"
37#include <algorithm>
38using namespace std;
39
40BitwiseInputStream::BitwiseInputStream(std::vector<uint8_t> buf) {
41 mBuf = buf;
42 mEnd = buf.size() << 3;
43 mPos = 0;
44
45}
46
47BitwiseInputStream::~BitwiseInputStream() {
48 // TODO Auto-generated destructor stub
49}
50
51int BitwiseInputStream::available() {
52 return mEnd - mPos;
53}
54
55uint32_t BitwiseInputStream::read(uint32_t bits) {
56 uint32_t index = mPos >> 3;
57 uint32_t offset = 16 - (mPos & 0x07) - bits; // &7==%8
58 if ((bits < 0) || (bits > 8) || ((mPos + bits) > mEnd)) {
59 //TDB
60 return -1;
61 }
62 uint32_t data = (mBuf[index] & 0xFF) << 8;
63 if (offset < 8)
64 data |= mBuf[index + 1] & 0xFF;
65 data >>= offset;
66 data &= (0xFFFFFFFF >> (32 - bits));
67 mPos += bits;
68 return data;
69}
70
71//uint8_t* BitwiseInputStream::readByteArray(uint32_t bits, int* length) {
72// uint32_t bytes = (bits >> 3) + ((bits & 0x07) > 0 ? 1 : 0); // &7==%8
73// uint8_t* arr = new uint8_t[bytes];
74// for (int i = 0; i < bytes; i++) {
75// int increment = std::min((uint32_t) 8, bits - (i << 3));
76// arr[i] = (uint8_t) (read(increment) << (8 - increment));
77// }
78// return arr;
79//}
80
81std::vector<uint8_t> BitwiseInputStream::readByteVector(uint32_t bits) {
82 uint32_t bytes = (bits >> 3) + ((bits & 0x07) > 0 ? 1 : 0); // &7==%8
83 std::vector<uint8_t> arr;
84 //uint8_t* arr = new uint8_t[bytes];
85 for (int i = 0; i < bytes; i++) {
86 int increment = std::min((uint32_t) 8, bits - (i << 3));
87 arr.push_back((uint8_t) (read(increment) << (8 - increment)));
88 //arr[i] = (uint8_t) (read(increment) << (8 - increment));
89 }
90 return arr;
91}
92
93void BitwiseInputStream::skip(uint32_t bits) {
94 if ((mPos + bits) > mEnd) {
95 //TBD log
96 return;
97 }
98 mPos += bits;
99}