* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "utf8_utils.h"
namespace OHOS::Request::Utf8Utils {
namespace {
static constexpr size_t TWO_OCTET = 2;
static constexpr size_t THREE_OCTET = 3;
static constexpr size_t FOUR_OCTET = 4;
bool GetNextByte(const std::vector<uint8_t> &v, size_t &index, uint8_t &next)
{
index += 1;
if (index >= v.size()) {
return false;
}
next = v[index];
return true;
}
size_t Utf8CharWidth(uint8_t b)
{
static const size_t UTF8_CHAR_WIDTH[256] = {
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
return UTF8_CHAR_WIDTH[b];
}
bool Check2Bytes(const std::vector<uint8_t> &v, size_t &index)
{
uint8_t next = 0;
return GetNextByte(v, index, next) && (next >= 0x80 && next <= 0xBF);
}
bool Check3Bytes(const std::vector<uint8_t> &v, const size_t &first, size_t &index)
{
uint8_t next = 0;
if (!GetNextByte(v, index, next)) {
return false;
};
if (first == 0xE0 && next >= 0xA0 && next <= 0xBF) {
} else if (first >= 0xE1 && first <= 0xEC && next >= 0x80 && next <= 0xBF) {
} else if (first == 0xED && next >= 0x80 && next <= 0x9F) {
} else if (first >= 0xEE && first <= 0xEF && next >= 0x80 && next <= 0xBF) {
} else {
return false;
};
return Check2Bytes(v, index);
}
bool Check4Bytes(const std::vector<uint8_t> &v, const size_t &first, size_t &index)
{
uint8_t next = 0;
if (!GetNextByte(v, index, next)) {
return false;
};
if (first == 0xF0 && next >= 0x90 && next <= 0xBF) {
} else if (first >= 0xF1 && first <= 0xF3 && next >= 0x80 && next <= 0xBF) {
} else if (first == 0xF4 && next >= 0x80 && next <= 0x8F) {
} else {
return false;
}
return Check2Bytes(v, index) && Check2Bytes(v, index);
}
}
bool RunUtf8Validation(const std::vector<uint8_t> &v)
{
size_t index = 0;
size_t len = v.size();
while (index < len) {
uint8_t first = v[index];
if (first <= 0x7F) {
index += 1;
continue;
}
size_t w = Utf8CharWidth(first);
if (w == TWO_OCTET) {
if (!Check2Bytes(v, index)) {
return false;
}
} else if (w == THREE_OCTET) {
if (!Check3Bytes(v, first, index)) {
return false;
}
} else if (w == FOUR_OCTET) {
if (!Check4Bytes(v, first, index)) {
return false;
}
} else {
return false;
};
index += 1;
}
return true;
}
}