-
Notifications
You must be signed in to change notification settings - Fork 1
/
axwrpdf.cpp
642 lines (562 loc) · 15.9 KB
/
axwrpdf.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
// axwinpdf.cpp : DLL アプリケーション用にエクスポートされる関数を定義します。
//
#include "stdafx.h"
#include <cstdlib>
#include <utility>
#include <vector>
#include <string>
#include <algorithm>
#include <memory>
#include <thread>
#include <ctime>
#include <windows.h>
#include <Shlwapi.h>
#include <shcore.h>
#include <assert.h>
#include <wincodec.h>
#include <comdef.h>
#include <safeint.h>
#include <windows.storage.h>
#include <windows.storage.streams.h>
#include <windows.data.pdf.h>
#include <windows.data.pdf.interop.h>
#include <wrl/client.h>
#include <wrl/async.h>
#include <wrl/event.h>
#include <wrl/wrappers/corewrappers.h>
#pragma comment(lib, "Shlwapi.lib")
#pragma comment(lib, "shcore.lib")
#pragma comment(lib, "runtimeobject.lib")
#include "utility.hpp"
using namespace ABI::Windows::Foundation;
using namespace ABI::Windows::Data::Pdf;
using namespace ABI::Windows::Storage;
using namespace ABI::Windows::Storage::Streams;
using namespace Microsoft::WRL;
using namespace Microsoft::WRL::Wrappers;
#define CHECK_HR(...) \
do { \
HRESULT _hr = __VA_ARGS__; \
if (FAILED(_hr)) { \
_com_error err(_hr); \
OutputDebugString(err.ErrorMessage()); \
_com_issue_error(_hr); \
} \
} while (0)
#define REFCOUNT_DEBUG(async) OutputDebugString((L"<<" L#async L">>ref count: " + std::to_wstring((async->AddRef(), async->Release())) + L"\n").c_str())
#define AXWRPDF_USE_THUNK_THREAD
static HMODULE g_hModule = nullptr;
static bool getAccurateSize = false;
static float scaling = 2.0;
// エラーコード
#define SPI_SUCCESS 0 // 正常終了
#define SPI_NOT_IMPLEMENT (-1) // その機能はインプリメントされていない
#define SPI_USER_CANCEL 1 // コールバック関数が非0を返したので展開を中止した
#define SPI_UNKNOWN_FORMAT 2 // 未知のフォーマット
#define SPI_DATA_BROKEN 3 // データが壊れている
#define SPI_NO_MEMORY 4 // メモリーが確保出来ない
#define SPI_MEMORY_ERR 5 // メモリーエラー(Lock出来ない、等)
#define SPI_FILE_READ_ERR 6 // ファイルリードエラー
#define SPI_RESERVED_ERR 7 // (予約)
#define SPI_INTERNAL_ERR 8 // 内部エラー
#ifdef _WIN64
typedef __time64_t susie_time_t;
#else
typedef __time32_t susie_time_t;
#endif
#include <pshpack1.h>
// ファイル情報構造体
struct fileInfo
{
unsigned char method[8]; // 圧縮法の種類
ULONG_PTR position; // ファイル上での位置
ULONG_PTR compsize; // 圧縮されたサイズ
ULONG_PTR filesize; // 元のファイルサイズ
susie_time_t timestamp; // ファイルの更新日時
char path[200]; // 相対パス
char filename[200]; // ファイルネーム
unsigned long crc; // CRC
#ifdef _WIN64
// 64bit版の構造体サイズは444bytesですが、実際のサイズは
// アラインメントにより448bytesになります。環境によりdummyが必要です。
char dummy[4];
#endif
};
#include <poppack.h>
struct fileInfoW
{
unsigned char method[8]; // 圧縮法の種類
ULONG_PTR position; // ファイル上での位置
ULONG_PTR compsize; // 圧縮されたサイズ
ULONG_PTR filesize; // 元のファイルサイズ
susie_time_t timestamp; // ファイルの更新日時
WCHAR path[200]; // 相対パス
WCHAR filename[200]; // ファイルネーム
unsigned long crc; // CRC
#ifdef _WIN64
char dummy[4];
#endif
};
enum class SpiResult
{
Success = SPI_SUCCESS,
NotImplement = SPI_NOT_IMPLEMENT,
UserCancel = SPI_USER_CANCEL,
UnknownFormat = SPI_UNKNOWN_FORMAT,
DataBroken = SPI_DATA_BROKEN,
NoMemory = SPI_NO_MEMORY,
MemoryError = SPI_MEMORY_ERR,
FileReadError = SPI_FILE_READ_ERR,
Resered = SPI_RESERVED_ERR,
InternalError = SPI_INTERNAL_ERR,
};
inline HRESULT AsRandomAccessStream(IStream* src, _Outptr_ IRandomAccessStream** stream, BSOS_OPTIONS option = BSOS_DEFAULT) noexcept
{
return ::CreateRandomAccessStreamOverStream(src, option, IID_PPV_ARGS(stream));
}
class Waiter
{
Event m_event;
public:
Waiter()
: m_event(::CreateEventEx(nullptr, nullptr, CREATE_EVENT_MANUAL_RESET, EVENT_ALL_ACCESS))
{
}
void Set()
{
::SetEvent(m_event.Get());
}
HANDLE GetHandle()
{
return m_event.Get();
}
void Reset()
{
::ResetEvent(m_event.Get());
}
operator Event& ()
{
return m_event;
}
bool Wait(DWORD timeout = INFINITE)
{
return ::WaitForSingleObjectEx(m_event.Get(), timeout, FALSE) == WAIT_OBJECT_0;
}
DWORD get_Status()
{
DWORD dw = ::WaitForSingleObjectEx(m_event.Get(), 0, FALSE);
return dw;
}
__declspec(property(get = get_Status)) DWORD Status;
};
inline HRESULT GetExtensionFromEncoder(REFGUID encoderId, WCHAR* fileExtensions, UINT cchfileExtensions) noexcept
{
HRESULT hr;
ComPtr<IWICBitmapEncoder> encoder;
hr = ::CoCreateInstance(encoderId, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&encoder));
if (FAILED(hr)) return hr;
ComPtr<IWICBitmapEncoderInfo> encoderInfo;
hr = encoder->GetEncoderInfo(&encoderInfo);
if (FAILED(hr)) return hr;
UINT cchActual;
hr = encoderInfo->GetFileExtensions(cchfileExtensions, fileExtensions, &cchActual);
if (FAILED(hr)) return hr;
auto sep = wcschr(fileExtensions, L';');
if (sep)
{
*sep = L'\0';
}
return S_OK;
}
EXTERN_C int __stdcall GetPluginInfo(int infono, LPSTR buf, int buflen)
{
if (buf == nullptr || buflen <= 0)
{
return 0;
}
if (infono == 0)
{
return static_cast<int>(::strlen(::lstrcpynA(buf, "00AM", buflen)));
}
else if (infono == 1)
{
return static_cast<int>(::strlen(::lstrcpynA(buf, "axwrpdf build at " __TIMESTAMP__, buflen)));
}
else if (infono == 2)
{
return static_cast<int>(::strlen(::lstrcpynA(buf, ".pdf", buflen)));
}
else if (infono == 3)
{
return static_cast<int>(::strlen(::lstrcpynA(buf, "PDF", buflen)));
}
return 0;
}
EXTERN_C int __stdcall IsSupported(LPSTR filename, void* dw)
{
BYTE buf[2048 + 1] = {};
if (HIWORD(dw) == 0) // file handle
{
if (::ReadFile(dw, buf, _countof(buf), nullptr, nullptr))
{
dw = buf;
}
if (::GetLastError() != ERROR_MORE_DATA)
{
return FALSE;
}
}
if (memcmp(dw, "%PDF-", 5) == 0)
return TRUE;
return FALSE;
}
inline SpiResult HresultToSpiResult(HRESULT hr) noexcept
{
switch (hr)
{
case S_OK:
case S_FALSE:
return SpiResult::Success;
case E_NOTIMPL:
return SpiResult::NotImplement;
case E_OUTOFMEMORY:
return SpiResult::NoMemory;
case E_ABORT:
return SpiResult::UserCancel;
case E_ACCESSDENIED:
return SpiResult::FileReadError;
default:
return SpiResult::InternalError;
}
}
inline void GetPdfPage(IPdfPage* page, IStream* outStream, HANDLE hCompleteEvent)
{
UINT i;
CHECK_HR(page->get_Index(&i));
auto callback = Callback<IAsyncActionCompletedHandler>([i, &hCompleteEvent](IAsyncAction* async, AsyncStatus status)
{
OutputDebugString((L"Start " + std::to_wstring(i) + L"\n").c_str());
if (status != AsyncStatus::Completed)
{
return S_FALSE;
}
OutputDebugString((L"End " + std::to_wstring(i) + L"\n").c_str());
::SetEvent(hCompleteEvent);
return S_OK;
});
ComPtr<IRandomAccessStream> stream;
CHECK_HR(AsRandomAccessStream(outStream, &stream));
ComPtr<IPdfPageRenderOptions> renderOptions;
CHECK_HR(Windows::Foundation::ActivateInstance(
HStringReference(RuntimeClass_Windows_Data_Pdf_PdfPageRenderOptions).Get(), &renderOptions));
if (scaling != 1.0)
{
Size size;
CHECK_HR(page->get_Size(&size));
UINT32 height = static_cast<UINT32>(size.Height * scaling);
UINT32 width = static_cast<UINT32>(size.Width * scaling);
CHECK_HR(renderOptions->put_DestinationHeight(height));
CHECK_HR(renderOptions->put_DestinationWidth(width));
}
ComPtr<IAsyncAction> async;
CHECK_HR(page->RenderWithOptionsToStreamAsync(stream.Get(), renderOptions.Get(), &async));
CHECK_HR(async->put_Completed(callback.Get()));
}
inline SIZE_T GetPdfPageSize(IPdfPage* page)
{
Waiter w;
ComPtr<IStream> memoryStream;
CHECK_HR(::CreateStreamOnHGlobal(nullptr, TRUE, &memoryStream));
GetPdfPage(page, memoryStream.Get(), w.GetHandle());
w.Wait();
HGLOBAL hGlobal = nullptr;
CHECK_HR(::GetHGlobalFromStream(memoryStream.Get(), &hGlobal));
return ::GlobalSize(hGlobal);
}
inline void PdfPageToFileInfo(IPdfPage* page, UINT index, fileInfo& fileInfo, LPCSTR ext, SIZE_T filesize) noexcept
{
memcpy(fileInfo.method, "PDF\0", 4);
fileInfo.position = index;
fileInfo.compsize = filesize;
fileInfo.filesize = filesize;
fileInfo.timestamp = static_cast<susie_time_t>(std::time(nullptr));
sprintf_s(fileInfo.filename, "%08u%s", index, ext);
fileInfo.crc = 0;
fileInfo.path[0] = NULL;
}
template<class TCallback>
HRESULT GetPdfDocument(LPCSTR buf, LONG_PTR len, bool isMemory, TCallback action)
{
try
{
ComPtr<IPdfDocument> doc;
ComPtr<IAsyncOperation<PdfDocument*>> async;
Waiter w;
{
ComPtr<IRandomAccessStream> inStream;
if (isMemory)
{
return E_NOTIMPL;
}
else
{
CHECK_HR(::CreateRandomAccessStreamOnFile(
a2wstring(buf).c_str(), FileAccessMode_Read, IID_PPV_ARGS(&inStream)));
}
ComPtr<IPdfDocumentStatics> pdfDocumentsStatics;
CHECK_HR(Windows::Foundation::GetActivationFactory(
HStringReference(RuntimeClass_Windows_Data_Pdf_PdfDocument).Get(), &pdfDocumentsStatics));
//return S_OK;
CHECK_HR(pdfDocumentsStatics->LoadFromStreamAsync(inStream.Get(), &async));
}
HRESULT hr;
auto callback = Callback<IAsyncOperationCompletedHandler<PdfDocument*>>(
[&](_In_ IAsyncOperation<PdfDocument*>* pdfAsync, AsyncStatus status)
{
if (status == AsyncStatus::Started)
return S_FALSE;
std::shared_ptr<void> x(((Event&)w).Get(), ::SetEvent);
if (status != AsyncStatus::Completed)
return S_FALSE;
try
{
CHECK_HR(pdfAsync->GetResults(&doc));
ComPtr<IPdfPageRenderOptions> renderOptions;
WCHAR ext[MAX_PATH];
{
CHECK_HR(Windows::Foundation::ActivateInstance(
HStringReference(RuntimeClass_Windows_Data_Pdf_PdfPageRenderOptions).Get(), &renderOptions));
GUID encoderId;
CHECK_HR(renderOptions->get_BitmapEncoderId(&encoderId));
CHECK_HR(GetExtensionFromEncoder(encoderId, ext, _countof(ext)));
}
hr = action(doc.Get(), ext);
return hr;
}
catch (const _com_error& e)
{
return e.Error();
}
});
CHECK_HR(async->put_Completed(callback.Get()));
w.Wait(60 * 1000);
return hr;
}
catch (const _com_error& e)
{
return e.Error();
}
}
HRESULT GetArchiveInfoInternal(LPCSTR buf, LONG_PTR len, unsigned int flag, HLOCAL* lphInf)
{
if (lphInf == nullptr)
{
return E_INVALIDARG;
}
HRESULT hr = GetPdfDocument(buf, len, flag & 0b000111, [&](IPdfDocument* doc, const WCHAR(&ext)[MAX_PATH])
{
try
{
UINT count;
CHECK_HR(doc->get_PageCount(&count));
UINT countPlusOne;
if (!msl::utilities::SafeAdd(count, 1u, countPlusOne))
{
return E_OUTOFMEMORY;
}
auto mem = ::LocalAlloc(LMEM_ZEROINIT, sizeof(fileInfo) * countPlusOne);
if (mem == nullptr)
return E_OUTOFMEMORY;
std::unique_ptr<fileInfo[], decltype(&::LocalFree) > fileList(reinterpret_cast<fileInfo*>(mem), &::LocalFree);
for (UINT i = 0; i < count; i++)
{
ComPtr<IPdfPage> page;
CHECK_HR(doc->GetPage(i, &page));
SIZE_T fileSize = getAccurateSize ? GetPdfPageSize(page.Get()) : 1;
PdfPageToFileInfo(page.Get(), i, fileList[i], w2string(ext).c_str(), fileSize);
}
*lphInf = fileList.release();
return S_OK;
}
catch (const _com_error& e)
{
return e.Error();
}
catch (const std::bad_alloc&)
{
return E_OUTOFMEMORY;
}
});
return hr;
}
EXTERN_C int __stdcall GetArchiveInfo(LPCSTR buf, LONG_PTR len, unsigned int flag, HLOCAL* lphInf) noexcept
{
HRESULT hr;
try
{
#ifdef AXWRPDF_USE_THUNK_THREAD
std::thread worker([&] {
RoInitializeWrapper init(RO_INIT_MULTITHREADED);
hr = init;
if (FAILED(hr)) return;
#endif
hr = GetArchiveInfoInternal(buf, len, flag, lphInf);
#ifdef AXWRPDF_USE_THUNK_THREAD
});
worker.join();
#endif
}
catch (const _com_error& err)
{
hr = err.Error();
}
return (int)HresultToSpiResult(hr);
}
HRESULT GetFileInfoInternal(LPCSTR buf, LONG_PTR len, LPCSTR filename, unsigned int flag, fileInfo* lpInfo)
{
HRESULT hr = GetPdfDocument(buf, len, flag & 0b000111, [&](IPdfDocument* doc, const WCHAR(&ext)[MAX_PATH])
{
try
{
UINT count;
CHECK_HR(doc->get_PageCount(&count));
UINT index = count;
sscanf_s(filename, "%08u", &index);
if (index >= count)
{
return E_INVALIDARG;
}
ComPtr<ABI::Windows::Data::Pdf::IPdfPage> page;
CHECK_HR(doc->GetPage(index, &page));
#if 0 // NVIDIA Dispaly Driver 26.21.14.3527より前だと、プロセス終了時になぜかヒープ破損が発生していた。
if (i == count - 1)
{
REFCOUNT_DEBUG(page.Get());
page.Get()->AddRef();// TODO なぜか参照カウントを増やさないと終了時にアクセス違反が発生する。
//workaroundRef = page.Get();
REFCOUNT_DEBUG(page.Get());
}
#endif
SIZE_T fileSize = getAccurateSize ? GetPdfPageSize(page.Get()) : 1;
PdfPageToFileInfo(page.Get(), index, *lpInfo, w2string(ext).c_str(), fileSize);
return S_OK;
}
catch (const _com_error& e)
{
return e.Error();
}
catch (const std::bad_alloc&)
{
return E_OUTOFMEMORY;
}
});
return hr;
}
EXTERN_C int __stdcall GetFileInfo(LPCSTR buf, LONG_PTR len, LPCSTR filename, unsigned int flag, fileInfo* lpInfo) noexcept
{
HRESULT hr;
try
{
#ifdef AXWRPDF_USE_THUNK_THREAD
std::thread worker([&] {
RoInitializeWrapper init(RO_INIT_MULTITHREADED);
hr = init;
if (FAILED(hr)) return;
#endif
hr = GetFileInfoInternal(buf, len, filename, flag, lpInfo);
#ifdef AXWRPDF_USE_THUNK_THREAD
});
worker.join();
#endif
}
catch (const std::exception&)
{
return (int)HresultToSpiResult(E_FAIL);
}
return (int)HresultToSpiResult(hr);
}
HRESULT GetFileInternal(LPCSTR buf, LONG_PTR len, LPSTR dest, unsigned int flag, FARPROC /*progressCallback*/, intptr_t /*lData*/)
{
const bool useFile = (flag & 0b0000011100000000) == 0;
HRESULT hr = GetPdfDocument(buf, len, flag & 0b000111, [&](IPdfDocument* doc, const WCHAR(&ext)[MAX_PATH])
{
try
{
UINT count;
CHECK_HR(doc->get_PageCount(&count));
if (len >= count)
{
return E_INVALIDARG;
}
UINT index = len;
ComPtr<IStream> outStream;
if (useFile)
{
WCHAR path[MAX_PATH] = {};
swprintf_s(path, L"%S\\%08u%s", buf, index, ext);
CHECK_HR(::SHCreateStreamOnFileEx(path, STGM_WRITE | STGM_CREATE, FILE_ATTRIBUTE_NORMAL, TRUE, nullptr, &outStream));
}
else
{
CHECK_HR(::CreateStreamOnHGlobal(nullptr, FALSE, &outStream));
}
Waiter w;
ComPtr<ABI::Windows::Data::Pdf::IPdfPage> page;
CHECK_HR(doc->GetPage(index, &page));
GetPdfPage(page.Get(), outStream.Get(), w.GetHandle());
#if 0 // NVIDIA Dispaly Driver 26.21.14.3527より前だと、プロセス終了時になぜかヒープ破損が発生していた。
if (i == count - 1)
{
REFCOUNT_DEBUG(page.Get());
page.Get()->AddRef();// TODO なぜか参照カウントを増やさないと終了時にアクセス違反が発生する。
//workaroundRef = page.Get();
REFCOUNT_DEBUG(page.Get());
}
#endif
w.Wait();
if (useFile)
{
// nothing
}
else
{
HGLOBAL hGlobal = nullptr;
CHECK_HR(::GetHGlobalFromStream(outStream.Get(), &hGlobal));
*reinterpret_cast<HLOCAL*>(dest) = hGlobal;
}
return S_OK;
}
catch (const _com_error& e)
{
return e.Error();
}
catch (const std::bad_alloc&)
{
return E_OUTOFMEMORY;
}
});
return hr;
}
EXTERN_C int __stdcall GetFile(LPCSTR buf, LONG_PTR len, LPSTR dest, unsigned int flag, FARPROC progressCallback, intptr_t lData) noexcept
{
HRESULT hr;
try
{
#ifdef AXWRPDF_USE_THUNK_THREAD
std::thread worker([&] {
RoInitializeWrapper init(RO_INIT_MULTITHREADED);
hr = init;
if (FAILED(hr)) return;
#endif
hr = GetFileInternal(buf, len, dest, flag, progressCallback, lData);
#ifdef AXWRPDF_USE_THUNK_THREAD
});
worker.join();
#endif
}
catch (const std::exception&)
{
return (int)HresultToSpiResult(E_FAIL);
}
return (int)HresultToSpiResult(hr);
}