From 2763fba8755e3ef4bc15eb5c347ea2f291c6736a Mon Sep 17 00:00:00 2001 From: tsepez Date: Tue, 24 Jan 2017 10:50:20 -0800 Subject: Replace some loose (ptr, len) pairs with CFX_ByteStringC in fpdfapi. These separate scalars are an anti-pattern given the ability to pass a single entity and later operate on it sensibly. Review-Url: https://codereview.chromium.org/2652033002 --- core/fpdfapi/page/cpdf_streamcontentparser.cpp | 59 +++++++++----------------- core/fpdfapi/page/cpdf_streamcontentparser.h | 6 +-- core/fpdfapi/page/cpdf_streamparser.cpp | 3 +- core/fpdfapi/page/cpdf_streamparser.h | 5 ++- 4 files changed, 27 insertions(+), 46 deletions(-) diff --git a/core/fpdfapi/page/cpdf_streamcontentparser.cpp b/core/fpdfapi/page/cpdf_streamcontentparser.cpp index 141442bb28..2426027976 100644 --- a/core/fpdfapi/page/cpdf_streamcontentparser.cpp +++ b/core/fpdfapi/page/cpdf_streamcontentparser.cpp @@ -316,18 +316,17 @@ int CPDF_StreamContentParser::GetNextParamPos() { return index; } -void CPDF_StreamContentParser::AddNameParam(const FX_CHAR* name, int len) { - CFX_ByteStringC bsName(name, len); +void CPDF_StreamContentParser::AddNameParam(const CFX_ByteStringC& bsName) { ContentParam& param = m_ParamBuf[GetNextParamPos()]; - if (len > 32) { + if (bsName.GetLength() > 32) { param.m_Type = ContentParam::OBJECT; param.m_pObject = pdfium::MakeUnique( m_pDocument->GetByteStringPool(), PDF_NameDecode(bsName)); } else { param.m_Type = ContentParam::NAME; if (bsName.Find('#') == -1) { - FXSYS_memcpy(param.m_Name.m_Buffer, name, len); - param.m_Name.m_Len = len; + FXSYS_memcpy(param.m_Name.m_Buffer, bsName.raw_str(), bsName.GetLength()); + param.m_Name.m_Len = bsName.GetLength(); } else { CFX_ByteString str = PDF_NameDecode(bsName); FXSYS_memcpy(param.m_Name.m_Buffer, str.c_str(), str.GetLength()); @@ -336,11 +335,10 @@ void CPDF_StreamContentParser::AddNameParam(const FX_CHAR* name, int len) { } } -void CPDF_StreamContentParser::AddNumberParam(const FX_CHAR* str, int len) { +void CPDF_StreamContentParser::AddNumberParam(const CFX_ByteStringC& str) { ContentParam& param = m_ParamBuf[GetNextParamPos()]; param.m_Type = ContentParam::NUMBER; - param.m_Number.m_bInteger = - FX_atonum(CFX_ByteStringC(str, len), ¶m.m_Number.m_Integer); + param.m_Number.m_bInteger = FX_atonum(str, ¶m.m_Number.m_Integer); } void CPDF_StreamContentParser::AddObjectParam( @@ -570,21 +568,10 @@ CPDF_StreamContentParser::InitializeOpCodes() { }); } -void CPDF_StreamContentParser::OnOperator(const FX_CHAR* op) { - int i = 0; - uint32_t opid = 0; - while (i < 4 && op[i]) { - opid = (opid << 8) + op[i]; - i++; - } - while (i < 4) { - opid <<= 8; - i++; - } - +void CPDF_StreamContentParser::OnOperator(const CFX_ByteStringC& op) { static const OpCodes s_OpCodes = InitializeOpCodes(); - auto it = s_OpCodes.find(opid); + auto it = s_OpCodes.find(op.GetID()); if (it != s_OpCodes.end()) (this->*it->second)(); } @@ -632,9 +619,7 @@ void CPDF_StreamContentParser::Handle_BeginImage() { while (1) { CPDF_StreamParser::SyntaxType type = m_pSyntax->ParseNextElement(); if (type == CPDF_StreamParser::Keyword) { - CFX_ByteString bsKeyword(m_pSyntax->GetWordBuf(), - m_pSyntax->GetWordSize()); - if (bsKeyword != "ID") { + if (m_pSyntax->GetWord() != "ID") { m_pSyntax->SetPos(savePos); return; } @@ -642,8 +627,7 @@ void CPDF_StreamContentParser::Handle_BeginImage() { if (type != CPDF_StreamParser::Name) { break; } - CFX_ByteString key((const FX_CHAR*)m_pSyntax->GetWordBuf() + 1, - m_pSyntax->GetWordSize() - 1); + CFX_ByteString key(m_pSyntax->GetWord().Mid(1)); auto pObj = m_pSyntax->ReadNextObject(false, 0); if (!key.IsEmpty()) { uint32_t dwObjNum = pObj ? pObj->GetObjNum() : 0; @@ -677,8 +661,7 @@ void CPDF_StreamContentParser::Handle_BeginImage() { if (type != CPDF_StreamParser::Keyword) { continue; } - if (m_pSyntax->GetWordSize() == 2 && m_pSyntax->GetWordBuf()[0] == 'E' && - m_pSyntax->GetWordBuf()[1] == 'I') { + if (m_pSyntax->GetWord() == "EI") { break; } } @@ -1561,15 +1544,14 @@ uint32_t CPDF_StreamContentParser::Parse(const uint8_t* pData, case CPDF_StreamParser::EndOfData: return m_pSyntax->GetPos(); case CPDF_StreamParser::Keyword: - OnOperator((char*)syntax.GetWordBuf()); + OnOperator(syntax.GetWord()); ClearAllParams(); break; case CPDF_StreamParser::Number: - AddNumberParam((char*)syntax.GetWordBuf(), syntax.GetWordSize()); + AddNumberParam(syntax.GetWord()); break; case CPDF_StreamParser::Name: - AddNameParam((const FX_CHAR*)syntax.GetWordBuf() + 1, - syntax.GetWordSize() - 1); + AddNameParam(syntax.GetWord().Mid(1)); break; default: AddObjectParam(syntax.GetObject()); @@ -1589,9 +1571,10 @@ void CPDF_StreamContentParser::ParsePathObject() { case CPDF_StreamParser::EndOfData: return; case CPDF_StreamParser::Keyword: { - int len = m_pSyntax->GetWordSize(); + CFX_ByteStringC strc = m_pSyntax->GetWord(); + int len = strc.GetLength(); if (len == 1) { - switch (m_pSyntax->GetWordBuf()[0]) { + switch (strc[0]) { case kPathOperatorSubpath: AddPathPoint(params[0], params[1], FXPT_MOVETO); nParams = 0; @@ -1627,8 +1610,8 @@ void CPDF_StreamContentParser::ParsePathObject() { break; } } else if (len == 2) { - if (m_pSyntax->GetWordBuf()[0] == kPathOperatorRectangle[0] && - m_pSyntax->GetWordBuf()[1] == kPathOperatorRectangle[1]) { + if (strc[0] == kPathOperatorRectangle[0] && + strc[1] == kPathOperatorRectangle[1]) { AddPathRect(params[0], params[1], params[2], params[3]); nParams = 0; } else { @@ -1647,9 +1630,7 @@ void CPDF_StreamContentParser::ParsePathObject() { break; int value; - bool bInteger = FX_atonum( - CFX_ByteStringC(m_pSyntax->GetWordBuf(), m_pSyntax->GetWordSize()), - &value); + bool bInteger = FX_atonum(m_pSyntax->GetWord(), &value); params[nParams++] = bInteger ? (FX_FLOAT)value : *(FX_FLOAT*)&value; break; } diff --git a/core/fpdfapi/page/cpdf_streamcontentparser.h b/core/fpdfapi/page/cpdf_streamcontentparser.h index 6429b02bbd..e23ae8ea99 100644 --- a/core/fpdfapi/page/cpdf_streamcontentparser.h +++ b/core/fpdfapi/page/cpdf_streamcontentparser.h @@ -77,16 +77,16 @@ class CPDF_StreamContentParser { std::unordered_map; static OpCodes InitializeOpCodes(); - void AddNumberParam(const FX_CHAR* str, int len); + void AddNameParam(const CFX_ByteStringC& str); + void AddNumberParam(const CFX_ByteStringC& str); void AddObjectParam(std::unique_ptr pObj); - void AddNameParam(const FX_CHAR* name, int size); int GetNextParamPos(); void ClearAllParams(); CPDF_Object* GetObject(uint32_t index); CFX_ByteString GetString(uint32_t index); FX_FLOAT GetNumber(uint32_t index); int GetInteger(uint32_t index) { return (int32_t)(GetNumber(index)); } - void OnOperator(const FX_CHAR* op); + void OnOperator(const CFX_ByteStringC& op); void AddTextObject(CFX_ByteString* pText, FX_FLOAT fInitKerning, FX_FLOAT* pKerning, diff --git a/core/fpdfapi/page/cpdf_streamparser.cpp b/core/fpdfapi/page/cpdf_streamparser.cpp index e26de605b0..14efba2200 100644 --- a/core/fpdfapi/page/cpdf_streamparser.cpp +++ b/core/fpdfapi/page/cpdf_streamparser.cpp @@ -209,8 +209,7 @@ std::unique_ptr CPDF_StreamParser::ReadInlineStream( dwStreamSize += m_Pos - dwPrevPos; continue; } - if (GetWordSize() == 2 && GetWordBuf()[0] == 'E' && - GetWordBuf()[1] == 'I') { + if (GetWord() == "EI") { m_Pos = dwPrevPos; break; } diff --git a/core/fpdfapi/page/cpdf_streamparser.h b/core/fpdfapi/page/cpdf_streamparser.h index ce01dd04ee..dc3b7dcb40 100644 --- a/core/fpdfapi/page/cpdf_streamparser.h +++ b/core/fpdfapi/page/cpdf_streamparser.h @@ -28,8 +28,9 @@ class CPDF_StreamParser { ~CPDF_StreamParser(); SyntaxType ParseNextElement(); - const uint8_t* GetWordBuf() const { return m_WordBuffer; } - uint32_t GetWordSize() const { return m_WordSize; } + CFX_ByteStringC GetWord() const { + return CFX_ByteStringC(m_WordBuffer, m_WordSize); + } uint32_t GetPos() const { return m_Pos; } void SetPos(uint32_t pos) { m_Pos = pos; } std::unique_ptr GetObject() { return std::move(m_pLastObj); } -- cgit v1.2.3