Skip to content

Instantly share code, notes, and snippets.

@ihnorton
Last active December 14, 2015 15:09
Show Gist options
  • Save ihnorton/5106056 to your computer and use it in GitHub Desktop.
Save ihnorton/5106056 to your computer and use it in GitHub Desktop.
libsodium wrapper
```julia
julia> ENV["JULIAHOME"] = "/cmn/git/julia"
"/cmn/git/julia"
julia> clang_includes = map(x->joinpath(ENV["JULIAHOME"], x), [
"deps/llvm-3.2/build/Release/lib/clang/3.2/include",
"deps/llvm-3.2/include",
"deps/llvm-3.2/include",
"deps/llvm-3.2/build/include/",
"deps/llvm-3.2/include/"
]);
julia> push!(clang_includes, "/cmn/git/libsodium/src/libsodium/include/")
6-element ASCIIString Array:
"/cmn/git/julia/deps/llvm-3.2/build/Release/lib/clang/3.2/include"
"/cmn/git/julia/deps/llvm-3.2/include"
"/cmn/git/julia/deps/llvm-3.2/include"
"/cmn/git/julia/deps/llvm-3.2/build/include/"
"/cmn/git/julia/deps/llvm-3.2/include/"
"/cmn/git/libsodium/src/libsodium/include/"
julia> using Clang.wrap_c
julia> ush(th, hpath) = (match(r"sodium", hpath) != nothing)
julia> hprof(hpath) = "libsodium.jl"
julia> hprof(hpath) = "libsodium.jl"
julia> wc = wrap_c.WrapContext(pwd(), "libsodium_h.jl", clang_includes, ASCIIString[], ush, hlib, hprof)
WrapContext("/cmn/git/libsodium/src/libsodium/include","libsodium_h.jl",["/cmn/git/julia/deps/llvm-3.2/build/Release/lib/clang/3.2/include" … "/cmn/git/libsodium/src/libsodium/include/"],[],ush,hlib,hprof,Ptr{Void} @0x0000000002f10530,None,Set{ASCIIString}(),Dict{ASCIIString,IOStream}())
julia> wrap_c.wrap_c_headers(wc, ["sodium.h"])
```
# Julia wrapper for header: sodium.h
# Automatically generated using Clang.jl wrap_c, version 0.0.0
@c Int32 crypto_auth_hmacsha512256_ref (Ptr{:Uint8},Ptr{:Uint8},:Uint64,Ptr{:Uint8}) libsodium
@c Int32 crypto_auth_hmacsha512256_ref_verify (Ptr{:Uint8},Ptr{:Uint8},:Uint64,Ptr{:Uint8}) libsodium
@c Int32 crypto_auth_hmacsha256_ref (Ptr{:Uint8},Ptr{:Uint8},:Uint64,Ptr{:Uint8}) libsodium
@c Int32 crypto_auth_hmacsha256_ref_verify (Ptr{:Uint8},Ptr{:Uint8},:Uint64,Ptr{:Uint8}) libsodium
@c Int32 crypto_box_curve25519xsalsa20poly1305_ref (Ptr{:Uint8},Ptr{:Uint8},:Uint64,Ptr{:Uint8},Ptr{:Uint8},Ptr{:Uint8}) libsodium
@c Int32 crypto_box_curve25519xsalsa20poly1305_ref_open (Ptr{:Uint8},Ptr{:Uint8},:Uint64,Ptr{:Uint8},Ptr{:Uint8},Ptr{:Uint8}) libsodium
@c Int32 crypto_box_curve25519xsalsa20poly1305_ref_keypair (Ptr{:Uint8},Ptr{:Uint8}) libsodium
@c Int32 crypto_box_curve25519xsalsa20poly1305_ref_beforenm (Ptr{:Uint8},Ptr{:Uint8},Ptr{:Uint8}) libsodium
@c Int32 crypto_box_curve25519xsalsa20poly1305_ref_afternm (Ptr{:Uint8},Ptr{:Uint8},:Uint64,Ptr{:Uint8},Ptr{:Uint8}) libsodium
@c Int32 crypto_box_curve25519xsalsa20poly1305_ref_open_afternm (Ptr{:Uint8},Ptr{:Uint8},:Uint64,Ptr{:Uint8},Ptr{:Uint8}) libsodium
@c Int32 crypto_core_hsalsa20_ref2 (Ptr{:Uint8},Ptr{:Uint8},Ptr{:Uint8},Ptr{:Uint8}) libsodium
@c Int32 crypto_core_salsa20_ref (Ptr{:Uint8},Ptr{:Uint8},Ptr{:Uint8},Ptr{:Uint8}) libsodium
@c Int32 crypto_core_salsa2012_ref (Ptr{:Uint8},Ptr{:Uint8},Ptr{:Uint8},Ptr{:Uint8}) libsodium
@c Int32 crypto_core_salsa208_ref (Ptr{:Uint8},Ptr{:Uint8},Ptr{:Uint8},Ptr{:Uint8}) libsodium
@c Int32 crypto_hash_sha512_ref (Ptr{:Uint8},Ptr{:Uint8},:Uint64) libsodium
@c Int32 crypto_hash_sha256_ref (Ptr{:Uint8},Ptr{:Uint8},:Uint64) libsodium
@c Int32 crypto_hashblocks_sha256_ref (Ptr{:Uint8},Ptr{:Uint8},:Uint64) libsodium
@c Int32 crypto_hashblocks_sha512_ref (Ptr{:Uint8},Ptr{:Uint8},:Uint64) libsodium
@c Int32 crypto_onetimeauth_poly1305_ref (Ptr{:Uint8},Ptr{:Uint8},:Uint64,Ptr{:Uint8}) libsodium
@c Int32 crypto_onetimeauth_poly1305_ref_verify (Ptr{:Uint8},Ptr{:Uint8},:Uint64,Ptr{:Uint8}) libsodium
@c Int32 crypto_scalarmult_curve25519_ref (Ptr{:Uint8},Ptr{:Uint8},Ptr{:Uint8}) libsodium
@c Int32 crypto_scalarmult_curve25519_ref_base (Ptr{:Uint8},Ptr{:Uint8}) libsodium
@c Int32 crypto_secretbox_xsalsa20poly1305_ref (Ptr{:Uint8},Ptr{:Uint8},:Uint64,Ptr{:Uint8},Ptr{:Uint8}) libsodium
@c Int32 crypto_secretbox_xsalsa20poly1305_ref_open (Ptr{:Uint8},Ptr{:Uint8},:Uint64,Ptr{:Uint8},Ptr{:Uint8}) libsodium
@c Int32 crypto_sign_ed25519_ref (Ptr{:Uint8},Ptr{:Uint64},Ptr{:Uint8},:Uint64,Ptr{:Uint8}) libsodium
@c Int32 crypto_sign_ed25519_ref_open (Ptr{:Uint8},Ptr{:Uint64},Ptr{:Uint8},:Uint64,Ptr{:Uint8}) libsodium
@c Int32 crypto_sign_ed25519_ref_keypair (Ptr{:Uint8},Ptr{:Uint8}) libsodium
@c Int32 crypto_sign_ed25519_ref_seed_keypair (Ptr{:Uint8},Ptr{:Uint8},Ptr{:Uint8}) libsodium
@c Int32 crypto_sign_edwards25519sha512batch_ref (Ptr{:Uint8},Ptr{:Uint64},Ptr{:Uint8},:Uint64,Ptr{:Uint8}) libsodium
@c Int32 crypto_sign_edwards25519sha512batch_ref_open (Ptr{:Uint8},Ptr{:Uint64},Ptr{:Uint8},:Uint64,Ptr{:Uint8}) libsodium
@c Int32 crypto_sign_edwards25519sha512batch_ref_keypair (Ptr{:Uint8},Ptr{:Uint8}) libsodium
@c Int32 crypto_stream_xsalsa20_ref (Ptr{:Uint8},:Uint64,Ptr{:Uint8},Ptr{:Uint8}) libsodium
@c Int32 crypto_stream_xsalsa20_ref_xor (Ptr{:Uint8},Ptr{:Uint8},:Uint64,Ptr{:Uint8},Ptr{:Uint8}) libsodium
@c Int32 crypto_stream_xsalsa20_ref_beforenm (Ptr{:Uint8},Ptr{:Uint8}) libsodium
@c Int32 crypto_stream_xsalsa20_ref_afternm (Ptr{:Uint8},:Uint64,Ptr{:Uint8},Ptr{:Uint8}) libsodium
@c Int32 crypto_stream_xsalsa20_ref_xor_afternm (Ptr{:Uint8},Ptr{:Uint8},:Uint64,Ptr{:Uint8},Ptr{:Uint8}) libsodium
@c Int32 crypto_stream_aes128ctr_portable (Ptr{:Uint8},:Uint64,Ptr{:Uint8},Ptr{:Uint8}) libsodium
@c Int32 crypto_stream_aes128ctr_portable_xor (Ptr{:Uint8},Ptr{:Uint8},:Uint64,Ptr{:Uint8},Ptr{:Uint8}) libsodium
@c Int32 crypto_stream_aes128ctr_portable_beforenm (Ptr{:Uint8},Ptr{:Uint8}) libsodium
@c Int32 crypto_stream_aes128ctr_portable_afternm (Ptr{:Uint8},:Uint64,Ptr{:Uint8},Ptr{:Uint8}) libsodium
@c Int32 crypto_stream_aes128ctr_portable_xor_afternm (Ptr{:Uint8},Ptr{:Uint8},:Uint64,Ptr{:Uint8},Ptr{:Uint8}) libsodium
@c Int32 crypto_stream_salsa20_ref (Ptr{:Uint8},:Uint64,Ptr{:Uint8},Ptr{:Uint8}) libsodium
@c Int32 crypto_stream_salsa20_ref_xor (Ptr{:Uint8},Ptr{:Uint8},:Uint64,Ptr{:Uint8},Ptr{:Uint8}) libsodium
@c Int32 crypto_stream_salsa20_ref_beforenm (Ptr{:Uint8},Ptr{:Uint8}) libsodium
@c Int32 crypto_stream_salsa20_ref_afternm (Ptr{:Uint8},:Uint64,Ptr{:Uint8},Ptr{:Uint8}) libsodium
@c Int32 crypto_stream_salsa20_ref_xor_afternm (Ptr{:Uint8},Ptr{:Uint8},:Uint64,Ptr{:Uint8},Ptr{:Uint8}) libsodium
@c Int32 crypto_stream_salsa2012_ref (Ptr{:Uint8},:Uint64,Ptr{:Uint8},Ptr{:Uint8}) libsodium
@c Int32 crypto_stream_salsa2012_ref_xor (Ptr{:Uint8},Ptr{:Uint8},:Uint64,Ptr{:Uint8},Ptr{:Uint8}) libsodium
@c Int32 crypto_stream_salsa2012_ref_beforenm (Ptr{:Uint8},Ptr{:Uint8}) libsodium
@c Int32 crypto_stream_salsa2012_ref_afternm (Ptr{:Uint8},:Uint64,Ptr{:Uint8},Ptr{:Uint8}) libsodium
@c Int32 crypto_stream_salsa2012_ref_xor_afternm (Ptr{:Uint8},Ptr{:Uint8},:Uint64,Ptr{:Uint8},Ptr{:Uint8}) libsodium
@c Int32 crypto_stream_salsa208_ref (Ptr{:Uint8},:Uint64,Ptr{:Uint8},Ptr{:Uint8}) libsodium
@c Int32 crypto_stream_salsa208_ref_xor (Ptr{:Uint8},Ptr{:Uint8},:Uint64,Ptr{:Uint8},Ptr{:Uint8}) libsodium
@c Int32 crypto_stream_salsa208_ref_beforenm (Ptr{:Uint8},Ptr{:Uint8}) libsodium
@c Int32 crypto_stream_salsa208_ref_afternm (Ptr{:Uint8},:Uint64,Ptr{:Uint8},Ptr{:Uint8}) libsodium
@c Int32 crypto_stream_salsa208_ref_xor_afternm (Ptr{:Uint8},Ptr{:Uint8},:Uint64,Ptr{:Uint8},Ptr{:Uint8}) libsodium
@c Int32 crypto_verify_16_ref (Ptr{:Uint8},Ptr{:Uint8}) libsodium
@c Int32 crypto_verify_32_ref (Ptr{:Uint8},Ptr{:Uint8}) libsodium
@c Int32 randombytes_set_implementation (Ptr{:randombytes_implementation},) libsodium
@c None randombytes (Ptr{:Uint8},:Uint64) libsodium
@c Ptr{:Uint8} randombytes_implementation_name () libsodium
@c uint32_t randombytes_random () libsodium
@c None randombytes_stir () libsodium
@c uint32_t randombytes_uniform (:uint32_t,) libsodium
@c None randombytes_buf (Ptr{:None},:size_t) libsodium
@c Int32 randombytes_close () libsodium
@c Ptr{:Uint8} sodium_version_string () libsodium
@c Int32 sodium_version_major () libsodium
@c Int32 sodium_version_minor () libsodium
recurs_sym_type(ex::Any) =
(ex==None || typeof(ex)==Symbol || length(ex.args)==1) ? eval(ex) : Expr(ex.head, ex.args[1], recurs_sym_type(ex.args[2]))
macro c(ret_type, func, arg_types, lib)
local _arg_types = Expr(:tuple, [recurs_sym_type(a) for a in arg_types.args]...)
local _ret_type = recurs_sym_type(ret_type)
local _args_in = Any[ symbol(string('a',x)) for x in 1:length(_arg_types.args) ]
local _lib = eval(lib)
quote
$(esc(func))($(_args_in...)) = ccall( ($(string(func)), $(Expr(:quote, _lib)) ), $_ret_type, $_arg_types, $(_args_in...) )
end
end
macro ctypedef(fake_t,real_t)
real_t = recurs_sym_type(real_t)
quote
typealias $fake_t $real_t
end
end
libsodium = :libsodium
@ctypedef randombytes_implementation Void
### Fixups
typealias size_t Int32
typealias uint64_t Uint64
typealias int64_t Int64
typealias uint32_t Uint32
typealias int32_t Int32
typealias uint8_t Uint8
typealias int8_t Int8
typealias uint16_t Uint16
typealias int16_t Int16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment