[AD] restore old-style encyption reading

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


a.cc thread: http://www.allegro.cc/forums/view_thread.php?_id=480238

No response yet. I'm posting the fixed up patch here so we don't forget about it.

Peter
Index: include/allegro/file.h
===================================================================
RCS file: /cvsroot/alleg/allegro/include/allegro/file.h,v
retrieving revision 1.12
diff -u -r1.12 file.h
--- include/allegro/file.h	8 Mar 2005 10:00:15 -0000	1.12
+++ include/allegro/file.h	13 Apr 2005 14:18:23 -0000
@@ -79,6 +79,7 @@
 #define PACKFILE_FLAG_CHUNK      4     /* file is a sub-chunk */
 #define PACKFILE_FLAG_EOF        8     /* reached the end-of-file */
 #define PACKFILE_FLAG_ERROR      16    /* an error has occurred */
+#define PACKFILE_FLAG_OLD_CRYPT  32    /* backward compatibility mode */
 #define PACKFILE_FLAG_EXEDAT     64    /* reading from our executable */
 
 
Index: src/file.c
===================================================================
RCS file: /cvsroot/alleg/allegro/src/file.c,v
retrieving revision 1.58
diff -u -r1.58 file.c
--- src/file.c	14 Mar 2005 11:41:59 -0000	1.58
+++ src/file.c	13 Apr 2005 14:18:24 -0000
@@ -1639,6 +1639,49 @@
 
 	 header = pack_mgetl(f->normal.parent);
 
+	 if ((f->normal.parent->normal.passpos) &&
+	     ((header == encrypt_id(F_PACK_MAGIC, FALSE)) ||
+	      (header == encrypt_id(F_NOPACK_MAGIC, FALSE))))
+	 {
+	    /* duplicate the file descriptor */
+	    int fd2 = dup(fd);
+  
+	    if (fd2<0) {
+	       pack_fclose(f->normal.parent);
+	       free_packfile(f);
+	       *allegro_errno = errno;
+	       return NULL;
+	    }
+  
+	    /* close the parent file (logically, not physically) */
+	    pack_fclose(f->normal.parent);
+  
+	    /* backward compatibility mode */
+	    if (!clone_password(f)) {
+	       free_packfile(f);
+	       return NULL;
+	    }
+  
+	    f->normal.flags |= PACKFILE_FLAG_OLD_CRYPT;
+  
+	    /* re-open the parent file */
+	    lseek(fd2, 0, SEEK_SET);
+  
+	    if ((f->normal.parent = _pack_fdopen(fd2, F_READ)) == NULL) {
+	       free_packfile(f);
+	       return NULL;
+	    }
+  
+	    f->normal.parent->normal.flags |= PACKFILE_FLAG_OLD_CRYPT;
+  
+	    pack_mgetl(f->normal.parent);
+  
+	    if (header == encrypt_id(F_PACK_MAGIC, FALSE))
+	       header = encrypt_id(F_PACK_MAGIC, TRUE);
+	    else
+	       header = encrypt_id(F_NOPACK_MAGIC, TRUE);
+	 }
+
 	 if (header == encrypt_id(F_PACK_MAGIC, TRUE)) {
 	    f->normal.todo = LONG_MAX;
 	 }
@@ -1899,6 +1942,21 @@
       chunk->normal.flags = PACKFILE_FLAG_CHUNK;
       chunk->normal.parent = f;
 
+      if (f->normal.flags & PACKFILE_FLAG_OLD_CRYPT) {
+	 /* backward compatibility mode */
+	 if (f->normal.passdata) {
+	    if ((chunk->normal.passdata = malloc(strlen(f->normal.passdata)+1)) == NULL) {
+	       *allegro_errno = ENOMEM;
+	       free(chunk);
+	       return NULL;
+	    }
+	    _al_sane_strncpy(chunk->normal.passdata, f->normal.passdata, strlen(f->normal.passdata)+1);
+	    chunk->normal.passpos = chunk->normal.passdata + (long)f->normal.passpos - (long)f->normal.passdata;
+	    f->normal.passpos = f->normal.passdata;
+	 }
+	 chunk->normal.flags |= PACKFILE_FLAG_OLD_CRYPT;
+      }
+
       if (_packfile_datasize < 0) {
 	 /* read a packed chunk */
          chunk->normal.unpack_data = create_lzss_unpack_data();
@@ -2015,6 +2073,9 @@
 	 f->normal.unpack_data = NULL;
       }
 
