Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
Gray::cMem Struct Reference

#include <cMem.h>

Static Public Member Functions

static void __cdecl IsValidFailHandler (int nSig)
 
static ptrdiff_t Diff (const void *pEnd, const void *pStart) noexcept
 
static bool IsValidApp (const void *pData) noexcept
 
static bool __stdcall IsValid (const void *pData, size_t nSize=1, bool bWriteAccess=false) noexcept
 
static bool IsCorrupt (const void *pData, size_t nSize=1, bool bWriteAccess=false) noexcept
 
static bool IsZeros (const void *pData, size_t nSize) noexcept
 
static COMPARE_t Compare (const void *p1, const void *p2, size_t nSizeBlock) noexcept
 
static COMPARE_t CompareSecure (const void *p1, const void *p2, size_t nSizeBlock) noexcept
 
static size_t __stdcall CompareIndex (const void *p1, const void *p2, size_t nSizeBlock)
 
static void Zero (void *pData, size_t nSizeBlock) noexcept
 
static void ZeroSecure (void *pData, size_t nSizeBlock) noexcept
 
static void Xor (BYTE *pDst, const BYTE *pSrc, size_t nBlockSize) noexcept
 
static void Xor2 (BYTE *pDst, const BYTE *pSrc1, const BYTE *pSrc2, size_t nBlockSize) noexcept
 
static void Copy (void *pDst, const void *pSrc, size_t nSizeBlock) noexcept
 
static void CopyOverlap (void *pDst, const void *pSrc, size_t nSizeBlock) noexcept
 
static void ReverseBytes (void *pDst, size_t nSizeBlock) noexcept
 
static void CopyReverse (void *pDst, const void *pSrc, size_t nSizeBlock) noexcept
 
static void Fill (void *pDst, size_t nSize, BYTE bVal) noexcept
 
static void CopyRepeat (void *pDst, size_t nDstSize, const void *pSrc, size_t nSrcSize) noexcept
 
static void CopyHtoN (BYTE *pDst, const void *pSrc, size_t nSizeBlock) noexcept
 
static void CopyNtoH (void *pDst, const BYTE *pSrc, size_t nSizeBlock) noexcept
 
static void Swap (void *pvMem1, void *pvMem2, size_t nBlockSize) noexcept
 
static StrLen_t __stdcall ConvertToString (char *pszDst, StrLen_t iSizeDstMax, const BYTE *pSrc, size_t iLenSrc)
 
static size_t __stdcall ReadFromString (BYTE *pDst, size_t iLenBytesMax, const char *pszSrc)
 
static StrLen_t GetHexDigestSize (size_t nSize) noexcept
 
static StrLen_t __stdcall GetHexDigest (OUT char *pszHexString, const BYTE *pData, size_t nSizeData)
 
static HRESULT __stdcall SetHexDigest (const char *pszHexString, OUT BYTE *pData, size_t nSizeData)
 

Static Public Attributes

static volatile uintptr_t sm_bDontOptimizeOut0 = 0
 used to trick the optimizer. Always 0. More...
 
static volatile uintptr_t sm_bDontOptimizeOutX = 1
 used to trick the optimizer. Unknown value. More...
 

Friends

class cMemTests
 

Detailed Description

a void type memory block. test bytes, Move memory bytes around. May be on heap, const memory space or static in stack. do NOT assume. use cHeap.

Member Function Documentation

◆ Compare()

static COMPARE_t Gray::cMem::Compare ( const void *  p1,
const void *  p2,
size_t  nSizeBlock 
)
inlinestaticnoexcept

Compare two blocks of memory. ASSUME both are at least nSizeBlock sized. Does not assume memory alignment.

◆ CompareIndex()

size_t GRAYCALL Gray::cMem::CompareIndex ( const void *  p1,
const void *  p2,
size_t  nSizeBlock 
)
static

Compare two buffers and return at what point they differ. Does not assume memory alignment for uintptr_t block compares.

◆ CompareSecure()

static COMPARE_t Gray::cMem::CompareSecure ( const void *  p1,
const void *  p2,
size_t  nSizeBlock 
)
inlinestaticnoexcept

