PCRE2_SERIALIZE_DECODE(3)

Hi bestie πŸ’– you're on the PCRE2_SERIALIZE_DECODE(3) page from the Library Functions Manual, and we're about to make serialized regex data make sense. No cap.

✨ NAME ✨

PCRE2 - Perl-compatible regular expressions (revised API). πŸ¦‹

✨ SYNOPSIS ✨

       #include <pcre2.h>

       int32_t pcre2_serialize_decode(pcre2_code **codes,
         int32_t number_of_codes, const uint8_t *bytes,
         pcre2_general_context *gcontext);

πŸ’– DESCRIPTION πŸ’–

Okay so this function takes a serialized set of compiled patterns and decodes it back into a list of individual patterns. It's giving "unpack the regex besties." ✨ But plot twist: this is possible only on a host that is running the same version of PCRE2, with the same code unit width, and the host must also have the same endianness, pointer width and PCRE2_SIZE type. Highkey a matching-host situation, periodt.

The arguments for pcre2_serialize_decode() are:

  • codes β€” pointer to a vector where the list gets built
  • number_of_codes β€” number of slots in that vector
  • bytes β€” the serialized byte stream
  • gcontext β€” pointer to a general context or NULL

The bytes argument must point to a block of data that was originally created by pcre2_serialize_encode(), though it may have been saved on disc or elsewhere in the meantime. If there are more codes in the serialized data than slots in the list, only those compiled patterns that will fit are decoded. 🫢

The yield of the function is the number of decoded patterns, or one of the following negative error codes:

  • PCRE2_ERROR_BADDATA β€” number_of_codes is zero or less
  • PCRE2_ERROR_BADMAGIC β€” mismatch of id bytes in bytes
  • PCRE2_ERROR_BADMODE β€” mismatch of variable unit size or PCRE version
  • PCRE2_ERROR_NOMEMORY β€” memory allocation failed
  • PCRE2_ERROR_NULL β€” codes or bytes is NULL

PCRE2_ERROR_BADMAGIC may mean that the data is corrupt, or that it was compiled on a system with different endianness. πŸ’€ So lowkey vibe-check your endianness before you panic.

There is a complete description of the PCRE2 native API in the pcre2api page and a description of the serialization functions in the pcre2serialize page. Go read those if you want the full lore. πŸ“š


πŸ’– Cute little sign-off: this page is from PCRE2 10.47, dated 22 April 2022. Stay iconic, bestie! πŸ’…

πŸ“¦ where this page comes from