Home | History | Annotate | Download | only in codec
      1 // Copyright 2014 PDFium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
      6 
      7 #include "core/fxcodec/fx_codec.h"
      8 
      9 #include <algorithm>
     10 #include <cmath>
     11 #include <memory>
     12 #include <utility>
     13 
     14 #include "core/fxcodec/codec/codec_int.h"
     15 #include "core/fxcrt/fx_ext.h"
     16 #include "core/fxcrt/fx_safe_types.h"
     17 #include "third_party/base/logging.h"
     18 #include "third_party/base/ptr_util.h"
     19 
     20 CCodec_ModuleMgr::CCodec_ModuleMgr()
     21     : m_pBasicModule(new CCodec_BasicModule),
     22       m_pFaxModule(new CCodec_FaxModule),
     23       m_pJpegModule(new CCodec_JpegModule),
     24       m_pJpxModule(new CCodec_JpxModule),
     25       m_pJbig2Module(new CCodec_Jbig2Module),
     26       m_pIccModule(new CCodec_IccModule),
     27       m_pFlateModule(new CCodec_FlateModule) {
     28 }
     29 
     30 CCodec_ModuleMgr::~CCodec_ModuleMgr() {}
     31 
     32 CCodec_ScanlineDecoder::CCodec_ScanlineDecoder()
     33     : CCodec_ScanlineDecoder(0, 0, 0, 0, 0, 0, 0) {}
     34 
     35 CCodec_ScanlineDecoder::CCodec_ScanlineDecoder(int nOrigWidth,
     36                                                int nOrigHeight,
     37                                                int nOutputWidth,
     38                                                int nOutputHeight,
     39                                                int nComps,
     40                                                int nBpc,
     41                                                uint32_t nPitch)
     42     : m_OrigWidth(nOrigWidth),
     43       m_OrigHeight(nOrigHeight),
     44       m_OutputWidth(nOutputWidth),
     45       m_OutputHeight(nOutputHeight),
     46       m_nComps(nComps),
     47       m_bpc(nBpc),
     48       m_Pitch(nPitch),
     49       m_NextLine(-1),
     50       m_pLastScanline(nullptr) {}
     51 
     52 CCodec_ScanlineDecoder::~CCodec_ScanlineDecoder() {}
     53 
     54 const uint8_t* CCodec_ScanlineDecoder::GetScanline(int line) {
     55   if (m_NextLine == line + 1)
     56     return m_pLastScanline;
     57 
     58   if (m_NextLine < 0 || m_NextLine > line) {
     59     if (!v_Rewind())
     60       return nullptr;
     61     m_NextLine = 0;
     62   }
     63   while (m_NextLine < line) {
     64     ReadNextLine();
     65     m_NextLine++;
     66   }
     67   m_pLastScanline = ReadNextLine();
     68   m_NextLine++;
     69   return m_pLastScanline;
     70 }
     71 
     72 bool CCodec_ScanlineDecoder::SkipToScanline(int line, IFX_Pause* pPause) {
     73   if (m_NextLine == line || m_NextLine == line + 1)
     74     return false;
     75 
     76   if (m_NextLine < 0 || m_NextLine > line) {
     77     v_Rewind();
     78     m_NextLine = 0;
     79   }
     80   m_pLastScanline = nullptr;
     81   while (m_NextLine < line) {
     82     m_pLastScanline = ReadNextLine();
     83     m_NextLine++;
     84     if (pPause && pPause->NeedToPauseNow()) {
     85       return true;
     86     }
     87   }
     88   return false;
     89 }
     90 
     91 uint8_t* CCodec_ScanlineDecoder::ReadNextLine() {
     92   return v_GetNextLine();
     93 }
     94 
     95 bool CCodec_BasicModule::RunLengthEncode(const uint8_t* src_buf,
     96                                          uint32_t src_size,
     97                                          uint8_t** dest_buf,
     98                                          uint32_t* dest_size) {
     99   // Check inputs
    100   if (!src_buf || !dest_buf || !dest_size || src_size == 0)
    101     return false;
    102 
    103   // Edge case
    104   if (src_size == 1) {
    105     *dest_buf = FX_Alloc(uint8_t, 3);
    106     (*dest_buf)[0] = 0;
    107     (*dest_buf)[1] = src_buf[0];
    108     (*dest_buf)[2] = 128;
    109     *dest_size = 3;
    110     return true;
    111   }
    112 
    113   // Worst case: 1 nonmatch, 2 match, 1 nonmatch, 2 match, etc. This becomes
    114   // 4 output chars for every 3 input, plus up to 4 more for the 1-2 chars
    115   // rounded off plus the terminating character.
    116   uint32_t est_size = 4 * ((src_size + 2) / 3) + 1;
    117   *dest_buf = FX_Alloc(uint8_t, est_size);
    118 
    119   // Set up pointers.
    120   uint8_t* out = *dest_buf;
    121   uint32_t run_start = 0;
    122   uint32_t run_end = 1;
    123   uint8_t x = src_buf[run_start];
    124   uint8_t y = src_buf[run_end];
    125   while (run_end < src_size) {
    126     uint32_t max_len = std::min((uint32_t)128, src_size - run_start);
    127     while (x == y && (run_end - run_start < max_len - 1))
    128       y = src_buf[++run_end];
    129 
    130     // Reached end with matched run. Update variables to expected values.
    131     if (x == y) {
    132       run_end++;
    133       if (run_end < src_size)
    134         y = src_buf[run_end];
    135     }
    136     if (run_end - run_start > 1) {  // Matched run but not at end of input.
    137       out[0] = 257 - (run_end - run_start);
    138       out[1] = x;
    139       x = y;
    140       run_start = run_end;
    141       run_end++;
    142       if (run_end < src_size)
    143         y = src_buf[run_end];
    144       out += 2;
    145       continue;
    146     }
    147     // Mismatched run
    148     while (x != y && run_end <= run_start + max_len) {
    149       out[run_end - run_start] = x;
    150       x = y;
    151       run_end++;
    152       if (run_end == src_size) {
    153         if (run_end <= run_start + max_len) {
    154           out[run_end - run_start] = x;
    155           run_end++;
    156         }
    157         break;
    158       }
    159       y = src_buf[run_end];
    160     }
    161     out[0] = run_end - run_start - 2;
    162     out += run_end - run_start;
    163     run_start = run_end - 1;
    164   }
    165   if (run_start < src_size) {  // 1 leftover character
    166     out[0] = 0;
    167     out[1] = x;
    168     out += 2;
    169   }
    170   *out = 128;
    171   *dest_size = out + 1 - *dest_buf;
    172   return true;
    173 }
    174 
    175 bool CCodec_BasicModule::A85Encode(const uint8_t* src_buf,
    176                                    uint32_t src_size,
    177                                    uint8_t** dest_buf,
    178                                    uint32_t* dest_size) {
    179   // Check inputs.
    180   if (!src_buf || !dest_buf || !dest_size)
    181     return false;
    182 
    183   if (src_size == 0) {
    184     *dest_size = 0;
    185     return false;
    186   }
    187 
    188   // Worst case: 5 output for each 4 input (plus up to 4 from leftover), plus
    189   // 2 character new lines each 75 output chars plus 2 termination chars. May
    190   // have fewer if there are special "z" chars.
    191   uint32_t est_size = 5 * (src_size / 4) + 4 + src_size / 30 + 2;
    192   *dest_buf = FX_Alloc(uint8_t, est_size);
    193 
    194   // Set up pointers.
    195   uint8_t* out = *dest_buf;
    196   uint32_t pos = 0;
    197   uint32_t line_length = 0;
    198   while (src_size >= 4 && pos < src_size - 3) {
    199     uint32_t val = ((uint32_t)(src_buf[pos]) << 24) +
    200                    ((uint32_t)(src_buf[pos + 1]) << 16) +
    201                    ((uint32_t)(src_buf[pos + 2]) << 8) +
    202                    (uint32_t)(src_buf[pos + 3]);
    203     pos += 4;
    204     if (val == 0) {  // All zero special case
    205       *out = 'z';
    206       out++;
    207       line_length++;
    208     } else {  // Compute base 85 characters and add 33.
    209       for (int i = 4; i >= 0; i--) {
    210         out[i] = (uint8_t)(val % 85) + 33;
    211         val = val / 85;
    212       }
    213       out += 5;
    214       line_length += 5;
    215     }
    216     if (line_length >= 75) {  // Add a return.
    217       *out++ = '\r';
    218       *out++ = '\n';
    219       line_length = 0;
    220     }
    221   }
    222   if (pos < src_size) {  // Leftover bytes
    223     uint32_t val = 0;
    224     int count = 0;
    225     while (pos < src_size) {
    226       val += (uint32_t)(src_buf[pos]) << (8 * (3 - count));
    227       count++;
    228       pos++;
    229     }
    230     for (int i = 4; i >= 0; i--) {
    231       if (i <= count)
    232         out[i] = (uint8_t)(val % 85) + 33;
    233       val = val / 85;
    234     }
    235     out += count + 1;
    236   }
    237 
    238   // Terminating characters.
    239   out[0] = '~';
    240   out[1] = '>';
    241   out += 2;
    242   *dest_size = out - *dest_buf;
    243   return true;
    244 }
    245 
    246 #ifdef PDF_ENABLE_XFA
    247 CFX_DIBAttribute::CFX_DIBAttribute()
    248     : m_nXDPI(-1),
    249       m_nYDPI(-1),
    250       m_fAspectRatio(-1.0f),
    251       m_wDPIUnit(0),
    252       m_nGifLeft(0),
    253       m_nGifTop(0),
    254       m_pGifLocalPalette(nullptr),
    255       m_nGifLocalPalNum(0),
    256       m_nBmpCompressType(0) {
    257   FXSYS_memset(m_strTime, 0, sizeof(m_strTime));
    258 }
    259 CFX_DIBAttribute::~CFX_DIBAttribute() {
    260   for (const auto& pair : m_Exif)
    261     FX_Free(pair.second);
    262 }
    263 #endif  // PDF_ENABLE_XFA
    264 
    265 class CCodec_RLScanlineDecoder : public CCodec_ScanlineDecoder {
    266  public:
    267   CCodec_RLScanlineDecoder();
    268   ~CCodec_RLScanlineDecoder() override;
    269 
    270   bool Create(const uint8_t* src_buf,
    271               uint32_t src_size,
    272               int width,
    273               int height,
    274               int nComps,
    275               int bpc);
    276 
    277   // CCodec_ScanlineDecoder
    278   bool v_Rewind() override;
    279   uint8_t* v_GetNextLine() override;
    280   uint32_t GetSrcOffset() override { return m_SrcOffset; }
    281 
    282  protected:
    283   bool CheckDestSize();
    284   void GetNextOperator();
    285   void UpdateOperator(uint8_t used_bytes);
    286 
    287   uint8_t* m_pScanline;
    288   const uint8_t* m_pSrcBuf;
    289   uint32_t m_SrcSize;
    290   uint32_t m_dwLineBytes;
    291   uint32_t m_SrcOffset;
    292   bool m_bEOD;
    293   uint8_t m_Operator;
    294 };
    295 CCodec_RLScanlineDecoder::CCodec_RLScanlineDecoder()
    296     : m_pScanline(nullptr),
    297       m_pSrcBuf(nullptr),
    298       m_SrcSize(0),
    299       m_dwLineBytes(0),
    300       m_SrcOffset(0),
    301       m_bEOD(false),
    302       m_Operator(0) {}
    303 CCodec_RLScanlineDecoder::~CCodec_RLScanlineDecoder() {
    304   FX_Free(m_pScanline);
    305 }
    306 bool CCodec_RLScanlineDecoder::CheckDestSize() {
    307   uint32_t i = 0;
    308   uint32_t old_size = 0;
    309   uint32_t dest_size = 0;
    310   while (i < m_SrcSize) {
    311     if (m_pSrcBuf[i] < 128) {
    312       old_size = dest_size;
    313       dest_size += m_pSrcBuf[i] + 1;
    314       if (dest_size < old_size) {
    315         return false;
    316       }
    317       i += m_pSrcBuf[i] + 2;
    318     } else if (m_pSrcBuf[i] > 128) {
    319       old_size = dest_size;
    320       dest_size += 257 - m_pSrcBuf[i];
    321       if (dest_size < old_size) {
    322         return false;
    323       }
    324       i += 2;
    325     } else {
    326       break;
    327     }
    328   }
    329   if (((uint32_t)m_OrigWidth * m_nComps * m_bpc * m_OrigHeight + 7) / 8 >
    330       dest_size) {
    331     return false;
    332   }
    333   return true;
    334 }
    335 bool CCodec_RLScanlineDecoder::Create(const uint8_t* src_buf,
    336                                       uint32_t src_size,
    337                                       int width,
    338                                       int height,
    339                                       int nComps,
    340                                       int bpc) {
    341   m_pSrcBuf = src_buf;
    342   m_SrcSize = src_size;
    343   m_OutputWidth = m_OrigWidth = width;
    344   m_OutputHeight = m_OrigHeight = height;
    345   m_nComps = nComps;
    346   m_bpc = bpc;
    347   // Aligning the pitch to 4 bytes requires an integer overflow check.
    348   FX_SAFE_UINT32 pitch = width;
    349   pitch *= nComps;
    350   pitch *= bpc;
    351   pitch += 31;
    352   pitch /= 32;
    353   pitch *= 4;
    354   if (!pitch.IsValid()) {
    355     return false;
    356   }
    357   m_Pitch = pitch.ValueOrDie();
    358   // Overflow should already have been checked before this is called.
    359   m_dwLineBytes = (static_cast<uint32_t>(width) * nComps * bpc + 7) / 8;
    360   m_pScanline = FX_Alloc(uint8_t, m_Pitch);
    361   return CheckDestSize();
    362 }
    363 bool CCodec_RLScanlineDecoder::v_Rewind() {
    364   FXSYS_memset(m_pScanline, 0, m_Pitch);
    365   m_SrcOffset = 0;
    366   m_bEOD = false;
    367   m_Operator = 0;
    368   return true;
    369 }
    370 uint8_t* CCodec_RLScanlineDecoder::v_GetNextLine() {
    371   if (m_SrcOffset == 0) {
    372     GetNextOperator();
    373   } else {
    374     if (m_bEOD) {
    375       return nullptr;
    376     }
    377   }
    378   FXSYS_memset(m_pScanline, 0, m_Pitch);
    379   uint32_t col_pos = 0;
    380   bool eol = false;
    381   while (m_SrcOffset < m_SrcSize && !eol) {
    382     if (m_Operator < 128) {
    383       uint32_t copy_len = m_Operator + 1;
    384       if (col_pos + copy_len >= m_dwLineBytes) {
    385         copy_len = m_dwLineBytes - col_pos;
    386         eol = true;
    387       }
    388       if (copy_len >= m_SrcSize - m_SrcOffset) {
    389         copy_len = m_SrcSize - m_SrcOffset;
    390         m_bEOD = true;
    391       }
    392       FXSYS_memcpy(m_pScanline + col_pos, m_pSrcBuf + m_SrcOffset, copy_len);
    393       col_pos += copy_len;
    394       UpdateOperator((uint8_t)copy_len);
    395     } else if (m_Operator > 128) {
    396       int fill = 0;
    397       if (m_SrcOffset - 1 < m_SrcSize - 1) {
    398         fill = m_pSrcBuf[m_SrcOffset];
    399       }
    400       uint32_t duplicate_len = 257 - m_Operator;
    401       if (col_pos + duplicate_len >= m_dwLineBytes) {
    402         duplicate_len = m_dwLineBytes - col_pos;
    403         eol = true;
    404       }
    405       FXSYS_memset(m_pScanline + col_pos, fill, duplicate_len);
    406       col_pos += duplicate_len;
    407       UpdateOperator((uint8_t)duplicate_len);
    408     } else {
    409       m_bEOD = true;
    410       break;
    411     }
    412   }
    413   return m_pScanline;
    414 }
    415 void CCodec_RLScanlineDecoder::GetNextOperator() {
    416   if (m_SrcOffset >= m_SrcSize) {
    417     m_Operator = 128;
    418     return;
    419   }
    420   m_Operator = m_pSrcBuf[m_SrcOffset];
    421   m_SrcOffset++;
    422 }
    423 void CCodec_RLScanlineDecoder::UpdateOperator(uint8_t used_bytes) {
    424   if (used_bytes == 0) {
    425     return;
    426   }
    427   if (m_Operator < 128) {
    428     ASSERT((uint32_t)m_Operator + 1 >= used_bytes);
    429     if (used_bytes == m_Operator + 1) {
    430       m_SrcOffset += used_bytes;
    431       GetNextOperator();
    432       return;
    433     }
    434     m_Operator -= used_bytes;
    435     m_SrcOffset += used_bytes;
    436     if (m_SrcOffset >= m_SrcSize) {
    437       m_Operator = 128;
    438     }
    439     return;
    440   }
    441   uint8_t count = 257 - m_Operator;
    442   ASSERT((uint32_t)count >= used_bytes);
    443   if (used_bytes == count) {
    444     m_SrcOffset++;
    445     GetNextOperator();
    446     return;
    447   }
    448   count -= used_bytes;
    449   m_Operator = 257 - count;
    450 }
    451 
    452 std::unique_ptr<CCodec_ScanlineDecoder>
    453 CCodec_BasicModule::CreateRunLengthDecoder(const uint8_t* src_buf,
    454                                            uint32_t src_size,
    455                                            int width,
    456                                            int height,
    457                                            int nComps,
    458                                            int bpc) {
    459   auto pDecoder = pdfium::MakeUnique<CCodec_RLScanlineDecoder>();
    460   if (!pDecoder->Create(src_buf, src_size, width, height, nComps, bpc))
    461     return nullptr;
    462 
    463   return std::move(pDecoder);
    464 }
    465