# Example Programs
Standalone example programs are in `synta-ffi/examples/c/`. Each example
compiles independently against libcsynta.
## Compiling the Examples
A `Makefile` in `synta-ffi/examples/c/` can build all examples at once:
```bash
cd synta-ffi/examples/c/
make SYNTA_INCLUDE=../../../include SYNTA_LIB=../../../target/release
```
Or compile a single example:
```bash
# With pkg-config
gcc synta-ffi/examples/c/decode_integer.c \
$(pkg-config --cflags --libs csynta) \
-o decode_integer
# Without pkg-config
gcc synta-ffi/examples/c/decode_integer.c \
-I./include \
-L./target/release -lcsynta -lpthread -ldl -lm \
-o decode_integer
```
## Running the Integration Tests
C integration tests are compiled and executed via the Rust test harness:
```bash
cargo test -p synta-ffi
```
The test suite in `synta-ffi/tests/` covers:
- Roundtrip encode/decode for all primitive types
- Error propagation from all decode functions
- Memory safety under Valgrind (zero leak policy)
- X.509 certificate parsing and cross-validation
- Running all C example programs as integration tests (`c_examples.rs`)
To run tests under Valgrind:
```bash
./contrib/ci/local-ci.sh --valgrind c-test
```
## 1. `decode_integer.c` — Decode a DER INTEGER
Decode a DER-encoded INTEGER and print its value using `synta_decode_integer`.
## 2. `encode_sequence.c` — Encode a SEQUENCE
Build a SEQUENCE with INTEGER and BOOLEAN fields, then encode to DER using the
encoder API.
## 3. `oid_usage.c` — OID helpers
Create, encode, and compare OIDs using the OID helper functions.
## 4. `parse_certificate.c` — Parse an X.509 certificate
Parse an X.509 certificate from DER bytes and print subject, issuer, serial
number, and validity period.
## 5. `openssl_migration_cert.c` — OpenSSL → csynta certificate migration
Port an OpenSSL-based certificate parser to csynta, showing the one-to-one
API mapping for common certificate fields.
## 6. `cms_signed_data.c` — CMS SignedData parsing
Parse a CMS SignedData structure and inspect the embedded certificates and
signer info.
## 7. `openssl_migration_cms.c` — OpenSSL → csynta CMS migration
Port an OpenSSL-based CMS parser to csynta, covering `ContentInfo`,
`SignedData`, and embedded certificate extraction.
## 8. `codegen_from_schema.c` — Code-generated decoder/encoder
Use a generated decoder/encoder pair produced by `synta-codegen` from a
simple ASN.1 schema.
## 9. `all_features_example.c` — All code-generation features
Comprehensive example covering every ASN.1 code-generation feature:
OPTIONAL/DEFAULT fields, tagged types, SEQUENCE OF, CHOICE, OID, and
BIT STRING.
## 10. `certificate_codegen_example.c` — X.509-style certificate via codegen
Encode and decode an X.509-style certificate using a fully generated
DER codec, without touching the hand-written PKI bindings.
## 11. `test_generated_code.c` — Generated code regression tests
Regression tests that verify generated code correctness: roundtrip
encode/decode for all primitive and constructed types.