Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdint>
- #include <cstring>
- #include <string>
- #include <iostream>
- #include "nbt.hpp"
- void alloc_tag(int id, TAG **ptr) {
- switch (id) {
- case TAG_Byte_id:
- *ptr = new TAG_Byte; break;
- case TAG_Short_id:
- *ptr = new TAG_Short; break;
- case TAG_Int_id:
- *ptr = new TAG_Int; break;
- case TAG_Long_id:
- *ptr = new TAG_Long; break;
- case TAG_Float_id:
- *ptr = new TAG_Float; break;
- case TAG_Double_id:
- *ptr = new TAG_Double; break;
- case TAG_Byte_Array_id:
- *ptr = new TAG_Byte_Array; break;
- case TAG_String_id:
- *ptr = new TAG_String; break;
- case TAG_List_id:
- *ptr = new TAG_List; break;
- case TAG_Compound_id:
- *ptr = new TAG_Compound; break;
- case TAG_Int_Array_id:
- *ptr = new TAG_Int_Array; break;
- }
- };
- void dealloc_tag(int id, TAG **ptr) {
- switch (id) {
- case TAG_Byte_id:
- delete dynamic_cast<TAG_Byte*>(*ptr); break;
- case TAG_Short_id:
- delete dynamic_cast<TAG_Short*>(*ptr); break;
- case TAG_Int_id:
- delete dynamic_cast<TAG_Int*>(*ptr); break;
- case TAG_Long_id:
- delete dynamic_cast<TAG_Long*>(*ptr); break;
- case TAG_Float_id:
- delete dynamic_cast<TAG_Float*>(*ptr); break;
- case TAG_Double_id:
- delete dynamic_cast<TAG_Double*>(*ptr); break;
- case TAG_Byte_Array_id:
- delete dynamic_cast<TAG_Byte_Array*>(*ptr); break;
- case TAG_String_id:
- delete dynamic_cast<TAG_String*>(*ptr); break;
- case TAG_List_id:
- delete dynamic_cast<TAG_List*>(*ptr); break;
- case TAG_Compound_id:
- delete dynamic_cast<TAG_Compound*>(*ptr); break;
- case TAG_Int_Array_id:
- delete dynamic_cast<TAG_Int_Array*>(*ptr); break;
- }
- };
- std::string TAG::get_name() {
- return name;
- };
- void TAG::set_name(std::string new_name) {
- name = new_name;
- };
- template<typename T, int id>
- _TAG_Numeric<T, id>::_TAG_Numeric(T new_val) {
- val = new_val;
- };
- template<typename T, int id>
- unsigned int _TAG_Numeric<T, id>::tag_size() {
- return sizeof(T);
- };
- template<typename T, int id>
- std::string _TAG_Numeric<T, id>::pretty_print(int tabs) {
- return "";
- };
- template<typename T, int id>
- int _TAG_Numeric<T, id>::get_id() {
- return id;
- };
- template<typename T, int id>
- T _TAG_Numeric<T, id>::get_val() {
- return val;
- };
- template<typename T, int id>
- void _TAG_Numeric<T, id>::set_val(T new_val) {
- val = new_val;
- };
- template class _TAG_Numeric<int8_t, TAG_Byte_id>;
- template class _TAG_Numeric<int16_t, TAG_Short_id>;
- template class _TAG_Numeric<int32_t, TAG_Int_id>;
- template class _TAG_Numeric<int64_t, TAG_Long_id>;
- template class _TAG_Numeric<float, TAG_Float_id>;
- template class _TAG_Numeric<double, TAG_Double_id>;
- template<typename T, typename Shift_T, int id>
- unsigned char* _TAG_Integral<T, Shift_T, id>::encode(unsigned char *buffer) {
- unsigned char *ptr = buffer;
- for (int i = sizeof(T)-1; i>=0; --i, ++ptr) {
- *ptr = val>>(i*8);
- }
- return ptr++;
- };
- template<typename T, typename Shift_T, int id>
- unsigned char* _TAG_Integral<T, Shift_T, id>::decode(unsigned char *buffer) {
- unsigned char *ptr = buffer;
- Shift_T shift;
- val = 0;
- for (int i = sizeof(T)-1; i>=0; --i, ++ptr) {
- shift = *ptr;
- val = val | shift<<(i*8);
- }
- return ptr++;
- };
- template class _TAG_Integral<int8_t, uint8_t, TAG_Byte_id>;
- template class _TAG_Integral<int16_t, uint16_t, TAG_Short_id>;
- template class _TAG_Integral<int32_t, uint32_t, TAG_Int_id>;
- template class _TAG_Integral<int64_t, uint64_t, TAG_Long_id>;
- template<typename T, typename Shift_T, int id>
- unsigned char* _TAG_Precision<T, Shift_T, id>::encode(unsigned char *buffer) {
- unsigned char *ptr = buffer;
- union {
- T p;
- Shift_T s;
- } u;
- u.p = val;
- for (int i = sizeof(T)-1; i>=0; --i, ++ptr) {
- *ptr = u.s>>(i*8);
- }
- return ptr++;
- };
- template<typename T, typename Shift_T, int id>
- unsigned char* _TAG_Precision<T, Shift_T, id>::decode(unsigned char *buffer) {
- unsigned char *ptr = buffer;
- union {
- T p;
- Shift_T s;
- } u;
- Shift_T shift;
- u.s = 0;
- for (int i = sizeof(T)-1; i>=0; --i, ++ptr) {
- shift = *ptr;
- u.s = u.s | shift<<(i*8);
- }
- val = u.p;
- return ptr++;
- };
- template class _TAG_Precision<float, uint32_t, TAG_Float_id>;
- template class _TAG_Precision<double, uint64_t, TAG_Double_id>;
- template<typename T, class TAG_T, int id>
- unsigned char* _TAG_Array<T, TAG_T, id>::encode(unsigned char *buffer) {
- unsigned char *ptr = buffer;
- ptr = TAG_Int(vtr.size()).encode(ptr);
- for (unsigned int i = 0; i<vtr.size(); ++i) {
- ptr = TAG_T(vtr[i]).encode(ptr);
- }
- return ptr;
- };
- template<typename T, class TAG_T, int id>
- unsigned char* _TAG_Array<T, TAG_T, id>::decode(unsigned char *buffer) {
- unsigned char *ptr = buffer;
- TAG_Int size_tag;
- ptr = size_tag.decode(ptr);
- vtr.resize(size_tag.get_val());
- TAG_T decode_tag;
- for (unsigned int i = 0; i<vtr.size(); ++i) {
- ptr = decode_tag.decode(ptr);
- vtr[i] = decode_tag.get_val();
- }
- return ptr;
- };
- template<typename T, class TAG_T, int id>
- unsigned int _TAG_Array<T, TAG_T, id>::tag_size() {
- return 4+(vtr.size()*sizeof(T)); // 4 bytes for int prefix
- };
- template<typename T, class TAG_T, int id>
- std::string _TAG_Array<T, TAG_T, id>::pretty_print(int tabs) {
- return "";
- };
- template<typename T, class TAG_T, int id>
- int _TAG_Array<T, TAG_T, id>::get_id() {
- return id;
- };
- template<typename T, class TAG_T, int id>
- T& _TAG_Array<T, TAG_T, id>::operator[](unsigned int i) {
- return vtr[i];
- };
- template<typename T, class TAG_T, int id>
- unsigned int _TAG_Array<T, TAG_T, id>::array_size() {
- return vtr.size();
- };
- template<typename T, class TAG_T, int id>
- void _TAG_Array<T, TAG_T, id>::resize(unsigned int size) {
- vtr.resize(size);
- };
- template class _TAG_Array<int8_t, TAG_Byte, TAG_Byte_Array_id>;
- template class _TAG_Array<int32_t, TAG_Int, TAG_Int_Array_id>;
- TAG_String::TAG_String(std::string new_str) {
- str = new_str;
- };
- unsigned char* TAG_String::encode(unsigned char *buffer) {
- unsigned char *ptr = buffer;
- unsigned short len = str.length();
- ptr = _TAG_Integral<uint16_t, uint16_t, -1>(len).encode(ptr);
- std::memcpy(ptr, str.data(), len);
- ptr+=len;
- return ptr;
- };
- unsigned char* TAG_String::decode(unsigned char *buffer) {
- unsigned char *ptr = buffer;
- _TAG_Integral<uint16_t, uint16_t, -1> decode_tag;
- ptr = decode_tag.decode(ptr);
- unsigned short len = decode_tag.get_val();
- str.assign(reinterpret_cast<char*>(ptr), len);
- ptr+=len;
- return ptr;
- };
- unsigned int TAG_String::tag_size() {
- return 2+str.length(); // 2 bytes for short prefix
- };
- std::string TAG_String::pretty_print(int tabs) {
- return "";
- };
- int TAG_String::get_id() {
- return TAG_String_id;
- };
- std::string TAG_String::get_val() {
- return str;
- };
- void TAG_String::set_val(std::string new_str) {
- str = new_str;
- };
- TAG_List::TAG_List(int id, int size) {
- tag_type = id;
- resize(size);
- };
- TAG_List::~TAG_List() {
- resize(0);
- };
- unsigned char* TAG_List::encode(unsigned char *buffer) {
- unsigned char *ptr = buffer;
- *ptr = tag_type;
- ++ptr;
- ptr = TAG_Int(vtr.size()).encode(ptr);
- for (unsigned int i = 0; i<vtr.size(); ++i) {
- ptr = vtr[i]->encode(ptr);
- }
- return ptr;
- };
- unsigned char* TAG_List::decode(unsigned char *buffer) {
- resize(0);
- unsigned char *ptr = buffer;
- tag_type = *ptr;
- ++ptr;
- TAG_Int decode_tag;
- ptr = decode_tag.decode(ptr);
- int len = decode_tag.get_val();
- resize(len);
- for (int i = 0; i < len; ++i) {
- ptr = vtr[i]->decode(ptr);
- }
- return ptr;
- };
- unsigned int TAG_List::tag_size() {
- unsigned int size = 5; //1 byte for type id, 4 bytes for int prefix
- for (unsigned int i = 0; i<vtr.size(); ++i) {
- size += vtr[i]->tag_size();
- }
- return size;
- };
- std::string TAG_List::pretty_print(int tabs) {
- return "";
- };
- int TAG_List::get_id() {
- return TAG_List_id;
- };
- TAG* TAG_List::operator[](unsigned int i) {
- return vtr[i];
- };
- unsigned int TAG_List::array_size() {
- return vtr.size();
- };
- void TAG_List::resize(unsigned int new_size) {
- unsigned int old_size = vtr.size();
- if (new_size>old_size) {
- vtr.resize(new_size);
- for (unsigned int i = old_size; i<new_size; ++i) {
- alloc_tag(tag_type, &vtr[i]);
- }
- } else if (new_size<old_size) {
- for (unsigned int i = new_size; i<old_size; ++i) {
- //Using get_id instead of tag_type for dealloc, better safe than sorry
- dealloc_tag(vtr[i]->get_id(), &vtr[i]);
- }
- vtr.resize(new_size);
- }
- };
- int TAG_List::get_type() {
- return tag_type;
- };
- void TAG_List::set_type(int id) {
- resize(0);
- tag_type = id;
- };
- unsigned char* TAG_Compound::encode(unsigned char *buffer){return buffer;};
- unsigned char* TAG_Compound::decode(unsigned char *buffer){return buffer;};
- unsigned int TAG_Compound::tag_size(){return 0;};
- std::string TAG_Compound::pretty_print(int tabs) {
- return "";
- };
- int TAG_Compound::get_id(){return 0;};
- TAG* TAG_Compound::operator[](unsigned int i){TAG *ptr=NULL; return ptr;};
- unsigned int TAG_Compound::array_size(){return 0;};
- void TAG_Compound::resize(unsigned int size){};
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement