Mercurial > hg > dmlib
view src/dmbstr.c @ 812:1e5cf1144f36
Move library source under src/ subdirectory.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Fri, 16 May 2014 03:22:39 +0300 |
parents | dmbstr.c@c63e24f9aa9a |
children | 27d041472c8f |
line wrap: on
line source
/* * DMLib * -- Simple bitstream I/O functions * Programmed and designed by Matti 'ccr' Hamalainen * (C) Copyright 2012 Tecnic Software productions (TNSP) */ #include "dmbstr.h" static int dmPutByteFILE(DMBitStreamContext *ctx, Uint8 val) { return fputc(val, (FILE *) ctx->fp); } void dmInitBitStreamContext(DMBitStreamContext *ctx) { ctx->buf = 0; ctx->bytecnt = 0; ctx->bitcnt = 8; } int dmInitBitStreamFILE(DMBitStreamContext *ctx, FILE *fp) { if (ctx == NULL || fp == NULL) return DMERR_NULLPTR; ctx->putByte = dmPutByteFILE; ctx->fp = (void *) fp; dmInitBitStreamContext(ctx); return DMERR_OK; } BOOL dmPutBits(DMBitStreamContext *ctx, const int val, const int n) { int i; unsigned int mask = 1 << (n - 1); for (i = 0; i < n; i++) { ctx->buf <<= 1; if (val & mask) ctx->buf |= 1; mask >>= 1; ctx->bitcnt--; if (ctx->bitcnt == 0) { if (ctx->putByte(ctx, (ctx->buf & 0xff)) == EOF) return FALSE; ctx->bitcnt = 8; ctx->bytecnt++; } } return TRUE; } int dmFlushBitStream(DMBitStreamContext *ctx) { if (ctx == NULL) return DMERR_NULLPTR; if (ctx->bitcnt != 8) dmPutBits(ctx, 0, ctx->bitcnt); return 0; }