move to meson&ninja (#1)

This commit is contained in:
T. von Dein
2025-11-21 00:33:11 +01:00
parent 4a23ca80f2
commit 12b6bbd3cb
11 changed files with 582 additions and 469 deletions

View File

@@ -6,13 +6,28 @@ labels:
platform: ${platform} platform: ${platform}
steps: steps:
build-n-test: build-berkeley:
when: when:
event: [push] event: [push]
image: alpine:latest image: alpine:latest
commands: commands:
- apk update - apk update
- apk add --no-cache bash build-base words-en gdb perl autoconf automake pcre pcre-dev gdbm gdbm-dev pkgconfig - apk add --no-cache bash build-base words-en gdb perl pcre2 pcre2-dev db db-dev pkgconfig meson ninja
- ./autogen.sh - meson setup --reconfigure build
- ./configure - ninja -C build
- make - rm -f test.db
- build/dbtool -d test.db -i -k "test" -v "blah blah blah"
- build/dbtool -d test.db -D | grep blah
build-gdbm:
when:
event: [push]
image: alpine:latest
commands:
- apk update
- apk add --no-cache bash build-base words-en gdb perl pcre2 pcre2-dev gdbm gdbm-dev pkgconfig meson ninja
- meson setup --reconfigure build
- ninja -C build
- rm -f test.db
- build/dbtool -d test.db -i -k "test" -v "blah blah blah"
- build/dbtool -d test.db -D | grep blah

View File

@@ -10,10 +10,11 @@ steps:
image: alpine:latest image: alpine:latest
commands: commands:
- apk update - apk update
- apk add --no-cache bash build-base - apk add --no-cache bash build-base words-en gdb perl pcre2 pcre2-dev gdbm gdbm-dev pkgconfig meson ninja
- make static - meson setup --reconfigure --prefer-static build
- file dicepwgen_static - ninja -C build
- mv dicepwgen_static dicepwgen-linux-amd64 - file build/dbtool
- mv build/dbtool dbtool-linux-amd64
release: release:
image: alpine:latest image: alpine:latest
@@ -25,4 +26,4 @@ steps:
commands: commands:
- apk update - apk update
- apk add --no-cache bash httpie jq git - apk add --no-cache bash httpie jq git
- .woodpecker/release.sh dicepwgen-linux-amd64 - .woodpecker/release.sh dbtool-linux-amd64

View File

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

View File

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

140
engine.cc
View File

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

View File

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

120
meson.build Normal file
View File

@@ -0,0 +1,120 @@
project(
'dbtool',
'cpp',
license: 'GPL',
version: '1.9.1',
meson_version: '>=1.3',
default_options: [
'warning_level=2',
'werror=true',
],
)
add_project_arguments(
[
'-Wno-unused-parameter',
'-Wno-unused-result',
'-Wno-missing-braces',
'-Wno-format-zero-length',
'-Wvla',
'-Wno-sign-compare',
'-Wno-narrowing'
],
language: 'cpp',
)
cpp = meson.get_compiler('cpp')
conf = configuration_data()
dbtool_inc = include_directories('.')
if host_machine.system().startswith('freebsd')
dbtool_inc = include_directories('.', '/usr/local/include')
add_project_link_arguments('LDFLAGS=/usr/local/lib')
endif
# check for funcs.
foreach func : ['getopt', 'fdopen', 'fgetc', 'getenv', 'getpass']
conf.set('HAVE_'+func.to_upper(), cpp.has_function(func, prefix : '#include <unistd.h>\n#include <stdio.h>\n#include <stdlib.h>'))
endforeach
# check for libraries with CMAKE or pkg-config
pcre2 = dependency('libpcre2-8')
# manually check for libraries
lib_berkeley = cpp.find_library('db_cxx', required: false,
dirs : ['/usr', '/usr/local'])
inc_berkeley = include_directories('/usr', '/usr/local')
lib_gdbm = cpp.find_library('gdbm', required: false,
dirs : ['/usr', '/usr/local'])
inc_gdbm = include_directories('/usr', '/usr/local')
if not lib_gdbm.found() and not lib_berkeley.found()
error('neither GDBM nor BerkeleyDB are installed')
endif
# check commandline options
prefix = get_option('prefix')
if get_option('buildtype') == 'debug'
conf.set('DEBUG', '1')
endif
# setup conf map
version = '@0@'.format(meson.project_version())
conf.set('HAVE_LIBPCRE', pcre2.found())
conf.set('HAVE_BERKELEY', lib_berkeley.found())
conf.set('HAVE_GDBM', lib_gdbm.found())
conf.set('prefix', prefix)
conf.set('VERSION', version)
# write out the config header
m = configure_file(
input : 'platform.h.in',
output : 'platform.h',
configuration : conf,
)
# code
dbtool_sources = files(
'cipher.cc',
'config.cc',
'dbtool.cc',
'digest.cc',
'engine.cc',
'rijndael.cc'
)
# add dependencies, manual libs are added directly below
dbtool_deps = [
pcre2
]
executable(
'dbtool',
[dbtool_sources],
include_directories: [dbtool_inc, inc_berkeley, inc_gdbm],
dependencies: [dbtool_deps, lib_berkeley, lib_gdbm],
install: true
)
# build manual page
pod2man = find_program('pod2man', native: true)
if pod2man.found()
res = run_command(pod2man.full_path(), 'dbtool.pod', 'dbtool.1', check:true)
if res.returncode() == 0
install_man('dbtool.1')
endif
endif

1
meson_options.txt Normal file
View File

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

View File

@@ -1,92 +1,19 @@
/* platform.h.in. Generated from configure.ac by autoheader. */ /* platform.h.in. Generated from configure.ac by autoheader. */
/* "Berkeley DB C++ Header File" */ #mesondefine HAVE_BERKELEY
#undef DB_CXX_HEADER #mesondefine HAVE_GDBM
#mesondefine HAVE_LIBPCRE
#mesondefine HAVE_GETPASS
#mesondefine HAVE_GETOPT
#mesondefine HAVE_FDOPEN
#mesondefine HAVE_FGETC
#mesondefine HAVE_GETENV
/* Define to 1 if you have the <dlfcn.h> header file. */
#undef HAVE_DLFCN_H
/* Define to 1 if you have the `fdopen' function. */ #define PCRE2_CODE_UNIT_WIDTH 8
#undef HAVE_FDOPEN #define PACKAGE "dbtool"
/* Define to 1 if you have the `fgetc' function. */ #define VERSION "@VERSION@"
#undef HAVE_FGETC
/* Define to 1 if you have the `getenv' function. */
#undef HAVE_GETENV
/* Define to 1 if you have the `getopt' function. */
#undef HAVE_GETOPT
/* Define to 1 if you have the `getpass' function. */
#undef HAVE_GETPASS
/* Define to 1 if you have the <inttypes.h> header file. */
#undef HAVE_INTTYPES_H
/* Define to 1 if you have the `gdbm' library (-lgdbm). */
#undef HAVE_LIBGDBM
/* Define to 1 if you have the `pcre' library (-lpcre). */
#undef HAVE_LIBPCRE
/* Define to 1 if you have the <memory.h> header file. */
#undef HAVE_MEMORY_H
/* Define to 1 if you have the <stdint.h> header file. */
#undef HAVE_STDINT_H
/* Define to 1 if you have the <stdio.h> header file. */
#undef HAVE_STDIO_H
/* Define to 1 if you have the <stdlib.h> header file. */
#undef HAVE_STDLIB_H
/* Define to 1 if you have the <strings.h> header file. */
#undef HAVE_STRINGS_H
/* Define to 1 if you have the <string.h> header file. */
#undef HAVE_STRING_H
/* Define to 1 if you have the <sys/stat.h> header file. */
#undef HAVE_SYS_STAT_H
/* Define to 1 if you have the <sys/types.h> header file. */
#undef HAVE_SYS_TYPES_H
/* Define to 1 if you have the <unistd.h> header file. */
#undef HAVE_UNISTD_H
/* Define to the sub-directory in which libtool stores uninstalled libraries.
*/
#undef LT_OBJDIR
/* Name of package */
#undef PACKAGE
/* Define to the address where bug reports for this package should be sent. */
#undef PACKAGE_BUGREPORT
/* Define to the full name of this package. */
#undef PACKAGE_NAME
/* Define to the full name and version of this package. */
#undef PACKAGE_STRING
/* Define to the one symbol short name of this package. */
#undef PACKAGE_TARNAME
/* Define to the home page for this package. */
#undef PACKAGE_URL
/* Define to the version of this package. */
#undef PACKAGE_VERSION
/* Define to 1 if you have the ANSI C header files. */
#undef STDC_HEADERS
/* Version number of package */
#undef VERSION
/* Define to empty if `const' does not conform to ANSI C. */ /* Define to empty if `const' does not conform to ANSI C. */
#undef const #undef const

3
platform.sh Executable file
View File

@@ -0,0 +1,3 @@
#!/bin/sh
echo "$@"> platform.h

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][0] = (iv[3][0] << 1) | (iv[3][1] >> 7);
iv[3][1] = (iv[3][1] << 1) | (iv[3][2] >> 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][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; 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][0] = (iv[3][0] << 1) | (iv[3][1] >> 7);
iv[3][1] = (iv[3][1] << 1) | (iv[3][2] >> 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][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); outBuffer[k/8] ^= (block[0] & 0x80) >> (k & 7);
} }
} }