]> arthur.barton.de Git - netatalk.git/blobdiff - bin/megatron/macbin.c
Merge branch 'develop'
[netatalk.git] / bin / megatron / macbin.c
index b11d088c4713363e372af207c51fd4206ddfb26e..51b7ac5f616ede35aeb604892e423e3bbf4a35e6 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * $Id: macbin.c,v 1.8 2001-06-29 14:14:46 rufustfirefly Exp $
+ * $Id: macbin.c,v 1.15 2010-01-27 21:27:53 didg Exp $
  */
 
 #ifdef HAVE_CONFIG_H
 #include <sys/uio.h>
 #include <sys/time.h>
 #include <sys/param.h>
-#ifdef HAVE_FCNTL_H
 #include <fcntl.h>
-#endif /* HAVE_FCNTL_H */
-#ifdef HAVE_UNISTD_H
 #include <unistd.h>
-#endif /* HAVE_UNISTD_H */
 #include <string.h>
 #include <strings.h>
-#include <syslog.h>
 #include <ctype.h>
 #include <stdio.h>
 #include <time.h>
@@ -27,6 +22,7 @@
 #include <netatalk/endian.h>
 #include "megatron.h"
 #include "macbin.h"
+#include "updcrc.h"
 
 /* This allows megatron to generate .bin files that won't choke other
    well-known converter apps. It also makes sure that checksums
@@ -53,7 +49,7 @@
        following globals; therefore this module can only be used
        for one of the two functions at a time.
  */
-struct bin_file_data {
+static struct bin_file_data {
     u_int32_t          forklen[ NUMFORKS ];
     char               path[ MAXPATHLEN + 1];
     int                        filed;
@@ -62,7 +58,7 @@ struct bin_file_data {
 }              bin;
 
 extern char    *forkname[];
-u_char         head_buf[HEADBUFSIZ];
+static u_char  head_buf[HEADBUFSIZ];
 
 /* 
  * bin_open must be called first.  pass it a filename that is supposed
@@ -70,10 +66,7 @@ u_char               head_buf[HEADBUFSIZ];
  * somewhat initialized; bin_filed is set.
  */
 
-int bin_open( binfile, flags, fh, options )
-    char               *binfile;
-    int                        flags, options;
-    struct FHeader     *fh;
+int bin_open(char *binfile, int flags, struct FHeader *fh, int options)
 {
     int                        maxlen;
     int                        rc;
@@ -146,8 +139,7 @@ int bin_open( binfile, flags, fh, options )
  * Otherwise, a value of -1 is returned.
  */
 
-int bin_close( keepflag )
-    int                        keepflag;
+int bin_close(int keepflag)
 {
 #if DEBUG
     fprintf( stderr, "entering bin_close\n" );
@@ -171,14 +163,11 @@ int bin_close( keepflag )
  * return zero and no more than that.
  */
 
-int bin_read( fork, buffer, length )
-    int                        fork;
-    char               *buffer;
-    int                        length;
+ssize_t bin_read( int fork, char *buffer, size_t length)
 {
     char               *buf_ptr;
-    int                        readlen;
-    int                        cc = 1;
+    size_t             readlen;
+    ssize_t            cc = 1;
     off_t              pos;
 
 #if DEBUG >= 3
@@ -186,9 +175,9 @@ int bin_read( fork, buffer, length )
     fprintf( stderr, "bin_read: remaining length is %d\n", bin.forklen[fork] );
 #endif /* DEBUG >= 3 */
 
-    if ( bin.forklen[ fork ] < 0 ) {
-       fprintf( stderr, "This should never happen, dude!\n" );
-       return( bin.forklen[ fork ] );
+    if (bin.forklen[fork] > 0x7FFFFFFF) {
+       fprintf(stderr, "This should never happen, dude! fork length == %u\n", bin.forklen[fork]);
+       return -1;
     }
 
     if ( bin.forklen[ fork ] == 0 ) {
@@ -243,14 +232,11 @@ int bin_read( fork, buffer, length )
  * bin_write 
  */
 
-int bin_write( fork, buffer, length )
-    int                        fork;
-    char               *buffer;
-    int                        length;
+ssize_t bin_write(int fork, char *buffer, size_t length)
 {
     char               *buf_ptr;
-    int                        writelen;
-    int                        cc = 0;
+    ssize_t            writelen;
+    ssize_t            cc = 0;
     off_t              pos;
     u_char             padchar = 0x7f;
                /* Not sure why, but it seems this must be 0x7f to match
@@ -287,12 +273,8 @@ int bin_write( fork, buffer, length )
        perror( "Couldn't write to macbinary file:" );
        return( cc );
     }
-    bin.forklen[ fork ] -= length;
 
-    if ( bin.forklen[ fork ] < 0 ) {
-       fprintf( stderr, "This should never happen, dude!\n" );
-       return( bin.forklen[ fork ] );
-    }
+    bin.forklen[fork] -= length;
 
 /*
  * add the padding at end of data and resource forks
@@ -329,9 +311,7 @@ int bin_write( fork, buffer, length )
  * of the bytes of the other two forks can be read, as well.
  */
 
-int bin_header_read( fh, revision )
-    struct FHeader     *fh;
-    int                        revision;
+int bin_header_read(struct FHeader *fh, int revision)
 {
     u_short            mask;
 
@@ -431,8 +411,7 @@ int bin_header_read( fh, revision )
  * bin_header_write and bin_header_read are opposites.
  */
 
-int bin_header_write( fh )
-    struct FHeader     *fh;
+int bin_header_write(struct FHeader *fh)
 {
     char               *write_ptr;
     u_int32_t           t;
@@ -540,7 +519,7 @@ int bin_header_write( fh )
 int test_header(void)
 {
     const char          zeros[25] = "";
-    u_int32_t          cc;
+    ssize_t            cc;
     u_short            header_crc;
     u_char             namelen;
 
@@ -596,13 +575,14 @@ int test_header(void)
         return -1;
 
     /* macbinary forks aren't larger than 0x7FFFFF */
+    /* we allow forks to be larger, breaking the specs */
     memcpy(&cc, head_buf + 83, sizeof(cc));
     cc = ntohl(cc);
-    if (cc > 0x7FFFFF)
+    if (cc > 0x7FFFFFFF)
         return -1;
     memcpy(&cc, head_buf + 87, sizeof(cc));
     cc = ntohl(cc);
-    if (cc > 0x7FFFFF)
+    if (cc > 0x7FFFFFFF)
         return -1;