Skip to content

C and C++ bindings

Use the native library to create CSRs, build and sign invoices, validate XML and call the ZATCA API. Headers and the shared library must come from the same release. Download them from the release assets.

Configure your runtime loader to find the shared library: .so on Linux, .dylib on macOS, or the DLL on Windows. Select your language for its include paths, return values and cleanup rules.

Include one header, check the shared status, and release returned owners. Passing NULL as the error argument is supported when only the status matters. The deliberately incomplete invoice below fails validation.

#include "fatoora.h"
#include <stdio.h>

int main(void) {
    FatooraError *error = NULL;
    bool valid = false;
    FatooraStatus status = fatoora_xml_validate("<Invoice></Invoice>", &valid, &error);
    if (status == FATOORA_OK) printf("valid: %s\n", valid ? "true" : "false");
    else fprintf(stderr, "error %d: %s\n", (int)status, fatoora_error_message(error));
    fatoora_error_free(error);
    return status == FATOORA_OK && valid ? 0 : 1;
}

String getters return owned char * values through output pointers; release them with fatoora_string_free(). Optional text returns NULL for absence and an allocated empty string for a present empty value. Decimal values are exact strings. Constructors and line items use named-field option structs. See the complete builder example and the ownership rules below.

Compile and link

Use matching headers and native libraries from the same release. Compile bindings/c/fatoora.c once alongside your application, include bindings/c, and link fatoora_ffi:

cc -std=c11 validate.c bindings/c/fatoora.c \
  -Ibindings/c -L/path/to/native-library -lfatoora_ffi -o validate

Release archives use include/fatoora.h, src/fatoora.c and include/generated/. Compile the archive's src/fatoora.c with -Iinclude and link the matching native library. If your application is C++, compile this adapter as C, then link the object into your application.

Values and ownership

Value Contract
Text input Borrowed NUL-terminated UTF-8 for the duration of the call
Optional text input NULL means absent; "" is present empty text
Decimal input Exact decimal string; no exponent or whitespace
Text output Owned char *; free with fatoora_string_free
Object output Owned opaque handle; free with its fatoora_<type>_free function
Byte output FatooraBytes owner; its data view borrows from that owner
Error Owned FatooraError; its message and details strings borrow from the error
Output arguments Cleared on entry; free any old value before reusing its slot
Enumerated input Typed C enum such as FatooraEnvironment, FatooraInvoiceTypeKind, FatooraVatCategory, FatooraAdjustmentKind or FatooraClearanceMode; a value that is not a declared enumerator returns FATOORA_INVALID_INPUT
Enumerated output Typed pointer such as FatooraVatCategory *out
Optional status output FatooraReportingStatus, FatooraClearanceStatus, FatooraValidationStatus or FatooraMessageType plus bool *has_value; *_UNKNOWN means present but not recognized by this version, and the _raw getter returns the exact text
Invoice flags Integer bitmask of FATOORA_FLAG_* (FatooraInvoiceFlag) values

Invoice, party, address and response getters return independent owned snapshots. They survive destruction of the source object. Optional owned output is NULL when absent. Freeing a null owner is safe. Passing NULL for the error-output slot discards detailed diagnostics but retains the returned status.

The facade validates enum arguments. The raw Diplomat headers under generated/ use enumerators such as Environment_NonProduction and VatCategory_Standard and do not validate them: passing a value that is not a declared enumerator is undefined behaviour. Prefer the facade from C.

Do not copy opaque owners by value or free them with the system allocator. FATOORA_OK from local invoice validation means a report was produced; check fatoora_zatca_validation_report_is_valid on that report.

C++ headers provide their own C-linkage declarations. Include them directly; no external extern "C" wrapper is needed. They return diplomat::result and manage objects with std::unique_ptr. Inspect is_ok(), then move the result's .ok() or .err() value to take ownership. Owners release their native objects automatically.

#include "fatoora/BindingError.hpp"
#include "fatoora/Xml.hpp"
#include <iostream>
#include <utility>

int main() {
    auto result = fatoora::Xml::validate("<Invoice></Invoice>");
    if (!result.is_ok()) {
        auto error = std::move(result).err().value();
        std::cerr << error->code() << ": " << error->message() << '\n';
        return 1;
    }
    return std::move(result).ok().value() ? 0 : 1;
}

Enums are scoped wrappers such as fatoora::Environment::NonProduction and fatoora::VatCategory::Standard; optional response statuses return std::optional<fatoora::ReportingStatus> (and likewise for the other status enums), with the exact wire text available from the *_raw() accessor as a std::optional<std::string>. fatoora::InvoiceFlag(fatoora::InvoiceFlag::Export).bit() returns a flag's bit for the uint8_t mask.

Fallible C++ text accessors return a result containing std::string; error text accessors return std::string directly. Owned strings survive destruction of their source objects. Byte spans still borrow from their Bytes owner.

Compile and link

Use C++17 or later and headers matching the native library release:

c++ -std=c++17 validate.cpp -Ibindings/cpp \
  -L/path/to/native-library -lfatoora_ffi -o validate_cpp

Text inputs are borrowed for the call. Decimal inputs and outputs use exact strings. Optional text is a std::optional<std::string>, except ValidationResponse::cleared_invoice_xml() and SignedInvoice::zatca_key_signature(), which fail when the value is absent: check cleared_invoice_xml_present() or zatca_key_signature_present() first. Returned invoice, party, address and response objects are independent snapshots. Keep a Bytes owner alive while using its byte span.

Consumption and safety

Builder input errors allow correction. build() consumes the builder's inner state even when final validation fails. Signing consumes a finalized invoice, and the consuming XML getter consumes a signed invoice. Subsequent operations on a consumed owner return an error; the owner remains safe to destroy.

Callers must provide valid pointers, preserve borrowed storage lifetimes, and synchronize mutable access, consumption, and destruction. Malformed bytes are validated only after the caller has supplied a valid byte view. Invalid pointers, double destruction, and concurrent conflicting access violate the native contract.

Fallible operations catch unwinding Rust panics and return Internal errors. Abort-mode panics and allocation aborts cannot be recovered this way. A panic can leave an operation consumed; discard the affected owner after an Internal failure. See errors for numeric codes and structured details.

Reference by operation