buffer.hxx

00001 // Copyright (C) 2004  EPITA Research and Development Laboratory
00002 //
00003 // This  file is  part of  the Olena  Library.  This  library  is free
00004 // software; you can redistribute it  and/or modify it under the terms
00005 // of the  GNU General  Public License version  2 as published  by the
00006 // Free Software Foundation.
00007 //
00008 // This library is distributed in the hope that it will be useful, but
00009 // WITHOUT  ANY  WARRANTY;  without   even  the  implied  warranty  of
00010 // MERCHANTABILITY or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU
00011 // General Public License for more details.
00012 //
00013 // You should have  received a copy of the  GNU General Public License
00014 // along with  this library; see the  file COPYING.  If  not, write to
00015 // the Free Software Foundation, 59  Temple Place - Suite 330, Boston,
00016 // MA 02111-1307, USA.
00017 //
00018 // As a  special exception, you  may use this  file as part of  a free
00019 // software library without restriction.  Specifically, if other files
00020 // instantiate templates  or use macros or inline  functions from this
00021 // file, or  you compile  this file  and link it  with other  files to
00022 // produce  an executable,  this file  does  not by  itself cause  the
00023 // resulting  executable  to be  covered  by  the  GNU General  Public
00024 // License.   This exception  does  not however  invalidate any  other
00025 // reasons why the executable file might be covered by the GNU General
00026 // Public License.
00027 
00028 #ifndef OLENA_OLN_UTILS_BUFFER_HXX
00029 # define OLENA_OLN_UTILS_BUFFER_HXX
00030 
00031 // Ctor implementation
00032 inline
00033 buffer::buffer(): current_bit_(0),
00034                   data_(),
00035                   len1_(ntg_zero_val(value_type)),
00036                   len2_(ntg_zero_val(value_type)),
00037                   padding_appended_(false),
00038                   length_appended_(false)
00039 {
00040   data_.reserve(length_);
00041   data_.push_back(ntg_zero_val(value_type));
00042 };
00043 
00044 // add implementation
00045 template <class E>
00046 inline
00047 void buffer::add(const E &e, bool count)
00048 {
00049   ntg_storage_type(E)   mask = 1 << (ntg_nbits(E) - 1);
00050   ntg_storage_type(E) e_cast = e;
00051 
00052   while (mask)
00053     {
00054       push_back(e_cast & mask, !count);
00055       mask >>= 1;
00056     }
00057 };
00058 
00059 // push_back implementation
00060 inline
00061 void buffer::push_back(bool bit, bool is_padding)
00062 {
00063   precondition(current_bit_ < 32);
00064 
00065   current_bit_ += 1;
00066   data_.back() *= 2;
00067   if (bit)
00068     data_.back() += ntg_unit_val(value_type);
00069   if (current_bit_ == 32)
00070     {
00071       if (data_.size() == data_.capacity())
00072         data_.reserve(data_.capacity() + length_);
00073       current_bit_ = 0;
00074       data_.push_back(ntg_zero_val(value_type));
00075     }
00076   if (!is_padding)
00077     {
00078       if (len1_ == ntg_max_val(value_type))
00079         {
00080           if (len2_ == ntg_max_val(value_type))
00081             len2_ = ntg_unit_val(value_type);
00082           else
00083             len2_ += ntg_unit_val(value_type);
00084         }
00085       len1_ += ntg_unit_val(value_type);
00086     }
00087 }
00088 
00089 // reorder implementation
00090 inline
00091 buffer::value_type buffer::reorder(value_type x) const
00092 {
00093   ntg_storage_type_(value_type) r = 0;
00094   ntg_storage_type_(value_type) mask = 0xFF;
00095 
00096   for (unsigned i = 0; i < (sizeof(ntg_storage_type_(value_type))); ++i)
00097     {
00098       r <<= 8;
00099       r |= (mask & ntg_storage_type_(value_type)(x)) >> (i * 8);
00100       mask <<= 8;
00101     }
00102   return r;
00103 }
00104 
00105 // [] operator implementation
00106 inline
00107 ntg::int_u32 buffer::operator[] (unsigned n) const
00108 {
00109   precondition(n < data_.size());
00110 
00111   return reorder(data_[n]);
00112 };
00113 
00114 // append padding implementation
00115 inline
00116 void buffer::append_padding()
00117 {
00118   push_back(true, true);
00119   while (((data_.size() * 32) % 512) != 448)
00120     push_back(false, true);
00121   padding_appended_ = true;
00122 };
00123 
00124 // append_length implementation
00125 inline
00126 void buffer::append_length()
00127 {
00128   precondition(padding_appended_);
00129 
00130   add(reorder(len2_), false);
00131   add(reorder(len1_), false);
00132   length_appended_ = true;
00133 };
00134 
00135 // size implementation.
00136 inline
00137 unsigned buffer::size() const
00138 {
00139   return data_.size();
00140 };
00141 
00142 // bit len implementation.
00143 inline
00144 unsigned buffer::bit_len() const
00145 {
00146   return data_.size() * 32;
00147 }
00148 
00149 // is_append_padding implementation.
00150 inline
00151 bool buffer::is_append_padding() const
00152 {
00153   return padding_appended_;
00154 }
00155 
00156 // is append_length implementation.
00157 inline
00158 bool buffer::is_append_length() const
00159 {
00160   return length_appended_;
00161 }
00162 
00163 #endif // !OLENA_OLN_UTILS_BUFFER_HXX

Generated on Thu Apr 15 20:13:07 2004 for Olena by doxygen 1.3.6-20040222