ebmlvoid.c 5.58 KB
Newer Older
1
/*
Steve Lhomme's avatar
Steve Lhomme committed
2
 * $Id$
3
 * Copyright (c) 2008-2010, Matroska (non-profit organisation)
4 5 6 7 8 9 10 11 12
 * 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.
13
 *     * Neither the name of the Matroska assocation nor the
14 15 16
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
17
 * THIS SOFTWARE IS PROVIDED BY the Matroska association ``AS IS'' AND ANY
18 19 20 21 22 23 24 25 26 27 28
 * 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 The Matroska Foundation 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 "ebml/ebml.h"
29
#include "ebml/ebml_internal.h"
30 31 32 33 34 35

static bool_t IsDefaultValue(const ebml_element *Element)
{
    return 0;
}

36
static err_t ReadData(ebml_element *Element, stream *Input, const ebml_parser_context *ParserContext, bool_t AllowDummyElt, int Scope, size_t DepthCheckCRC)
37 38 39 40 41
{
	EBML_ElementSkipData(Element,Input,ParserContext,NULL,AllowDummyElt);
	return ERR_NONE;
}

42
#if defined(CONFIG_EBML_WRITING)
43
static err_t RenderData(ebml_element *Element, stream *Output, bool_t bForceWithoutMandatory, bool_t bWithDefault, filepos_t *Rendered)
44
{
45
    size_t Written, Left = (size_t)Element->DataSize;
46 47 48 49 50 51 52 53 54 55
    err_t Err = ERR_NONE;
    uint8_t Buf[2*1024]; // write 2 KB chunks at a time
    memset(Buf,0,sizeof(Buf));
    while (Err==ERR_NONE && Left)
    {
        Err = Stream_Write(Output,Buf,min(Left,sizeof(Buf)),&Written);
        if (Err == ERR_NONE)
            Left -= min(Left,sizeof(Buf));
    }
    if (Rendered)
56
        *Rendered = Element->DataSize - Left;
57 58 59 60
    return Err;
}
#endif

61 62 63 64 65 66 67 68 69 70
static ebml_element *Copy(const ebml_element *Element, const void *Cookie)
{
    ebml_element *Result = EBML_ElementCreate(Element,Element->Context,0,Cookie);
    if (Result)
    {
        Result->bValueIsSet = Element->bValueIsSet;
        Result->DataSize = Element->DataSize;
        Result->ElementPosition = Element->ElementPosition;
        Result->SizeLength = Element->SizeLength;
        Result->SizePosition = Element->SizePosition;
71
        Result->bNeedDataSizeUpdate = Element->bNeedDataSizeUpdate;
72 73 74 75
    }
    return Result;
}

76 77
META_START(EBMLVoid_Class,EBML_VOID_CLASS)
META_VMT(TYPE_FUNC,ebml_element_vmt,IsDefaultValue,IsDefaultValue)
78
META_VMT(TYPE_FUNC,ebml_element_vmt,ReadData,ReadData)
79 80 81
#if defined(CONFIG_EBML_WRITING)
META_VMT(TYPE_FUNC,ebml_element_vmt,RenderData,RenderData)
#endif
82
META_VMT(TYPE_FUNC,ebml_element_vmt,Copy,Copy)
83 84
META_END(EBML_ELEMENT_CLASS)

85
#if defined(CONFIG_EBML_WRITING)
86
bool_t EBML_VoidSetFullSize(ebml_element *Void, filepos_t DataSize)
87 88
{
    assert(Node_IsPartOf(Void,EBML_VOID_CLASS));
89
    Void->DataSize = DataSize - 1 - EBML_CodedSizeLength(DataSize,0,1); // 1 is the length of the Void ID
90
    Void->bValueIsSet = 1;
91
    Void->bNeedDataSizeUpdate = 0;
92
    return Void->DataSize >= 0;
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
}

filepos_t EBML_VoidReplaceWith(ebml_element *Void, ebml_element *ReplacedWith, stream *Output, bool_t ComeBackAfterward, bool_t bWithDefault)
{
    filepos_t CurrentPosition;
    assert(Node_IsPartOf(Void,EBML_VOID_CLASS));

	EBML_ElementUpdateSize(ReplacedWith,bWithDefault,0);
	if (EBML_ElementFullSize(Void,1) < EBML_ElementFullSize(ReplacedWith,1))
		// the element can't be written here !
		return INVALID_FILEPOS_T;
	if (EBML_ElementFullSize(Void,1) - EBML_ElementFullSize(ReplacedWith,1) == 1)
		// there is not enough space to put a filling element
		return INVALID_FILEPOS_T;

	CurrentPosition = Stream_Seek(Output,0,SEEK_CUR);

    Stream_Seek(Output,Void->ElementPosition,SEEK_SET);
111
    EBML_ElementRender(ReplacedWith,Output,bWithDefault,0,1,NULL);
112 113 114 115 116 117 118 119

    if (EBML_ElementFullSize(Void,1) - EBML_ElementFullSize(ReplacedWith,1) > 1)
    {
        // fill the rest with another void element
        ebml_element *aTmp = EBML_ElementCreate(Void,Void->Context,0,NULL);
        if (aTmp)
        {
            filepos_t HeadBefore,HeadAfter;
120
            EBML_VoidSetFullSize(aTmp, EBML_ElementFullSize(Void,1) - EBML_ElementFullSize(ReplacedWith,1));
121 122 123
            HeadBefore = EBML_ElementFullSize(aTmp,1) - aTmp->DataSize;
            aTmp->DataSize = aTmp->DataSize - EBML_CodedSizeLength(aTmp->DataSize, aTmp->SizeLength, EBML_ElementIsFiniteSize(aTmp));
            HeadAfter = EBML_ElementFullSize(aTmp,1) - aTmp->DataSize;
124
            if (HeadBefore != HeadAfter)
125
                aTmp->SizeLength = (int8_t)(EBML_CodedSizeLength(aTmp->DataSize, aTmp->SizeLength, EBML_ElementIsFiniteSize(aTmp)) - (HeadAfter - HeadBefore));
126 127 128 129 130 131 132 133 134 135
            EBML_ElementRenderHead(aTmp,Output,0,NULL);
            NodeDelete((node*)aTmp);
        }
    }

	if (ComeBackAfterward)
        Stream_Seek(Output,CurrentPosition,SEEK_SET);

	return EBML_ElementFullSize(Void,1);
}
136
#endif