Skip to content

Commit

Permalink
Merge pull request 'Fix hwp bugs' (#209) from fix/hwp into release/v8…
Browse files Browse the repository at this point in the history
  • Loading branch information
K0R0L committed Jan 31, 2025
2 parents 81a4d9c + 90bcd47 commit 1e8d220
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 10 deletions.
2 changes: 1 addition & 1 deletion HwpFile/HWPFile.pro
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ PWD_ROOT_DIR = $$PWD

include($$CORE_ROOT_DIR/Common/base.pri)

ADD_DEPENDENCY(kernel, UnicodeConverter, graphics)
ADD_DEPENDENCY(kernel, UnicodeConverter, graphics, CryptoPPLib)

DEFINES += HWPFILE_USE_DYNAMIC_LIBRARY

Expand Down
7 changes: 4 additions & 3 deletions HwpFile/HwpDoc/Conversion/Converter2OOXML.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1561,10 +1561,11 @@ void CConverter2OOXML::WriteRunnerStyle(short shCharShapeID, NSStringUtils::CStr
if (!bStrike && pCharShape->StrikeOut())
oBuilder.WriteString(L"<w:strike/>");

double dSpacing = ((double)pCharShape->GetHeight() / 100.) * ((double)pCharShape->GetSpacing(ELang::HANGUL) / 100) * 0.8 + 0.4;
dSpacing *= 20; // pt to twips (20 = 1440 / 72)
//TODO:: на данный момент вычисляется не правильно. Необходимо более точно разобраться
// double dSpacing = ((double)pCharShape->GetHeight() / 100.) * ((double)pCharShape->GetSpacing(ELang::HANGUL) / 100) * 0.8 + 0.4;
// dSpacing *= 20; // pt to twips (20 = 1440 / 72)

oBuilder.WriteString(L"<w:spacing w:val=\"" + std::to_wstring((int)std::round(dSpacing)) + L"\"/>");
// oBuilder.WriteString(L"<w:spacing w:val=\"" + std::to_wstring((int)std::round(dSpacing)) + L"\"/>");

if (nullptr != oState.m_pHighlightColor)
oBuilder.WriteString(L"<w:highlight w:val=\"" + ConvertIntRgbToStr(*oState.m_pHighlightColor) + L"\"/>");
Expand Down
47 changes: 44 additions & 3 deletions HwpFile/HwpDoc/HWPFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
#include "../OfficeUtils/src/OfficeUtils.h"
#include "../DesktopEditor/common/Directory.h"

// For decrypt
#include "../../Common/3dParty/cryptopp/modes.h"
#include "../../Common/3dParty/cryptopp/aes.h"
#include "../../Common/3dParty/cryptopp/filters.h"
// ----------

#define DEFAULT_BUFFER_SIZE 8096

namespace HWP
Expand Down Expand Up @@ -67,7 +73,7 @@ bool CHWPFile::Open()
if (!m_oFileHeader.Distributable() && !GetBodyText(m_nVersion))
return false;

if (!m_oFileHeader.Distributable() && !GetViewText(m_nVersion))
if (m_oFileHeader.Distributable() && !GetViewText(m_nVersion))
return false;

return true;
Expand Down Expand Up @@ -292,8 +298,43 @@ bool CHWPFile::Decrypt(CHWPStream& oInput, CHWPStream& oBuffer)
if (256 != nSize)
return false;

//TODO:: реализовать
return false;
CHWPStream oDocData(256);
oDocData.Copy(oInput, 256);
oInput.Skip(256);

int nSeed;
oDocData.ReadInt(nSeed);
oDocData.Skip(-4);

srand(nSeed);

unsigned char chKey;

for (unsigned int unIndex = 0, unCount = 0; unIndex < 256; ++unIndex)
{
if (0 == unCount)
{
chKey = rand() & 0xFF;
unCount = (rand() & 0xF) + 1;
}
if (unIndex >= 4)
*(oDocData.GetCurPtr() + unIndex) = oDocData[unIndex] ^ chKey;

--unCount;
}

int nHashOffset = (nSeed & 0x0f) + 4;

oBuffer.Expand(oInput.SizeToEnd());

using namespace CryptoPP;

ECB_Mode<AES>::Decryption oDecryptor;
oDecryptor.SetKey((byte*)(oDocData.GetCurPtr() + nHashOffset), 16);

ArraySource((byte*)oInput.GetCurPtr(), oInput.SizeToEnd(), true, new StreamTransformationFilter(oDecryptor, new ArraySink( (byte*)oBuffer.GetCurPtr(), oBuffer.GetSize()), StreamTransformationFilter::NO_PADDING));

return true;
}

bool CHWPFile::GetBodyText(int nVersion)
Expand Down
6 changes: 3 additions & 3 deletions HwpFile/HwpDoc/HWPSection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -529,8 +529,6 @@ int CHWPSection::ParseCtrlRecurse(CCtrl* pCurrCtrl, int nRunLevel, CHWPStream& o
ParseRecurse(pNewPara, nLevel, oBuffer, 0, nVersion);
}
else if (ECtrlObjectType::Shape == pCtrl->GetCtrlType())
// else if (nullptr != dynamic_cast<CCtrlShapeRect*>(pCtrl) ||
// nullptr != dynamic_cast<CCtrlGeneralShape*>(pCtrl))
{
CCtrlCommon* pCtrlCommon = (CCtrlCommon*)pCtrl;
oBuffer.Skip(-6);
Expand Down Expand Up @@ -573,7 +571,6 @@ int CHWPSection::ParseCtrlRecurse(CCtrl* pCurrCtrl, int nRunLevel, CHWPStream& o
case HWPTAG_SHAPE_COMPONENT_TEXTART:
case HWPTAG_SHAPE_COMPONENT_UNKNOWN:
{
// if (nullptr != dynamic_cast<CCtrlGeneralShape*>(pCtrl))
if (ECtrlObjectType::Shape == pCtrl->GetCtrlType())
ParseCtrlRecurse((CCtrlGeneralShape*)pCtrl, nLevel, oBuffer, 0, nVersion);
else
Expand Down Expand Up @@ -856,6 +853,8 @@ int CHWPSection::ParseCtrlRecurse(CCtrl* pCurrCtrl, int nRunLevel, CHWPStream& o
}
}

oBuffer.RemoveLastSavedPos();

return 0;
}

Expand Down Expand Up @@ -895,6 +894,7 @@ int CHWPSection::ParseContainerRecurse(CCtrlContainer* pContainer, int nRunLevel
while (oBuffer.CanRead())
{
oBuffer.ReadInt(nHeader);
oBuffer.Skip(-4);
nTagNum = nHeader & 0x3FF; // 10 bits (0 - 9 bit)
nLevel = (nHeader & 0xFFC00) >> 10; // 10 bits (10-19 bit)
nSize = (nHeader & 0xFFF00000) >> 20; // 12 bits (20-31 bit)
Expand Down
5 changes: 5 additions & 0 deletions HwpFile/HwpDoc/HWPStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ void CHWPStream::Clear()
m_arSavedPositions.pop();
}

void CHWPStream::Copy(CHWPStream& oStream, unsigned long ulSize)
{
memcpy(m_pCur, oStream.GetCurPtr(), (std::min)(SizeToEnd(), (unsigned long)(std::min)(ulSize, oStream.SizeToEnd())));
}

void CHWPStream::SetStream(HWP_BYTE* pBuffer, unsigned long ulSize, bool bExternalBuffer)
{
m_pBegin = pBuffer;
Expand Down
1 change: 1 addition & 0 deletions HwpFile/HwpDoc/HWPStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class CHWPStream
~CHWPStream();

void Clear();
void Copy(CHWPStream& oStream, unsigned long ulSize);

void SetStream(HWP_BYTE* pBuffer, unsigned long ulSize, bool bExternalBuffer = true);

Expand Down

0 comments on commit 1e8d220

Please sign in to comment.