Updated card stuff to support T=0 cards.
[gnupg.git] / g10 / photoid.c
index b311bfa..885bbca 100644 (file)
@@ -1,5 +1,5 @@
 /* photoid.c - photo ID handling code
- * Copyright (C) 2001, 2002 Free Software Foundation, Inc.
+ * Copyright (C) 2001, 2002, 2005 Free Software Foundation, Inc.
  *
  * This file is part of GnuPG.
  *
  *
  * 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
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
+ * USA.
  */
 
 #include <config.h>
 #include <errno.h>
 #include <stdio.h>
 #include <string.h>
-#ifdef __MINGW32__ 
+#ifdef _WIN32 
 # include <windows.h>
 # ifndef VER_PLATFORM_WIN32_WINDOWS
 #  define VER_PLATFORM_WIN32_WINDOWS 1
 #include "options.h"
 #include "main.h"
 #include "photoid.h"
+#include "ttyio.h"
 
 /* Generate a new photo id packet, or return NULL if canceled */
-PKT_user_id *generate_photo_id(PKT_public_key *pk)
+PKT_user_id *
+generate_photo_id(PKT_public_key *pk,const char *photo_name)
 {
   PKT_user_id *uid;
   int error=1,i;
   unsigned int len;
-  char *filename=NULL;
+  char *filename;
   byte *photo=NULL;
   byte header[16];
   IOBUF file;
@@ -60,48 +63,77 @@ PKT_user_id *generate_photo_id(PKT_public_key *pk)
     header[i]=0;
 
 #define EXTRA_UID_NAME_SPACE 71
-  uid=m_alloc_clear(sizeof(*uid)+71);
+  uid=xmalloc_clear(sizeof(*uid)+71);
 
-  printf(_("\nPick an image to use for your photo ID.  "
-          "The image must be a JPEG file.\n"
-          "Remember that the image is stored within your public key.  "
-          "If you use a\n"
-          "very large picture, your key will become very large as well!\n"
-          "Keeping the image close to 240x288 is a good size to use.\n"));
+  if(photo_name && *photo_name)
+    filename=make_filename(photo_name,(void *)NULL);
+  else
+    {
+      tty_printf(_("\nPick an image to use for your photo ID."
+                  "  The image must be a JPEG file.\n"
+                  "Remember that the image is stored within your public key."
+                  "  If you use a\n"
+                  "very large picture, your key will become very large"
+                  " as well!\n"
+                  "Keeping the image close to 240x288 is a good size"
+                  " to use.\n"));
+      filename=NULL;
+    }
 
   while(photo==NULL)
     {
-      printf("\n");
+      if(filename==NULL)
+       {
+         char *tempname;
+
+         tty_printf("\n");
 
-      m_free(filename);
+         tty_enable_completion(NULL);
 
-      filename=cpr_get("photoid.jpeg.add",
-                      _("Enter JPEG filename for photo ID: "));
+         tempname=cpr_get("photoid.jpeg.add",
+                          _("Enter JPEG filename for photo ID: "));
 
-      if(strlen(filename)==0)
-       goto scram;
+         tty_disable_completion();
+
+         filename=make_filename(tempname,(void *)NULL);
+
+         xfree(tempname);
+
+         if(strlen(filename)==0)
+           goto scram;
+       }
 
       file=iobuf_open(filename);
+      if (file && is_secured_file (iobuf_get_fd (file)))
+        {
+          iobuf_close (file);
+          file = NULL;
+          errno = EPERM;
+        }
       if(!file)
        {
-         log_error(_("Unable to open photo \"%s\": %s\n"),
+         log_error(_("unable to open JPEG file `%s': %s\n"),
                    filename,strerror(errno));
+         xfree(filename);
+         filename=NULL;
          continue;
        }
 
       len=iobuf_get_filelength(file);
       if(len>6144)
        {
-         printf("This JPEG is really large (%d bytes) !\n",len);
+         tty_printf( _("This JPEG is really large (%d bytes) !\n"),len);
          if(!cpr_get_answer_is_yes("photoid.jpeg.size",
-                           _("Are you sure you want to use it (y/N)? ")))
+                           _("Are you sure you want to use it? (y/N) ")))
          {
            iobuf_close(file);
+           xfree(filename);
+           filename=NULL;
            continue;
          }
        }
 
-      photo=m_alloc(len);
+      photo=xmalloc(len);
       iobuf_read(file,photo,len);
       iobuf_close(file);
 
@@ -109,9 +141,11 @@ PKT_user_id *generate_photo_id(PKT_public_key *pk)
       if(photo[0]!=0xFF || photo[1]!=0xD8 ||
         photo[6]!='J' || photo[7]!='F' || photo[8]!='I' || photo[9]!='F')
        {
-         log_error(_("\"%s\" is not a JPEG file\n"),filename);
-         m_free(photo);
+         log_error(_("`%s' is not a JPEG file\n"),filename);
+         xfree(photo);
          photo=NULL;
+         xfree(filename);
+         filename=NULL;
          continue;
        }
 
@@ -132,8 +166,10 @@ PKT_user_id *generate_photo_id(PKT_public_key *pk)
              goto scram;
            case 0:
              free_attributes(uid);
-             m_free(photo);
+             xfree(photo);
              photo=NULL;
+             xfree(filename);
+             filename=NULL;
              continue;
            }
        }
@@ -143,13 +179,13 @@ PKT_user_id *generate_photo_id(PKT_public_key *pk)
   uid->ref=1;
 
  scram:
-  m_free(filename);
-  m_free(photo);
+  xfree(filename);
+  xfree(photo);
 
   if(error)
     {
       free_attributes(uid);
-      m_free(uid);
+      xfree(uid);
       return NULL;
     }
 
@@ -223,7 +259,7 @@ char *image_type_to_string(byte type,int style)
 #if !defined(FIXED_PHOTO_VIEWER) && !defined(DISABLE_PHOTO_VIEWER)
 static const char *get_default_photo_command(void)
 {
-#if defined(__MINGW32__)
+#if defined(_WIN32)
   OSVERSIONINFO osvi;
 
   memset(&osvi,0,sizeof(osvi));
@@ -283,7 +319,7 @@ void show_photos(const struct user_attribute *attrs,
        if(!command)
          goto fail;
 
-       name=m_alloc(16+strlen(EXTSEP_S)+
+       name=xmalloc(16+strlen(EXTSEP_S)+
                     strlen(image_type_to_string(args.imagetype,0))+1);
 
        /* Make the filename.  Notice we are not using the image
@@ -302,7 +338,7 @@ void show_photos(const struct user_attribute *attrs,
 
        if(exec_write(&spawn,NULL,command,name,1,1)!=0)
          {
-           m_free(name);
+           xfree(name);
            goto fail;
          }
 
@@ -311,7 +347,7 @@ void show_photos(const struct user_attribute *attrs,
                                         image_type_to_string(args.imagetype,2));
 #endif
 
-       m_free(name);
+       xfree(name);
 
        fwrite(&attrs[i].data[offset],attrs[i].len-offset,1,spawn->tochild);