1
/* VerifyPN - TAPAAL Petri Net Engine
2
* Copyright (C) 2016 Peter Gjøl Jensen <root@petergjoel.dk>
4
* This program is free software: you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation, either version 3 of the License, or
7
* (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19
* File: binarywrapper.cpp
20
* Author: Peter G. Jensen
22
* Created on 10 June 2015, 19:20
25
#include "PetriEngine/Structures/binarywrapper.h"
29
const uchar binarywrapper_t::_masks[8] = {
30
static_cast <uchar>(0x80),
31
static_cast <uchar>(0x40),
32
static_cast <uchar>(0x20),
33
static_cast <uchar>(0x10),
34
static_cast <uchar>(0x08),
35
static_cast <uchar>(0x04),
36
static_cast <uchar>(0x02),
37
static_cast <uchar>(0x01)
40
size_t binarywrapper_t::overhead(uint size)
50
size_t binarywrapper_t::bytes(uint size)
52
return (size + overhead(size))/8;
56
binarywrapper_t::binarywrapper_t(uint size)
58
_nbytes = (size + overhead(size)) / 8;
59
_blob = zallocate(_nbytes);
63
binarywrapper_t::binarywrapper_t(const binarywrapper_t& other, uint offset)
67
_nbytes = other._nbytes/*.load()*/;
74
_blob = allocate(_nbytes);
75
memcpy(raw(), &(other.const_raw()[offset]), _nbytes);
76
assert(raw()[0] == other.const_raw()[offset]);
79
binarywrapper_t::binarywrapper_t
80
(uchar* org, uint size, uint offset, uint encodingsize)
82
if(size == 0 || offset >= encodingsize)
89
uint so = size + offset;
90
offset = ((so - 1) / 8) - ((size - 1) / 8);
92
_nbytes = ((encodingsize + this->overhead(encodingsize)) / 8);
101
uchar* tmp = &(org[offset]);
102
if(_nbytes <= __BW_BSIZE__)
104
memcpy(raw(), tmp, _nbytes);
110
assert(org[offset] == raw()[0]);
115
binarywrapper_t::binarywrapper_t(uchar* org, uint size)
117
_nbytes = size / 8 + (size % 8 ? 1 : 0);
120
if(_nbytes <= __BW_BSIZE__)
121
memcpy(raw(), org, _nbytes);
123
// assert(raw[0] == const_raw()[0]);
127
void binarywrapper_t::copy(const binarywrapper_t& other, uint offset)
129
memcpy(&(raw()[offset / 8]), other.const_raw(), other._nbytes);
130
assert(other.const_raw()[0] == raw()[0]);
134
void binarywrapper_t::copy(const uchar* data, uint size)
138
_blob = allocate(size);
140
memcpy(raw(), data, size);
141
assert(data[0] == raw()[0]);
152
void binarywrapper_t::print(std::ostream& stream, size_t length) const
154
stream << _nbytes << " bytes : ";
155
for (size_t i = 0; i < _nbytes * 8 && i < length; i++) {
156
if (i % 8 == 0 && i != 0) stream << "-";
157
stream << this->at(i);
163
std::ostream &operator<<(std::ostream &os, const ptrie::binarywrapper_t &b) {