exip  Alpha 0.5.4
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ioUtil.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 "ioUtil.h"
19 
20 void moveBitPointer(EXIStream* strm, unsigned int bitPositions)
21 {
22  int nbits;
23 
24  strm->context.bufferIndx += bitPositions/8;
25  nbits = bitPositions % 8;
26  if(nbits < 8 - strm->context.bitPointer) // The remaining (0-7) bit positions can be moved within the current byte
27  {
28  strm->context.bitPointer += nbits;
29  }
30  else
31  {
32  strm->context.bufferIndx += 1;
33  strm->context.bitPointer = nbits - (8 - strm->context.bitPointer);
34  }
35 }
36 
37 unsigned char getBitsNumber(uint64_t val)
38 {
39  switch(val)
40  {
41  case 0:
42  return 0;
43  case 1:
44  return 1;
45  case 2:
46  return 2;
47  case 3:
48  return 2;
49  case 4:
50  return 3;
51  case 5:
52  return 3;
53  case 6:
54  return 3;
55  case 7:
56  return 3;
57  case 8:
58  return 4;
59  case 9:
60  return 4;
61  case 10:
62  return 4;
63  case 11:
64  return 4;
65  case 12:
66  return 4;
67  case 13:
68  return 4;
69  case 14:
70  return 4;
71  case 15:
72  return 4;
73  default:
74  {
75  if(val < 32)
76  return 5;
77  else
78  return log2INT(val) + 1;
79  }
80  }
81 }
82 
83 unsigned int log2INT(uint64_t val)
84 {
85  const uint64_t b[] = {0x2, 0xC, 0xF0, 0xFF00, 0xFFFF0000, 0xFFFFFFFF00000000};
86  const unsigned int S[] = {1, 2, 4, 8, 16, 32};
87  int i;
88 
89  unsigned int r = 0; // result of log2(v) will go here
90  for (i = 5; i >= 0; i--) // unroll for speed...
91  {
92  if (val & b[i])
93  {
94  val >>= S[i];
95  r |= S[i];
96  }
97  }
98  return r;
99 }
100 
101 errorCode readEXIChunkForParsing(EXIStream* strm, unsigned int numBytesToBeRead)
102 {
103  Index bytesCopied = strm->buffer.bufContent - strm->context.bufferIndx;
104  Index bytesRead = 0;
105 
106  if(strm->buffer.ioStrm.readWriteToStream == NULL)
108 
109  /* Checks for possible overlaps when copying the left Over Bits,
110  * normally should not happen when the size of strm->buffer is set
111  * reasonably and not too small */
112  if(2*bytesCopied > strm->buffer.bufLen)
113  {
114  DEBUG_MSG(ERROR, DEBUG_STREAM_IO, ("\n> The size of strm->buffer is too small! Set to at least: %d", 2*bytesCopied));
116  }
117 
118  memcpy(strm->buffer.buf, strm->buffer.buf + strm->context.bufferIndx, bytesCopied);
119 
120  bytesRead = strm->buffer.ioStrm.readWriteToStream(strm->buffer.buf + bytesCopied, strm->buffer.bufLen - bytesCopied, strm->buffer.ioStrm.stream);
121  strm->buffer.bufContent = bytesCopied + bytesRead;
122  if(strm->buffer.bufContent < numBytesToBeRead)
123  return EXIP_UNEXPECTED_ERROR;
124 
125  strm->context.bufferIndx = 0;
126 
127  return EXIP_OK;
128 }
129 
131 {
132  char leftOverBits;
133  Index numBytesWritten = 0;
134 
135  if(strm->buffer.ioStrm.readWriteToStream == NULL)
137 
138  leftOverBits = strm->buffer.buf[strm->context.bufferIndx];
139 
140  numBytesWritten = strm->buffer.ioStrm.readWriteToStream(strm->buffer.buf, strm->context.bufferIndx, strm->buffer.ioStrm.stream);
141  if(numBytesWritten < strm->context.bufferIndx)
142  return EXIP_UNEXPECTED_ERROR;
143 
144  strm->buffer.buf[0] = leftOverBits;
145  strm->context.bufferIndx = 0;
146 
147  return EXIP_OK;
148 }