Skip to content

Instantly share code, notes, and snippets.

@SeanPesce
Last active July 16, 2024 20:02
Show Gist options
  • Save SeanPesce/57200f694674d73cac4704f11a8eb90b to your computer and use it in GitHub Desktop.
Save SeanPesce/57200f694674d73cac4704f11a8eb90b to your computer and use it in GitHub Desktop.
Ghidra CONCAT Implementations
// Author: Sean Pesce
//
// Manual implementations of the CONCAT operations produced by the Ghidra decompiler.
// These definitions are helpful for compiling re-implementations of native code using
// decompiler output (e.g., with gcc).
//
// Note that these implementations would be outperformed by minimal C preprocessor macros
// that replicate the same logic.
#include <stdint.h>
uint16_t CONCAT11(uint8_t left, uint8_t right)
{
uint16_t result = 0;
result |= (((uint16_t)left) << 8);
result |= right;
return result;
}
uint32_t CONCAT12(uint8_t left, uint16_t right)
{
uint32_t result = 0;
result |= (((uint32_t)left) << 16);
result |= right;
return result;
}
uint32_t CONCAT13(uint8_t left, uint32_t right)
{
uint32_t result = 0;
result |= (((uint32_t)left) << 24);
result |= right & 0x00ffffff;
return result;
}
uint64_t CONCAT14(uint8_t left, uint32_t right)
{
uint64_t result = 0;
result |= (((uint64_t)left) << 32);
result |= right;
return result;
}
uint64_t CONCAT15(uint8_t left, uint64_t right)
{
uint64_t result = 0;
result |= (((uint64_t)left) << 40);
result |= right & 0x000000ffffffffff;
return result;
}
uint64_t CONCAT16(uint8_t left, uint64_t right)
{
uint64_t result = 0;
result |= (((uint64_t)left) << 48);
result |= right & 0x0000ffffffffffff;
return result;
}
uint64_t CONCAT17(uint8_t left, uint64_t right)
{
uint64_t result = 0;
result |= (((uint64_t)left) << 56);
result |= right & 0x00ffffffffffffff;
return result;
}
uint32_t CONCAT21(uint16_t left, uint8_t right)
{
uint32_t result = 0;
result |= (((uint32_t)left) << 8);
result |= right;
return result;
}
uint32_t CONCAT22(uint16_t left, uint16_t right)
{
uint32_t result = 0;
result |= (((uint32_t)left) << 16);
result |= right;
return result;
}
uint64_t CONCAT23(uint16_t left, uint32_t right)
{
uint64_t result = 0;
result |= (((uint64_t)left) << 24);
result |= right & 0x00ffffff;
return result;
}
uint64_t CONCAT24(uint16_t left, uint32_t right)
{
uint64_t result = 0;
result |= (((uint64_t)left) << 32);
result |= right;
return result;
}
uint64_t CONCAT25(uint16_t left, uint64_t right)
{
uint64_t result = 0;
result |= (((uint64_t)left) << 40);
result |= right & 0x000000ffffffffff;
return result;
}
uint64_t CONCAT26(uint16_t left, uint64_t right)
{
uint64_t result = 0;
result |= (((uint64_t)left) << 48);
result |= right & 0x0000ffffffffffff;
return result;
}
uint32_t CONCAT31(uint32_t left, uint8_t right)
{
uint32_t result = 0;
result |= (left << 8);
result |= right;
return result;
}
uint64_t CONCAT32(uint32_t left, uint16_t right)
{
uint64_t result = 0;
result |= (((uint64_t)left) << 16) & 0x000000ffffff0000;
result |= right;
return result;
}
uint64_t CONCAT33(uint32_t left, uint32_t right)
{
uinr64_t result = 0;
result |= (((uint64_t)left) << 24) & 0x0000ffffff000000;
result |= right & 0x00ffffff;
return result;
}
uint64_t CONCAT34(uint32_t left, uint32_t right)
{
uint64_t result = 0;
result |= (((uint64_t)left) << 32) & 0x00ffffff00000000;
result |= right;
return result;
}
uint64_t CONCAT35(uint32_t left, uint64_t right)
{
uint64_t result = 0;
result |= (((uint64_t)left) << 40) & 0xffffff0000000000;
result |= right & 0x000000ffffffffff;
return result;
}
uint64_t CONCAT41(uint32_t left, uint8_t right)
{
uint64_t result = 0;
result |= (((uint64_t)left) << 8) & 0x000000ffffffff00;
result |= right;
return result;
}
uint64_t CONCAT42(uint32_t left, uint16_t right)
{
uint64_t result = 0;
result |= (((uint64_t)left) << 16) & 0x0000ffffffff0000;
result |= right;
return result;
}
uint64_t CONCAT43(uint32_t left, uint32_t right)
{
uint64_t result = 0;
result |= (((uint64_t)left) << 24) & 0x00ffffffff000000;
result |= right & 0x00ffffff;
return result;
}
uint64_t CONCAT44(uint32_t left, uint32_t right)
{
uint64_t result = 0;
result |= (((uint64_t)left) << 32) & 0xffffffff00000000;
result |= right;
return result;
}
uint64_t CONCAT51(uint64_t left, uint8_t right)
{
uint64_t result = 0;
result |= (((uint64_t)left) << 8) & 0x0000ffffffffff00;
result |= right;
return result;
}
uint64_t CONCAT52(uint64_t left, uint16_t right)
{
uint64_t result = 0;
result |= (((uint64_t)left) << 16) & 0x00ffffffffff0000;
result |= right;
return result;
}
uint64_t CONCAT53(uint64_t left, uint32_t right)
{
uint64_t result = 0;
result |= (((uint64_t)left) << 24) & 0xffffffffff000000;
result |= right & 0x00ffffff;
return result;
}
uint64_t CONCAT61(uint64_t left, uint8_t right)
{
uint64_t result = 0;
result |= (((uint64_t)left) << 8) & 0x00ffffffffffff00;
result |= right;
return result;
}
uint64_t CONCAT62(uint64_t left, uint16_t right)
{
uint64_t result = 0;
result |= (((uint64_t)left) << 16) & 0xffffffffffff0000;
result |= right;
return result;
}
uint64_t CONCAT71(uint64_t left, uint8_t right)
{
uint64_t result = 0;
result |= (((uint64_t)left) << 8) & 0xffffffffffffff00;
result |= right;
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment