<?xml version='1.0'?> <!--*-nxml-*-->
<!DOCTYPE refentry PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
  "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
<!-- SPDX-License-Identifier: LGPL-2.1-or-later -->

<refentry id="sd-id128"
  xmlns:xi="http://www.w3.org/2001/XInclude">

  <refentryinfo>
    <title>sd-id128</title>
    <productname>systemd</productname>
  </refentryinfo>

  <refmeta>
    <refentrytitle>sd-id128</refentrytitle>
    <manvolnum>3</manvolnum>
  </refmeta>

  <refnamediv>
    <refname>sd-id128</refname>
    <refname>SD_ID128_ALLF</refname>
    <refname>SD_ID128_CONST_STR</refname>
    <refname>SD_ID128_FORMAT_STR</refname>
    <refname>SD_ID128_FORMAT_VAL</refname>
    <refname>SD_ID128_MAKE</refname>
    <refname>SD_ID128_MAKE_STR</refname>
    <refname>SD_ID128_MAKE_UUID_STR</refname>
    <refname>SD_ID128_NULL</refname>
    <refname>SD_ID128_UUID_FORMAT_STR</refname>
    <refname>sd_id128_equal</refname>
    <refname>sd_id128_in_set</refname>
    <refname>sd_id128_in_set_sentinel</refname>
    <refname>sd_id128_in_setv</refname>
    <refname>sd_id128_is_allf</refname>
    <refname>sd_id128_is_null</refname>
    <refname>sd_id128_t</refname>
    <refpurpose>APIs for processing 128-bit IDs</refpurpose>
  </refnamediv>

  <refsynopsisdiv>
    <funcsynopsis>
      <funcsynopsisinfo>#include &lt;systemd/sd-id128.h&gt;</funcsynopsisinfo>
    </funcsynopsis>

    <cmdsynopsis>
      <command>pkg-config --cflags --libs libsystemd</command>
    </cmdsynopsis>

  </refsynopsisdiv>

  <refsect1>
    <title>Description</title>

    <para><filename>sd-id128.h</filename> provides APIs to process and generate 128-bit ID values. The
    128-bit ID values processed and generated by these APIs are a generalization of OSF UUIDs as defined by
    <ulink url="https://tools.ietf.org/html/rfc4122">RFC 4122</ulink> but use a simpler string format. These
    functions impose no structure on the used IDs, much unlike OSF UUIDs or Microsoft GUIDs, but are mostly
    compatible with those types of IDs.
    </para>

    <para>See
    <citerefentry><refentrytitle>sd_id128_to_string</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
    <citerefentry><refentrytitle>sd_id128_randomize</refentrytitle><manvolnum>3</manvolnum></citerefentry>
    and
    <citerefentry><refentrytitle>sd_id128_get_machine</refentrytitle><manvolnum>3</manvolnum></citerefentry>
    for more information about the implemented functions.</para>

    <para>A 128-bit ID is implemented as the following
    union type:</para>

    <programlisting>typedef union sd_id128 {
  uint8_t bytes[16];
  uint64_t qwords[2];
} sd_id128_t;</programlisting>

    <para>This union type allows accessing the 128-bit ID as 16
    separate bytes or two 64-bit words. It is generally safer to
    access the ID components by their 8-bit array to avoid endianness
    issues. This union is intended to be passed call-by-value (as
    opposed to call-by-reference) and may be directly manipulated by
    clients.</para>

    <para>A couple of macros are defined to denote and decode 128-bit
    IDs:</para>

    <para><function>SD_ID128_MAKE()</function> may be used to denote a
    constant 128-bit ID in source code. A commonly used idiom is to
    assign a name to a 128-bit ID using this macro:</para>

    <programlisting>#define SD_MESSAGE_COREDUMP SD_ID128_MAKE(fc,2e,22,bc,6e,e6,47,b6,b9,07,29,ab,34,a2,50,b1)</programlisting>

    <para><constant>SD_ID128_NULL</constant> may be used to refer to the 128-bit ID consisting of only
    <constant>NUL</constant> bytes (i.e. all bits off).</para>

    <para><constant>SD_ID128_ALLF</constant> may be used to refer to the 128-bit ID consisting of only
    <constant>0xFF</constant> bytes (i.e. all bits on).</para>

    <para><function>SD_ID128_MAKE_STR()</function> is similar to <function>SD_ID128_MAKE()</function>, but creates a
    <type>const char*</type> expression that can be conveniently used in message formats and such:</para>

    <programlisting>#include &lt;stdio.h&gt;
#define SD_MESSAGE_COREDUMP_STR SD_ID128_MAKE_STR(fc,2e,22,bc,6e,e6,47,b6,b9,07,29,ab,34,a2,50,b1)

