parser-inl.h 7.86 KB
Newer Older
gejun's avatar
gejun committed
1
// mcpack2pb - Make protobuf be front-end of mcpack/compack
gejun's avatar
gejun committed
2
// Copyright (c) 2015 Baidu, Inc.
gejun's avatar
gejun committed
3 4 5 6 7 8 9 10 11 12 13 14
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
gejun's avatar
gejun committed
15

gejun's avatar
gejun committed
16
// Author: Ge,Jun (gejun@baidu.com)
gejun's avatar
gejun committed
17 18
// Date: Mon Oct 19 17:17:36 CST 2015

gejun's avatar
gejun committed
19 20
#ifndef MCPACK2PB_MCPACK_PARSER_INL_H
#define MCPACK2PB_MCPACK_PARSER_INL_H
gejun's avatar
gejun committed
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

namespace mcpack2pb {

// Binary head before items of array/object except isomorphic array.
struct ItemsHead {
    uint32_t item_count;
} __attribute__((__packed__));

inline size_t InputStream::popn(size_t n) {
    const size_t saved_n = n;
    do {
        if (_size >= (int64_t)n) {
            _data = (const char*)_data + n;
            _size -= n;
            _popped_bytes += saved_n;
            return saved_n;
        }
        n -= _size;
    } while (_zc_stream->Next(&_data, &_size));
    _data = NULL;
    _size = 0;
    _popped_bytes += saved_n - n;
    return saved_n - n;
}
    
inline size_t InputStream::cutn(void* out, size_t n) {
    const size_t saved_n = n;
    do {
        if (_size >= (int64_t)n) {
            memcpy(out, _data, n);
            _data = (const char*)_data + n;
            _size -= n;
            _popped_bytes += saved_n;
            return saved_n;
        }
        if (_size) {
            memcpy(out, _data, _size);
            out = (char*)out + _size;
            n -= _size;
        }
    } while (_zc_stream->Next(&_data, &_size));
    _data = NULL;
    _size = 0;
    _popped_bytes += saved_n - n;
    return saved_n - n;
}

template <typename T>
inline size_t InputStream::cut_packed_pod(T* packed_pod) {
    if (_size >= (int)sizeof(T)) {
        *packed_pod = *(T*)_data;
        _data = (const char*)_data + sizeof(T);
        _size -= sizeof(T);
        _popped_bytes += sizeof(T);
        return sizeof(T);
    }
    return cutn(packed_pod, sizeof(T));
}

template <typename T>
inline T InputStream::cut_packed_pod() {
    T packed_pod;
    if (_size >= (int)sizeof(T)) {
        packed_pod = *(T*)_data;
        _data = (const char*)_data + sizeof(T);
        _size -= sizeof(T);
        _popped_bytes += sizeof(T);
        return packed_pod;
    }
    cutn(&packed_pod, sizeof(T));
    return packed_pod;
}
    
94
inline butil::StringPiece InputStream::ref_cut(std::string* aux, size_t n) {
gejun's avatar
gejun committed
95
    if (_size >= (int64_t)n) {
96
        butil::StringPiece ret((const char*)_data, n);
gejun's avatar
gejun committed
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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
        _data = (const char*)_data + n;
        _size -= n;
        _popped_bytes += n;
        return ret;
    }
    aux->resize(n);
    size_t m = cutn(&(*aux)[0], n);
    if (m != n) {
        aux->resize(m);
    }
    return *aux;
}

inline uint8_t InputStream::peek1() {
    if (_size > 0) {
        return *(const uint8_t*)_data;
    }
    while (_zc_stream->Next(&_data, &_size)) {
        if (_size > 0) {
            return *(const uint8_t*)_data;
        }
    }
    return 0;
}

// Binary head before items of isomorphic array.
struct IsoItemsHead {
    uint8_t type;
} __attribute__((__packed__));

inline ObjectIterator UnparsedValue::as_object() {
    return ObjectIterator(_stream, _size);
}

inline ArrayIterator UnparsedValue::as_array() {
    return ArrayIterator(_stream, _size);
}

inline ISOArrayIterator UnparsedValue::as_iso_array() {
    return ISOArrayIterator(_stream, _size);
}

inline void ObjectIterator::init(InputStream* stream, size_t size) {
    _field_count = 0;
    _stream = stream;
    _expected_popped_bytes = _stream->popped_bytes() + sizeof(ItemsHead);
    _expected_popped_end = _stream->popped_bytes() + size;
    ItemsHead items_head;
    if (_stream->cut_packed_pod(&items_head) != sizeof(ItemsHead)) {
        CHECK(false) << "buffer(size=" << size << ") is not enough";
        return set_bad();
    }
    _field_count = items_head.item_count;
    operator++();
}

inline void ArrayIterator::init(InputStream* stream, size_t size) {
    _item_count = 0;
    _stream = stream;
    _expected_popped_bytes = _stream->popped_bytes() + sizeof(ItemsHead);
    _expected_popped_end = _stream->popped_bytes() + size;
    ItemsHead items_head;
    if (_stream->cut_packed_pod(&items_head) != sizeof(ItemsHead)) {
        CHECK(false) << "buffer(size=" << size << ") is not enough";
        return set_bad();
    }
    _item_count = items_head.item_count;
    operator++();
}

inline void ISOArrayIterator::init(InputStream* stream, size_t size) {
    _buf_index = 0;
    _buf_count = 0;
    _stream = stream;
    _item_type = (PrimitiveFieldType)0;
    _item_size = 0;
    _item_count = 0;
    _left_item_count = 0;
    IsoItemsHead items_head;
    if (_stream->cut_packed_pod(&items_head) != sizeof(IsoItemsHead)) {
        CHECK(false) << "Not enough data";
        return set_bad();
    }
    _item_type = (PrimitiveFieldType)items_head.type;
    _item_size = get_primitive_type_size(_item_type);
    if (!_item_size) {
        CHECK(false) << "type=" << type2str(_item_type)
                   << " in primitive isoarray is not primitive";
        return set_bad();
    }
    const size_t items_full_size = size - sizeof(IsoItemsHead);
    _item_count = items_full_size / _item_size;
    if (_item_count * _item_size != items_full_size) {
        CHECK(false) << "inconsistent item_count(" << _item_count
                   << ") and value_size(" << items_full_size
                   << "), item_size=" << _item_size;
        return set_bad();
    }
    _left_item_count = _item_count;
    operator++();
}

inline void ISOArrayIterator::operator++() {
    if (_buf_index + 1 < _buf_count) {
        ++_buf_index;
        return;
    }
    // Iterate all items in isomorphic array. We have to do this
    // right here because the items are lacking of headings.
    // Call on_primitives in batch to reduce overhead.
    if (_left_item_count == 0) {
        set_end();
        return;
    }
    _buf_count = std::min((uint32_t)sizeof(_item_buf) / _item_size, _left_item_count);
    _buf_index = 0;
    if (_stream->cutn(_item_buf, _buf_count * _item_size) !=
        _buf_count * _item_size) {
        CHECK(false) << "Not enough data";
        return set_bad();
    }
    _left_item_count -= _buf_count;
}

template <typename T>
inline T ISOArrayIterator::as_integer() const {
    const void* ptr = (_item_buf + _buf_index * _item_size);
    switch ((PrimitiveFieldType)_item_type) {
    case PRIMITIVE_FIELD_INT8:
        return *static_cast<const int8_t*>(ptr);
    case PRIMITIVE_FIELD_INT16:
        return *static_cast<const int16_t*>(ptr);
    case PRIMITIVE_FIELD_INT32:
        return *static_cast<const int32_t*>(ptr);
    case PRIMITIVE_FIELD_INT64:
        return *static_cast<const int64_t*>(ptr);
    case PRIMITIVE_FIELD_UINT8:
        return *static_cast<const uint8_t*>(ptr);
    case PRIMITIVE_FIELD_UINT16:
        return *static_cast<const uint16_t*>(ptr);
    case PRIMITIVE_FIELD_UINT32:
        return *static_cast<const uint32_t*>(ptr);
    case PRIMITIVE_FIELD_UINT64:
        return *static_cast<const uint64_t*>(ptr);
    case PRIMITIVE_FIELD_BOOL:
        return *static_cast<const bool*>(ptr);
    case PRIMITIVE_FIELD_FLOAT:
        return 0;
    case PRIMITIVE_FIELD_DOUBLE:
        return 0;
    }
    return 0;
}

template <typename T>
inline T ISOArrayIterator::as_fp() const {
    const void* ptr = (_item_buf + _buf_index * _item_size);
    if (_item_type == PRIMITIVE_FIELD_FLOAT) {
        return *static_cast<const float*>(ptr);
    } else if (_item_type == PRIMITIVE_FIELD_DOUBLE) {
        return *static_cast<const double*>(ptr);
    }
    return T();
}

}  // namespace mcpack2pb

gejun's avatar
gejun committed
264
#endif  // MCPACK2PB_MCPACK_PARSER_INL_H