• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "commonlibrary/ets_utils/js_api_module/buffer/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             this->length_ = blob->length_;
44             if (memcpy_s(raw_, blob->length_ - start, blob->raw_ + start, blob->length_ - start) != EOK) {
45                 HILOG_FATAL("Blob constructor(start) memcpy_s failed");
46             }
47         }
48     }
49 }
50 
Init(Blob * blob,int start,int end)51 void Blob::Init(Blob *blob, int start, int end)
52 {
53     if (start > end) {
54         return;
55     }
56     if ((start > 0 && end < 0) || (start < 0 && end > 0)) {
57         return;
58     }
59     if (blob == nullptr) {
60         return;
61     }
62     unsigned int length = static_cast<unsigned int>(end - start);
63     length = blob->length_ > length ? length : blob->length_;
64     this->raw_ = reinterpret_cast<uint8_t *>(malloc(length));
65     if (raw_ == nullptr) {
66         HILOG_FATAL("Blob constructor malloc failed");
67     } else {
68         this->length_ = length;
69         if (start >= 0) {
70             if (memcpy_s(this->raw_, length, blob->raw_ + start, length) != EOK) {
71                 HILOG_FATAL("Blob constructor(start >= 0, end) memcpy_s failed");
72             }
73         } else {
74             if (memcpy_s(raw_, length, blob->raw_ + blob->length_ + start, length) != EOK) {
75                 HILOG_FATAL("Blob constructor(start, end) memcpy_s failed");
76             }
77         }
78     }
79 }
80 
~Blob()81 Blob::~Blob()
82 {
83     if (raw_ != nullptr) {
84         free(raw_);
85         raw_ = nullptr;
86     }
87 }
88 
GetByte(int index)89 uint8_t Blob::GetByte(int index)
90 {
91     return *(this->raw_ + index);
92 }
93 
GetRaw()94 uint8_t *Blob::GetRaw()
95 {
96     return this->raw_;
97 }
98 
GetLength()99 unsigned int Blob::GetLength()
100 {
101     return this->length_;
102 }
103 
ReadBytes(uint8_t * data,int length)104 void Blob::ReadBytes(uint8_t *data, int length)
105 {
106     if (memcpy_s(data, length, raw_, length) != EOK) {
107         HILOG_FATAL("read bytes from blob error");
108     }
109 }
110 } // namespace OHOS::buffer