<commit_before>// Copyright (c) 2017-2019 The Khronos Group Inc.
// Copyright (c) 2017-2019 Valve Corporation
// Copyright (c) 2017-2019 LunarG, Inc.
//
// 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.
//
// Author: <NAME> <<EMAIL>>
//
#include <cstring>
#include <fstream>
#include <iostream>
#include <sstream>

#include "platform_utils.hpp"
#include "manifest_file.hpp"
#include "xr_generated_dispatch_table.h"
#include "api_layer_interface.hpp"
#include "loader_interfaces.h"
#include "loader_logger.hpp"

#define OPENXR_ENABLE_LAYERS_ENV_VAR "XR_ENABLE_API_LAYERS"

// Add any layers defined in the loader layer environment variable.
static void AddEnvironmentApiLayers(const std::string& openxr_command, std::vector<std::string>& enabled_layers) {
    try {
    char* layer_environment_variable = PlatformUtilsGetEnv(OPENXR_ENABLE_LAYERS_ENV_VAR);
    if (nullptr != layer_environment_variable) {
        std::string layers = layer_environment_variable;
        PlatformUtilsFreeEnv(layer_environment_variable);

        std::size_t last_found = 0;
        std::size_t found = layers.find_first_of(PATH_SEPARATOR);
        std::string cur_search;

        // Handle any path listings in the string (separated by the appropriate path separator)
        while (found != std::string::npos) {
            cur_search = layers.substr(last_found, found);
            enabled_layers.push_back(cur_search);
            last_found = found + 1;
            found = layers.find_first_of(PATH_SEPARATOR, last_found);
        }

        // If there's something remaining in the string, copy it over
        if (last_found < layers.size()) {
            cur_search = layers.substr(last_found);
            enabled_layers.push_back(cur_search);
        }
    }
    } catch (...) {
        LoaderLogger::LogErrorMessage(openxr_command, "AddEnvironmentApiLayers - unknown error occurred");
        throw;
    }
}

XrResult ApiLayerInterface::GetApiLayerProperties(const std::string& openxr_command, uint32_t incoming_count,
                                                  uint32_t* outgoing_count, XrApiLayerProperties* api_layer_properties) {
    try {
    std::vector<std::unique_ptr<ApiLayerManifestFile>> manifest_files;
    uint32_t manifest_count = 0;

    // Find any implicit layers which we may need to report information for.
    XrResult result = ApiLayerManifestFile::FindManifestFiles(MANIFEST_TYPE_IMPLICIT_API_LAYER, manifest_files);
    if (XR_SUCCESS == result) {
        // Find any explicit layers which we may need to report information for.
        result = ApiLayerManifestFile::FindManifestFiles(MANIFEST_TYPE_EXPLICIT_API_LAYER, manifest_files);
    }
    if (XR_SUCCESS != result) {
        LoaderLogger::LogErrorMessage(openxr_command,
                                      "ApiLayerInterface::GetApiLayerProperties - failed searching for API layer manifest files");
        return result;
    }

    manifest_count = static_cast<uint32_t>(manifest_files.size());
    if (0 == incoming_count) {
        *outgoing_count = manifest_count;
    } else if (nullptr != api_layer_properties) {
        if (incoming_count < manifest_count && nullptr != api_layer_properties) {
            LoaderLogger::LogErrorMessage(
                "xrEnumerateInstanceExtensionProperties",
                "VUID-xrEnumerateApiLayerProperties-propertyCapacityInput-parameter: insufficient space in array");
            *outgoing_count = manifest_count;
            return XR_ERROR_SIZE_INSUFFICIENT;
        }

        uint32_t prop = 0;
        bool properties_valid = true;
        for (; prop < incoming_count && prop < manifest_count; ++prop) {
            if (XR_TYPE_API_LAYER_PROPERTIES != api_layer_properties[prop].type) {
                LoaderLogger::LogErrorMessage(openxr_command,
                                              "VUID-XrApiLayerProperties-type-type: unknown type in api_layer_properties");
                properties_valid = false;
            }
            if (nullptr != api_layer_properties[prop].next) {
                LoaderLogger::LogErrorMessage(openxr_command, "VUID-XrApiLayerProperties-next-next: expected NULL");
                properties_valid = false;
            }
            if (properties_valid) {
                api_layer_properties[prop] = manifest_files[prop]->GetApiLayerProperties();
            }
        }
        if (!properties_valid) {
            LoaderLogger::LogErrorMessage(openxr_command,
                                          "VUID-xrEnumerateApiLayerProperties-properties-parameter: invalid properties");
            return XR_ERROR_VALIDATION_FAILURE;
        } else if (nullptr != outgoing_count) {
            *outgoing_count = prop;
        }
    }
    return XR_SUCCESS;
    } catch (...) {
        LoaderLogger::LogErrorMessage(openxr_command, "ApiLayerInterface::GetApiLayerProperties - unknown error occurred");
        return XR_ERROR_VALIDATION_FAILURE;
    }
}

