1 |
twoaday |
24 |
/* wptFileCBS.cpp |
2 |
|
|
* Copyright (C) 2001-2005 Timo Schulz |
3 |
|
|
* Copyright (C) 2005 g10 Code GmbH |
4 |
|
|
* |
5 |
|
|
* This file is part of WinPT. |
6 |
|
|
* |
7 |
|
|
* WinPT is free software; you can redistribute it and/or |
8 |
|
|
* modify it under the terms of the GNU General Public License |
9 |
|
|
* as published by the Free Software Foundation; either version 2 |
10 |
|
|
* of the License, or (at your option) any later version. |
11 |
|
|
* |
12 |
|
|
* WinPT is distributed in the hope that it will be useful, |
13 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 |
|
|
* General Public License for more details. |
16 |
|
|
* |
17 |
|
|
* You should have received a copy of the GNU General Public License |
18 |
|
|
* along with WinPT; if not, write to the Free Software Foundation, |
19 |
|
|
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
20 |
|
|
*/ |
21 |
|
|
#include <windows.h> |
22 |
|
|
#include <stdio.h> |
23 |
|
|
#include <malloc.h> |
24 |
|
|
#include <errno.h> |
25 |
|
|
#include <stdlib.h> |
26 |
|
|
|
27 |
|
|
#include "wptListView.h" |
28 |
|
|
#include "wptGPG.h" |
29 |
|
|
#include "w32gpgme.h" |
30 |
|
|
#include "wptFileManager.h" |
31 |
|
|
|
32 |
|
|
/* Predefined read callback. */ |
33 |
|
|
static long |
34 |
|
|
read_cb (void *handle, void *buffer, size_t size) |
35 |
|
|
{ |
36 |
|
|
file_data_t cb = (file_data_t)handle; |
37 |
|
|
return fread (buffer, 1, size, cb->handle); |
38 |
|
|
} |
39 |
|
|
|
40 |
|
|
|
41 |
|
|
/* Predefined write callback. */ |
42 |
|
|
static long |
43 |
|
|
write_cb (void *handle, const void *buffer, size_t size) |
44 |
|
|
{ |
45 |
|
|
file_data_t cb = (file_data_t)handle; |
46 |
|
|
return fwrite (buffer, 1, size, cb->handle); |
47 |
|
|
} |
48 |
|
|
|
49 |
|
|
|
50 |
|
|
/* Create a new data -> file association with a static callback. |
51 |
|
|
@fname is the file which is associated to the object. |
52 |
|
|
@r_cb is the context which holds all information. |
53 |
|
|
@for_read is 1 if the file is opened for read only. |
54 |
|
|
Return value: 0 on success. */ |
55 |
|
|
gpgme_error_t |
56 |
|
|
gpg_file_data_new (const char *fname, int for_read, file_data_t *r_cb) |
57 |
|
|
|
58 |
|
|
{ |
59 |
|
|
gpgme_error_t err; |
60 |
|
|
file_data_t cb; |
61 |
|
|
FILE *f; |
62 |
|
|
|
63 |
|
|
f = fopen (fname, for_read?"rb" : "wb"); |
64 |
|
|
if (!f) |
65 |
|
|
return gpgme_err_code_from_errno (errno); |
66 |
|
|
|
67 |
|
|
cb = (file_data_t)calloc (1, sizeof *cb); |
68 |
|
|
if (!cb) |
69 |
|
|
abort (); |
70 |
|
|
cb->cbs.read = read_cb; |
71 |
|
|
cb->cbs.write = write_cb; |
72 |
|
|
cb->handle = f; |
73 |
|
|
|
74 |
|
|
err = gpgme_data_new_from_cbs (&cb->dat, &cb->cbs, cb); |
75 |
|
|
if (err) { |
76 |
|
|
fclose (f); |
77 |
|
|
free (cb); |
78 |
|
|
return err; |
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
*r_cb = cb; |
82 |
|
|
return err; |
83 |
|
|
} |
84 |
|
|
|
85 |
|
|
|
86 |
|
|
/* Release the context in @cb. Close all internal handles if possible. */ |
87 |
|
|
void |
88 |
|
|
gpg_file_data_release (file_data_t cb) |
89 |
|
|
{ |
90 |
|
|
if (!cb) |
91 |
|
|
return; |
92 |
|
|
if (cb->handle) { |
93 |
|
|
FILE *f = (FILE *)cb->handle; |
94 |
|
|
fclose (f); |
95 |
|
|
} |
96 |
|
|
if (cb->dat) |
97 |
|
|
gpgme_data_release (cb->dat); |
98 |
|
|
free (cb); |
99 |
|
|
} |