/**
 * PROJECT:         ExectOS
 * COPYRIGHT:       See COPYING.md in the top level directory
 * FILE:            sdk/xtdk/rtltypes.h
 * DESCRIPTION:     Runtime library structures definitions
 * DEVELOPERS:      Rafal Kupiec <belliash@codingworkshop.eu.org>
 *                  Aiken Harris <harraiken91@gmail.com>
 */

#ifndef __XTDK_RTLTYPES_H
#define __XTDK_RTLTYPES_H

#include <xtbase.h>
#include <xttypes.h>
#include ARCH_HEADER(xtstruct.h)


/* UUID string lengths */
#define GUID_STRING_LENGTH                              38
#define PARTUUID_STRING_LENGTH                          13

/* Maximum double/integer value string length */
#define MAX_DOUBLE_STRING_SIZE                          15
#define MAX_INTEGER_STRING_SIZE                         25

/* Floating point definitions */
#define DOUBLE_EXPONENT_MASK                            0x7FF0000000000000ULL
#define DOUBLE_EXPONENT_SHIFT                           0x34
#define DOUBLE_EXPONENT_BIAS                            0x3FF
#define DOUBLE_HIGH_VALUE_MASK                          0x000FFFFF
#define DOUBLE_HIGH_VALUE_SHIFT                         0x20
#define DOUBLE_PRECISION                                6
#define DOUBLE_HEX_PRECISION                            13
#define DOUBLE_SCIENTIFIC_PRECISION                     -4
#define DOUBLE_SIGN_BIT                                 0x8000000000000000ULL

/* Exception Record flags */
#define EXCEPTION_CONTINUE_SEARCH                       0x00
#define EXCEPTION_NONCONTINUABLE                        0x01
#define EXCEPTION_UNWINDING                             0x02
#define EXCEPTION_EXIT_UNWIND                           0x04
#define EXCEPTION_STACK_INVALID                         0x08
#define EXCEPTION_NESTED_CALL                           0x10
#define EXCEPTION_TARGET_UNWIND                         0x20
#define EXCEPTION_COLLIDED_UNWIND                       0x40

/* Exception Record accessors */
#define EXCEPTION_CODE                                  _exception_code()
#define EXCEPTION_INFORMATION                           (PEXCEPTION_POINTERS)_exception_info()

/* Maximum number of lead bytes for NLS */
#define NLS_MAXIMUM_LEADBYTES                           12

/* Print flag definitions */
#define PFL_ALWAYS_PRINT_SIGN                           0x00000001
#define PFL_SPACE_FOR_PLUS                              0x00000002
#define PFL_LEFT_JUSTIFIED                              0x00000004
#define PFL_LEADING_ZEROES                              0x00000008
#define PFL_LONG_INTEGER                                0x00000010
#define PFL_LONG_DOUBLE                                 0x00000020
#define PFL_WIDE_CHARACTER                              0x00000040
#define PFL_SHORT_VALUE                                 0x00000080
#define PFL_UNSIGNED                                    0x00000100
#define PFL_UPPERCASE                                   0x00000200
#define PFL_PRINT_RADIX                                 0x00000400
#define PFL_FLOAT_FORMAT                                0x00000800
#define PFL_SCI_FORMAT                                  0x00001000
#define PFL_DIGIT_PRECISION                             0x00002000
#define PFL_THOUSANDS_GROUPING                          0x00004000

/* Red-black tree definitions */
#define RTL_BALANCED_NODE_RESERVED_PARENT_MASK          3
#define RTL_BALANCED_NODE_COLOR_MASK                    1

/* Cryptographic related definitions */
#define SHA1_BLOCK_SIZE                                 64
#define SHA1_DIGEST_SIZE                                20

/* Time related definitions */
#define TIME_SECONDS_PER_MINUTE                         60
#define TIME_SECONDS_PER_HOUR                           3600
#define TIME_SECONDS_PER_DAY                            86400
#define TIME_TICKS_PER_SECOND                           10000000
#define TIME_TICKS_PER_MILLISECOND                      10000


/* C/C++ specific code */
#ifndef __XTOS_ASSEMBLER__

/* Runtime Library routine callbacks */
typedef XTSTATUS (*PWRITE_CHARACTER)(IN CHAR Character);
typedef XTSTATUS (*PWRITE_WIDE_CHARACTER)(IN WCHAR Character);

/* Compressed data information structure definition */
typedef struct _COMPRESSED_DATA_INFO
{
    USHORT CompressionFormatAndEngine;
    UCHAR CompressionUnitShift;
    UCHAR ChunkShift;
    UCHAR ClusterShift;
    UCHAR Reserved;
    USHORT NumberOfChunks;
    ULONG CompressedChunkSizes[1];
} COMPRESSED_DATA_INFO, *PCOMPRESSED_DATA_INFO;

/* Code page table structure definition */
typedef struct _CPTABLE_INFO
{
    USHORT CodePage;
    USHORT MaximumCharacterSize;
    USHORT DefaultChar;
    USHORT UniDefaultChar;
    USHORT TransDefaultChar;
    USHORT TransUniDefaultChar;
    USHORT DBCSCodePage;
    UCHAR LeadByte[NLS_MAXIMUM_LEADBYTES];
    PUSHORT MultiByteTable;
    PVOID WideCharTable;
    PUSHORT DBCSRanges;
    PUSHORT DBCSOffsets;
} CPTABLE_INFO, *PCPTABLE_INFO;

