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

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: more comment 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
« no previous file with comments | « no previous file | core/src/fpdfapi/fpdf_parser/fpdf_parser_decode_embeddertest.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..871653eb22734f00dfd12b70f65e919f62d0eca1 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) {
- return (FX_DWORD)-1;
- }
- if (zcount * 4 > UINT_MAX - (pos - zcount)) {
+
+ if (zcount > (UINT_MAX - (pos - zcount)) / 4) {
return (FX_DWORD)-1;
}
dest_buf = FX_Alloc(uint8_t, zcount * 4 + (pos - zcount));
Lei Zhang 2016/02/06 01:22:02 I looked at this just now - isn't this just: zcoun
Wei Li 2016/02/06 02:18:59 Now let's make it right :)
- int state = 0;
+ size_t state = 0;
uint32_t res = 0;
pos = dest_size = 0;
while (pos < src_size) {
@@ -90,46 +87,49 @@ 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;
+ if (src_size == 0) {
+ dest_buf = nullptr;
+ return 0;
+ }
+
+ FX_DWORD i = 0;
+ // Find the end of data.
+ while (i < src_size && src_buf[i] != '>')
+ i++;
+
+ 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 +218,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 +242,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 +271,7 @@ static FX_BOOL CheckFlateDecodeParams(int Colors,
}
return TRUE;
}
+
ICodec_ScanlineDecoder* FPDFAPI_CreateFlateDecoder(
const uint8_t* src_buf,
FX_DWORD src_size,
@@ -285,13 +288,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 +320,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 +358,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 +422,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 +470,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 +516,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 +546,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 +556,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 +572,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,
« no previous file with comments | « no previous file | core/src/fpdfapi/fpdf_parser/fpdf_parser_decode_embeddertest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698