Blame src/lib/openmj2/bio.c

Packit caffb5
/*
Packit caffb5
 * The copyright in this software is being made available under the 2-clauses
Packit caffb5
 * BSD License, included below. This software may be subject to other third
Packit caffb5
 * party and contributor rights, including patent rights, and no such rights
Packit caffb5
 * are granted under this license.
Packit caffb5
 *
Packit caffb5
 * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
Packit caffb5
 * Copyright (c) 2002-2014, Professor Benoit Macq
Packit caffb5
 * Copyright (c) 2001-2003, David Janssens
Packit caffb5
 * Copyright (c) 2002-2003, Yannick Verschueren
Packit caffb5
 * Copyright (c) 2003-2007, Francois-Olivier Devaux
Packit caffb5
 * Copyright (c) 2003-2014, Antonin Descampe
Packit caffb5
 * Copyright (c) 2005, Herve Drolon, FreeImage Team
Packit caffb5
 * All rights reserved.
Packit caffb5
 *
Packit caffb5
 * Redistribution and use in source and binary forms, with or without
Packit caffb5
 * modification, are permitted provided that the following conditions
Packit caffb5
 * are met:
Packit caffb5
 * 1. Redistributions of source code must retain the above copyright
Packit caffb5
 *    notice, this list of conditions and the following disclaimer.
Packit caffb5
 * 2. Redistributions in binary form must reproduce the above copyright
Packit caffb5
 *    notice, this list of conditions and the following disclaimer in the
Packit caffb5
 *    documentation and/or other materials provided with the distribution.
Packit caffb5
 *
Packit caffb5
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
Packit caffb5
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit caffb5
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit caffb5
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
Packit caffb5
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
Packit caffb5
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Packit caffb5
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Packit caffb5
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Packit caffb5
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Packit caffb5
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Packit caffb5
 * POSSIBILITY OF SUCH DAMAGE.
Packit caffb5
 */
