+/*
+ * Set the endianness of a filesec and an ACL; if 'acl' is NULL, use the
+ * ACL interior to 'fsec' instead. If the endianness doesn't change, then
+ * this function will have no effect.
+ *
+ * Parameters: kendian The endianness to set; this is either
+ * KAUTH_ENDIAN_HOST or KAUTH_ENDIAN_DISK.
+ * fsec The filesec to convert.
+ * acl The ACL to convert (optional)
+ *
+ * Returns: (void)
+ *
+ * Notes: We use ntohl() because it has a transitive property on Intel
+ * machines and no effect on PPC mancines. This guarantees us
+ * that the swapping only occurs if the endiannes is wrong.
+ */
+void
+kauth_filesec_acl_setendian(int kendian, kauth_filesec_t fsec, kauth_acl_t acl)
+{
+ uint32_t compare_magic = KAUTH_FILESEC_MAGIC;
+ uint32_t invert_magic = ntohl(KAUTH_FILESEC_MAGIC);
+ uint32_t compare_acl_entrycount;
+ uint32_t i;
+
+ if (compare_magic == invert_magic)
+ return;
+
+ /* If no ACL, use ACL interior to 'fsec' instead */
+ if (acl == NULL)
+ acl = &fsec->fsec_acl;
+
+ compare_acl_entrycount = acl->acl_entrycount;
+
+ /*
+ * Only convert what needs to be converted, and only if the arguments
+ * are valid. The following switch and tests effectively reject
+ * conversions on invalid magic numbers as a desirable side effect.
+ */
+ switch(kendian) {
+ case KAUTH_ENDIAN_HOST: /* not in host, convert to host */
+ if (fsec->fsec_magic != invert_magic)
+ return;
+ /* acl_entrycount is byteswapped */
+ compare_acl_entrycount = ntohl(acl->acl_entrycount);
+ break;
+ case KAUTH_ENDIAN_DISK: /* not in disk, convert to disk */
+ if (fsec->fsec_magic != compare_magic)
+ return;
+ break;
+ default: /* bad argument */
+ return;
+ }
+
+ /* We are go for conversion */
+ fsec->fsec_magic = ntohl(fsec->fsec_magic);
+ acl->acl_entrycount = ntohl(acl->acl_entrycount);
+ if (compare_acl_entrycount != KAUTH_FILESEC_NOACL) {
+ acl->acl_flags = ntohl(acl->acl_flags);
+
+ /* swap ACE rights and flags */
+ for (i = 0; i < compare_acl_entrycount; i++) {
+ acl->acl_ace[i].ace_flags = ntohl(acl->acl_ace[i].ace_flags);
+ acl->acl_ace[i].ace_rights = ntohl(acl->acl_ace[i].ace_rights);
+ }
+ }
+ }
+