63 lines
1.7 KiB
C
Raw Normal View History

2025-04-10 17:31:33 +08:00
#include "bzutil.h"
int32_t BZ2_bzBuffBlkSize (void)
{
#if (defined CHIP_EC616 || defined CHIP_EC616_Z0 || defined CHIP_EC616S || defined CHIP_EC626)
return BZ2_BUFF_BLOCK_SMALL_SIZE;
#else
return BZ2_BUFF_BLOCK_LARGE_SIZE;
#endif
}
void BZ2_bzBuffOpen(buf_handle* hd, uint32_t size, uint8_t* buff)
{
hd->cursor = 0;
hd->encBufSize = size;
hd->encBuf = buff;
}
int32_t BZ2_bzBuffRead (char* buffer, uint32_t size, uint32_t count, buf_handle* handle)
{
int32_t hasread = 0;
if(handle == NULL)
return 0;
if(handle->cursor >= handle->encBufSize){
return 0;
}
if(size*count >= handle->encBufSize - handle->cursor){
hasread = handle->encBufSize - handle->cursor;
memcpy(buffer, handle->encBuf + handle->cursor, hasread);
handle->cursor = handle->encBufSize;
}else{
hasread = size*count;
memcpy(buffer, handle->encBuf + handle->cursor, hasread);
handle->cursor += hasread;
}
//printf("bsRead:%d bytes\n", hasread);
return hasread/size;
}
int32_t BZ2_bzBuffWrite (char* buffer, uint32_t size, uint32_t count, buf_handle* handle)
{
int32_t haswrite = 0;
if(handle == NULL)
return 0;
if(handle->cursor >= handle->encBufSize){
return 0;
}
if(size*count >= handle->encBufSize - handle->cursor){
haswrite = handle->encBufSize - handle->cursor;
memcpy(handle->encBuf + handle->cursor, buffer, haswrite);
handle->cursor = handle->encBufSize;
//printf("reach the handle->encBufSize!!!!!!!!!!!!!!!!!\n");
}else{
haswrite = size*count;
memcpy(handle->encBuf + handle->cursor, buffer, haswrite);
handle->cursor += haswrite;
}
//printf("size=%d, count=%d, haswrite=%d\n", size, count, haswrite);
return haswrite/size;
}