aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/vespa/document/util/bytebuffer.cpp
blob: 3b67cc2f29a960e7d7b8d22c5b333240a6bab657 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
/**
   @author Thomas F. Gundersen, �ystein Fledsberg
   @version $Id$
   @date 2004-03-15
*/

#include "bytebuffer.h"
#include "bufferexceptions.h"
#include "stringutil.h"
#include <vespa/vespalib/stllike/asciistream.h>
#include <sstream>
#include <arpa/inet.h>

using vespalib::alloc::Alloc;
using vespalib::make_string;

namespace document {

namespace {

[[noreturn]] static void throwOutOfBounds(size_t want, size_t has) __attribute__((noinline));

void throwOutOfBounds(size_t want, size_t has)
{
    throw BufferOutOfBoundsException(want, has, VESPA_STRLOC);
}

}

#if defined(__i386__) || defined(__x86_64__) || defined(__AARCH64EL__)

template<typename T>
void
ByteBuffer::getDoubleLongNetwork(T &val) {
    //TODO: Change this if we move to big-endian hardware
    if (__builtin_expect(getRemaining() < (int)sizeof(T), 0)) {
        throwOutOfBounds(sizeof(T), getRemaining());
    }

    auto * data = reinterpret_cast<unsigned char*>(&val);
    for (int i=sizeof(T)-1; i>=0; --i) {
        getByte(data[i]);
    }
}

#else
#error "getDoubleLongNetwork is undefined for this arcitecture"
#endif

VESPA_IMPLEMENT_EXCEPTION_SPINE(BufferOutOfBoundsException);

vespalib::string BufferOutOfBoundsException::createMessage(size_t pos, size_t len) {
    vespalib::asciistream ost;
    ost << pos << " > " << len;
    return ost.str();
}

BufferOutOfBoundsException::BufferOutOfBoundsException(size_t pos, size_t len, const vespalib::string& location)
    : IoException(createMessage(pos, len), IoException::NO_SPACE, location, 1)
{
}

ByteBuffer::ByteBuffer(Alloc buffer, uint32_t len)
  : _buffer(static_cast<const char *>(buffer.get())),
    _len(len),
    _pos(0),
    _ownedBuffer(std::make_unique<Alloc>(std::move(buffer)))
{
}

ByteBuffer::ByteBuffer(std::unique_ptr<Alloc> buffer, uint32_t len)
    : _buffer(static_cast<const char *>(buffer->get())),
      _len(len),
      _pos(0),
      _ownedBuffer(std::move(buffer))
{
}

ByteBuffer::ByteBuffer(const ByteBuffer& rhs)
    : _buffer(nullptr),
      _len(rhs._len),
      _pos(rhs._pos),
      _ownedBuffer()
{
    if (rhs._len > 0 && rhs._buffer) {
        auto buf = Alloc::alloc(rhs._len);
        memcpy(buf.get(), rhs._buffer, rhs._len);
        _buffer = static_cast<const char *>(buf.get());
        _ownedBuffer = std::make_unique<Alloc>(std::move(buf));
    }
}

ByteBuffer
ByteBuffer::copyBuffer(const char* buffer, uint32_t len)
{
    if (buffer && len) {
        Alloc newBuf = Alloc::alloc(len);
        memcpy(newBuf.get(), buffer, len);
        return ByteBuffer(std::make_unique<Alloc>(std::move(newBuf)), len);
    } else {
        return ByteBuffer();
    }
}

void ByteBuffer::incPos(uint32_t pos)
{
    if (_pos + pos > _len) {
        throwOutOfBounds(_pos + pos, _len);
    } else {
        _pos+=pos;
    }
}

void ByteBuffer::getNumeric(uint8_t & v) {
    if (__builtin_expect(getRemaining() < sizeof(v), 0)) {
        throwOutOfBounds(getRemaining(), sizeof(v));
    } else {
        v = *reinterpret_cast<const uint8_t *>(getBufferAtPos());
        incPosNoCheck(sizeof(v));
    }
}

void ByteBuffer::getNumericNetwork(int16_t & v) {
    if (__builtin_expect(getRemaining() < sizeof(v), 0)) {
        throwOutOfBounds(getRemaining(), sizeof(v));
    } else {
        uint16_t val;
        memcpy(&val, getBufferAtPos(), sizeof(val));
        v = ntohs(val);
        incPosNoCheck(sizeof(v));
    }
}

void ByteBuffer::getNumericNetwork(int32_t & v) {
    if (__builtin_expect(getRemaining() < sizeof(v), 0)) {
        throwOutOfBounds(getRemaining(), sizeof(v));
    } else {
        uint32_t val;
        memcpy(&val, getBufferAtPos(), sizeof(val));
        v = ntohl(val);
        incPosNoCheck(sizeof(v));
    }
}

void ByteBuffer::getNumeric(int64_t& v) {
    if (__builtin_expect(getRemaining() < sizeof(v), 0)) {
        throwOutOfBounds(getRemaining(), sizeof(v));
    } else {
        memcpy(&v, getBufferAtPos(), sizeof(v));
        incPosNoCheck(sizeof(v));
    }
}

void ByteBuffer::getNumericNetwork(double & v) {
    getDoubleLongNetwork(v);
}

void ByteBuffer::getNumericNetwork(int64_t & v) {
    getDoubleLongNetwork(v);
}

void ByteBuffer::getBytes(void *buffer, uint32_t count)
{
    const char *v = getBufferAtPos();
    incPos(count);
    if (count > 0) {
        memcpy(buffer, v, count);
    }
}

} // document