github.com/cellofellow/gopkg@v0.0.0-20140722061823-eec0544a62ad/image/jxr/jxrlib/src/jxr_stream_discard.c (about)

     1  // Copyright 2014 <chaishushan{AT}gmail.com>. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  #include "jxr.h"
     6  #include "jxr_private.h"
     7  
     8  static
     9  ERR CloseWS_Discard(struct WMPStream** ppWS)
    10  {
    11  	ERR err = WMP_errSuccess;
    12  
    13  	Call(WMPFree((void**)ppWS));
    14  
    15  Cleanup:
    16  	return err;
    17  }
    18  
    19  static
    20  Bool EOSWS_Discard(struct WMPStream* pWS)
    21  {
    22  	return pWS->state.buf.cbBuf <= pWS->state.buf.cbCur;
    23  }
    24  
    25  static
    26  ERR ReadWS_Discard(struct WMPStream* pWS, void* pv, size_t cb)
    27  {
    28  	ERR err = WMP_errSuccess;
    29  
    30  	if(pWS->state.buf.cbBuf < pWS->state.buf.cbCur) {
    31  		return WMP_errBufferOverflow;
    32  	}
    33  	if(pWS->state.buf.cbBuf < pWS->state.buf.cbCur + cb) {
    34  		cb = pWS->state.buf.cbBuf - pWS->state.buf.cbCur;
    35  	}
    36  	// memcpy(pv, pWS->state.buf.pbBuf + pWS->state.buf.cbCur, cb);
    37  	memset(pv, 0, cb);
    38  	pWS->state.buf.cbCur += cb;
    39  
    40  Cleanup:
    41  	return err;
    42  }
    43  
    44  static
    45  ERR WriteWS_Discard(struct WMPStream* pWS, const void* pv, size_t cb)
    46  {
    47  	ERR err = WMP_errSuccess;
    48  
    49  	FailIf(pWS->state.buf.cbCur + cb < pWS->state.buf.cbCur, WMP_errBufferOverflow);
    50  	FailIf(pWS->state.buf.cbBuf < pWS->state.buf.cbCur + cb, WMP_errBufferOverflow);
    51  
    52  	// memcpy(pWS->state.buf.pbBuf + pWS->state.buf.cbCur, pv, cb);
    53  	pWS->state.buf.cbCur += cb;
    54  
    55  Cleanup:
    56  	return err;
    57  }
    58  
    59  static
    60  ERR SetPosWS_Discard(struct WMPStream* pWS, size_t offPos)
    61  {
    62  	// While the following condition is possibly useful, failure occurs
    63  	// at the end of a file since packets beyond the end may be accessed
    64  	// FailIf(pWS->state.buf.cbBuf < offPos, WMP_errBufferOverflow);
    65  	pWS->state.buf.cbCur = offPos;
    66  	return WMP_errSuccess;
    67  }
    68  
    69  static
    70  ERR GetPosWS_Discard(struct WMPStream* pWS, size_t* poffPos)
    71  {
    72  	*poffPos = pWS->state.buf.cbCur;
    73  	return WMP_errSuccess;
    74  }
    75  
    76  ERR CreateWS_Discard(struct WMPStream** ppWS)
    77  {
    78  	ERR err = WMP_errSuccess;
    79  	struct WMPStream* pWS = NULL;
    80  
    81  	Call(WMPAlloc((void** )ppWS, sizeof(**ppWS)));
    82  	pWS = *ppWS;
    83  
    84  	pWS->state.buf.pbBuf = NULL;
    85  	pWS->state.buf.cbBuf = (1<<30); // 1GB
    86  	pWS->state.buf.cbCur = 0;
    87  
    88  	pWS->Close = CloseWS_Discard;
    89  	pWS->EOS = EOSWS_Discard;
    90  
    91  	pWS->Read = ReadWS_Discard;
    92  	pWS->Write = WriteWS_Discard;
    93  
    94  	pWS->SetPos = SetPosWS_Discard;
    95  	pWS->GetPos = GetPosWS_Discard;
    96  
    97  Cleanup:
    98  	return err;
    99  }