/**
* @file libc.h
*
* @brief Exposes a handful of libc-lite utility functions used by other
*        modules.
*
* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of NVIDIA CORPORATION nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef LIBC__LIBC_H
#define LIBC__LIBC_H
#define FSP__LIBC__LIBC_H                             1

#include <stddef.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdbool.h>

#include <misc/ct-assert.h>
#include <misc/macros.h>

START_RFD_BLOCK(MISRA, DEVIATE, Rule_1_2, "Approval: Bug 200531996, DR: SWE-FSP-009-SWSADR.docx",
                MISRA, DEVIATE, Directive_4_6, "Approval: Bug 200543266, DR: SWE-FSP-003-SWSADR.docx",
                MISRA, DEVIATE, Directive_4_9, "Approval: Bug 200531995, DR: SWE-FSP-012-SWSADR.docx",
                MISRA, DEVIATE, Rule_21_2, "Approval: Jira TID-1970, DR: SWE-FSP-066-SWSADR.docx",
                CERTC, DEVIATE, DCL37_C, "Approval: Bug 2731179, DR: SWE-FSP-067-SWSADR.docx")

/**
 * @brief libc memcpy
 *
 * Freestanding version of memcpy for RISCV units.
 * Copies n characters from the object pointer to by src
 * into the object pointed to by dst.
 *
 * @param[out] dst      destination pointer
 * @param[in] src       source pointer
 * @param[in] n         number of bytes to copy
 *
 * @return              resulting destination pointer
 */
void   *memcpy(void *dst, const void *src, size_t n);

/**
 * @brief libc memcmp
 *
 * Freestanding version of memcpy for RISCV units.
 * Compares the first n characters of the object pointed to
 * by s1 to the first n characters of the object pointer to
 * by s2.
 *
 * @param[in] s1    first pointer
 * @param[in] s2    second pointer
 * @param[in] n     number of bytes to compare
 *
 * @return          zero if both memories are equal, otherwise unequal
 */
int memcmp(const void *s1, const void *s2, size_t n);

/**
 * @brief libc memset
 *
 * Freestanding version of memset for RISCV units.
 * Copies the value of c (converted into an unsigned char)
 * into each of the first n characters of the object pointer to by s.
 *
 * @param[out] s        pointer to memory to set
 * @param[in] c         character to set
 * @param[in] n         number of words to set
 *
 * @return              pointer to memory set (s)
 */
void   *memset(void *s, int c, size_t n);

/**
 * @brief libc isprint
 *
 * Freestanding version of isprint for RISCV units.
 * Tests for any printing character including space (' ').
 *
 * @param[in] c     character to print
 *
 * @return          zero if the character is print-able
 *
 * NOTES:
 * - MISRA RULE 10.3 fix - explicit conversion of characters
 */
__attribute__( ( always_inline ) ) static inline int isprint(int c)
{
    int is_p = 0;

    if ((c >= (int)(' ')) && (c <= (int)('~')))
    {
        is_p = 1;
    }

    return is_p;
}

END_RFD_BLOCK(MISRA, DEVIATE, Rule_1_2, "Approval: Bug 200531996, DR: SWE-FSP-009-SWSADR.docx",
              MISRA, DEVIATE, Directive_4_6, "Approval: Bug 200543266, DR: SWE-FSP-003-SWSADR.docx",
              MISRA, DEVIATE, Directive_4_9, "Approval: Bug 200531995, DR: SWE-FSP-012-SWSADR.docx",
              MISRA, DEVIATE, Rule_21_2, "Approval: Jira TID-1970, DR: SWE-FSP-066-SWSADR.docx",
              CERTC, DEVIATE, DCL37_C, "Approval: Bug 2731179, DR: SWE-FSP-067-SWSADR.docx")

#endif // LIBNVRISCV_LIBC_H
