fixed compiler warnings and errors

This commit is contained in:
2025-11-20 23:45:47 +01:00
parent 7b697a5e97
commit c5953d152b
6 changed files with 422 additions and 375 deletions

View File

@@ -79,18 +79,22 @@ string cipher::encrypt(const string& source) {
rijn.init(Rijndael::CBC, Rijndael::Encrypt, key, Rijndael::Key32Bytes);
/* encrypt the source */
unsigned char output[size + 16];
unsigned char *output = (unsigned char *)malloc(size + 16);
int res = rijn.padEncrypt(plainText, (int)size, output);
/* convert the result back to char[] */
char outText[res];
char *outText = (char *)malloc(res);
for(int y=0; y<(res); y++) {
outText[y] = output[y];
}
free(output);
/* return the crypted string */
if(res >= 0) {
return string(outText, res);
string text = string(outText, res);
free(outText);
return text;
}
else {
cerr << "Failed to encrypt: " << error(res) << "!" << endl;
@@ -113,18 +117,23 @@ string cipher::decrypt(const string& source) {
rijn.init(Rijndael::CBC, Rijndael::Decrypt, key, Rijndael::Key32Bytes);
/* decrypt the source */
unsigned char output[size];
unsigned char *output = (unsigned char *)malloc(size);
int res = rijn.padDecrypt(cryptedText, (int)size, output);
/* convert the result back to char[] */
char outText[res];
char *outText = (char *)malloc(res);
for(int y=0; y<(res); y++) {
outText[y] = output[y];
}
free(output);
/* return the decrypted string */
if (res >= 0)
return string(outText, res);
if (res >= 0) {
string text = string(outText, res);
free(outText);
return text;
}
else {
cerr << "Failed to decrypt: " << error(res) << " (passphrase invalid?) !" << endl;
exit(1);

View File

@@ -127,7 +127,6 @@ int main(int argc, char *argv[]) {
string readpass() {
char *pass;
char *envpass;
envpass = getenv(PW_VARNAME);
if(envpass != NULL) {
@@ -136,7 +135,7 @@ string readpass() {
}
else {
#ifdef HAVE_GETPASS
pass = getpass("passphrase: ");
char *pass = getpass("passphrase: ");
string password = pass;
free(pass);
#else

140
engine.cc
View File

@@ -45,8 +45,6 @@
*/
#include "dbtool.h"
#include "engine.h"
#include "platform.h"
@@ -184,23 +182,35 @@ void Engine::dump() {
}
/*
* search for regexp given in config.key
*/
void Engine::regexp() {
init();
int num;
pcre *p_pcre;
pcre_extra *p_pcre_extra;
char *err_str;
int sub_len = 9;
int *sub_vec;
int erroffset;
p_pcre_extra = NULL;
p_pcre = pcre_compile((char *)config.key.c_str(), 0,
(const char **)(&err_str), &erroffset, NULL);
int errnumber;
PCRE2_SIZE erroffset;
pcre2_match_data *match_data;
pcre2_code *p_pcre = pcre2_compile(
(PCRE2_SPTR)config.key.c_str(), /* the pattern */
PCRE2_ZERO_TERMINATED, /* indicates pattern is zero-terminated */
0, /* default options */
&errnumber, /* for error number */
&erroffset, /* for error offset */
NULL); /* use default compile context */
if (p_pcre == NULL) {
PCRE2_UCHAR buffer[256];
pcre2_get_error_message(errnumber, buffer, sizeof(buffer));
cerr << "PCRE2 compilation failed at offset: " << erroffset << " with: " << buffer << endl;
exit(1);
}
match_data = pcre2_match_data_create_from_pattern(p_pcre, NULL);
#ifdef HAVE_BERKELEY
Dbc *dbcp;
db->cursor(NULL, &dbcp, 0);
@@ -218,9 +228,10 @@ void Engine::regexp() {
string K(key.dptr, key.dsize);
string V(value.dptr, value.dsize);
#endif
sub_vec = new int(sub_len);
num = pcre_exec(p_pcre, p_pcre_extra, (char *)K.c_str(),
(int)K.length(), 0, 0, (int *)sub_vec, sub_len);
num = pcre2_match(p_pcre, (PCRE2_SPTR)K.c_str(), (PCRE2_SIZE)K.length(), 0, 0, match_data, NULL);
if(num == 1){
if(config.reverse == 1) {
cout << decode(V);
@@ -243,7 +254,10 @@ void Engine::regexp() {
key = gdbm_nextkey(db,key);
#endif
}
pcre_free(p_pcre);
pcre2_match_data_free(match_data);
pcre2_code_free(p_pcre);
#ifdef HAVE_BERKELEY
dbcp->close();
db->close(0);
@@ -253,10 +267,6 @@ void Engine::regexp() {
}
/*
* Insert data into the db
*/
@@ -276,47 +286,72 @@ void Engine::from_input() {
string value = "";
string line = "";
int num, match;
pcre *p_pcre;
pcre_extra *p_pcre_extra;
char *err_str;
int sub_len = 9;
int *sub_vec;
char *v1;
char *v2;
int erroffset;
p_pcre_extra = NULL;
p_pcre = pcre_compile((char *)config.token.c_str(), 0,
(const char **)(&err_str), &erroffset, NULL);
int num;
int errnumber;
PCRE2_SIZE erroffset;
pcre2_match_data *match_data;
PCRE2_SIZE *ovector;
pcre2_code *p_pcre = pcre2_compile(
(PCRE2_SPTR)config.token.c_str(),
PCRE2_ZERO_TERMINATED,
0,
&errnumber,
&erroffset,
NULL);
if (p_pcre == NULL) {
PCRE2_UCHAR buffer[256];
pcre2_get_error_message(errnumber, buffer, sizeof(buffer));
cerr << "PCRE2 compilation failed at offset: " << erroffset << " with: " << buffer << endl;
exit(1);
}
match_data = pcre2_match_data_create_from_pattern(p_pcre, NULL);
while((c = fgetc(stream)) != EOF) {
if(c == '\n') {
// record end
mode = "key";
if(config.token != "") {
v1 = new char[line.length()];
v2 = new char[line.length()];
sub_vec = new int[sub_len];
num = pcre_exec(p_pcre, p_pcre_extra, (char *)line.c_str(),
(int)line.length(), 0, 0, (int *)sub_vec, sub_len);
//char **subs = new char*[2];
char *subs[2];
num = pcre2_match(p_pcre, (PCRE2_SPTR)line.c_str(), (PCRE2_SIZE)line.length(), 0, 0, match_data, NULL);
if(num < 0)
cerr << "Token \"" << config.token << "\" did not match on \"" << line << "\"!\n";
else if(num == 1)
cerr << "Token " << config.token << " did not produce sub strings!\n";
else {
match = pcre_copy_substring((char *)line.c_str(), sub_vec, num, 1, v1, line.length());
match = pcre_copy_substring((char *)line.c_str(), sub_vec, num, 2, v2, line.length());
ovector = pcre2_get_ovector_pointer(match_data);
for (int i = 0; i < num; i++) {
char * substring_start = const_cast<char*>(line.c_str()) + ovector[2*i];
if (i == 2) {
break;
}
subs[i] = substring_start;
}
free(ovector);
if(config.reverse) {
value = v1;
key = v2;
value = subs[0];
key = subs[1];
}
else {
value = v2;
key = v1;
value = subs[1];
key = subs[0];
}
}
delete(sub_vec);
pcre_free((void *)v1);
pcre_free((void *)v2);
//delete(subs);
line = "";
}
value = encode(value);
@@ -379,7 +414,10 @@ void Engine::from_input() {
value += char(c);
}
}
pcre_free(p_pcre);
pcre2_match_data_free(match_data);
pcre2_code_free(p_pcre);
#ifdef HAVE_BERKELEY
db->close(0);
#else
@@ -447,7 +485,6 @@ void Engine::insert() {
}
/*
* update a database
*/
@@ -521,15 +558,14 @@ void Engine::update() {
}
/*
* remove an entry
*/
void Engine::remove() {
init();
int ret;
#ifdef HAVE_BERKELEY
Dbt key((char *)config.key.c_str(), config.key.length() + 1);
int ret;
if((ret = db->del(NULL, &key, 0)) != 0) {
cerr << "Database error" << "(" << strerror(ret) << ")" << endl;
exit(1);
@@ -537,13 +573,16 @@ void Engine::remove() {
db->close(0);
#else
datum key = {(char *)config.key.c_str(), config.key.length()};
ret = gdbm_delete(db, key);
int ret;
if((ret = gdbm_delete(db, key)) != 0) {
cerr << "Database error" << "(" << strerror(ret) << ")" << endl;
exit(1);
}
gdbm_close(db);
#endif
}
/*
* search for specific data
*/
@@ -604,4 +643,3 @@ string Engine::decode(const string& data) {
else
return data;
}

View File

@@ -58,9 +58,10 @@
#include <string>
extern "C" {
#include "platform.h"
#include <stdio.h>
#include <stdlib.h>
#include <pcre.h>
#include <pcre2.h>
}
#ifdef HAVE_BERKELEY
@@ -74,8 +75,7 @@ extern "C" {
#endif
#endif
#include "platform.h"
#include DB_CXX_HEADER
#include <db_cxx.h>
#else

1
meson_options.txt Normal file
View File

@@ -0,0 +1 @@
# custom build options

View File

@@ -1108,7 +1108,7 @@ int Rijndael::blockEncrypt(const UINT8 *input,int inputLen,UINT8 *outBuffer)
iv[3][0] = (iv[3][0] << 1) | (iv[3][1] >> 7);
iv[3][1] = (iv[3][1] << 1) | (iv[3][2] >> 7);
iv[3][2] = (iv[3][2] << 1) | (iv[3][3] >> 7);
iv[3][3] = (iv[3][3] << 1) | (outBuffer[k/8] >> (7-(k&7))) & 1;
iv[3][3] = (iv[3][3] << 1) | ((outBuffer[k/8] >> (7-(k&7))) & 1);
}
}
break;
@@ -1262,7 +1262,7 @@ int Rijndael::blockDecrypt(const UINT8 *input, int inputLen, UINT8 *outBuffer)
iv[3][0] = (iv[3][0] << 1) | (iv[3][1] >> 7);
iv[3][1] = (iv[3][1] << 1) | (iv[3][2] >> 7);
iv[3][2] = (iv[3][2] << 1) | (iv[3][3] >> 7);
iv[3][3] = (iv[3][3] << 1) | (input[k/8] >> (7-(k&7))) & 1;
iv[3][3] = (iv[3][3] << 1) | ((input[k/8] >> (7-(k&7))) & 1);
outBuffer[k/8] ^= (block[0] & 0x80) >> (k & 7);
}
}