BB2 C port
API reference: Blitz libraries in C and the shared code
Loading...
Searching...
No Matches
xtra.c File Reference
#include "xtra_int.h"

Macros

#define OFF_ICON   1
#define OFF_SW   2
#define OFF_BUF   30
#define OFF_RES   54
#define RES_SIZE   68
#define OFF_PATH   598
#define OFF_NAME   790
#define OFF_CLI   982
#define AB3_OFF_BUF   32
#define AB3_OFF_RES   56
#define AB3_OFF_PATH   600
#define AB3_OFF_NAME   792
#define AB3_OFF_CLI   984
#define AB3_CLITEXT   116

Functions

static void zero (void *p, size_t n)
static void copy (void *to, const void *from, size_t n)
static uint16_t get16 (const uint8_t *p)
static uint32_t get32 (const uint8_t *p)
static void put16 (uint8_t *p, uint16_t v)
static void put32 (uint8_t *p, uint32_t v)
static size_t fieldlen (const uint8_t *p, size_t n)
int xtra_fail (struct xtra_error *err, int code, int in, size_t offset, uint32_t line)
static int fail (struct xtra_error *err, int code, int in, size_t offset)
const char * xtra_strerror (int code)
 The message of an error code (English, without position).
void xtra_init (struct xtra *x)
 Initialise settings with the defaults.
void xtra_apply_vers0 (struct xtra *x)
 What blitz2's loadxtra does after reading a vers 0 file.
size_t xtra_text_cap (int field)
 The bytes of a text field in a Blitz 2 .xtra (with the 0): a text is shorter than this.
const uint8_t * xtra_get_text (const struct xtra *x, const struct xtra_base *b, int field, size_t *len)
 A text field.
int xtra_set_text (struct xtra *x, int field, const void *p, size_t len)
 Set a text field to the caller's bytes.
void xtra_set_maxima (struct xtra *x, const struct xtra_max *m, uint32_t n)
 The maxima from the caller's array instead of those of the .xtra/header.
void xtra_max_begin (struct xtra_iter *it, const struct xtra *x, const struct xtra_base *b)
 Start iterating the maxima of x.
int xtra_next_max (struct xtra_iter *it, struct xtra_max *m)
 The next maximum.
void xtra_take_blitz2 (struct xtra *to, const struct xtra *from)
 Take what a Blitz 2 .xtra holds from from into to.
int xtra_detect (const uint8_t *buf, size_t len, const char *path)
 The type of a source.
int xtra_has_companion (int type)
 Whether the settings of a source of this type are in NAME.xtra.
int xtra_companion_name (const char *path, char *out, size_t cap)
 The name of the companion of the source path: path + ".xtra" (as TED and blitz2 make it).
int xtra_is_ab3_xtra (const uint8_t *xtra, size_t len)
 Whether a companion .xtra has AmiBlitz3's binary format with 15 switch words (since AmiBlitz/AmiBlitz3@bdae3f9e, docs/formats.md 1.3) - not Blitz 2's 14.
static int read_blitz2 (struct xtra *x, const uint8_t *buf, size_t size, struct xtra_error *err)
static int read_bb22 (struct xtra *x, const uint8_t *buf, size_t size, struct xtra_error *err)
int xtra_load (struct xtra *x, int type, const uint8_t *src, size_t len, const uint8_t *xtra, size_t xlen, struct xtra_error *err)
 Load the settings of a source.
size_t xtra_blitz2_size (const struct xtra *x)
 The bytes xtra_write_blitz2 writes: the fixed part of x->vers and 4 per maximum with a library number.
static int put_text (const struct xtra *x, const struct xtra_base *b, int f, uint8_t *out, size_t cap)
int xtra_write_blitz2 (const struct xtra *x, const struct xtra_base *b, uint8_t *out, size_t cap, size_t *len, unsigned *skipped, struct xtra_error *err)
 Write x as Blitz 2 .xtra.

Macro Definition Documentation

◆ AB3_CLITEXT

#define AB3_CLITEXT   116

◆ AB3_OFF_BUF

#define AB3_OFF_BUF   32

◆ AB3_OFF_CLI

#define AB3_OFF_CLI   984

◆ AB3_OFF_NAME

#define AB3_OFF_NAME   792

◆ AB3_OFF_PATH

#define AB3_OFF_PATH   600

◆ AB3_OFF_RES

#define AB3_OFF_RES   56

◆ OFF_BUF

#define OFF_BUF   30

◆ OFF_CLI

#define OFF_CLI   982

◆ OFF_ICON

#define OFF_ICON   1

◆ OFF_NAME

#define OFF_NAME   790

◆ OFF_PATH

#define OFF_PATH   598

◆ OFF_RES

#define OFF_RES   54

◆ OFF_SW

#define OFF_SW   2

◆ RES_SIZE

