first revision

This commit is contained in:
Laurent Vivier 2005-11-23 22:39:03 +00:00
parent 2b7965a1d9
commit d76f74e1eb
7 changed files with 211 additions and 0 deletions

18
libblock/Makefile Normal file
View File

@ -0,0 +1,18 @@
#
# (c) 2005 Laurent Vivier <LaurentVivier@wanadoo.fr>
#
TOP=$(shell pwd)
CFLAGS = -nostdlib -nodefaultlibs -Wall -Werror -Wno-multichar -fpic -O2
CPPFLAGS = -I$(TOP)/../libmacos -DARCH_M68K -I$(TOP)/../libstream
LIBRARY = libblock.a
SOURCES = block_close.c block_fstat.c block_lseek.c block_open.c block_read.c
HEADERS = libblock.h
all: $(LIBRARY)
include $(TOP)/../Rules.mk

19
libblock/block_close.c Normal file
View File

@ -0,0 +1,19 @@
/*
*
* (c) 2005 Laurent Vivier <LaurentVivier@wanadoo.fr>
*
*/
#include <stdlib.h>
#include "libblock.h"
int block_close(block_FILE *file)
{
if (file == NULL)
return -1;
free(file);
return 0;
}

19
libblock/block_fstat.c Normal file
View File

@ -0,0 +1,19 @@
/*
*
* (c) 2005 Laurent Vivier <LaurentVivier@wanadoo.fr>
*
*/
#include "libblock.h"
int block_fstat(block_FILE *file, struct stream_stat *buf)
{
if (buf == NULL)
return -1;
if (file->size == -1)
return -1;
buf->st_size = file->size;
return 0;
}

31
libblock/block_lseek.c Normal file
View File

@ -0,0 +1,31 @@
/*
*
* (c) 2005 Laurent Vivier <LaurentVivier@wanadoo.fr>
*
*/
#include "libblock.h"
int block_lseek(block_FILE *file, off_t offset, int whence)
{
long new_offset;
switch(whence)
{
case SEEK_SET:
new_offset = offset;
break;
case SEEK_CUR:
new_offset = file->offset + offset;
break;
default:
return -1;
}
if (new_offset < 0)
return -1;
file->offset = new_offset;
return new_offset;
}

52
libblock/block_open.c Normal file
View File

@ -0,0 +1,52 @@
/*
*
* (c) 2005 Laurent Vivier <LaurentVivier@wanadoo.fr>
*
*/
#include <stdlib.h>
#include "libblock.h"
#define NB_SECTORS (18*2)
/*
* path is "<first>,<size>"
* where <first> is the offset of the first byte to read on the device
* and <size> is the number of bytes to read then.
*/
block_FILE *block_open(device_io_t *device, char *path)
{
block_FILE *block;
int blocksize = device->get_blocksize(device);
int first, size;
first = strtol(path, &path, 0);
if ( (*path != ',') && (*path != 0) )
return NULL;
if (*path == ',')
{
size = strtol(path, &path, 0);
if (*path != 0)
return NULL;
}
else
size = -1;
block = (block_FILE *)malloc(sizeof(block_FILE) +
NB_SECTORS * blocksize);
if (block == NULL)
return NULL;
block->base = first;
block->offset = 0;
block->size = size;
block->device = device;
block->current = -1;
block->buffer_size = NB_SECTORS * blocksize;
return block;
}

46
libblock/block_read.c Normal file
View File

@ -0,0 +1,46 @@
/*
*
* (c) 2005 Laurent Vivier <LaurentVivier@wanadoo.fr>
*
*/
#include <string.h>
#include "libblock.h"
size_t block_read(block_FILE *file, void *ptr, size_t size)
{
int read = 0;
int ret;
int blocksize = file->device->get_blocksize(&file->device);
while (size != 0)
{
int part;
int block_nb = (file->offset + file->base) / file->buffer_size;
int block_offset = (file->offset + file->base) % file->buffer_size;
if (block_nb != file->current)
{
ret = file->device->read_sector(file->device,
(block_nb * file->buffer_size) / blocksize,
file->buffer,
file->buffer_size);
if (ret == -1)
return read;
file->current = block_nb;
}
part = file->buffer_size - block_offset;
if (part > size)
part = size;
memcpy(ptr, file->buffer + block_offset, part);
size -= part;
ptr = (char*)ptr + part;
file->offset += part;
read += part;
}
return read;
}

26
libblock/libblock.h Normal file
View File

@ -0,0 +1,26 @@
/*
*
* (c) 2005 Laurent Vivier <LaurentVivier@wanadoo.fr>
*
*/
#include <sys/types.h>
#include <unistd.h>
#include <libstream.h>
typedef struct {
int base;
int offset;
int size;
device_io_t *device;
int current;
int buffer_size;
unsigned char buffer[0];
} block_FILE;
extern block_FILE *block_open(device_io_t *device, char *path);
extern int block_close(block_FILE *file);
extern size_t block_read(block_FILE *file, void *ptr, size_t size);
extern int block_lseek(block_FILE *file, off_t offset, int whence);
extern int block_fstat(block_FILE *file, struct stream_stat *buf);