Search
lxdream.org :: lxdream/src/drivers/cdrom/isomem.c :: diff
lxdream 0.9.1
released Jun 29
Download Now
filename src/drivers/cdrom/isomem.c
changeset 1152:6464d890cc9e
next1158:2b237e3417dd
author nkeynes
date Thu Dec 23 17:50:10 2010 +1000 (13 years ago)
permissions -rw-r--r--
last change Clone iso_memory_stream_new() as iso_mem_stream_new(), since current
versions of libisofs have made it unlinkable on linux
file annotate diff log raw
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/drivers/cdrom/isomem.c Thu Dec 23 17:50:10 2010 +1000
1.3 @@ -0,0 +1,178 @@
1.4 +/*
1.5 + * Copyright (c) 2007 Vreixo Formoso
1.6 + * Copyright (c) 2009 Thomas Schmitt
1.7 + *
1.8 + * This file is part of the libisofs project; you can redistribute it and/or
1.9 + * modify it under the terms of the GNU General Public License version 2
1.10 + * or later as published by the Free Software Foundation.
1.11 + * See COPYING file for details.
1.12 + *
1.13 + * Memory stream extracted for use in lxdream by Nathan Keynes 2010.
1.14 + */
1.15 +
1.16 +#include <libisofs.h>
1.17 +
1.18 +#include <stdlib.h>
1.19 +#include <string.h>
1.20 +#include <limits.h>
1.21 +#include <stdio.h>
1.22 +
1.23 +#ifndef MIN
1.24 +#define MIN(a,b) ((a)<=(b) ? (a) : (b))
1.25 +#endif
1.26 +
1.27 +#define ISO_MEM_FS_ID 4
1.28 +
1.29 +ino_t mem_serial_id = (ino_t)1;
1.30 +
1.31 +typedef struct
1.32 +{
1.33 + uint8_t *buf;
1.34 + ssize_t offset; /* -1 if stream closed */
1.35 + ino_t ino_id;
1.36 + size_t size;
1.37 +} MemStreamData;
1.38 +
1.39 +static
1.40 +int mem_open(IsoStream *stream)
1.41 +{
1.42 + MemStreamData *data;
1.43 + if (stream == NULL) {
1.44 + return ISO_NULL_POINTER;
1.45 + }
1.46 + data = (MemStreamData*)stream->data;
1.47 + if (data->offset != -1) {
1.48 + return ISO_FILE_ALREADY_OPENED;
1.49 + }
1.50 + data->offset = 0;
1.51 + return ISO_SUCCESS;
1.52 +}
1.53 +
1.54 +static
1.55 +int mem_close(IsoStream *stream)
1.56 +{
1.57 + MemStreamData *data;
1.58 + if (stream == NULL) {
1.59 + return ISO_NULL_POINTER;
1.60 + }
1.61 + data = (MemStreamData*)stream->data;
1.62 + if (data->offset == -1) {
1.63 + return ISO_FILE_NOT_OPENED;
1.64 + }
1.65 + data->offset = -1;
1.66 + return ISO_SUCCESS;
1.67 +}
1.68 +
1.69 +static
1.70 +off_t mem_get_size(IsoStream *stream)
1.71 +{
1.72 + MemStreamData *data;
1.73 + data = (MemStreamData*)stream->data;
1.74 +
1.75 + return (off_t)data->size;
1.76 +}
1.77 +
1.78 +static
1.79 +int mem_read(IsoStream *stream, void *buf, size_t count)
1.80 +{
1.81 + size_t len;
1.82 + MemStreamData *data;
1.83 + if (stream == NULL || buf == NULL) {
1.84 + return ISO_NULL_POINTER;
1.85 + }
1.86 + if (count == 0) {
1.87 + return ISO_WRONG_ARG_VALUE;
1.88 + }
1.89 + data = stream->data;
1.90 +
1.91 + if (data->offset == -1) {
1.92 + return ISO_FILE_NOT_OPENED;
1.93 + }
1.94 +
1.95 + if (data->offset >= data->size) {
1.96 + return 0; /* EOF */
1.97 + }
1.98 +
1.99 + len = MIN(count, data->size - data->offset);
1.100 + memcpy(buf, data->buf + data->offset, len);
1.101 + data->offset += len;
1.102 + return len;
1.103 +}
1.104 +
1.105 +static
1.106 +int mem_is_repeatable(IsoStream *stream)
1.107 +{
1.108 + return 1;
1.109 +}
1.110 +
1.111 +static
1.112 +void mem_get_id(IsoStream *stream, unsigned int *fs_id, dev_t *dev_id,
1.113 + ino_t *ino_id)
1.114 +{
1.115 + MemStreamData *data;
1.116 + data = (MemStreamData*)stream->data;
1.117 + *fs_id = ISO_MEM_FS_ID;
1.118 + *dev_id = 0;
1.119 + *ino_id = data->ino_id;
1.120 +}
1.121 +
1.122 +static
1.123 +void mem_free(IsoStream *stream)
1.124 +{
1.125 + MemStreamData *data;
1.126 + data = (MemStreamData*)stream->data;
1.127 + free(data->buf);
1.128 + free(data);
1.129 +}
1.130 +
1.131 +IsoStreamIface mem_stream_class = {
1.132 + 0,
1.133 + "mem ",
1.134 + mem_open,
1.135 + mem_close,
1.136 + mem_get_size,
1.137 + mem_read,
1.138 + mem_is_repeatable,
1.139 + mem_get_id,
1.140 + mem_free
1.141 +};
1.142 +
1.143 +/**
1.144 + * Create a stream for reading from a arbitrary memory buffer.
1.145 + * When the Stream refcount reach 0, the buffer is free(3).
1.146 + *
1.147 + * @return
1.148 + * 1 sucess, < 0 error
1.149 + */
1.150 +int iso_mem_stream_new(unsigned char *buf, size_t size, IsoStream **stream)
1.151 +{
1.152 + IsoStream *str;
1.153 + MemStreamData *data;
1.154 +
1.155 + if (buf == NULL || stream == NULL) {
1.156 + return ISO_NULL_POINTER;
1.157 + }
1.158 +
1.159 + str = malloc(sizeof(IsoStream));
1.160 + if (str == NULL) {
1.161 + return ISO_OUT_OF_MEM;
1.162 + }
1.163 + data = malloc(sizeof(MemStreamData));
1.164 + if (data == NULL) {
1.165 + free(str);
1.166 + return ISO_OUT_OF_MEM;
1.167 + }
1.168 +
1.169 + /* fill data */
1.170 + data->buf = buf;
1.171 + data->size = size;
1.172 + data->offset = -1;
1.173 + data->ino_id = mem_serial_id++;
1.174 +
1.175 + str->refcount = 1;
1.176 + str->data = data;
1.177 + str->class = &mem_stream_class;
1.178 +
1.179 + *stream = str;
1.180 + return ISO_SUCCESS;
1.181 +}
.