diff --git a/tools/bftest.c b/tools/bftest.c index 8a1572c2b..3c1d93bd9 100644 --- a/tools/bftest.c +++ b/tools/bftest.c @@ -1,106 +1,107 @@ /* bftest.c - Blowfish test program * Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc. * * This file is part of GnuPG. * * GnuPG is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * GnuPG is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, see . */ #include #include #include #include #ifdef HAVE_DOSISH_SYSTEM #include #include #endif +#define INCLUDED_BY_MAIN_MODULE 1 #include "util.h" #include "cipher.h" #include "i18n.h" static void my_usage(void) { fprintf(stderr, "usage: bftest [-e][-d] algo key\n"); exit(1); } const char * strusage( int level ) { return default_strusage(level); } static void i18n_init(void) { #ifdef ENABLE_NLS setlocale( LC_ALL, "" ); bindtextdomain (PACKAGE, LOCALEDIR); textdomain( PACKAGE ); #endif } int main(int argc, char **argv) { int encode=0; CIPHER_HANDLE hd; char buf[4096]; int n, size=4096; int algo; #ifdef HAVE_DOSISH_SYSTEM setmode( fileno(stdin), O_BINARY ); setmode( fileno(stdout), O_BINARY ); #endif i18n_init(); if( argc > 1 && !strcmp(argv[1], "-e") ) { encode++; argc--; argv++; } else if( argc > 1 && !strcmp(argv[1], "-E") ) { encode++; argc--; argv++; size = 10; } else if( argc > 1 && !strcmp(argv[1], "-d") ) { argc--; argv++; } else if( argc > 1 && !strcmp(argv[1], "-D") ) { argc--; argv++; size = 10; } if( argc != 3 ) my_usage(); argc--; argv++; algo = string_to_cipher_algo( *argv ); argc--; argv++; hd = cipher_open( algo, CIPHER_MODE_CFB, 0 ); cipher_setkey( hd, *argv, strlen(*argv) ); cipher_setiv( hd, NULL, 0 ); while( (n = fread( buf, 1, size, stdin )) > 0 ) { if( encode ) cipher_encrypt( hd, buf, buf, n ); else cipher_decrypt( hd, buf, buf, n ); if( fwrite( buf, 1, n, stdout) != n ) log_fatal("write error\n"); } cipher_close(hd); return 0; } diff --git a/tools/mpicalc.c b/tools/mpicalc.c index 46e5fc824..e75d4af39 100644 --- a/tools/mpicalc.c +++ b/tools/mpicalc.c @@ -1,386 +1,387 @@ /* mpicalc.c - test the mpi functions * Copyright (C) 1997, 1998, 1999, 2004, 2006 Werner Koch * * This is an RPN calculator; values must be given in hex. * Operation is like dc(1) except that the input/output radix is * always 16 and you can use a '-' to prefix a negative number. * Addition operators: ++ and --. All operators must be delimited by a blank * * WARNING: This is an old test utility which is not anymore * maintained as part of GnuPG. However, Libgcrypt has a * copy of it which uses the libgcrypt functions. * * This file is part of GnuPG. * * GnuPG is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * GnuPG is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, see . */ #include #include #include #include +#define INCLUDED_BY_MAIN_MODULE 1 #include "util.h" #include "mpi.h" #include "i18n.h" #define STACKSIZE 100 static MPI stack[STACKSIZE]; static int stackidx; const char * strusage( int level ) { const char *p; switch( level ) { case 10: case 0: p = "mpicalc - v" VERSION "; " "Copyright 1997 Werner Koch (dd9jn)" ; break; case 13: p = "mpicalc"; break; case 14: p = VERSION; break; case 1: case 11: p = "Usage: mpicalc (-h for help)"; break; case 2: case 12: p = "\nSyntax: mpicalc [options] [files]\n" "MPI RPN calculator\n"; break; default: p = default_strusage(level); } return p; } static void i18n_init(void) { #ifdef ENABLE_NLS setlocale( LC_ALL, "" ); bindtextdomain (PACKAGE, LOCALEDIR); textdomain( PACKAGE ); #endif } static void do_add(void) { if( stackidx < 2 ) { fputs("stack underflow\n",stderr); return; } mpi_add( stack[stackidx-2], stack[stackidx-2], stack[stackidx-1] ); stackidx--; } static void do_sub(void) { if( stackidx < 2 ) { fputs("stack underflow\n", stderr); return; } mpi_sub( stack[stackidx-2], stack[stackidx-2], stack[stackidx-1] ); stackidx--; } static void do_inc(void) { if( stackidx < 1 ) { fputs("stack underflow\n", stderr); return; } mpi_add_ui( stack[stackidx-1], stack[stackidx-1], 1 ); } static void do_dec(void) { if( stackidx < 1 ) { fputs("stack underflow\n", stderr); return; } /* mpi_sub_ui( stack[stackidx-1], stack[stackidx-1], 1 ); */ } static void do_mul(void) { if( stackidx < 2 ) { fputs("stack underflow\n", stderr); return; } mpi_mul( stack[stackidx-2], stack[stackidx-2], stack[stackidx-1] ); stackidx--; } static void do_mulm(void) { if( stackidx < 3 ) { fputs("stack underflow\n", stderr); return; } mpi_mulm( stack[stackidx-3], stack[stackidx-3], stack[stackidx-2], stack[stackidx-1] ); stackidx -= 2; } static void do_div(void) { if( stackidx < 2 ) { fputs("stack underflow\n", stderr); return; } mpi_fdiv_q( stack[stackidx-2], stack[stackidx-2], stack[stackidx-1] ); stackidx--; } static void do_rem(void) { if( stackidx < 2 ) { fputs("stack underflow\n", stderr); return; } mpi_fdiv_r( stack[stackidx-2], stack[stackidx-2], stack[stackidx-1] ); stackidx--; } static void do_powm(void) { MPI a; if( stackidx < 3 ) { fputs("stack underflow\n", stderr); return; } a= mpi_alloc(10); mpi_powm( a, stack[stackidx-3], stack[stackidx-2], stack[stackidx-1] ); mpi_free(stack[stackidx-3]); stack[stackidx-3] = a; stackidx -= 2; } static void do_inv(void) { MPI a = mpi_alloc(40); if( stackidx < 2 ) { fputs("stack underflow\n", stderr); return; } mpi_invm( a, stack[stackidx-2], stack[stackidx-1] ); mpi_set(stack[stackidx-2],a); mpi_free(a); stackidx--; } static void do_gcd(void) { MPI a = mpi_alloc(40); if( stackidx < 2 ) { fputs("stack underflow\n", stderr); return; } mpi_gcd( a, stack[stackidx-2], stack[stackidx-1] ); mpi_set(stack[stackidx-2],a); mpi_free(a); stackidx--; } static void do_rshift(void) { if( stackidx < 1 ) { fputs("stack underflow\n", stderr); return; } mpi_rshift( stack[stackidx-1],stack[stackidx-1], 1 ); } int main(int argc, char **argv) { static ARGPARSE_OPTS opts[] = { {0} }; ARGPARSE_ARGS pargs; int i, c; int state = 0; char strbuf[1000]; int stridx=0; pargs.argc = &argc; pargs.argv = &argv; pargs.flags = 0; i18n_init(); while( arg_parse( &pargs, opts) ) { switch( pargs.r_opt ) { default : pargs.err = 2; break; } } if( argc ) usage(1); for(i=0; i < STACKSIZE; i++ ) stack[i] = NULL; stackidx =0; while( (c=getc(stdin)) != EOF ) { if( !state ) { /* waiting */ if( isdigit(c) ) { state = 1; ungetc(c, stdin); strbuf[0] = '0'; strbuf[1] = 'x'; stridx=2; } else if( isspace(c) ) ; else { switch(c) { case '+': if( (c=getc(stdin)) == '+' ) do_inc(); else { ungetc(c, stdin); do_add(); } break; case '-': if( (c=getc(stdin)) == '-' ) do_dec(); else if( isdigit(c) || (c >='A' && c <= 'F') ) { state = 1; ungetc(c, stdin); strbuf[0] = '-'; strbuf[1] = '0'; strbuf[2] = 'x'; stridx=3; } else { ungetc(c, stdin); do_sub(); } break; case '*': do_mul(); break; case 'm': do_mulm(); break; case '/': do_div(); break; case '%': do_rem(); break; case '^': do_powm(); break; case 'I': do_inv(); break; case 'G': do_gcd(); break; case '>': do_rshift(); break; case 'i': /* dummy */ if( !stackidx ) fputs("stack underflow\n", stderr); else { mpi_free(stack[stackidx-1]); stackidx--; } break; case 'd': /* duplicate the tos */ if( !stackidx ) fputs("stack underflow\n", stderr); else if( stackidx < STACKSIZE ) { mpi_free(stack[stackidx]); stack[stackidx] = mpi_copy( stack[stackidx-1] ); stackidx++; } else fputs("stack overflow\n", stderr); break; case 'c': for(i=0; i < stackidx; i++ ) mpi_free(stack[i]), stack[i] = NULL; stackidx = 0; break; case 'p': /* print the tos */ if( !stackidx ) puts("stack is empty"); else { mpi_print(stdout, stack[stackidx-1], 1 ); putchar('\n'); } break; case 'f': /* print the stack */ for( i = stackidx-1 ; i >= 0; i-- ) { printf("[%2d]: ", i ); mpi_print(stdout, stack[i], 1 ); putchar('\n'); } break; default: fputs("invalid operator\n", stderr); } } } else if( state == 1 ) { /* in a number */ if( !isxdigit(c) ) { /* store the number */ state = 0; ungetc(c, stdin); if( stridx < 1000 ) strbuf[stridx] = 0; if( stackidx < STACKSIZE ) { if( !stack[stackidx] ) stack[stackidx] = mpi_alloc(10); if( mpi_fromstr(stack[stackidx], strbuf) ) fputs("invalid number\n", stderr); else stackidx++; } else fputs("stack overflow\n", stderr); } else { /* store digit */ if( stridx < 999 ) strbuf[stridx++] = c; else if( stridx == 999 ) { strbuf[stridx] = 0; fputs("string too large - truncated\n", stderr); stridx++; } } } } for(i=0; i < stackidx; i++ ) mpi_free(stack[i]); return 0; } diff --git a/tools/shmtest.c b/tools/shmtest.c index 76513cbcd..7bf5aa25d 100644 --- a/tools/shmtest.c +++ b/tools/shmtest.c @@ -1,201 +1,202 @@ /* shmtest.c * Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc. * * This file is free software; as a special exception the author gives * unlimited permission to copy and/or distribute it, with or without * modifications, as long as this notice is preserved. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY, to the extent permitted by law; without even the * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */ #include #include #include #include #include #include #include #ifdef HAVE_SYS_IPC_H #include #include #endif #ifdef HAVE_SYS_SHM_H #include #endif +#define INCLUDED_BY_MAIN_MODULE 1 #include "util.h" #include "ttyio.h" #include "i18n.h" #ifdef HAVE_DOSISH_SYSTEM int main( int argc, char **argv ) { fprintf(stderr, "Sorry, not yet available for DOSish systems\n"); exit(1); } #else static int serverpid = -1; static void my_usage(void) { fprintf(stderr, "usage: shmtest gpg-command-line\n"); exit(1); } const char * strusage( int level ) { return default_strusage(level); } static void i18n_init(void) { #ifdef ENABLE_NLS setlocale( LC_ALL, "" ); bindtextdomain (PACKAGE, LOCALEDIR); textdomain( PACKAGE ); #endif } static void do_get_string( int mode, const char *keyword, byte *area, size_t areasize ) { size_t n, len; char *p=NULL; int yes=0; n = area[0] << 8 | area[1]; /* fixme: do some sanity checks here */ if( mode == 1 ) p = tty_get( keyword ); else if( mode == 3 ) p = tty_get_hidden( keyword ); else yes = tty_get_answer_is_yes( keyword ); if( p ) { len = strlen(p); memcpy( area+n+2, p, len ); area[n] = len >> 8; area[n+1] = len; xfree(p); } else { /* bool */ area[n] = 0; area[n+1] = 1; area[n+2] = yes; } area[3] = 1; /* we should better use a semaphore */ kill( serverpid, SIGUSR1 ); } int main(int argc, char **argv) { void *area = NULL; size_t areasize = 4096; int shm_id = -1; FILE *fp; char buf[200]; char *p, *p2; size_t n; int i; log_set_name("shmtest"); i18n_init(); #ifndef USE_SHM_COPROCESSING log_info("SHM_COPRPOCESSING is not available\n"); #else if( argc < 1 ) my_usage(); for(n=0,i=1; i < argc; i++ ) n += strlen(argv[i]) + 1; p = xmalloc( 100 + n ); strcpy( p, "../g10/gpg --status-fd 1 --run-as-shm-coprocess 0"); for(i=1; i < argc; i++ ) { strcat(p, " " ); strcat(p, argv[i] ); } fp = popen( p, "r" ); xfree( p ); if( !fp ) log_error("popen failed: %s\n", strerror(errno)); while ( fgets (buf, sizeof (buf) - 1, fp ) != NULL ) { size_t len = strlen(buf); if( len >= 9 && !memcmp( buf, "[GNUPG:] ", 9 ) ) { int word=0; int is_info = 0, is_get = 0; for( p = strtok(buf+9, " \n"); p ; p = strtok(NULL, " \n")) { word++; if( word==1 && !strcmp(p,"SHM_INFO") ) { if( !area ) is_info=1; else log_error("duplicate SHM_INFO ignored\n" ); } else if( is_info && (p2 = strchr(p, '=' )) ) { int val; *p2++ = 0; val = atoi(p2); /* should be atou() for some values */ if( !strcmp(p, "pv" ) ) { if( atoi(p2) != 1 ) log_fatal("invalid protocol version %d\n", val ); is_info = 2; } else if( !strcmp(p, "pid" ) ) serverpid = val; else if( !strcmp(p, "shmid" ) ) shm_id = val; } else if( word == 1 && !strcmp(p,"SHM_GET") ) is_get = 1; else if( word == 1 && !strcmp(p,"SHM_GET_BOOL") ) is_get = 2; else if( word == 1 && !strcmp(p,"SHM_GET_HIDDEN") ) is_get = 3; else if( word == 2 && is_get ) { do_get_string( is_get, p, area, areasize ); break; } else if( word == 1 ) log_info("Status: %s\n", p); } if( is_info ) { if( is_info < 2 ) log_fatal("SHM info without protocol version\n"); if( serverpid == -1 ) log_fatal("SHM info without server's pid\n"); if( shm_id == -1 ) log_fatal("SHM info without id\n"); log_info("Shared memory info: server=%d shm_id=%d\n", serverpid, shm_id); area = shmat( shm_id, 0, 0 ); if( area == (void*)-1 ) log_fatal("attach to shared memory failed: %s\n", strerror(errno)); } } else fputs (buf, stdout); } if( pclose(fp) ) log_error("pclose failed\n"); return 0; #endif } #endif