netatalk  4.6.0
Free and Open Source Apple Filing Protocol (AFP) Server
Loading...
Searching...
No Matches
hqx.c File Reference
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/time.h>
#include <sys/param.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <netinet/in.h>
#include <atalk/adouble.h>
#include <netatalk/endian.h>
#include "nad.h"
#include "megatron.h"
#include "nad_adouble.h"
#include "hqx.h"
#include "crc16.h"

Data Structures

struct  hqx_file_data

Macros

#define NOWAY   0
#define SURETHANG   1
#define FIRST   0
#define OTHER   1
#define RUNCHAR   0x90
#define BHH_VERSION   1
#define BHH_TCSIZ   8
#define BHH_FLAGSIZ   2
#define BHH_DATASIZ   4
#define BHH_RESSIZ   4
#define BHH_CRCSIZ   2
#define BHH_HEADSIZ   21

Functions

static int hqx_write_all (const void *buf, size_t len)
static int hqx_put_char (unsigned char c)
static int hqx_emit_6bit (unsigned char b)
static int hqx_emit_byte (unsigned char b)
static int hqx_emit_data (const void *buf, size_t len)
static int hqx_flush_6bit (void)
static int hqx_finish_fork (int fork)
int hqx_open (char *hqxfile, int flags, struct FHeader *fh, int options)
 Open a BinHex file for reading or writing.
int hqx_close (int keepflag)
 Close the active BinHex file.
const char * hqx_path (void)
ssize_t hqx_read (int fork, char *buffer, size_t length)
 Read decoded data from a BinHex fork.
int hqx_header_read (struct FHeader *fh)
 Read and validate a BinHex header.
int hqx_header_write (struct FHeader *fh)
 Encode and write a BinHex header.
ssize_t hqx_write (int fork, char *buffer, size_t length)
ssize_t hqx7_fill (unsigned char *hqx7_ptr)
 Fill the BinHex text input buffer.
int skip_junk (int line)
 Skip non-BinHex text until encoded data is found.
size_t hqx_7tobin (char *outbuf, size_t datalen)
 Decode BinHex text data to binary.

Variables

static struct hqx_file_data hqx
char * forkname []
static unsigned char hqx7_buf [8192]
static unsigned char * hqx7_first
static unsigned char * hqx7_last
static int first_flag
static const unsigned char hqxchars []
static const unsigned char hqxlookup []

Macro Definition Documentation

◆ BHH_CRCSIZ

#define BHH_CRCSIZ   2

◆ BHH_DATASIZ

#define BHH_DATASIZ   4

◆ BHH_FLAGSIZ

#define BHH_FLAGSIZ   2

◆ BHH_HEADSIZ

#define BHH_HEADSIZ   21

◆ BHH_RESSIZ

#define BHH_RESSIZ   4

◆ BHH_TCSIZ

#define BHH_TCSIZ   8

◆ BHH_VERSION

#define BHH_VERSION   1

◆ FIRST

#define FIRST   0

◆ NOWAY

#define NOWAY   0

◆ OTHER

#define OTHER   1

◆ RUNCHAR

#define RUNCHAR   0x90

◆ SURETHANG

#define SURETHANG   1

Function Documentation

◆ hqx7_fill()

ssize_t hqx7_fill ( unsigned char * hqx7_ptr)

Fill the BinHex text input buffer.

This is called from skip_junk() and hqx_7tobin(). It reads from the BinHex file into the hqx7 buffer and updates hqx7_first and hqx7_last to delimit valid data.

Parameters
[in]hqx7_ptrpointer within hqx7_buf where reading should start
Returns
number of bytes read, 0 on end of file, -1 on error

◆ hqx_7tobin()

size_t hqx_7tobin ( char * outbuf,
size_t datalen )

Decode BinHex text data to binary.

This is called by hqx_header_read() for header data and by hqx_read() for fork data and CRC fields. It buffers decoded data so callers get the requested length unless end of file is reached.

Parameters
[out]outbufdestination buffer for decoded bytes
[in]datalennumber of decoded bytes requested
Returns
number of decoded bytes written to outbuf

◆ hqx_close()

int hqx_close ( int keepflag)

Close the active BinHex file.

This must be called before opening another file with hqx_open(). In write mode, KEEP finishes any remaining fork data, flushes the encoder, and writes the BinHex terminator. TRASH closes the output and removes the incomplete file.

Parameters
[in]keepflagKEEP to keep the output, TRASH to discard it
Returns
0 on success, -1 on error

◆ hqx_emit_6bit()

int hqx_emit_6bit ( unsigned char b)
static

◆ hqx_emit_byte()

int hqx_emit_byte ( unsigned char b)
static

◆ hqx_emit_data()

int hqx_emit_data ( const void * buf,
size_t len )
static

◆ hqx_finish_fork()

int hqx_finish_fork ( int fork)
static

◆ hqx_flush_6bit()

int hqx_flush_6bit ( void )
static

◆ hqx_header_read()

int hqx_header_read ( struct FHeader * fh)

Read and validate a BinHex header.

This is called by hqx_open() before any fork data is read. It decodes the header fields, initializes fork lengths and CRC state, and verifies the header CRC.

Parameters
[out]fhfile header to populate
Returns
0 on success, negative value on error

◆ hqx_header_write()

int hqx_header_write ( struct FHeader * fh)

Encode and write a BinHex header.

Parameters
[in]fhfile header to write
Returns
0 on success, -1 on error

◆ hqx_open()

int hqx_open ( char * hqxfile,
int flags,
struct FHeader * fh,
int options )

Open a BinHex file for reading or writing.

This must be called before other hqx operations. When opening for reading, it skips the preamble and reads the BinHex header. When opening for writing, it initializes output state and writes the BinHex preamble and header.

Parameters
[in]hqxfileinput file path, or - for standard input
[in]flagsopen flags; O_RDONLY selects read mode
[in,out]fhfile header to read or write
[in]optionsoutput options, such as OPTION_STDOUT
Returns
0 on success, -1 on error

◆ hqx_path()

const char * hqx_path ( void )

◆ hqx_put_char()

int hqx_put_char ( unsigned char c)
static

◆ hqx_read()

ssize_t hqx_read ( int fork,
char * buffer,
size_t length )

Read decoded data from a BinHex fork.

Call this until it returns zero for each fork. When no fork data remains, the stored fork CRC is read and compared with the calculated CRC before returning zero.

Note
hqx_read() must be called enough times to return zero, and no more than that, for each fork.
Parameters
[in]forkfork selector, DATA or RESOURCE
[out]bufferdestination buffer
[in]lengthmaximum number of bytes to read
Returns
number of bytes read, 0 when the fork is complete, -1 on error

◆ hqx_write()

ssize_t hqx_write ( int fork,
char * buffer,
size_t length )

◆ hqx_write_all()

int hqx_write_all ( const void * buf,
size_t len )
static

◆ skip_junk()

int skip_junk ( int line)

Skip non-BinHex text until encoded data is found.

This is called from hqx_open() to find the first valid BinHex line, and from hqx_7tobin() to find subsequent encoded lines.

Parameters
[in]lineFIRST for the first encoded line, OTHER thereafter
Returns
0 on success, -1 if valid encoded data is not found

Variable Documentation

◆ first_flag

int first_flag
static

◆ forkname

char* forkname[]
extern

◆ hqx

struct hqx_file_data hqx
static

◆ hqx7_buf

unsigned char hqx7_buf[8192]
static

◆ hqx7_first

unsigned char* hqx7_first
static

◆ hqx7_last

unsigned char* hqx7_last
static

◆ hqxchars

const unsigned char hqxchars[]
static
Initial value:
=
"!\"#$%&'()*+,-012345689@ABCDEFGHIJKLMNPQRSTUVXYZ[`abcdefhijklmpqr"

◆ hqxlookup

const unsigned char hqxlookup[]
static