/* Exception Pointers structure definition */
typedef struct _EXCEPTION_POINTERS
{
    PEXCEPTION_RECORD ExceptionRecord;
    PCONTEXT ContextRecord;
} EXCEPTION_POINTERS, *PEXCEPTION_POINTERS;

/* 128-bit buffer containing a unique identifier value */
typedef struct _GUID
{
    UINT Data1;
    USHORT Data2;
    USHORT Data3;
    UCHAR Data4[8];
} GUID, *PGUID;

/* 32-bit double linked list structure definition */
typedef struct _LIST_ENTRY32
{
    ULONG Flink;
    ULONG Blink;
} LIST_ENTRY32, *PLIST_ENTRY32;

/* 64-bit double linked list structure definition */
typedef struct _LIST_ENTRY64
{
    ULONGLONG Flink;
    ULONGLONG Blink;
} LIST_ENTRY64, *PLIST_ENTRY64;

/* NLS table structure definition */
typedef struct _NLSTABLE_INFO
{
    CPTABLE_INFO OemTableInfo;
    CPTABLE_INFO AnsiTableInfo;
    PUSHORT UpperCaseTable;
    PUSHORT LowerCaseTable;
} NLSTABLE_INFO, *PNLSTABLE_INFO;

/* Red-black tree node color enumeration list */
typedef enum _RTL_BALANCED_NODE_COLOR
{
    NodeBlack,
    NodeRed
} RTL_BALANCED_NODE_COLOR, *PRTL_BALANCED_NODE_COLOR;

/* Variable types enumeration list */
typedef enum _RTL_VARIABLE_TYPE
{
    TypeUnknown,
    TypeAnsiString,
    TypeBoolean,
    TypeChar,
    TypeFloat,
    TypeGuid,
    TypeInteger,
    TypeString,
    TypeUnicodeString,
    TypeWideChar,
    TypeWideString
} RTL_VARIABLE_TYPE, *PRTL_VARIABLE_TYPE;

/* Activation context stack structure definition */
typedef struct _ACTIVATION_CONTEXT_STACK
{
    PRTL_ACTIVATION_CONTEXT_STACK_FRAME ActiveFrame;
    LIST_ENTRY FrameListCache;
    ULONG Flags;
    ULONG NextCookieSequenceNumber;
    ULONG StackId;
} ACTIVATION_CONTEXT_STACK, *PACTIVATION_CONTEXT_STACK;

/* Activation context stack frame structure definition */
typedef struct _RTL_ACTIVATION_CONTEXT_STACK_FRAME
{
    PRTL_ACTIVATION_CONTEXT_STACK_FRAME Previous;
    PVOID ActivationContext;
    ULONG Flags;
} RTL_ACTIVATION_CONTEXT_STACK_FRAME, *PRTL_ACTIVATION_CONTEXT_STACK_FRAME;

/* Runtime Library red-black tree balanced node structure definition */
typedef struct _RTL_BALANCED_NODE
{
    union
    {
        PRTL_BALANCED_NODE Children[2];
        struct
        {
            PRTL_BALANCED_NODE Left;
            PRTL_BALANCED_NODE Right;
        };
    };
    union
    {
        UCHAR Red:1;
        UCHAR Balance:2;
        ULONG_PTR ParentValue;
    };
} RTL_BALANCED_NODE, *PRTL_BALANCED_NODE;

/* Bit Map structure definition */
typedef struct _RTL_BITMAP
{
    ULONG Size;
    PULONG_PTR Buffer;
} RTL_BITMAP, *PRTL_BITMAP;

/* Runtime Library print context structure definition */
typedef struct _RTL_PRINT_CONTEXT
{
    PWRITE_CHARACTER WriteCharacter;
    PWRITE_WIDE_CHARACTER WriteWideCharacter;
    ULONG CharactersWritten;
} RTL_PRINT_CONTEXT, *PRTL_PRINT_CONTEXT;

/* Runtime Library print format properties structure definition */
typedef struct _RTL_PRINT_FORMAT_PROPERTIES
{
    RTL_VARIABLE_TYPE VariableType;
    ULONG Radix;
    LONG FieldWidth;
    LONG IntegerSize;
    LONG Precision;
    LONG Flags;
} RTL_PRINT_FORMAT_PROPERTIES, *PRTL_PRINT_FORMAT_PROPERTIES;

/* Runtime Library red-black tree structure definition */
typedef struct _RTL_RB_TREE
{
    PRTL_BALANCED_NODE Root;
    PRTL_BALANCED_NODE Min;
} RTL_RB_TREE, *PRTL_RB_TREE;

/* Runtime Library SHA-1 context structure definition */
typedef struct _RTL_SHA1_CONTEXT
{
    ULONG State[5];
    ULONG Count[2];
    UCHAR Buffer[SHA1_BLOCK_SIZE];
} RTL_SHA1_CONTEXT, *PRTL_SHA1_CONTEXT;

/* Single linked list structure definition */
typedef struct _SINGLE_LIST_ENTRY
{
    PSINGLE_LIST_ENTRY Next;
} SINGLE_LIST_ENTRY, *PSINGLE_LIST_ENTRY;

/* Runtime time fields structure definition */
typedef struct _TIME_FIELDS
{
    SHORT Year;
    SHORT Month;
    SHORT Day;
    SHORT Hour;
    SHORT Minute;
    SHORT Second;
    SHORT Milliseconds;
    SHORT Weekday;
} TIME_FIELDS, *PTIME_FIELDS;

#endif /* __XTOS_ASSEMBLER__ */
#endif /* __XTDK_RTLTYPES_H */
