Zig 0.17.0-dev (Split by item)
This is an example of documentation generated by
ZigDoc , an alternative to Zig's built-in
Auto Doc feature. See also
examples in other modes/formats . The project being documented here (as the example) is the Zig library itself.
Zig › std/ › crypto/ › Certificate/ › Bundle.zig › addCertsFromFile
addCertsFromFile
Bundle.addCertsFromFile
pub fn addCertsFromFile (cb : *Bundle , gpa : Allocator , file_reader : *Io .File .Reader , now_sec : i64 ) AddCertsFromFileError !void
File
Code
pub fn addCertsFromFile (cb : *Bundle , gpa : Allocator , file_reader : *Io .File .Reader , now_sec : i64 ) AddCertsFromFileError !void {
const size = try file_reader .getSize ();
// This is possible by computing the decoded length and reserving the space
// for the decoded bytes first.
const decoded_size_upper_bound = size / 4 * 3 ;
const needed_capacity = std .math .cast (u32 , decoded_size_upper_bound + size ) orelse
return error .CertificateAuthorityBundleTooBig ;
try cb .bytes .ensureUnusedCapacity (gpa , needed_capacity );
const end_reserved : u32 = @intCast (cb .bytes .items .len + decoded_size_upper_bound );
const buffer = cb .bytes .allocatedSlice ()[end_reserved ..];
const end_index = file_reader .interface .readSliceShort (buffer ) catch |err | switch (err ) {
error .ReadFailed => return file_reader .err .?,
};
const encoded_bytes = buffer [0 ..end_index ];
const begin_marker = "-----BEGIN CERTIFICATE-----" ;
const end_marker = "-----END CERTIFICATE-----" ;
var start_index : usize = 0 ;
while (mem .findPos (u8 , encoded_bytes , start_index , begin_marker )) |begin_marker_start | {
const cert_start = begin_marker_start + begin_marker .len ;
const cert_end = mem .findPos (u8 , encoded_bytes , cert_start , end_marker ) orelse
return error .MissingEndCertificateMarker ;
start_index = cert_end + end_marker .len ;
const encoded_cert = mem .trim (u8 , encoded_bytes [cert_start ..cert_end ], " \t\r\n" );
const decoded_start : u32 = @intCast (cb .bytes .items .len );
const dest_buf = cb .bytes .allocatedSlice ()[decoded_start ..];
cb .bytes .items .len += try base64 .decode (dest_buf , encoded_cert );
try cb .parseCert (gpa , decoded_start , now_sec );
}
}