Exemple #1
0
int encode_SNM_INFO( uint8_t *base,
                     size_t  *idx,
                     const snm_hdr_t      *hdr,
                     const n2n_SNM_INFO_t *info )
{
    int i, retval = 0;
    retval += encode_SNM_hdr(base, idx, hdr);
    retval += encode_uint16(base, idx, info->sn_num);
    retval += encode_uint16(base, idx, info->comm_num);

    if (GET_S(hdr->flags) || GET_A(hdr->flags)) /* SNM / ADV adresses */
    {
        for (i = 0; i < info->sn_num; i++)
        {
            retval += encode_sock(base, idx, &info->sn_ptr[i]);
        }
    }
    if (GET_C(hdr->flags) || GET_N(hdr->flags))
    {
        for (i = 0; i < info->comm_num; i++)
        {
            retval += encode_SNM_comm(base, idx, &info->comm_ptr[i]);
        }
    }
    return retval;
}
Exemple #2
0
DVector<int> TileMap::_get_tile_data() const {

	DVector<int> data;
	data.resize(tile_map.size()*2);
	DVector<int>::Write w = data.write();

	int idx=0;
	for(const Map<PosKey,Cell>::Element *E=tile_map.front();E;E=E->next()) {

		uint8_t *ptr = (uint8_t*)&w[idx];
		encode_uint16(E->key().x,&ptr[0]);
		encode_uint16(E->key().y,&ptr[2]);
		uint32_t val = E->get().id;
		if (E->get().flip_h)
			val|=(1<<29);
		if (E->get().flip_v)
			val|=(1<<30);

		encode_uint32(val,&ptr[4]);
		idx+=2;
	}


	w = DVector<int>::Write();

	return data;

}
Exemple #3
0
void test_command_with_args() {
    uint8_t buf[255];
    int pos = 0;
    int rc;
    uint8_t dest;
    uint8_t code;
    uint16_t mref;

    rc = encode_cmd(buf, &pos, LS1P_ADDR_ARM, LS1P_REQ_CODE_CMDLOG, 314);
    assert(rc == LS1P_API_OK);
    assert(pos == 3);
    assert(buf[0] == 0x01);

    rc = encode_uint8(buf, &pos, 123);
    assert(rc == LS1P_API_OK);
    assert(pos == 4);

    rc = encode_uint16(buf, &pos, 4023);
    assert(rc == LS1P_API_OK);
    assert(pos == 6);

    assert(buf[0] == 0x01);
    assert(buf[1] == 0x01);
    assert(buf[2] == 0x3A);
    assert(buf[3] == 0x7B);
    assert(buf[4] == 0x0F);
    assert(buf[5] == 0xB7);

    rc = decode_cmd(buf, &pos, &dest, &code, &mref);
    assert(rc == LS1P_API_OK);
    assert(dest == LS1P_ADDR_ARM);
    assert(code == LS1P_REQ_CODE_CMDLOG);
    assert(mref == 314);
    assert(pos == 3);
}
Exemple #4
0
 inline mtn::byte_t*
 encode_bytes(const mtn::byte_t* input,
              uint16_t           size,
              mtn::byte_t*       output)
 {
     memcpy(encode_uint16(size, output), input, size);
     return output + size + sizeof(uint16_t);
 }
Exemple #5
0
 size_t encode_string_list(size_t offset, const std::vector<std::string>& value) {
   size_t pos = encode_uint16(offset, static_cast<uint16_t>(value.size()));
   for (std::vector<std::string>::const_iterator it = value.begin(),
        end = value.end(); it != end; ++it) {
     pos = encode_string(pos, it->data(), static_cast<uint16_t>(it->size()));
   }
   return pos;
 }
Exemple #6
0
 size_t encode_string_map(size_t offset, const std::map<std::string, std::string>& value) {
   size_t pos = encode_uint16(offset, static_cast<uint16_t>(value.size()));
   for (std::map<std::string, std::string>::const_iterator it = value.begin();
        it != value.end(); ++it) {
     pos = encode_string(pos, it->first.c_str(), static_cast<uint16_t>(it->first.size()));
     pos = encode_string(pos, it->second.c_str(), static_cast<uint16_t>(it->second.size()));
   }
   return pos;
 }
