exip  Alpha 0.5.4
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
streamWrite.c
Go to the documentation of this file.
1 /*==================================================================*\
2 | EXIP - Embeddable EXI Processor in C |
3 |--------------------------------------------------------------------|
4 | This work is licensed under BSD 3-Clause License |
5 | The full license terms and conditions are located in LICENSE.txt |
6 \===================================================================*/
7 
18 #include "streamWrite.h"
19 #include "ioUtil.h"
20 
21 extern const unsigned char BIT_MASK[];
22 
23 errorCode writeNextBit(EXIStream* strm, boolean bit_val)
24 {
25  if(strm->buffer.bufLen <= strm->context.bufferIndx) // the whole buffer is filled! flush it!
26  {
27  Index numBytesWritten = 0;
28  if(strm->buffer.ioStrm.readWriteToStream == NULL)
30  numBytesWritten = strm->buffer.ioStrm.readWriteToStream(strm->buffer.buf, strm->buffer.bufLen, strm->buffer.ioStrm.stream);
31  if(numBytesWritten < strm->buffer.bufLen)
33  strm->context.bitPointer = 0;
34  strm->context.bufferIndx = 0;
35  }
36 
37  if(bit_val == FALSE)
38  strm->buffer.buf[strm->context.bufferIndx] = strm->buffer.buf[strm->context.bufferIndx] & (~(1<<REVERSE_BIT_POSITION(strm->context.bitPointer)));
39  else
41 
42  moveBitPointer(strm, 1);
43  DEBUG_MSG(INFO, DEBUG_STREAM_IO, (" @%u:%u", (unsigned int) strm->context.bufferIndx, strm->context.bitPointer));
44  return EXIP_OK;
45 }
46 
47 errorCode writeNBits(EXIStream* strm, unsigned char nbits, unsigned int bits_val)
48 {
49  unsigned int numBitsWrite = 0; // Number of the bits written so far
50  unsigned char tmp = 0;
51  int bits_in_byte = 0; // Number of bits written in one iteration
52  unsigned int numBytesToBeWritten = ((unsigned int) nbits) / 8 + (8 - strm->context.bitPointer < nbits % 8 );
53 
54  if(strm->buffer.bufLen <= strm->context.bufferIndx + numBytesToBeWritten)
55  {
56  // The buffer end is reached: there are fewer than nbits bits left in the buffer
57  // Flush the buffer if possible
58  errorCode tmp_err_code = EXIP_UNEXPECTED_ERROR;
59 
61  }
62 
63  while(numBitsWrite < nbits)
64  {
65  if((unsigned int)(nbits - numBitsWrite) <= (unsigned int)(8 - strm->context.bitPointer)) // The rest of the unwritten bits can be put in the current byte from the stream
66  bits_in_byte = nbits - numBitsWrite;
67  else // The rest of the unwritten bits are more than the bits in the current byte from the stream
68  bits_in_byte = 8 - strm->context.bitPointer;
69 
70  tmp = (bits_val >> (nbits - numBitsWrite - bits_in_byte)) & BIT_MASK[bits_in_byte];
71  tmp = tmp << (8 - strm->context.bitPointer - bits_in_byte);
72  strm->buffer.buf[strm->context.bufferIndx] = strm->buffer.buf[strm->context.bufferIndx] & (~BIT_MASK[8 - strm->context.bitPointer]); // Initialize the unused bits with 0s
73  strm->buffer.buf[strm->context.bufferIndx] = strm->buffer.buf[strm->context.bufferIndx] | tmp;
74 
75  numBitsWrite += bits_in_byte;
76  moveBitPointer(strm, bits_in_byte);
77  }
78  DEBUG_MSG(INFO, DEBUG_STREAM_IO, (" @%u:%u\n", (unsigned int) strm->context.bufferIndx, strm->context.bitPointer));
79 
80  return EXIP_OK;
81 }