rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* 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 | #include <cstring> |
| 36 | #include <algorithm> |
| 37 | #include "BitwiseOutputStream.h" |
| 38 | using namespace std; |
| 39 | |
| 40 | BitwiseOutputStream::BitwiseOutputStream(int startingLength) { |
| 41 | mBuf = new uint8_t[startingLength]; |
| 42 | memset(mBuf, 0, startingLength * sizeof(uint8_t)); |
| 43 | mEnd = startingLength << 3; |
| 44 | mPos = 0; |
| 45 | } |
| 46 | |
| 47 | uint8_t* BitwiseOutputStream::toByteArray(uint32_t* length) { |
| 48 | int len = (mPos >> 3) + ((mPos & 0x07) > 0 ? 1 : 0); // &7==%8 |
| 49 | uint8_t* newBuf = new uint8_t[len]; |
| 50 | memset(newBuf, 0, len * sizeof(uint8_t)); |
| 51 | memcpy(newBuf, mBuf, len * sizeof(uint8_t)); |
| 52 | (*length) = len; |
| 53 | return newBuf; |
| 54 | } |
| 55 | |
| 56 | std::vector<uint8_t> BitwiseOutputStream::toByteVector() { |
| 57 | int len = (mPos >> 3) + ((mPos & 0x07) > 0 ? 1 : 0); // &7==%8 |
| 58 | std::vector<uint8_t> v(mBuf, mBuf + len); |
| 59 | return v; |
| 60 | } |
| 61 | |
| 62 | void BitwiseOutputStream::possExpand(uint32_t bits) { |
| 63 | if ((mPos + bits) < mEnd) |
| 64 | return; |
| 65 | uint8_t* newBuf = new uint8_t[(mPos + bits) >> 2]; |
| 66 | memset(newBuf, 0, mEnd >> 3); |
| 67 | memcpy(newBuf, mBuf, mEnd >> 3); |
| 68 | delete[] mBuf; |
| 69 | mBuf = nullptr; |
| 70 | mBuf = newBuf; |
| 71 | mEnd = (mEnd >> 3) << 3; |
| 72 | } |
| 73 | |
| 74 | void BitwiseOutputStream::write(uint32_t bits, uint32_t data) { |
| 75 | if ((bits < 0) || (bits > 8)) { |
| 76 | //TBD,log |
| 77 | return; |
| 78 | } |
| 79 | possExpand(bits); |
| 80 | data &= (0xFFFFFFFF >> (32 - bits)); |
| 81 | uint32_t index = mPos >> 3; |
| 82 | uint32_t offset = 16 - (mPos & 0x07) - bits; // &7==%8 |
| 83 | data <<= offset; |
| 84 | mPos += bits; |
| 85 | mBuf[index] |= data >> 8; |
| 86 | if (offset < 8) |
| 87 | mBuf[index + 1] |= data & 0xFF; |
| 88 | } |
| 89 | |
| 90 | void BitwiseOutputStream::writeByteArray(uint32_t bits, uint8_t arr[], |
| 91 | uint32_t size) { |
| 92 | for (uint32_t i = 0; i < size; i++) { |
| 93 | uint32_t increment = std::min((uint32_t) 8, bits - (i << 3)); |
| 94 | if (increment > 0) { |
| 95 | write(increment, (uint8_t) (arr[i] >> (8 - increment))); |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | void BitwiseOutputStream::writeByteVector(uint32_t bits, |
| 101 | std::vector<uint8_t> v) { |
| 102 | for (uint32_t i = 0; i < v.size(); i++) { |
| 103 | uint32_t increment = std::min((uint32_t) 8, bits - (i << 3)); |
| 104 | if (increment > 0) { |
| 105 | write(increment, (uint8_t) (v[i] >> (8 - increment))); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | void BitwiseOutputStream::skip(uint32_t bits) { |
| 111 | possExpand(bits); |
| 112 | mPos += bits; |
| 113 | } |
| 114 | BitwiseOutputStream::~BitwiseOutputStream() { |
| 115 | delete[] mBuf; |
| 116 | mBuf = nullptr; |
| 117 | } |
| 118 | |