void FakeSMCSensor::encodeNumericValue(float value, void *outBuffer) { if ((type[0] == 'u' || type[0] == 's') && type[1] == 'i') { bool minus = value < 0; bool signd = type[0] == 's'; if (minus) value = -value; switch (type[2]) { case '8': if (type[3] == '\0' && size == 1) { UInt8 encoded = (UInt8)value; if (signd) bit_write(signd && minus, encoded, BIT(7)); bcopy(&encoded, outBuffer, 1); } break; case '1': if (type[3] == '6' && size == 2) { UInt16 encoded = (UInt16)value; if (signd) bit_write(signd && minus, encoded, BIT(15)); OSWriteBigInt16(outBuffer, 0, encoded); } break; case '3': if (type[3] == '2' && size == 4) { UInt32 encoded = (UInt32)value; if (signd) bit_write(signd && minus, encoded, BIT(31)); OSWriteBigInt32(outBuffer, 0, encoded); } break; default: return; } } else if ((type[0] == 'f' || type[0] == 's') && type[1] == 'p') { bool minus = value < 0; bool signd = type[0] == 's'; UInt8 i = get_index(type[2]); UInt8 f = get_index(type[3]); if (i + f == (signd ? 15 : 16)) { if (minus) value = -value; UInt16 encoded = value * (float)BIT(f); if (signd) bit_write(minus, encoded, BIT(15)); OSWriteBigInt16(outBuffer, 0, encoded); } } }
/** * Encode integer value to SMC integer format * * @param value Integer value to be encoded * @param type Integer SMC type ("ui8 ", "ui16", "ui32", "si8 ") * @param size Buffer size for encoded bytes, one byte for ui8, 2 bytes for ui16 etc. * @param outBuffer Buffer where encoded bytes will be copied to, should be already allocated with correct size * * @return True on success False otherwise */ bool fakeSMCPluginEncodeIntValue(int value, const char *type, const UInt8 size, void *outBuffer) { if (type && outBuffer) { size_t typeLength = strnlen(type, 4); if (typeLength >= 3 && (type[0] == 'u' || type[0] == 's') && type[1] == 'i') { bool minus = value < 0; bool signd = type[0] == 's'; if (minus) value = -value; switch (type[2]) { case '8': if (type[3] == '\0' && size == 1) { UInt8 encoded = (UInt8)value; if (signd) bit_write(signd && minus, encoded, BIT(7)); bcopy(&encoded, outBuffer, 1); return true; } break; case '1': if (type[3] == '6' && size == 2) { UInt16 encoded = (UInt16)value; if (signd) bit_write(signd && minus, encoded, BIT(15)); OSWriteBigInt16(outBuffer, 0, encoded); return true; } break; case '3': if (type[3] == '2' && size == 4) { UInt32 encoded = (UInt32)value; if (signd) bit_write(signd && minus, encoded, BIT(31)); OSWriteBigInt32(outBuffer, 0, encoded); return true; } break; } } } return false; }
/** * Encode floating point value to SMC float format * * @param value Floating point value to be encoded * @param type Floating point SMC type name ("fp2e", "fpe2", "sp78" are correct float SMC types) * @param size Buffer size for encoded bytes, for every floating SMC type should be 2 bytes * @param outBuffer Buffer where encoded bytes will be copied to, should be already allocated with correct size * * @return True on success False otherwise */ bool fakeSMCPluginEncodeFloatValue(float value, const char *type, const UInt8 size, void *outBuffer) { if (type && outBuffer) { size_t typeLength = strnlen(type, 4); if (typeLength >= 3 && (type[0] == 'f' || type[0] == 's') && type[1] == 'p') { bool minus = value < 0; bool signd = type[0] == 's'; UInt8 i = fakeSMCPluginGetIndexFromChar(type[2]); UInt8 f = fakeSMCPluginGetIndexFromChar(type[3]); if (i + f == (signd ? 15 : 16)) { if (minus) value = -value; UInt16 encoded = value * (float)BIT(f); if (signd) bit_write(minus, encoded, BIT(15)); OSWriteBigInt16(outBuffer, 0, encoded); return true; } } } return false; }
static int FuseHFS_getxattr(const char *path, const char *name, char *value, size_t size, uint32_t position) { //dprintf("getxattr %s %s %p %lu %u\n", path, name, value, size, position); // convert to hfs path char *hfspath = mkhfspath(path); if (hfspath == NULL) return -ENOENT; // find file hfsdirent ent; if (hfs_stat(NULL, hfspath, &ent) == -1) { free(hfspath); return -ENOENT; } if (strcmp(name, XATTR_FINDERINFO_NAME) == 0) { if (value == NULL) { free(hfspath); return 32; } if (size < 32) { free(hfspath); return -ERANGE; } // return finder info if (ent.flags & HFS_ISDIR) { // directory info OSWriteBigInt16(value, 0, ent.u.dir.rect.top); OSWriteBigInt16(value, 2, ent.u.dir.rect.left); OSWriteBigInt16(value, 4, ent.u.dir.rect.bottom); OSWriteBigInt16(value, 6, ent.u.dir.rect.right); OSWriteBigInt16(value, 8, ent.fdflags); OSWriteBigInt16(value, 10, ent.fdlocation.v); OSWriteBigInt16(value, 12, ent.fdlocation.h); OSWriteBigInt16(value, 14, ent.u.dir.view); // DXInfo OSWriteBigInt16(value, 16, ((DXInfo*)(ent.u.dir.xinfo))->frScroll.v); OSWriteBigInt16(value, 18, ((DXInfo*)(ent.u.dir.xinfo))->frScroll.h); OSWriteBigInt32(value, 20, ((DXInfo*)(ent.u.dir.xinfo))->frOpenChain); OSWriteBigInt16(value, 24, ((DXInfo*)(ent.u.dir.xinfo))->frUnused); OSWriteBigInt16(value, 26, ((DXInfo*)(ent.u.dir.xinfo))->frComment); OSWriteBigInt32(value, 28, ((DXInfo*)(ent.u.dir.xinfo))->frPutAway); } else { // file info memcpy(value, ent.u.file.type, 4); memcpy(value+4, ent.u.file.creator, 4); OSWriteBigInt16(value, 8, ent.fdflags); OSWriteBigInt16(value, 10, ent.fdlocation.v); OSWriteBigInt16(value, 12, ent.fdlocation.h); OSWriteBigInt16(value, 14, ent.u.file.window); // FXInfo OSWriteBigInt16(value, 16, ((FXInfo*)(ent.u.file.xinfo))->fdIconID); OSWriteBigInt16(value, 18, ((FXInfo*)(ent.u.file.xinfo))->fdUnused[0]); OSWriteBigInt16(value, 20, ((FXInfo*)(ent.u.file.xinfo))->fdUnused[1]); OSWriteBigInt16(value, 22, ((FXInfo*)(ent.u.file.xinfo))->fdUnused[2]); OSWriteBigInt16(value, 24, ((FXInfo*)(ent.u.file.xinfo))->fdUnused[3]); OSWriteBigInt16(value, 26, ((FXInfo*)(ent.u.file.xinfo))->fdComment); OSWriteBigInt32(value, 28, ((FXInfo*)(ent.u.file.xinfo))->fdPutAway); } free(hfspath); return 32; } else if (strcmp(name, XATTR_RESOURCEFORK_NAME) == 0 && (!(ent.flags & HFS_ISDIR)) && ent.u.file.rsize) { // resource fork if (value == NULL) { free(hfspath); return ent.u.file.rsize-position; } int bw = ent.u.file.rsize-position; if (bw > size) bw = size; // copy resource fork hfsfile *fp = hfs_open(NULL, hfspath); hfs_setfork(fp, 1); hfs_seek(fp, position, SEEK_SET); hfs_read(fp, value, bw); hfs_close(fp); // the end free(hfspath); return bw; } free(hfspath); dprintf("getxattr: ENOATTR\n"); return -ENOATTR; }