+      if ((f->normal.passpos) && (f->normal.flags & PACKFILE_FLAG_OLD_CRYPT))
+	 parent->normal.passpos = parent->normal.passdata + (long)f->normal.passpos - (long)f->normal.passdata;
+
       free_packfile(f);
    }
 
@@ -2662,7 +2723,7 @@
 	 sz = read(f->normal.hndl, f->normal.buf+done, f->normal.buf_size-done);
       }
 
-      if (f->normal.passpos) {
+      if ((f->normal.passpos) && (!(f->normal.flags & PACKFILE_FLAG_OLD_CRYPT))) {
 	 for (i=0; i<f->normal.buf_size; i++) {
 	    f->normal.buf[i] ^= *(f->normal.passpos++);
 	    if (!*f->normal.passpos)
@@ -2704,7 +2765,7 @@
 	    goto Error;
       }
       else {
-	 if (f->normal.passpos) {
+	 if ((f->normal.passpos) && (!(f->normal.flags & PACKFILE_FLAG_OLD_CRYPT))) {
 	    for (i=0; i<f->normal.buf_size; i++) {
 	       f->normal.buf[i] ^= *(f->normal.passpos++);
 	       if (!*f->normal.passpos)
Index: src/lzss.c
===================================================================
RCS file: /cvsroot/alleg/allegro/src/lzss.c,v
retrieving revision 1.1
diff -u -r1.1 lzss.c
--- src/lzss.c	8 Mar 2005 09:57:39 -0000	1.1
+++ src/lzss.c	13 Apr 2005 14:18:24 -0000
@@ -327,6 +327,16 @@
       }
 
       if ((mask <<= 1) == 0) {                  /* shift mask left one bit */
+
+	 if ((file->is_normal_packfile) && (file->normal.passpos) &&
+	     (file->normal.flags & PACKFILE_FLAG_OLD_CRYPT))
+	 {
+	    dat->code_buf[0] ^= *file->normal.passpos;
+	    file->normal.passpos++;
+	    if (!*file->normal.passpos)
+	       file->normal.passpos = file->normal.passdata;
+	 }
+
 	 for (i=0; i<code_buf_ptr; i++)         /* send at most 8 units of */
 	    pack_putc(dat->code_buf[i], file);  /* code together */
 
@@ -374,6 +384,16 @@
    } while (len > 0);   /* until length of string to be processed is zero */
 
    if (code_buf_ptr > 1) {         /* send remaining code */
+
+      if ((file->is_normal_packfile) && (file->normal.passpos) &&
+	  (file->normal.flags & PACKFILE_FLAG_OLD_CRYPT))
+      {
+	 dat->code_buf[0] ^= *file->normal.passpos;
+	 file->normal.passpos++;
+	 if (!*file->normal.passpos)
+	    file->normal.passpos = file->normal.passdata;
+      }
+
       for (i=0; i<code_buf_ptr; i++) {
 	 pack_putc(dat->code_buf[i], file);
 	 if (pack_ferror(file)) {
@@ -465,6 +485,15 @@
       if (((flags >>= 1) & 256) == 0) {
 	 if ((c = pack_getc(file)) == EOF)
 	    break;
+  
+	 if ((file->is_normal_packfile) && (file->normal.passpos) &&
+	     (file->normal.flags & PACKFILE_FLAG_OLD_CRYPT))
+	 {
+	    c ^= *file->normal.passpos;
+	    file->normal.passpos++;
+	    if (!*file->normal.passpos)
+	       file->normal.passpos = file->normal.passdata;
+	 }
 
 	 flags = c | 0xFF00;        /* uses higher byte to count eight */
       }


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/