/*
- * $Id: hqx.c,v 1.5 2001-05-01 13:21:22 rufustfirefly Exp $
+ * $Id: hqx.c,v 1.18 2010-01-27 21:27:53 didg Exp $
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
-#endif
+#endif /* HAVE_CONFIG_H */
-#include <stdlib.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/time.h>
#include <sys/param.h>
-#ifdef notdef
-#if BSD >= 199006
-# include <machine/endian.h>
-#else
-# include <netinet/in.h>
-#endif
-#endif notdef
-#include <time.h>
-#include <fcntl.h>
+
#include <string.h>
-#include <syslog.h>
#include <ctype.h>
#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+
+#include <unistd.h>
+#ifdef HAVE_FCNTL_H
+#include <fcntl.h>
+#endif /* HAVE_FCNTL_H */
+
+#include <netinet/in.h>
+
#include <atalk/adouble.h>
#include <netatalk/endian.h>
+
#include "megatron.h"
+#include "nad.h"
+#include "hqx.h"
+#include "updcrc.h"
#define HEXOUTPUT 0
*/
#ifndef STDIN
# define STDIN "-"
-#endif
+#endif /* ! STDIN */
/* Yes and no
*/
#define BHH_CRCSIZ 2
#define BHH_HEADSIZ 21
-u_short updcrc();
-
#if HEXOUTPUT
FILE *rawhex, *expandhex;
-#endif
+#endif /* HEXOUTPUT */
-struct hqx_file_data {
+static struct hqx_file_data {
u_int32_t forklen[ NUMFORKS ];
u_short forkcrc[ NUMFORKS ];
char path[ MAXPATHLEN + 1];
} hqx;
extern char *forkname[];
-u_char hqx7_buf[8192];
-u_char *hqx7_first;
-u_char *hqx7_last;
-int first_flag;
+static u_char hqx7_buf[8192];
+static u_char *hqx7_first;
+static u_char *hqx7_last;
+static int first_flag;
/*
hqx_open must be called first. pass it a filename that is supposed
here; skip_junk leaves hqx7_first and hqx7_last set.
*/
-int hqx_open( hqxfile, flags, fh, options )
- char *hqxfile;
- int flags, options;
- struct FHeader *fh;
+int hqx_open(char *hqxfile, int flags, struct FHeader *fh, int options)
{
int maxlen;
#if DEBUG
fprintf( stderr, "megatron: entering hqx_open\n" );
-#endif
+#endif /* DEBUG */
+ select_charset( options);
if ( flags == O_RDONLY ) {
#if HEXOUTPUT
rawhex = fopen( "rawhex.unhex", "w" );
expandhex = fopen( "expandhex.unhex", "w" );
-#endif
+#endif /* HEXOUTPUT */
first_flag = 0;
#if DEBUG
off_t pos;
- pos = lseek( hqx.filed, 0, L_INCR );
+ pos = lseek( hqx.filed, 0, SEEK_CUR );
fprintf( stderr, "megatron: current position is %ld\n", pos );
-#endif
+#endif /* DEBUG */
return( 0 );
}
}
* Otherwise, a value of -1 is returned.
*/
-int hqx_close( keepflag )
- int keepflag;
+int hqx_close(int keepflag)
{
if ( keepflag == KEEP ) {
return( close( hqx.filed ));
* return zero and no more than that.
*/
-int hqx_read( fork, buffer, length )
- int fork;
- char *buffer;
- int length;
+ssize_t hqx_read(int fork, char *buffer, size_t length)
{
u_short storedcrc;
- int readlen;
- int cc;
+ size_t readlen;
+ size_t cc;
#if DEBUG >= 3
{
off_t pos;
- pos = lseek( hqx.filed, 0, L_INCR );
+ pos = lseek( hqx.filed, 0, SEEK_CUR );
fprintf( stderr, "hqx_read: current position is %ld\n", pos );
}
fprintf( stderr, "hqx_read: fork is %s\n", forkname[ fork ] );
fprintf( stderr, "hqx_read: remaining length is %d\n", hqx.forklen[fork] );
-#endif
+#endif /* DEBUG >= 3 */
- if ( hqx.forklen[ fork ] < 0 ) {
- fprintf( stderr, "This should never happen, dude!\n" );
- return( hqx.forklen[ fork ] );
+ if (hqx.forklen[fork] > 0x7FFFFFFF) {
+ fprintf(stderr, "This should never happen, dude!, fork length == %u\n", hqx.forklen[fork]);
+ return -1;
}
if ( hqx.forklen[ fork ] == 0 ) {
#if DEBUG >= 4
fprintf( stderr, "hqx_read: storedcrc\t\t%x\n", storedcrc );
fprintf( stderr, "hqx_read: observed crc\t\t%x\n\n", hqx.forkcrc[fork] );
-#endif
+#endif /* DEBUG >= 4 */
if ( storedcrc == hqx.forkcrc[ fork ] ) {
return( 0 );
}
}
#if DEBUG >= 3
fprintf( stderr, "hqx_read: readlen is %d\n", readlen );
-#endif
+#endif /* DEBUG >= 3 */
cc = hqx_7tobin( buffer, readlen );
if ( cc > 0 ) {
}
#if DEBUG >= 3
fprintf( stderr, "hqx_read: chars read is %d\n", cc );
-#endif
+#endif /* DEBUG >= 3 */
return( cc );
}
* to fill the hqx_header fields.
*/
-int hqx_header_read( fh )
- struct FHeader *fh;
+int hqx_header_read(struct FHeader *fh)
{
char *headerbuf, *headerptr;
u_int32_t time_seconds;
#if HEXOUTPUT
int headerfork;
headerfork = open( "headerfork", O_WRONLY|O_CREAT, 0622 );
-#endif
+#endif /* HEXOUTPUT */
mask = htons( 0xfcee );
hqx.headercrc = 0;
#if HEXOUTPUT
write( headerfork, &namelen, sizeof( namelen ));
-#endif
+#endif /* HEXOUTPUT */
if (( headerbuf =
- (char *)malloc( (unsigned int)( namelen + BHH_HEADSIZ ))) == 0 ) {
+ (char *)malloc( (unsigned int)( namelen + BHH_HEADSIZ ))) == NULL ) {
return( -1 );
}
if ( hqx_7tobin( headerbuf, ( namelen + BHH_HEADSIZ )) == 0 ) {
#if HEXOUTPUT
write( headerfork, headerbuf, ( namelen + BHH_HEADSIZ ));
-#endif
+#endif /* HEXOUTPUT */
/*
* stuff from the hqx file header
memcpy( fh->name, headerptr, (int)namelen );
headerptr += namelen;
- headerptr += BVERSION;
- memcpy(&fh->finder_info, headerptr, TCSIZ );
- headerptr += TCSIZ;
- memcpy(&fh->finder_info.fdFlags, headerptr, FLAGSIZ );
+ headerptr += BHH_VERSION;
+ memcpy(&fh->finder_info, headerptr, BHH_TCSIZ );
+ headerptr += BHH_TCSIZ;
+ memcpy(&fh->finder_info.fdFlags, headerptr, BHH_FLAGSIZ );
fh->finder_info.fdFlags = fh->finder_info.fdFlags & mask;
headerptr += BHH_FLAGSIZ;
memcpy(&fh->forklen[ DATA ], headerptr, BHH_DATASIZ );
fprintf( stderr, "observed crc\t\t%x\n", hqx.headercrc );
fprintf( stderr, "\n" );
}
-#endif
+#endif /* DEBUG >= 5 */
/*
* create and modify times are figured from right now
* hqx_header_write.
*/
-int hqx_header_write( fh )
- struct FHeader *fh;
+int hqx_header_write(struct FHeader *fh _U_)
{
return( -1 );
}
* it sets the pointers to the hqx7 buffer up to point to the valid data.
*/
-int hqx7_fill( hqx7_ptr )
- u_char *hqx7_ptr;
+ssize_t hqx7_fill(u_char *hqx7_ptr)
{
- int cc;
- int cs;
+ ssize_t cc;
+ size_t cs;
cs = hqx7_ptr - hqx7_buf;
if ( cs >= sizeof( hqx7_buf )) return( -1 );
0xFC signals a whitespace character.
*/
-u_char hqxlookup[] = {
+static const u_char hqxlookup[] = {
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFC, 0xFE, 0xFF, 0xFF, 0xFE, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
* OTHER when looking for any subsequent line.
*/
-int skip_junk( line )
-int line;
+int skip_junk(int line)
{
int found = NOWAY;
int stopflag;
* file is reached.
*/
-int hqx_7tobin( outbuf, datalen )
- char *outbuf;
- int datalen;
+size_t hqx_7tobin( char *outbuf, size_t datalen)
{
static u_char hqx8[3];
static int hqx8i;
#if DEBUG
fprintf( stderr, "hqx_7tobin: datalen entering %d\n", datalen );
fprintf( stderr, "hqx_7tobin: hqx8i entering %d\n", hqx8i );
-#endif
+#endif /* DEBUG */
if ( first_flag == 0 ) {
prev_hqx8 = 0;
#if DEBUG
fprintf( stderr, "hqx_7tobin: hqx8i entering %d\n", hqx8i );
-#endif
+#endif /* DEBUG */
out_first = outbuf;
out_last = out_first + datalen;
#if HEXOUTPUT
putc( hqx8i, rawhex );
putc( hqx8[ hqx8i ], rawhex );
-#endif
+#endif /* HEXOUTPUT */
if ( prev_hqx8 == RUNCHAR ) {
if ( hqx8[ hqx8i ] == 0 ) {
*out_first = prev_hqx8;
#if HEXOUTPUT
putc( *out_first, expandhex );
-#endif
+#endif /* HEXOUTPUT */
prev_out = prev_hqx8;
out_first++;
}
*out_first = prev_out;
#if HEXOUTPUT
putc( *out_first, expandhex );
-#endif
+#endif /* HEXOUTPUT */
hqx8[ hqx8i ]--;
out_first++;
}
*out_first = prev_hqx8;
#if HEXOUTPUT
putc( *out_first, expandhex );
-#endif
+#endif /* HEXOUTPUT */
prev_out = prev_hqx8;
out_first++;
}