1 /* -*- c++ -*- */
2 /*
3  * Copyright 2014 Free Software Foundation, Inc.
4  *
5  * This file is part of GNU Radio
6  *
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  *
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 #if HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 
27 #include <gnuradio/blocks/pack_k_bits.h>
28 #include <iostream>
29 #include <stdexcept>
30 
31 namespace gr {
32 namespace blocks {
33 namespace kernel {
34 
pack_k_bits(unsigned k)35 pack_k_bits::pack_k_bits(unsigned k) : d_k(k)
36 {
37     if (d_k == 0)
38         throw std::out_of_range("pack_k_bits: k must be > 0");
39 }
40 
~pack_k_bits()41 pack_k_bits::~pack_k_bits() {}
42 
pack(unsigned char * bytes,const unsigned char * bits,int nbytes) const43 void pack_k_bits::pack(unsigned char* bytes, const unsigned char* bits, int nbytes) const
44 {
45     for (int i = 0; i < nbytes; i++) {
46         bytes[i] = 0x00;
47         for (unsigned int j = 0; j < d_k; j++) {
48             bytes[i] |= (0x01 & bits[i * d_k + j]) << (d_k - j - 1);
49         }
50     }
51 }
52 
pack_rev(unsigned char * bytes,const unsigned char * bits,int nbytes) const53 void pack_k_bits::pack_rev(unsigned char* bytes,
54                            const unsigned char* bits,
55                            int nbytes) const
56 {
57     for (int i = 0; i < nbytes; i++) {
58         bytes[i] = 0x00;
59         for (unsigned int j = 0; j < d_k; j++) {
60             bytes[i] |= (0x01 & bits[i * d_k + j]) << j;
61         }
62     }
63 }
64 
65 } /* namespace kernel */
66 } /* namespace blocks */
67 } /* namespace gr */
68