OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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/fpdfdoc/include/cpdf_filespec.h" |
| 8 |
| 9 #include "core/fpdfapi/fpdf_parser/include/cpdf_dictionary.h" |
| 10 #include "core/fpdfapi/fpdf_parser/include/cpdf_object.h" |
| 11 #include "core/fpdfapi/fpdf_parser/include/fpdf_parser_decode.h" |
| 12 #include "core/fxcrt/include/fx_system.h" |
| 13 |
| 14 namespace { |
| 15 |
| 16 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ || \ |
| 17 _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
| 18 CFX_WideString ChangeSlashToPlatform(const FX_WCHAR* str) { |
| 19 CFX_WideString result; |
| 20 while (*str) { |
| 21 if (*str == '/') { |
| 22 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ |
| 23 result += ':'; |
| 24 #else |
| 25 result += '\\'; |
| 26 #endif |
| 27 } else { |
| 28 result += *str; |
| 29 } |
| 30 str++; |
| 31 } |
| 32 return result; |
| 33 } |
| 34 |
| 35 CFX_WideString ChangeSlashToPDF(const FX_WCHAR* str) { |
| 36 CFX_WideString result; |
| 37 while (*str) { |
| 38 if (*str == '\\' || *str == ':') |
| 39 result += '/'; |
| 40 else |
| 41 result += *str; |
| 42 |
| 43 str++; |
| 44 } |
| 45 return result; |
| 46 } |
| 47 #endif // _FXM_PLATFORM_APPLE_ || _FXM_PLATFORM_WINDOWS_ |
| 48 |
| 49 } // namespace |
| 50 |
| 51 CFX_WideString CPDF_FileSpec::DecodeFileName(const CFX_WideStringC& filepath) { |
| 52 if (filepath.GetLength() <= 1) |
| 53 return CFX_WideString(); |
| 54 |
| 55 #if _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ |
| 56 if (filepath.Left(sizeof("/Mac") - 1) == CFX_WideStringC(L"/Mac")) |
| 57 return ChangeSlashToPlatform(filepath.c_str() + 1); |
| 58 return ChangeSlashToPlatform(filepath.c_str()); |
| 59 #elif _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
| 60 |
| 61 if (filepath.GetAt(0) != '/') |
| 62 return ChangeSlashToPlatform(filepath.c_str()); |
| 63 if (filepath.GetAt(1) == '/') |
| 64 return ChangeSlashToPlatform(filepath.c_str() + 1); |
| 65 if (filepath.GetAt(2) == '/') { |
| 66 CFX_WideString result; |
| 67 result += filepath.GetAt(1); |
| 68 result += ':'; |
| 69 result += ChangeSlashToPlatform(filepath.c_str() + 2); |
| 70 return result; |
| 71 } |
| 72 CFX_WideString result; |
| 73 result += '\\'; |
| 74 result += ChangeSlashToPlatform(filepath.c_str()); |
| 75 return result; |
| 76 #else |
| 77 return CFX_WideString(filepath); |
| 78 #endif |
| 79 } |
| 80 |
| 81 bool CPDF_FileSpec::GetFileName(CFX_WideString* csFileName) const { |
| 82 if (CPDF_Dictionary* pDict = m_pObj->AsDictionary()) { |
| 83 *csFileName = pDict->GetUnicodeTextBy("UF"); |
| 84 if (csFileName->IsEmpty()) { |
| 85 *csFileName = |
| 86 CFX_WideString::FromLocal(pDict->GetStringBy("F").AsStringC()); |
| 87 } |
| 88 if (pDict->GetStringBy("FS") == "URL") |
| 89 return true; |
| 90 if (csFileName->IsEmpty()) { |
| 91 if (pDict->KeyExist("DOS")) { |
| 92 *csFileName = |
| 93 CFX_WideString::FromLocal(pDict->GetStringBy("DOS").AsStringC()); |
| 94 } else if (pDict->KeyExist("Mac")) { |
| 95 *csFileName = |
| 96 CFX_WideString::FromLocal(pDict->GetStringBy("Mac").AsStringC()); |
| 97 } else if (pDict->KeyExist("Unix")) { |
| 98 *csFileName = |
| 99 CFX_WideString::FromLocal(pDict->GetStringBy("Unix").AsStringC()); |
| 100 } else { |
| 101 return false; |
| 102 } |
| 103 } |
| 104 } else if (m_pObj->IsString()) { |
| 105 *csFileName = CFX_WideString::FromLocal(m_pObj->GetString().AsStringC()); |
| 106 } else { |
| 107 return false; |
| 108 } |
| 109 *csFileName = DecodeFileName(csFileName->AsStringC()); |
| 110 return true; |
| 111 } |
| 112 |
| 113 CPDF_FileSpec::CPDF_FileSpec() { |
| 114 m_pObj = new CPDF_Dictionary; |
| 115 m_pObj->AsDictionary()->SetAtName("Type", "Filespec"); |
| 116 } |
| 117 |
| 118 CFX_WideString CPDF_FileSpec::EncodeFileName(const CFX_WideStringC& filepath) { |
| 119 if (filepath.GetLength() <= 1) |
| 120 return CFX_WideString(); |
| 121 |
| 122 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
| 123 if (filepath.GetAt(1) == ':') { |
| 124 CFX_WideString result; |
| 125 result = '/'; |
| 126 result += filepath.GetAt(0); |
| 127 if (filepath.GetAt(2) != '\\') |
| 128 result += '/'; |
| 129 |
| 130 result += ChangeSlashToPDF(filepath.c_str() + 2); |
| 131 return result; |
| 132 } |
| 133 if (filepath.GetAt(0) == '\\' && filepath.GetAt(1) == '\\') |
| 134 return ChangeSlashToPDF(filepath.c_str() + 1); |
| 135 |
| 136 if (filepath.GetAt(0) == '\\') { |
| 137 CFX_WideString result; |
| 138 result = '/'; |
| 139 result += ChangeSlashToPDF(filepath.c_str()); |
| 140 return result; |
| 141 } |
| 142 return ChangeSlashToPDF(filepath.c_str()); |
| 143 #elif _FXM_PLATFORM_ == _FXM_PLATFORM_APPLE_ |
| 144 if (filepath.Left(sizeof("Mac") - 1) == FX_WSTRC(L"Mac")) { |
| 145 CFX_WideString result; |
| 146 result = '/'; |
| 147 result += ChangeSlashToPDF(filepath.c_str()); |
| 148 return result; |
| 149 } |
| 150 return ChangeSlashToPDF(filepath.c_str()); |
| 151 #else |
| 152 return CFX_WideString(filepath); |
| 153 #endif |
| 154 } |
| 155 |
| 156 void CPDF_FileSpec::SetFileName(const CFX_WideStringC& wsFileName) { |
| 157 if (!m_pObj) |
| 158 return; |
| 159 |
| 160 CFX_WideString wsStr = EncodeFileName(wsFileName); |
| 161 if (m_pObj->IsString()) { |
| 162 m_pObj->SetString(CFX_ByteString::FromUnicode(wsStr)); |
| 163 } else if (CPDF_Dictionary* pDict = m_pObj->AsDictionary()) { |
| 164 pDict->SetAtString("F", CFX_ByteString::FromUnicode(wsStr)); |
| 165 pDict->SetAtString("UF", PDF_EncodeText(wsStr)); |
| 166 } |
| 167 } |
OLD | NEW |