/* * The FileMaker Viewer's reader: fmptools (Evan Miller, MIT) behind a few * plain C functions. The page copies a file in and opens it; the tables and * their fields come back as JSON, and each table's values as one buffer of * records: * * u32 row (1, 2, ... in file order; rows with no values are not counted) * u32 field index (the "index" of a field in the JSON) * u32 byte length, then the value as UTF-8 (no terminator) * * Repetitions of a repeating field are joined with the ASCII group separator * (0x1D), as fmptools and FileMaker's own exports do. * * fm_pool and fm_stream (below) read the pictures and files kept in the * container fields of .fmp12 files, from fmptools' own block and chunk * reader; the library itself is unchanged. */ #include #include #include #include #include #include "fmp.h" #include "fmp_internal.h" static uint8_t *in = NULL; static size_t in_len = 0; static fmp_file_t *file = NULL; static fmp_table_array_t *tables = NULL; /* ── A growing byte buffer (the JSON and the values) ───────────────────── */ typedef struct { char *p; size_t len, cap; int failed; } sbuf; static void sb_put(sbuf *b, const void *s, size_t n) { if (b->failed) return; /* Fail cleanly rather than wrap around when the size would not fit. */ if (n > SIZE_MAX - 1 - b->len) { b->failed = 1; return; } if (b->len + n + 1 > b->cap) { size_t need = b->len + n + 1; size_t cap = b->cap ? b->cap : 65536; while (cap < need) { if (cap > SIZE_MAX / 2) { cap = need; break; } cap *= 2; } char *p = (char *)realloc(b->p, cap); if (!p) { b->failed = 1; return; } b->p = p; b->cap = cap; } memcpy(b->p + b->len, s, n); b->len += n; b->p[b->len] = 0; } static void sb_str(sbuf *b, const char *s) { sb_put(b, s, strlen(s)); } static void sb_int(sbuf *b, long long v) { char t[32]; snprintf(t, sizeof t, "%lld", v); sb_str(b, t); } static void sb_u32(sbuf *b, uint32_t v) { sb_put(b, &v, 4); } /* wasm is little-endian */ static void sb_json(sbuf *b, const char *s) { sb_put(b, "\"", 1); for (const unsigned char *c = (const unsigned char *)(s ? s : ""); *c; c++) { char esc[8]; if (*c == '"') sb_put(b, "\\\"", 2); else if (*c == '\\') sb_put(b, "\\\\", 2); else if (*c < 0x20) { snprintf(esc, sizeof esc, "\\u%04x", *c); sb_put(b, esc, 6); } else sb_put(b, c, 1); } sb_put(b, "\"", 1); } static void sb_free(sbuf *b) { free(b->p); b->p = NULL; b->len = b->cap = 0; b->failed = 0; } static sbuf meta = {0}, out = {0}; /* ── Opening ───────────────────────────────────────────────────────────── */ EMSCRIPTEN_KEEPALIVE void fm_close(void) { if (tables) fmp_free_tables(tables); tables = NULL; if (file) fmp_close_file(file); file = NULL; free(in); in = NULL; in_len = 0; sb_free(&meta); sb_free(&out); } /** Room for a file of n bytes; the page copies it in, then calls fm_open. */ EMSCRIPTEN_KEEPALIVE uint8_t *fm_input(size_t n) { fm_close(); in = (uint8_t *)malloc(n ? n : 1); in_len = in ? n : 0; return in; } /** 0 when the file opened and its tables were listed, else fmptools' error code. */ EMSCRIPTEN_KEEPALIVE int fm_open(void) { fmp_error_t error = FMP_OK; if (!in) return FMP_ERROR_MALLOC; file = fmp_open_buffer(in, in_len, &error); if (!file) return error ? error : FMP_ERROR_OPEN; tables = fmp_list_tables(file, &error); if (!tables) return error ? error : FMP_ERROR_READ; return FMP_OK; } /** The file's version and its tables with their fields, as JSON. */ EMSCRIPTEN_KEEPALIVE const char *fm_meta(void) { sb_free(&meta); if (!file || !tables) return "{}"; sb_str(&meta, "{\"version\":"); sb_int(&meta, file->version_num); sb_str(&meta, ",\"creator\":"); sb_json(&meta, file->version_string); sb_str(&meta, ",\"date\":"); sb_json(&meta, file->version_date_string); sb_str(&meta, ",\"tables\":["); for (size_t t = 0; t < tables->count; t++) { fmp_table_t *table = &tables->tables[t]; fmp_error_t error = FMP_OK; fmp_column_array_t *columns = fmp_list_columns(file, table, &error); if (t) sb_str(&meta, ","); sb_str(&meta, "{\"index\":"); sb_int(&meta, table->index); sb_str(&meta, ",\"name\":"); sb_json(&meta, table->utf8_name); sb_str(&meta, ",\"error\":"); sb_int(&meta, error); sb_str(&meta, ",\"fields\":["); for (size_t c = 0; columns && c < columns->count; c++) { fmp_column_t *column = &columns->columns[c]; if (c) sb_str(&meta, ","); sb_str(&meta, "{\"index\":"); sb_int(&meta, column->index); sb_str(&meta, ",\"name\":"); sb_json(&meta, column->utf8_name); sb_str(&meta, ",\"type\":"); sb_int(&meta, column->type); sb_str(&meta, ",\"collation\":"); sb_int(&meta, column->collation); sb_str(&meta, "}"); } sb_str(&meta, "]}"); fmp_free_columns(columns); } sb_str(&meta, "]}"); return meta.failed ? "{}" : meta.p; } /* ── Values ────────────────────────────────────────────────────────────── */ static fmp_handler_status_t handle_value(int row, fmp_column_t *column, const char *value, void *ctx) { size_t n = strlen(value); sb_u32(&out, (uint32_t)row); sb_u32(&out, (uint32_t)column->index); sb_u32(&out, (uint32_t)n); sb_put(&out, value, n); return out.failed ? FMP_HANDLER_ABORT : FMP_HANDLER_OK; } /** * Reads the values of the t-th table listed (0-based) into the buffer that * fm_out and fm_out_length give. Returns fmptools' error code (0 when all * was read); after an error the buffer holds what was read before it. */ EMSCRIPTEN_KEEPALIVE int fm_read(int t) { sb_free(&out); if (!file || !tables || t < 0 || (size_t)t >= tables->count) return FMP_ERROR_READ; fmp_error_t error = fmp_read_values(file, &tables->tables[t], handle_value, NULL); if (out.failed) return FMP_ERROR_MALLOC; return error; } EMSCRIPTEN_KEEPALIVE const char *fm_out(void) { return out.p ? out.p : ""; } EMSCRIPTEN_KEEPALIVE size_t fm_out_length(void) { return out.len; } /** Frees the buffer once the page has copied it (a container file can be large), keeping the file open. */ EMSCRIPTEN_KEEPALIVE void fm_out_free(void) { sb_free(&out); } /* ── Container fields (.fmp12) ─────────────────────────────────────────── */ /* * A picture or file in a container field of a .fmp12 file is kept once, in * its table's object pool, at [128+table].[31].[5].[object]; the record holds * the object's number in hex followed by U+E000. An object has a directory of * the streams it holds (key 2: a count, then a 4-letter tag and a length for * each), its stored file name and path (long key "FNAM"), a few 4-byte values * (MAIN: the stream that is the file; SIZE: width and height) and, at * [...].[object].[tag], each stream: key 0 its length, then its bytes in * parts numbered from 1 (data segments, or a last short part as key n). * * fm_pool lists every table's pool in one pass, without the streams' bytes, * into the buffer fm_out gives, as records of seven u32 (kind, table, * object, tag, key, block, length) followed by `length` bytes for kinds 1, * 2, 3 and 5: * * 1 a key of the object (key = its number) * 2 a long key of the object (tag = the key's 4 letters, e.g. FNAM) * 3 a 4-letter value of the object (tag = MAIN, SIZE, FORK...) * 4 a part of a stream (tag, key = part number, block = the block it is in, * length = its size; no bytes follow) * 5 a stream's stated length (tag) * 6 a part of a FNAM stored as a stream (as 4, with its bytes) * * fm_stream then gives one stream's parts, reading only the blocks from the * first part's to the last's, as records of (u32 part number, u32 length, * bytes), so the page holds one file at a time. */ typedef struct { int block; const uint8_t *start, *end; uint32_t table, object, tag; } pool_ctx; /* * Whether n bytes at p lie inside the block being read. fmptools does not * bound every length it reads from a block (and process_blocks still hands * over the chunks of a block it could only partly parse), so nothing is * copied out unless it is inside the block. */ static int in_block(const pool_ctx *ctx, const uint8_t *p, size_t n) { return p && ctx->start && p >= ctx->start && p <= ctx->end && n <= (size_t)(ctx->end - p); } static uint32_t tag_of(const fmp_data_t *d) { if (!d || d->len != 4) return 0; return ((uint32_t)d->bytes[0] << 24) | ((uint32_t)d->bytes[1] << 16) | ((uint32_t)d->bytes[2] << 8) | d->bytes[3]; } static void pool_record(uint32_t kind, uint32_t table, uint32_t object, uint32_t tag, uint32_t key, uint32_t block, const void *bytes, uint32_t len, int with_bytes) { sb_u32(&out, kind); sb_u32(&out, table); sb_u32(&out, object); sb_u32(&out, tag); sb_u32(&out, key); sb_u32(&out, block); sb_u32(&out, len); if (with_bytes && len) sb_put(&out, bytes, len); } /** 1 when the chunk is in an object pool: sets the table and object, and the stream's tag (0 at the object's own level). */ static int pool_path(fmp_chunk_t *chunk, uint32_t *table, uint32_t *object, uint32_t *tag) { if (chunk->version_num < 7 || chunk->path_level < 4 || chunk->path_level > 5) return 0; uint64_t t = path_value(chunk, path_at(chunk, 0)); if (t < 128 || !path_is(chunk, path_at(chunk, 1), 31) || !path_is(chunk, path_at(chunk, 2), 5)) return 0; fmp_data_t *o = path_at(chunk, 3); /* Objects numbered 32,896 and up take three bytes, of which fmptools' path_value keeps only two in these files: refused rather than read as another object. */ if (!o || o->len > 2) return 0; *table = (uint32_t)(t - 128); *object = (uint32_t)path_value(chunk, o); *tag = 0; if (chunk->path_level == 5) { *tag = tag_of(path_at(chunk, 4)); if (!*tag) return 0; } return 1; } static int pool_block(fmp_block_t *block, void *v) { pool_ctx *ctx = (pool_ctx *)v; ctx->block = block->this_id; ctx->start = block->payload; ctx->end = block->payload + block->payload_len; return 1; } #define TAG_FNAM 0x464E414Du static chunk_status_t pool_chunk(fmp_chunk_t *chunk, void *v) { pool_ctx *ctx = (pool_ctx *)v; uint32_t table, object, tag; if (!pool_path(chunk, &table, &object, &tag)) return CHUNK_NEXT; if (!in_block(ctx, chunk->data.bytes, chunk->data.len)) return CHUNK_NEXT; if (!tag) { if (chunk->type == FMP_CHUNK_FIELD_REF_SIMPLE) { pool_record(1, table, object, 0, chunk->ref_simple, ctx->block, chunk->data.bytes, chunk->data.len, 1); } else if (chunk->type == FMP_CHUNK_FIELD_REF_LONG) { if (!in_block(ctx, chunk->ref_long.bytes, chunk->ref_long.len)) return CHUNK_NEXT; pool_record(2, table, object, tag_of(&chunk->ref_long), 0, ctx->block, chunk->data.bytes, chunk->data.len, 1); } else if (chunk->type == FMP_CHUNK_DATA_SIMPLE && chunk->code >= 0x19 && chunk->code <= 0x1D && chunk->data.len == 4) { /* fmptools reads the 4-letter key and steps over the value after it. */ size_t n = (chunk->code == 0x19) + 2 * (chunk->code - 0x19); const uint8_t *value = chunk->data.bytes + 4; if (in_block(ctx, value, n)) pool_record(3, table, object, tag_of(&chunk->data), 0, ctx->block, value, (uint32_t)n, 1); } } else if (chunk->type == FMP_CHUNK_FIELD_REF_SIMPLE && chunk->ref_simple == 0) { pool_record(5, table, object, tag, 0, ctx->block, chunk->data.bytes, chunk->data.len, 1); } else if ((chunk->type == FMP_CHUNK_FIELD_REF_SIMPLE && chunk->ref_simple >= 1) || (chunk->type == FMP_CHUNK_DATA_SEGMENT && chunk->segment_index >= 1)) { uint32_t part = chunk->type == FMP_CHUNK_DATA_SEGMENT ? chunk->segment_index : chunk->ref_simple; int named = tag == TAG_FNAM; pool_record(named ? 6 : 4, table, object, tag, part, ctx->block, chunk->data.bytes, chunk->data.len, named); } return out.failed ? CHUNK_ABORT : CHUNK_NEXT; } /** Lists every table's object pool (see above). Returns fmptools' error code; 0 when all was read. */ EMSCRIPTEN_KEEPALIVE int fm_pool(void) { sb_free(&out); if (!file) return FMP_ERROR_READ; pool_ctx ctx = {0}; fmp_error_t error = process_blocks(file, pool_block, pool_chunk, &ctx); if (out.failed) return FMP_ERROR_MALLOC; return error; } /* fmp.c's own, not in its headers. */ fmp_error_t process_chunk_chain(fmp_file_t *file, fmp_chunk_t *chunk, chunk_handler handle_chunk, void *user_ctx); void free_chunk_chain(fmp_block_t *block); static chunk_status_t stream_chunk(fmp_chunk_t *chunk, void *v) { pool_ctx *ctx = (pool_ctx *)v; uint32_t table, object, tag; if (!pool_path(chunk, &table, &object, &tag) || table != ctx->table || object != ctx->object || tag != ctx->tag) return CHUNK_NEXT; if (!in_block(ctx, chunk->data.bytes, chunk->data.len)) return CHUNK_NEXT; uint32_t part = 0; if (chunk->type == FMP_CHUNK_DATA_SEGMENT) part = chunk->segment_index; else if (chunk->type == FMP_CHUNK_FIELD_REF_SIMPLE) part = chunk->ref_simple; if (!part) return CHUNK_NEXT; sb_u32(&out, part); sb_u32(&out, (uint32_t)chunk->data.len); sb_put(&out, chunk->data.bytes, chunk->data.len); return out.failed ? CHUNK_ABORT : CHUNK_NEXT; } /** * The parts of one stream (table, object, tag as fm_pool lists them), read * from block `first` along the chain of blocks to block `last`. Returns * fmptools' error code; FMP_ERROR_BAD_SECTOR when the chain does not reach * `last`. */ EMSCRIPTEN_KEEPALIVE int fm_stream(uint32_t table, uint32_t object, uint32_t tag, int first, int last) { sb_free(&out); if (!file || first < 1 || last < 1 || (size_t)first > file->num_blocks || (size_t)last > file->num_blocks) return FMP_ERROR_READ; pool_ctx ctx = {0}; ctx.table = table; ctx.object = object; ctx.tag = tag; int next = first; for (size_t steps = 0; steps < file->num_blocks; steps++) { fmp_block_t *block = file->blocks[next - 1]; if (!block) return FMP_ERROR_BAD_SECTOR; fmp_error_t error = process_block(file, block); block->this_id = next; ctx.start = block->payload; ctx.end = block->payload + block->payload_len; if (error == FMP_OK) error = process_chunk_chain(file, block->chunk, stream_chunk, &ctx); free_chunk_chain(block); if (out.failed) return FMP_ERROR_MALLOC; if (error != FMP_OK) return error; if (next == last) return FMP_OK; next = block->next_id; if (next < 1 || (size_t)next > file->num_blocks) break; } return FMP_ERROR_BAD_SECTOR; }