Made load/save strings dynamically reallocated

This commit is contained in:
tengel 2024-03-20 09:22:23 -05:00
parent 9bae66a55b
commit eb6555d5df
2 changed files with 23 additions and 4 deletions

View file

@ -194,12 +194,14 @@ int KpasmanDoc::loadInit(const char* filename, const char *password)
unsigned short ciphertext[4];
char key[128];
Krc2* krc2 = new Krc2();
bufsize = 0;
fd = fopen (filename, "rb");
if (fd == NULL)
return PWERR_OPEN;
buffer = (char *)malloc(LOAD_BUFFER_LENGTH);
bufsize = LOAD_BUFFER_LENGTH;
buffer = (char *)malloc(bufsize);
for (j = 0; password[j] != '\0'; j++) {
key[j] = password[j];
}
@ -239,6 +241,11 @@ int KpasmanDoc::loadInit(const char* filename, const char *password)
} /* for (count2) */
krc2->rc2_decrypt (plaintext);
// Do a sanity check to make sure the buffer is large enough
if (((int)strlen(buffer)+9) >= bufsize) {
bufsize += LOAD_BUFFER_LENGTH;
buffer = (char *)realloc(buffer, bufsize);
}
memcpy ((unsigned char *) (buffer + bufferIndex), plaintext, 8);
bufferIndex += 8;
buffer[bufferIndex + 1] = '\0';
@ -339,6 +346,7 @@ int KpasmanDoc::saveInit(const char *filename, const char *password)
unsigned int keylength;
int val, count2;
Krc2* krc2 = new Krc2();
bufsize = 0;
/* first we should check the permissions of the filename */
if (QFile::exists(filename)) {
@ -356,8 +364,9 @@ int KpasmanDoc::saveInit(const char *filename, const char *password)
fd = fopen (filename, "wb");
if (fd == NULL)
return PWERR_OPEN;
buffer = (char*)malloc(SAVE_BUFFER_LENGTH);
bufsize = SAVE_BUFFER_LENGTH;
buffer = (char*)malloc(bufsize);
/* make the key ready */
for (j = 0; password[j] != '\0'; j++) {
@ -385,7 +394,16 @@ int KpasmanDoc::saveEntry(char *entry[4])
unsigned short ciphertext[4];
Krc2* krc2 = new Krc2();
buffer = (char*)memset(buffer, '\0', SAVE_BUFFER_LENGTH);
// Make sure buffer is large enough for this entry
int allcount = (int)(strlen(buffer)+strlen(entry[0])+
strlen(entry[1])+strlen(entry[2])+
strlen(entry[3]))+5;
if (allcount >= bufsize) {
bufsize = allcount + 1;
buffer = (char*)realloc(buffer, bufsize);
}
buffer = (char*)memset(buffer, '\0', bufsize);
for (count2 = 0; count2 < 4; count2++) {
text1 = entry[count2];

View file

@ -131,6 +131,7 @@ class KpasmanDoc : public QObject
FILE *fd;
unsigned short iv[4];
char *buffer;
int bufsize;
/** these two are global because save_entry() and save_finalize() both need them */
int bufferIndex;
unsigned short plaintext[4];