<commit_before>// Copyright (c) 2021 - 2022 by Apex.AI Inc. All rights reserved.
//
// 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.
//
// SPDX-License-Identifier: Apache-2.0
#ifndef IOX_HOOFS_CXX_HELPLETS_INL
#define IOX_HOOFS_CXX_HELPLETS_INL

#include "iceoryx_hoofs/cxx/helplets.hpp"

namespace iox
{
namespace cxx
{
template <uint64_t StringCapacity>
inline bool isValidPathEntry(const string<StringCapacity>& name,
                             const RelativePathComponents& relativePathComponents) noexcept
{
    const string<StringCapacity> currentDirectory(".");
    const string<StringCapacity> parentDirectory("..");

    if (name == currentDirectory || name == parentDirectory)
    {
        return relativePathComponents == RelativePathComponents::ACCEPT;
    }

    const auto nameSize = name.size();

    for (uint64_t i = 0; i < nameSize; ++i)
    {
        const char c = name[i];

        const bool isSmallLetter = (internal::ASCII_A <= c && c <= internal::ASCII_Z);
        const bool isCapitalLetter = (internal::ASCII_CAPITAL_A <= c && c <= internal::ASCII_CAPITAL_Z);
        const bool isNumber = (internal::ASCII_0 <= c && c <= internal::ASCII_9);
        const bool isSpecialCharacter = (c == internal::ASCII_MINUS || c == internal::ASCII_DOT
                                         || c == internal::ASCII_COLON || c == internal::ASCII_UNDERSCORE);

        if (!isSmallLetter && !isCapitalLetter && !isNumber && !isSpecialCharacter)
        {
            return false;
        }
    }

    if (nameSize == 0)
    {
        return true;
    }

    // dot at the end is invalid to be compatible with windows api
    return !(name[nameSize - 1] == '.');
}

template <uint64_t StringCapacity>
inline bool isValidFileName(const string<StringCapacity>& name) noexcept
{
    if (name.empty())
    {
        return false;
    }

    // check if the file contains only valid characters
    return isValidPathEntry(name, RelativePathComponents::REJECT);
}

template <uint64_t StringCapacity>
inline bool isValidPathToFile(const string<StringCapacity>& name) noexcept
{
    if (doesEndWithPathSeparator(name))
    {
        return false;
    }

    auto lastSeparatorPosition = name.find_last_of(platform::IOX_PATH_SEPARATORS);
    auto filePart = (lastSeparatorPosition) ? name.substr(*lastSeparatorPosition + 1).value() : name;
    auto pathPart = (lastSeparatorPosition) ? name.substr(0, *lastSeparatorPosition).value() : string<StringCapacity>();

    return (pathPart.empty() || isValidPathToDirectory(pathPart)) && isValidFileName(filePart);
}

template <uint64_t StringCapacity>
inline bool isValidPathToDirectory(const string<StringCapacity>& name) noexcept
{
    if (name.empty())
    {
        return false;
    }

    auto temp = name;

    const string<StringCapacity> currentDirectory(".");
    const string<StringCapacity> parentDirectory("..");

    while (!temp.empty())
    {
        auto separatorPosition = temp.find_first_of(platform::IOX_PATH_SEPARATORS);

        // multiple slashes are explicitly allowed. the following paths
        // are equivalent:
        // /some/fuu/bar
        // //some///fuu////bar
        if (separatorPosition && *separatorPosition == 0)
        {
            temp = *temp.substr(*separatorPosition + 1);
            continue;
        }

        // verify if the entry between two path separators is a valid directory
        // name, e.g. either it has the relative component . or .. or conforms<commit_msg>iox-#1196 Fix segfault issue in helplets file/path validation<commit_after>// Copyright (c) 2021 - 2022 by Apex.AI Inc. All rights reserved.
//
// 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.
//
// SPDX-License-Identifier: Apache-2.0
#ifndef IOX_HOOFS_CXX_HELPLETS_INL
#define IOX_HOOFS_CXX_HELPLETS_INL

#include "iceoryx_hoofs/cxx/helplets.hpp"

namespace iox
{
namespace cxx
{
template <uint64_t StringCapacity>
inline bool isValidPathEntry(const string<StringCapacity>& name,
                             const RelativePathComponents& relativePathComponents) noexcept
{
    const string<StringCapacity> currentDirectory(".");
    const string<StringCapacity> parentDirectory("..");

    if (name == currentDirectory || name == parentDirectory)
    {
        return relativePathComponents == RelativePathComponents::ACCEPT;
    }

    const auto nameSize = name.size();

    for (uint64_t i = 0; i < nameSize; ++i)
    {
        const char c = name[i];

        const bool isSmallLetter = (internal::ASCII_A <= c && c <= internal::ASCII_Z);
        const bool isCapitalLetter = (internal::ASCII_CAPITAL_A <= c && c <= internal::ASCII_CAPITAL_Z);
        const bool isNumber = (internal::ASCII_0 <= c && c <= internal::ASCII_9);
        const bool isSpecialCharacter = (c == internal::ASCII_MINUS || c == internal::ASCII_DOT
                                         || c == internal::ASCII_COLON || c == internal::ASCII_UNDERSCORE);

        if (!isSmallLetter && !isCapitalLetter && !isNumber && !isSpecialCharacter)
        {
            return false;
        }
    }

    if (nameSize == 0)
    {
        return true;
    }

    // dot at the end is invalid to be compatible with windows api
    return !(name[nameSize - 1] == '.');
}

template <uint64_t StringCapacity>
inline bool isValidFileName(const string<StringCapacity>& name) noexcept
{
    if (name.empty())
    {
        return false;
    }

    // check if the file contains only valid characters
    return isValidPathEntry(name, RelativePathComponents::REJECT);
}

template <uint64_t StringCapacity>
inline bool isValidPathToFile(const string<StringCapacity>& name) noexcept
{
    if (doesEndWithPathSeparator(name))
    {
        return false;
    }

    auto lastSeparatorPosition = name.find_last_of(cxx::string<platform::IOX_NUMBER_OF_PATH_SEPARATORS>(
        cxx::TruncateToCapacity, &platform::IOX_PATH_SEPARATORS[0], platform::IOX_NUMBER_OF_PATH_SEPARATORS));
    auto filePart = (lastSeparatorPosition) ? name.substr(*lastSeparatorPosition + 1).value() : name;
    auto pathPart = (lastSeparatorPosition) ? name.substr(0, *lastSeparatorPosition).value() : string<StringCapacity>();

    return (pathPart.empty() || isValidPathToDirectory(pathPart)) && isValidFileName(filePart);
}

template <uint64_t StringCapacity>
inline bool isValidPathToDirectory(const string<StringCapacity>& name) noexcept
{
    if (name.empty())
    {
        return false;
    }

    auto temp = name;

    const string<StringCapacity> currentDirectory(".");
    const string<StringCapacity> parentDirectory("..");

    while (!temp.empty())
    {
        auto separatorPosition = temp.find_first_of(cxx::string<platform::IOX_NUMBER_OF_PATH_SEPARATORS>(
            cxx::TruncateToCapacity, &platform::IOX_PATH_SEPARATORS[0], platform::IOX_NUMBER_OF_PATH_SEPARATORS));

        // multiple slashes are explicitly allowed. the following paths
        // are equivalent:
        // /some/fuu/bar
        // //some///fuu////bar
        if (separatorPosition && *separatorPosition == 0)
        {
            temp = *temp.substr(*separatorPosition + 1);
            continue;
        }

        // verify if the entry between two path separators is a valid directory
        // name, e.g. either it has the relative component . or .. or conforms
{'fraction_non_alphanumeric': 0.10994448467294231, 'fraction_numerical': 0.008689355539464157, 'mean_word_length': 3.1311066799601197, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3870608', 'n_tokens_mistral': 2419, 'n_tokens_neox': 2327, 'n_words': 666}