Packit caffb5
Packit caffb5
#include "opj_includes.h"
Packit caffb5
Packit caffb5
/** @defgroup BIO BIO - Individual bit input-output stream */
Packit caffb5
/*@{*/
Packit caffb5
Packit caffb5
/** @name Local static functions */
Packit caffb5
/*@{*/
Packit caffb5
Packit caffb5
/**
Packit caffb5
Write a bit
Packit caffb5
@param bio BIO handle
Packit caffb5
@param b Bit to write (0 or 1)
Packit caffb5
*/
Packit caffb5
static void bio_putbit(opj_bio_t *bio, int b);
Packit caffb5
/**
Packit caffb5
Read a bit
Packit caffb5
@param bio BIO handle
Packit caffb5
@return Returns the read bit
Packit caffb5
*/
Packit caffb5
static int bio_getbit(opj_bio_t *bio);
Packit caffb5
/**
Packit caffb5
Write a byte
Packit caffb5
@param bio BIO handle
Packit caffb5
@return Returns 0 if successful, returns 1 otherwise
Packit caffb5
*/
Packit caffb5
static int bio_byteout(opj_bio_t *bio);
Packit caffb5
/**
Packit caffb5
Read a byte
Packit caffb5
@param bio BIO handle
Packit caffb5
@return Returns 0 if successful, returns 1 otherwise
Packit caffb5
*/
Packit caffb5
static int bio_bytein(opj_bio_t *bio);
Packit caffb5
Packit caffb5
/*@}*/
Packit caffb5
Packit caffb5
/*@}*/
Packit caffb5
Packit caffb5
/*
Packit caffb5
==========================================================
Packit caffb5
   local functions
Packit caffb5
==========================================================
Packit caffb5
*/
Packit caffb5
Packit caffb5
static int bio_byteout(opj_bio_t *bio)
Packit caffb5
{
Packit caffb5
    bio->buf = (bio->buf << 8) & 0xffff;
Packit caffb5
    bio->ct = bio->buf == 0xff00 ? 7 : 8;
Packit caffb5
    if (bio->bp >= bio->end) {
Packit caffb5
        return 1;
Packit caffb5
    }
Packit caffb5
    *bio->bp++ = bio->buf >> 8;
Packit caffb5
    return 0;
Packit caffb5
}
Packit caffb5
Packit caffb5
static int bio_bytein(opj_bio_t *bio)
Packit caffb5
{
Packit caffb5
    bio->buf = (bio->buf << 8) & 0xffff;
Packit caffb5
    bio->ct = bio->buf == 0xff00 ? 7 : 8;
Packit caffb5
    if (bio->bp >= bio->end) {
Packit caffb5
        return 1;
Packit caffb5
    }
Packit caffb5
    bio->buf |= *bio->bp++;
Packit caffb5
    return 0;
Packit caffb5
}
Packit caffb5
Packit caffb5
static void bio_putbit(opj_bio_t *bio, int b)
Packit caffb5
{
Packit caffb5
    if (bio->ct == 0) {
Packit caffb5
        bio_byteout(bio);
Packit caffb5
    }
Packit caffb5
    bio->ct--;
Packit caffb5
    bio->buf |= b << bio->ct;
Packit caffb5
}
Packit caffb5
Packit caffb5
static int bio_getbit(opj_bio_t *bio)
Packit caffb5
{
Packit caffb5
    if (bio->ct == 0) {
Packit caffb5
        bio_bytein(bio);
Packit caffb5
    }
Packit caffb5
    bio->ct--;
Packit caffb5
    return (bio->buf >> bio->ct) & 1;
Packit caffb5
}
Packit caffb5
Packit caffb5
/*
Packit caffb5
==========================================================
Packit caffb5
   Bit Input/Output interface
Packit caffb5
==========================================================
Packit caffb5
*/
Packit caffb5
Packit caffb5
opj_bio_t* bio_create(void)
Packit caffb5
{
Packit caffb5
    opj_bio_t *bio = (opj_bio_t*)opj_malloc(sizeof(opj_bio_t));
Packit caffb5
    return bio;
Packit caffb5
}
Packit caffb5
Packit caffb5
void bio_destroy(opj_bio_t *bio)
Packit caffb5
{
Packit caffb5
    if (bio) {
Packit caffb5
        opj_free(bio);
Packit caffb5
    }
Packit caffb5
}
Packit caffb5
Packit caffb5
int bio_numbytes(opj_bio_t *bio)
Packit caffb5
{
Packit caffb5
    return (bio->bp - bio->start);
Packit caffb5
}
Packit caffb5
Packit caffb5
void bio_init_enc(opj_bio_t *bio, unsigned char *bp, int len)
Packit caffb5
{
Packit caffb5
    bio->start = bp;
Packit caffb5
    bio->end = bp + len;
Packit caffb5
    bio->bp = bp;
Packit caffb5
    bio->buf = 0;
Packit caffb5
    bio->ct = 8;
Packit caffb5
}
Packit caffb5
Packit caffb5
void bio_init_dec(opj_bio_t *bio, unsigned char *bp, int len)
Packit caffb5
{
Packit caffb5
    bio->start = bp;
Packit caffb5
    bio->end = bp + len;
Packit caffb5
    bio->bp = bp;
Packit caffb5
    bio->buf = 0;
Packit caffb5
    bio->ct = 0;
Packit caffb5
}
Packit caffb5
Packit caffb5
void bio_write(opj_bio_t *bio, int v, int n)
Packit caffb5
{
Packit caffb5
    int i;
Packit caffb5
    for (i = n - 1; i >= 0; i--) {
Packit caffb5
        bio_putbit(bio, (v >> i) & 1);
Packit caffb5
    }
Packit caffb5
}
Packit caffb5
Packit caffb5
int bio_read(opj_bio_t *bio, int n)
Packit caffb5
{
Packit caffb5
    int i, v;
Packit caffb5
    v = 0;
Packit caffb5
    for (i = n - 1; i >= 0; i--) {
Packit caffb5
        v += bio_getbit(bio) << i;
Packit caffb5
    }
Packit caffb5
    return v;
Packit caffb5
}
Packit caffb5
Packit caffb5
int bio_flush(opj_bio_t *bio)
Packit caffb5
{
Packit caffb5
    bio->ct = 0;
Packit caffb5
    if (bio_byteout(bio)) {
Packit caffb5
        return 1;
Packit caffb5
    }
Packit caffb5
    if (bio->ct == 7) {
Packit caffb5
        bio->ct = 0;
Packit caffb5
        if (bio_byteout(bio)) {
Packit caffb5
            return 1;
Packit caffb5
        }
Packit caffb5
    }
Packit caffb5
    return 0;
Packit caffb5
}
Packit caffb5
Packit caffb5
int bio_inalign(opj_bio_t *bio)
Packit caffb5
{
Packit caffb5
    bio->ct = 0;
Packit caffb5
    if ((bio->buf & 0xff) == 0xff) {
Packit caffb5
        if (bio_bytein(bio)) {
Packit caffb5
            return 1;
Packit caffb5
        }
Packit caffb5
        bio->ct = 0;
Packit caffb5
    }
Packit caffb5
    return 0;
Packit caffb5
}