Exemple #7
0
void StreamPeer::put_16(int16_t p_val) {

	if (big_endian) {
		p_val = BSWAP16(p_val);
	}
	uint8_t buf[2];
	encode_uint16(p_val, buf);
	put_data(buf, 2);
}
Exemple #8
0
void Collection::encode_items_uint16(char* buf) const {
  for (BufferVec::const_iterator i = items_.begin(),
       end = items_.end(); i != end; ++i) {
    encode_uint16(buf, i->size());
    buf += sizeof(uint16_t);
    memcpy(buf, i->data(), i->size());
    buf += i->size();
  }
}
Exemple #9
0
int encode_SNM_hdr( uint8_t *base,
                    size_t  *idx,
                    const snm_hdr_t *hdr )
{
    int retval = 0;
    retval += encode_uint8(base, idx, hdr->type);
    retval += encode_uint8(base, idx, hdr->flags);
    retval += encode_uint16(base, idx, hdr->seq_num);
    return retval;
}
Exemple #10
0
 int encode_int < uint16_t > ( uint16_t val, uint8_t * start, uint8_t * end )
 {
     int ret = encode_uint16 ( val, start, end );
     if ( ret > 0 )
     {
         uint16_t val2;
         int ret2 = decode_uint16 ( start, start + ret, & val2 );
         assert ( ret == ret2 && val == val2 );
     }
     return ret;
 }
void BufferCollection::encode(int version, char* buf) const {
  assert(version == 1 || version == 2);
  char* pos = buf;
  for (BufferVec::const_iterator it = bufs_.begin(),
      end = bufs_.end(); it != end; ++it) {
    encode_uint16(pos, it->size());
    pos += sizeof(uint16_t);

    memcpy(pos, it->data(), it->size());
    pos += it->size();
  }
}
Exemple #12
0
bool Statement::calculate_routing_key(const std::vector<size_t>& key_indices, std::string* routing_key) const {
  if (key_indices.empty()) return false;

  if (key_indices.size() == 1) {
    assert(key_indices.front() < elements().size());
    const AbstractData::Element& element(elements()[key_indices.front()]);
    if (element.is_unset() || element.is_null()) {
      return false;
    }
    Buffer buf(element.get_buffer(CASS_HIGHEST_SUPPORTED_PROTOCOL_VERSION));
    routing_key->assign(buf.data() + sizeof(int32_t),
                        buf.size() - sizeof(int32_t));
  } else {
    size_t length = 0;

    for (std::vector<size_t>::const_iterator i = key_indices.begin();
         i != key_indices.end(); ++i) {
      assert(*i < elements().size());
      const AbstractData::Element& element(elements()[*i]);
      if (element.is_unset() || element.is_null()) {
        return false;
      }
      size_t size = element.get_size(CASS_HIGHEST_SUPPORTED_PROTOCOL_VERSION) - sizeof(int32_t);
      length += sizeof(uint16_t) + size + 1;
    }

    routing_key->clear();
    routing_key->reserve(length);

    for (std::vector<size_t>::const_iterator i = key_indices.begin();
         i != key_indices.end(); ++i) {
      const AbstractData::Element& element(elements()[*i]);
      Buffer buf(element.get_buffer(CASS_HIGHEST_SUPPORTED_PROTOCOL_VERSION));
      size_t size = buf.size() - sizeof(int32_t);

      char size_buf[sizeof(uint16_t)];
      encode_uint16(size_buf, size);
      routing_key->append(size_buf, sizeof(uint16_t));
      routing_key->append(buf.data() + sizeof(int32_t), size);
      routing_key->push_back(0);
    }
  }

  return true;
}
Exemple #13
0
int encode_SNM_REQ( uint8_t *base,
                    size_t  *idx,
                    const snm_hdr_t     *hdr,
                    const n2n_SNM_REQ_t *req )
{
    int i, retval = 0;
    retval += encode_SNM_hdr(base, idx, hdr);
    if (GET_N(hdr->flags))
    {
        retval += encode_uint16(base, idx, req->comm_num);

        for (i = 0; i < req->comm_num; i++)
        {
            retval += encode_SNM_comm(base, idx, &req->comm_ptr[i]);
        }
    }
    return retval;
}
Exemple #14
0
int encode_SNM_ADV( uint8_t *base,
                    size_t  *idx,
                    const snm_hdr_t     *hdr,
                    const n2n_SNM_ADV_t *adv )
{
    int i, retval = 0;
    retval += encode_SNM_hdr(base, idx, hdr);
    retval += encode_sock(base, idx, &adv->sn);
    if (GET_N(hdr->flags))
    {
        retval += encode_uint16(base, idx, adv->comm_num);

        for (i = 0; i < adv->comm_num; i++)
        {
            retval += encode_SNM_comm(base, idx, &adv->comm_ptr[i]);
        }
    }
    return retval;
}
Exemple #15
0
 size_t encode_string(size_t offset, const char* value, uint16_t size) {
   size_t pos = encode_uint16(offset, size);
   return copy(pos, value, size);
 }
