AdjustCRC.AdjustCRC
— ModuleThis module exports two functions, adjust_crc
and adjust_crc!
, which allow you to write 4 bytes to a file or array in order to adjust the 32-bit CRC checksum (either CRC32 or CRC32c) to equal any desired value.
This is useful, for example, to store the checksum of data within the data itself, or simply to set the checksum to be a hard-coded value like 0x01020304
for ease of later checks.
AdjustCRC.adjust_crc!
— Methodadjust_crc!(crc, a::AbstractVector{UInt8}, wantcrc::UInt32, fixpos::Integer)
Write 4 bytes to a[fixpos:fixpos+3]
so that crc(a)
becomes equal to wantcrc
. This is especially useful if you want to store the checksum of some data within the data itself, or simply to set the crc to an arbitrary predetermined value. Here, crc
is a function that computes a 32-bit CRC checksum: either crc32c
from the CRC32c
standard library or crc32
from the CRC32.jl package.
Note that the adjust_crc!
function is most efficient when fixpos
is close to the end of the array a
, and is slowest for fixpos
near the beginning. (Though in all cases the cost should scale linearly with length(a)
.) See also adjust_crc
to append similar padding bytes to the end of a file or I/O stream (which has the advantage of not requiring you to read the entire file into memory at once).
AdjustCRC.adjust_crc
— Methodadjust_crc(crc, filename::AbstractString, wantcrc::UInt32)
adjust_crc(crc, io::IO, wantcrc::UInt32)
Write 4 bytes of "padding" to the end of the the I/O stream io
(which must be seekable and read/write) or the file filename
, in order to cause the crc
checksum of the whole stream/file to equal wantcrc
. Here, crc
is a function that computes a 32-bit CRC checksum: either crc32c
from the CRC32c
standard library or crc32
from the CRC32.jl package.
(This is mainly useful if you want to store the checksum of the file within the file: simply set wantcrc
to be an arbitrary number, such as rand(UInt32)
, store it within the file as desired, and then call adjust_crc
to write padding bytes that force the checksum to match wantcrc
. Even more simply, you could force all of your files to have a checksum that matches a hard-coded value like 0x01020304
, in which case you don't need to store the checksum in the file itself.) See also adjust_crc!
to write similar padding bytes to an arbitrary position within an array.