forked from DBCTRADO/TSTask
-
Notifications
You must be signed in to change notification settings - Fork 2
/
tstask.patch
6758 lines (6693 loc) · 206 KB
/
tstask.patch
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
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
diff -ruN a/TSTask/BonTsEngine/BonTsEngine.vcxproj b/TSTask/BonTsEngine/BonTsEngine.vcxproj
--- a/TSTask/BonTsEngine/BonTsEngine.vcxproj 2015-08-08 23:16:06.000000000 +0900
+++ b/TSTask/BonTsEngine/BonTsEngine.vcxproj 2015-08-12 19:35:00.000000000 +0900
@@ -35,6 +35,8 @@
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Use</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Use</PrecompiledHeader>
</ClCompile>
+ <ClCompile Include="CardReader.cpp" />
+ <ClCompile Include="CasCard.cpp" />
<ClCompile Include="EventInfo.cpp" />
<ClCompile Include="Exception.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Use</PrecompiledHeader>
@@ -52,6 +54,8 @@
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Use</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Use</PrecompiledHeader>
</ClCompile>
+ <ClCompile Include="Multi2Decoder.cpp" />
+ <ClCompile Include="Multi2DecoderSIMD.cpp" />
<ClCompile Include="PATGenerator.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Use</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Use</PrecompiledHeader>
@@ -68,6 +72,7 @@
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Use</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Use</PrecompiledHeader>
</ClCompile>
+ <ClCompile Include="TsDescrambler.cpp" />
<ClCompile Include="TsDescriptor.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Use</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Use</PrecompiledHeader>
@@ -117,6 +122,8 @@
<ClInclude Include="BonBaseClass.h" />
<ClInclude Include="BonSrcDecoder.h" />
<ClInclude Include="BonTsEngineConfig.h" />
+ <ClInclude Include="CardReader.h" />
+ <ClInclude Include="CasCard.h" />
<ClInclude Include="Common.h" />
<ClInclude Include="EventInfo.h" />
<ClInclude Include="Exception.h" />
@@ -125,10 +132,13 @@
<ClInclude Include="MediaData.h" />
<ClInclude Include="MediaDecoder.h" />
<ClInclude Include="MediaTee.h" />
+ <ClInclude Include="Multi2Decoder.h" />
+ <ClInclude Include="Multi2DecoderSIMD.h" />
<ClInclude Include="PATGenerator.h" />
<ClInclude Include="stdafx.h" />
<ClInclude Include="StdUtil.h" />
<ClInclude Include="TsAnalyzer.h" />
+ <ClInclude Include="TsDescrambler.h" />
<ClInclude Include="TsDescriptor.h" />
<ClInclude Include="TsEncode.h" />
<ClInclude Include="TsGrabber.h" />
diff -ruN a/TSTask/BonTsEngine/BonTsEngine.vcxproj.filters b/TSTask/BonTsEngine/BonTsEngine.vcxproj.filters
--- a/TSTask/BonTsEngine/BonTsEngine.vcxproj.filters 2015-08-08 23:16:06.000000000 +0900
+++ b/TSTask/BonTsEngine/BonTsEngine.vcxproj.filters 2015-08-12 19:35:00.000000000 +0900
@@ -86,6 +86,21 @@
<ClCompile Include="TsRecorder.cpp">
<Filter>繧ス繝シ繧ケ 繝輔ぃ繧、繝ォ</Filter>
</ClCompile>
+ <ClCompile Include="TsDescrambler.cpp">
+ <Filter>繧ス繝シ繧ケ 繝輔ぃ繧、繝ォ</Filter>
+ </ClCompile>
+ <ClCompile Include="CardReader.cpp">
+ <Filter>繧ス繝シ繧ケ 繝輔ぃ繧、繝ォ</Filter>
+ </ClCompile>
+ <ClCompile Include="CasCard.cpp">
+ <Filter>繧ス繝シ繧ケ 繝輔ぃ繧、繝ォ</Filter>
+ </ClCompile>
+ <ClCompile Include="Multi2Decoder.cpp">
+ <Filter>繧ス繝シ繧ケ 繝輔ぃ繧、繝ォ</Filter>
+ </ClCompile>
+ <ClCompile Include="Multi2DecoderSIMD.cpp">
+ <Filter>繧ス繝シ繧ケ 繝輔ぃ繧、繝ォ</Filter>
+ </ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Bitstream.h">
@@ -178,5 +193,20 @@
<ClInclude Include="TsRecorder.h">
<Filter>繝倥ャ繝繝シ 繝輔ぃ繧、繝ォ</Filter>
</ClInclude>
+ <ClInclude Include="Multi2DecoderSIMD.h">
+ <Filter>繝倥ャ繝繝シ 繝輔ぃ繧、繝ォ</Filter>
+ </ClInclude>
+ <ClInclude Include="TsDescrambler.h">
+ <Filter>繝倥ャ繝繝シ 繝輔ぃ繧、繝ォ</Filter>
+ </ClInclude>
+ <ClInclude Include="Multi2Decoder.h">
+ <Filter>繝倥ャ繝繝シ 繝輔ぃ繧、繝ォ</Filter>
+ </ClInclude>
+ <ClInclude Include="CardReader.h">
+ <Filter>繝倥ャ繝繝シ 繝輔ぃ繧、繝ォ</Filter>
+ </ClInclude>
+ <ClInclude Include="CasCard.h">
+ <Filter>繝倥ャ繝繝シ 繝輔ぃ繧、繝ォ</Filter>
+ </ClInclude>
</ItemGroup>
</Project>
\ 繝輔ぃ繧、繝ォ譛ォ蟆セ縺ォ謾ケ陦後′縺ゅj縺セ縺帙s
diff -ruN a/TSTask/BonTsEngine/CardReader.cpp b/TSTask/BonTsEngine/CardReader.cpp
--- a/TSTask/BonTsEngine/CardReader.cpp 1970-01-01 09:00:00.000000000 +0900
+++ b/TSTask/BonTsEngine/CardReader.cpp 2015-08-12 19:35:00.000000000 +0900
@@ -0,0 +1,1131 @@
+#include "stdafx.h"
+#include "CardReader.h"
+#include "StdUtil.h"
+
+#ifdef _DEBUG
+#undef THIS_FILE
+static char THIS_FILE[]=__FILE__;
+#define new DEBUG_NEW
+#endif
+
+
+
+
+CCardReader::CCardReader()
+{
+}
+
+
+CCardReader::~CCardReader()
+{
+}
+
+
+LPCTSTR CCardReader::EnumReader(int Index) const
+{
+ if (Index != 0)
+ return NULL;
+ return GetReaderName();
+}
+
+
+CCardReader *CCardReader::CreateCardReader(ReaderType Type)
+{
+ CCardReader *pReader;
+
+ switch (Type) {
+ case READER_SCARD:
+ pReader = new CSCardReader;
+ break;
+
+#ifdef CARDREADER_SCARD_DYNAMIC_SUPPORT
+ case READER_SCARD_DYNAMIC:
+ pReader = new CDynamicSCardReader;
+ break;
+#endif
+
+#ifdef CARDREADER_BONCASCLIENT_SUPPORT
+ case READER_BONCASCLIENT:
+ pReader = new CBonCasClientCardReader;
+ break;
+#endif
+
+ default:
+ return NULL;
+ }
+
+ pReader->m_ReaderType = Type;
+
+ return pReader;
+}
+
+
+
+
+///////////////////////////////////////////////////////////////////////////////
+//
+// 標準スマートカードリーダー
+//
+///////////////////////////////////////////////////////////////////////////////
+
+
+#pragma comment(lib, "WinScard.lib")
+
+
+static LPCTSTR GetSCardErrorText(LONG Code)
+{
+ switch (Code) {
+ case ERROR_BROKEN_PIPE:
+ return TEXT("ERROR_BROKEN_PIPE: The client attempted a smart card operation in a remote session.");
+ case SCARD_F_INTERNAL_ERROR:
+ return TEXT("SCARD_F_INTERNAL_ERROR: An internal consistency check failed.");
+ case SCARD_E_CANCELLED:
+ return TEXT("SCARD_E_CANCELLED: The action was cancelled by an SCardCancel request.");
+ case SCARD_E_INVALID_HANDLE:
+ return TEXT("SCARD_E_INVALID_HANDLE: The supplied handle was invalid.");
+ case SCARD_E_INVALID_PARAMETER:
+ return TEXT("SCARD_E_INVALID_PARAMETER: One or more of the supplied parameters could not be properly interpreted.");
+ case SCARD_E_INVALID_TARGET:
+ return TEXT("SCARD_E_INVALID_TARGET: Registry startup information is missing or invalid.");
+ case SCARD_E_NO_MEMORY:
+ return TEXT("SCARD_E_NO_MEMORY: Not enough memory available to complete this command.");
+ case SCARD_F_WAITED_TOO_LONG:
+ return TEXT("SCARD_F_WAITED_TOO_LONG: An internal consistency timer has expired.");
+ case SCARD_E_INSUFFICIENT_BUFFER:
+ return TEXT("SCARD_E_INSUFFICIENT_BUFFER: The data buffer to receive returned data is too small for the returned data.");
+ case SCARD_E_UNKNOWN_READER:
+ return TEXT("SCARD_E_UNKNOWN_READER: The specified reader name is not recognized.");
+ case SCARD_E_TIMEOUT:
+ return TEXT("SCARD_E_TIMEOUT: The user-specified timeout value has expired.");
+ case SCARD_E_SHARING_VIOLATION:
+ return TEXT("SCARD_E_SHARING_VIOLATION: The smart card cannot be accessed because of other connections outstanding.");
+ case SCARD_E_NO_SMARTCARD:
+ return TEXT("SCARD_E_NO_SMARTCARD: The operation requires a Smart Card, but no Smart Card is currently in the device.");
+ case SCARD_E_UNKNOWN_CARD:
+ return TEXT("SCARD_E_UNKNOWN_CARD: The specified smart card name is not recognized.");
+ case SCARD_E_CANT_DISPOSE:
+ return TEXT("SCARD_E_CANT_DISPOSE: The system could not dispose of the media in the requested manner.");
+ case SCARD_E_PROTO_MISMATCH:
+ return TEXT("SCARD_E_PROTO_MISMATCH: The requested protocols are incompatible with the protocol currently in use with the smart card.");
+ case SCARD_E_NOT_READY:
+ return TEXT("SCARD_E_NOT_READY: The reader or smart card is not ready to accept commands.");
+ case SCARD_E_INVALID_VALUE:
+ return TEXT("SCARD_E_INVALID_VALUE: One or more of the supplied parameters values could not be properly interpreted.");
+ case SCARD_E_SYSTEM_CANCELLED:
+ return TEXT("SCARD_E_SYSTEM_CANCELLED: The action was cancelled by the system, presumably to log off or shut down.");
+ case SCARD_F_COMM_ERROR:
+ return TEXT("SCARD_F_COMM_ERROR: An internal communications error has been detected.");
+ case SCARD_F_UNKNOWN_ERROR:
+ return TEXT("SCARD_F_UNKNOWN_ERROR: An internal error has been detected, but the source is unknown.");
+ case SCARD_E_INVALID_ATR:
+ return TEXT("SCARD_E_INVALID_ATR: An ATR obtained from the registry is not a valid ATR string.");
+ case SCARD_E_NOT_TRANSACTED:
+ return TEXT("SCARD_E_NOT_TRANSACTED: An attempt was made to end a non-existent transaction.");
+ case SCARD_E_READER_UNAVAILABLE:
+ return TEXT("SCARD_E_READER_UNAVAILABLE: The specified reader is not currently available for use.");
+ case SCARD_P_SHUTDOWN:
+ return TEXT("SCARD_P_SHUTDOWN: The operation has been aborted to allow the server application to exit.");
+ case SCARD_E_PCI_TOO_SMALL:
+ return TEXT("SCARD_E_PCI_TOO_SMALL: The PCI Receive buffer was too small.");
+ case SCARD_E_READER_UNSUPPORTED:
+ return TEXT("SCARD_E_READER_UNSUPPORTED: The reader driver does not meet minimal requirements for support.");
+ case SCARD_E_DUPLICATE_READER:
+ return TEXT("SCARD_E_DUPLICATE_READER: The reader driver did not produce a unique reader name.");
+ case SCARD_E_CARD_UNSUPPORTED:
+ return TEXT("SCARD_E_CARD_UNSUPPORTED: The smart card does not meet minimal requirements for support.");
+ case SCARD_E_NO_SERVICE:
+ return TEXT("SCARD_E_NO_SERVICE: The Smart card resource manager is not running.");
+ case SCARD_E_SERVICE_STOPPED:
+ return TEXT("SCARD_E_SERVICE_STOPPED: The Smart card resource manager has shut down.");
+ case SCARD_E_UNEXPECTED:
+ return TEXT("SCARD_E_UNEXPECTED: An unexpected card error has occurred.");
+ case SCARD_E_ICC_INSTALLATION:
+ return TEXT("SCARD_E_ICC_INSTALLATION: No Primary Provider can be found for the smart card.");
+ case SCARD_E_ICC_CREATEORDER:
+ return TEXT("SCARD_E_ICC_CREATEORDER: The requested order of object creation is not supported.");
+ case SCARD_E_UNSUPPORTED_FEATURE:
+ return TEXT("SCARD_E_UNSUPPORTED_FEATURE: This smart card does not support the requested feature.");
+ case SCARD_E_DIR_NOT_FOUND:
+ return TEXT("SCARD_E_DIR_NOT_FOUND: The identified directory does not exist in the smart card.");
+ case SCARD_E_FILE_NOT_FOUND:
+ return TEXT("SCARD_E_FILE_NOT_FOUND: The identified file does not exist in the smart card.");
+ case SCARD_E_NO_DIR:
+ return TEXT("SCARD_E_NO_DIR: The supplied path does not represent a smart card directory.");
+ case SCARD_E_NO_FILE:
+ return TEXT("SCARD_E_NO_FILE: The supplied path does not represent a smart card file.");
+ case SCARD_E_NO_ACCESS:
+ return TEXT("SCARD_E_NO_ACCESS: Access is denied to this file.");
+ case SCARD_E_WRITE_TOO_MANY:
+ return TEXT("SCARD_E_WRITE_TOO_MANY: The smartcard does not have enough memory to store the information.");
+ case SCARD_E_BAD_SEEK:
+ return TEXT("SCARD_E_BAD_SEEK: There was an error trying to set the smart card file object pointer.");
+ case SCARD_E_INVALID_CHV:
+ return TEXT("SCARD_E_INVALID_CHV: The supplied PIN is incorrect.");
+ case SCARD_E_UNKNOWN_RES_MNG:
+ return TEXT("SCARD_E_UNKNOWN_RES_MNG: An unrecognized error code was returned from a layered component.");
+ case SCARD_E_NO_SUCH_CERTIFICATE:
+ return TEXT("SCARD_E_NO_SUCH_CERTIFICATE: The requested certificate does not exist.");
+ case SCARD_E_CERTIFICATE_UNAVAILABLE:
+ return TEXT("SCARD_E_CERTIFICATE_UNAVAILABLE: The requested certificate could not be obtained.");
+ case SCARD_E_NO_READERS_AVAILABLE:
+ return TEXT("SCARD_E_NO_READERS_AVAILABLE: Cannot find a smart card reader.");
+ case SCARD_E_COMM_DATA_LOST:
+ return TEXT("SCARD_E_COMM_DATA_LOST: A communications error with the smart card has been detected. Retry the operation.");
+ case SCARD_E_NO_KEY_CONTAINER:
+ return TEXT("SCARD_E_NO_KEY_CONTAINER: The requested key container does not exist on the smart card.");
+ case SCARD_E_SERVER_TOO_BUSY:
+ return TEXT("SCARD_E_SERVER_TOO_BUSY: The Smart card resource manager is too busy to complete this operation.");
+ case SCARD_W_UNSUPPORTED_CARD:
+ return TEXT("SCARD_W_UNSUPPORTED_CARD: The reader cannot communicate with the smart card, due to ATR configuration conflicts.");
+ case SCARD_W_UNRESPONSIVE_CARD:
+ return TEXT("SCARD_W_UNRESPONSIVE_CARD: The smart card is not responding to a reset.");
+ case SCARD_W_UNPOWERED_CARD:
+ return TEXT("SCARD_W_UNPOWERED_CARD: Power has been removed from the smart card, so that further communication is not possible.");
+ case SCARD_W_RESET_CARD:
+ return TEXT("SCARD_W_RESET_CARD: The smart card has been reset, so any shared state information is invalid.");
+ case SCARD_W_REMOVED_CARD:
+ return TEXT("SCARD_W_REMOVED_CARD: The smart card has been removed, so that further communication is not possible.");
+ case SCARD_W_SECURITY_VIOLATION:
+ return TEXT("SCARD_W_SECURITY_VIOLATION: Access was denied because of a security violation.");
+ case SCARD_W_WRONG_CHV:
+ return TEXT("SCARD_W_WRONG_CHV: The card cannot be accessed because the wrong PIN was presented.");
+ case SCARD_W_CHV_BLOCKED:
+ return TEXT("SCARD_W_CHV_BLOCKED: The card cannot be accessed because the maximum number of PIN entry attempts has been reached.");
+ case SCARD_W_EOF:
+ return TEXT("SCARD_W_EOF: The end of the smart card file has been reached.");
+ case SCARD_W_CANCELLED_BY_USER:
+ return TEXT("SCARD_W_CANCELLED_BY_USER: The action was cancelled by the user.");
+ case SCARD_W_CARD_NOT_AUTHENTICATED:
+ return TEXT("SCARD_W_CARD_NOT_AUTHENTICATED: No PIN was presented to the smart card.");
+ case SCARD_W_CACHE_ITEM_NOT_FOUND:
+ return TEXT("SCARD_W_CACHE_ITEM_NOT_FOUND: The requested item could not be found in the cache.");
+ case SCARD_W_CACHE_ITEM_STALE:
+ return TEXT("SCARD_W_CACHE_ITEM_STALE: The requested cache item is too old and was deleted from the cache.");
+ case SCARD_W_CACHE_ITEM_TOO_BIG:
+ return TEXT("SCARD_W_CACHE_ITEM_TOO_BIG: The new cache item exceeds the maximum per-item size defined for the cache.");
+ }
+ return NULL;
+}
+
+static DWORD GetSCardErrorMessage(LONG Code,LPTSTR pszMessage,DWORD MaxLength)
+{
+ LPCTSTR pszText = GetSCardErrorText(Code);
+ DWORD Length = 0;
+ if (pszText != NULL) {
+ Length = (DWORD)(::StrStr(pszText, TEXT(" ")) - pszText + 1);
+ if (Length > MaxLength)
+ Length = MaxLength;
+ ::lstrcpyn(pszMessage, pszText, Length + 1);
+ }
+ Length = ::FormatMessage(
+ FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL,
+ Code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
+ pszMessage + Length, MaxLength - Length, NULL);
+ if (Length == 0) {
+ if (pszText != NULL)
+ ::lstrcpyn(pszMessage, pszText, MaxLength);
+ else
+ pszMessage[0] = '\0';
+ }
+ return Length;
+}
+
+static bool CheckReaderList(LPCTSTR pReaderList, DWORD Length)
+{
+ if (Length < 2)
+ return false;
+ return pReaderList[Length - 2] == _T('\0')
+ && pReaderList[Length - 1] == _T('\0');
+}
+
+
+// サービスAPI用
+#pragma comment(lib, "advapi32.lib")
+
+// スマートカードサービスが有効か調べる
+enum SCardCheckResult {
+ SCARD_CHECK_ENABLED,
+ SCARD_CHECK_DISABLED,
+ SCARD_CHECK_ERR_SERVICE_NOT_FOUND,
+ SCARD_CHECK_ERR_OPEN_MANAGER,
+ SCARD_CHECK_ERR_OPEN_SERVICE,
+ SCARD_CHECK_ERR_QUERY_CONFIG
+};
+
+static SCardCheckResult CheckSmartCardService()
+{
+ SCardCheckResult Result;
+
+ SC_HANDLE hManager = ::OpenSCManager(NULL, NULL, GENERIC_READ);
+ if (hManager == NULL)
+ return SCARD_CHECK_ERR_OPEN_MANAGER;
+
+ TCHAR szName[256];
+ DWORD Length = _countof(szName);
+ if (!::GetServiceKeyName(hManager, TEXT("Smart Card"), szName, &Length))
+ ::lstrcpy(szName, TEXT("SCardSvr"));
+
+ SC_HANDLE hService = ::OpenService(hManager, szName, SERVICE_QUERY_CONFIG);
+ if (hService == NULL) {
+ if (::GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST)
+ Result = SCARD_CHECK_ERR_SERVICE_NOT_FOUND;
+ else
+ Result = SCARD_CHECK_ERR_OPEN_SERVICE;
+ ::CloseServiceHandle(hManager);
+ return Result;
+ }
+
+ Result = SCARD_CHECK_ERR_QUERY_CONFIG;
+ DWORD Size = 0;
+ ::QueryServiceConfig(hService, NULL, 0, &Size);
+ if (Size > 0) {
+ BYTE *pBuffer = new BYTE[Size];
+ QUERY_SERVICE_CONFIG *pConfig = (QUERY_SERVICE_CONFIG*)pBuffer;
+
+ if (::QueryServiceConfig(hService, pConfig, Size, &Size)) {
+ if (pConfig->dwStartType == SERVICE_DISABLED)
+ Result = SCARD_CHECK_DISABLED;
+ else
+ Result = SCARD_CHECK_ENABLED;
+ }
+ delete [] pBuffer;
+ }
+
+ ::CloseServiceHandle(hService);
+ ::CloseServiceHandle(hManager);
+
+ return Result;
+}
+
+
+CSCardReader::CSCardReader()
+ : m_hSCard(0)
+ , m_bIsEstablish(false)
+ , m_pReaderList(NULL)
+ , m_NumReaders(0)
+ , m_pszReaderName(NULL)
+{
+ TRACE(TEXT("SCardEstablishContext\n"));
+ if (::SCardEstablishContext(SCARD_SCOPE_USER, NULL, NULL, &m_SCardContext) == SCARD_S_SUCCESS) {
+ m_bIsEstablish = true;
+
+ // カードリーダを列挙する
+ DWORD dwBuffLength = SCARD_AUTOALLOCATE;
+
+ TRACE(TEXT("SCardListReaders\n"));
+ if (::SCardListReaders(m_SCardContext, NULL,
+ reinterpret_cast<LPTSTR>(&m_pReaderList), &dwBuffLength) == SCARD_S_SUCCESS) {
+ if (CheckReaderList(m_pReaderList, dwBuffLength)) {
+ LPCTSTR p = m_pReaderList;
+ while (*p) {
+ TRACE(TEXT(" Reader%d : %s\n"), m_NumReaders, p);
+ m_NumReaders++;
+ p += ::lstrlen(p) + 1;
+ }
+ } else {
+ ::SCardFreeMemory(m_SCardContext, m_pReaderList);
+ m_pReaderList = NULL;
+ }
+ }
+ }
+}
+
+
+CSCardReader::~CSCardReader()
+{
+ Close();
+
+ if (m_bIsEstablish) {
+ if (m_pReaderList)
+ ::SCardFreeMemory(m_SCardContext, m_pReaderList);
+
+ TRACE(TEXT("SCardReleaseContext\n"));
+ ::SCardReleaseContext(m_SCardContext);
+ }
+}
+
+
+bool CSCardReader::Open(LPCTSTR pszReader)
+{
+ if (!m_bIsEstablish) {
+ SetError(TEXT("コンテキストを確立できません。"),
+ TEXT("Smart Card サービスが有効であるか確認してください。"));
+ return false;
+ }
+
+ // 一旦クローズする
+ Close();
+
+ if (pszReader) {
+ // 指定されたカードリーダに対してオープンを試みる
+ TRACE(TEXT("Open card reader \"%s\"\n"), pszReader);
+
+ LONG Result;
+
+ SCARD_READERSTATE ReaderState;
+ ::ZeroMemory(&ReaderState, sizeof(ReaderState));
+ ReaderState.szReader = pszReader;
+
+ TRACE(TEXT("SCardGetStatusChange\n"));
+ Result = ::SCardGetStatusChange(m_SCardContext, 0, &ReaderState, 1);
+ if (Result != SCARD_S_SUCCESS) {
+ TCHAR szMessage[256];
+ GetSCardErrorMessage(Result, szMessage, _countof(szMessage));
+ SetError(TEXT("カードリーダの状態を取得できません。"), NULL, szMessage);
+ return false;
+ }
+
+ if (!(ReaderState.dwEventState & SCARD_STATE_PRESENT)) {
+ TCHAR szMessage[256];
+ StdUtil::snprintf(szMessage, _countof(szMessage),
+ TEXT("カードリーダが利用できません。(State %08x)"),
+ ReaderState.dwEventState);
+ SetError(szMessage);
+ return false;
+ }
+
+ DWORD dwActiveProtocol = SCARD_PROTOCOL_UNDEFINED;
+
+ TRACE(TEXT("SCardConnect\n"));
+ Result = ::SCardConnect(m_SCardContext, pszReader, SCARD_SHARE_SHARED, SCARD_PROTOCOL_T1, &m_hSCard, &dwActiveProtocol);
+ if (Result != SCARD_S_SUCCESS) {
+ TCHAR szMessage[256];
+ GetSCardErrorMessage(Result, szMessage, _countof(szMessage));
+ SetError(TEXT("カードリーダに接続できません。"), NULL, szMessage);
+ return false;
+ }
+
+ if (dwActiveProtocol != SCARD_PROTOCOL_T1) {
+ Close();
+ SetError(TEXT("アクティブプロトコルが不正です。"));
+ return false;
+ }
+
+ LPTSTR pszReaderName;
+ BYTE Atr[32];
+ DWORD dwReaderLen = SCARD_AUTOALLOCATE, dwState, dwProtocol, dwAtrLen = sizeof(Atr);
+ TRACE(TEXT("SCardStatus\n"));
+ Result = ::SCardStatus(m_hSCard, reinterpret_cast<LPTSTR>(&pszReaderName), &dwReaderLen, &dwState, &dwProtocol, Atr, &dwAtrLen);
+ if (Result != SCARD_S_SUCCESS) {
+ Close();
+ TCHAR szMessage[256];
+ GetSCardErrorMessage(Result, szMessage, _countof(szMessage));
+ SetError(TEXT("カードの状態を取得できません。"), NULL, szMessage);
+ return false;
+ }
+ TCHAR szAtr[sizeof(Atr) * 3 + 1];
+ for (DWORD i = 0; i < dwAtrLen; i++)
+ ::wsprintf(&szAtr[i * 2], TEXT("%02x "), Atr[i]);
+ TRACE(TEXT("\nName : %s\nState : %08x\nProtocol : %u\nATR size : %u\nATR : %s\n"),
+ pszReaderName, dwState, dwProtocol, dwAtrLen, dwAtrLen ? szAtr : TEXT("n/a"));
+ ::SCardFreeMemory(m_SCardContext, pszReaderName);
+
+ m_pszReaderName = StdUtil::strdup(pszReader);
+ } else {
+ // 全てのカードリーダに対してオープンを試みる
+ if (m_pReaderList == NULL) {
+ SetError(TEXT("カードリーダが見付かりません。"));
+ return false;
+ }
+
+ LPCTSTR p = m_pReaderList;
+ while (*p) {
+ if (Open(p))
+ return true;
+ p += ::lstrlen(p) + 1;
+ }
+ return false;
+ }
+
+ ClearError();
+
+ return true;
+}
+
+
+void CSCardReader::Close()
+{
+ if (m_hSCard) {
+ TRACE(TEXT("SCardDisconnect\n"));
+ ::SCardDisconnect(m_hSCard, SCARD_LEAVE_CARD);
+ m_hSCard = 0;
+ }
+
+ if (m_pszReaderName) {
+ delete [] m_pszReaderName;
+ m_pszReaderName = NULL;
+ }
+}
+
+
+LPCTSTR CSCardReader::GetReaderName() const
+{
+ return m_pszReaderName;
+}
+
+
+int CSCardReader::NumReaders() const
+{
+ return m_NumReaders;
+}
+
+
+LPCTSTR CSCardReader::EnumReader(int Index) const
+{
+ if (Index < 0 || Index >= m_NumReaders)
+ return NULL;
+ LPCTSTR p = m_pReaderList;
+ for (int i = 0; i < Index; i++)
+ p += ::lstrlen(p) + 1;
+ return p;
+}
+
+
+bool CSCardReader::IsAvailable()
+{
+ return m_bIsEstablish
+ && ::SCardIsValidContext(m_SCardContext) == SCARD_S_SUCCESS
+ && m_NumReaders > 0;
+}
+
+
+bool CSCardReader::IsReaderAvailable(LPCTSTR pszReader)
+{
+ if (!m_bIsEstablish || pszReader == NULL)
+ return false;
+
+ LONG Result;
+
+ SCARD_READERSTATE ReaderState;
+ ::ZeroMemory(&ReaderState, sizeof(ReaderState));
+ ReaderState.szReader = pszReader;
+
+ Result = ::SCardGetStatusChange(m_SCardContext, 0, &ReaderState, 1);
+ if (Result != SCARD_S_SUCCESS)
+ return false;
+
+ if (!(ReaderState.dwEventState & SCARD_STATE_PRESENT))
+ return false;
+
+ return true;
+}
+
+
+bool CSCardReader::CheckAvailability(bool *pbAvailable, LPTSTR pszMessage, int MaxLength)
+{
+ bool bAvailable = false;
+
+ switch (CheckSmartCardService()) {
+ case SCARD_CHECK_ENABLED:
+ if (pszMessage)
+ ::lstrcpyn(pszMessage, TEXT("Smart Card サービスは有効です。"), MaxLength);
+ bAvailable = true;
+ break;
+
+ case SCARD_CHECK_DISABLED:
+ if (pszMessage)
+ ::lstrcpyn(pszMessage, TEXT("Smart Card サービスが無効になっています。"), MaxLength);
+ break;
+
+ case SCARD_CHECK_ERR_SERVICE_NOT_FOUND:
+ if (pszMessage)
+ ::lstrcpyn(pszMessage, TEXT("Smart Card サービスがインストールされていません。"), MaxLength);
+ break;
+
+ default:
+ return false;
+ }
+
+ *pbAvailable = bAvailable;
+
+ return true;
+}
+
+
+bool CSCardReader::Transmit(const void *pSendData, DWORD SendSize, void *pRecvData, DWORD *pRecvSize)
+{
+ if (!m_hSCard) {
+ SetError(TEXT("カードリーダが開かれていません。"));
+ return false;
+ }
+
+ LONG Result = ::SCardTransmit(m_hSCard, SCARD_PCI_T1,
+ static_cast<LPCBYTE>(pSendData), SendSize, NULL, static_cast<LPBYTE>(pRecvData), pRecvSize);
+
+ if (Result != SCARD_S_SUCCESS) {
+ TCHAR szMessage[256];
+ GetSCardErrorMessage(Result,szMessage, _countof(szMessage));
+ SetError(TEXT("コマンド送信エラーです。"), NULL, szMessage);
+ return false;
+ }
+
+ ClearError();
+
+ return true;
+}
+
+
+
+
+#ifdef CARDREADER_SCARD_DYNAMIC_SUPPORT
+
+///////////////////////////////////////////////////////////////////////////////
+//
+// winscard.dll互換カードリーダー(動的リンク)
+//
+///////////////////////////////////////////////////////////////////////////////
+
+
+#ifdef UNICODE
+#define FUNC_NAME(symbol) symbol "W"
+#else
+#define FUNC_NAME(symbol) symbol "A"
+#endif
+
+template<typename T> bool GetLibraryFunc(HMODULE hLib, T &Func, const char *pSymbol)
+{
+ return (Func = reinterpret_cast<T>(::GetProcAddress(hLib, pSymbol))) != NULL;
+}
+
+
+CDynamicSCardReader::CDynamicSCardReader()
+ : m_hLib(NULL)
+ , m_hSCard(0)
+ , m_pReaderList(NULL)
+ , m_pszReaderName(NULL)
+
+ , m_pSCardReleaseContext(NULL)
+ , m_pSCardConnect(NULL)
+ , m_pSCardDisconnect(NULL)
+ , m_pSCardTransmit(NULL)
+{
+}
+
+
+CDynamicSCardReader::~CDynamicSCardReader()
+{
+ Unload();
+}
+
+
+bool CDynamicSCardReader::Load(LPCTSTR pszFileName)
+{
+ m_hLib = ::LoadLibrary(pszFileName);
+ if (m_hLib == NULL) {
+ SetError(TEXT("ライブラリをロードできません。"));
+ return false;
+ }
+
+ typedef LONG (WINAPI *SCardEstablishContextFunc)(DWORD, LPCVOID, LPCVOID, LPSCARDCONTEXT);
+ typedef LONG (WINAPI *SCardListReadersFunc)(SCARDCONTEXT, LPCTSTR, LPTSTR, LPDWORD);
+ SCardEstablishContextFunc pEstablishContext;
+ SCardListReadersFunc pListReaders;
+
+ if (!GetLibraryFunc(m_hLib, pEstablishContext, "SCardEstablishContext")
+ || !GetLibraryFunc(m_hLib, pListReaders, FUNC_NAME("SCardListReaders"))
+ || !GetLibraryFunc(m_hLib, m_pSCardReleaseContext, "SCardReleaseContext")
+ || !GetLibraryFunc(m_hLib, m_pSCardConnect, FUNC_NAME("SCardConnect"))
+ || !GetLibraryFunc(m_hLib, m_pSCardDisconnect, "SCardDisconnect")
+ || !GetLibraryFunc(m_hLib, m_pSCardTransmit, "SCardTransmit")) {
+ ::FreeLibrary(m_hLib);
+ m_hLib = NULL;
+ SetError(TEXT("関数のアドレスを取得できません。"));
+ return false;
+ }
+
+ if (pEstablishContext(SCARD_SCOPE_USER, NULL, NULL, &m_SCardContext) != SCARD_S_SUCCESS) {
+ ::FreeLibrary(m_hLib);
+ m_hLib = NULL;
+ SetError(TEXT("コンテキストを確立できません。"));
+ return false;
+ }
+
+ // カードリーダを列挙する
+ DWORD dwBuffLength = 0;
+
+ if (pListReaders(m_SCardContext, NULL, NULL, &dwBuffLength) == SCARD_S_SUCCESS) {
+ m_pReaderList = new TCHAR[dwBuffLength];
+ if (pListReaders(m_SCardContext, NULL, m_pReaderList, &dwBuffLength) == SCARD_S_SUCCESS) {
+ if (!CheckReaderList(m_pReaderList, dwBuffLength)) {
+ Unload();
+ SetError(TEXT("カードリーダのリストが不正です。"));
+ return false;
+ }
+ } else {
+ delete [] m_pReaderList;
+ m_pReaderList = NULL;
+ }
+ }
+
+ return true;
+}
+
+
+void CDynamicSCardReader::Unload()
+{
+ Close();
+
+ if (m_hLib) {
+ m_pSCardReleaseContext(m_SCardContext);
+
+ ::FreeLibrary(m_hLib);
+ m_hLib = NULL;
+ }
+
+ if (m_pReaderList) {
+ delete [] m_pReaderList;
+ m_pReaderList = NULL;
+ }
+}
+
+
+// "ファイル名|リーダ名" の形式で指定する
+bool CDynamicSCardReader::Open(LPCTSTR pszReader)
+{
+ if (pszReader == NULL) {
+ SetError(TEXT("ファイル名が指定されていません。"));
+ return false;
+ }
+
+ LPCTSTR pszReaderName = ::StrChr(pszReader, _T('|'));
+ if (pszReaderName != NULL) {
+ pszReaderName++;
+ if (!*pszReaderName)
+ pszReaderName = NULL;
+ }
+ TCHAR szFileName[MAX_PATH];
+ if (pszReaderName != NULL) {
+ size_t FileNameLength = pszReaderName - pszReader;
+ if (FileNameLength > _countof(szFileName)) {
+ SetError(TEXT("ファイル名が長過ぎます。"));
+ return false;
+ }
+ ::lstrcpyn(szFileName, pszReader, (int)FileNameLength);
+ } else {
+ if (::lstrlen(pszReader) >= _countof(szFileName)) {
+ SetError(TEXT("ファイル名が長過ぎます。"));
+ return false;
+ }
+ ::lstrcpy(szFileName, pszReader);
+ }
+ if (m_hLib == NULL) {
+ if (!Load(szFileName))
+ return false;
+ } else {
+ // 一旦クローズする
+ Close();
+ }
+
+ if (pszReaderName) {
+ // 指定されたカードリーダに対してオープンを試みる
+ LONG Result;
+ DWORD dwActiveProtocol = SCARD_PROTOCOL_UNDEFINED;
+
+ Result=m_pSCardConnect(m_SCardContext, pszReaderName, SCARD_SHARE_SHARED, SCARD_PROTOCOL_T1, &m_hSCard, &dwActiveProtocol);
+ if (Result != SCARD_S_SUCCESS) {
+ TCHAR szMessage[256];
+ GetSCardErrorMessage(Result, szMessage, _countof(szMessage));
+ SetError(TEXT("カードリーダに接続できません。"), NULL, szMessage);
+ return false;
+ }
+
+ if (dwActiveProtocol != SCARD_PROTOCOL_T1) {
+ Close();
+ SetError(TEXT("アクティブプロトコルが不正です。"));
+ return false;
+ }
+
+ m_pszReaderName = StdUtil::strdup(pszReader);
+ } else {
+ // 全てのカードリーダに対してオープンを試みる
+ if (m_pReaderList == NULL || m_pReaderList[0] == _T('\0')) {
+ SetError(TEXT("カードリーダが見付かりません。"));
+ return false;
+ }
+
+ LPCTSTR p = m_pReaderList;
+ while (*p) {
+ TCHAR szReader[MAX_PATH + 256];
+
+ StdUtil::snprintf(szReader, _countof(szReader), TEXT("%s|%s"), szFileName, p);
+ if (Open(szReader))
+ return true;
+ p += ::lstrlen(p) + 1;
+ }
+ return false;
+ }
+
+ ClearError();
+
+ return true;
+}
+
+
+void CDynamicSCardReader::Close()
+{
+ if (m_hSCard) {
+ m_pSCardDisconnect(m_hSCard, SCARD_LEAVE_CARD);
+ m_hSCard = 0;
+ }
+
+ if (m_pszReaderName) {
+ delete [] m_pszReaderName;
+ m_pszReaderName = NULL;
+ }
+}
+
+
+LPCTSTR CDynamicSCardReader::GetReaderName() const
+{
+ return m_pszReaderName;
+}
+
+
+int CDynamicSCardReader::NumReaders() const
+{
+ if (!m_pReaderList)
+ return 0;
+ LPCTSTR p = m_pReaderList;
+ int i;
+ for (i = 0; *p; i++) {
+ p += ::lstrlen(p) + 1;
+ }
+ return i;
+}
+
+
+LPCTSTR CDynamicSCardReader::EnumReader(int Index) const
+{
+ if (!m_pReaderList)
+ return NULL;
+ LPCTSTR p = m_pReaderList;
+ for (int i = 0; i < Index; i++) {
+ if (!*p)
+ return NULL;
+ p += ::lstrlen(p) + 1;
+ }
+ return p;
+}
+
+
+bool CDynamicSCardReader::IsAvailable()
+{
+ return true;
+}
+
+
+bool CDynamicSCardReader::IsReaderAvailable(LPCTSTR pszReader)
+{
+ return true;
+}
+
+
+bool CDynamicSCardReader::CheckAvailability(bool *pbAvailable, LPTSTR pszMessage, int MaxLength)
+{
+ return false;
+}
+
+
+bool CDynamicSCardReader::Transmit(const void *pSendData,DWORD SendSize,void *pRecvData,DWORD *pRecvSize)
+{
+ if (!m_hSCard) {
+ SetError(TEXT("カードリーダが開かれていません。"));
+ return false;
+ }
+
+ LONG Result = m_pSCardTransmit(m_hSCard, SCARD_PCI_T1,
+ static_cast<LPCBYTE>(pSendData), SendSize, NULL, static_cast<LPBYTE>(pRecvData), pRecvSize);
+
+ if (Result != SCARD_S_SUCCESS) {
+ TCHAR szMessage[256];
+ GetSCardErrorMessage(Result, szMessage, _countof(szMessage));
+ SetError(TEXT("コマンド送信エラーです。"), NULL, szMessage);
+ return false;
+ }
+
+ ClearError();
+
+ return true;
+}
+
+#endif // CARDREADER_SCARD_DYNAMIC_SUPPORT
+
+
+
+
+#ifdef CARDREADER_BONCASCLIENT_SUPPORT
+
+///////////////////////////////////////////////////////////////////////////////
+//
+// BonCasClient
+//
+///////////////////////////////////////////////////////////////////////////////
+
+#define BONCASCLIENT_MODULE_NAME TEXT("BonCasClient.dll")
+
+
+CBonCasClientCardReader::CBonCasClientCardReader()
+ : m_hLib(NULL)
+ , m_hSCard(0)
+ , m_pReaderList(NULL)
+ , m_pszReaderName(NULL)
+
+ , m_pCasLinkReleaseContext(NULL)
+ , m_pCasLinkConnect(NULL)
+ , m_pCasLinkDisconnect(NULL)
+ , m_pCasLinkTransmit(NULL)
+{
+}
+
+
+CBonCasClientCardReader::~CBonCasClientCardReader()
+{
+ Close();
+}
+
+
+void CBonCasClientCardReader::GetModulePath(LPTSTR pszPath) const
+{
+ ::GetModuleFileName(NULL, pszPath, MAX_PATH);
+ ::lstrcpy(::PathFindFileName(pszPath), BONCASCLIENT_MODULE_NAME);
+}
+
+
+bool CBonCasClientCardReader::Load(LPCTSTR pszFileName)
+{
+ m_hLib = ::LoadLibrary(pszFileName);
+ if (m_hLib == NULL) {