1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "js_blob.h"
17 #include "securec.h"
18 using namespace std;
19 
20 namespace OHOS::buffer {
Init(uint8_t * blob,unsigned int length)21 void Blob::Init(uint8_t *blob, unsigned int length)
22 {
23     if (blob != nullptr) {
24         this->raw_ = reinterpret_cast<uint8_t *>(malloc(length));
25         if (raw_ == nullptr) {
26             HILOG_FATAL("Blob constructor malloc failed");
27         } else {
28             this->length_ = length;
29             if (memcpy_s(raw_, length, blob, length) != EOK) {
30                 HILOG_FATAL("Blob constructor(length) memcpy_s failed");
31             }
32         }
33     }
34 }
35 
Init(Blob * blob,int start)36 void Blob::Init(Blob *blob, int start)
37 {
38     if (blob != nullptr) {
39         this->raw_ = reinterpret_cast<uint8_t *>(malloc(blob->length_));
40         if (raw_ == nullptr) {
41             HILOG_FATAL("Blob constructor malloc failed");
42         } else {
43             if ((blob->raw_ + start) == nullptr) {
44                 HILOG_FATAL("Blob constructor(start) memcpy_s failed");
45                 return;
46             }
47             this->length_ = blob->length_;
48             if (memcpy_s(raw_, blob->length_ - start, blob->raw_ + start, blob->length_ - start) != EOK) {
49                 HILOG_FATAL("Blob constructor(start) memcpy_s failed");
50             }
51         }
52     }
53 }
54 
Init(Blob * blob,int start,int end)55 void Blob::Init(Blob *blob, int start, int end)
56 {
57     if (start > end) {
58         return;
59     }
60     if ((start > 0 && end < 0) || (start < 0 && end > 0)) {
61         return;
62     }
63     if (blob == nullptr) {
64         return;
65     }
66     unsigned int length = static_cast<unsigned int>(end - start);
67     length = blob->length_ > length ? length : blob->length_;
68     this->raw_ = reinterpret_cast<uint8_t *>(malloc(length));
69     if (raw_ == nullptr) {
70         HILOG_FATAL("Blob constructor malloc failed");
71     } else {
72         this->length_ = length;
73         if (start >= 0) {
74             if ((blob->raw_ + start) == nullptr) {
75                 HILOG_FATAL("Blob constructor(start >= 0, end) memcpy_s failed");
76                 return;
77             }
78             if (memcpy_s(this->raw_, length, blob->raw_ + start, length) != EOK) {
79                 HILOG_FATAL("Blob constructor(start >= 0, end) memcpy_s failed");
80             }
81         } else {
82             if ((blob->raw_ + blob->length_ + start) == nullptr) {
83                 HILOG_FATAL("Blob constructor(start, end) memcpy_s failed");
84                 return;
85             }
86             if (memcpy_s(raw_, length, blob->raw_ + blob->length_ + start, length) != EOK) {
87                 HILOG_FATAL("Blob constructor(start, end) memcpy_s failed");
88             }
89         }
90     }
91 }
92 
~Blob()93 Blob::~Blob()
94 {
95     if (raw_ != nullptr) {
96         free(raw_);
97         raw_ = nullptr;
98     }
99 }
100 
GetByte(int index)101 uint8_t Blob::GetByte(int index)
102 {
103     return *(this->raw_ + index);
104 }
105 
GetRaw()106 uint8_t *Blob::GetRaw()
107 {
108     return this->raw_;
109 }
110 
GetLength()111 unsigned int Blob::GetLength()
112 {
113     return this->length_;
114 }
115 
ReadBytes(uint8_t * data,int length)116 void Blob::ReadBytes(uint8_t *data, int length)
117 {
118     if (raw_ == nullptr) {
119         HILOG_FATAL("blob is null");
120         return;
121     }
122     if (memcpy_s(data, length, raw_, length) != EOK) {
123         HILOG_FATAL("read bytes from blob error");
124     }
125 }
126 } // namespace OHOS::buffer