1 |
/* w32-clip.c - W32 API clipboard functions |
2 |
* Copyright (C) 2001, 2002, 2003 Timo Schulz |
3 |
* |
4 |
* This file is part of MyGPGME. |
5 |
* |
6 |
* MyGPGME is free software; you can redistribute it and/or modify |
7 |
* it under the terms of the GNU General Public License as published by |
8 |
* the Free Software Foundation; either version 2 of the License, or |
9 |
* (at your option) any later version. |
10 |
* |
11 |
* MyGPGME is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with this program; if not, write to the Free Software |
18 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
19 |
*/ |
20 |
|
21 |
#include <windows.h> |
22 |
|
23 |
#include "util.h" |
24 |
#include "ops.h" |
25 |
#include "context.h" |
26 |
|
27 |
|
28 |
static char* |
29 |
get_w32_clip_text( int *r_size ) |
30 |
{ |
31 |
int rc, size; |
32 |
char *private_clip = NULL; |
33 |
char *clip = NULL; |
34 |
HANDLE cb; |
35 |
|
36 |
rc = OpenClipboard( NULL ); |
37 |
if( !rc ) |
38 |
return NULL; |
39 |
cb = GetClipboardData( CF_TEXT ); |
40 |
if( !cb ) |
41 |
goto leave; |
42 |
private_clip = GlobalLock( cb ); |
43 |
if( !private_clip ) |
44 |
goto leave; |
45 |
size = strlen( private_clip ); |
46 |
clip = malloc( size + 1 ); |
47 |
if( !clip ) { |
48 |
GlobalUnlock( cb ); |
49 |
goto leave; |
50 |
} |
51 |
memcpy( clip, private_clip, size ); |
52 |
clip[size] = '\0'; |
53 |
*r_size = size; |
54 |
GlobalUnlock( cb ); |
55 |
|
56 |
leave: |
57 |
CloseClipboard(); |
58 |
return clip; |
59 |
} /* get_w32_clip_text */ |
60 |
|
61 |
|
62 |
static void |
63 |
set_w32_clip_text( const char *data, int size ) |
64 |
{ |
65 |
int rc; |
66 |
HANDLE cb; |
67 |
char *private_data; |
68 |
|
69 |
rc = OpenClipboard( NULL ); |
70 |
if( !rc ) |
71 |
return; |
72 |
EmptyClipboard(); |
73 |
cb = GlobalAlloc( GHND, size+1 ); |
74 |
if( !cb ) |
75 |
goto leave; |
76 |
private_data = GlobalLock( cb ); |
77 |
if( !private_data ) |
78 |
goto leave; |
79 |
memcpy( private_data, data, size ); |
80 |
private_data[size] = '\0'; |
81 |
SetClipboardData( CF_TEXT, cb ); |
82 |
GlobalUnlock( cb ); |
83 |
|
84 |
leave: |
85 |
CloseClipboard( ); |
86 |
GlobalFree( cb ); |
87 |
} /* set_w32_clip_text */ |
88 |
|
89 |
|
90 |
gpgme_error_t |
91 |
gpgme_data_new_from_clipboard (gpgme_data_t *r_dh) |
92 |
{ |
93 |
gpgme_error_t err = 0; |
94 |
gpgme_data_t dh; |
95 |
char *clip_text; |
96 |
int size; |
97 |
|
98 |
if (!r_dh) |
99 |
return mk_error (Invalid_Value); |
100 |
*r_dh = NULL; |
101 |
clip_text = get_w32_clip_text (&size); |
102 |
if (!clip_text) |
103 |
return mk_error (No_Data); |
104 |
err = gpgme_data_new_from_mem (&dh, clip_text, size, 1); |
105 |
safe_free (clip_text); |
106 |
*r_dh = dh; |
107 |
return err; |
108 |
} /* gpgme_data_new_from_clipboard */ |
109 |
|
110 |
|
111 |
void |
112 |
gpgme_data_release_and_set_clipboard (gpgme_data_t dh) |
113 |
{ |
114 |
char *clip_text; |
115 |
int size = 0; |
116 |
|
117 |
if (!dh) |
118 |
return; |
119 |
clip_text = _gpgme_data_get_as_string (dh); |
120 |
if (clip_text && (size = strlen (clip_text)) > 0) { |
121 |
set_w32_clip_text (clip_text, size); |
122 |
memset (clip_text, 0xFF, size); |
123 |
safe_free (clip_text); |
124 |
} |
125 |
gpgme_data_release (dh); |
126 |
} /* gpgme_data_release_and_set_clipboard */ |
127 |
|
128 |
|
129 |
gpgme_error_t |
130 |
gpgme_data_fix_clipboard( gpgme_data_t dh ) |
131 |
{ |
132 |
const char *packets[] = { |
133 |
"-----BEGIN PGP MESSAGE-----" |
134 |
"-----BEGIN PGP PRIVATE KEY-----", |
135 |
"-----BEGIN PGP PUBLIC KEY-----", |
136 |
"-----BEGIN PGP SIGNED MESSAGE-----" |
137 |
}; |
138 |
const char *headers[] = { |
139 |
"Comment" |
140 |
"Hash", |
141 |
"Version" |
142 |
}; |
143 |
char *p; |
144 |
int idx; |
145 |
|
146 |
p = _gpgme_data_get_as_string( dh ); |
147 |
if( p == NULL ) |
148 |
return mk_error( Invalid_Value ); |
149 |
for( idx = 0; idx < 4; idx++ ) { |
150 |
if( strstr( p, packets[idx] ) ) |
151 |
break; |
152 |
} |
153 |
safe_free( p ); |
154 |
|
155 |
return 1; |
156 |
} /* gpgme_data_fix_clipboard */ |