aboutsummaryrefslogtreecommitdiffstats
path: root/complus/tgpgcom.c
blob: 27516b1d4cdf6b7eabc47dd124aa4d54e62a1476 (plain)
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
/* tgpgcom.c - Test the IGpgme classes
 *	Copyright (C) 2001 g10 Code GmbH
 *
 * This file is part of GPGME.
 *
 * GPGME is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * GPGME is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 */


#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <time.h>
#include <windows.h>

#define INITGUID
#include "igpgme.h"


int 
main (int argc, char **argv)
{
    IUnknown *pUnknown = NULL;
    IGpgme   *pGpgme;
    HRESULT hr;
    BSTR bs;
    
    hr = CoInitializeEx (NULL, COINIT_APARTMENTTHREADED); 
    if (hr)
        fprintf (stderr, "CoInitializeEx() failed: hr=%lu\n", hr);

    fprintf (stderr, "system initialized\n");
    hr = CoCreateInstance (&CLSID_Gpgme, NULL, CLSCTX_LOCAL_SERVER,
                           &IID_IUnknown, (void**)&pUnknown );
    if (hr)
        fprintf (stderr, "CoCreateInstance() failed: hr=%lx\n", hr);
    if (!pUnknown)
        exit (1);

    fprintf (stderr,"got object %p - querying %s\n",
             pUnknown, debugstr_guid(&IID_IGpgme));
    hr = IGpgme_QueryInterface (pUnknown, &IID_IGpgme, (void**)&pGpgme);
    if (hr) {
        fprintf (stderr, "QueryInterface() failed: hr=%lx\n", hr);
        goto leave;
    }
    fprintf (stderr, "got interface %p\n", pGpgme);

    hr = IGpgme_SetArmor (pGpgme, 1);
    fprintf (stderr, "SetArmor returned %lx\n", hr);

    hr = IGpgme_SetTextmode (pGpgme, 0);
    fprintf (stderr, "SetTextmode returned %lx\n", hr);

    hr = IGpgme_ClearRecipients (pGpgme);
    fprintf (stderr, "ClearRecipients returned %lx\n", hr);

    bs = SysAllocString (L"alice");
    if (!bs)
      fprintf (stderr, "SysAllocString failed: ec=%d\n", (int)GetLastError());
    else {
      int i;
      
      for (i=-4; i < 12; i++ )
        fprintf (stderr," %02X", ((unsigned char*)bs)[i] );
      putc ('\n', stderr);
    }
    hr = IGpgme_AddRecipient (pGpgme, bs, -1);
    fprintf (stderr, "AddRecipients returned %lx\n", hr);
    
    {
      SAFEARRAY *sa;
      VARIANT v;
      char *p;
      
      sa = SafeArrayCreateVector (VT_UI1, 0, 20);
      if (!sa) {
        fprintf (stderr, "SafeArrayCreateVector failed\n");
        goto leave;
      }

      hr = SafeArrayAccessData (sa, (void**)&p);
      if (hr) {
        fprintf (stderr,"SafeArrayAccessData failed: hr=%lx\n", hr);
        goto leave;
      }

      memcpy (p, "=> Omnis enim res <=", 20 );
      SafeArrayUnaccessData (sa);

      VariantInit (&v);
      v.vt = (VT_ARRAY|VT_UI1);
      v.u.parray = sa;
      
      hr = IGpgme_SetPlaintext (pGpgme, v );
      fprintf (stderr, "SetPlaintext returned %lx\n", hr);
      SafeArrayDestroyData (sa);
      SafeArrayDestroy (sa);

      VariantClear (&v);
    }

    hr = IGpgme_Encrypt (pGpgme);
    fprintf (stderr, "Encrypt returned %lx\n", hr);

    {
      VARIANT v;
    
      hr = IGpgme_GetCiphertext (pGpgme, &v);
      fprintf (stderr, "GetCiphertext returned %lx\n", hr);
      if (!hr) {
          if (v.vt != (VT_ARRAY|VT_UI1)) 
              fprintf (stderr, "Invalid array typed returned\n");
          else {
              unsigned char *p;
              
              hr = SafeArrayAccessData (v.u.parray, (void**)&p);
              if (hr) 
                  fprintf (stderr,"*** SafeArrayAccessData failed: %lx\n", hr);
              else {
                  size_t arraysize = v.u.parray->rgsabound[0].cElements;
                  fprintf (stderr,"*** got %d bytes\n", (int)arraysize);
                  for (;arraysize; arraysize--, p++ )
                      putc (*p, stderr);
                  SafeArrayUnaccessData (v.u.parray);
              }
          }
      }
    }
    IGpgme_Release (pGpgme);

 leave:
    CoUninitialize ();
    fprintf (stderr, "system uninitialized\n");
    return 0;
}