2323#include < lsp-plug.in/io/OutFileStream.h>
2424#include < lsp-plug.in/common/endian.h>
2525
26- #define BITSTREAM_BUFSZ (sizeof (umword_t ) * 8 )
27- #define BITSTREAM_BUFSZ32 (sizeof (uint32_t ) * 8 )
28-
2926namespace lsp
3027{
3128 namespace io
3229 {
30+ static constexpr size_t BITSTREAM_BUFSZ = sizeof (umword_t ) * 8 ;
31+ static constexpr size_t BITSTREAM_BUFSZ32 = sizeof (uint32_t ) * 8 ;
32+
3333 OutBitStream::OutBitStream ()
3434 {
3535 pOS = NULL ;
@@ -234,23 +234,23 @@ namespace lsp
234234
235235 size_t written = 0 ;
236236
237- #ifdef LSP_UNALIGNED_MEMORY_SAFE
238- // x86 allows unaligned access, write with machine words first
239- const umword_t *wptr = reinterpret_cast <const umword_t *>(buf);
240- size_t blocks = count & (~(sizeof (umword_t ) - 1 ));
237+ #ifdef LSP_UNALIGNED_MEMORY_SAFE
238+ // x86 allows unaligned access, write with machine words first
239+ const umword_t *wptr = reinterpret_cast <const umword_t *>(buf);
240+ size_t blocks = count & (~(sizeof (umword_t ) - 1 ));
241241
242- for ( ; written < blocks; written += sizeof (umword_t ))
242+ for ( ; written < blocks; written += sizeof (umword_t ))
243+ {
244+ status_t res = writev (BE_TO_CPU (*(wptr++)), sizeof (umword_t )*8 );
245+ if (res != STATUS_OK)
243246 {
244- status_t res = writev (BE_TO_CPU (*(wptr++)), sizeof (umword_t )*8 );
245- if (res != STATUS_OK)
246- {
247- set_error (res);
248- return (written <= 0 ) ? -res : written;
249- }
247+ set_error (res);
248+ return (written <= 0 ) ? -res : written;
250249 }
250+ }
251251
252- buf = wptr;
253- #endif /* LSP_UNALIGNED_MEMORY_SAFE */
252+ buf = wptr;
253+ #endif /* LSP_UNALIGNED_MEMORY_SAFE */
254254
255255 // Write the rest data with bytes
256256 const uint8_t *bptr = reinterpret_cast <const uint8_t *>(buf);
@@ -274,22 +274,22 @@ namespace lsp
274274
275275 size_t written = 0 ;
276276
277- #ifdef LSP_UNALIGNED_MEMORY_SAFE
278- // x86 allows unaligned memory access, write with machine words first
279- const umword_t *wptr = reinterpret_cast <const umword_t *>(buf);
280- size_t blocks = bits & (~((sizeof (umword_t ) << 3 ) - 1 ));
281- for ( ; written < blocks; written += sizeof (umword_t )*8 )
277+ #ifdef LSP_UNALIGNED_MEMORY_SAFE
278+ // x86 allows unaligned memory access, write with machine words first
279+ const umword_t *wptr = reinterpret_cast <const umword_t *>(buf);
280+ size_t blocks = bits & (~((sizeof (umword_t ) << 3 ) - 1 ));
281+ for ( ; written < blocks; written += sizeof (umword_t )*8 )
282+ {
283+ status_t res = writev (BE_TO_CPU (*(wptr++)), sizeof (umword_t )*8 );
284+ if (res != STATUS_OK)
282285 {
283- status_t res = writev (BE_TO_CPU (*(wptr++)), sizeof (umword_t )*8 );
284- if (res != STATUS_OK)
285- {
286- set_error (res);
287- return (written <= 0 ) ? -res : written;
288- }
286+ set_error (res);
287+ return (written <= 0 ) ? -res : written;
289288 }
289+ }
290290
291- buf = wptr;
292- #endif /* LSP_UNALIGNED_MEMORY_SAFE */
291+ buf = wptr;
292+ #endif /* LSP_UNALIGNED_MEMORY_SAFE */
293293
294294 // Write the rest data with bytes
295295 const uint8_t *bptr = reinterpret_cast <const uint8_t *>(buf);
@@ -389,7 +389,7 @@ namespace lsp
389389 }
390390
391391 size_t avail = lsp_min (bits, BITSTREAM_BUFSZ - nBits);
392- nBuffer = (nBuffer << avail) | (value >> (BITSTREAM_BUFSZ32 - avail));
392+ nBuffer = (avail < BITSTREAM_BUFSZ) ? ( nBuffer << avail) | (value >> (BITSTREAM_BUFSZ32 - avail)) : avail ;
393393 nBits += avail;
394394 bits -= avail;
395395 value <<= avail;
@@ -401,10 +401,22 @@ namespace lsp
401401 status_t OutBitStream::writev (uint64_t value, size_t bits)
402402 {
403403 status_t res;
404- if (pOS == NULL )
405- return set_error (STATUS_CLOSED);
406404
407- #if defined(ARCH_64BIT)
405+ #if defined(ARCH_32BIT)
406+ // Need to write high part?
407+ if (bits > BITSTREAM_BUFSZ32)
408+ {
409+ if ((res = writev (uint32_t (value >> BITSTREAM_BUFSZ32), bits - BITSTREAM_BUFSZ32)) != STATUS_OK)
410+ return res;
411+ bits = BITSTREAM_BUFSZ32;
412+ }
413+
414+ // Write low part
415+ return writev (uint32_t (value), bits);
416+ #else
417+ if (pOS == NULL )
418+ return set_error (STATUS_CLOSED);
419+
408420 value <<= (BITSTREAM_BUFSZ - bits);
409421 while (bits > 0 )
410422 {
@@ -416,26 +428,14 @@ namespace lsp
416428 }
417429
418430 size_t avail = lsp_min (bits, BITSTREAM_BUFSZ - nBits);
419- nBuffer = (nBuffer << avail) | (value >> (BITSTREAM_BUFSZ - avail));
431+ nBuffer = (avail < BITSTREAM_BUFSZ) ? ( nBuffer << avail) | (value >> (BITSTREAM_BUFSZ - avail)) : value ;
420432 nBits += avail;
421433 bits -= avail;
422434 value <<= avail;
423435 }
424436
425437 return set_error (STATUS_OK);
426-
427- #else
428- // Need to write high part?
429- if (bits > BITSTREAM_BUFSZ)
430- {
431- if ((res = writev (uint32_t (value >> BITSTREAM_BUFSZ), bits - BITSTREAM_BUFSZ)) != STATUS_OK)
432- return res;
433- bits = BITSTREAM_BUFSZ;
434- }
435-
436- // Write low part
437- return writev (uint32_t (value), bits);
438- #endif
438+ #endif /* ARCH_32BIT */
439439 }
440440
441441 } /* namespace io */
0 commit comments