xterm.c was absurd, no need to put trivial functions like this in a
[apps/madmutt.git] / crypt-gpgme.c
index ef199cb..ea750ec 100644 (file)
 
 #ifdef CRYPT_BACKEND_GPGME
 
+#include <lib-lib/mem.h>
+#include <lib-lib/str.h>
+#include <lib-lib/ascii.h>
+#include <lib-lib/macros.h>
+
 #include "mutt.h"
 #include "mutt_crypt.h"
 #include "mutt_menu.h"
 #include "mutt_curses.h"
-#include "ascii.h"
 #include "handler.h"
 #include "enter.h"
 #include "mime.h"
@@ -31,9 +35,6 @@
 #include "recvattach.h"
 #include "sort.h"
 
-#include "lib/mem.h"
-#include "lib/intl.h"
-#include "lib/str.h"
 #include "lib/debug.h"
 
 #include <sys/wait.h>
@@ -132,12 +133,10 @@ static void print_utf8 (FILE * fp, const char *buf, size_t len)
 {
   char *tstr;
 
-  tstr = mem_malloc (len + 1);
-  memcpy (tstr, buf, len);
-  tstr[len] = 0;
+  tstr = p_dupstr(buf, len);
   mutt_convert_string (&tstr, "utf-8", Charset, M_ICONV_HOOK_FROM);
   fputs (tstr, fp);
-  mem_free (&tstr);
+  p_delete(&tstr);
 }
 
 
@@ -153,7 +152,7 @@ static const char *crypt_keyid (crypt_key_t * k)
 
   if (k->kobj && k->kobj->subkeys) {
     s = k->kobj->subkeys->keyid;
-    if ((!option (OPTPGPLONGIDS)) && (str_len (s) == 16))
+    if ((!option (OPTPGPLONGIDS)) && (m_strlen(s) == 16))
       /* Return only the short keyID.  */
       s += 8;
   }
@@ -212,11 +211,11 @@ static char crypt_flags (int flags)
 }
 
 /* Return a copy of KEY. */
-static crypt_key_t *crypt_copy_key (crypt_key_t * key)
+static crypt_key_t *crypt_copy_key (crypt_key_t *key)
 {
   crypt_key_t *k;
 
-  k = mem_calloc (1, sizeof *k);
+  k = p_new(crypt_key_t, 1);
   k->kobj = key->kobj;
   gpgme_key_ref (key->kobj);
   k->idx = key->idx;
@@ -233,7 +232,7 @@ static void crypt_free_key (crypt_key_t ** keylist)
   while (*keylist) {
     crypt_key_t *k = (*keylist)->next;
 
-    mem_free (&k);
+    p_delete(&k);
     *keylist = k;
   }
 }
@@ -509,7 +508,7 @@ static char *data_object_to_tempfile (gpgme_data_t data, FILE ** ret_fp)
   }
   if (ret_fp)
     *ret_fp = fp;
-  return str_dup (tempfile);
+  return m_strdup(tempfile);
 }
 
 
@@ -554,13 +553,13 @@ static gpgme_key_t *create_recipient_set (const char *keylist,
           err = gpgme_get_key (context, buf, &key, 0);
 
         if (!err) {
-          mem_realloc (&rset, sizeof (*rset) * (rset_n + 1));
+          p_realloc(&rset, rset_n + 1);
           rset[rset_n++] = key;
         }
         else {
           mutt_error (_("error adding recipient `%s': %s\n"),
                       buf, gpgme_strerror (err));
-          mem_free (&rset);
+          p_delete(&rset);
           return NULL;
         }
       }
@@ -568,7 +567,7 @@ static gpgme_key_t *create_recipient_set (const char *keylist,
   }
 
   /* NULL terminate.  */
-  mem_realloc (&rset, sizeof (*rset) * (rset_n + 1));
+  p_realloc(&rset, rset_n + 1);
   rset[rset_n++] = NULL;
 
   if (context)
@@ -761,7 +760,7 @@ static BODY *sign_message (BODY * a, int use_smime)
 
   t = mutt_new_body ();
   t->type = TYPEMULTIPART;