#define RES_SIZE   68

Function Documentation

◆ copy()

void copy ( void * to,
const void * from,
size_t n )
static

◆ fail()

int fail ( struct xtra_error * err,
int code,
int in,
size_t offset )
static

◆ fieldlen()

size_t fieldlen ( const uint8_t * p,
size_t n )
static

◆ get16()

uint16_t get16 ( const uint8_t * p)
static

◆ get32()

uint32_t get32 ( const uint8_t * p)
static

◆ put16()

void put16 ( uint8_t * p,
uint16_t v )
static

◆ put32()

void put32 ( uint8_t * p,
uint32_t v )
static

◆ put_text()

int put_text ( const struct xtra * x,
const struct xtra_base * b,
int f,
uint8_t * out,
size_t cap )
static

◆ read_bb22()

int read_bb22 ( struct xtra * x,
const uint8_t * buf,
size_t size,
struct xtra_error * err )
static

◆ read_blitz2()

int read_blitz2 ( struct xtra * x,
const uint8_t * buf,
size_t size,
struct xtra_error * err )
static

◆ xtra_apply_vers0()

void xtra_apply_vers0 ( struct xtra * x)

What blitz2's loadxtra does after reading a vers 0 file.

Auto Run on, Interrupt/Overflow Checking from their defaults, Assembler Checking off (blitz2.s:15371). The loader leaves the file values alone.

Parameters
xthe settings

◆ xtra_blitz2_size()

size_t xtra_blitz2_size ( const struct xtra * x)

The bytes xtra_write_blitz2 writes: the fixed part of x->vers and 4 per maximum with a library number.

Parameters
xthe settings
Returns
the size

◆ xtra_companion_name()

int xtra_companion_name ( const char * path,
char * out,
size_t cap )

The name of the companion of the source path: path + ".xtra" (as TED and blitz2 make it).

Parameters
paththe source's file name
outthe name, 0-terminated
capthe size of out in bytes
Returns
0 or XTRA_E_SPACE

◆ xtra_detect()

int xtra_detect ( const uint8_t * buf,
size_t len,
const char * path )

The type of a source.

"BB22" at the start: XTRA_T_BB22; a $00 byte: XTRA_T_TED; first line "; XTRA": XTRA_T_AB3; other text: XTRA_T_AB3 if path ends in ".ab3" (any case, AmiBlitz3 reads every .ab3 so), else XTRA_T_TEXT. The caller may pass another type to xtra_load.

Parameters
bufthe source
lenits length
pathits file name (may be NULL)
Returns
an enum xtra_type

◆ xtra_fail()

int xtra_fail ( struct xtra_error * err,
int code,
int in,
size_t offset,
uint32_t line )

◆ xtra_get_text()

const uint8_t * xtra_get_text ( const struct xtra * x,
const struct xtra_base * b,
int field,
size_t * len )

A text field.

Parameters
xthe settings
bthe bases of the views (may be NULL)
fieldan enum xtra_field
lenout: the length
Returns
its bytes (not 0-terminated); "" for an empty one; NULL (and *len 0) for a field number out of range or a view whose base b does not give

◆ xtra_has_companion()

int xtra_has_companion ( int type)

Whether the settings of a source of this type are in NAME.xtra.

Parameters
typean enum xtra_type
Returns
1 for XTRA_T_TED, XTRA_T_BB22 and XTRA_T_TEXT, else 0

◆ xtra_init()

void xtra_init ( struct xtra * x)

Initialise settings with the defaults.

Blitz 2's defaults (options.src, the initial gadget flags), no residents, empty texts, no maxima, no editor part, AmiBlitz3's defaults for its own numbers; vers 1, type XTRA_T_TED, XTRA_SRC_NONE.

Parameters
xthe settings

◆ xtra_is_ab3_xtra()

int xtra_is_ab3_xtra ( const uint8_t * xtra,
size_t len )

Whether a companion .xtra has AmiBlitz3's binary format with 15 switch words (since AmiBlitz/AmiBlitz3@bdae3f9e, docs/formats.md 1.3) - not Blitz 2's 14.

