Index: uspace/lib/c/generic/uuid.c
===================================================================
--- uspace/lib/c/generic/uuid.c	(revision 0b1f01c852f49bfda51d70983ad40bace28195a1)
+++ uspace/lib/c/generic/uuid.c	(revision 7cec2164e3bdb4c02f6ae514bc16e9c7c48be618)
@@ -41,4 +41,13 @@
 #include <stdio.h>
 
+static void encode16_be(uint8_t *, uint16_t);
+static void encode16_le(uint8_t *, uint16_t);
+static void encode32_be(uint8_t *, uint32_t);
+static void encode32_le(uint8_t *, uint32_t);
+static uint16_t decode16_be(uint8_t *);
+static uint16_t decode16_le(uint8_t *);
+static uint32_t decode32_be(uint8_t *);
+static uint32_t decode32_le(uint8_t *);
+
 /** Generate UUID.
  *
@@ -56,6 +65,36 @@
 		return EIO;
 
-	for (i = 0; i < uuid_bytes; i++) {
-		rc = rndgen_uint8(rndgen, &uuid->b[i]);
+	rc = rndgen_uint32(rndgen, &uuid->time_low);
+	if (rc != EOK) {
+		rc = EIO;
+		goto error;
+	}
+
+	rc = rndgen_uint16(rndgen, &uuid->time_mid);
+	if (rc != EOK) {
+		rc = EIO;
+		goto error;
+	}
+
+	rc = rndgen_uint16(rndgen, &uuid->time_hi_and_version);
+	if (rc != EOK) {
+		rc = EIO;
+		goto error;
+	}
+
+	rc = rndgen_uint8(rndgen, &uuid->clock_seq_hi_and_reserved);
+	if (rc != EOK) {
+		rc = EIO;
+		goto error;
+	}
+
+	rc = rndgen_uint8(rndgen, &uuid->clock_seq_low);
+	if (rc != EOK) {
+		rc = EIO;
+		goto error;
+	}
+
+	for (i = 0; i < _UUID_NODE_LEN; i++) {
+		rc = rndgen_uint8(rndgen, &uuid->node[i]);
 		if (rc != EOK) {
 			rc = EIO;
@@ -65,6 +104,6 @@
 
 	/* Version 4 UUID from random or pseudo-random numbers */
-	uuid->b[6] = (uuid->b[6] & 0x0f) | 0x40;
-	uuid->b[8] = (uuid->b[8] & 0x3f) | 0x80;
+	uuid->time_hi_and_version = (uuid->time_hi_and_version & 0x0fff) | 0x4000;
+	uuid->clock_seq_hi_and_reserved = (uuid->clock_seq_hi_and_reserved & 0x3f) | 0x80;
 
 error:
@@ -73,4 +112,5 @@
 }
 
+
 /** Encode UUID into binary form per RFC 4122.
  *
@@ -82,6 +122,12 @@
 	int i;
 
-	for (i = 0; i < uuid_bytes; i++)
-		buf[i] = uuid->b[i];
+	encode32_be(buf, uuid->time_low);
+	encode16_be(buf + 4, uuid->time_mid);
+	encode16_be(buf + 6, uuid->time_hi_and_version);
+	buf[8] = uuid->clock_seq_hi_and_reserved;
+	buf[9] = uuid->clock_seq_low;
+
+	for (i = 0; i < _UUID_NODE_LEN; i++)
+		buf[10 + i] = uuid->node[i];
 }
 
@@ -95,6 +141,50 @@
 	int i;
 
-	for (i = 0; i < uuid_bytes; i++)
-		uuid->b[i] = buf[i];
+	uuid->time_low = decode32_be(buf);
+	uuid->time_mid = decode16_be(buf + 4);
+	uuid->time_hi_and_version = decode16_be(buf + 6);
+	uuid->clock_seq_hi_and_reserved = buf[8];
+	uuid->clock_seq_low = buf[9];
+
+	for (i = 0; i < _UUID_NODE_LEN; i++)
+		uuid->node[i] = buf[10 + i];
+}
+
+/** Encode UUID into little-endian (GPT) binary form.
+ *
+ * @param uuid UUID
+ * @param buf 16-byte destination buffer
+ */
+void uuid_encode_le(uuid_t *uuid, uint8_t *buf)
+{
+	int i;
+
+	encode32_le(buf, uuid->time_low);
+	encode16_le(buf + 4, uuid->time_mid);
+	encode16_le(buf + 6, uuid->time_hi_and_version);
+	buf[8] = uuid->clock_seq_hi_and_reserved;
+	buf[9] = uuid->clock_seq_low;
+
+	for (i = 0; i < _UUID_NODE_LEN; i++)
+		buf[10 + i] = uuid->node[i];
+}
+
+/** Decode UUID from little-endian (GPT) binary form.
+ *
+ * @param buf 16-byte source buffer
+ * @param uuid Place to store UUID
+ */
+void uuid_decode_le(uint8_t *buf, uuid_t *uuid)
+{
+	int i;
+
+	uuid->time_low = decode32_le(buf);
+	uuid->time_mid = decode16_le(buf + 4);
+	uuid->time_hi_and_version = decode16_le(buf + 6);
+	uuid->clock_seq_hi_and_reserved = buf[8];
+	uuid->clock_seq_low = buf[9];
+
+	for (i = 0; i < _UUID_NODE_LEN; i++)
+		uuid->node[i] = buf[10 + i];
 }
 
