diff --git a/BUGS b/BUGS index e04347a7c..09429f55b 100644 --- a/BUGS +++ b/BUGS @@ -36,6 +36,7 @@ and after about half a day in the rsync snapshots. --> Solaris fixed. --> IRIX bug still there but someone should test this again! ( reports that it is still alive in 1.0.0) + (seems to be fixed now. wk 2000-01-14) [ *] #18 1999-05-27 0.9.7 rndunix hangs on hp/ux. The problem is related to my_plcose which is diff --git a/ChangeLog b/ChangeLog index 31c217388..b5139d51f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +Mon Jan 24 13:04:28 CET 2000 Werner Koch + + * jnlib/ : New. + + * configure.in: Do set development version when the version has + a dash in it. Suggested by Dave Dykstra. + Thu Dec 9 17:22:27 CET 1999 Werner Koch * acinclude.m4 (GNUPG_FIX_HDR_VERSION): New. diff --git a/Makefile.am b/Makefile.am index b7836126c..60fb64c88 100644 --- a/Makefile.am +++ b/Makefile.am @@ -17,7 +17,7 @@ else checks = checks endif -SUBDIRS = intl zlib util mpi cipher ${gcrypt} g10 po doc ${checks} +SUBDIRS = intl zlib jnlib util mpi cipher ${gcrypt} g10 po doc ${checks} EXTRA_DIST = README-alpha VERSION PROJECTS BUGS # gettext never gets it right, so we take here care of deleting the # symlink. my_clean_gcrypt is just a kludge until we can include diff --git a/README-alpha b/README-alpha new file mode 100644 index 000000000..1b5dffffe --- /dev/null +++ b/README-alpha @@ -0,0 +1,9 @@ +The 1.1 series are the current development branch of GnuPG. + + DO NOT USE IN A PRODUCTION ENVIRONMENT! + +The source may change quite often and may have serious +problems; it may even not compile on some machines. + +Use GnuPG from the stable 1.0.x series for real work. + diff --git a/cipher/ChangeLog b/cipher/ChangeLog index df0eeee12..33b04e4f2 100644 --- a/cipher/ChangeLog +++ b/cipher/ChangeLog @@ -1,3 +1,8 @@ +Mon Jan 24 13:04:28 CET 2000 Werner Koch + + * pubkey.c (pubkey_nbits): Removed and replaced by ... + (gcry_pk_get_nbits): this new one. + Wed Dec 8 21:58:32 CET 1999 Werner Koch * dsa.c: s/mpi_powm/gcry_mpi_powm/g diff --git a/cipher/pubkey.c b/cipher/pubkey.c index 86b797df2..949e3b9ca 100644 --- a/cipher/pubkey.c +++ b/cipher/pubkey.c @@ -196,6 +196,14 @@ setup_pubkey_table(void) pubkey_table[i].name = NULL; } +static void +release_mpi_array( MPI *array ) +{ + for( ; *array; array++ ) { + mpi_free(*array); + *array = NULL; + } +} /**************** * Try to load all modules and return true if new modules are available @@ -423,27 +431,6 @@ pubkey_get_nenc( int algo ) return 0; } -/**************** - * Get the number of nbits from the public key - * FIXME: This should also take a S-Expt but must be optimized in - * some way becuase it is used in keylistsings ans such (store it with the - * S-Exp as some private data?) - */ -unsigned -pubkey_nbits( int algo, MPI *pkey ) -{ - int i; - - do { - for(i=0; pubkey_table[i].name; i++ ) - if( pubkey_table[i].algo == algo ) - return (*pubkey_table[i].get_nbits)( algo, pkey ); - } while( load_pubkey_modules() ); - if( is_RSA(algo) ) /* we always wanna see the length of a key :-) */ - return mpi_get_nbits( pkey[0] ); - return 0; -} - int pubkey_generate( int algo, unsigned nbits, MPI *skey, MPI **retfactors ) @@ -605,14 +592,6 @@ pubkey_verify( int algo, MPI hash, MPI *data, MPI *pkey, } -static void -release_mpi_array( MPI *array ) -{ - for( ; *array; array++ ) { - mpi_free(*array); - *array = NULL; - } -} /**************** * Convert a S-Exp with either a private or a public key to our @@ -910,6 +889,41 @@ gcry_pk_verify( GCRY_SEXP s_sig, GCRY_SEXP s_hash, GCRY_SEXP s_pkey ) } +/**************** + * Get the number of nbits from the public key + * Hmmm: Should we have really this function or is it + * better to have a more general function to retrieve + * different propoerties of the key? + */ +unsigned int +gcry_pk_get_nbits( GCRY_SEXP key ) +{ + int rc, i, algo; + MPI *keyarr; + unsigned int nbits = 0; + + rc = sexp_to_key( key, 0, &keyarr, &algo ); + if( rc == GCRYERR_INV_OBJ ) + rc = sexp_to_key( key, 0, &keyarr, &algo ); + if( rc ) + return 0; + + do { + for(i=0; pubkey_table[i].name; i++ ) + if( pubkey_table[i].algo == algo ) { + nbits = (*pubkey_table[i].get_nbits)( algo, keyarr ); + goto leave; + } + } while( load_pubkey_modules() ); + if( is_RSA(algo) ) /* we always wanna see the length of a key :-) */ + nbits = mpi_get_nbits( keyarr[0] ); + leave: + release_mpi_array( keyarr ); + return nbits; +} + + + int gcry_pk_ctl( int cmd, void *buffer, size_t buflen) { diff --git a/configure.in b/configure.in index 2651b87ab..ff584f533 100644 --- a/configure.in +++ b/configure.in @@ -667,7 +667,7 @@ AC_SUBST(ZLIBS) changequote(,)dnl tmp_pat='[a-zA-Z]' changequote([,])dnl -if echo "$VERSION" | grep "$tmp_pat" >/dev/null ; then +if echo "$VERSION" | sed 's/-.*//' | grep "$tmp_pat" >/dev/null ; then AC_DEFINE(IS_DEVELOPMENT_VERSION) fi @@ -720,6 +720,7 @@ AC_OUTPUT([ Makefile intl/Makefile po/Makefile.in +jnlib/Makefile util/Makefile mpi/Makefile cipher/Makefile diff --git a/doc/Makefile.am b/doc/Makefile.am index 9d06f9bd7..39fabf9b4 100644 --- a/doc/Makefile.am +++ b/doc/Makefile.am @@ -17,6 +17,8 @@ else endif +gcryptref.dvi : gcryptref.sgml + %.dvi: %.sgml db2dvi $< diff --git a/g10/ChangeLog b/g10/ChangeLog index 45f6af4db..e73bae4c1 100644 --- a/g10/ChangeLog +++ b/g10/ChangeLog @@ -1,3 +1,16 @@ +Mon Jan 24 13:04:28 CET 2000 Werner Koch + + * misc.c (mpi_print): Use gcry_mpi_aprint. + (pubkey_nbits): Kludge to use the gcry_pk_ API. + + * seskey.c (encode_session_key): Replaced mpi_set_buffer by *_scan. + (do_encode_md): Ditto. + (encode_md_value): Ditto. + * seckey-cert.c (protect_secret_key): Ditto. + * comment.c (make_mpi_comment_node): Replaced mpi_get_buffer by _print. + * pubkey-enc.c (get_it): Ditto. + * sig-check.c (do_signature_check): Ditto. + Fri Dec 31 12:48:31 CET 1999 Werner Koch * keyid.c (do_fingerprint_md): Replaced mpi_get_buffer by gcry_mpi_print. diff --git a/g10/Makefile.am b/g10/Makefile.am index 7c2450f12..f0f6068c5 100644 --- a/g10/Makefile.am +++ b/g10/Makefile.am @@ -4,7 +4,7 @@ INCLUDES = -I$(top_srcdir)/include -I$(top_srcdir)/intl EXTRA_DIST = OPTIONS pubring.asc options.skel OMIT_DEPENDENCIES = zlib.h zconf.h LDFLAGS = @LDFLAGS@ @DYNLINK_LDFLAGS@ -needed_libs = ../util/libutil.la ../gcrypt/libgcrypt.la +needed_libs = ../util/libutil.la ../gcrypt/libgcrypt.la ../jnlib/libjnlib.la #noinst_PROGRAMS = gpgd bin_PROGRAMS = gpg diff --git a/g10/armor.c b/g10/armor.c index 192a4188c..828113b0c 100644 --- a/g10/armor.c +++ b/g10/armor.c @@ -28,7 +28,7 @@ #include "errors.h" #include "iobuf.h" -#include "memory.h" +#include #include "util.h" #include "filter.h" #include "packet.h" @@ -1014,7 +1014,7 @@ armor_filter( void *opaque, int control, if( afx->qp_detected ) log_error(_("quoted printable character in armor - " "probably a buggy MTA has been used\n") ); - m_free( afx->buffer ); + gcry_free( afx->buffer ); afx->buffer = NULL; } else if( control == IOBUFCTRL_DESC ) @@ -1031,7 +1031,7 @@ make_radix64_string( const byte *data, size_t len ) { char *buffer, *p; - buffer = p = m_alloc( (len+2)/3*4 + 1 ); + buffer = p = gcry_xmalloc( (len+2)/3*4 + 1 ); for( ; len >= 3 ; len -= 3, data += 3 ) { *p++ = bintoasc[(data[0] >> 2) & 077]; *p++ = bintoasc[(((data[0] <<4)&060)|((data[1] >> 4)&017))&077]; diff --git a/g10/build-packet.c b/g10/build-packet.c index 84ca3a8db..34fd4ae1d 100644 --- a/g10/build-packet.c +++ b/g10/build-packet.c @@ -29,7 +29,7 @@ #include "iobuf.h" #include "util.h" #include "dummy-cipher.h" -#include "memory.h" +#include #include "options.h" #include "main.h" @@ -667,16 +667,16 @@ build_sig_subpkt( PKT_signature *sig, sigsubpkttype_t type, | sig->hashed_data[1]) : 0; n = n0 + nlen + 1 + buflen; /* length, type, buffer */ realloced = !!sig->hashed_data; - data = sig->hashed_data ? m_realloc( sig->hashed_data, n+2 ) - : m_alloc( n+2 ); + data = sig->hashed_data ? gcry_xrealloc( sig->hashed_data, n+2 ) + : gcry_xmalloc( n+2 ); } else { n0 = sig->unhashed_data ? ((*sig->unhashed_data << 8) | sig->unhashed_data[1]) : 0; n = n0 + nlen + 1 + buflen; /* length, type, buffer */ realloced = !!sig->unhashed_data; - data = sig->unhashed_data ? m_realloc( sig->unhashed_data, n+2 ) - : m_alloc( n+2 ); + data = sig->unhashed_data ? gcry_xrealloc( sig->unhashed_data, n+2 ) + : gcry_xmalloc( n+2 ); } if( critical ) @@ -707,12 +707,12 @@ build_sig_subpkt( PKT_signature *sig, sigsubpkttype_t type, if( hashed ) { if( !realloced ) - m_free(sig->hashed_data); + gcry_free(sig->hashed_data); sig->hashed_data = data; } else { if( !realloced ) - m_free(sig->unhashed_data); + gcry_free(sig->unhashed_data); sig->unhashed_data = data; } } diff --git a/g10/cipher.c b/g10/cipher.c index ef4c86502..48dde31cc 100644 --- a/g10/cipher.c +++ b/g10/cipher.c @@ -28,7 +28,6 @@ #include #include "errors.h" #include "iobuf.h" -#include "memory.h" #include "util.h" #include "filter.h" #include "packet.h" diff --git a/g10/comment.c b/g10/comment.c index 6d27e481b..f0abd0bac 100644 --- a/g10/comment.c +++ b/g10/comment.c @@ -29,7 +29,7 @@ #include "packet.h" #include "errors.h" #include "iobuf.h" -#include "memory.h" +#include #include "util.h" #include "main.h" #include "keydb.h" @@ -45,13 +45,13 @@ write_comment( IOBUF out, const char *s ) pkt.pkttype = PKT_COMMENT; if( *s != '#' ) { - pkt.pkt.comment = m_alloc( sizeof *pkt.pkt.comment + n ); + pkt.pkt.comment = gcry_xmalloc( sizeof *pkt.pkt.comment + n ); pkt.pkt.comment->len = n+1; *pkt.pkt.comment->data = '#'; strcpy(pkt.pkt.comment->data+1, s); } else { - pkt.pkt.comment = m_alloc( sizeof *pkt.pkt.comment + n - 1 ); + pkt.pkt.comment = gcry_xmalloc( sizeof *pkt.pkt.comment + n - 1 ); pkt.pkt.comment->len = n; strcpy(pkt.pkt.comment->data, s); } @@ -68,9 +68,9 @@ make_comment_node( const char *s ) PACKET *pkt; size_t n = strlen(s); - pkt = m_alloc_clear( sizeof *pkt ); + pkt = gcry_xcalloc( 1, sizeof *pkt ); pkt->pkttype = PKT_COMMENT; - pkt->pkt.comment = m_alloc( sizeof *pkt->pkt.comment + n - 1 ); + pkt->pkt.comment = gcry_xmalloc( sizeof *pkt->pkt.comment + n - 1 ); pkt->pkt.comment->len = n; strcpy(pkt->pkt.comment->data, s); return new_kbnode( pkt ); @@ -81,22 +81,20 @@ KBNODE make_mpi_comment_node( const char *s, MPI a ) { PACKET *pkt; - byte *buf, *p, *pp; - unsigned n1, nb1; + char *buf, *pp; + unsigned n1; size_t n = strlen(s); - nb1 = mpi_get_nbits( a ); - p = buf = mpi_get_buffer( a, &n1, NULL ); - pkt = m_alloc_clear( sizeof *pkt ); + if( gcry_mpi_aprint( GCRYMPI_FMT_PGP, &buf, &n1, a ) ) + BUG(); + pkt = gcry_xcalloc( 1, sizeof *pkt ); pkt->pkttype = PKT_COMMENT; - pkt->pkt.comment = m_alloc( sizeof *pkt->pkt.comment + n + 2 + n1 ); + pkt->pkt.comment = gcry_xmalloc( sizeof *pkt->pkt.comment + n + 1 + n1 ); pkt->pkt.comment->len = n+1+2+n1; pp = pkt->pkt.comment->data; memcpy(pp, s, n+1); - pp[n+1] = nb1 >> 8; - pp[n+2] = nb1 ; - memcpy(pp+n+3, p, n1 ); - m_free(buf); + memcpy(pp+n+1, buf, n1 ); + gcry_free(buf); return new_kbnode( pkt ); } diff --git a/g10/compress.c b/g10/compress.c index 4862346ad..478da5684 100644 --- a/g10/compress.c +++ b/g10/compress.c @@ -28,7 +28,7 @@ #include #include "util.h" -#include "memory.h" +#include #include "packet.h" #include "filter.h" #include "options.h" @@ -63,7 +63,7 @@ init_compress( compress_filter_context_t *zfx, z_stream *zs ) } zfx->outbufsize = 8192; - zfx->outbuf = m_alloc( zfx->outbufsize ); + zfx->outbuf = gcry_xmalloc( zfx->outbufsize ); } static int @@ -121,7 +121,7 @@ init_uncompress( compress_filter_context_t *zfx, z_stream *zs ) } zfx->inbufsize = 2048; - zfx->inbuf = m_alloc( zfx->inbufsize ); + zfx->inbuf = gcry_xmalloc( zfx->inbufsize ); zs->avail_in = 0; } @@ -196,7 +196,7 @@ compress_filter( void *opaque, int control, if( control == IOBUFCTRL_UNDERFLOW ) { if( !zfx->status ) { - zs = zfx->opaque = m_alloc_clear( sizeof *zs ); + zs = zfx->opaque = gcry_xcalloc( 1, sizeof *zs ); init_uncompress( zfx, zs ); zfx->status = 1; } @@ -221,7 +221,7 @@ compress_filter( void *opaque, int control, pkt.pkt.compressed = &cd; if( build_packet( a, &pkt )) log_bug("build_packet(PKT_COMPRESSED) failed\n"); - zs = zfx->opaque = m_alloc_clear( sizeof *zs ); + zs = zfx->opaque = gcry_xcalloc( 1, sizeof *zs ); init_compress( zfx, zs ); zfx->status = 2; } @@ -233,18 +233,18 @@ compress_filter( void *opaque, int control, else if( control == IOBUFCTRL_FREE ) { if( zfx->status == 1 ) { inflateEnd(zs); - m_free(zs); + gcry_free(zs); zfx->opaque = NULL; - m_free(zfx->outbuf); zfx->outbuf = NULL; + gcry_free(zfx->outbuf); zfx->outbuf = NULL; } else if( zfx->status == 2 ) { zs->next_in = buf; zs->avail_in = 0; do_compress( zfx, zs, Z_FINISH, a ); deflateEnd(zs); - m_free(zs); + gcry_free(zs); zfx->opaque = NULL; - m_free(zfx->outbuf); zfx->outbuf = NULL; + gcry_free(zfx->outbuf); zfx->outbuf = NULL; } } else if( control == IOBUFCTRL_DESC ) diff --git a/g10/dearmor.c b/g10/dearmor.c index e87dffcd9..70255629c 100644 --- a/g10/dearmor.c +++ b/g10/dearmor.c @@ -27,7 +27,7 @@ #include "errors.h" #include "iobuf.h" -#include "memory.h" +#include #include "util.h" #include "filter.h" #include "packet.h" diff --git a/g10/decrypt.c b/g10/decrypt.c index df6b0a7c0..c742a92d4 100644 --- a/g10/decrypt.c +++ b/g10/decrypt.c @@ -30,7 +30,7 @@ #include "errors.h" #include "iobuf.h" #include "keydb.h" -#include "memory.h" +#include #include "util.h" #include "main.h" #include "i18n.h" diff --git a/g10/delkey.c b/g10/delkey.c index a324fd631..9de3a7b08 100644 --- a/g10/delkey.c +++ b/g10/delkey.c @@ -31,7 +31,7 @@ #include "errors.h" #include "iobuf.h" #include "keydb.h" -#include "memory.h" +#include #include "util.h" #include "main.h" #include "trustdb.h" @@ -125,7 +125,7 @@ delete_key( const char *username, int secret ) keyid[1], datestr_from_pk(pk) ); p = get_user_id( keyid, &n ); tty_print_utf8_string( p, n ); - m_free(p); + gcry_free(p); tty_printf("\n\n"); yes = cpr_get_answer_is_yes( secret? "delete_key.secret.okay" diff --git a/g10/encode.c b/g10/encode.c index 15761d09d..e4d701e30 100644 --- a/g10/encode.c +++ b/g10/encode.c @@ -30,7 +30,7 @@ #include "errors.h" #include "iobuf.h" #include "keydb.h" -#include "memory.h" +#include #include "util.h" #include "main.h" #include "filter.h" @@ -98,7 +98,7 @@ encode_simple( const char *filename, int mode ) cfx.dek = NULL; if( mode ) { - s2k = m_alloc_clear( sizeof *s2k ); + s2k = gcry_xcalloc( 1, sizeof *s2k ); s2k->mode = opt.rfc1991? 0:opt.s2k_mode; s2k->hash_algo = opt.def_digest_algo ? opt.def_digest_algo : opt.s2k_digest_algo; @@ -107,8 +107,8 @@ encode_simple( const char *filename, int mode ) : opt.s2k_cipher_algo , s2k, 2 ); if( !cfx.dek || !cfx.dek->keylen ) { rc = G10ERR_PASSPHRASE; - m_free(cfx.dek); - m_free(s2k); + gcry_free(cfx.dek); + gcry_free(s2k); iobuf_close(inp); log_error(_("error creating passphrase: %s\n"), g10_errstr(rc) ); return rc; @@ -117,8 +117,8 @@ encode_simple( const char *filename, int mode ) if( (rc = open_outfile( filename, opt.armor? 1:0, &out )) ) { iobuf_close(inp); - m_free(cfx.dek); - m_free(s2k); + gcry_free(cfx.dek); + gcry_free(s2k); return rc; } @@ -133,7 +133,7 @@ encode_simple( const char *filename, int mode ) } #endif if( s2k && !opt.rfc1991 ) { - PKT_symkey_enc *enc = m_alloc_clear( sizeof *enc ); + PKT_symkey_enc *enc = gcry_xcalloc( 1, sizeof *enc ); enc->version = 4; enc->cipher_algo = cfx.dek->algo; enc->s2k = *s2k; @@ -141,7 +141,7 @@ encode_simple( const char *filename, int mode ) pkt.pkt.symkey_enc = enc; if( (rc = build_packet( out, &pkt )) ) log_error("build symkey packet failed: %s\n", g10_errstr(rc) ); - m_free(enc); + gcry_free(enc); } if (!opt.no_literal) { @@ -149,13 +149,13 @@ encode_simple( const char *filename, int mode ) if( filename || opt.set_filename ) { char *s = make_basename( opt.set_filename ? opt.set_filename : filename ); - pt = m_alloc( sizeof *pt + strlen(s) - 1 ); + pt = gcry_xmalloc( sizeof *pt + strlen(s) - 1 ); pt->namelen = strlen(s); memcpy(pt->name, s, pt->namelen ); - m_free(s); + gcry_free(s); } else { /* no filename */ - pt = m_alloc( sizeof *pt - 1 ); + pt = gcry_xmalloc( sizeof *pt - 1 ); pt->namelen = 0; } } @@ -221,8 +221,8 @@ encode_simple( const char *filename, int mode ) if (pt) pt->buf = NULL; free_packet(&pkt); - m_free(cfx.dek); - m_free(s2k); + gcry_free(cfx.dek); + gcry_free(s2k); return rc; } @@ -283,7 +283,7 @@ encode_crypt( const char *filename, STRLIST remusr ) } #endif /* create a session key */ - cfx.dek = m_alloc_secure( sizeof *cfx.dek ); + cfx.dek = gcry_xmalloc_secure( sizeof *cfx.dek ); if( !opt.def_cipher_algo ) { /* try to get it from the prefs */ cfx.dek->algo = select_algo_from_prefs( pk_list, PREFTYPE_SYM ); if( cfx.dek->algo == -1 ) @@ -303,13 +303,13 @@ encode_crypt( const char *filename, STRLIST remusr ) /* setup the inner packet */ if( filename || opt.set_filename ) { char *s = make_basename( opt.set_filename ? opt.set_filename : filename ); - pt = m_alloc( sizeof *pt + strlen(s) - 1 ); + pt = gcry_xmalloc( sizeof *pt + strlen(s) - 1 ); pt->namelen = strlen(s); memcpy(pt->name, s, pt->namelen ); - m_free(s); + gcry_free(s); } else { /* no filename */ - pt = m_alloc( sizeof *pt - 1 ); + pt = gcry_xmalloc( sizeof *pt - 1 ); pt->namelen = 0; } } @@ -377,7 +377,7 @@ encode_crypt( const char *filename, STRLIST remusr ) if( pt ) pt->buf = NULL; free_packet(&pkt); - m_free(cfx.dek); + gcry_free(cfx.dek); release_pk_list( pk_list ); return rc; } @@ -401,7 +401,7 @@ encrypt_filter( void *opaque, int control, } else if( control == IOBUFCTRL_FLUSH ) { /* encrypt */ if( !efx->header_okay ) { - efx->cfx.dek = m_alloc_secure( sizeof *efx->cfx.dek ); + efx->cfx.dek = gcry_xmalloc_secure( sizeof *efx->cfx.dek ); if( !opt.def_cipher_algo ) { /* try to get it from the prefs */ efx->cfx.dek->algo = @@ -453,7 +453,7 @@ write_pubkey_enc_from_list( PK_LIST pk_list, DEK *dek, IOBUF out ) pk = pk_list->pk; print_pubkey_algo_note( pk->pubkey_algo ); - enc = m_alloc_clear( sizeof *enc ); + enc = gcry_xcalloc( 1, sizeof *enc ); enc->pubkey_algo = pk->pubkey_algo; keyid_from_pk( pk, enc->keyid ); enc->throw_keyid = opt.throw_keyid; @@ -483,7 +483,7 @@ write_pubkey_enc_from_list( PK_LIST pk_list, DEK *dek, IOBUF out ) log_info(_("%s/%s encrypted for: %s\n"), gcry_pk_algo_name(enc->pubkey_algo), gcry_cipher_algo_name(dek->algo), ustr ); - m_free(ustr); + gcry_free(ustr); } /* and write it */ init_packet(&pkt); diff --git a/g10/encr-data.c b/g10/encr-data.c index c4c9ff2f0..286475003 100644 --- a/g10/encr-data.c +++ b/g10/encr-data.c @@ -25,7 +25,6 @@ #include #include #include "util.h" -#include "memory.h" #include "packet.h" #include "dummy-cipher.h" #include "options.h" diff --git a/g10/export.c b/g10/export.c index 911a71599..4021c050f 100644 --- a/g10/export.c +++ b/g10/export.c @@ -29,7 +29,7 @@ #include "packet.h" #include "errors.h" #include "keydb.h" -#include "memory.h" +#include #include "util.h" #include "main.h" #include "i18n.h" diff --git a/g10/free-packet.c b/g10/free-packet.c index 93c93de19..56a65d7ae 100644 --- a/g10/free-packet.c +++ b/g10/free-packet.c @@ -28,14 +28,14 @@ #include "iobuf.h" #include "util.h" #include "dummy-cipher.h" -#include "memory.h" +#include #include "options.h" #include "main.h" void free_symkey_enc( PKT_symkey_enc *enc ) { - m_free(enc); + gcry_free(enc); } void @@ -47,7 +47,7 @@ free_pubkey_enc( PKT_pubkey_enc *enc ) mpi_release(enc->data[0]); for(i=0; i < n; i++ ) mpi_release( enc->data[i] ); - m_free(enc); + gcry_free(enc); } void @@ -59,9 +59,9 @@ free_seckey_enc( PKT_signature *sig ) mpi_release(sig->data[0]); for(i=0; i < n; i++ ) mpi_release( sig->data[i] ); - m_free(sig->hashed_data); - m_free(sig->unhashed_data); - m_free(sig); + gcry_free(sig->hashed_data); + gcry_free(sig->unhashed_data); + gcry_free(sig); } @@ -78,7 +78,7 @@ release_public_key_parts( PKT_public_key *pk ) pk->pkey[i] = NULL; } if( pk->namehash ) { - m_free(pk->namehash); + gcry_free(pk->namehash); pk->namehash = NULL; } } @@ -88,7 +88,7 @@ void free_public_key( PKT_public_key *pk ) { release_public_key_parts( pk ); - m_free(pk); + gcry_free(pk); } @@ -101,7 +101,7 @@ cp_data_block( byte *s ) if( !s ) return NULL; len = (s[0] << 8) | s[1]; - d = m_alloc( len+2 ); + d = gcry_xmalloc( len+2 ); memcpy(d, s, len+2); return d; } @@ -114,14 +114,14 @@ copy_public_key_new_namehash( PKT_public_key *d, PKT_public_key *s, int n, i; if( !d ) - d = m_alloc(sizeof *d); + d = gcry_xmalloc(sizeof *d); memcpy( d, s, sizeof *d ); if( namehash ) { - d->namehash = m_alloc( 20 ); + d->namehash = gcry_xmalloc( 20 ); memcpy(d->namehash, namehash, 20 ); } else if( s->namehash ) { - d->namehash = m_alloc( 20 ); + d->namehash = gcry_xmalloc( 20 ); memcpy(d->namehash, s->namehash, 20 ); } n = pubkey_get_npkey( s->pubkey_algo ); @@ -146,7 +146,7 @@ copy_signature( PKT_signature *d, PKT_signature *s ) int n, i; if( !d ) - d = m_alloc(sizeof *d); + d = gcry_xmalloc(sizeof *d); memcpy( d, s, sizeof *d ); n = pubkey_get_nsig( s->pubkey_algo ); if( !n ) @@ -165,7 +165,7 @@ PKT_user_id * copy_user_id( PKT_user_id *d, PKT_user_id *s ) { if( !d ) - d = m_alloc(sizeof *d + s->len - 1 ); + d = gcry_xmalloc(sizeof *d + s->len - 1 ); memcpy( d, s, sizeof *d + s->len - 1 ); return d; } @@ -190,7 +190,7 @@ void free_secret_key( PKT_secret_key *sk ) { release_secret_key_parts( sk ); - m_free(sk); + gcry_free(sk); } PKT_secret_key * @@ -199,7 +199,7 @@ copy_secret_key( PKT_secret_key *d, PKT_secret_key *s ) int n, i; if( !d ) - d = m_alloc(sizeof *d); + d = gcry_xmalloc(sizeof *d); memcpy( d, s, sizeof *d ); n = pubkey_get_nskey( s->pubkey_algo ); if( !n ) @@ -214,13 +214,13 @@ copy_secret_key( PKT_secret_key *d, PKT_secret_key *s ) void free_comment( PKT_comment *rem ) { - m_free(rem); + gcry_free(rem); } void free_user_id( PKT_user_id *uid ) { - m_free(uid); + gcry_free(uid); } void @@ -232,7 +232,7 @@ free_compressed( PKT_compressed *zd ) while( iobuf_read( zd->buf, NULL, 1<<30 ) != -1 ) ; } - m_free(zd); + gcry_free(zd); } void @@ -253,7 +253,7 @@ free_encrypted( PKT_encrypted *ed ) } } } - m_free(ed); + gcry_free(ed); } @@ -275,7 +275,7 @@ free_plaintext( PKT_plaintext *pt ) } } } - m_free(pt); + gcry_free(pt); } /**************** @@ -324,7 +324,7 @@ free_packet( PACKET *pkt ) free_plaintext( pkt->pkt.plaintext ); break; default: - m_free( pkt->pkt.generic ); + gcry_free( pkt->pkt.generic ); break; } pkt->pkt.generic = NULL; diff --git a/g10/g10.c b/g10/g10.c index 871f31026..192ac4ae4 100644 --- a/g10/g10.c +++ b/g10/g10.c @@ -30,7 +30,6 @@ #include "packet.h" #include "iobuf.h" -#include "memory.h" #include "util.h" #include "main.h" #include "options.h" @@ -422,13 +421,14 @@ build_list( const char *text, const char * (*mapf)(int), int (*chkf)(int) ) size_t n=strlen(text)+2; char *list, *p; - if( maybe_setuid ) - secmem_init( 0 ); /* drop setuid */ + if( maybe_setuid ) { + gcry_control( GCRYCTL_DROP_PRIVS ); /* drop setuid */ + } for(i=1; i < 110; i++ ) if( !chkf(i) ) n += strlen(mapf(i)) + 2; - list = m_alloc( 21 + n ); *list = 0; + list = gcry_xmalloc( 21 + n ); *list = 0; for(p=NULL, i=1; i < 110; i++ ) { if( !chkf(i) ) { if( !p ) @@ -480,7 +480,7 @@ make_username( const char *string ) if( utf8_strings ) p = native_to_utf8( string ); else - p = m_strdup(string); + p = gcry_xstrdup(string); return p; } @@ -498,7 +498,7 @@ register_extension( const char *mainpgm, const char *fname ) else tmp = make_filename(GNUPG_LIBDIR, fname, NULL); register_cipher_extension( mainpgm, tmp ); - m_free(tmp); + gcry_free(tmp); } else register_cipher_extension( mainpgm, fname ); @@ -510,10 +510,13 @@ register_extension( const char *mainpgm, const char *fname ) static void set_debug(void) { + #if 0 + #warning memory debuggig not enabled if( opt.debug & DBG_MEMORY_VALUE ) memory_debug_mode = 1; if( opt.debug & DBG_MEMSTAT_VALUE ) memory_stat_debug_mode = 1; + #endif if( opt.debug & DBG_MPI_VALUE ) gcry_control( GCRYCTL_SET_DEBUG_FLAGS, 2 ); @@ -588,7 +591,7 @@ main( int argc, char **argv ) trap_unaligned(); set_strusage( my_strusage ); - secmem_set_flags( secmem_get_flags() | 2 ); /* suspend warnings */ + gcry_control( GCRYCTL_SUSPEND_SECMEM_WARN ); /* Please note that we may running SUID(ROOT), so be very CAREFUL * when adding any stuff between here and the call to * secmem_init() somewhere after the option parsing @@ -667,7 +670,7 @@ main( int argc, char **argv ) } #endif /* initialize the secure memory. */ - secmem_init( 16384 ); + gcry_control( GCRYCTL_INIT_SECMEM, 16384, 0 ); maybe_setuid = 0; /* Okay, we are now working under our real uid */ @@ -695,7 +698,7 @@ main( int argc, char **argv ) configname, strerror(errno) ); g10_exit(2); } - m_free(configname); configname = NULL; + gcry_free(configname); configname = NULL; } if( parse_debug && configname ) log_info(_("reading options from `%s'\n"), configname ); @@ -774,8 +777,8 @@ main( int argc, char **argv ) case oOptions: /* config files may not be nested (silently ignore them) */ if( !configfp ) { - m_free(configname); - configname = m_strdup(pargs.r.ret_str); + gcry_free(configname); + configname = gcry_xstrdup(pargs.r.ret_str); goto next_pass; } break; @@ -799,11 +802,11 @@ main( int argc, char **argv ) opt.def_recipient = make_username(pargs.r.ret_str); break; case oDefRecipientSelf: - m_free(opt.def_recipient); opt.def_recipient = NULL; + gcry_free(opt.def_recipient); opt.def_recipient = NULL; opt.def_recipient_self = 1; break; case oNoDefRecipient: - m_free(opt.def_recipient); opt.def_recipient = NULL; + gcry_free(opt.def_recipient); opt.def_recipient = NULL; opt.def_recipient_self = 0; break; case oNoOptions: break; /* no-options */ @@ -856,8 +859,8 @@ main( int argc, char **argv ) case oForceV3Sigs: opt.force_v3_sigs = 1; break; case oForceMDC: opt.force_mdc = 1; break; case oS2KMode: opt.s2k_mode = pargs.r.ret_int; break; - case oS2KDigest: s2k_digest_string = m_strdup(pargs.r.ret_str); break; - case oS2KCipher: s2k_cipher_string = m_strdup(pargs.r.ret_str); break; + case oS2KDigest: s2k_digest_string = gcry_xstrdup(pargs.r.ret_str); break; + case oS2KCipher: s2k_cipher_string = gcry_xstrdup(pargs.r.ret_str); break; case oNoEncryptTo: opt.no_encrypt_to = 1; break; case oEncryptTo: /* store the recipient in the second list */ @@ -874,9 +877,9 @@ main( int argc, char **argv ) break; case oCompress: opt.compress = pargs.r.ret_int; break; case oPasswdFD: pwfd = pargs.r.ret_int; break; - case oCipherAlgo: def_cipher_string = m_strdup(pargs.r.ret_str); break; - case oDigestAlgo: def_digest_string = m_strdup(pargs.r.ret_str); break; - case oNoSecmemWarn: secmem_set_flags( secmem_get_flags() | 1 ); break; + case oCipherAlgo: def_cipher_string = gcry_xstrdup(pargs.r.ret_str); break; + case oDigestAlgo: def_digest_string = gcry_xstrdup(pargs.r.ret_str); break; + case oNoSecmemWarn: gcry_control( GCRYCTL_DISABLE_SECMEM_WARN ); break; case oCharset: if( set_native_charset( pargs.r.ret_str ) ) log_error(_("%s is not a valid character set\n"), @@ -925,10 +928,10 @@ main( int argc, char **argv ) if( configfp ) { fclose( configfp ); configfp = NULL; - m_free(configname); configname = NULL; + gcry_free(configname); configname = NULL; goto next_pass; } - m_free( configname ); configname = NULL; + gcry_free( configname ); configname = NULL; if( log_get_errorcount(0) ) g10_exit(2); if( nogreeting ) @@ -962,7 +965,7 @@ main( int argc, char **argv ) if( opt.batch ) tty_batchmode( 1 ); - secmem_set_flags( secmem_get_flags() & ~2 ); /* resume warnings */ + gcry_control( GCRYCTL_RESUME_SECMEM_WARN ); set_debug(); /* FIXME: should set filenames of libgcrypt explicitly @@ -972,25 +975,25 @@ main( int argc, char **argv ) * may try to load a module */ if( def_cipher_string ) { opt.def_cipher_algo = gcry_cipher_map_name(def_cipher_string); - m_free(def_cipher_string); def_cipher_string = NULL; + gcry_free(def_cipher_string); def_cipher_string = NULL; if( openpgp_cipher_test_algo(opt.def_cipher_algo) ) log_error(_("selected cipher algorithm is invalid\n")); } if( def_digest_string ) { opt.def_digest_algo = gcry_md_map_name(def_digest_string); - m_free(def_digest_string); def_digest_string = NULL; + gcry_free(def_digest_string); def_digest_string = NULL; if( openpgp_md_test_algo(opt.def_digest_algo) ) log_error(_("selected digest algorithm is invalid\n")); } if( s2k_cipher_string ) { opt.s2k_cipher_algo = gcry_cipher_map_name(s2k_cipher_string); - m_free(s2k_cipher_string); s2k_cipher_string = NULL; + gcry_free(s2k_cipher_string); s2k_cipher_string = NULL; if( openpgp_cipher_test_algo(opt.s2k_cipher_algo) ) log_error(_("selected cipher algorithm is invalid\n")); } if( s2k_digest_string ) { opt.s2k_digest_algo = gcry_md_map_name(s2k_digest_string); - m_free(s2k_digest_string); s2k_digest_string = NULL; + gcry_free(s2k_digest_string); s2k_digest_string = NULL; if( openpgp_md_test_algo(opt.s2k_digest_algo) ) log_error(_("selected digest algorithm is invalid\n")); } @@ -1125,7 +1128,7 @@ main( int argc, char **argv ) if( argc > 1 ) wrong_args(_("--sign [filename]")); if( argc ) { - sl = m_alloc_clear( sizeof *sl + strlen(fname)); + sl = gcry_xcalloc( 1, sizeof *sl + strlen(fname)); strcpy(sl->d, fname); } } @@ -1138,7 +1141,7 @@ main( int argc, char **argv ) if( argc > 1 ) wrong_args(_("--sign --encrypt [filename]")); if( argc ) { - sl = m_alloc_clear( sizeof *sl + strlen(fname)); + sl = gcry_xcalloc( 1, sizeof *sl + strlen(fname)); strcpy(sl->d, fname); } else @@ -1173,7 +1176,7 @@ main( int argc, char **argv ) wrong_args(_("--sign-key user-id")); username = make_username( fname ); keyedit_menu(fname, locusr, NULL, 1 ); - m_free(username); + gcry_free(username); break; case aLSignKey: @@ -1181,7 +1184,7 @@ main( int argc, char **argv ) wrong_args(_("--lsign-key user-id")); username = make_username( fname ); keyedit_menu(fname, locusr, NULL, 2 ); - m_free(username); + gcry_free(username); break; case aEditKey: /* Edit a key signature */ @@ -1197,7 +1200,7 @@ main( int argc, char **argv ) } else keyedit_menu(username, locusr, NULL, 0 ); - m_free(username); + gcry_free(username); break; case aDeleteSecretKey: @@ -1209,7 +1212,7 @@ main( int argc, char **argv ) username = make_username( fname ); if( (rc = delete_key(username, cmd==aDeleteSecretKey)) ) log_error("%s: delete key failed: %s\n", username, g10_errstr(rc) ); - m_free(username); + gcry_free(username); break; @@ -1294,7 +1297,7 @@ main( int argc, char **argv ) wrong_args("--gen-revoke user-id"); username = make_username(*argv); gen_revoke( username ); - m_free( username ); + gcry_free( username ); break; case aDeArmor: @@ -1365,7 +1368,7 @@ main( int argc, char **argv ) p = gcry_random_bytes( n, level ); fwrite( p, n, 1, stdout ); - m_free(p); + gcry_free(p); if( !endless ) count -= n; } @@ -1424,7 +1427,7 @@ main( int argc, char **argv ) for( ; argc; argc--, argv++ ) { username = make_username( *argv ); check_trustdb( username ); - m_free(username); + gcry_free(username); } } break; @@ -1441,7 +1444,7 @@ main( int argc, char **argv ) for( ; argc; argc--, argv++ ) { username = make_username( *argv ); list_trust_path( username ); - m_free(username); + gcry_free(username); } break; @@ -1501,12 +1504,12 @@ void g10_exit( int rc ) { if( opt.debug & DBG_MEMSTAT_VALUE ) { - m_print_stats("on exit"); + gcry_control( GCRYCTL_DUMP_MEMORY_STATS ); gcry_control( GCRYCTL_DUMP_RANDOM_STATS ); } if( opt.debug ) gcry_control( GCRYCTL_DUMP_SECMEM_STATS ); - secmem_term(); + gcry_control( GCRYCTL_TERM_SECMEM ); rc = rc? rc : log_get_errorcount(0)? 2 : g10_errors_seen? 1 : 0; /*write_status( STATUS_LEAVE );*/ @@ -1562,16 +1565,16 @@ print_mds( const char *fname, int algo ) if( !fname ) { fp = stdin; - pname = m_strdup("[stdin]: "); + pname = gcry_xstrdup("[stdin]: "); } else { - pname = m_alloc(strlen(fname)+3); + pname = gcry_xmalloc(strlen(fname)+3); strcpy(stpcpy(pname,fname),": "); fp = fopen( fname, "rb" ); } if( !fp ) { log_error("%s%s\n", pname, strerror(errno) ); - m_free(pname); + gcry_free(pname); return; } diff --git a/g10/getkey.c b/g10/getkey.c index a94b5de48..bf36fcb96 100644 --- a/g10/getkey.c +++ b/g10/getkey.c @@ -27,7 +27,6 @@ #include #include "util.h" #include "packet.h" -#include "memory.h" #include "iobuf.h" #include "keydb.h" #include "options.h" @@ -208,7 +207,7 @@ cache_public_key( PKT_public_key *pk ) return; } pk_cache_entries++; - ce = m_alloc( sizeof *ce ); + ce = gcry_xmalloc( sizeof *ce ); ce->next = pk_cache; pk_cache = ce; ce->pk = copy_public_key( NULL, pk ); @@ -237,10 +236,10 @@ cache_user_id( PKT_user_id *uid, u32 *keyid ) /* fixme: use another algorithm to free some cache slots */ r = user_id_db; user_id_db = r->next; - m_free(r); + gcry_free(r); uid_cache_entries--; } - r = m_alloc( sizeof *r + uid->len-1 ); + r = gcry_xmalloc( sizeof *r + uid->len-1 ); r->keyid[0] = keyid[0]; r->keyid[1] = keyid[1]; r->len = uid->len; @@ -259,7 +258,7 @@ getkey_disable_caches() keyid_list_t kl, kl2; for( kl = unknown_keyids; kl; kl = kl2 ) { kl2 = kl->next; - m_free(kl); + gcry_free(kl); } unknown_keyids = NULL; unk_cache_disabled = 1; @@ -272,7 +271,7 @@ getkey_disable_caches() for( ce = pk_cache; ce; ce = ce2 ) { ce2 = ce->next; free_public_key( ce->pk ); - m_free( ce ); + gcry_free( ce ); } pk_cache_disabled=1; pk_cache_entries = 0; @@ -317,7 +316,7 @@ get_pubkey( PKT_public_key *pk, u32 *keyid ) #endif /* more init stuff */ if( !pk ) { - pk = m_alloc_clear( sizeof *pk ); + pk = gcry_xcalloc( 1, sizeof *pk ); internal++; } @@ -348,7 +347,7 @@ get_pubkey( PKT_public_key *pk, u32 *keyid ) else { keyid_list_t kl; - kl = m_alloc( sizeof *kl ); + kl = gcry_xmalloc( sizeof *kl ); kl->keyid[0] = keyid[0]; kl->keyid[1] = keyid[1]; kl->next = unknown_keyids; @@ -369,7 +368,7 @@ get_pubkey( PKT_public_key *pk, u32 *keyid ) KBNODE get_pubkeyblock( u32 *keyid ) { - PKT_public_key *pk = m_alloc_clear( sizeof *pk ); + PKT_public_key *pk = gcry_xcalloc( 1, sizeof *pk ); struct getkey_ctx_s ctx; int rc = 0; KBNODE keyblock = NULL; @@ -454,7 +453,7 @@ seckey_available( u32 *keyid ) struct getkey_ctx_s ctx; PKT_secret_key *sk; - sk = m_alloc_clear( sizeof *sk ); + sk = gcry_xcalloc( 1, sizeof *sk ); memset( &ctx, 0, sizeof ctx ); ctx.not_allocated = 1; ctx.nitems = 1; @@ -704,7 +703,7 @@ key_byname( GETKEY_CTX *retctx, STRLIST namelist, /* and we don't have mode 6 */ for(n=0, r=namelist; r; r = r->next ) n++; - ctx = m_alloc_clear( sizeof *ctx + (n-1)*sizeof ctx->items ); + ctx = gcry_xcalloc( 1, sizeof *ctx + (n-1)*sizeof ctx->items ); ctx->nitems = n; for(n=0, r=namelist; r; r = r->next, n++ ) { @@ -714,7 +713,7 @@ key_byname( GETKEY_CTX *retctx, STRLIST namelist, &ctx->items[n].name, NULL ); if( !ctx->items[n].mode ) { - m_free( ctx ); + gcry_free( ctx ); return G10ERR_INV_USER_ID; } if( ctx->items[n].mode == 6 ) { @@ -736,8 +735,8 @@ key_byname( GETKEY_CTX *retctx, STRLIST namelist, /* Hmmm, why not get_pubkey-end here?? */ enum_keyblocks( 2, &ctx->kbpos, NULL ); /* close */ for(n=0; n < ctx->nitems; n++ ) - m_free( ctx->items[n].namebuf ); - m_free( ctx ); + gcry_free( ctx->items[n].namebuf ); + gcry_free( ctx ); } return rc; @@ -754,7 +753,7 @@ get_pubkey_byname( GETKEY_CTX *retctx, PKT_public_key *pk, if( !pk ) { /* Performance Hint: key_byname should not need a pk here */ - pk = m_alloc_clear( sizeof *pk ); + pk = gcry_xcalloc( 1, sizeof *pk ); rc = key_byname( retctx, namelist, pk, NULL, ret_keyblock ); free_public_key( pk ); } @@ -773,7 +772,7 @@ get_pubkey_bynames( GETKEY_CTX *retctx, PKT_public_key *pk, if( !pk ) { /* Performance Hint: key_byname should not need a pk here */ - pk = m_alloc_clear( sizeof *pk ); + pk = gcry_xcalloc( 1, sizeof *pk ); rc = key_byname( retctx, names, pk, NULL, ret_keyblock ); free_public_key( pk ); } @@ -790,7 +789,7 @@ get_pubkey_next( GETKEY_CTX ctx, PKT_public_key *pk, KBNODE *ret_keyblock ) if( !pk ) { /* Performance Hint: lookup_read should not need a pk in this case */ - pk = m_alloc_clear( sizeof *pk ); + pk = gcry_xcalloc( 1, sizeof *pk ); rc = lookup_pk( ctx, pk, ret_keyblock ); free_public_key( pk ); } @@ -807,9 +806,9 @@ get_pubkey_end( GETKEY_CTX ctx ) enum_keyblocks( 2, &ctx->kbpos, NULL ); /* close */ for(n=0; n < ctx->nitems; n++ ) - m_free( ctx->items[n].namebuf ); + gcry_free( ctx->items[n].namebuf ); if( !ctx->not_allocated ) - m_free( ctx ); + gcry_free( ctx ); } } @@ -845,7 +844,7 @@ get_keyblock_byfprint( KBNODE *ret_keyblock, const byte *fprint, size_t fprint_len ) { int rc; - PKT_public_key *pk = m_alloc_clear( sizeof *pk ); + PKT_public_key *pk = gcry_xcalloc( 1, sizeof *pk ); if( fprint_len == 20 || fprint_len == 16 ) { struct getkey_ctx_s ctx; @@ -873,7 +872,7 @@ int get_keyblock_bylid( KBNODE *ret_keyblock, ulong lid ) { int rc; - PKT_public_key *pk = m_alloc_clear( sizeof *pk ); + PKT_public_key *pk = gcry_xcalloc( 1, sizeof *pk ); struct getkey_ctx_s ctx; u32 kid[2]; @@ -942,7 +941,7 @@ get_seckey_bynames( GETKEY_CTX *retctx, PKT_secret_key *sk, if( !sk ) { /* Performance Hint: key_byname should not need a sk here */ - sk = m_alloc_secure_clear( sizeof *sk ); + sk = gcry_xcalloc_secure( 1, sizeof *sk ); rc = key_byname( retctx, names, NULL, sk, ret_keyblock ); free_secret_key( sk ); } @@ -960,7 +959,7 @@ get_seckey_next( GETKEY_CTX ctx, PKT_secret_key *sk, KBNODE *ret_keyblock ) if( !sk ) { /* Performance Hint: lookup_read should not need a pk in this case */ - sk = m_alloc_secure_clear( sizeof *sk ); + sk = gcry_xcalloc_secure( 1, sizeof *sk ); rc = lookup_sk( ctx, sk, ret_keyblock ); free_secret_key( sk ); } @@ -977,9 +976,9 @@ get_seckey_end( GETKEY_CTX ctx ) enum_keyblocks( 2, &ctx->kbpos, NULL ); /* close */ for(n=0; n < ctx->nitems; n++ ) - m_free( ctx->items[n].namebuf ); + gcry_free( ctx->items[n].namebuf ); if( !ctx->not_allocated ) - m_free( ctx ); + gcry_free( ctx ); } } @@ -1051,7 +1050,7 @@ prepare_word_match( const byte *name ) int c; /* the original length is always enough for the pattern */ - p = pattern = m_alloc(strlen(name)+1); + p = pattern = gcry_xmalloc(strlen(name)+1); do { /* skip leading delimiters */ while( *name && !word_match_chars[*name] ) @@ -1817,7 +1816,7 @@ enum_secret_keys( void **context, PKT_secret_key *sk, int with_subkeys ) if( !c ) { /* make a new context */ - c = m_alloc_clear( sizeof *c ); + c = gcry_xcalloc( 1, sizeof *c ); *context = c; c->sequence = 0; c->name = enum_keyblock_resources( &c->sequence, 1 ); @@ -1826,7 +1825,7 @@ enum_secret_keys( void **context, PKT_secret_key *sk, int with_subkeys ) if( !sk ) { /* free the context */ if( c->iobuf ) iobuf_close(c->iobuf); - m_free( c ); + gcry_free( c ); *context = NULL; return 0; } @@ -1878,12 +1877,12 @@ get_user_id_string( u32 *keyid ) do { for(r=user_id_db; r; r = r->next ) if( r->keyid[0] == keyid[0] && r->keyid[1] == keyid[1] ) { - p = m_alloc( r->len + 10 ); + p = gcry_xmalloc( r->len + 10 ); sprintf(p, "%08lX %.*s", (ulong)keyid[1], r->len, r->name ); return p; } } while( ++pass < 2 && !get_pubkey( NULL, keyid ) ); - p = m_alloc( 15 ); + p = gcry_xmalloc( 15 ); sprintf(p, "%08lX [?]", (ulong)keyid[1] ); return p; } @@ -1898,13 +1897,13 @@ get_long_user_id_string( u32 *keyid ) do { for(r=user_id_db; r; r = r->next ) if( r->keyid[0] == keyid[0] && r->keyid[1] == keyid[1] ) { - p = m_alloc( r->len + 20 ); + p = gcry_xmalloc( r->len + 20 ); sprintf(p, "%08lX%08lX %.*s", (ulong)keyid[0], (ulong)keyid[1], r->len, r->name ); return p; } } while( ++pass < 2 && !get_pubkey( NULL, keyid ) ); - p = m_alloc( 25 ); + p = gcry_xmalloc( 25 ); sprintf(p, "%08lX%08lX [?]", (ulong)keyid[0], (ulong)keyid[1] ); return p; } @@ -1919,13 +1918,13 @@ get_user_id( u32 *keyid, size_t *rn ) do { for(r=user_id_db; r; r = r->next ) if( r->keyid[0] == keyid[0] && r->keyid[1] == keyid[1] ) { - p = m_alloc( r->len ); + p = gcry_xmalloc( r->len ); memcpy(p, r->name, r->len ); *rn = r->len; return p; } } while( ++pass < 2 && !get_pubkey( NULL, keyid ) ); - p = m_alloc( 19 ); + p = gcry_xmalloc( 19 ); memcpy(p, "[User id not found]", 19 ); *rn = 19; return p; diff --git a/g10/gpgd.c b/g10/gpgd.c index 6ee87ec8a..50eb7db59 100644 --- a/g10/gpgd.c +++ b/g10/gpgd.c @@ -112,7 +112,7 @@ build_list( const char *text, const char * (*mapf)(int), int (*chkf)(int) ) for(i=1; i < 100; i++ ) if( !chkf(i) && (s=mapf(i)) ) n += strlen(s) + 2; - list = m_alloc( 21 + n ); *list = 0; + list = gcry_xmalloc( 21 + n ); *list = 0; for(p=NULL, i=1; i < 100; i++ ) { if( !chkf(i) && (s=mapf(i)) ) { if( !p ) @@ -203,7 +203,7 @@ main( int argc, char **argv ) configname, strerror(errno) ); g10_exit(1); } - m_free(configname); configname = NULL; + gcry_free(configname); configname = NULL; } if( parse_debug && configname ) log_info("reading options from `%s'\n", configname ); @@ -216,8 +216,8 @@ main( int argc, char **argv ) case 'v': opt.verbose++; break; case 501: if( !configfp ) { - m_free(configname); - configname = m_strdup(pargs.r.ret_str); + gcry_free(configname); + configname = gcry_xstrdup(pargs.r.ret_str); goto next_pass; } break; @@ -230,10 +230,10 @@ main( int argc, char **argv ) if( configfp ) { fclose( configfp ); configfp = NULL; - m_free(configname); configname = NULL; + gcry_free(configname); configname = NULL; goto next_pass; } - m_free( configname ); configname = NULL; + gcry_free( configname ); configname = NULL; if( log_get_errorcount(0) ) g10_exit(2); diff --git a/g10/hkp.c b/g10/hkp.c index 507bc5ce1..a608f001d 100644 --- a/g10/hkp.c +++ b/g10/hkp.c @@ -30,7 +30,7 @@ #include "util.h" #include "ttyio.h" #include "i18n.h" -#include "memory.h" +#include #include "options.h" #include "filter.h" #include "http.h" @@ -61,7 +61,7 @@ hkp_ask_import( u32 *keyid ) return -1; log_info("requesting key %08lX from %s ...\n", (ulong)keyid[1], opt.keyserver_name ); - request = m_alloc( strlen( opt.keyserver_name ) + 100 ); + request = gcry_xmalloc( strlen( opt.keyserver_name ) + 100 ); /* hkp does not accept the long keyid - we should really write a * nicer one */ sprintf( request, "x-hkp://%s:11371/pks/lookup?op=get&search=0x%08lX", @@ -77,7 +77,7 @@ hkp_ask_import( u32 *keyid ) http_close( &hd ); } - m_free( request ); + gcry_free( request ); return rc; #endif } @@ -141,7 +141,7 @@ hkp_export( STRLIST users ) iobuf_flush_temp( temp ); - request = m_alloc( strlen( opt.keyserver_name ) + 100 ); + request = gcry_xmalloc( strlen( opt.keyserver_name ) + 100 ); sprintf( request, "x-hkp://%s:11371/pks/add", opt.keyserver_name ); rc = http_open( &hd, HTTP_REQ_POST, request , 0 ); if( rc ) { @@ -150,14 +150,14 @@ hkp_export( STRLIST users ) rc == G10ERR_NETWORK? strerror(errno) : g10_errstr(rc) ); iobuf_close(temp); - m_free( request ); + gcry_free( request ); return rc; } sprintf( request, "Content-Length: %u\n", (unsigned)iobuf_get_temp_length(temp) + 9 ); iobuf_writestr( hd.fp_write, request ); - m_free( request ); + gcry_free( request ); http_start_data( &hd ); iobuf_writestr( hd.fp_write, "keytext=" ); diff --git a/g10/import.c b/g10/import.c index 368ea8b75..01f70dbd4 100644 --- a/g10/import.c +++ b/g10/import.c @@ -29,7 +29,7 @@ #include "packet.h" #include "errors.h" #include "keydb.h" -#include "memory.h" +#include #include "util.h" #include "trustdb.h" #include "main.h" @@ -145,7 +145,7 @@ import( IOBUF inp, int fast, const char* fname ) getkey_disable_caches(); if( !opt.no_armor ) { /* armored reading is not disabled */ - armor_filter_context_t *afx = m_alloc_clear( sizeof *afx ); + armor_filter_context_t *afx = gcry_xcalloc( 1, sizeof *afx ); afx->only_keyblocks = 1; iobuf_push_filter2( inp, armor_filter, afx, 1 ); } @@ -244,7 +244,7 @@ read_block( IOBUF a, PACKET **pending_pkt, KBNODE *ret_root ) } else in_cert = 0; - pkt = m_alloc( sizeof *pkt ); + pkt = gcry_xmalloc( sizeof *pkt ); init_packet(pkt); while( (rc=parse_packet(a, pkt)) != -1 ) { if( rc ) { /* ignore errors */ @@ -276,7 +276,7 @@ read_block( IOBUF a, PACKET **pending_pkt, KBNODE *ret_root ) goto ready; } { - compress_filter_context_t *cfx = m_alloc_clear( sizeof *cfx ); + compress_filter_context_t *cfx = gcry_xcalloc( 1, sizeof *cfx ); cfx->algo = pkt->pkt.compressed->algorithm; pkt->pkt.compressed->buf = NULL; iobuf_push_filter2( a, compress_filter, cfx, 1 ); @@ -300,7 +300,7 @@ read_block( IOBUF a, PACKET **pending_pkt, KBNODE *ret_root ) root = new_kbnode( pkt ); else add_kbnode( root, new_kbnode( pkt ) ); - pkt = m_alloc( sizeof *pkt ); + pkt = gcry_xmalloc( sizeof *pkt ); } init_packet(pkt); break; @@ -315,7 +315,7 @@ read_block( IOBUF a, PACKET **pending_pkt, KBNODE *ret_root ) else *ret_root = root; free_packet( pkt ); - m_free( pkt ); + gcry_free( pkt ); return rc; } @@ -383,7 +383,7 @@ import_one( const char *fname, KBNODE keyblock, int fast ) /* do we have this key already in one of our pubrings ? */ - pk_orig = m_alloc_clear( sizeof *pk_orig ); + pk_orig = gcry_xcalloc( 1, sizeof *pk_orig ); rc = get_pubkey( pk_orig, keyid ); if( rc && rc != G10ERR_NO_PUBKEY ) { log_error( _("key %08lX: public key not found: %s\n"), @@ -411,7 +411,7 @@ import_one( const char *fname, KBNODE keyblock, int fast ) if( is_status_enabled() ) { char *us = get_long_user_id_string( keyid ); write_status_text( STATUS_IMPORTED, us ); - m_free(us); + gcry_free(us); } stats.imported++; if( is_RSA( pk->pubkey_algo ) ) @@ -609,7 +609,7 @@ import_revoke_cert( const char *fname, KBNODE node ) keyid[0] = node->pkt->pkt.signature->keyid[0]; keyid[1] = node->pkt->pkt.signature->keyid[1]; - pk = m_alloc_clear( sizeof *pk ); + pk = gcry_xcalloc( 1, sizeof *pk ); rc = get_pubkey( pk, keyid ); if( rc == G10ERR_NO_PUBKEY ) { log_info( _("key %08lX: no public key - " diff --git a/g10/kbnode.c b/g10/kbnode.c index 282d8b42e..79c1386aa 100644 --- a/g10/kbnode.c +++ b/g10/kbnode.c @@ -24,7 +24,7 @@ #include #include #include "util.h" -#include "memory.h" +#include #include "packet.h" #include "keydb.h" @@ -41,7 +41,7 @@ alloc_node(void) if( n ) unused_nodes = n->next; else - n = m_alloc( sizeof *n ); + n = gcry_xmalloc( sizeof *n ); n->next = NULL; n->pkt = NULL; n->flag = 0; @@ -58,7 +58,7 @@ free_node( KBNODE n ) n->next = unused_nodes; unused_nodes = n; #else - m_free( n ); + gcry_free( n ); #endif } } @@ -94,7 +94,7 @@ release_kbnode( KBNODE n ) n2 = n->next; if( !is_cloned_kbnode(n) ) { free_packet( n->pkt ); - m_free( n->pkt ); + gcry_free( n->pkt ); } free_node( n ); n = n2; @@ -266,7 +266,7 @@ commit_kbnode( KBNODE *root ) nl->next = n->next; if( !is_cloned_kbnode(n) ) { free_packet( n->pkt ); - m_free( n->pkt ); + gcry_free( n->pkt ); } free_node( n ); changed = 1; @@ -290,7 +290,7 @@ remove_kbnode( KBNODE *root, KBNODE node ) nl->next = n->next; if( !is_cloned_kbnode(n) ) { free_packet( n->pkt ); - m_free( n->pkt ); + gcry_free( n->pkt ); } free_node( n ); } diff --git a/g10/keyedit.c b/g10/keyedit.c index 91bba4723..74f97aa95 100644 --- a/g10/keyedit.c +++ b/g10/keyedit.c @@ -31,7 +31,7 @@ #include "errors.h" #include "iobuf.h" #include "keydb.h" -#include "memory.h" +#include #include "util.h" #include "main.h" #include "trustdb.h" @@ -158,7 +158,7 @@ print_and_check_one_sig( KBNODE keyblock, KBNODE node, size_t n; char *p = get_user_id( sig->keyid, &n ); tty_print_utf8_string( p, n > 40? 40 : n ); - m_free(p); + gcry_free(p); } tty_printf("\n"); } @@ -331,7 +331,7 @@ sign_uids( KBNODE keyblock, STRLIST locusr, int *ret_modified, int local ) "with your key: \"")); p = get_user_id( sk_keyid, &n ); tty_print_utf8_string( p, n ); - m_free(p); p = NULL; + gcry_free(p); p = NULL; tty_printf("\"\n\n"); if( local ) @@ -373,7 +373,7 @@ sign_uids( KBNODE keyblock, STRLIST locusr, int *ret_modified, int local ) *ret_modified = 1; /* we changed the keyblock */ upd_trust = 1; - pkt = m_alloc_clear( sizeof *pkt ); + pkt = gcry_xcalloc( 1, sizeof *pkt ); pkt->pkttype = PKT_SIGNATURE; pkt->pkt.signature = sig; insert_kbnode( node, new_kbnode(pkt), PKT_SIGNATURE ); @@ -443,7 +443,7 @@ change_passphrase( KBNODE keyblock ) tty_printf(_("Can't edit this key: %s\n"), g10_errstr(rc)); else { DEK *dek = NULL; - STRING2KEY *s2k = m_alloc_secure( sizeof *s2k ); + STRING2KEY *s2k = gcry_xmalloc_secure( sizeof *s2k ); tty_printf(_("Enter the new passphrase for this secret key.\n\n") ); @@ -483,12 +483,12 @@ change_passphrase( KBNODE keyblock ) break; } } - m_free(s2k); - m_free(dek); + gcry_free(s2k); + gcry_free(dek); } leave: - m_free( passphrase ); + gcry_free( passphrase ); set_next_passphrase( NULL ); return changed && !rc; } @@ -665,14 +665,14 @@ keyedit_menu( const char *username, STRLIST locusr, STRLIST commands, redisplay = 0; } do { - m_free(answer); + gcry_free(answer); if( have_commands ) { if( commands ) { - answer = m_strdup( commands->d ); + answer = gcry_xstrdup( commands->d ); commands = commands->next; } else if( opt.batch ) { - answer = m_strdup("quit"); + answer = gcry_xstrdup("quit"); } else have_commands = 0; @@ -980,7 +980,7 @@ keyedit_menu( const char *username, STRLIST locusr, STRLIST commands, leave: release_kbnode( keyblock ); release_kbnode( sec_keyblock ); - m_free(answer); + gcry_free(answer); } @@ -1020,7 +1020,7 @@ show_prefs( KBNODE keyblock, PKT_user_id *uid ) } tty_printf("\n"); - m_free(p); + gcry_free(p); } @@ -1224,7 +1224,7 @@ menu_adduid( KBNODE pub_keyblock, KBNODE sec_keyblock ) } /* insert/append to secret keyblock */ - pkt = m_alloc_clear( sizeof *pkt ); + pkt = gcry_xcalloc( 1, sizeof *pkt ); pkt->pkttype = PKT_USER_ID; pkt->pkt.user_id = copy_user_id(NULL, uid); node = new_kbnode(pkt); @@ -1232,7 +1232,7 @@ menu_adduid( KBNODE pub_keyblock, KBNODE sec_keyblock ) insert_kbnode( sec_where, node, 0 ); else add_kbnode( sec_keyblock, node ); - pkt = m_alloc_clear( sizeof *pkt ); + pkt = gcry_xcalloc( 1, sizeof *pkt ); pkt->pkttype = PKT_SIGNATURE; pkt->pkt.signature = copy_signature(NULL, sig); if( sec_where ) @@ -1240,7 +1240,7 @@ menu_adduid( KBNODE pub_keyblock, KBNODE sec_keyblock ) else add_kbnode( sec_keyblock, new_kbnode(pkt) ); /* insert/append to public keyblock */ - pkt = m_alloc_clear( sizeof *pkt ); + pkt = gcry_xcalloc( 1, sizeof *pkt ); pkt->pkttype = PKT_USER_ID; pkt->pkt.user_id = uid; node = new_kbnode(pkt); @@ -1248,7 +1248,7 @@ menu_adduid( KBNODE pub_keyblock, KBNODE sec_keyblock ) insert_kbnode( pub_where, node, 0 ); else add_kbnode( pub_keyblock, node ); - pkt = m_alloc_clear( sizeof *pkt ); + pkt = gcry_xcalloc( 1, sizeof *pkt ); pkt->pkttype = PKT_SIGNATURE; pkt->pkt.signature = copy_signature(NULL, sig); if( pub_where ) @@ -1521,18 +1521,18 @@ menu_expire( KBNODE pub_keyblock, KBNODE sec_keyblock ) return 0; } /* replace the packet */ - newpkt = m_alloc_clear( sizeof *newpkt ); + newpkt = gcry_xcalloc( 1, sizeof *newpkt ); newpkt->pkttype = PKT_SIGNATURE; newpkt->pkt.signature = newsig; free_packet( node->pkt ); - m_free( node->pkt ); + gcry_free( node->pkt ); node->pkt = newpkt; if( sn ) { - newpkt = m_alloc_clear( sizeof *newpkt ); + newpkt = gcry_xcalloc( 1, sizeof *newpkt ); newpkt->pkttype = PKT_SIGNATURE; newpkt->pkt.signature = copy_signature( NULL, newsig ); free_packet( sn->pkt ); - m_free( sn->pkt ); + gcry_free( sn->pkt ); sn->pkt = newpkt; } } @@ -1815,7 +1815,7 @@ menu_revsig( KBNODE keyblock ) memset( &attrib, 0, sizeof attrib ); node->flag &= ~NODFLG_MARK_A; - sk = m_alloc_secure_clear( sizeof *sk ); + sk = gcry_xcalloc_secure( 1, sizeof *sk ); if( get_seckey( sk, node->pkt->pkt.signature->keyid ) ) { log_info(_("no secret key\n")); continue; @@ -1835,7 +1835,7 @@ menu_revsig( KBNODE keyblock ) changed = 1; /* we changed the keyblock */ upd_trust = 1; - pkt = m_alloc_clear( sizeof *pkt ); + pkt = gcry_xcalloc( 1, sizeof *pkt ); pkt->pkttype = PKT_SIGNATURE; pkt->pkt.signature = sig; insert_kbnode( unode, new_kbnode(pkt), 0 ); @@ -1884,7 +1884,7 @@ menu_revkey( KBNODE pub_keyblock, KBNODE sec_keyblock ) changed = 1; /* we changed the keyblock */ upd_trust = 1; - pkt = m_alloc_clear( sizeof *pkt ); + pkt = gcry_xcalloc( 1, sizeof *pkt ); pkt->pkttype = PKT_SIGNATURE; pkt->pkt.signature = sig; insert_kbnode( node, new_kbnode(pkt), 0 ); diff --git a/g10/keygen.c b/g10/keygen.c index 10f27952a..805160696 100644 --- a/g10/keygen.c +++ b/g10/keygen.c @@ -26,7 +26,7 @@ #include #include #include "util.h" -#include "memory.h" +#include #include "main.h" #include "packet.h" #include "dummy-cipher.h" @@ -40,11 +40,11 @@ static void write_uid( KBNODE root, const char *s ) { - PACKET *pkt = m_alloc_clear(sizeof *pkt ); + PACKET *pkt = gcry_xcalloc( 1,sizeof *pkt ); size_t n = strlen(s); pkt->pkttype = PKT_USER_ID; - pkt->pkt.user_id = m_alloc( sizeof *pkt->pkt.user_id + n - 1 ); + pkt->pkt.user_id = gcry_xmalloc( sizeof *pkt->pkt.user_id + n - 1 ); pkt->pkt.user_id->len = n; strcpy(pkt->pkt.user_id->name, s); add_kbnode( root, new_kbnode( pkt ) ); @@ -141,7 +141,7 @@ write_selfsig( KBNODE root, KBNODE pub_root, PKT_secret_key *sk ) return rc; } - pkt = m_alloc_clear( sizeof *pkt ); + pkt = gcry_xcalloc( 1, sizeof *pkt ); pkt->pkttype = PKT_SIGNATURE; pkt->pkt.signature = sig; add_kbnode( root, new_kbnode( pkt ) ); @@ -182,7 +182,7 @@ write_keybinding( KBNODE root, KBNODE pub_root, PKT_secret_key *sk ) return rc; } - pkt = m_alloc_clear( sizeof *pkt ); + pkt = gcry_xcalloc( 1, sizeof *pkt ); pkt->pkttype = PKT_SIGNATURE; pkt->pkt.signature = sig; add_kbnode( root, new_kbnode( pkt ) ); @@ -210,8 +210,8 @@ gen_elg(int algo, unsigned nbits, KBNODE pub_root, KBNODE sec_root, DEK *dek, return rc; } - sk = m_alloc_clear( sizeof *sk ); - pk = m_alloc_clear( sizeof *pk ); + sk = gcry_xcalloc( 1, sizeof *sk ); + pk = gcry_xcalloc( 1, sizeof *pk ); sk->timestamp = pk->timestamp = make_timestamp(); sk->version = pk->version = version; if( expireval ) { @@ -244,14 +244,14 @@ gen_elg(int algo, unsigned nbits, KBNODE pub_root, KBNODE sec_root, DEK *dek, } } - pkt = m_alloc_clear(sizeof *pkt); + pkt = gcry_xcalloc( 1,sizeof *pkt); pkt->pkttype = ret_sk ? PKT_PUBLIC_KEY : PKT_PUBLIC_SUBKEY; pkt->pkt.public_key = pk; add_kbnode(pub_root, new_kbnode( pkt )); /* don't know whether it makes sense to have the factors, so for now * we store them in the secret keyring (but they are not secret) */ - pkt = m_alloc_clear(sizeof *pkt); + pkt = gcry_xcalloc( 1,sizeof *pkt); pkt->pkttype = ret_sk ? PKT_SECRET_KEY : PKT_SECRET_SUBKEY; pkt->pkt.secret_key = sk; add_kbnode(sec_root, new_kbnode( pkt )); @@ -287,8 +287,8 @@ gen_dsa(unsigned nbits, KBNODE pub_root, KBNODE sec_root, DEK *dek, return rc; } - sk = m_alloc_clear( sizeof *sk ); - pk = m_alloc_clear( sizeof *pk ); + sk = gcry_xcalloc( 1, sizeof *sk ); + pk = gcry_xcalloc( 1, sizeof *pk ); sk->timestamp = pk->timestamp = make_timestamp(); sk->version = pk->version = 4; if( expireval ) { @@ -323,7 +323,7 @@ gen_dsa(unsigned nbits, KBNODE pub_root, KBNODE sec_root, DEK *dek, } } - pkt = m_alloc_clear(sizeof *pkt); + pkt = gcry_xcalloc( 1,sizeof *pkt); pkt->pkttype = ret_sk ? PKT_PUBLIC_KEY : PKT_PUBLIC_SUBKEY; pkt->pkt.public_key = pk; add_kbnode(pub_root, new_kbnode( pkt )); @@ -334,7 +334,7 @@ gen_dsa(unsigned nbits, KBNODE pub_root, KBNODE sec_root, DEK *dek, * We store only f1 to f_n-1; fn can be calculated because p and q * are known. */ - pkt = m_alloc_clear(sizeof *pkt); + pkt = gcry_xcalloc( 1,sizeof *pkt); pkt->pkttype = ret_sk ? PKT_SECRET_KEY : PKT_SECRET_SUBKEY; pkt->pkt.secret_key = sk; add_kbnode(sec_root, new_kbnode( pkt )); @@ -400,7 +400,7 @@ ask_algo( int *ret_v4, int addmode ) answer = cpr_get("keygen.algo",_("Your selection? ")); cpr_kill_prompt(); algo = *answer? atoi(answer): 1; - m_free(answer); + gcry_free(answer); if( algo == 1 && !addmode ) { algo = 0; /* create both keys */ break; @@ -450,7 +450,7 @@ ask_keysize( int algo ) _("What keysize do you want? (1024) ")); cpr_kill_prompt(); nbits = *answer? atoi(answer): 1024; - m_free(answer); + gcry_free(answer); if( algo == GCRY_PK_DSA && (nbits < 512 || nbits > 1024) ) tty_printf(_("DSA only allows keysizes from 512 to 1024\n")); else if( nbits < 768 ) @@ -523,7 +523,7 @@ ask_expire_interval(void) u32 abs_date=0; u32 curtime=0;; - m_free(answer); + gcry_free(answer); answer = cpr_get("keygen.valid",_("Key is valid for? (0) ")); cpr_kill_prompt(); trim_spaces(answer); @@ -564,7 +564,7 @@ ask_expire_interval(void) _("Is this correct (y/n)? ")) ) break; } - m_free(answer); + gcry_free(answer); return interval; } @@ -614,7 +614,7 @@ ask_user_id( int mode ) if( !aname ) { for(;;) { - m_free(aname); + gcry_free(aname); aname = cpr_get("keygen.name",_("Real name: ")); trim_spaces(aname); cpr_kill_prompt(); @@ -630,7 +630,7 @@ ask_user_id( int mode ) } if( !amail ) { for(;;) { - m_free(amail); + gcry_free(amail); amail = cpr_get("keygen.email",_("Email address: ")); trim_spaces(amail); cpr_kill_prompt(); @@ -649,7 +649,7 @@ ask_user_id( int mode ) } if( !acomment ) { for(;;) { - m_free(acomment); + gcry_free(acomment); acomment = cpr_get("keygen.comment",_("Comment: ")); trim_spaces(acomment); cpr_kill_prompt(); @@ -662,8 +662,8 @@ ask_user_id( int mode ) } } - m_free(uid); - uid = p = m_alloc(strlen(aname)+strlen(amail)+strlen(acomment)+12+10); + gcry_free(uid); + uid = p = gcry_xmalloc(strlen(aname)+strlen(amail)+strlen(acomment)+12+10); p = stpcpy(p, aname ); if( *acomment ) p = stpcpy(stpcpy(stpcpy(p," ("), acomment),")"); @@ -672,8 +672,11 @@ ask_user_id( int mode ) /* append a warning if we do not have dev/random * or it is switched into quick testmode */ + #warning quick_random_gen() not available + #if 0 if( quick_random_gen(-1) ) strcpy(p, " (INSECURE!)" ); + #endif /* print a note in case that UTF8 mapping has to be done */ for(p=uid; *p; p++ ) { @@ -692,7 +695,7 @@ ask_user_id( int mode ) if( strlen(ansstr) != 10 ) BUG(); if( cpr_enabled() ) { - answer = m_strdup(ansstr+6); + answer = gcry_xstrdup(ansstr+6); answer[1] = 0; } else { @@ -703,40 +706,40 @@ ask_user_id( int mode ) if( strlen(answer) > 1 ) ; else if( *answer == ansstr[0] || *answer == ansstr[1] ) { - m_free(aname); aname = NULL; + gcry_free(aname); aname = NULL; break; } else if( *answer == ansstr[2] || *answer == ansstr[3] ) { - m_free(acomment); acomment = NULL; + gcry_free(acomment); acomment = NULL; break; } else if( *answer == ansstr[4] || *answer == ansstr[5] ) { - m_free(amail); amail = NULL; + gcry_free(amail); amail = NULL; break; } else if( *answer == ansstr[6] || *answer == ansstr[7] ) { - m_free(aname); aname = NULL; - m_free(acomment); acomment = NULL; - m_free(amail); amail = NULL; + gcry_free(aname); aname = NULL; + gcry_free(acomment); acomment = NULL; + gcry_free(amail); amail = NULL; break; } else if( *answer == ansstr[8] || *answer == ansstr[9] ) { - m_free(aname); aname = NULL; - m_free(acomment); acomment = NULL; - m_free(amail); amail = NULL; - m_free(uid); uid = NULL; + gcry_free(aname); aname = NULL; + gcry_free(acomment); acomment = NULL; + gcry_free(amail); amail = NULL; + gcry_free(uid); uid = NULL; break; } - m_free(answer); + gcry_free(answer); } - m_free(answer); + gcry_free(answer); if( !amail && !acomment && !amail ) break; - m_free(uid); uid = NULL; + gcry_free(uid); uid = NULL; } if( uid ) { char *p = native_to_utf8( uid ); - m_free( uid ); + gcry_free( uid ); uid = p; } return uid; @@ -751,7 +754,7 @@ ask_passphrase( STRING2KEY **ret_s2k ) tty_printf(_("You need a Passphrase to protect your secret key.\n\n") ); - s2k = m_alloc_secure( sizeof *s2k ); + s2k = gcry_xmalloc_secure( sizeof *s2k ); for(;;) { s2k->mode = opt.s2k_mode; s2k->hash_algo = opt.s2k_digest_algo; @@ -760,8 +763,8 @@ ask_passphrase( STRING2KEY **ret_s2k ) tty_printf(_("passphrase not correctly repeated; try again.\n")); } else if( !dek->keylen ) { - m_free(dek); dek = NULL; - m_free(s2k); s2k = NULL; + gcry_free(dek); dek = NULL; + gcry_free(s2k); s2k = NULL; tty_printf(_( "You don't want a passphrase - this is probably a *bad* idea!\n" "I will do it anyway. You can change your passphrase at any time,\n" @@ -825,7 +828,7 @@ generate_user_id() if( !p ) return NULL; n = strlen(p); - uid = m_alloc( sizeof *uid + n - 1 ); + uid = gcry_xmalloc( sizeof *uid + n - 1 ); uid->len = n; strcpy(uid->name, p); return uid; @@ -977,11 +980,11 @@ generate_keypair() release_kbnode( sec_root ); if( sk ) /* the unprotected secret key */ free_secret_key(sk); - m_free(uid); - m_free(dek); - m_free(s2k); - m_free(pub_fname); - m_free(sec_fname); + gcry_free(uid); + gcry_free(dek); + gcry_free(s2k); + gcry_free(pub_fname); + gcry_free(sec_fname); } @@ -1053,7 +1056,7 @@ generate_subkeypair( KBNODE pub_keyblock, KBNODE sec_keyblock ) goto leave; if( passphrase ) { - s2k = m_alloc_secure( sizeof *s2k ); + s2k = gcry_xmalloc_secure( sizeof *s2k ); s2k->mode = opt.s2k_mode; s2k->hash_algo = opt.s2k_digest_algo; set_next_passphrase( passphrase ); @@ -1072,9 +1075,9 @@ generate_subkeypair( KBNODE pub_keyblock, KBNODE sec_keyblock ) leave: if( rc ) log_error(_("Key generation failed: %s\n"), g10_errstr(rc) ); - m_free( passphrase ); - m_free( dek ); - m_free( s2k ); + gcry_free( passphrase ); + gcry_free( dek ); + gcry_free( s2k ); if( sk ) /* release the copy of the (now unprotected) secret key */ free_secret_key(sk); set_next_passphrase( NULL ); diff --git a/g10/keyid.c b/g10/keyid.c index 774b90a9a..ddaf0d5d3 100644 --- a/g10/keyid.c +++ b/g10/keyid.c @@ -28,7 +28,6 @@ #include #include "util.h" #include "main.h" -#include "memory.h" #include "packet.h" #include "options.h" #include "keydb.h" @@ -70,8 +69,8 @@ do_fingerprint_md( PKT_public_key *pk ) rc = gcry_mpi_print( GCRYMPI_FMT_PGP, NULL, &nbytes, pk->pkey[i] ); assert( !rc ); /* fixme: we should try to allocate a buffer on the stack */ - pp[i] = m_alloc(nbytes); - rc = gcry_mpi_print( GCRYMPI_FMT_PGP, pp[1], &nbytes, pk->pkey[i] ); + pp[i] = gcry_xmalloc(nbytes); + rc = gcry_mpi_print( GCRYMPI_FMT_PGP, pp[i], &nbytes, pk->pkey[i] ); assert( !rc ); nn[i] = nbytes; n += nn[i]; @@ -104,7 +103,7 @@ do_fingerprint_md( PKT_public_key *pk ) gcry_md_putc( md, pk->pubkey_algo ); for(i=0; i < npkey; i++ ) { gcry_md_write( md, pp[i], nn[i] ); - m_free(pp[i]); + gcry_free(pp[i]); } gcry_md_final( md ); @@ -139,7 +138,7 @@ v3_keyid( MPI a, u32 *ki ) rc = gcry_mpi_print( GCRYMPI_FMT_USG, NULL, &nbytes, a ); assert( !rc ); /* fixme: allocate it on the stack */ - buffer = m_alloc(nbytes); + buffer = gcry_xmalloc(nbytes); rc = gcry_mpi_print( GCRYMPI_FMT_USG, buffer, &nbytes, a ); assert( !rc ); if( nbytes < 8 ) { /* oops */ @@ -149,7 +148,7 @@ v3_keyid( MPI a, u32 *ki ) memcpy( ki+0, buffer+nbytes-8, 4); memcpy( ki+1, buffer+nbytes-4, 4); } - m_free( buffer ); + gcry_free( buffer ); } @@ -372,10 +371,9 @@ expirestr_from_sk( PKT_secret_key *sk ) byte * fingerprint_from_pk( PKT_public_key *pk, byte *array, size_t *ret_len ) { - byte *p, *buf; + byte *buf; const char *dp; size_t len; - unsigned n; if( pk->version < 4 && is_RSA(pk->pubkey_algo) ) { /* RSA in version 3 packets is special */ @@ -391,23 +389,23 @@ fingerprint_from_pk( PKT_public_key *pk, byte *array, size_t *ret_len ) rc = gcry_mpi_print( GCRYMPI_FMT_USG, NULL, &nbytes, pk->pkey[0] ); assert( !rc ); /* fixme: allocate it on the stack */ - buf = m_alloc(nbytes); + buf = gcry_xmalloc(nbytes); rc = gcry_mpi_print( GCRYMPI_FMT_USG, buf, &nbytes, pk->pkey[0] ); assert( !rc ); gcry_md_write( md, buf, nbytes ); - m_free(buf); + gcry_free(buf); rc = gcry_mpi_print( GCRYMPI_FMT_USG, NULL, &nbytes, pk->pkey[1] ); assert( !rc ); /* fixme: allocate it on the stack */ - buf = m_alloc(nbytes); + buf = gcry_xmalloc(nbytes); rc = gcry_mpi_print( GCRYMPI_FMT_USG, buf, &nbytes, pk->pkey[1] ); assert( !rc ); gcry_md_write( md, buf, nbytes ); - m_free(buf); + gcry_free(buf); } gcry_md_final(md); if( !array ) - array = m_alloc( 16 ); + array = gcry_xmalloc( 16 ); len = 16; memcpy(array, gcry_md_read(md, GCRY_MD_MD5), 16 ); gcry_md_close(md); @@ -419,7 +417,7 @@ fingerprint_from_pk( PKT_public_key *pk, byte *array, size_t *ret_len ) len = gcry_md_get_algo_dlen( gcry_md_get_algo( md ) ); assert( len <= MAX_FINGERPRINT_LEN ); if( !array ) - array = m_alloc( len ); + array = gcry_xmalloc( len ); memcpy(array, dp, len ); gcry_md_close(md); } @@ -431,10 +429,9 @@ fingerprint_from_pk( PKT_public_key *pk, byte *array, size_t *ret_len ) byte * fingerprint_from_sk( PKT_secret_key *sk, byte *array, size_t *ret_len ) { - byte *p, *buf; + byte *buf; const char *dp; size_t len; - unsigned n; if( sk->version < 4 && is_RSA(sk->pubkey_algo) ) { /* RSA in version 3 packets is special */ @@ -451,23 +448,23 @@ fingerprint_from_sk( PKT_secret_key *sk, byte *array, size_t *ret_len ) rc = gcry_mpi_print( GCRYMPI_FMT_USG, NULL, &nbytes, sk->skey[1] ); assert( !rc ); /* fixme: allocate it on the stack */ - buf = m_alloc(nbytes); + buf = gcry_xmalloc(nbytes); rc = gcry_mpi_print( GCRYMPI_FMT_USG, buf, &nbytes, sk->skey[1] ); assert( !rc ); gcry_md_write( md, buf, nbytes ); - m_free(buf); + gcry_free(buf); rc = gcry_mpi_print( GCRYMPI_FMT_USG, NULL, &nbytes, sk->skey[0] ); assert( !rc ); /* fixme: allocate it on the stack */ - buf = m_alloc(nbytes); + buf = gcry_xmalloc(nbytes); rc = gcry_mpi_print( GCRYMPI_FMT_USG, buf, &nbytes, sk->skey[0] ); assert( !rc ); gcry_md_write( md, buf, nbytes ); - m_free(buf); + gcry_free(buf); } gcry_md_final(md); if( !array ) - array = m_alloc( 16 ); + array = gcry_xmalloc( 16 ); len = 16; memcpy(array, gcry_md_read(md, GCRY_MD_MD5), 16 ); gcry_md_close(md); @@ -479,7 +476,7 @@ fingerprint_from_sk( PKT_secret_key *sk, byte *array, size_t *ret_len ) len = gcry_md_get_algo_dlen( gcry_md_get_algo( md ) ); assert( len <= MAX_FINGERPRINT_LEN ); if( !array ) - array = m_alloc( len ); + array = gcry_xmalloc( len ); memcpy(array, dp, len ); gcry_md_close(md); } diff --git a/g10/keylist.c b/g10/keylist.c index 53e88f468..91e660352 100644 --- a/g10/keylist.c +++ b/g10/keylist.c @@ -29,7 +29,7 @@ #include "packet.h" #include "errors.h" #include "keydb.h" -#include "memory.h" +#include #include "util.h" #include "trustdb.h" #include "main.h" @@ -159,7 +159,7 @@ print_key_data( PKT_public_key *pk, u32 *keyid ) int i; for(i=0; i < n; i++ ) { - printf("pkd:%d:%u:", i, mpi_get_nbits( pk->pkey[i] ) ); + printf("pkd:%d:%u:", i, gcry_mpi_get_nbits( pk->pkey[i] ) ); mpi_print(stdout, pk->pkey[i], 1 ); putchar(':'); putchar('\n'); @@ -412,7 +412,7 @@ list_keyblock( KBNODE keyblock, int secret ) print_string( stdout, p, n, ':' ); else print_utf8_string( stdout, p, n ); - m_free(p); + gcry_free(p); } if( opt.with_colons ) printf(":%02x:", sig->sig_class ); @@ -463,6 +463,6 @@ fingerprint( PKT_public_key *pk, PKT_secret_key *sk ) } } putchar('\n'); - m_free(array); + gcry_free(array); } diff --git a/g10/main.h b/g10/main.h index 03cba92c5..9d632db6d 100644 --- a/g10/main.h +++ b/g10/main.h @@ -71,6 +71,7 @@ int pubkey_get_npkey( int algo ); int pubkey_get_nskey( int algo ); int pubkey_get_nsig( int algo ); int pubkey_get_nenc( int algo ); +unsigned int pubkey_nbits( int algo, MPI *pkey ); /*-- helptext.c --*/ diff --git a/g10/mainproc.c b/g10/mainproc.c index af26118c7..6facd822b 100644 --- a/g10/mainproc.c +++ b/g10/mainproc.c @@ -28,7 +28,6 @@ #include #include "packet.h" #include "iobuf.h" -#include "memory.h" #include "options.h" #include "util.h" #include "keydb.h" @@ -90,7 +89,7 @@ release_list( CTX c ) release_kbnode( c->list ); while( c->failed_pkenc ) { struct kidlist_item *tmp = c->failed_pkenc->next; - m_free( c->failed_pkenc ); + gcry_free( c->failed_pkenc ); c->failed_pkenc = tmp; } c->failed_pkenc = NULL; @@ -225,10 +224,10 @@ proc_pubkey_enc( CTX c, PACKET *pkt ) || is_RSA(enc->pubkey_algo) ) { if ( !c->dek && ((!enc->keyid[0] && !enc->keyid[1]) || !seckey_available( enc->keyid )) ) { - c->dek = m_alloc_secure( sizeof *c->dek ); + c->dek = gcry_xmalloc_secure( sizeof *c->dek ); if( (result = get_session_key( enc, c->dek )) ) { /* error: delete the DEK */ - m_free(c->dek); c->dek = NULL; + gcry_free(c->dek); c->dek = NULL; } } else @@ -244,7 +243,7 @@ proc_pubkey_enc( CTX c, PACKET *pkt ) log_info( _("public key encrypted data: good DEK\n") ); } else { /* store it for later display */ - struct kidlist_item *x = m_alloc( sizeof *x ); + struct kidlist_item *x = gcry_xmalloc( sizeof *x ); x->kid[0] = enc->keyid[0]; x->kid[1] = enc->keyid[1]; x->pubkey_algo = enc->pubkey_algo; @@ -265,7 +264,7 @@ static void print_failed_pkenc( struct kidlist_item *list ) { for( ; list; list = list->next ) { - PKT_public_key *pk = m_alloc_clear( sizeof *pk ); + PKT_public_key *pk = gcry_xcalloc( 1, sizeof *pk ); const char *algstr = gcry_pk_algo_name( list->pubkey_algo ); pk->pubkey_algo = list->pubkey_algo; @@ -278,7 +277,7 @@ print_failed_pkenc( struct kidlist_item *list ) fputs(" \"", log_stream() ); p = get_user_id( list->kid, &n ); print_string( log_stream(), p, n, '"' ); - m_free(p); + gcry_free(p); fputs("\"\n", log_stream() ); } else { @@ -323,7 +322,7 @@ proc_encrypted( CTX c, PACKET *pkt ) result = G10ERR_NO_SECKEY; if( !result ) result = decrypt_data( c, pkt->pkt.encrypted, c->dek ); - m_free(c->dek); c->dek = NULL; + gcry_free(c->dek); c->dek = NULL; if( result == -1 ) ; else if( !result ) { @@ -851,7 +850,7 @@ list_node( CTX c, KBNODE node ) else { p = get_user_id( sig->keyid, &n ); print_string( stdout, p, n, opt.with_colons ); - m_free(p); + gcry_free(p); } if( opt.with_colons ) printf(":%02x:", sig->sig_class ); @@ -866,11 +865,11 @@ int proc_packets( void *anchor, IOBUF a ) { int rc; - CTX c = m_alloc_clear( sizeof *c ); + CTX c = gcry_xcalloc( 1, sizeof *c ); c->anchor = anchor; rc = do_proc_packets( c, a ); - m_free( c ); + gcry_free( c ); return rc; } @@ -878,7 +877,7 @@ int proc_signature_packets( void *anchor, IOBUF a, STRLIST signedfiles, const char *sigfilename ) { - CTX c = m_alloc_clear( sizeof *c ); + CTX c = gcry_xcalloc( 1, sizeof *c ); int rc; c->anchor = anchor; @@ -886,20 +885,20 @@ proc_signature_packets( void *anchor, IOBUF a, c->signed_data = signedfiles; c->sigfilename = sigfilename; rc = do_proc_packets( c, a ); - m_free( c ); + gcry_free( c ); return rc; } int proc_encryption_packets( void *anchor, IOBUF a ) { - CTX c = m_alloc_clear( sizeof *c ); + CTX c = gcry_xcalloc( 1, sizeof *c ); int rc; c->anchor = anchor; c->encrypt_only = 1; rc = do_proc_packets( c, a ); - m_free( c ); + gcry_free( c ); return rc; } @@ -907,7 +906,7 @@ proc_encryption_packets( void *anchor, IOBUF a ) int do_proc_packets( CTX c, IOBUF a ) { - PACKET *pkt = m_alloc( sizeof *pkt ); + PACKET *pkt = gcry_xmalloc( sizeof *pkt ); int rc=0; int any_data=0; int newpkt; @@ -1000,7 +999,7 @@ do_proc_packets( CTX c, IOBUF a ) if( newpkt == -1 ) ; else if( newpkt ) { - pkt = m_alloc( sizeof *pkt ); + pkt = gcry_xmalloc( sizeof *pkt ); init_packet(pkt); } else @@ -1016,9 +1015,9 @@ do_proc_packets( CTX c, IOBUF a ) leave: release_list( c ); - m_free(c->dek); + gcry_free(c->dek); free_packet( pkt ); - m_free( pkt ); + gcry_free( pkt ); free_md_filter_context( &c->mfx ); return rc; } @@ -1055,7 +1054,7 @@ check_sig_and_print( CTX c, KBNODE node ) us = get_long_user_id_string( sig->keyid ); write_status_text( rc? STATUS_BADSIG : STATUS_GOODSIG, us ); - m_free(us); + gcry_free(us); /* fixme: list only user ids which are valid and add information * about the trustworthiness of each user id, sort them. @@ -1085,7 +1084,7 @@ check_sig_and_print( CTX c, KBNODE node ) if( !rc && is_status_enabled() ) { /* print a status response with the fingerprint */ - PKT_public_key *pk = m_alloc_clear( sizeof *pk ); + PKT_public_key *pk = gcry_xcalloc( 1, sizeof *pk ); if( !get_pubkey( pk, sig->keyid ) ) { byte array[MAX_FINGERPRINT_LEN], *p; diff --git a/g10/mdfilter.c b/g10/mdfilter.c index 54eadcf6c..abb55d8c1 100644 --- a/g10/mdfilter.c +++ b/g10/mdfilter.c @@ -27,7 +27,7 @@ #include "errors.h" #include "iobuf.h" -#include "memory.h" +#include #include "util.h" #include "filter.h" diff --git a/g10/misc.c b/g10/misc.c index a750cca75..1c311a6c0 100644 --- a/g10/misc.c +++ b/g10/misc.c @@ -35,7 +35,6 @@ #include #include "util.h" #include "main.h" -#include "memory.h" #include "options.h" #include "i18n.h" @@ -156,7 +155,7 @@ mpi_read(IOBUF inp, unsigned int *ret_nread, int secure) } nread = 2; nbytes = (nbits+7) / 8; - buf = secure? m_alloc_secure( nbytes+2 ) : m_alloc( nbytes+2 ); + buf = secure? gcry_xmalloc_secure( nbytes+2 ) : gcry_xmalloc( nbytes+2 ); p = buf; p[0] = c1; p[1] = c2; @@ -169,7 +168,7 @@ mpi_read(IOBUF inp, unsigned int *ret_nread, int secure) a = NULL; leave: - m_free(buf); + gcry_free(buf); if( nread > *ret_nread ) log_bug("mpi larger than packet"); else @@ -202,7 +201,7 @@ mpi_read_opaque(IOBUF inp, unsigned *ret_nread ) } nread = 2; nbytes = (nbits+7) / 8; - buf = m_alloc( nbytes ); + buf = gcry_xmalloc( nbytes ); p = buf; for( i=0 ; i < nbytes; i++ ) { p[i] = iobuf_get(inp) & 0xff; @@ -213,7 +212,7 @@ mpi_read_opaque(IOBUF inp, unsigned *ret_nread ) buf = NULL; leave: - m_free(buf); + gcry_free(buf); if( nread > *ret_nread ) log_bug("mpi larger than packet"); else @@ -236,17 +235,13 @@ mpi_print( FILE *fp, MPI a, int mode ) } else { int rc; - byte *buffer; - size_t nbytes; + char *buffer; - rc = gcry_mpi_print( GCRYMPI_FMT_HEX, NULL, &nbytes, a ); - assert( !rc ); - buffer = m_is_secure(a)? m_alloc_secure(nbytes) : m_alloc(nbytes); - rc = gcry_mpi_print( GCRYMPI_FMT_HEX, buffer, &nbytes, a ); + rc = gcry_mpi_aprint( GCRYMPI_FMT_HEX, (void **)&buffer, NULL, a ); assert( !rc ); fputs( buffer, fp ); n += strlen(buffer); - m_free( buffer ); + gcry_free( buffer ); } return n; } @@ -286,11 +281,11 @@ checksum_mpi( MPI a ) * should use a stack based buffer and only allocate * a larger one when the mpi_print return an error */ - buffer = m_is_secure(a)? m_alloc_secure(nbytes) : m_alloc(nbytes); + buffer = gcry_is_secure(a)? gcry_xmalloc_secure(nbytes) : gcry_xmalloc(nbytes); rc = gcry_mpi_print( GCRYMPI_FMT_PGP, buffer, &nbytes, a ); assert( !rc ); csum = checksum( buffer, nbytes ); - m_free( buffer ); + gcry_free( buffer ); return csum; } @@ -425,8 +420,42 @@ pubkey_get_nenc( int algo ) return n > 0? n : 0; } -int -pubkey_nbits() +unsigned int +pubkey_nbits( int algo, MPI *key ) { + int nbits; + GCRY_SEXP sexp; + + + if( algo == GCRY_PK_DSA ) { + sexp = SEXP_CONS( SEXP_NEW( "public-key", 0 ), + gcry_sexp_vlist( SEXP_NEW( "dsa", 3 ), + gcry_sexp_new_name_mpi( "p", key[0] ), + gcry_sexp_new_name_mpi( "q", key[1] ), + gcry_sexp_new_name_mpi( "g", key[2] ), + gcry_sexp_new_name_mpi( "y", key[3] ), + NULL )); + } + else if( algo == GCRY_PK_ELG || algo == GCRY_PK_ELG_E ) { + sexp = SEXP_CONS( SEXP_NEW( "public-key", 0 ), + gcry_sexp_vlist( SEXP_NEW( "elg", 3 ), + gcry_sexp_new_name_mpi( "p", key[0] ), + gcry_sexp_new_name_mpi( "g", key[1] ), + gcry_sexp_new_name_mpi( "y", key[2] ), + NULL )); + } + else if( algo == GCRY_PK_RSA ) { + sexp = SEXP_CONS( SEXP_NEW( "public-key", 0 ), + gcry_sexp_vlist( SEXP_NEW( "rsa", 3 ), + gcry_sexp_new_name_mpi( "n", key[0] ), + gcry_sexp_new_name_mpi( "e", key[1] ), + NULL )); + } + else + return 0; + + nbits = gcry_pk_get_nbits( sexp ); + gcry_sexp_release( sexp ); + return nbits; } diff --git a/g10/openfile.c b/g10/openfile.c index 4f73334b8..60910031a 100644 --- a/g10/openfile.c +++ b/g10/openfile.c @@ -26,7 +26,7 @@ #include #include #include "util.h" -#include "memory.h" +#include #include "ttyio.h" #include "options.h" #include "main.h" @@ -89,14 +89,14 @@ make_outfile_name( const char *iname ) size_t n; if( (!iname || (*iname=='-' && !iname[1]) )) - return m_strdup("-"); + return gcry_xstrdup("-"); n = strlen(iname); if( n > 4 && ( !CMP_FILENAME(iname+n-4,".gpg") || !CMP_FILENAME(iname+n-4,".pgp") || !CMP_FILENAME(iname+n-4,".sig") || !CMP_FILENAME(iname+n-4,".asc") ) ) { - char *buf = m_strdup( iname ); + char *buf = gcry_xstrdup( iname ); buf[n-4] = 0; return buf; } @@ -127,19 +127,19 @@ ask_outfile_name( const char *name, size_t namelen ) n = strlen(s) + namelen + 10; defname = name && namelen? make_printable_string( name, namelen, 0): NULL; - prompt = m_alloc(n); + prompt = gcry_xmalloc(n); if( defname ) sprintf(prompt, "%s [%s]: ", s, defname ); else sprintf(prompt, "%s: ", s ); fname = cpr_get("openfile.askoutname", prompt ); cpr_kill_prompt(); - m_free(prompt); + gcry_free(prompt); if( !*fname ) { - m_free( fname ); fname = NULL; + gcry_free( fname ); fname = NULL; fname = defname; defname = NULL; } - m_free(defname); + gcry_free(defname); return fname; } @@ -187,7 +187,7 @@ open_outfile( const char *iname, int mode, IOBUF *a ) const char *newsfx = mode==1 ? ".asc" : mode==2 ? ".sig" : ".gpg"; - buf = m_alloc(strlen(iname)+4+1); + buf = gcry_xmalloc(strlen(iname)+4+1); strcpy(buf,iname); dot = strchr(buf, '.' ); if( dot && dot > buf && dot[1] && strlen(dot) <= 4 @@ -199,7 +199,7 @@ open_outfile( const char *iname, int mode, IOBUF *a ) else strcat( buf, newsfx ); #else - buf = m_alloc(strlen(iname)+4+1); + buf = gcry_xmalloc(strlen(iname)+4+1); strcpy(stpcpy(buf,iname), mode==1 ? ".asc" : mode==2 ? ".sig" : ".gpg"); #endif @@ -216,7 +216,7 @@ open_outfile( const char *iname, int mode, IOBUF *a ) } else rc = G10ERR_FILE_EXISTS; - m_free(buf); + gcry_free(buf); } return rc; } @@ -238,12 +238,12 @@ open_sigfile( const char *iname ) if( len > 4 && ( !strcmp(iname + len - 4, ".sig") || !strcmp(iname + len - 4, ".asc")) ) { char *buf; - buf = m_strdup(iname); + buf = gcry_xstrdup(iname); buf[len-4] = 0 ; a = iobuf_open( buf ); if( opt.verbose ) log_info(_("assuming signed data in `%s'\n"), buf ); - m_free(buf); + gcry_free(buf); } } return a; @@ -265,12 +265,12 @@ copy_options_file( const char *destdir ) if( opt.dry_run ) return; - fname = m_alloc( strlen(datadir) + strlen(destdir) + 15 ); + fname = gcry_xmalloc( strlen(datadir) + strlen(destdir) + 15 ); strcpy(stpcpy(fname, datadir), "/options" SKELEXT ); src = fopen( fname, "r" ); if( !src ) { log_error(_("%s: can't open: %s\n"), fname, strerror(errno) ); - m_free(fname); + gcry_free(fname); return; } strcpy(stpcpy(fname, destdir), "/options" ); @@ -278,7 +278,7 @@ copy_options_file( const char *destdir ) if( !dst ) { log_error(_("%s: can't create: %s\n"), fname, strerror(errno) ); fclose( src ); - m_free(fname); + gcry_free(fname); return; } @@ -293,6 +293,6 @@ copy_options_file( const char *destdir ) fclose( dst ); fclose( src ); log_info(_("%s: new options file created\n"), fname ); - m_free(fname); + gcry_free(fname); } diff --git a/g10/options.h b/g10/options.h index 937d452ed..abb0c7ad0 100644 --- a/g10/options.h +++ b/g10/options.h @@ -100,6 +100,7 @@ struct { #define DBG_PACKET (opt.debug & DBG_PACKET_VALUE) #define DBG_FILTER (opt.debug & DBG_FILTER_VALUE) +#define DBG_MEMORY (opt.debug & DBG_MEMORY_VALUE) #define DBG_CACHE (opt.debug & DBG_CACHE_VALUE) #define DBG_TRUST (opt.debug & DBG_TRUST_VALUE) #define DBG_CIPHER (opt.debug & DBG_CIPHER_VALUE) diff --git a/g10/parse-packet.c b/g10/parse-packet.c index e72e16d65..dffb874b0 100644 --- a/g10/parse-packet.c +++ b/g10/parse-packet.c @@ -28,7 +28,7 @@ #include "iobuf.h" #include "util.h" #include "dummy-cipher.h" -#include "memory.h" +#include #include "filter.h" #include "options.h" #include "main.h" @@ -391,12 +391,12 @@ parse( IOBUF inp, PACKET *pkt, int reqtype, ulong *retpos, switch( pkttype ) { case PKT_PUBLIC_KEY: case PKT_PUBLIC_SUBKEY: - pkt->pkt.public_key = m_alloc_clear(sizeof *pkt->pkt.public_key ); + pkt->pkt.public_key = gcry_xcalloc( 1,sizeof *pkt->pkt.public_key ); rc = parse_key(inp, pkttype, pktlen, hdr, hdrlen, pkt ); break; case PKT_SECRET_KEY: case PKT_SECRET_SUBKEY: - pkt->pkt.secret_key = m_alloc_clear(sizeof *pkt->pkt.secret_key ); + pkt->pkt.secret_key = gcry_xcalloc( 1,sizeof *pkt->pkt.secret_key ); rc = parse_key(inp, pkttype, pktlen, hdr, hdrlen, pkt ); break; case PKT_SYMKEY_ENC: @@ -406,11 +406,11 @@ parse( IOBUF inp, PACKET *pkt, int reqtype, ulong *retpos, rc = parse_pubkeyenc(inp, pkttype, pktlen, pkt ); break; case PKT_SIGNATURE: - pkt->pkt.signature = m_alloc_clear(sizeof *pkt->pkt.signature ); + pkt->pkt.signature = gcry_xcalloc( 1,sizeof *pkt->pkt.signature ); rc = parse_signature(inp, pkttype, pktlen, pkt->pkt.signature ); break; case PKT_ONEPASS_SIG: - pkt->pkt.onepass_sig = m_alloc_clear(sizeof *pkt->pkt.onepass_sig ); + pkt->pkt.onepass_sig = gcry_xcalloc( 1,sizeof *pkt->pkt.onepass_sig ); rc = parse_onepass_sig(inp, pkttype, pktlen, pkt->pkt.onepass_sig ); break; case PKT_USER_ID: @@ -547,7 +547,7 @@ read_rest( IOBUF inp, size_t pktlen ) p = NULL; } else { - p = m_alloc( pktlen ); + p = gcry_xmalloc( pktlen ); for(i=0; pktlen; pktlen--, i++ ) p[i] = iobuf_get(inp); } @@ -597,7 +597,7 @@ parse_symkeyenc( IOBUF inp, int pkttype, unsigned long pktlen, PACKET *packet ) goto leave; } seskeylen = pktlen - minlen; - k = packet->pkt.symkey_enc = m_alloc_clear( sizeof *packet->pkt.symkey_enc + k = packet->pkt.symkey_enc = gcry_xcalloc( 1, sizeof *packet->pkt.symkey_enc + seskeylen - 1 ); k->version = version; k->cipher_algo = cipher_algo; @@ -640,7 +640,7 @@ parse_pubkeyenc( IOBUF inp, int pkttype, unsigned long pktlen, PACKET *packet ) int i, ndata; PKT_pubkey_enc *k; - k = packet->pkt.pubkey_enc = m_alloc_clear(sizeof *packet->pkt.pubkey_enc); + k = packet->pkt.pubkey_enc = gcry_xcalloc( 1,sizeof *packet->pkt.pubkey_enc); if( pktlen < 12 ) { log_error("packet(%d) too short\n", pkttype); goto leave; @@ -1046,7 +1046,7 @@ parse_signature( IOBUF inp, int pkttype, unsigned long pktlen, goto leave; } if( n ) { - sig->hashed_data = m_alloc( n + 2 ); + sig->hashed_data = gcry_xmalloc( n + 2 ); sig->hashed_data[0] = n >> 8; sig->hashed_data[1] = n; if( iobuf_read(inp, sig->hashed_data+2, n ) != n ) { @@ -1063,7 +1063,7 @@ parse_signature( IOBUF inp, int pkttype, unsigned long pktlen, goto leave; } if( n ) { - sig->unhashed_data = m_alloc( n + 2 ); + sig->unhashed_data = gcry_xmalloc( n + 2 ); sig->unhashed_data[0] = n >> 8; sig->unhashed_data[1] = n; if( iobuf_read(inp, sig->unhashed_data+2, n ) != n ) { @@ -1473,7 +1473,7 @@ parse_user_id( IOBUF inp, int pkttype, unsigned long pktlen, PACKET *packet ) { byte *p; - packet->pkt.user_id = m_alloc(sizeof *packet->pkt.user_id + pktlen); + packet->pkt.user_id = gcry_xmalloc(sizeof *packet->pkt.user_id + pktlen); packet->pkt.user_id->len = pktlen; p = packet->pkt.user_id->name; for( ; pktlen; pktlen--, p++ ) @@ -1502,7 +1502,7 @@ parse_comment( IOBUF inp, int pkttype, unsigned long pktlen, PACKET *packet ) { byte *p; - packet->pkt.comment = m_alloc(sizeof *packet->pkt.comment + pktlen - 1); + packet->pkt.comment = gcry_xmalloc(sizeof *packet->pkt.comment + pktlen - 1); packet->pkt.comment->len = pktlen; p = packet->pkt.comment->data; for( ; pktlen; pktlen--, p++ ) @@ -1530,7 +1530,7 @@ parse_trust( IOBUF inp, int pkttype, unsigned long pktlen, PACKET *pkt ) int c; c = iobuf_get_noeof(inp); - pkt->pkt.ring_trust = m_alloc( sizeof *pkt->pkt.ring_trust ); + pkt->pkt.ring_trust = gcry_xmalloc( sizeof *pkt->pkt.ring_trust ); pkt->pkt.ring_trust->trustval = c; if( list_mode ) printf(":trust packet: flag=%02x\n", c ); @@ -1552,7 +1552,7 @@ parse_plaintext( IOBUF inp, int pkttype, unsigned long pktlen, } mode = iobuf_get_noeof(inp); if( pktlen ) pktlen--; namelen = iobuf_get_noeof(inp); if( pktlen ) pktlen--; - pt = pkt->pkt.plaintext = m_alloc(sizeof *pkt->pkt.plaintext + namelen -1); + pt = pkt->pkt.plaintext = gcry_xmalloc(sizeof *pkt->pkt.plaintext + namelen -1); pt->new_ctb = new_ctb; pt->mode = mode; pt->namelen = namelen; @@ -1601,7 +1601,7 @@ parse_compressed( IOBUF inp, int pkttype, unsigned long pktlen, * (this should be the last object in a file or * the compress algorithm should know the length) */ - zd = pkt->pkt.compressed = m_alloc(sizeof *pkt->pkt.compressed ); + zd = pkt->pkt.compressed = gcry_xmalloc(sizeof *pkt->pkt.compressed ); zd->len = 0; /* not yet used */ zd->algorithm = iobuf_get_noeof(inp); zd->new_ctb = new_ctb; @@ -1618,7 +1618,7 @@ parse_encrypted( IOBUF inp, int pkttype, unsigned long pktlen, { PKT_encrypted *ed; - ed = pkt->pkt.encrypted = m_alloc(sizeof *pkt->pkt.encrypted ); + ed = pkt->pkt.encrypted = gcry_xmalloc(sizeof *pkt->pkt.encrypted ); ed->len = pktlen; ed->buf = NULL; ed->new_ctb = new_ctb; diff --git a/g10/passphrase.c b/g10/passphrase.c index ae937e26d..ef59db0d4 100644 --- a/g10/passphrase.c +++ b/g10/passphrase.c @@ -25,7 +25,7 @@ #include #include #include "util.h" -#include "memory.h" +#include #include "options.h" #include "ttyio.h" #include "dummy-cipher.h" @@ -53,10 +53,10 @@ have_static_passphrase() void set_next_passphrase( const char *s ) { - m_free(next_pw); + gcry_free(next_pw); next_pw = NULL; if( s ) { - next_pw = m_alloc_secure( strlen(s)+1 ); + next_pw = gcry_xmalloc_secure( strlen(s)+1 ); strcpy(next_pw, s ); } } @@ -87,7 +87,7 @@ read_passphrase_from_fd( int fd ) if( i >= len-1 ) { char *pw2 = pw; len += 100; - pw = m_alloc_secure( len ); + pw = gcry_xmalloc_secure( len ); if( pw2 ) memcpy(pw, pw2, i ); else @@ -100,7 +100,7 @@ read_passphrase_from_fd( int fd ) if( !opt.batch ) tty_printf("\b\b\b \n" ); - m_free( fd_passwd ); + gcry_free( fd_passwd ); fd_passwd = pw; } @@ -152,7 +152,7 @@ passphrase_to_dek( u32 *keyid, int pubkey_algo, } if( keyid && !opt.batch && !next_pw ) { - PKT_public_key *pk = m_alloc_clear( sizeof *pk ); + PKT_public_key *pk = gcry_xcalloc( 1, sizeof *pk ); size_t n; char *p; @@ -160,7 +160,7 @@ passphrase_to_dek( u32 *keyid, int pubkey_algo, "user: \"") ); p = get_user_id( keyid, &n ); tty_print_utf8_string( p, n ); - m_free(p); + gcry_free(p); tty_printf("\"\n"); if( !get_pubkey( pk, keyid ) ) { @@ -183,12 +183,12 @@ passphrase_to_dek( u32 *keyid, int pubkey_algo, next_pw = NULL; } else if( fd_passwd ) { - pw = m_alloc_secure( strlen(fd_passwd)+1 ); + pw = gcry_xmalloc_secure( strlen(fd_passwd)+1 ); strcpy( pw, fd_passwd ); } else if( opt.batch ) { log_error(_("can't query password in batchmode\n")); - pw = m_strdup( "" ); /* return an empty passphrase */ + pw = gcry_xstrdup( "" ); /* return an empty passphrase */ } else { pw = cpr_get_hidden("passphrase.enter", _("Enter passphrase: ") ); @@ -198,24 +198,24 @@ passphrase_to_dek( u32 *keyid, int pubkey_algo, _("Repeat passphrase: ") ); tty_kill_prompt(); if( strcmp(pw, pw2) ) { - m_free(pw2); - m_free(pw); + gcry_free(pw2); + gcry_free(pw); return NULL; } - m_free(pw2); + gcry_free(pw2); } } if( !pw || !*pw ) write_status( STATUS_MISSING_PASSPHRASE ); - dek = m_alloc_secure( sizeof *dek ); + dek = gcry_xmalloc_secure( sizeof *dek ); dek->algo = cipher_algo; if( !*pw && mode == 2 ) dek->keylen = 0; else hash_passphrase( dek, pw, s2k, mode==2 ); - m_free(last_pw); + gcry_free(last_pw); last_pw = pw; return dek; } diff --git a/g10/pkclist.c b/g10/pkclist.c index 4f47e5563..18c1e9860 100644 --- a/g10/pkclist.c +++ b/g10/pkclist.c @@ -31,7 +31,6 @@ #include "main.h" #include "errors.h" #include "keydb.h" -#include "memory.h" #include "util.h" #include "trustdb.h" #include "ttyio.h" @@ -124,7 +123,7 @@ show_paths( ulong lid, int only_first ) return; } - pk = m_alloc_clear( sizeof *pk ); + pk = gcry_xcalloc( 1, sizeof *pk ); rc = get_pubkey( pk, keyid ); if( rc ) { log_error("key %08lX: public key not found: %s\n", @@ -152,7 +151,7 @@ show_paths( ulong lid, int only_first ) p = get_user_id( keyid, &n ); tty_print_utf8_string( p, n ), - m_free(p); + gcry_free(p); tty_printf("\"\n"); free_public_key( pk ); } @@ -185,7 +184,7 @@ do_edit_ownertrust( ulong lid, int mode, unsigned *new_trust, int defer_help ) return 0; } - pk = m_alloc_clear( sizeof *pk ); + pk = gcry_xcalloc( 1, sizeof *pk ); rc = get_pubkey( pk, keyid ); if( rc ) { log_error("key %08lX: public key not found: %s\n", @@ -206,7 +205,7 @@ do_edit_ownertrust( ulong lid, int mode, unsigned *new_trust, int defer_help ) (ulong)keyid[1], datestr_from_pk( pk ) ); p = get_user_id( keyid, &n ); tty_print_utf8_string( p, n ), - m_free(p); + gcry_free(p); tty_printf("\"\n"); print_fpr( pk ); tty_printf("\n"); @@ -262,10 +261,10 @@ do_edit_ownertrust( ulong lid, int mode, unsigned *new_trust, int defer_help ) quit = 1; break ; /* back to the menu */ } - m_free(p); p = NULL; + gcry_free(p); p = NULL; } - m_free(p); - m_free(pk); + gcry_free(p); + gcry_free(pk); return show? -2: quit? -1 : changed; } @@ -463,7 +462,7 @@ do_we_trust_pre( PKT_public_key *pk, int trustlevel ) (ulong)keyid[1], datestr_from_pk( pk ) ); p = get_user_id( keyid, &n ); tty_print_utf8_string( p, n ), - m_free(p); + gcry_free(p); tty_printf("\"\n"); print_fpr( pk ); tty_printf("\n"); @@ -498,7 +497,7 @@ do_we_trust_pre( PKT_public_key *pk, int trustlevel ) int check_signatures_trust( PKT_signature *sig ) { - PKT_public_key *pk = m_alloc_clear( sizeof *pk ); + PKT_public_key *pk = gcry_xcalloc( 1, sizeof *pk ); int trustlevel; int did_add = 0; int rc=0; @@ -622,7 +621,7 @@ release_pk_list( PK_LIST pk_list ) for( ; pk_list; pk_list = pk_rover ) { pk_rover = pk_list->next; free_public_key( pk_list->pk ); - m_free( pk_list ); + gcry_free( pk_list ); } } @@ -651,10 +650,10 @@ default_recipient(void) int i; if( opt.def_recipient ) - return m_strdup( opt.def_recipient ); + return gcry_xstrdup( opt.def_recipient ); if( !opt.def_recipient_self ) return NULL; - sk = m_alloc_clear( sizeof *sk ); + sk = gcry_xcalloc( 1, sizeof *sk ); i = get_seckey_byname( sk, NULL, 0 ); if( i ) { free_secret_key( sk ); @@ -663,7 +662,7 @@ default_recipient(void) n = MAX_FINGERPRINT_LEN; fingerprint_from_sk( sk, fpr, &n ); free_secret_key( sk ); - p = m_alloc( 2*n+3 ); + p = gcry_xmalloc( 2*n+3 ); *p++ = '0'; *p++ = 'x'; for(i=0; i < n; i++ ) @@ -689,7 +688,7 @@ build_pk_list( STRLIST remusr, PK_LIST *ret_pk_list, unsigned use ) if( !(rov->flags & 1) ) any_recipients = 1; else if( (use & GCRY_PK_USAGE_ENCR) && !opt.no_encrypt_to ) { - pk = m_alloc_clear( sizeof *pk ); + pk = gcry_xcalloc( 1, sizeof *pk ); pk->pubkey_usage = use; if( (rc = get_pubkey_byname( NULL, pk, rov->d, NULL )) ) { free_public_key( pk ); pk = NULL; @@ -706,7 +705,7 @@ build_pk_list( STRLIST remusr, PK_LIST *ret_pk_list, unsigned use ) } else { PK_LIST r; - r = m_alloc( sizeof *r ); + r = gcry_xmalloc( sizeof *r ); r->pk = pk; pk = NULL; r->next = pk_list; r->mark = 0; @@ -731,7 +730,7 @@ build_pk_list( STRLIST remusr, PK_LIST *ret_pk_list, unsigned use ) "You did not specify a user ID. (you may use \"-r\")\n\n")); for(;;) { rc = 0; - m_free(answer); + gcry_free(answer); if( have_def_rec ) { answer = def_rec; def_rec = NULL; @@ -746,7 +745,7 @@ build_pk_list( STRLIST remusr, PK_LIST *ret_pk_list, unsigned use ) break; if( pk ) free_public_key( pk ); - pk = m_alloc_clear( sizeof *pk ); + pk = gcry_xcalloc( 1, sizeof *pk ); pk->pubkey_usage = use; rc = get_pubkey_byname( NULL, pk, answer, NULL ); if( rc ) @@ -759,7 +758,7 @@ build_pk_list( STRLIST remusr, PK_LIST *ret_pk_list, unsigned use ) "already set as default recipient\n") ); } else { - PK_LIST r = m_alloc( sizeof *r ); + PK_LIST r = gcry_xmalloc( sizeof *r ); r->pk = pk; pk = NULL; r->next = pk_list; r->mark = 0; @@ -790,7 +789,7 @@ build_pk_list( STRLIST remusr, PK_LIST *ret_pk_list, unsigned use ) else { PK_LIST r; - r = m_alloc( sizeof *r ); + r = gcry_xmalloc( sizeof *r ); r->pk = pk; pk = NULL; r->next = pk_list; r->mark = 0; @@ -801,23 +800,23 @@ build_pk_list( STRLIST remusr, PK_LIST *ret_pk_list, unsigned use ) } } } - m_free(def_rec); def_rec = NULL; + gcry_free(def_rec); def_rec = NULL; have_def_rec = 0; } - m_free(answer); + gcry_free(answer); if( pk ) { free_public_key( pk ); pk = NULL; } } else if( !any_recipients && (def_rec = default_recipient()) ) { - pk = m_alloc_clear( sizeof *pk ); + pk = gcry_xcalloc( 1, sizeof *pk ); pk->pubkey_usage = use; rc = get_pubkey_byname( NULL, pk, def_rec, NULL ); if( rc ) log_error(_("unknown default recipient `%s'\n"), def_rec ); else if( !(rc=openpgp_pk_test_algo(pk->pubkey_algo, use)) ) { - PK_LIST r = m_alloc( sizeof *r ); + PK_LIST r = gcry_xmalloc( sizeof *r ); r->pk = pk; pk = NULL; r->next = pk_list; r->mark = 0; @@ -828,7 +827,7 @@ build_pk_list( STRLIST remusr, PK_LIST *ret_pk_list, unsigned use ) free_public_key( pk ); pk = NULL; } - m_free(def_rec); def_rec = NULL; + gcry_free(def_rec); def_rec = NULL; } else { any_recipients = 0; @@ -836,7 +835,7 @@ build_pk_list( STRLIST remusr, PK_LIST *ret_pk_list, unsigned use ) if( (remusr->flags & 1) ) continue; /* encrypt-to keys are already handled */ - pk = m_alloc_clear( sizeof *pk ); + pk = gcry_xcalloc( 1, sizeof *pk ); pk->pubkey_usage = use; if( (rc = get_pubkey_byname( NULL, pk, remusr->d, NULL )) ) { free_public_key( pk ); pk = NULL; @@ -872,7 +871,7 @@ build_pk_list( STRLIST remusr, PK_LIST *ret_pk_list, unsigned use ) } else { PK_LIST r; - r = m_alloc( sizeof *r ); + r = gcry_xmalloc( sizeof *r ); r->pk = pk; pk = NULL; r->next = pk_list; r->mark = 0; @@ -953,7 +952,7 @@ select_algo_from_prefs( PK_LIST pk_list, int preftype ) } if( preftype == PREFTYPE_SYM ) mask[0] |= (1<<2); /* 3DES is implicitly there */ - m_free(pref); + gcry_free(pref); pref = get_pref_data( pkr->pk->local_id, pkr->pk->namehash, &npref); any = 0; if( pref ) { @@ -1026,7 +1025,7 @@ select_algo_from_prefs( PK_LIST pk_list, int preftype ) i = 1; /* yep; we can use compression algo 1 */ } - m_free(pref); + gcry_free(pref); return i; } diff --git a/g10/plaintext.c b/g10/plaintext.c index 592455f12..88b76bc70 100644 --- a/g10/plaintext.c +++ b/g10/plaintext.c @@ -25,7 +25,7 @@ #include #include #include "util.h" -#include "memory.h" +#include #include "options.h" #include "packet.h" #include "ttyio.h" @@ -56,7 +56,7 @@ handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx, if( nooutput ) ; else if( opt.outfile ) { - fname = m_alloc( strlen( opt.outfile ) + 1); + fname = gcry_xmalloc( strlen( opt.outfile ) + 1); strcpy(fname, opt.outfile ); } else if( pt->namelen == 8 && !memcmp( pt->name, "_CONSOLE", 8 ) ) { @@ -120,7 +120,7 @@ handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx, } } else { /* binary mode */ - byte *buffer = m_alloc( 32768 ); + byte *buffer = gcry_xmalloc( 32768 ); while( pt->len ) { int len = pt->len > 32768 ? 32768 : pt->len; len = iobuf_read( pt->buf, buffer, len ); @@ -128,7 +128,7 @@ handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx, log_error("Problem reading source (%u bytes remaining)\n", (unsigned)pt->len); rc = G10ERR_READ_FILE; - m_free( buffer ); + gcry_free( buffer ); goto leave; } if( mfx->md ) @@ -138,13 +138,13 @@ handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx, log_error("Error writing to `%s': %s\n", fname, strerror(errno) ); rc = G10ERR_WRITE_FILE; - m_free( buffer ); + gcry_free( buffer ); goto leave; } } pt->len -= len; } - m_free( buffer ); + gcry_free( buffer ); } } else if( !clearsig ) { @@ -165,7 +165,7 @@ handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx, } } else { /* binary mode */ - byte *buffer = m_alloc( 32768 ); + byte *buffer = gcry_xmalloc( 32768 ); int eof; for( eof=0; !eof; ) { /* Why do we check for len < 32768: @@ -186,12 +186,12 @@ handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx, log_error("Error writing to `%s': %s\n", fname, strerror(errno) ); rc = G10ERR_WRITE_FILE; - m_free( buffer ); + gcry_free( buffer ); goto leave; } } } - m_free( buffer ); + gcry_free( buffer ); } pt->buf = NULL; } @@ -248,7 +248,7 @@ handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx, leave: if( fp && fp != stdout ) fclose(fp); - m_free(fname); + gcry_free(fname); return rc; } @@ -310,7 +310,7 @@ ask_for_detached_datafile( GCRY_MD_HD md, GCRY_MD_HD md2, int any=0; tty_printf(_("Detached signature.\n")); do { - m_free(answer); + gcry_free(answer); answer = cpr_get("detached_signature.filename", _("Please enter name of data file: ")); cpr_kill_prompt(); @@ -342,7 +342,7 @@ ask_for_detached_datafile( GCRY_MD_HD md, GCRY_MD_HD md2, leave: - m_free(answer); + gcry_free(answer); return rc; } diff --git a/g10/pubkey-enc.c b/g10/pubkey-enc.c index 94fe2777b..925b5a4d0 100644 --- a/g10/pubkey-enc.c +++ b/g10/pubkey-enc.c @@ -24,7 +24,7 @@ #include #include #include "util.h" -#include "memory.h" +#include #include "packet.h" #include "main.h" #include "keydb.h" @@ -55,7 +55,7 @@ get_session_key( PKT_pubkey_enc *k, DEK *dek ) goto leave; if( k->keyid[0] || k->keyid[1] ) { - sk = m_alloc_clear( sizeof *sk ); + sk = gcry_xcalloc( 1, sizeof *sk ); sk->pubkey_algo = k->pubkey_algo; /* we want a pubkey with this algo*/ if( !(rc = get_seckey( sk, k->keyid )) ) rc = get_it( k, dek, sk, k->keyid ); @@ -67,7 +67,7 @@ get_session_key( PKT_pubkey_enc *k, DEK *dek ) for(;;) { if( sk ) free_secret_key( sk ); - sk = m_alloc_clear( sizeof *sk ); + sk = gcry_xcalloc( 1, sizeof *sk ); rc=enum_secret_keys( &enum_context, sk, 1); if( rc ) { rc = G10ERR_NO_SECKEY; @@ -102,13 +102,16 @@ get_it( PKT_pubkey_enc *k, DEK *dek, PKT_secret_key *sk, u32 *keyid ) int rc; MPI plain_dek = NULL; byte *frame = NULL; - unsigned n, nframe; + unsigned n; + size_t nframe; u16 csum, csum2; rc = pubkey_decrypt(sk->pubkey_algo, &plain_dek, k->data, sk->skey ); if( rc ) goto leave; - frame = mpi_get_buffer( plain_dek, &nframe, NULL ); + if( gcry_mpi_aprint( GCRYMPI_FMT_USG, &frame, &nframe, plain_dek ) ) + BUG(); + mpi_release( plain_dek ); plain_dek = NULL; /* Now get the DEK (data encryption key) from the frame @@ -121,7 +124,8 @@ get_it( PKT_pubkey_enc *k, DEK *dek, PKT_secret_key *sk, u32 *keyid ) * * 0 2 RND(n bytes) 0 A DEK(k bytes) CSUM(2 bytes) * - * (mpi_get_buffer already removed the leading zero). + * (mpi_get_buffer already removed the leading zero - still true + * for gcry_mpi_aprint(0 which is used now?) * * RND are non-zero randow bytes. * A is the cipher algorithm @@ -174,7 +178,7 @@ get_it( PKT_pubkey_enc *k, DEK *dek, PKT_secret_key *sk, u32 *keyid ) log_hexdump("DEK is:", dek->key, dek->keylen ); /* check that the algo is in the preferences */ { - PKT_public_key *pk = m_alloc_clear( sizeof *pk ); + PKT_public_key *pk = gcry_xcalloc( 1, sizeof *pk ); if( (rc = get_pubkey( pk, keyid )) ) log_error("public key problem: %s\n", g10_errstr(rc) ); else if( !pk->local_id && query_trust_record(pk) ) @@ -197,7 +201,7 @@ get_it( PKT_pubkey_enc *k, DEK *dek, PKT_secret_key *sk, u32 *keyid ) leave: mpi_release(plain_dek); - m_free(frame); + gcry_free(frame); return rc; } diff --git a/g10/revoke.c b/g10/revoke.c index 64c9c764c..e71124515 100644 --- a/g10/revoke.c +++ b/g10/revoke.c @@ -29,7 +29,7 @@ #include "packet.h" #include "errors.h" #include "keydb.h" -#include "memory.h" +#include #include "util.h" #include "main.h" #include "ttyio.h" @@ -114,10 +114,10 @@ gen_revoke( const char *uname ) size_t n; char *p = get_user_id( sk_keyid, &n ); tty_print_utf8_string( p, n ); - m_free(p); + gcry_free(p); tty_printf("\n"); } - pk = m_alloc_clear( sizeof *pk ); + pk = gcry_xcalloc( 1, sizeof *pk ); rc = get_pubkey( pk, sk_keyid ); if( rc ) { log_error(_("no corresponding public key: %s\n"), g10_errstr(rc) ); diff --git a/g10/ringedit.c b/g10/ringedit.c index b5eedd6ef..b4091f41c 100644 --- a/g10/ringedit.c +++ b/g10/ringedit.c @@ -53,7 +53,7 @@ #endif #include "util.h" #include "packet.h" -#include "memory.h" +#include #include "iobuf.h" #include "keydb.h" #include "host2net.h" @@ -234,7 +234,7 @@ add_keyblock_resource( const char *url, int force, int secret ) filename = make_filename(opt.homedir, resname, NULL); } else - filename = m_strdup( resname ); + filename = gcry_xstrdup( resname ); if( !force ) force = secret? !any_secret : !any_public; @@ -373,7 +373,7 @@ add_keyblock_resource( const char *url, int force, int secret ) /* fixme: avoid duplicate resources */ resource_table[i].used = 1; resource_table[i].secret = !!secret; - resource_table[i].fname = m_strdup(filename); + resource_table[i].fname = gcry_xstrdup(filename); resource_table[i].iobuf = iobuf; resource_table[i].rt = rt; if( secret ) @@ -388,7 +388,7 @@ add_keyblock_resource( const char *url, int force, int secret ) any_secret = 1; else any_public = 1; - m_free( filename ); + gcry_free( filename ); return rc; } @@ -498,7 +498,7 @@ int find_keyblock_byname( KBPOS *kbpos, const char *username ) { PACKET pkt; - PKT_public_key *pk = m_alloc_clear( sizeof *pk ); + PKT_public_key *pk = gcry_xcalloc( 1, sizeof *pk ); int rc; rc = get_pubkey_byname( NULL, pk, username, NULL ); @@ -559,7 +559,7 @@ int find_secret_keyblock_byname( KBPOS *kbpos, const char *username ) { PACKET pkt; - PKT_secret_key *sk = m_alloc_clear( sizeof *sk ); + PKT_secret_key *sk = gcry_xcalloc( 1, sizeof *sk ); int rc; rc = get_seckey_byname( sk, username, 0 ); @@ -828,7 +828,7 @@ enum_keyblocks( int mode, KBPOS *kbpos, KBNODE *ret_root ) } /* release pending packet */ free_packet( kbpos->pkt ); - m_free( kbpos->pkt ); + gcry_free( kbpos->pkt ); } return rc; } @@ -954,7 +954,7 @@ compile_bm_table( const byte *pattern, size_t len ) ushort *dist; int i; - dist = m_alloc_clear( 256 * sizeof *dist ); + dist = gcry_xcalloc( 1, 256 * sizeof *dist ); for(i=0; i < 256; i++ ) dist[i] = len; for(i=0; i < len-1; i++ ) @@ -1012,7 +1012,7 @@ scan_user_file_open( const byte *name ) size_t *dist; int i; - hd = m_alloc_clear( sizeof *hd ); + hd = gcry_xcalloc( 1, sizeof *hd ); dist = hd->dist; /* compile the distance table */ for(i=0; i < 256; i++ ) @@ -1027,7 +1027,7 @@ scan_user_file_open( const byte *name ) static int scan_user_file_close( SCAN_USER_HANDLE hd ) { - m_free( hd ); + gcry_free( hd ); } static int @@ -1175,7 +1175,7 @@ keyring_read( KBPOS *kbpos, KBNODE *ret_root ) return G10ERR_KEYRING_OPEN; } - pkt = m_alloc( sizeof *pkt ); + pkt = gcry_xmalloc( sizeof *pkt ); init_packet(pkt); kbpos->count=0; while( (rc=parse_packet(a, pkt)) != -1 ) { @@ -1209,7 +1209,7 @@ keyring_read( KBPOS *kbpos, KBNODE *ret_root ) root = new_kbnode( pkt ); else add_kbnode( root, new_kbnode( pkt ) ); - pkt = m_alloc( sizeof *pkt ); + pkt = gcry_xmalloc( sizeof *pkt ); init_packet(pkt); break; } @@ -1224,7 +1224,7 @@ keyring_read( KBPOS *kbpos, KBNODE *ret_root ) else *ret_root = root; free_packet( pkt ); - m_free( pkt ); + gcry_free( pkt ); iobuf_close(a); return rc; } @@ -1246,7 +1246,7 @@ keyring_enum( KBPOS *kbpos, KBNODE *ret_root, int skipsigs ) kbpos->pkt = NULL; } - pkt = m_alloc( sizeof *pkt ); + pkt = gcry_xmalloc( sizeof *pkt ); init_packet(pkt); while( (rc=parse_packet(kbpos->fp, pkt)) != -1 ) { if( rc ) { /* ignore errors */ @@ -1275,7 +1275,7 @@ keyring_enum( KBPOS *kbpos, KBNODE *ret_root, int skipsigs ) goto ready; } root = new_kbnode( pkt ); - pkt = m_alloc( sizeof *pkt ); + pkt = gcry_xmalloc( sizeof *pkt ); init_packet(pkt); break; @@ -1293,7 +1293,7 @@ keyring_enum( KBPOS *kbpos, KBNODE *ret_root, int skipsigs ) break; } add_kbnode( root, new_kbnode( pkt ) ); - pkt = m_alloc( sizeof *pkt ); + pkt = gcry_xmalloc( sizeof *pkt ); init_packet(pkt); break; } @@ -1307,7 +1307,7 @@ keyring_enum( KBPOS *kbpos, KBNODE *ret_root, int skipsigs ) else *ret_root = root; free_packet( pkt ); - m_free( pkt ); + gcry_free( pkt ); return rc; } @@ -1391,23 +1391,23 @@ keyring_copy( KBPOS *kbpos, int mode, KBNODE root ) */ if( strlen(rentry->fname) > 4 && !strcmp(rentry->fname+strlen(rentry->fname)-4, ".gpg") ) { - bakfname = m_alloc( strlen( rentry->fname ) + 1 ); + bakfname = gcry_xmalloc( strlen( rentry->fname ) + 1 ); strcpy(bakfname,rentry->fname); strcpy(bakfname+strlen(rentry->fname)-4, ".bak"); - tmpfname = m_alloc( strlen( rentry->fname ) + 1 ); + tmpfname = gcry_xmalloc( strlen( rentry->fname ) + 1 ); strcpy(tmpfname,rentry->fname); strcpy(tmpfname+strlen(rentry->fname)-4, ".tmp"); } else { /* file does not end with gpg; hmmm */ - bakfname = m_alloc( strlen( rentry->fname ) + 5 ); + bakfname = gcry_xmalloc( strlen( rentry->fname ) + 5 ); strcpy(stpcpy(bakfname,rentry->fname),".bak"); - tmpfname = m_alloc( strlen( rentry->fname ) + 5 ); + tmpfname = gcry_xmalloc( strlen( rentry->fname ) + 5 ); strcpy(stpcpy(tmpfname,rentry->fname),".tmp"); } #else - bakfname = m_alloc( strlen( rentry->fname ) + 2 ); + bakfname = gcry_xmalloc( strlen( rentry->fname ) + 2 ); strcpy(stpcpy(bakfname,rentry->fname),"~"); - tmpfname = m_alloc( strlen( rentry->fname ) + 5 ); + tmpfname = gcry_xmalloc( strlen( rentry->fname ) + 5 ); strcpy(stpcpy(tmpfname,rentry->fname),".tmp"); #endif newfp = iobuf_create( tmpfname ); @@ -1538,8 +1538,8 @@ keyring_copy( KBPOS *kbpos, int mode, KBNODE root ) leave: unlock_rentry( rentry ); - m_free(bakfname); - m_free(tmpfname); + gcry_free(bakfname); + gcry_free(tmpfname); return rc; } @@ -1704,25 +1704,25 @@ do_gdbm_locate_by_keyid( GDBM_FILE dbf, KBPOS *kbpos, u32 *keyid ) if( content.dsize < 2 ) { log_error("gdbm_fetch did not return enough data\n" ); - free( content.dptr ); /* can't use m_free() here */ + free( content.dptr ); /* can't use gcry_free() here */ return G10ERR_INV_KEYRING; } if( *content.dptr != 2 ) { log_error("gdbm_fetch returned unexpected type %d\n", *(byte*)content.dptr ); - free( content.dptr ); /* can't use m_free() here */ + free( content.dptr ); /* can't use gcry_free() here */ return G10ERR_INV_KEYRING; } if( content.dsize < 21 ) { log_error("gdbm_fetch did not return a complete fingerprint\n" ); - free( content.dptr ); /* can't use m_free() here */ + free( content.dptr ); /* can't use gcry_free() here */ return G10ERR_INV_KEYRING; } if( content.dsize > 21 ) log_info("gdbm_fetch: WARNING: more than one fingerprint\n" ); rc = do_gdbm_locate( dbf, kbpos, content.dptr+1, 20 ); - free( content.dptr ); /* can't use m_free() here */ + free( content.dptr ); /* can't use gcry_free() here */ return rc; } @@ -1750,20 +1750,20 @@ do_gdbm_read( KBPOS *kbpos, KBNODE *ret_root ) } if( content.dsize < 2 ) { log_error("gdbm_fetch did not return enough data\n" ); - free( content.dptr ); /* can't use m_free() here */ + free( content.dptr ); /* can't use gcry_free() here */ return G10ERR_INV_KEYRING; } if( *content.dptr != 1 ) { log_error("gdbm_fetch returned unexpected type %d\n", *(byte*)content.dptr ); - free( content.dptr ); /* can't use m_free() here */ + free( content.dptr ); /* can't use gcry_free() here */ return G10ERR_INV_KEYRING; } a = iobuf_temp_with_content( content.dptr+1, content.dsize-1 ); - free( content.dptr ); /* can't use m_free() here */ + free( content.dptr ); /* can't use gcry_free() here */ - pkt = m_alloc( sizeof *pkt ); + pkt = gcry_xmalloc( sizeof *pkt ); init_packet(pkt); kbpos->count=0; while( (rc=parse_packet(a, pkt)) != -1 ) { @@ -1784,7 +1784,7 @@ do_gdbm_read( KBPOS *kbpos, KBNODE *ret_root ) root = new_kbnode( pkt ); else add_kbnode( root, new_kbnode( pkt ) ); - pkt = m_alloc( sizeof *pkt ); + pkt = gcry_xmalloc( sizeof *pkt ); init_packet(pkt); } if( rc == -1 && root ) @@ -1794,7 +1794,7 @@ do_gdbm_read( KBPOS *kbpos, KBNODE *ret_root ) else *ret_root = root; free_packet( pkt ); - m_free( pkt ); + gcry_free( pkt ); iobuf_close(a); return rc; } @@ -1824,18 +1824,18 @@ do_gdbm_enum( KBPOS *kbpos, KBNODE *ret_root ) while( key.dptr && (!key.dsize || *key.dptr != 1) ) { helpkey = key; key = gdbm_nextkey( rentry->dbf, helpkey ); - free( helpkey.dptr ); /* free and not m_free() ! */ + free( helpkey.dptr ); /* free and not gcry_free() ! */ } if( !key.dptr ) return -1; /* eof */ if( key.dsize < 21 ) { - free( key.dptr ); /* free and not m_free() ! */ + free( key.dptr ); /* free and not gcry_free() ! */ log_error("do_gdm_enum: key is too short\n" ); return G10ERR_INV_KEYRING; } memcpy( kbpos->keybuf, key.dptr, 21 ); - free( key.dptr ); /* free and not m_free() ! */ + free( key.dptr ); /* free and not gcry_free() ! */ return do_gdbm_read( kbpos, ret_root ); } diff --git a/g10/seckey-cert.c b/g10/seckey-cert.c index af4110590..10f7092d2 100644 --- a/g10/seckey-cert.c +++ b/g10/seckey-cert.c @@ -26,7 +26,6 @@ #include #include "util.h" -#include "memory.h" #include "packet.h" #include "keydb.h" #include "main.h" @@ -59,7 +58,7 @@ do_check( PKT_secret_key *sk ) keyid_from_sk( sk, keyid ); keyid[2] = keyid[3] = 0; if( !sk->is_primary ) { - PKT_secret_key *sk2 = m_alloc_clear( sizeof *sk2 ); + PKT_secret_key *sk2 = gcry_xcalloc( 1, sizeof *sk2 ); if( !get_primary_seckey( sk2, keyid ) ) keyid_from_sk( sk2, keyid+2 ); free_secret_key( sk2 ); @@ -77,7 +76,7 @@ do_check( PKT_secret_key *sk ) if( gcry_cipher_setkey( cipher_hd, dek->key, dek->keylen ) ) log_fatal("set key failed: %s\n", gcry_strerror(-1) ); - m_free(dek); + gcry_free(dek); save_sk = copy_secret_key( NULL, sk ); if( gcry_cipher_setiv( cipher_hd, sk->protect.iv, sk->protect.ivlen )) log_fatal("set IV failed: %s\n", gcry_strerror(-1) ); @@ -91,7 +90,7 @@ do_check( PKT_secret_key *sk ) assert( gcry_mpi_get_flag( sk->skey[i], GCRYMPI_FLAG_OPAQUE ) ); p = gcry_mpi_get_opaque( sk->skey[i], &ndatabits ); ndata = (ndatabits+7)/8; - data = m_alloc_secure( ndata ); + data = gcry_xmalloc_secure( ndata ); gcry_cipher_decrypt( cipher_hd, data, ndata, p, ndata ); mpi_release( sk->skey[i] ); sk->skey[i] = NULL ; p = data; @@ -109,7 +108,7 @@ do_check( PKT_secret_key *sk ) if( sk->csum == csum ) { for( ; i < pubkey_get_nskey(sk->pubkey_algo); i++ ) { nbytes = ndata; - assert( m_is_secure( p ) ); + assert( gcry_is_secure( p ) ); res = gcry_mpi_scan( &sk->skey[i], GCRYMPI_FMT_PGP, p, &nbytes); if( res ) @@ -119,7 +118,7 @@ do_check( PKT_secret_key *sk ) p += nbytes; } } - m_free(data); + gcry_free(data); } else { for(i=pubkey_get_npkey(sk->pubkey_algo); @@ -131,7 +130,7 @@ do_check( PKT_secret_key *sk ) assert( gcry_mpi_get_flag( sk->skey[i], GCRYMPI_FLAG_OPAQUE ) ); p = gcry_mpi_get_opaque( sk->skey[i], &ndatabits ); ndata = (ndatabits+7)/8; - data = m_alloc_secure( ndata ); + data = gcry_xmalloc_secure( ndata ); gcry_cipher_sync( cipher_hd ); gcry_cipher_decrypt( cipher_hd, data, ndata, p, ndata ); mpi_release( sk->skey[i] ); sk->skey[i] = NULL ; @@ -142,7 +141,7 @@ do_check( PKT_secret_key *sk ) log_bug("gcry_mpi_scan failed in do_check: rc=%d\n", res); csum += checksum_mpi( sk->skey[i] ); - m_free( buffer ); + gcry_free( buffer ); } } gcry_cipher_close( cipher_hd ); @@ -287,15 +286,19 @@ protect_secret_key( PKT_secret_key *sk, DEK *dek ) for(j=0, i = pubkey_get_npkey(sk->pubkey_algo); i < pubkey_get_nskey(sk->pubkey_algo); i++, j++ ) { assert( !gcry_mpi_get_flag( sk->skey[i], GCRYMPI_FLAG_OPAQUE ) ); - bufarr[j] = mpi_get_buffer( sk->skey[i], &narr[j], NULL ); - nbits[j] = mpi_get_nbits( sk->skey[i] ); + + if( gcry_mpi_aprint( GCRYMPI_FMT_USG, (char*)bufarr+j, + narr+j, sk->skey[i])) + BUG(); + + nbits[j] = gcry_mpi_get_nbits( sk->skey[i] ); ndata += narr[j] + 2; } for( ; j < NMPIS; j++ ) bufarr[j] = NULL; ndata += 2; /* for checksum */ - data = m_alloc_secure( ndata ); + data = gcry_xmalloc_secure( ndata ); p = data; for(j=0; j < NMPIS && bufarr[j]; j++ ) { p[0] = nbits[j] >> 8 ; @@ -303,7 +306,7 @@ protect_secret_key( PKT_secret_key *sk, DEK *dek ) p += 2; memcpy(p, bufarr[j], narr[j] ); p += narr[j]; - m_free(bufarr[j]); + gcry_free(bufarr[j]); } #undef NMPIS csum = checksum( data, ndata-2); @@ -328,12 +331,20 @@ protect_secret_key( PKT_secret_key *sk, DEK *dek ) for(i=pubkey_get_npkey(sk->pubkey_algo); i < pubkey_get_nskey(sk->pubkey_algo); i++ ) { csum += checksum_mpi( sk->skey[i] ); - buffer = mpi_get_buffer( sk->skey[i], &nbytes, NULL ); + + if( gcry_mpi_aprint( GCRYMPI_FMT_USG, + &buffer, &nbytes, sk->skey[i] ) ) + BUG(); + gcry_cipher_sync( cipher_hd ); assert( !gcry_mpi_get_flag( sk->skey[i], GCRYMPI_FLAG_OPAQUE ) ); gcry_cipher_encrypt( cipher_hd, buffer, nbytes, NULL, 0 ); - mpi_set_buffer( sk->skey[i], buffer, nbytes, 0 ); - m_free( buffer ); + gcry_mpi_release( sk->skey[i] ); + if( gcry_mpi_scan( &sk->skey[i], GCRYMPI_FMT_USG, + buffer,&nbytes ) ) + BUG(); + + gcry_free( buffer ); } sk->csum = csum; } diff --git a/g10/seskey.c b/g10/seskey.c index eac15c41f..261e904b7 100644 --- a/g10/seskey.c +++ b/g10/seskey.c @@ -29,7 +29,6 @@ #include "dummy-cipher.h" #include "main.h" #include "i18n.h" -#include "memory.h" /**************** @@ -110,7 +109,7 @@ encode_session_key( DEK *dek, unsigned nbits ) for( p = dek->key, i=0; i < dek->keylen; i++ ) csum += *p++; - frame = m_alloc_secure( nframe ); + frame = gcry_xmalloc_secure( nframe ); n = 0; frame[n++] = 0; frame[n++] = 2; @@ -133,10 +132,10 @@ encode_session_key( DEK *dek, unsigned nbits ) for(j=0; j < i && k ; j++ ) if( !p[j] ) p[j] = pp[--k]; - m_free(pp); + gcry_free(pp); } memcpy( frame+n, p, i ); - m_free(p); + gcry_free(p); n += i; frame[n++] = 0; frame[n++] = dek->algo; @@ -144,9 +143,10 @@ encode_session_key( DEK *dek, unsigned nbits ) frame[n++] = csum >>8; frame[n++] = csum; assert( n == nframe ); - a = mpi_secure_new( nframe ); - mpi_set_buffer( a, frame, nframe, 0 ); - m_free(frame); + if( gcry_mpi_scan( &a, GCRYMPI_FMT_USG, frame, &nframe ) ) + BUG(); + gcry_free(frame); + return a; } @@ -170,8 +170,8 @@ do_encode_md( GCRY_MD_HD md, int algo, size_t len, unsigned nbits, * * PAD consists of FF bytes. */ - frame = gcry_md_is_secure(md)? m_alloc_secure( nframe ) - : m_alloc( nframe ); + frame = gcry_md_is_secure(md)? gcry_xmalloc_secure( nframe ) + : gcry_xmalloc( nframe ); n = 0; frame[n++] = 0; frame[n++] = algo; @@ -182,9 +182,9 @@ do_encode_md( GCRY_MD_HD md, int algo, size_t len, unsigned nbits, memcpy( frame+n, asn, asnlen ); n += asnlen; memcpy( frame+n, gcry_md_read(md, algo), len ); n += len; assert( n == nframe ); - a = gcry_md_is_secure(md)? mpi_secure_new( nframe ) : mpi_new( nframe ); - mpi_set_buffer( a, frame, nframe, 0 ); - m_free(frame); + if( gcry_mpi_scan( &a, GCRYMPI_FMT_USG, frame, &nframe ) ) + BUG(); + gcry_free(frame); return a; } @@ -196,11 +196,10 @@ encode_md_value( int pubkey_algo, GCRY_MD_HD md, int hash_algo, unsigned nbits ) MPI frame; if( pubkey_algo == GCRY_PK_DSA ) { - frame = gcry_md_is_secure(md)? - mpi_secure_new( gcry_md_get_algo_dlen(hash_algo) ) - : mpi_new( gcry_md_get_algo_dlen(hash_algo) ); - mpi_set_buffer( frame, gcry_md_read(md, hash_algo), - gcry_md_get_algo_dlen(hash_algo), 0 ); + size_t n = gcry_md_get_algo_dlen(hash_algo); + if( gcry_mpi_scan( &frame, GCRYMPI_FMT_USG, + gcry_md_read(md, hash_algo), &n ) ) + BUG(); } else { byte *asn; @@ -209,12 +208,12 @@ encode_md_value( int pubkey_algo, GCRY_MD_HD md, int hash_algo, unsigned nbits ) if( gcry_md_algo_info( algo, GCRYCTL_GET_ASNOID, NULL, &asnlen ) ) log_fatal("can't get OID of algo %d: %s\n", algo, gcry_strerror(-1)); - asn = m_alloc( asnlen ); + asn = gcry_xmalloc( asnlen ); if( gcry_md_algo_info( algo, GCRYCTL_GET_ASNOID, asn, &asnlen ) ) BUG(); frame = do_encode_md( md, algo, gcry_md_get_algo_dlen( algo ), nbits, asn, asnlen ); - m_free( asn ); + gcry_free( asn ); } return frame; } diff --git a/g10/sig-check.c b/g10/sig-check.c index 842055d17..aec50ab38 100644 --- a/g10/sig-check.c +++ b/g10/sig-check.c @@ -27,7 +27,6 @@ #include #include "util.h" #include "packet.h" -#include "memory.h" #include "keydb.h" #include "main.h" #include "status.h" @@ -138,7 +137,7 @@ signature_check( PKT_signature *sig, GCRY_MD_HD digest ) static int do_signature_check( PKT_signature *sig, GCRY_MD_HD digest, u32 *r_expire ) { - PKT_public_key *pk = m_alloc_clear( sizeof *pk ); + PKT_public_key *pk = gcry_xcalloc( 1, sizeof *pk ); int rc=0; if( is_RSA(sig->pubkey_algo) ) @@ -176,22 +175,23 @@ do_signature_check( PKT_signature *sig, GCRY_MD_HD digest, u32 *r_expire ) gcry_md_putc( digest, (a >> 8) & 0xff ); gcry_md_putc( digest, a & 0xff ); for(i=0; i < nsig; i++ ) { - unsigned n = mpi_get_nbits( sig->data[i]); + size_t n = gcry_mpi_get_nbits( sig->data[i]); gcry_md_putc( md, n>>8); gcry_md_putc( md, n ); - p = mpi_get_buffer( sig->data[i], &n, NULL ); + if( gcry_mpi_aprint( GCRYMPI_FMT_USG, &p, &n, sig->data[i] ) ) + BUG(); gcry_md_write( md, p, n ); - m_free(p); + gcry_free(p); } gcry_md_final( md ); p = make_radix64_string( gcry_md_read( md, 0 ), 20 ); - buffer = m_alloc( strlen(p) + 60 ); + buffer = gcry_xmalloc( strlen(p) + 60 ); sprintf( buffer, "%s %s %lu", p, strtimestamp( sig->timestamp ), (ulong)sig->timestamp ); write_status_text( STATUS_SIG_ID, buffer ); - m_free(buffer); - m_free(p); + gcry_free(buffer); + gcry_free(p); gcry_md_close(md); } @@ -260,8 +260,11 @@ mdc_kludge_check( PKT_signature *sig, GCRY_MD_HD digest ) log_debug("sig_data[0] is NULL\n"); else { unsigned s2len; - byte *s2; - s2 = mpi_get_buffer( sig->data[0], &s2len, NULL ); + char *s2; + + if( gcry_mpi_print( GCRYMPI_FMT_USG, &s2, &s2len, sig->data[0] )) + BUG(); + log_hexdump( "MDC stored ", s2, s2len ); if( s2len != s1len ) @@ -270,7 +273,7 @@ mdc_kludge_check( PKT_signature *sig, GCRY_MD_HD digest ) log_debug("MDC check: hashs differ\n"); else rc = 0; - m_free(s2); + gcry_free(s2); } } @@ -445,7 +448,7 @@ do_check( PKT_public_key *pk, PKT_signature *sig, GCRY_MD_HD digest ) gcry_md_final( digest ); result = encode_md_value( pk->pubkey_algo, digest, sig->digest_algo, - mpi_get_nbits(pk->pkey[0])); + gcry_mpi_get_nbits(pk->pkey[0])); ctx.sig = sig; ctx.md = digest; diff --git a/g10/sign.c b/g10/sign.c index d65e0e2d1..a227a0c25 100644 --- a/g10/sign.c +++ b/g10/sign.c @@ -31,7 +31,6 @@ #include "errors.h" #include "iobuf.h" #include "keydb.h" -#include "memory.h" #include "util.h" #include "main.h" #include "filter.h" @@ -124,7 +123,7 @@ mk_notation_and_policy( PKT_signature *sig ) n1 = s - string; s++; n2 = strlen(s); - buf = m_alloc( 8 + n1 + n2 ); + buf = gcry_xmalloc( 8 + n1 + n2 ); buf[0] = 0x80; /* human readable */ buf[1] = buf[2] = buf[3] = 0; buf[4] = n1 >> 8; @@ -179,7 +178,7 @@ do_sign( PKT_secret_key *sk, PKT_signature *sig, sig->digest_start[0] = dp[0]; sig->digest_start[1] = dp[1]; frame = encode_md_value( sk->pubkey_algo, md, - digest_algo, mpi_get_nbits(sk->skey[0])); + digest_algo, gcry_mpi_get_nbits(sk->skey[0])); rc = pk_sign( sk->pubkey_algo, sig->data, frame, sk->skey ); mpi_release(frame); if( rc ) @@ -189,7 +188,7 @@ do_sign( PKT_secret_key *sk, PKT_signature *sig, char *ustr = get_user_id_string( sig->keyid ); log_info(_("%s signature from: %s\n"), gcry_pk_algo_name(sk->pubkey_algo), ustr ); - m_free(ustr); + gcry_free(ustr); } } return rc; @@ -391,7 +390,7 @@ sign_file( STRLIST filenames, int detached, STRLIST locusr, break; sk = sk_rover->sk; - ops = m_alloc_clear( sizeof *ops ); + ops = gcry_xcalloc( 1, sizeof *ops ); ops->sig_class = opt.textmode && !outfile ? 0x01 : 0x00; ops->digest_algo = hash_for(sk->pubkey_algo); ops->pubkey_algo = sk->pubkey_algo; @@ -447,13 +446,13 @@ sign_file( STRLIST filenames, int detached, STRLIST locusr, if (!opt.no_literal) { if( fname || opt.set_filename ) { char *s = make_basename( opt.set_filename ? opt.set_filename : fname ); - pt = m_alloc( sizeof *pt + strlen(s) - 1 ); + pt = gcry_xmalloc( sizeof *pt + strlen(s) - 1 ); pt->namelen = strlen(s); memcpy(pt->name, s, pt->namelen ); - m_free(s); + gcry_free(s); } else { /* no filename */ - pt = m_alloc( sizeof *pt - 1 ); + pt = gcry_xmalloc( sizeof *pt - 1 ); pt->namelen = 0; } } @@ -513,7 +512,7 @@ sign_file( STRLIST filenames, int detached, STRLIST locusr, /* build the signature packet */ /* fixme: this code is partly duplicated in make_keysig_packet */ - sig = m_alloc_clear( sizeof *sig ); + sig = gcry_xcalloc( 1, sizeof *sig ); sig->version = old_style || opt.force_v3_sigs ? 3 : sk->version; keyid_from_sk( sk, sig->keyid ); sig->digest_algo = hash_for(sk->pubkey_algo); @@ -709,7 +708,7 @@ clearsign_file( const char *fname, STRLIST locusr, const char *outfile ) /* build the signature packet */ /* fixme: this code is duplicated above */ - sig = m_alloc_clear( sizeof *sig ); + sig = gcry_xcalloc( 1, sizeof *sig ); sig->version = old_style || opt.force_v3_sigs ? 3 : sk->version; keyid_from_sk( sk, sig->keyid ); sig->digest_algo = hash_for(sk->pubkey_algo); @@ -840,7 +839,7 @@ make_keysig_packet( PKT_signature **ret_sig, PKT_public_key *pk, gcry_md_write( md, uid->name, uid->len ); } /* and make the signature packet */ - sig = m_alloc_clear( sizeof *sig ); + sig = gcry_xcalloc( 1, sizeof *sig ); sig->version = sk->version; keyid_from_sk( sk, sig->keyid ); sig->pubkey_algo = sk->pubkey_algo; diff --git a/g10/signal.c b/g10/signal.c index 6ed23e5a0..fcb012e02 100644 --- a/g10/signal.c +++ b/g10/signal.c @@ -29,7 +29,7 @@ #include "options.h" #include "errors.h" -#include "memory.h" +#include #include "util.h" #include "main.h" #include "ttyio.h" @@ -58,7 +58,7 @@ got_fatal_signal( int sig ) raise( sig ); caught_fatal_sig = 1; - secmem_term(); + gcry_control( GCRYCTL_TERM_SECMEM ); #ifdef IS_DEVELOPMENT_VERSION write(2, "\n", 1 ); s = log_get_name(); if( s ) write(2, s, strlen(s) ); diff --git a/g10/skclist.c b/g10/skclist.c index be9769185..8d06364d8 100644 --- a/g10/skclist.c +++ b/g10/skclist.c @@ -30,7 +30,6 @@ #include "packet.h" #include "errors.h" #include "keydb.h" -#include "memory.h" #include "util.h" #include "i18n.h" #include "dummy-cipher.h" @@ -45,7 +44,7 @@ release_sk_list( SK_LIST sk_list ) for( ; sk_list; sk_list = sk_rover ) { sk_rover = sk_list->next; free_secret_key( sk_list->sk ); - m_free( sk_list ); + gcry_free( sk_list ); } } @@ -60,7 +59,7 @@ build_sk_list( STRLIST locusr, SK_LIST *ret_sk_list, int unlock, if( !locusr ) { /* use the default one */ PKT_secret_key *sk; - sk = m_alloc_clear( sizeof *sk ); + sk = gcry_xcalloc( 1, sizeof *sk ); sk->pubkey_usage = use; if( (rc = get_seckey_byname( sk, NULL, unlock )) ) { free_secret_key( sk ); sk = NULL; @@ -75,7 +74,7 @@ build_sk_list( STRLIST locusr, SK_LIST *ret_sk_list, int unlock, free_secret_key( sk ); sk = NULL; } else { - r = m_alloc( sizeof *r ); + r = gcry_xmalloc( sizeof *r ); r->sk = sk; sk = NULL; r->next = sk_list; r->mark = 0; @@ -91,7 +90,7 @@ build_sk_list( STRLIST locusr, SK_LIST *ret_sk_list, int unlock, for(; locusr; locusr = locusr->next ) { PKT_secret_key *sk; - sk = m_alloc_clear( sizeof *sk ); + sk = gcry_xcalloc( 1, sizeof *sk ); sk->pubkey_usage = use; if( (rc = get_seckey_byname( sk, locusr->d, unlock )) ) { free_secret_key( sk ); sk = NULL; @@ -107,7 +106,7 @@ build_sk_list( STRLIST locusr, SK_LIST *ret_sk_list, int unlock, free_secret_key( sk ); sk = NULL; } else { - r = m_alloc( sizeof *r ); + r = gcry_xmalloc( sizeof *r ); r->sk = sk; sk = NULL; r->next = sk_list; r->mark = 0; diff --git a/g10/status.c b/g10/status.c index 079c0dbd9..3eeec4648 100644 --- a/g10/status.c +++ b/g10/status.c @@ -43,7 +43,7 @@ #include "ttyio.h" #include "options.h" #include "main.h" -#include "memory.h" +#include #include "i18n.h" static int fd = -1; @@ -257,7 +257,7 @@ do_shm_get( const char *keyword, int hidden, int bool ) if( bool ) return p[0]? "" : NULL; - string = hidden? m_alloc_secure( n+1 ) : m_alloc( n+1 ); + string = hidden? gcry_xmalloc_secure( n+1 ) : gcry_xmalloc( n+1 ); memcpy(string, p, n ); string[n] = 0; /* make sure it is a string */ if( hidden ) /* invalidate the memory */ @@ -292,7 +292,7 @@ cpr_get( const char *keyword, const char *prompt ) for(;;) { p = tty_get( prompt ); if( *p=='?' && !p[1] && !(keyword && !*keyword)) { - m_free(p); + gcry_free(p); display_online_help( keyword ); } else @@ -307,7 +307,7 @@ cpr_get_utf8( const char *keyword, const char *prompt ) p = cpr_get( keyword, prompt ); if( p ) { char *utf8 = native_to_utf8( p ); - m_free( p ); + gcry_free( p ); p = utf8; } return p; @@ -325,7 +325,7 @@ cpr_get_hidden( const char *keyword, const char *prompt ) for(;;) { p = tty_get_hidden( prompt ); if( *p == '?' && !p[1] ) { - m_free(p); + gcry_free(p); display_online_help( keyword ); } else @@ -358,13 +358,13 @@ cpr_get_answer_is_yes( const char *keyword, const char *prompt ) p = tty_get( prompt ); trim_spaces(p); /* it is okay to do this here */ if( *p == '?' && !p[1] ) { - m_free(p); + gcry_free(p); display_online_help( keyword ); } else { tty_kill_prompt(); yes = answer_is_yes(p); - m_free(p); + gcry_free(p); return yes; } } @@ -384,13 +384,13 @@ cpr_get_answer_yes_no_quit( const char *keyword, const char *prompt ) p = tty_get( prompt ); trim_spaces(p); /* it is okay to do this here */ if( *p == '?' && !p[1] ) { - m_free(p); + gcry_free(p); display_online_help( keyword ); } else { tty_kill_prompt(); yes = answer_is_yes_no_quit(p); - m_free(p); + gcry_free(p); return yes; } } diff --git a/g10/tdbdump.c b/g10/tdbdump.c index 1d608bd19..6ffa0465c 100644 --- a/g10/tdbdump.c +++ b/g10/tdbdump.c @@ -33,7 +33,7 @@ #include "errors.h" #include "iobuf.h" #include "keydb.h" -#include "memory.h" +#include #include "util.h" #include "trustdb.h" #include "options.h" @@ -349,7 +349,7 @@ list_trustdb( const char *username ) username, g10_errstr(rc)); } else if( username ) { - PKT_public_key *pk = m_alloc_clear( sizeof *pk ); + PKT_public_key *pk = gcry_xcalloc( 1, sizeof *pk ); int rc; if( (rc = get_pubkey_byname( NULL, pk, username, NULL )) ) @@ -492,7 +492,7 @@ import_ownertrust( const char *fname ) write_record( &rec ); } else if( rc == -1 ) { /* not found; get the key from the ring */ - PKT_public_key *pk = m_alloc_clear( sizeof *pk ); + PKT_public_key *pk = gcry_xcalloc( 1, sizeof *pk ); log_info_f(fname, _("key not in trustdb, searching ring.\n")); rc = get_pubkey_byfprint( pk, line, fprlen ); diff --git a/g10/tdbio.c b/g10/tdbio.c index b840d723a..3118d71ac 100644 --- a/g10/tdbio.c +++ b/g10/tdbio.c @@ -31,7 +31,7 @@ #include "errors.h" #include "iobuf.h" -#include "memory.h" +#include #include "util.h" #include "options.h" #include "main.h" @@ -180,7 +180,7 @@ put_record_into_cache( ulong recno, const char *data ) } /* see whether we reached the limit */ if( cache_entries < MAX_CACHE_ENTRIES_SOFT ) { /* no */ - r = m_alloc( sizeof *r ); + r = gcry_xmalloc( sizeof *r ); r->flags.used = 1; r->recno = recno; memcpy( r->data, data, TRUST_RECORD_LEN ); @@ -223,7 +223,7 @@ put_record_into_cache( ulong recno, const char *data ) if( cache_entries < MAX_CACHE_ENTRIES_HARD ) { /* no */ if( opt.debug && !(cache_entries % 100) ) log_debug("increasing tdbio cache size\n"); - r = m_alloc( sizeof *r ); + r = gcry_xmalloc( sizeof *r ); r->flags.used = 1; r->recno = recno; memcpy( r->data, data, TRUST_RECORD_LEN ); @@ -421,13 +421,13 @@ tdbio_set_dbname( const char *new_dbname, int create ) atexit( cleanup ); initialized = 1; } - fname = new_dbname? m_strdup( new_dbname ) + fname = new_dbname? gcry_xstrdup( new_dbname ) : make_filename(opt.homedir, "trustdb.gpg", NULL ); if( access( fname, R_OK ) ) { if( errno != ENOENT ) { log_error( _("%s: can't access: %s\n"), fname, strerror(errno) ); - m_free(fname); + gcry_free(fname); return G10ERR_TRUSTDB; } if( create ) { @@ -457,7 +457,7 @@ tdbio_set_dbname( const char *new_dbname, int create ) if( !fp ) log_fatal( _("%s: can't create: %s\n"), fname, strerror(errno) ); fclose(fp); - m_free(db_name); + gcry_free(db_name); db_name = fname; #ifdef HAVE_DOSISH_SYSTEM db_fd = open( db_name, O_RDWR | O_BINARY ); @@ -496,7 +496,7 @@ tdbio_set_dbname( const char *new_dbname, int create ) return 0; } } - m_free(db_name); + gcry_free(db_name); db_name = fname; return 0; } diff --git a/g10/textfilter.c b/g10/textfilter.c index 3588156ad..06f85dc64 100644 --- a/g10/textfilter.c +++ b/g10/textfilter.c @@ -28,7 +28,6 @@ #include #include "errors.h" #include "iobuf.h" -#include "memory.h" #include "util.h" #include "filter.h" #include "i18n.h" @@ -128,7 +127,7 @@ text_filter( void *opaque, int control, if( tfx->truncated ) log_error(_("can't handle text lines longer than %d characters\n"), MAX_LINELEN ); - m_free( tfx->buffer ); + gcry_free( tfx->buffer ); tfx->buffer = NULL; } else if( control == IOBUFCTRL_DESC ) diff --git a/g10/trustdb.c b/g10/trustdb.c index 263bac261..6f0ed59a7 100644 --- a/g10/trustdb.c +++ b/g10/trustdb.c @@ -33,7 +33,7 @@ #include "errors.h" #include "iobuf.h" #include "keydb.h" -#include "memory.h" +#include #include "util.h" #include "trustdb.h" #include "options.h" @@ -222,7 +222,7 @@ new_lid_table(void) memset( a, 0, sizeof *a ); } else - a = m_alloc_clear( sizeof *a ); + a = gcry_xcalloc( 1, sizeof *a ); return a; } @@ -260,7 +260,7 @@ ins_lid_table_item( LOCAL_ID_TABLE tbl, ulong lid, unsigned flag ) if( a ) unused_lid_items = a->next; else - a = m_alloc( sizeof *a ); + a = gcry_xmalloc( sizeof *a ); a->lid = lid; a->flag = flag; a->next = tbl->items[lid & 0x0f]; @@ -294,7 +294,7 @@ new_tn(void) memset( t, 0, sizeof *t ); } else - t = m_alloc_clear( sizeof *t ); + t = gcry_xcalloc( 1, sizeof *t ); if( ++alloced_tns > max_alloced_tns ) max_alloced_tns = alloced_tns; return t; @@ -409,7 +409,7 @@ get_dir_record( PKT_public_key *pk, TRUSTREC *rec ) static ulong lid_from_keyid_no_sdir( u32 *keyid ) { - PKT_public_key *pk = m_alloc_clear( sizeof *pk ); + PKT_public_key *pk = gcry_xcalloc( 1, sizeof *pk ); TRUSTREC rec; ulong lid = 0; int rc; @@ -442,8 +442,8 @@ verify_own_keys(void) { int rc; void *enum_context = NULL; - PKT_secret_key *sk = m_alloc_clear( sizeof *sk ); - PKT_public_key *pk = m_alloc_clear( sizeof *pk ); + PKT_secret_key *sk = gcry_xcalloc( 1, sizeof *sk ); + PKT_public_key *pk = gcry_xcalloc( 1, sizeof *pk ); u32 keyid[2]; while( !(rc=enum_secret_keys( &enum_context, sk, 0 ) ) ) { @@ -531,7 +531,7 @@ setup_trustdb( int level, const char *dbname ) if( trustdb_args.init ) return 0; trustdb_args.level = level; - trustdb_args.dbname = dbname? m_strdup(dbname): NULL; + trustdb_args.dbname = dbname? gcry_xstrdup(dbname): NULL; return 0; } @@ -595,7 +595,7 @@ print_user_id( FILE *fp, const char *text, u32 *keyid ) tty_print_utf8_string( p, n ); tty_printf( "\"\n" ); } - m_free(p); + gcry_free(p); } @@ -654,7 +654,7 @@ print_path( int pathlen, TN ME .........., FILE *fp, ulong highlight ) putc('\"', fp); print_utf8_string( fp, p, n > 40? 40:n, 0 ); putc('\"', fp); - m_free(p); + gcry_free(p); putc('\n', fp ); } } @@ -948,7 +948,7 @@ make_key_records( KBNODE keyblock, ulong lid, u32 *keyid, int *mainrev ) fingerprint_from_pk( pk, fpr, &fprlen ); /* create the key record */ - k = m_alloc_clear( sizeof *k ); + k = gcry_xcalloc( 1, sizeof *k ); k->rectype = RECTYPE_KEY; k->r.key.lid = lid; k->r.key.pubkey_algo = pk->pubkey_algo; @@ -971,7 +971,7 @@ make_key_records( KBNODE keyblock, ulong lid, u32 *keyid, int *mainrev ) k->r.key.next = k->next->recnum; write_record( k ); k2 = k->next; - m_free( k ); + gcry_free( k ); } return keyrecno; } @@ -1212,7 +1212,7 @@ make_sig_records( KBNODE keyblock, KBNODE uidnode, /* create the sig record */ if( !sigidx ) { - s = m_alloc_clear( sizeof *s ); + s = gcry_xcalloc( 1, sizeof *s ); s->rectype = RECTYPE_SIG; s->r.sig.lid = lid; } @@ -1245,7 +1245,7 @@ make_sig_records( KBNODE keyblock, KBNODE uidnode, s->r.sig.next = s->next->recnum; write_record( s ); s2 = s->next; - m_free( s ); + gcry_free( s ); } return sigrecno; } @@ -1286,7 +1286,7 @@ make_pref_record( PKT_signature *sig, ulong lid ) continue; for( ; n; n--, s++ ) { if( !idx ) { - p = m_alloc_clear( sizeof *p ); + p = gcry_xcalloc( 1, sizeof *p ); p->rectype = RECTYPE_PREF; p->r.pref.lid = lid; } @@ -1313,7 +1313,7 @@ make_pref_record( PKT_signature *sig, ulong lid ) p->r.pref.next = p->next->recnum; write_record( p ); p2 = p->next; - m_free( p ); + gcry_free( p ); } return precno; } @@ -1338,7 +1338,7 @@ make_uid_records( KBNODE keyblock, ulong lid, u32 *keyid, u32 *min_expire ) gcry_md_hash_buffer( GCRY_MD_RMD160, uidhash, uid->name, uid->len ); /* create the uid record */ - u = m_alloc_clear( sizeof *u ); + u = gcry_xcalloc( 1, sizeof *u ); u->rectype = RECTYPE_UID; u->r.uid.lid = lid; memcpy(u->r.uid.namehash, uidhash, 20 ); @@ -1364,7 +1364,7 @@ make_uid_records( KBNODE keyblock, ulong lid, u32 *keyid, u32 *min_expire ) u->r.uid.next = u->next->recnum; write_record( u ); u2 = u->next; - m_free( u ); + gcry_free( u ); } return uidrecno; } @@ -1768,7 +1768,7 @@ build_cert_tree( ulong lid, int depth, int max_depth, TN helproot ) if( dirrec.rectype != RECTYPE_SDIR ) log_debug("lid %lu, has rectype %d" " - skipped\n", lid, dirrec.rectype ); - m_free(keynode); + gcry_free(keynode); return NULL; } @@ -2356,7 +2356,7 @@ list_trust_path( const char *username ) ulong lid; TRUSTREC rec; TN tree; - PKT_public_key *pk = m_alloc_clear( sizeof *pk ); + PKT_public_key *pk = gcry_xcalloc( 1, sizeof *pk ); init_trustdb(); if( (rc = get_pubkey_byname(NULL, pk, username, NULL )) ) @@ -2430,7 +2430,7 @@ enum_cert_paths( void **context, ulong *lid, ctx = *context; for(tsl = ctx->tsl_head; tsl; tsl = tsl2 ) { tsl2 = tsl->next; - m_free( tsl ); + gcry_free( tsl ); } *context = NULL; } @@ -2444,15 +2444,15 @@ enum_cert_paths( void **context, ulong *lid, if( !*lid ) return -1; - ctx = m_alloc_clear( sizeof *ctx ); + ctx = gcry_xcalloc( 1, sizeof *ctx ); *context = ctx; /* collect the paths */ #if 0 read_record( *lid, &rec, RECTYPE_DIR ); - tmppath = m_alloc_clear( (opt.max_cert_depth+1)* sizeof *tmppath ); + tmppath = gcry_xcalloc( 1, (opt.max_cert_depth+1)* sizeof *tmppath ); tsl = NULL; collect_paths( 0, opt.max_cert_depth, 1, &rec, tmppath, &tsl ); - m_free( tmppath ); + gcry_free( tmppath ); sort_tsl_list( &tsl ); #endif /* setup the context */ @@ -2555,7 +2555,7 @@ get_pref_data( ulong lid, const byte *namehash, size_t *ret_n ) read_record( rec.r.uid.prefrec, &rec, RECTYPE_PREF ); if( rec.r.pref.next ) log_info(_("WARNING: can't yet handle long pref records\n")); - buf = m_alloc( ITEMS_PER_PREF_RECORD ); + buf = gcry_xmalloc( ITEMS_PER_PREF_RECORD ); memcpy( buf, rec.r.pref.data, ITEMS_PER_PREF_RECORD ); *ret_n = ITEMS_PER_PREF_RECORD; return buf; diff --git a/g10/verify.c b/g10/verify.c index 589b1b7bb..bdb6f178f 100644 --- a/g10/verify.c +++ b/g10/verify.c @@ -30,7 +30,7 @@ #include "errors.h" #include "iobuf.h" #include "keydb.h" -#include "memory.h" +#include #include "util.h" #include "main.h" #include "filter.h" diff --git a/include/ChangeLog b/include/ChangeLog index 48124e9f3..e37b9a3db 100644 --- a/include/ChangeLog +++ b/include/ChangeLog @@ -1,3 +1,7 @@ +Mon Jan 24 13:04:28 CET 2000 Werner Koch + + * memory.h: Removed. + Wed Dec 8 21:58:32 CET 1999 Werner Koch * util.h: Moved argparse stuff to the argparse header. Move some diff --git a/include/memory.h b/include/memory.h deleted file mode 100644 index 32c42fcd5..000000000 --- a/include/memory.h +++ /dev/null @@ -1,85 +0,0 @@ -/* memory.h - memory allocation - * Copyright (C) 1998 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 2 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, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA - */ - -#ifndef G10_MEMORY_H -#define G10_MEMORY_H - -#ifdef M_DEBUG -#ifndef STR - #define STR(v) #v -#endif -#define M_DBGINFO(a) __FUNCTION__ "["__FILE__ ":" STR(a) "]" -#define m_alloc(n) m_debug_alloc((n), M_DBGINFO( __LINE__ ) ) -#define m_alloc_clear(n) m_debug_alloc_clear((n), M_DBGINFO(__LINE__) ) -#define m_alloc_secure(n) m_debug_alloc((n), M_DBGINFO(__LINE__) ) -#define m_alloc_secure_clear(n) m_debug_alloc((n), M_DBGINFO(__LINE__) ) -#define m_realloc(n,m) m_debug_realloc((n),(m), M_DBGINFO(__LINE__) ) -#define m_free(n) m_debug_free((n), M_DBGINFO(__LINE__) ) -#define m_check(n) m_debug_check((n), M_DBGINFO(__LINE__) ) -/*#define m_copy(a) m_debug_copy((a), M_DBGINFO(__LINE__) )*/ -#define m_strdup(a) m_debug_strdup((a), M_DBGINFO(__LINE__) ) - -void *m_debug_alloc( size_t n, const char *info ); -void *m_debug_alloc_clear( size_t n, const char *info ); -void *m_debug_alloc_secure( size_t n, const char *info ); -void *m_debug_alloc_secure_clear( size_t n, const char *info ); -void *m_debug_realloc( void *a, size_t n, const char *info ); -void m_debug_free( void *p, const char *info ); -void m_debug_check( const void *a, const char *info ); -/*void *m_debug_copy( const void *a, const char *info );*/ -char *m_debug_strdup( const char *a, const char *info ); - -#else -void *m_alloc( size_t n ); -void *m_alloc_clear( size_t n ); -void *m_alloc_secure( size_t n ); -void *m_alloc_secure_clear( size_t n ); -void *m_realloc( void *a, size_t n ); -void m_free( void *p ); -void m_check( const void *a ); -/*void *m_copy( const void *a );*/ -char *m_strdup( const char * a); -#endif - -int m_is_secure( const void *a ); - -size_t m_size( const void *a ); -void m_print_stats(const char *prefix); - -/*-- secmem.c --*/ -void secmem_init( size_t npool ); -void secmem_term( void ); -void *secmem_malloc( size_t size ); -void *secmem_realloc( void *a, size_t newsize ); -void secmem_free( void *a ); -void secmem_dump_stats(void); -void secmem_set_flags( unsigned flags ); -unsigned secmem_get_flags(void); - - - -#define DBG_MEMORY memory_debug_mode -#define DBG_MEMSTAT memory_stat_debug_mode -int memory_debug_mode; -int memory_stat_debug_mode; - - - -#endif /*G10_MEMORY_H*/ diff --git a/include/util.h b/include/util.h index 190bff6ff..db221bba6 100644 --- a/include/util.h +++ b/include/util.h @@ -27,9 +27,10 @@ #include #include "types.h" #include "errors.h" -#include "../util/mischelp.h" -#include "../util/stringhelp.h" -#include "../util/argparse.h" +#include "../jnlib/mischelp.h" +#include "../jnlib/stringhelp.h" +#include "../jnlib/argparse.h" +#include "../jnlib/dotlock.h" /*-- logger.c --*/ @@ -91,14 +92,6 @@ void g10_log_hexdump( const char *text, const char *buf, size_t len ); const char * g10_errstr( int no ); -/*-- dotlock.c --*/ -struct dotlock_handle; -typedef struct dotlock_handle *DOTLOCK; - -DOTLOCK create_dotlock( const char *file_to_lock ); -int make_dotlock( DOTLOCK h, long timeout ); -int release_dotlock( DOTLOCK h ); - /*-- fileutil.c --*/ char * make_basename(const char *filepath); diff --git a/mpi/ChangeLog b/mpi/ChangeLog index 125fa1a68..2d2ec8bdb 100644 --- a/mpi/ChangeLog +++ b/mpi/ChangeLog @@ -1,3 +1,11 @@ +Mon Jan 24 13:04:28 CET 2000 Werner Koch + + * mpiutil.c: Removed all memory debugging code. + + * mpicoder.c (gcry_mpi_aprint): New. + + * Replaced all m_ memory functions by g10_ ones. + Fri Dec 31 14:06:56 CET 1999 Werner Koch * mpi-bit.c (gcry_mpi_get_nbits): New. diff --git a/mpi/mpi-mpow.c b/mpi/mpi-mpow.c index a8c561dd1..556f7e116 100644 --- a/mpi/mpi-mpow.c +++ b/mpi/mpi-mpow.c @@ -45,16 +45,16 @@ static int build_index( MPI *exparray, int k, int i, int t ) { int j, bitno; - int index = 0; + int idx = 0; bitno = t-i; for(j=k-1; j >= 0; j-- ) { - index <<= 1; + idx <<= 1; if( mpi_test_bit( exparray[j], bitno ) ) - index |= 1; + idx |= 1; } - /*log_debug("t=%d i=%d index=%d\n", t, i, index );*/ - return index; + /*log_debug("t=%d i=%d idx=%d\n", t, i, idx );*/ + return idx; } /**************** @@ -87,7 +87,7 @@ mpi_mulpowm( MPI res, MPI *basearray, MPI *exparray, MPI m) assert(t); assert( k < 10 ); - G = m_alloc_clear( (1<sign; *nbytes = a->nlimbs * BYTES_PER_MPI_LIMB; - p = buffer = force_secure || mpi_is_secure(a) ? m_alloc_secure( *nbytes) - : m_alloc( *nbytes ); + p = buffer = force_secure || mpi_is_secure(a) ? g10_xmalloc_secure( *nbytes) + : g10_xmalloc( *nbytes ); for(i=a->nlimbs-1; i >= 0; i-- ) { alimb = a->d[i]; @@ -340,6 +340,7 @@ gcry_mpi_scan( struct gcry_mpi **ret_mpi, enum gcry_mpi_format format, /* TODO: add a way to allocate the MPI in secure memory * Hmmm: maybe it is better to retrieve this information from * the provided buffer. */ + #warning secure memory is not used here. if( format == GCRYMPI_FMT_STD ) { const byte *s = buffer; @@ -458,7 +459,7 @@ gcry_mpi_print( enum gcry_mpi_format format, char *buffer, size_t *nbytes, } if( n > len && buffer ) { - m_free(tmp); + g10_free(tmp); return GCRYERR_TOO_SHORT; /* the provided buffer is too short */ } if( buffer ) { @@ -468,7 +469,24 @@ gcry_mpi_print( enum gcry_mpi_format format, char *buffer, size_t *nbytes, memcpy( s, tmp, n-extra ); } - m_free(tmp); + g10_free(tmp); + *nbytes = n; + return 0; + } + else if( format == GCRYMPI_FMT_USG ) { + unsigned int n = (nbits + 7)/8; + + /* we ignore the sign for this format */ + /* FIXME: for performance reasons we should put this into + * mpi_aprint becuase we can then use the buffer directly */ + if( n > len && buffer ) + return GCRYERR_TOO_SHORT; /* the provided buffer is too short */ + if( buffer ) { + char *tmp; + tmp = mpi_get_buffer( a, &n, NULL ); + memcpy( buffer, tmp, n ); + g10_free(tmp); + } *nbytes = n; return 0; } @@ -488,7 +506,7 @@ gcry_mpi_print( enum gcry_mpi_format format, char *buffer, size_t *nbytes, tmp = mpi_get_buffer( a, &n, NULL ); memcpy( s+2, tmp, n ); - m_free(tmp); + g10_free(tmp); } *nbytes = n+2; return 0; @@ -508,7 +526,7 @@ gcry_mpi_print( enum gcry_mpi_format format, char *buffer, size_t *nbytes, } if( n+4 > len && buffer ) { - m_free(tmp); + g10_free(tmp); return GCRYERR_TOO_SHORT; /* the provided buffer is too short */ } if( buffer ) { @@ -522,7 +540,7 @@ gcry_mpi_print( enum gcry_mpi_format format, char *buffer, size_t *nbytes, memcpy( s, tmp, n-extra ); } - m_free(tmp); + g10_free(tmp); *nbytes = 4+n; return 0; } @@ -534,10 +552,10 @@ gcry_mpi_print( enum gcry_mpi_format format, char *buffer, size_t *nbytes, tmp = mpi_get_buffer( a, &n, NULL ); if( !n || (*tmp & 0x80) ) - extra=1; + extra=2; - if( 2*n+3+1 > len && buffer ) { - m_free(tmp); + if( 2*n + extra + !!a->sign + 1 > len && buffer ) { + g10_free(tmp); return GCRYERR_TOO_SHORT; /* the provided buffer is too short */ } if( buffer ) { @@ -559,17 +577,38 @@ gcry_mpi_print( enum gcry_mpi_format format, char *buffer, size_t *nbytes, *nbytes = (char*)s - buffer; } else { - *nbytes = n; - if( a->sign ) - ++*nbytes; - if( extra ) - *nbytes += 2; - ++*nbytes; /* terminating Nul */ + *nbytes = 2*n + extra + !!a->sign + 1; } - m_free(tmp); + g10_free(tmp); return 0; } else return GCRYERR_INV_ARG; } +/**************** + * Like gcry_mpi_print but this function allocates the buffer itself. + * The caller has to supply the address of a pointer. nbytes may be + * NULL. + */ +int +gcry_mpi_aprint( enum gcry_mpi_format format, void **buffer, size_t *nbytes, + struct gcry_mpi *a ) +{ + size_t n; + int rc; + + *buffer = NULL; + rc = gcry_mpi_print( format, NULL, &n, a ); + if( rc ) + return rc; + *buffer = mpi_is_secure(a) ? g10_xmalloc_secure( n ) : g10_xmalloc( n ); + rc = gcry_mpi_print( format, *buffer, &n, a ); + if( rc ) { + g10_free(*buffer); + *buffer = NULL; + } + return rc; +} + + diff --git a/mpi/mpiutil.c b/mpi/mpiutil.c index 58d06970d..2dad135d4 100644 --- a/mpi/mpiutil.c +++ b/mpi/mpiutil.c @@ -1,5 +1,5 @@ /* mpiutil.ac - Utility functions for MPI - * Copyright (C) 1998 Free Software Foundation, Inc. + * Copyright (C) 1998, 2000 Free Software Foundation, Inc. * * This file is part of GnuPG. * @@ -41,8 +41,6 @@ mpi_alloc( unsigned nlimbs ) { MPI a; - if( DBG_MEMORY ) - log_debug("mpi_alloc(%u)\n", nlimbs*BITS_PER_MPI_LIMB ); a = g10_xmalloc( sizeof *a ); a->d = nlimbs? mpi_alloc_limb_space( nlimbs, 0 ) : NULL; a->alloced = nlimbs; @@ -64,8 +62,6 @@ mpi_alloc_secure( unsigned nlimbs ) { MPI a; - if( DBG_MEMORY ) - log_debug("mpi_alloc_secure(%u)\n", nlimbs*BITS_PER_MPI_LIMB ); a = g10_xmalloc( sizeof *a ); a->d = nlimbs? mpi_alloc_limb_space( nlimbs, 1 ) : NULL; a->alloced = nlimbs; @@ -83,9 +79,6 @@ mpi_alloc_limb_space( unsigned nlimbs, int secure ) size_t len = nlimbs * sizeof(mpi_limb_t); mpi_ptr_t p; - if( DBG_MEMORY ) - log_debug("mpi_alloc_limb_space(%u)\n", (unsigned)len*8 ); - p = secure? g10_xmalloc_secure( len ) : g10_xmalloc( len ); return p; @@ -96,9 +89,6 @@ mpi_free_limb_space( mpi_ptr_t a ) { if( !a ) return; - if( DBG_MEMORY ) - log_debug("mpi_free_limb_space\n" ); - g10_free(a); } @@ -147,8 +137,6 @@ mpi_free( MPI a ) { if( !a ) return; - if( DBG_MEMORY ) - log_debug("mpi_free\n" ); if( a->flags & 4 ) g10_free( a->d ); else { @@ -385,7 +373,7 @@ gcry_mpi_randomize( GCRY_MPI w, char *p = mpi_is_secure(w) ? gcry_random_bytes( (nbits+7)/8, level ) : gcry_random_bytes_secure( (nbits+7)/8, level ); mpi_set_buffer( w, p, (nbits+7)/8, 0 ); - m_free(p); + g10_free(p); } diff --git a/po/POTFILES.in b/po/POTFILES.in index 68f9996b7..dffdd65b0 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -2,8 +2,8 @@ # Copyright (c) 1998 by Werner Koch (dd9jn) # utility -util/secmem.c -util/argparse.c +gcrypt/secmem.c +jnlib/argparse.c util/miscutil.c util/errors.c util/logger.c diff --git a/util/ChangeLog b/util/ChangeLog index 923d35705..e890e8d2f 100644 --- a/util/ChangeLog +++ b/util/ChangeLog @@ -1,3 +1,19 @@ +Mon Jan 24 13:04:28 CET 2000 Werner Koch + + * memory.c: Removed + * secmem.c: Moved to ../gcrypt. + * argparse.c argparse.h logging.c logging.h + mischelp.h stringhelp.c stringhelp.h xmalloc.c + xmalloc.h dotlock.c: Moved to ../jnlib + * libutil-config.h: Removed. + + * logging.c (log_set_file): New. + (log_printf): New. + (do_logv): Add kludge to insert LFs. + + * Replaced all m_ memory fucntions by gcry_ ones. + * README: New. + Fri Dec 31 12:48:31 CET 1999 Werner Koch * memory.c (m_is_secure): New. diff --git a/util/Makefile.am b/util/Makefile.am index acb8f05cb..7668de61e 100644 --- a/util/Makefile.am +++ b/util/Makefile.am @@ -1,21 +1,14 @@ ## Process this file with automake to produce Makefile.in -# Those 2 files are in the CVS but currently not used. -EXTRA_DIST = xmalloc.c xmalloc.h logging.c logging.c - INCLUDES = -I$(top_srcdir)/include -I$(top_srcdir)/intl +EXTRA_DIST = README noinst_LTLIBRARIES = libutil.la - libutil_la_LDFLAGS = libutil_la_SOURCES = logger.c fileutil.c miscutil.c strgutil.c \ - ttyio.c memory.c secmem.c errors.c iobuf.c \ - dotlock.c http.c simple-gettext.c \ - libutil-config.h \ - mischelp.h \ - stringhelp.h stringhelp.c \ - argparse.h argparse.c + ttyio.c errors.c iobuf.c \ + http.c simple-gettext.c http-test: http.c diff --git a/util/README b/util/README new file mode 100644 index 000000000..168c58a7c --- /dev/null +++ b/util/README @@ -0,0 +1,7 @@ +Here you find supporting code for GnuPG and the tools. It needs some +support from libgcrypt. + +util is not a good name, so at some time we should rename the whole source +tree. However this is not easy for CVS and diff reasons. + + diff --git a/util/argparse.c b/util/argparse.c deleted file mode 100644 index ba51980a9..000000000 --- a/util/argparse.c +++ /dev/null @@ -1,996 +0,0 @@ -/* [argparse.c wk 17.06.97] Argument Parser for option handling - * Copyright (C) 1998,1999 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 2 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, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA - */ - -#include -#include -#include -#include -#include - -#include "libutil-config.h" -#include "mischelp.h" -#include "stringhelp.h" -#ifndef LIBUTIL_CONFIG_OF_GNUPG - #include "logging.h" /* currently not used in GnUPG */ -#endif -#include "argparse.h" - - -/********************************* - * @Summary arg_parse - * #include - * - * typedef struct { - * char *argc; pointer to argc (value subject to change) - * char ***argv; pointer to argv (value subject to change) - * unsigned flags; Global flags (DO NOT CHANGE) - * int err; print error about last option - * 1 = warning, 2 = abort - * int r_opt; return option - * int r_type; type of return value (0 = no argument found) - * union { - * int ret_int; - * long ret_long - * ulong ret_ulong; - * char *ret_str; - * } r; Return values - * struct { - * int idx; - * const char *last; - * void *aliases; - * } internal; DO NOT CHANGE - * } ARGPARSE_ARGS; - * - * typedef struct { - * int short_opt; - * const char *long_opt; - * unsigned flags; - * } ARGPARSE_OPTS; - * - * int arg_parse( ARGPARSE_ARGS *arg, ARGPARSE_OPTS *opts ); - * - * @Description - * This is my replacement for getopt(). See the example for a typical usage. - * Global flags are: - * Bit 0 : Do not remove options form argv - * Bit 1 : Do not stop at last option but return other args - * with r_opt set to -1. - * Bit 2 : Assume options and real args are mixed. - * Bit 3 : Do not use -- to stop option processing. - * Bit 4 : Do not skip the first arg. - * Bit 5 : allow usage of long option with only one dash - * Bit 6 : ignore --version - * all other bits must be set to zero, this value is modified by the - * function, so assume this is write only. - * Local flags (for each option): - * Bit 2-0 : 0 = does not take an argument - * 1 = takes int argument - * 2 = takes string argument - * 3 = takes long argument - * 4 = takes ulong argument - * Bit 3 : argument is optional (r_type will the be set to 0) - * Bit 4 : allow 0x etc. prefixed values. - * Bit 7 : this is a command and not an option - * You stop the option processing by setting opts to NULL, the function will - * then return 0. - * @Return Value - * Returns the args.r_opt or 0 if ready - * r_opt may be -2/-7 to indicate an unknown option/command. - * @See Also - * ArgExpand - * @Notes - * You do not need to process the options 'h', '--help' or '--version' - * because this function includes standard help processing; but if you - * specify '-h', '--help' or '--version' you have to do it yourself. - * The option '--' stops argument processing; if bit 1 is set the function - * continues to return normal arguments. - * To process float args or unsigned args you must use a string args and do - * the conversion yourself. - * @Example - * - * ARGPARSE_OPTS opts[] = { - * { 'v', "verbose", 0 }, - * { 'd', "debug", 0 }, - * { 'o', "output", 2 }, - * { 'c', "cross-ref", 2|8 }, - * { 'm', "my-option", 1|8 }, - * { 500, "have-no-short-option-for-this-long-option", 0 }, - * {0} }; - * ARGPARSE_ARGS pargs = { &argc, &argv, 0 } - * - * while( ArgParse( &pargs, &opts) ) { - * switch( pargs.r_opt ) { - * case 'v': opt.verbose++; break; - * case 'd': opt.debug++; break; - * case 'o': opt.outfile = pargs.r.ret_str; break; - * case 'c': opt.crf = pargs.r_type? pargs.r.ret_str:"a.crf"; break; - * case 'm': opt.myopt = pargs.r_type? pargs.r.ret_int : 1; break; - * case 500: opt.a_long_one++; break - * default : pargs.err = 1; break; -- force warning output -- - * } - * } - * if( argc > 1 ) - * log_fatal( "Too many args"); - * - */ - -typedef struct alias_def_s *ALIAS_DEF; -struct alias_def_s { - ALIAS_DEF next; - char *name; /* malloced buffer with name, \0, value */ - const char *value; /* ptr into name */ -}; - -static const char *(*strusage_handler)( int ) = NULL; - -static int set_opt_arg(ARGPARSE_ARGS *arg, unsigned flags, char *s); -static void show_help(ARGPARSE_OPTS *opts, unsigned flags); -static void show_version(void); - - -static void -initialize( ARGPARSE_ARGS *arg, const char *filename, unsigned *lineno ) -{ - if( !(arg->flags & (1<<15)) ) { /* initialize this instance */ - arg->internal.idx = 0; - arg->internal.last = NULL; - arg->internal.inarg = 0; - arg->internal.stopped = 0; - arg->internal.aliases = NULL; - arg->internal.cur_alias = NULL; - arg->err = 0; - arg->flags |= 1<<15; /* mark initialized */ - if( *arg->argc < 0 ) - libutil_log_bug("Invalid argument for ArgParse\n"); - } - - - if( arg->err ) { /* last option was erroneous */ - const char *s; - - if( filename ) { - if( arg->r_opt == -6 ) - s = "%s:%u: argument not expected\n"; - else if( arg->r_opt == -5 ) - s = "%s:%u: read error\n"; - else if( arg->r_opt == -4 ) - s = "%s:%u: keyword too long\n"; - else if( arg->r_opt == -3 ) - s = "%s:%u: missing argument\n"; - else if( arg->r_opt == -7 ) - s = "%s:%u: invalid command\n"; - else if( arg->r_opt == -10 ) - s = "%s:%u: invalid alias definition\n"; - else - s = "%s:%u: invalid option\n"; - libutil_log_error(s, filename, *lineno ); - } - else { - if( arg->r_opt == -3 ) - s = "Missing argument for option \"%.50s\"\n"; - else if( arg->r_opt == -6 ) - s = "Option \"%.50s\" does not expect an argument\n"; - else if( arg->r_opt == -7 ) - s = "Invalid command \"%.50s\"\n"; - else if( arg->r_opt == -8 ) - s = "Option \"%.50s\" is ambiguous\n"; - else if( arg->r_opt == -9 ) - s = "Command \"%.50s\" is ambiguous\n"; - else - s = "Invalid option \"%.50s\"\n"; - libutil_log_error(s, arg->internal.last? arg->internal.last:"[??]" ); - } - if( arg->err != 1 ) - exit(2); - arg->err = 0; - } - - /* clearout the return value union */ - arg->r.ret_str = NULL; - arg->r.ret_long= 0; -} - - -static void -store_alias( ARGPARSE_ARGS *arg, char *name, char *value ) -{ - /* TODO: replace this dummy function with a rea one - * and fix the probelms IRIX has with (ALIAS_DEV)arg.. - * used as lvalue - */ -#if 0 - ALIAS_DEF a = m_alloc( sizeof *a ); - a->name = name; - a->value = value; - a->next = (ALIAS_DEF)arg->internal.aliases; - (ALIAS_DEF)arg->internal.aliases = a; -#endif -} - -/**************** - * Get options from a file. - * Lines starting with '#' are comment lines. - * Syntax is simply a keyword and the argument. - * Valid keywords are all keywords from the long_opt list without - * the leading dashes. The special keywords "help", "warranty" and "version" - * are not valid here. - * The special keyword "alias" may be used to store alias definitions, - * which are later expanded like long options. - * Caller must free returned strings. - * If called with FP set to NULL command line args are parse instead. - * - * Q: Should we allow the syntax - * keyword = value - * and accept for boolean options a value of 1/0, yes/no or true/false? - * Note: Abbreviation of options is here not allowed. - */ -int -optfile_parse( FILE *fp, const char *filename, unsigned *lineno, - ARGPARSE_ARGS *arg, ARGPARSE_OPTS *opts) -{ - int state, i, c; - int idx=0; - char keyword[100]; - char *buffer = NULL; - size_t buflen = 0; - int inverse=0; - int in_alias=0; - - if( !fp ) /* same as arg_parse() in this case */ - return arg_parse( arg, opts ); - - initialize( arg, filename, lineno ); - - /* find the next keyword */ - state = i = 0; - for(;;) { - c=getc(fp); - if( c == '\n' || c== EOF ) { - if( c != EOF ) - ++*lineno; - if( state == -1 ) - break; - else if( state == 2 ) { - keyword[i] = 0; - for(i=0; opts[i].short_opt; i++ ) - if( opts[i].long_opt && !strcmp( opts[i].long_opt, keyword) ) - break; - idx = i; - arg->r_opt = opts[idx].short_opt; - if( inverse ) /* this does not have an effect, hmmm */ - arg->r_opt = -arg->r_opt; - if( !opts[idx].short_opt ) /* unknown command/option */ - arg->r_opt = (opts[idx].flags & 256)? -7:-2; - else if( (opts[idx].flags & 8) ) /* no argument */ - arg->r_opt = -3; /* error */ - else /* no or optional argument */ - arg->r_type = 0; /* okay */ - break; - } - else if( state == 3 ) { /* no argument found */ - if( in_alias ) - arg->r_opt = -3; /* error */ - else if( !(opts[idx].flags & 7) ) /* does not take an arg */ - arg->r_type = 0; /* okay */ - else if( (opts[idx].flags & 8) ) /* no optional argument */ - arg->r_type = 0; /* okay */ - else /* no required argument */ - arg->r_opt = -3; /* error */ - break; - } - else if( state == 4 ) { /* have an argument */ - if( in_alias ) { - if( !buffer ) - arg->r_opt = -6; - else { - char *p; - - buffer[i] = 0; - p = strpbrk( buffer, " \t" ); - if( p ) { - *p++ = 0; - trim_spaces( p ); - } - if( !p || !*p ) { - libutil_free( buffer ); - arg->r_opt = -10; - } - else { - store_alias( arg, buffer, p ); - } - } - } - else if( !(opts[idx].flags & 7) ) /* does not take an arg */ - arg->r_opt = -6; /* error */ - else { - char *p; - if( !buffer ) { - keyword[i] = 0; - buffer = libutil_strdup(keyword); - } - else - buffer[i] = 0; - - trim_spaces( buffer ); - p = buffer; - if( *p == '"' ) { /* remove quotes */ - p++; - if( *p && p[strlen(p)-1] == '"' ) - p[strlen(p)-1] = 0; - } - if( !set_opt_arg(arg, opts[idx].flags, p) ) - libutil_free(buffer); - } - break; - } - else if( c == EOF ) { - if( ferror(fp) ) - arg->r_opt = -5; /* read error */ - else - arg->r_opt = 0; /* eof */ - break; - } - state = 0; - i = 0; - } - else if( state == -1 ) - ; /* skip */ - else if( !state && isspace(c) ) - ; /* skip leading white space */ - else if( !state && c == '#' ) - state = 1; /* start of a comment */ - else if( state == 1 ) - ; /* skip comments */ - else if( state == 2 && isspace(c) ) { - keyword[i] = 0; - for(i=0; opts[i].short_opt; i++ ) - if( opts[i].long_opt && !strcmp( opts[i].long_opt, keyword) ) - break; - idx = i; - arg->r_opt = opts[idx].short_opt; - if( !opts[idx].short_opt ) { - if( !strcmp( keyword, "alias" ) ) { - in_alias = 1; - state = 3; - } - else { - arg->r_opt = (opts[idx].flags & 256)? -7:-2; - state = -1; /* skip rest of line and leave */ - } - } - else - state = 3; - } - else if( state == 3 ) { /* skip leading spaces of the argument */ - if( !isspace(c) ) { - i = 0; - keyword[i++] = c; - state = 4; - } - } - else if( state == 4 ) { /* collect the argument */ - if( buffer ) { - if( i < buflen-1 ) - buffer[i++] = c; - else { - buflen += 50; - buffer = libutil_realloc(buffer, buflen); - buffer[i++] = c; - } - } - else if( i < DIM(keyword)-1 ) - keyword[i++] = c; - else { - buflen = DIM(keyword)+50; - buffer = libutil_xmalloc(buflen); - memcpy(buffer, keyword, i); - buffer[i++] = c; - } - } - else if( i >= DIM(keyword)-1 ) { - arg->r_opt = -4; /* keyword to long */ - state = -1; /* skip rest of line and leave */ - } - else { - keyword[i++] = c; - state = 2; - } - } - - return arg->r_opt; -} - - - -static int -find_long_option( ARGPARSE_ARGS *arg, - ARGPARSE_OPTS *opts, const char *keyword ) -{ - int i; - size_t n; - - /* Would be better if we can do a binary search, but it is not - possible to reorder our option table because we would mess - up our help strings - What we can do is: Build a nice option - lookup table wehn this function is first invoked */ - if( !*keyword ) - return -1; - for(i=0; opts[i].short_opt; i++ ) - if( opts[i].long_opt && !strcmp( opts[i].long_opt, keyword) ) - return i; - #if 0 - { - ALIAS_DEF a; - /* see whether it is an alias */ - for( a = args->internal.aliases; a; a = a->next ) { - if( !strcmp( a->name, keyword) ) { - /* todo: must parse the alias here */ - args->internal.cur_alias = a; - return -3; /* alias available */ - } - } - } - #endif - /* not found, see whether it is an abbreviation */ - /* aliases may not be abbreviated */ - n = strlen( keyword ); - for(i=0; opts[i].short_opt; i++ ) { - if( opts[i].long_opt && !strncmp( opts[i].long_opt, keyword, n ) ) { - int j; - for(j=i+1; opts[j].short_opt; j++ ) { - if( opts[j].long_opt - && !strncmp( opts[j].long_opt, keyword, n ) ) - return -2; /* abbreviation is ambiguous */ - } - return i; - } - } - return -1; -} - -int -arg_parse( ARGPARSE_ARGS *arg, ARGPARSE_OPTS *opts) -{ - int idx; - int argc; - char **argv; - char *s, *s2; - int i; - - initialize( arg, NULL, NULL ); - argc = *arg->argc; - argv = *arg->argv; - idx = arg->internal.idx; - - if( !idx && argc && !(arg->flags & (1<<4)) ) { /* skip the first entry */ - argc--; argv++; idx++; - } - - next_one: - if( !argc ) { /* no more args */ - arg->r_opt = 0; - goto leave; /* ready */ - } - - s = *argv; - arg->internal.last = s; - - if( arg->internal.stopped && (arg->flags & (1<<1)) ) { - arg->r_opt = -1; /* not an option but a argument */ - arg->r_type = 2; - arg->r.ret_str = s; - argc--; argv++; idx++; /* set to next one */ - } - else if( arg->internal.stopped ) { /* ready */ - arg->r_opt = 0; - goto leave; - } - else if( *s == '-' && s[1] == '-' ) { /* long option */ - char *argpos; - - arg->internal.inarg = 0; - if( !s[2] && !(arg->flags & (1<<3)) ) { /* stop option processing */ - arg->internal.stopped = 1; - argc--; argv++; idx++; - goto next_one; - } - - argpos = strchr( s+2, '=' ); - if( argpos ) - *argpos = 0; - i = find_long_option( arg, opts, s+2 ); - if( argpos ) - *argpos = '='; - - if( i < 0 && !strcmp( "help", s+2) ) - show_help(opts, arg->flags); - else if( i < 0 && !strcmp( "version", s+2) ) { - if( !(arg->flags & (1<<6)) ) { - show_version(); - exit(0); - } - } - else if( i < 0 && !strcmp( "warranty", s+2) ) { - puts( strusage(16) ); - exit(0); - } - else if( i < 0 && !strcmp( "dump-options", s+2) ) { - for(i=0; opts[i].short_opt; i++ ) { - if( opts[i].long_opt ) - printf( "--%s\n", opts[i].long_opt ); - } - fputs("--dump-options\n--help\n--version\n--warranty\n", stdout ); - exit(0); - } - - if( i == -2 ) /* ambiguous option */ - arg->r_opt = -8; - else if( i == -1 ) { - arg->r_opt = -2; - arg->r.ret_str = s+2; - } - else - arg->r_opt = opts[i].short_opt; - if( i < 0 ) - ; - else if( (opts[i].flags & 7) ) { - if( argpos ) { - s2 = argpos+1; - if( !*s2 ) - s2 = NULL; - } - else - s2 = argv[1]; - if( !s2 && (opts[i].flags & 8) ) { /* no argument but it is okay*/ - arg->r_type = 0; /* because it is optional */ - } - else if( !s2 ) { - arg->r_opt = -3; /* missing argument */ - } - else if( !argpos && *s2 == '-' && (opts[i].flags & 8) ) { - /* the argument is optional and the next seems to be - * an option. We do not check this possible option - * but assume no argument */ - arg->r_type = 0; - } - else { - set_opt_arg(arg, opts[i].flags, s2); - if( !argpos ) { - argc--; argv++; idx++; /* skip one */ - } - } - } - else { /* does not take an argument */ - if( argpos ) - arg->r_type = -6; /* argument not expected */ - else - arg->r_type = 0; - } - argc--; argv++; idx++; /* set to next one */ - } - else if( (*s == '-' && s[1]) || arg->internal.inarg ) { /* short option */ - int dash_kludge = 0; - i = 0; - if( !arg->internal.inarg ) { - arg->internal.inarg++; - if( arg->flags & (1<<5) ) { - for(i=0; opts[i].short_opt; i++ ) - if( opts[i].long_opt && !strcmp( opts[i].long_opt, s+1)) { - dash_kludge=1; - break; - } - } - } - s += arg->internal.inarg; - - if( !dash_kludge ) { - for(i=0; opts[i].short_opt; i++ ) - if( opts[i].short_opt == *s ) - break; - } - - if( !opts[i].short_opt && ( *s == 'h' || *s == '?' ) ) - show_help(opts, arg->flags); - - arg->r_opt = opts[i].short_opt; - if( !opts[i].short_opt ) { - arg->r_opt = (opts[i].flags & 256)? -7:-2; - arg->internal.inarg++; /* point to the next arg */ - arg->r.ret_str = s; - } - else if( (opts[i].flags & 7) ) { - if( s[1] && !dash_kludge ) { - s2 = s+1; - set_opt_arg(arg, opts[i].flags, s2); - } - else { - s2 = argv[1]; - if( !s2 && (opts[i].flags & 8) ) { /* no argument but it is okay*/ - arg->r_type = 0; /* because it is optional */ - } - else if( !s2 ) { - arg->r_opt = -3; /* missing argument */ - } - else if( *s2 == '-' && s2[1] && (opts[i].flags & 8) ) { - /* the argument is optional and the next seems to be - * an option. We do not check this possible option - * but assume no argument */ - arg->r_type = 0; - } - else { - set_opt_arg(arg, opts[i].flags, s2); - argc--; argv++; idx++; /* skip one */ - } - } - s = "x"; /* so that !s[1] yields false */ - } - else { /* does not take an argument */ - arg->r_type = 0; - arg->internal.inarg++; /* point to the next arg */ - } - if( !s[1] || dash_kludge ) { /* no more concatenated short options */ - arg->internal.inarg = 0; - argc--; argv++; idx++; - } - } - else if( arg->flags & (1<<2) ) { - arg->r_opt = -1; /* not an option but a argument */ - arg->r_type = 2; - arg->r.ret_str = s; - argc--; argv++; idx++; /* set to next one */ - } - else { - arg->internal.stopped = 1; /* stop option processing */ - goto next_one; - } - - leave: - *arg->argc = argc; - *arg->argv = argv; - arg->internal.idx = idx; - return arg->r_opt; -} - - - -static int -set_opt_arg(ARGPARSE_ARGS *arg, unsigned flags, char *s) -{ - int base = (flags & 16)? 0 : 10; - - switch( arg->r_type = (flags & 7) ) { - case 1: /* takes int argument */ - arg->r.ret_int = (int)strtol(s,NULL,base); - return 0; - case 3: /* takes long argument */ - arg->r.ret_long= strtol(s,NULL,base); - return 0; - case 4: /* takes ulong argument */ - arg->r.ret_ulong= strtoul(s,NULL,base); - return 0; - case 2: /* takes string argument */ - default: - arg->r.ret_str = s; - return 1; - } -} - - -static size_t -long_opt_strlen( ARGPARSE_OPTS *o ) -{ - size_t n = strlen(o->long_opt); - - if( o->description && *o->description == '|' ) { - const char *s; - - s=o->description+1; - if( *s != '=' ) - n++; - for(; *s && *s != '|'; s++ ) - n++; - } - return n; -} - -/**************** - * Print formatted help. The description string has some special - * meanings: - * - A description string which is "@" suppresses help output for - * this option - * - a description,ine which starts with a '@' and is followed by - * any other characters is printed as is; this may be used for examples - * ans such. - * - A description which starts with a '|' outputs the string between this - * bar and the next one as arguments of the long option. - */ -static void -show_help( ARGPARSE_OPTS *opts, unsigned flags ) -{ - const char *s; - - show_version(); - putchar('\n'); - s = strusage(41); - puts(s); - if( opts[0].description ) { /* auto format the option description */ - int i,j, indent; - /* get max. length of long options */ - for(i=indent=0; opts[i].short_opt; i++ ) { - if( opts[i].long_opt ) - if( !opts[i].description || *opts[i].description != '@' ) - if( (j=long_opt_strlen(opts+i)) > indent && j < 35 ) - indent = j; - } - /* example: " -v, --verbose Viele Sachen ausgeben" */ - indent += 10; - if( *opts[0].description != '@' ) - puts("Options:"); - for(i=0; opts[i].short_opt; i++ ) { - s = _( opts[i].description ); - if( s && *s== '@' && !s[1] ) /* hide this line */ - continue; - if( s && *s == '@' ) { /* unindented comment only line */ - for(s++; *s; s++ ) { - if( *s == '\n' ) { - if( s[1] ) - putchar('\n'); - } - else - putchar(*s); - } - putchar('\n'); - continue; - } - - j = 3; - if( opts[i].short_opt < 256 ) { - printf(" -%c", opts[i].short_opt ); - if( !opts[i].long_opt ) { - if(s && *s == '|' ) { - putchar(' '); j++; - for(s++ ; *s && *s != '|'; s++, j++ ) - putchar(*s); - if( *s ) - s++; - } - } - } - else - fputs(" ", stdout); - if( opts[i].long_opt ) { - j += printf("%c --%s", opts[i].short_opt < 256?',':' ', - opts[i].long_opt ); - if(s && *s == '|' ) { - if( *++s != '=' ) { - putchar(' '); - j++; - } - for( ; *s && *s != '|'; s++, j++ ) - putchar(*s); - if( *s ) - s++; - } - fputs(" ", stdout); - j += 3; - } - for(;j < indent; j++ ) - putchar(' '); - if( s ) { - if( *s && j > indent ) { - putchar('\n'); - for(j=0;j < indent; j++ ) - putchar(' '); - } - for(; *s; s++ ) { - if( *s == '\n' ) { - if( s[1] ) { - putchar('\n'); - for(j=0;j < indent; j++ ) - putchar(' '); - } - } - else - putchar(*s); - } - } - putchar('\n'); - } - if( flags & 32 ) - puts("\n(A single dash may be used instead of the double ones)"); - } - if( (s=strusage(19)) ) { /* bug reports to ... */ - putchar('\n'); - fputs(s, stdout); - } - fflush(stdout); - exit(0); -} - -static void -show_version() -{ - const char *s; - int i; - /* version line */ - fputs(strusage(11), stdout); - if( (s=strusage(12)) ) - printf(" (%s)", s ); - printf(" %s\n", strusage(13) ); - /* additional version lines */ - for(i=20; i < 30; i++ ) - if( (s=strusage(i)) ) - printf("%s\n", s ); - /* copyright string */ - if( (s=strusage(14)) ) - printf("%s\n", s ); - /* copying conditions */ - if( (s=strusage(15)) ) - fputs(s, stdout); - /* thanks */ - if( (s=strusage(18)) ) - fputs(s, stdout); - /* additional program info */ - for(i=30; i < 40; i++ ) - if( (s=strusage(i)) ) - fputs( (const byte*)s, stdout); - fflush(stdout); -} - - -void -usage( int level ) -{ - if( !level ) { - fprintf(stderr,"%s %s; %s\n", strusage(11), strusage(13), - strusage(14) ); - fflush(stderr); - } - else if( level == 1 ) { - fputs(strusage(40),stderr); - exit(2); - } - else if( level == 2 ) { - puts(strusage(41)); - exit(0); - } -} - -/* Level - * 0: Copyright String auf stderr ausgeben - * 1: Kurzusage auf stderr ausgeben und beenden - * 2: Langusage auf stdout ausgeben und beenden - * 11: name of program - * 12: optional name of package which includes this program. - * 13: version string - * 14: copyright string - * 15: Short copying conditions (with LFs) - * 16: Long copying conditions (with LFs) - * 17: Optional printable OS name - * 18: Optional thanks list (with LFs) - * 19: Bug report info - *20..29: Additional lib version strings. - *30..39: Additional program info (with LFs) - * 40: short usage note (with LF) - * 41: long usage note (with LF) - */ -const char * -strusage( int level ) -{ - const char *p = strusage_handler? strusage_handler(level) : NULL; - - if( p ) - return p; - - switch( level ) { - case 11: p = "foo"; break; - case 13: p = "0.0"; break; - case 14: p = "Copyright (C) 1999 Free Software Foundation, Inc."; break; - case 15: p = -"This program comes with ABSOLUTELY NO WARRANTY.\n" -"This is free software, and you are welcome to redistribute it\n" -"under certain conditions. See the file COPYING for details.\n"; break; - case 16: p = -"This is free software; you can redistribute it and/or modify\n" -"it under the terms of the GNU General Public License as published by\n" -"the Free Software Foundation; either version 2 of the License, or\n" -"(at your option) any later version.\n\n" -"It is distributed in the hope that it will be useful,\n" -"but WITHOUT ANY WARRANTY; without even the implied warranty of\n" -"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n" -"GNU General Public License for more details.\n\n" -"You should have received a copy of the GNU General Public License\n" -"along with this program; if not, write to the Free Software\n" -"Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.\n"; - break; - case 40: /* short and long usage */ - case 41: p = ""; break; - } - - return p; -} - -void -set_strusage( const char *(*f)( int ) ) -{ - strusage_handler = f; -} - - -#ifdef TEST -static struct { - int verbose; - int debug; - char *outfile; - char *crf; - int myopt; - int echo; - int a_long_one; -}opt; - -int -main(int argc, char **argv) -{ - ARGPARSE_OPTS opts[] = { - { 'v', "verbose", 0 , "Laut sein"}, - { 'e', "echo" , 0 , "Zeile ausgeben, damit wir sehen, was wir einegegeben haben"}, - { 'd', "debug", 0 , "Debug\nfalls mal etasws\nSchief geht"}, - { 'o', "output", 2 }, - { 'c', "cross-ref", 2|8, "cross-reference erzeugen\n" }, - { 'm', "my-option", 1|8 }, - { 500, "a-long-option", 0 }, - {0} }; - ARGPARSE_ARGS pargs = { &argc, &argv, 2|4|32 }; - int i; - - while( ArgParse( &pargs, opts) ) { - switch( pargs.r_opt ) { - case -1 : printf( "arg=`%s'\n", pargs.r.ret_str); break; - case 'v': opt.verbose++; break; - case 'e': opt.echo++; break; - case 'd': opt.debug++; break; - case 'o': opt.outfile = pargs.r.ret_str; break; - case 'c': opt.crf = pargs.r_type? pargs.r.ret_str:"a.crf"; break; - case 'm': opt.myopt = pargs.r_type? pargs.r.ret_int : 1; break; - case 500: opt.a_long_one++; break; - default : pargs.err = 1; break; /* force warning output */ - } - } - for(i=0; i < argc; i++ ) - printf("%3d -> (%s)\n", i, argv[i] ); - puts("Options:"); - if( opt.verbose ) - printf(" verbose=%d\n", opt.verbose ); - if( opt.debug ) - printf(" debug=%d\n", opt.debug ); - if( opt.outfile ) - printf(" outfile=`%s'\n", opt.outfile ); - if( opt.crf ) - printf(" crffile=`%s'\n", opt.crf ); - if( opt.myopt ) - printf(" myopt=%d\n", opt.myopt ); - if( opt.a_long_one ) - printf(" a-long-one=%d\n", opt.a_long_one ); - if( opt.echo ) - printf(" echo=%d\n", opt.echo ); - return 0; -} -#endif - -/**** bottom of file ****/ diff --git a/util/argparse.h b/util/argparse.h deleted file mode 100644 index 12132edd3..000000000 --- a/util/argparse.h +++ /dev/null @@ -1,64 +0,0 @@ -/* argparse.h - * Copyright (C) 1998,1999 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 2 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, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA - */ - -#ifndef LIBUTIL_ARGPARSE_H -#define LIBUTIL_ARGPARSE_H - -typedef struct { - int *argc; /* pointer to argc (value subject to change) */ - char ***argv; /* pointer to argv (value subject to change) */ - unsigned flags; /* Global flags (DO NOT CHANGE) */ - int err; /* print error about last option */ - /* 1 = warning, 2 = abort */ - int r_opt; /* return option */ - int r_type; /* type of return value (0 = no argument found)*/ - union { - int ret_int; - long ret_long; - ulong ret_ulong; - char *ret_str; - } r; /* Return values */ - struct { - int idx; - int inarg; - int stopped; - const char *last; - void *aliases; - const void *cur_alias; - } internal; /* DO NOT CHANGE */ -} ARGPARSE_ARGS; - -typedef struct { - int short_opt; - const char *long_opt; - unsigned flags; - const char *description; /* optional option description */ -} ARGPARSE_OPTS; - - - -int arg_parse( ARGPARSE_ARGS *arg, ARGPARSE_OPTS *opts); -int optfile_parse( FILE *fp, const char *filename, unsigned *lineno, - ARGPARSE_ARGS *arg, ARGPARSE_OPTS *opts); -void usage( int level ); -const char *strusage( int level ); -void set_strusage( const char *(*f)( int ) ); - -#endif /*LIBUTIL_ARGPARSE_H*/ diff --git a/util/dotlock.c b/util/dotlock.c deleted file mode 100644 index 369a3d42a..000000000 --- a/util/dotlock.c +++ /dev/null @@ -1,346 +0,0 @@ -/* dotlock.c - dotfile locking - * Copyright (C) 1998 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 2 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, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA - */ - -#include -#include -#include -#include -#include -#include -#include -#ifndef HAVE_DOSISH_SYSTEM -#include -#endif -#include -#include -#include -#include -#include -#include "types.h" -#include "util.h" -#include "memory.h" - -struct dotlock_handle { - struct dotlock_handle *next; - char *tname; /* name of lockfile template */ - char *lockname; /* name of the real lockfile */ - int locked; /* lock status */ -}; - - -static DOTLOCK all_lockfiles; - -static int read_lockfile( const char *name ); -static void remove_lockfiles(void); - -/**************** - * Create a lockfile with the given name and return an object of - * type DOTLOCK which may be used later to actually do the lock. - * A cleanup routine gets installed to cleanup left over locks - * or other files used together with the lockmechanism. - * Althoug the function is called dotlock, this does not necessarily - * mean that real lockfiles are used - the function may decide to - * use fcntl locking. Calling the function with NULL only install - * the atexit handler and maybe used to assure that the cleanup - * is called after all other atexit handlers. - * - * Notes: This function creates a lock file in the same directory - * as file_to_lock with the name "file_to_lock.lock" - * A temporary file ".#lk..pid[.threadid] is used. - * This function does nothing for Windoze. - */ -DOTLOCK -create_dotlock( const char *file_to_lock ) -{ - static int initialized; - DOTLOCK h; - int fd = -1; - char pidstr[16]; - #ifndef HAVE_DOSISH_SYSTEM - struct utsname utsbuf; - #endif - const char *nodename; - const char *dirpart; - int dirpartlen; - - if( !initialized ) { - atexit( remove_lockfiles ); - initialized = 1; - } - if( !file_to_lock ) - return NULL; - - h = m_alloc_clear( sizeof *h ); -#ifndef HAVE_DOSISH_SYSTEM - sprintf( pidstr, "%10d\n", (int)getpid() ); - /* fixme: add the hostname to the second line (FQDN or IP addr?) */ - - /* create a temporary file */ - if( uname( &utsbuf ) ) - nodename = "unknown"; - else - nodename = utsbuf.nodename; - - if( !(dirpart = strrchr( file_to_lock, '/' )) ) { - dirpart = "."; - dirpartlen = 1; - } - else { - dirpartlen = dirpart - file_to_lock; - dirpart = file_to_lock; - } - - #ifdef _REENTRANT - /* fixme: aquire mutex on all_lockfiles */ - #endif - h->next = all_lockfiles; - all_lockfiles = h; - - h->tname = m_alloc( dirpartlen + 6+30+ strlen(nodename) + 11 ); - sprintf( h->tname, "%.*s/.#lk%p.%s.%d", - dirpartlen, dirpart, h, nodename, (int)getpid() ); - - do { - errno = 0; - fd = open( h->tname, O_WRONLY|O_CREAT|O_EXCL, - S_IRUSR|S_IRGRP|S_IROTH|S_IWUSR ); - } while( fd == -1 && errno == EINTR ); - if( fd == -1 ) { - all_lockfiles = h->next; - log_error( "failed to create temporary file `%s': %s\n", - h->tname, strerror(errno)); - m_free(h->tname); - m_free(h); - return NULL; - } - if( write(fd, pidstr, 11 ) != 11 ) { - all_lockfiles = h->next; - #ifdef _REENTRANT - /* release mutex */ - #endif - log_fatal( "error writing to `%s': %s\n", h->tname, strerror(errno) ); - close(fd); - unlink(h->tname); - m_free(h->tname); - m_free(h); - return NULL; - } - if( close(fd) ) { - all_lockfiles = h->next; - #ifdef _REENTRANT - /* release mutex */ - #endif - log_error( "error closing `%s': %s\n", h->tname, strerror(errno)); - unlink(h->tname); - m_free(h->tname); - m_free(h); - return NULL; - } - - #ifdef _REENTRANT - /* release mutex */ - #endif -#endif /* !HAVE_DOSISH_SYSTEM */ - h->lockname = m_alloc( strlen(file_to_lock) + 6 ); - strcpy(stpcpy(h->lockname, file_to_lock), ".lock"); - return h; -} - -static int -maybe_deadlock( DOTLOCK h ) -{ - DOTLOCK r; - - for( r=all_lockfiles; r; r = r->next ) { - if( r != h && r->locked ) - return 1; - } - return 0; -} - -/**************** - * Do a lock on H. A TIMEOUT of 0 returns immediately, - * -1 waits forever (hopefully not), other - * values are timeouts in milliseconds. - * Returns: 0 on success - */ -int -make_dotlock( DOTLOCK h, long timeout ) -{ -#ifdef HAVE_DOSISH_SYSTEM - return 0; -#else - int pid; - const char *maybe_dead=""; - int backoff=0; - - if( h->locked ) { - log_debug("oops, `%s' is already locked\n", h->lockname ); - return 0; - } - - for(;;) { - if( !link(h->tname, h->lockname) ) { - /* fixme: better use stat to check the link count */ - h->locked = 1; - return 0; /* okay */ - } - if( errno != EEXIST ) { - log_error( "lock not made: link() failed: %s\n", strerror(errno) ); - return -1; - } - if( (pid = read_lockfile(h->lockname)) == -1 ) { - if( errno != ENOENT ) { - log_info("cannot read lockfile\n"); - return -1; - } - log_info( "lockfile disappeared\n"); - continue; - } - else if( pid == getpid() ) { - log_info( "Oops: lock already hold by us\n"); - h->locked = 1; - return 0; /* okay */ - } - else if( kill(pid, 0) && errno == ESRCH ) { - maybe_dead = " - probably dead"; - #if 0 /* we should not do this without checking the permissions */ - /* and the hostname */ - log_info( "removing stale lockfile (created by %d)", pid ); - #endif - } - if( timeout == -1 ) { - struct timeval tv; - log_info( "waiting for lock (hold by %d%s) %s...\n", - pid, maybe_dead, maybe_deadlock(h)? "(deadlock?) ":""); - - - /* can't use sleep, cause signals may be blocked */ - tv.tv_sec = 1 + backoff; - tv.tv_usec = 0; - select(0, NULL, NULL, NULL, &tv); - if( backoff < 10 ) - backoff++ ; - } - else - return -1; - } - /*not reached */ -#endif /* !HAVE_DOSISH_SYSTEM */ -} - - -/**************** - * release a lock - * Returns: 0 := success - */ -int -release_dotlock( DOTLOCK h ) -{ -#ifdef HAVE_DOSISH_SYSTEM - return 0; -#else - int pid; - - if( !h->locked ) { - log_debug("oops, `%s' is not locked\n", h->lockname ); - return 0; - } - - pid = read_lockfile( h->lockname ); - if( pid == -1 ) { - log_error( "release_dotlock: lockfile error\n"); - return -1; - } - if( pid != getpid() ) { - log_error( "release_dotlock: not our lock (pid=%d)\n", pid); - return -1; - } - if( unlink( h->lockname ) ) { - log_error( "release_dotlock: error removing lockfile `%s'", - h->lockname); - return -1; - } - /* fixme: check that the link count is now 1 */ - h->locked = 0; - return 0; -#endif /* !HAVE_DOSISH_SYSTEM */ -} - - -/**************** - * Read the lock file and return the pid, returns -1 on error. - */ -static int -read_lockfile( const char *name ) -{ - #ifdef HAVE_DOSISH_SYSTEM - return 0; - #else - int fd, pid; - char pidstr[16]; - - if( (fd = open(name, O_RDONLY)) == -1 ) { - int e = errno; - log_debug("error opening lockfile `%s': %s\n", name, strerror(errno) ); - errno = e; - return -1; - } - if( read(fd, pidstr, 10 ) != 10 ) { /* Read 10 digits w/o newline */ - log_debug("error reading lockfile `%s'", name ); - close(fd); - errno = 0; - return -1; - } - pidstr[10] = 0; /* terminate pid string */ - close(fd); - pid = atoi(pidstr); - if( !pid || pid == -1 ) { - log_error("invalid pid %d in lockfile `%s'", pid, name ); - errno = 0; - return -1; - } - return pid; - #endif -} - - -static void -remove_lockfiles() -{ - #ifndef HAVE_DOSISH_SYSTEM - DOTLOCK h, h2; - - h = all_lockfiles; - all_lockfiles = NULL; - - while( h ) { - h2 = h->next; - if( h->locked ) - unlink( h->lockname ); - unlink(h->tname); - m_free(h->tname); - m_free(h->lockname); - m_free(h); - h = h2; - } - #endif -} - diff --git a/util/fileutil.c b/util/fileutil.c index 7910631c2..fe92c5c17 100644 --- a/util/fileutil.c +++ b/util/fileutil.c @@ -45,10 +45,10 @@ make_basename(const char *filepath) if ( !(p=strrchr(filepath, ':')) ) #endif { - return m_strdup(filepath); + return gcry_xstrdup(filepath); } - return m_strdup(p+1); + return gcry_xstrdup(p+1); } @@ -72,11 +72,11 @@ make_dirname(const char *filepath) if ( !(p=strrchr(filepath, ':')) ) #endif { - return m_strdup("."); + return gcry_xstrdup("."); } dirname_length = p-filepath; - dirname = m_alloc(dirname_length+1); + dirname = gcry_xmalloc(dirname_length+1); strncpy(dirname, filepath, dirname_length); dirname[dirname_length] = 0; @@ -108,7 +108,7 @@ make_filename( const char *first_part, ... ) && (home = getenv("HOME")) && *home ) n += strlen(home); - name = m_alloc(n); + name = gcry_xmalloc(n); p = home ? stpcpy(stpcpy(name,home), first_part+1) : stpcpy(name, first_part); va_start( arg_ptr, first_part ) ; diff --git a/util/http.c b/util/http.c index 4bac8d845..fac26744b 100644 --- a/util/http.c +++ b/util/http.c @@ -175,7 +175,7 @@ http_close( HTTP_HD hd ) iobuf_close( hd->fp_read ); iobuf_close( hd->fp_write ); release_parsed_uri( hd->uri ); - m_free( hd->buffer ); + gcry_free( hd->buffer ); hd->initialized = 0; } @@ -189,7 +189,7 @@ http_close( HTTP_HD hd ) static int parse_uri( PARSED_URI *ret_uri, const char *uri ) { - *ret_uri = m_alloc_clear( sizeof(**ret_uri) + strlen(uri) ); + *ret_uri = gcry_xcalloc( 1, sizeof(**ret_uri) + strlen(uri) ); strcpy( (*ret_uri)->buffer, uri ); return do_parse_uri( *ret_uri, 0 ); } @@ -203,9 +203,9 @@ release_parsed_uri( PARSED_URI uri ) for( r = uri->query; r; r = r2 ) { r2 = r->next; - m_free( r ); + gcry_free( r ); } - m_free( uri ); + gcry_free( uri ); } } @@ -397,7 +397,7 @@ parse_tuple( byte *string ) return NULL; /* bad URI */ if( n != strlen( p ) ) return NULL; /* name with a Nul in it */ - tuple = m_alloc_clear( sizeof *tuple ); + tuple = gcry_xcalloc( 1, sizeof *tuple ); tuple->name = p; if( !p2 ) { /* we have only the name, so we assume an empty value string */ @@ -406,7 +406,7 @@ parse_tuple( byte *string ) } else { /* name and value */ if( (n = remove_escapes( p2 )) < 0 ) { - m_free( tuple ); + gcry_free( tuple ); return NULL; /* bad URI */ } tuple->value = p2; @@ -436,16 +436,16 @@ send_request( HTTP_HD hd ) return G10ERR_NETWORK; p = build_rel_path( hd->uri ); - request = m_alloc( strlen(p) + 20 ); + request = gcry_xmalloc( strlen(p) + 20 ); sprintf( request, "%s %s%s HTTP/1.0\r\n", hd->req_type == HTTP_REQ_GET ? "GET" : hd->req_type == HTTP_REQ_HEAD? "HEAD": hd->req_type == HTTP_REQ_POST? "POST": "OOPS", *p == '/'? "":"/", p ); - m_free(p); + gcry_free(p); rc = write_server( hd->sock, request, strlen(request) ); - m_free( request ); + gcry_free( request ); return rc; } @@ -476,7 +476,7 @@ build_rel_path( PARSED_URI uri ) n++; /* now allocate and copy */ - p = rel_path = m_alloc( n ); + p = rel_path = gcry_xmalloc( n ); n = insert_escapes( p, uri->path, "%;?&" ); p += n; /* todo: add params */ diff --git a/util/iobuf.c b/util/iobuf.c index 83c1e475d..e07431024 100644 --- a/util/iobuf.c +++ b/util/iobuf.c @@ -132,7 +132,7 @@ file_filter(void *opaque, int control, IOBUF chain, byte *buf, size_t *ret_len) fclose(fp); } fp = NULL; - m_free(a); /* we can free our context now */ + gcry_free(a); /* we can free our context now */ } return rc; @@ -267,7 +267,7 @@ block_filter(void *opaque, int control, IOBUF chain, byte *buf, size_t *ret_len) if( nbytes < OP_MIN_PARTIAL_CHUNK ) { /* not enough to write a partial block out; so we store it*/ if( !a->buffer ) - a->buffer = m_alloc( OP_MIN_PARTIAL_CHUNK ); + a->buffer = gcry_xmalloc( OP_MIN_PARTIAL_CHUNK ); memcpy( a->buffer + a->buflen, buf, size ); a->buflen += size; } @@ -305,7 +305,7 @@ block_filter(void *opaque, int control, IOBUF chain, byte *buf, size_t *ret_len) assert( !a->buflen ); assert( nbytes < OP_MIN_PARTIAL_CHUNK ); if( !a->buffer ) - a->buffer = m_alloc( OP_MIN_PARTIAL_CHUNK ); + a->buffer = gcry_xmalloc( OP_MIN_PARTIAL_CHUNK ); memcpy( a->buffer, p, nbytes ); a->buflen = nbytes; } @@ -393,7 +393,7 @@ block_filter(void *opaque, int control, IOBUF chain, byte *buf, size_t *ret_len) log_error("block_filter: write error: %s\n",strerror(errno)); rc = G10ERR_WRITE_FILE; } - m_free( a->buffer ); a->buffer = NULL; a->buflen = 0; + gcry_free( a->buffer ); a->buffer = NULL; a->buflen = 0; } else { iobuf_writebyte(chain, 0); @@ -405,7 +405,7 @@ block_filter(void *opaque, int control, IOBUF chain, byte *buf, size_t *ret_len) } if( DBG_IOBUF ) log_debug("free block_filter %p\n", a ); - m_free(a); /* we can free our context now */ + gcry_free(a); /* we can free our context now */ } return rc; @@ -449,9 +449,9 @@ iobuf_alloc(int use, size_t bufsize) IOBUF a; static int number=0; - a = m_alloc_clear(sizeof *a); + a = gcry_xcalloc( 1,sizeof *a); a->use = use; - a->d.buf = m_alloc( bufsize ); + a->d.buf = gcry_xmalloc( bufsize ); a->d.size = bufsize; a->no = ++number; a->subno = 0; @@ -470,7 +470,7 @@ iobuf_close( IOBUF a ) if( a && a->directfp ) { fclose( a->directfp ); - m_free( a->real_fname ); + gcry_free( a->real_fname ); if( DBG_IOBUF ) log_debug("iobuf_close -> %p\n", a->directfp ); return 0; @@ -486,9 +486,9 @@ iobuf_close( IOBUF a ) if( a->filter && (rc = a->filter(a->filter_ov, IOBUFCTRL_FREE, a->chain, NULL, &dummy_len)) ) log_error("IOBUFCTRL_FREE failed on close: %s\n", g10_errstr(rc) ); - m_free(a->real_fname); - m_free(a->d.buf); - m_free(a); + gcry_free(a->real_fname); + gcry_free(a->d.buf); + gcry_free(a); } return rc; } @@ -556,12 +556,12 @@ iobuf_open( const char *fname ) else if( !(fp = fopen(fname, "rb")) ) return NULL; a = iobuf_alloc(1, 8192 ); - fcx = m_alloc( sizeof *fcx + strlen(fname) ); + fcx = gcry_xmalloc( sizeof *fcx + strlen(fname) ); fcx->fp = fp; fcx->print_only_name = print_only; strcpy(fcx->fname, fname ); if( !print_only ) - a->real_fname = m_strdup( fname ); + a->real_fname = gcry_xstrdup( fname ); a->filter = file_filter; a->filter_ov = fcx; file_filter( fcx, IOBUFCTRL_DESC, NULL, (byte*)&a->desc, &len ); @@ -588,7 +588,7 @@ iobuf_fdopen( int fd, const char *mode ) if( !(fp = fdopen(fd, mode)) ) return NULL; a = iobuf_alloc( strchr( mode, 'w')? 2:1, 8192 ); - fcx = m_alloc( sizeof *fcx + 20 ); + fcx = gcry_xmalloc( sizeof *fcx + 20 ); fcx->fp = fp; fcx->print_only_name = 1; sprintf(fcx->fname, "[fd %d]", fd ); @@ -622,12 +622,12 @@ iobuf_create( const char *fname ) else if( !(fp = fopen(fname, "wb")) ) return NULL; a = iobuf_alloc(2, 8192 ); - fcx = m_alloc( sizeof *fcx + strlen(fname) ); + fcx = gcry_xmalloc( sizeof *fcx + strlen(fname) ); fcx->fp = fp; fcx->print_only_name = print_only; strcpy(fcx->fname, fname ); if( !print_only ) - a->real_fname = m_strdup( fname ); + a->real_fname = gcry_xstrdup( fname ); a->filter = file_filter; a->filter_ov = fcx; file_filter( fcx, IOBUFCTRL_DESC, NULL, (byte*)&a->desc, &len ); @@ -655,10 +655,10 @@ iobuf_append( const char *fname ) else if( !(fp = fopen(fname, "ab")) ) return NULL; a = iobuf_alloc(2, 8192 ); - fcx = m_alloc( sizeof *fcx + strlen(fname) ); + fcx = gcry_xmalloc( sizeof *fcx + strlen(fname) ); fcx->fp = fp; strcpy(fcx->fname, fname ); - a->real_fname = m_strdup( fname ); + a->real_fname = gcry_xstrdup( fname ); a->filter = file_filter; a->filter_ov = fcx; file_filter( fcx, IOBUFCTRL_DESC, NULL, (byte*)&a->desc, &len ); @@ -682,10 +682,10 @@ iobuf_openrw( const char *fname ) else if( !(fp = fopen(fname, "r+b")) ) return NULL; a = iobuf_alloc(2, 8192 ); - fcx = m_alloc( sizeof *fcx + strlen(fname) ); + fcx = gcry_xmalloc( sizeof *fcx + strlen(fname) ); fcx->fp = fp; strcpy(fcx->fname, fname ); - a->real_fname = m_strdup( fname ); + a->real_fname = gcry_xstrdup( fname ); a->filter = file_filter; a->filter_ov = fcx; file_filter( fcx, IOBUFCTRL_DESC, NULL, (byte*)&a->desc, &len ); @@ -719,7 +719,7 @@ iobuf_fopen( const char *fname, const char *mode ) return NULL; a = iobuf_alloc(1, 8192 ); a->directfp = fp; - a->real_fname = m_strdup( fname ); + a->real_fname = gcry_xstrdup( fname ); if( DBG_IOBUF ) log_debug("iobuf_fopen -> %p\n", a->directfp ); @@ -760,12 +760,12 @@ iobuf_push_filter2( IOBUF a, * The contents of the buffers are transferred to the * new stream. */ - b = m_alloc(sizeof *b); + b = gcry_xmalloc(sizeof *b); memcpy(b, a, sizeof *b ); /* fixme: it is stupid to keep a copy of the name at every level * but we need the name somewhere because the name known by file_filter * may have been released when we need the name of the file */ - b->real_fname = a->real_fname? m_strdup(a->real_fname):NULL; + b->real_fname = a->real_fname? gcry_xstrdup(a->real_fname):NULL; /* remove the filter stuff from the new stream */ a->filter = NULL; a->filter_ov = NULL; @@ -775,12 +775,12 @@ iobuf_push_filter2( IOBUF a, a->use = 2; /* make a write stream from a temp stream */ if( a->use == 2 ) { /* allocate a fresh buffer for the original stream */ - b->d.buf = m_alloc( a->d.size ); + b->d.buf = gcry_xmalloc( a->d.size ); b->d.len = 0; b->d.start = 0; } else { /* allocate a fresh buffer for the new stream */ - a->d.buf = m_alloc( a->d.size ); + a->d.buf = gcry_xmalloc( a->d.size ); a->d.len = 0; a->d.start = 0; } @@ -831,10 +831,10 @@ pop_filter( IOBUF a, int (*f)(void *opaque, int control, if( !a->filter ) { /* this is simple */ b = a->chain; assert(b); - m_free(a->d.buf); - m_free(a->real_fname); + gcry_free(a->d.buf); + gcry_free(a->real_fname); memcpy(a,b, sizeof *a); - m_free(b); + gcry_free(b); return 0; } for(b=a ; b; b = b->chain ) @@ -855,7 +855,7 @@ pop_filter( IOBUF a, int (*f)(void *opaque, int control, return rc; } if( b->filter_ov && b->filter_ov_owner ) { - m_free( b->filter_ov ); + gcry_free( b->filter_ov ); b->filter_ov = NULL; } @@ -868,10 +868,10 @@ pop_filter( IOBUF a, int (*f)(void *opaque, int control, * a flush has been done on the to be removed entry */ b = a->chain; - m_free(a->d.buf); - m_free(a->real_fname); + gcry_free(a->d.buf); + gcry_free(a->real_fname); memcpy(a,b, sizeof *a); - m_free(b); + gcry_free(b); if( DBG_IOBUF ) log_debug("iobuf-%d.%d: popped filter\n", a->no, a->subno ); } @@ -906,10 +906,10 @@ underflow(IOBUF a) if( DBG_IOBUF ) log_debug("iobuf-%d.%d: pop `%s' in underflow\n", a->no, a->subno, a->desc ); - m_free(a->d.buf); - m_free(a->real_fname); + gcry_free(a->d.buf); + gcry_free(a->real_fname); memcpy(a, b, sizeof *a); - m_free(b); + gcry_free(b); print_chain(a); } else @@ -962,7 +962,7 @@ underflow(IOBUF a) NULL, &dummy_len)) ) log_error("IOBUFCTRL_FREE failed: %s\n", g10_errstr(rc) ); if( a->filter_ov && a->filter_ov_owner ) { - m_free( a->filter_ov ); + gcry_free( a->filter_ov ); a->filter_ov = NULL; } a->filter = NULL; @@ -975,10 +975,10 @@ underflow(IOBUF a) log_debug("iobuf-%d.%d: pop `%s' in underflow (!len)\n", a->no, a->subno, a->desc ); print_chain(a); - m_free(a->d.buf); - m_free(a->real_fname); + gcry_free(a->d.buf); + gcry_free(a->real_fname); memcpy(a,b, sizeof *a); - m_free(b); + gcry_free(b); print_chain(a); } } @@ -1019,9 +1019,9 @@ iobuf_flush(IOBUF a) log_debug("increasing temp iobuf from %lu to %lu\n", (ulong)a->d.size, (ulong)newsize ); - newbuf = m_alloc( newsize ); + newbuf = gcry_xmalloc( newsize ); memcpy( newbuf, a->d.buf, a->d.len ); - m_free(a->d.buf); + gcry_free(a->d.buf); a->d.buf = newbuf; a->d.size = newsize; return 0; @@ -1057,7 +1057,7 @@ iobuf_readbyte(IOBUF a) if( a->unget.buf ) { if( a->unget.start < a->unget.len ) return a->unget.buf[a->unget.start++]; - m_free(a->unget.buf); + gcry_free(a->unget.buf); a->unget.buf = NULL; a->nofast &= ~2; } @@ -1401,7 +1401,7 @@ iobuf_get_fname( IOBUF a ) void iobuf_set_block_mode( IOBUF a, size_t n ) { - block_filter_ctx_t *ctx = m_alloc_clear( sizeof *ctx ); + block_filter_ctx_t *ctx = gcry_xcalloc( 1, sizeof *ctx ); assert( a->use == 1 || a->use == 2 ); ctx->use = a->use; @@ -1423,7 +1423,7 @@ iobuf_set_block_mode( IOBUF a, size_t n ) void iobuf_set_partial_block_mode( IOBUF a, size_t len ) { - block_filter_ctx_t *ctx = m_alloc_clear( sizeof *ctx ); + block_filter_ctx_t *ctx = gcry_xcalloc( 1, sizeof *ctx ); assert( a->use == 1 || a->use == 2 ); ctx->use = a->use; @@ -1478,7 +1478,7 @@ iobuf_read_line( IOBUF a, byte **addr_of_buffer, if( !buffer ) { /* must allocate a new buffer */ length = 256; - buffer = m_alloc( length ); + buffer = gcry_xmalloc( length ); *addr_of_buffer = buffer; *length_of_buffer = length; } @@ -1498,7 +1498,7 @@ iobuf_read_line( IOBUF a, byte **addr_of_buffer, } length += 3; /* correct for the reserved byte */ length += length < 1024? 256 : 1024; - buffer = m_realloc( buffer, length ); + buffer = gcry_xrealloc( buffer, length ); *addr_of_buffer = buffer; *length_of_buffer = length; length -= 3; /* and reserve again */ diff --git a/util/libutil-config.h b/util/libutil-config.h deleted file mode 100644 index a4c80115b..000000000 --- a/util/libutil-config.h +++ /dev/null @@ -1,56 +0,0 @@ -/* libutil-config.h - configuration of the libutil functions - * Copyright (C) 1999 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 2 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, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA - */ - -/**************** - * This header is to be included only by the files in this directory - * it should not be used by other modules. - */ - -#ifndef LIBUTIL_CONFIG_H -#define LIBUTIL_CONFIG_H - -#define LIBUTIL_CONFIG_OF_GNUPG 1 /* currently we need this kludge */ - -#include - -#ifndef HAVE_BYTE_TYPEDEF - #undef byte /* (this matches the test used by configure) */ - typedef unsigned char byte; - #define HAVE_BYTE_TYPEDEF -#endif - -#include "types.h" -#include "memory.h" -#include "util.h" -#include "i18n.h" - -#define libutil_xmalloc(a) m_alloc( (a) ) -#define libutil_realloc(a,n) m_realloc( (a), (n) ) -#define libutil_strdup(a) m_strdup( (a) ) -#define libutil_free(a) m_free( (a) ) - -#define libutil_log_debug log_debug -#define libutil_log_info log_info -#define libutil_log_error log_error -#define libutil_log_fatal log_fatal -#define libutil_log_bug log_bug - - -#endif /*LIBUTIL_CONFIGH*/ diff --git a/util/logger.c b/util/logger.c index a1fb1f3d7..ae6984f48 100644 --- a/util/logger.c +++ b/util/logger.c @@ -69,9 +69,9 @@ log_stream() void log_set_name( const char *name ) { - m_free(pgm_name); + gcry_free(pgm_name); if( name ) - pgm_name = m_strdup(name); + pgm_name = gcry_xstrdup(name); else pgm_name = NULL; } diff --git a/util/logging.c b/util/logging.c deleted file mode 100644 index 76931f9be..000000000 --- a/util/logging.c +++ /dev/null @@ -1,170 +0,0 @@ -/* logging.c - useful logging functions - * Copyright (C) 1998, 1999 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 2 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, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA - */ - - -/* This file should replace logger.c in the future - for now it is not - * used GnuPG. - * It is a quite simple implemenation but sufficient for most purposes. - */ - -#include -#include -#include -#include -#include - -#include "libutil-config.h" -#include "logging.h" - -enum my_log_levels { - MY_LOG_CONT, - MY_LOG_INFO, - MY_LOG_WARN, - MY_LOG_ERROR, - MY_LOG_FATAL, - MY_LOG_BUG, - MY_LOG_DEBUG -}; - - -#if 0 -static void -write2stderr( const char *s ) -{ - write( 2, s, strlen(s) ); -} - - -static void -do_die(int rc, const char *text ) -{ - write2stderr("\nFatal error: "); - write2stderr(text); - write2stderr("\n"); - abort(); -} -#endif - - -static void -do_logv( int level, const char *fmt, va_list arg_ptr ) -{ - switch ( level ) { - case MY_LOG_CONT: break; - case MY_LOG_INFO: break; - case MY_LOG_WARN: break; - case MY_LOG_ERROR: break; - case MY_LOG_FATAL: fputs("Fatal: ",stderr ); break; - case MY_LOG_BUG: fputs("Ohhhh jeeee: ", stderr); break; - case MY_LOG_DEBUG: fputs("DBG: ", stderr ); break; - default: fprintf(stderr,"[Unknown log level %d]: ", level ); break; - } - vfprintf(stderr,fmt,arg_ptr) ; - - if( level == MY_LOG_FATAL ) - exit(2); - if( level == MY_LOG_BUG ) - abort(); -} - -static void -do_log( int level, const char *fmt, ... ) -{ - va_list arg_ptr ; - - va_start( arg_ptr, fmt ) ; - do_logv( level, fmt, arg_ptr ); - va_end(arg_ptr); -} - - - -void -log_info( const char *fmt, ... ) -{ - va_list arg_ptr ; - - va_start( arg_ptr, fmt ) ; - do_logv( MY_LOG_INFO, fmt, arg_ptr ); - va_end(arg_ptr); -} - -void -log_error( const char *fmt, ... ) -{ - va_list arg_ptr ; - - va_start( arg_ptr, fmt ) ; - do_logv( MY_LOG_ERROR, fmt, arg_ptr ); - va_end(arg_ptr); -} - - -void -log_fatal( const char *fmt, ... ) -{ - va_list arg_ptr ; - - va_start( arg_ptr, fmt ) ; - do_logv( MY_LOG_FATAL, fmt, arg_ptr ); - va_end(arg_ptr); - abort(); /* never called, bugs it makes the compiler happy */ -} - -void -log_bug( const char *fmt, ... ) -{ - va_list arg_ptr ; - - va_start( arg_ptr, fmt ) ; - do_logv( MY_LOG_BUG, fmt, arg_ptr ); - va_end(arg_ptr); - abort(); /* never called, bugs it makes the compiler happy */ -} - -void -log_debug( const char *fmt, ... ) -{ - va_list arg_ptr ; - - va_start( arg_ptr, fmt ) ; - do_logv( MY_LOG_DEBUG, fmt, arg_ptr ); - va_end(arg_ptr); -} - - -#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5 ) -void -bug_at( const char *file, int line, const char *func ) -{ - do_log( MY_LOG_BUG, - ("... this is a bug (%s:%d:%s)\n"), file, line, func ); - abort(); /* never called, but it makes the compiler happy */ -} -#else -void -bug_at( const char *file, int line ) -{ - do_log( MY_LOG_BUG, - _("you found a bug ... (%s:%d)\n"), file, line); - abort(); /* never called, but it makes the compiler happy */ -} -#endif - diff --git a/util/logging.h b/util/logging.h deleted file mode 100644 index 961751b52..000000000 --- a/util/logging.h +++ /dev/null @@ -1,41 +0,0 @@ -/* logging.h - * Copyright (C) 1999 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 2 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, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA - */ - -#ifndef LIBUTIL_LOGGING_H -#define LIBUTIL_LOGGING_H - -#include "mischelp.h" - -#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5 ) - void bug_at( const char *file, int line, const char *func ) LIBUTIL_GCC_A_NR; -# define BUG() bug_at( __FILE__ , __LINE__, __FUNCTION__ ) -#else - void bug_at( const char *file, int line ); -# define BUG() bug_at( __FILE__ , __LINE__ ) -#endif - -void log_bug( const char *fmt, ... ) LIBUTIL_GCC_A_NR_PRINTF(1,2); -void log_fatal( const char *fmt, ... ) LIBUTIL_GCC_A_NR_PRINTF(1,2); -void log_error( const char *fmt, ... ) LIBUTIL_GCC_A_PRINTF(1,2); -void log_info( const char *fmt, ... ) LIBUTIL_GCC_A_PRINTF(1,2); -void log_debug( const char *fmt, ... ) LIBUTIL_GCC_A_PRINTF(1,2); - - -#endif /*LIBUTIL_LOGGING_H*/ diff --git a/util/memory.c b/util/memory.c deleted file mode 100644 index fba760f04..000000000 --- a/util/memory.c +++ /dev/null @@ -1,651 +0,0 @@ -/* memory.c - memory allocation - * Copyright (C) 1998 Free Software Foundation, Inc. - * - * We use our own memory allocation functions instead of plain malloc(), - * so that we can provide some special enhancements: - * a) functions to provide memory from a secure memory. - * b) by looking at the requested allocation size we - * can reuse memory very quickly (e.g. MPI storage) - * (really needed?) - * c) memory usage reporting if compiled with M_DEBUG - * d) memory checking if compiled with M_GUARD - * - * 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 2 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, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA - */ - -#include -#include -#include -#include -#include - -#include "types.h" -#include "memory.h" -#include "util.h" - -/* FXIME: ugly hack. Need a prototype here but can't include g10lib.h */ -int g10_private_is_secure( const void *p ); - - -#define MAGIC_NOR_BYTE 0x55 -#define MAGIC_SEC_BYTE 0xcc -#define MAGIC_END_BYTE 0xaa - -#if SIZEOF_UNSIGNED_LONG == 8 - #define EXTRA_ALIGN 4 -#else - #define EXTRA_ALIGN 0 -#endif - -#if defined(M_DEBUG) || defined(M_GUARD) - static void membug( const char *fmt, ... ); -#endif - -#ifdef M_DEBUG - - #ifndef M_GUARD - #define M_GUARD 1 - #endif - #undef m_alloc - #undef m_alloc_clear - #undef m_alloc_secure - #undef m_alloc_secure_clear - #undef m_realloc - #undef m_free - #undef m_check - #undef m_strdup - #define FNAME(a) m_debug_ ##a - #define FNAMEPRT , const char *info - #define FNAMEARG , info - #define store_len(p,n,m) do { add_entry(p,n,m, \ - info, __FUNCTION__); } while(0) -#else - #define FNAME(a) m_ ##a - #define FNAMEPRT - #define FNAMEARG - #define store_len(p,n,m) do { ((byte*)p)[EXTRA_ALIGN+0] = n; \ - ((byte*)p)[EXTRA_ALIGN+1] = n >> 8 ; \ - ((byte*)p)[EXTRA_ALIGN+2] = n >> 16 ; \ - ((byte*)p)[EXTRA_ALIGN+3] = m? MAGIC_SEC_BYTE \ - : MAGIC_NOR_BYTE; \ - } while(0) -#endif - - -#ifdef M_GUARD -static long used_memory; -#endif - -#ifdef M_DEBUG /* stuff used for memory debuging */ - -struct info_entry { - struct info_entry *next; - unsigned count; /* call count */ - const char *info; /* the reference to the info string */ -}; - -struct memtbl_entry { - const void *user_p; /* for reference: the pointer given to the user */ - size_t user_n; /* length requested by the user */ - struct memtbl_entry *next; /* to build a list of unused entries */ - const struct info_entry *info; /* points into the table with */ - /* the info strings */ - unsigned inuse:1; /* this entry is in use */ - unsigned count:31; -}; - - -#define INFO_BUCKETS 53 -#define info_hash(p) ( *(u32*)((p)) % INFO_BUCKETS ) -static struct info_entry *info_strings[INFO_BUCKETS]; /* hash table */ - -static struct memtbl_entry *memtbl; /* the table with the memory info */ -static unsigned memtbl_size; /* number of allocated entries */ -static unsigned memtbl_len; /* number of used entries */ -static struct memtbl_entry *memtbl_unused;/* to keep track of unused entries */ - -static void dump_table_at_exit(void); -static void dump_table(void); -static void check_allmem( const char *info ); - -/**************** - * Put the new P into the debug table and return a pointer to the table entry. - * mode is true for security. BY is the name of the function which called us. - */ -static void -add_entry( byte *p, unsigned n, int mode, const char *info, const char *by ) -{ - unsigned index; - struct memtbl_entry *e; - struct info_entry *ie; - - if( memtbl_len < memtbl_size ) - index = memtbl_len++; - else { - struct memtbl_entry *e; - /* look for a used entry in the table. We take the first one, - * so that freed entries remain as long as possible in the table - * (free appends a new one) - */ - if( (e = memtbl_unused) ) { - index = e - memtbl; - memtbl_unused = e->next; - e->next = NULL; - } - else { /* no free entries in the table: extend the table */ - if( !memtbl_size ) { /* first time */ - memtbl_size = 100; - if( !(memtbl = calloc( memtbl_size, sizeof *memtbl )) ) - membug("memory debug table malloc failed\n"); - index = 0; - memtbl_len = 1; - atexit( dump_table_at_exit ); - } - else { /* realloc */ - unsigned n = memtbl_size / 4; /* enlarge by 25% */ - if(!(memtbl = realloc(memtbl, (memtbl_size+n)*sizeof *memtbl))) - membug("memory debug table realloc failed\n"); - memset(memtbl+memtbl_size, 0, n*sizeof *memtbl ); - memtbl_size += n; - index = memtbl_len++; - } - } - } - e = memtbl+index; - if( e->inuse ) - membug("Ooops: entry %u is flagged as in use\n", index); - e->user_p = p + 4; - e->user_n = n; - e->count++; - if( e->next ) - membug("Ooops: entry is in free entry list\n"); - /* do we already have this info string */ - for( ie = info_strings[info_hash(info)]; ie; ie = ie->next ) - if( ie->info == info ) - break; - if( !ie ) { /* no: make a new entry */ - if( !(ie = malloc( sizeof *ie )) ) - membug("can't allocate info entry\n"); - ie->next = info_strings[info_hash(info)]; - info_strings[info_hash(info)] = ie; - ie->info = info; - ie->count = 0; - } - ie->count++; - e->info = ie; - e->inuse = 1; - - /* put the index at the start of the memory */ - p[0] = index; - p[1] = index >> 8 ; - p[2] = index >> 16 ; - p[3] = mode? MAGIC_SEC_BYTE : MAGIC_NOR_BYTE ; - if( DBG_MEMORY ) - log_debug( "%s allocates %u bytes using %s\n", info, e->user_n, by ); -} - - - -/**************** - * Check that the memory block is correct. The magic byte has already been - * checked. Checks which are done here: - * - see whether the index points into our memory table - * - see whether P is the same as the one stored in the table - * - see whether we have already freed this block. - */ -struct memtbl_entry * -check_mem( const byte *p, const char *info ) -{ - unsigned n; - struct memtbl_entry *e; - - n = p[EXTRA_ALIGN+0]; - n |= p[EXTRA_ALIGN+1] << 8; - n |= p[EXTRA_ALIGN+2] << 16; - - if( n >= memtbl_len ) - membug("memory at %p corrupted: index=%u table_len=%u (%s)\n", - p+EXTRA_ALIGN+4, n, memtbl_len, info ); - e = memtbl+n; - - if( e->user_p != p+EXTRA_ALIGN+4 ) - membug("memory at %p corrupted: reference mismatch (%s)\n", - p+EXTRA_ALIGN+4, info ); - if( !e->inuse ) - membug("memory at %p corrupted: marked as free (%s)\n", - p+EXTRA_ALIGN+4, info ); - - if( !(p[EXTRA_ALIGN+3] == MAGIC_NOR_BYTE - || p[EXTRA_ALIGN+3] == MAGIC_SEC_BYTE) ) - membug("memory at %p corrupted: underflow=%02x (%s)\n", - p+EXTRA_ALIGN+4, p[EXTRA_ALIGN+3], info ); - if( p[EXTRA_ALIGN+4+e->user_n] != MAGIC_END_BYTE ) - membug("memory at %p corrupted: overflow=%02x (%s)\n", - p+EXTRA_ALIGN+4, p[EXTRA_ALIGN+4+e->user_n], info ); - return e; -} - - -/**************** - * free the entry and the memory (replaces free) - */ -static void -free_entry( byte *p, const char *info ) -{ - struct memtbl_entry *e, *e2; - - check_allmem("add_entry"); - - e = check_mem(p, info); - if( DBG_MEMORY ) - log_debug( "%s frees %u bytes alloced by %s\n", - info, e->user_n, e->info->info ); - if( !e->inuse ) { - if( e->user_p == p + EXTRA_ALIGN+ 4 ) - membug("freeing an already freed pointer at %p\n", p+EXTRA_ALIGN+4 ); - else - membug("freeing pointer %p which is flagged as freed\n", p+EXTRA_ALIGN+4 ); - } - - e->inuse = 0; - e->next = NULL; - if( !memtbl_unused ) - memtbl_unused = e; - else { - for(e2=memtbl_unused; e2->next; e2 = e2->next ) - ; - e2->next = e; - } - memset(p,'f', e->user_n+5); - free(p); -} - -static void -dump_entry(struct memtbl_entry *e ) -{ - unsigned n = e - memtbl; - - fprintf(stderr, "mem %4u%c %5u %p %5u %s (%u)\n", - n, e->inuse?'a':'u', e->count, e->user_p, e->user_n, - e->info->info, e->info->count ); - - -} - - -static void -dump_table_at_exit( void) -{ - if( DBG_MEMSTAT ) - dump_table(); -} - -static void -dump_table( void) -{ - unsigned n; - struct memtbl_entry *e; - ulong sum = 0, chunks =0; - - for( e = memtbl, n = 0; n < memtbl_len; n++, e++ ) { - if(e->inuse) { - dump_entry(e); - sum += e->user_n; - chunks++; - } - } - fprintf(stderr, " memory used: %8lu bytes in %ld chunks\n", - sum, chunks ); -} - - -static void -check_allmem( const char *info ) -{ - unsigned n; - struct memtbl_entry *e; - - for( e = memtbl, n = 0; n < memtbl_len; n++, e++ ) - if( e->inuse ) - check_mem(e->user_p-4-EXTRA_ALIGN, info); -} - -#endif /* M_DEBUG */ - -#if defined(M_DEBUG) || defined(M_GUARD) -static void -membug( const char *fmt, ... ) -{ - va_list arg_ptr ; - - fprintf(stderr, "\nMemory Error: " ) ; - va_start( arg_ptr, fmt ) ; - vfprintf(stderr,fmt,arg_ptr) ; - va_end(arg_ptr); - fflush(stderr); - #ifdef M_DEBUG - if( DBG_MEMSTAT ) - dump_table(); - #endif - abort(); -} -#endif - -void -m_print_stats( const char *prefix ) -{ - #ifdef M_DEBUG - unsigned n; - struct memtbl_entry *e; - ulong sum = 0, chunks =0; - - for( e = memtbl, n = 0; n < memtbl_len; n++, e++ ) { - if(e->inuse) { - sum += e->user_n; - chunks++; - } - } - - log_debug( "%s%smemstat: %8lu bytes in %ld chunks used\n", - prefix? prefix:"", prefix? ": ":"", sum, chunks ); - #elif defined(M_GUARD) - log_debug( "%s%smemstat: %8ld bytes\n", - prefix? prefix:"", prefix? ": ":"", used_memory ); - #endif -} - -void -m_dump_table( const char *prefix ) -{ - #if M_DEBUG - fprintf(stderr,"Memory-Table-Dump: %s\n", prefix); - dump_table(); - #endif - m_print_stats( prefix ); -} - - -static void -out_of_core(size_t n, int secure) -{ - log_fatal("out of %s memory while allocating %u bytes\n", - secure? "secure":"" ,(unsigned)n ); - -} - - - -/**************** - * Allocate memory of size n. - * Return NULL if we are out of memory. - */ -void * -g10_private_malloc( size_t n) -{ - char *p; - - #ifdef M_GUARD - if( !(p = malloc( n + EXTRA_ALIGN+5 )) ) - return NULL; - store_len(p,n,0); - used_memory += n; - p[4+EXTRA_ALIGN+n] = MAGIC_END_BYTE; - return p+EXTRA_ALIGN+4; - #else /* fixme: This can be done with a macro */ - return malloc( n ); - #endif -} - -/**************** - * Allocate memory of size n. - * This function gives up if we do not have enough memory - */ -void * -FNAME(alloc)( size_t n FNAMEPRT ) -{ - char *p = g10_private_malloc( n ); - if( !p ) - out_of_core(n,0); - return p; -} - - -/**************** - * Allocate memory of size n from the secure memory pool. - * Return NULL if we are out of memory. - */ -void * -g10_private_malloc_secure( size_t n) -{ - char *p; - - #ifdef M_GUARD - if( !(p = secmem_malloc( n +EXTRA_ALIGN+ 5 )) ) - return NULL; - store_len(p,n,1); - p[4+EXTRA_ALIGN+n] = MAGIC_END_BYTE; - return p+EXTRA_ALIGN+4; - #else - return secmem_malloc( n ); - #endif -} - -/**************** - * Allocate memory of size n from the secure memory pool. - * This function gives up if we do not have enough memory - */ -void * -FNAME(alloc_secure)( size_t n FNAMEPRT ) -{ - char *p = g10_private_malloc_secure( n ); - if( !p ) - out_of_core(n,1); - return p; -} - -void * -FNAME(alloc_clear)( size_t n FNAMEPRT ) -{ - void *p; - p = FNAME(alloc)( n FNAMEARG ); - memset(p, 0, n ); - return p; -} - -void * -FNAME(alloc_secure_clear)( size_t n FNAMEPRT) -{ - void *p; - p = FNAME(alloc_secure)( n FNAMEARG ); - memset(p, 0, n ); - return p; -} - -/**************** - * realloc and clear the old space - * Return NULL if there is not enoug memory. - */ -void * -g10_private_realloc( void *a, size_t n ) -{ - #ifdef M_GUARD - unsigned char *p = a; - void *b; - size_t len = m_size(a); - - if( len >= n ) /* we don't shrink for now */ - return a; - if( p[-1] == MAGIC_SEC_BYTE ) - b = g10_private_malloc_secure(n); - else - b = g10_private_malloc(n); - if( !b ) - return NULL; - memset(p, 0, n ); - FNAME(check)(NULL FNAMEARG); - memcpy(b, a, len ); - FNAME(free)(p FNAMEARG); - return b; - #else - if( g10_private_is_secure(a) ) { - return secmem_realloc( a, n ); - } - else { - return realloc( a, n ); - } - #endif -} - - -/**************** - * realloc and clear the old space - */ -void * -FNAME(realloc)( void *a, size_t n FNAMEPRT ) -{ - void *b = g10_private_realloc( a, n ); - if( !b ) - out_of_core(n, g10_private_is_secure(a)); - return b; -} - - -/**************** - * Free a pointer - */ -void -FNAME(free)( void *a FNAMEPRT ) -{ - byte *p = a; - - if( !p ) - return; - #ifdef M_DEBUG - free_entry(p-EXTRA_ALIGN-4, info); - #elif M_GUARD - m_check(p); - if( g10_private_is_secure(a) ) - secmem_free(p-EXTRA_ALIGN-4); - else { - used_memory -= m_size(a); - free(p-EXTRA_ALIGN-4); - } - #else - if( g10_private_is_secure(a) ) - secmem_free(p); - else - free(p); - #endif -} - -void -g10_private_free( void *a ) -{ - m_free(a); -} - -void -FNAME(check)( const void *a FNAMEPRT ) -{ - #ifdef M_GUARD - const byte *p = a; - - #ifdef M_DEBUG - if( p ) - check_mem(p-EXTRA_ALIGN-4, info); - else - check_allmem(info); - #else - if( !p ) - return; - if( !(p[-1] == MAGIC_NOR_BYTE || p[-1] == MAGIC_SEC_BYTE) ) - membug("memory at %p corrupted (underflow=%02x)\n", p, p[-1] ); - else if( p[m_size(p)] != MAGIC_END_BYTE ) - membug("memory at %p corrupted (overflow=%02x)\n", p, p[-1] ); - #endif - #endif -} - - -void -g10_private_check_heap( const void *p ) -{ - m_check(p); -} - - -size_t -m_size( const void *a ) -{ - #ifndef M_GUARD - log_debug("Ooops, m_size called\n"); - return 0; - #else - const byte *p = a; - size_t n; - - #ifdef M_DEBUG - n = check_mem(p-EXTRA_ALIGN-4, "m_size")->user_n; - #else - n = ((byte*)p)[-4]; - n |= ((byte*)p)[-3] << 8; - n |= ((byte*)p)[-2] << 16; - #endif - return n; - #endif -} - - -#if 0 /* not used */ -/**************** - * Make a copy of the memory block at a - */ -void * -FNAME(copy)( const void *a FNAMEPRT ) -{ - void *b; - size_t n; - - if( !a ) - return NULL; - - n = m_size(a); Aiiiih woher nehmen - if( g10_private_is_secure(a) ) - b = FNAME(alloc_secure)(n FNAMEARG); - else - b = FNAME(alloc)(n FNAMEARG); - memcpy(b, a, n ); - return b; -} -#endif - -char * -FNAME(strdup)( const char *a FNAMEPRT ) -{ - size_t n = strlen(a); - char *p = FNAME(alloc)(n+1 FNAMEARG); - strcpy(p, a); - return p; -} - -int -m_is_secure( const void *a ) -{ - return g10_private_is_secure(a); -} - - diff --git a/util/mischelp.h b/util/mischelp.h deleted file mode 100644 index 1c83da1be..000000000 --- a/util/mischelp.h +++ /dev/null @@ -1,42 +0,0 @@ -/* mischelp.h - * Copyright (C) 1999 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 2 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, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA - */ - -#ifndef LIBUTIL_MISCHELP_H -#define LIBUTIL_MISCHHELP_H - - -#define DIM(v) (sizeof(v)/sizeof((v)[0])) -#define DIMof(type,member) DIM(((type *)0)->member) - - -#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5 ) -# define LIBUTIL_GCC_A_NR __attribute__ ((noreturn)) -# define LIBUTIL_GCC_A_PRINTF( f, a ) __attribute__ ((format (printf,f,a))) -# define LIBUTIL_GCC_A_NR_PRINTF( f, a ) \ - __attribute__ ((noreturn, format (printf,f,a))) -#else -# define LIBUTIL_GCC_A_NR -# define LIBUTIL_GCC_A_PRINTF( f, a ) -# define LIBUTIL_GCC_A_NR_PRINTF( f, a ) -#endif - - - -#endif /*LIBUTIL_MISCHELP_H*/ diff --git a/util/miscutil.c b/util/miscutil.c index eb72415bb..c96608d98 100644 --- a/util/miscutil.c +++ b/util/miscutil.c @@ -210,7 +210,7 @@ print_utf8_string( FILE *fp, const byte *p, size_t n ) if( i < n ) { buf = utf8_to_native( p, n ); fputs( buf, fp ); - m_free( buf ); + gcry_free( buf ); } else print_string( fp, p, n, 0 ); @@ -218,7 +218,7 @@ print_utf8_string( FILE *fp, const byte *p, size_t n ) /**************** * This function returns a string which is suitable for printing - * Caller must release it with m_free() + * Caller must release it with gcry_free() */ char * make_printable_string( const byte *p, size_t n, int delim ) @@ -242,7 +242,7 @@ make_printable_string( const byte *p, size_t n, int delim ) p = save_p; n = save_n; /* and now make the string */ - d = buffer = m_alloc( buflen ); + d = buffer = gcry_xmalloc( buflen ); for( ; n; n--, p++ ) { if( iscntrl( *p ) || *p == delim ) { *d++ = '\\'; diff --git a/util/secmem.c b/util/secmem.c deleted file mode 100644 index 2eb2dfc9e..000000000 --- a/util/secmem.c +++ /dev/null @@ -1,422 +0,0 @@ -/* secmem.c - memory allocation from a secure heap - * Copyright (C) 1998,1999 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 2 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, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA - */ - -#include -#include -#include -#include -#include -#include -#include -#if defined(HAVE_MLOCK) || defined(HAVE_MMAP) - #include - #include - #include - #ifdef USE_CAPABILITIES - #include - #endif -#endif - -#include "types.h" -#include "memory.h" -#include "util.h" -#include "i18n.h" - -#if defined(MAP_ANON) && !defined(MAP_ANONYMOUS) - #define MAP_ANONYMOUS MAP_ANON -#endif - -#define DEFAULT_POOLSIZE 16384 - -typedef struct memblock_struct MEMBLOCK; -struct memblock_struct { - unsigned size; - union { - MEMBLOCK *next; - PROPERLY_ALIGNED_TYPE aligned; - } u; -}; - - - -static void *pool; -static volatile int pool_okay; /* may be checked in an atexit function */ -static int pool_is_mmapped; -static size_t poolsize; /* allocated length */ -static size_t poollen; /* used length */ -static MEMBLOCK *unused_blocks; -static unsigned max_alloced; -static unsigned cur_alloced; -static unsigned max_blocks; -static unsigned cur_blocks; -static int disable_secmem; -static int show_warning; -static int no_warning; -static int suspend_warning; - - -static void -print_warn(void) -{ - if( !no_warning ) - log_info(_("Warning: using insecure memory!\n")); -} - - -static void -lock_pool( void *p, size_t n ) -{ - #if defined(USE_CAPABILITIES) && defined(HAVE_MLOCK) - int err; - - cap_set_proc( cap_from_text("cap_ipc_lock+ep") ); - err = mlock( p, n ); - if( err && errno ) - err = errno; - cap_set_proc( cap_from_text("cap_ipc_lock+p") ); - - if( err ) { - if( errno != EPERM - #ifdef EAGAIN /* OpenBSD returns this */ - && errno != EAGAIN - #endif - #ifdef ENOSYS /* Some SCOs return this (function not implemented) */ - && errno != ENOSYS - #endif - ) - log_error("can't lock memory: %s\n", strerror(err)); - show_warning = 1; - } - - #elif defined(HAVE_MLOCK) - uid_t uid; - int err; - - uid = getuid(); - - #ifdef HAVE_BROKEN_MLOCK - if( uid ) { - errno = EPERM; - err = errno; - } - else { - err = mlock( p, n ); - if( err && errno ) - err = errno; - } - #else - err = mlock( p, n ); - if( err && errno ) - err = errno; - #endif - - if( uid && !geteuid() ) { - if( setuid( uid ) || getuid() != geteuid() ) - log_fatal("failed to reset uid: %s\n", strerror(errno)); - } - - if( err ) { - if( errno != EPERM - #ifdef EAGAIN /* OpenBSD returns this */ - && errno != EAGAIN - #endif - #ifdef ENOSYS /* Some SCOs return this (function not implemented) */ - && errno != ENOSYS - #endif - ) - log_error("can't lock memory: %s\n", strerror(err)); - show_warning = 1; - } - - #else - log_info("Please note that you don't have secure memory on this system\n"); - #endif -} - - -static void -init_pool( size_t n) -{ - size_t pgsize; - - poolsize = n; - - if( disable_secmem ) - log_bug("secure memory is disabled"); - - #ifdef HAVE_GETPAGESIZE - pgsize = getpagesize(); - #else - pgsize = 4096; - #endif - - #if HAVE_MMAP - poolsize = (poolsize + pgsize -1 ) & ~(pgsize-1); - #ifdef MAP_ANONYMOUS - pool = mmap( 0, poolsize, PROT_READ|PROT_WRITE, - MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); - #else /* map /dev/zero instead */ - { int fd; - - fd = open("/dev/zero", O_RDWR); - if( fd == -1 ) { - log_error("can't open /dev/zero: %s\n", strerror(errno) ); - pool = (void*)-1; - } - else { - pool = mmap( 0, poolsize, PROT_READ|PROT_WRITE, - MAP_PRIVATE, fd, 0); - } - } - #endif - if( pool == (void*)-1 ) - log_info("can't mmap pool of %u bytes: %s - using malloc\n", - (unsigned)poolsize, strerror(errno)); - else { - pool_is_mmapped = 1; - pool_okay = 1; - } - - #endif - if( !pool_okay ) { - pool = malloc( poolsize ); - if( !pool ) - log_fatal("can't allocate memory pool of %u bytes\n", - (unsigned)poolsize); - else - pool_okay = 1; - } - lock_pool( pool, poolsize ); - poollen = 0; -} - - -/* concatenate unused blocks */ -static void -compress_pool(void) -{ - /* fixme: we really should do this */ -} - -void -secmem_set_flags( unsigned flags ) -{ - int was_susp = suspend_warning; - - no_warning = flags & 1; - suspend_warning = flags & 2; - - /* and now issue the warning if it is not longer suspended */ - if( was_susp && !suspend_warning && show_warning ) { - show_warning = 0; - print_warn(); - } -} - -unsigned -secmem_get_flags(void) -{ - unsigned flags; - - flags = no_warning ? 1:0; - flags |= suspend_warning ? 2:0; - return flags; -} - -void -secmem_init( size_t n ) -{ - if( !n ) { - #ifdef USE_CAPABILITIES - /* drop all capabilities */ - cap_set_proc( cap_from_text("all-eip") ); - - #elif !defined(HAVE_DOSISH_SYSTEM) - uid_t uid; - - disable_secmem=1; - uid = getuid(); - if( uid != geteuid() ) { - if( setuid( uid ) || getuid() != geteuid() ) - log_fatal("failed to drop setuid\n" ); - } - #endif - } - else { - if( n < DEFAULT_POOLSIZE ) - n = DEFAULT_POOLSIZE; - if( !pool_okay ) - init_pool(n); - else - log_error("Oops, secure memory pool already initialized\n"); - } -} - - -void * -secmem_malloc( size_t size ) -{ - MEMBLOCK *mb, *mb2; - int compressed=0; - - if( !pool_okay ) { - log_info( - _("operation is not possible without initialized secure memory\n")); - log_info(_("(you may have used the wrong program for this task)\n")); - exit(2); - } - if( show_warning && !suspend_warning ) { - show_warning = 0; - print_warn(); - } - - /* blocks are always a multiple of 32 */ - size += sizeof(MEMBLOCK); - size = ((size + 31) / 32) * 32; - - retry: - /* try to get it from the used blocks */ - for(mb = unused_blocks,mb2=NULL; mb; mb2=mb, mb = mb->u.next ) - if( mb->size >= size ) { - if( mb2 ) - mb2->u.next = mb->u.next; - else - unused_blocks = mb->u.next; - goto leave; - } - /* allocate a new block */ - if( (poollen + size <= poolsize) ) { - mb = (void*)((char*)pool + poollen); - poollen += size; - mb->size = size; - } - else if( !compressed ) { - compressed=1; - compress_pool(); - goto retry; - } - else - return NULL; - - leave: - cur_alloced += mb->size; - cur_blocks++; - if( cur_alloced > max_alloced ) - max_alloced = cur_alloced; - if( cur_blocks > max_blocks ) - max_blocks = cur_blocks; - - return &mb->u.aligned.c; -} - - -void * -secmem_realloc( void *p, size_t newsize ) -{ - MEMBLOCK *mb; - size_t size; - void *a; - - mb = (MEMBLOCK*)((char*)p - ((size_t) &((MEMBLOCK*)0)->u.aligned.c)); - size = mb->size; - if( newsize < size ) - return p; /* it is easier not to shrink the memory */ - a = secmem_malloc( newsize ); - memcpy(a, p, size); - memset((char*)a+size, 0, newsize-size); - secmem_free(p); - return a; -} - - -void -secmem_free( void *a ) -{ - MEMBLOCK *mb; - size_t size; - - if( !a ) - return; - - mb = (MEMBLOCK*)((char*)a - ((size_t) &((MEMBLOCK*)0)->u.aligned.c)); - size = mb->size; - /* This does not make much sense: probably this memory is held in the - * cache. We do it anyway: */ - memset(mb, 0xff, size ); - memset(mb, 0xaa, size ); - memset(mb, 0x55, size ); - memset(mb, 0x00, size ); - mb->size = size; - mb->u.next = unused_blocks; - unused_blocks = mb; - cur_blocks--; - cur_alloced -= size; -} - - -int -g10_private_is_secure( const void *p ) -{ - return p >= pool && p < (void*)((char*)pool+poolsize); -} - - - -/**************** - * Warning: This code might be called by an interrupt handler - * and frankly, there should really be such a handler, - * to make sure that the memory is wiped out. - * We hope that the OS wipes out mlocked memory after - * receiving a SIGKILL - it really should do so, otherwise - * there is no chance to get the secure memory cleaned. - */ -void -secmem_term() -{ - if( !pool_okay ) - return; - - memset( pool, 0xff, poolsize); - memset( pool, 0xaa, poolsize); - memset( pool, 0x55, poolsize); - memset( pool, 0x00, poolsize); - #if HAVE_MMAP - if( pool_is_mmapped ) - munmap( pool, poolsize ); - #endif - pool = NULL; - pool_okay = 0; - poolsize=0; - poollen=0; - unused_blocks=NULL; -} - - -void -secmem_dump_stats() -{ - if( disable_secmem ) - return; - fprintf(stderr, - "secmem usage: %u/%u bytes in %u/%u blocks of pool %lu/%lu\n", - cur_alloced, max_alloced, cur_blocks, max_blocks, - (ulong)poollen, (ulong)poolsize ); -} - diff --git a/util/strgutil.c b/util/strgutil.c index a20ba2c75..274f2cda8 100644 --- a/util/strgutil.c +++ b/util/strgutil.c @@ -96,7 +96,7 @@ free_strlist( STRLIST sl ) for(; sl; sl = sl2 ) { sl2 = sl->next; - m_free(sl); + gcry_free(sl); } } @@ -106,7 +106,7 @@ add_to_strlist( STRLIST *list, const char *string ) { STRLIST sl; - sl = m_alloc( sizeof *sl + strlen(string)); + sl = gcry_xmalloc( sizeof *sl + strlen(string)); sl->flags = 0; strcpy(sl->d, string); sl->next = *list; @@ -128,7 +128,7 @@ add_to_strlist2( STRLIST *list, const char *string, int is_utf8 ) else { char *p = native_to_utf8( string ); sl = add_to_strlist( list, p ); - m_free( p ); + gcry_free( p ); } return sl; } @@ -138,7 +138,7 @@ append_to_strlist( STRLIST *list, const char *string ) { STRLIST r, sl; - sl = m_alloc( sizeof *sl + strlen(string)); + sl = gcry_xmalloc( sizeof *sl + strlen(string)); sl->flags = 0; strcpy(sl->d, string); sl->next = NULL; @@ -162,7 +162,7 @@ append_to_strlist2( STRLIST *list, const char *string, int is_utf8 ) else { char *p = native_to_utf8( string ); sl = append_to_strlist( list, p ); - m_free( p ); + gcry_free( p ); } return sl; } @@ -299,7 +299,7 @@ native_to_utf8( const char *string ) if( *s & 0x80 ) length += 2; /* we may need 3 bytes */ } - buffer = m_alloc( length + 1 ); + buffer = gcry_xmalloc( length + 1 ); for(p=buffer, s=string; *s; s++ ) { if( *s & 0x80 ) { ushort val = active_charset[ *s & 0x7f ]; @@ -324,7 +324,7 @@ native_to_utf8( const char *string ) if( *s & 0x80 ) length++; } - buffer = m_alloc( length + 1 ); + buffer = gcry_xmalloc( length + 1 ); for(p=buffer, s=string; *s; s++ ) { if( *s & 0x80 ) { *p++ = 0xc0 | ((*s >> 6) & 3); @@ -485,7 +485,7 @@ utf8_to_native( const char *string, size_t length ) } } if( !buffer ) { /* allocate the buffer after the first pass */ - buffer = p = m_alloc( n + 1 ); + buffer = p = gcry_xmalloc( n + 1 ); } else { *p = 0; /* make a string */ diff --git a/util/stringhelp.c b/util/stringhelp.c deleted file mode 100644 index 32318dad3..000000000 --- a/util/stringhelp.c +++ /dev/null @@ -1,219 +0,0 @@ -/* stringhelp.c - standard string helper functions - * Copyright (C) 1998, 1999 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 2 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, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA - */ - -#include -#include -#include -#include - -#include "libutil-config.h" -#include "stringhelp.h" - - -/**************** - * look for the substring SUB in buffer and return a pointer to that - * substring in BUF or NULL if not found. - * Comparison is case-insensitive. - */ -const char * -memistr( const char *buf, size_t buflen, const char *sub ) -{ - const byte *t, *s ; - size_t n; - - for( t=buf, n=buflen, s=sub ; n ; t++, n-- ) - if( toupper(*t) == toupper(*s) ) { - for( buf=t++, buflen = n--, s++; - n && toupper(*t) == toupper(*s); t++, s++, n-- ) - ; - if( !*s ) - return buf; - t = buf; n = buflen; s = sub ; - } - - return NULL ; -} - -/**************** - * Wie strncpy(), aber es werden maximal n-1 zeichen kopiert und ein - * '\0' angehängt. Ist n = 0, so geschieht nichts, ist Destination - * gleich NULL, so wird via libutil_malloc Speicher besorgt, ist dann nicht - * genügend Speicher vorhanden, so bricht die funktion ab. - */ -char * -mem2str( char *dest , const void *src , size_t n ) -{ - char *d; - const char *s; - - if( n ) { - if( !dest ) - dest = libutil_xmalloc( n ) ; - d = dest; - s = src ; - for(n--; n && *s; n-- ) - *d++ = *s++; - *d = '\0' ; - } - - return dest ; -} - - -/**************** - * remove leading and trailing white spaces - */ -char * -trim_spaces( char *str ) -{ - char *string, *p, *mark; - - string = str; - /* find first non space character */ - for( p=string; *p && isspace( *(byte*)p ) ; p++ ) - ; - /* move characters */ - for( (mark = NULL); (*string = *p); string++, p++ ) - if( isspace( *(byte*)p ) ) { - if( !mark ) - mark = string ; - } - else - mark = NULL ; - if( mark ) - *mark = '\0' ; /* remove trailing spaces */ - - return str ; -} - -/**************** - * remove trailing white spaces - */ -char * -trim_trailing_spaces( char *string ) -{ - char *p, *mark; - - for( mark = NULL, p = string; *p; p++ ) { - if( isspace( *(byte*)p ) ) { - if( !mark ) - mark = p; - } - else - mark = NULL; - } - if( mark ) - *mark = '\0' ; - - return string ; -} - - - -unsigned -trim_trailing_chars( byte *line, unsigned len, const char *trimchars ) -{ - byte *p, *mark; - unsigned n; - - for(mark=NULL, p=line, n=0; n < len; n++, p++ ) { - if( strchr(trimchars, *p ) ) { - if( !mark ) - mark = p; - } - else - mark = NULL; - } - - if( mark ) { - *mark = 0; - return mark - line; - } - return len; -} - -/**************** - * remove trailing white spaces and return the length of the buffer - */ -unsigned -trim_trailing_ws( byte *line, unsigned len ) -{ - return trim_trailing_chars( line, len, " \t\r\n" ); -} - - - -/********************************************* - ********** missing string functions ********* - *********************************************/ - -#ifndef HAVE_STPCPY -char * -stpcpy(char *a,const char *b) -{ - while( *b ) - *a++ = *b++; - *a = 0; - - return (char*)a; -} -#endif - -#ifndef HAVE_STRLWR -char * -strlwr(char *s) -{ - char *p; - for(p=s; *p; p++ ) - *p = tolower(*p); - return s; -} -#endif - - -#ifndef HAVE_STRCASECMP -int -strcasecmp( const char *a, const char *b ) -{ - for( ; *a && *b; a++, b++ ) { - if( *a != *b && toupper(*a) != toupper(*b) ) - break; - } - return *(const byte*)a - *(const byte*)b; -} -#endif - - -/**************** - * mingw32/cpd has a memicmp() - */ -#ifndef HAVE_MEMICMP -int -memicmp( const char *a, const char *b, size_t n ) -{ - for( ; n; n--, a++, b++ ) - if( *a != *b && toupper(*(const byte*)a) != toupper(*(const byte*)b) ) - return *(const byte *)a - *(const byte*)b; - return 0; -} -#endif - - - diff --git a/util/stringhelp.h b/util/stringhelp.h deleted file mode 100644 index c51cb552f..000000000 --- a/util/stringhelp.h +++ /dev/null @@ -1,60 +0,0 @@ -/* stringhelp.h - * Copyright (C) 1998,1999 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 2 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, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA - */ - -#ifndef LIBUTIL_STRINGHELP_H -#define LIBUTIL_STRINGHELP_H - - -const char *memistr( const char *buf, size_t buflen, const char *sub ); -char *mem2str( char *, const void *, size_t); -char *trim_spaces( char *string ); -char *trim_trailing_spaces( char *string ); -unsigned int trim_trailing_chars( unsigned char *line, unsigned len, - const char *trimchars); -unsigned int trim_trailing_ws( unsigned char *line, unsigned len ); - - -#ifndef HAVE_MEMICMP -int memicmp( const char *a, const char *b, size_t n ); -#endif -#ifndef HAVE_STPCPY -char *stpcpy(char *a,const char *b); -#endif -#ifndef HAVE_STRLWR -char *strlwr(char *a); -#endif -#ifndef HAVE_STRTOUL - #define strtoul(a,b,c) ((unsigned long)strtol((a),(b),(c))) -#endif -#ifndef HAVE_MEMMOVE - #define memmove(d, s, n) bcopy((s), (d), (n)) -#endif -#ifndef HAVE_STRICMP - #define stricmp(a,b) strcasecmp( (a), (b) ) -#endif - - -#ifndef STR - #define STR(v) #v -#endif -#define STR2(v) STR(v) - - -#endif /*LIBUTIL_STRINGHELP_H*/ diff --git a/util/ttyio.c b/util/ttyio.c index 35e0d318f..8dacb555e 100644 --- a/util/ttyio.c +++ b/util/ttyio.c @@ -161,19 +161,19 @@ tty_printf( const char *fmt, ... ) * it, so we use a static buffer for now */ do { if( n == -1 || !buf ) { - m_free(buf); + gcry_free(buf); bufsize += 200; /* better check the new size; (we use M$ functions) */ if( bufsize > 50000 ) log_bug("vsnprintf probably failed\n"); - buf = m_alloc( bufsize ); + buf = gcry_xmalloc( bufsize ); } n = _vsnprintf(buf, bufsize-1, fmt, arg_ptr); } while( n == -1 ); #else if( !buf ) { bufsize += 1000; - buf = m_alloc( bufsize ); + buf = gcry_xmalloc( bufsize ); } n = vsprintf(buf, fmt, arg_ptr); if( n == -1 ) @@ -252,7 +252,7 @@ tty_print_utf8_string( byte *p, size_t n ) if( i < n ) { buf = utf8_to_native( p, n ); tty_printf("%s", buf ); - m_free( buf ); + gcry_free( buf ); } else tty_print_string( p, n ); @@ -284,7 +284,7 @@ do_get( const char *prompt, int hidden ) last_prompt_len = 0; tty_printf( prompt ); - buf = m_alloc(n=50); + buf = gcry_xmalloc(n=50); i = 0; #ifdef __MINGW32__ /* windoze version */ @@ -313,7 +313,7 @@ do_get( const char *prompt, int hidden ) continue; if( !(i < n-1) ) { n += 50; - buf = m_realloc( buf, n ); + buf = gcry_xrealloc( buf, n ); } buf[i++] = c; } @@ -353,7 +353,7 @@ do_get( const char *prompt, int hidden ) continue; if( !(i < n-1) ) { n += 50; - buf = m_realloc( buf, n ); + buf = gcry_xrealloc( buf, n ); } buf[i++] = c; } @@ -425,7 +425,7 @@ tty_get_answer_is_yes( const char *prompt ) char *p = tty_get( prompt ); tty_kill_prompt(); yes = answer_is_yes(p); - m_free(p); + gcry_free(p); return yes; } diff --git a/util/xmalloc.c b/util/xmalloc.c deleted file mode 100644 index 21e23d0dc..000000000 --- a/util/xmalloc.c +++ /dev/null @@ -1,70 +0,0 @@ -/* xmalloc.c - standard malloc wrappers - * Copyright (C) 1999 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 2 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, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA - */ - -#include -#include -#include - -#include "libutil-config.h" -#include "xmalloc.h" - -static void -out_of_core(void) -{ - fputs("\nfatal: out of memory\n", stderr ); - exit(2); -} - - -void * -xmalloc( size_t n ) -{ - void *p = malloc( n ); - if( !p ) - out_of_core(); - return p; -} - -void * -xrealloc( void *a, size_t n ) -{ - void *p = realloc( a, n ); - if( !p ) - out_of_core(); - return p; -} - -void * -xcalloc( size_t n, size_t m ) -{ - void *p = calloc( n, m ); - if( !p ) - out_of_core(); - return p; -} - -char * -xstrdup( const char *string ) -{ - void *p = xmalloc( strlen(string)+1 ); - strcpy( p, string ); - return p; -} - diff --git a/util/xmalloc.h b/util/xmalloc.h deleted file mode 100644 index 4a33d36a5..000000000 --- a/util/xmalloc.h +++ /dev/null @@ -1,30 +0,0 @@ -/* xmalloc.h - * Copyright (C) 1999 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 2 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, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA - */ - -#ifndef LIBUTIL_XMALLOC_H -#define LIBUTIL_XMALLOC_H - -void *xmalloc( size_t n ); -void *xrealloc( void *a, size_t n ); -void *xcalloc( size_t n, size_t m ); -char *xstrdup( const char *string ); - - -#endif /*LIBUTIL_XMALLOC_H*/