CFX_ByteString GetNameFromTT(const uint8_t* name_table, uint32_t name_table_size, uint32_t name_id) { if (!name_table || name_table_size < 6) { return CFX_ByteString(); } uint32_t name_count = GET_TT_SHORT(name_table + 2); uint32_t string_offset = GET_TT_SHORT(name_table + 4); // We will ignore the possibility of overlap of structures and // string table as if it's all corrupt there's not a lot we can do. if (name_table_size < string_offset) { return CFX_ByteString(); } const uint8_t* string_ptr = name_table + string_offset; uint32_t string_ptr_size = name_table_size - string_offset; name_table += 6; name_table_size -= 6; if (name_table_size < name_count * 12) { return CFX_ByteString(); } for (uint32_t i = 0; i < name_count; i++, name_table += 12) { if (GET_TT_SHORT(name_table + 6) == name_id && GET_TT_SHORT(name_table) == 1 && GET_TT_SHORT(name_table + 2) == 0) { return GetStringFromTable(string_ptr, string_ptr_size, GET_TT_SHORT(name_table + 10), GET_TT_SHORT(name_table + 8)); } } return CFX_ByteString(); }
CFX_ByteString CPDF_Object::GetString() const { switch (m_Type) { case PDFOBJ_BOOLEAN: return ((CPDF_Boolean*)this)->m_bValue ? "true" : "false"; case PDFOBJ_NUMBER: return ((CPDF_Number*)this)->GetString(); case PDFOBJ_STRING: return ((CPDF_String*)this)->m_String; case PDFOBJ_NAME: return ((CPDF_Name*)this)->m_Name; case PDFOBJ_REFERENCE: { CPDF_Reference* pRef = (CPDF_Reference*)(FX_LPVOID)this; if (pRef->m_pObjList == NULL) { break; } CPDF_Object* pObj = pRef->m_pObjList->GetIndirectObject(pRef->m_RefObjNum); if (pObj == NULL) { return CFX_ByteString(); } return pObj->GetString(); } } return CFX_ByteString(); }
CFX_ByteString CFPF_SkiaFont::GetFamilyName() { if (!m_Face) { return CFX_ByteString(); } return CFX_ByteString(FXFT_Get_Face_Family_Name(m_Face)); }
CFX_ByteString CPDF_StreamParser::ReadHexString() { if (!PositionIsInBounds()) return CFX_ByteString(); CFX_ByteTextBuf buf; bool bFirst = true; int code = 0; while (PositionIsInBounds()) { int ch = m_pBuf[m_Pos++]; if (ch == '>') break; if (!std::isxdigit(ch)) continue; int val = FXSYS_toHexDigit(ch); if (bFirst) { code = val * 16; } else { code += val; buf.AppendByte((uint8_t)code); } bFirst = !bFirst; } if (!bFirst) buf.AppendChar((char)code); if (buf.GetLength() > MAX_STRING_LENGTH) return CFX_ByteString(buf.GetBuffer(), MAX_STRING_LENGTH); return buf.GetByteString(); }
CFX_ByteString CFX_Font::GetFamilyName() const { if (!m_Face && !m_pSubstFont) { return CFX_ByteString(); } if (m_Face) { return CFX_ByteString(FXFT_Get_Face_Family_Name(m_Face)); } return m_pSubstFont->m_Family; }
static CFX_ByteString GetStringFromTable(const uint8_t* string_ptr, uint32_t string_ptr_length, uint16_t offset, uint16_t length) { if (string_ptr_length < static_cast<uint32_t>(offset + length)) { return CFX_ByteString(); } return CFX_ByteString(string_ptr + offset, length); }
CFX_ByteString CFX_Font::GetFamilyName() const { if (m_Face == NULL && m_pSubstFont == NULL) { return CFX_ByteString(); } if (m_Face) { return CFX_ByteString(FXFT_Get_Face_Family_Name(m_Face)); } else { return m_pSubstFont->m_Family; } }
bool CXML_Element::GetAttrFloat(const CFX_ByteStringC& space, const CFX_ByteStringC& name, FX_FLOAT& attribute) const { const CFX_WideString* pValue = m_AttrMap.Lookup(CFX_ByteString(space), CFX_ByteString(name)); if (pValue) { attribute = pValue->GetFloat(); return true; } return false; }
bool CXML_Element::GetAttrInteger(const CFX_ByteStringC& space, const CFX_ByteStringC& name, int& attribute) const { const CFX_WideString* pwsValue = m_AttrMap.Lookup(CFX_ByteString(space), CFX_ByteString(name)); if (pwsValue) { attribute = pwsValue->GetInteger(); return true; } return false; }
bool CXML_Element::GetAttrValue(const CFX_ByteStringC& space, const CFX_ByteStringC& name, CFX_WideString& attribute) const { const CFX_WideString* pValue = m_AttrMap.Lookup(CFX_ByteString(space), CFX_ByteString(name)); if (pValue) { attribute = *pValue; return true; } return false; }
bool CXML_Element::GetAttrInteger(const CFX_ByteStringC& name, int& attribute) const { CFX_ByteStringC bsSpace; CFX_ByteStringC bsName; FX_XML_SplitQualifiedName(name, bsSpace, bsName); const CFX_WideString* pwsValue = m_AttrMap.Lookup(CFX_ByteString(bsSpace), CFX_ByteString(bsName)); if (pwsValue) { attribute = pwsValue->GetInteger(); return true; } return false; }
void CPDF_StandardSecurityHandler::AES256_SetPassword( CPDF_Dictionary* pEncryptDict, const uint8_t* password, FX_DWORD size, FX_BOOL bOwner, const uint8_t* key) { uint8_t sha[128]; CRYPT_SHA1Start(sha); CRYPT_SHA1Update(sha, key, 32); CRYPT_SHA1Update(sha, (uint8_t*)"hello", 5); uint8_t digest[20]; CRYPT_SHA1Finish(sha, digest); CFX_ByteString ukey = pEncryptDict->GetString(FX_BSTRC("U")); uint8_t digest1[48]; if (m_Revision >= 6) { Revision6_Hash(password, size, digest, (bOwner ? (const uint8_t*)ukey : NULL), digest1); } else { CRYPT_SHA256Start(sha); CRYPT_SHA256Update(sha, password, size); CRYPT_SHA256Update(sha, digest, 8); if (bOwner) { CRYPT_SHA256Update(sha, ukey, ukey.GetLength()); } CRYPT_SHA256Finish(sha, digest1); } FXSYS_memcpy(digest1 + 32, digest, 16); pEncryptDict->SetAtString(bOwner ? FX_BSTRC("O") : FX_BSTRC("U"), CFX_ByteString(digest1, 48)); if (m_Revision >= 6) { Revision6_Hash(password, size, digest + 8, (bOwner ? (const uint8_t*)ukey : NULL), digest1); } else { CRYPT_SHA256Start(sha); CRYPT_SHA256Update(sha, password, size); CRYPT_SHA256Update(sha, digest + 8, 8); if (bOwner) { CRYPT_SHA256Update(sha, ukey, ukey.GetLength()); } CRYPT_SHA256Finish(sha, digest1); } uint8_t* aes = FX_Alloc(uint8_t, 2048); CRYPT_AESSetKey(aes, 16, digest1, 32, TRUE); uint8_t iv[16]; FXSYS_memset(iv, 0, 16); CRYPT_AESSetIV(aes, iv); CRYPT_AESEncrypt(aes, digest1, key, 32); FX_Free(aes); pEncryptDict->SetAtString(bOwner ? FX_BSTRC("OE") : FX_BSTRC("UE"), CFX_ByteString(digest1, 32)); }
CFX_ByteString CPDF_StreamParser::ReadHexString() { if (m_Size <= m_Pos) { return CFX_ByteString(); } int ch = m_pBuf[m_Pos++]; CFX_ByteTextBuf buf; FX_BOOL bFirst = TRUE; int code = 0; while (1) { if (ch == '>') { break; } if (ch >= '0' && ch <= '9') { if (bFirst) { code = (ch - '0') * 16; } else { code += ch - '0'; buf.AppendChar((char)code); } bFirst = !bFirst; } else if (ch >= 'A' && ch <= 'F') { if (bFirst) { code = (ch - 'A' + 10) * 16; } else { code += ch - 'A' + 10; buf.AppendChar((char)code); } bFirst = !bFirst; } else if (ch >= 'a' && ch <= 'f') { if (bFirst) { code = (ch - 'a' + 10) * 16; } else { code += ch - 'a' + 10; buf.AppendChar((char)code); } bFirst = !bFirst; } if (m_Size <= m_Pos) { break; } ch = m_pBuf[m_Pos++]; } if (!bFirst) { buf.AppendChar((char)code); } if (buf.GetLength() > MAX_STRING_LENGTH) { return CFX_ByteString(buf.GetBuffer(), MAX_STRING_LENGTH); } return buf.GetByteString(); }
CFX_ByteString CPDFSDK_DateTime::ToPDFDateTimeString() { CFX_ByteString dtStr; char tempStr[32]; sprintf(tempStr, "D:%04d%02d%02d%02d%02d%02d", dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second); dtStr = CFX_ByteString(tempStr); if (dt.tzHour < 0) dtStr += CFX_ByteString("-"); else dtStr += CFX_ByteString("+"); sprintf(tempStr, "%02d'%02d'", abs(dt.tzHour), dt.tzMinute); dtStr += CFX_ByteString(tempStr); return dtStr; }
CPDF_Object* CPDF_StructElementImpl::GetAttr(const CFX_ByteStringC& owner, const CFX_ByteStringC& name, FX_BOOL bInheritable, FX_FLOAT fLevel) { if (fLevel > nMaxRecursion) { return nullptr; } if (bInheritable) { CPDF_Object* pAttr = GetAttr(owner, name, FALSE); if (pAttr) { return pAttr; } if (!m_pParent) { return nullptr; } return m_pParent->GetAttr(owner, name, TRUE, fLevel + 1); } CPDF_Object* pA = m_pDict->GetDirectObjectBy("A"); if (pA) { CPDF_Dictionary* pAttrDict = FindAttrDict(pA, owner); if (pAttrDict) { CPDF_Object* pAttr = pAttrDict->GetDirectObjectBy(CFX_ByteString(name)); if (pAttr) { return pAttr; } } } CPDF_Object* pC = m_pDict->GetDirectObjectBy("C"); if (!pC) return nullptr; CPDF_Dictionary* pClassMap = m_pTree->m_pTreeRoot->GetDictBy("ClassMap"); if (!pClassMap) return nullptr; if (CPDF_Array* pArray = pC->AsArray()) { for (uint32_t i = 0; i < pArray->GetCount(); i++) { CFX_ByteString class_name = pArray->GetStringAt(i); CPDF_Dictionary* pClassDict = pClassMap->GetDictBy(class_name); if (pClassDict && pClassDict->GetStringBy("O") == owner) return pClassDict->GetDirectObjectBy(CFX_ByteString(name)); } return nullptr; } CFX_ByteString class_name = pC->GetString(); CPDF_Dictionary* pClassDict = pClassMap->GetDictBy(class_name); if (pClassDict && pClassDict->GetStringBy("O") == owner) return pClassDict->GetDirectObjectBy(CFX_ByteString(name)); return nullptr; }
CFX_ByteString CFX_FontMapper::GetPSNameFromTT(void* hFont) { if (!m_pFontInfo) return CFX_ByteString(); uint32_t size = m_pFontInfo->GetFontData(hFont, kTableNAME, nullptr, 0); if (!size) return CFX_ByteString(); std::vector<uint8_t> buffer(size); uint8_t* buffer_ptr = buffer.data(); uint32_t bytes_read = m_pFontInfo->GetFontData(hFont, kTableNAME, buffer_ptr, size); return bytes_read == size ? GetNameFromTT(buffer_ptr, bytes_read, 6) : CFX_ByteString(); }
CPDF_Object* CPDF_StreamContentParser::GetObject(FX_DWORD index) { if (index >= m_ParamCount) { return NULL; } int real_index = m_ParamStartPos + m_ParamCount - index - 1; if (real_index >= PARAM_BUF_SIZE) { real_index -= PARAM_BUF_SIZE; } ContentParam& param = m_ParamBuf1[real_index]; if (param.m_Type == PDFOBJ_NUMBER) { CPDF_Number* pNumber = param.m_Number.m_bInteger ? new CPDF_Number(param.m_Number.m_Integer) : new CPDF_Number(param.m_Number.m_Float); param.m_Type = 0; param.m_pObject = pNumber; return pNumber; } if (param.m_Type == PDFOBJ_NAME) { CPDF_Name* pName = new CPDF_Name( CFX_ByteString(param.m_Name.m_Buffer, param.m_Name.m_Len)); param.m_Type = 0; param.m_pObject = pName; return pName; } if (param.m_Type == 0) { return param.m_pObject; } ASSERT(FALSE); return NULL; }
CFX_ByteString CPWL_Edit::GetCaretAppearanceStream( const CPDF_Point& ptOffset) const { if (m_pEditCaret) return m_pEditCaret->GetCaretAppearanceStream(ptOffset); return CFX_ByteString(); }
CFX_ByteString CPDF_InterForm::GetNativeFont(void* pLogFont) { #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ return GetNativeFont(GetNativeCharSet(), pLogFont); #else return CFX_ByteString(); #endif }
CFX_ByteString CPDF_Dest::GetRemoteName() { if (m_pObj == NULL) { return CFX_ByteString(); } return m_pObj->GetString(); }
CFX_ByteString CFX_ByteString::Mid(FX_STRSIZE nFirst, FX_STRSIZE nCount) const { if (!m_pData) return CFX_ByteString(); nFirst = std::min(std::max(nFirst, 0), m_pData->m_nDataLength); nCount = std::min(std::max(nCount, 0), m_pData->m_nDataLength - nFirst); if (nCount == 0) return CFX_ByteString(); if (nFirst == 0 && nCount == m_pData->m_nDataLength) return *this; CFX_ByteString dest; AllocCopy(dest, nCount, nFirst); return dest; }
void CPDF_SecurityHandler::AES256_SetPerms(CPDF_Dictionary* pEncryptDict, uint32_t permissions, FX_BOOL bEncryptMetadata, const uint8_t* key) { uint8_t buf[16]; buf[0] = (uint8_t)permissions; buf[1] = (uint8_t)(permissions >> 8); buf[2] = (uint8_t)(permissions >> 16); buf[3] = (uint8_t)(permissions >> 24); buf[4] = 0xff; buf[5] = 0xff; buf[6] = 0xff; buf[7] = 0xff; buf[8] = bEncryptMetadata ? 'T' : 'F'; buf[9] = 'a'; buf[10] = 'd'; buf[11] = 'b'; uint8_t* aes = FX_Alloc(uint8_t, 2048); CRYPT_AESSetKey(aes, 16, key, 32, TRUE); uint8_t iv[16], buf1[16]; FXSYS_memset(iv, 0, 16); CRYPT_AESSetIV(aes, iv); CRYPT_AESEncrypt(aes, buf1, buf, 16); FX_Free(aes); pEncryptDict->SetAtString("Perms", CFX_ByteString(buf1, 16)); }
CFX_ByteString CPDF_Dictionary::GetString(const CFX_ByteStringC& key) const { CPDF_Object* p = GetElement(key); if (p) { return p->GetString(); } return CFX_ByteString(); }
void CFGAS_FontMgr::RegisterFace(FXFT_Face pFace, const CFX_WideString* pFaceName) { if ((pFace->face_flags & FT_FACE_FLAG_SCALABLE) == 0) return; std::unique_ptr<CFX_FontDescriptor> pFont(new CFX_FontDescriptor); pFont->m_dwFontStyles |= FXFT_Is_Face_Bold(pFace) ? FX_FONTSTYLE_Bold : 0; pFont->m_dwFontStyles |= FXFT_Is_Face_Italic(pFace) ? FX_FONTSTYLE_Italic : 0; pFont->m_dwFontStyles |= GetFlags(pFace); std::vector<uint16_t> charsets = GetCharsets(pFace); GetUSBCSB(pFace, pFont->m_dwUsb, pFont->m_dwCsb); FT_ULong dwTag; FT_ENC_TAG(dwTag, 'n', 'a', 'm', 'e'); std::vector<uint8_t> table; unsigned long nLength = 0; unsigned int error = FXFT_Load_Sfnt_Table(pFace, dwTag, 0, nullptr, &nLength); if (error == 0 && nLength != 0) { table.resize(nLength); if (FXFT_Load_Sfnt_Table(pFace, dwTag, 0, table.data(), nullptr)) table.clear(); } GetNames(table.empty() ? nullptr : table.data(), pFont->m_wsFamilyNames); pFont->m_wsFamilyNames.Add(CFX_ByteString(pFace->family_name).UTF8Decode()); pFont->m_wsFaceName = pFaceName ? *pFaceName : CFX_WideString::FromLocal(FXFT_Get_Postscript_Name(pFace)); pFont->m_nFaceIndex = pFace->face_index; m_InstalledFonts.Add(pFont.release()); }
std::unique_ptr<CPDF_Font> CPDF_Font::Create(CPDF_Document* pDoc, CPDF_Dictionary* pFontDict) { CFX_ByteString type = pFontDict->GetStringFor("Subtype"); std::unique_ptr<CPDF_Font> pFont; if (type == "TrueType") { CFX_ByteString tag = pFontDict->GetStringFor("BaseFont").Left(4); for (size_t i = 0; i < FX_ArraySize(kChineseFontNames); ++i) { if (tag == CFX_ByteString(kChineseFontNames[i], 4)) { CPDF_Dictionary* pFontDesc = pFontDict->GetDictFor("FontDescriptor"); if (!pFontDesc || !pFontDesc->KeyExist("FontFile2")) pFont.reset(new CPDF_CIDFont); break; } } if (!pFont) pFont.reset(new CPDF_TrueTypeFont); } else if (type == "Type3") { pFont.reset(new CPDF_Type3Font); } else if (type == "Type0") { pFont.reset(new CPDF_CIDFont); } else { pFont.reset(new CPDF_Type1Font); } pFont->m_pFontDict = pFontDict; pFont->m_pDocument = pDoc; pFont->m_BaseFont = pFontDict->GetStringFor("BaseFont"); return pFont->Load() ? std::move(pFont) : nullptr; }
void CPDF_StandardSecurityHandler::AES256_SetPerms(CPDF_Dictionary* pEncryptDict, FX_DWORD permissions, FX_BOOL bEncryptMetadata, FX_LPCBYTE key) { FX_BYTE buf[16]; buf[0] = (FX_BYTE)permissions; buf[1] = (FX_BYTE)(permissions >> 8); buf[2] = (FX_BYTE)(permissions >> 16); buf[3] = (FX_BYTE)(permissions >> 24); buf[4] = 0xff; buf[5] = 0xff; buf[6] = 0xff; buf[7] = 0xff; buf[8] = bEncryptMetadata ? 'T' : 'F'; buf[9] = 'a'; buf[10] = 'd'; buf[11] = 'b'; FX_BYTE* aes = FX_Alloc(FX_BYTE, 2048); CRYPT_AESSetKey(aes, 16, key, 32, TRUE); FX_BYTE iv[16], buf1[16]; FXSYS_memset32(iv, 0, 16); CRYPT_AESSetIV(aes, iv); CRYPT_AESEncrypt(aes, buf1, buf, 16); FX_Free(aes); pEncryptDict->SetAtString(FX_BSTRC("Perms"), CFX_ByteString(buf1, 16)); }
void CPDF_FormField::SyncFieldFlags() { CFX_ByteString type_name = FPDF_GetFieldAttr(m_pDict, "FT") ? FPDF_GetFieldAttr(m_pDict, "FT")->GetString() : CFX_ByteString(); uint32_t flags = FPDF_GetFieldAttr(m_pDict, "Ff") ? FPDF_GetFieldAttr(m_pDict, "Ff")->GetInteger() : 0; m_Flags = 0; if (flags & 1) m_Flags |= kFormFieldReadOnly; if (flags & 2) m_Flags |= kFormFieldRequired; if (flags & 4) m_Flags |= kFormFieldNoExport; if (type_name == "Btn") { if (flags & 0x8000) { m_Type = RadioButton; if (flags & 0x4000) m_Flags |= kFormRadioNoToggleOff; if (flags & 0x2000000) m_Flags |= kFormRadioUnison; } else if (flags & 0x10000) { m_Type = PushButton; } else { m_Type = CheckBox; } } else if (type_name == "Tx") { if (flags & 0x100000) { m_Type = File; } else if (flags & 0x2000000) { m_Type = RichText; } else { m_Type = Text; if (flags & 0x1000) m_Flags |= kFormTextMultiLine; if (flags & 0x2000) m_Flags |= kFormTextPassword; if (flags & 0x800000) m_Flags |= kFormTextNoScroll; if (flags & 0x100000) m_Flags |= kFormTextComb; } LoadDA(); } else if (type_name == "Ch") { if (flags & 0x20000) { m_Type = ComboBox; if (flags & 0x40000) m_Flags |= kFormComboEdit; } else { m_Type = ListBox; if (flags & 0x200000) m_Flags |= kFormListMultiSelect; } LoadDA(); } else if (type_name == "Sig") { m_Type = Sign; } }
CFX_ByteString CPDF_StreamContentParser::GetString(FX_DWORD index) { if (index >= m_ParamCount) { return CFX_ByteString(); } int real_index = m_ParamStartPos + m_ParamCount - index - 1; if (real_index >= PARAM_BUF_SIZE) { real_index -= PARAM_BUF_SIZE; } ContentParam& param = m_ParamBuf1[real_index]; if (param.m_Type == PDFOBJ_NAME) { return CFX_ByteString(param.m_Name.m_Buffer, param.m_Name.m_Len); } if (param.m_Type == 0 && param.m_pObject) { return param.m_pObject->GetString(); } return CFX_ByteString(); }
CFX_ByteString CPDF_Array::GetString(FX_DWORD i) const { if (this && i < (FX_DWORD)m_Objects.GetSize()) { CPDF_Object* p = (CPDF_Object*)m_Objects.GetAt(i); return p->GetString(); } return CFX_ByteString(); }
CFX_ByteString _FPDF_LoadTableFromTTStreamFile(IFX_FileStream* pFile, FX_LPCBYTE pTables, FX_DWORD nTables, FX_DWORD tag) { for (FX_DWORD i = 0; i < nTables; i ++) { FX_LPCBYTE p = pTables + i * 16; if (GET_TT_LONG(p) == tag) { FX_DWORD offset = GET_TT_LONG(p + 8); FX_DWORD size = GET_TT_LONG(p + 12); CFX_ByteString buffer; if (!pFile->ReadBlock(buffer.GetBuffer(size), offset, size)) { return CFX_ByteString(); } buffer.ReleaseBuffer(size); return buffer; } } return CFX_ByteString(); }