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

Side by Side Diff: core/src/fxcodec/codec/fx_codec_flate.cpp

Issue 1136673005: Merge to XFA: Add safe FX_Alloc2D() macro (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@xfa
Patch Set: Created 5 years, 7 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 unified diff | Download patch
« no previous file with comments | « core/src/fxcodec/codec/fx_codec_fax.cpp ('k') | core/src/fxcodec/codec/fx_codec_jpeg.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 PDFium Authors. All rights reserved. 1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 6
7 #include "../../../../third_party/base/nonstd_unique_ptr.h" 7 #include "../../../../third_party/base/nonstd_unique_ptr.h"
8 #include "../../../include/fxcodec/fx_codec.h" 8 #include "../../../include/fxcodec/fx_codec.h"
9 #include "../../fx_zlib.h" 9 #include "../../fx_zlib.h"
10 #include "codec_int.h" 10 #include "codec_int.h"
11 11
12 extern "C" 12 extern "C"
13 { 13 {
14 static void* my_alloc_func (void* opaque, unsigned int items, unsigned int s ize) 14 static void* my_alloc_func (void* opaque, unsigned int items, unsigned int s ize)
15 { 15 {
16 return FX_Alloc(FX_BYTE, items * size); 16 return FX_Alloc2D(FX_BYTE, items, size);
17 } 17 }
18 static void my_free_func (void* opaque, void* address) 18 static void my_free_func (void* opaque, void* address)
19 { 19 {
20 FX_Free(address); 20 FX_Free(address);
21 } 21 }
22 void* FPDFAPI_FlateInit(void* (*alloc_func)(void*, unsigned int, unsigned in t), 22 void* FPDFAPI_FlateInit(void* (*alloc_func)(void*, unsigned int, unsigned in t),
23 void (*free_func)(void*, void*)) 23 void (*free_func)(void*, void*))
24 { 24 {
25 z_stream* p = (z_stream*)alloc_func(0, 1, sizeof(z_stream)); 25 z_stream* p = (z_stream*)alloc_func(0, 1, sizeof(z_stream));
26 if (p == NULL) { 26 if (p == NULL) {
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after
234 static FX_BOOL PNG_PredictorEncode(FX_LPBYTE& data_buf, FX_DWORD& data_size, 234 static FX_BOOL PNG_PredictorEncode(FX_LPBYTE& data_buf, FX_DWORD& data_size,
235 int predictor, int Colors, 235 int predictor, int Colors,
236 int BitsPerComponent, int Columns) 236 int BitsPerComponent, int Columns)
237 { 237 {
238 const int BytesPerPixel = (Colors * BitsPerComponent + 7) / 8; 238 const int BytesPerPixel = (Colors * BitsPerComponent + 7) / 8;
239 const int row_size = (Colors * BitsPerComponent * Columns + 7) / 8; 239 const int row_size = (Colors * BitsPerComponent * Columns + 7) / 8;
240 if (row_size <= 0) 240 if (row_size <= 0)
241 return FALSE; 241 return FALSE;
242 const int row_count = (data_size + row_size - 1) / row_size; 242 const int row_count = (data_size + row_size - 1) / row_size;
243 const int last_row_size = data_size % row_size; 243 const int last_row_size = data_size % row_size;
244 FX_LPBYTE dest_buf = FX_Alloc( FX_BYTE, (row_size + 1) * row_count); 244 FX_LPBYTE dest_buf = FX_Alloc2D(FX_BYTE, row_size + 1, row_count);
245 if (dest_buf == NULL)
246 return FALSE;
247 int byte_cnt = 0; 245 int byte_cnt = 0;
248 FX_LPBYTE pSrcData = data_buf; 246 FX_LPBYTE pSrcData = data_buf;
249 FX_LPBYTE pDestData = dest_buf; 247 FX_LPBYTE pDestData = dest_buf;
250 for (int row = 0; row < row_count; row++) { 248 for (int row = 0; row < row_count; row++) {
251 if (predictor == 10) { 249 if (predictor == 10) {
252 pDestData[0] = 0; 250 pDestData[0] = 0;
253 int move_size = row_size; 251 int move_size = row_size;
254 if (move_size * (row + 1) > (int)data_size) { 252 if (move_size * (row + 1) > (int)data_size) {
255 move_size = data_size - (move_size * row); 253 move_size = data_size - (move_size * row);
256 } 254 }
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
390 } 388 }
391 static FX_BOOL PNG_Predictor(FX_LPBYTE& data_buf, FX_DWORD& data_size, 389 static FX_BOOL PNG_Predictor(FX_LPBYTE& data_buf, FX_DWORD& data_size,
392 int Colors, int BitsPerComponent, int Columns) 390 int Colors, int BitsPerComponent, int Columns)
393 { 391 {
394 const int BytesPerPixel = (Colors * BitsPerComponent + 7) / 8; 392 const int BytesPerPixel = (Colors * BitsPerComponent + 7) / 8;
395 const int row_size = (Colors * BitsPerComponent * Columns + 7) / 8; 393 const int row_size = (Colors * BitsPerComponent * Columns + 7) / 8;
396 if (row_size <= 0) 394 if (row_size <= 0)
397 return FALSE; 395 return FALSE;
398 const int row_count = (data_size + row_size) / (row_size + 1); 396 const int row_count = (data_size + row_size) / (row_size + 1);
399 const int last_row_size = data_size % (row_size + 1); 397 const int last_row_size = data_size % (row_size + 1);
400 FX_LPBYTE dest_buf = FX_Alloc( FX_BYTE, row_size * row_count); 398 FX_LPBYTE dest_buf = FX_Alloc2D(FX_BYTE, row_size, row_count);
401 if (dest_buf == NULL)
402 return FALSE;
403 int byte_cnt = 0; 399 int byte_cnt = 0;
404 FX_LPBYTE pSrcData = data_buf; 400 FX_LPBYTE pSrcData = data_buf;
405 FX_LPBYTE pDestData = dest_buf; 401 FX_LPBYTE pDestData = dest_buf;
406 for (int row = 0; row < row_count; row ++) { 402 for (int row = 0; row < row_count; row ++) {
407 FX_BYTE tag = pSrcData[0]; 403 FX_BYTE tag = pSrcData[0];
408 byte_cnt++; 404 byte_cnt++;
409 if (tag == 0) { 405 if (tag == 0) {
410 int move_size = row_size; 406 int move_size = row_size;
411 if ((row + 1) * (move_size + 1) > (int)data_size) { 407 if ((row + 1) * (move_size + 1) > (int)data_size) {
412 move_size = last_row_size - 1; 408 move_size = last_row_size - 1;
(...skipping 531 matching lines...) Expand 10 before | Expand all | Expand 10 after
944 dest_size = src_size + src_size / 1000 + 12; 940 dest_size = src_size + src_size / 1000 + 12;
945 dest_buf = FX_Alloc( FX_BYTE, dest_size); 941 dest_buf = FX_Alloc( FX_BYTE, dest_size);
946 if (dest_buf == NULL) { 942 if (dest_buf == NULL) {
947 return FALSE; 943 return FALSE;
948 } 944 }
949 unsigned long temp_size = dest_size; 945 unsigned long temp_size = dest_size;
950 FPDFAPI_FlateCompress(dest_buf, &temp_size, src_buf, src_size); 946 FPDFAPI_FlateCompress(dest_buf, &temp_size, src_buf, src_size);
951 dest_size = (FX_DWORD)temp_size; 947 dest_size = (FX_DWORD)temp_size;
952 return TRUE; 948 return TRUE;
953 } 949 }
OLDNEW
« no previous file with comments | « core/src/fxcodec/codec/fx_codec_fax.cpp ('k') | core/src/fxcodec/codec/fx_codec_jpeg.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698