Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2387)

Unified Diff: core/src/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp

Issue 1666663004: Add unit tests for ascii85 and hex decoders. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: core/src/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp
diff --git a/core/src/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp b/core/src/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp
index 39139c40ee12a0f042a40a68c0997755dd356b59..eb378833468682107512c17cdfd689ca8ca23d1d 100644
--- a/core/src/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp
+++ b/core/src/fpdfapi/fpdf_parser/fpdf_parser_decode.cpp
@@ -49,35 +49,32 @@ FX_DWORD A85Decode(const uint8_t* src_buf,
uint8_t*& dest_buf,
FX_DWORD& dest_size) {
dest_size = 0;
- dest_buf = NULL;
- if (src_size == 0) {
+ dest_buf = nullptr;
+ if (src_size == 0)
return 0;
- }
+
+ // Count legal characters and zeros.
FX_DWORD zcount = 0;
FX_DWORD pos = 0;
while (pos < src_size) {
uint8_t ch = src_buf[pos];
- if (ch < '!' && ch != '\n' && ch != '\r' && ch != ' ' && ch != '\t') {
- break;
- }
if (ch == 'z') {
zcount++;
- } else if (ch > 'u') {
+ } else if ((ch < '!' || ch > 'u') && !PDFCharIsLineEnding(ch) &&
+ ch != ' ' && ch != '\t') {
break;
}
pos++;
}
- if (pos == 0) {
+ // No content to decode.
+ if (pos == 0)
return 0;
- }
- if (zcount > UINT_MAX / 4) {
Lei Zhang 2016/02/05 01:25:33 Why remove this?
Wei Li 2016/02/06 00:11:44 This one is redundant as the next statement should
Lei Zhang 2016/02/06 00:17:04 Sorry, it's not. If |zcount| is very large, zcount
Wei Li 2016/02/06 01:16:26 In that case, this should fix it.
- return (FX_DWORD)-1;
- }
+
if (zcount * 4 > UINT_MAX - (pos - zcount)) {
return (FX_DWORD)-1;
}
dest_buf = FX_Alloc(uint8_t, zcount * 4 + (pos - zcount));
- int state = 0;
+ size_t state = 0;
uint32_t res = 0;
pos = dest_size = 0;
while (pos < src_size) {
@@ -90,46 +87,48 @@ FX_DWORD A85Decode(const uint8_t* src_buf,
state = 0;
res = 0;
dest_size += 4;
- } else {
- if (ch < '!' || ch > 'u') {
- break;
- }
+ } else if (ch >= '!' && ch <= 'u') {
res = res * 85 + ch - 33;
state++;
if (state == 5) {
- for (int i = 0; i < 4; i++) {
+ for (size_t i = 0; i < 4; i++) {
dest_buf[dest_size++] = (uint8_t)(res >> (3 - i) * 8);
}
state = 0;
res = 0;
}
+ } else {
+ // The end or illegal character.
+ break;
}
}
+ // Handle partial group.
if (state) {
- int i;
- for (i = state; i < 5; i++) {
+ for (size_t i = state; i < 5; i++)
res = res * 85 + 84;
- }
- for (i = 0; i < state - 1; i++) {
+ for (size_t i = 0; i < state - 1; i++)
dest_buf[dest_size++] = (uint8_t)(res >> (3 - i) * 8);
- }
}
- if (pos < src_size && src_buf[pos] == '>') {
+ if (pos < src_size && src_buf[pos] == '>')
pos++;
- }
return pos;
}
+
FX_DWORD HexDecode(const uint8_t* src_buf,
FX_DWORD src_size,
uint8_t*& dest_buf,
FX_DWORD& dest_size) {
- FX_DWORD i;
- for (i = 0; i < src_size; i++)
- if (src_buf[i] == '>') {
- break;
- }
- dest_buf = FX_Alloc(uint8_t, i / 2 + 1);
dest_size = 0;
+ dest_buf = nullptr;
Lei Zhang 2016/02/05 01:25:33 Move this line into the 0 src_size handler?
Wei Li 2016/02/06 00:11:44 Done.
+ if (src_size == 0)
+ return 0;
+
+ FX_DWORD i = 0;
+ // Find the end of data.
+ while (i < src_size && src_buf[i++] != '>') {
Lei Zhang 2016/02/05 01:25:33 Is |i| going to be incremented by 1 more in the ne
Wei Li 2016/02/06 00:11:44 Done.
+ }
+
+ dest_buf = FX_Alloc(uint8_t, i / 2 + 1);
bool bFirst = true;
for (i = 0; i < src_size; i++) {
uint8_t ch = src_buf[i];
@@ -218,6 +217,7 @@ FX_DWORD RunLengthDecode(const uint8_t* src_buf,
}
return ret;
}
+
ICodec_ScanlineDecoder* FPDFAPI_CreateFaxDecoder(
const uint8_t* src_buf,
FX_DWORD src_size,
@@ -241,13 +241,14 @@ ICodec_ScanlineDecoder* FPDFAPI_CreateFaxDecoder(
Rows = 0;
}
if (Columns <= 0 || Rows < 0 || Columns > USHRT_MAX || Rows > USHRT_MAX) {
- return NULL;
+ return nullptr;
}
}
return CPDF_ModuleMgr::Get()->GetFaxModule()->CreateDecoder(
src_buf, src_size, width, height, K, EndOfLine, ByteAlign, BlackIs1,
Columns, Rows);
}
+
static FX_BOOL CheckFlateDecodeParams(int Colors,
int BitsPerComponent,
int Columns) {
@@ -269,6 +270,7 @@ static FX_BOOL CheckFlateDecodeParams(int Colors,
}
return TRUE;
}
+
ICodec_ScanlineDecoder* FPDFAPI_CreateFlateDecoder(
const uint8_t* src_buf,
FX_DWORD src_size,
@@ -285,13 +287,14 @@ ICodec_ScanlineDecoder* FPDFAPI_CreateFlateDecoder(
BitsPerComponent = pParams->GetIntegerBy("BitsPerComponent", 8);
Columns = pParams->GetIntegerBy("Columns", 1);
if (!CheckFlateDecodeParams(Colors, BitsPerComponent, Columns)) {
- return NULL;
+ return nullptr;
}
}
return CPDF_ModuleMgr::Get()->GetFlateModule()->CreateDecoder(
src_buf, src_size, width, height, nComps, bpc, predictor, Colors,
BitsPerComponent, Columns);
}
+
FX_DWORD FPDFAPI_FlateOrLZWDecode(FX_BOOL bLZW,
const uint8_t* src_buf,
FX_DWORD src_size,
@@ -316,6 +319,7 @@ FX_DWORD FPDFAPI_FlateOrLZWDecode(FX_BOOL bLZW,
bLZW, src_buf, src_size, bEarlyChange, predictor, Colors,
BitsPerComponent, Columns, estimated_size, dest_buf, dest_size);
}
+
FX_BOOL PDF_DataDecode(const uint8_t* src_buf,
FX_DWORD src_size,
const CPDF_Dictionary* pDict,
@@ -353,7 +357,7 @@ FX_BOOL PDF_DataDecode(const uint8_t* src_buf,
int estimated_size =
i == DecoderList.GetSize() - 1 ? last_estimated_size : 0;
CFX_ByteString decoder = DecoderList[i];
- // Use ToDictionary here because we can push NULL into the ParamList.
+ // Use ToDictionary here because we can push nullptr into the ParamList.
CPDF_Dictionary* pParam = ToDictionary(ParamList[i]);
uint8_t* new_buf = nullptr;
FX_DWORD new_size = (FX_DWORD)-1;
@@ -417,6 +421,7 @@ FX_BOOL PDF_DataDecode(const uint8_t* src_buf,
dest_size = last_size;
return TRUE;
}
+
CFX_WideString PDF_DecodeText(const uint8_t* src_data,
FX_DWORD src_len,
CFX_CharMap* pCharMap) {
@@ -464,6 +469,7 @@ CFX_WideString PDF_DecodeText(const uint8_t* src_data,
}
return result;
}
+
CFX_ByteString PDF_EncodeText(const FX_WCHAR* pString,
int len,
CFX_CharMap* pCharMap) {
@@ -509,6 +515,7 @@ CFX_ByteString PDF_EncodeText(const FX_WCHAR* pString,
result.ReleaseBuffer(encLen);
return result;
}
+
CFX_ByteString PDF_EncodeString(const CFX_ByteString& src, FX_BOOL bHex) {
CFX_ByteTextBuf result;
int srclen = src.GetLength();
@@ -538,6 +545,7 @@ CFX_ByteString PDF_EncodeString(const CFX_ByteString& src, FX_BOOL bHex) {
result.AppendChar(')');
return result.GetByteString();
}
+
void FlateEncode(const uint8_t* src_buf,
FX_DWORD src_size,
uint8_t*& dest_buf,
@@ -547,6 +555,7 @@ void FlateEncode(const uint8_t* src_buf,
pEncoders->GetFlateModule()->Encode(src_buf, src_size, dest_buf, dest_size);
}
}
+
void FlateEncode(const uint8_t* src_buf,
FX_DWORD src_size,
int predictor,
@@ -562,6 +571,7 @@ void FlateEncode(const uint8_t* src_buf,
dest_size);
}
}
+
FX_DWORD FlateDecode(const uint8_t* src_buf,
FX_DWORD src_size,
uint8_t*& dest_buf,

Powered by Google App Engine
This is Rietveld 408576698