Search
lxdream.org :: lxdream/src/drivers/cdrom/isomem.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/drivers/cdrom/isomem.c
changeset 1227:cf6126df0398
prev1179:af1c5d7d5a5a
author nkeynes
date Thu Feb 23 14:59:58 2012 +1000 (8 years ago)
permissions -rw-r--r--
last change mem_stream_class should be static (to avoid potential conflicts with the
real one)
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  */
    14 #include <stdlib.h>
    15 #include <string.h>
    16 #include <limits.h>
    17 #include <stdio.h>
    18 #include <stdint.h>
    19 #include <libisofs.h>
    22 #ifndef MIN
    23 #define MIN(a,b) ((a)<=(b) ? (a) : (b))
    24 #endif
    26 #define ISO_MEM_FS_ID 4
    28 static ino_t mem_serial_id = (ino_t)1;
    30 typedef struct
    31 {
    32     uint8_t *buf;
    33     ssize_t offset; /* -1 if stream closed */
    34     ino_t ino_id;
    35     size_t size;
    36 } MemStreamData;
    38 static
    39 int mem_open(IsoStream *stream)
    40 {
    41     MemStreamData *data;
    42     if (stream == NULL) {
    43         return ISO_NULL_POINTER;
    44     }
    45     data = (MemStreamData*)stream->data;
    46     if (data->offset != -1) {
    47         return ISO_FILE_ALREADY_OPENED;
    48     }
    49     data->offset = 0;
    50     return ISO_SUCCESS;
    51 }
    53 static
    54 int mem_close(IsoStream *stream)
    55 {
    56     MemStreamData *data;
    57     if (stream == NULL) {
    58         return ISO_NULL_POINTER;
    59     }
    60     data = (MemStreamData*)stream->data;
    61     if (data->offset == -1) {
    62         return ISO_FILE_NOT_OPENED;
    63     }
    64     data->offset = -1;
    65     return ISO_SUCCESS;
    66 }
    68 static
    69 off_t mem_get_size(IsoStream *stream)
    70 {
    71     MemStreamData *data;
    72     data = (MemStreamData*)stream->data;
    74     return (off_t)data->size;
    75 }
    77 static
    78 int mem_read(IsoStream *stream, void *buf, size_t count)
    79 {
    80     size_t len;
    81     MemStreamData *data;
    82     if (stream == NULL || buf == NULL) {
    83         return ISO_NULL_POINTER;
    84     }
    85     if (count == 0) {
    86         return ISO_WRONG_ARG_VALUE;
    87     }
    88     data = stream->data;
    90     if (data->offset == -1) {
    91         return ISO_FILE_NOT_OPENED;
    92     }
    94     if (data->offset >= data->size) {
    95         return 0; /* EOF */
    96     }
    98     len = MIN(count, data->size - data->offset);
    99     memcpy(buf, data->buf + data->offset, len);
   100     data->offset += len;
   101     return len;
   102 }
   104 static
   105 int mem_is_repeatable(IsoStream *stream)
   106 {
   107     return 1;
   108 }
   110 static
   111 void mem_get_id(IsoStream *stream, unsigned int *fs_id, dev_t *dev_id,
   112                 ino_t *ino_id)
   113 {
   114     MemStreamData *data;
   115     data = (MemStreamData*)stream->data;
   116     *fs_id = ISO_MEM_FS_ID;
   117     *dev_id = 0;
   118     *ino_id = data->ino_id;
   119 }
   121 static
   122 void mem_free(IsoStream *stream)
   123 {
   124     MemStreamData *data;
   125     data = (MemStreamData*)stream->data;
   126     free(data->buf);
   127     free(data);
   128 }
   130 static IsoStreamIface mem_stream_class = {
   131     0,
   132     "mem ",
   133     mem_open,
   134     mem_close,
   135     mem_get_size,
   136     mem_read,
   137     mem_is_repeatable,
   138     mem_get_id,
   139     mem_free
   140 };
   142 /**
   143  * Create a stream for reading from a arbitrary memory buffer.
   144  * When the Stream refcount reach 0, the buffer is free(3).
   145  *
   146  * @return
   147  *      1 sucess, < 0 error
   148  */
   149 int iso_mem_stream_new(unsigned char *buf, size_t size, IsoStream **stream)
   150 {
   151     IsoStream *str;
   152     MemStreamData *data;
   154     if (buf == NULL || stream == NULL) {
   155         return ISO_NULL_POINTER;
   156     }
   158     str = malloc(sizeof(IsoStream));
   159     if (str == NULL) {
   160         return ISO_OUT_OF_MEM;
   161     }
   162     data = malloc(sizeof(MemStreamData));
   163     if (data == NULL) {
   164         free(str);
   165         return ISO_OUT_OF_MEM;
   166     }
   168     /* fill data */
   169     data->buf = buf;
   170     data->size = size;
   171     data->offset = -1;
   172     data->ino_id = mem_serial_id++;
   174     str->refcount = 1;
   175     str->data = data;
   176     str->class = &mem_stream_class;
   178     *stream = str;
   179     return ISO_SUCCESS;
   180 }
.