OSDN Git Service

support level 0 header (limited)
authorKoji Arai <jca02266@gmail.com>
Mon, 24 Mar 2008 19:17:17 +0000 (04:17 +0900)
committerKoji Arai <jca02266@gmail.com>
Mon, 24 Mar 2008 19:17:17 +0000 (04:17 +0900)
ar.c

diff --git a/ar.c b/ar.c
index 155ecef..fb4c150 100644 (file)
--- a/ar.c
+++ b/ar.c
@@ -233,6 +233,38 @@ put_char(char *buf, char *p, size_t size)
 }
 
 static int
+read_header_lv0(FILE *fp, char *buf, struct lzh_header *h)
+{
+    int headersize;
+    int headersum;
+    int ext_headersize;
+
+    headersize = get_byte(&buf[0]);
+    headersum = get_byte(&buf[1]);
+
+    fread_crc(buf + 21, headersize - (21 - 2), fp);     /* CRC not used */
+
+    buf += 2;
+
+    if (calc_headersum(buf, headersize) != headersum)
+        error("Header sum error");
+
+    get_char(&buf[0], h->method, 5);
+    h->compsize = get_dword(&buf[5]);
+    h->origsize = get_dword(&buf[9]);
+    h->ftime    = get_dword(&buf[13]);
+    /* attrib   = get_byte(&buf[17]); */
+    h->level    = get_byte(&buf[18]); /* header level */
+    h->namelen = get_byte(&buf[19]);
+    get_char(&buf[20], h->filename, h->namelen);
+    h->filename[h->namelen] = 0;
+    h->file_crc = get_word(&buf[20+h->namelen]);
+    h->os_id    = 0;            /* generic */
+
+    return 1;                   /* success */
+}
+
+static int
 read_header_lv1(FILE *fp, char *buf, struct lzh_header *h)
 {
     int headersize;
@@ -353,6 +385,7 @@ read_header(FILE *fp, struct lzh_header *h)
     fread_crc(buf + 1, 21 - 1, fp);
     switch (buf[20]) {
     case 0:
+        return read_header_lv0(fp, buf, h);
         break;
     case 1:
         return read_header_lv1(fp, buf, h);
@@ -372,6 +405,31 @@ read_header(FILE *fp, struct lzh_header *h)
 void
 write_header_lv0(FILE *fp, struct lzh_header *h)
 {
+    char buf[4096], *p = buf;
+    int sum;
+    int headersize;
+
+    headersize = 22 + h->namelen;
+
+    put_byte(&buf[0], 0);       /* dummy */
+    put_byte(&buf[1], 0);       /* dummy */
+    put_char(&buf[2], h->method, 5);
+    put_dword(&buf[7], h->compsize);    /* packed size */
+    put_dword(&buf[11], h->origsize);   /* original size */
+    put_dword(&buf[15], h->ftime);      /* ftime */
+    put_byte(&buf[19], 0x20);           /* attribute */
+    put_byte(&buf[20], 0);              /* level */
+    put_byte(&buf[21], h->namelen);     /* length of pathname */
+    put_char(&buf[22], h->filename, h->namelen);
+    put_word(&buf[22+h->namelen], h->file_crc);
+
+    headersize += 0;            /* size of ext-header (old style) */
+    put_byte(&buf[0], headersize);
+
+    sum = calc_headersum(buf+2, headersize);
+    put_byte(&buf[1], sum);
+
+    fwrite_crc(buf, headersize+2, fp);
 }
 
 void
@@ -384,14 +442,14 @@ write_header_lv1(FILE *fp, struct lzh_header *h)
     headersize = 25 + h->namelen;
 
     put_byte(&buf[0], headersize);
-    put_byte(&buf[1], 0); /* dummy */
+    put_byte(&buf[1], 0);       /* dummy */
     put_char(&buf[2], h->method, 5);
-    put_dword(&buf[7], h->compsize);   /* packed size */
+    put_dword(&buf[7], h->compsize);    /* packed size */
     put_dword(&buf[11], h->origsize);   /* original size */
-    put_dword(&buf[15], h->ftime);   /* ftime */
-    put_byte(&buf[19], 0x20);   /* attribute */
-    put_byte(&buf[20], 1);  /* level */
-    put_byte(&buf[21], h->namelen); /* length of pathname */
+    put_dword(&buf[15], h->ftime);      /* ftime */
+    put_byte(&buf[19], 0x20);           /* attribute */
+    put_byte(&buf[20], 1);              /* level */
+    put_byte(&buf[21], h->namelen);     /* length of pathname */
     put_char(&buf[22], h->filename, h->namelen);
     put_word(&buf[22+h->namelen], h->file_crc);
     put_byte(&buf[22+h->namelen+2], 'M');
@@ -664,7 +722,7 @@ static void
 list_start(void)
 {
     if (opts.quiet < 2)
-        printf("Filename         Original Compressed Ratio CRC Method\n");
+        printf("Filename         Original Compressed Ratio CRC Method Lv\n");
 }
 
 static void
@@ -676,8 +734,14 @@ list(struct lzh_header *h)
     if (h->namelen > 14)
         printf("\n              ");
     r = ratio(h->compsize, h->origsize);
-    printf(" %10lu %10lu %u.%03u %04X %5.5s\n",
-           h->origsize, h->compsize, r / 1000, r % 1000, h->file_crc, h->method);
+    printf(" %10lu %10lu %u.%03u %04X %5.5s [%d]\n",
+           h->origsize,
+           h->compsize,
+           r / 1000,
+           r % 1000,
+           h->file_crc,
+           h->method,
+           h->level);
 }
 
 static int