From e74bfeef39406b1d74075ba0ce340588618ea58d Mon Sep 17 00:00:00 2001 From: Apple Date: Sat, 5 May 2007 20:31:57 +0000 Subject: [PATCH 1/1] hfs-165.tar.gz --- hfs_util/Info.plist | 26 ++++-- hfs_util/Makefile | 2 +- hfs_util/hfsutil_jnl.c | 197 ++++++++++++++++++++++++++++++++++++++-- hfs_util/hfsutil_main.c | 170 ++++++++++++++++++++++------------ 4 files changed, 325 insertions(+), 70 deletions(-) diff --git a/hfs_util/Info.plist b/hfs_util/Info.plist index 7734cbb..b715b38 100644 --- a/hfs_util/Info.plist +++ b/hfs_util/Info.plist @@ -13,11 +13,11 @@ CFBundlePackageType fs CFBundleShortVersionString - 1.4.2 + 1.5.0 CFBundleSignature ???? CFBundleVersion - 1.4 + 1.5 FSMediaTypes Apple_Boot @@ -167,6 +167,8 @@ -n FSVerificationExecutable ../../../../../../sbin/fsck_hfs + FSDoesVerifyLive + HFS+ @@ -196,6 +198,8 @@ -fn FSVerificationExecutable ../../../../../../sbin/fsck_hfs + FSDoesVerifyLive + Journaled HFS+ @@ -206,7 +210,7 @@ FSFormatExecutable ../../../../../../sbin/newfs_hfs FSFormatMinimumSize - 4194304 + 9437184 FSFormatMaximumSize 9223372034707292160 FSMountArguments @@ -218,13 +222,17 @@ FSSubType 1 FSRepairArguments - -y + -fy FSRepairExecutable ../../../../../../sbin/fsck_hfs FSVerificationArguments -fn FSVerificationExecutable ../../../../../../sbin/fsck_hfs + FSDoesVerifyLive + + FSLiveVerificationArguments + -l Case-sensitive HFS+ @@ -254,6 +262,8 @@ -fn FSVerificationExecutable ../../../../../../sbin/fsck_hfs + FSDoesVerifyLive + Case-sensitive Journaled HFS+ @@ -264,7 +274,7 @@ FSFormatExecutable ../../../../../../sbin/newfs_hfs FSFormatMinimumSize - 4194304 + 9437184 FSFormatMaximumSize 9223372034707292160 FSMountArguments @@ -276,13 +286,17 @@ FSSubType 3 FSRepairArguments - -y + -fy FSRepairExecutable ../../../../../../sbin/fsck_hfs FSVerificationArguments -fn FSVerificationExecutable ../../../../../../sbin/fsck_hfs + FSDoesVerifyLive + + FSLiveVerificationArguments + -l diff --git a/hfs_util/Makefile b/hfs_util/Makefile index 91f775d..25e7f90 100644 --- a/hfs_util/Makefile +++ b/hfs_util/Makefile @@ -25,7 +25,7 @@ MAKEFILEDIR = $(MAKEFILEPATH)/pb_makefiles CODE_GEN_STYLE = DYNAMIC MAKEFILE = tool.make NEXTSTEP_INSTALLDIR = $(SYSTEM_LIBRARY_DIR)/Filesystems/hfs.fs -LIBS = -lcrypto +LIBS = DEBUG_LIBS = $(LIBS) PROF_LIBS = $(LIBS) diff --git a/hfs_util/hfsutil_jnl.c b/hfs_util/hfsutil_jnl.c index b87b275..f83536d 100644 --- a/hfs_util/hfsutil_jnl.c +++ b/hfs_util/hfsutil_jnl.c @@ -486,16 +486,16 @@ DoMakeJournaled(char *volname, int jsize) } // swap the data before we copy it - jib.flags = NXSwapBigLongToHost(jib.flags); - jib.offset = NXSwapBigLongLongToHost(jib.offset); - jib.size = NXSwapBigLongLongToHost(jib.size); + jib.flags = OSSwapBigToHostInt32(jib.flags); + jib.offset = OSSwapBigToHostInt64(jib.offset); + jib.size = OSSwapBigToHostInt64(jib.size); memcpy(buf, &jib, sizeof(jib)); // now put it back the way it was - jib.size = NXSwapBigLongLongToHost(jib.size); - jib.offset = NXSwapBigLongLongToHost(jib.offset); - jib.flags = NXSwapBigLongToHost(jib.flags); + jib.size = OSSwapBigToHostInt64(jib.size); + jib.offset = OSSwapBigToHostInt64(jib.offset); + jib.flags = OSSwapBigToHostInt32(jib.flags); if (write(fd, buf, block_size) != block_size) { fprintf(stderr, "Failed to write journal info block on volume %s (%s)!\n", @@ -648,3 +648,188 @@ DoGetJournalInfo(char *volname) return 0; } + + +int +RawDisableJournaling(char *devname) +{ + int fd = -1, ret = 0; + char *buff = NULL, rawdev[256]; + u_int64_t disksize; + u_int64_t blkcnt; + u_int32_t blksize; + daddr_t mdb_offset; + HFSMasterDirectoryBlock *mdbp; + HFSPlusVolumeHeader *vhp; + off_t embeddedOffset, hdr_offset; + struct statfs sfs; + struct stat st; + + restart: + if (stat(devname, &st) != 0) { + fprintf(stderr, "Could not access %s (%s)\n", devname, strerror(errno)); + ret = -1; + goto out; + } + + if (S_ISCHR(st.st_mode) == 0) { + // hmmm, it's not the character special raw device so we + // should try to figure out the real device. + if (S_ISBLK(st.st_mode)) { + strcpy(rawdev, "/dev/r"); + strcat(rawdev, devname + 5); + } else { + if (statfs(devname, &sfs) != 0) { + fprintf(stderr, "Can't find out any info about the fs for path %s (%s)\n", + devname, strerror(errno)); + ret = -1; + goto out; + } + + // copy the "/dev/" + strncpy(rawdev, sfs.f_mntfromname, 5); + rawdev[5] = 'r'; + strcpy(&rawdev[6], &sfs.f_mntfromname[5]); + } + + devname = &rawdev[0]; + goto restart; + } + + fd = open(devname, O_RDWR); + if (fd < 0) { + fprintf(stderr, "can't open: %s (%s)\n", devname, strerror(errno)); + ret = -1; + goto out; + } + + /* Get the real physical block size. */ + if (ioctl(fd, DKIOCGETBLOCKSIZE, (caddr_t)&blksize) != 0) { + fprintf(stderr, "can't get the device block size (%s). assuming 512\n", strerror(errno)); + blksize = 512; + ret = -1; + goto out; + } + + /* Get the number of physical blocks. */ + if (ioctl(fd, DKIOCGETBLOCKCOUNT, (caddr_t)&blkcnt)) { + struct stat st; + + if (fstat(fd, &st) != 0) { + ret = -1; + goto out; + } + + blkcnt = st.st_size / blksize; + } + + /* Compute an accurate disk size */ + disksize = blkcnt * (u_int64_t)blksize; + + /* + * There are only 31 bits worth of block count in + * the buffer cache. So for large volumes a 4K + * physical block size is needed. + */ + if (blksize == 512 && blkcnt > (u_int64_t)0x000000007fffffff) { + blksize = 4096; + } + + /* + * At this point: + * blksize has our prefered physical block size + * blkcnt has the total number of physical blocks + */ + + buff = (char *)malloc(blksize); + + hdr_offset = HFS_PRI_SECTOR(blksize)*blksize; + if (pread(fd, buff, blksize, hdr_offset) != blksize) { + fprintf(stderr, "failed to read volume header @ offset %lld (%s)\n", + hdr_offset, strerror(errno)); + ret = -1; + goto out; + } + + // if we read in an empty bunch of junk at location zero, then + // retry at offset 0x400 which is where the header normally is. + if (*(int *)buff == 0 && hdr_offset == 0) { + hdr_offset = 0x400; + if (pread(fd, buff, blksize, hdr_offset) != blksize) { + fprintf(stderr, "failed to read volume header @ offset %lld (%s)\n", + hdr_offset, strerror(errno)); + ret = -1; + goto out; + } + } + + + + mdbp = (HFSMasterDirectoryBlock *)buff; + if ((SWAP_BE16(mdbp->drSigWord) == kHFSSigWord) && (SWAP_BE16(mdbp->drEmbedSigWord) != kHFSPlusSigWord)) { + // normal hfs can not ever be journaled + fprintf(stderr, "disable_journaling: volume is only regular HFS, not HFS+\n"); + goto out; + } + + /* Get the embedded Volume Header */ + if (SWAP_BE16(mdbp->drEmbedSigWord) == kHFSPlusSigWord) { + embeddedOffset = SWAP_BE16(mdbp->drAlBlSt) * 512; + embeddedOffset += (u_int64_t)SWAP_BE16(mdbp->drEmbedExtent.startBlock) * (u_int64_t)SWAP_BE32(mdbp->drAlBlkSiz); + + /* + * If the embedded volume doesn't start on a block + * boundary, then switch the device to a 512-byte + * block size so everything will line up on a block + * boundary. + */ + if ((embeddedOffset % blksize) != 0) { + fprintf(stderr, "HFS Mount: embedded volume offset not" + " a multiple of physical block size (%d);" + " switching to 512\n", blksize); + + blkcnt *= (blksize / 512); + blksize = 512; + } + + disksize = (u_int64_t)SWAP_BE16(mdbp->drEmbedExtent.blockCount) * (u_int64_t)SWAP_BE32(mdbp->drAlBlkSiz); + + mdb_offset = (embeddedOffset / blksize) + HFS_PRI_SECTOR(blksize); + hdr_offset = mdb_offset * blksize; + if (pread(fd, buff, blksize, hdr_offset) != blksize) { + fprintf(stderr, "failed to read the embedded vhp @ offset %d\n", mdb_offset * blksize); + ret = -1; + goto out; + } + + vhp = (HFSPlusVolumeHeader*) buff; + } else /* pure HFS+ */ { + embeddedOffset = 0; + vhp = (HFSPlusVolumeHeader*) mdbp; + } + + + if ((SWAP_BE32(vhp->attributes) & kHFSVolumeJournaledMask) != 0) { + unsigned int tmp = SWAP_BE32(vhp->attributes); + + tmp &= ~kHFSVolumeJournaledMask; + vhp->attributes = SWAP_BE32(tmp); + if ((tmp = pwrite(fd, buff, blksize, hdr_offset)) != blksize) { + fprintf(stderr, "Update of super-block on %s failed! (%d != %d, %s)\n", + devname, tmp, blksize, strerror(errno)); + } else { + fprintf(stderr, "Turned off the journaling bit for %s\n", devname); + } + } else { + fprintf(stderr, "disable_journaling: volume was not journaled.\n"); + } + + + out: + if (buff) + free(buff); + if (fd >= 0) + close(fd); + + return ret; +} diff --git a/hfs_util/hfsutil_main.c b/hfs_util/hfsutil_main.c index ff56507..15bb2d1 100644 --- a/hfs_util/hfsutil_main.c +++ b/hfs_util/hfsutil_main.c @@ -65,12 +65,21 @@ #include #include -#include +/* + * CommonCrypto provides a more stable API than OpenSSL guarantees; + * the #define causes it to use the same API for MD5 and SHA1, so the rest of + * the code need not change. + */ +#define COMMON_DIGEST_FOR_OPENSSL +#include -#include +#include #include +#include +#include + #define READ_DEFAULT_ENCODING 1 #ifndef FSUC_ADOPT @@ -97,6 +106,10 @@ #define FSUC_UNJNL 'U' #endif +#ifndef FSUC_UNJNL_RAW +#define FSUC_UNJNL_RAW 'N' +#endif + #ifndef FSUC_JNLINFO #define FSUC_JNLINFO 'I' #endif @@ -181,6 +194,7 @@ static int DoDisown( const char * theDeviceNamePtr ); extern int DoMakeJournaled( const char * volNamePtr, int journalSize ); // XXXdbg extern int DoUnJournal( const char * volNamePtr ); // XXXdbg extern int DoGetJournalInfo( const char * volNamePtr ); +extern int RawDisableJournaling( const char *devname ); static int ParseArgs( int argc, const char * argv[], const char ** actionPtr, const char ** mountPointPtr, boolean_t * isEjectablePtr, boolean_t * isLockedPtr, boolean_t * isSetuidPtr, boolean_t * isDevPtr ); @@ -213,6 +227,8 @@ static int GetEncodingBias(void); CF_EXPORT Boolean _CFStringGetFileSystemRepresentation(CFStringRef string, UInt8 *buffer, CFIndex maxBufLen); +static void uuid_create_md5_from_name(uuid_t result_uuid, const uuid_t namespace, const void *name, int namelen); + /* * The fuction CFStringGetSystemEncoding does not work correctly in * our context (autodiskmount deamon). We include a local copy here @@ -436,6 +452,10 @@ int main (int argc, const char *argv[]) result = DoUnJournal( argv[2] ); break; + case FSUC_UNJNL_RAW: + result = RawDisableJournaling( argv[2] ); + break; + case FSUC_JNLINFO: result = DoGetJournalInfo( argv[2] ); break; @@ -675,8 +695,8 @@ DoProbe(char *deviceNamePtr) goto Return; /* get classic HFS volume name (from MDB) */ - if (NXSwapBigShortToHost(mdbPtr->drSigWord) == kHFSSigWord && - NXSwapBigShortToHost(mdbPtr->drEmbedSigWord) != kHFSPlusSigWord) { + if (OSSwapBigToHostInt16(mdbPtr->drSigWord) == kHFSSigWord && + OSSwapBigToHostInt16(mdbPtr->drEmbedSigWord) != kHFSPlusSigWord) { Boolean cfOK; CFStringRef cfstr; CFStringEncoding encoding; @@ -688,7 +708,7 @@ DoProbe(char *deviceNamePtr) } /* Check for an encoding hint in the Finder Info (field 4). */ - encoding = GET_HFS_TEXT_ENCODING(NXSwapBigLongToHost(mdbPtr->drFndrInfo[4])); + encoding = GET_HFS_TEXT_ENCODING(OSSwapBigToHostInt32(mdbPtr->drFndrInfo[4])); if (encoding == kCFStringEncodingInvalidId) { /* Next try the encoding bias in the kernel. */ encoding = GetEncodingBias(); @@ -722,13 +742,13 @@ DoProbe(char *deviceNamePtr) } /* get HFS Plus volume name (from Catalog) */ - } else if ((NXSwapBigShortToHost(volHdrPtr->signature) == kHFSPlusSigWord) || - (NXSwapBigShortToHost(volHdrPtr->signature) == kHFSXSigWord) || - (NXSwapBigShortToHost(mdbPtr->drSigWord) == kHFSSigWord && - NXSwapBigShortToHost(mdbPtr->drEmbedSigWord) == kHFSPlusSigWord)) { + } else if ((OSSwapBigToHostInt16(volHdrPtr->signature) == kHFSPlusSigWord) || + (OSSwapBigToHostInt16(volHdrPtr->signature) == kHFSXSigWord) || + (OSSwapBigToHostInt16(mdbPtr->drSigWord) == kHFSSigWord && + OSSwapBigToHostInt16(mdbPtr->drEmbedSigWord) == kHFSPlusSigWord)) { off_t startOffset; - if (NXSwapBigShortToHost(volHdrPtr->signature) == kHFSSigWord) { + if (OSSwapBigToHostInt16(volHdrPtr->signature) == kHFSSigWord) { /* embedded volume, first find offset */ result = GetEmbeddedHFSPlusVol(mdbPtr, &startOffset); if ( result != FSUR_IO_SUCCESS ) @@ -769,11 +789,34 @@ Return: } /* DoProbe */ +/* + * Create a version 3 UUID from a unique "name" in the given "name space". + * Version 3 UUID are derived using "name" via MD5 checksum. + * + * Parameters: + * result_uuid - resulting UUID. + * namespace - namespace in which given name exists and UUID should be created. + * name - unique string used to create version 3 UUID. + * namelen - length of the name string. + */ +static void +uuid_create_md5_from_name(uuid_t result_uuid, const uuid_t namespace, const void *name, int namelen) +{ + MD5_CTX c; + + MD5_Init(&c); + MD5_Update(&c, namespace, sizeof(uuid_t)); + MD5_Update(&c, name, namelen); + MD5_Final(result_uuid, &c); + + result_uuid[6] = (result_uuid[6] & 0x0F) | 0x30; + result_uuid[8] = (result_uuid[8] & 0x3F) | 0x80; +} /* **************************************** DoGetUUIDKey ******************************************* Purpose - - This routine will open the given block device and return the volume UUID in text form written to stdout. + This routine will open the given block device and return the 128-bit volume UUID in text form written to stdout. Input - theDeviceNamePtr - pointer to the device name (full path, like /dev/disk0s2). Output - @@ -783,13 +826,18 @@ static int DoGetUUIDKey( const char * theDeviceNamePtr ) { int result; VolumeUUID targetVolumeUUID; - VolumeUUIDString UUIDString; - char uuidLine[VOLUMEUUIDLENGTH+2]; - + uuid_t uuid; + char uuidLine[40]; + + unsigned char rawUUID[8]; + if ((result = GetVolumeUUID(theDeviceNamePtr, &targetVolumeUUID, FALSE)) != FSUR_IO_SUCCESS) goto Err_Exit; - - ConvertVolumeUUIDToString( &targetVolumeUUID, UUIDString); - strncpy(uuidLine, UUIDString, VOLUMEUUIDLENGTH+1); + + ((uint32_t *)rawUUID)[0] = OSSwapHostToBigInt32(targetVolumeUUID.v.high); + ((uint32_t *)rawUUID)[1] = OSSwapHostToBigInt32(targetVolumeUUID.v.low); + + uuid_create_md5_from_name(uuid, kFSUUIDNamespaceSHA1, rawUUID, sizeof(rawUUID)); + uuid_unparse(uuid, uuidLine); write(1, uuidLine, strlen(uuidLine)); result = FSUR_IO_SUCCESS; @@ -1050,6 +1098,11 @@ ParseArgs(int argc, const char *argv[], const char ** actionPtr, doLengthCheck = 0; break; + case FSUC_UNJNL_RAW: + index = 0; + doLengthCheck = 0; + break; + case FSUC_JNLINFO: index = 0; doLengthCheck = 0; @@ -1143,6 +1196,7 @@ DoDisplayUsage(const char *argv[]) printf(" -%c (Adopt permissions)\n", FSUC_ADOPT); printf(" -%c (Make a file system journaled)\n", FSUC_MKJNL); printf(" -%c (Turn off journaling on a file system)\n", FSUC_UNJNL); + printf(" -%c (Turn off journaling on a raw device)\n", FSUC_UNJNL_RAW); printf(" -%c (Get size & location of journaling on a file system)\n", FSUC_JNLINFO); printf("device_arg:\n"); printf(" device we are acting upon (for example, 'disk0s2')\n"); @@ -1240,8 +1294,8 @@ ReadHeaderBlock(int fd, void *bufPtr, off_t *startOffset, VolumeUUID **finderInf * If this is a wrapped HFS Plus volume, read the Volume Header from * sector 2 of the embedded volume. */ - if (NXSwapBigShortToHost(mdbPtr->drSigWord) == kHFSSigWord && - NXSwapBigShortToHost(mdbPtr->drEmbedSigWord) == kHFSPlusSigWord) { + if (OSSwapBigToHostInt16(mdbPtr->drSigWord) == kHFSSigWord && + OSSwapBigToHostInt16(mdbPtr->drEmbedSigWord) == kHFSPlusSigWord) { result = GetEmbeddedHFSPlusVol(mdbPtr, startOffset); if (result != FSUR_IO_SUCCESS) goto Err_Exit; @@ -1255,10 +1309,10 @@ ReadHeaderBlock(int fd, void *bufPtr, off_t *startOffset, VolumeUUID **finderInf * volumes (including wrapped HFS Plus). Verify the signature and grab the * UUID from the Finder Info. */ - if (NXSwapBigShortToHost(mdbPtr->drSigWord) == kHFSSigWord) { + if (OSSwapBigToHostInt16(mdbPtr->drSigWord) == kHFSSigWord) { *finderInfoUUIDPtr = (VolumeUUID *)(&mdbPtr->drFndrInfo[6]); - } else if (NXSwapBigShortToHost(volHdrPtr->signature) == kHFSPlusSigWord || - NXSwapBigShortToHost(volHdrPtr->signature) == kHFSXSigWord) { + } else if (OSSwapBigToHostInt16(volHdrPtr->signature) == kHFSPlusSigWord || + OSSwapBigToHostInt16(volHdrPtr->signature) == kHFSXSigWord) { *finderInfoUUIDPtr = (VolumeUUID *)&volHdrPtr->finderInfo[24]; } else { result = FSUR_UNRECOGNIZED; @@ -1312,8 +1366,8 @@ GetVolumeUUIDRaw(const char *deviceNamePtr, VolumeUUID *volumeUUIDPtr) /* * Copy the volume UUID out of the Finder Info */ - volumeUUIDPtr->v.high = NXSwapBigLongToHost(finderInfoUUIDPtr->v.high); - volumeUUIDPtr->v.low = NXSwapBigLongToHost(finderInfoUUIDPtr->v.low); + volumeUUIDPtr->v.high = OSSwapBigToHostInt32(finderInfoUUIDPtr->v.high); + volumeUUIDPtr->v.low = OSSwapBigToHostInt32(finderInfoUUIDPtr->v.low); Err_Exit: if (fd > 0) close(fd); @@ -1369,8 +1423,8 @@ SetVolumeUUIDRaw(const char *deviceNamePtr, VolumeUUID *volumeUUIDPtr) /* * Update the UUID in the Finder Info */ - finderInfoUUIDPtr->v.high = NXSwapHostLongToBig(volumeUUIDPtr->v.high); - finderInfoUUIDPtr->v.low = NXSwapHostLongToBig(volumeUUIDPtr->v.low); + finderInfoUUIDPtr->v.high = OSSwapHostToBigInt32(volumeUUIDPtr->v.high); + finderInfoUUIDPtr->v.low = OSSwapHostToBigInt32(volumeUUIDPtr->v.low); /* * Write the modified MDB or VHB back to disk @@ -1422,8 +1476,8 @@ GetVolumeUUIDAttr(const char *path, VolumeUUID *volumeUUIDPtr) /* Copy the UUID from the Finder Into to caller's buffer */ finderInfoUUIDPtr = (VolumeUUID *)(&volFinderInfo.finderinfo[6]); - volumeUUIDPtr->v.high = NXSwapBigLongToHost(finderInfoUUIDPtr->v.high); - volumeUUIDPtr->v.low = NXSwapBigLongToHost(finderInfoUUIDPtr->v.low); + volumeUUIDPtr->v.high = OSSwapBigToHostInt32(finderInfoUUIDPtr->v.high); + volumeUUIDPtr->v.low = OSSwapBigToHostInt32(finderInfoUUIDPtr->v.low); result = FSUR_IO_SUCCESS; Err_Exit: @@ -1465,8 +1519,8 @@ SetVolumeUUIDAttr(const char *path, VolumeUUID *volumeUUIDPtr) /* Update the UUID in the Finder Info */ finderInfoUUIDPtr = (VolumeUUID *)(&volFinderInfo.finderinfo[6]); - finderInfoUUIDPtr->v.high = NXSwapHostLongToBig(volumeUUIDPtr->v.high); - finderInfoUUIDPtr->v.low = NXSwapHostLongToBig(volumeUUIDPtr->v.low); + finderInfoUUIDPtr->v.high = OSSwapHostToBigInt32(volumeUUIDPtr->v.high); + finderInfoUUIDPtr->v.low = OSSwapHostToBigInt32(volumeUUIDPtr->v.low); /* Write the Finder Info back to the volume */ result = setattrlist(path, &alist, &volFinderInfo.finderinfo, sizeof(volFinderInfo.finderinfo), 0); @@ -1595,21 +1649,21 @@ GetEmbeddedHFSPlusVol (HFSMasterDirectoryBlock * hfsMasterDirectoryBlockPtr, off int result = FSUR_IO_SUCCESS; u_int32_t allocationBlockSize, firstAllocationBlock, startBlock, blockCount; - if (NXSwapBigShortToHost(hfsMasterDirectoryBlockPtr->drSigWord) != kHFSSigWord) { + if (OSSwapBigToHostInt16(hfsMasterDirectoryBlockPtr->drSigWord) != kHFSSigWord) { result = FSUR_UNRECOGNIZED; goto Return; } - allocationBlockSize = NXSwapBigLongToHost(hfsMasterDirectoryBlockPtr->drAlBlkSiz); - firstAllocationBlock = NXSwapBigShortToHost(hfsMasterDirectoryBlockPtr->drAlBlSt); + allocationBlockSize = OSSwapBigToHostInt32(hfsMasterDirectoryBlockPtr->drAlBlkSiz); + firstAllocationBlock = OSSwapBigToHostInt16(hfsMasterDirectoryBlockPtr->drAlBlSt); - if (NXSwapBigShortToHost(hfsMasterDirectoryBlockPtr->drEmbedSigWord) != kHFSPlusSigWord) { + if (OSSwapBigToHostInt16(hfsMasterDirectoryBlockPtr->drEmbedSigWord) != kHFSPlusSigWord) { result = FSUR_UNRECOGNIZED; goto Return; } - startBlock = NXSwapBigShortToHost(hfsMasterDirectoryBlockPtr->drEmbedExtent.startBlock); - blockCount = NXSwapBigShortToHost(hfsMasterDirectoryBlockPtr->drEmbedExtent.blockCount); + startBlock = OSSwapBigToHostInt16(hfsMasterDirectoryBlockPtr->drEmbedExtent.startBlock); + blockCount = OSSwapBigToHostInt16(hfsMasterDirectoryBlockPtr->drEmbedExtent.blockCount); if ( startOffsetPtr ) *startOffsetPtr = ((u_int64_t)startBlock * (u_int64_t)allocationBlockSize) + @@ -1663,8 +1717,8 @@ GetNameFromHFSPlusVolumeStartingAt(int fd, off_t hfsPlusVolumeOffset, unsigned c /* Verify that it is an HFS+ volume. */ - if (NXSwapBigShortToHost(volHdrPtr->signature) != kHFSPlusSigWord && - NXSwapBigShortToHost(volHdrPtr->signature) != kHFSXSigWord) { + if (OSSwapBigToHostInt16(volHdrPtr->signature) != kHFSPlusSigWord && + OSSwapBigToHostInt16(volHdrPtr->signature) != kHFSXSigWord) { result = FSUR_IO_FAIL; #if TRACE_HFS_UTIL fprintf(stderr, "hfs.util: GetNameFromHFSPlusVolumeStartingAt: volHdrPtr->signature != kHFSPlusSigWord\n"); @@ -1672,7 +1726,7 @@ GetNameFromHFSPlusVolumeStartingAt(int fd, off_t hfsPlusVolumeOffset, unsigned c goto Return; } - blockSize = NXSwapBigLongToHost(volHdrPtr->blockSize); + blockSize = OSSwapBigToHostInt32(volHdrPtr->blockSize); catalogExtents = (HFSPlusExtentDescriptor *) malloc(sizeof(HFSPlusExtentRecord)); if ( ! catalogExtents ) { result = FSUR_IO_FAIL; @@ -1682,7 +1736,7 @@ GetNameFromHFSPlusVolumeStartingAt(int fd, off_t hfsPlusVolumeOffset, unsigned c catalogExtCount = kHFSPlusExtentDensity; /* if there are overflow catalog extents, then go get them */ - if (NXSwapBigLongToHost(catalogExtents[7].blockCount) != 0) { + if (OSSwapBigToHostInt32(catalogExtents[7].blockCount) != 0) { result = GetCatalogOverflowExtents(fd, hfsPlusVolumeOffset, volHdrPtr, &catalogExtents, &catalogExtCount); if (result != FSUR_IO_SUCCESS) goto Return; @@ -1722,7 +1776,7 @@ GetNameFromHFSPlusVolumeStartingAt(int fd, off_t hfsPlusVolumeOffset, unsigned c HFSPlusCatalogKey * k; CFStringRef cfstr; - if ( bTreeNodeDescriptorPtr->numRecords < 1) { + if ( OSSwapBigToHostInt16(bTreeNodeDescriptorPtr->numRecords) < 1) { result = FSUR_IO_FAIL; #if TRACE_HFS_UTIL fprintf(stderr, "hfs.util: ERROR: bTreeNodeDescriptorPtr->numRecords < 1\n"); @@ -1737,12 +1791,12 @@ GetNameFromHFSPlusVolumeStartingAt(int fd, off_t hfsPlusVolumeOffset, unsigned c // Get a pointer to the first record. - p = bufPtr + NXSwapBigShortToHost(*v); // pointer arithmetic in bytes + p = bufPtr + OSSwapBigToHostInt16(*v); // pointer arithmetic in bytes k = (HFSPlusCatalogKey *)p; // There should be only one record whose parent is the root parent. It should be the first record. - if (NXSwapBigLongToHost(k->parentID) != kHFSRootParentID) { + if (OSSwapBigToHostInt32(k->parentID) != kHFSRootParentID) { result = FSUR_IO_FAIL; #if TRACE_HFS_UTIL fprintf(stderr, "hfs.util: ERROR: k->parentID != kHFSRootParentID\n"); @@ -1761,10 +1815,10 @@ GetNameFromHFSPlusVolumeStartingAt(int fd, off_t hfsPlusVolumeOffset, unsigned c result = FSUR_IO_FAIL; goto Return; } - swapped->length = NXSwapBigShortToHost(k->nodeName.length); + swapped->length = OSSwapBigToHostInt16(k->nodeName.length); for (i=0; ilength; i++) { - swapped->unicode[i] = NXSwapBigShortToHost(k->nodeName.unicode[i]); + swapped->unicode[i] = OSSwapBigToHostInt16(k->nodeName.unicode[i]); } swapped->unicode[i] = 0; cfstr = CFStringCreateWithCharacters(kCFAllocatorDefault, swapped->unicode, swapped->length); @@ -1836,12 +1890,12 @@ GetBTreeNodeInfo(int fd, off_t hfsPlusVolumeOffset, u_int32_t blockSize, goto free; } - *nodeSize = NXSwapBigShortToHost(bTreeHeaderPtr->header.nodeSize); + *nodeSize = OSSwapBigToHostInt16(bTreeHeaderPtr->header.nodeSize); - if (NXSwapBigLongToHost(bTreeHeaderPtr->header.leafRecords) == 0) + if (OSSwapBigToHostInt32(bTreeHeaderPtr->header.leafRecords) == 0) *firstLeafNode = 0; else - *firstLeafNode = NXSwapBigLongToHost(bTreeHeaderPtr->header.firstLeafNode); + *firstLeafNode = OSSwapBigToHostInt32(bTreeHeaderPtr->header.firstLeafNode); free:; free((char*) bTreeHeaderPtr); @@ -1864,6 +1918,7 @@ GetCatalogOverflowExtents(int fd, off_t hfsPlusVolumeOffset, u_int32_t *catalogExtCount) { off_t offset; + u_int32_t numRecords; u_int32_t nodeSize; u_int32_t leafNode; u_int32_t blockSize; @@ -1874,10 +1929,10 @@ GetCatalogOverflowExtents(int fd, off_t hfsPlusVolumeOffset, int i; int result; - blockSize = NXSwapBigLongToHost(volHdrPtr->blockSize); + blockSize = OSSwapBigToHostInt32(volHdrPtr->blockSize); listsize = *catalogExtCount * sizeof(HFSPlusExtentDescriptor); extents = *catalogExtents; - offset = (off_t)volHdrPtr->extentsFile.extents[0].startBlock * + offset = (off_t)OSSwapBigToHostInt32(volHdrPtr->extentsFile.extents[0].startBlock) * (off_t)blockSize; /* Read the header node of the extents B-Tree */ @@ -1918,7 +1973,8 @@ again: goto Return; } - for (i = 1; i <= bTreeNodeDescriptorPtr->numRecords; ++i) { + numRecords = OSSwapBigToHostInt16(bTreeNodeDescriptorPtr->numRecords); + for (i = 1; i <= numRecords; ++i) { u_int16_t * v; char * p; HFSPlusExtentKey * k; @@ -1932,23 +1988,23 @@ again: /* Get a pointer to the record */ - p = bufPtr + NXSwapBigShortToHost(*v); /* pointer arithmetic in bytes */ + p = bufPtr + OSSwapBigToHostInt16(*v); /* pointer arithmetic in bytes */ k = (HFSPlusExtentKey *)p; - if (NXSwapBigLongToHost(k->fileID) != kHFSCatalogFileID) + if (OSSwapBigToHostInt32(k->fileID) != kHFSCatalogFileID) goto Return; /* grow list and copy additional extents */ listsize += sizeof(HFSPlusExtentRecord); extents = (HFSPlusExtentDescriptor *) realloc(extents, listsize); - bcopy(p + NXSwapBigShortToHost(k->keyLength) + sizeof(u_int16_t), + bcopy(p + OSSwapBigToHostInt16(k->keyLength) + sizeof(u_int16_t), &extents[*catalogExtCount], sizeof(HFSPlusExtentRecord)); *catalogExtCount += kHFSPlusExtentDensity; *catalogExtents = extents; } - if ((leafNode = bTreeNodeDescriptorPtr->fLink) != 0) { + if ((leafNode = OSSwapBigToHostInt32(bTreeNodeDescriptorPtr->fLink)) != 0) { offset = (off_t) leafNode * (off_t) nodeSize; @@ -1997,7 +2053,7 @@ static int LogicalToPhysical(off_t offset, ssize_t length, u_int32_t blockSize, /* Find the extent containing logicalBlock */ for (extent = 0; extent < extentCount; ++extent) { - blockCount = NXSwapBigLongToHost(extentList[extent].blockCount); + blockCount = OSSwapBigToHostInt32(extentList[extent].blockCount); if (blockCount == 0) return FSUR_IO_FAIL; /* Tried to map past physical end of file */ @@ -2017,7 +2073,7 @@ static int LogicalToPhysical(off_t offset, ssize_t length, u_int32_t blockSize, */ /* Compute the physical starting position */ - temp = NXSwapBigLongToHost(extentList[extent].startBlock) + logicalBlock; /* First physical block */ + temp = OSSwapBigToHostInt32(extentList[extent].startBlock) + logicalBlock; /* First physical block */ temp *= blockSize; /* Byte offset of first physical block */ *physicalOffset = temp + offset; -- 2.45.2