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 standard library.
std › crypto › Certificate › Bundle › addCertsFromFile Public Function addCertsFromFile Doc Source Public Function std.crypto.Certificate.Bundle.addCertsFromFile
pub fn addCertsFromFile (cb : *Bundle , gpa : Allocator , file_reader : *Io .File .Reader , now_sec : i64 ) AddCertsFromFileError !void Params File crypto/Certificate/Bundle.zig: 254
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286 pub fn addCertsFromFile (cb : *Bundle , gpa : Allocator , file_reader : *Io .File .Reader , now_sec : i64 ) AddCertsFromFileError !void {
const size = try file_reader .getSize ();
// We borrow `bytes` as a temporary buffer for the base64-encoded data.
// 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 );
}
}