The Util.Encoders package defines the Encoder and Decoder types which provide a mechanism to transform a stream from one format into another format. The basic encoder and decoder support base16, base32, base64, base64url and sha1. The following code extract will encode in base64:

C : constant Encoder := Util.Encoders.Create ("base64");
S : constant String := C.Encode ("Ada is great!");

and the next code extract will decode the base64:

D : constant Decoder := Util.Encoders.Create ("base64");
S : constant String := D.Decode ("QWRhIGlzIGdyZWF0IQ==");

To use the packages described here, use the following GNAT project:

with "utilada_sys";

URI Encoder and Decoder

The Util.Encoders.URI package provides operations to encode and decode using the URI percent encoding and decoding scheme. A string encoded using percent encoding as described in RFC 3986 is simply decoded as follows:

Decoded : constant String := Util.Encoders.URI.Decode ("%20%2F%3A");

To encode a string, one must choose the character set that must be encoded and then call the Encode function. The character set indicates those characters that must be percent encoded. Two character sets are provided,

  • HREF_STRICT defines a strict character set to encode all reserved characters as defined by RFC 3986. This is the default.
  • HREF_LOOSE defines a character set that does not encode the reserved characters such as -_.+!*'(),%#@?=;:/&$.
Encoded : constant String := Util.Encoders.URI.Encode (" /:");

Error Correction Code

The Util.Encoders.ECC package provides operations to support error correction codes. The error correction works on blocks of 256 or 512 bytes and can detect 2-bit errors and correct 1-bit error. The ECC uses only three additional bytes. The ECC algorithm implemented by this package is implemented by several NAND Flash memory. It can be used to increase the robustness of data to bit-tempering when the data is restored from an external storage (note that if the external storage has its own ECC correction, adding another software ECC correction will probably not help).

The ECC code is generated by using the Make procedure that gets a block of 256 or 512 bytes and produces the 3 bytes ECC code. The ECC code must be saved together with the data block.

Code : Util.Encoders.ECC.ECC_Code;
Util.Encoders.ECC.Make (Data, Code);

When reading the data block, you can verify and correct it by running again the Make procedure on the data block and then compare the current ECC code with the expected ECC code produced by the first call. The Correct function is then called with the data block, the expected ECC code that was saved with the data block and the computed ECC code.

New_Code : Util.Encoders.ECC.ECC_Code;
Util.Encoders.ECC.Make (Data, New_Code);
case Util.Encoders.ECC.Correct (Data, Expect_Code, New_Code) is
   when others => ...
end case;