1ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov/*
2ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov * The copyright in this software is being made available under the 2-clauses
3ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov * BSD License, included below. This software may be subject to other third
4ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov * party and contributor rights, including patent rights, and no such rights
5ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov * are granted under this license.
6ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov *
7ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov * Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
8ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov * Copyright (c) 2002-2014, Professor Benoit Macq
9ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov * Copyright (c) 2003-2007, Francois-Olivier Devaux
10ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov * Copyright (c) 2003-2014, Antonin Descampe
11ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov * Copyright (c) 2005, Herve Drolon, FreeImage Team
12ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov * All rights reserved.
13ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov *
14ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov * Redistribution and use in source and binary forms, with or without
15ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov * modification, are permitted provided that the following conditions
16ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov * are met:
17ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov * 1. Redistributions of source code must retain the above copyright
18ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov *    notice, this list of conditions and the following disclaimer.
19ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov * 2. Redistributions in binary form must reproduce the above copyright
20ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov *    notice, this list of conditions and the following disclaimer in the
21ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov *    documentation and/or other materials provided with the distribution.
22ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov *
23ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
24ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
27ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov * POSSIBILITY OF SUCH DAMAGE.
34ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov */
35ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov
36ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov#include "opj_includes.h"
37ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov
38ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov/*
39ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov==========================================================
40ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov   local functions
41ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov==========================================================
42ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov*/
43ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov
44ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov
45ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov/*
46ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov==========================================================
47ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov   RAW encoding interface
48ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov==========================================================
49ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov*/
50ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov
51ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganovopj_raw_t* opj_raw_create(void) {
52ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov	opj_raw_t *raw = (opj_raw_t*)opj_malloc(sizeof(opj_raw_t));
53ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov	return raw;
54ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov}
55ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov
56ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganovvoid opj_raw_destroy(opj_raw_t *raw) {
57ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov	if(raw) {
58ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov		opj_free(raw);
59ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov	}
60ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov}
61ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov
62ee451cb395940862dad63c85adfe8f2fd55e864cSvet GanovOPJ_UINT32 opj_raw_numbytes(opj_raw_t *raw) {
63ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov	const ptrdiff_t diff = raw->bp - raw->start;
64ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov  assert( diff <= (ptrdiff_t)0xffffffff && diff >= 0 ); /* UINT32_MAX */
65ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov	return (OPJ_UINT32)diff;
66ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov}
67ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov
68ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganovvoid opj_raw_init_dec(opj_raw_t *raw, OPJ_BYTE *bp, OPJ_UINT32 len) {
69ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov	raw->start = bp;
70ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov	raw->lenmax = len;
71ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov	raw->len = 0;
72ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov	raw->c = 0;
73ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov	raw->ct = 0;
74ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov}
75ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov
76ee451cb395940862dad63c85adfe8f2fd55e864cSvet GanovOPJ_UINT32 opj_raw_decode(opj_raw_t *raw) {
77ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov	OPJ_UINT32 d;
78ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov	if (raw->ct == 0) {
79ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov		raw->ct = 8;
80ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov		if (raw->len == raw->lenmax) {
81ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov			raw->c = 0xff;
82ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov		} else {
83ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov			if (raw->c == 0xff) {
84ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov				raw->ct = 7;
85ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov			}
86ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov			raw->c = *(raw->start + raw->len);
87ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov			raw->len++;
88ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov		}
89ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov	}
90ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov	raw->ct--;
91ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov	d = (raw->c >> raw->ct) & 0x01;
92ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov
93ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov	return d;
94ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov}
95ee451cb395940862dad63c85adfe8f2fd55e864cSvet Ganov
96