├── .gitignore ├── core-packer.sln ├── core-packer.suo └── core-packer ├── DllEntryPoint32.cpp ├── DllEntryPoint64.cpp ├── ExeEntryPoint32.cpp ├── LoadLibrary.cpp ├── LoadLibrary.h ├── core-packer.vcxproj ├── core-packer.vcxproj.filters ├── core-packer.vcxproj.user ├── decrypt.h ├── deprecated.cpp ├── distorm.h ├── distorm ├── config.h ├── decoder.c ├── decoder.h ├── distorm.c ├── instructions.c ├── instructions.h ├── insts.c ├── insts.h ├── mnemonics.c ├── operands.c ├── operands.h ├── prefix.c ├── prefix.h ├── textdefs.c ├── textdefs.h ├── wstring.c ├── wstring.h └── x86defs.h ├── dll32.h ├── dll64.h ├── dll_fix32.asm ├── dll_fix64.asm ├── library.cpp ├── library.h ├── macro.h ├── main.cpp ├── main32.cpp ├── main64.cpp ├── mnemonics.h ├── patchutils.cpp ├── patchutils.h ├── pe_fix32.asm ├── peasm ├── pe_amd64.cpp ├── pe_i386.cpp ├── peasm.cpp ├── peasm.h ├── pesection.cpp ├── pesection.h └── types.h ├── rc4.cpp ├── rc4.h ├── reloc.cpp ├── reloc.h ├── rva.cpp ├── rva.h ├── symbols.cpp ├── symbols.h ├── tclap ├── Arg.h ├── ArgException.h ├── ArgTraits.h ├── CmdLine.h ├── CmdLineInterface.h ├── CmdLineOutput.h ├── Constraint.h ├── DocBookOutput.h ├── HelpVisitor.h ├── IgnoreRestVisitor.h ├── MultiArg.h ├── MultiSwitchArg.h ├── OptionalUnlabeledTracker.h ├── StandardTraits.h ├── StdOutput.h ├── SwitchArg.h ├── UnlabeledMultiArg.h ├── UnlabeledValueArg.h ├── ValueArg.h ├── ValuesConstraint.h ├── VersionVisitor.h ├── Visitor.h ├── XorHandler.h └── ZshCompletionOutput.h ├── tea.cpp ├── tea.h └── unpack32.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | Release* 2 | Debug* 3 | *.sdf -------------------------------------------------------------------------------- /core-packer.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 11.00 3 | # Visual Studio 2010 4 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "core-packer", "core-packer\core-packer.vcxproj", "{CFCDC238-E14C-43D7-AA69-03FF46BDC5D0}" 5 | EndProject 6 | Global 7 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 8 | Debug|Win32 = Debug|Win32 9 | Debug|x64 = Debug|x64 10 | Release|Win32 = Release|Win32 11 | Release|x64 = Release|x64 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {CFCDC238-E14C-43D7-AA69-03FF46BDC5D0}.Debug|Win32.ActiveCfg = Debug|Win32 15 | {CFCDC238-E14C-43D7-AA69-03FF46BDC5D0}.Debug|Win32.Build.0 = Debug|Win32 16 | {CFCDC238-E14C-43D7-AA69-03FF46BDC5D0}.Debug|x64.ActiveCfg = Debug|x64 17 | {CFCDC238-E14C-43D7-AA69-03FF46BDC5D0}.Debug|x64.Build.0 = Debug|x64 18 | {CFCDC238-E14C-43D7-AA69-03FF46BDC5D0}.Release|Win32.ActiveCfg = Release|Win32 19 | {CFCDC238-E14C-43D7-AA69-03FF46BDC5D0}.Release|Win32.Build.0 = Release|Win32 20 | {CFCDC238-E14C-43D7-AA69-03FF46BDC5D0}.Release|x64.ActiveCfg = Release|x64 21 | {CFCDC238-E14C-43D7-AA69-03FF46BDC5D0}.Release|x64.Build.0 = Release|x64 22 | EndGlobalSection 23 | GlobalSection(SolutionProperties) = preSolution 24 | HideSolutionNode = FALSE 25 | EndGlobalSection 26 | EndGlobal 27 | -------------------------------------------------------------------------------- /core-packer.suo: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hackedteam/core-packer/88a2ee1b9e85b410abf225ae609e30c34d7c99a6/core-packer.suo -------------------------------------------------------------------------------- /core-packer/LoadLibrary.h: -------------------------------------------------------------------------------- 1 | #ifndef __LOADLIBRARY_H_ 2 | #define __LOADLIBRARY_H_ 3 | 4 | #pragma once 5 | 6 | class CLoadLibrary 7 | { 8 | private: 9 | LPVOID _lpBaseAddress; // Library Base Address 10 | 11 | public: 12 | CLoadLibrary(void); 13 | virtual ~CLoadLibrary(void); 14 | 15 | public: 16 | PIMAGE_SECTION_HEADER GetLastSectionHeader(); 17 | PIMAGE_SECTION_HEADER AddSectionHeader(); 18 | 19 | LPVOID ExpandOptionalHeader(WORD requiredBytes); 20 | 21 | BOOL SaveLibraryToFile(TCHAR* lpOutFileName); 22 | BOOL SaveLibrary64ToFile(TCHAR* lpOutFileName); 23 | 24 | PIMAGE_NT_HEADERS GetNTHeader(); 25 | PIMAGE_SECTION_HEADER SectionHeader(); 26 | 27 | BOOL LoadLibrary(TCHAR* lpFileName, DWORD dwAdditionalPages); 28 | }; 29 | 30 | #endif 31 | -------------------------------------------------------------------------------- /core-packer/core-packer.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | {90a8df99-4dfd-480b-a399-ad83e4da3c76} 18 | 19 | 20 | {176c3d2e-2681-4b78-88a8-0b67aa6df10c} 21 | 22 | 23 | {5708e92c-22b1-4d7a-bcdd-831328ce9077} 24 | 25 | 26 | {0fac6947-e136-477c-8d9c-24b7b647a938} 27 | 28 | 29 | {1673165e-ff14-4ff2-81cc-4a01c1c3647f} 30 | 31 | 32 | {672bb42d-1835-4cda-9705-6141fb92aa64} 33 | 34 | 35 | {47336c1b-014c-42fd-b1a8-f527cee74b87} 36 | 37 | 38 | {57260724-2794-4e2f-9781-d81788c16fd5} 39 | 40 | 41 | 42 | 43 | Header Files\tclap 44 | 45 | 46 | Header Files\tclap 47 | 48 | 49 | Header Files\tclap 50 | 51 | 52 | Header Files\tclap 53 | 54 | 55 | Header Files\tclap 56 | 57 | 58 | Header Files\tclap 59 | 60 | 61 | Header Files\tclap 62 | 63 | 64 | Header Files\tclap 65 | 66 | 67 | Header Files\tclap 68 | 69 | 70 | Header Files\tclap 71 | 72 | 73 | Header Files\tclap 74 | 75 | 76 | Header Files\tclap 77 | 78 | 79 | Header Files\tclap 80 | 81 | 82 | Header Files\tclap 83 | 84 | 85 | Header Files\tclap 86 | 87 | 88 | Header Files\tclap 89 | 90 | 91 | Header Files\tclap 92 | 93 | 94 | Header Files\tclap 95 | 96 | 97 | Header Files\tclap 98 | 99 | 100 | Header Files\tclap 101 | 102 | 103 | Header Files\tclap 104 | 105 | 106 | Header Files\tclap 107 | 108 | 109 | Header Files\tclap 110 | 111 | 112 | Header Files\tclap 113 | 114 | 115 | Header Files 116 | 117 | 118 | Header Files 119 | 120 | 121 | Header Files 122 | 123 | 124 | Header Files 125 | 126 | 127 | Header Files 128 | 129 | 130 | Header Files 131 | 132 | 133 | Header Files 134 | 135 | 136 | Header Files 137 | 138 | 139 | Header Files 140 | 141 | 142 | Header Files 143 | 144 | 145 | Header Files 146 | 147 | 148 | Header Files 149 | 150 | 151 | Header Files\peasm 152 | 153 | 154 | Header Files\peasm 155 | 156 | 157 | Header Files\peasm 158 | 159 | 160 | Header Files\distorm 161 | 162 | 163 | Header Files\distorm 164 | 165 | 166 | Header Files\distorm 167 | 168 | 169 | Header Files\distorm 170 | 171 | 172 | Header Files\distorm 173 | 174 | 175 | Header Files\distorm 176 | 177 | 178 | Header Files\distorm 179 | 180 | 181 | Header Files\distorm 182 | 183 | 184 | Header Files\distorm 185 | 186 | 187 | Header Files 188 | 189 | 190 | Header Files 191 | 192 | 193 | 194 | 195 | Source Files 196 | 197 | 198 | Source Files 199 | 200 | 201 | Source Files 202 | 203 | 204 | Source Files 205 | 206 | 207 | Source Files 208 | 209 | 210 | Source Files 211 | 212 | 213 | Source Files 214 | 215 | 216 | Source Files 217 | 218 | 219 | Source Files 220 | 221 | 222 | Source Files 223 | 224 | 225 | Source Files 226 | 227 | 228 | Source Files\peasm 229 | 230 | 231 | Source Files\peasm 232 | 233 | 234 | Source Files\stub 235 | 236 | 237 | Source Files\stub 238 | 239 | 240 | Source Files\stub 241 | 242 | 243 | Source Files\peasm 244 | 245 | 246 | Source Files\peasm 247 | 248 | 249 | Source Files 250 | 251 | 252 | Source Files\distorm 253 | 254 | 255 | Source Files\distorm 256 | 257 | 258 | Source Files\distorm 259 | 260 | 261 | Source Files\distorm 262 | 263 | 264 | Source Files\distorm 265 | 266 | 267 | Source Files\distorm 268 | 269 | 270 | Source Files\distorm 271 | 272 | 273 | Source Files\distorm 274 | 275 | 276 | Source Files\distorm 277 | 278 | 279 | Source Files 280 | 281 | 282 | 283 | 284 | asm 285 | 286 | 287 | asm 288 | 289 | 290 | asm 291 | 292 | 293 | -------------------------------------------------------------------------------- /core-packer/core-packer.vcxproj.user: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | z:\windows\core z:\core.dll 5 | WindowsLocalDebugger 6 | 7 | 8 | z:\core_test\core64.dll z:\core_test\packed64.dll 9 | WindowsLocalDebugger 10 | 11 | 12 | j:\prescout.exe c:\temp\ 13 | WindowsLocalDebugger 14 | 15 | -------------------------------------------------------------------------------- /core-packer/decrypt.h: -------------------------------------------------------------------------------- 1 | #ifndef __DECRYPT_H_ 2 | 3 | #ifdef _BUILD32 4 | BOOL WINAPI decrypt(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved); 5 | #else 6 | BOOL WINAPI decrypt(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved); 7 | #endif 8 | 9 | #endif 10 | -------------------------------------------------------------------------------- /core-packer/deprecated.cpp: -------------------------------------------------------------------------------- 1 | /*** 2 | * main32.cpp 3 | * in main32 4 | * 5 | * 6 | ============================================================================================== 7 | else if (memcmp(pSectionHeader->Name, ".rdata", 6) == 0) 8 | { // encrypt rdata section 9 | 10 | if (pInfectMe->IsDLL()) 11 | { // ignore 12 | } 13 | else 14 | { 15 | ++ /*uint32_t *key = (uint32_t *) rc4sbox; 16 | ++ LPDWORD encptr = (LPDWORD) pProcessSection->RawData(); 17 | ++ 18 | ++ for(DWORD dwPtr = 0; dwPtr < pProcessSection->SizeOfRawData(); dwPtr += 8, encptr += 2) 19 | ++ tea_encrypt((uint32_t *) encptr, key); // CLOSE COMMENT HERE! 20 | } 21 | 22 | } 23 | 24 | ++ //else if (memcmp(pSectionHeader->Name, ".rdata", 6) == 0) 25 | ++ //{ 26 | ++ // pSectionHeader->Characteristics |= 0x03; 27 | ++ 28 | ++ // /*DWORD sizeOfSection = 29 | ++ // pInfectMeNtHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress 30 | ++ // - pProcessSection->VirtualAddress 31 | ++ // - pInfectMeNtHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IAT].Size; 32 | ++ // 33 | ++ // LPVOID sectionAddress = rva2addr(pInfectMe, pInfectMeNtHeader, (LPVOID) (pProcessSection->VirtualAddress + pInfectMeNtHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IAT].Size)); 34 | 35 | ++ // if (pInfectMe->IsDLL()) 36 | ++ // cypher_msg(rc4sbox, (PBYTE) sectionAddress, sizeOfSection); 37 | ++ // else 38 | ++ // { 39 | ++ // uint32_t *key = (uint32_t *) rc4sbox; 40 | ++ // LPDWORD encptr = (LPDWORD) sectionAddress; 41 | 42 | ++ // for(DWORD dwPtr = 0; dwPtr < sizeOfSection; dwPtr += 8, encptr += 2) 43 | ++ // tea_encrypt((uint32_t *) encptr, key); 44 | ++ // } 45 | ++ //} 46 | 47 | } 48 | 49 | //memcpy(pInfectSection->Name, szHermitName, 8); 50 | 51 | //PIMAGE_SECTION_HEADER pInfectSection = IMAGE_FIRST_SECTION(pInfectMeNtHeader); 52 | ============================================================================================== 53 | 54 | /*** 55 | * DllEntryPoint32.cpp 56 | * function 57 | * #pragma code_seg(".pedll32") 58 | * BOOL WINAPI decrypt(struct _vtbl *vtbl, HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) 59 | * 60 | ============================================================================================== 61 | else if (__memcmp((char *) pSection->Name, szData, 5) == 0) 62 | { 63 | cypher_msg(sbox, (PBYTE) lpAddress, pSection->SizeOfRawData); // decrypt done! 64 | } 65 | ++ 66 | ++// 67 | ++// if ((pSection->Characteristics & 0x03) == 3) 68 | ++// { 69 | ++// DWORD sizeOfSection = 70 | ++// pImageNtHeaders32->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress 71 | ++// - pSection->VirtualAddress 72 | ++// - pImageNtHeaders32->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IAT].Size; 73 | ++// 74 | ++// LPVOID lpNewAddress = CALC_OFFSET(LPVOID, lpAddress, pImageNtHeaders32->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IAT].Size); 75 | ++// 76 | ++// cypher_msg(sbox, (PBYTE) lpNewAddress, sizeOfSection); // decrypt done! 77 | ++// 78 | ++// } 79 | ++// else if (pSection->Characteristics & 0x02) 80 | ++// { // packed section! 81 | ++// LPDWORD lpSectionName = (LPDWORD) pSection->Name; 82 | ++// if (*lpSectionName == 0x7865742e) 83 | ++// { // text section! load from disk!! 84 | ++// char szFileName[MAX_PATH]; 85 | ++// DWORD dw = _GetModuleFileNameA(hinstDLL, szFileName, MAX_PATH); 86 | ++// HANDLE h = vtbl->file_open(szFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL); 87 | ++// 88 | ++// g_lpTextBaseAddr = vtbl->mem_alloc(0x0, pSection->Misc.VirtualSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE); 89 | ++// 90 | ++// vtbl->file_seek(h, 0x400, 0, SEEK_SET); // << 0x400 - offset on physical disk of first section 91 | ++// _ReadFile(h, g_lpTextBaseAddr, pSection->Misc.VirtualSize, &dw, NULL); //_ReadFile(h, lpAddress, pSection->Misc.VirtualSize, &dw, NULL); 92 | ++// _CloseHandle(h); 93 | ++// cypher_msg(sbox, (PBYTE) g_lpTextBaseAddr, pSection->Misc.VirtualSize); // cypher_msg(sbox, (PBYTE) lpAddress, pSection->Misc.VirtualSize); 94 | ++///////////// 95 | ++// } 96 | ++// else 97 | ++// 98 | ++// } 99 | ============================================================================================== 100 | // apply reloc in current section! 101 | ULONG ptrReloc = CALC_OFFSET(ULONG, pImageDosHeader, (ULONG) lpRelocAddress); 102 | 103 | if (g_decrypted == 0) // relocation must be done only 1st time! 104 | 105 | ***/ -------------------------------------------------------------------------------- /core-packer/distorm/config.h: -------------------------------------------------------------------------------- 1 | /* 2 | config.h 3 | 4 | diStorm3 - Powerful disassembler for X86/AMD64 5 | http://ragestorm.net/distorm/ 6 | distorm at gmail dot com 7 | Copyright (C) 2003-2012 Gil Dabah 8 | 9 | This program is free software: you can redistribute it and/or modify 10 | it under the terms of the GNU General Public License as published by 11 | the Free Software Foundation, either version 3 of the License, or 12 | (at your option) any later version. 13 | 14 | This program is distributed in the hope that it will be useful, 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | GNU General Public License for more details. 18 | 19 | You should have received a copy of the GNU General Public License 20 | along with this program. If not, see 21 | */ 22 | 23 | 24 | #ifndef CONFIG_H 25 | #define CONFIG_H 26 | 27 | /* diStorm version number. */ 28 | #define __DISTORMV__ 0x030300 29 | 30 | #include /* memset, memcpy - can be easily self implemented for libc independency. */ 31 | 32 | #include "../distorm.h" 33 | 34 | 35 | /* 36 | * 64 bit offsets support: 37 | * This macro should be defined from compiler command line flags, e.g: -DSUPPORT_64BIT_OFFSET 38 | * Note: make sure that the caller (library user) defines it too! 39 | */ 40 | /* #define SUPPORT_64BIT_OFFSET */ 41 | 42 | /* 43 | * If you compile diStorm as a dynamic library (.dll or .so) file, make sure you uncomment the next line. 44 | * So the interface functions will be exported, otherwise they are useable only for static library. 45 | * For example, this macro is being set for compiling diStorm as a .dll for Python with CTypes. 46 | */ 47 | /* #define DISTORM_DYNAMIC */ 48 | 49 | /* 50 | * If DISTORM_LIGHT is defined, everything involved in formatting the instructions 51 | * as text will be excluded from compilation. 52 | * distorm_decode(..) and distorm_format(..) will not be available. 53 | * This will decrease the size of the executable and leave you with decomposition functionality only. 54 | * 55 | * Note: it should be either set in the preprocessor definitions manually or in command line -D switch. 56 | * #define DISTORM_LIGHT 57 | */ 58 | 59 | /* 60 | * diStorm now supports little/big endian CPU's. 61 | * It should detect the endianness according to predefined macro's of the compiler. 62 | * If you don't use GCC/MSVC you will have to define it on your own. 63 | */ 64 | 65 | /* These macros are used in order to make the code portable. */ 66 | #ifdef __GNUC__ 67 | 68 | #include 69 | 70 | #define _DLLEXPORT_ 71 | #define _FASTCALL_ 72 | #define _INLINE_ static 73 | /* GCC ignores this directive... */ 74 | /*#define _FASTCALL_ __attribute__((__fastcall__))*/ 75 | 76 | /* Set endianity (supposed to be LE though): */ 77 | #ifdef __BIG_ENDIAN__ 78 | #define BE_SYSTEM 79 | #endif 80 | 81 | /* End of __GCC__ */ 82 | 83 | #elif __WATCOMC__ 84 | 85 | #include 86 | 87 | #define _DLLEXPORT_ 88 | #define _FASTCALL_ 89 | #define _INLINE_ __inline 90 | 91 | /* End of __WATCOMC__ */ 92 | 93 | #elif __DMC__ 94 | 95 | #include 96 | 97 | #define _DLLEXPORT_ 98 | #define _FASTCALL_ 99 | #define _INLINE_ __inline 100 | 101 | /* End of __DMC__ */ 102 | 103 | #elif __TINYC__ 104 | 105 | #include 106 | 107 | #define _DLLEXPORT_ 108 | #define _FASTCALL_ 109 | #define _INLINE_ 110 | 111 | /* End of __TINYC__ */ 112 | 113 | #elif _MSC_VER 114 | 115 | /* stdint alternative is defined in distorm.h */ 116 | 117 | #define _DLLEXPORT_ __declspec(dllexport) 118 | #define _FASTCALL_ __fastcall 119 | #define _INLINE_ __inline 120 | 121 | /* Set endianity (supposed to be LE though): */ 122 | #if !defined(_M_IX86) && !defined(_M_X64) 123 | #define BE_SYSTEM 124 | #endif 125 | 126 | #endif /* #elif _MSC_VER */ 127 | 128 | /* If the library isn't compiled as a dynamic library don't export any functions. */ 129 | #ifndef DISTORM_DYNAMIC 130 | #undef _DLLEXPORT_ 131 | #define _DLLEXPORT_ 132 | #endif 133 | 134 | #ifndef FALSE 135 | #define FALSE 0 136 | #endif 137 | #ifndef TRUE 138 | #define TRUE 1 139 | #endif 140 | 141 | /* Define stream read functions for big endian systems. */ 142 | #ifdef BE_SYSTEM 143 | /* 144 | * These functions can read from the stream safely! 145 | * Swap endianity of input to little endian. 146 | */ 147 | static _INLINE_ int16_t RSHORT(const uint8_t *s) 148 | { 149 | return s[0] | (s[1] << 8); 150 | } 151 | static _INLINE_ uint16_t RUSHORT(const uint8_t *s) 152 | { 153 | return s[0] | (s[1] << 8); 154 | } 155 | static _INLINE_ int32_t RLONG(const uint8_t *s) 156 | { 157 | return s[0] | (s[1] << 8) | (s[2] << 16) | (s[3] << 24); 158 | } 159 | static _INLINE_ uint32_t RULONG(const uint8_t *s) 160 | { 161 | return s[0] | (s[1] << 8) | (s[2] << 16) | (s[3] << 24); 162 | } 163 | static _INLINE_ int64_t RLLONG(const uint8_t *s) 164 | { 165 | return s[0] | (s[1] << 8) | (s[2] << 16) | (s[3] << 24) | ((uint64_t)s[4] << 32) | ((uint64_t)s[5] << 40) | ((uint64_t)s[6] << 48) | ((uint64_t)s[7] << 56); 166 | } 167 | static _INLINE_ uint64_t RULLONG(const uint8_t *s) 168 | { 169 | return s[0] | (s[1] << 8) | (s[2] << 16) | (s[3] << 24) | ((uint64_t)s[4] << 32) | ((uint64_t)s[5] << 40) | ((uint64_t)s[6] << 48) | ((uint64_t)s[7] << 56); 170 | } 171 | #else 172 | /* Little endian macro's will just make the cast. */ 173 | #define RSHORT(x) *(int16_t *)x 174 | #define RUSHORT(x) *(uint16_t *)x 175 | #define RLONG(x) *(int32_t *)x 176 | #define RULONG(x) *(uint32_t *)x 177 | #define RLLONG(x) *(int64_t *)x 178 | #define RULLONG(x) *(uint64_t *)x 179 | #endif 180 | 181 | #endif /* CONFIG_H */ 182 | -------------------------------------------------------------------------------- /core-packer/distorm/decoder.h: -------------------------------------------------------------------------------- 1 | /* 2 | decoder.h 3 | 4 | diStorm3 - Powerful disassembler for X86/AMD64 5 | http://ragestorm.net/distorm/ 6 | distorm at gmail dot com 7 | Copyright (C) 2011 Gil Dabah 8 | 9 | This program is free software: you can redistribute it and/or modify 10 | it under the terms of the GNU General Public License as published by 11 | the Free Software Foundation, either version 3 of the License, or 12 | (at your option) any later version. 13 | 14 | This program is distributed in the hope that it will be useful, 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | GNU General Public License for more details. 18 | 19 | You should have received a copy of the GNU General Public License 20 | along with this program. If not, see 21 | */ 22 | 23 | 24 | #ifndef DECODER_H 25 | #define DECODER_H 26 | 27 | #include "config.h" 28 | 29 | typedef unsigned int _iflags; 30 | 31 | _DecodeResult decode_internal(_CodeInfo* ci, int supportOldIntr, _DInst result[], unsigned int maxResultCount, unsigned int* usedInstructionsCount); 32 | 33 | #endif /* DECODER_H */ 34 | -------------------------------------------------------------------------------- /core-packer/distorm/insts.h: -------------------------------------------------------------------------------- 1 | /* 2 | insts.h 3 | 4 | diStorm3 - Powerful disassembler for X86/AMD64 5 | http://ragestorm.net/distorm/ 6 | distorm at gmail dot com 7 | Copyright (C) 2003-2012 Gil Dabah 8 | 9 | This program is free software: you can redistribute it and/or modify 10 | it under the terms of the GNU General Public License as published by 11 | the Free Software Foundation, either version 3 of the License, or 12 | (at your option) any later version. 13 | 14 | This program is distributed in the hope that it will be useful, 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | GNU General Public License for more details. 18 | 19 | You should have received a copy of the GNU General Public License 20 | along with this program. If not, see 21 | */ 22 | 23 | 24 | #ifndef INSTS_H 25 | #define INSTS_H 26 | 27 | #include "instructions.h" 28 | 29 | 30 | /* Flags Table */ 31 | extern _iflags FlagsTable[]; 32 | 33 | /* Root Trie DB */ 34 | extern _InstSharedInfo InstSharedInfoTable[]; 35 | extern _InstInfo InstInfos[]; 36 | extern _InstInfoEx InstInfosEx[]; 37 | extern _InstNode InstructionsTree[]; 38 | 39 | /* 3DNow! Trie DB */ 40 | extern _InstNode Table_0F_0F; 41 | /* AVX related: */ 42 | extern _InstNode Table_0F, Table_0F_38, Table_0F_3A; 43 | 44 | /* 45 | * The inst_lookup will return on of these two instructions according to the specified decoding mode. 46 | * ARPL or MOVSXD on 64 bits is one byte instruction at index 0x63. 47 | */ 48 | extern _InstInfo II_ARPL; 49 | extern _InstInfo II_MOVSXD; 50 | 51 | /* 52 | * The NOP instruction can be prefixed by REX in 64bits, therefore we have to decide in runtime whether it's an XCHG or NOP instruction. 53 | * If 0x90 is prefixed by a useable REX it will become XCHG, otherwise it will become a NOP. 54 | * Also note that if it's prefixed by 0xf3, it becomes a Pause. 55 | */ 56 | extern _InstInfo II_NOP; 57 | extern _InstInfo II_PAUSE; 58 | 59 | /* 60 | * Used for letting the extract operand know the type of operands without knowing the 61 | * instruction itself yet, because of the way those instructions work. 62 | * See function instructions.c!inst_lookup_3dnow. 63 | */ 64 | extern _InstInfo II_3DNOW; 65 | 66 | /* Helper tables for pesudo compare mnemonics. */ 67 | extern uint16_t CmpMnemonicOffsets[8]; /* SSE */ 68 | extern uint16_t VCmpMnemonicOffsets[32]; /* AVX */ 69 | 70 | #endif /* INSTS_H */ 71 | -------------------------------------------------------------------------------- /core-packer/distorm/operands.h: -------------------------------------------------------------------------------- 1 | /* 2 | operands.h 3 | 4 | diStorm3 - Powerful disassembler for X86/AMD64 5 | http://ragestorm.net/distorm/ 6 | distorm at gmail dot com 7 | Copyright (C) 2003-2012 Gil Dabah 8 | 9 | This program is free software: you can redistribute it and/or modify 10 | it under the terms of the GNU General Public License as published by 11 | the Free Software Foundation, either version 3 of the License, or 12 | (at your option) any later version. 13 | 14 | This program is distributed in the hope that it will be useful, 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | GNU General Public License for more details. 18 | 19 | You should have received a copy of the GNU General Public License 20 | along with this program. If not, see 21 | */ 22 | 23 | 24 | #ifndef OPERANDS_H 25 | #define OPERANDS_H 26 | 27 | #include "config.h" 28 | #include "decoder.h" 29 | #include "prefix.h" 30 | #include "instructions.h" 31 | 32 | 33 | extern uint16_t _REGISTERTORCLASS[]; 34 | 35 | int operands_extract(_CodeInfo* ci, _DInst* di, _InstInfo* ii, 36 | _iflags instFlags, _OpType type, _OperandNumberType opNum, 37 | unsigned int modrm, _PrefixState* ps, _DecodeType effOpSz, 38 | _DecodeType effAdrSz, int* lockableInstruction); 39 | 40 | #endif /* OPERANDS_H */ 41 | -------------------------------------------------------------------------------- /core-packer/distorm/prefix.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hackedteam/core-packer/88a2ee1b9e85b410abf225ae609e30c34d7c99a6/core-packer/distorm/prefix.c -------------------------------------------------------------------------------- /core-packer/distorm/prefix.h: -------------------------------------------------------------------------------- 1 | /* 2 | prefix.h 3 | 4 | diStorm3 - Powerful disassembler for X86/AMD64 5 | http://ragestorm.net/distorm/ 6 | distorm at gmail dot com 7 | Copyright (C) 2003-2012 Gil Dabah 8 | 9 | This program is free software: you can redistribute it and/or modify 10 | it under the terms of the GNU General Public License as published by 11 | the Free Software Foundation, either version 3 of the License, or 12 | (at your option) any later version. 13 | 14 | This program is distributed in the hope that it will be useful, 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | GNU General Public License for more details. 18 | 19 | You should have received a copy of the GNU General Public License 20 | along with this program. If not, see 21 | */ 22 | 23 | 24 | #ifndef PREFIX_H 25 | #define PREFIX_H 26 | 27 | #include "config.h" 28 | #include "decoder.h" 29 | 30 | 31 | /* Specifies the type of the extension prefix, such as: REX, 2 bytes VEX, 3 bytes VEX. */ 32 | typedef enum {PET_NONE = 0, PET_REX, PET_VEX2BYTES, PET_VEX3BYTES} _PrefixExtType; 33 | 34 | /* Specifies an index into a table of prefixes by their type. */ 35 | typedef enum {PFXIDX_NONE = -1, PFXIDX_REX, PFXIDX_LOREP, PFXIDX_SEG, PFXIDX_OP_SIZE, PFXIDX_ADRS, PFXIDX_MAX} _PrefixIndexer; 36 | 37 | /* 38 | * This holds the prefixes state for the current instruction we decode. 39 | * decodedPrefixes includes all specific prefixes that the instruction got. 40 | * start is a pointer to the first prefix to take into account. 41 | * last is a pointer to the last byte we scanned. 42 | * Other pointers are used to keep track of prefixes positions and help us know if they appeared already and where. 43 | */ 44 | typedef struct { 45 | _iflags decodedPrefixes, usedPrefixes; 46 | const uint8_t *start, *last, *vexPos, *rexPos; 47 | _PrefixExtType prefixExtType; 48 | uint16_t unusedPrefixesMask; 49 | /* Indicates whether the operand size prefix (0x66) was used as a mandatory prefix. */ 50 | int isOpSizeMandatory; 51 | /* If VEX prefix is used, store the VEX.vvvv field. */ 52 | unsigned int vexV; 53 | /* The fields B/X/R/W/L of REX and VEX are stored together in this byte. */ 54 | unsigned int vrex; 55 | 56 | /* !! Make sure pfxIndexer is LAST! Otherwise memset won't work well with it. !! */ 57 | 58 | /* Holds the offset to the prefix byte by its type. */ 59 | int pfxIndexer[PFXIDX_MAX]; 60 | } _PrefixState; 61 | 62 | /* 63 | * Intel supports 6 types of prefixes, whereas AMD supports 5 types (lock is seperated from rep/nz). 64 | * REX is the fifth prefix type, this time I'm based on AMD64. 65 | * VEX is the 6th, though it can't be repeated. 66 | */ 67 | #define MAX_PREFIXES (5) 68 | 69 | int prefixes_is_valid(unsigned int ch, _DecodeType dt); 70 | void prefixes_ignore(_PrefixState* ps, _PrefixIndexer pi); 71 | void prefixes_ignore_all(_PrefixState* ps); 72 | uint16_t prefixes_set_unused_mask(_PrefixState* ps); 73 | void prefixes_decode(const uint8_t* code, int codeLen, _PrefixState* ps, _DecodeType dt); 74 | void prefixes_use_segment(_iflags defaultSeg, _PrefixState* ps, _DecodeType dt, _DInst* di); 75 | 76 | #endif /* PREFIX_H */ 77 | -------------------------------------------------------------------------------- /core-packer/distorm/textdefs.c: -------------------------------------------------------------------------------- 1 | /* 2 | textdefs.c 3 | 4 | diStorm3 - Powerful disassembler for X86/AMD64 5 | http://ragestorm.net/distorm/ 6 | distorm at gmail dot com 7 | Copyright (C) 2003-2012 Gil Dabah 8 | 9 | This program is free software: you can redistribute it and/or modify 10 | it under the terms of the GNU General Public License as published by 11 | the Free Software Foundation, either version 3 of the License, or 12 | (at your option) any later version. 13 | 14 | This program is distributed in the hope that it will be useful, 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | GNU General Public License for more details. 18 | 19 | You should have received a copy of the GNU General Public License 20 | along with this program. If not, see 21 | */ 22 | 23 | 24 | #include "textdefs.h" 25 | 26 | #ifndef DISTORM_LIGHT 27 | 28 | static uint8_t Nibble2ChrTable[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; 29 | #define NIBBLE_TO_CHR Nibble2ChrTable[t] 30 | 31 | void _FASTCALL_ str_hex_b(_WString* s, unsigned int x) 32 | { 33 | /* 34 | * def prebuilt(): 35 | * s = "" 36 | * for i in xrange(256): 37 | * if ((i % 0x10) == 0): 38 | * s += "\r\n" 39 | * s += "\"%02x\", " % (i) 40 | * return s 41 | */ 42 | static int8_t TextBTable[256][3] = { 43 | "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "0a", "0b", "0c", "0d", "0e", "0f", 44 | "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "1a", "1b", "1c", "1d", "1e", "1f", 45 | "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "2a", "2b", "2c", "2d", "2e", "2f", 46 | "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "3a", "3b", "3c", "3d", "3e", "3f", 47 | "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "4a", "4b", "4c", "4d", "4e", "4f", 48 | "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "5a", "5b", "5c", "5d", "5e", "5f", 49 | "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "6a", "6b", "6c", "6d", "6e", "6f", 50 | "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "7a", "7b", "7c", "7d", "7e", "7f", 51 | "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "8a", "8b", "8c", "8d", "8e", "8f", 52 | "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "9a", "9b", "9c", "9d", "9e", "9f", 53 | "a0", "a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9", "aa", "ab", "ac", "ad", "ae", "af", 54 | "b0", "b1", "b2", "b3", "b4", "b5", "b6", "b7", "b8", "b9", "ba", "bb", "bc", "bd", "be", "bf", 55 | "c0", "c1", "c2", "c3", "c4", "c5", "c6", "c7", "c8", "c9", "ca", "cb", "cc", "cd", "ce", "cf", 56 | "d0", "d1", "d2", "d3", "d4", "d5", "d6", "d7", "d8", "d9", "da", "db", "dc", "dd", "de", "df", 57 | "e0", "e1", "e2", "e3", "e4", "e5", "e6", "e7", "e8", "e9", "ea", "eb", "ec", "ed", "ee", "ef", 58 | "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7", "f8", "f9", "fa", "fb", "fc", "fd", "fe", "ff" 59 | }; 60 | 61 | /* 62 | * Fixed length of 3 including null terminate character. 63 | */ 64 | memcpy(&s->p[s->length], TextBTable[x & 255], 3); 65 | s->length += 2; 66 | } 67 | 68 | void _FASTCALL_ str_code_hb(_WString* s, unsigned int x) 69 | { 70 | static int8_t TextHBTable[256][5] = { 71 | /* 72 | * def prebuilt(): 73 | * s = "" 74 | * for i in xrange(256): 75 | * if ((i % 0x10) == 0): 76 | * s += "\r\n" 77 | * s += "\"0x%x\", " % (i) 78 | * return s 79 | */ 80 | "0x0", "0x1", "0x2", "0x3", "0x4", "0x5", "0x6", "0x7", "0x8", "0x9", "0xa", "0xb", "0xc", "0xd", "0xe", "0xf", 81 | "0x10", "0x11", "0x12", "0x13", "0x14", "0x15", "0x16", "0x17", "0x18", "0x19", "0x1a", "0x1b", "0x1c", "0x1d", "0x1e", "0x1f", 82 | "0x20", "0x21", "0x22", "0x23", "0x24", "0x25", "0x26", "0x27", "0x28", "0x29", "0x2a", "0x2b", "0x2c", "0x2d", "0x2e", "0x2f", 83 | "0x30", "0x31", "0x32", "0x33", "0x34", "0x35", "0x36", "0x37", "0x38", "0x39", "0x3a", "0x3b", "0x3c", "0x3d", "0x3e", "0x3f", 84 | "0x40", "0x41", "0x42", "0x43", "0x44", "0x45", "0x46", "0x47", "0x48", "0x49", "0x4a", "0x4b", "0x4c", "0x4d", "0x4e", "0x4f", 85 | "0x50", "0x51", "0x52", "0x53", "0x54", "0x55", "0x56", "0x57", "0x58", "0x59", "0x5a", "0x5b", "0x5c", "0x5d", "0x5e", "0x5f", 86 | "0x60", "0x61", "0x62", "0x63", "0x64", "0x65", "0x66", "0x67", "0x68", "0x69", "0x6a", "0x6b", "0x6c", "0x6d", "0x6e", "0x6f", 87 | "0x70", "0x71", "0x72", "0x73", "0x74", "0x75", "0x76", "0x77", "0x78", "0x79", "0x7a", "0x7b", "0x7c", "0x7d", "0x7e", "0x7f", 88 | "0x80", "0x81", "0x82", "0x83", "0x84", "0x85", "0x86", "0x87", "0x88", "0x89", "0x8a", "0x8b", "0x8c", "0x8d", "0x8e", "0x8f", 89 | "0x90", "0x91", "0x92", "0x93", "0x94", "0x95", "0x96", "0x97", "0x98", "0x99", "0x9a", "0x9b", "0x9c", "0x9d", "0x9e", "0x9f", 90 | "0xa0", "0xa1", "0xa2", "0xa3", "0xa4", "0xa5", "0xa6", "0xa7", "0xa8", "0xa9", "0xaa", "0xab", "0xac", "0xad", "0xae", "0xaf", 91 | "0xb0", "0xb1", "0xb2", "0xb3", "0xb4", "0xb5", "0xb6", "0xb7", "0xb8", "0xb9", "0xba", "0xbb", "0xbc", "0xbd", "0xbe", "0xbf", 92 | "0xc0", "0xc1", "0xc2", "0xc3", "0xc4", "0xc5", "0xc6", "0xc7", "0xc8", "0xc9", "0xca", "0xcb", "0xcc", "0xcd", "0xce", "0xcf", 93 | "0xd0", "0xd1", "0xd2", "0xd3", "0xd4", "0xd5", "0xd6", "0xd7", "0xd8", "0xd9", "0xda", "0xdb", "0xdc", "0xdd", "0xde", "0xdf", 94 | "0xe0", "0xe1", "0xe2", "0xe3", "0xe4", "0xe5", "0xe6", "0xe7", "0xe8", "0xe9", "0xea", "0xeb", "0xec", "0xed", "0xee", "0xef", 95 | "0xf0", "0xf1", "0xf2", "0xf3", "0xf4", "0xf5", "0xf6", "0xf7", "0xf8", "0xf9", "0xfa", "0xfb", "0xfc", "0xfd", "0xfe", "0xff" 96 | }; 97 | 98 | if (x < 0x10) { /* < 0x10 has a fixed length of 4 including null terminate. */ 99 | memcpy(&s->p[s->length], TextHBTable[x & 255], 4); 100 | s->length += 3; 101 | } else { /* >= 0x10 has a fixed length of 5 including null terminate. */ 102 | memcpy(&s->p[s->length], TextHBTable[x & 255], 5); 103 | s->length += 4; 104 | } 105 | } 106 | 107 | void _FASTCALL_ str_code_hdw(_WString* s, uint32_t x) 108 | { 109 | int8_t* buf; 110 | int i = 0, shift = 0; 111 | unsigned int t = 0; 112 | 113 | buf = (int8_t*)&s->p[s->length]; 114 | 115 | buf[0] = '0'; 116 | buf[1] = 'x'; 117 | buf += 2; 118 | 119 | for (shift = 28; shift != 0; shift -= 4) { 120 | t = (x >> shift) & 0xf; 121 | if (i | t) buf[i++] = NIBBLE_TO_CHR; 122 | } 123 | t = x & 0xf; 124 | buf[i++] = NIBBLE_TO_CHR; 125 | 126 | s->length += i + 2; 127 | buf[i] = '\0'; 128 | } 129 | 130 | void _FASTCALL_ str_code_hqw(_WString* s, uint8_t src[8]) 131 | { 132 | int8_t* buf; 133 | int i = 0, shift = 0; 134 | uint32_t x = RULONG(&src[sizeof(int32_t)]); 135 | int t; 136 | 137 | buf = (int8_t*)&s->p[s->length]; 138 | buf[0] = '0'; 139 | buf[1] = 'x'; 140 | buf += 2; 141 | 142 | for (shift = 28; shift != -4; shift -= 4) { 143 | t = (x >> shift) & 0xf; 144 | if (i | t) buf[i++] = NIBBLE_TO_CHR; 145 | } 146 | 147 | x = RULONG(src); 148 | for (shift = 28; shift != 0; shift -= 4) { 149 | t = (x >> shift) & 0xf; 150 | if (i | t) buf[i++] = NIBBLE_TO_CHR; 151 | } 152 | t = x & 0xf; 153 | buf[i++] = NIBBLE_TO_CHR; 154 | 155 | s->length += i + 2; 156 | buf[i] = '\0'; 157 | } 158 | 159 | #ifdef SUPPORT_64BIT_OFFSET 160 | void _FASTCALL_ str_off64(_WString* s, OFFSET_INTEGER x) 161 | { 162 | int8_t* buf; 163 | int i = 0, shift = 0; 164 | OFFSET_INTEGER t = 0; 165 | 166 | buf = (int8_t*)&s->p[s->length]; 167 | 168 | buf[0] = '0'; 169 | buf[1] = 'x'; 170 | buf += 2; 171 | 172 | for (shift = 60; shift != 0; shift -= 4) { 173 | t = (x >> shift) & 0xf; 174 | if (i | t) buf[i++] = NIBBLE_TO_CHR; 175 | } 176 | t = x & 0xf; 177 | buf[i++] = NIBBLE_TO_CHR; 178 | 179 | s->length += i + 2; 180 | buf[i] = '\0'; 181 | } 182 | #endif /* SUPPORT_64BIT_OFFSET */ 183 | 184 | #endif /* DISTORM_LIGHT */ 185 | -------------------------------------------------------------------------------- /core-packer/distorm/textdefs.h: -------------------------------------------------------------------------------- 1 | /* 2 | textdefs.h 3 | 4 | diStorm3 - Powerful disassembler for X86/AMD64 5 | http://ragestorm.net/distorm/ 6 | distorm at gmail dot com 7 | Copyright (C) 2003-2012 Gil Dabah 8 | 9 | This program is free software: you can redistribute it and/or modify 10 | it under the terms of the GNU General Public License as published by 11 | the Free Software Foundation, either version 3 of the License, or 12 | (at your option) any later version. 13 | 14 | This program is distributed in the hope that it will be useful, 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | GNU General Public License for more details. 18 | 19 | You should have received a copy of the GNU General Public License 20 | along with this program. If not, see 21 | */ 22 | 23 | 24 | #ifndef TEXTDEFS_H 25 | #define TEXTDEFS_H 26 | 27 | #include "config.h" 28 | #include "wstring.h" 29 | 30 | #ifndef DISTORM_LIGHT 31 | 32 | #define PLUS_DISP_CHR '+' 33 | #define MINUS_DISP_CHR '-' 34 | #define OPEN_CHR '[' 35 | #define CLOSE_CHR ']' 36 | #define SP_CHR ' ' 37 | #define SEG_OFF_CHR ':' 38 | 39 | /* 40 | Naming Convention: 41 | 42 | * get - returns a pointer to a string. 43 | * str - concatenates to string. 44 | 45 | * hex - means the function is used for hex dump (number is padded to required size) - Little Endian output. 46 | * code - means the function is used for disassembled instruction - Big Endian output. 47 | * off - means the function is used for 64bit offset - Big Endian output. 48 | 49 | * h - '0x' in front of the string. 50 | 51 | * b - byte 52 | * dw - double word (can be used for word also) 53 | * qw - quad word 54 | 55 | * all numbers are in HEX. 56 | */ 57 | 58 | extern int8_t TextBTable[256][4]; 59 | 60 | void _FASTCALL_ str_hex_b(_WString* s, unsigned int x); 61 | void _FASTCALL_ str_code_hb(_WString* s, unsigned int x); 62 | void _FASTCALL_ str_code_hdw(_WString* s, uint32_t x); 63 | void _FASTCALL_ str_code_hqw(_WString* s, uint8_t src[8]); 64 | 65 | #ifdef SUPPORT_64BIT_OFFSET 66 | void _FASTCALL_ str_off64(_WString* s, OFFSET_INTEGER x); 67 | #endif 68 | 69 | #endif /* DISTORM_LIGHT */ 70 | 71 | #endif /* TEXTDEFS_H */ 72 | -------------------------------------------------------------------------------- /core-packer/distorm/wstring.c: -------------------------------------------------------------------------------- 1 | /* 2 | wstring.c 3 | 4 | diStorm3 - Powerful disassembler for X86/AMD64 5 | http://ragestorm.net/distorm/ 6 | distorm at gmail dot com 7 | Copyright (C) 2003-2012 Gil Dabah 8 | 9 | This program is free software: you can redistribute it and/or modify 10 | it under the terms of the GNU General Public License as published by 11 | the Free Software Foundation, either version 3 of the License, or 12 | (at your option) any later version. 13 | 14 | This program is distributed in the hope that it will be useful, 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | GNU General Public License for more details. 18 | 19 | You should have received a copy of the GNU General Public License 20 | along with this program. If not, see 21 | */ 22 | 23 | 24 | #include "wstring.h" 25 | 26 | #ifndef DISTORM_LIGHT 27 | 28 | void strclear_WS(_WString* s) 29 | { 30 | s->p[0] = '\0'; 31 | s->length = 0; 32 | } 33 | 34 | void chrcat_WS(_WString* s, uint8_t ch) 35 | { 36 | s->p[s->length] = ch; 37 | s->p[s->length + 1] = '\0'; 38 | s->length += 1; 39 | } 40 | 41 | void strcpylen_WS(_WString* s, const int8_t* buf, unsigned int len) 42 | { 43 | s->length = len; 44 | memcpy((int8_t*)s->p, buf, len + 1); 45 | } 46 | 47 | void strcatlen_WS(_WString* s, const int8_t* buf, unsigned int len) 48 | { 49 | memcpy((int8_t*)&s->p[s->length], buf, len + 1); 50 | s->length += len; 51 | } 52 | 53 | void strcat_WS(_WString* s, const _WString* s2) 54 | { 55 | memcpy((int8_t*)&s->p[s->length], s2->p, s2->length + 1); 56 | s->length += s2->length; 57 | } 58 | 59 | #endif /* DISTORM_LIGHT */ 60 | -------------------------------------------------------------------------------- /core-packer/distorm/wstring.h: -------------------------------------------------------------------------------- 1 | /* 2 | wstring.h 3 | 4 | diStorm3 - Powerful disassembler for X86/AMD64 5 | http://ragestorm.net/distorm/ 6 | distorm at gmail dot com 7 | Copyright (C) 2003-2012 Gil Dabah 8 | 9 | This program is free software: you can redistribute it and/or modify 10 | it under the terms of the GNU General Public License as published by 11 | the Free Software Foundation, either version 3 of the License, or 12 | (at your option) any later version. 13 | 14 | This program is distributed in the hope that it will be useful, 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | GNU General Public License for more details. 18 | 19 | You should have received a copy of the GNU General Public License 20 | along with this program. If not, see 21 | */ 22 | 23 | 24 | #ifndef WSTRING_H 25 | #define WSTRING_H 26 | 27 | #include "config.h" 28 | 29 | #ifndef DISTORM_LIGHT 30 | 31 | void strclear_WS(_WString* s); 32 | void chrcat_WS(_WString* s, uint8_t ch); 33 | void strcpylen_WS(_WString* s, const int8_t* buf, unsigned int len); 34 | void strcatlen_WS(_WString* s, const int8_t* buf, unsigned int len); 35 | void strcat_WS(_WString* s, const _WString* s2); 36 | 37 | /* 38 | * Warning, this macro should be used only when the compiler knows the size of string in advance! 39 | * This macro is used in order to spare the call to strlen when the strings are known already. 40 | * Note: sizeof includes NULL terminated character. 41 | */ 42 | #define strcat_WSN(s, t) strcatlen_WS((s), ((const int8_t*)t), sizeof((t))-1) 43 | #define strcpy_WSN(s, t) strcpylen_WS((s), ((const int8_t*)t), sizeof((t))-1) 44 | 45 | #endif /* DISTORM_LIGHT */ 46 | 47 | #endif /* WSTRING_H */ 48 | -------------------------------------------------------------------------------- /core-packer/distorm/x86defs.h: -------------------------------------------------------------------------------- 1 | /* 2 | x86defs.h 3 | 4 | diStorm3 - Powerful disassembler for X86/AMD64 5 | http://ragestorm.net/distorm/ 6 | distorm at gmail dot com 7 | Copyright (C) 2003-2012 Gil Dabah 8 | 9 | This program is free software: you can redistribute it and/or modify 10 | it under the terms of the GNU General Public License as published by 11 | the Free Software Foundation, either version 3 of the License, or 12 | (at your option) any later version. 13 | 14 | This program is distributed in the hope that it will be useful, 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 | GNU General Public License for more details. 18 | 19 | You should have received a copy of the GNU General Public License 20 | along with this program. If not, see 21 | */ 22 | 23 | 24 | #ifndef X86DEFS_H 25 | #define X86DEFS_H 26 | 27 | 28 | #define SEG_REGS_MAX (6) 29 | #define CREGS_MAX (9) 30 | #define DREGS_MAX (8) 31 | 32 | /* Maximum instruction size, including prefixes */ 33 | #define INST_MAXIMUM_SIZE (15) 34 | 35 | /* Maximum range of imm8 (comparison type) of special SSE CMP instructions. */ 36 | #define INST_CMP_MAX_RANGE (8) 37 | 38 | /* Maximum range of imm8 (comparison type) of special AVX VCMP instructions. */ 39 | #define INST_VCMP_MAX_RANGE (32) 40 | 41 | /* Wait instruction byte code. */ 42 | #define INST_WAIT_INDEX (0x9b) 43 | 44 | /* Lea instruction byte code. */ 45 | #define INST_LEA_INDEX (0x8d) 46 | 47 | /* NOP/XCHG instruction byte code. */ 48 | #define INST_NOP_INDEX (0x90) 49 | 50 | /* ARPL/MOVSXD instruction byte code. */ 51 | #define INST_ARPL_INDEX (0x63) 52 | 53 | /* 54 | * Minimal MODR/M value of divided instructions. 55 | * It's 0xc0, two MSBs set, which indicates a general purpose register is used too. 56 | */ 57 | #define INST_DIVIDED_MODRM (0xc0) 58 | 59 | /* This is the escape byte value used for 3DNow! instructions. */ 60 | #define _3DNOW_ESCAPE_BYTE (0x0f) 61 | 62 | #define PREFIX_LOCK (0xf0) 63 | #define PREFIX_REPNZ (0xf2) 64 | #define PREFIX_REP (0xf3) 65 | #define PREFIX_CS (0x2e) 66 | #define PREFIX_SS (0x36) 67 | #define PREFIX_DS (0x3e) 68 | #define PREFIX_ES (0x26) 69 | #define PREFIX_FS (0x64) 70 | #define PREFIX_GS (0x65) 71 | #define PREFIX_OP_SIZE (0x66) 72 | #define PREFIX_ADDR_SIZE (0x67) 73 | #define PREFIX_VEX2b (0xc5) 74 | #define PREFIX_VEX3b (0xc4) 75 | 76 | /* REX prefix value range, 64 bits mode decoding only. */ 77 | #define PREFIX_REX_LOW (0x40) 78 | #define PREFIX_REX_HI (0x4f) 79 | /* In order to use the extended GPR's we have to add 8 to the Modr/M info values. */ 80 | #define EX_GPR_BASE (8) 81 | 82 | /* Mask for REX and VEX features: */ 83 | /* Base */ 84 | #define PREFIX_EX_B (1) 85 | /* Index */ 86 | #define PREFIX_EX_X (2) 87 | /* Register */ 88 | #define PREFIX_EX_R (4) 89 | /* Operand Width */ 90 | #define PREFIX_EX_W (8) 91 | /* Vector Lengh */ 92 | #define PREFIX_EX_L (0x10) 93 | 94 | #endif /* X86DEFS_H */ 95 | -------------------------------------------------------------------------------- /core-packer/dll32.h: -------------------------------------------------------------------------------- 1 | #ifndef __DLL32_H_ 2 | #define __DLL32_H_ 3 | 4 | extern ULONG64 dwRelocSize; 5 | extern ULONG64 lpRelocAddress; 6 | extern ULONG64 _rc4key0; 7 | extern ULONG64 _rc4key1; 8 | extern ULONG64 _baseAddress; 9 | 10 | //extern "C" g_hKernel32; 11 | 12 | //extern "C" VirtualAlloc_ptr _VirtualAlloc; 13 | extern VirtualProtect_ptr _VirtualProtect; 14 | extern VirtualAlloc_ptr _VirtualAlloc; 15 | 16 | //extern "C" BOOL WINAPI _EntryPoint(LPVOID, HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved); 17 | //extern "C" HMODULE g_hKernel32; 18 | extern "C" BYTE g_decrypted; 19 | extern "C" LPVOID g_lpTextBaseAddr; 20 | 21 | extern "C" VOID WINAPI _FakeEntryPoint0(); 22 | extern "C" VOID WINAPI _FakeEntryPoint1(); 23 | extern "C" VOID WINAPI _FakeEntryPoint2(); 24 | extern "C" VOID WINAPI _FakeEntryPoint3(); 25 | extern "C" VOID WINAPI _FakeEntryPoint4(); 26 | extern "C" VOID WINAPI _FakeEntryPoint5(); 27 | extern "C" VOID WINAPI _FakeEntryPoint6(); 28 | extern "C" VOID WINAPI _FakeEntryPoint7(); 29 | extern "C" VOID WINAPI _FakeEntryPoint8(); 30 | extern "C" VOID WINAPI _FakeEntryPoint9(); 31 | extern "C" VOID WINAPI _FakeEntryPointA(); 32 | extern "C" VOID WINAPI _FakeEntryPointB(); 33 | extern "C" VOID WINAPI _FakeEntryPointC(); 34 | extern "C" VOID WINAPI _FakeEntryPointD(); 35 | extern "C" VOID WINAPI _FakeEntryPointE(); 36 | extern "C" VOID WINAPI _FakeEntryPointF(); 37 | 38 | #endif 39 | -------------------------------------------------------------------------------- /core-packer/dll64.h: -------------------------------------------------------------------------------- 1 | #ifndef __DLL64_H_ 2 | #define __DLL64_H_ 3 | 4 | extern ULONG64 dwRelocSize; 5 | extern ULONG64 lpRelocAddress; 6 | extern ULONG64 _rc4key0; 7 | extern ULONG64 _rc4key1; 8 | extern ULONG64 _baseAddress; 9 | 10 | extern "C" HMODULE g_hKernel32; 11 | 12 | //extern "C" VirtualAlloc_ptr _VirtualAlloc; 13 | extern VirtualProtect_ptr _VirtualProtect; 14 | extern VirtualAlloc_ptr _VirtualAlloc; 15 | 16 | extern "C" BOOL WINAPI _EntryPoint(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved); 17 | extern "C" HMODULE g_hKernel32; 18 | extern "C" BYTE g_decrypted; 19 | extern "C" LPVOID g_lpTextBaseAddr; 20 | 21 | extern "C" VOID WINAPI _FakeEntryPoint0(); 22 | extern "C" VOID WINAPI _FakeEntryPoint1(); 23 | extern "C" VOID WINAPI _FakeEntryPoint2(); 24 | extern "C" VOID WINAPI _FakeEntryPoint3(); 25 | extern "C" VOID WINAPI _FakeEntryPoint4(); 26 | extern "C" VOID WINAPI _FakeEntryPoint5(); 27 | extern "C" VOID WINAPI _FakeEntryPoint6(); 28 | extern "C" VOID WINAPI _FakeEntryPoint7(); 29 | extern "C" VOID WINAPI _FakeEntryPoint8(); 30 | extern "C" VOID WINAPI _FakeEntryPoint9(); 31 | 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /core-packer/dll_fix32.asm: -------------------------------------------------------------------------------- 1 | .model flat, stdcall 2 | 3 | option casemap :none 4 | 5 | code SEGMENT PUBLIC READ EXECUTE ALIAS('.pedll32') 6 | 7 | ;public _VirtualProtect 8 | ;public _VirtualAlloc 9 | ;public g_hKernel32 10 | ;public _dll32_LoadLibraryA 11 | ;public _dll32_GetProcAddress 12 | ;public _EntryPoint 13 | 14 | extern DELAYDECRYPT@4 : PROC 15 | extern DELAYENCRYPT@0 : PROC 16 | 17 | ;g_hKernel32 dd ? ; symbol! 18 | 19 | ; jmp to KERNEL32!VirtualProtect in relocated module 20 | 21 | ;_VirtualProtect PROC 22 | ; db 0e9h 23 | ; dq 0babecafe00000005h 24 | ;_VirtualProtect ENDP 25 | 26 | ;_VirtualAlloc PROC 27 | ; db 0e9h 28 | ; dq 0BABECAFEBAD00002h 29 | ;_VirtualAlloc ENDP 30 | 31 | end 32 | -------------------------------------------------------------------------------- /core-packer/dll_fix64.asm: -------------------------------------------------------------------------------- 1 | option casemap :none 2 | 3 | hermit64 SEGMENT READ EXECUTE ALIAS('.pedll64') 4 | 5 | ;public _VirtualProtect 6 | ;public _VirtualAlloc 7 | public g_hKernel32 8 | 9 | public _FakeEntryPoint0 10 | public _FakeEntryPoint1 11 | public _FakeEntryPoint2 12 | public _FakeEntryPoint3 13 | public _FakeEntryPoint4 14 | public _FakeEntryPoint5 15 | public _FakeEntryPoint6 16 | public _FakeEntryPoint7 17 | public _FakeEntryPoint8 18 | public _FakeEntryPoint9 19 | public _EntryPoint 20 | 21 | extern DELAYDECRYPT : PROC 22 | 23 | g_hKernel32 dq ? ; symbol! 24 | 25 | ; jmp to KERNEL32!VirtualProtect in relocated module 26 | 27 | ;_VirtualProtect PROC 28 | ; db 0e9h 29 | ; dq 0babecafe00000005h 30 | ;_VirtualProtect ENDP 31 | 32 | ;_VirtualAlloc PROC 33 | ; db 0e9h 34 | ; dq 0BABECAFEBAD00002h 35 | ;_VirtualAlloc ENDP 36 | 37 | ; LoadLibraryA 38 | _LoadLibraryA PROC 39 | mov rax, qword ptr [g_hKernel32] 40 | add rax, 11223344h 41 | jmp qword ptr [rax] 42 | _LoadLibraryA ENDP 43 | 44 | _GetProcAddress PROC 45 | mov rax, qword ptr [g_hKernel32] 46 | add rax, 11223344h 47 | jmp qword ptr [rax] 48 | _GetProcAddress ENDP 49 | 50 | _EntryPoint PROC 51 | db 0e9h 52 | dq 0BABECAFEBAD00000h 53 | _EntryPoint ENDP 54 | 55 | _FakeEntryPoint0 PROC 56 | sub rsp, 48h 57 | mov [rsp+00h], rcx 58 | mov [rsp+08h], rdx 59 | mov [rsp+10h], r8 60 | mov [rsp+18h], r9 61 | 62 | call DELAYDECRYPT 63 | 64 | mov r9, [rsp+18h] 65 | mov r8, [rsp+10h] 66 | mov rdx, [rsp+08h] 67 | mov rcx, [rsp+00h] 68 | add rsp, 48h 69 | 70 | db 0e9h 71 | dq 0BABECAFEBAD00000h 72 | _FakeEntryPoint0 ENDP 73 | 74 | _FakeEntryPoint1 PROC 75 | sub rsp, 48h 76 | mov [rsp+00h], rcx 77 | mov [rsp+08h], rdx 78 | mov [rsp+10h], r8 79 | mov [rsp+18h], r9 80 | 81 | call DELAYDECRYPT 82 | 83 | mov r9, [rsp+18h] 84 | mov r8, [rsp+10h] 85 | mov rdx, [rsp+08h] 86 | mov rcx, [rsp+00h] 87 | add rsp, 48h 88 | db 0e9h 89 | dq 0BABECAFEBAD00000h 90 | _FakeEntryPoint1 ENDP 91 | 92 | _FakeEntryPoint2 PROC 93 | sub rsp, 48h 94 | mov [rsp+00h], rcx 95 | mov [rsp+08h], rdx 96 | mov [rsp+10h], r8 97 | mov [rsp+18h], r9 98 | 99 | call DELAYDECRYPT 100 | 101 | mov r9, [rsp+18h] 102 | mov r8, [rsp+10h] 103 | mov rdx, [rsp+08h] 104 | mov rcx, [rsp+00h] 105 | add rsp, 48h 106 | db 0e9h 107 | dq 0BABECAFEBAD00000h 108 | _FakeEntryPoint2 ENDP 109 | 110 | _FakeEntryPoint3 PROC 111 | sub rsp, 48h 112 | mov [rsp+00h], rcx 113 | mov [rsp+08h], rdx 114 | mov [rsp+10h], r8 115 | mov [rsp+18h], r9 116 | 117 | call DELAYDECRYPT 118 | 119 | mov r9, [rsp+18h] 120 | mov r8, [rsp+10h] 121 | mov rdx, [rsp+08h] 122 | mov rcx, [rsp+00h] 123 | add rsp, 48h 124 | db 0e9h 125 | dq 0BABECAFEBAD00000h 126 | _FakeEntryPoint3 ENDP 127 | 128 | _FakeEntryPoint4 PROC 129 | sub rsp, 48h 130 | mov [rsp+00h], rcx 131 | mov [rsp+08h], rdx 132 | mov [rsp+10h], r8 133 | mov [rsp+18h], r9 134 | 135 | call DELAYDECRYPT 136 | 137 | mov r9, [rsp+18h] 138 | mov r8, [rsp+10h] 139 | mov rdx, [rsp+08h] 140 | mov rcx, [rsp+00h] 141 | add rsp, 48h 142 | db 0e9h 143 | dq 0BABECAFEBAD00000h 144 | _FakeEntryPoint4 ENDP 145 | 146 | _FakeEntryPoint5 PROC 147 | sub rsp, 48h 148 | mov [rsp+00h], rcx 149 | mov [rsp+08h], rdx 150 | mov [rsp+10h], r8 151 | mov [rsp+18h], r9 152 | 153 | call DELAYDECRYPT 154 | 155 | mov r9, [rsp+18h] 156 | mov r8, [rsp+10h] 157 | mov rdx, [rsp+08h] 158 | mov rcx, [rsp+00h] 159 | add rsp, 48h 160 | db 0e9h 161 | dq 0BABECAFEBAD00000h 162 | _FakeEntryPoint5 ENDP 163 | 164 | _FakeEntryPoint6 PROC 165 | sub rsp, 48h 166 | mov [rsp+00h], rcx 167 | mov [rsp+08h], rdx 168 | mov [rsp+10h], r8 169 | mov [rsp+18h], r9 170 | 171 | call DELAYDECRYPT 172 | 173 | mov r9, [rsp+18h] 174 | mov r8, [rsp+10h] 175 | mov rdx, [rsp+08h] 176 | mov rcx, [rsp+00h] 177 | add rsp, 48h 178 | db 0e9h 179 | dq 0BABECAFEBAD00000h 180 | _FakeEntryPoint6 ENDP 181 | 182 | _FakeEntryPoint7 PROC 183 | sub rsp, 48h 184 | mov [rsp+00h], rcx 185 | mov [rsp+08h], rdx 186 | mov [rsp+10h], r8 187 | mov [rsp+18h], r9 188 | 189 | call DELAYDECRYPT 190 | 191 | mov r9, [rsp+18h] 192 | mov r8, [rsp+10h] 193 | mov rdx, [rsp+08h] 194 | mov rcx, [rsp+00h] 195 | add rsp, 48h 196 | 197 | db 0e9h 198 | dq 0BABECAFEBAD00000h 199 | _FakeEntryPoint7 ENDP 200 | 201 | _FakeEntryPoint8 PROC 202 | sub rsp, 48h 203 | mov [rsp+00h], rcx 204 | mov [rsp+08h], rdx 205 | mov [rsp+10h], r8 206 | mov [rsp+18h], r9 207 | 208 | call DELAYDECRYPT 209 | 210 | mov r9, [rsp+18h] 211 | mov r8, [rsp+10h] 212 | mov rdx, [rsp+08h] 213 | mov rcx, [rsp+00h] 214 | add rsp, 48h 215 | 216 | db 0e9h 217 | dq 0BABECAFEBAD00000h 218 | _FakeEntryPoint8 ENDP 219 | 220 | _FakeEntryPoint9 PROC 221 | sub rsp, 48h 222 | mov [rsp+00h], rcx 223 | mov [rsp+08h], rdx 224 | mov [rsp+10h], r8 225 | mov [rsp+18h], r9 226 | 227 | call DELAYDECRYPT 228 | 229 | mov r9, [rsp+18h] 230 | mov r8, [rsp+10h] 231 | mov rdx, [rsp+08h] 232 | mov rcx, [rsp+00h] 233 | add rsp, 48h 234 | 235 | db 0e9h 236 | dq 0BABECAFEBAD00000h 237 | _FakeEntryPoint9 ENDP 238 | 239 | end 240 | -------------------------------------------------------------------------------- /core-packer/library.h: -------------------------------------------------------------------------------- 1 | #ifndef __LIBRARY_H__ 2 | #define __LIBRARY_H__ 3 | 4 | /** 5 | * Load a DLL in memory, without applying relocations, resolving iat and others... 6 | **/ 7 | LPVOID InternalLoadLibrary(TCHAR* lpFileName, DWORD dwAdditionalPages); 8 | BOOL InternalWriteLibraryToFile(LPVOID lpBase, TCHAR* lpOutFileName); 9 | 10 | PIMAGE_NT_HEADERS GetNTHeader(LPVOID lpBaseAddress); 11 | PIMAGE_SECTION_HEADER SectionHeader(LPVOID lpBaseAddress); 12 | 13 | /** 14 | * Increment the PE header section count 15 | **/ 16 | PIMAGE_SECTION_HEADER AddSection(LPVOID lpBaseAddress); 17 | PIMAGE_SECTION_HEADER LastHeader(LPVOID lpBaseAddress); 18 | 19 | /** 20 | * Increase the optional header 21 | **/ 22 | LPVOID ExpandOptionalHeader(LPVOID lpBaseAddress, WORD requiredBytes); 23 | 24 | DWORD __stdcall RoundUp(DWORD value, DWORD base); 25 | 26 | BOOL SaveLibrary64ToFile(LPVOID lpBase, TCHAR* lpOutFileName); 27 | BOOL SaveLibraryToFile(LPVOID lpBase, TCHAR* lpOutFileName); 28 | #endif 29 | -------------------------------------------------------------------------------- /core-packer/macro.h: -------------------------------------------------------------------------------- 1 | #ifndef __MACRO_H_ 2 | #define __MACRO_H_ 3 | 4 | #define CALC_OFFSET(type, ptr, offset) (type) (((ULONG64) ptr) + offset) 5 | #define CALC_OFFSET_DISP(type, base, offset, disp) (type)((DWORD)(base) + (DWORD)(offset) + disp) 6 | #define CALC_DISP(type, offset, ptr) (type) (((ULONG64) offset) - (ULONG64) ptr) 7 | #endif 8 | -------------------------------------------------------------------------------- /core-packer/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include "tclap/CmdLine.h" 6 | 7 | extern BOOL WINAPI DllEntryPoint(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved); 8 | 9 | int main(int argc, char *argv[]) 10 | { 11 | #ifdef _BUILD32 12 | #define _PACKAGENAME "packer32" 13 | #else 14 | #define _PACKAGENAME "packer64" 15 | #endif 16 | 17 | TCLAP::CmdLine cmdline(_PACKAGENAME, ' ', "0.5 - XPACK FIGHT!"); 18 | 19 | TCLAP::ValueArg inputFile("i", "infile", "Input file", true, "homer", "filename"); 20 | TCLAP::ValueArg outputFile("o", "outfile", "Output file", false, "homer", "filename"); 21 | TCLAP::ValueArg verbose("v", "verbose", "Verbose output", false, "", ""); 22 | TCLAP::ValueArg test("t", "testmode", "Enable test mode", false, "", ""); 23 | 24 | cmdline.add(inputFile); 25 | cmdline.add(outputFile); 26 | cmdline.add(verbose); 27 | cmdline.add(test); 28 | 29 | //cmdline.parse(argc, argv); 30 | 31 | #ifdef _BUILD32 32 | 33 | #ifdef _TESTCASE 34 | 35 | extern int main32_test(int argc, char *argv[]); 36 | return main32_test(argc, argv); 37 | #else 38 | extern int main32(int, char*argv[]); 39 | extern int unpack32(int, char*argv[]); 40 | 41 | if (argv[1][0] == '-' && argv[1][1] == 'u') 42 | return unpack32(argc, argv); 43 | else 44 | return main32(argc, argv); 45 | return main32(argc, argv); 46 | #endif 47 | 48 | #else 49 | extern int main64(int argc, char *argv[]); 50 | return main64(argc, argv); 51 | #endif 52 | 53 | 54 | } 55 | -------------------------------------------------------------------------------- /core-packer/patchutils.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "library.h" 3 | #include "macro.h" 4 | #include "rva.h" 5 | #include "rc4.h" 6 | #include "symbols.h" 7 | 8 | /** 9 | * NextPointerToRawData 10 | * in: PIMAGE_NT_HEADERS (32 bit) 11 | * out: ([LAST SECTION ON IMAGE].PointerToRawData + SizeOfRawData) 12 | **/ 13 | DWORD NextPointerToRawData(PIMAGE_NT_HEADERS pHeader) 14 | { 15 | DWORD dwPointerToRawData = 0; 16 | PIMAGE_SECTION_HEADER pSectionToProcess = NULL; 17 | 18 | WORD NumberOfSections = pHeader->FileHeader.NumberOfSections; 19 | 20 | for(PIMAGE_SECTION_HEADER pSection = IMAGE_FIRST_SECTION(pHeader); NumberOfSections > 0; NumberOfSections--, pSection++) 21 | { 22 | if (pSection->PointerToRawData > dwPointerToRawData) 23 | { 24 | dwPointerToRawData = pSection->PointerToRawData; 25 | pSectionToProcess = pSection; 26 | } 27 | } 28 | 29 | if (pSectionToProcess != NULL) 30 | { 31 | dwPointerToRawData = RoundUp(dwPointerToRawData + pSectionToProcess->SizeOfRawData, pHeader->OptionalHeader.FileAlignment); 32 | } 33 | 34 | return dwPointerToRawData; 35 | } 36 | 37 | /** 38 | * NextPointerToRawData 39 | * in: PIMAGE_NT_HEADERS (64 bit) 40 | * out: ([LAST SECTION ON IMAGE].PointerToRawData + SizeOfRawData) 41 | **/ 42 | DWORD NextPointerToRawData64(PIMAGE_NT_HEADERS64 pHeader) 43 | { 44 | DWORD dwPointerToRawData = 0; 45 | PIMAGE_SECTION_HEADER pSectionToProcess = NULL; 46 | 47 | WORD NumberOfSections = pHeader->FileHeader.NumberOfSections; 48 | 49 | for(PIMAGE_SECTION_HEADER pSection = IMAGE_FIRST_SECTION(pHeader); NumberOfSections > 0; NumberOfSections--, pSection++) 50 | { 51 | if (pSection->PointerToRawData > dwPointerToRawData) 52 | { 53 | dwPointerToRawData = pSection->PointerToRawData; 54 | pSectionToProcess = pSection; 55 | } 56 | } 57 | 58 | if (pSectionToProcess != NULL) 59 | { 60 | dwPointerToRawData = RoundUp(dwPointerToRawData + pSectionToProcess->SizeOfRawData, pHeader->OptionalHeader.FileAlignment); 61 | } 62 | 63 | return dwPointerToRawData; 64 | } 65 | 66 | /** 67 | * NextVirtualAddress 68 | * in: PIMAGE_NT_HEADERS (32 bit) 69 | * out: ([LAST SECTION ON IMAGE].VirtualAddress + VirtualSize) 70 | **/ 71 | DWORD NextVirtualAddress(PIMAGE_NT_HEADERS pHeader) 72 | { 73 | DWORD dwNextVirtualAddress = 0; 74 | PIMAGE_SECTION_HEADER pSectionToProcess = NULL; 75 | 76 | WORD NumberOfSections = pHeader->FileHeader.NumberOfSections; 77 | 78 | for(PIMAGE_SECTION_HEADER pSection = IMAGE_FIRST_SECTION(pHeader); NumberOfSections > 0; NumberOfSections--, pSection++) 79 | { 80 | if (pSection->VirtualAddress > dwNextVirtualAddress) 81 | { 82 | dwNextVirtualAddress = pSection->VirtualAddress; 83 | pSectionToProcess = pSection; 84 | } 85 | } 86 | 87 | if (pSectionToProcess != NULL) 88 | { 89 | dwNextVirtualAddress = RoundUp(dwNextVirtualAddress + pSectionToProcess->Misc.VirtualSize, pHeader->OptionalHeader.SectionAlignment); 90 | } 91 | 92 | return dwNextVirtualAddress; 93 | } 94 | 95 | /** 96 | * NextVirtualAddress 97 | * in: PIMAGE_NT_HEADERS (64 bit) 98 | * out: ([LAST SECTION ON IMAGE].VirtualAddress + VirtualSize) 99 | **/ 100 | DWORD NextVirtualAddress64(PIMAGE_NT_HEADERS64 pHeader) 101 | { 102 | DWORD dwNextVirtualAddress = 0; 103 | PIMAGE_SECTION_HEADER pSectionToProcess = NULL; 104 | 105 | WORD NumberOfSections = pHeader->FileHeader.NumberOfSections; 106 | 107 | for(PIMAGE_SECTION_HEADER pSection = IMAGE_FIRST_SECTION(pHeader); NumberOfSections > 0; NumberOfSections--, pSection++) 108 | { 109 | if (pSection->VirtualAddress > dwNextVirtualAddress) 110 | { 111 | dwNextVirtualAddress = pSection->VirtualAddress; 112 | pSectionToProcess = pSection; 113 | } 114 | } 115 | 116 | if (pSectionToProcess != NULL) 117 | { 118 | dwNextVirtualAddress = RoundUp(dwNextVirtualAddress + pSectionToProcess->Misc.VirtualSize, pHeader->OptionalHeader.SectionAlignment); 119 | } 120 | 121 | return dwNextVirtualAddress; 122 | } 123 | // find a block of memory to patch! 124 | LPVOID FindBlockMem(LPBYTE lpInitialMem, DWORD dwSize, LPVOID lpSignature, DWORD dwSignatureSize) 125 | { 126 | if (lpSignature == NULL) 127 | return NULL; 128 | 129 | while(dwSize >= dwSignatureSize) 130 | { 131 | if (memcmp(lpInitialMem, lpSignature, dwSignatureSize) == 0) 132 | return (LPVOID) lpInitialMem; 133 | 134 | dwSize --; 135 | lpInitialMem++; 136 | } 137 | 138 | return NULL; 139 | } 140 | 141 | void Patch_JMP(LPBYTE lpInstruction, DWORD dwNewOffset) 142 | { 143 | LPDWORD dummy = CALC_OFFSET(LPDWORD, lpInstruction, 1); 144 | 145 | *dummy = dwNewOffset; 146 | } 147 | 148 | void Patch_MOV(LPBYTE lpInstruction, DWORD dwNewOffset) 149 | { 150 | LPDWORD dummy = CALC_OFFSET(LPDWORD, lpInstruction, 9); 151 | *dummy = dwNewOffset; 152 | } 153 | 154 | void Patch_MARKER_QWORD(LPVOID lpBaseBlock, LPBYTE lpInitialMem, DWORD dwSize, LPVOID lpSignature, ULONG64 value) 155 | { 156 | LPVOID lpInitialByte = FindBlockMem((LPBYTE) lpInitialMem, dwSize, lpSignature, 8); 157 | 158 | if (lpInitialByte != NULL) 159 | { 160 | memcpy(lpInitialByte, &value, sizeof(ULONG64)); 161 | } 162 | } 163 | 164 | void Patch_MARKER_DWORD(LPVOID lpBaseBlock, LPBYTE lpInitialMem, DWORD dwSize, LPVOID lpSignature, DWORD value) 165 | { 166 | LPVOID lpInitialByte = FindBlockMem((LPBYTE) lpInitialMem, dwSize, lpSignature, 8); 167 | 168 | if (lpInitialByte != NULL) 169 | { 170 | ULONG64 qValue = (ULONG64) value; 171 | 172 | memcpy(lpInitialByte, &qValue, sizeof(ULONG64)); 173 | } 174 | } 175 | 176 | void Patch_Entry(LPVOID lpBaseBlock, LPBYTE lpInitialMem, DWORD dwSize, LPVOID lpSignature, DWORD dwSignatureSize, DWORD dwOldOffset) 177 | { 178 | LPVOID lpInitialByte = FindBlockMem((LPBYTE) lpInitialMem, dwSize, lpSignature, dwSignatureSize); 179 | 180 | if (lpInitialByte != NULL) 181 | { 182 | LPDWORD c = CALC_OFFSET(LPDWORD, lpInitialByte, 0x10); 183 | *c = dwOldOffset; 184 | } 185 | } 186 | 187 | void Patch_Entry(LPVOID lpBaseBlock, LPBYTE lpInitialMem, DWORD dwSize, LPVOID lpSignature, DWORD dwSignatureSize, DWORD dwOldOffset, DWORD dwDisp) 188 | { 189 | LPVOID lpInitialByte = FindBlockMem((LPBYTE) lpInitialMem, dwSize, lpSignature, dwSignatureSize); 190 | 191 | if (lpInitialByte != NULL) 192 | { 193 | LPDWORD c = CALC_OFFSET(LPDWORD, lpInitialByte, dwDisp); 194 | *c = dwOldOffset; 195 | } 196 | } 197 | 198 | void Patch_MARKER(LPVOID lpBaseBlock, LPBYTE lpInitialMem, DWORD dwSize, LPVOID lpSignature, DWORD dwSignatureSize, DWORD dwOldOffset) 199 | { 200 | if (lpSignature == NULL) 201 | return; 202 | 203 | LPVOID lpInitialByte = FindBlockMem((LPBYTE) lpInitialMem, dwSize, lpSignature, dwSignatureSize); 204 | 205 | if (lpInitialByte != NULL) 206 | { 207 | LPBYTE c = CALC_OFFSET(LPBYTE, lpInitialByte, 0); 208 | 209 | if (*c == 0xe9) // jmp marker 210 | { 211 | #ifdef _BUILD64 212 | ULONG64 rva = ((ULONG64) lpInitialByte + 5) - ((ULONG64) lpInitialMem) + ((ULONG64) lpBaseBlock); 213 | DWORD dwNewValue = diff_rva64(NULL, NULL, dwOldOffset, (DWORD) rva); 214 | #else 215 | ULONG64 x = ((ULONG64) lpInitialByte + 5) - ((ULONG64) lpBaseBlock); 216 | DWORD dwNewValue = diff_rva32(NULL, NULL, dwOldOffset, (DWORD) x); 217 | #endif 218 | 219 | Patch_JMP((LPBYTE) lpInitialByte, dwNewValue); 220 | } 221 | else if (*c == 0x48) // mov marker! 222 | { 223 | Patch_MOV((LPBYTE) lpInitialByte, dwOldOffset); 224 | } 225 | else if (*c == 0x55) 226 | { // push ebp ... 227 | LPDWORD sum = CALC_OFFSET(LPDWORD, c, 0x0c); 228 | *sum = dwOldOffset; 229 | } 230 | else if (*c == 0x8b && c[1] == 0xe5) 231 | { 232 | LPDWORD sum = CALC_OFFSET(LPDWORD, c, 0x09); 233 | *sum = dwOldOffset; 234 | } 235 | else if (*c == 0x90 && c[1] == 0x90) 236 | { 237 | LPDWORD sum = CALC_OFFSET(LPDWORD, c, 0x09); 238 | *sum = dwOldOffset; 239 | } 240 | } 241 | } 242 | -------------------------------------------------------------------------------- /core-packer/patchutils.h: -------------------------------------------------------------------------------- 1 | #ifndef __PATCHUTILS_H_ 2 | #define __PATCHUTILS_H_ 3 | 4 | /** 5 | * @fn NextPointerToRawData 6 | * @brief Calculate the new "Pointer to Raw Data" for new section! 7 | **/ 8 | DWORD NextPointerToRawData(PIMAGE_NT_HEADERS pHeader); 9 | 10 | /** 11 | * @fn NextPointerToRawData64 12 | * @brief Calculate the new "Pointer to Raw Data" for new section! 13 | **/ 14 | DWORD NextPointerToRawData64(PIMAGE_NT_HEADERS64 pHeader); 15 | 16 | DWORD NextVirtualAddress(PIMAGE_NT_HEADERS pHeader); 17 | 18 | DWORD NextVirtualAddress64(PIMAGE_NT_HEADERS64 pHeader); 19 | 20 | LPVOID FindBlockMem(LPBYTE lpInitialMem, DWORD dwSize, LPVOID lpSignature, DWORD dwSignatureSize); 21 | 22 | void Patch_JMP(LPBYTE lpInstruction, DWORD dwNewOffset); 23 | void Patch_MOV(LPBYTE lpInstruction, DWORD dwNewOffset); 24 | void Patch_MARKER_QWORD(LPVOID lpBaseBlock, LPBYTE lpInitialMem, DWORD dwSize, LPVOID lpSignature, ULONG64 value); 25 | void Patch_MARKER_DWORD(LPVOID lpBaseBlock, LPBYTE lpInitialMem, DWORD dwSize, LPVOID lpSignature, DWORD value); 26 | 27 | void Patch_Entry(LPVOID lpBaseBlock, LPBYTE lpInitialMem, DWORD dwSize, LPVOID lpSignature, DWORD dwSignatureSize, DWORD dwOldOffset); 28 | void Patch_Entry(LPVOID lpBaseBlock, LPBYTE lpInitialMem, DWORD dwSize, LPVOID lpSignature, DWORD dwSignatureSize, DWORD dwOldOffset, DWORD dwDisp); 29 | void Patch_MARKER(LPVOID lpBaseBlock, LPBYTE lpInitialMem, DWORD dwSize, LPVOID lpSignature, DWORD dwSignatureSize, DWORD dwOldOffset); 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /core-packer/pe_fix32.asm: -------------------------------------------------------------------------------- 1 | .model flat, stdcall 2 | 3 | option casemap :none 4 | 5 | peexe SEGMENT PUBLIC READ EXECUTE ALIAS('.peexe32') 6 | 7 | public exe_g_hKernel32 8 | ;public _exe_LoadLibraryA 9 | ;public _exe_GetProcAddress 10 | public _exe_EntryPoint 11 | 12 | exe_g_hKernel32 dd ? ; symbol! 13 | 14 | ; jmp to KERNEL32!VirtualProtect in relocated module 15 | 16 | ;_VirtualProtect PROC 17 | ; db 0e9h 18 | ; dq 0babecafe00000005h 19 | ;_VirtualProtect ENDP 20 | 21 | ;_VirtualAlloc PROC 22 | ; db 0e9h 23 | ; dq 0BABECAFEBAD00002h 24 | ;_VirtualAlloc ENDP 25 | 26 | ; LoadLibraryA 27 | ;_exe_LoadLibraryA PROC param1: DWORD 28 | ;mov esp, ebp 29 | ;pop ebp 30 | ;call _GETBASE 31 | ;add eax, 11223340h 32 | ;jmp dword ptr ds:[eax] 33 | ;nop 34 | ;nop 35 | ;nop 36 | ;nop 37 | ;nop 38 | ;_exe_LoadLibraryA ENDP 39 | 40 | ;_exe_GetProcAddress PROC param1: DWORD, param2: DWORD 41 | ;mov esp, ebp 42 | ;pop ebp 43 | ;call _GETBASE 44 | ;add eax, 11223341h 45 | ;jmp dword ptr [eax] 46 | ;nop 47 | ;nop 48 | ;nop 49 | ;nop 50 | ;nop 51 | ;_exe_GetProcAddress ENDP 52 | 53 | 54 | ;_exe_SetFilePointer PROC param1: DWORD, param2: DWORD, param3: DWORD, p4: DWORD; 55 | ;mov esp, ebp 56 | ;pop ebp 57 | ;call _GETBASE 58 | ;add eax, 11223342h 59 | ;jmp dword ptr [eax] 60 | ;nop 61 | ;nop 62 | ;nop 63 | ;nop 64 | ;nop 65 | ;_exe_SetFilePointer ENDP 66 | 67 | _exe_CloseHandle PROC param1: DWORD 68 | mov esp, ebp 69 | pop ebp 70 | call _GETBASE 71 | add eax, 11223343h 72 | jmp dword ptr [eax] 73 | nop 74 | nop 75 | nop 76 | nop 77 | nop 78 | _exe_CloseHandle ENDP 79 | 80 | _exe_ReadFile PROC param1: DWORD, param2: DWORD, param3: DWORD, p4: DWORD, p5: DWORD 81 | mov esp, ebp 82 | pop ebp 83 | call _GETBASE 84 | add eax, 11223344h 85 | jmp dword ptr [eax] 86 | nop 87 | nop 88 | nop 89 | nop 90 | nop 91 | _exe_ReadFile ENDP 92 | 93 | _exe_GetModuleFileNameA PROC param1: DWORD, param2: DWORD, param3: DWORD 94 | mov esp, ebp 95 | pop ebp 96 | call _GETBASE 97 | add eax, 11223345h 98 | jmp dword ptr [eax] 99 | nop 100 | nop 101 | nop 102 | nop 103 | nop 104 | _exe_GetModuleFileNameA ENDP 105 | 106 | ;_exe_CreateFileA PROC lpFileName: DWORD, dwDesiredAccess: DWORD, dwShareMode: DWORD, lpSecurityAttribytes: DWORD, dwCreationDisposition: DWORD, dwFlagsAndAttributes: DWORD, hTemplateFile: DWORD 107 | ; mov esp, ebp 108 | ; pop ebp 109 | ;call _GETBASE 110 | ;add eax, 11223346h 111 | ;jmp dword ptr [eax] 112 | ;nop 113 | ;nop 114 | ;nop 115 | ;nop 116 | ;nop 117 | ;_exe_CreateFileA ENDP 118 | 119 | _exe_EntryPoint PROC param1: DWORD, param2: DWORD, param3: DWORD, param4: DWORD 120 | push dword ptr [ebp+14h] 121 | push dword ptr [ebp+10h] 122 | push dword ptr [ebp+0ch] 123 | mov eax, dword ptr [ebp+08h] 124 | add eax, 10101010h 125 | call eax 126 | ret 127 | _exe_EntryPoint ENDP 128 | 129 | _GETBASE PROC 130 | mov ecx, 11223346h 131 | call @next 132 | @next: 133 | pop eax 134 | sub eax, ecx 135 | and eax, 0fffff000h 136 | ret 137 | _GETBASE ENDP 138 | 139 | _CrtStartup PROC param1: DWORD 140 | mov eax, dword ptr [ebp+08h] 141 | mov esp, ebp 142 | pop ebp 143 | add eax, 10101010h 144 | jmp eax 145 | _CrtStartup ENDP 146 | 147 | end 148 | -------------------------------------------------------------------------------- /core-packer/peasm/pe_amd64.cpp: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * PE Assembly 3 | * i386 routine 4 | ***************************************************************************/ 5 | 6 | #include 7 | 8 | #include "peasm.h" 9 | #include "pesection.h" 10 | 11 | static BOOL load_image(CPeAssembly *pe, HANDLE hFile, SECTION_ARRAY *sections) 12 | { 13 | DWORD numberOfBytesRead; 14 | PIMAGE_DOS_HEADER dos_header = pe->DosHeader(); 15 | PIMAGE_NT_HEADERS64 nt_header = (PIMAGE_NT_HEADERS64) pe->NtHeader(); 16 | 17 | PIMAGE_SECTION_HEADER section_header = IMAGE_FIRST_SECTION(nt_header); 18 | short NumberOfSections = nt_header->FileHeader.NumberOfSections; 19 | 20 | for(unsigned short i = 0; i < NumberOfSections; i++, section_header++) 21 | { 22 | void *region = malloc(pe->round_section(section_header->Misc.VirtualSize)); 23 | 24 | SetFilePointer(hFile, section_header->PointerToRawData, NULL, FILE_BEGIN); 25 | ReadFile(hFile, region, section_header->SizeOfRawData, &numberOfBytesRead, NULL); 26 | CPeSection *section = new CPeSection(pe, section_header, section_header->VirtualAddress, section_header->Misc.VirtualSize, region); // duplicate this section! 27 | 28 | SECTION_ITEM dummy = { section_header->VirtualAddress, section }; 29 | sections->push_back(dummy); 30 | 31 | free(region); 32 | } 33 | 34 | return TRUE; 35 | } 36 | 37 | static BOOL write_image(CPeAssembly *pe, HANDLE hFile, SECTION_ARRAY *sections) 38 | { 39 | DWORD numberOfBytesWritten; 40 | PIMAGE_DOS_HEADER dos_header = pe->DosHeader(); 41 | PIMAGE_NT_HEADERS64 nt_header = (PIMAGE_NT_HEADERS64) pe->NtHeader(); 42 | 43 | if(hFile == INVALID_HANDLE_VALUE) 44 | return FALSE; 45 | 46 | if (dos_header->e_magic != IMAGE_DOS_SIGNATURE || dos_header->e_lfanew == 0) 47 | { 48 | return FALSE; 49 | } 50 | 51 | if( !WriteFile( hFile, dos_header, dos_header->e_lfanew, &numberOfBytesWritten, NULL)) 52 | return FALSE; 53 | 54 | // PE header 55 | 56 | if (nt_header->Signature != IMAGE_NT_SIGNATURE) 57 | { 58 | return FALSE; 59 | } 60 | 61 | DWORD dwSizeHeader = nt_header->FileHeader.SizeOfOptionalHeader + sizeof(nt_header->FileHeader) + 4;// + 62 | //sizeof(IMAGE_SECTION_HEADER)*pe_header->FileHeader.NumberOfSections; 63 | 64 | // sort items via iterator using "va" 65 | sections->sort(); 66 | 67 | SECTION_ITERATOR it = sections->begin(); 68 | 69 | // -- UPDATE HEADER BEFORE WRITING!!! 70 | //pe->update_section_header(); // transfer new code! 71 | 72 | /* TODO: adjust SizeOfImage and others... */ 73 | // ++ UPDATE HEADER BEFORE WRITING!!! 74 | 75 | // write PE header! 76 | WriteFile( hFile, nt_header, 0x400 - dos_header->e_lfanew, &numberOfBytesWritten,NULL); 77 | 78 | // write sections... 79 | it = sections->begin(); // writing section data! 80 | do 81 | { 82 | LPVOID rawData = it->descriptor->RawData(); 83 | DWORD size = it->descriptor->SizeOfRawData(); 84 | 85 | if (size > 0) 86 | { // write 87 | WriteFile(hFile, rawData, size, &numberOfBytesWritten, NULL); 88 | } 89 | 90 | it++; 91 | } while (it != sections->end()); 92 | 93 | return TRUE; 94 | } 95 | 96 | struct _file_support pe_amd64 = 97 | { 98 | IMAGE_FILE_MACHINE_AMD64, 99 | load_image, 100 | write_image 101 | }; 102 | -------------------------------------------------------------------------------- /core-packer/peasm/pe_i386.cpp: -------------------------------------------------------------------------------- 1 | /**************************************************************************** 2 | * PE Assembly 3 | * i386 routine 4 | ***************************************************************************/ 5 | 6 | #include 7 | #include "peasm.h" 8 | #include "pesection.h" 9 | 10 | static BOOL load_image(CPeAssembly *pe, HANDLE hFile, SECTION_ARRAY *sections) 11 | { 12 | DWORD numberOfBytesRead; 13 | PIMAGE_DOS_HEADER dos_header = pe->DosHeader(); 14 | PIMAGE_NT_HEADERS32 nt_header = pe->NtHeader(); 15 | 16 | PIMAGE_SECTION_HEADER section_header = IMAGE_FIRST_SECTION(nt_header); 17 | short NumberOfSections = nt_header->FileHeader.NumberOfSections; 18 | 19 | for(unsigned short i = 0; i < NumberOfSections; i++, section_header++) 20 | { 21 | void *region = malloc(pe->round_section(section_header->Misc.VirtualSize)); 22 | 23 | SetFilePointer(hFile, section_header->PointerToRawData, NULL, FILE_BEGIN); 24 | ReadFile(hFile, region, section_header->SizeOfRawData, &numberOfBytesRead, NULL); 25 | CPeSection *section = new CPeSection(pe, section_header, section_header->VirtualAddress, section_header->Misc.VirtualSize, region); // duplicate this section! 26 | 27 | SECTION_ITEM dummy = { section_header->VirtualAddress, section }; 28 | sections->push_back(dummy); 29 | 30 | free(region); 31 | } 32 | 33 | return TRUE; 34 | } 35 | 36 | static BOOL write_image(CPeAssembly *pe, HANDLE hFile, SECTION_ARRAY *sections) 37 | { 38 | DWORD numberOfBytesWritten; 39 | PIMAGE_DOS_HEADER dos_header = pe->DosHeader(); 40 | PIMAGE_NT_HEADERS32 nt_header = pe->NtHeader(); 41 | 42 | if (dos_header->e_magic != IMAGE_DOS_SIGNATURE || dos_header->e_lfanew == 0) 43 | { 44 | return FALSE; 45 | } 46 | 47 | if( !WriteFile( hFile, dos_header, dos_header->e_lfanew, &numberOfBytesWritten, NULL)) 48 | return FALSE; 49 | 50 | // PE header 51 | 52 | if (nt_header->Signature != IMAGE_NT_SIGNATURE) 53 | { 54 | return FALSE; 55 | } 56 | 57 | DWORD dwSizeHeader = nt_header->FileHeader.SizeOfOptionalHeader + sizeof(nt_header->FileHeader) + 4;// + 58 | //sizeof(IMAGE_SECTION_HEADER)*pe_header->FileHeader.NumberOfSections; 59 | 60 | //PIMAGE_SECTION_HEADER sections = (PIMAGE_SECTION_HEADER)((DWORD)(dos_header) + dwSizeHeader + dos_header->e_lfanew); 61 | 62 | // sort items via iterator using "va" 63 | sections->sort(); 64 | 65 | std::list::iterator it = sections->begin(); 66 | 67 | // -- UPDATE HEADER BEFORE WRITING!!! 68 | //update_section_header(); // transfer new code! 69 | /* TODO: adjust SizeOfImage and others... */ 70 | // ++ UPDATE HEADER BEFORE WRITING!!! 71 | 72 | // write PE header! 73 | WriteFile( hFile, nt_header, 0x400 - dos_header->e_lfanew, &numberOfBytesWritten,NULL); 74 | 75 | // write sections... 76 | it = sections->begin(); // writing section data! 77 | do 78 | { 79 | LPVOID rawData = it->descriptor->RawData(); 80 | DWORD size = it->descriptor->SizeOfRawData(); 81 | 82 | if (size > 0) 83 | { // write 84 | WriteFile(hFile, rawData, size, &numberOfBytesWritten, NULL); 85 | } 86 | 87 | it++; 88 | } while (it != sections->end()); 89 | 90 | return TRUE; 91 | } 92 | 93 | struct _file_support pe_i386 = 94 | { 95 | IMAGE_FILE_MACHINE_I386, 96 | load_image, 97 | write_image 98 | }; 99 | -------------------------------------------------------------------------------- /core-packer/peasm/peasm.h: -------------------------------------------------------------------------------- 1 | /** 2 | * PE Assembly Library 3 | **/ 4 | 5 | #ifndef __PEASM_H_ 6 | #define __PEASM_H_ 7 | 8 | #ifndef CALC_OFFSET 9 | #define CALC_OFFSET(type, ptr, offset) (type) (((ULONG64) ptr) + offset) 10 | #define CALC_OFFSET_DISP(type, base, offset, disp) (type)((DWORD)(base) + (DWORD)(offset) + disp) 11 | #define CALC_DISP(type, offset, ptr) (type) (((ULONG64) offset) - (ULONG64) ptr) 12 | #endif 13 | 14 | #include 15 | 16 | #include "types.h" 17 | 18 | class CPeSection; // PIMP! 19 | class CPeAssembly; 20 | 21 | typedef struct _section_item { 22 | virtualaddress_t va; 23 | CPeSection* descriptor; 24 | } SECTION_ITEM; 25 | 26 | bool operator < (SECTION_ITEM &first, SECTION_ITEM &second); 27 | bool operator == (const SECTION_ITEM &first, const SECTION_ITEM &second); 28 | 29 | typedef std::list::iterator SECTION_ITERATOR; 30 | typedef std::list SECTION_ARRAY; 31 | 32 | 33 | struct _file_support { 34 | DWORD IMAGE; 35 | BOOL (*read)(CPeAssembly *pe, HANDLE hFile, SECTION_ARRAY *sections); 36 | BOOL (*write)(CPeAssembly *pe, HANDLE hFile, SECTION_ARRAY *sections); 37 | }; 38 | 39 | /** 40 | * CPeAssembly 41 | * an editor for PE file (WIN32/WIN64) 42 | **/ 43 | class CPeAssembly 44 | { 45 | public: 46 | CPeAssembly(); 47 | ~CPeAssembly(); 48 | 49 | CPeAssembly(void *); 50 | /**************************************************************************** 51 | * Load/Save libraries! 52 | ***************************************************************************/ 53 | bool Load(char *pFileName); 54 | bool Save(char *pFileName); 55 | 56 | /**************************************************************************** 57 | * query on virtual address 58 | ***************************************************************************/ 59 | virtualaddress_t getBaseAddress(); // retrieve virtualaddress 60 | void setBaseAddress(virtualaddress_t newva); //set new virtualaddress 61 | 62 | virtualaddress_t getMinva(); // retrieve min. virtual address! 63 | virtualaddress_t getMaxva(); // retrieve max. virtual address! 64 | 65 | short NumberOfSections(); 66 | 67 | CPeSection* getSection(int index); 68 | CPeSection* LookupSectionByName(const char *szSectionName); 69 | 70 | bool RemoveSection(int index); 71 | bool RemoveSection(const char *szSectionName); 72 | 73 | CPeSection* AddSection(const char *szSectionName, virtualaddress_t newva, size_t size); 74 | CPeSection* AddSection(const char *szSectionName, virtualaddress_t newva, size_t size, size_t rawsize); 75 | CPeSection* MergeSection(CPeSection *sect0, CPeSection *sect1); 76 | 77 | void* RawPointer(virtualaddress_t va); 78 | 79 | /**************************************************************************** 80 | * I/O in virtual address (read/write bytes) 81 | ***************************************************************************/ 82 | bool ReadByte(virtualaddress_t va, uint8_t *out); 83 | bool ReadWord(virtualaddress_t va, uint16_t *out); 84 | bool ReadDword(virtualaddress_t va, uint32_t *out); 85 | bool ReadQWord(virtualaddress_t va, uint64_t *out); 86 | 87 | bool PatchByte(virtualaddress_t va, uint8_t *in); 88 | bool PatchWord(virtualaddress_t va, uint16_t *in); 89 | bool PatchDword(virtualaddress_t va, uint32_t *in); 90 | bool PatchQWord(virtualaddress_t va, uint64_t *in); 91 | 92 | 93 | /** 94 | * DataDirectory 95 | **/ 96 | inline PIMAGE_DATA_DIRECTORY DataDirectory() { return _lpNtHeader->OptionalHeader.DataDirectory; }; 97 | inline PIMAGE_DATA_DIRECTORY DataDirectory64() { return _lpNtHeader64->OptionalHeader.DataDirectory; }; 98 | 99 | 100 | inline bool IsDLL() { return (_lpNtHeader->FileHeader.Characteristics & IMAGE_FILE_DLL) ? true : false; }; 101 | inline bool IsEXE() { return (_lpNtHeader->FileHeader.Characteristics & IMAGE_FILE_EXECUTABLE_IMAGE) ? true : false; }; 102 | 103 | inline PIMAGE_NT_HEADERS64 NtHeader64() { return _lpNtHeader64; }; 104 | 105 | inline PIMAGE_NT_HEADERS32 NtHeader() { return _lpNtHeader; }; 106 | inline PIMAGE_DOS_HEADER DosHeader() { return _lpDosHeader; }; 107 | 108 | /////////////////////////////////////////////////////////////////////////////// 109 | // 110 | PIMAGE_EXPORT_DIRECTORY ExportDirectory(); 111 | PIMAGE_IMPORT_DESCRIPTOR ImportDirectory(); 112 | PIMAGE_RELOCATION RelocationDirectory(); 113 | 114 | PIMAGE_LOAD_CONFIG_DIRECTORY32 LoadConfig32Directory(); 115 | PIMAGE_LOAD_CONFIG_DIRECTORY64 LoadConfig64Directory(); 116 | 117 | 118 | public: // facility methods 119 | virtualaddress_t round_section(virtualaddress_t value); 120 | virtualaddress_t round_file(virtualaddress_t value); 121 | 122 | protected: 123 | void *rva2addr(virtualaddress_t address); 124 | 125 | PIMAGE_SECTION_HEADER LastSectionHeader(); 126 | PIMAGE_SECTION_HEADER GetSectionHeader(int index); 127 | 128 | virtualaddress_t nextva(); // retrieve next virtual address in "sections" 129 | virtualaddress_t nextrawdata(); 130 | 131 | private: 132 | void lock_datadir(); 133 | 134 | virtualaddress_t roundup(virtualaddress_t value, virtualaddress_t base); 135 | void update_section_header(); 136 | 137 | void update_datadirectory(virtualaddress_t newbaseaddress, virtualaddress_t baseaddress, size_t size); 138 | void update_importentries(virtualaddress_t newbaseaddress, virtualaddress_t baseaddress, size_t size); 139 | void update_delayimportentries(virtualaddress_t newbaseaddress, virtualaddress_t baseaddress, size_t size); 140 | void update_exportentries(virtualaddress_t newbaseaddress, virtualaddress_t baseaddress, size_t size); 141 | void update_relocentries(virtualaddress_t newbaseaddress, virtualaddress_t baseaddress, size_t size); 142 | void update_relocentries(virtualaddress_t newimagebase, virtualaddress_t imagebase); 143 | void update_rsrc(virtualaddress_t newbaseaddress, virtualaddress_t baseaddress, size_t size); 144 | void update_header(virtualaddress_t newbaseaddress, virtualaddress_t baseaddress, size_t size); 145 | 146 | std::list _sections; 147 | 148 | void* _lpBase; // base address of virtual memory! 149 | PIMAGE_DOS_HEADER _lpDosHeader; // alias of "lpBase" 150 | PIMAGE_NT_HEADERS32 _lpNtHeader; // alias of DOS->PE pointer (cast must be to PIMAGE_NT_HEADERS64) 151 | PIMAGE_NT_HEADERS64 _lpNtHeader64; // alias for x64 152 | 153 | /////////// 154 | // temporary! backup of "DATADIRECTORY" (to remove!) 155 | IMAGE_DATA_DIRECTORY _DATADIR[IMAGE_NUMBEROF_DIRECTORY_ENTRIES]; 156 | }; 157 | 158 | #endif 159 | -------------------------------------------------------------------------------- /core-packer/peasm/pesection.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "types.h" 3 | #include "peasm.h" 4 | #include "pesection.h" 5 | 6 | ///////////////////////////////////////////////////////////////////////////// 7 | // 8 | 9 | /** 10 | * !CPeSection 11 | * default <> invoked 12 | **/ 13 | CPeSection::CPeSection(CPeAssembly *parent, PIMAGE_SECTION_HEADER header, virtualaddress_t base, virtualaddress_t size) 14 | : _parent(parent), _base(base), _size(size), _rawData(NULL) 15 | { 16 | memcpy(&_header, header, sizeof(IMAGE_SECTION_HEADER)); 17 | } 18 | 19 | CPeSection::CPeSection(CPeAssembly *parent, PIMAGE_SECTION_HEADER header, virtualaddress_t base, virtualaddress_t size, void *rawData) 20 | : _parent(parent), _base(base), _size(size) 21 | { 22 | memcpy(&_header, header, sizeof(IMAGE_SECTION_HEADER)); 23 | _rawData = VirtualAlloc(0, size, MEM_COMMIT, PAGE_READWRITE); 24 | memcpy(_rawData, rawData, size); 25 | } 26 | 27 | 28 | CPeSection::~CPeSection() 29 | { 30 | if (_rawData != NULL) 31 | VirtualFree(_rawData, _size, MEM_RELEASE); 32 | } 33 | 34 | bool CPeSection::ReadByte(virtualaddress_t VirtualAddress, uint8_t *out) 35 | { 36 | memcpy(out, CALC_OFFSET(LPVOID, _rawData, VirtualAddress - _base), 1); 37 | return true; 38 | } 39 | 40 | bool CPeSection::ReadWord(virtualaddress_t VirtualAddress, uint16_t *out) 41 | { 42 | memcpy(out, CALC_OFFSET(LPVOID, _rawData, VirtualAddress - _base), 2); 43 | return true; 44 | } 45 | 46 | 47 | bool CPeSection::ReadDword(virtualaddress_t VirtualAddress, uint32_t *out) 48 | { 49 | memcpy(out, CALC_OFFSET(LPVOID, _rawData, VirtualAddress - _base), 4); 50 | return true; 51 | } 52 | 53 | bool CPeSection::ReadQWord(virtualaddress_t VirtualAddress, uint64_t *out) 54 | { 55 | memcpy(out, CALC_OFFSET(LPVOID, _rawData, VirtualAddress - _base), 8); 56 | return true; 57 | } 58 | 59 | 60 | bool CPeSection::PatchByte(virtualaddress_t VirtualAddress, uint8_t *in) 61 | { 62 | memcpy(CALC_OFFSET(LPVOID, _rawData, VirtualAddress - _base), in, 1); 63 | return true; 64 | } 65 | 66 | bool CPeSection::PatchWord(virtualaddress_t VirtualAddress, uint16_t *in) 67 | { 68 | memcpy(CALC_OFFSET(LPVOID, _rawData, VirtualAddress - _base), in, 2); 69 | return true; 70 | } 71 | 72 | bool CPeSection::PatchDword(virtualaddress_t VirtualAddress, uint32_t *in) 73 | { 74 | memcpy(CALC_OFFSET(LPVOID, _rawData, VirtualAddress - _base), in, 4); 75 | return true; 76 | } 77 | 78 | /** 79 | * \!PatchQword 80 | **/ 81 | bool CPeSection::PatchQWord(virtualaddress_t VirtualAddress, uint64_t *in) 82 | { 83 | memcpy(CALC_OFFSET(LPVOID, _rawData, VirtualAddress - _base), in, 8); 84 | return true; 85 | } 86 | 87 | 88 | 89 | void CPeSection::AddSize(size_t size) 90 | { 91 | if (size == 0) 92 | return; 93 | 94 | size_t newsize = _size + size; 95 | 96 | void *tmp = VirtualAlloc(NULL, newsize, MEM_COMMIT, PAGE_READWRITE); 97 | 98 | memset(tmp, 0x00, _parent->round_section(newsize)); 99 | memcpy(tmp, _rawData, _size); // ok data transfered! 100 | 101 | VirtualFree(_rawData, _size, MEM_RELEASE); 102 | 103 | _rawData = tmp; 104 | _size = newsize; 105 | _header.SizeOfRawData = _parent->round_file(newsize); 106 | _header.Misc.VirtualSize = _parent->round_section(newsize); 107 | } 108 | -------------------------------------------------------------------------------- /core-packer/peasm/pesection.h: -------------------------------------------------------------------------------- 1 | #ifndef __PESECTION_H_ 2 | #define __PESECTION_H_ 3 | 4 | class CPeAssembly; // PIMPL idiom 5 | 6 | /** 7 | * CPeSection 8 | * SECTION object 9 | **/ 10 | class CPeSection 11 | { 12 | public: 13 | CPeSection(CPeAssembly *parent, PIMAGE_SECTION_HEADER header, virtualaddress_t base, virtualaddress_t size); 14 | CPeSection(CPeAssembly *parent, PIMAGE_SECTION_HEADER header, virtualaddress_t base, virtualaddress_t size, void *rawdata); 15 | ~CPeSection(); 16 | 17 | /**************************************************************************** 18 | * I/O in virtual address (read/write bytes) 19 | ***************************************************************************/ 20 | bool ReadByte(virtualaddress_t VirtualAddress, uint8_t *out); 21 | bool ReadWord(virtualaddress_t VirtualAddress, uint16_t *out); 22 | bool ReadDword(virtualaddress_t VirtualAddress, uint32_t *out); 23 | bool ReadQWord(virtualaddress_t VirtualAddress, uint64_t *out); 24 | 25 | bool PatchByte(virtualaddress_t VirtualAddress, uint8_t *in); 26 | bool PatchWord(virtualaddress_t VirtualAddress, uint16_t *in); 27 | bool PatchDword(virtualaddress_t VirtualAddress, uint32_t *in); 28 | bool PatchQWord(virtualaddress_t VirtualAddress, uint64_t *in); 29 | 30 | inline virtualaddress_t VirtualAddress() { return _base; }; 31 | inline void SetNewVirtualAddress(virtualaddress_t va) { _base = va; _header.VirtualAddress = va; }; 32 | 33 | inline virtualaddress_t VirtualSize() { return _size; }; 34 | 35 | inline virtualaddress_t SizeOfRawData() { return _header.SizeOfRawData; }; 36 | 37 | inline virtualaddress_t PointerToRawData() { return _header.PointerToRawData; }; 38 | inline void SetPointerToRawData(virtualaddress_t value) { _header.PointerToRawData = value; }; 39 | 40 | inline void* RawData() { return _rawData; }; 41 | inline PIMAGE_SECTION_HEADER GetSectionHeader() { return &_header; }; 42 | 43 | void AddSize(size_t size); 44 | inline bool hide(bool value) { this->_hidesection = value; return this->_hidesection; }; 45 | inline bool hide() { return this->_hidesection; }; 46 | 47 | protected: 48 | inline bool isInSection(virtualaddress_t va) 49 | { 50 | if (va < _base || va > (_base + _size)) return false; 51 | return true; 52 | } 53 | 54 | private: 55 | CPeAssembly *_parent; 56 | IMAGE_SECTION_HEADER _header; 57 | virtualaddress_t _base; 58 | virtualaddress_t _size; 59 | 60 | bool _hidesection; 61 | void* _rawData; 62 | }; 63 | 64 | #endif 65 | -------------------------------------------------------------------------------- /core-packer/peasm/types.h: -------------------------------------------------------------------------------- 1 | #ifndef __TYPES_H_ 2 | #define __TYPES_H_ 3 | 4 | 5 | typedef unsigned char uint8_t; 6 | typedef unsigned short uint16_t; 7 | typedef unsigned int uint32_t; 8 | typedef unsigned __int64 uint64_t; 9 | 10 | typedef unsigned int virtualaddress_t; 11 | 12 | #ifndef _SIZE_T_DEFINED 13 | typedef unsigned int size_t; 14 | #endif 15 | 16 | #endif 17 | -------------------------------------------------------------------------------- /core-packer/rc4.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | //#pragma section(".hermit", read, execute) 4 | 5 | void swap(PBYTE a, PBYTE b) 6 | { 7 | BYTE tmp = *a; 8 | 9 | *a = *b; 10 | *b = tmp; 11 | } 12 | 13 | void init_sbox(LPBYTE RC4_SBOX) 14 | { 15 | for (int i = 0; i < 256; i++) 16 | RC4_SBOX[i] = i; 17 | } 18 | 19 | void init_sbox_key(LPBYTE RC4_SBOX, PBYTE key, int length) 20 | { 21 | int j = 0; 22 | 23 | for(int i = 0; i < 256; i++) 24 | { 25 | j = (j + RC4_SBOX[i] + key[i % length]) % 256; 26 | swap(&RC4_SBOX[i], &RC4_SBOX[j]); 27 | } 28 | } 29 | 30 | void cypher_msg(LPBYTE RC4_SBOX, PBYTE msg, int length) 31 | { 32 | int i=0, j=0; 33 | 34 | while(length > 0) 35 | { 36 | i = (i+1) % 256; 37 | j = (j+RC4_SBOX[i]) % 256; 38 | swap(&RC4_SBOX[i], &RC4_SBOX[j]); 39 | *msg++ ^= RC4_SBOX[(RC4_SBOX[i] + RC4_SBOX[j]) % 256]; 40 | length--; 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /core-packer/rc4.h: -------------------------------------------------------------------------------- 1 | #ifndef __RC4_H_ 2 | #define __RC4_H_ 3 | 4 | void cypher_msg(LPBYTE RC4_SBOX, PBYTE msg, int length); 5 | void init_sbox_key(LPBYTE RC4_SBOX, PBYTE key, int length); 6 | void init_sbox(LPBYTE RC4_SBOX); 7 | 8 | #endif 9 | -------------------------------------------------------------------------------- /core-packer/reloc.cpp: -------------------------------------------------------------------------------- 1 | //#include 2 | //#include "reloc.h" 3 | //#include "rva.h" 4 | //#include "macro.h" 5 | //#include "symbols.h" 6 | // 7 | // 8 | //#ifdef _BUILD32 9 | // #include "dll32.h" 10 | //#endif 11 | // 12 | //#pragma section(".hermit", read, execute) 13 | // 14 | //// Parse reloc table 15 | //#ifdef _BUILD64 16 | //#pragma code_seg(".hermit") 17 | //void Reloc_Process(LPVOID pModule, PIMAGE_NT_HEADERS64 pImageNtHeader, PIMAGE_SECTION_HEADER pSectionPointer, LPVOID lpRelocAddress, DWORD dwRelocSize) 18 | //{ 19 | // if (dwRelocSize == 0 || lpRelocAddress == NULL) 20 | // return; // no reloc table here! 21 | // 22 | // base_relocation_block_t *relocation_page = (base_relocation_block_t *) lpRelocAddress; 23 | // 24 | // if (relocation_page == NULL) 25 | // return; // no relocation page available! 26 | // 27 | // // for each page! 28 | // while(relocation_page->BlockSize > 0) 29 | // { 30 | // if (relocation_page->PageRVA < pSectionPointer->VirtualAddress || relocation_page->PageRVA > (pSectionPointer->VirtualAddress + pSectionPointer->Misc.VirtualSize)) 31 | // { // skip current page! 32 | // relocation_page = CALC_OFFSET(base_relocation_block_t *, relocation_page, relocation_page->BlockSize); 33 | // } 34 | // else 35 | // { // ok.. we can process this page! 36 | // typedef short relocation_entry; 37 | // 38 | // int BlockSize = relocation_page->BlockSize - 8; 39 | // relocation_entry *entries = CALC_OFFSET(relocation_entry *, relocation_page, 8); 40 | // 41 | // while(BlockSize > 0) 42 | // { 43 | // short type = ((*entries & 0xf000) >> 12); 44 | // long offset = (*entries & 0x0fff); 45 | // 46 | // ULONG64 *ptr = CALC_OFFSET(PULONG64, pModule, offset + relocation_page->PageRVA); 47 | // ULONG64 value = *ptr; 48 | // ULONG64 dwNewValue = 0; 49 | // 50 | // switch(type) 51 | // { 52 | // case IMAGE_REL_BASED_HIGHLOW: 53 | // value = value - pImageNtHeader->OptionalHeader.ImageBase; 54 | // value = value + (DWORD) pModule; 55 | // *ptr = value; 56 | // break; 57 | // case IMAGE_REL_BASED_DIR64: 58 | // dwNewValue = value - pImageNtHeader->OptionalHeader.ImageBase + (ULONG64) pModule; 59 | // *ptr = dwNewValue; 60 | // break; 61 | // } 62 | // 63 | // entries++; 64 | // BlockSize -= 2; 65 | // } 66 | // 67 | // relocation_page = CALC_OFFSET(base_relocation_block_t *, relocation_page, relocation_page->BlockSize); 68 | // } 69 | // } 70 | // 71 | //} 72 | //#endif 73 | // 74 | //#ifdef _BUILD32 75 | // 76 | //#pragma code_seg(".hermit") 77 | //BOOL reloc_is_text(PIMAGE_NT_HEADERS32 pImageNtHeader, PIMAGE_SECTION_HEADER pSectionText, DWORD offset) 78 | //{ 79 | // DWORD ImageBase = (DWORD) _baseAddress; 80 | // 81 | // DWORD minVirtualAddress = pSectionText->VirtualAddress; 82 | // DWORD maxVirtualAddress = pSectionText->VirtualAddress + pSectionText->Misc.VirtualSize; 83 | // 84 | // offset -= ImageBase; 85 | // 86 | // if (minVirtualAddress <= offset && offset < maxVirtualAddress) 87 | // return TRUE; 88 | // 89 | // return FALSE; 90 | //} 91 | // 92 | //#pragma code_seg(".hermit") 93 | //void reloctext(LPVOID pModule, PIMAGE_NT_HEADERS32 pImageNtHeader, PIMAGE_SECTION_HEADER pSectionPointer, LPVOID lpRelocAddress, DWORD dwRelocSize, LPVOID lpTextAddr) 94 | //{ 95 | // DWORD ImageBase = (DWORD) _baseAddress; 96 | // 97 | // base_relocation_block_t *relocation_page = (base_relocation_block_t *) lpRelocAddress; 98 | // 99 | // if (dwRelocSize == 0 || relocation_page == NULL) 100 | // return; // no reloc table here! 101 | // 102 | // // for each page! 103 | // while(relocation_page->BlockSize > 0) 104 | // { 105 | // if (relocation_page->PageRVA < pSectionPointer->VirtualAddress || relocation_page->PageRVA > (pSectionPointer->VirtualAddress + pSectionPointer->Misc.VirtualSize)) 106 | // { // skip current page! 107 | // relocation_page = CALC_OFFSET(base_relocation_block_t *, relocation_page, relocation_page->BlockSize); 108 | // } 109 | // else 110 | // { // ok.. we can process this page! 111 | // typedef short relocation_entry; 112 | // 113 | // int BlockSize = relocation_page->BlockSize - 8; 114 | // relocation_entry *entries = CALC_OFFSET(relocation_entry *, relocation_page, 8); 115 | // 116 | // while(BlockSize > 0) 117 | // { 118 | // short type = ((*entries & 0xf000) >> 12); 119 | // long offset = (*entries & 0x0fff); 120 | // 121 | // //ULONG *ptr = CALC_OFFSET(PULONG, pModule, offset + relocation_page->PageRVA); 122 | // ULONG *ptr = CALC_OFFSET(PULONG, lpTextAddr, offset + relocation_page->PageRVA - 0x1000); // base address of .text 123 | // ULONG value = *ptr; 124 | // ULONG dwNewValue = 0; 125 | // 126 | // if (reloc_is_text(pImageNtHeader, pSectionPointer, (DWORD) value) == FALSE) 127 | // { 128 | // switch(type) 129 | // { 130 | // case IMAGE_REL_BASED_HIGHLOW: 131 | // value = value - ImageBase; 132 | // value = value + (DWORD) pModule; 133 | // *ptr = value; 134 | // break; 135 | // case IMAGE_REL_BASED_DIR64: 136 | // dwNewValue = value - ImageBase + (ULONG) pModule; 137 | // *ptr = dwNewValue; 138 | // break; 139 | // default: 140 | // break; 141 | // } 142 | // } 143 | // else 144 | // { // applying different patch! 145 | // if (type == IMAGE_REL_BASED_HIGHLOW) 146 | // { 147 | // value = value - ImageBase - 0x1000; 148 | // value = value + (DWORD) lpTextAddr; 149 | // *ptr = value; 150 | // } 151 | // } 152 | // 153 | // entries++; 154 | // 155 | // BlockSize -= 2; 156 | // } 157 | // 158 | // relocation_page = CALC_OFFSET(base_relocation_block_t *, relocation_page, relocation_page->BlockSize); 159 | // } 160 | // } 161 | // 162 | //} 163 | // 164 | //#pragma code_seg(".hermit") 165 | //void Reloc_Process(LPVOID pModule, PIMAGE_NT_HEADERS32 pImageNtHeader, PIMAGE_SECTION_HEADER pSectionPointer, LPVOID lpRelocAddress, DWORD dwRelocSize, PIMAGE_SECTION_HEADER pTextPointer, LPVOID lpTextAddr) 166 | //{ 167 | // DWORD ImageBase = (DWORD) _baseAddress; 168 | // 169 | // if (dwRelocSize == 0 || lpRelocAddress == NULL) 170 | // return; // no reloc table here! 171 | // 172 | // base_relocation_block_t *relocation_page = (base_relocation_block_t *) lpRelocAddress; 173 | // 174 | // if (relocation_page == NULL) 175 | // return; // no relocation page available! 176 | // 177 | // // for each page! 178 | // while(relocation_page->BlockSize > 0) 179 | // { 180 | // if (relocation_page->PageRVA < pSectionPointer->VirtualAddress || relocation_page->PageRVA > (pSectionPointer->VirtualAddress + pSectionPointer->Misc.VirtualSize)) 181 | // { // skip current page! 182 | // relocation_page = CALC_OFFSET(base_relocation_block_t *, relocation_page, relocation_page->BlockSize); 183 | // } 184 | // else 185 | // { // ok.. we can process this page! 186 | // typedef short relocation_entry; 187 | // 188 | // int BlockSize = relocation_page->BlockSize - 8; 189 | // relocation_entry *entries = CALC_OFFSET(relocation_entry *, relocation_page, 8); 190 | // 191 | // while(BlockSize > 0) 192 | // { 193 | // short type = ((*entries & 0xf000) >> 12); 194 | // long offset = (*entries & 0x0fff); 195 | // 196 | // ULONG *ptr = CALC_OFFSET(PULONG, pModule, offset + relocation_page->PageRVA); 197 | // ULONG value = *ptr; 198 | // ULONG dwNewValue = 0; 199 | // 200 | // if (reloc_is_text(pImageNtHeader, pTextPointer, (DWORD) value) == FALSE) 201 | // { 202 | // switch(type) 203 | // { 204 | // case IMAGE_REL_BASED_HIGHLOW: 205 | // value = value - ImageBase; 206 | // value = value + (DWORD) pModule; 207 | // *ptr = value; 208 | // break; 209 | // case IMAGE_REL_BASED_DIR64: 210 | // dwNewValue = value - ImageBase + (ULONG) pModule; 211 | // *ptr = dwNewValue; 212 | // break; 213 | // default: 214 | // break; 215 | // } 216 | // } 217 | // else 218 | // { // applying different patch! 219 | // if (type == IMAGE_REL_BASED_HIGHLOW) 220 | // { 221 | // value = value - ImageBase - 0x1000; 222 | // value = value + (DWORD) lpTextAddr; 223 | // *ptr = value; 224 | // } 225 | // } 226 | // 227 | // 228 | // /*switch(type) 229 | // { 230 | // case IMAGE_REL_BASED_HIGHLOW: 231 | // value = value - pImageNtHeader->OptionalHeader.ImageBase; 232 | // value = value + (DWORD) pModule; 233 | // *ptr = value; 234 | // break; 235 | // case IMAGE_REL_BASED_DIR64: 236 | // dwNewValue = value - pImageNtHeader->OptionalHeader.ImageBase + (ULONG) pModule; 237 | // *ptr = dwNewValue; 238 | // break; 239 | // }*/ 240 | // entries++; 241 | // BlockSize -= 2; 242 | // } 243 | // 244 | // relocation_page = CALC_OFFSET(base_relocation_block_t *, relocation_page, relocation_page->BlockSize); 245 | // } 246 | // } 247 | // 248 | //} 249 | // 250 | //#endif 251 | -------------------------------------------------------------------------------- /core-packer/reloc.h: -------------------------------------------------------------------------------- 1 | #ifndef __RELOC_H_ 2 | #define __RELOC_H_ 3 | 4 | 5 | typedef struct base_relocation_block 6 | { 7 | DWORD PageRVA; 8 | DWORD BlockSize; 9 | } base_relocation_block_t; 10 | 11 | typedef struct base_relocation_entry 12 | { 13 | WORD offset : 12; 14 | WORD type : 4; 15 | } base_relocation_entry_t; 16 | #define relocation_block_t base_relocation_block_t 17 | #define relocation_entry_t base_relocation_entry_t 18 | 19 | typedef short relocation_entry; 20 | 21 | #ifndef _UNPACKERSECTION 22 | 23 | #ifdef _BUILD64 24 | void Reloc_Process(LPVOID pModule, PIMAGE_NT_HEADERS64 pImageNtHeader, PIMAGE_SECTION_HEADER pSectionPointer, LPVOID lpRelocAddress, DWORD dwRelocSize); 25 | #else 26 | //void Reloc_Process(LPVOID pModule, PIMAGE_NT_HEADERS32 pImageNtHeader, PIMAGE_SECTION_HEADER pSectionPointer, LPVOID lpRelocAddress, DWORD dwRelocSize); 27 | void Reloc_Process(LPVOID pModule, PIMAGE_NT_HEADERS32 pImageNtHeader, PIMAGE_SECTION_HEADER pSectionPointer, LPVOID lpRelocAddress, DWORD dwRelocSize, PIMAGE_SECTION_HEADER pTextPointer, LPVOID lpTextAddr); 28 | void reloctext(LPVOID pModule, PIMAGE_NT_HEADERS32 pImageNtHeader, PIMAGE_SECTION_HEADER pSectionPointer, LPVOID lpRelocAddress, DWORD dwRelocSize, LPVOID lpTextAddr); 29 | 30 | #endif 31 | 32 | #endif 33 | 34 | #endif 35 | 36 | -------------------------------------------------------------------------------- /core-packer/rva.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "rva.h" 3 | 4 | LPVOID rva2addr(PIMAGE_DOS_HEADER pImageDosHeader, PIMAGE_NT_HEADERS64 pImageNtHeaders64, LPVOID lpAddress) 5 | { 6 | ULONG64 dwImageDosHeader = (ULONG64) pImageDosHeader; // new base address! 7 | ULONG64 dwAddress = (ULONG64) lpAddress; // rva 8 | 9 | if (dwAddress > pImageNtHeaders64->OptionalHeader.ImageBase) 10 | dwAddress -= pImageNtHeaders64->OptionalHeader.ImageBase; 11 | 12 | dwAddress += dwImageDosHeader; 13 | 14 | return (LPVOID) dwAddress; 15 | } 16 | 17 | DWORD diff_rva64(PIMAGE_DOS_HEADER pImageDosHeader, PIMAGE_NT_HEADERS64 pImageNtHeaders64, DWORD lpAddress1, DWORD lpAddress2) 18 | { 19 | if (lpAddress1 > lpAddress2) 20 | { 21 | return lpAddress1 - lpAddress2; 22 | } 23 | else 24 | { 25 | DWORD x = (lpAddress2 - lpAddress1); 26 | x = ~x + 1; 27 | return x; 28 | } 29 | } 30 | 31 | LPVOID rva2addr(PIMAGE_DOS_HEADER pImageDosHeader, PIMAGE_NT_HEADERS32 pImageNtHeaders32, LPVOID lpAddress) 32 | { 33 | ULONG64 dwImageDosHeader = (ULONG) pImageDosHeader; // new base address! 34 | ULONG64 dwAddress = (ULONG) lpAddress; // rva 35 | 36 | if (dwAddress > pImageNtHeaders32->OptionalHeader.ImageBase) 37 | dwAddress -= pImageNtHeaders32->OptionalHeader.ImageBase; 38 | 39 | dwAddress += dwImageDosHeader; 40 | 41 | return (LPVOID) dwAddress; 42 | } 43 | 44 | DWORD diff_rva32(PIMAGE_DOS_HEADER pImageDosHeader, PIMAGE_NT_HEADERS32 pImageNtHeaders32, DWORD lpAddress1, DWORD lpAddress2) 45 | { 46 | if (lpAddress1 > lpAddress2) 47 | { 48 | return lpAddress1 - lpAddress2; 49 | } 50 | else 51 | { 52 | DWORD x = (lpAddress2 - lpAddress1); 53 | x = ~x + 1; 54 | return x; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /core-packer/rva.h: -------------------------------------------------------------------------------- 1 | #ifndef __RVA_H_ 2 | #define __RVA_H_ 3 | 4 | // rva2addr -> Transform a virtual address in "real address" 5 | LPVOID rva2addr(PIMAGE_DOS_HEADER pImageDosHeader, PIMAGE_NT_HEADERS64 pImageNtHeaders64, LPVOID lpAddress); 6 | DWORD diff_rva64(PIMAGE_DOS_HEADER pImageDosHeader, PIMAGE_NT_HEADERS64 pImageNtHeaders64, DWORD lpAddress1, DWORD lpAddress2); 7 | 8 | LPVOID rva2addr(PIMAGE_DOS_HEADER pImageDosHeader, PIMAGE_NT_HEADERS32 pImageNtHeaders32, LPVOID lpAddress); 9 | DWORD diff_rva32(PIMAGE_DOS_HEADER pImageDosHeader, PIMAGE_NT_HEADERS32 pImageNtHeaders32, DWORD lpAddress1, DWORD lpAddress2); 10 | 11 | #endif 12 | 13 | -------------------------------------------------------------------------------- /core-packer/symbols.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "symbols.h" 3 | 4 | //#pragma section(".hermit", read, write, execute) 5 | 6 | typedef struct _configuration 7 | { 8 | ULONG64 dwRelocSize; 9 | ULONG64 lpRelocAddress; 10 | ULONG64 _key0; 11 | ULONG64 _key1; 12 | ULONG64 _baseAddress; 13 | BYTE decrypted; 14 | LPVOID lpTextBaseAddr; 15 | } CONFIGURATION; 16 | 17 | //#pragma section(".hermit", read, write, execute) 18 | 19 | /*CONFIGURATION dll32_configuration = { 20 | 0xBABECAFEBAD00021, 21 | 0xBABECAFEBAD00020, 22 | 0xBABECAFEBAD00010, 23 | 0xBABECAFEBAD00011, 24 | 0xBABECAFEBAD00100, 25 | FALSE, 26 | NULL 27 | };*/ 28 | // 29 | //__declspec(allocate(".hermit")) 30 | //ULONG64 dwRelocSize = 0xBABECAFEBAD00021; 31 | // 32 | //__declspec(allocate(".hermit")) 33 | //ULONG64 lpRelocAddress = 0xBABECAFEBAD00020; 34 | // 35 | //__declspec(allocate(".hermit")) 36 | //ULONG64 _rc4key0 = 0xBABECAFEBAD00010; 37 | // 38 | //__declspec(allocate(".hermit")) 39 | //ULONG64 _rc4key1 = 0xBABECAFEBAD00011; 40 | 41 | 42 | //__declspec(allocate(".hermit")) 43 | //ULONG64 _baseAddress = 0xBABECAFEBAD00100; 44 | 45 | // Fixed symbols from loader 46 | //__declspec(allocate(".hermit")) 47 | //LoadLibraryA_ptr _LoadLibraryA = (LoadLibraryA_ptr) 0xBABECAFEBAD00004; 48 | 49 | //__declspec(allocate(".hermit")) 50 | //GetProcAddress_ptr _GetProcAddress = (GetProcAddress_ptr) 0xBABECAFEBAD00003; 51 | 52 | //__declspec(allocate(".hermit")) 53 | //HMODULE g_hKernel32 = NULL; 54 | 55 | //__declspec(allocate(".hermit")) 56 | //VirtualProtect_ptr _VirtualProtect; 57 | 58 | //__declspec(allocate(".hermit")) 59 | //VirtualAlloc_ptr _VirtualAlloc; 60 | 61 | extern "C" { 62 | 63 | //__declspec(allocate(".hermit")) 64 | //BYTE g_decrypted = FALSE; 65 | 66 | //__declspec(allocate(".hermit")) 67 | //LPVOID g_lpTextBaseAddr = (LPVOID) 0L; 68 | 69 | } 70 | -------------------------------------------------------------------------------- /core-packer/symbols.h: -------------------------------------------------------------------------------- 1 | #ifndef __SYMBOLS_H_ 2 | #define __SYMBOLS_H_ 3 | 4 | typedef HMODULE (WINAPI *LoadLibraryA_ptr)(LPCTSTR lpFileName); 5 | typedef FARPROC (WINAPI *GetProcAddress_ptr)(HMODULE hModule, LPCSTR lpProcName); 6 | 7 | typedef LPVOID (WINAPI *VirtualAlloc_ptr)(LPVOID lpAddress, SIZE_T dwSize, DWORD flAllocationType, DWORD flProtect); 8 | typedef BOOL (WINAPI *VirtualProtect_ptr)(LPVOID lpAddress, SIZE_T dwSize, DWORD flNewProtect, PDWORD lpflOldProtect); 9 | 10 | ///////////////////////////////////////////////////////////////////// 11 | // Critical section pointers 12 | typedef void (WINAPI *InitializeCriticalSection_ptr)(LPCRITICAL_SECTION lpCriticalSection); 13 | typedef void (WINAPI *EnterCriticalSection_ptr)(LPCRITICAL_SECTION lpCriticalSection); 14 | typedef void (WINAPI *LeaveCriticalSection_ptr)(LPCRITICAL_SECTION lpCriticalSection); 15 | typedef void (WINAPI *DeleteCriticalSection_ptr)(LPCRITICAL_SECTION lpCriticalSection); 16 | 17 | typedef BOOL (WINAPI *DllMain_ptr)(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved); 18 | 19 | typedef HMODULE (WINAPI *GetModuleHandleA_ptr)(LPCTSTR lpModuleName); 20 | 21 | typedef HANDLE (WINAPI *CreateFileA_ptr)(LPCTSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile); 22 | 23 | typedef DWORD (WINAPI *SetFilePointer_ptr)(HANDLE hFile, LONG lDistanceToMove, PLONG lpDistanceToMoveHigh, DWORD dwMoveMethod); 24 | 25 | #ifdef _BUILD32 26 | HMODULE WINAPI _dll32_LoadLibraryA(LPCTSTR lpFileName); // C porting 27 | LPVOID WINAPI _dll32_GetProcAddress(HMODULE hModule, LPCSTR lpProcName); 28 | HANDLE WINAPI _CreateFileA(LPCTSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile); 29 | DWORD WINAPI _GetModuleFileNameA(HMODULE hModule, LPTSTR lpFilename, DWORD nSize); 30 | DWORD WINAPI _SetFilePointer(HANDLE hFile, LONG lDistanceToMove, PLONG lpDistanceToMoveHigh, DWORD dwMoveMethod); 31 | BOOL WINAPI _ReadFile(HANDLE hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead, LPDWORD lpNumberOfBytesRead, LPOVERLAPPED lpOverlapped); 32 | BOOL WINAPI _CloseHandle(HANDLE hObject); 33 | BOOL WINAPI _EntryPoint(LPVOID lpBase, HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved); 34 | 35 | extern "C" 36 | { 37 | HMODULE WINAPI _exe_LoadLibraryA(LPCTSTR lpFileName); 38 | 39 | FARPROC WINAPI _exe_GetProcAddress(HMODULE hModule, LPCSTR lpProcName); 40 | 41 | 42 | HANDLE WINAPI _exe_CreateFileA(LPCTSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile); 43 | 44 | DWORD WINAPI _exe_GetModuleFileNameA(HMODULE hModule, LPTSTR lpFilename, DWORD nSize); 45 | 46 | DWORD WINAPI _exe_SetFilePointer(HANDLE hFile, LONG lDistanceToMove, PLONG lpDistanceToMoveHigh, DWORD dwMoveMethod); 47 | 48 | BOOL WINAPI _exe_ReadFile(HANDLE hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead, LPDWORD lpNumberOfBytesRead, LPOVERLAPPED lpOverlapped); 49 | 50 | BOOL WINAPI _exe_CloseHandle(HANDLE hObject); 51 | 52 | BOOL WINAPI _exe_EntryPoint(LPVOID lpBase, HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved); 53 | 54 | int WINAPI _CrtStartup(LPVOID lpBase); // NO EXIT! 55 | LPVOID WINAPI _GETBASE(); 56 | } 57 | 58 | extern "C" HMODULE exe_g_hKernel32; 59 | 60 | #else 61 | extern "C" HMODULE WINAPI _LoadLibraryA(LPCTSTR lpFileName); 62 | extern "C" FARPROC WINAPI _GetProcAddress(HMODULE hModule, LPCSTR lpProcName); 63 | //extern "C" BOOL WINAPI _EntryPoint(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved); 64 | //extern "C" HMODULE g_hKernel32; 65 | #endif 66 | 67 | extern VirtualProtect_ptr _VirtualProtect; 68 | extern VirtualAlloc_ptr _VirtualAlloc; 69 | 70 | extern VirtualProtect_ptr exe_VirtualProtect; 71 | extern VirtualAlloc_ptr exe_VirtualAlloc; 72 | 73 | #endif 74 | -------------------------------------------------------------------------------- /core-packer/tclap/ArgException.h: -------------------------------------------------------------------------------- 1 | // -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*- 2 | 3 | /****************************************************************************** 4 | * 5 | * file: ArgException.h 6 | * 7 | * Copyright (c) 2003, Michael E. Smoot . 8 | * All rights reverved. 9 | * 10 | * See the file COPYING in the top directory of this distribution for 11 | * more information. 12 | * 13 | * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS 14 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 16 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 19 | * DEALINGS IN THE SOFTWARE. 20 | * 21 | *****************************************************************************/ 22 | 23 | 24 | #ifndef TCLAP_ARG_EXCEPTION_H 25 | #define TCLAP_ARG_EXCEPTION_H 26 | 27 | #include 28 | #include 29 | 30 | namespace TCLAP { 31 | 32 | /** 33 | * A simple class that defines and argument exception. Should be caught 34 | * whenever a CmdLine is created and parsed. 35 | */ 36 | class ArgException : public std::exception 37 | { 38 | public: 39 | 40 | /** 41 | * Constructor. 42 | * \param text - The text of the exception. 43 | * \param id - The text identifying the argument source. 44 | * \param td - Text describing the type of ArgException it is. 45 | * of the exception. 46 | */ 47 | ArgException( const std::string& text = "undefined exception", 48 | const std::string& id = "undefined", 49 | const std::string& td = "Generic ArgException") 50 | : std::exception(), 51 | _errorText(text), 52 | _argId( id ), 53 | _typeDescription(td) 54 | { } 55 | 56 | /** 57 | * Destructor. 58 | */ 59 | virtual ~ArgException() throw() { } 60 | 61 | /** 62 | * Returns the error text. 63 | */ 64 | std::string error() const { return ( _errorText ); } 65 | 66 | /** 67 | * Returns the argument id. 68 | */ 69 | std::string argId() const 70 | { 71 | if ( _argId == "undefined" ) 72 | return " "; 73 | else 74 | return ( "Argument: " + _argId ); 75 | } 76 | 77 | /** 78 | * Returns the arg id and error text. 79 | */ 80 | const char* what() const throw() 81 | { 82 | static std::string ex; 83 | ex = _argId + " -- " + _errorText; 84 | return ex.c_str(); 85 | } 86 | 87 | /** 88 | * Returns the type of the exception. Used to explain and distinguish 89 | * between different child exceptions. 90 | */ 91 | std::string typeDescription() const 92 | { 93 | return _typeDescription; 94 | } 95 | 96 | 97 | private: 98 | 99 | /** 100 | * The text of the exception message. 101 | */ 102 | std::string _errorText; 103 | 104 | /** 105 | * The argument related to this exception. 106 | */ 107 | std::string _argId; 108 | 109 | /** 110 | * Describes the type of the exception. Used to distinguish 111 | * between different child exceptions. 112 | */ 113 | std::string _typeDescription; 114 | 115 | }; 116 | 117 | /** 118 | * Thrown from within the child Arg classes when it fails to properly 119 | * parse the argument it has been passed. 120 | */ 121 | class ArgParseException : public ArgException 122 | { 123 | public: 124 | /** 125 | * Constructor. 126 | * \param text - The text of the exception. 127 | * \param id - The text identifying the argument source 128 | * of the exception. 129 | */ 130 | ArgParseException( const std::string& text = "undefined exception", 131 | const std::string& id = "undefined" ) 132 | : ArgException( text, 133 | id, 134 | std::string( "Exception found while parsing " ) + 135 | std::string( "the value the Arg has been passed." )) 136 | { } 137 | }; 138 | 139 | /** 140 | * Thrown from CmdLine when the arguments on the command line are not 141 | * properly specified, e.g. too many arguments, required argument missing, etc. 142 | */ 143 | class CmdLineParseException : public ArgException 144 | { 145 | public: 146 | /** 147 | * Constructor. 148 | * \param text - The text of the exception. 149 | * \param id - The text identifying the argument source 150 | * of the exception. 151 | */ 152 | CmdLineParseException( const std::string& text = "undefined exception", 153 | const std::string& id = "undefined" ) 154 | : ArgException( text, 155 | id, 156 | std::string( "Exception found when the values ") + 157 | std::string( "on the command line do not meet ") + 158 | std::string( "the requirements of the defined ") + 159 | std::string( "Args." )) 160 | { } 161 | }; 162 | 163 | /** 164 | * Thrown from Arg and CmdLine when an Arg is improperly specified, e.g. 165 | * same flag as another Arg, same name, etc. 166 | */ 167 | class SpecificationException : public ArgException 168 | { 169 | public: 170 | /** 171 | * Constructor. 172 | * \param text - The text of the exception. 173 | * \param id - The text identifying the argument source 174 | * of the exception. 175 | */ 176 | SpecificationException( const std::string& text = "undefined exception", 177 | const std::string& id = "undefined" ) 178 | : ArgException( text, 179 | id, 180 | std::string("Exception found when an Arg object ")+ 181 | std::string("is improperly defined by the ") + 182 | std::string("developer." )) 183 | { } 184 | 185 | }; 186 | 187 | class ExitException { 188 | public: 189 | ExitException(int estat) : _estat(estat) {} 190 | 191 | int getExitStatus() const { return _estat; } 192 | 193 | private: 194 | int _estat; 195 | }; 196 | 197 | } // namespace TCLAP 198 | 199 | #endif 200 | 201 | -------------------------------------------------------------------------------- /core-packer/tclap/ArgTraits.h: -------------------------------------------------------------------------------- 1 | // -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*- 2 | 3 | /****************************************************************************** 4 | * 5 | * file: ArgTraits.h 6 | * 7 | * Copyright (c) 2007, Daniel Aarno, Michael E. Smoot . 8 | * All rights reverved. 9 | * 10 | * See the file COPYING in the top directory of this distribution for 11 | * more information. 12 | * 13 | * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS 14 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 16 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 19 | * DEALINGS IN THE SOFTWARE. 20 | * 21 | *****************************************************************************/ 22 | 23 | // This is an internal tclap file, you should probably not have to 24 | // include this directly 25 | 26 | #ifndef TCLAP_ARGTRAITS_H 27 | #define TCLAP_ARGTRAITS_H 28 | 29 | namespace TCLAP { 30 | 31 | // We use two empty structs to get compile type specialization 32 | // function to work 33 | 34 | /** 35 | * A value like argument value type is a value that can be set using 36 | * operator>>. This is the default value type. 37 | */ 38 | struct ValueLike { 39 | typedef ValueLike ValueCategory; 40 | virtual ~ValueLike() {} 41 | }; 42 | 43 | /** 44 | * A string like argument value type is a value that can be set using 45 | * operator=(string). Usefull if the value type contains spaces which 46 | * will be broken up into individual tokens by operator>>. 47 | */ 48 | struct StringLike { 49 | virtual ~StringLike() {} 50 | }; 51 | 52 | /** 53 | * A class can inherit from this object to make it have string like 54 | * traits. This is a compile time thing and does not add any overhead 55 | * to the inherenting class. 56 | */ 57 | struct StringLikeTrait { 58 | typedef StringLike ValueCategory; 59 | virtual ~StringLikeTrait() {} 60 | }; 61 | 62 | /** 63 | * A class can inherit from this object to make it have value like 64 | * traits. This is a compile time thing and does not add any overhead 65 | * to the inherenting class. 66 | */ 67 | struct ValueLikeTrait { 68 | typedef ValueLike ValueCategory; 69 | virtual ~ValueLikeTrait() {} 70 | }; 71 | 72 | /** 73 | * Arg traits are used to get compile type specialization when parsing 74 | * argument values. Using an ArgTraits you can specify the way that 75 | * values gets assigned to any particular type during parsing. The two 76 | * supported types are StringLike and ValueLike. 77 | */ 78 | template 79 | struct ArgTraits { 80 | typedef typename T::ValueCategory ValueCategory; 81 | virtual ~ArgTraits() {} 82 | //typedef ValueLike ValueCategory; 83 | }; 84 | 85 | #endif 86 | 87 | } // namespace 88 | -------------------------------------------------------------------------------- /core-packer/tclap/CmdLineInterface.h: -------------------------------------------------------------------------------- 1 | 2 | /****************************************************************************** 3 | * 4 | * file: CmdLineInterface.h 5 | * 6 | * Copyright (c) 2003, Michael E. Smoot . 7 | * Copyright (c) 2004, Michael E. Smoot, Daniel Aarno. 8 | * All rights reverved. 9 | * 10 | * See the file COPYING in the top directory of this distribution for 11 | * more information. 12 | * 13 | * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS 14 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 16 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 19 | * DEALINGS IN THE SOFTWARE. 20 | * 21 | *****************************************************************************/ 22 | 23 | #ifndef TCLAP_COMMANDLINE_INTERFACE_H 24 | #define TCLAP_COMMANDLINE_INTERFACE_H 25 | 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | 33 | namespace TCLAP { 34 | 35 | class Arg; 36 | class CmdLineOutput; 37 | class XorHandler; 38 | 39 | /** 40 | * The base class that manages the command line definition and passes 41 | * along the parsing to the appropriate Arg classes. 42 | */ 43 | class CmdLineInterface 44 | { 45 | public: 46 | 47 | /** 48 | * Destructor 49 | */ 50 | virtual ~CmdLineInterface() {} 51 | 52 | /** 53 | * Adds an argument to the list of arguments to be parsed. 54 | * \param a - Argument to be added. 55 | */ 56 | virtual void add( Arg& a )=0; 57 | 58 | /** 59 | * An alternative add. Functionally identical. 60 | * \param a - Argument to be added. 61 | */ 62 | virtual void add( Arg* a )=0; 63 | 64 | /** 65 | * Add two Args that will be xor'd. 66 | * If this method is used, add does 67 | * not need to be called. 68 | * \param a - Argument to be added and xor'd. 69 | * \param b - Argument to be added and xor'd. 70 | */ 71 | virtual void xorAdd( Arg& a, Arg& b )=0; 72 | 73 | /** 74 | * Add a list of Args that will be xor'd. If this method is used, 75 | * add does not need to be called. 76 | * \param xors - List of Args to be added and xor'd. 77 | */ 78 | virtual void xorAdd( std::vector& xors )=0; 79 | 80 | /** 81 | * Parses the command line. 82 | * \param argc - Number of arguments. 83 | * \param argv - Array of arguments. 84 | */ 85 | virtual void parse(int argc, const char * const * argv)=0; 86 | 87 | /** 88 | * Parses the command line. 89 | * \param args - A vector of strings representing the args. 90 | * args[0] is still the program name. 91 | */ 92 | void parse(std::vector& args); 93 | 94 | /** 95 | * Returns the CmdLineOutput object. 96 | */ 97 | virtual CmdLineOutput* getOutput()=0; 98 | 99 | /** 100 | * \param co - CmdLineOutput object that we want to use instead. 101 | */ 102 | virtual void setOutput(CmdLineOutput* co)=0; 103 | 104 | /** 105 | * Returns the version string. 106 | */ 107 | virtual std::string& getVersion()=0; 108 | 109 | /** 110 | * Returns the program name string. 111 | */ 112 | virtual std::string& getProgramName()=0; 113 | 114 | /** 115 | * Returns the argList. 116 | */ 117 | virtual std::list& getArgList()=0; 118 | 119 | /** 120 | * Returns the XorHandler. 121 | */ 122 | virtual XorHandler& getXorHandler()=0; 123 | 124 | /** 125 | * Returns the delimiter string. 126 | */ 127 | virtual char getDelimiter()=0; 128 | 129 | /** 130 | * Returns the message string. 131 | */ 132 | virtual std::string& getMessage()=0; 133 | 134 | /** 135 | * Indicates whether or not the help and version switches were created 136 | * automatically. 137 | */ 138 | virtual bool hasHelpAndVersion()=0; 139 | 140 | /** 141 | * Resets the instance as if it had just been constructed so that the 142 | * instance can be reused. 143 | */ 144 | virtual void reset()=0; 145 | }; 146 | 147 | } //namespace 148 | 149 | 150 | #endif 151 | -------------------------------------------------------------------------------- /core-packer/tclap/CmdLineOutput.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | /****************************************************************************** 4 | * 5 | * file: CmdLineOutput.h 6 | * 7 | * Copyright (c) 2004, Michael E. Smoot 8 | * All rights reverved. 9 | * 10 | * See the file COPYING in the top directory of this distribution for 11 | * more information. 12 | * 13 | * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS 14 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 16 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 19 | * DEALINGS IN THE SOFTWARE. 20 | * 21 | *****************************************************************************/ 22 | 23 | #ifndef TCLAP_CMDLINEOUTPUT_H 24 | #define TCLAP_CMDLINEOUTPUT_H 25 | 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | 33 | namespace TCLAP { 34 | 35 | class CmdLineInterface; 36 | class ArgException; 37 | 38 | /** 39 | * The interface that any output object must implement. 40 | */ 41 | class CmdLineOutput 42 | { 43 | 44 | public: 45 | 46 | /** 47 | * Virtual destructor. 48 | */ 49 | virtual ~CmdLineOutput() {} 50 | 51 | /** 52 | * Generates some sort of output for the USAGE. 53 | * \param c - The CmdLine object the output is generated for. 54 | */ 55 | virtual void usage(CmdLineInterface& c)=0; 56 | 57 | /** 58 | * Generates some sort of output for the version. 59 | * \param c - The CmdLine object the output is generated for. 60 | */ 61 | virtual void version(CmdLineInterface& c)=0; 62 | 63 | /** 64 | * Generates some sort of output for a failure. 65 | * \param c - The CmdLine object the output is generated for. 66 | * \param e - The ArgException that caused the failure. 67 | */ 68 | virtual void failure( CmdLineInterface& c, 69 | ArgException& e )=0; 70 | 71 | }; 72 | 73 | } //namespace TCLAP 74 | #endif 75 | -------------------------------------------------------------------------------- /core-packer/tclap/Constraint.h: -------------------------------------------------------------------------------- 1 | 2 | /****************************************************************************** 3 | * 4 | * file: Constraint.h 5 | * 6 | * Copyright (c) 2005, Michael E. Smoot 7 | * All rights reverved. 8 | * 9 | * See the file COPYING in the top directory of this distribution for 10 | * more information. 11 | * 12 | * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS 13 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 14 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 15 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 16 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 17 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 18 | * DEALINGS IN THE SOFTWARE. 19 | * 20 | *****************************************************************************/ 21 | 22 | #ifndef TCLAP_CONSTRAINT_H 23 | #define TCLAP_CONSTRAINT_H 24 | 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | namespace TCLAP { 33 | 34 | /** 35 | * The interface that defines the interaction between the Arg and Constraint. 36 | */ 37 | template 38 | class Constraint 39 | { 40 | 41 | public: 42 | /** 43 | * Returns a description of the Constraint. 44 | */ 45 | virtual std::string description() const =0; 46 | 47 | /** 48 | * Returns the short ID for the Constraint. 49 | */ 50 | virtual std::string shortID() const =0; 51 | 52 | /** 53 | * The method used to verify that the value parsed from the command 54 | * line meets the constraint. 55 | * \param value - The value that will be checked. 56 | */ 57 | virtual bool check(const T& value) const =0; 58 | 59 | /** 60 | * Destructor. 61 | * Silences warnings about Constraint being a base class with virtual 62 | * functions but without a virtual destructor. 63 | */ 64 | virtual ~Constraint() { ; } 65 | }; 66 | 67 | } //namespace TCLAP 68 | #endif 69 | -------------------------------------------------------------------------------- /core-packer/tclap/DocBookOutput.h: -------------------------------------------------------------------------------- 1 | // -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*- 2 | 3 | /****************************************************************************** 4 | * 5 | * file: DocBookOutput.h 6 | * 7 | * Copyright (c) 2004, Michael E. Smoot 8 | * All rights reverved. 9 | * 10 | * See the file COPYING in the top directory of this distribution for 11 | * more information. 12 | * 13 | * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS 14 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 16 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 19 | * DEALINGS IN THE SOFTWARE. 20 | * 21 | *****************************************************************************/ 22 | 23 | #ifndef TCLAP_DOCBOOKOUTPUT_H 24 | #define TCLAP_DOCBOOKOUTPUT_H 25 | 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | #include 33 | #include 34 | #include 35 | #include 36 | 37 | namespace TCLAP { 38 | 39 | /** 40 | * A class that generates DocBook output for usage() method for the 41 | * given CmdLine and its Args. 42 | */ 43 | class DocBookOutput : public CmdLineOutput 44 | { 45 | 46 | public: 47 | 48 | /** 49 | * Prints the usage to stdout. Can be overridden to 50 | * produce alternative behavior. 51 | * \param c - The CmdLine object the output is generated for. 52 | */ 53 | virtual void usage(CmdLineInterface& c); 54 | 55 | /** 56 | * Prints the version to stdout. Can be overridden 57 | * to produce alternative behavior. 58 | * \param c - The CmdLine object the output is generated for. 59 | */ 60 | virtual void version(CmdLineInterface& c); 61 | 62 | /** 63 | * Prints (to stderr) an error message, short usage 64 | * Can be overridden to produce alternative behavior. 65 | * \param c - The CmdLine object the output is generated for. 66 | * \param e - The ArgException that caused the failure. 67 | */ 68 | virtual void failure(CmdLineInterface& c, 69 | ArgException& e ); 70 | 71 | protected: 72 | 73 | /** 74 | * Substitutes the char r for string x in string s. 75 | * \param s - The string to operate on. 76 | * \param r - The char to replace. 77 | * \param x - What to replace r with. 78 | */ 79 | void substituteSpecialChars( std::string& s, char r, std::string& x ); 80 | void removeChar( std::string& s, char r); 81 | void basename( std::string& s ); 82 | 83 | void printShortArg(Arg* it); 84 | void printLongArg(Arg* it); 85 | 86 | char theDelimiter; 87 | }; 88 | 89 | 90 | inline void DocBookOutput::version(CmdLineInterface& _cmd) 91 | { 92 | std::cout << _cmd.getVersion() << std::endl; 93 | } 94 | 95 | inline void DocBookOutput::usage(CmdLineInterface& _cmd ) 96 | { 97 | std::list argList = _cmd.getArgList(); 98 | std::string progName = _cmd.getProgramName(); 99 | std::string xversion = _cmd.getVersion(); 100 | theDelimiter = _cmd.getDelimiter(); 101 | XorHandler xorHandler = _cmd.getXorHandler(); 102 | std::vector< std::vector > xorList = xorHandler.getXorList(); 103 | basename(progName); 104 | 105 | std::cout << "" << std::endl; 106 | std::cout << "" << std::endl << std::endl; 108 | 109 | std::cout << "" << std::endl; 110 | 111 | std::cout << "" << std::endl; 112 | std::cout << "" << progName << "" << std::endl; 113 | std::cout << "1" << std::endl; 114 | std::cout << "" << std::endl; 115 | 116 | std::cout << "" << std::endl; 117 | std::cout << "" << progName << "" << std::endl; 118 | std::cout << "" << _cmd.getMessage() << "" << std::endl; 119 | std::cout << "" << std::endl; 120 | 121 | std::cout << "" << std::endl; 122 | std::cout << "" << std::endl; 123 | 124 | std::cout << "" << progName << "" << std::endl; 125 | 126 | // xor 127 | for ( int i = 0; (unsigned int)i < xorList.size(); i++ ) 128 | { 129 | std::cout << "" << std::endl; 130 | for ( ArgVectorIterator it = xorList[i].begin(); 131 | it != xorList[i].end(); it++ ) 132 | printShortArg((*it)); 133 | 134 | std::cout << "" << std::endl; 135 | } 136 | 137 | // rest of args 138 | for (ArgListIterator it = argList.begin(); it != argList.end(); it++) 139 | if ( !xorHandler.contains( (*it) ) ) 140 | printShortArg((*it)); 141 | 142 | std::cout << "" << std::endl; 143 | std::cout << "" << std::endl; 144 | 145 | std::cout << "" << std::endl; 146 | std::cout << "Description" << std::endl; 147 | std::cout << "" << std::endl; 148 | std::cout << _cmd.getMessage() << std::endl; 149 | std::cout << "" << std::endl; 150 | std::cout << "" << std::endl; 151 | 152 | std::cout << "" << std::endl; 153 | std::cout << "Options" << std::endl; 154 | 155 | std::cout << "" << std::endl; 156 | 157 | for (ArgListIterator it = argList.begin(); it != argList.end(); it++) 158 | printLongArg((*it)); 159 | 160 | std::cout << "" << std::endl; 161 | std::cout << "" << std::endl; 162 | 163 | std::cout << "" << std::endl; 164 | std::cout << "Version" << std::endl; 165 | std::cout << "" << std::endl; 166 | std::cout << xversion << std::endl; 167 | std::cout << "" << std::endl; 168 | std::cout << "" << std::endl; 169 | 170 | std::cout << "" << std::endl; 171 | 172 | } 173 | 174 | inline void DocBookOutput::failure( CmdLineInterface& _cmd, 175 | ArgException& e ) 176 | { 177 | static_cast(_cmd); // unused 178 | std::cout << e.what() << std::endl; 179 | throw ExitException(1); 180 | } 181 | 182 | inline void DocBookOutput::substituteSpecialChars( std::string& s, 183 | char r, 184 | std::string& x ) 185 | { 186 | size_t p; 187 | while ( (p = s.find_first_of(r)) != std::string::npos ) 188 | { 189 | s.erase(p,1); 190 | s.insert(p,x); 191 | } 192 | } 193 | 194 | inline void DocBookOutput::removeChar( std::string& s, char r) 195 | { 196 | size_t p; 197 | while ( (p = s.find_first_of(r)) != std::string::npos ) 198 | { 199 | s.erase(p,1); 200 | } 201 | } 202 | 203 | inline void DocBookOutput::basename( std::string& s ) 204 | { 205 | size_t p = s.find_last_of('/'); 206 | if ( p != std::string::npos ) 207 | { 208 | s.erase(0, p + 1); 209 | } 210 | } 211 | 212 | inline void DocBookOutput::printShortArg(Arg* a) 213 | { 214 | std::string lt = "<"; 215 | std::string gt = ">"; 216 | 217 | std::string id = a->shortID(); 218 | substituteSpecialChars(id,'<',lt); 219 | substituteSpecialChars(id,'>',gt); 220 | removeChar(id,'['); 221 | removeChar(id,']'); 222 | 223 | std::string choice = "opt"; 224 | if ( a->isRequired() ) 225 | choice = "plain"; 226 | 227 | std::cout << "acceptsMultipleValues() ) 229 | std::cout << " rep='repeat'"; 230 | 231 | 232 | std::cout << '>'; 233 | if ( !a->getFlag().empty() ) 234 | std::cout << a->flagStartChar() << a->getFlag(); 235 | else 236 | std::cout << a->nameStartString() << a->getName(); 237 | if ( a->isValueRequired() ) 238 | { 239 | std::string arg = a->shortID(); 240 | removeChar(arg,'['); 241 | removeChar(arg,']'); 242 | removeChar(arg,'<'); 243 | removeChar(arg,'>'); 244 | arg.erase(0, arg.find_last_of(theDelimiter) + 1); 245 | std::cout << theDelimiter; 246 | std::cout << "" << arg << ""; 247 | } 248 | std::cout << "" << std::endl; 249 | 250 | } 251 | 252 | inline void DocBookOutput::printLongArg(Arg* a) 253 | { 254 | std::string lt = "<"; 255 | std::string gt = ">"; 256 | 257 | std::string desc = a->getDescription(); 258 | substituteSpecialChars(desc,'<',lt); 259 | substituteSpecialChars(desc,'>',gt); 260 | 261 | std::cout << "" << std::endl; 262 | 263 | if ( !a->getFlag().empty() ) 264 | { 265 | std::cout << "" << std::endl; 266 | std::cout << "" << std::endl; 269 | std::cout << "" << std::endl; 270 | } 271 | 272 | std::cout << "" << std::endl; 273 | std::cout << "" << std::endl; 287 | std::cout << "" << std::endl; 288 | 289 | std::cout << "" << std::endl; 290 | std::cout << "" << std::endl; 291 | std::cout << desc << std::endl; 292 | std::cout << "" << std::endl; 293 | std::cout << "" << std::endl; 294 | 295 | std::cout << "" << std::endl; 296 | } 297 | 298 | } //namespace TCLAP 299 | #endif 300 | -------------------------------------------------------------------------------- /core-packer/tclap/HelpVisitor.h: -------------------------------------------------------------------------------- 1 | 2 | /****************************************************************************** 3 | * 4 | * file: HelpVisitor.h 5 | * 6 | * Copyright (c) 2003, Michael E. Smoot . 7 | * All rights reverved. 8 | * 9 | * See the file COPYING in the top directory of this distribution for 10 | * more information. 11 | * 12 | * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS 13 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 14 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 15 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 16 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 17 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 18 | * DEALINGS IN THE SOFTWARE. 19 | * 20 | *****************************************************************************/ 21 | 22 | #ifndef TCLAP_HELP_VISITOR_H 23 | #define TCLAP_HELP_VISITOR_H 24 | 25 | #include 26 | #include 27 | #include 28 | 29 | namespace TCLAP { 30 | 31 | /** 32 | * A Visitor object that calls the usage method of the given CmdLineOutput 33 | * object for the specified CmdLine object. 34 | */ 35 | class HelpVisitor: public Visitor 36 | { 37 | private: 38 | /** 39 | * Prevent accidental copying. 40 | */ 41 | HelpVisitor(const HelpVisitor& rhs); 42 | HelpVisitor& operator=(const HelpVisitor& rhs); 43 | 44 | protected: 45 | 46 | /** 47 | * The CmdLine the output will be generated for. 48 | */ 49 | CmdLineInterface* _cmd; 50 | 51 | /** 52 | * The output object. 53 | */ 54 | CmdLineOutput** _out; 55 | 56 | public: 57 | 58 | /** 59 | * Constructor. 60 | * \param cmd - The CmdLine the output will be generated for. 61 | * \param out - The type of output. 62 | */ 63 | HelpVisitor(CmdLineInterface* cmd, CmdLineOutput** out) 64 | : Visitor(), _cmd( cmd ), _out( out ) { } 65 | 66 | /** 67 | * Calls the usage method of the CmdLineOutput for the 68 | * specified CmdLine. 69 | */ 70 | void visit() { (*_out)->usage(*_cmd); throw ExitException(0); } 71 | 72 | }; 73 | 74 | } 75 | 76 | #endif 77 | -------------------------------------------------------------------------------- /core-packer/tclap/IgnoreRestVisitor.h: -------------------------------------------------------------------------------- 1 | 2 | /****************************************************************************** 3 | * 4 | * file: IgnoreRestVisitor.h 5 | * 6 | * Copyright (c) 2003, Michael E. Smoot . 7 | * All rights reverved. 8 | * 9 | * See the file COPYING in the top directory of this distribution for 10 | * more information. 11 | * 12 | * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS 13 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 14 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 15 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 16 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 17 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 18 | * DEALINGS IN THE SOFTWARE. 19 | * 20 | *****************************************************************************/ 21 | 22 | 23 | #ifndef TCLAP_IGNORE_REST_VISITOR_H 24 | #define TCLAP_IGNORE_REST_VISITOR_H 25 | 26 | #include 27 | #include 28 | 29 | namespace TCLAP { 30 | 31 | /** 32 | * A Vistor that tells the CmdLine to begin ignoring arguments after 33 | * this one is parsed. 34 | */ 35 | class IgnoreRestVisitor: public Visitor 36 | { 37 | public: 38 | 39 | /** 40 | * Constructor. 41 | */ 42 | IgnoreRestVisitor() : Visitor() {} 43 | 44 | /** 45 | * Sets Arg::_ignoreRest. 46 | */ 47 | void visit() { Arg::beginIgnoring(); } 48 | }; 49 | 50 | } 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /core-packer/tclap/MultiSwitchArg.h: -------------------------------------------------------------------------------- 1 | 2 | /****************************************************************************** 3 | * 4 | * file: MultiSwitchArg.h 5 | * 6 | * Copyright (c) 2003, Michael E. Smoot . 7 | * Copyright (c) 2004, Michael E. Smoot, Daniel Aarno. 8 | * Copyright (c) 2005, Michael E. Smoot, Daniel Aarno, Erik Zeek. 9 | * All rights reverved. 10 | * 11 | * See the file COPYING in the top directory of this distribution for 12 | * more information. 13 | * 14 | * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | * DEALINGS IN THE SOFTWARE. 21 | * 22 | *****************************************************************************/ 23 | 24 | 25 | #ifndef TCLAP_MULTI_SWITCH_ARG_H 26 | #define TCLAP_MULTI_SWITCH_ARG_H 27 | 28 | #include 29 | #include 30 | 31 | #include 32 | 33 | namespace TCLAP { 34 | 35 | /** 36 | * A multiple switch argument. If the switch is set on the command line, then 37 | * the getValue method will return the number of times the switch appears. 38 | */ 39 | class MultiSwitchArg : public SwitchArg 40 | { 41 | protected: 42 | 43 | /** 44 | * The value of the switch. 45 | */ 46 | int _value; 47 | 48 | /** 49 | * Used to support the reset() method so that ValueArg can be 50 | * reset to their constructed value. 51 | */ 52 | int _default; 53 | 54 | public: 55 | 56 | /** 57 | * MultiSwitchArg constructor. 58 | * \param flag - The one character flag that identifies this 59 | * argument on the command line. 60 | * \param name - A one word name for the argument. Can be 61 | * used as a long flag on the command line. 62 | * \param desc - A description of what the argument is for or 63 | * does. 64 | * \param init - Optional. The initial/default value of this Arg. 65 | * Defaults to 0. 66 | * \param v - An optional visitor. You probably should not 67 | * use this unless you have a very good reason. 68 | */ 69 | MultiSwitchArg(const std::string& flag, 70 | const std::string& name, 71 | const std::string& desc, 72 | int init = 0, 73 | Visitor* v = NULL); 74 | 75 | 76 | /** 77 | * MultiSwitchArg constructor. 78 | * \param flag - The one character flag that identifies this 79 | * argument on the command line. 80 | * \param name - A one word name for the argument. Can be 81 | * used as a long flag on the command line. 82 | * \param desc - A description of what the argument is for or 83 | * does. 84 | * \param parser - A CmdLine parser object to add this Arg to 85 | * \param init - Optional. The initial/default value of this Arg. 86 | * Defaults to 0. 87 | * \param v - An optional visitor. You probably should not 88 | * use this unless you have a very good reason. 89 | */ 90 | MultiSwitchArg(const std::string& flag, 91 | const std::string& name, 92 | const std::string& desc, 93 | CmdLineInterface& parser, 94 | int init = 0, 95 | Visitor* v = NULL); 96 | 97 | 98 | /** 99 | * Handles the processing of the argument. 100 | * This re-implements the SwitchArg version of this method to set the 101 | * _value of the argument appropriately. 102 | * \param i - Pointer the the current argument in the list. 103 | * \param args - Mutable list of strings. Passed 104 | * in from main(). 105 | */ 106 | virtual bool processArg(int* i, std::vector& args); 107 | 108 | /** 109 | * Returns int, the number of times the switch has been set. 110 | */ 111 | int getValue(); 112 | 113 | /** 114 | * Returns the shortID for this Arg. 115 | */ 116 | std::string shortID(const std::string& val) const; 117 | 118 | /** 119 | * Returns the longID for this Arg. 120 | */ 121 | std::string longID(const std::string& val) const; 122 | 123 | void reset(); 124 | 125 | }; 126 | 127 | ////////////////////////////////////////////////////////////////////// 128 | //BEGIN MultiSwitchArg.cpp 129 | ////////////////////////////////////////////////////////////////////// 130 | inline MultiSwitchArg::MultiSwitchArg(const std::string& flag, 131 | const std::string& name, 132 | const std::string& desc, 133 | int init, 134 | Visitor* v ) 135 | : SwitchArg(flag, name, desc, false, v), 136 | _value( init ), 137 | _default( init ) 138 | { } 139 | 140 | inline MultiSwitchArg::MultiSwitchArg(const std::string& flag, 141 | const std::string& name, 142 | const std::string& desc, 143 | CmdLineInterface& parser, 144 | int init, 145 | Visitor* v ) 146 | : SwitchArg(flag, name, desc, false, v), 147 | _value( init ), 148 | _default( init ) 149 | { 150 | parser.add( this ); 151 | } 152 | 153 | inline int MultiSwitchArg::getValue() { return _value; } 154 | 155 | inline bool MultiSwitchArg::processArg(int *i, std::vector& args) 156 | { 157 | if ( _ignoreable && Arg::ignoreRest() ) 158 | return false; 159 | 160 | if ( argMatches( args[*i] )) 161 | { 162 | // so the isSet() method will work 163 | _alreadySet = true; 164 | 165 | // Matched argument: increment value. 166 | ++_value; 167 | 168 | _checkWithVisitor(); 169 | 170 | return true; 171 | } 172 | else if ( combinedSwitchesMatch( args[*i] ) ) 173 | { 174 | // so the isSet() method will work 175 | _alreadySet = true; 176 | 177 | // Matched argument: increment value. 178 | ++_value; 179 | 180 | // Check for more in argument and increment value. 181 | while ( combinedSwitchesMatch( args[*i] ) ) 182 | ++_value; 183 | 184 | _checkWithVisitor(); 185 | 186 | return false; 187 | } 188 | else 189 | return false; 190 | } 191 | 192 | inline std::string 193 | MultiSwitchArg::shortID(const std::string& val) const 194 | { 195 | return Arg::shortID(val) + " ... "; 196 | } 197 | 198 | inline std::string 199 | MultiSwitchArg::longID(const std::string& val) const 200 | { 201 | return Arg::longID(val) + " (accepted multiple times)"; 202 | } 203 | 204 | inline void 205 | MultiSwitchArg::reset() 206 | { 207 | MultiSwitchArg::_value = MultiSwitchArg::_default; 208 | } 209 | 210 | ////////////////////////////////////////////////////////////////////// 211 | //END MultiSwitchArg.cpp 212 | ////////////////////////////////////////////////////////////////////// 213 | 214 | } //namespace TCLAP 215 | 216 | #endif 217 | -------------------------------------------------------------------------------- /core-packer/tclap/OptionalUnlabeledTracker.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | /****************************************************************************** 4 | * 5 | * file: OptionalUnlabeledTracker.h 6 | * 7 | * Copyright (c) 2005, Michael E. Smoot . 8 | * All rights reverved. 9 | * 10 | * See the file COPYING in the top directory of this distribution for 11 | * more information. 12 | * 13 | * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS 14 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 16 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 19 | * DEALINGS IN THE SOFTWARE. 20 | * 21 | *****************************************************************************/ 22 | 23 | 24 | #ifndef TCLAP_OPTIONAL_UNLABELED_TRACKER_H 25 | #define TCLAP_OPTIONAL_UNLABELED_TRACKER_H 26 | 27 | #include 28 | 29 | namespace TCLAP { 30 | 31 | class OptionalUnlabeledTracker 32 | { 33 | 34 | public: 35 | 36 | static void check( bool req, const std::string& argName ); 37 | 38 | static void gotOptional() { alreadyOptionalRef() = true; } 39 | 40 | static bool& alreadyOptional() { return alreadyOptionalRef(); } 41 | 42 | private: 43 | 44 | static bool& alreadyOptionalRef() { static bool ct = false; return ct; } 45 | }; 46 | 47 | 48 | inline void OptionalUnlabeledTracker::check( bool req, const std::string& argName ) 49 | { 50 | if ( OptionalUnlabeledTracker::alreadyOptional() ) 51 | throw( SpecificationException( 52 | "You can't specify ANY Unlabeled Arg following an optional Unlabeled Arg", 53 | argName ) ); 54 | 55 | if ( !req ) 56 | OptionalUnlabeledTracker::gotOptional(); 57 | } 58 | 59 | 60 | } // namespace TCLAP 61 | 62 | #endif 63 | -------------------------------------------------------------------------------- /core-packer/tclap/StandardTraits.h: -------------------------------------------------------------------------------- 1 | // -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*- 2 | 3 | /****************************************************************************** 4 | * 5 | * file: StandardTraits.h 6 | * 7 | * Copyright (c) 2007, Daniel Aarno, Michael E. Smoot . 8 | * All rights reverved. 9 | * 10 | * See the file COPYING in the top directory of this distribution for 11 | * more information. 12 | * 13 | * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS 14 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 16 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 19 | * DEALINGS IN THE SOFTWARE. 20 | * 21 | *****************************************************************************/ 22 | 23 | // This is an internal tclap file, you should probably not have to 24 | // include this directly 25 | 26 | #ifndef TCLAP_STANDARD_TRAITS_H 27 | #define TCLAP_STANDARD_TRAITS_H 28 | 29 | #ifdef HAVE_CONFIG_H 30 | #include // To check for long long 31 | #endif 32 | 33 | // If Microsoft has already typedef'd wchar_t as an unsigned 34 | // short, then compiles will break because it's as if we're 35 | // creating ArgTraits twice for unsigned short. Thus... 36 | #ifdef _MSC_VER 37 | #ifndef _NATIVE_WCHAR_T_DEFINED 38 | #define TCLAP_DONT_DECLARE_WCHAR_T_ARGTRAITS 39 | #endif 40 | #endif 41 | 42 | namespace TCLAP { 43 | 44 | // ====================================================================== 45 | // Integer types 46 | // ====================================================================== 47 | 48 | /** 49 | * longs have value-like semantics. 50 | */ 51 | template<> 52 | struct ArgTraits { 53 | typedef ValueLike ValueCategory; 54 | }; 55 | 56 | /** 57 | * ints have value-like semantics. 58 | */ 59 | template<> 60 | struct ArgTraits { 61 | typedef ValueLike ValueCategory; 62 | }; 63 | 64 | /** 65 | * shorts have value-like semantics. 66 | */ 67 | template<> 68 | struct ArgTraits { 69 | typedef ValueLike ValueCategory; 70 | }; 71 | 72 | /** 73 | * chars have value-like semantics. 74 | */ 75 | template<> 76 | struct ArgTraits { 77 | typedef ValueLike ValueCategory; 78 | }; 79 | 80 | #ifdef HAVE_LONG_LONG 81 | /** 82 | * long longs have value-like semantics. 83 | */ 84 | template<> 85 | struct ArgTraits { 86 | typedef ValueLike ValueCategory; 87 | }; 88 | #endif 89 | 90 | // ====================================================================== 91 | // Unsigned integer types 92 | // ====================================================================== 93 | 94 | /** 95 | * unsigned longs have value-like semantics. 96 | */ 97 | template<> 98 | struct ArgTraits { 99 | typedef ValueLike ValueCategory; 100 | }; 101 | 102 | /** 103 | * unsigned ints have value-like semantics. 104 | */ 105 | template<> 106 | struct ArgTraits { 107 | typedef ValueLike ValueCategory; 108 | }; 109 | 110 | /** 111 | * unsigned shorts have value-like semantics. 112 | */ 113 | template<> 114 | struct ArgTraits { 115 | typedef ValueLike ValueCategory; 116 | }; 117 | 118 | /** 119 | * unsigned chars have value-like semantics. 120 | */ 121 | template<> 122 | struct ArgTraits { 123 | typedef ValueLike ValueCategory; 124 | }; 125 | 126 | // Microsoft implements size_t awkwardly. 127 | #if defined(_MSC_VER) && defined(_M_X64) 128 | /** 129 | * size_ts have value-like semantics. 130 | */ 131 | template<> 132 | struct ArgTraits { 133 | typedef ValueLike ValueCategory; 134 | }; 135 | #endif 136 | 137 | 138 | #ifdef HAVE_LONG_LONG 139 | /** 140 | * unsigned long longs have value-like semantics. 141 | */ 142 | template<> 143 | struct ArgTraits { 144 | typedef ValueLike ValueCategory; 145 | }; 146 | #endif 147 | 148 | // ====================================================================== 149 | // Float types 150 | // ====================================================================== 151 | 152 | /** 153 | * floats have value-like semantics. 154 | */ 155 | template<> 156 | struct ArgTraits { 157 | typedef ValueLike ValueCategory; 158 | }; 159 | 160 | /** 161 | * doubles have value-like semantics. 162 | */ 163 | template<> 164 | struct ArgTraits { 165 | typedef ValueLike ValueCategory; 166 | }; 167 | 168 | // ====================================================================== 169 | // Other types 170 | // ====================================================================== 171 | 172 | /** 173 | * bools have value-like semantics. 174 | */ 175 | template<> 176 | struct ArgTraits { 177 | typedef ValueLike ValueCategory; 178 | }; 179 | 180 | 181 | /** 182 | * wchar_ts have value-like semantics. 183 | */ 184 | #ifndef TCLAP_DONT_DECLARE_WCHAR_T_ARGTRAITS 185 | template<> 186 | struct ArgTraits { 187 | typedef ValueLike ValueCategory; 188 | }; 189 | #endif 190 | 191 | /** 192 | * Strings have string like argument traits. 193 | */ 194 | template<> 195 | struct ArgTraits { 196 | typedef StringLike ValueCategory; 197 | }; 198 | 199 | template 200 | void SetString(T &dst, const std::string &src) 201 | { 202 | dst = src; 203 | } 204 | 205 | } // namespace 206 | 207 | #endif 208 | 209 | -------------------------------------------------------------------------------- /core-packer/tclap/StdOutput.h: -------------------------------------------------------------------------------- 1 | // -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*- 2 | 3 | /****************************************************************************** 4 | * 5 | * file: StdOutput.h 6 | * 7 | * Copyright (c) 2004, Michael E. Smoot 8 | * All rights reverved. 9 | * 10 | * See the file COPYING in the top directory of this distribution for 11 | * more information. 12 | * 13 | * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS 14 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 16 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 19 | * DEALINGS IN THE SOFTWARE. 20 | * 21 | *****************************************************************************/ 22 | 23 | #ifndef TCLAP_STDCMDLINEOUTPUT_H 24 | #define TCLAP_STDCMDLINEOUTPUT_H 25 | 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | #include 33 | #include 34 | #include 35 | #include 36 | 37 | namespace TCLAP { 38 | 39 | /** 40 | * A class that isolates any output from the CmdLine object so that it 41 | * may be easily modified. 42 | */ 43 | class StdOutput : public CmdLineOutput 44 | { 45 | 46 | public: 47 | 48 | /** 49 | * Prints the usage to stdout. Can be overridden to 50 | * produce alternative behavior. 51 | * \param c - The CmdLine object the output is generated for. 52 | */ 53 | virtual void usage(CmdLineInterface& c); 54 | 55 | /** 56 | * Prints the version to stdout. Can be overridden 57 | * to produce alternative behavior. 58 | * \param c - The CmdLine object the output is generated for. 59 | */ 60 | virtual void version(CmdLineInterface& c); 61 | 62 | /** 63 | * Prints (to stderr) an error message, short usage 64 | * Can be overridden to produce alternative behavior. 65 | * \param c - The CmdLine object the output is generated for. 66 | * \param e - The ArgException that caused the failure. 67 | */ 68 | virtual void failure(CmdLineInterface& c, 69 | ArgException& e ); 70 | 71 | protected: 72 | 73 | /** 74 | * Writes a brief usage message with short args. 75 | * \param c - The CmdLine object the output is generated for. 76 | * \param os - The stream to write the message to. 77 | */ 78 | void _shortUsage( CmdLineInterface& c, std::ostream& os ) const; 79 | 80 | /** 81 | * Writes a longer usage message with long and short args, 82 | * provides descriptions and prints message. 83 | * \param c - The CmdLine object the output is generated for. 84 | * \param os - The stream to write the message to. 85 | */ 86 | void _longUsage( CmdLineInterface& c, std::ostream& os ) const; 87 | 88 | /** 89 | * This function inserts line breaks and indents long strings 90 | * according the params input. It will only break lines at spaces, 91 | * commas and pipes. 92 | * \param os - The stream to be printed to. 93 | * \param s - The string to be printed. 94 | * \param maxWidth - The maxWidth allowed for the output line. 95 | * \param indentSpaces - The number of spaces to indent the first line. 96 | * \param secondLineOffset - The number of spaces to indent the second 97 | * and all subsequent lines in addition to indentSpaces. 98 | */ 99 | void spacePrint( std::ostream& os, 100 | const std::string& s, 101 | int maxWidth, 102 | int indentSpaces, 103 | int secondLineOffset ) const; 104 | 105 | }; 106 | 107 | 108 | inline void StdOutput::version(CmdLineInterface& _cmd) 109 | { 110 | std::string progName = _cmd.getProgramName(); 111 | std::string xversion = _cmd.getVersion(); 112 | 113 | std::cout << std::endl << progName << " version: " 114 | << xversion << std::endl << std::endl; 115 | } 116 | 117 | inline void StdOutput::usage(CmdLineInterface& _cmd ) 118 | { 119 | std::cout << std::endl << "USAGE: " << std::endl << std::endl; 120 | 121 | _shortUsage( _cmd, std::cout ); 122 | 123 | std::cout << std::endl << std::endl << "Where: " << std::endl << std::endl; 124 | 125 | _longUsage( _cmd, std::cout ); 126 | 127 | std::cout << std::endl; 128 | 129 | } 130 | 131 | inline void StdOutput::failure( CmdLineInterface& _cmd, 132 | ArgException& e ) 133 | { 134 | std::string progName = _cmd.getProgramName(); 135 | 136 | std::cerr << "PARSE ERROR: " << e.argId() << std::endl 137 | << " " << e.error() << std::endl << std::endl; 138 | 139 | if ( _cmd.hasHelpAndVersion() ) 140 | { 141 | std::cerr << "Brief USAGE: " << std::endl; 142 | 143 | _shortUsage( _cmd, std::cerr ); 144 | 145 | std::cerr << std::endl << "For complete USAGE and HELP type: " 146 | << std::endl << " " << progName << " --help" 147 | << std::endl << std::endl; 148 | } 149 | else 150 | usage(_cmd); 151 | 152 | throw ExitException(1); 153 | } 154 | 155 | inline void 156 | StdOutput::_shortUsage( CmdLineInterface& _cmd, 157 | std::ostream& os ) const 158 | { 159 | std::list argList = _cmd.getArgList(); 160 | std::string progName = _cmd.getProgramName(); 161 | XorHandler xorHandler = _cmd.getXorHandler(); 162 | std::vector< std::vector > xorList = xorHandler.getXorList(); 163 | 164 | std::string s = progName + " "; 165 | 166 | // first the xor 167 | for ( int i = 0; static_cast(i) < xorList.size(); i++ ) 168 | { 169 | s += " {"; 170 | for ( ArgVectorIterator it = xorList[i].begin(); 171 | it != xorList[i].end(); it++ ) 172 | s += (*it)->shortID() + "|"; 173 | 174 | s[s.length()-1] = '}'; 175 | } 176 | 177 | // then the rest 178 | for (ArgListIterator it = argList.begin(); it != argList.end(); it++) 179 | if ( !xorHandler.contains( (*it) ) ) 180 | s += " " + (*it)->shortID(); 181 | 182 | // if the program name is too long, then adjust the second line offset 183 | int secondLineOffset = static_cast(progName.length()) + 2; 184 | if ( secondLineOffset > 75/2 ) 185 | secondLineOffset = static_cast(75/2); 186 | 187 | spacePrint( os, s, 75, 3, secondLineOffset ); 188 | } 189 | 190 | inline void 191 | StdOutput::_longUsage( CmdLineInterface& _cmd, 192 | std::ostream& os ) const 193 | { 194 | std::list argList = _cmd.getArgList(); 195 | std::string message = _cmd.getMessage(); 196 | XorHandler xorHandler = _cmd.getXorHandler(); 197 | std::vector< std::vector > xorList = xorHandler.getXorList(); 198 | 199 | // first the xor 200 | for ( int i = 0; static_cast(i) < xorList.size(); i++ ) 201 | { 202 | for ( ArgVectorIterator it = xorList[i].begin(); 203 | it != xorList[i].end(); 204 | it++ ) 205 | { 206 | spacePrint( os, (*it)->longID(), 75, 3, 3 ); 207 | spacePrint( os, (*it)->getDescription(), 75, 5, 0 ); 208 | 209 | if ( it+1 != xorList[i].end() ) 210 | spacePrint(os, "-- OR --", 75, 9, 0); 211 | } 212 | os << std::endl << std::endl; 213 | } 214 | 215 | // then the rest 216 | for (ArgListIterator it = argList.begin(); it != argList.end(); it++) 217 | if ( !xorHandler.contains( (*it) ) ) 218 | { 219 | spacePrint( os, (*it)->longID(), 75, 3, 3 ); 220 | spacePrint( os, (*it)->getDescription(), 75, 5, 0 ); 221 | os << std::endl; 222 | } 223 | 224 | os << std::endl; 225 | 226 | spacePrint( os, message, 75, 3, 0 ); 227 | } 228 | 229 | inline void StdOutput::spacePrint( std::ostream& os, 230 | const std::string& s, 231 | int maxWidth, 232 | int indentSpaces, 233 | int secondLineOffset ) const 234 | { 235 | int len = static_cast(s.length()); 236 | 237 | if ( (len + indentSpaces > maxWidth) && maxWidth > 0 ) 238 | { 239 | int allowedLen = maxWidth - indentSpaces; 240 | int start = 0; 241 | while ( start < len ) 242 | { 243 | // find the substring length 244 | // int stringLen = std::min( len - start, allowedLen ); 245 | // doing it this way to support a VisualC++ 2005 bug 246 | using namespace std; 247 | int stringLen = min( len - start, allowedLen ); 248 | 249 | // trim the length so it doesn't end in middle of a word 250 | if ( stringLen == allowedLen ) 251 | while ( stringLen >= 0 && 252 | s[stringLen+start] != ' ' && 253 | s[stringLen+start] != ',' && 254 | s[stringLen+start] != '|' ) 255 | stringLen--; 256 | 257 | // ok, the word is longer than the line, so just split 258 | // wherever the line ends 259 | if ( stringLen <= 0 ) 260 | stringLen = allowedLen; 261 | 262 | // check for newlines 263 | for ( int i = 0; i < stringLen; i++ ) 264 | if ( s[start+i] == '\n' ) 265 | stringLen = i+1; 266 | 267 | // print the indent 268 | for ( int i = 0; i < indentSpaces; i++ ) 269 | os << " "; 270 | 271 | if ( start == 0 ) 272 | { 273 | // handle second line offsets 274 | indentSpaces += secondLineOffset; 275 | 276 | // adjust allowed len 277 | allowedLen -= secondLineOffset; 278 | } 279 | 280 | os << s.substr(start,stringLen) << std::endl; 281 | 282 | // so we don't start a line with a space 283 | while ( s[stringLen+start] == ' ' && start < len ) 284 | start++; 285 | 286 | start += stringLen; 287 | } 288 | } 289 | else 290 | { 291 | for ( int i = 0; i < indentSpaces; i++ ) 292 | os << " "; 293 | os << s << std::endl; 294 | } 295 | } 296 | 297 | } //namespace TCLAP 298 | #endif 299 | -------------------------------------------------------------------------------- /core-packer/tclap/SwitchArg.h: -------------------------------------------------------------------------------- 1 | 2 | /****************************************************************************** 3 | * 4 | * file: SwitchArg.h 5 | * 6 | * Copyright (c) 2003, Michael E. Smoot . 7 | * Copyright (c) 2004, Michael E. Smoot, Daniel Aarno. 8 | * All rights reverved. 9 | * 10 | * See the file COPYING in the top directory of this distribution for 11 | * more information. 12 | * 13 | * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS 14 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 16 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 19 | * DEALINGS IN THE SOFTWARE. 20 | * 21 | *****************************************************************************/ 22 | 23 | 24 | #ifndef TCLAP_SWITCH_ARG_H 25 | #define TCLAP_SWITCH_ARG_H 26 | 27 | #include 28 | #include 29 | 30 | #include 31 | 32 | namespace TCLAP { 33 | 34 | /** 35 | * A simple switch argument. If the switch is set on the command line, then 36 | * the getValue method will return the opposite of the default value for the 37 | * switch. 38 | */ 39 | class SwitchArg : public Arg 40 | { 41 | protected: 42 | 43 | /** 44 | * The value of the switch. 45 | */ 46 | bool _value; 47 | 48 | /** 49 | * Used to support the reset() method so that ValueArg can be 50 | * reset to their constructed value. 51 | */ 52 | bool _default; 53 | 54 | public: 55 | 56 | /** 57 | * SwitchArg constructor. 58 | * \param flag - The one character flag that identifies this 59 | * argument on the command line. 60 | * \param name - A one word name for the argument. Can be 61 | * used as a long flag on the command line. 62 | * \param desc - A description of what the argument is for or 63 | * does. 64 | * \param def - The default value for this Switch. 65 | * \param v - An optional visitor. You probably should not 66 | * use this unless you have a very good reason. 67 | */ 68 | SwitchArg(const std::string& flag, 69 | const std::string& name, 70 | const std::string& desc, 71 | bool def = false, 72 | Visitor* v = NULL); 73 | 74 | 75 | /** 76 | * SwitchArg constructor. 77 | * \param flag - The one character flag that identifies this 78 | * argument on the command line. 79 | * \param name - A one word name for the argument. Can be 80 | * used as a long flag on the command line. 81 | * \param desc - A description of what the argument is for or 82 | * does. 83 | * \param parser - A CmdLine parser object to add this Arg to 84 | * \param def - The default value for this Switch. 85 | * \param v - An optional visitor. You probably should not 86 | * use this unless you have a very good reason. 87 | */ 88 | SwitchArg(const std::string& flag, 89 | const std::string& name, 90 | const std::string& desc, 91 | CmdLineInterface& parser, 92 | bool def = false, 93 | Visitor* v = NULL); 94 | 95 | 96 | /** 97 | * Handles the processing of the argument. 98 | * This re-implements the Arg version of this method to set the 99 | * _value of the argument appropriately. 100 | * \param i - Pointer the the current argument in the list. 101 | * \param args - Mutable list of strings. Passed 102 | * in from main(). 103 | */ 104 | virtual bool processArg(int* i, std::vector& args); 105 | 106 | /** 107 | * Checks a string to see if any of the chars in the string 108 | * match the flag for this Switch. 109 | */ 110 | bool combinedSwitchesMatch(std::string& combined); 111 | 112 | /** 113 | * Returns bool, whether or not the switch has been set. 114 | */ 115 | bool getValue(); 116 | 117 | virtual void reset(); 118 | 119 | private: 120 | /** 121 | * Checks to see if we've found the last match in 122 | * a combined string. 123 | */ 124 | bool lastCombined(std::string& combined); 125 | 126 | /** 127 | * Does the common processing of processArg. 128 | */ 129 | void commonProcessing(); 130 | }; 131 | 132 | ////////////////////////////////////////////////////////////////////// 133 | //BEGIN SwitchArg.cpp 134 | ////////////////////////////////////////////////////////////////////// 135 | inline SwitchArg::SwitchArg(const std::string& flag, 136 | const std::string& name, 137 | const std::string& desc, 138 | bool default_val, 139 | Visitor* v ) 140 | : Arg(flag, name, desc, false, false, v), 141 | _value( default_val ), 142 | _default( default_val ) 143 | { } 144 | 145 | inline SwitchArg::SwitchArg(const std::string& flag, 146 | const std::string& name, 147 | const std::string& desc, 148 | CmdLineInterface& parser, 149 | bool default_val, 150 | Visitor* v ) 151 | : Arg(flag, name, desc, false, false, v), 152 | _value( default_val ), 153 | _default(default_val) 154 | { 155 | parser.add( this ); 156 | } 157 | 158 | inline bool SwitchArg::getValue() { return _value; } 159 | 160 | inline bool SwitchArg::lastCombined(std::string& combinedSwitches ) 161 | { 162 | for ( unsigned int i = 1; i < combinedSwitches.length(); i++ ) 163 | if ( combinedSwitches[i] != Arg::blankChar() ) 164 | return false; 165 | 166 | return true; 167 | } 168 | 169 | inline bool SwitchArg::combinedSwitchesMatch(std::string& combinedSwitches ) 170 | { 171 | // make sure this is actually a combined switch 172 | if ( combinedSwitches.length() > 0 && 173 | combinedSwitches[0] != Arg::flagStartString()[0] ) 174 | return false; 175 | 176 | // make sure it isn't a long name 177 | if ( combinedSwitches.substr( 0, Arg::nameStartString().length() ) == 178 | Arg::nameStartString() ) 179 | return false; 180 | 181 | // make sure the delimiter isn't in the string 182 | if ( combinedSwitches.find_first_of( Arg::delimiter() ) != std::string::npos ) 183 | return false; 184 | 185 | // ok, we're not specifying a ValueArg, so we know that we have 186 | // a combined switch list. 187 | for ( unsigned int i = 1; i < combinedSwitches.length(); i++ ) 188 | if ( _flag.length() > 0 && 189 | combinedSwitches[i] == _flag[0] && 190 | _flag[0] != Arg::flagStartString()[0] ) 191 | { 192 | // update the combined switches so this one is no longer present 193 | // this is necessary so that no unlabeled args are matched 194 | // later in the processing. 195 | //combinedSwitches.erase(i,1); 196 | combinedSwitches[i] = Arg::blankChar(); 197 | return true; 198 | } 199 | 200 | // none of the switches passed in the list match. 201 | return false; 202 | } 203 | 204 | inline void SwitchArg::commonProcessing() 205 | { 206 | if ( _xorSet ) 207 | throw(CmdLineParseException( 208 | "Mutually exclusive argument already set!", toString())); 209 | 210 | if ( _alreadySet ) 211 | throw(CmdLineParseException("Argument already set!", toString())); 212 | 213 | _alreadySet = true; 214 | 215 | if ( _value == true ) 216 | _value = false; 217 | else 218 | _value = true; 219 | 220 | _checkWithVisitor(); 221 | } 222 | 223 | inline bool SwitchArg::processArg(int *i, std::vector& args) 224 | { 225 | if ( _ignoreable && Arg::ignoreRest() ) 226 | return false; 227 | 228 | // if the whole string matches the flag or name string 229 | if ( argMatches( args[*i] ) ) 230 | { 231 | commonProcessing(); 232 | 233 | return true; 234 | } 235 | // if a substring matches the flag as part of a combination 236 | else if ( combinedSwitchesMatch( args[*i] ) ) 237 | { 238 | // check again to ensure we don't misinterpret 239 | // this as a MultiSwitchArg 240 | if ( combinedSwitchesMatch( args[*i] ) ) 241 | throw(CmdLineParseException("Argument already set!", 242 | toString())); 243 | 244 | commonProcessing(); 245 | 246 | // We only want to return true if we've found the last combined 247 | // match in the string, otherwise we return true so that other 248 | // switches in the combination will have a chance to match. 249 | return lastCombined( args[*i] ); 250 | } 251 | else 252 | return false; 253 | } 254 | 255 | inline void SwitchArg::reset() 256 | { 257 | Arg::reset(); 258 | _value = _default; 259 | } 260 | ////////////////////////////////////////////////////////////////////// 261 | //End SwitchArg.cpp 262 | ////////////////////////////////////////////////////////////////////// 263 | 264 | } //namespace TCLAP 265 | 266 | #endif 267 | -------------------------------------------------------------------------------- /core-packer/tclap/UnlabeledMultiArg.h: -------------------------------------------------------------------------------- 1 | 2 | /****************************************************************************** 3 | * 4 | * file: UnlabeledMultiArg.h 5 | * 6 | * Copyright (c) 2003, Michael E. Smoot. 7 | * All rights reverved. 8 | * 9 | * See the file COPYING in the top directory of this distribution for 10 | * more information. 11 | * 12 | * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS 13 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 14 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 15 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 16 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 17 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 18 | * DEALINGS IN THE SOFTWARE. 19 | * 20 | *****************************************************************************/ 21 | 22 | 23 | #ifndef TCLAP_MULTIPLE_UNLABELED_ARGUMENT_H 24 | #define TCLAP_MULTIPLE_UNLABELED_ARGUMENT_H 25 | 26 | #include 27 | #include 28 | 29 | #include 30 | #include 31 | 32 | namespace TCLAP { 33 | 34 | /** 35 | * Just like a MultiArg, except that the arguments are unlabeled. Basically, 36 | * this Arg will slurp up everything that hasn't been matched to another 37 | * Arg. 38 | */ 39 | template 40 | class UnlabeledMultiArg : public MultiArg 41 | { 42 | 43 | // If compiler has two stage name lookup (as gcc >= 3.4 does) 44 | // this is requried to prevent undef. symbols 45 | using MultiArg::_ignoreable; 46 | using MultiArg::_hasBlanks; 47 | using MultiArg::_extractValue; 48 | using MultiArg::_typeDesc; 49 | using MultiArg::_name; 50 | using MultiArg::_description; 51 | using MultiArg::_alreadySet; 52 | using MultiArg::toString; 53 | 54 | public: 55 | 56 | /** 57 | * Constructor. 58 | * \param name - The name of the Arg. Note that this is used for 59 | * identification, not as a long flag. 60 | * \param desc - A description of what the argument is for or 61 | * does. 62 | * \param req - Whether the argument is required on the command 63 | * line. 64 | * \param typeDesc - A short, human readable description of the 65 | * type that this object expects. This is used in the generation 66 | * of the USAGE statement. The goal is to be helpful to the end user 67 | * of the program. 68 | * \param ignoreable - Whether or not this argument can be ignored 69 | * using the "--" flag. 70 | * \param v - An optional visitor. You probably should not 71 | * use this unless you have a very good reason. 72 | */ 73 | UnlabeledMultiArg( const std::string& name, 74 | const std::string& desc, 75 | bool req, 76 | const std::string& typeDesc, 77 | bool ignoreable = false, 78 | Visitor* v = NULL ); 79 | /** 80 | * Constructor. 81 | * \param name - The name of the Arg. Note that this is used for 82 | * identification, not as a long flag. 83 | * \param desc - A description of what the argument is for or 84 | * does. 85 | * \param req - Whether the argument is required on the command 86 | * line. 87 | * \param typeDesc - A short, human readable description of the 88 | * type that this object expects. This is used in the generation 89 | * of the USAGE statement. The goal is to be helpful to the end user 90 | * of the program. 91 | * \param parser - A CmdLine parser object to add this Arg to 92 | * \param ignoreable - Whether or not this argument can be ignored 93 | * using the "--" flag. 94 | * \param v - An optional visitor. You probably should not 95 | * use this unless you have a very good reason. 96 | */ 97 | UnlabeledMultiArg( const std::string& name, 98 | const std::string& desc, 99 | bool req, 100 | const std::string& typeDesc, 101 | CmdLineInterface& parser, 102 | bool ignoreable = false, 103 | Visitor* v = NULL ); 104 | 105 | /** 106 | * Constructor. 107 | * \param name - The name of the Arg. Note that this is used for 108 | * identification, not as a long flag. 109 | * \param desc - A description of what the argument is for or 110 | * does. 111 | * \param req - Whether the argument is required on the command 112 | * line. 113 | * \param constraint - A pointer to a Constraint object used 114 | * to constrain this Arg. 115 | * \param ignoreable - Whether or not this argument can be ignored 116 | * using the "--" flag. 117 | * \param v - An optional visitor. You probably should not 118 | * use this unless you have a very good reason. 119 | */ 120 | UnlabeledMultiArg( const std::string& name, 121 | const std::string& desc, 122 | bool req, 123 | Constraint* constraint, 124 | bool ignoreable = false, 125 | Visitor* v = NULL ); 126 | 127 | /** 128 | * Constructor. 129 | * \param name - The name of the Arg. Note that this is used for 130 | * identification, not as a long flag. 131 | * \param desc - A description of what the argument is for or 132 | * does. 133 | * \param req - Whether the argument is required on the command 134 | * line. 135 | * \param constraint - A pointer to a Constraint object used 136 | * to constrain this Arg. 137 | * \param parser - A CmdLine parser object to add this Arg to 138 | * \param ignoreable - Whether or not this argument can be ignored 139 | * using the "--" flag. 140 | * \param v - An optional visitor. You probably should not 141 | * use this unless you have a very good reason. 142 | */ 143 | UnlabeledMultiArg( const std::string& name, 144 | const std::string& desc, 145 | bool req, 146 | Constraint* constraint, 147 | CmdLineInterface& parser, 148 | bool ignoreable = false, 149 | Visitor* v = NULL ); 150 | 151 | /** 152 | * Handles the processing of the argument. 153 | * This re-implements the Arg version of this method to set the 154 | * _value of the argument appropriately. It knows the difference 155 | * between labeled and unlabeled. 156 | * \param i - Pointer the the current argument in the list. 157 | * \param args - Mutable list of strings. Passed from main(). 158 | */ 159 | virtual bool processArg(int* i, std::vector& args); 160 | 161 | /** 162 | * Returns the a short id string. Used in the usage. 163 | * \param val - value to be used. 164 | */ 165 | virtual std::string shortID(const std::string& val="val") const; 166 | 167 | /** 168 | * Returns the a long id string. Used in the usage. 169 | * \param val - value to be used. 170 | */ 171 | virtual std::string longID(const std::string& val="val") const; 172 | 173 | /** 174 | * Opertor ==. 175 | * \param a - The Arg to be compared to this. 176 | */ 177 | virtual bool operator==(const Arg& a) const; 178 | 179 | /** 180 | * Pushes this to back of list rather than front. 181 | * \param argList - The list this should be added to. 182 | */ 183 | virtual void addToList( std::list& argList ) const; 184 | }; 185 | 186 | template 187 | UnlabeledMultiArg::UnlabeledMultiArg(const std::string& name, 188 | const std::string& desc, 189 | bool req, 190 | const std::string& typeDesc, 191 | bool ignoreable, 192 | Visitor* v) 193 | : MultiArg("", name, desc, req, typeDesc, v) 194 | { 195 | _ignoreable = ignoreable; 196 | OptionalUnlabeledTracker::check(true, toString()); 197 | } 198 | 199 | template 200 | UnlabeledMultiArg::UnlabeledMultiArg(const std::string& name, 201 | const std::string& desc, 202 | bool req, 203 | const std::string& typeDesc, 204 | CmdLineInterface& parser, 205 | bool ignoreable, 206 | Visitor* v) 207 | : MultiArg("", name, desc, req, typeDesc, v) 208 | { 209 | _ignoreable = ignoreable; 210 | OptionalUnlabeledTracker::check(true, toString()); 211 | parser.add( this ); 212 | } 213 | 214 | 215 | template 216 | UnlabeledMultiArg::UnlabeledMultiArg(const std::string& name, 217 | const std::string& desc, 218 | bool req, 219 | Constraint* constraint, 220 | bool ignoreable, 221 | Visitor* v) 222 | : MultiArg("", name, desc, req, constraint, v) 223 | { 224 | _ignoreable = ignoreable; 225 | OptionalUnlabeledTracker::check(true, toString()); 226 | } 227 | 228 | template 229 | UnlabeledMultiArg::UnlabeledMultiArg(const std::string& name, 230 | const std::string& desc, 231 | bool req, 232 | Constraint* constraint, 233 | CmdLineInterface& parser, 234 | bool ignoreable, 235 | Visitor* v) 236 | : MultiArg("", name, desc, req, constraint, v) 237 | { 238 | _ignoreable = ignoreable; 239 | OptionalUnlabeledTracker::check(true, toString()); 240 | parser.add( this ); 241 | } 242 | 243 | 244 | template 245 | bool UnlabeledMultiArg::processArg(int *i, std::vector& args) 246 | { 247 | 248 | if ( _hasBlanks( args[*i] ) ) 249 | return false; 250 | 251 | // never ignore an unlabeled multi arg 252 | 253 | 254 | // always take the first value, regardless of the start string 255 | _extractValue( args[(*i)] ); 256 | 257 | /* 258 | // continue taking args until we hit the end or a start string 259 | while ( (unsigned int)(*i)+1 < args.size() && 260 | args[(*i)+1].find_first_of( Arg::flagStartString() ) != 0 && 261 | args[(*i)+1].find_first_of( Arg::nameStartString() ) != 0 ) 262 | _extractValue( args[++(*i)] ); 263 | */ 264 | 265 | _alreadySet = true; 266 | 267 | return true; 268 | } 269 | 270 | template 271 | std::string UnlabeledMultiArg::shortID(const std::string& val) const 272 | { 273 | static_cast(val); // Ignore input, don't warn 274 | return std::string("<") + _typeDesc + "> ..."; 275 | } 276 | 277 | template 278 | std::string UnlabeledMultiArg::longID(const std::string& val) const 279 | { 280 | static_cast(val); // Ignore input, don't warn 281 | return std::string("<") + _typeDesc + "> (accepted multiple times)"; 282 | } 283 | 284 | template 285 | bool UnlabeledMultiArg::operator==(const Arg& a) const 286 | { 287 | if ( _name == a.getName() || _description == a.getDescription() ) 288 | return true; 289 | else 290 | return false; 291 | } 292 | 293 | template 294 | void UnlabeledMultiArg::addToList( std::list& argList ) const 295 | { 296 | argList.push_back( const_cast(static_cast(this)) ); 297 | } 298 | 299 | } 300 | 301 | #endif 302 | -------------------------------------------------------------------------------- /core-packer/tclap/ValuesConstraint.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | /****************************************************************************** 4 | * 5 | * file: ValuesConstraint.h 6 | * 7 | * Copyright (c) 2005, Michael E. Smoot 8 | * All rights reverved. 9 | * 10 | * See the file COPYING in the top directory of this distribution for 11 | * more information. 12 | * 13 | * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS 14 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 16 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 19 | * DEALINGS IN THE SOFTWARE. 20 | * 21 | *****************************************************************************/ 22 | 23 | #ifndef TCLAP_VALUESCONSTRAINT_H 24 | #define TCLAP_VALUESCONSTRAINT_H 25 | 26 | #include 27 | #include 28 | #include 29 | 30 | #ifdef HAVE_CONFIG_H 31 | #include 32 | #else 33 | #define HAVE_SSTREAM 34 | #endif 35 | 36 | #if defined(HAVE_SSTREAM) 37 | #include 38 | #elif defined(HAVE_STRSTREAM) 39 | #include 40 | #else 41 | #error "Need a stringstream (sstream or strstream) to compile!" 42 | #endif 43 | 44 | namespace TCLAP { 45 | 46 | /** 47 | * A Constraint that constrains the Arg to only those values specified 48 | * in the constraint. 49 | */ 50 | template 51 | class ValuesConstraint : public Constraint 52 | { 53 | 54 | public: 55 | 56 | /** 57 | * Constructor. 58 | * \param allowed - vector of allowed values. 59 | */ 60 | ValuesConstraint(std::vector& allowed); 61 | 62 | /** 63 | * Virtual destructor. 64 | */ 65 | virtual ~ValuesConstraint() {} 66 | 67 | /** 68 | * Returns a description of the Constraint. 69 | */ 70 | virtual std::string description() const; 71 | 72 | /** 73 | * Returns the short ID for the Constraint. 74 | */ 75 | virtual std::string shortID() const; 76 | 77 | /** 78 | * The method used to verify that the value parsed from the command 79 | * line meets the constraint. 80 | * \param value - The value that will be checked. 81 | */ 82 | virtual bool check(const T& value) const; 83 | 84 | protected: 85 | 86 | /** 87 | * The list of valid values. 88 | */ 89 | std::vector _allowed; 90 | 91 | /** 92 | * The string used to describe the allowed values of this constraint. 93 | */ 94 | std::string _typeDesc; 95 | 96 | }; 97 | 98 | template 99 | ValuesConstraint::ValuesConstraint(std::vector& allowed) 100 | : _allowed(allowed), 101 | _typeDesc("") 102 | { 103 | for ( unsigned int i = 0; i < _allowed.size(); i++ ) 104 | { 105 | 106 | #if defined(HAVE_SSTREAM) 107 | std::ostringstream os; 108 | #elif defined(HAVE_STRSTREAM) 109 | std::ostrstream os; 110 | #else 111 | #error "Need a stringstream (sstream or strstream) to compile!" 112 | #endif 113 | 114 | os << _allowed[i]; 115 | 116 | std::string temp( os.str() ); 117 | 118 | if ( i > 0 ) 119 | _typeDesc += "|"; 120 | _typeDesc += temp; 121 | } 122 | } 123 | 124 | template 125 | bool ValuesConstraint::check( const T& val ) const 126 | { 127 | if ( std::find(_allowed.begin(),_allowed.end(),val) == _allowed.end() ) 128 | return false; 129 | else 130 | return true; 131 | } 132 | 133 | template 134 | std::string ValuesConstraint::shortID() const 135 | { 136 | return _typeDesc; 137 | } 138 | 139 | template 140 | std::string ValuesConstraint::description() const 141 | { 142 | return _typeDesc; 143 | } 144 | 145 | 146 | } //namespace TCLAP 147 | #endif 148 | 149 | -------------------------------------------------------------------------------- /core-packer/tclap/VersionVisitor.h: -------------------------------------------------------------------------------- 1 | // -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*- 2 | 3 | /****************************************************************************** 4 | * 5 | * file: VersionVisitor.h 6 | * 7 | * Copyright (c) 2003, Michael E. Smoot . 8 | * All rights reverved. 9 | * 10 | * See the file COPYING in the top directory of this distribution for 11 | * more information. 12 | * 13 | * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS 14 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 16 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 19 | * DEALINGS IN THE SOFTWARE. 20 | * 21 | *****************************************************************************/ 22 | 23 | 24 | #ifndef TCLAP_VERSION_VISITOR_H 25 | #define TCLAP_VERSION_VISITOR_H 26 | 27 | #include 28 | #include 29 | #include 30 | 31 | namespace TCLAP { 32 | 33 | /** 34 | * A Vistor that will call the version method of the given CmdLineOutput 35 | * for the specified CmdLine object and then exit. 36 | */ 37 | class VersionVisitor: public Visitor 38 | { 39 | private: 40 | /** 41 | * Prevent accidental copying 42 | */ 43 | VersionVisitor(const VersionVisitor& rhs); 44 | VersionVisitor& operator=(const VersionVisitor& rhs); 45 | 46 | protected: 47 | 48 | /** 49 | * The CmdLine of interest. 50 | */ 51 | CmdLineInterface* _cmd; 52 | 53 | /** 54 | * The output object. 55 | */ 56 | CmdLineOutput** _out; 57 | 58 | public: 59 | 60 | /** 61 | * Constructor. 62 | * \param cmd - The CmdLine the output is generated for. 63 | * \param out - The type of output. 64 | */ 65 | VersionVisitor( CmdLineInterface* cmd, CmdLineOutput** out ) 66 | : Visitor(), _cmd( cmd ), _out( out ) { } 67 | 68 | /** 69 | * Calls the version method of the output object using the 70 | * specified CmdLine. 71 | */ 72 | void visit() { 73 | (*_out)->version(*_cmd); 74 | throw ExitException(0); 75 | } 76 | 77 | }; 78 | 79 | } 80 | 81 | #endif 82 | -------------------------------------------------------------------------------- /core-packer/tclap/Visitor.h: -------------------------------------------------------------------------------- 1 | 2 | /****************************************************************************** 3 | * 4 | * file: Visitor.h 5 | * 6 | * Copyright (c) 2003, Michael E. Smoot . 7 | * All rights reverved. 8 | * 9 | * See the file COPYING in the top directory of this distribution for 10 | * more information. 11 | * 12 | * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS 13 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 14 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 15 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 16 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 17 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 18 | * DEALINGS IN THE SOFTWARE. 19 | * 20 | *****************************************************************************/ 21 | 22 | 23 | #ifndef TCLAP_VISITOR_H 24 | #define TCLAP_VISITOR_H 25 | 26 | namespace TCLAP { 27 | 28 | /** 29 | * A base class that defines the interface for visitors. 30 | */ 31 | class Visitor 32 | { 33 | public: 34 | 35 | /** 36 | * Constructor. Does nothing. 37 | */ 38 | Visitor() { } 39 | 40 | /** 41 | * Destructor. Does nothing. 42 | */ 43 | virtual ~Visitor() { } 44 | 45 | /** 46 | * Does nothing. Should be overridden by child. 47 | */ 48 | virtual void visit() { } 49 | }; 50 | 51 | } 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /core-packer/tclap/XorHandler.h: -------------------------------------------------------------------------------- 1 | 2 | /****************************************************************************** 3 | * 4 | * file: XorHandler.h 5 | * 6 | * Copyright (c) 2003, Michael E. Smoot . 7 | * Copyright (c) 2004, Michael E. Smoot, Daniel Aarno. 8 | * All rights reverved. 9 | * 10 | * See the file COPYING in the top directory of this distribution for 11 | * more information. 12 | * 13 | * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS 14 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 16 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 19 | * DEALINGS IN THE SOFTWARE. 20 | * 21 | *****************************************************************************/ 22 | 23 | #ifndef TCLAP_XORHANDLER_H 24 | #define TCLAP_XORHANDLER_H 25 | 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | namespace TCLAP { 33 | 34 | /** 35 | * This class handles lists of Arg's that are to be XOR'd on the command 36 | * line. This is used by CmdLine and you shouldn't ever use it. 37 | */ 38 | class XorHandler 39 | { 40 | protected: 41 | 42 | /** 43 | * The list of of lists of Arg's to be or'd together. 44 | */ 45 | std::vector< std::vector > _orList; 46 | 47 | public: 48 | 49 | /** 50 | * Constructor. Does nothing. 51 | */ 52 | XorHandler( ) : _orList(std::vector< std::vector >()) {} 53 | 54 | /** 55 | * Add a list of Arg*'s that will be orred together. 56 | * \param ors - list of Arg* that will be xor'd. 57 | */ 58 | void add( std::vector& ors ); 59 | 60 | /** 61 | * Checks whether the specified Arg is in one of the xor lists and 62 | * if it does match one, returns the size of the xor list that the 63 | * Arg matched. If the Arg matches, then it also sets the rest of 64 | * the Arg's in the list. You shouldn't use this. 65 | * \param a - The Arg to be checked. 66 | */ 67 | int check( const Arg* a ); 68 | 69 | /** 70 | * Returns the XOR specific short usage. 71 | */ 72 | std::string shortUsage(); 73 | 74 | /** 75 | * Prints the XOR specific long usage. 76 | * \param os - Stream to print to. 77 | */ 78 | void printLongUsage(std::ostream& os); 79 | 80 | /** 81 | * Simply checks whether the Arg is contained in one of the arg 82 | * lists. 83 | * \param a - The Arg to be checked. 84 | */ 85 | bool contains( const Arg* a ); 86 | 87 | std::vector< std::vector >& getXorList(); 88 | 89 | }; 90 | 91 | 92 | ////////////////////////////////////////////////////////////////////// 93 | //BEGIN XOR.cpp 94 | ////////////////////////////////////////////////////////////////////// 95 | inline void XorHandler::add( std::vector& ors ) 96 | { 97 | _orList.push_back( ors ); 98 | } 99 | 100 | inline int XorHandler::check( const Arg* a ) 101 | { 102 | // iterate over each XOR list 103 | for ( int i = 0; static_cast(i) < _orList.size(); i++ ) 104 | { 105 | // if the XOR list contains the arg.. 106 | ArgVectorIterator ait = std::find( _orList[i].begin(), 107 | _orList[i].end(), a ); 108 | if ( ait != _orList[i].end() ) 109 | { 110 | // first check to see if a mutually exclusive switch 111 | // has not already been set 112 | for ( ArgVectorIterator it = _orList[i].begin(); 113 | it != _orList[i].end(); 114 | it++ ) 115 | if ( a != (*it) && (*it)->isSet() ) 116 | throw(CmdLineParseException( 117 | "Mutually exclusive argument already set!", 118 | (*it)->toString())); 119 | 120 | // go through and set each arg that is not a 121 | for ( ArgVectorIterator it = _orList[i].begin(); 122 | it != _orList[i].end(); 123 | it++ ) 124 | if ( a != (*it) ) 125 | (*it)->xorSet(); 126 | 127 | // return the number of required args that have now been set 128 | if ( (*ait)->allowMore() ) 129 | return 0; 130 | else 131 | return static_cast(_orList[i].size()); 132 | } 133 | } 134 | 135 | if ( a->isRequired() ) 136 | return 1; 137 | else 138 | return 0; 139 | } 140 | 141 | inline bool XorHandler::contains( const Arg* a ) 142 | { 143 | for ( int i = 0; static_cast(i) < _orList.size(); i++ ) 144 | for ( ArgVectorIterator it = _orList[i].begin(); 145 | it != _orList[i].end(); 146 | it++ ) 147 | if ( a == (*it) ) 148 | return true; 149 | 150 | return false; 151 | } 152 | 153 | inline std::vector< std::vector >& XorHandler::getXorList() 154 | { 155 | return _orList; 156 | } 157 | 158 | 159 | 160 | ////////////////////////////////////////////////////////////////////// 161 | //END XOR.cpp 162 | ////////////////////////////////////////////////////////////////////// 163 | 164 | } //namespace TCLAP 165 | 166 | #endif 167 | -------------------------------------------------------------------------------- /core-packer/tclap/ZshCompletionOutput.h: -------------------------------------------------------------------------------- 1 | // -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*- 2 | 3 | /****************************************************************************** 4 | * 5 | * file: ZshCompletionOutput.h 6 | * 7 | * Copyright (c) 2006, Oliver Kiddle 8 | * All rights reverved. 9 | * 10 | * See the file COPYING in the top directory of this distribution for 11 | * more information. 12 | * 13 | * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS 14 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 16 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 19 | * DEALINGS IN THE SOFTWARE. 20 | * 21 | *****************************************************************************/ 22 | 23 | #ifndef TCLAP_ZSHCOMPLETIONOUTPUT_H 24 | #define TCLAP_ZSHCOMPLETIONOUTPUT_H 25 | 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | #include 33 | #include 34 | #include 35 | #include 36 | 37 | namespace TCLAP { 38 | 39 | /** 40 | * A class that generates a Zsh completion function as output from the usage() 41 | * method for the given CmdLine and its Args. 42 | */ 43 | class ZshCompletionOutput : public CmdLineOutput 44 | { 45 | 46 | public: 47 | 48 | ZshCompletionOutput(); 49 | 50 | /** 51 | * Prints the usage to stdout. Can be overridden to 52 | * produce alternative behavior. 53 | * \param c - The CmdLine object the output is generated for. 54 | */ 55 | virtual void usage(CmdLineInterface& c); 56 | 57 | /** 58 | * Prints the version to stdout. Can be overridden 59 | * to produce alternative behavior. 60 | * \param c - The CmdLine object the output is generated for. 61 | */ 62 | virtual void version(CmdLineInterface& c); 63 | 64 | /** 65 | * Prints (to stderr) an error message, short usage 66 | * Can be overridden to produce alternative behavior. 67 | * \param c - The CmdLine object the output is generated for. 68 | * \param e - The ArgException that caused the failure. 69 | */ 70 | virtual void failure(CmdLineInterface& c, 71 | ArgException& e ); 72 | 73 | protected: 74 | 75 | void basename( std::string& s ); 76 | void quoteSpecialChars( std::string& s ); 77 | 78 | std::string getMutexList( CmdLineInterface& _cmd, Arg* a ); 79 | void printOption( Arg* it, std::string mutex ); 80 | void printArg( Arg* it ); 81 | 82 | std::map common; 83 | char theDelimiter; 84 | }; 85 | 86 | ZshCompletionOutput::ZshCompletionOutput() 87 | : common(std::map()), 88 | theDelimiter('=') 89 | { 90 | common["host"] = "_hosts"; 91 | common["hostname"] = "_hosts"; 92 | common["file"] = "_files"; 93 | common["filename"] = "_files"; 94 | common["user"] = "_users"; 95 | common["username"] = "_users"; 96 | common["directory"] = "_directories"; 97 | common["path"] = "_directories"; 98 | common["url"] = "_urls"; 99 | } 100 | 101 | inline void ZshCompletionOutput::version(CmdLineInterface& _cmd) 102 | { 103 | std::cout << _cmd.getVersion() << std::endl; 104 | } 105 | 106 | inline void ZshCompletionOutput::usage(CmdLineInterface& _cmd ) 107 | { 108 | std::list argList = _cmd.getArgList(); 109 | std::string progName = _cmd.getProgramName(); 110 | std::string xversion = _cmd.getVersion(); 111 | theDelimiter = _cmd.getDelimiter(); 112 | basename(progName); 113 | 114 | std::cout << "#compdef " << progName << std::endl << std::endl << 115 | "# " << progName << " version " << _cmd.getVersion() << std::endl << std::endl << 116 | "_arguments -s -S"; 117 | 118 | for (ArgListIterator it = argList.begin(); it != argList.end(); it++) 119 | { 120 | if ( (*it)->shortID().at(0) == '<' ) 121 | printArg((*it)); 122 | else if ( (*it)->getFlag() != "-" ) 123 | printOption((*it), getMutexList(_cmd, *it)); 124 | } 125 | 126 | std::cout << std::endl; 127 | } 128 | 129 | inline void ZshCompletionOutput::failure( CmdLineInterface& _cmd, 130 | ArgException& e ) 131 | { 132 | static_cast(_cmd); // unused 133 | std::cout << e.what() << std::endl; 134 | } 135 | 136 | inline void ZshCompletionOutput::quoteSpecialChars( std::string& s ) 137 | { 138 | size_t idx = s.find_last_of(':'); 139 | while ( idx != std::string::npos ) 140 | { 141 | s.insert(idx, 1, '\\'); 142 | idx = s.find_last_of(':', idx); 143 | } 144 | idx = s.find_last_of('\''); 145 | while ( idx != std::string::npos ) 146 | { 147 | s.insert(idx, "'\\'"); 148 | if (idx == 0) 149 | idx = std::string::npos; 150 | else 151 | idx = s.find_last_of('\'', --idx); 152 | } 153 | } 154 | 155 | inline void ZshCompletionOutput::basename( std::string& s ) 156 | { 157 | size_t p = s.find_last_of('/'); 158 | if ( p != std::string::npos ) 159 | { 160 | s.erase(0, p + 1); 161 | } 162 | } 163 | 164 | inline void ZshCompletionOutput::printArg(Arg* a) 165 | { 166 | static int count = 1; 167 | 168 | std::cout << " \\" << std::endl << " '"; 169 | if ( a->acceptsMultipleValues() ) 170 | std::cout << '*'; 171 | else 172 | std::cout << count++; 173 | std::cout << ':'; 174 | if ( !a->isRequired() ) 175 | std::cout << ':'; 176 | 177 | std::cout << a->getName() << ':'; 178 | std::map::iterator compArg = common.find(a->getName()); 179 | if ( compArg != common.end() ) 180 | { 181 | std::cout << compArg->second; 182 | } 183 | else 184 | { 185 | std::cout << "_guard \"^-*\" " << a->getName(); 186 | } 187 | std::cout << '\''; 188 | } 189 | 190 | inline void ZshCompletionOutput::printOption(Arg* a, std::string mutex) 191 | { 192 | std::string flag = a->flagStartChar() + a->getFlag(); 193 | std::string name = a->nameStartString() + a->getName(); 194 | std::string desc = a->getDescription(); 195 | 196 | // remove full stop and capitalisation from description as 197 | // this is the convention for zsh function 198 | if (!desc.compare(0, 12, "(required) ")) 199 | { 200 | desc.erase(0, 12); 201 | } 202 | if (!desc.compare(0, 15, "(OR required) ")) 203 | { 204 | desc.erase(0, 15); 205 | } 206 | size_t len = desc.length(); 207 | if (len && desc.at(--len) == '.') 208 | { 209 | desc.erase(len); 210 | } 211 | if (len) 212 | { 213 | desc.replace(0, 1, 1, tolower(desc.at(0))); 214 | } 215 | 216 | std::cout << " \\" << std::endl << " '" << mutex; 217 | 218 | if ( a->getFlag().empty() ) 219 | { 220 | std::cout << name; 221 | } 222 | else 223 | { 224 | std::cout << "'{" << flag << ',' << name << "}'"; 225 | } 226 | if ( theDelimiter == '=' && a->isValueRequired() ) 227 | std::cout << "=-"; 228 | quoteSpecialChars(desc); 229 | std::cout << '[' << desc << ']'; 230 | 231 | if ( a->isValueRequired() ) 232 | { 233 | std::string arg = a->shortID(); 234 | arg.erase(0, arg.find_last_of(theDelimiter) + 1); 235 | if ( arg.at(arg.length()-1) == ']' ) 236 | arg.erase(arg.length()-1); 237 | if ( arg.at(arg.length()-1) == ']' ) 238 | { 239 | arg.erase(arg.length()-1); 240 | } 241 | if ( arg.at(0) == '<' ) 242 | { 243 | arg.erase(arg.length()-1); 244 | arg.erase(0, 1); 245 | } 246 | size_t p = arg.find('|'); 247 | if ( p != std::string::npos ) 248 | { 249 | do 250 | { 251 | arg.replace(p, 1, 1, ' '); 252 | } 253 | while ( (p = arg.find_first_of('|', p)) != std::string::npos ); 254 | quoteSpecialChars(arg); 255 | std::cout << ": :(" << arg << ')'; 256 | } 257 | else 258 | { 259 | std::cout << ':' << arg; 260 | std::map::iterator compArg = common.find(arg); 261 | if ( compArg != common.end() ) 262 | { 263 | std::cout << ':' << compArg->second; 264 | } 265 | } 266 | } 267 | 268 | std::cout << '\''; 269 | } 270 | 271 | inline std::string ZshCompletionOutput::getMutexList( CmdLineInterface& _cmd, Arg* a) 272 | { 273 | XorHandler xorHandler = _cmd.getXorHandler(); 274 | std::vector< std::vector > xorList = xorHandler.getXorList(); 275 | 276 | if (a->getName() == "help" || a->getName() == "version") 277 | { 278 | return "(-)"; 279 | } 280 | 281 | std::ostringstream list; 282 | if ( a->acceptsMultipleValues() ) 283 | { 284 | list << '*'; 285 | } 286 | 287 | for ( int i = 0; static_cast(i) < xorList.size(); i++ ) 288 | { 289 | for ( ArgVectorIterator it = xorList[i].begin(); 290 | it != xorList[i].end(); 291 | it++) 292 | if ( a == (*it) ) 293 | { 294 | list << '('; 295 | for ( ArgVectorIterator iu = xorList[i].begin(); 296 | iu != xorList[i].end(); 297 | iu++ ) 298 | { 299 | bool notCur = (*iu) != a; 300 | bool hasFlag = !(*iu)->getFlag().empty(); 301 | if ( iu != xorList[i].begin() && (notCur || hasFlag) ) 302 | list << ' '; 303 | if (hasFlag) 304 | list << (*iu)->flagStartChar() << (*iu)->getFlag() << ' '; 305 | if ( notCur || hasFlag ) 306 | list << (*iu)->nameStartString() << (*iu)->getName(); 307 | } 308 | list << ')'; 309 | return list.str(); 310 | } 311 | } 312 | 313 | // wasn't found in xor list 314 | if (!a->getFlag().empty()) { 315 | list << "(" << a->flagStartChar() << a->getFlag() << ' ' << 316 | a->nameStartString() << a->getName() << ')'; 317 | } 318 | 319 | return list.str(); 320 | } 321 | 322 | } //namespace TCLAP 323 | #endif 324 | -------------------------------------------------------------------------------- /core-packer/tea.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | **/ 3 | typedef unsigned int uint32_t; 4 | 5 | //#pragma code_seg(".peexe") 6 | void tea_encrypt (uint32_t* v, uint32_t* k) { 7 | uint32_t v0=v[0], v1=v[1], sum=0, i; /* set up */ 8 | uint32_t delta=0x9e3779b9; /* a key schedule constant */ 9 | uint32_t k0=k[0], k1=k[1], k2=k[2], k3=k[3]; /* cache key */ 10 | for (i=0; i < 32; i++) { /* basic cycle start */ 11 | sum += delta; 12 | v0 += ((v1<<4) + k0) ^ (v1 + sum) ^ ((v1>>5) + k1); 13 | v1 += ((v0<<4) + k2) ^ (v0 + sum) ^ ((v0>>5) + k3); 14 | } /* end cycle */ 15 | v[0]=v0; v[1]=v1; 16 | } 17 | 18 | #pragma section(".peexe32", read, execute) 19 | #pragma code_seg(".peexe32") 20 | void tea_decrypt_end_marker(void) 21 | { 22 | 23 | return; 24 | } 25 | 26 | #pragma code_seg(".peexe32") 27 | void tea_decrypt (uint32_t* v, uint32_t* k) { 28 | uint32_t v0=v[0], v1=v[1], sum=0xC6EF3720, i; /* set up */ 29 | uint32_t delta=0x9e3779b9; /* a key schedule constant */ 30 | uint32_t k0=k[0], k1=k[1], k2=k[2], k3=k[3]; /* cache key */ 31 | for (i=0; i<32; i++) { /* basic cycle start */ 32 | v1 -= ((v0<<4) + k2) ^ (v0 + sum) ^ ((v0>>5) + k3); 33 | v0 -= ((v1<<4) + k0) ^ (v1 + sum) ^ ((v1>>5) + k1); 34 | sum -= delta; 35 | } /* end cycle */ 36 | v[0]=v0; v[1]=v1; 37 | 38 | } 39 | -------------------------------------------------------------------------------- /core-packer/tea.h: -------------------------------------------------------------------------------- 1 | #ifndef __TEA_H_ 2 | #define __TEA_H_ 3 | 4 | typedef unsigned int uint32_t; 5 | 6 | /** 7 | * !tea_encrypt 8 | * Tiny Encryption Algorithm - encryption routine! 9 | **/ 10 | void tea_encrypt (uint32_t* v, uint32_t* k); 11 | 12 | /** 13 | * !tea_decrypt 14 | * Tiny Encryption Algorithm - decryption routine! 15 | **/ 16 | void tea_decrypt (uint32_t* v, uint32_t* k); 17 | void tea_decrypt_end_marker(void); // GENERIC MARKER! 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /core-packer/unpack32.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "library.h" 4 | #include "macro.h" 5 | #include "rva.h" 6 | #include "rc4.h" 7 | #include "symbols.h" 8 | #include "dll32.h" 9 | #include "tea.h" 10 | #include "patchutils.h" 11 | 12 | #ifdef _BUILD32 13 | // reloc table 14 | typedef struct _relocation_block { 15 | DWORD PageRVA; 16 | DWORD BlockSize; 17 | } relocation_block_t; 18 | 19 | typedef short relocation_entry; 20 | 21 | PIMAGE_SECTION_HEADER lookup_unpack_section(PIMAGE_DOS_HEADER pImageDosHeader, PIMAGE_NT_HEADERS32 pImageNtHeaders32) 22 | { 23 | short NumberOfSections = pImageNtHeaders32->FileHeader.NumberOfSections; 24 | 25 | PIMAGE_SECTION_HEADER pResult = NULL; 26 | 27 | for(PIMAGE_SECTION_HEADER pSection = IMAGE_FIRST_SECTION(pImageNtHeaders32); NumberOfSections > 0; NumberOfSections--, pSection++) 28 | { 29 | if (memcmp(".textbss", pSection->Name, 8) == 0) 30 | { 31 | std::cout << pSection->Name << "/32bit section found in code" << std::endl; 32 | 33 | std::cout << "\tSize of raw data: " << std::hex << pSection->SizeOfRawData << std::endl; 34 | std::cout << "\t Virtual size: " << std::hex << pSection->Misc.VirtualSize << std::endl; 35 | std::cout << "\t RVA: " << std::hex << pSection->VirtualAddress << std::endl; 36 | std::cout << "\t Virtual Address: " << std::hex << rva2addr(pImageDosHeader, pImageNtHeaders32, CALC_OFFSET(LPVOID, pImageDosHeader, pSection->VirtualAddress)) << std::endl; 37 | 38 | pResult = pSection; 39 | break; 40 | } 41 | } 42 | 43 | return pResult; 44 | } 45 | 46 | int unpack32(int argc, char *argv[]) 47 | { 48 | std::cout << "unpack32/ht " << std::endl; 49 | 50 | if (argc != 4) 51 | { 52 | std::cout << "unpack32 -u infile outfile" << std::endl; 53 | } 54 | 55 | // find patterns! 56 | PIMAGE_DOS_HEADER pInfectMe = (PIMAGE_DOS_HEADER) InternalLoadLibrary(argv[2], 0); 57 | PIMAGE_NT_HEADERS pInfectMeNtHeader = CALC_OFFSET(PIMAGE_NT_HEADERS, pInfectMe, pInfectMe->e_lfanew); 58 | 59 | PIMAGE_SECTION_HEADER pSectionInput = lookup_unpack_section(pInfectMe, pInfectMeNtHeader); 60 | 61 | if (pSectionInput == NULL) 62 | { 63 | std::cout << "Cannot find .textbss section" << std::endl; 64 | return -1; 65 | } 66 | 67 | PIMAGE_SECTION_HEADER pFirstSection = IMAGE_FIRST_SECTION(pInfectMeNtHeader); 68 | PIMAGE_SECTION_HEADER pLastSection = CALC_OFFSET(PIMAGE_SECTION_HEADER, pFirstSection, sizeof(IMAGE_SECTION_HEADER) * pInfectMeNtHeader->FileHeader.NumberOfSections); 69 | 70 | BYTE rc4sbox[256]; 71 | 72 | PIMAGE_IMPORT_DESCRIPTOR ImportAddressTable = CALC_OFFSET(PIMAGE_IMPORT_DESCRIPTOR, pInfectMe, pInfectMeNtHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress); 73 | 74 | PIMAGE_SECTION_HEADER pDestSection = NULL; 75 | 76 | LPBYTE ptrTextBss = (LPBYTE) rva2addr(pInfectMe, pInfectMeNtHeader, (LPVOID) pSectionInput->VirtualAddress); 77 | 78 | ULONG64 _rc4key0, _rc4key1; 79 | memcpy(&_rc4key1, CALC_OFFSET(LPVOID, ptrTextBss, 8), sizeof(ULONG64)); 80 | memcpy(&_rc4key0, CALC_OFFSET(LPVOID, ptrTextBss, 8 + sizeof(ULONG64)), sizeof(ULONG64)); 81 | 82 | ULONG64 rc4key[2] = { _rc4key0, _rc4key1 }; 83 | 84 | for(PIMAGE_SECTION_HEADER pProcessSection = IMAGE_FIRST_SECTION(pInfectMeNtHeader); pProcessSection < pLastSection; pProcessSection++) 85 | { // each section must be packed 86 | init_sbox(rc4sbox); 87 | init_sbox_key(rc4sbox, (BYTE *) rc4key, sizeof(rc4key)); 88 | 89 | if ((pProcessSection->Characteristics & IMAGE_SCN_MEM_SHARED) == IMAGE_SCN_MEM_SHARED) 90 | { // skip current section 91 | 92 | } 93 | else if ((pProcessSection->Characteristics & IMAGE_SCN_MEM_EXECUTE) == IMAGE_SCN_MEM_EXECUTE) 94 | { 95 | if (strcmp((char *) pProcessSection->Name, ".text") == 0) 96 | { 97 | HANDLE h = CreateFile(argv[2], GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL); 98 | 99 | SetFilePointer(h, 0x400, NULL, SEEK_SET); 100 | 101 | PIMAGE_SECTION_HEADER next = pProcessSection + 1; 102 | 103 | while(next->PointerToRawData == 0) 104 | { 105 | std::cout << "Skip " << next->Name << std::endl; 106 | next++; 107 | } 108 | 109 | DWORD dummy = 0; 110 | 111 | ReadFile(h, rva2addr(pInfectMe, pInfectMeNtHeader, (LPVOID) pProcessSection->VirtualAddress), next->PointerToRawData - 0x400, &dummy, NULL); 112 | CloseHandle(h); 113 | 114 | pProcessSection->SizeOfRawData = next->PointerToRawData - 0x400;; 115 | pProcessSection->PointerToRawData = 0x400; 116 | 117 | } 118 | 119 | cypher_msg(rc4sbox, (PBYTE) rva2addr(pInfectMe, pInfectMeNtHeader, (LPVOID) pProcessSection->VirtualAddress), pProcessSection->SizeOfRawData); 120 | 121 | } 122 | else if (memcmp(pProcessSection->Name, ".data", 5) == 0) 123 | { 124 | pProcessSection->Characteristics ^= 0x02; 125 | 126 | cypher_msg(rc4sbox, (PBYTE) rva2addr(pInfectMe, pInfectMeNtHeader, (LPVOID) pProcessSection->VirtualAddress), pProcessSection->SizeOfRawData); 127 | } 128 | else if (memcmp(pProcessSection->Name, ".rdata", 6) == 0) 129 | { 130 | pProcessSection->Characteristics ^= 0x03; 131 | 132 | DWORD sizeOfSection = 133 | pInfectMeNtHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress 134 | - pProcessSection->VirtualAddress 135 | - pInfectMeNtHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IAT].Size; 136 | 137 | LPVOID sectionAddress = rva2addr(pInfectMe, pInfectMeNtHeader, (LPVOID) (pProcessSection->VirtualAddress + pInfectMeNtHeader->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IAT].Size)); 138 | 139 | cypher_msg(rc4sbox, (PBYTE) sectionAddress, sizeOfSection); 140 | } 141 | 142 | } 143 | 144 | SaveLibraryToFile(pInfectMe, argv[3]); 145 | 146 | return 0; 147 | } 148 | #endif 149 | --------------------------------------------------------------------------------