Fabcoin Core  0.16.2
P2P Digital Currency
Functions
misc.cpp File Reference
#include "pch.h"
#include "config.h"
#include "misc.h"
#include "words.h"
#include "stdcpp.h"
#include "integer.h"
Include dependency graph for misc.cpp:

Go to the source code of this file.

Functions

void xorbuf (byte *buf, const byte *mask, size_t count)
 Performs an XOR of a buffer with a mask. More...
 
void xorbuf (byte *output, const byte *input, const byte *mask, size_t count)
 Performs an XOR of an input buffer with a mask and stores the result in an output buffer. More...
 
bool VerifyBufsEqual (const byte *buf, const byte *mask, size_t count)
 Performs a near constant-time comparison of two equally sized buffers. More...
 
std::string StringNarrow (const wchar_t *str, bool throwOnError)
 Converts a wide character C-string to a multibyte string. More...
 
void CallNewHandler ()
 Attempts to reclaim unused memory. More...
 
void * UnalignedAllocate (size_t size)
 Allocates a buffer. More...
 
void UnalignedDeallocate (void *p)
 Frees a buffer allocated with UnalignedAllocate. More...
 

Function Documentation

void CallNewHandler ( )

Attempts to reclaim unused memory.

Exceptions
bad_alloc

In the normal course of running a program, a request for memory normally succeeds. If a call to AlignedAllocate or UnalignedAllocate fails, then CallNewHandler is called in an effort to recover. Internally, CallNewHandler calls set_new_handler(NULL) in an effort to free memory. There is no guarantee CallNewHandler will be able to procure more memory so an allocation succeeds. If the call to set_new_handler fails, then CallNewHandler throws a bad_alloc exception.

Definition at line 200 of file misc.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

std::string StringNarrow ( const wchar_t *  str,
bool  throwOnError = true 
)

Converts a wide character C-string to a multibyte string.

Parameters
strC-string consisting of wide characters
throwOnErrorflag indication the function should throw on error
Returns
str converted to a multibyte string or an empty string.

StringNarrow converts a wide string to a narrow string using C++ std::wcstombs() under the executing thread's locale. A locale must be set before using this function, and it can be set with std::setlocale() if needed. Upon success, the converted string is returned.

Upon failure with throwOnError as false, the function returns an empty string. If throwOnError as true, the function throws an InvalidArgument() exception.

Note
If you try to convert, say, the Chinese character for "bone" from UTF-16 (0x9AA8) to UTF-8 (0xE9 0xAA 0xA8), then you must ensure the locale is available. If the locale is not available, then a 0x21 error is returned on Windows which eventually results in an InvalidArgument() exception.

Definition at line 136 of file misc.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

void* UnalignedAllocate ( size_t  size)

Allocates a buffer.

Parameters
sizethe size of the buffer

Definition at line 257 of file misc.cpp.

Here is the call graph for this function:

Here is the caller graph for this function:

void UnalignedDeallocate ( void *  ptr)

Frees a buffer allocated with UnalignedAllocate.

Parameters
ptrthe buffer to free

Definition at line 265 of file misc.cpp.

Here is the caller graph for this function:

bool VerifyBufsEqual ( const byte buf1,
const byte buf2,
size_t  count 
)

Performs a near constant-time comparison of two equally sized buffers.

Parameters
buf1the first buffer
buf2the second buffer
countthe size of the buffers, in bytes

The function effectively performs an XOR of the elements in two equally sized buffers and retruns a result based on the XOR operation. The function is near constant-time because CPU micro-code timings could affect the "constant-ness". Calling code is responsible for mitigating timing attacks if the buffers are not equally sized.

See also
ModPowerOf2

Definition at line 96 of file misc.cpp.

Here is the caller graph for this function:

void xorbuf ( byte buf,
const byte mask,
size_t  count 
)

Performs an XOR of a buffer with a mask.

Parameters
bufthe buffer to XOR with the mask
maskthe mask to XOR with the buffer
countthe size of the buffers, in bytes

The function effectively visits each element in the buffers and performs buf[i] ^= mask[i]. buf and mask must be of equal size.

Definition at line 28 of file misc.cpp.

Here is the caller graph for this function:

void xorbuf ( byte output,
const byte input,
const byte mask,
size_t  count 
)

Performs an XOR of an input buffer with a mask and stores the result in an output buffer.

Parameters
outputthe destination buffer
inputthe source buffer to XOR with the mask
maskthe mask buffer to XOR with the input buffer
countthe size of the buffers, in bytes

The function effectively visits each element in the buffers and performs output[i] = input[i] ^ mask[i]. output, input and mask must be of equal size.

Definition at line 61 of file misc.cpp.