summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLei Zhang <thestig@chromium.org>2018-10-24 17:26:50 +0000
committerChromium commit bot <commit-bot@chromium.org>2018-10-24 17:26:50 +0000
commit2237541071da7f309c16b3033daa4c6be170dca4 (patch)
treea468618a44666558fae3432ece2d3439a0ac2537
parentd86805176f390e0fec1802aae7dbbf1d1d9f53b0 (diff)
downloadpdfium-2237541071da7f309c16b3033daa4c6be170dca4.tar.xz
Check CPDFFooObjectFromFPDFPageObject() result.
Instead of checking the FPDF_OBJECT itself. Remove redundant checks and fix nits. Change-Id: Id566847d39d16331c08f0b5f3b46431716717b5c Reviewed-on: https://pdfium-review.googlesource.com/c/44535 Reviewed-by: Tom Sepez <tsepez@chromium.org> Commit-Queue: Lei Zhang <thestig@chromium.org>
-rw-r--r--fpdfsdk/fpdf_editimg.cpp63
-rw-r--r--fpdfsdk/fpdf_editpage.cpp110
-rw-r--r--fpdfsdk/fpdf_editpath.cpp8
-rw-r--r--fpdfsdk/fpdf_edittext.cpp9
4 files changed, 85 insertions, 105 deletions
diff --git a/fpdfsdk/fpdf_editimg.cpp b/fpdfsdk/fpdf_editimg.cpp
index be7e571afe..af4d125db0 100644
--- a/fpdfsdk/fpdf_editimg.cpp
+++ b/fpdfsdk/fpdf_editimg.cpp
@@ -51,17 +51,22 @@ RetainPtr<IFX_SeekableReadStream> MakeSeekableReadStream(
return pdfium::MakeRetain<CPDFSDK_CustomAccess>(pFileAccess);
}
+CPDF_ImageObject* CPDFImageObjectFromFPDFPageObject(
+ FPDF_PAGEOBJECT image_object) {
+ CPDF_PageObject* pPageObject = CPDFPageObjectFromFPDFPageObject(image_object);
+ return pPageObject ? pPageObject->AsImage() : nullptr;
+}
+
bool LoadJpegHelper(FPDF_PAGE* pages,
int nCount,
FPDF_PAGEOBJECT image_object,
FPDF_FILEACCESS* fileAccess,
bool inlineJpeg) {
- if (!image_object || !fileAccess)
+ CPDF_ImageObject* pImgObj = CPDFImageObjectFromFPDFPageObject(image_object);
+ if (!pImgObj)
return false;
- CPDF_ImageObject* pImgObj =
- CPDFPageObjectFromFPDFPageObject(image_object)->AsImage();
- if (!pImgObj)
+ if (!fileAccess)
return false;
if (pages) {
@@ -120,11 +125,7 @@ FPDFImageObj_SetMatrix(FPDF_PAGEOBJECT image_object,
double d,
double e,
double f) {
- if (!image_object)
- return false;
-
- CPDF_ImageObject* pImgObj =
- CPDFPageObjectFromFPDFPageObject(image_object)->AsImage();
+ CPDF_ImageObject* pImgObj = CPDFImageObjectFromFPDFPageObject(image_object);
if (!pImgObj)
return false;
@@ -141,14 +142,16 @@ FPDFImageObj_SetBitmap(FPDF_PAGE* pages,
int nCount,
FPDF_PAGEOBJECT image_object,
FPDF_BITMAP bitmap) {
- if (!image_object || !bitmap || !pages)
+ if (!pages)
return false;
- CPDF_ImageObject* pImgObj =
- CPDFPageObjectFromFPDFPageObject(image_object)->AsImage();
+ CPDF_ImageObject* pImgObj = CPDFImageObjectFromFPDFPageObject(image_object);
if (!pImgObj)
return false;
+ if (!bitmap)
+ return false;
+
for (int index = 0; index < nCount; index++) {
CPDF_Page* pPage = CPDFPageFromFPDFPage(pages[index]);
if (pPage)
@@ -163,11 +166,11 @@ FPDFImageObj_SetBitmap(FPDF_PAGE* pages,
FPDF_EXPORT FPDF_BITMAP FPDF_CALLCONV
FPDFImageObj_GetBitmap(FPDF_PAGEOBJECT image_object) {
- CPDF_PageObject* pObj = CPDFPageObjectFromFPDFPageObject(image_object);
- if (!pObj || !pObj->IsImage())
+ CPDF_ImageObject* pImgObj = CPDFImageObjectFromFPDFPageObject(image_object);
+ if (!pImgObj)
return nullptr;
- RetainPtr<CPDF_Image> pImg = pObj->AsImage()->GetImage();
+ RetainPtr<CPDF_Image> pImg = pImgObj->GetImage();
if (!pImg)
return nullptr;
@@ -192,11 +195,11 @@ FPDF_EXPORT unsigned long FPDF_CALLCONV
FPDFImageObj_GetImageDataDecoded(FPDF_PAGEOBJECT image_object,
void* buffer,
unsigned long buflen) {
- CPDF_PageObject* pObj = CPDFPageObjectFromFPDFPageObject(image_object);
- if (!pObj || !pObj->IsImage())
+ CPDF_ImageObject* pImgObj = CPDFImageObjectFromFPDFPageObject(image_object);
+ if (!pImgObj)
return 0;
- RetainPtr<CPDF_Image> pImg = pObj->AsImage()->GetImage();
+ RetainPtr<CPDF_Image> pImg = pImgObj->GetImage();
if (!pImg)
return 0;
@@ -211,11 +214,11 @@ FPDF_EXPORT unsigned long FPDF_CALLCONV
FPDFImageObj_GetImageDataRaw(FPDF_PAGEOBJECT image_object,
void* buffer,
unsigned long buflen) {
- CPDF_PageObject* pObj = CPDFPageObjectFromFPDFPageObject(image_object);
- if (!pObj || !pObj->IsImage())
+ CPDF_ImageObject* pImgObj = CPDFImageObjectFromFPDFPageObject(image_object);
+ if (!pImgObj)
return 0;
- RetainPtr<CPDF_Image> pImg = pObj->AsImage()->GetImage();
+ RetainPtr<CPDF_Image> pImg = pImgObj->GetImage();
if (!pImg)
return 0;
@@ -236,11 +239,11 @@ FPDFImageObj_GetImageDataRaw(FPDF_PAGEOBJECT image_object,
FPDF_EXPORT int FPDF_CALLCONV
FPDFImageObj_GetImageFilterCount(FPDF_PAGEOBJECT image_object) {
- CPDF_PageObject* pObj = CPDFPageObjectFromFPDFPageObject(image_object);
- if (!pObj || !pObj->IsImage())
+ CPDF_ImageObject* pImgObj = CPDFImageObjectFromFPDFPageObject(image_object);
+ if (!pImgObj)
return 0;
- RetainPtr<CPDF_Image> pImg = pObj->AsImage()->GetImage();
+ RetainPtr<CPDF_Image> pImg = pImgObj->GetImage();
if (!pImg)
return 0;
@@ -284,23 +287,23 @@ FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
FPDFImageObj_GetImageMetadata(FPDF_PAGEOBJECT image_object,
FPDF_PAGE page,
FPDF_IMAGEOBJ_METADATA* metadata) {
- CPDF_PageObject* pObj = CPDFPageObjectFromFPDFPageObject(image_object);
- if (!pObj || !pObj->IsImage() || !metadata)
+ CPDF_ImageObject* pImgObj = CPDFImageObjectFromFPDFPageObject(image_object);
+ if (!pImgObj || !metadata)
return false;
- RetainPtr<CPDF_Image> pImg = pObj->AsImage()->GetImage();
+ RetainPtr<CPDF_Image> pImg = pImgObj->GetImage();
if (!pImg)
return false;
- metadata->marked_content_id = pObj->m_ContentMarks.GetMarkedContentID();
+ metadata->marked_content_id = pImgObj->m_ContentMarks.GetMarkedContentID();
const int nPixelWidth = pImg->GetPixelWidth();
const int nPixelHeight = pImg->GetPixelHeight();
metadata->width = nPixelWidth;
metadata->height = nPixelHeight;
- const float nWidth = pObj->GetRect().Width();
- const float nHeight = pObj->GetRect().Height();
+ const float nWidth = pImgObj->GetRect().Width();
+ const float nHeight = pImgObj->GetRect().Height();
constexpr int nPointsPerInch = 72;
if (nWidth != 0 && nHeight != 0) {
metadata->horizontal_dpi = nPixelWidth / nWidth * nPointsPerInch;
diff --git a/fpdfsdk/fpdf_editpage.cpp b/fpdfsdk/fpdf_editpage.cpp
index 585e7e1740..d0ff962252 100644
--- a/fpdfsdk/fpdf_editpage.cpp
+++ b/fpdfsdk/fpdf_editpage.cpp
@@ -137,13 +137,14 @@ unsigned int GetUnsignedAlpha(float alpha) {
return static_cast<unsigned int>(alpha * 255.f + 0.5f);
}
-const CPDF_PageObjectList* CPDFPageObjListFromFPDFFormObject(
- FPDF_PAGEOBJECT page_object) {
+CPDF_FormObject* CPDFFormObjectFromFPDFPageObject(FPDF_PAGEOBJECT page_object) {
auto* pPageObj = CPDFPageObjectFromFPDFPageObject(page_object);
- if (!pPageObj)
- return nullptr;
+ return pPageObj ? pPageObj->AsForm() : nullptr;
+}
- CPDF_FormObject* pFormObject = pPageObj->AsForm();
+const CPDF_PageObjectList* CPDFPageObjListFromFPDFFormObject(
+ FPDF_PAGEOBJECT page_object) {
+ CPDF_FormObject* pFormObject = CPDFFormObjectFromFPDFPageObject(page_object);
if (!pFormObject)
return nullptr;
@@ -250,8 +251,8 @@ FPDF_EXPORT void FPDF_CALLCONV FPDFPage_InsertObject(FPDF_PAGE page,
CPDF_Page* pPage = CPDFPageFromFPDFPage(page);
if (!IsPageObject(pPage))
return;
- pPageObj->SetDirty(true);
+ pPageObj->SetDirty(true);
pPage->AppendPageObject(std::move(pPageObjHolder));
CalcBoundingBox(pPageObj);
}
@@ -297,24 +298,24 @@ FPDF_EXPORT void FPDF_CALLCONV FPDFPageObj_Destroy(FPDF_PAGEOBJECT page_obj) {
FPDF_EXPORT int FPDF_CALLCONV
FPDFPageObj_CountMarks(FPDF_PAGEOBJECT page_object) {
- if (!page_object)
+ CPDF_PageObject* pPageObj = CPDFPageObjectFromFPDFPageObject(page_object);
+ if (!pPageObj)
return -1;
- const auto& mark =
- CPDFPageObjectFromFPDFPageObject(page_object)->m_ContentMarks;
- return mark.CountItems();
+ return pPageObj->m_ContentMarks.CountItems();
}
FPDF_EXPORT FPDF_PAGEOBJECTMARK FPDF_CALLCONV
FPDFPageObj_GetMark(FPDF_PAGEOBJECT page_object, unsigned long index) {
- if (!page_object)
+ CPDF_PageObject* pPageObj = CPDFPageObjectFromFPDFPageObject(page_object);
+ if (!pPageObj)
return nullptr;
- auto* mark = &CPDFPageObjectFromFPDFPageObject(page_object)->m_ContentMarks;
- if (index >= mark->CountItems())
+ auto& mark = pPageObj->m_ContentMarks;
+ if (index >= mark.CountItems())
return nullptr;
- return FPDFPageObjectMarkFromCPDFContentMarkItem(mark->GetItem(index));
+ return FPDFPageObjectMarkFromCPDFContentMarkItem(mark.GetItem(index));
}
FPDF_EXPORT FPDF_PAGEOBJECTMARK FPDF_CALLCONV
@@ -323,13 +324,11 @@ FPDFPageObj_AddMark(FPDF_PAGEOBJECT page_object, FPDF_BYTESTRING name) {
if (!pPageObj)
return nullptr;
- auto* mark = &pPageObj->m_ContentMarks;
- mark->AddMark(name);
- unsigned long index = mark->CountItems() - 1;
-
+ auto& mark = pPageObj->m_ContentMarks;
+ mark.AddMark(name);
+ unsigned long index = mark.CountItems() - 1;
pPageObj->SetDirty(true);
-
- return FPDFPageObjectMarkFromCPDFContentMarkItem(mark->GetItem(index));
+ return FPDFPageObjectMarkFromCPDFContentMarkItem(mark.GetItem(index));
}
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
@@ -483,10 +482,10 @@ FPDFPageObjMark_GetParamBlobValue(FPDF_PAGEOBJECTMARK mark,
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
FPDFPageObj_HasTransparency(FPDF_PAGEOBJECT pageObject) {
- if (!pageObject)
+ CPDF_PageObject* pPageObj = CPDFPageObjectFromFPDFPageObject(pageObject);
+ if (!pPageObj)
return false;
- CPDF_PageObject* pPageObj = CPDFPageObjectFromFPDFPageObject(pageObject);
int blend_type = pPageObj->m_GeneralState.GetBlendType();
if (blend_type != FXDIB_BLEND_NORMAL)
return true;
@@ -598,11 +597,8 @@ FPDFPageObjMark_RemoveParam(FPDF_PAGEOBJECT page_object,
}
FPDF_EXPORT int FPDF_CALLCONV FPDFPageObj_GetType(FPDF_PAGEOBJECT pageObject) {
- if (!pageObject)
- return FPDF_PAGEOBJ_UNKNOWN;
-
CPDF_PageObject* pPageObj = CPDFPageObjectFromFPDFPageObject(pageObject);
- return pPageObj->GetType();
+ return pPageObj ? pPageObj->GetType() : FPDF_PAGEOBJ_UNKNOWN;
}
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPage_GenerateContent(FPDF_PAGE page) {
@@ -692,11 +688,11 @@ FPDF_BOOL FPDFPageObj_SetFillColor(FPDF_PAGEOBJECT page_object,
unsigned int G,
unsigned int B,
unsigned int A) {
- if (!page_object || R > 255 || G > 255 || B > 255 || A > 255)
+ CPDF_PageObject* pPageObj = CPDFPageObjectFromFPDFPageObject(page_object);
+ if (!pPageObj || R > 255 || G > 255 || B > 255 || A > 255)
return false;
std::vector<float> rgb = {R / 255.f, G / 255.f, B / 255.f};
- auto* pPageObj = CPDFPageObjectFromFPDFPageObject(page_object);
pPageObj->m_GeneralState.SetFillAlpha(A / 255.f);
pPageObj->m_ColorState.SetFillColor(
CPDF_ColorSpace::GetStockCS(PDFCS_DEVICERGB), rgb);
@@ -728,10 +724,10 @@ FPDFPageObj_GetBounds(FPDF_PAGEOBJECT pageObject,
float* bottom,
float* right,
float* top) {
- if (!pageObject)
+ CPDF_PageObject* pPageObj = CPDFPageObjectFromFPDFPageObject(pageObject);
+ if (!pPageObj)
return false;
- CPDF_PageObject* pPageObj = CPDFPageObjectFromFPDFPageObject(pageObject);
const CFX_FloatRect& bbox = pPageObj->GetRect();
*left = bbox.left;
*bottom = bbox.bottom;
@@ -799,34 +795,38 @@ FPDFPageObj_GetStrokeWidth(FPDF_PAGEOBJECT page_object, float* width) {
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
FPDFPageObj_SetLineJoin(FPDF_PAGEOBJECT page_object, int line_join) {
- if (!page_object)
+ auto* pPageObj = CPDFPageObjectFromFPDFPageObject(page_object);
+ if (!pPageObj)
return false;
- if (line_join <
- static_cast<int>(CFX_GraphStateData::LineJoin::LineJoinMiter) ||
- line_join >
- static_cast<int>(CFX_GraphStateData::LineJoin::LineJoinBevel)) {
+
+ constexpr int kLineJoinMiter =
+ static_cast<int>(CFX_GraphStateData::LineJoin::LineJoinMiter);
+ constexpr int kLineJoinBevel =
+ static_cast<int>(CFX_GraphStateData::LineJoin::LineJoinBevel);
+ if (line_join < kLineJoinMiter || line_join > kLineJoinBevel)
return false;
- }
- auto* pPageObj = CPDFPageObjectFromFPDFPageObject(page_object);
- CFX_GraphStateData::LineJoin lineJoin =
- static_cast<CFX_GraphStateData::LineJoin>(line_join);
- pPageObj->m_GraphState.SetLineJoin(lineJoin);
+
+ pPageObj->m_GraphState.SetLineJoin(
+ static_cast<CFX_GraphStateData::LineJoin>(line_join));
pPageObj->SetDirty(true);
return true;
}
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
FPDFPageObj_SetLineCap(FPDF_PAGEOBJECT page_object, int line_cap) {
- if (!page_object)
+ auto* pPageObj = CPDFPageObjectFromFPDFPageObject(page_object);
+ if (!pPageObj)
return false;
- if (line_cap < static_cast<int>(CFX_GraphStateData::LineCap::LineCapButt) ||
- line_cap > static_cast<int>(CFX_GraphStateData::LineCap::LineCapSquare)) {
+
+ constexpr int kLineCapButt =
+ static_cast<int>(CFX_GraphStateData::LineCap::LineCapButt);
+ constexpr int kLineCapSquare =
+ static_cast<int>(CFX_GraphStateData::LineCap::LineCapSquare);
+ if (line_cap < kLineCapButt || line_cap > kLineCapSquare)
return false;
- }
- auto* pPageObj = CPDFPageObjectFromFPDFPageObject(page_object);
- CFX_GraphStateData::LineCap lineCap =
- static_cast<CFX_GraphStateData::LineCap>(line_cap);
- pPageObj->m_GraphState.SetLineCap(lineCap);
+
+ pPageObj->m_GraphState.SetLineCap(
+ static_cast<CFX_GraphStateData::LineCap>(line_cap));
pPageObj->SetDirty(true);
return true;
}
@@ -835,10 +835,7 @@ FPDF_EXPORT int FPDF_CALLCONV
FPDFFormObj_CountObjects(FPDF_PAGEOBJECT page_object) {
const CPDF_PageObjectList* pObjectList =
CPDFPageObjListFromFPDFFormObject(page_object);
- if (!pObjectList)
- return -1;
-
- return pObjectList->size();
+ return pObjectList ? pObjectList->size() : -1;
}
FPDF_EXPORT FPDF_PAGEOBJECT FPDF_CALLCONV
@@ -860,12 +857,8 @@ FPDFFormObj_GetMatrix(FPDF_PAGEOBJECT form_object,
double* d,
double* e,
double* f) {
- if (!form_object || !a || !b || !c || !d || !e || !f)
- return false;
-
- auto* pPageObj = CPDFPageObjectFromFPDFPageObject(form_object);
- CPDF_FormObject* pFormObj = pPageObj->AsForm();
- if (!pFormObj)
+ CPDF_FormObject* pFormObj = CPDFFormObjectFromFPDFPageObject(form_object);
+ if (!pFormObj || !a || !b || !c || !d || !e || !f)
return false;
const CFX_Matrix& matrix = pFormObj->form_matrix();
@@ -875,6 +868,5 @@ FPDFFormObj_GetMatrix(FPDF_PAGEOBJECT form_object,
*d = matrix.d;
*e = matrix.e;
*f = matrix.f;
-
return true;
}
diff --git a/fpdfsdk/fpdf_editpath.cpp b/fpdfsdk/fpdf_editpath.cpp
index a42aaa9c95..83662aa8b5 100644
--- a/fpdfsdk/fpdf_editpath.cpp
+++ b/fpdfsdk/fpdf_editpath.cpp
@@ -257,7 +257,6 @@ FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPath_GetMatrix(FPDF_PAGEOBJECT path,
*d = pPathObj->m_Matrix.d;
*e = pPathObj->m_Matrix.e;
*f = pPathObj->m_Matrix.f;
-
return true;
}
@@ -268,9 +267,6 @@ FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPath_SetMatrix(FPDF_PAGEOBJECT path,
double d,
double e,
double f) {
- if (!path)
- return false;
-
CPDF_PathObject* pPathObj = CPDFPathObjectFromFPDFPageObject(path);
if (!pPathObj)
return false;
@@ -282,7 +278,6 @@ FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV FPDFPath_SetMatrix(FPDF_PAGEOBJECT path,
pPathObj->m_Matrix.e = e;
pPathObj->m_Matrix.f = f;
pPathObj->SetDirty(true);
-
return true;
}
@@ -312,14 +307,12 @@ FPDFPathSegment_GetPoint(FPDF_PATHSEGMENT segment, float* x, float* y) {
*x = pPathPoint->m_Point.x;
*y = pPathPoint->m_Point.y;
-
return true;
}
FPDF_EXPORT int FPDF_CALLCONV
FPDFPathSegment_GetType(FPDF_PATHSEGMENT segment) {
auto* pPathPoint = FXPathPointFromFPDFPathSegment(segment);
-
return pPathPoint ? static_cast<int>(pPathPoint->m_Type)
: FPDF_SEGMENT_UNKNOWN;
}
@@ -327,6 +320,5 @@ FPDFPathSegment_GetType(FPDF_PATHSEGMENT segment) {
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
FPDFPathSegment_GetClose(FPDF_PATHSEGMENT segment) {
auto* pPathPoint = FXPathPointFromFPDFPathSegment(segment);
-
return pPathPoint ? pPathPoint->m_CloseFigure : false;
}
diff --git a/fpdfsdk/fpdf_edittext.cpp b/fpdfsdk/fpdf_edittext.cpp
index 32ea6db607..1c6709f915 100644
--- a/fpdfsdk/fpdf_edittext.cpp
+++ b/fpdfsdk/fpdf_edittext.cpp
@@ -452,11 +452,7 @@ FPDFPageObj_NewTextObj(FPDF_DOCUMENT document,
FPDF_EXPORT FPDF_BOOL FPDF_CALLCONV
FPDFText_SetText(FPDF_PAGEOBJECT text_object, FPDF_WIDESTRING text) {
- if (!text_object)
- return false;
-
- CPDF_TextObject* pTextObj =
- CPDFPageObjectFromFPDFPageObject(text_object)->AsText();
+ CPDF_TextObject* pTextObj = CPDFTextObjectFromFPDFPageObject(text_object);
if (!pTextObj)
return false;
@@ -614,9 +610,6 @@ FPDFPageObj_CreateTextObj(FPDF_DOCUMENT document,
}
FPDF_EXPORT int FPDF_CALLCONV FPDFText_GetTextRenderMode(FPDF_PAGEOBJECT text) {
- if (!text)
- return -1;
-
CPDF_TextObject* pTextObj = CPDFTextObjectFromFPDFPageObject(text);
if (!pTextObj)
return -1;