lib

package module
v0.83.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 12, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package lib provides Go bindings for the c2pa-rs Content Authenticity SDK.

The package exposes the C2PA reader, builder, signer, settings and context APIs through cgo so Go programs can read, create and sign C2PA manifests on any asset format that c2pa-rs supports (JPEG, PNG, TIFF, MP4, and others).

All operations are scoped to a Context. A context optionally carries Settings (trust list, verification policy, builder defaults, etc.). Create a context either with NewContext for defaults, or via NewContextBuilder when you need to attach settings:

ctx, err := lib.NewContext()
if err != nil { ... }
defer ctx.Close()

Reading manifests

Use ReaderFromFile (or NewReader with a stream) to inspect manifests embedded in or accompanying an asset:

r, err := lib.ReaderFromFile(ctx, "signed.jpg")
if err != nil { ... }
defer r.Close()
fmt.Println(r.Json())

Signing assets

Implement the Signer interface (or wrap your own signing callback) and drive Builder.Sign:

b, err := lib.BuilderFromJson(ctx, manifestJson)
if err != nil { ... }
defer b.Close()
manifest, err := b.Sign("in.jpg", "out.jpg", signer)

The Builder type mirrors the upstream API, including SetIntent, SetRemoteUrl, AddAction, AddResource, AddIngredient, and archive helpers.

Linking

This package links against the c2pa_c shared/static library built from the c2pa-rs c2pa_c_ffi crate. See the project README for build instructions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func C2paError

func C2paError() string

func CpaVersion

func CpaVersion() string

CpaVersion returns the version string from the c2pa library.

func GoSignerCallback

func GoSignerCallback(context C.uintptr_t, input *C.uint8_t, input_size C.uintptr_t, output *C.uint8_t, output_size C.uintptr_t) C.intptr_t

func StreamFlush

func StreamFlush(context C.uintptr_t) C.intptr_t

func StreamRead

func StreamRead(context C.uintptr_t, buffer *C.uint8_t, size C.intptr_t) C.intptr_t

func StreamSeek

func StreamSeek(context C.uintptr_t, offset C.intptr_t, mode C.C2paSeekMode) C.intptr_t

func StreamWrite

func StreamWrite(context C.uintptr_t, buffer *C.uint8_t, size C.intptr_t) C.intptr_t

Types

type Builder

type Builder struct {
	// contains filtered or unexported fields
}

Builder wraps a C C2paBuilder*. It holds the underlying C pointer and provides a place to attach methods that operate on the C Builder.

func BuilderFromArchive

func BuilderFromArchive(ctx *Context, file *os.File) (*Builder, error)

BuilderFromArchive creates a Builder from an archive previously produced by ToArchive, using the supplied Context.

func BuilderFromArchiveFile

func BuilderFromArchiveFile(ctx *Context, path string) (*Builder, error)

BuilderFromArchiveFile is a convenience wrapper around BuilderFromArchive.

func BuilderFromDefinition added in v0.83.1

func BuilderFromDefinition(ctx *Context, def *schema.ManifestDefinition) (*Builder, error)

BuilderFromDefinition creates a Builder from a typed ManifestDefinition. It marshals the definition to JSON and forwards it to BuilderFromJson.

func BuilderFromJson

func BuilderFromJson(ctx *Context, json string) (*Builder, error)

BuilderFromJson creates a Builder from the given JSON manifest definition using the supplied Context.

func NewBuilder

func NewBuilder(ctx *Context) (*Builder, error)

NewBuilder creates a new Builder from the given Context.

func (*Builder) AddAction

func (b *Builder) AddAction(actionJson string) error

AddAction adds an action assertion described by the given JSON string.

func (*Builder) AddActionTyped added in v0.83.1

func (b *Builder) AddActionTyped(action any) error

AddActionTyped marshals action to JSON and adds it as an action assertion.

func (*Builder) AddIngredientFromFile

func (b *Builder) AddIngredientFromFile(ingredientJson string, path string) error

AddIngredientFromFile is a convenience wrapper that opens path and adds it as an ingredient, deriving the format from the file extension.

func (*Builder) AddIngredientFromStream

func (b *Builder) AddIngredientFromStream(ingredientJson string, format string, file *os.File) error

AddIngredientFromStream adds an ingredient described by ingredientJson, read from file with the given format (mime type or file extension).

func (*Builder) AddResource

func (b *Builder) AddResource(uri string, file *os.File) error

AddResource adds a resource read from file under the given URI identifier.

func (*Builder) AddResourceFromFile

func (b *Builder) AddResourceFromFile(uri string, path string) error

AddResourceFromFile is a convenience wrapper that opens path and adds it as a resource.

func (*Builder) Close

func (b *Builder) Close()

func (*Builder) SetBasePath

func (b *Builder) SetBasePath(path string) error