The layout as AmiBlitz3's write_xtrafile writes it: vers 1, icon, 15 switch words, 6 buffer sizes at 32, 8 residents at 56, path and name of the executable at 600 and 792, CLI argument (116 bytes) and stack, version, builds at 984 (fixed part XTRA_AB3_FIXED), the {lib, max} pairs, 4 more bytes, maybe PED's XtraExtend. Recognised by: at least the fixed part + 4 bytes, a multiple of 4 (a Blitz 2 .xtra is 982/1110

  • 4n), vers 1, icon and switch words 0 or XTRA_ON, every buffer size below 16 MB (in the 14-word layout's place a buffer's low word would be the next one's high word), the text fields 0-terminated. An older AmiBlitz3 .xtra with 14 switch words is not recognised.
Parameters
xtrathe file, xtra[0..len) (NULL: none)
lenits length
Returns
1 or 0

◆ xtra_load()

int xtra_load ( struct xtra * x,
int type,
const uint8_t * src,
size_t len,
const uint8_t * xtra,
size_t xlen,
struct xtra_error * err )

Load the settings of a source.

x is initialised (xtra_init) and gets:

  • XTRA_T_TED, XTRA_T_TEXT: the .xtra (source XTRA_SRC_BLITZ2) or the defaults (XTRA_SRC_NONE); hdr 0
  • XTRA_T_BB22: the header's editor part, hdr 14/16; the .xtra
  • XTRA_T_AB3: the embedded header (XTRA_SRC_AB3_TEXT, its size in hdr) or AmiBlitz3's defaults (hdr 0)

The views point into the .xtra and into src[0..x->hdr) - the caller keeps both (struct xtra_base). Strict: a .xtra must be 982 + 4n (vers 0) or 1110 + 4n bytes (vers 1) with 0-terminated text fields; the AB3 header must be well-formed (docs/formats.md 7.4).

Parameters
xthe settings
typean enum xtra_type
srcthe source, src[0..len) (may be NULL for XTRA_T_TED/ XTRA_T_TEXT)
lenits length
xtraits companion .xtra, xtra[0..xlen) (NULL: none; ignored for XTRA_T_AB3)
xlenits length
errthe place of an error (may be NULL)
Returns
0 or an enum xtra_err; on an error x holds the defaults

◆ xtra_max_begin()

void xtra_max_begin ( struct xtra_iter * it,
const struct xtra * x,
const struct xtra_base * b )

Start iterating the maxima of x.

Parameters
itthe iterator
xthe settings
bthe bases of the views

◆ xtra_next_max()

int xtra_next_max ( struct xtra_iter * it,
struct xtra_max * m )

The next maximum.

Parameters
itthe iterator (xtra_max_begin)
mout: the maximum
Returns
1 with *m set, 0 at the end

◆ xtra_set_maxima()

void xtra_set_maxima ( struct xtra * x,
const struct xtra_max * m,
uint32_t n )

The maxima from the caller's array instead of those of the .xtra/header.

The AmiBlitz3 writer then replaces all "Max" lines (written in front of "; /XTRA").

Parameters
xthe settings
mthe maxima, m[0..n) (kept by the caller)
ntheir number; 0: none

◆ xtra_set_text()

int xtra_set_text ( struct xtra * x,
int field,
const void * p,
size_t len )

Set a text field to the caller's bytes.

Parameters
xthe settings
fieldan enum xtra_field
pthe text, p[0..len) - kept by the caller as long as x is used (a writer reads it from there)
lenits length
Returns
0, XTRA_E_FIELD, XTRA_E_LONG (len >= xtra_text_cap) or XTRA_E_TEXT (a 0, CR or LF)

◆ xtra_strerror()

const char * xtra_strerror ( int code)

The message of an error code (English, without position).

Parameters
codean enum xtra_err
Returns
the message; "unknown error" for another code

◆ xtra_take_blitz2()

void xtra_take_blitz2 ( struct xtra * to,
const struct xtra * from )

Take what a Blitz 2 .xtra holds from from into to.

vers, icon, switches, buffers, residents (texts, pointers), executable path and name, CLI argument - not the maxima, the editor part, AmiBlitz3's own fields. from's views keep pointing into its .xtra: the base used with to needs from's .xtra (e.g. an .ab3 header updated from a .xtra).

Parameters
tothe settings to change
fromthe settings to take from

◆ xtra_text_cap()

size_t xtra_text_cap ( int field)

The bytes of a text field in a Blitz 2 .xtra (with the 0): a text is shorter than this.

Parameters
fieldan enum xtra_field
Returns
the size; 0 for a field number out of range

◆ xtra_write_blitz2()

int xtra_write_blitz2 ( const struct xtra * x,
const struct xtra_base * b,
uint8_t * out,
size_t cap,
size_t * len,
unsigned * skipped,
struct xtra_error * err )

Write x as Blitz 2 .xtra.

vers as in x (0 without CLI block); a text from the .xtra as its whole field (the bytes after the 0 included: a loaded file gives the same bytes), others 0-terminated and padded with 0; the pairs of the maxima with XTRA_MAX_LIB in their order (the others skipped).

Parameters
xthe settings
bthe bases of the views
outthe output
capits size in bytes
lenout: the size written
skippedout: the maxima skipped (may be NULL)
errthe place of an error (may be NULL)
Returns
0 or XTRA_E_VERS, XTRA_E_SPACE, XTRA_E_LONG, XTRA_E_BASE

◆ zero()

void zero ( void * p,
size_t n )
static