CPDF_IndexedCS::~CPDF_IndexedCS() { if (m_pCompMinMax) { FX_Free(m_pCompMinMax); } }
CWeightTable::~CWeightTable() { FX_Free(m_pWeightTables); }
CJBig2_SymbolDict* CJBig2_SDDProc::decode_Arith( CJBig2_ArithDecoder* pArithDecoder, std::vector<JBig2ArithCtx>* gbContext, std::vector<JBig2ArithCtx>* grContext) { CJBig2_Image** SDNEWSYMS; FX_DWORD HCHEIGHT, NSYMSDECODED; int32_t HCDH; FX_DWORD SYMWIDTH, TOTWIDTH; int32_t DW; CJBig2_Image* BS; FX_DWORD I, J, REFAGGNINST; FX_BOOL* EXFLAGS; FX_DWORD EXINDEX; FX_BOOL CUREXFLAG; FX_DWORD EXRUNLENGTH; FX_DWORD nTmp; FX_DWORD SBNUMSYMS; uint8_t SBSYMCODELEN; int32_t RDXI, RDYI; CJBig2_Image** SBSYMS; std::unique_ptr<CJBig2_ArithIaidDecoder> IAID; std::unique_ptr<CJBig2_SymbolDict> pDict; std::unique_ptr<CJBig2_ArithIntDecoder> IADH(new CJBig2_ArithIntDecoder); std::unique_ptr<CJBig2_ArithIntDecoder> IADW(new CJBig2_ArithIntDecoder); std::unique_ptr<CJBig2_ArithIntDecoder> IAAI(new CJBig2_ArithIntDecoder); std::unique_ptr<CJBig2_ArithIntDecoder> IARDX(new CJBig2_ArithIntDecoder); std::unique_ptr<CJBig2_ArithIntDecoder> IARDY(new CJBig2_ArithIntDecoder); std::unique_ptr<CJBig2_ArithIntDecoder> IAEX(new CJBig2_ArithIntDecoder); std::unique_ptr<CJBig2_ArithIntDecoder> IADT(new CJBig2_ArithIntDecoder); std::unique_ptr<CJBig2_ArithIntDecoder> IAFS(new CJBig2_ArithIntDecoder); std::unique_ptr<CJBig2_ArithIntDecoder> IADS(new CJBig2_ArithIntDecoder); std::unique_ptr<CJBig2_ArithIntDecoder> IAIT(new CJBig2_ArithIntDecoder); std::unique_ptr<CJBig2_ArithIntDecoder> IARI(new CJBig2_ArithIntDecoder); std::unique_ptr<CJBig2_ArithIntDecoder> IARDW(new CJBig2_ArithIntDecoder); std::unique_ptr<CJBig2_ArithIntDecoder> IARDH(new CJBig2_ArithIntDecoder); nTmp = 0; while ((FX_DWORD)(1 << nTmp) < (SDNUMINSYMS + SDNUMNEWSYMS)) { nTmp++; } IAID.reset(new CJBig2_ArithIaidDecoder((uint8_t)nTmp)); SDNEWSYMS = FX_Alloc(CJBig2_Image*, SDNUMNEWSYMS); FXSYS_memset(SDNEWSYMS, 0, SDNUMNEWSYMS * sizeof(CJBig2_Image*)); HCHEIGHT = 0; NSYMSDECODED = 0; while (NSYMSDECODED < SDNUMNEWSYMS) { BS = nullptr; IADH->decode(pArithDecoder, &HCDH); HCHEIGHT = HCHEIGHT + HCDH; if ((int)HCHEIGHT < 0 || (int)HCHEIGHT > JBIG2_MAX_IMAGE_SIZE) { goto failed; } SYMWIDTH = 0; TOTWIDTH = 0; for (;;) { if (!IADW->decode(pArithDecoder, &DW)) break; if (NSYMSDECODED >= SDNUMNEWSYMS) goto failed; SYMWIDTH = SYMWIDTH + DW; if ((int)SYMWIDTH < 0 || (int)SYMWIDTH > JBIG2_MAX_IMAGE_SIZE) goto failed; if (HCHEIGHT == 0 || SYMWIDTH == 0) { TOTWIDTH = TOTWIDTH + SYMWIDTH; SDNEWSYMS[NSYMSDECODED] = nullptr; NSYMSDECODED = NSYMSDECODED + 1; continue; } TOTWIDTH = TOTWIDTH + SYMWIDTH; if (SDREFAGG == 0) { std::unique_ptr<CJBig2_GRDProc> pGRD(new CJBig2_GRDProc()); pGRD->MMR = 0; pGRD->GBW = SYMWIDTH; pGRD->GBH = HCHEIGHT; pGRD->GBTEMPLATE = SDTEMPLATE; pGRD->TPGDON = 0; pGRD->USESKIP = 0; pGRD->GBAT[0] = SDAT[0]; pGRD->GBAT[1] = SDAT[1]; pGRD->GBAT[2] = SDAT[2]; pGRD->GBAT[3] = SDAT[3]; pGRD->GBAT[4] = SDAT[4]; pGRD->GBAT[5] = SDAT[5]; pGRD->GBAT[6] = SDAT[6]; pGRD->GBAT[7] = SDAT[7]; BS = pGRD->decode_Arith(pArithDecoder, gbContext->data()); if (!BS) { goto failed; } } else { IAAI->decode(pArithDecoder, (int*)&REFAGGNINST); if (REFAGGNINST > 1) { std::unique_ptr<CJBig2_TRDProc> pDecoder(new CJBig2_TRDProc()); pDecoder->SBHUFF = SDHUFF; pDecoder->SBREFINE = 1; pDecoder->SBW = SYMWIDTH; pDecoder->SBH = HCHEIGHT; pDecoder->SBNUMINSTANCES = REFAGGNINST; pDecoder->SBSTRIPS = 1; pDecoder->SBNUMSYMS = SDNUMINSYMS + NSYMSDECODED; SBNUMSYMS = pDecoder->SBNUMSYMS; nTmp = 0; while ((FX_DWORD)(1 << nTmp) < SBNUMSYMS) { nTmp++; } SBSYMCODELEN = (uint8_t)nTmp; pDecoder->SBSYMCODELEN = SBSYMCODELEN; SBSYMS = FX_Alloc(CJBig2_Image*, SBNUMSYMS); JBIG2_memcpy(SBSYMS, SDINSYMS, SDNUMINSYMS * sizeof(CJBig2_Image*)); JBIG2_memcpy(SBSYMS + SDNUMINSYMS, SDNEWSYMS, NSYMSDECODED * sizeof(CJBig2_Image*)); pDecoder->SBSYMS = SBSYMS; pDecoder->SBDEFPIXEL = 0; pDecoder->SBCOMBOP = JBIG2_COMPOSE_OR; pDecoder->TRANSPOSED = 0; pDecoder->REFCORNER = JBIG2_CORNER_TOPLEFT; pDecoder->SBDSOFFSET = 0; std::unique_ptr<CJBig2_HuffmanTable> SBHUFFFS(new CJBig2_HuffmanTable( HuffmanTable_B6, FX_ArraySize(HuffmanTable_B6), HuffmanTable_HTOOB_B6)); std::unique_ptr<CJBig2_HuffmanTable> SBHUFFDS(new CJBig2_HuffmanTable( HuffmanTable_B8, FX_ArraySize(HuffmanTable_B8), HuffmanTable_HTOOB_B8)); std::unique_ptr<CJBig2_HuffmanTable> SBHUFFDT(new CJBig2_HuffmanTable( HuffmanTable_B11, FX_ArraySize(HuffmanTable_B11), HuffmanTable_HTOOB_B11)); std::unique_ptr<CJBig2_HuffmanTable> SBHUFFRDW( new CJBig2_HuffmanTable(HuffmanTable_B15, FX_ArraySize(HuffmanTable_B15), HuffmanTable_HTOOB_B15)); std::unique_ptr<CJBig2_HuffmanTable> SBHUFFRDH( new CJBig2_HuffmanTable(HuffmanTable_B15, FX_ArraySize(HuffmanTable_B15), HuffmanTable_HTOOB_B15)); std::unique_ptr<CJBig2_HuffmanTable> SBHUFFRDX( new CJBig2_HuffmanTable(HuffmanTable_B15, FX_ArraySize(HuffmanTable_B15), HuffmanTable_HTOOB_B15)); std::unique_ptr<CJBig2_HuffmanTable> SBHUFFRDY( new CJBig2_HuffmanTable(HuffmanTable_B15, FX_ArraySize(HuffmanTable_B15), HuffmanTable_HTOOB_B15)); std::unique_ptr<CJBig2_HuffmanTable> SBHUFFRSIZE( new CJBig2_HuffmanTable(HuffmanTable_B1, FX_ArraySize(HuffmanTable_B1), HuffmanTable_HTOOB_B1)); pDecoder->SBHUFFFS = SBHUFFFS.get(); pDecoder->SBHUFFDS = SBHUFFDS.get(); pDecoder->SBHUFFDT = SBHUFFDT.get(); pDecoder->SBHUFFRDW = SBHUFFRDW.get(); pDecoder->SBHUFFRDH = SBHUFFRDH.get(); pDecoder->SBHUFFRDX = SBHUFFRDX.get(); pDecoder->SBHUFFRDY = SBHUFFRDY.get(); pDecoder->SBHUFFRSIZE = SBHUFFRSIZE.get(); pDecoder->SBRTEMPLATE = SDRTEMPLATE; pDecoder->SBRAT[0] = SDRAT[0]; pDecoder->SBRAT[1] = SDRAT[1]; pDecoder->SBRAT[2] = SDRAT[2]; pDecoder->SBRAT[3] = SDRAT[3]; JBig2IntDecoderState ids; ids.IADT = IADT.get(); ids.IAFS = IAFS.get(); ids.IADS = IADS.get(); ids.IAIT = IAIT.get(); ids.IARI = IARI.get(); ids.IARDW = IARDW.get(); ids.IARDH = IARDH.get(); ids.IARDX = IARDX.get(); ids.IARDY = IARDY.get(); ids.IAID = IAID.get(); BS = pDecoder->decode_Arith(pArithDecoder, grContext->data(), &ids); if (!BS) { FX_Free(SBSYMS); goto failed; } FX_Free(SBSYMS); } else if (REFAGGNINST == 1) { SBNUMSYMS = SDNUMINSYMS + NSYMSDECODED; FX_DWORD IDI; IAID->decode(pArithDecoder, &IDI); IARDX->decode(pArithDecoder, &RDXI); IARDY->decode(pArithDecoder, &RDYI); if (IDI >= SBNUMSYMS) { goto failed; } SBSYMS = FX_Alloc(CJBig2_Image*, SBNUMSYMS); JBIG2_memcpy(SBSYMS, SDINSYMS, SDNUMINSYMS * sizeof(CJBig2_Image*)); JBIG2_memcpy(SBSYMS + SDNUMINSYMS, SDNEWSYMS, NSYMSDECODED * sizeof(CJBig2_Image*)); if (!SBSYMS[IDI]) { FX_Free(SBSYMS); goto failed; } std::unique_ptr<CJBig2_GRRDProc> pGRRD(new CJBig2_GRRDProc()); pGRRD->GRW = SYMWIDTH; pGRRD->GRH = HCHEIGHT; pGRRD->GRTEMPLATE = SDRTEMPLATE; pGRRD->GRREFERENCE = SBSYMS[IDI]; pGRRD->GRREFERENCEDX = RDXI; pGRRD->GRREFERENCEDY = RDYI; pGRRD->TPGRON = 0; pGRRD->GRAT[0] = SDRAT[0]; pGRRD->GRAT[1] = SDRAT[1]; pGRRD->GRAT[2] = SDRAT[2]; pGRRD->GRAT[3] = SDRAT[3]; BS = pGRRD->decode(pArithDecoder, grContext->data()); if (!BS) { FX_Free(SBSYMS); goto failed; } FX_Free(SBSYMS); } }
CPSOutput::~CPSOutput() { if (m_pBuf) { FX_Free(m_pBuf); } }
FX_BASEARRAYDATA::~FX_BASEARRAYDATA() { FX_Free(pBuffer); }
void color_apply_icc_profile(opj_image_t* image) { cmsHPROFILE out_prof; cmsUInt32Number in_type; cmsUInt32Number out_type; int* r; int* g; int* b; int max; cmsHPROFILE in_prof = cmsOpenProfileFromMem(image->icc_profile_buf, image->icc_profile_len); if (!in_prof) { return; } cmsColorSpaceSignature out_space = cmsGetColorSpace(in_prof); cmsUInt32Number intent = cmsGetHeaderRenderingIntent(in_prof); int max_w = (int)image->comps[0].w; int max_h = (int)image->comps[0].h; int prec = (int)image->comps[0].prec; OPJ_COLOR_SPACE oldspace = image->color_space; if (out_space == cmsSigRgbData) { if (prec <= 8) { in_type = TYPE_RGB_8; out_type = TYPE_RGB_8; } else { in_type = TYPE_RGB_16; out_type = TYPE_RGB_16; } out_prof = cmsCreate_sRGBProfile(); image->color_space = OPJ_CLRSPC_SRGB; } else if (out_space == cmsSigGrayData) { if (prec <= 8) { in_type = TYPE_GRAY_8; out_type = TYPE_RGB_8; } else { in_type = TYPE_GRAY_16; out_type = TYPE_RGB_16; } out_prof = cmsCreate_sRGBProfile(); image->color_space = OPJ_CLRSPC_SRGB; } else if (out_space == cmsSigYCbCrData) { in_type = TYPE_YCbCr_16; out_type = TYPE_RGB_16; out_prof = cmsCreate_sRGBProfile(); image->color_space = OPJ_CLRSPC_SRGB; } else { return; } cmsHTRANSFORM transform = cmsCreateTransform(in_prof, in_type, out_prof, out_type, intent, 0); cmsCloseProfile(in_prof); cmsCloseProfile(out_prof); if (!transform) { image->color_space = oldspace; return; } if (image->numcomps > 2) { if (prec <= 8) { unsigned char *inbuf, *outbuf, *in, *out; max = max_w * max_h; cmsUInt32Number nr_samples = max * 3 * sizeof(unsigned char); in = inbuf = FX_Alloc(unsigned char, nr_samples); out = outbuf = FX_Alloc(unsigned char, nr_samples); r = image->comps[0].data; g = image->comps[1].data; b = image->comps[2].data; for (int i = 0; i < max; ++i) { *in++ = (unsigned char)*r++; *in++ = (unsigned char)*g++; *in++ = (unsigned char)*b++; } cmsDoTransform(transform, inbuf, outbuf, (cmsUInt32Number)max); r = image->comps[0].data; g = image->comps[1].data; b = image->comps[2].data; for (int i = 0; i < max; ++i) { *r++ = (int)*out++; *g++ = (int)*out++; *b++ = (int)*out++; } FX_Free(inbuf); FX_Free(outbuf); } else {
static void my_free_func (void* opaque, void* address) { FX_Free(address); }
CPDF_Function::~CPDF_Function() { FX_Free(m_pDomains); FX_Free(m_pRanges); }
void CJS_GlobalData::LoadGlobalPersistentVariables() { uint8_t* pBuffer = nullptr; int32_t nLength = 0; LoadFileBuffer(m_sFilePath.c_str(), pBuffer, nLength); CRYPT_ArcFourCryptBlock(pBuffer, nLength, JS_RC4KEY, sizeof(JS_RC4KEY)); if (pBuffer) { uint8_t* p = pBuffer; uint16_t wType = *((uint16_t*)p); p += sizeof(uint16_t); if (wType == (uint16_t)(('X' << 8) | 'F')) { uint16_t wVersion = *((uint16_t*)p); p += sizeof(uint16_t); ASSERT(wVersion <= 2); uint32_t dwCount = *((uint32_t*)p); p += sizeof(uint32_t); uint32_t dwSize = *((uint32_t*)p); p += sizeof(uint32_t); if (dwSize == nLength - sizeof(uint16_t) * 2 - sizeof(uint32_t) * 2) { for (int32_t i = 0, sz = dwCount; i < sz; i++) { if (p > pBuffer + nLength) break; uint32_t dwNameLen = *((uint32_t*)p); p += sizeof(uint32_t); if (p + dwNameLen > pBuffer + nLength) break; CFX_ByteString sEntry = CFX_ByteString(p, dwNameLen); p += sizeof(char) * dwNameLen; JS_GlobalDataType wDataType = static_cast<JS_GlobalDataType>(*((uint16_t*)p)); p += sizeof(uint16_t); switch (wDataType) { case JS_GlobalDataType::NUMBER: { double dData = 0; switch (wVersion) { case 1: { uint32_t dwData = *((uint32_t*)p); p += sizeof(uint32_t); dData = dwData; } break; case 2: { dData = *((double*)p); p += sizeof(double); } break; } SetGlobalVariableNumber(sEntry, dData); SetGlobalVariablePersistent(sEntry, TRUE); } break; case JS_GlobalDataType::BOOLEAN: { uint16_t wData = *((uint16_t*)p); p += sizeof(uint16_t); SetGlobalVariableBoolean(sEntry, (bool)(wData == 1)); SetGlobalVariablePersistent(sEntry, TRUE); } break; case JS_GlobalDataType::STRING: { uint32_t dwLength = *((uint32_t*)p); p += sizeof(uint32_t); if (p + dwLength > pBuffer + nLength) break; SetGlobalVariableString(sEntry, CFX_ByteString(p, dwLength)); SetGlobalVariablePersistent(sEntry, TRUE); p += sizeof(char) * dwLength; } break; case JS_GlobalDataType::NULLOBJ: { SetGlobalVariableNull(sEntry); SetGlobalVariablePersistent(sEntry, TRUE); } case JS_GlobalDataType::OBJECT: break; } } } } FX_Free(pBuffer); } }
CPDF_ExpIntFunc::~CPDF_ExpIntFunc() { FX_Free(m_pBeginValues); FX_Free(m_pEndValues); }
CPDF_StitchFunc::~CPDF_StitchFunc() { FX_Free(m_pBounds); FX_Free(m_pEncode); }
CPDF_Stream* CPDF_StreamParser::ReadInlineStream(CPDF_Document* pDoc, CPDF_Dictionary* pDict, CPDF_Object* pCSObj, FX_BOOL bDecode) { if (m_Pos == m_Size) { return NULL; } if (PDF_CharType[m_pBuf[m_Pos]] == 'W') { m_Pos ++; } CFX_ByteString Decoder; CPDF_Dictionary* pParam = NULL; CPDF_Object* pFilter = pDict->GetElementValue(FX_BSTRC("Filter")); if (pFilter == NULL) { } else if (pFilter->GetType() == PDFOBJ_ARRAY) { Decoder = ((CPDF_Array*)pFilter)->GetString(0); CPDF_Array* pParams = pDict->GetArray(FX_BSTRC("DecodeParms")); if (pParams) { pParam = pParams->GetDict(0); } } else { Decoder = pFilter->GetString(); pParam = pDict->GetDict(FX_BSTRC("DecodeParms")); } FX_DWORD width = pDict->GetInteger(FX_BSTRC("Width")); FX_DWORD height = pDict->GetInteger(FX_BSTRC("Height")); FX_DWORD OrigSize = 0; if (pCSObj != NULL) { FX_DWORD bpc = pDict->GetInteger(FX_BSTRC("BitsPerComponent")); FX_DWORD nComponents = 1; CPDF_ColorSpace* pCS = pDoc->LoadColorSpace(pCSObj); if (pCS == NULL) { nComponents = 3; } else { nComponents = pCS->CountComponents(); pDoc->GetPageData()->ReleaseColorSpace(pCSObj); } FX_DWORD pitch = width; if (bpc && pitch > INT_MAX / bpc) { return NULL; } pitch *= bpc; if (nComponents && pitch > INT_MAX / nComponents) { return NULL; } pitch *= nComponents; if (pitch > INT_MAX - 7) { return NULL; } pitch += 7; pitch /= 8; OrigSize = pitch; } else { if (width > INT_MAX - 7) { return NULL; } OrigSize = ((width + 7) / 8); } if (height && OrigSize > INT_MAX / height) { return NULL; } OrigSize *= height; FX_LPBYTE pData = NULL; FX_DWORD dwStreamSize; if (Decoder.IsEmpty()) { if (OrigSize > m_Size - m_Pos) { OrigSize = m_Size - m_Pos; } pData = FX_Alloc(FX_BYTE, OrigSize); FXSYS_memcpy32(pData, m_pBuf + m_Pos, OrigSize); dwStreamSize = OrigSize; m_Pos += OrigSize; } else { FX_DWORD dwDestSize = OrigSize; dwStreamSize = PDF_DecodeInlineStream(m_pBuf + m_Pos, m_Size - m_Pos, width, height, Decoder, pParam, pData, dwDestSize); if ((int)dwStreamSize < 0) { return NULL; } if (bDecode) { m_Pos += dwStreamSize; dwStreamSize = dwDestSize; if (pFilter->GetType() == PDFOBJ_ARRAY) { ((CPDF_Array*)pFilter)->RemoveAt(0); CPDF_Array* pParams = pDict->GetArray(FX_BSTRC("DecodeParms")); if (pParams) { pParams->RemoveAt(0); } } else { pDict->RemoveAt(FX_BSTRC("Filter")); pDict->RemoveAt(FX_BSTRC("DecodeParms")); } } else { if (pData) { FX_Free(pData); } FX_DWORD dwSavePos = m_Pos; m_Pos += dwStreamSize; while (1) { FX_DWORD dwPrevPos = m_Pos; CPDF_StreamParser::SyntaxType type = ParseNextElement(); if (type == CPDF_StreamParser::EndOfData) { break; } if (type != CPDF_StreamParser::Keyword) { dwStreamSize += m_Pos - dwPrevPos; continue; } if (GetWordSize() == 2 && GetWordBuf()[0] == 'E' && GetWordBuf()[1] == 'I') { m_Pos = dwPrevPos; break; } dwStreamSize += m_Pos - dwPrevPos; } m_Pos = dwSavePos; pData = FX_Alloc(FX_BYTE, dwStreamSize); FXSYS_memcpy32(pData, m_pBuf + m_Pos, dwStreamSize); m_Pos += dwStreamSize; } } pDict->SetAtInteger(FX_BSTRC("Length"), (int)dwStreamSize); return CPDF_Stream::Create(pData, dwStreamSize, pDict); }
static void jpeg_free_func(void* p) { FX_Free(p); }
static void _JpegEncode(const CFX_DIBSource* pSource, uint8_t*& dest_buf, FX_STRSIZE& dest_size, int quality, const uint8_t* icc_buf, FX_DWORD icc_length) { struct jpeg_error_mgr jerr; jerr.error_exit = _error_do_nothing; jerr.emit_message = _error_do_nothing1; jerr.output_message = _error_do_nothing; jerr.format_message = _error_do_nothing2; jerr.reset_error_mgr = _error_do_nothing; struct jpeg_compress_struct cinfo; memset(&cinfo, 0, sizeof(cinfo)); cinfo.err = &jerr; jpeg_create_compress(&cinfo); int Bpp = pSource->GetBPP() / 8; FX_DWORD nComponents = Bpp >= 3 ? (pSource->IsCmykImage() ? 4 : 3) : 1; FX_DWORD pitch = pSource->GetPitch(); FX_DWORD width = pdfium::base::checked_cast<FX_DWORD>(pSource->GetWidth()); FX_DWORD height = pdfium::base::checked_cast<FX_DWORD>(pSource->GetHeight()); FX_SAFE_DWORD safe_buf_len = width; safe_buf_len *= height; safe_buf_len *= nComponents; safe_buf_len += 1024; if (icc_length) { safe_buf_len += 255 * 18; safe_buf_len += icc_length; } FX_DWORD dest_buf_length = 0; if (!safe_buf_len.IsValid()) { dest_buf = nullptr; } else { dest_buf_length = safe_buf_len.ValueOrDie(); dest_buf = FX_TryAlloc(uint8_t, dest_buf_length); const int MIN_TRY_BUF_LEN = 1024; while (!dest_buf && dest_buf_length > MIN_TRY_BUF_LEN) { dest_buf_length >>= 1; dest_buf = FX_TryAlloc(uint8_t, dest_buf_length); } } if (!dest_buf) { FX_OutOfMemoryTerminate(); } struct jpeg_destination_mgr dest; dest.init_destination = _dest_do_nothing; dest.term_destination = _dest_do_nothing; dest.empty_output_buffer = _dest_empty; dest.next_output_byte = dest_buf; dest.free_in_buffer = dest_buf_length; cinfo.dest = &dest; cinfo.image_width = width; cinfo.image_height = height; cinfo.input_components = nComponents; if (nComponents == 1) { cinfo.in_color_space = JCS_GRAYSCALE; } else if (nComponents == 3) { cinfo.in_color_space = JCS_RGB; } else { cinfo.in_color_space = JCS_CMYK; } uint8_t* line_buf = NULL; if (nComponents > 1) { line_buf = FX_Alloc2D(uint8_t, width, nComponents); } jpeg_set_defaults(&cinfo); if (quality != 75) { jpeg_set_quality(&cinfo, quality, TRUE); } jpeg_start_compress(&cinfo, TRUE); _JpegEmbedIccProfile(&cinfo, icc_buf, icc_length); JSAMPROW row_pointer[1]; JDIMENSION row; while (cinfo.next_scanline < cinfo.image_height) { const uint8_t* src_scan = pSource->GetScanline(cinfo.next_scanline); if (nComponents > 1) { uint8_t* dest_scan = line_buf; if (nComponents == 3) { for (int i = 0; i < width; i++) { dest_scan[0] = src_scan[2]; dest_scan[1] = src_scan[1]; dest_scan[2] = src_scan[0]; dest_scan += 3; src_scan += Bpp; } } else { for (int i = 0; i < pitch; i++) { *dest_scan++ = ~*src_scan++; } } row_pointer[0] = line_buf; } else { row_pointer[0] = (uint8_t*)src_scan; } row = cinfo.next_scanline; jpeg_write_scanlines(&cinfo, row_pointer, 1); if (cinfo.next_scanline == row) { dest_buf = FX_Realloc(uint8_t, dest_buf, dest_buf_length + JPEG_BLOCK_SIZE); dest.next_output_byte = dest_buf + dest_buf_length - dest.free_in_buffer; dest_buf_length += JPEG_BLOCK_SIZE; dest.free_in_buffer += JPEG_BLOCK_SIZE; } } jpeg_finish_compress(&cinfo); jpeg_destroy_compress(&cinfo); FX_Free(line_buf); dest_size = dest_buf_length - (FX_STRSIZE)dest.free_in_buffer; }
static void sycc422_to_rgb(opj_image_t* img) { if (!sycc422_size_is_valid(img)) return; int prec = img->comps[0].prec; if (prec <= 0 || prec >= 32) return; int offset = 1 << (prec - 1); int upb = (1 << prec) - 1; OPJ_UINT32 maxw = img->comps[0].w; OPJ_UINT32 maxh = img->comps[0].h; FX_SAFE_SIZE_T max_size = maxw; max_size *= maxh; if (!max_size.IsValid()) return; const int* y = img->comps[0].data; const int* cb = img->comps[1].data; const int* cr = img->comps[2].data; if (!y || !cb || !cr) return; int *d0, *d1, *d2, *r, *g, *b; d0 = r = FX_Alloc(int, max_size.ValueOrDie()); d1 = g = FX_Alloc(int, max_size.ValueOrDie()); d2 = b = FX_Alloc(int, max_size.ValueOrDie()); for (uint32_t i = 0; i < maxh; ++i) { OPJ_UINT32 j; for (j = 0; j < (maxw & ~static_cast<OPJ_UINT32>(1)); j += 2) { sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b); ++y; ++r; ++g; ++b; sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b); ++y; ++r; ++g; ++b; ++cb; ++cr; } if (j < maxw) { sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b); ++y; ++r; ++g; ++b; ++cb; ++cr; } } FX_Free(img->comps[0].data); img->comps[0].data = d0; FX_Free(img->comps[1].data); img->comps[1].data = d1; FX_Free(img->comps[2].data); img->comps[2].data = d2; img->comps[1].w = maxw; img->comps[1].h = maxh; img->comps[2].w = maxw; img->comps[2].h = maxh; img->comps[1].dx = img->comps[0].dx; img->comps[2].dx = img->comps[0].dx; img->comps[1].dy = img->comps[0].dy; img->comps[2].dy = img->comps[0].dy; }
void Revision6_Hash(const uint8_t* password, FX_DWORD size, const uint8_t* salt, const uint8_t* vector, uint8_t* hash) { int iBlockSize = 32; uint8_t sha[128]; CRYPT_SHA256Start(sha); CRYPT_SHA256Update(sha, password, size); CRYPT_SHA256Update(sha, salt, 8); if (vector) { CRYPT_SHA256Update(sha, vector, 48); } uint8_t digest[32]; CRYPT_SHA256Finish(sha, digest); CFX_ByteTextBuf buf; uint8_t* input = digest; uint8_t* key = input; uint8_t* iv = input + 16; uint8_t* E = buf.GetBuffer(); int iBufLen = buf.GetLength(); CFX_ByteTextBuf interDigest; int i = 0; uint8_t* aes = FX_Alloc(uint8_t, 2048); while (i < 64 || i < E[iBufLen - 1] + 32) { int iRoundSize = size + iBlockSize; if (vector) { iRoundSize += 48; } iBufLen = iRoundSize * 64; buf.EstimateSize(iBufLen); E = buf.GetBuffer(); CFX_ByteTextBuf content; for (int j = 0; j < 64; ++j) { content.AppendBlock(password, size); content.AppendBlock(input, iBlockSize); if (vector) { content.AppendBlock(vector, 48); } } CRYPT_AESSetKey(aes, 16, key, 16, TRUE); CRYPT_AESSetIV(aes, iv); CRYPT_AESEncrypt(aes, E, content.GetBuffer(), iBufLen); int iHash = 0; switch (BigOrder64BitsMod3(E)) { case 0: iHash = 0; iBlockSize = 32; break; case 1: iHash = 1; iBlockSize = 48; break; default: iHash = 2; iBlockSize = 64; break; } interDigest.EstimateSize(iBlockSize); input = interDigest.GetBuffer(); if (iHash == 0) { CRYPT_SHA256Generate(E, iBufLen, input); } else if (iHash == 1) { CRYPT_SHA384Generate(E, iBufLen, input); } else if (iHash == 2) { CRYPT_SHA512Generate(E, iBufLen, input); } key = input; iv = input + 16; ++i; } FX_Free(aes); if (hash) { FXSYS_memcpy(hash, input, 32); } }
void sycc420_to_rgb(opj_image_t* img) { if (!sycc420_size_is_valid(img)) return; OPJ_UINT32 prec = img->comps[0].prec; if (!prec) return; OPJ_UINT32 offset = 1 << (prec - 1); OPJ_UINT32 upb = (1 << prec) - 1; OPJ_UINT32 yw = img->comps[0].w; OPJ_UINT32 yh = img->comps[0].h; OPJ_UINT32 cbw = img->comps[1].w; OPJ_UINT32 cbh = img->comps[1].h; OPJ_UINT32 crw = img->comps[2].w; bool extw = sycc420_must_extend_cbcr(yw, cbw); bool exth = sycc420_must_extend_cbcr(yh, cbh); FX_SAFE_UINT32 safeSize = yw; safeSize *= yh; if (!safeSize.IsValid()) return; int* r = FX_Alloc(int, safeSize.ValueOrDie()); int* g = FX_Alloc(int, safeSize.ValueOrDie()); int* b = FX_Alloc(int, safeSize.ValueOrDie()); int* d0 = r; int* d1 = g; int* d2 = b; const int* y = img->comps[0].data; const int* cb = img->comps[1].data; const int* cr = img->comps[2].data; if (!y || !cb || !cr) return; const int* ny = nullptr; int* nr = nullptr; int* ng = nullptr; int* nb = nullptr; OPJ_UINT32 i = 0; OPJ_UINT32 j = 0; for (i = 0; i < (yh & ~(OPJ_UINT32)1); i += 2) { ny = y + yw; nr = r + yw; ng = g + yw; nb = b + yw; for (j = 0; j < (yw & ~(OPJ_UINT32)1); j += 2) { sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b); ++y; ++r; ++g; ++b; sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b); ++y; ++r; ++g; ++b; sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb); ++ny; ++nr; ++ng; ++nb; sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb); ++ny; ++nr; ++ng; ++nb; ++cb; ++cr; } if (j < yw) { if (extw) { --cb; --cr; } sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b); ++y; ++r; ++g; ++b; sycc_to_rgb(offset, upb, *ny, *cb, *cr, nr, ng, nb); ++ny; ++nr; ++ng; ++nb; ++cb; ++cr; } y += yw; r += yw; g += yw; b += yw; } if (i < yh) { if (exth) { cb -= cbw; cr -= crw; } for (j = 0; j < (yw & ~(OPJ_UINT32)1); j += 2) { sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b); ++y; ++r; ++g; ++b; sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b); ++y; ++r; ++g; ++b; ++cb; ++cr; } if (j < yw) { if (extw) { --cb; --cr; } sycc_to_rgb(offset, upb, *y, *cb, *cr, r, g, b); } } FX_Free(img->comps[0].data); img->comps[0].data = d0; FX_Free(img->comps[1].data); img->comps[1].data = d1; FX_Free(img->comps[2].data); img->comps[2].data = d2; img->comps[1].w = yw; img->comps[1].h = yh; img->comps[2].w = yw; img->comps[2].h = yh; img->comps[1].w = yw; img->comps[1].h = yh; img->comps[2].w = yw; img->comps[2].h = yh; img->comps[1].dx = img->comps[0].dx; img->comps[2].dx = img->comps[0].dx; img->comps[1].dy = img->comps[0].dy; img->comps[2].dy = img->comps[0].dy; }
FX_BOOL CPDF_StandardSecurityHandler::AES256_CheckPassword( const uint8_t* password, FX_DWORD size, FX_BOOL bOwner, uint8_t* key) { CFX_ByteString okey = m_pEncryptDict ? m_pEncryptDict->GetString(FX_BSTRC("O")) : CFX_ByteString(); if (okey.GetLength() < 48) { return FALSE; } CFX_ByteString ukey = m_pEncryptDict ? m_pEncryptDict->GetString(FX_BSTRC("U")) : CFX_ByteString(); if (ukey.GetLength() < 48) { return FALSE; } const uint8_t* pkey = bOwner ? (const uint8_t*)okey : (const uint8_t*)ukey; uint8_t sha[128]; uint8_t digest[32]; if (m_Revision >= 6) { Revision6_Hash(password, size, (const uint8_t*)pkey + 32, (bOwner ? (const uint8_t*)ukey : NULL), digest); } else { CRYPT_SHA256Start(sha); CRYPT_SHA256Update(sha, password, size); CRYPT_SHA256Update(sha, pkey + 32, 8); if (bOwner) { CRYPT_SHA256Update(sha, ukey, 48); } CRYPT_SHA256Finish(sha, digest); } if (FXSYS_memcmp(digest, pkey, 32) != 0) { return FALSE; } if (key == NULL) { return TRUE; } if (m_Revision >= 6) { Revision6_Hash(password, size, (const uint8_t*)pkey + 40, (bOwner ? (const uint8_t*)ukey : NULL), digest); } else { CRYPT_SHA256Start(sha); CRYPT_SHA256Update(sha, password, size); CRYPT_SHA256Update(sha, pkey + 40, 8); if (bOwner) { CRYPT_SHA256Update(sha, ukey, 48); } CRYPT_SHA256Finish(sha, digest); } CFX_ByteString ekey = m_pEncryptDict ? m_pEncryptDict->GetString(bOwner ? FX_BSTRC("OE") : FX_BSTRC("UE")) : CFX_ByteString(); if (ekey.GetLength() < 32) { return FALSE; } uint8_t* aes = FX_Alloc(uint8_t, 2048); CRYPT_AESSetKey(aes, 16, digest, 32, FALSE); uint8_t iv[16]; FXSYS_memset(iv, 0, 16); CRYPT_AESSetIV(aes, iv); CRYPT_AESDecrypt(aes, key, ekey, 32); CRYPT_AESSetKey(aes, 16, key, 32, FALSE); CRYPT_AESSetIV(aes, iv); CFX_ByteString perms = m_pEncryptDict->GetString(FX_BSTRC("Perms")); if (perms.IsEmpty()) { return FALSE; } uint8_t perms_buf[16]; FXSYS_memset(perms_buf, 0, sizeof(perms_buf)); FX_DWORD copy_len = sizeof(perms_buf); if (copy_len > (FX_DWORD)perms.GetLength()) { copy_len = perms.GetLength(); } FXSYS_memcpy(perms_buf, (const uint8_t*)perms, copy_len); uint8_t buf[16]; CRYPT_AESDecrypt(aes, buf, perms_buf, 16); FX_Free(aes); if (buf[9] != 'a' || buf[10] != 'd' || buf[11] != 'b') { return FALSE; } if (FXDWORD_GET_LSBFIRST(buf) != m_Permissions) { return FALSE; } if ((buf[8] == 'T' && !IsMetadataEncrypted()) || (buf[8] == 'F' && IsMetadataEncrypted())) { return FALSE; } return TRUE; }
CFX_DIBitmap* CPDF_RenderStatus::LoadSMask(CPDF_Dictionary* pSMaskDict, FX_RECT* pClipRect, const CFX_AffineMatrix* pMatrix) { if (pSMaskDict == NULL) { return NULL; } CFX_DIBitmap* pMask = NULL; int width = pClipRect->right - pClipRect->left; int height = pClipRect->bottom - pClipRect->top; FX_BOOL bLuminosity = FALSE; bLuminosity = pSMaskDict->GetConstString(FX_BSTRC("S")) != FX_BSTRC("Alpha"); CPDF_Stream* pGroup = pSMaskDict->GetStream(FX_BSTRC("G")); if (pGroup == NULL) { return NULL; } CPDF_Function* pFunc = NULL; CPDF_Object* pFuncObj = pSMaskDict->GetElementValue(FX_BSTRC("TR")); if (pFuncObj && (pFuncObj->GetType() == PDFOBJ_DICTIONARY || pFuncObj->GetType() == PDFOBJ_STREAM)) { pFunc = CPDF_Function::Load(pFuncObj); } CFX_AffineMatrix matrix = *pMatrix; matrix.TranslateI(-pClipRect->left, -pClipRect->top); CPDF_Form form(m_pContext->m_pDocument, m_pContext->m_pPageResources, pGroup); form.ParseContent(NULL, NULL, NULL, NULL); CFX_FxgeDevice bitmap_device; #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ if (!bitmap_device.Create(width, height, bLuminosity ? FXDIB_Rgb32 : FXDIB_8bppMask)) { return NULL; } #else if (!bitmap_device.Create(width, height, bLuminosity ? FXDIB_Rgb : FXDIB_8bppMask)) { return NULL; } #endif CFX_DIBitmap& bitmap = *bitmap_device.GetBitmap(); CPDF_Object* pCSObj = NULL; CPDF_ColorSpace* pCS = NULL; if (bLuminosity) { CPDF_Array* pBC = pSMaskDict->GetArray(FX_BSTRC("BC")); FX_ARGB back_color = 0xff000000; if (pBC) { CPDF_Dictionary* pDict = pGroup->GetDict(); if (pDict && pDict->GetDict(FX_BSTRC("Group"))) pCSObj = pDict->GetDict(FX_BSTRC("Group"))->GetElementValue(FX_BSTRC("CS")); else pCSObj = NULL; pCS = m_pContext->m_pDocument->LoadColorSpace(pCSObj); if (pCS) { FX_FLOAT R, G, B; FX_DWORD num_floats = 8; if (pCS->CountComponents() > (FX_INT32)num_floats) { num_floats = (FX_DWORD)pCS->CountComponents(); } CFX_FixedBufGrow<FX_FLOAT, 8> float_array(num_floats); FX_FLOAT* pFloats = float_array; FXSYS_memset32(pFloats, 0, num_floats * sizeof(FX_FLOAT)); int count = pBC->GetCount() > 8 ? 8 : pBC->GetCount(); for (int i = 0; i < count; i ++) { pFloats[i] = pBC->GetNumber(i); } pCS->GetRGB(pFloats, R, G, B); back_color = 0xff000000 | ((FX_INT32)(R * 255) << 16) | ((FX_INT32)(G * 255) << 8) | (FX_INT32)(B * 255); m_pContext->m_pDocument->GetPageData()->ReleaseColorSpace(pCSObj); } } bitmap.Clear(back_color); } else { bitmap.Clear(0); } CPDF_Dictionary* pFormResource = NULL; if (form.m_pFormDict) { pFormResource = form.m_pFormDict->GetDict(FX_BSTRC("Resources")); } CPDF_RenderOptions options; options.m_ColorMode = bLuminosity ? RENDER_COLOR_NORMAL : RENDER_COLOR_ALPHA; CPDF_RenderStatus status; status.Initialize(m_pContext, &bitmap_device, NULL, NULL, NULL, NULL, &options, 0, m_bDropObjects, pFormResource, TRUE, NULL, 0, pCS ? pCS->GetFamily() : 0, bLuminosity); status.RenderObjectList(&form, &matrix); pMask = FX_NEW CFX_DIBitmap; if (!pMask->Create(width, height, FXDIB_8bppMask)) { delete pMask; return NULL; } FX_LPBYTE dest_buf = pMask->GetBuffer(); int dest_pitch = pMask->GetPitch(); FX_LPBYTE src_buf = bitmap.GetBuffer(); int src_pitch = bitmap.GetPitch(); FX_LPBYTE pTransfer = FX_Alloc(FX_BYTE, 256); if (pFunc) { CFX_FixedBufGrow<FX_FLOAT, 16> results(pFunc->CountOutputs()); for (int i = 0; i < 256; i ++) { FX_FLOAT input = (FX_FLOAT)i / 255.0f; int nresult; pFunc->Call(&input, 1, results, nresult); pTransfer[i] = FXSYS_round(results[0] * 255); } } else { for (int i = 0; i < 256; i ++) { pTransfer[i] = i; } } if (bLuminosity) { int Bpp = bitmap.GetBPP() / 8; for (int row = 0; row < height; row ++) { FX_LPBYTE dest_pos = dest_buf + row * dest_pitch; FX_LPBYTE src_pos = src_buf + row * src_pitch; for (int col = 0; col < width; col ++) { *dest_pos ++ = pTransfer[FXRGB2GRAY(src_pos[2], src_pos[1], *src_pos)]; src_pos += Bpp; } } } else if (pFunc) { int size = dest_pitch * height; for (int i = 0; i < size; i ++) { dest_buf[i] = pTransfer[src_buf[i]]; } } else { FXSYS_memcpy32(dest_buf, src_buf, dest_pitch * height); } if (pFunc) { delete pFunc; } FX_Free(pTransfer); return pMask; }
FX_BOOL CFX_PSRenderer::DrawDIBits(const CFX_DIBSource* pSource, FX_DWORD color, const CFX_AffineMatrix* pMatrix, FX_DWORD flags, int alpha_flag, void* pIccTransform) { StartRendering(); if ((pMatrix->a == 0 && pMatrix->b == 0) || (pMatrix->c == 0 && pMatrix->d == 0)) { return TRUE; } if (pSource->HasAlpha()) { return FALSE; } int alpha = FXGETFLAG_COLORTYPE(alpha_flag) ? FXGETFLAG_ALPHA_FILL(color) : FXARGB_A(color); if (pSource->IsAlphaMask() && (alpha < 255 || pSource->GetBPP() != 1)) { return FALSE; } OUTPUT_PS("q\n"); CFX_ByteTextBuf buf; buf << FX_BSTRC("[") << pMatrix->a << FX_BSTRC(" ") << pMatrix->b << FX_BSTRC(" ") << pMatrix->c << FX_BSTRC(" ") << pMatrix->d << FX_BSTRC(" ") << pMatrix->e << FX_BSTRC(" ") << pMatrix->f << FX_BSTRC("]cm "); int width = pSource->GetWidth(); int height = pSource->GetHeight(); buf << width << FX_BSTRC(" ") << height; if (pSource->GetBPP() == 1 && pSource->GetPalette() == NULL) { int pitch = (width + 7) / 8; FX_DWORD src_size = height * pitch; FX_LPBYTE src_buf = FX_Alloc(FX_BYTE, src_size); if (!src_buf) { return FALSE; } for (int row = 0; row < height; row ++) { FX_LPCBYTE src_scan = pSource->GetScanline(row); FXSYS_memcpy32(src_buf + row * pitch, src_scan, pitch); } FX_LPBYTE output_buf; FX_DWORD output_size; FaxCompressData(src_buf, width, height, output_buf, output_size); if (pSource->IsAlphaMask()) { SetColor(color, alpha_flag, pIccTransform); m_bColorSet = FALSE; buf << FX_BSTRC(" true["); } else { buf << FX_BSTRC(" 1["); } buf << width << FX_BSTRC(" 0 0 -") << height << FX_BSTRC(" 0 ") << height << FX_BSTRC("]currentfile/ASCII85Decode filter "); if (output_buf != src_buf) buf << FX_BSTRC("<</K -1/EndOfBlock false/Columns ") << width << FX_BSTRC("/Rows ") << height << FX_BSTRC(">>/CCITTFaxDecode filter "); if (pSource->IsAlphaMask()) { buf << FX_BSTRC("iM\n"); } else { buf << FX_BSTRC("false 1 colorimage\n"); } m_pOutput->OutputPS((FX_LPCSTR)buf.GetBuffer(), buf.GetSize()); WritePSBinary(output_buf, output_size); FX_Free(output_buf); } else { CFX_DIBSource* pConverted = (CFX_DIBSource*)pSource; if (pIccTransform) { FXDIB_Format format = m_bCmykOutput ? FXDIB_Cmyk : FXDIB_Rgb; pConverted = pSource->CloneConvert(format, NULL, pIccTransform); } else { switch (pSource->GetFormat()) { case FXDIB_1bppRgb: case FXDIB_Rgb32: pConverted = pSource->CloneConvert(FXDIB_Rgb); break; case FXDIB_8bppRgb: if (pSource->GetPalette() != NULL) { pConverted = pSource->CloneConvert(FXDIB_Rgb); } break; case FXDIB_1bppCmyk: pConverted = pSource->CloneConvert(FXDIB_Cmyk); break; case FXDIB_8bppCmyk: if (pSource->GetPalette() != NULL) { pConverted = pSource->CloneConvert(FXDIB_Cmyk); } break; default: break; } } if (pConverted == NULL) { OUTPUT_PS("\nQ\n"); return FALSE; } int Bpp = pConverted->GetBPP() / 8; FX_LPBYTE output_buf = NULL; FX_STRSIZE output_size = 0; FX_LPCSTR filter = NULL; if (flags & FXRENDER_IMAGE_LOSSY) { CCodec_ModuleMgr* pEncoders = CFX_GEModule::Get()->GetCodecModule(); if (pEncoders && pEncoders->GetJpegModule()->Encode(pConverted, output_buf, output_size)) { filter = "/DCTDecode filter "; } } if (filter == NULL) { int src_pitch = width * Bpp; output_size = height * src_pitch; output_buf = FX_Alloc(FX_BYTE, output_size); if (!output_buf) { if (pConverted != pSource) { delete pConverted; pConverted = NULL; } return FALSE; } for (int row = 0; row < height; row ++) { FX_LPCBYTE src_scan = pConverted->GetScanline(row); FX_LPBYTE dest_scan = output_buf + row * src_pitch; if (Bpp == 3) { for (int col = 0; col < width; col ++) { *dest_scan++ = src_scan[2]; *dest_scan++ = src_scan[1]; *dest_scan++ = *src_scan; src_scan += 3; } } else { FXSYS_memcpy32(dest_scan, src_scan, src_pitch); } } FX_LPBYTE compressed_buf; FX_DWORD compressed_size; PSCompressData(m_PSLevel, output_buf, output_size, compressed_buf, compressed_size, filter); if (output_buf != compressed_buf) { FX_Free(output_buf); } output_buf = compressed_buf; output_size = compressed_size; } if (pConverted != pSource) { delete pConverted; pConverted = NULL; } buf << FX_BSTRC(" 8["); buf << width << FX_BSTRC(" 0 0 -") << height << FX_BSTRC(" 0 ") << height << FX_BSTRC("]"); buf << FX_BSTRC("currentfile/ASCII85Decode filter "); if (filter) { buf << filter; } buf << FX_BSTRC("false ") << Bpp; buf << FX_BSTRC(" colorimage\n"); m_pOutput->OutputPS((FX_LPCSTR)buf.GetBuffer(), buf.GetSize()); WritePSBinary(output_buf, output_size); FX_Free(output_buf); } OUTPUT_PS("\nQ\n"); return TRUE; }
void CPDF_StreamAcc::LoadAllData(const CPDF_Stream* pStream, FX_BOOL bRawAccess, FX_DWORD estimated_size, FX_BOOL bImageAcc) { if (pStream == NULL || pStream->GetType() != PDFOBJ_STREAM) { return; } m_pStream = pStream; if (pStream->IsMemoryBased() && (!pStream->GetDict()->KeyExist(FX_BSTRC("Filter")) || bRawAccess)) { m_dwSize = pStream->m_dwSize; m_pData = (FX_LPBYTE)pStream->m_pDataBuf; return; } FX_LPBYTE pSrcData; FX_DWORD dwSrcSize = pStream->m_dwSize; if (dwSrcSize == 0) { return; } if (!pStream->IsMemoryBased()) { pSrcData = m_pSrcData = FX_Alloc(FX_BYTE, dwSrcSize); if (!pSrcData || !pStream->ReadRawData(0, pSrcData, dwSrcSize)) { return; } } else { pSrcData = pStream->m_pDataBuf; } FX_LPBYTE pDecryptedData; FX_DWORD dwDecryptedSize; if (pStream->m_pCryptoHandler) { CFX_BinaryBuf dest_buf; dest_buf.EstimateSize(pStream->m_pCryptoHandler->DecryptGetSize(dwSrcSize)); FX_LPVOID context = pStream->m_pCryptoHandler->DecryptStart(pStream->GetObjNum(), pStream->m_GenNum); pStream->m_pCryptoHandler->DecryptStream(context, pSrcData, dwSrcSize, dest_buf); pStream->m_pCryptoHandler->DecryptFinish(context, dest_buf); pDecryptedData = dest_buf.GetBuffer(); dwDecryptedSize = dest_buf.GetSize(); dest_buf.DetachBuffer(); } else { pDecryptedData = pSrcData; dwDecryptedSize = dwSrcSize; } if (!pStream->GetDict()->KeyExist(FX_BSTRC("Filter")) || bRawAccess) { m_pData = pDecryptedData; m_dwSize = dwDecryptedSize; } else { FX_BOOL bRet = PDF_DataDecode(pDecryptedData, dwDecryptedSize, m_pStream->GetDict(), m_pData, m_dwSize, m_ImageDecoder, m_pImageParam, estimated_size, bImageAcc); if (!bRet) { m_pData = pDecryptedData; m_dwSize = dwDecryptedSize; } } if (pSrcData != pStream->m_pDataBuf && pSrcData != m_pData) { FX_Free(pSrcData); } if (pDecryptedData != pSrcData && pDecryptedData != m_pData) { FX_Free(pDecryptedData); } m_pSrcData = NULL; m_bNewBuf = m_pData != pStream->m_pDataBuf; }
CFX_DIBitmap* CFX_WindowsDIB::LoadFromDDB(HDC hDC, HBITMAP hBitmap, FX_DWORD* pPalette, FX_DWORD palsize) { FX_BOOL bCreatedDC = hDC == NULL; if (hDC == NULL) { hDC = CreateCompatibleDC(NULL); } BITMAPINFOHEADER bmih; FXSYS_memset32(&bmih, 0, sizeof bmih); bmih.biSize = sizeof bmih; GetDIBits(hDC, hBitmap, 0, 0, NULL, (BITMAPINFO*)&bmih, DIB_RGB_COLORS); int width = bmih.biWidth; int height = abs(bmih.biHeight); bmih.biHeight = -height; bmih.biCompression = BI_RGB; CFX_DIBitmap* pDIBitmap = FX_NEW CFX_DIBitmap; if (!pDIBitmap) { return NULL; } int ret = 0; if (bmih.biBitCount == 1 || bmih.biBitCount == 8) { int size = sizeof (BITMAPINFOHEADER) + 8; if (bmih.biBitCount == 8) { size += sizeof (FX_DWORD) * 254; } BITMAPINFO* pbmih = (BITMAPINFO*)FX_Alloc(FX_BYTE, size); if (!pbmih) { delete pDIBitmap; if (bCreatedDC) { DeleteDC(hDC); } return NULL; } pbmih->bmiHeader.biSize = sizeof(BITMAPINFOHEADER); pbmih->bmiHeader.biBitCount = bmih.biBitCount; pbmih->bmiHeader.biCompression = BI_RGB; pbmih->bmiHeader.biHeight = -height; pbmih->bmiHeader.biPlanes = 1; pbmih->bmiHeader.biWidth = bmih.biWidth; if (!pDIBitmap->Create(bmih.biWidth, height, bmih.biBitCount == 1 ? FXDIB_1bppRgb : FXDIB_8bppRgb)) { delete pDIBitmap; FX_Free(pbmih); if (bCreatedDC) { DeleteDC(hDC); } return NULL; } ret = GetDIBits(hDC, hBitmap, 0, height, pDIBitmap->GetBuffer(), (BITMAPINFO*)pbmih, DIB_RGB_COLORS); FX_Free(pbmih); pbmih = NULL; pDIBitmap->CopyPalette(pPalette, palsize); } else { if (bmih.biBitCount <= 24) { bmih.biBitCount = 24; } else { bmih.biBitCount = 32; } if (!pDIBitmap->Create(bmih.biWidth, height, bmih.biBitCount == 24 ? FXDIB_Rgb : FXDIB_Rgb32)) { delete pDIBitmap; if (bCreatedDC) { DeleteDC(hDC); } return NULL; } ret = GetDIBits(hDC, hBitmap, 0, height, pDIBitmap->GetBuffer(), (BITMAPINFO*)&bmih, DIB_RGB_COLORS); if (ret != 0 && bmih.biBitCount == 32) { int pitch = pDIBitmap->GetPitch(); for (int row = 0; row < height; row ++) { FX_BYTE* dest_scan = (FX_BYTE*)(pDIBitmap->GetBuffer() + row * pitch); for (int col = 0; col < width; col++) { dest_scan[3] = 255; dest_scan += 4; } } } } if (ret == 0) { if (pDIBitmap) { delete pDIBitmap; } pDIBitmap = NULL; } if (bCreatedDC) { DeleteDC(hDC); } return pDIBitmap; }
CFDE_GdiDevice::~CFDE_GdiDevice() { delete m_pGraphics; delete m_pBitmap; FX_Free(m_pGlyphBuf); }
qboolean FX_FreeSystem( void ) { return (qboolean)FX_Free( true ); }
CStretchEngine::~CStretchEngine() { FX_Free(m_pDestScanline); FX_Free(m_pInterBuf); FX_Free(m_pExtraAlphaBuf); FX_Free(m_pDestMaskScanline); }
CFX_BasicArray::~CFX_BasicArray() { FX_Free(m_pData); }
bool CWeightTable::Calc(int dest_len, int dest_min, int dest_max, int src_len, int src_min, int src_max, int flags) { FX_Free(m_pWeightTables); m_pWeightTables = nullptr; m_dwWeightTablesSize = 0; const double scale = (FX_FLOAT)src_len / (FX_FLOAT)dest_len; const double base = dest_len < 0 ? (FX_FLOAT)(src_len) : 0; const int ext_size = flags & FXDIB_BICUBIC_INTERPOL ? 3 : 1; m_ItemSize = sizeof(int) * 2 + (int)(sizeof(int) * (FXSYS_ceil(FXSYS_fabs((FX_FLOAT)scale)) + ext_size)); m_DestMin = dest_min; if ((dest_max - dest_min) > (int)((1U << 30) - 4) / m_ItemSize) return false; m_dwWeightTablesSize = (dest_max - dest_min) * m_ItemSize + 4; m_pWeightTables = FX_TryAlloc(uint8_t, m_dwWeightTablesSize); if (!m_pWeightTables) return false; if ((flags & FXDIB_NOSMOOTH) != 0 || FXSYS_fabs((FX_FLOAT)scale) < 1.0f) { for (int dest_pixel = dest_min; dest_pixel < dest_max; dest_pixel++) { PixelWeight& pixel_weights = *GetPixelWeight(dest_pixel); double src_pos = dest_pixel * scale + scale / 2 + base; if (flags & FXDIB_INTERPOL) { pixel_weights.m_SrcStart = (int)FXSYS_floor((FX_FLOAT)src_pos - 1.0f / 2); pixel_weights.m_SrcEnd = (int)FXSYS_floor((FX_FLOAT)src_pos + 1.0f / 2); if (pixel_weights.m_SrcStart < src_min) { pixel_weights.m_SrcStart = src_min; } if (pixel_weights.m_SrcEnd >= src_max) { pixel_weights.m_SrcEnd = src_max - 1; } if (pixel_weights.m_SrcStart == pixel_weights.m_SrcEnd) { pixel_weights.m_Weights[0] = 65536; } else { pixel_weights.m_Weights[1] = FXSYS_round( (FX_FLOAT)(src_pos - pixel_weights.m_SrcStart - 1.0f / 2) * 65536); pixel_weights.m_Weights[0] = 65536 - pixel_weights.m_Weights[1]; } } else if (flags & FXDIB_BICUBIC_INTERPOL) { pixel_weights.m_SrcStart = (int)FXSYS_floor((FX_FLOAT)src_pos - 1.0f / 2); pixel_weights.m_SrcEnd = (int)FXSYS_floor((FX_FLOAT)src_pos + 1.0f / 2); int start = pixel_weights.m_SrcStart - 1; int end = pixel_weights.m_SrcEnd + 1; if (start < src_min) { start = src_min; } if (end >= src_max) { end = src_max - 1; } if (pixel_weights.m_SrcStart < src_min) { src_pos += src_min - pixel_weights.m_SrcStart; pixel_weights.m_SrcStart = src_min; } if (pixel_weights.m_SrcEnd >= src_max) { pixel_weights.m_SrcEnd = src_max - 1; } int weight; weight = FXSYS_round( (FX_FLOAT)(src_pos - pixel_weights.m_SrcStart - 1.0f / 2) * 256); if (start == end) { pixel_weights.m_Weights[0] = (SDP_Table[256 + weight] + SDP_Table[weight] + SDP_Table[256 - weight] + SDP_Table[512 - weight]) << 8; } else if ((start == pixel_weights.m_SrcStart && (pixel_weights.m_SrcStart == pixel_weights.m_SrcEnd || end == pixel_weights.m_SrcEnd) && start < end) || (start < pixel_weights.m_SrcStart && pixel_weights.m_SrcStart == pixel_weights.m_SrcEnd && end == pixel_weights.m_SrcEnd)) { if (start < pixel_weights.m_SrcStart) { pixel_weights.m_Weights[0] = SDP_Table[256 + weight] << 8; pixel_weights.m_Weights[1] = (SDP_Table[weight] + SDP_Table[256 - weight] + SDP_Table[512 - weight]) << 8; } else { if (pixel_weights.m_SrcStart == pixel_weights.m_SrcEnd) { pixel_weights.m_Weights[0] = (SDP_Table[256 + weight] + SDP_Table[weight] + SDP_Table[256 - weight]) << 8; pixel_weights.m_Weights[1] = SDP_Table[512 - weight] << 8; } else { pixel_weights.m_Weights[0] = (SDP_Table[256 + weight] + SDP_Table[weight]) << 8; pixel_weights.m_Weights[1] = (SDP_Table[256 - weight] + SDP_Table[512 - weight]) << 8; } } if (pixel_weights.m_SrcStart == pixel_weights.m_SrcEnd) { pixel_weights.m_SrcEnd = end; } if (start < pixel_weights.m_SrcStart) { pixel_weights.m_SrcStart = start; } } else if (start == pixel_weights.m_SrcStart && start < pixel_weights.m_SrcEnd && pixel_weights.m_SrcEnd < end) { pixel_weights.m_Weights[0] = (SDP_Table[256 + weight] + SDP_Table[weight]) << 8; pixel_weights.m_Weights[1] = SDP_Table[256 - weight] << 8; pixel_weights.m_Weights[2] = SDP_Table[512 - weight] << 8; pixel_weights.m_SrcEnd = end; } else if (start < pixel_weights.m_SrcStart && pixel_weights.m_SrcStart < pixel_weights.m_SrcEnd && pixel_weights.m_SrcEnd == end) { pixel_weights.m_Weights[0] = SDP_Table[256 + weight] << 8; pixel_weights.m_Weights[1] = SDP_Table[weight] << 8; pixel_weights.m_Weights[2] = (SDP_Table[256 - weight] + SDP_Table[512 - weight]) << 8; pixel_weights.m_SrcStart = start; } else { pixel_weights.m_Weights[0] = SDP_Table[256 + weight] << 8; pixel_weights.m_Weights[1] = SDP_Table[weight] << 8; pixel_weights.m_Weights[2] = SDP_Table[256 - weight] << 8; pixel_weights.m_Weights[3] = SDP_Table[512 - weight] << 8; pixel_weights.m_SrcStart = start; pixel_weights.m_SrcEnd = end; } } else { pixel_weights.m_SrcStart = pixel_weights.m_SrcEnd = (int)FXSYS_floor((FX_FLOAT)src_pos); if (pixel_weights.m_SrcStart < src_min) { pixel_weights.m_SrcStart = src_min; } if (pixel_weights.m_SrcEnd >= src_max) { pixel_weights.m_SrcEnd = src_max - 1; } pixel_weights.m_Weights[0] = 65536; } } return true; } for (int dest_pixel = dest_min; dest_pixel < dest_max; dest_pixel++) { PixelWeight& pixel_weights = *GetPixelWeight(dest_pixel); double src_start = dest_pixel * scale + base; double src_end = src_start + scale; int start_i, end_i; if (src_start < src_end) { start_i = (int)FXSYS_floor((FX_FLOAT)src_start); end_i = (int)FXSYS_ceil((FX_FLOAT)src_end); } else { start_i = (int)FXSYS_floor((FX_FLOAT)src_end); end_i = (int)FXSYS_ceil((FX_FLOAT)src_start); } if (start_i < src_min) { start_i = src_min; } if (end_i >= src_max) { end_i = src_max - 1; } if (start_i > end_i) { if (start_i >= src_max) { start_i = src_max - 1; } pixel_weights.m_SrcStart = start_i; pixel_weights.m_SrcEnd = start_i; continue; } pixel_weights.m_SrcStart = start_i; pixel_weights.m_SrcEnd = end_i; for (int j = start_i; j <= end_i; j++) { double dest_start = ((FX_FLOAT)j - base) / scale; double dest_end = ((FX_FLOAT)(j + 1) - base) / scale; if (dest_start > dest_end) { double temp = dest_start; dest_start = dest_end; dest_end = temp; } double area_start = dest_start > (FX_FLOAT)(dest_pixel) ? dest_start : (FX_FLOAT)(dest_pixel); double area_end = dest_end > (FX_FLOAT)(dest_pixel + 1) ? (FX_FLOAT)(dest_pixel + 1) : dest_end; double weight = area_start >= area_end ? 0.0f : area_end - area_start; if (weight == 0 && j == end_i) { pixel_weights.m_SrcEnd--; break; } size_t idx = j - start_i; if (idx >= GetPixelWeightSize()) return false; pixel_weights.m_Weights[idx] = FXSYS_round((FX_FLOAT)(weight * 65536)); } } return true; }
void CBC_OneDimWriter::ShowChars(const CFX_WideStringC& contents, CFX_DIBitmap* pOutBitmap, CFX_RenderDevice* device, const CFX_Matrix* matrix, int32_t barWidth, int32_t multiple, int32_t& e) { if (!device && !pOutBitmap) { e = BCExceptionIllegalArgument; return; } if (!m_pFont) { e = BCExceptionNullPointer; return; } CFX_ByteString str = FX_UTF8Encode(contents); int32_t iLen = str.GetLength(); FXTEXT_CHARPOS* pCharPos = FX_Alloc(FXTEXT_CHARPOS, iLen); FXSYS_memset(pCharPos, 0, sizeof(FXTEXT_CHARPOS) * iLen); FX_FLOAT charsLen = 0; FX_FLOAT geWidth = 0; if (m_locTextLoc == BC_TEXT_LOC_ABOVEEMBED || m_locTextLoc == BC_TEXT_LOC_BELOWEMBED) { geWidth = 0; } else if (m_locTextLoc == BC_TEXT_LOC_ABOVE || m_locTextLoc == BC_TEXT_LOC_BELOW) { geWidth = (FX_FLOAT)barWidth; } int32_t iFontSize = (int32_t)fabs(m_fFontSize); int32_t iTextHeight = iFontSize + 1; CalcTextInfo(str, pCharPos, m_pFont, geWidth, iFontSize, charsLen); if (charsLen < 1) { return; } int32_t locX = 0; int32_t locY = 0; switch (m_locTextLoc) { case BC_TEXT_LOC_ABOVEEMBED: locX = (int32_t)(barWidth - charsLen) / 2; locY = 0; geWidth = charsLen; break; case BC_TEXT_LOC_ABOVE: locX = 0; locY = 0; geWidth = (FX_FLOAT)barWidth; break; case BC_TEXT_LOC_BELOWEMBED: locX = (int32_t)(barWidth - charsLen) / 2; locY = m_Height - iTextHeight; geWidth = charsLen; break; case BC_TEXT_LOC_BELOW: default: locX = 0; locY = m_Height - iTextHeight; geWidth = (FX_FLOAT)barWidth; break; } if (device) { ShowDeviceChars(device, matrix, str, geWidth, pCharPos, (FX_FLOAT)locX, (FX_FLOAT)locY, barWidth); } else { ShowBitmapChars(pOutBitmap, str, geWidth, pCharPos, (FX_FLOAT)locX, (FX_FLOAT)locY, barWidth); } FX_Free(pCharPos); }
CPDF_Font* CPDF_Document::AddWindowsFont(LOGFONTA* pLogFont, FX_BOOL bVert, FX_BOOL bTranslateName) { pLogFont->lfHeight = -1000; pLogFont->lfWidth = 0; HGDIOBJ hFont = CreateFontIndirectA(pLogFont); HDC hDC = CreateCompatibleDC(nullptr); hFont = SelectObject(hDC, hFont); int tm_size = GetOutlineTextMetrics(hDC, 0, nullptr); if (tm_size == 0) { hFont = SelectObject(hDC, hFont); DeleteObject(hFont); DeleteDC(hDC); return nullptr; } LPBYTE tm_buf = FX_Alloc(BYTE, tm_size); OUTLINETEXTMETRIC* ptm = reinterpret_cast<OUTLINETEXTMETRIC*>(tm_buf); GetOutlineTextMetrics(hDC, tm_size, ptm); int flags = CalculateFlags(false, pLogFont->lfItalic != 0, (pLogFont->lfPitchAndFamily & 3) == FIXED_PITCH, (pLogFont->lfPitchAndFamily & 0xf8) == FF_ROMAN, (pLogFont->lfPitchAndFamily & 0xf8) == FF_SCRIPT, pLogFont->lfCharSet == FXFONT_SYMBOL_CHARSET); bool bCJK = pLogFont->lfCharSet == FXFONT_CHINESEBIG5_CHARSET || pLogFont->lfCharSet == FXFONT_GB2312_CHARSET || pLogFont->lfCharSet == FXFONT_HANGUL_CHARSET || pLogFont->lfCharSet == FXFONT_SHIFTJIS_CHARSET; CFX_ByteString basefont; if (bTranslateName && bCJK) basefont = FPDF_GetPSNameFromTT(hDC); if (basefont.IsEmpty()) basefont = pLogFont->lfFaceName; int italicangle = ptm->otmItalicAngle / 10; int ascend = ptm->otmrcFontBox.top; int descend = ptm->otmrcFontBox.bottom; int capheight = ptm->otmsCapEmHeight; int bbox[4] = {ptm->otmrcFontBox.left, ptm->otmrcFontBox.bottom, ptm->otmrcFontBox.right, ptm->otmrcFontBox.top}; FX_Free(tm_buf); basefont.Replace(" ", ""); CPDF_Dictionary* pBaseDict = new CPDF_Dictionary(m_pByteStringPool); pBaseDict->SetNameFor("Type", "Font"); CPDF_Dictionary* pFontDict = pBaseDict; if (!bCJK) { if (pLogFont->lfCharSet == FXFONT_ANSI_CHARSET || pLogFont->lfCharSet == FXFONT_DEFAULT_CHARSET || pLogFont->lfCharSet == FXFONT_SYMBOL_CHARSET) { pBaseDict->SetNameFor("Encoding", "WinAnsiEncoding"); } else { CalculateEncodingDict(pLogFont->lfCharSet, pBaseDict); } int char_widths[224]; GetCharWidth(hDC, 32, 255, char_widths); CPDF_Array* pWidths = new CPDF_Array; for (size_t i = 0; i < 224; i++) pWidths->AddInteger(char_widths[i]); ProcessNonbCJK(pBaseDict, pLogFont->lfWeight > FW_MEDIUM, pLogFont->lfItalic != 0, basefont, pWidths); } else { pFontDict = ProcessbCJK(pBaseDict, pLogFont->lfCharSet, bVert, basefont, [&hDC](FX_WCHAR start, FX_WCHAR end, CPDF_Array* widthArr) { InsertWidthArray(hDC, start, end, widthArr); }); } AddIndirectObject(pBaseDict); CPDF_Array* pBBox = new CPDF_Array; for (int i = 0; i < 4; i++) pBBox->AddInteger(bbox[i]); CPDF_Dictionary* pFontDesc = CalculateFontDesc(this, basefont, flags, italicangle, ascend, descend, pBBox, pLogFont->lfWeight / 5); pFontDesc->SetIntegerFor("CapHeight", capheight); pFontDict->SetReferenceFor("FontDescriptor", this, AddIndirectObject(pFontDesc)); hFont = SelectObject(hDC, hFont); DeleteObject(hFont); DeleteDC(hDC); return LoadFont(pBaseDict); }
CFX_BinaryBuf::~CFX_BinaryBuf() { if (m_pBuffer) { FX_Free(m_pBuffer); } }