constant-time buffer comparison. NOT efficient. Prevents timing based hacks.

◆ ConvertToString()

StrLen_t GRAYCALL Gray::cMem::ConvertToString ( char *  pszDst,
StrLen_t  iSizeDstMax,
const BYTE *  pSrc,
size_t  iLenSrc 
)
static

Write bytes out to a string as comma separated base 10 number values. Try to use SetHexDigest() instead. opposite of cMem::ReadFromString().

Returns
the actual size of the string.
Note
using Base64 would be better.

◆ Copy()

static void Gray::cMem::Copy ( void *  pDst,
const void *  pSrc,
size_t  nSizeBlock 
)
inlinestaticnoexcept

Copy a block of memory. same as CopyMemory(), RtlCopyMemory, memcpy()

Note
: Some older architectures needed versions of this to do 'huge' memory copies.

◆ CopyHtoN()

static void Gray::cMem::CopyHtoN ( BYTE *  pDst,
const void *  pSrc,
size_t  nSizeBlock 
)
inlinestaticnoexcept

Copy from Host (Local Native) Order into Network Order (Big Endian)

◆ CopyNtoH()

static void Gray::cMem::CopyNtoH ( void *  pDst,
const BYTE *  pSrc,
size_t  nSizeBlock 
)
inlinestaticnoexcept

Copy from Network Order (Big Endian) to Host Order (Local Native)

◆ CopyOverlap()

static void Gray::cMem::CopyOverlap ( void *  pDst,
const void *  pSrc,
size_t  nSizeBlock 
)
inlinestaticnoexcept

Copy possibly overlapping blocks of memory. start from end or beginning if needed. same as MoveMemory RtlMoveMemory, memmove, hmemcpy(),

Note
: Some older architectures needed versions of this to do 'huge' memory moves.

◆ CopyRepeat()

static void Gray::cMem::CopyRepeat ( void *  pDst,
size_t  nDstSize,
const void *  pSrc,
size_t  nSrcSize 
)
inlinestaticnoexcept

Fill pDst with repeating copies of pSrc.

◆ CopyReverse()

static void Gray::cMem::CopyReverse ( void *  pDst,
const void *  pSrc,
size_t  nSizeBlock 
)
inlinestaticnoexcept

Copy a block of memory BYTEs reversed. e.g. {3,2,1} = {1,2,3}, nSizeBlock = 3

◆ Diff()

static ptrdiff_t Gray::cMem::Diff ( const void *  pEnd,
const void *  pStart 
)
inlinestaticnoexcept
Returns
Difference in bytes. Assume it is a reasonable sized block? like GET_INDEX_IN()

◆ Fill()

static void Gray::cMem::Fill ( void *  pDst,
size_t  nSize,
BYTE  bVal 
)
inlinestaticnoexcept

Same as memset(). but with argument order change.

◆ GetHexDigest()

StrLen_t GRAYCALL Gray::cMem::GetHexDigest ( OUT char *  pszHexString,
const BYTE *  pData,
size_t  nSizeData 
)
static

Get the final hash as a pre-formatted string of hex digits. opposite of cMem::SetHexDigest ASSUME sizeof(pszHexString) >= SizeHexDigest

Note
using Base64 would be better.

◆ GetHexDigestSize()

static StrLen_t Gray::cMem::GetHexDigestSize ( size_t  nSize)
inlinestaticnoexcept

< How much space does the hex digest need?

◆ IsCorrupt()

static bool Gray::cMem::IsCorrupt ( const void *  pData,
size_t  nSize = 1,
bool  bWriteAccess = false 
)
inlinestaticnoexcept

is this a NOT valid/corrupt pointer? nullptr is not corrupt.

Note
this should only ever be used in debug code. and only in an ASSERT.

◆ IsValid()

bool GRAYCALL Gray::cMem::IsValid ( const void *  pData,
size_t  nSize = 1,
bool  bWriteAccess = false 
)
staticnoexcept

Is this pointer valid to read/write to ? On heap, stack or static const data space. similar to _MFC_VER AfxIsValidAddress(), AtlIsValidAddress()