Exemple #16
0
 inline mtn::byte_t*
 encode_parition(uint16_t     input,
                 mtn::byte_t* output)
 {
     return encode_uint16(input, output);
 }
Error EditorSampleImportPlugin::import(const String& p_path, const Ref<ResourceImportMetadata>& p_from){

	ERR_FAIL_COND_V(p_from->get_source_count()!=1,ERR_INVALID_PARAMETER);

	Ref<ResourceImportMetadata> from=p_from;

	String src_path=EditorImportPlugin::expand_source_path(from->get_source_path(0));
	Ref<Sample> smp = ResourceLoader::load(src_path);
	ERR_FAIL_COND_V(smp.is_null(),ERR_CANT_OPEN);


	float rate = smp->get_mix_rate();
	bool is16 = smp->get_format()==Sample::FORMAT_PCM16;
	int chans = smp->is_stereo()?2:1;
	int len = smp->get_length();
	Sample::LoopFormat loop= smp->get_loop_format();
	int loop_beg = smp->get_loop_begin();
	int loop_end = smp->get_loop_end();

	print_line("Input Sample: ");
	print_line("\tlen: "+itos(len));
	print_line("\tchans: "+itos(chans));
	print_line("\t16bits: "+itos(is16));
	print_line("\trate: "+itos(rate));
	print_line("\tloop: "+itos(loop));
	print_line("\tloop begin: "+itos(loop_beg));
	print_line("\tloop end: "+itos(loop_end));
	Vector<float> data;
	data.resize(len*chans);

	{
		DVector<uint8_t> src_data = smp->get_data();
		DVector<uint8_t>::Read sr = src_data.read();


		for(int i=0;i<len*chans;i++) {

			float s=0;
			if (is16) {

				int16_t i16 = decode_uint16(&sr[i*2]);
				s=i16/32767.0;
			} else {

				int8_t i8 = sr[i];
				s=i8/127.0;
			}
			data[i]=s;
		}
	}

	//apply frequency limit

	bool limit_rate = from->get_option("force/max_rate");
	int limit_rate_hz = from->get_option("force/max_rate_hz");
	if (limit_rate && rate > limit_rate_hz) {
		//resampleeee!!!
		int new_data_len = len * limit_rate_hz / rate;
		Vector<float> new_data;
		new_data.resize( new_data_len * chans );
		for(int c=0;c<chans;c++) {

			for(int i=0;i<new_data_len;i++) {

				//simple cubic interpolation should be enough.
				float pos = float(i) * len / new_data_len;
				float mu = pos-Math::floor(pos);
				int ipos = int(Math::floor(pos));

				float y0=data[MAX(0,ipos-1)*chans+c];
				float y1=data[ipos*chans+c];
				float y2=data[MIN(len-1,ipos+1)*chans+c];
				float y3=data[MIN(len-1,ipos+2)*chans+c];

				float mu2 = mu*mu;
				float a0 = y3 - y2 - y0 + y1;
				float a1 = y0 - y1 - a0;
				float a2 = y2 - y0;
				float a3 = y1;

				float res=(a0*mu*mu2+a1*mu2+a2*mu+a3);

				new_data[i*chans+c]=res;
			}
		}

		if (loop) {

			loop_beg=loop_beg*new_data_len/len;
			loop_end=loop_end*new_data_len/len;
		}
		data=new_data;
		rate=limit_rate_hz;
		len=new_data_len;
	}


	bool normalize = from->get_option("edit/normalize");

	if (normalize) {

		float max=0;
		for(int i=0;i<data.size();i++) {

			float amp = Math::abs(data[i]);
			if (amp>max)
				max=amp;
		}

		if (max>0) {

			float mult=1.0/max;
			for(int i=0;i<data.size();i++) {

				data[i]*=mult;
			}

		}
	}

	bool trim = from->get_option("edit/trim");

	if (trim && !loop) {

		int first=0;
		int last=(len*chans)-1;
		bool found=false;
		float limit = Math::db2linear(-30);
		for(int i=0;i<data.size();i++) {
			float amp = Math::abs(data[i]);

			if (!found && amp > limit) {
				first=i;
				found=true;
			}

			if (found && amp > limit) {
				last=i;
			}
		}

		first/=chans;
		last/=chans;

		if (first<last) {

			Vector<float> new_data;
			new_data.resize((last-first+1)*chans);
			for(int i=first*chans;i<=last*chans;i++) {
				new_data[i-first*chans]=data[i];
			}

			data=new_data;
			len=data.size()/chans;
		}

	}

	bool make_loop = from->get_option("edit/loop");

	if (make_loop && !loop) {

		loop=Sample::LOOP_FORWARD;
		loop_beg=0;
		loop_end=len;
	}

	int compression = from->get_option("compress/mode");
	bool force_mono = from->get_option("force/mono");


	if (force_mono && chans==2) {

		Vector<float> new_data;
		new_data.resize(data.size()/2);
		for(int i=0;i<len;i++) {
			new_data[i]=(data[i*2+0]+data[i*2+1])/2.0;
		}

		data=new_data;
		chans=1;
	}

	bool force_8_bit = from->get_option("force/8_bit");
	if (force_8_bit) {

		is16=false;
	}


	DVector<uint8_t> dst_data;
	Sample::Format dst_format;

	if ( compression == _EditorSampleImportOptions::COMPRESS_MODE_RAM) {

		dst_format=Sample::FORMAT_IMA_ADPCM;
		if (chans==1) {
			_compress_ima_adpcm(data,dst_data);
		} else {

			print_line("INTERLEAAVE!");



			//byte interleave
			Vector<float> left;
			Vector<float> right;

			int tlen = data.size()/2;
			left.resize(tlen);
			right.resize(tlen);

			for(int i=0;i<tlen;i++) {
				left[i]=data[i*2+0];
				right[i]=data[i*2+1];
			}

			DVector<uint8_t> bleft;
			DVector<uint8_t> bright;

			_compress_ima_adpcm(left,bleft);
			_compress_ima_adpcm(right,bright);

			int dl = bleft.size();
			dst_data.resize( dl *2 );

			DVector<uint8_t>::Write w=dst_data.write();
			DVector<uint8_t>::Read rl=bleft.read();
			DVector<uint8_t>::Read rr=bright.read();

			for(int i=0;i<dl;i++) {
				w[i*2+0]=rl[i];
				w[i*2+1]=rr[i];
			}
		}

//		print_line("compressing ima-adpcm, resulting buffersize is "+itos(dst_data.size())+" from "+itos(data.size()));

	} else {

		dst_format=is16?Sample::FORMAT_PCM16:Sample::FORMAT_PCM8;
		dst_data.resize( data.size() * (is16?2:1));
		{
			DVector<uint8_t>::Write w = dst_data.write();

			int ds=data.size();
			for(int i=0;i<ds;i++) {

				if (is16) {
					int16_t v = CLAMP(data[i]*32767,-32768,32767);
					encode_uint16(v,&w[i*2]);
				} else {
					int8_t v = CLAMP(data[i]*127,-128,127);
					w[i]=v;
				}
			}
		}
	}


	Ref<Sample> target;

	if (ResourceCache::has(p_path)) {

		target = Ref<Sample>( ResourceCache::get(p_path)->cast_to<Sample>() );
	} else {

		target = smp;
	}

	target->create(dst_format,chans==2?true:false,len);
	target->set_data(dst_data);
	target->set_mix_rate(rate);
	target->set_loop_format(loop);
	target->set_loop_begin(loop_beg);
	target->set_loop_end(loop_end);

	from->set_source_md5(0,FileAccess::get_md5(src_path));
	from->set_editor(get_name());
	target->set_import_metadata(from);


	Error err = ResourceSaver::save(p_path,smp);

	return err;

}
Exemple #18
0
void EditorExportPlatformAndroid::_fix_resources(Vector<uint8_t>& p_manifest) {


	const int UTF8_FLAG = 0x00000100;
	print_line("*******************GORRRGLE***********************");

	uint32_t header = decode_uint32(&p_manifest[0]);
	uint32_t filesize = decode_uint32(&p_manifest[4]);
	uint32_t string_block_len = decode_uint32(&p_manifest[16]);
	uint32_t string_count = decode_uint32(&p_manifest[20]);
	uint32_t string_flags = decode_uint32(&p_manifest[28]);
	const uint32_t string_table_begins = 40;

	Vector<String> string_table;

	printf("stirng block len: %i\n",string_block_len);
	printf("stirng count: %i\n",string_count);
	printf("flags: %x\n",string_flags);

	for(int i=0;i<string_count;i++) {

		uint32_t offset = decode_uint32(&p_manifest[string_table_begins+i*4]);
		offset+=string_table_begins+string_count*4;

		String str = _parse_string(&p_manifest[offset],string_flags&UTF8_FLAG);

		if (str.begins_with("godot-project-name")) {


			if (str=="godot-project-name") {
				//project name
				str = get_project_name();

			} else {

				String lang = str.substr(str.find_last("-")+1,str.length()).replace("-","_");
				String prop = "application/name_"+lang;
				if (Globals::get_singleton()->has(prop)) {
					str = Globals::get_singleton()->get(prop);
				} else {
					str = get_project_name();
				}
			}
		}

		string_table.push_back(str);

	}

	//write a new string table, but use 16 bits
	Vector<uint8_t> ret;
	ret.resize(string_table_begins+string_table.size()*4);

	for(int i=0;i<string_table_begins;i++) {

		ret[i]=p_manifest[i];
	}

	int ofs=0;
	for(int i=0;i<string_table.size();i++) {

		encode_uint32(ofs,&ret[string_table_begins+i*4]);
		ofs+=string_table[i].length()*2+2+2;
	}

	ret.resize(ret.size()+ofs);
	uint8_t *chars=&ret[ret.size()-ofs];
	for(int i=0;i<string_table.size();i++) {

		String s = string_table[i];
		encode_uint16(s.length(),chars);
		chars+=2;
		for(int j=0;j<s.length();j++) {
			encode_uint16(s[j],chars);
			chars+=2;
		}
		encode_uint16(0,chars);
		chars+=2;
	}

	//pad
	while(ret.size()%4)
		ret.push_back(0);

	//change flags to not use utf8
	encode_uint32(string_flags&~0x100,&ret[28]);
	//change length
	encode_uint32(ret.size()-12,&ret[16]);
	//append the rest...
	int rest_from = 12+string_block_len;
	int rest_to = ret.size();
	int rest_len = (p_manifest.size() - rest_from);
	ret.resize(ret.size() + (p_manifest.size() - rest_from) );
	for(int i=0;i<rest_len;i++) {
		ret[rest_to+i]=p_manifest[rest_from+i];
	}
	//finally update the size
	encode_uint32(ret.size(),&ret[4]);


	p_manifest=ret;
	printf("end\n");
}
Ref<AudioStreamSample> AudioEffectRecord::get_recording() const {
	AudioStreamSample::Format dst_format = format;
	bool stereo = true; //forcing mono is not implemented

	PoolVector<uint8_t> dst_data;

	if (dst_format == AudioStreamSample::FORMAT_8_BITS) {
		int data_size = current_instance->recording_data.size();
		dst_data.resize(data_size);
		PoolVector<uint8_t>::Write w = dst_data.write();

		for (int i = 0; i < data_size; i++) {
			int8_t v = CLAMP(current_instance->recording_data[i] * 128, -128, 127);
			w[i] = v;
		}
	} else if (dst_format == AudioStreamSample::FORMAT_16_BITS) {
		int data_size = current_instance->recording_data.size();
		dst_data.resize(data_size * 2);
		PoolVector<uint8_t>::Write w = dst_data.write();

		for (int i = 0; i < data_size; i++) {
			int16_t v = CLAMP(current_instance->recording_data[i] * 32768, -32768, 32767);
			encode_uint16(v, &w[i * 2]);
		}
	} else if (dst_format == AudioStreamSample::FORMAT_IMA_ADPCM) {
		//byte interleave
		Vector<float> left;
		Vector<float> right;

		int tframes = current_instance->recording_data.size() / 2;
		left.resize(tframes);
		right.resize(tframes);

		for (int i = 0; i < tframes; i++) {
			left.set(i, current_instance->recording_data[i * 2 + 0]);
			right.set(i, current_instance->recording_data[i * 2 + 1]);
		}

		PoolVector<uint8_t> bleft;
		PoolVector<uint8_t> bright;

		ResourceImporterWAV::_compress_ima_adpcm(left, bleft);
		ResourceImporterWAV::_compress_ima_adpcm(right, bright);

		int dl = bleft.size();
		dst_data.resize(dl * 2);

		PoolVector<uint8_t>::Write w = dst_data.write();
		PoolVector<uint8_t>::Read rl = bleft.read();
		PoolVector<uint8_t>::Read rr = bright.read();

		for (int i = 0; i < dl; i++) {
			w[i * 2 + 0] = rl[i];
			w[i * 2 + 1] = rr[i];
		}
	} else {
		ERR_PRINT("Format not implemented.");
	}

	Ref<AudioStreamSample> sample;
	sample.instance();
	sample->set_data(dst_data);
	sample->set_format(dst_format);
	sample->set_mix_rate(AudioServer::get_singleton()->get_mix_rate());
	sample->set_loop_mode(AudioStreamSample::LOOP_DISABLED);
	sample->set_loop_begin(0);
	sample->set_loop_end(0);
	sample->set_stereo(stereo);

	return sample;
}