Develop and Download Open Source Software

Browse Subversion Repository

Annotation of /trunk/teraterm/teraterm/clipboar.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 8738 - (hide annotations) (download) (as text)
Sat Apr 25 15:58:53 2020 UTC (3 years, 11 months ago) by zmatsuo
File MIME type: text/x-csrc
File size: 14752 byte(s)
ペーストの確認ダイアログでcancelしたときメモリリークが発生していた
1 doda 6806 /*
2     * Copyright (C) 1994-1998 T. Teranishi
3 zmatsuo 8459 * (C) 2006-2020 TeraTerm Project
4 doda 6806 * All rights reserved.
5     *
6 doda 6841 * Redistribution and use in source and binary forms, with or without
7     * modification, are permitted provided that the following conditions
8     * are met:
9 doda 6806 *
10 doda 6841 * 1. Redistributions of source code must retain the above copyright
11     * notice, this list of conditions and the following disclaimer.
12     * 2. Redistributions in binary form must reproduce the above copyright
13     * notice, this list of conditions and the following disclaimer in the
14     * documentation and/or other materials provided with the distribution.
15     * 3. The name of the author may not be used to endorse or promote products
16     * derived from this software without specific prior written permission.
17 doda 6806 *
18 doda 6841 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
19     * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20     * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21     * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22     * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23     * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24     * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25     * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26     * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27     * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 doda 6806 */
29 maya 3227
30     /* TERATERM.EXE, Clipboard routines */
31     #include "teraterm.h"
32     #include "tttypes.h"
33     #include "vtdisp.h"
34 doda 7140 #include "vtterm.h"
35 maya 3227 #include <string.h>
36     #include <stdlib.h>
37     #include <stdio.h>
38 yutakapon 3635 #include <commctrl.h>
39 doda 8445 #define _CRTDBG_MAP_ALLOC
40     #include <crtdbg.h>
41     #include <wchar.h>
42 maya 3227
43     #include "ttwinman.h"
44     #include "ttcommon.h"
45 doda 4769 #include "ttlib.h"
46 zmatsuo 7509 #include "dlglib.h"
47 doda 8445 #include "codeconv.h"
48 maya 3227
49     #include "clipboar.h"
50     #include "tt_res.h"
51 zmatsuo 8586 #include "fileread.h"
52 doda 8445 #include "sendmem.h"
53     #include "clipboarddlg.h"
54 maya 3227
55     // for clipboard paste
56     static HGLOBAL CBMemHandle = NULL;
57     static PCHAR CBMemPtr = NULL;
58     static LONG CBMemPtr2 = 0;
59     static BYTE CBByte;
60     static BOOL CBRetrySend;
61     static BOOL CBRetryEcho;
62     static BOOL CBSendCR;
63 doda 3974 static BOOL CBEchoOnly;
64 doda 5259 static BOOL CBInsertDelay = FALSE;
65 maya 3227
66 doda 8445 static const wchar_t BracketStartW[] = L"\033[200~";
67     static const wchar_t BracketEndW[] = L"\033[201~";
68 doda 6456
69 doda 8445 static void CBEcho(void);
70    
71     /**
72     * ���������M
73     * ���������������g�p����������
74     * - DDE(TTL)
75     * - �u���[�h�L���X�g
76     */
77 doda 6440 void CBStartSend(PCHAR DataPtr, int DataSize, BOOL EchoOnly)
78 maya 3227 {
79 doda 6440 if (! cv.Ready) {
80     return;
81     }
82     if (TalkStatus!=IdTalkKeyb) {
83     return;
84     }
85    
86     CBEchoOnly = EchoOnly;
87    
88 doda 6452 if (CBMemHandle) {
89     GlobalFree(CBMemHandle);
90     }
91 doda 6440 CBMemHandle = NULL;
92     CBMemPtr = NULL;
93     CBMemPtr2 = 0;
94    
95     CBInsertDelay = FALSE;
96    
97     CBRetrySend = FALSE;
98     CBRetryEcho = FALSE;
99     CBSendCR = FALSE;
100    
101 doda 6535 if ((CBMemHandle = GlobalAlloc(GHND, DataSize+1)) != NULL) {
102 doda 6440 if ((CBMemPtr = GlobalLock(CBMemHandle)) != NULL) {
103     memcpy(CBMemPtr, DataPtr, DataSize);
104 doda 6535 // WM_COPYDATA ���������������f�[�^�� NUL Terminate ���������������� NUL ���t������
105     CBMemPtr[DataSize] = 0;
106 doda 6440 GlobalUnlock(CBMemHandle);
107     CBMemPtr=NULL;
108     TalkStatus=IdTalkCB;
109     }
110     }
111     if (TalkStatus != IdTalkCB) {
112     CBEndPaste();
113     }
114     }
115    
116 doda 8445 static void TrimTrailingNLW(wchar_t *src)
117     {
118     wchar_t *tail = src + wcslen(src) - 1;
119     while(tail >= src) {
120     if (*tail != L'\r' && *tail != L'\n') {
121     break;
122     }
123     *tail = L'\0';
124     tail--;
125     }
126     }
127    
128 zmatsuo 8737 /**
129     * ���s�R�[�h�� CR+LF ����������
130     * @return ����������������
131     */
132 doda 8445 static wchar_t *NormalizeLineBreakW(const wchar_t *src_)
133     {
134     const wchar_t *src = src_;
135     wchar_t *dest_top;
136     wchar_t *dest;
137     size_t len, need_len, alloc_len;
138    
139     // �\���t���f�[�^������(len)�A���������K�������f�[�^������(need_len)���J�E���g
140     for (len=0, need_len=0, src=src_; *src != '\0'; src++, len++, need_len++) {
141     if (*src == CR) {
142     need_len++;
143     if (*(src+1) == LF) {
144     len++;
145     src++;
146     }
147     }
148     else if (*src == LF) {
149     need_len++;
150     }
151     }
152    
153     // ���K�������f�[�^�������������� => ���K�����K�v����
154     if (need_len == len) {
155     dest = _wcsdup(src_);
156     return dest;
157     }
158     alloc_len = need_len + 1;
159    
160     dest_top = (wchar_t *)malloc(sizeof(wchar_t) * alloc_len);
161    
162     src = src_ + len - 1;
163     dest = dest_top + need_len;
164     *dest-- = '\0';
165    
166     while (len > 0 && dest_top <= dest) {
167     if (*src == LF) {
168     *dest-- = *src--;
169     if (--len == 0) {
170     *dest = CR;
171     break;
172     }
173     if (*src != CR) {
174     *dest-- = CR;
175 zmatsuo 8737 continue;
176 doda 8445 }
177     }
178     else if (*src == CR) {
179     *dest-- = LF;
180     if (src == dest)
181     break;
182     }
183     *dest-- = *src--;
184     len--;
185     }
186    
187     return dest_top;
188     }
189    
190     /**
191     * �t�@�C�������`���������������Atext���������������������B
192     * ���������� TRUE������
193     */
194     static BOOL search_dictW(char *filename, const wchar_t *text)
195     {
196     BOOL result = FALSE;
197     const wchar_t *buf_top = LoadFileWA(filename, NULL);
198     const wchar_t *buf = buf_top;
199     if (buf == NULL) {
200     return FALSE;
201     }
202    
203     for(;;) {
204     const wchar_t *line_end;
205     size_t len;
206     wchar_t *search_str;
207     if (*buf == 0) {
208     break;
209     }
210     if (*buf == '\r' || *buf == '\n') {
211     buf++;
212     continue;
213     }
214     line_end = wcspbrk(buf, L"\r\n");
215     if (line_end == NULL) {
216     // ���s������
217     len = wcslen(buf);
218     if (len == 0) {
219     // �I��
220     break;
221     }
222     } else {
223     len = line_end - buf;
224     }
225     search_str = (wchar_t *)malloc(sizeof(wchar_t) * (len+1));
226     if (search_str == NULL)
227     continue;
228     memcpy(search_str, buf, sizeof(wchar_t) * len);
229     search_str[len] = 0;
230     buf += len;
231     result = (wcsstr(text, search_str) != NULL);
232     free(search_str);
233     if (result) {
234     result = TRUE;
235     break;
236     }
237     }
238     free((void *)buf_top);
239     return result;
240     }
241    
242 doda 6456 /*
243     * �N���b�v�{�[�h�����e���m�F���A�\���t�����s�����m�F�_�C�A���O���o���B
244     *
245     * �����l:
246     * TRUE -> ���������A�\���t�������{
247     * FALSE -> �\���t�����~
248     */
249 zmatsuo 8736 static BOOL CheckClipboardContentW(HWND HWin, const wchar_t *str_w, BOOL AddCR, wchar_t **out_str_w)
250 doda 6456 {
251 zmatsuo 8736 INT_PTR ret;
252 doda 6456 BOOL confirm = FALSE;
253    
254 zmatsuo 8736 *out_str_w = NULL;
255 doda 6594 if ((ts.PasteFlag & CPF_CONFIRM_CHANGEPASTE) == 0) {
256 doda 6456 return TRUE;
257     }
258    
259     /*
260     * ConfirmChangePasteCR ����������
261     * �����������������������B
262     *
263     * ChangePasteCR !ChangePasteCR
264     * AddCR !AddCR AddCR !AddCR
265     * ���s���� o o x(2) o
266     * ���s���� o(1) x x x
267     *
268     * ChangePasteCR �� AddCR �������m�F���s����(1���m�F����)���������������A
269     * !ChangePasteCR �������l�������AAddCR ���������� CR ����������������
270     * �m�F���s������������ 2 �����������m�F���s�p���������v�\�������������B
271     * 2 ����������������������?
272     */
273     if (AddCR) {
274 doda 6594 if (ts.PasteFlag & CPF_CONFIRM_CHANGEPASTE_CR) {
275 doda 6456 confirm = TRUE;
276     }
277     }
278     else {
279 doda 8445 size_t pos = wcscspn(str_w, L"\r\n"); // ���s����������������
280     if (str_w[pos] != '\0') {
281     confirm = TRUE;
282     }
283     }
284    
285     // �������T�[�`����
286     if (!confirm && search_dictW(ts.ConfirmChangePasteStringFile, str_w)) {
287     confirm = TRUE;
288     }
289    
290     ret = IDOK;
291     if (confirm) {
292     clipboarddlgdata dlg_data;
293     dlg_data.strW_ptr = str_w;
294     dlg_data.UILanguageFile = ts.UILanguageFile;
295     ret = clipboarddlg(hInst, HWin, &dlg_data);
296     *out_str_w = dlg_data.strW_edited_ptr;
297     }
298    
299     if (ret == IDOK) {
300     return TRUE;
301     }
302     else {
303     return FALSE;
304     }
305     }
306    
307     /**
308     * �N���b�v�{�[�h�p�e�L�X�g���M����
309     *
310     * @param str_w �����������|�C���^
311     * malloc()�������o�b�t�@�A���M��������������free()������
312     * @param str_len ������(wchar_t�P��)
313 zmatsuo 8456 * 0 �������� L'\0' ����
314 doda 8445 */
315     static void CBSendStart(wchar_t *str_w, size_t str_len)
316     {
317     SendMem *sm;
318     if (str_len == 0) {
319     str_len = wcslen(str_w);
320     }
321 zmatsuo 8456 sm = SendMemTextW(str_w, str_len);
322 doda 8445 if (sm == NULL)
323     return;
324 zmatsuo 8588 if (ts.PasteDelayPerLine == 0) {
325     SendMemInitDelay(sm, SENDMEM_DELAYTYPE_NO_DELAY, 0, 0);
326 doda 8445 }
327 zmatsuo 8588 else {
328     SendMemInitDelay(sm, SENDMEM_DELAYTYPE_PER_LINE, ts.PasteDelayPerLine, 0);
329     }
330 doda 8445 #if 0
331     SendMemInitDialog(sm, hInst, HVTWin, ts.UILanguageFile);
332     SendMemInitDialogCaption(sm, L"from clipboard");
333     SendMemInitDialogFilename(sm, L"Clipboard");
334     #endif
335     SendMemStart(sm);
336     }
337    
338     void CBStartPaste(HWND HWin, BOOL AddCR, BOOL Bracketed)
339     {
340     // unsigned int StrLen = 0;
341     wchar_t *str_w;
342     wchar_t *str_w_edited;
343    
344     if (! cv.Ready) {
345     return;
346     }
347     if (TalkStatus!=IdTalkKeyb) {
348     return;
349     }
350    
351     CBEchoOnly = FALSE;
352    
353     str_w = GetClipboardTextW(HWin, FALSE);
354     if (str_w == NULL) {
355     // �N���b�v�{�[�h����������������������������
356     CBEndPaste();
357     return;
358     }
359    
360     if (ts.PasteFlag & CPF_TRIM_TRAILING_NL) {
361     // �o�b�t�@���������s������
362     TrimTrailingNLW(str_w);
363     }
364    
365 zmatsuo 8459 {
366 doda 8445 // ���s�����K��
367     wchar_t *dest = NormalizeLineBreakW(str_w);
368     free(str_w);
369     str_w = dest;
370     }
371    
372     if (!CheckClipboardContentW(HWin, str_w, AddCR, &str_w_edited)) {
373 zmatsuo 8738 free(str_w);
374 doda 8445 CBEndPaste();
375     return;
376     }
377     if (str_w_edited != NULL) {
378     // �_�C�A���O�����W������
379     free(str_w);
380     str_w = str_w_edited;
381     }
382    
383     if (AddCR) {
384     size_t str_len = wcslen(str_w) + 2;
385     str_w = realloc(str_w, sizeof(wchar_t) * str_len);
386     str_w[str_len-2] = L'\r';
387     str_w[str_len-1] = 0;
388     }
389    
390     if (Bracketed) {
391     const size_t BracketStartLenW = _countof(BracketStartW) - 1;
392     const size_t BracketEndLenW = _countof(BracketEndW) - 1;
393     size_t str_len = wcslen(str_w);
394     size_t dest_len = str_len + BracketStartLenW + BracketEndLenW;
395     wchar_t *dest = malloc(sizeof(wchar_t) * (dest_len+1));
396     size_t i = 0;
397     wmemcpy(&dest[i], BracketStartW, BracketStartLenW);
398     i += BracketStartLenW;
399     wmemcpy(&dest[i], str_w, str_len);
400     i += str_len;
401     wmemcpy(&dest[i], BracketEndW, BracketEndLenW);
402     i += BracketEndLenW;
403     dest[i] = 0;
404     free(str_w);
405     str_w = dest;
406     }
407    
408     CBSendStart(str_w, 0);
409     }
410    
411 doda 4769 void CBStartPasteB64(HWND HWin, PCHAR header, PCHAR footer)
412     {
413 doda 8445 size_t mb_len, b64_len, header_len = 0, footer_len = 0;
414     wchar_t *str_w = NULL;
415     char *str_mb = NULL;
416     char *str_b64 = NULL;
417    
418     if (! cv.Ready) {
419     return;
420     }
421     if (TalkStatus!=IdTalkKeyb) {
422     return;
423     }
424    
425     CBEchoOnly = FALSE;
426    
427     str_w = GetClipboardTextW(HWin, FALSE);
428     if (str_w == NULL) {
429     // �N���b�v�{�[�h����������������������������
430     goto error;
431     }
432    
433     if (ts.Language == IdUtf8 || ts.KanjiCodeSend == IdUTF8) {
434     str_mb = ToU8W(str_w);
435     }
436     else {
437     str_mb = ToCharW(str_w);
438     }
439    
440     if (str_mb == NULL) {
441     goto error;
442     }
443    
444     if (header != NULL) {
445     header_len = strlen(header);
446     }
447     if (footer != NULL) {
448     footer_len = strlen(footer);
449     }
450    
451     mb_len = strlen(str_mb);
452     b64_len = (mb_len + 2) / 3 * 4 + header_len + footer_len + 1;
453    
454     if ((str_b64 = malloc(b64_len)) == NULL) {;
455     goto error;
456     }
457    
458     if (header_len > 0) {
459     strncpy_s(str_b64, b64_len, header, _TRUNCATE);
460     }
461    
462     b64encode(str_b64 + header_len, b64_len - header_len, str_mb, mb_len);
463    
464     if (footer_len > 0) {
465     strncat_s(str_b64, b64_len, footer, _TRUNCATE);
466     }
467    
468     free(str_w);
469     if ((str_w = ToWcharA(str_b64)) == NULL) {
470     goto error;
471     }
472    
473     free(str_mb);
474     free(str_b64);
475    
476     // �\���t�����������������o����
477     CBSendStart(str_w, 0);
478    
479     return;
480    
481     error:
482     free(str_w);
483     free(str_mb);
484     free(str_b64);
485     CBEndPaste();
486     return;
487     }
488    
489 maya 3227 // �����������N���b�v�{�[�h������DDE�f�[�^���[�������������B
490     //
491     // CBMemHandle�n���h�����O���[�o�������������A�����������I�������������A
492     // �����N���b�v�{�[�h������DDE�f�[�^�������������������������i�j�����������\�������j�B
493     // �����A�f�[�^���� null-terminate �����������������O�����������������A�������f�[�^����
494     // �����������B
495     // (2006.11.6 yutaka)
496     void CBSend()
497     {
498 maya 3393 int c;
499     BOOL EndFlag;
500 doda 3688 static DWORD lastcr;
501     DWORD now;
502 maya 3227
503 maya 3393 if (CBMemHandle==NULL) {
504     return;
505     }
506 maya 3227
507 doda 3974 if (CBEchoOnly) {
508     CBEcho();
509     return;
510     }
511    
512 doda 5259 if (CBInsertDelay) {
513     now = GetTickCount();
514     if (now - lastcr < (DWORD)ts.PasteDelayPerLine) {
515     return;
516     }
517 doda 3688 }
518    
519 maya 3393 if (CBRetrySend) {
520     CBRetryEcho = (ts.LocalEcho>0);
521     c = CommTextOut(&cv,(PCHAR)&CBByte,1);
522     CBRetrySend = (c==0);
523     if (CBRetrySend) {
524     return;
525     }
526     }
527 maya 3227
528 maya 3393 if (CBRetryEcho) {
529     c = CommTextEcho(&cv,(PCHAR)&CBByte,1);
530     CBRetryEcho = (c==0);
531     if (CBRetryEcho) {
532     return;
533     }
534     }
535 maya 3227
536 maya 4009 CBMemPtr = GlobalLock(CBMemHandle);
537     if (CBMemPtr==NULL) {
538 maya 3393 return;
539     }
540 maya 3227
541 maya 3393 do {
542     if (CBSendCR && (CBMemPtr[CBMemPtr2]==0x0a)) {
543     CBMemPtr2++;
544     // added PasteDelayPerLine (2009.4.12 maya)
545 doda 5259 if (CBInsertDelay) {
546 doda 3690 lastcr = now;
547     CBSendCR = FALSE;
548     SetTimer(HVTWin, IdPasteDelayTimer, ts.PasteDelayPerLine, NULL);
549     break;
550     }
551 maya 3393 }
552    
553     EndFlag = (CBMemPtr[CBMemPtr2]==0);
554 doda 6449 if (! EndFlag) {
555 maya 3393 CBByte = CBMemPtr[CBMemPtr2];
556     CBMemPtr2++;
557 maya 3227 // Decoding characters which are encoded by MACRO
558     // to support NUL character sending
559     //
560     // [encoded character] --> [decoded character]
561     // 01 01 --> 00
562     // 01 02 --> 01
563 maya 3393 if (CBByte==0x01) { /* 0x01 from MACRO */
564     CBByte = CBMemPtr[CBMemPtr2];
565     CBMemPtr2++;
566     CBByte = CBByte - 1; // character just after 0x01
567     }
568     }
569     else {
570     CBEndPaste();
571     return;
572     }
573 maya 3227
574 maya 3393 if (! EndFlag) {
575     c = CommTextOut(&cv,(PCHAR)&CBByte,1);
576     CBSendCR = (CBByte==0x0D);
577     CBRetrySend = (c==0);
578     if ((! CBRetrySend) &&
579     (ts.LocalEcho>0)) {
580     c = CommTextEcho(&cv,(PCHAR)&CBByte,1);
581     CBRetryEcho = (c==0);
582     }
583     }
584     else {
585     c=0;
586     }
587     }
588     while (c>0);
589 maya 3227
590 maya 4009 if (CBMemPtr!=NULL) {
591 maya 3393 GlobalUnlock(CBMemHandle);
592     CBMemPtr=NULL;
593     }
594 maya 3227 }
595    
596 doda 8445 static void CBEcho()
597 doda 3974 {
598     if (CBMemHandle==NULL) {
599     return;
600     }
601    
602     if (CBRetryEcho && CommTextEcho(&cv,(PCHAR)&CBByte,1) == 0) {
603     return;
604     }
605    
606     if ((CBMemPtr = GlobalLock(CBMemHandle)) == NULL) {
607     return;
608     }
609    
610     do {
611     if (CBSendCR && (CBMemPtr[CBMemPtr2]==0x0a)) {
612     CBMemPtr2++;
613     }
614    
615     if (CBMemPtr[CBMemPtr2] == 0) {
616     CBRetryEcho = FALSE;
617     CBEndPaste();
618     return;
619     }
620    
621     CBByte = CBMemPtr[CBMemPtr2];
622     CBMemPtr2++;
623    
624     // Decoding characters which are encoded by MACRO
625     // to support NUL character sending
626     //
627     // [encoded character] --> [decoded character]
628     // 01 01 --> 00
629     // 01 02 --> 01
630     if (CBByte==0x01) { /* 0x01 from MACRO */
631     CBByte = CBMemPtr[CBMemPtr2];
632     CBMemPtr2++;
633     CBByte = CBByte - 1; // character just after 0x01
634     }
635    
636     CBSendCR = (CBByte==0x0D);
637    
638     } while (CommTextEcho(&cv,(PCHAR)&CBByte,1) > 0);
639    
640     CBRetryEcho = TRUE;
641    
642     if (CBMemHandle != NULL) {
643     GlobalUnlock(CBMemHandle);
644     CBMemPtr=NULL;
645     }
646     }
647    
648 maya 3227 void CBEndPaste()
649     {
650 maya 3393 TalkStatus = IdTalkKeyb;
651 maya 3227
652 maya 3393 if (CBMemHandle!=NULL) {
653     if (CBMemPtr!=NULL) {
654     GlobalUnlock(CBMemHandle);
655     }
656 doda 6449 GlobalFree(CBMemHandle);
657 maya 3393 }
658 maya 3227
659 maya 3393 CBMemHandle = NULL;
660     CBMemPtr = NULL;
661     CBMemPtr2 = 0;
662 doda 3974 CBEchoOnly = FALSE;
663 doda 5259 CBInsertDelay = FALSE;
664 doda 8445 _CrtCheckMemory();
665 maya 3227 }
666    

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26