memstream.c 3.76 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
/*****************************************************************************
 * 
 * Copyright (c) 2008-2010, CoreCodec, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of CoreCodec, Inc. nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY CoreCodec, Inc. ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL CoreCodec, Inc. BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 ****************************************************************************/

#include "file.h"

static err_t MemRead(memstream* p,void* Data,size_t Size,size_t* Readed)
{
    err_t Err = ERR_NONE;
	size_t Pos = p->Pos;
    assert(Pos <= p->Size);

    if (Size > p->Size - Pos)
    {
        Size = p->Size - Pos;
        Err = ERR_END_OF_FILE;
    }

	memcpy(Data,p->Ptr+Pos,Size);
	p->Pos = Pos+Size;

    if (Readed)
        *Readed = Size;

	return Err;
}

53 54 55 56 57
static err_t MemWrite(memstream* p, const void* Data, size_t Size, size_t* Written)
{
    *Written = 0;
    if (Size + p->Pos > p->Size)
        return ERR_INVALID_PARAM;
58
    assert(p->Ptr!=NULL);
59 60 61 62 63 64
    memcpy(p->Ptr + p->Pos, Data, Size);
    p->Pos += Size;
    *Written = Size;
    return ERR_NONE;
}

65
static filepos_t MemSeek(memstream* p,filepos_t VirtualPos,int SeekMode)
66 67 68 69 70
{
	switch (SeekMode)
	{
	default:
	case SEEK_SET: break;
71 72
	case SEEK_CUR: VirtualPos += p->Pos + p->VirtualOffset; break;
	case SEEK_END: VirtualPos += p->Size + p->VirtualOffset; break;
73 74 75
	}

    // TODO: these safety checks should return an error
76
	if (VirtualPos<0)
77
    {
78 79
        assert(VirtualPos>=0);
		VirtualPos=0;
80 81
    }

82
    if (VirtualPos>(filepos_t)p->Size + p->VirtualOffset)
83
    {
84 85
        assert(VirtualPos<=(filepos_t)p->Size);
        VirtualPos=p->Size + p->VirtualOffset;
86 87
    }

88 89
    p->Pos = (size_t)(VirtualPos - p->VirtualOffset);
	return VirtualPos;
90 91 92 93 94 95 96 97 98 99
}

static err_t MemLength(memstream* p, dataid UNUSED_PARAM(Id), filepos_t* Data, size_t UNUSED_PARAM(Size))
{
    *Data = p->Size;
    return ERR_NONE;
}

static err_t MemData(memstream* p, dataid UNUSED_PARAM(Id), const void* Data, size_t Size)
{
100
    assert(Size==0 || Data!=NULL);
101 102 103 104 105 106 107 108 109
	p->Ptr = (const uint8_t*)Data;
	p->Size = Size;
    p->Pos = 0;
	return ERR_NONE;
}

META_START(MemStream_Class,MEMSTREAM_CLASS)
META_CLASS(SIZE,sizeof(memstream))
META_VMT(TYPE_FUNC,stream_vmt,Read,MemRead)
110
META_VMT(TYPE_FUNC,stream_vmt,Write,MemWrite)
111 112 113
META_VMT(TYPE_FUNC,stream_vmt,Seek,MemSeek)
META_PARAM(GET,STREAM_LENGTH,MemLength)
META_PARAM(SET,MEMSTREAM_DATA,MemData)
114
META_DATA(TYPE_FILEPOS,MEMSTREAM_OFFSET,memstream,VirtualOffset)
115 116
META_DATA_RDONLY(TYPE_PTR,MEMSTREAM_PTR,memstream,Ptr)
META_END(STREAM_CLASS)