int main(int argc, char **argv) {
  puts("Match for coredumps: MESSAGE_ID=" SD_MESSAGE_COREDUMP_STR);
}</programlisting>

    <para><function>SD_ID128_CONST_STR()</function> may be used to
    convert constant 128-bit IDs into constant strings for output. The
    following example code will output the string
    "fc2e22bc6ee647b6b90729ab34a250b1":</para>
    <programlisting>int main(int argc, char *argv[]) {
  puts("Match for coredumps: %s", SD_ID128_CONST_STR(SD_MESSAGE_COREDUMP));
}</programlisting>

    <para><constant>SD_ID128_FORMAT_STR</constant> and <function>SD_ID128_FORMAT_VAL()</function> may
    be used to format a 128-bit ID in a
    <citerefentry project='man-pages'><refentrytitle>printf</refentrytitle><manvolnum>3</manvolnum></citerefentry>
    format string, as shown in the following example:</para>

    <programlisting>int main(int argc, char *argv[]) {
  sd_id128_t id;
  id = SD_ID128_MAKE(ee,89,be,71,bd,6e,43,d6,91,e6,c5,5d,eb,03,02,07);
  printf("The ID encoded in this C file is " SD_ID128_FORMAT_STR ".\n", SD_ID128_FORMAT_VAL(id));
  return 0;
}</programlisting>

    <para><constant>SD_ID128_UUID_FORMAT_STR</constant> and <function>SD_ID128_MAKE_UUID_STR()</function>
    are similar to
    <constant>SD_ID128_FORMAT_STR</constant> and <function>SD_ID128_MAKE_STR()</function>,
    but include separating hyphens to conform to the
    "<ulink url="https://en.wikipedia.org/wiki/Universally_unique_identifier#Format">canonical representation</ulink>".
    They format the string based on <ulink
    url="https://tools.ietf.org/html/rfc4122">RFC4122</ulink> Variant 1 rules, i.e. converting from Big
    Endian byte order. This matches behaviour of most other Linux userspace infrastructure. It's probably
    best to avoid UUIDs of other variants, in order to avoid unnecessary ambiguities. All 128-bit IDs
    generated by the sd-id128 APIs strictly conform to Variant 1 Version 4 UUIDs, as per RFC 4122.</para>

    <para>Use <function>sd_id128_equal()</function> to compare two 128-bit IDs:</para>

    <programlisting>int main(int argc, char *argv[]) {
  sd_id128_t a, b, c;
  a = SD_ID128_MAKE(ee,89,be,71,bd,6e,43,d6,91,e6,c5,5d,eb,03,02,07);
  b = SD_ID128_MAKE(f2,28,88,9c,5f,09,44,15,9d,d7,04,77,58,cb,e7,3e);
  c = a;
  assert(sd_id128_equal(a, c));
  assert(!sd_id128_equal(a, b));
  return 0;
}</programlisting>

    <para>Use <function>sd_id128_is_null()</function> to check if an 128-bit ID consists of only
    <constant>NUL</constant> bytes:</para>

    <programlisting>assert(sd_id128_is_null(SD_ID128_NULL));</programlisting>

    <para>Similarly, use <function>sd_id128_is_allf()</function> to check if an 128-bit ID consists of only
    <constant>0xFF</constant> bytes (all bits on):</para>

    <programlisting>assert(sd_id128_is_allf(SD_ID128_ALLF));</programlisting>

    <para>For convenience, <function>sd_id128_in_set()</function> takes a list of IDs and
    returns true if any are equal to the first argument:</para>

    <programlisting>int main(int argc, char *argv[]) {
  sd_id12_t a = SD_ID128_MAKE(ee,89,be,71,bd,6e,43,d6,91,e6,c5,5d,eb,03,02,07);
  assert(sd_id128_in_set(a, a));
  assert(sd_id128_in_set(a, a, a));
  assert(!sd_id128_in_set(a));
  assert(!sd_id128_in_set(a,
                          SD_ID128_MAKE(f2,28,88,9c,5f,09,44,15,9d,d7,04,77,58,cb,e7,3e)
                          SD_ID128_MAKE(2f,88,28,5f,9c,44,09,9d,d7,15,77,04,bc,85,7e,e3)
                          SD_ID128_ALLF));
  return 0;
}
</programlisting>

    <para><function>sd_id128_in_set()</function> is defined as a macro over
    <function>sd_id128_in_set_sentinel()</function>, adding the <constant>SD_ID128_NULL</constant>
    sentinel. Since <function>sd_id128_in_set_sentinel()</function> uses <constant>SD_ID128_NULL</constant>
    as the sentinel, <constant>SD_ID128_NULL</constant> cannot be otherwise placed in the argument list.
    </para>

    <para><function>sd_id128_in_setv()</function> is similar to
    <function>sd_id128_in_set_sentinel()</function>, but takes a <structname>struct varargs</structname>
    argument.</para>

    <para>Note that new, randomized IDs may be generated with
    <citerefentry><refentrytitle>systemd-id128</refentrytitle><manvolnum>1</manvolnum></citerefentry>'s
    <command>new</command> command.</para>
  </refsect1>

  <xi:include href="libsystemd-pkgconfig.xml" />

  <refsect1>
    <title>See Also</title>
    <para>
      <citerefentry><refentrytitle>systemd</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
      <citerefentry><refentrytitle>sd_id128_to_string</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
      <citerefentry><refentrytitle>sd_id128_randomize</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
      <citerefentry><refentrytitle>sd_id128_get_machine</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
      <citerefentry project='man-pages'><refentrytitle>printf</refentrytitle><manvolnum>3</manvolnum></citerefentry>,
      <citerefentry><refentrytitle>journalctl</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
      <citerefentry><refentrytitle>sd-journal</refentrytitle><manvolnum>7</manvolnum></citerefentry>,
      <citerefentry project='die-net'><refentrytitle>pkg-config</refentrytitle><manvolnum>1</manvolnum></citerefentry>,
      <citerefentry><refentrytitle>machine-id</refentrytitle><manvolnum>5</manvolnum></citerefentry>
    </para>
  </refsect1>

</refentry>