With the change on #36895 :
libbitcoinkernel's witness stack element accessor becomes O(n) in the index of the witness element; using it to iterate a whole witness may therefore become O(n^2).
This adds btck_witness_stack_get_items which reads the stack in one O(n) pass and writes each item's pointer and size into two arrays passed to it. WitnessStackApi::GetItems is the C++ wrapper. The pointers are valid as long as the witness stack exists.
An earlier version passed each item to a callback from the bottom of the stack to the top. That order followed the serialization, which can only be read from the bottom up, more than how a stack should actually be read. This version lets the items be read from either end.