XrResult ApiLayerInterface::GetInstanceExtensionProperties(const std::string& openxr_command, const char* layer_name,
                                                           std::vector<XrExtensionProperties>& extension_properties) {
    try {
    std::vector<std::unique_ptr<ApiLayerManifestFile>> manifest_files;<commit_msg>loader: No else after return<commit_after>// Copyright (c) 2017-2019 The Khronos Group Inc.
// Copyright (c) 2017-2019 Valve Corporation
// Copyright (c) 2017-2019 LunarG, Inc.
//
// 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.
//
// Author: <NAME> <<EMAIL>>
//
#include <cstring>
#include <fstream>
#include <iostream>
#include <sstream>

#include "platform_utils.hpp"
#include "manifest_file.hpp"
#include "xr_generated_dispatch_table.h"
#include "api_layer_interface.hpp"
#include "loader_interfaces.h"
#include "loader_logger.hpp"

#define OPENXR_ENABLE_LAYERS_ENV_VAR "XR_ENABLE_API_LAYERS"

// Add any layers defined in the loader layer environment variable.
static void AddEnvironmentApiLayers(const std::string& openxr_command, std::vector<std::string>& enabled_layers) {
    try {
    char* layer_environment_variable = PlatformUtilsGetEnv(OPENXR_ENABLE_LAYERS_ENV_VAR);
    if (nullptr != layer_environment_variable) {
        std::string layers = layer_environment_variable;
        PlatformUtilsFreeEnv(layer_environment_variable);

        std::size_t last_found = 0;
        std::size_t found = layers.find_first_of(PATH_SEPARATOR);
        std::string cur_search;

        // Handle any path listings in the string (separated by the appropriate path separator)
        while (found != std::string::npos) {
            cur_search = layers.substr(last_found, found);
            enabled_layers.push_back(cur_search);
            last_found = found + 1;
            found = layers.find_first_of(PATH_SEPARATOR, last_found);
        }

        // If there's something remaining in the string, copy it over
        if (last_found < layers.size()) {
            cur_search = layers.substr(last_found);
            enabled_layers.push_back(cur_search);
        }
    }
    } catch (...) {
        LoaderLogger::LogErrorMessage(openxr_command, "AddEnvironmentApiLayers - unknown error occurred");
        throw;
    }
}

XrResult ApiLayerInterface::GetApiLayerProperties(const std::string& openxr_command, uint32_t incoming_count,
                                                  uint32_t* outgoing_count, XrApiLayerProperties* api_layer_properties) {
    try {
    std::vector<std::unique_ptr<ApiLayerManifestFile>> manifest_files;
    uint32_t manifest_count = 0;

    // Find any implicit layers which we may need to report information for.
    XrResult result = ApiLayerManifestFile::FindManifestFiles(MANIFEST_TYPE_IMPLICIT_API_LAYER, manifest_files);
    if (XR_SUCCESS == result) {
        // Find any explicit layers which we may need to report information for.
        result = ApiLayerManifestFile::FindManifestFiles(MANIFEST_TYPE_EXPLICIT_API_LAYER, manifest_files);
    }
    if (XR_SUCCESS != result) {
        LoaderLogger::LogErrorMessage(openxr_command,
                                      "ApiLayerInterface::GetApiLayerProperties - failed searching for API layer manifest files");
        return result;
    }

    manifest_count = static_cast<uint32_t>(manifest_files.size());
    if (0 == incoming_count) {
        *outgoing_count = manifest_count;
    } else if (nullptr != api_layer_properties) {
        if (incoming_count < manifest_count && nullptr != api_layer_properties) {
            LoaderLogger::LogErrorMessage(
                "xrEnumerateInstanceExtensionProperties",
                "VUID-xrEnumerateApiLayerProperties-propertyCapacityInput-parameter: insufficient space in array");
            *outgoing_count = manifest_count;
            return XR_ERROR_SIZE_INSUFFICIENT;
        }

        uint32_t prop = 0;
        bool properties_valid = true;
        for (; prop < incoming_count && prop < manifest_count; ++prop) {
            if (XR_TYPE_API_LAYER_PROPERTIES != api_layer_properties[prop].type) {
                LoaderLogger::LogErrorMessage(openxr_command,
                                              "VUID-XrApiLayerProperties-type-type: unknown type in api_layer_properties");
                properties_valid = false;
            }
            if (nullptr != api_layer_properties[prop].next) {
                LoaderLogger::LogErrorMessage(openxr_command, "VUID-XrApiLayerProperties-next-next: expected NULL");
                properties_valid = false;
            }
            if (properties_valid) {
                api_layer_properties[prop] = manifest_files[prop]->GetApiLayerProperties();
            }
        }
        if (!properties_valid) {
            LoaderLogger::LogErrorMessage(openxr_command,
                                          "VUID-xrEnumerateApiLayerProperties-properties-parameter: invalid properties");
            return XR_ERROR_VALIDATION_FAILURE;
        }
        if (nullptr != outgoing_count) {
            *outgoing_count = prop;
        }
    }
    return XR_SUCCESS;
    } catch (...) {
        LoaderLogger::LogErrorMessage(openxr_command, "ApiLayerInterface::GetApiLayerProperties - unknown error occurred");
        return XR_ERROR_VALIDATION_FAILURE;
    }
}

XrResult ApiLayerInterface::GetInstanceExtensionProperties(const std::string& openxr_command, const char* layer_name,
                                                           std::vector<XrExtensionProperties>& extension_properties) {
    try {
    std::vector<std::unique_ptr<ApiLayerManifestFile>> manifest_files;
{'fraction_non_alphanumeric': 0.08917142362912991, 'fraction_numerical': 0.007438159487977858, 'mean_word_length': 2.875, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2345198', 'n_tokens_mistral': 3207, 'n_tokens_neox': 3107, 'n_words': 742}