From f424a18112032cf61d2871a6b91a5af607c171ae Mon Sep 17 00:00:00 2001 From: Kirill Volinsky Date: Fri, 20 Jul 2012 16:21:49 +0000 Subject: CryptoPP: changed folder structure git-svn-id: http://svn.miranda-ng.org/main/trunk@1083 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/CryptoPP/src/GPGw/tools.c | 191 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 191 insertions(+) create mode 100644 plugins/CryptoPP/src/GPGw/tools.c (limited to 'plugins/CryptoPP/src/GPGw/tools.c') diff --git a/plugins/CryptoPP/src/GPGw/tools.c b/plugins/CryptoPP/src/GPGw/tools.c new file mode 100644 index 0000000000..002f18824d --- /dev/null +++ b/plugins/CryptoPP/src/GPGw/tools.c @@ -0,0 +1,191 @@ +#include "commonheaders.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=strlen(apattern); + replacementsize=strlen(areplacement); + + do + { + // textstelle suchen + last=pos; + pos=strstr(pos, apattern); + // etwas gefunden? + if(pos==NULL) break; + + // laenge des textes ermitteln + textsize=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) +{ + int size; + + if (*abuffer==NULL) size=0; + else size=strlen(*abuffer); + size++; // abschliessende 0 + if(atextsize==0) atextsize=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