CryptoSys PKI  23.0.0
Public Types | Static Public Member Functions | List of all members
dipki::Cnv Class Reference

Character conversion routines. More...

Public Types

enum class  EndianNess
 Byte order. More...
 

Static Public Member Functions

static int CheckUTF8 (const bvec_t &bv)
 Check that a byte array contains only valid UTF-8 encoded characters. More...
 
static std::string CheckUTF8CodeAsString (int n)
 Return a string describing an integer code returned by Cnv::CheckUTF8 or Cnv::CheckUTF8File. More...
 
static int CheckUTF8File (const std::string &fileName)
 Check that a file contains only valid UTF-8 encoded characters. More...
 
static bvec_t FromBase58 (const std::string &s)
 Decodes a base58-encoded string as an array of bytes. More...
 
static bvec_t FromBase64 (const std::string &s)
 Decodes a base64-encoded string as an array of bytes. More...
 
static bvec_t FromHex (const std::string &s)
 Decodes a hexadecimal-encoded string as an array of bytes. More...
 
static std::string Latin1FromUTF8Bytes (const bvec_t &bv)
 Converts UTF-8 encoded array of bytes into a Latin-1 string, if possible. More...
 
static uint32_t NumFromBytes (const bvec_t &bv, EndianNess endn=EndianNess::BigEndian)
 Convert the leftmost four bytes of an array to an unsigned 32-bit integer. More...
 
static bvec_t NumToBytes (uint32_t n, EndianNess endn=EndianNess::BigEndian)
 Convert a 32-bit integer to an array of 4 bytes. More...
 
static bvec_t ReverseBytes (const bvec_t &data)
 Reverse the order of a byte array. More...
 
static std::string ShortPathName (const std::wstring &filePath)
 Retrieve the Windows short path form of the specified path. More...
 
static std::string ToBase58 (const bvec_t &bv)
 Encodes an array of bytes as a base58-encoded string. More...
 
static std::string ToBase64 (const bvec_t &bv)
 Encodes an array of bytes as a base64-encoded string. More...
 
static std::string ToBase64 (const std::string &s)
 Encodes a string as a base64-encoded string. More...
 
static std::string ToHex (const bvec_t &bv)
 Encodes an array of bytes as a hexadecimal-encoded string. More...
 
static std::string ToHex (const std::string &s)
 Encodes a string as a hexadecimal-encoded string. More...
 
static bvec_t UTF8BytesFromLatin1 (const std::string &s)
 Converts a string of 8-bit Latin-1 characters into a UTF-8 encoded array of bytes. More...
 
static std::string Utf8FromWide (const std::wstring &wstr)
 Map a UTF-16 (wide character) string to a UTF-8-encoded string. More...
 

Detailed Description

Character conversion routines.

Member Enumeration Documentation

◆ EndianNess

Byte order.

Enumerator
BigEndian 

Most-significant byte first

LittleEndian 

Least-significant byte first

Member Function Documentation

◆ CheckUTF8()

static int dipki::Cnv::CheckUTF8 ( const bvec_t bv)
static

Check that a byte array contains only valid UTF-8 encoded characters.

Parameters
bvinput byte array to check
Returns
Zero if the encoded bytes is invalid UTF-8, or a positive number if the input contains valid UTF-8 data, where the value of the number indicates the nature of the encoded characters:
Return value Meaning
0 Not valid UTF-8
1 Valid UTF-8, all chars are 7-bit ASCII
2 Valid UTF-8, contains at least one multi-byte character equivalent to 8-bit ANSI
3 Valid UTF-8, contains at least one multi-byte character that cannot be represented in a single-byte character set
Remarks
"Overlong" UTF-8 sequences and illegal surrogates are rejected as invalid.
Use CheckUTF8CodeAsString() to get string describing integer code.
// Chinese - hardcoded UTF-8 bytes
// U+4E2D U+56FD => E4 B8 AD E5 9B BD (zhong guo)
dipki::bvec_t b{ 0xe4, 0xb8, 0xad, 0xE5, 0x9B, 0xBD };
cout << "Cnv::CheckUTF8=" << n << " ==> " << dipki::Cnv::CheckUTF8CodeAsString(n) << endl;
// Cnv::CheckUTF8=3 ==> Valid UTF-8, contains at least one multi-byte character that cannot be represented in a single-byte character set
static int CheckUTF8(const bvec_t &bv)
Check that a byte array contains only valid UTF-8 encoded characters.
static std::string CheckUTF8CodeAsString(int n)
Return a string describing an integer code returned by Cnv::CheckUTF8 or Cnv::CheckUTF8File.
std::vector< unsigned char > bvec_t
A vector of bytes (our typedef for a byte array)
Definition: dipki.hpp:79

