2 * Copyright (c) 2007 Vreixo Formoso
3 * Copyright (c) 2009 Thomas Schmitt
5 * This file is part of the libisofs project; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License version 2
7 * or later as published by the Free Software Foundation.
8 * See COPYING file for details.
10 * Memory stream extracted for use in lxdream by Nathan Keynes 2010.
23 #define MIN(a,b) ((a)<=(b) ? (a) : (b))
26 #define ISO_MEM_FS_ID 4
28 static ino_t mem_serial_id = (ino_t)1;
33 ssize_t offset; /* -1 if stream closed */
39 int mem_open(IsoStream *stream)
43 return ISO_NULL_POINTER;
45 data = (MemStreamData*)stream->data;
46 if (data->offset != -1) {
47 return ISO_FILE_ALREADY_OPENED;
54 int mem_close(IsoStream *stream)
58 return ISO_NULL_POINTER;
60 data = (MemStreamData*)stream->data;
61 if (data->offset == -1) {
62 return ISO_FILE_NOT_OPENED;
69 off_t mem_get_size(IsoStream *stream)
72 data = (MemStreamData*)stream->data;
74 return (off_t)data->size;
78 int mem_read(IsoStream *stream, void *buf, size_t count)
82 if (stream == NULL || buf == NULL) {
83 return ISO_NULL_POINTER;
86 return ISO_WRONG_ARG_VALUE;
90 if (data->offset == -1) {
91 return ISO_FILE_NOT_OPENED;
94 if (data->offset >= data->size) {
98 len = MIN(count, data->size - data->offset);
99 memcpy(buf, data->buf + data->offset, len);
105 int mem_is_repeatable(IsoStream *stream)
111 void mem_get_id(IsoStream *stream, unsigned int *fs_id, dev_t *dev_id,
115 data = (MemStreamData*)stream->data;
116 *fs_id = ISO_MEM_FS_ID;
118 *ino_id = data->ino_id;
122 void mem_free(IsoStream *stream)
125 data = (MemStreamData*)stream->data;
130 static IsoStreamIface mem_stream_class = {
143 * Create a stream for reading from a arbitrary memory buffer.
144 * When the Stream refcount reach 0, the buffer is free(3).
147 * 1 sucess, < 0 error
149 int iso_mem_stream_new(unsigned char *buf, size_t size, IsoStream **stream)
154 if (buf == NULL || stream == NULL) {
155 return ISO_NULL_POINTER;
158 str = malloc(sizeof(IsoStream));
160 return ISO_OUT_OF_MEM;
162 data = malloc(sizeof(MemStreamData));
165 return ISO_OUT_OF_MEM;
172 data->ino_id = mem_serial_id++;
176 str->class = &mem_stream_class;
.