-  t->subtype = str_dup ("signed");
+  t->subtype = m_strdup("signed");
   t->encoding = ENC7BIT;
   t->use_disp = 0;
   t->disposition = DISPINLINE;
@@ -785,15 +784,15 @@ static BODY *sign_message (BODY * a, int use_smime)
   t = t->parts->next;
   t->type = TYPEAPPLICATION;
   if (use_smime) {
-    t->subtype = str_dup ("pkcs7-signature");
+    t->subtype = m_strdup("pkcs7-signature");
     mutt_set_parameter ("name", "smime.p7s", &t->parameter);
     t->encoding = ENCBASE64;
     t->use_disp = 1;
     t->disposition = DISPATTACH;
-    t->d_filename = str_dup ("smime.p7s");
+    t->d_filename = m_strdup("smime.p7s");
   }
   else {
-    t->subtype = str_dup ("pgp-signature");
+    t->subtype = m_strdup("pgp-signature");
     t->use_disp = 0;
     t->disposition = DISPINLINE;
     t->encoding = ENC7BIT;
@@ -836,19 +835,19 @@ BODY *pgp_gpgme_encrypt_message (BODY * a, char *keylist, int sign)
     convert_to_7bit (a);
   plaintext = body_to_data_object (a, 0);
   if (!plaintext) {
-    mem_free (&rset);
+    p_delete(&rset);
     return NULL;
   }
 
   outfile = encrypt_gpgme_object (plaintext, rset, 0, sign);
   gpgme_data_release (plaintext);
-  mem_free (&rset);
+  p_delete(&rset);
   if (!outfile)
     return NULL;
 
   t = mutt_new_body ();
   t->type = TYPEMULTIPART;
-  t->subtype = str_dup ("encrypted");
+  t->subtype = m_strdup("encrypted");
   t->encoding = ENC7BIT;
   t->use_disp = 0;
   t->disposition = DISPINLINE;
@@ -858,18 +857,18 @@ BODY *pgp_gpgme_encrypt_message (BODY * a, char *keylist, int sign)
 
   t->parts = mutt_new_body ();
   t->parts->type = TYPEAPPLICATION;
-  t->parts->subtype = str_dup ("pgp-encrypted");
+  t->parts->subtype = m_strdup("pgp-encrypted");
   t->parts->encoding = ENC7BIT;
 
   t->parts->next = mutt_new_body ();
   t->parts->next->type = TYPEAPPLICATION;
-  t->parts->next->subtype = str_dup ("octet-stream");
+  t->parts->next->subtype = m_strdup("octet-stream");
   t->parts->next->encoding = ENC7BIT;
   t->parts->next->filename = outfile;
   t->parts->next->use_disp = 1;
   t->parts->next->disposition = DISPINLINE;
   t->parts->next->unlink = 1;   /* delete after sending the message */
-  t->parts->next->d_filename = str_dup ("msg.asc"); /* non pgp/mime
+  t->parts->next->d_filename = m_strdup("msg.asc"); /* non pgp/mime
                                                            can save */
 
   return t;
@@ -894,25 +893,25 @@ BODY *smime_gpgme_build_smime_entity (BODY * a, char *keylist)
 
   plaintext = body_to_data_object (a, 0);
   if (!plaintext) {
-    mem_free (&rset);
+    p_delete(&rset);
     return NULL;
   }
 
   outfile = encrypt_gpgme_object (plaintext, rset, 1, 0);
   gpgme_data_release (plaintext);
-  mem_free (&rset);
+  p_delete(&rset);
   if (!outfile)
     return NULL;
 
   t = mutt_new_body ();
   t->type = TYPEAPPLICATION;
-  t->subtype = str_dup ("pkcs7-mime");
+  t->subtype = m_strdup("pkcs7-mime");
   mutt_set_parameter ("name", "smime.p7m", &t->parameter);
   mutt_set_parameter ("smime-type", "enveloped-data", &t->parameter);
   t->encoding = ENCBASE64;      /* The output of OpenSSL SHOULD be binary */
   t->use_disp = 1;
   t->disposition = DISPATTACH;
-  t->d_filename = str_dup ("smime.p7m");
+  t->d_filename = m_strdup("smime.p7m");
   t->filename = outfile;
   t->unlink = 1;                /*delete after sending the message */
   t->parts = 0;
@@ -1034,10 +1033,10 @@ static void show_fingerprint (gpgme_key_t key, STATE * state)
     return;
   is_pgp = (key->protocol == GPGME_PROTOCOL_OpenPGP);
 
-  buf = mem_malloc (str_len (prefix) + str_len (s) * 4 + 2);
+  buf = xmalloc(m_strlen(prefix) + m_strlen(s) * 4 + 2);
   strcpy (buf, prefix);         /* __STRCPY_CHECKED__ */
-  p = buf + str_len (buf);
-  if (is_pgp && str_len (s) == 40) {     /* PGP v4 style formatted. */
+  p = buf + m_strlen(buf);
+  if (is_pgp && m_strlen(s) == 40) {     /* PGP v4 style formatted. */
     for (i = 0; *s && s[1] && s[2] && s[3] && s[4]; s += 4, i++) {
       *p++ = s[0];
       *p++ = s[1];
@@ -1064,7 +1063,7 @@ static void show_fingerprint (gpgme_key_t key, STATE * state)
   *p++ = '\n';
   *p = 0;
   state_attach_puts (buf, state);
-  mem_free (&buf);
+  p_delete(&buf);
 }
 
 /* Show the valididy of a key used for one signature. */
@@ -1302,7 +1301,7 @@ static int verify_one (BODY * sigbdy, STATE * s,
             if (notation->value) {
               state_attach_puts (notation->value, s);
               if (!(*notation->value
-                    && (notation->value[str_len (notation->value) - 1] ==
+                    && (notation->value[m_strlen(notation->value) - 1] ==
                         '\n')))
                 state_attach_puts ("\n", s);
             }
@@ -1729,7 +1728,7 @@ static void copy_clearsigned (gpgme_data_t data, STATE * s, char *charset)
   if (!fname)
     return;
   unlink (fname);
-  mem_free (&fname);
+  p_delete(&fname);
 
   fc = fgetconv_open (fp, charset, Charset, M_ICONV_HOOK_FROM);
 
@@ -1800,7 +1799,7 @@ int pgp_gpgme_application_handler (BODY * m, STATE * s)
       break;
 
     offset = ftello (s->fpin);
-    bytes -= (offset - last_pos);       /* don't rely on str_len(buf) */
+    bytes -= (offset - last_pos);       /* don't rely on m_strlen(buf) */
     last_pos = offset;
 
     if (!str_ncmp ("-----BEGIN PGP ", buf, 15)) {
@@ -1830,13 +1829,13 @@ int pgp_gpgme_application_handler (BODY * m, STATE * s)
 
       /* Copy PGP material to an data container */
       armored_data = create_gpgme_data ();
-      gpgme_data_write (armored_data, buf, str_len (buf));
+      gpgme_data_write (armored_data, buf, m_strlen(buf));
       while (bytes > 0 && fgets (buf, sizeof (buf) - 1, s->fpin) != NULL) {
         offset = ftello (s->fpin);
-        bytes -= (offset - last_pos);   /* don't rely on str_len(buf) */
+        bytes -= (offset - last_pos);   /* don't rely on m_strlen(buf) */
         last_pos = offset;
 
-        gpgme_data_write (armored_data, buf, str_len (buf));
+        gpgme_data_write (armored_data, buf, m_strlen(buf));
 
         if ((needpass && !str_cmp ("-----END PGP MESSAGE-----\n", buf))
             || (!needpass
@@ -1922,7 +1921,7 @@ int pgp_gpgme_application_handler (BODY * m, STATE * s)
           }
           else {
             unlink (tmpfname);
-            mem_free (&tmpfname);
+            p_delete(&tmpfname);
           }
         }
         gpgme_release (ctx);
@@ -2486,7 +2485,7 @@ static int print_dn_part (FILE * fp, struct dn_array_s *dn, const char *key)
     if (!str_cmp (dn->key, key)) {
       if (any)
         fputs (" + ", fp);
-      print_utf8 (fp, dn->value, str_len (dn->value));
+      print_utf8 (fp, dn->value, m_strlen(dn->value));
       any = 1;
     }
   }
@@ -2541,10 +2540,8 @@ static const unsigned char *parse_dn_part (struct dn_array_s *array,
   n = s - string;
   if (!n)
     return NULL;                /* empty key */
-  array->key = mem_malloc (n + 1);
+  array->key = p_dupstr(string, n );
   p = (unsigned char *) array->key;
-  memcpy (p, string, n);        /* fixme: trim trailing spaces */
-  p[n] = 0;
   string = s + 1;
 
   if (*string == '#') {         /* hexstring */
@@ -2555,7 +2552,7 @@ static const unsigned char *parse_dn_part (struct dn_array_s *array,
     if (!n || (n & 1))
       return NULL;              /* empty or odd number of digits */
     n /= 2;
-    p = mem_malloc (n + 1);
+    p = xmalloc(n + 1);
     array->value = (char *) p;
     for (s1 = string; n; s1 += 2, n--)
       *p++ = xtoi_2 (s1);
@@ -2585,7 +2582,7 @@ static const unsigned char *parse_dn_part (struct dn_array_s *array,
         n++;
     }
 
-    p = mem_malloc (n + 1);
+    p = xmalloc(n + 1);
     array->value = (char *) p;
     for (s = string; n; s++, n--) {
       if (*s == '\\') {
@@ -2616,7 +2613,7 @@ static struct dn_array_s *parse_dn (const unsigned char *string)
   int i;
 
   arraysize = 7;                /* C,ST,L,O,OU,CN,email */
-  array = mem_malloc ((arraysize + 1) * sizeof *array);
+  array = p_new(struct dn_array_s, arraysize + 1);
   arrayidx = 0;
   while (*string) {
     while (*string == ' ')
@@ -2627,12 +2624,12 @@ static struct dn_array_s *parse_dn (const unsigned char *string)
       struct dn_array_s *a2;
 
       arraysize += 5;
-      a2 = mem_malloc ((arraysize + 1) * sizeof *array);
+      a2 = p_new(struct dn_array_s, arraysize + 1);
       for (i = 0; i < arrayidx; i++) {
         a2[i].key = array[i].key;
         a2[i].value = array[i].value;
       }
-      mem_free (&array);
+      p_delete(&array);
       array = a2;
     }
     array[arrayidx].key = NULL;
@@ -2654,10 +2651,10 @@ static struct dn_array_s *parse_dn (const unsigned char *string)
 
 failure:
   for (i = 0; i < arrayidx; i++) {
-    mem_free (&array[i].key);
-    mem_free (&array[i].value);
+    p_delete(&array[i].key);
+    p_delete(&array[i].value);
   }
-  mem_free (&array);
+  p_delete(&array);
   return NULL;
 }
 
@@ -2688,10 +2685,10 @@ static void parse_and_print_user_id (FILE * fp, const char *userid)
     else {
       print_dn_parts (fp, dn);
       for (i = 0; dn[i].key; i++) {
-        mem_free (&dn[i].key);
-        mem_free (&dn[i].value);
+        p_delete(&dn[i].key);
+        p_delete(&dn[i].value);
       }
-      mem_free (&dn);
+      p_delete(&dn);
     }
   }
 }
@@ -2763,7 +2760,7 @@ static void print_key_info (gpgme_key_t key, FILE * fp)
       putc (' ', fp);
     }
     if (is_pgp)
-      print_utf8 (fp, s, str_len (s));
+      print_utf8 (fp, s, m_strlen(s));
     else
       parse_and_print_user_id (fp, s);
     putc ('\n', fp);
@@ -2825,7 +2822,7 @@ static void print_key_info (gpgme_key_t key, FILE * fp)
   if (key->subkeys) {
     s = key->subkeys->fpr;
     fputs (_("Fingerprint: "), fp);
-    if (is_pgp && str_len (s) == 40) {
+    if (is_pgp && m_strlen(s) == 40) {
       for (i = 0; *s && s[1] && s[2] && s[3] && s[4]; s += 4, i++) {
         putc (*s, fp);
         putc (s[1], fp);
@@ -2871,7 +2868,7 @@ static void print_key_info (gpgme_key_t key, FILE * fp)
       s = subkey->keyid;
 
       putc ('\n', fp);
-      if (str_len (s) == 16)
+      if (m_strlen(s) == 16)
         s += 8;                 /* display only the short keyID */
       fprintf (fp, _("Subkey ....: 0x%s"), s);
       if (subkey->revoked) {
@@ -3039,7 +3036,7 @@ static char *list_to_pattern (LIST * list)
     n++;                        /* delimiter or end of string */
   }
   n++;                          /* make sure to allocate at least one byte */
-  pattern = p = mem_calloc (1, n);
+  pattern = p = p_new(char, n);
   for (l = list; l; l = l->next) {
     s = l->data;
     if (*s) {
@@ -3087,7 +3084,7 @@ static crypt_key_t *get_candidates (LIST * hints, unsigned int app,
   err = gpgme_new (&ctx);
   if (err) {
     mutt_error (_("gpgme_new failed: %s"), gpgme_strerror (err));
-    mem_free (&pattern);
+    p_delete(&pattern);
     return NULL;
   }
 
@@ -3110,20 +3107,20 @@ static crypt_key_t *get_candidates (LIST * hints, unsigned int app,
     if (!n)
       goto no_pgphints;
 
-    patarr = mem_calloc (n + 1, sizeof *patarr);
+    patarr = p_new(char *, n + 1);
     for (l = hints, n = 0; l; l = l->next) {
       if (l->data && *l->data)
-        patarr[n++] = str_dup (l->data);
+        patarr[n++] = m_strdup(l->data);
     }
     patarr[n] = NULL;
     err = gpgme_op_keylist_ext_start (ctx, (const char **) patarr, secret, 0);
     for (n = 0; patarr[n]; n++)
-      mem_free (&patarr[n]);
-    mem_free (&patarr);
+      p_delete(&patarr[n]);
+    p_delete(&patarr);
     if (err) {
       mutt_error (_("gpgme_op_keylist_start failed: %s"), gpgme_strerror (err));
       gpgme_release (ctx);
-      mem_free (&pattern);
+      p_delete(&pattern);
       return NULL;
     }
 
@@ -3156,7 +3153,7 @@ static crypt_key_t *get_candidates (LIST * hints, unsigned int app,
 #endif /* DISABLED code */
 
       for (idx = 0, uid = key->uids; uid; idx++, uid = uid->next) {
-        k = mem_calloc (1, sizeof *k);
+        k = p_new(crypt_key_t, 1);
         k->kobj = key;
         k->idx = idx;
         k->uid = uid->uid;
@@ -3179,7 +3176,7 @@ static crypt_key_t *get_candidates (LIST * hints, unsigned int app,
     if (err) {
       mutt_error (_("gpgme_op_keylist_start failed: %s"), gpgme_strerror (err));
       gpgme_release (ctx);
-      mem_free (&pattern);
+      p_delete(&pattern);
       return NULL;
     }
 
@@ -3192,7 +3189,7 @@ static crypt_key_t *get_candidates (LIST * hints, unsigned int app,
         flags |= KEYFLAG_CANSIGN;
 
       for (idx = 0, uid = key->uids; uid; idx++, uid = uid->next) {
-        k = mem_calloc (1, sizeof *k);
+        k = p_new(crypt_key_t, 1);
         k->kobj = key;
         k->idx = idx;
         k->uid = uid->uid;
@@ -3207,7 +3204,7 @@ static crypt_key_t *get_candidates (LIST * hints, unsigned int app,
   }
 
   gpgme_release (ctx);
-  mem_free (&pattern);
+  p_delete(&pattern);
   return db;
 }
 
@@ -3218,16 +3215,16 @@ static LIST *crypt_add_string_to_hints (LIST * hints, const char *str)
   char *scratch;
   char *t;
 
-  if ((scratch = str_dup (str)) == NULL)
+  if ((scratch = m_strdup(str)) == NULL)
     return hints;
 
   for (t = strtok (scratch, " ,.:\"()<>\n"); t;
        t = strtok (NULL, " ,.:\"()<>\n")) {
-    if (str_len (t) > 3)
+    if (m_strlen(t) > 3)
       hints = mutt_add_list (hints, t);
   }
 
-  mem_free (&scratch);
+  p_delete(&scratch);
   return hints;
 }
 
@@ -3261,7 +3258,7 @@ static crypt_key_t *crypt_select_key (crypt_key_t * keys,
 
     if (i == keymax) {
       keymax += 20;
-      mem_realloc (&key_table, sizeof (crypt_key_t *) * keymax);
+      p_realloc(&key_table, keymax);
     }
 
     key_table[i++] = k;
@@ -3419,7 +3416,7 @@ static crypt_key_t *crypt_select_key (crypt_key_t * keys,
   }
 
   mutt_menuDestroy (&menu);
-  mem_free (&key_table);
+  p_delete(&key_table);
 
   set_option (OPTNEEDREDRAW);
 
@@ -3640,11 +3637,11 @@ static crypt_key_t *crypt_ask_for_key (char *tag,
       if (l)
         str_replace (&l->dflt, resp);
       else {
-        l = mem_malloc (sizeof (struct crypt_cache));
+        l = p_new(struct crypt_cache, 1);
         l->next = id_defaults;
         id_defaults = l;
-        l->what = str_dup (whatfor);
-        l->dflt = str_dup (resp);
+        l->what = m_strdup(whatfor);
+        l->dflt = m_strdup(resp);
       }
     }
 
@@ -3731,7 +3728,7 @@ static char *find_keys (ADDRESS * to, ADDRESS * cc, ADDRESS * bcc,
         }
       }
       else if (r == -1) {
-        mem_free (&keylist);
+        p_delete(&keylist);
         rfc822_free_address (&tmp);
         rfc822_free_address (&addr);
         return NULL;
@@ -3750,7 +3747,7 @@ static char *find_keys (ADDRESS * to, ADDRESS * cc, ADDRESS * bcc,
                                     app,
 #endif
                                     &forced_valid)) == NULL) {
-        mem_free (&keylist);
+        p_delete(&keylist);
         rfc822_free_address (&tmp);
         rfc822_free_address (&addr);
         return NULL;
@@ -3769,12 +3766,12 @@ static char *find_keys (ADDRESS * to, ADDRESS * cc, ADDRESS * bcc,
         *r_application &= ~APPLICATION_SMIME;
 #endif
 
-      keylist_size += str_len (s) + 4 + 1;
-      mem_realloc (&keylist, keylist_size);
+      keylist_size += m_strlen(s) + 4 + 1;
+      p_realloc(&keylist, keylist_size);
       sprintf (keylist + keylist_used, "%s0x%s%s",      /* __SPRINTF_CHECKED__ */
                keylist_used ? " " : "", s, forced_valid ? "!" : "");
     }
-    keylist_used = str_len (keylist);
+    keylist_used = m_strlen(keylist);
 
     crypt_free_key (&key);
     rfc822_free_address (&addr);
@@ -3928,9 +3925,9 @@ static int verify_sender (HEADER * h, gpgme_protocol_t protocol)
       int sender_length = 0;
       int uid_length = 0;
 
-      sender_length = str_len (sender->mailbox);
+      sender_length = m_strlen(sender->mailbox);
       for (uid = key->uids; uid && ret; uid = uid->next) {
-        uid_length = str_len (uid->email);
+        uid_length = m_strlen(uid->email);
         if (1 && (uid->email[0] == '<')
             && (uid->email[uid_length - 1] == '>')
             && (uid_length == sender_length + 2)