• 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 "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 && blob->length_ > 0) {
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 (start < 0) {
44                 HILOG_ERROR("Blob:: start position error");
45                 return;
46             }
47             unsigned int uStart = static_cast<unsigned int>(start);
48             if (uStart >= blob->length_) {
49                 HILOG_ERROR("Blob:: start position error");
50                 return;
51             }
52             if ((blob->raw_ + start) == nullptr) {
53                 HILOG_FATAL("Blob:: constructor(start) memcpy_s failed");
54                 return;
55             }
56             this->length_ = blob->length_;
57             if (memcpy_s(raw_, blob->length_ - start, blob->raw_ + start, blob->length_ - start) != EOK) {
58                 HILOG_FATAL("Blob:: constructor(start) memcpy_s failed");
59             }
60         }
61     }
62 }
63 
Init(Blob * blob,int start,int end)64 void Blob::Init(Blob *blob, int start, int end)
65 {
66     if (start > end) {
67         return;
68     }
69     if ((start > 0 && end < 0) || (start < 0 && end > 0)) {
70         return;
71     }
72     if (blob == nullptr) {
73         return;
74     }
75     unsigned int length = static_cast<unsigned int>(end - start);
76     length = blob->length_ > length ? length : blob->length_;
77     this->raw_ = reinterpret_cast<uint8_t *>(malloc(length));
78     if (raw_ == nullptr) {
79         HILOG_FATAL("Blob:: constructor malloc failed");
80     } else {
81         this->length_ = length;
82         if (start >= 0) {
83             if ((blob->raw_ + start) == nullptr) {
84                 HILOG_FATAL("Blob:: constructor(start >= 0, end) memcpy_s failed");
85                 return;
86             }
87             if (memcpy_s(this->raw_, length, blob->raw_ + start, length) != EOK) {
88                 HILOG_FATAL("Blob:: constructor(start >= 0, end) memcpy_s failed");
89             }
90         } else {
91             if ((blob->raw_ + blob->length_ + start) == nullptr) {
92                 HILOG_FATAL("Blob:: constructor(start, end) memcpy_s failed");
93                 return;
94             }
95             if (memcpy_s(raw_, length, blob->raw_ + blob->length_ + start, length) != EOK) {
96                 HILOG_FATAL("Blob:: constructor(start, end) memcpy_s failed");
97             }
98         }
99     }
100 }
101 
~Blob()102 Blob::~Blob()
103 {
104     if (raw_ != nullptr) {
105         free(raw_);
106         raw_ = nullptr;
107     }
108 }
109 
GetByte(int index)110 uint8_t Blob::GetByte(int index)
111 {
112     return *(this->raw_ + index);
113 }
114 
GetRaw()115 uint8_t *Blob::GetRaw()
116 {
117     return this->raw_;
118 }
119 
GetLength()120 unsigned int Blob::GetLength()
121 {
122     return this->length_;
123 }
124 
ReadBytes(uint8_t * data,int length)125 void Blob::ReadBytes(uint8_t *data, int length)
126 {
127     if (raw_ == nullptr) {
128         HILOG_FATAL("Blob:: raw_ is nullptr");
129         return;
130     }
131     if (memcpy_s(data, length, raw_, length) != EOK) {
132         HILOG_FATAL("Blob:: read bytes from blob error");
133     }
134 }
135 } // namespace OHOS::buffer