◆ CheckUTF8CodeAsString()

static std::string dipki::Cnv::CheckUTF8CodeAsString ( int  n)
static

Return a string describing an integer code returned by Cnv::CheckUTF8 or Cnv::CheckUTF8File.

Parameters
nInteger code
Returns
String describing result.

◆ CheckUTF8File()

static int dipki::Cnv::CheckUTF8File ( const std::string &  fileName)
static

Check that a file contains only valid UTF-8 encoded characters.

Parameters
fileNamename of file to check
Returns
Zero if the encoded bytes is invalid UTF-8, or a positive number if the input contains valid UTF-8 data, where the value of the number indicates the nature of the encoded characters:
Return value Meaning
0 Not valid UTF-8
1 Valid UTF-8, all chars are 7-bit ASCII
2 Valid UTF-8, contains at least one multi-byte character equivalent to 8-bit ANSI
3 Valid UTF-8, contains at least one multi-byte character that cannot be represented in a single-byte character set
Remarks
"Overlong" UTF-8 sequences and illegal surrogates are rejected as invalid.
Use CheckUTF8CodeAsString() to get string describing integer code.

◆ FromBase58()

static bvec_t dipki::Cnv::FromBase58 ( const std::string &  s)
static

Decodes a base58-encoded string as an array of bytes.

Parameters
sBase58-encoded string
Returns
Binary data in byte array, or an empty array on error.
Remarks
This uses the "Bitcoin" scheme of base58 encoding where the leading character '1' is reserved for representing an entire leading zero byte.
cout << "Cnv::FromBase58(\"16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM\")=0x" << dipki::Cnv::ToHex(dipki::Cnv::FromBase58("16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM")) << endl;
// Cnv::FromBase58("16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM")=0x00010966776006953D5567439E5E39F86A0D273BEED61967F6
static bvec_t FromBase58(const std::string &s)
Decodes a base58-encoded string as an array of bytes.
static std::string ToHex(const bvec_t &bv)
Encodes an array of bytes as a hexadecimal-encoded string.

◆ FromBase64()

static bvec_t dipki::Cnv::FromBase64 ( const std::string &  s)
static

Decodes a base64-encoded string as an array of bytes.

Parameters
sBase64-encoded string
Returns
Binary data in byte array, or an empty array on error.
Remarks
Whitespace characters are ignored, but other non-base64 characters will cause an error.

◆ FromHex()

static bvec_t dipki::Cnv::FromHex ( const std::string &  s)
static

Decodes a hexadecimal-encoded string as an array of bytes.

Parameters
sHexadecimal-encoded data to be decoded.
Returns
Binary data in byte array.
Remarks
Whitespace and ASCII punctuation characters are ignored, but other non-hex characters will cause an error.

◆ Latin1FromUTF8Bytes()

static std::string dipki::Cnv::Latin1FromUTF8Bytes ( const bvec_t bv)
static

Converts UTF-8 encoded array of bytes into a Latin-1 string, if possible.

Parameters
bvArray containing UTF-8 encoded data.
Returns
String containing Latin-1-encoded data, or the empty string on error.

◆ NumFromBytes()

static uint32_t dipki::Cnv::NumFromBytes ( const bvec_t bv,
EndianNess  endn = EndianNess::BigEndian 
)
static

Convert the leftmost four bytes of an array to an unsigned 32-bit integer.

Parameters
bvByte array to be converted
endnByte order (default = Big Endian)
Returns
Integer value
Remarks
An array shorter than 4 bytes will be padded on the right with zeros.
uint32_t ui = dipki::Cnv::NumFromBytes(bv);
cout << "Cnv::NumFromBytes(" << dipki::Cnv::ToHex(bv) << ")->" << ui << "=0x" << std::hex << ui << std::dec << endl;
// Cnv::NumFromBytes(DEADBEEF)->3735928559=0xdeadbeef
bv = dipki::Cnv::FromHex("DEAD"); // Short array
cout << "Cnv::NumFromBytes(" << dipki::Cnv::ToHex(bv) << ")->" << ui << "=0x" << std::hex << ui << std::dec << endl;
// Cnv::NumFromBytes(DEAD)->3735879680=0xdead0000
static uint32_t NumFromBytes(const bvec_t &bv, EndianNess endn=EndianNess::BigEndian)
Convert the leftmost four bytes of an array to an unsigned 32-bit integer.
static bvec_t FromHex(const std::string &s)
Decodes a hexadecimal-encoded string as an array of bytes.

◆ NumToBytes()

static bvec_t dipki::Cnv::NumToBytes ( uint32_t  n,
EndianNess  endn = EndianNess::BigEndian 
)
static

Convert a 32-bit integer to an array of 4 bytes.

Parameters
nInteger to be converted
endnByte order (default = Big Endian)
Returns
Byte array containing representation of integer in given order
cout << "Cnv::NumToBytes(0xdeadbeef,BE)->" + dipki::Cnv::ToHex(bv) << endl;
// Cnv::NumToBytes(0xdeadbeef,BE)->DEADBEEF
cout << "Cnv::NumToBytes(0xdeadbeef,LE)->" + dipki::Cnv::ToHex(bv) << endl;
// Cnv::NumToBytes(0xdeadbeef,LE)->EFBEADDE
@ LittleEndian
Least-significant byte first
static bvec_t NumToBytes(uint32_t n, EndianNess endn=EndianNess::BigEndian)
Convert a 32-bit integer to an array of 4 bytes.

◆ ReverseBytes()

static bvec_t dipki::Cnv::ReverseBytes ( const bvec_t data)
static

Reverse the order of a byte array.

Parameters
dataInput data to be reversed
Returns
Byte array in reverse order
dipki::bvec_t b{ 0x01, 0x02, 0x03, 0x04, 0x05 };
cout << "(before)=" << dipki::Cnv::ToHex(b) << endl;
// (before)=0102030405
cout << "(after)= " << dipki::Cnv::ToHex(b) << endl;
// (after) =0504030201
static bvec_t ReverseBytes(const bvec_t &data)
Reverse the order of a byte array.

◆ ShortPathName()

static std::string dipki::Cnv::ShortPathName ( const std::wstring &  filePath)
static

Retrieve the Windows short path form of the specified path.

Parameters
filePathPath name in wide characters.
Returns
Windows short path name or empty string if file does not exist.
Remarks
Windows only. The file path must exist. The short path is guaranteed to be ASCII. It may not have the same value on a different system.

◆ ToBase58()

static std::string dipki::Cnv::ToBase58 ( const bvec_t bv)
static

Encodes an array of bytes as a base58-encoded string.

Parameters
bvInput byte array.
Returns
Base58-encoded string.
Remarks
This uses the "Bitcoin" scheme of base58 encoding where the leading character '1' is reserved for representing an entire leading zero byte.

◆ ToBase64() [1/2]

static std::string dipki::Cnv::ToBase64 ( const bvec_t bv)
static

Encodes an array of bytes as a base64-encoded string.

Parameters
bvInput byte array.
Returns
Base64-encoded string.

◆ ToBase64() [2/2]

static std::string dipki::Cnv::ToBase64 ( const std::string &  s)
static

Encodes a string as a base64-encoded string.

Parameters
sString to be encoded
Returns
Base64-encoded string.
cout << dipki::Cnv::ToBase64("abc") << endl;
// YWJj
static std::string ToBase64(const bvec_t &bv)
Encodes an array of bytes as a base64-encoded string.

◆ ToHex() [1/2]

static std::string dipki::Cnv::ToHex ( const bvec_t bv)
static

Encodes an array of bytes as a hexadecimal-encoded string.

Parameters
bvInput byte array.
Returns
Hex-encoded string.

◆ ToHex() [2/2]

static std::string dipki::Cnv::ToHex ( const std::string &  s)
static

Encodes a string as a hexadecimal-encoded string.

Parameters
sString to be encoded
Returns
Hex-encoded string.
cout << dipki::Cnv::ToHex("abc") << endl;
// 616263

◆ UTF8BytesFromLatin1()

static bvec_t dipki::Cnv::UTF8BytesFromLatin1 ( const std::string &  s)
static

Converts a string of 8-bit Latin-1 characters into a UTF-8 encoded array of bytes.

Parameters
sInput string to be converted.
Returns
UTF-8-encoded sequence of bytes.

◆ Utf8FromWide()

static std::string dipki::Cnv::Utf8FromWide ( const std::wstring &  wstr)
static

Map a UTF-16 (wide character) string to a UTF-8-encoded string.

Parameters
wstrString of wide characters to be processed.
Returns
UTF-8-encoded string.
Copyright © 2004-24 D.I. Management Services Pty Limited t/a CryptoSys ABN 78 083 210 584 Australia. All rights reserved. <www.di-mgt.com.au> <www.cryptosys.net>. Generated on Mon Sep 23 2024 15:37:33 by Doxygen 1.9.1.