summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorTom Sepez <tsepez@chromium.org>2018-07-25 23:25:55 +0000
committerChromium commit bot <commit-bot@chromium.org>2018-07-25 23:25:55 +0000
commit11a6becb837a16972ffda8f94c8fb69ae100f3f4 (patch)
tree38f7e3508f1ef221eabd62b14665e78437da2d8f /core
parent91b8302dec04ca4ddc1f91545d192350665580cf (diff)
downloadpdfium-11a6becb837a16972ffda8f94c8fb69ae100f3f4.tar.xz
Remove some ASSERT (and cast) in favor of checked cases.
Because it is a stronger pattern at runtime. These were found by essentially: grep -ni '\bassert\b.*type' Change-Id: I913d77139053e8980528597a6633e1859e5204c4 Reviewed-on: https://pdfium-review.googlesource.com/38890 Reviewed-by: Lei Zhang <thestig@chromium.org> Commit-Queue: Tom Sepez <tsepez@chromium.org>
Diffstat (limited to 'core')
-rw-r--r--core/fpdfapi/cmaps/fpdf_cmaps.cpp92
-rw-r--r--core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp21
-rw-r--r--core/fxcodec/codec/fx_codec_flate.cpp75
3 files changed, 114 insertions, 74 deletions
diff --git a/core/fpdfapi/cmaps/fpdf_cmaps.cpp b/core/fpdfapi/cmaps/fpdf_cmaps.cpp
index ad1091910c..630942bde7 100644
--- a/core/fpdfapi/cmaps/fpdf_cmaps.cpp
+++ b/core/fpdfapi/cmaps/fpdf_cmaps.cpp
@@ -74,31 +74,40 @@ uint16_t FPDFAPI_CIDFromCharCode(const FXCMAP_CMap* pMap, uint32_t charcode) {
return 0;
}
- while (pMap) {
- if (!pMap->m_pWordMap)
- return 0;
- if (pMap->m_WordMapType == FXCMAP_CMap::Single) {
- const auto* begin = reinterpret_cast<const SingleCmap*>(pMap->m_pWordMap);
- const auto* end = begin + pMap->m_WordCount;
- const auto* found = std::lower_bound(
- begin, end, loword, [](const SingleCmap& element, uint16_t code) {
- return element.code < code;
- });
- if (found != end && found->code == loword)
- return found->cid;
- } else {
- ASSERT(pMap->m_WordMapType == FXCMAP_CMap::Range);
- const auto* begin = reinterpret_cast<const RangeCmap*>(pMap->m_pWordMap);
- const auto* end = begin + pMap->m_WordCount;
- const auto* found = std::lower_bound(
- begin, end, loword, [](const RangeCmap& element, uint16_t code) {
- return element.high < code;
- });
- if (found != end && loword >= found->low && loword <= found->high)
- return found->cid + loword - found->low;
+ while (pMap && pMap->m_pWordMap) {
+ switch (pMap->m_WordMapType) {
+ case FXCMAP_CMap::Single: {
+ const auto* begin =
+ reinterpret_cast<const SingleCmap*>(pMap->m_pWordMap);
+ const auto* end = begin + pMap->m_WordCount;
+ const auto* found = std::lower_bound(
+ begin, end, loword, [](const SingleCmap& element, uint16_t code) {
+ return element.code < code;
+ });
+ if (found != end && found->code == loword)
+ return found->cid;
+ break;
+ }
+ case FXCMAP_CMap::Range: {
+ const auto* begin =
+ reinterpret_cast<const RangeCmap*>(pMap->m_pWordMap);
+ const auto* end = begin + pMap->m_WordCount;
+ const auto* found = std::lower_bound(
+ begin, end, loword, [](const RangeCmap& element, uint16_t code) {
+ return element.high < code;
+ });
+ if (found != end && loword >= found->low && loword <= found->high)
+ return found->cid + loword - found->low;
+ break;
+ }
+ default: {
+ NOTREACHED();
+ break;
+ }
}
pMap = FindNextCMap(pMap);
}
+
return 0;
}
@@ -110,22 +119,31 @@ uint32_t FPDFAPI_CharCodeFromCID(const FXCMAP_CMap* pMap, uint16_t cid) {
// second while loop.)
ASSERT(pMap);
while (pMap) {
- if (pMap->m_WordMapType == FXCMAP_CMap::Single) {
- const auto* pCur = reinterpret_cast<const SingleCmap*>(pMap->m_pWordMap);
- const auto* pEnd = pCur + pMap->m_WordCount;
- while (pCur < pEnd) {
- if (pCur->cid == cid)
- return pCur->code;
- ++pCur;
+ switch (pMap->m_WordMapType) {
+ case FXCMAP_CMap::Single: {
+ const auto* pCur =
+ reinterpret_cast<const SingleCmap*>(pMap->m_pWordMap);
+ const auto* pEnd = pCur + pMap->m_WordCount;
+ while (pCur < pEnd) {
+ if (pCur->cid == cid)
+ return pCur->code;
+ ++pCur;
+ }
+ break;
+ }
+ case FXCMAP_CMap::Range: {
+ const auto* pCur = reinterpret_cast<const RangeCmap*>(pMap->m_pWordMap);
+ const auto* pEnd = pCur + pMap->m_WordCount;
+ while (pCur < pEnd) {
+ if (cid >= pCur->cid && cid <= pCur->cid + pCur->high - pCur->low)
+ return pCur->low + cid - pCur->cid;
+ ++pCur;
+ }
+ break;
}
- } else {
- ASSERT(pMap->m_WordMapType == FXCMAP_CMap::Range);
- const auto* pCur = reinterpret_cast<const RangeCmap*>(pMap->m_pWordMap);
- const auto* pEnd = pCur + pMap->m_WordCount;
- while (pCur < pEnd) {
- if (cid >= pCur->cid && cid <= pCur->cid + pCur->high - pCur->low)
- return pCur->low + cid - pCur->cid;
- ++pCur;
+ default: {
+ NOTREACHED();
+ break;
}
}
pMap = FindNextCMap(pMap);
diff --git a/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp b/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp
index 9693bc48ff..e2fa801a58 100644
--- a/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp
+++ b/core/fpdfapi/edit/cpdf_pagecontentgenerator.cpp
@@ -264,13 +264,20 @@ const CPDF_ContentMark* CPDF_PageContentGenerator::ProcessContentMarks(
}
// If there are parameters, write properties, direct or indirect.
- if (item->GetParamType() == CPDF_ContentMarkItem::DirectDict) {
- CPDF_StringArchiveStream archive_stream(buf);
- item->GetParam()->WriteTo(&archive_stream, nullptr);
- *buf << " ";
- } else {
- ASSERT(item->GetParamType() == CPDF_ContentMarkItem::PropertiesDict);
- *buf << "/" << item->GetPropertyName() << " ";
+ switch (item->GetParamType()) {
+ case CPDF_ContentMarkItem::DirectDict: {
+ CPDF_StringArchiveStream archive_stream(buf);
+ item->GetParam()->WriteTo(&archive_stream, nullptr);
+ *buf << " ";
+ break;
+ }
+ case CPDF_ContentMarkItem::PropertiesDict: {
+ *buf << "/" << item->GetPropertyName() << " ";
+ break;
+ }
+ default:
+ NOTREACHED();
+ break;
}
// Write BDC (begin dictionary content) operator.
diff --git a/core/fxcodec/codec/fx_codec_flate.cpp b/core/fxcodec/codec/fx_codec_flate.cpp
index eb68cedda5..39d27a4955 100644
--- a/core/fxcodec/codec/fx_codec_flate.cpp
+++ b/core/fxcodec/codec/fx_codec_flate.cpp
@@ -709,16 +709,21 @@ uint8_t* CCodec_FlatePredictorScanlineDecoder::v_GetNextLine() {
}
void CCodec_FlatePredictorScanlineDecoder::GetNextLineWithPredictedPitch() {
- if (m_Predictor == PredictorType::kPng) {
- FlateOutput(m_pFlate.get(), m_pPredictRaw, m_PredictPitch + 1);
- PNG_PredictLine(m_pScanline.get(), m_pPredictRaw, m_pLastLine,
- m_BitsPerComponent, m_Colors, m_Columns);
- memcpy(m_pLastLine, m_pScanline.get(), m_PredictPitch);
- } else {
- ASSERT(m_Predictor == PredictorType::kFlate);
- FlateOutput(m_pFlate.get(), m_pScanline.get(), m_Pitch);
- TIFF_PredictLine(m_pScanline.get(), m_PredictPitch, m_bpc, m_nComps,
- m_OutputWidth);
+ switch (m_Predictor) {
+ case PredictorType::kPng:
+ FlateOutput(m_pFlate.get(), m_pPredictRaw, m_PredictPitch + 1);
+ PNG_PredictLine(m_pScanline.get(), m_pPredictRaw, m_pLastLine,
+ m_BitsPerComponent, m_Colors, m_Columns);
+ memcpy(m_pLastLine, m_pScanline.get(), m_PredictPitch);
+ break;
+ case PredictorType::kFlate:
+ FlateOutput(m_pFlate.get(), m_pScanline.get(), m_Pitch);
+ TIFF_PredictLine(m_pScanline.get(), m_PredictPitch, m_bpc, m_nComps,
+ m_OutputWidth);
+ break;
+ default:
+ NOTREACHED();
+ break;
}
}
@@ -732,16 +737,21 @@ void CCodec_FlatePredictorScanlineDecoder::GetNextLineWithoutPredictedPitch() {
bytes_to_go -= read_leftover;
}
while (bytes_to_go) {
- if (m_Predictor == PredictorType::kPng) {
- FlateOutput(m_pFlate.get(), m_pPredictRaw, m_PredictPitch + 1);
- PNG_PredictLine(m_pPredictBuffer, m_pPredictRaw, m_pLastLine,
- m_BitsPerComponent, m_Colors, m_Columns);
- memcpy(m_pLastLine, m_pPredictBuffer, m_PredictPitch);
- } else {
- ASSERT(m_Predictor == PredictorType::kFlate);
- FlateOutput(m_pFlate.get(), m_pPredictBuffer, m_PredictPitch);
- TIFF_PredictLine(m_pPredictBuffer, m_PredictPitch, m_BitsPerComponent,
- m_Colors, m_Columns);
+ switch (m_Predictor) {
+ case PredictorType::kPng:
+ FlateOutput(m_pFlate.get(), m_pPredictRaw, m_PredictPitch + 1);
+ PNG_PredictLine(m_pPredictBuffer, m_pPredictRaw, m_pLastLine,
+ m_BitsPerComponent, m_Colors, m_Columns);
+ memcpy(m_pLastLine, m_pPredictBuffer, m_PredictPitch);
+ break;
+ case PredictorType::kFlate:
+ FlateOutput(m_pFlate.get(), m_pPredictBuffer, m_PredictPitch);
+ TIFF_PredictLine(m_pPredictBuffer, m_PredictPitch, m_BitsPerComponent,
+ m_Colors, m_Columns);
+ break;
+ default:
+ NOTREACHED();
+ break;
}
size_t read_bytes =
m_PredictPitch > bytes_to_go ? bytes_to_go : m_PredictPitch;
@@ -807,17 +817,22 @@ uint32_t CCodec_FlateModule::FlateOrLZWDecode(bool bLZW,
FlateUncompress(pdfium::make_span(src_buf, src_size), estimated_size,
*dest_buf, *dest_size, offset);
}
- if (predictor_type == PredictorType::kNone)
- return offset;
- bool ret;
- if (predictor_type == PredictorType::kPng) {
- ret =
- PNG_Predictor(*dest_buf, *dest_size, Colors, BitsPerComponent, Columns);
- } else {
- ASSERT(predictor_type == PredictorType::kFlate);
- ret = TIFF_Predictor(*dest_buf, *dest_size, Colors, BitsPerComponent,
- Columns);
+ bool ret = false;
+ switch (predictor_type) {
+ case PredictorType::kNone:
+ return offset;
+ case PredictorType::kPng:
+ ret = PNG_Predictor(*dest_buf, *dest_size, Colors, BitsPerComponent,
+ Columns);
+ break;
+ case PredictorType::kFlate:
+ ret = TIFF_Predictor(*dest_buf, *dest_size, Colors, BitsPerComponent,
+ Columns);
+ break;
+ default:
+ NOTREACHED();
+ break;
}
return ret ? offset : FX_INVALID_OFFSET;
}