00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
#ifndef TLSDIRECTORY_H
00014
#define TLSDIRECTORY_H
00015
00016
namespace PeLib
00017 {
00019
00022
template<
int bits>
00023 class TlsDirectory
00024 {
00025
private:
00026 PELIB_IMAGE_TLS_DIRECTORY<bits>
m_tls;
00027
00028
public:
00030
int read(
const std::string& strFilename,
unsigned int uiOffset,
unsigned int uiSize);
00032
void rebuild(std::vector<byte>& vBuffer)
const;
00034
unsigned int size() const;
00036
int write(const std::string& strFilename,
unsigned int dwOffset) const;
00037
00039 dword getStartAddressOfRawData() const;
00041 dword getEndAddressOfRawData() const;
00043 dword getAddressOfIndex() const;
00045 dword getAddressOfCallBacks() const;
00047 dword getSizeOfZeroFill() const;
00049 dword getCharacteristics() const;
00050
00052
void setStartAddressOfRawData(dword dwValue);
00054
void setEndAddressOfRawData(dword dwValue);
00056
void setAddressOfIndex(dword dwValue);
00058
void setAddressOfCallBacks(dword dwValue);
00060
void setSizeOfZeroFill(dword dwValue);
00062
void setCharacteristics(dword dwValue);
00063 };
00070 template<
int bits>
00071 int TlsDirectory<bits>::read(const std::string& strFilename,
unsigned int uiOffset,
unsigned int uiSize)
00072 {
00073
00074
if (uiSize < PELIB_IMAGE_TLS_DIRECTORY<bits>::size)
00075 {
00076
00077
return 1;
00078 }
00079
00080 std::ifstream ifFile(strFilename.c_str(), std::ios::binary);
00081
unsigned int ulFileSize = fileSize(ifFile);
00082
00083
if (!ifFile)
00084 {
00085
00086
return 1;
00087 }
00088
00089
if (ulFileSize < uiOffset + uiSize)
00090 {
00091
00092
return 1;
00093 }
00094
00095 ifFile.seekg(uiOffset, std::ios::beg);
00096
00097 std::vector<byte> vTlsDirectory(uiSize);
00098 ifFile.read(reinterpret_cast<char*>(&vTlsDirectory[0]), uiSize);
00099
00100 InputBuffer ibBuffer(vTlsDirectory);
00101
00102 PELIB_IMAGE_TLS_DIRECTORY<bits> itdCurr;
00103
00104 ibBuffer >> itdCurr.StartAddressOfRawData;
00105 ibBuffer >> itdCurr.EndAddressOfRawData;
00106 ibBuffer >> itdCurr.AddressOfIndex;
00107 ibBuffer >> itdCurr.AddressOfCallBacks;
00108 ibBuffer >> itdCurr.SizeOfZeroFill;
00109 ibBuffer >> itdCurr.Characteristics;
00110
00111 std::swap(itdCurr,
m_tls);
00112
00113
return 0;
00114 }
00115
00120
template<
int bits>
00121 void TlsDirectory<bits>::rebuild(std::vector<byte>& vBuffer)
const
00122
{
00123 OutputBuffer obBuffer(vBuffer);
00124
00125 obBuffer <<
m_tls.StartAddressOfRawData;
00126 obBuffer <<
m_tls.EndAddressOfRawData;
00127 obBuffer <<
m_tls.AddressOfIndex;
00128 obBuffer <<
m_tls.AddressOfCallBacks;
00129 obBuffer <<
m_tls.SizeOfZeroFill;
00130 obBuffer <<
m_tls.Characteristics;
00131 }
00132
00138
template<
int bits>
00139 unsigned int TlsDirectory<bits>::size()
const
00140
{
00141
return PELIB_IMAGE_TLS_DIRECTORY<bits>::size();
00142 }
00143
00148
template<
int bits>
00149 int TlsDirectory<bits>::write(
const std::string& strFilename,
unsigned int dwOffset)
const
00150
{
00151 std::fstream ofFile(strFilename.c_str(), std::ios::in | std::ios::out | std::ios::binary);
00152
00153
if (!ofFile)
00154 {
00155 ofFile.clear();
00156 ofFile.open(strFilename.c_str(), std::ios_base::binary | std::ios_base::in | std::ios_base::out | std::ios_base::trunc);
00157
if (!ofFile)
00158 {
00159
return 1;
00160
00161 }
00162 }
00163
00164 ofFile.seekp(dwOffset, std::ios::beg);
00165
00166 std::vector<unsigned char> vBuffer;
00167 rebuild(vBuffer);
00168
00169 ofFile.write(reinterpret_cast<const char*>(&vBuffer[0]), vBuffer.size());
00170
00171 ofFile.close();
00172
00173
return 0;
00174 }
00175
00179
template<
int bits>
00180 dword
TlsDirectory<bits>::getStartAddressOfRawData()
const
00181
{
00182
return m_tls.StartAddressOfRawData;
00183 }
00184
00188
template<
int bits>
00189 dword
TlsDirectory<bits>::getEndAddressOfRawData()
const
00190
{
00191
return m_tls.EndAddressOfRawData;
00192 }
00193
00197
template<
int bits>
00198 dword
TlsDirectory<bits>::getAddressOfIndex()
const
00199
{
00200
return m_tls.AddressOfIndex;
00201 }
00202
00206
template<
int bits>
00207 dword
TlsDirectory<bits>::getAddressOfCallBacks()
const
00208
{
00209
return m_tls.AddressOfCallBacks;
00210 }
00211
00215
template<
int bits>
00216 dword
TlsDirectory<bits>::getSizeOfZeroFill()
const
00217
{
00218
return m_tls.SizeOfZeroFill;
00219 }
00220
00224
template<
int bits>
00225 dword
TlsDirectory<bits>::getCharacteristics()
const
00226
{
00227
return m_tls.Characteristics;
00228 }
00229
00233
template<
int bits>
00234 void TlsDirectory<bits>::setStartAddressOfRawData(dword dwValue)
00235 {
00236
m_tls.StartAddressOfRawData = dwValue;
00237 }
00238
00242
template<
int bits>
00243 void TlsDirectory<bits>::setEndAddressOfRawData(dword dwValue)
00244 {
00245
m_tls.EndAddressOfRawData = dwValue;
00246 }
00247
00251
template<
int bits>
00252 void TlsDirectory<bits>::setAddressOfIndex(dword dwValue)
00253 {
00254
m_tls.AddressOfIndex = dwValue;
00255 }
00256
00260
template<
int bits>
00261 void TlsDirectory<bits>::setAddressOfCallBacks(dword dwValue)
00262 {
00263
m_tls.AddressOfCallBacks = dwValue;
00264 }
00265
00269
template<
int bits>
00270 void TlsDirectory<bits>::setSizeOfZeroFill(dword dwValue)
00271 {
00272
m_tls.SizeOfZeroFill = dwValue;
00273 }
00274
00278
template<
int bits>
00279 void TlsDirectory<bits>::setCharacteristics(dword dwValue)
00280 {
00281
m_tls.Characteristics = dwValue;
00282 }
00283
00284 }
00285
#endif