summaryrefslogtreecommitdiff
path: root/plugins/CryptoPP/src/GPGw/tools.cpp
blob: fb125016291b12c93c3f2cb558f928e2d9cf3eb2 (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
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
#include "../commonheaders.h"
#include "gpgw.h"

// escape sequence type
struct escapesequence
{
  char *sequence;
  char *code;
};

// supported escape sequences
struct escapesequence escsequence[]=
{
  {"\\n", "\n"},
  {"\\r", "\r"},
  {"\\t", "\t"}
};

// number of supported escape sequences
int escsequencecount=sizeof(escsequence)/sizeof(struct escapesequence);


void replace(char *atext, const char *apattern, const char *areplacement)
{
  char *pos, *last;
  long textsize;
  long patternsize;
  long replacementsize;

  pos=atext;
  patternsize = (long)strlen(apattern);
  replacementsize = (long)strlen(areplacement);

  do
  {
    // textstelle suchen
    last=pos;
    pos=strstr(pos, apattern);
    // etwas gefunden?
    if(pos==NULL) break;

    // laenge des textes ermitteln
    textsize = (long)strlen(last);
    // platz schaffen
    memmove(pos+replacementsize, pos+patternsize, textsize-((pos-last)+patternsize)+1);
    // ersetzen
    strncpy(pos, areplacement, replacementsize);
    pos+=replacementsize;
  }
  while(pos!=NULL);
}


char *getNextPart(char *aresult, char *atext, const char *aseparator)
{
  char *pos;

  strcpy(aresult, "");
  pos=strstr(atext, aseparator);

  if(pos!=NULL)
  {
    strncat(aresult, atext, pos-atext);
    pos+=strlen(aseparator);
  }
  else strcpy(aresult, atext);

  return pos;
}


void getLastPart(char *aresult, char *atext)
{
	strcpy(aresult, atext);
}


void appendText(char **abuffer, const char *atext, int atextsize)
{
  size_t size;

  if (*abuffer==NULL) size=0;
  else size = strlen(*abuffer);
  size++; // abschliessende 0
  if(atextsize==0) atextsize = (int)strlen(atext);
  size+=atextsize;

  *abuffer=(char *)realloc(*abuffer, size);
  strncat(*abuffer, atext, atextsize);
}


BOOL existsFile(const char *afilename)
{
  int attr;

  if(strlen(afilename)==0) return FALSE;

  attr = GetFileAttributes(afilename);

  return ( (attr!=-1) && ((attr&FILE_ATTRIBUTE_DIRECTORY)==0));
}


BOOL existsPath(const char *apathname)
{
  int attr;

  if(strlen(apathname)==0) return FALSE;

  attr = GetFileAttributes(apathname);

  return ( (attr!=-1) && (attr&FILE_ATTRIBUTE_DIRECTORY) );
}


BOOL writeToFile(char *afilename, const char *atext)
{
  FILE *handle;
  size_t written;
  size_t length;

  getTemporaryFileName(afilename);

  handle=fopen(afilename, "wb");

  if(handle==NULL)
  {
    LogMessage("--- ", "write to file", "\n");
	LogMessage("filename:\n", afilename, "\n");
    return FALSE;
  }

  length=strlen(atext);
  written=fwrite(atext, sizeof(char), length, handle);

  if(written!=length) return FALSE;

  fclose(handle);
  return TRUE;
}


BOOL readFromFile(char *aresult, const char *afilename)
{
  FILE *handle;
  size_t filesize;
  size_t read;

  handle=fopen(afilename, "rb");
  if(handle==NULL) 
  {
    LogMessage("--- ", "read from file", "\n");
	LogMessage("filename:\n", afilename, "\n");
    return FALSE;
  }

  fseek(handle, 0, SEEK_END);
  filesize=ftell(handle);
  fseek(handle, 0, SEEK_SET);

  read=fread(aresult, sizeof(char), filesize, handle);
  if(read!=filesize) return FALSE;

  fclose(handle);
  return TRUE;
}


void getTemporaryFileName(char *aresult)
{
  GetTempFileName(temporarydirectory,"gpg",0,aresult);
}


void quoteEscapeSequences(char *atext)
{
  int i;

  for(i=0; i<escsequencecount; i++)
    replace(atext, escsequence[i].code, escsequence[i].sequence);
}


void unquoteEscapeSequences(char *atext)
{
  int i;

  for(i=0; i<escsequencecount; i++)
    replace(atext, escsequence[i].sequence, escsequence[i].code);
}