SetBasePath sets the directory used to resolve resources not found in memory.

func (*Builder) SetIntent

func (b *Builder) SetIntent(intent BuilderIntent, digitalSourceType DigitalSourceType) error

SetIntent sets the builder intent. digitalSourceType is required for IntentCreate and ignored for other intents.

func (*Builder) SetNoEmbed

func (b *Builder) SetNoEmbed()

func (*Builder) SetRemoteUrl

func (b *Builder) SetRemoteUrl(url string) error

SetRemoteUrl sets the remote URL that will be embedded into the asset on signing.

func (*Builder) Sign

func (b *Builder) Sign(input_file string, output_file string, signer Signer) ([]byte, error)

func (*Builder) ToArchive

func (b *Builder) ToArchive(file *os.File) error

ToArchive writes a builder archive to the given file.

func (*Builder) ToArchiveFile

func (b *Builder) ToArchiveFile(path string) error

ToArchiveFile is a convenience wrapper that creates path and writes the builder archive to it.

type BuilderIntent

type BuilderIntent C.C2paBuilderIntent

BuilderIntent corresponds to C2paBuilderIntent.

const (
	IntentCreate BuilderIntent = C.Create
	IntentEdit   BuilderIntent = C.Edit
	IntentUpdate BuilderIntent = C.Update
)

type Context

type Context struct {
	// contains filtered or unexported fields
}

Context wraps an immutable C2paContext*. A Context is shareable and may be used to create multiple Reader and Builder instances.

func NewContext

func NewContext() (*Context, error)

NewContext creates a new immutable Context with default settings.

func (*Context) Cancel

func (c *Context) Cancel() error

Cancel requests cancellation of any in-progress operation on this context. Thread-safe.

func (*Context) Close

func (c *Context) Close()

Close releases the underlying C context. Safe to call once; subsequent calls are no-ops.

func (*Context) Ptr

func (c *Context) Ptr() *C.C2paContext

Ptr returns the underlying C pointer. Use carefully; callers must not keep the pointer past the lifetime of the Context.

type ContextBuilder

type ContextBuilder struct {
	// contains filtered or unexported fields
}

ContextBuilder wraps a C2paContextBuilder*. It is consumed by Build().

func NewContextBuilder

func NewContextBuilder() (*ContextBuilder, error)

NewContextBuilder creates a new context builder with default settings.

func (*ContextBuilder) Build

func (b *ContextBuilder) Build() (*Context, error)

Build consumes the builder and returns an immutable Context.

func (*ContextBuilder) Close

func (b *ContextBuilder) Close()

Close releases the underlying builder if it has not been consumed by Build.

func (*ContextBuilder) SetSettings

func (b *ContextBuilder) SetSettings(settings *Settings) error

SetSettings configures the builder with the given settings. Settings are cloned internally; the caller retains ownership and should still Close() it.

func (*ContextBuilder) SetSigner

func (b *ContextBuilder) SetSigner(signer *SignerAdapter) error

SetSigner attaches a signer to the context. The builder takes ownership of the underlying C signer; the SignerAdapter's pointer is cleared so its Close() will not double-free it. The Go-side handle is still released when the SignerAdapter is closed.

type DigitalSourceType

type DigitalSourceType C.C2paDigitalSourceType

DigitalSourceType corresponds to C2paDigitalSourceType.

const (
	SourceEmpty                                DigitalSourceType = C.Empty
	SourceTrainedAlgorithmicData               DigitalSourceType = C.TrainedAlgorithmicData
	SourceDigitalCapture                       DigitalSourceType = C.DigitalCapture
	SourceComputationalCapture                 DigitalSourceType = C.ComputationalCapture
	SourceNegativeFilm                         DigitalSourceType = C.NegativeFilm
	SourcePositiveFilm                         DigitalSourceType = C.PositiveFilm
	SourcePrint                                DigitalSourceType = C.Print
	SourceHumanEdits                           DigitalSourceType = C.HumanEdits
	SourceCompositeWithTrainedAlgorithmicMedia DigitalSourceType = C.CompositeWithTrainedAlgorithmicMedia
	SourceAlgorithmicallyEnhanced              DigitalSourceType = C.AlgorithmicallyEnhanced
	SourceDigitalCreation                      DigitalSourceType = C.DigitalCreation
	SourceDataDrivenMedia                      DigitalSourceType = C.DataDrivenMedia
	SourceTrainedAlgorithmicMedia              DigitalSourceType = C.TrainedAlgorithmicMedia
	SourceAlgorithmicMedia                     DigitalSourceType = C.AlgorithmicMedia
	SourceScreenCapture                        DigitalSourceType = C.ScreenCapture
	SourceVirtualRecording                     DigitalSourceType = C.VirtualRecording
	SourceComposite                            DigitalSourceType = C.Composite
	SourceCompositeCapture                     DigitalSourceType = C.CompositeCapture
	SourceCompositeSynthetic                   DigitalSourceType = C.CompositeSynthetic
)

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

