14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ci#ifndef ECMASCRIPT_REGEXP_DYN_BUFFER_H
174514f5e3Sopenharmony_ci#define ECMASCRIPT_REGEXP_DYN_BUFFER_H
184514f5e3Sopenharmony_ci
194514f5e3Sopenharmony_ci#include <cstring>
204514f5e3Sopenharmony_ci
214514f5e3Sopenharmony_ci#include "ecmascript/common.h"
224514f5e3Sopenharmony_ci#include "ecmascript/mem/regexp_cached_chunk.h"
234514f5e3Sopenharmony_ci#include "ecmascript/mem/chunk.h"
244514f5e3Sopenharmony_ci
254514f5e3Sopenharmony_cinamespace panda::ecmascript {
264514f5e3Sopenharmony_ciclass PUBLIC_API DynChunk {
274514f5e3Sopenharmony_cipublic:
284514f5e3Sopenharmony_ci    static constexpr size_t ALLOCATE_MIN_SIZE = 256;
294514f5e3Sopenharmony_ci    static constexpr int FAILURE = -1;
304514f5e3Sopenharmony_ci    static constexpr int SUCCESS = 0;
314514f5e3Sopenharmony_ci    explicit DynChunk(Chunk *chunk) : chunk_(chunk)
324514f5e3Sopenharmony_ci    {
334514f5e3Sopenharmony_ci        ASSERT(chunk_ != nullptr);
344514f5e3Sopenharmony_ci    };
354514f5e3Sopenharmony_ci
364514f5e3Sopenharmony_ci    ~DynChunk() = default;
374514f5e3Sopenharmony_ci
384514f5e3Sopenharmony_ci    NO_COPY_SEMANTIC(DynChunk);
394514f5e3Sopenharmony_ci    NO_MOVE_SEMANTIC(DynChunk);
404514f5e3Sopenharmony_ci
414514f5e3Sopenharmony_ci    int Expand(size_t newSize);
424514f5e3Sopenharmony_ci
434514f5e3Sopenharmony_ci    int Insert(uint32_t position, size_t len);
444514f5e3Sopenharmony_ci
454514f5e3Sopenharmony_ci    int PUBLIC_API Emit(const uint8_t *data, size_t len);
464514f5e3Sopenharmony_ci
474514f5e3Sopenharmony_ci    int EmitSelf(size_t offset, size_t len);
484514f5e3Sopenharmony_ci
494514f5e3Sopenharmony_ci    int PUBLIC_API EmitChar(uint8_t c);
504514f5e3Sopenharmony_ci
514514f5e3Sopenharmony_ci    int EmitStr(const char *str);
524514f5e3Sopenharmony_ci
534514f5e3Sopenharmony_ci    inline int EmitU16(uint16_t data)
544514f5e3Sopenharmony_ci    {
554514f5e3Sopenharmony_ci        return Emit(reinterpret_cast<uint8_t *>(&data), U16_SIZE);
564514f5e3Sopenharmony_ci    }
574514f5e3Sopenharmony_ci
584514f5e3Sopenharmony_ci    inline int EmitU32(uint32_t data)
594514f5e3Sopenharmony_ci    {
604514f5e3Sopenharmony_ci        return Emit(reinterpret_cast<uint8_t *>(&data), U32_SIZE);
614514f5e3Sopenharmony_ci    }
624514f5e3Sopenharmony_ci
634514f5e3Sopenharmony_ci    inline int EmitU64(uint64_t data)
644514f5e3Sopenharmony_ci    {
654514f5e3Sopenharmony_ci        return Emit(reinterpret_cast<uint8_t *>(&data), U64_SIZE);
664514f5e3Sopenharmony_ci    }
674514f5e3Sopenharmony_ci
684514f5e3Sopenharmony_ci    inline void SetError()
694514f5e3Sopenharmony_ci    {
704514f5e3Sopenharmony_ci        error_ = true;
714514f5e3Sopenharmony_ci    }
724514f5e3Sopenharmony_ci
734514f5e3Sopenharmony_ci    inline size_t GetSize() const
744514f5e3Sopenharmony_ci    {
754514f5e3Sopenharmony_ci        return size_;
764514f5e3Sopenharmony_ci    }
774514f5e3Sopenharmony_ci
784514f5e3Sopenharmony_ci    inline size_t GetAllocatedSize() const
794514f5e3Sopenharmony_ci    {
804514f5e3Sopenharmony_ci        return allocatedSize_;
814514f5e3Sopenharmony_ci    }
824514f5e3Sopenharmony_ci
834514f5e3Sopenharmony_ci    inline bool GetError() const
844514f5e3Sopenharmony_ci    {
854514f5e3Sopenharmony_ci        return error_;
864514f5e3Sopenharmony_ci    }
874514f5e3Sopenharmony_ci
884514f5e3Sopenharmony_ci    inline uint64_t GetU64(size_t offset) const
894514f5e3Sopenharmony_ci    {
904514f5e3Sopenharmony_ci        // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
914514f5e3Sopenharmony_ci        return *reinterpret_cast<uint64_t *>(buf_ + offset);
924514f5e3Sopenharmony_ci    }
934514f5e3Sopenharmony_ci
944514f5e3Sopenharmony_ci    inline uint32_t GetU32(size_t offset) const
954514f5e3Sopenharmony_ci    {
964514f5e3Sopenharmony_ci        // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
974514f5e3Sopenharmony_ci        return *reinterpret_cast<uint32_t *>(buf_ + offset);
984514f5e3Sopenharmony_ci    }
994514f5e3Sopenharmony_ci
1004514f5e3Sopenharmony_ci    inline void PutU32(size_t offset, uint32_t data) const
1014514f5e3Sopenharmony_ci    {
1024514f5e3Sopenharmony_ci        // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
1034514f5e3Sopenharmony_ci        *reinterpret_cast<uint32_t *>(buf_ + offset) = data;
1044514f5e3Sopenharmony_ci    }
1054514f5e3Sopenharmony_ci
1064514f5e3Sopenharmony_ci    inline uint32_t GetU16(size_t offset) const
1074514f5e3Sopenharmony_ci    {
1084514f5e3Sopenharmony_ci        // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
1094514f5e3Sopenharmony_ci        return *reinterpret_cast<uint16_t *>(buf_ + offset);
1104514f5e3Sopenharmony_ci    }
1114514f5e3Sopenharmony_ci
1124514f5e3Sopenharmony_ci    inline void PutU16(size_t offset, uint16_t data) const
1134514f5e3Sopenharmony_ci    {
1144514f5e3Sopenharmony_ci        // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
1154514f5e3Sopenharmony_ci        *reinterpret_cast<uint16_t *>(buf_ + offset) = data;
1164514f5e3Sopenharmony_ci    }
1174514f5e3Sopenharmony_ci
1184514f5e3Sopenharmony_ci    inline uint32_t GetU8(size_t offset) const
1194514f5e3Sopenharmony_ci    {
1204514f5e3Sopenharmony_ci        // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
1214514f5e3Sopenharmony_ci        return *(buf_ + offset);
1224514f5e3Sopenharmony_ci    }
1234514f5e3Sopenharmony_ci
1244514f5e3Sopenharmony_ci    inline void PutU8(size_t offset, uint8_t data) const
1254514f5e3Sopenharmony_ci    {
1264514f5e3Sopenharmony_ci        // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
1274514f5e3Sopenharmony_ci        *(buf_ + offset) = data;
1284514f5e3Sopenharmony_ci    }
1294514f5e3Sopenharmony_ci
1304514f5e3Sopenharmony_ci    ALWAYS_INLINE static inline constexpr uint32_t GetBufferOffset()
1314514f5e3Sopenharmony_ci    {
1324514f5e3Sopenharmony_ci        return MEMBER_OFFSET(DynChunk, buf_);
1334514f5e3Sopenharmony_ci    }
1344514f5e3Sopenharmony_ci
1354514f5e3Sopenharmony_ci    uint8_t *GetBegin() const
1364514f5e3Sopenharmony_ci    {
1374514f5e3Sopenharmony_ci        return buf_;
1384514f5e3Sopenharmony_ci    }
1394514f5e3Sopenharmony_ci
1404514f5e3Sopenharmony_ciprivate:
1414514f5e3Sopenharmony_ci    static constexpr size_t ALLOCATE_MULTIPLIER = 2;
1424514f5e3Sopenharmony_ci    static constexpr size_t U16_SIZE = 2;
1434514f5e3Sopenharmony_ci    static constexpr size_t U32_SIZE = 4;
1444514f5e3Sopenharmony_ci    static constexpr size_t U64_SIZE = 8;
1454514f5e3Sopenharmony_ci    friend class RegExpParser;
1464514f5e3Sopenharmony_ci    friend class RegExpOpCode;
1474514f5e3Sopenharmony_ci    friend class RegExpExecutor;
1484514f5e3Sopenharmony_ci
1494514f5e3Sopenharmony_ci    DynChunk(uint8_t *buf, RegExpCachedChunk *regExpCachedChunk) : buf_(buf), regExpCachedChunk_(regExpCachedChunk)
1504514f5e3Sopenharmony_ci    {
1514514f5e3Sopenharmony_ci        ASSERT(regExpCachedChunk_ != nullptr);
1524514f5e3Sopenharmony_ci    };
1534514f5e3Sopenharmony_ci
1544514f5e3Sopenharmony_ci    uint8_t *buf_ {nullptr};
1554514f5e3Sopenharmony_ci    size_t size_ {0};
1564514f5e3Sopenharmony_ci    size_t allocatedSize_ {0};
1574514f5e3Sopenharmony_ci    bool error_ {false};
1584514f5e3Sopenharmony_ci    RegExpCachedChunk *regExpCachedChunk_ {nullptr};
1594514f5e3Sopenharmony_ci    Chunk *chunk_ {nullptr};
1604514f5e3Sopenharmony_ci};
1614514f5e3Sopenharmony_ci}  // namespace panda::ecmascript
1624514f5e3Sopenharmony_ci#endif  // ECMASCRIPT_REGEXP_DYN_BUFFER_H
163