#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STDIO_H
#include <stdio.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#include "types.h"
#include "attrib.h"
#include "bitmap.h"
#include "debug.h"
#include "logging.h"
#include "misc.h"
void ntfs_bit_set(u8 *bitmap, const u64 bit, const u8 new_value)
{
if (!bitmap || new_value > 1)
return;
if (!new_value)
bitmap[bit >> 3] &= ~(1 << (bit & 7));
else
bitmap[bit >> 3] |= (1 << (bit & 7));
}
char ntfs_bit_get(const u8 *bitmap, const u64 bit)
{
if (!bitmap)
return -1;
return (bitmap[bit >> 3] >> (bit & 7)) & 1;
}
char ntfs_bit_get_and_set(u8 *bitmap, const u64 bit, const u8 new_value)
{
register u8 old_bit, shift;
if (!bitmap || new_value > 1)
return -1;
shift = bit & 7;
old_bit = (bitmap[bit >> 3] >> shift) & 1;
if (new_value != old_bit)
bitmap[bit >> 3] ^= 1 << shift;
return old_bit;
}
static int ntfs_bitmap_set_bits_in_run(ntfs_attr *na, s64 start_bit,
s64 count, int value)
{
s64 bufsize, br;
u8 *buf, *lastbyte_buf;
int bit, firstbyte, lastbyte, lastbyte_pos, tmp, ret = -1;
if (!na || start_bit < 0 || count < 0) {
errno = EINVAL;
ntfs_log_perror("%s: Invalid argument (%p, %lld, %lld)",
__FUNCTION__, na, (long long)start_bit, (long long)count);
return -1;
}
bit = start_bit & 7;
if (bit)
firstbyte = 1;
else
firstbyte = 0;
bufsize = ((count - (bit ? 8 - bit : 0) + 7) >> 3) + firstbyte;
if (bufsize > 8192)
bufsize = 8192;
buf = ntfs_malloc(bufsize);
if (!buf)
return -1;
memset(buf, value ? 0xff : 0, bufsize);
if (bit) {
br = ntfs_attr_pread(na, start_bit >> 3, 1, buf);
if (br != 1) {
if (br >= 0)
errno = EIO;
goto free_err_out;
}
while ((bit & 7) && count--) {
if (value)
*buf |= 1 << bit++;
else
*buf &= ~(1 << bit++);
}
start_bit = (start_bit + 7) & ~7;
}
lastbyte = 0;
lastbyte_buf = NULL;
bit = count & 7;
do {
if (count > 0 && bit) {
lastbyte_pos = ((count + 7) >> 3) + firstbyte;
if (!lastbyte_pos) {
ntfs_log_error("Lastbyte is zero. Leaving "
"inconsistent metadata.\n");
errno = EIO;
goto free_err_out;
}
if (lastbyte_pos <= bufsize) {
lastbyte_buf = buf + lastbyte_pos - 1;
br = ntfs_attr_pread(na, (start_bit + count) >>
3, 1, lastbyte_buf);
if (br != 1) {
if (br >= 0)
errno = EIO;
ntfs_log_perror("Reading of last byte "
"failed (%lld). Leaving inconsistent "
"metadata", (long long)br);
goto free_err_out;
}
while (bit && count--) {
if (value)
*lastbyte_buf |= 1 << --bit;
else
*lastbyte_buf &= ~(1 << --bit);
}
bit = 0;
lastbyte = 1;
}
}
tmp = (start_bit >> 3) - firstbyte;
br = ntfs_attr_pwrite(na, tmp, bufsize, buf);
if (br != bufsize) {
if (br >= 0)
errno = EIO;
ntfs_log_perror("Failed to write buffer to bitmap "
"(%lld != %lld). Leaving inconsistent metadata",
(long long)br, (long long)bufsize);
goto free_err_out;
}
tmp = (bufsize - firstbyte - lastbyte) << 3;
if (firstbyte) {
firstbyte = 0;
*buf = value ? 0xff : 0;
}
start_bit += tmp;
count -= tmp;
if (bufsize > (tmp = (count + 7) >> 3))
bufsize = tmp;
if (lastbyte && count != 0) {
ntfs_log_error("Last buffer but count is not zero "
"(%lld). Leaving inconsistent metadata.\n",
(long long)count);
errno = EIO;
goto free_err_out;
}
} while (count > 0);
ret = 0;
free_err_out:
free(buf);
return ret;
}
int ntfs_bitmap_set_run(ntfs_attr *na, s64 start_bit, s64 count)
{
int ret;
ntfs_log_enter("Set from bit %lld, count %lld\n",
(long long)start_bit, (long long)count);
ret = ntfs_bitmap_set_bits_in_run(na, start_bit, count, 1);
ntfs_log_leave("\n");
return ret;
}
int ntfs_bitmap_clear_run(ntfs_attr *na, s64 start_bit, s64 count)
{
int ret;
ntfs_log_enter("Clear from bit %lld, count %lld\n",
(long long)start_bit, (long long)count);
ret = ntfs_bitmap_set_bits_in_run(na, start_bit, count, 0);
ntfs_log_leave("\n");
return ret;
}