Note
this should only ever be used in _DEBUG code. and only in an ASSERT. Its slow. IsBadWritePtr() is just doing exception handling under the hood. _CrtIsValidPointer is just a null check in >= VS2010
This can cause problems with thread stack guard pages in theory. https://msdn.microsoft.com/en-us/library/bb288454.aspx
Todo:
make a faster (less thorough) version of this ?

linux / POSIX version of IsBadReadPtr()

◆ IsValidApp()

static bool Gray::cMem::IsValidApp ( const void *  pData)
inlinestaticnoexcept

Is this pointer into App space? Not kernel space. Kernel Space <= 1G or 2G for linux Does not mean I have write permissions. Used to sanity check pointers. Ensure NOT offset from nullptr?

◆ IsValidFailHandler()

void __cdecl Gray::cMem::IsValidFailHandler ( int  nSig)
static
Todo:
make s_CMem_IsValidFailJmpBuf thread safe.

◆ IsZeros()

static bool Gray::cMem::IsZeros ( const void *  pData,
size_t  nSize 
)
inlinestaticnoexcept

Is all zeros ? nSize = 0 = true.

◆ ReadFromString()

size_t GRAYCALL Gray::cMem::ReadFromString ( BYTE *  pDst,
size_t  iLenBytesMax,
const char *  pszSrc 
)
static

Read/Parse bytes in from string as comma separated base 10 number values. opposite of cMem::ConvertToString().

Returns
the number of bytes read.
Note
using Base64 would be better.

◆ ReverseBytes()

static void Gray::cMem::ReverseBytes ( void *  pDst,
size_t  nSizeBlock 
)
inlinestaticnoexcept

◆ SetHexDigest()

HRESULT GRAYCALL Gray::cMem::SetHexDigest ( const char *  pszHexString,
OUT BYTE *  pData,
size_t  nSizeData 
)
static

Set binary pDigest from string pszHexString opposite of cMem::GetHexDigest

  • nSizeData = The number of expected output digits.
    Note
    using Base64 would be better.
    Returns
    S_FALSE = was zero value.

◆ Swap()

static void Gray::cMem::Swap ( void *  pvMem1,
void *  pvMem2,
size_t  nBlockSize 
)
inlinestaticnoexcept

swap copy 2 blocks of memory by bytes. like cMemT::Swap() but for 2 arbitrary sized blocks. swap them byte by byte. use cMemT::Swap<> instead if possible for intrinsic types.

  • nBlockSize = size in bytes.
    Note
    DO NOT Use this for complex types that need special copiers. have pointers and such. use cMemT::Swap()

◆ Xor()

static void Gray::cMem::Xor ( BYTE *  pDst,
const BYTE *  pSrc,
size_t  nBlockSize 
)
inlinestaticnoexcept

◆ Xor2()

static void Gray::cMem::Xor2 ( BYTE *  pDst,
const BYTE *  pSrc1,
const BYTE *  pSrc2,
size_t  nBlockSize 
)
inlinestaticnoexcept

◆ Zero()

static void Gray::cMem::Zero ( void *  pData,
size_t  nSizeBlock 
)
inlinestaticnoexcept

Zero a block of memory. same as RtlZeroMemory() but maybe not the same as SecureZeroMeory() to make sure it is not optimized out. (for password clearing)

◆ ZeroSecure()

static void Gray::cMem::ZeroSecure ( void *  pData,
size_t  nSizeBlock 
)
inlinestaticnoexcept

This is for security purposes. Not for initialization. Zero destructed values so they leave no trace. like SecureZeroMeory() and RtlSecureZeroMemory(). ensure it doesn't get optimized out. (like in an inline destructor)

Friends And Related Function Documentation

◆ cMemTests

friend class cMemTests
friend

Member Data Documentation

◆ sm_bDontOptimizeOut0

uintptr_t VOLATILE Gray::cMem::sm_bDontOptimizeOut0 = 0
static

used to trick the optimizer. Always 0.

◆ sm_bDontOptimizeOutX

uintptr_t VOLATILE Gray::cMem::sm_bDontOptimizeOutX = 1
static

used to trick the optimizer. Unknown value.


The documentation for this struct was generated from the following files: