Skip to content

Instantly share code, notes, and snippets.

@rojaster
Last active December 12, 2018 06:07
Show Gist options
  • Save rojaster/fb1424f111403b4c31f2e6ce7de72bf4 to your computer and use it in GitHub Desktop.
Save rojaster/fb1424f111403b4c31f2e6ce7de72bf4 to your computer and use it in GitHub Desktop.
Value Decomposition with example
/*
Burning Midnight oil.
Alekum. 2018.
*/
#include <iostream>
#include <iomanip>
#include <stdint.h>
#include <inttypes.h>
#include <bitset>
// Version 1.0
template<typename Q, typename D, typename W, typename B>
union OverlapUnion {
Q QWORD;
D DWORD[2];
W WORD[4];
B BYTE[8];
};
using UnsignedOverlapper64_t = OverlapUnion<uint64_t,uint32_t,uint16_t,uint8_t>;
using SignedOverlapper64_t = OverlapUnion<int64_t,int32_t,int16_t,int8_t>;
using Overlapper64_t = UnsignedOverlapper64_t;
template<typename Overlaptype, int repetitions, int beforeFills, int afterFills, typename OStream>
void print_overlapper_generic(Overlaptype* value, OStream& out)
{
for(short i = repetitions-1; i >= 0; --i)
{
out << std::setw(beforeFills)
<< std::hex
<< value[i]
<< std::setw(afterFills)
<< " | ";
}
}
template<typename t32b, typename t16b, typename t8b, typename Overlapper_t, typename OStream>
void print_overlapper64_t(Overlapper_t& overlapper64value, OStream& out)
{
out << " | "
<< "64bit"
<< " | "
<< std::setw(30)
<< std::hex
<< overlapper64value.QWORD
<< std::setw(18)
<< " | "
<< std::endl;
out << " | 32bit | ";
print_overlapper_generic<t32b, 2, 15, 9>(overlapper64value.DWORD, out);
out << std::endl;
out << " | 16bit | ";
print_overlapper_generic<t16b, 4, 7, 5>(overlapper64value.WORD, out);
out << std::endl;
out << " | 8bit | ";
print_overlapper_generic<t8b, 8, 3, 3>(overlapper64value.BYTE, out);
out << std::endl;
}
// Version 0.1
template<typename TypeFrom, typename TypeTo, int beforeFills, int afterFills, typename OStream>
void print_value_decompisition(TypeFrom* TypeFromValue, OStream& out)
{
TypeTo* __typetoValue;
__typetoValue = (TypeTo*)TypeFromValue;
out << std::hex
<< std::setw(beforeFills)
<< *(__typetoValue+1)
<< std::setw(afterFills)
<< L" | ";
out << std::setw(beforeFills)
<< *(__typetoValue) \
<< std::setw(afterFills)
<< L" | ";
}
template<typename OStream>
void print_uint64bit_value_decomposition(uint64_t* __uint64bit, OStream& out)
{
uint32_t* __uint32bit_p;
__uint32bit_p = (uint32_t*)__uint64bit;
out << " | "
<< "UINT64"
<< " | "
<< std::setw(30)
<< std::hex
<< *__uint64bit
<< std::setw(18)
<< " | "
<< std::endl;
out << " | UINT32 | ";
print_value_decompisition<uint64_t, uint32_t, 15, 9>(__uint64bit, out);
out << std::endl;
out << " | UINT16 | ";
print_value_decompisition<uint32_t, uint16_t, 7, 5>(__uint32bit_p+1, out);
print_value_decompisition<uint32_t, uint16_t, 7, 5>(__uint32bit_p, out);
out << std::endl;
uint16_t* __uint16bit_p;
out << " | UINT8 | ";
__uint16bit_p = (uint16_t*)(__uint32bit_p+1);
print_value_decompisition<uint16_t, uint8_t, 3, 3>(__uint16bit_p+1, out);
print_value_decompisition<uint16_t, uint8_t, 3, 3>(__uint16bit_p, out);
__uint16bit_p = (uint16_t*)__uint32bit_p;
print_value_decompisition<uint16_t, uint8_t, 3, 3>(__uint16bit_p+1, out);
print_value_decompisition<uint16_t, uint8_t, 3, 3>(__uint16bit_p, out);
out << std::endl;
}
int main(int argv, char* argc[])
{
uint64_t __uint64bit[2] = {0xabcfcf1212113412, 0x12343412abcfcfab};
std::wcout << std::setw(30) <<"VERSION 0.1" << std::endl;
print_uint64bit_value_decomposition(&__uint64bit[0], std::wcout);
std::wcout << std::endl;
print_uint64bit_value_decomposition(&__uint64bit[1], std::wcout);
std::wcout << std::endl << std::endl;
std::wcout << std::setw(30) <<"VERSION 1.0" << std::endl;
Overlapper64_t value64;
value64.QWORD = __uint64bit[0];
print_overlapper64_t<uint32_t, uint16_t, uint8_t>(value64, std::wcout);
std::wcout << std::endl;
value64.QWORD = __uint64bit[1];
print_overlapper64_t<uint32_t, uint16_t, uint8_t>(value64, std::wcout);
std::wcout << std::endl;
SignedOverlapper64_t signed_value64;
signed_value64.QWORD = 0x323232322aa32323;
print_overlapper64_t<int32_t, int16_t, int8_t>(signed_value64, std::wcout);
std::wcout << std::endl;
std::wcout << std::hex << std::bitset<16>(signed_value64.BYTE[2]) << " " << std::bitset<16>(0xa3) << " " << std::bitset<16>((int8_t)0x00a3);
std::wcout << std::endl;
return 0;
}
@rojaster
Copy link
Author

output must be:

 /*
 Expected output for this example
 | UINT64 |               abcfcf1212113412                | 
 | UINT32 |        abcfcf12       |        12113412       | 
 | UINT16 |    abcf   |    cf12   |    1211   |    3412   | 
 | UINT8  |  ab |  cf |  cf |  12 |  12 |  11 |  34 |  12 | 
 | UINT64 |               12343412abcfcfab                | 
 | UINT32 |        12343412       |        abcfcfab       | 
 | UINT16 |    1234   |    3412   |    abcf   |    cfab   | 
 | UINT8  |  12 |  34 |  34 |  12 |  ab |  cf |  cf |  ab | 
 */

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment