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

Data Structures

struct  bin_file_data

Macros

#define MACBINARY_PLAY_NICE_WITH_OTHERS
#define NOWAY   0
#define SURETHANG   1
#define HEADBUFSIZ   128
#define MACBIN_NAMELEN   63

Functions

static uint16_t macbin_u16_from_bytes (unsigned char high, unsigned char low)
static uint32_t macbin_u32_from_header (const unsigned char *header, size_t offset)
static uint16_t macbin_fdflags_from_header (const unsigned char *header, int revision)
static void macbin_fdflags_to_header (unsigned char *header, uint16_t fdflags)
static int macbin_ad_date_from_header (uint32_t mac_date, uint32_t *ad_date)
static int macbin_ad_date_to_header (uint32_t ad_date, uint32_t *mac_date)
static int macbin_discard_bytes (size_t length)
static int macbin_write_repeat (unsigned char value, size_t length)
static int macbin_skip_padding (void)
static int macbin_write_padding (void)
int bin_open (char *binfile, int flags, struct FHeader *fh, int options)
 Open a MacBinary file for reading or writing.
int bin_close (int keepflag)
 Close the active MacBinary file.
const char * bin_path (void)
ssize_t bin_read (int fork, char *buffer, size_t length)
 Read data from a MacBinary fork.
ssize_t bin_write (int fork, char *buffer, size_t length)
 Write data to a MacBinary fork.
int bin_header_read (struct FHeader *fh, int revision)
 Read a MacBinary header into a file header.
int bin_header_write (struct FHeader *fh)
 Write a file header as a MacBinary header.
int test_header (void)
 Test the input header for a supported MacBinary revision.

Variables

static struct bin_file_data bin
char * forkname []
static unsigned char head_buf [HEADBUFSIZ]

Macro Definition Documentation

◆ HEADBUFSIZ

#define HEADBUFSIZ   128

◆ MACBIN_NAMELEN

#define MACBIN_NAMELEN   63

◆ MACBINARY_PLAY_NICE_WITH_OTHERS

#define MACBINARY_PLAY_NICE_WITH_OTHERS

◆ NOWAY

#define NOWAY   0

◆ SURETHANG

#define SURETHANG   1

Function Documentation

◆ bin_close()

int bin_close ( int keepflag)

Close the active MacBinary file.

This must be called before opening another file with bin_open(). KEEP closes the file and preserves it. TRASH closes the file and removes incomplete output.

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

◆ bin_header_read()

int bin_header_read ( struct FHeader * fh,
int revision )

Read a MacBinary header into a file header.

This is called by bin_open() before any fork data is read. It validates the MacBinary revision, decodes header fields, and initializes fork lengths.

Parameters
[out]fhfile header to populate
[in]revisionMacBinary revision returned by test_header()
Returns
0 on success, -1 on error

◆ bin_header_write()

int bin_header_write ( struct FHeader * fh)

Write a file header as a MacBinary header.

This is called by bin_open() before any fork data is written. It encodes the file header as a MacBinary III header and initializes fork lengths.

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

◆ bin_open()

int bin_open ( char * binfile,
int flags,
struct FHeader * fh,
int options )

Open a MacBinary file for reading or writing.

This must be called before other MacBinary operations. When opening for reading, it validates and reads the MacBinary header. When opening for writing, it initializes output state and writes the header.

Parameters
[in]binfileinput 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

◆ bin_path()

const char * bin_path ( void )

◆ bin_read()

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

Read data from a MacBinary fork.

Call this until it returns zero for each fork. When the data fork is complete, it skips MacBinary padding before the resource fork begins.

Note
bin_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

◆ bin_write()

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

Write data to a MacBinary fork.

Data must be written in fork order. The resource fork cannot be written until the data fork is complete. Padding is written when each fork is finished.

Parameters
[in]forkfork selector, DATA or RESOURCE
[in]buffersource buffer
[in]lengthnumber of bytes to write
Returns
number of bytes written, -1 on error

◆ macbin_ad_date_from_header()

int macbin_ad_date_from_header ( uint32_t mac_date,
uint32_t * ad_date )
static

◆ macbin_ad_date_to_header()

int macbin_ad_date_to_header ( uint32_t ad_date,
uint32_t * mac_date )
static

◆ macbin_discard_bytes()

int macbin_discard_bytes ( size_t length)
static

◆ macbin_fdflags_from_header()

uint16_t macbin_fdflags_from_header ( const unsigned char * header,
int revision )
static

◆ macbin_fdflags_to_header()

void macbin_fdflags_to_header ( unsigned char * header,
uint16_t fdflags )
static

◆ macbin_skip_padding()

int macbin_skip_padding ( void )
static

◆ macbin_u16_from_bytes()

uint16_t macbin_u16_from_bytes ( unsigned char high,
unsigned char low )
static

◆ macbin_u32_from_header()

uint32_t macbin_u32_from_header ( const unsigned char * header,
size_t offset )
static

◆ macbin_write_padding()

int macbin_write_padding ( void )
static

◆ macbin_write_repeat()

int macbin_write_repeat ( unsigned char value,
size_t length )
static

◆ test_header()

int test_header ( void )

Test the input header for a supported MacBinary revision.

Reads the first 128 bytes and determines whether the file is MacBinary, MacBinary II, MacBinary III, or not a MacBinary file.

Note
Apple's MacBinary II files can have a non-zero value at byte 74, so the byte 74 check is not very useful.
Returns
1 for MacBinary, 2 for MacBinary II, 3 for MacBinary III, -1 if the header is invalid

Variable Documentation

◆ bin

struct bin_file_data bin
static

◆ forkname

char* forkname[]
extern

◆ head_buf

unsigned char head_buf[HEADBUFSIZ]
static