Reader wraps a C C2paReader*. It holds the underlying C pointer and provides a place to attach methods that operate on the C reader.

func NewReader

func NewReader(ctx *Context) (*Reader, error)

NewReader creates a new Reader from the given Context. The reader must be configured with a stream (e.g. via ReaderFromFile or by reusing the Context with another helper) before it can be used.

func ReaderFromFile

func ReaderFromFile(ctx *Context, path string) (*Reader, error)

ReaderFromFile creates a Reader by opening the given file path using the supplied Context. Returns an error if the reader could not be created.

func (*Reader) Close

func (r *Reader) Close()

func (*Reader) DetailedJson

func (r *Reader) DetailedJson() string

DetailedJson returns a detailed JSON description of the manifest store.

func (*Reader) DetailedManifest added in v0.83.1

func (r *Reader) DetailedManifest() (*schema.ManifestStore, error)

DetailedManifest returns the detailed manifest store parsed into the typed schema.ManifestStore representation.

func (*Reader) IsEmbedded

func (r *Reader) IsEmbedded() bool

IsEmbedded reports whether the reader was created from an embedded manifest.

func (*Reader) Json

func (r *Reader) Json() string

func (*Reader) Manifest added in v0.83.1

func (r *Reader) Manifest() (*schema.ManifestStore, error)

Manifest returns the manifest store parsed into the typed schema.ManifestStore representation.

func (*Reader) Ptr

func (r *Reader) Ptr() *C.C2paReader

Ptr returns the underlying C pointer. Use carefully; callers must not keep the pointer past the lifetime of the Reader or the underlying C resource.

func (*Reader) RemoteUrl

func (r *Reader) RemoteUrl() string

RemoteUrl returns the remote URL the manifest was obtained from, or an empty string if the manifest was not remote.

func (*Reader) ResourceToFile

func (r *Reader) ResourceToFile(uri string, path string) (int64, error)

ResourceToFile writes the resource identified by uri to the given path.

func (*Reader) ResourceToStream

func (r *Reader) ResourceToStream(uri string, file *os.File) (int64, error)

ResourceToStream writes the resource identified by uri to file and returns the number of bytes written.

type Settings

type Settings struct {
	// contains filtered or unexported fields
}

Settings wraps a C2paSettings*. It is used to configure a ContextBuilder via ContextBuilder.SetSettings.

func NewSettings

func NewSettings() (*Settings, error)

NewSettings creates a new Settings with defaults.

func (*Settings) Close

func (s *Settings) Close()

Close releases the underlying C settings. Safe to call multiple times.

func (*Settings) SetValue

func (s *Settings) SetValue(path, jsonValue string) error

SetValue sets a single configuration value using dot notation. The value must be a JSON-encoded scalar or array (e.g. "true", "42", "\"ps256\"").

func (*Settings) UpdateFrom added in v0.83.1

func (s *Settings) UpdateFrom(settings *schema.Settings) error

UpdateFrom applies a typed schema.Settings value by marshaling it to JSON and calling UpdateFromString.

func (*Settings) UpdateFromString

func (s *Settings) UpdateFromString(content, format string) error

UpdateFromString loads settings from a JSON or TOML string. The format argument must be "json" or "toml".

type Signer

type Signer interface {
	Sign(input []byte, output []byte) (int, error)
	Alg() SigningAlg
	TimeStampUrl() string
	Certificates() string
}

type SignerAdapter

type SignerAdapter struct {
	// contains filtered or unexported fields
}

func NewSigner

func NewSigner(signer Signer) (*SignerAdapter, error)

func (*SignerAdapter) Close

func (s *SignerAdapter) Close()

func (*SignerAdapter) Sign

func (s *SignerAdapter) Sign(input []byte, output []byte) (int, error)

type SigningAlg

type SigningAlg C.C2paSigningAlg
const (
	SigningAlgPs256       SigningAlg = C.Ps256
	SigningAlgPs384       SigningAlg = C.Ps384
	SigningAlgPs512       SigningAlg = C.Ps512
	SigningAlgEs256       SigningAlg = C.Es256
	SigningAlgEs384       SigningAlg = C.Es384
	SigningAlgEs512       SigningAlg = C.Es512
	C2paSigningAlgEd25519 SigningAlg = C.Ed25519
)

type Stream

type Stream struct {
	// contains filtered or unexported fields
}

func NewStream

func NewStream(file *os.File) (*Stream, error)

NewStream creates a new Stream.

func (*Stream) Close

func (s *Stream) Close()

func (*Stream) Ptr

func (s *Stream) Ptr() *C.C2paStream

Ptr returns the underlying C pointer for the stream.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL