Access encoded data while encoding in order to compute checksum
Opened this issue · 2 comments
A .binarycookies file contains many pages of cookies, and the entire file contains a checksum near the end. The checksum is the sum of each page's checksum, which is the sum of every 4th byte in the page. I don't think it's possible to compute this with the current API, right?
I don't have a strong opinion about an ideal API for this, but if SequentialBinaryEncodingContainer.encode(_:)
returned the encoded data then I think that would at least provide the necessary data. In regard to the recent addition of the ability to encode arrays of BinaryEncodable, if the API returned the encoded data for the entire array I don't think that would work in this case, because the "sum every 4th byte" behaviour needed for this checksum starts at the beginning of each page (Hopefully that explanation is clear). This only means that for this file format each page would need to be encoded individually instead of encoding the entire array at once.
In the current implementation, each page encodes itself to Data during the encoding process in order to compute its checksum, so each Page is encoded twice over the course of encoding a file. I would imagine that for larger files the inefficiency of this would be more noticeable.
To clarify: is the request here to return the encoded Data from each of BinaryEncodingContainer
's encode
APIs?
E.g.
encode<T>(_ value: T) throws where T: BinaryEncodable -> Data
Yes I think that change would provide what's needed to compute the checksum.