@@ -142,20 +232,16 @@
 		return EINVAL;
 
-	uuid->b[0] = time_low >> 24;
-	uuid->b[1] = (time_low >> 16) & 0xff;
-	uuid->b[2] = (time_low >> 8) & 0xff;
-	uuid->b[3] = time_low & 0xff;
-
-	uuid->b[4] = time_mid >> 8;
-	uuid->b[5] = time_mid & 0xff;
-
-	uuid->b[6] = time_ver >> 8;
-	uuid->b[7] = time_ver & 0xff;
-
-	uuid->b[8] = clock >> 8;
-	uuid->b[9] = clock & 0xff;
-
-	for (i = 0; i < 6; i++)
-		uuid->b[10 + i] = (node >> 8 * (5 - i)) & 0xff;
+	uuid->time_low = time_low;
+
+	uuid->time_mid = time_mid;
+
+	uuid->time_hi_and_version = time_ver;
+
+	uuid->clock_seq_hi_and_reserved = clock >> 8;
+
+	uuid->clock_seq_low = clock & 0xff;
+
+	for (i = 0; i < _UUID_NODE_LEN; i++)
+		uuid->node[i] = (node >> 8 * (5 - i)) & 0xff;
 
 	if (endptr != NULL) {
@@ -183,9 +269,12 @@
 		return ENOMEM;
 
-	const char *format = "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x";
+	const char *format = "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x";
 	if (uppercase)
-		format = "%02X%02X%02X%02X-%02X%02X-%02X%02X-%02X%02X-%02X%02X%02X%02X%02X%02X";
-
-	int ret = snprintf(str, size, format, uuid->b[0], uuid->b[1], uuid->b[2], uuid->b[3], uuid->b[4], uuid->b[5], uuid->b[6], uuid->b[7], uuid->b[8], uuid->b[9], uuid->b[10], uuid->b[11], uuid->b[12], uuid->b[13], uuid->b[14], uuid->b[15]);
+		format = "%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X";
+
+	int ret = snprintf(str, size, format, uuid->time_low, uuid->time_mid,
+	    uuid->time_hi_and_version, uuid->clock_seq_hi_and_reserved,
+	    uuid->clock_seq_low, uuid->node[0], uuid->node[1], uuid->node[2],
+	    uuid->node[3], uuid->node[4], uuid->node[5]);
 
 	if (ret != 36) {
@@ -198,4 +287,52 @@
 }
 
+static void encode16_be(uint8_t *buf, uint16_t value)
+{
+	buf[0] = (value >> 8) & 0xff;
+	buf[1] = value & 0xff;
+}
+
+static void encode16_le(uint8_t *buf, uint16_t value)
+{
+	buf[0] = value & 0xff;
+	buf[1] = (value >> 8) & 0xff;
+}
+
+static void encode32_be(uint8_t *buf, uint32_t value)
+{
+	buf[0] = (value >> 24) & 0xff;
+	buf[1] = (value >> 16) & 0xff;
+	buf[2] = (value >> 8) & 0xff;
+	buf[3] = value & 0xff;
+}
+
+static void encode32_le(uint8_t *buf, uint32_t value)
+{
+	buf[0] = value & 0xff;
+	buf[1] = (value >> 8) & 0xff;
+	buf[2] = (value >> 16) & 0xff;
+	buf[3] = (value >> 24) & 0xff;
+}
+
+static uint16_t decode16_be(uint8_t *buf)
+{
+	return ((buf[0] << 8) | buf[1]);
+}
+
+static uint16_t decode16_le(uint8_t *buf)
+{
+	return ((buf[1] << 8) | buf[0]);
+}
+
+static uint32_t decode32_be(uint8_t *buf)
+{
+	return ((buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3]);
+}
+
+static uint32_t decode32_le(uint8_t *buf)
+{
+	return ((buf[3] << 24) | (buf[2] << 16) | (buf[1] << 8) | buf[0]);
+}
+
 /** @}
  */
