filename | src/drivers/cdrom/isomem.c |
changeset | 1152:6464d890cc9e |
next | 1158: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 |
view | annotate | diff | log | raw |
1 /*
2 * Copyright (c) 2007 Vreixo Formoso
3 * Copyright (c) 2009 Thomas Schmitt
4 *
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.
9 *
10 * Memory stream extracted for use in lxdream by Nathan Keynes 2010.
11 */
13 #include <libisofs.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <limits.h>
18 #include <stdio.h>
20 #ifndef MIN
21 #define MIN(a,b) ((a)<=(b) ? (a) : (b))
22 #endif
24 #define ISO_MEM_FS_ID 4
26 ino_t mem_serial_id = (ino_t)1;
28 typedef struct
29 {
30 uint8_t *buf;
31 ssize_t offset; /* -1 if stream closed */
32 ino_t ino_id;
33 size_t size;
34 } MemStreamData;
36 static
37 int mem_open(IsoStream *stream)
38 {
39 MemStreamData *data;
40 if (stream == NULL) {
41 return ISO_NULL_POINTER;
42 }
43 data = (MemStreamData*)stream->data;
44 if (data->offset != -1) {
45 return ISO_FILE_ALREADY_OPENED;
46 }
47 data->offset = 0;
48 return ISO_SUCCESS;
49 }
51 static
52 int mem_close(IsoStream *stream)
53 {
54 MemStreamData *data;
55 if (stream == NULL) {
56 return ISO_NULL_POINTER;
57 }
58 data = (MemStreamData*)stream->data;
59 if (data->offset == -1) {
60 return ISO_FILE_NOT_OPENED;
61 }
62 data->offset = -1;
63 return ISO_SUCCESS;
64 }
66 static
67 off_t mem_get_size(IsoStream *stream)
68 {
69 MemStreamData *data;
70 data = (MemStreamData*)stream->data;
72 return (off_t)data->size;
73 }
75 static
76 int mem_read(IsoStream *stream, void *buf, size_t count)
77 {
78 size_t len;
79 MemStreamData *data;
80 if (stream == NULL || buf == NULL) {
81 return ISO_NULL_POINTER;
82 }
83 if (count == 0) {
84 return ISO_WRONG_ARG_VALUE;
85 }
86 data = stream->data;
88 if (data->offset == -1) {
89 return ISO_FILE_NOT_OPENED;
90 }
92 if (data->offset >= data->size) {
93 return 0; /* EOF */
94 }
96 len = MIN(count, data->size - data->offset);
97 memcpy(buf, data->buf + data->offset, len);
98 data->offset += len;
99 return len;
100 }
102 static
103 int mem_is_repeatable(IsoStream *stream)
104 {
105 return 1;
106 }
108 static
109 void mem_get_id(IsoStream *stream, unsigned int *fs_id, dev_t *dev_id,
110 ino_t *ino_id)
111 {
112 MemStreamData *data;
113 data = (MemStreamData*)stream->data;
114 *fs_id = ISO_MEM_FS_ID;
115 *dev_id = 0;
116 *ino_id = data->ino_id;
117 }
119 static
120 void mem_free(IsoStream *stream)
121 {
122 MemStreamData *data;
123 data = (MemStreamData*)stream->data;
124 free(data->buf);
125 free(data);
126 }
128 IsoStreamIface mem_stream_class = {
129 0,
130 "mem ",
131 mem_open,
132 mem_close,
133 mem_get_size,
134 mem_read,
135 mem_is_repeatable,
136 mem_get_id,
137 mem_free
138 };
140 /**
141 * Create a stream for reading from a arbitrary memory buffer.
142 * When the Stream refcount reach 0, the buffer is free(3).
143 *
144 * @return
145 * 1 sucess, < 0 error
146 */
147 int iso_mem_stream_new(unsigned char *buf, size_t size, IsoStream **stream)
148 {
149 IsoStream *str;
150 MemStreamData *data;
152 if (buf == NULL || stream == NULL) {
153 return ISO_NULL_POINTER;
154 }
156 str = malloc(sizeof(IsoStream));
157 if (str == NULL) {
158 return ISO_OUT_OF_MEM;
159 }
160 data = malloc(sizeof(MemStreamData));
161 if (data == NULL) {
162 free(str);
163 return ISO_OUT_OF_MEM;
164 }
166 /* fill data */
167 data->buf = buf;
168 data->size = size;
169 data->offset = -1;
170 data->ino_id = mem_serial_id++;
172 str->refcount = 1;
173 str->data = data;
174 str->class = &mem_stream_class;
176 *stream = str;
177 return ISO_SUCCESS;
178 }
.