Exemple #1
0
void TileMap::_set_tile_data(const DVector<int>& p_data) {

	int c=p_data.size();
	DVector<int>::Read r = p_data.read();


	for(int i=0;i<c;i+=2) {

		const uint8_t *ptr=(const uint8_t*)&r[i];
		uint8_t local[8];
		for(int j=0;j<8;j++)
			local[j]=ptr[j];

#ifdef BIG_ENDIAN_ENABLED


		SWAP(local[0],local[3]);
		SWAP(local[1],local[2]);
		SWAP(local[4],local[7]);
		SWAP(local[5],local[6]);
#endif
		int x = decode_uint16(&local[0]);
		int y = decode_uint16(&local[2]);
		uint32_t v = decode_uint32(&local[4]);
		bool flip_h = v&(1<<29);
		bool flip_v = v&(1<<30);
		v&=(1<<29)-1;

//		if (x<-20 || y <-20 || x>4000 || y>4000)
//			continue;
		set_cell(x,y,v,flip_h,flip_v);
	}

}
Exemple #2
0
bool ErrorResponse::decode(int version, char* buffer, size_t size) {
  char* pos = decode_int32(buffer, code_);
  pos = decode_string(pos, &message_);

  switch (code_) {
    case CQL_ERROR_UNAVAILABLE:
      pos = decode_uint16(pos, cl_);
      pos = decode_int32(pos, required_);
      decode_int32(pos, received_);
      break;
    case CQL_ERROR_READ_TIMEOUT:
      pos = decode_uint16(pos, cl_);
      pos = decode_int32(pos, received_);
      pos = decode_int32(pos, required_);
      decode_byte(pos, data_present_);
      break;
    case CQL_ERROR_WRITE_TIMEOUT:
      pos = decode_uint16(pos, cl_);
      pos = decode_int32(pos, received_);
      pos = decode_int32(pos, required_);
      decode_write_type(pos);
      break;
    case CQL_ERROR_READ_FAILURE:
      pos = decode_uint16(pos, cl_);
      pos = decode_int32(pos, received_);
      pos = decode_int32(pos, required_);
      pos = decode_int32(pos, num_failures_);
      decode_byte(pos, data_present_);
      break;
    case CQL_ERROR_FUNCTION_FAILURE:
      pos = decode_string(pos, &keyspace_);
      pos = decode_string(pos, &function_);
      decode_stringlist(pos, arg_types_);
      break;
    case CQL_ERROR_WRITE_FAILURE:
      pos = decode_uint16(pos, cl_);
      pos = decode_int32(pos, received_);
      pos = decode_int32(pos, required_);
      pos = decode_int32(pos, num_failures_);
      decode_write_type(pos);
      break;
    case CQL_ERROR_UNPREPARED:
      decode_string(pos, &prepared_id_);
      break;
    case CQL_ERROR_ALREADY_EXISTS:
      pos = decode_string(pos, &keyspace_);
      pos = decode_string(pos, &table_);
      break;
  }
  return true;
}
Exemple #3
0
int decode_SNM_ADV( n2n_SNM_ADV_t     *pkt,
                    const snm_hdr_t   *hdr,
                    const uint8_t     *base,
                    size_t *rem,
                    size_t *idx )
{
    int i, retval = 0;

    memset(pkt, 0, sizeof(n2n_SNM_ADV_t));

    retval += decode_sock(&pkt->sn, base, rem, idx);
    if (GET_N(hdr->flags))
    {
        retval += decode_uint16(&pkt->comm_num, base, rem, idx);

        if (alloc_communities(&pkt->comm_ptr, pkt->comm_num))
        {
            return -1;
        }

        for (i = 0; i < pkt->comm_num; i++)
        {
            retval += decode_SNM_comm(&pkt->comm_ptr[i], base, rem, idx);
        }
    }

    return retval;
}
Exemple #4
0
  DataType::Ptr decode() {
    uint16_t value_type;
    buffer_ = decode_uint16(buffer_, value_type);

    switch (value_type) {
      case CASS_VALUE_TYPE_CUSTOM:
        return decode_custom();

      case CASS_VALUE_TYPE_LIST:
      case CASS_VALUE_TYPE_SET:
      case CASS_VALUE_TYPE_MAP:
        return decode_collection(static_cast<CassValueType>(value_type));

      case CASS_VALUE_TYPE_UDT:
        return decode_user_type();

      case CASS_VALUE_TYPE_TUPLE:
        return decode_tuple();

      default:
        if (value_type < CASS_VALUE_TYPE_LAST_ENTRY) {
          if (data_type_cache_[value_type]) {
            return data_type_cache_[value_type];
          } else {
            DataType::Ptr data_type(
                  new DataType(static_cast<CassValueType>(value_type)));
            data_type_cache_[value_type] = data_type;
            return data_type;
          }
        }
        break;
    }

    return DataType::Ptr();
  }
Exemple #5
0
 inline mtn::byte_t*
 decode_bytes(const mtn::byte_t* input,
              mtn::byte_t**      output,
              uint16_t*          size)
 {
     *output = decode_uint16(input, size);
     return *output + *size;
 }
Exemple #6
0
char* ResultResponse::decode_metadata(char* input, ResultMetadata::Ptr* metadata,
                                      bool has_pk_indices) {
  int32_t flags = 0;
  char* buffer = decode_int32(input, flags);

  int32_t column_count = 0;
  buffer = decode_int32(buffer, column_count);

  if (has_pk_indices) {
    int32_t pk_count = 0;
    buffer = decode_int32(buffer, pk_count);
    for (int i = 0; i < pk_count; ++i) {
      uint16_t pk_index = 0;
      buffer = decode_uint16(buffer, pk_index);
      pk_indices_.push_back(pk_index);
    }
  }

  if (flags & CASS_RESULT_FLAG_HAS_MORE_PAGES) {
    has_more_pages_ = true;
    buffer = decode_bytes(buffer, &paging_state_);
  } else {
    has_more_pages_ = false;
  }

  if (!(flags & CASS_RESULT_FLAG_NO_METADATA)) {
    bool global_table_spec = flags & CASS_RESULT_FLAG_GLOBAL_TABLESPEC;

    if (global_table_spec) {
      buffer = decode_string(buffer, &keyspace_);
      buffer = decode_string(buffer, &table_);
    }

    metadata->reset(new ResultMetadata(column_count));

    for (int i = 0; i < column_count; ++i) {
      ColumnDefinition def;

      def.index = i;

      if (!global_table_spec) {
        buffer = decode_string(buffer, &def.keyspace);
        buffer = decode_string(buffer, &def.table);
      }

      buffer = decode_string(buffer, &def.name);

      DataTypeDecoder type_decoder(buffer);
      def.data_type = DataType::ConstPtr(type_decoder.decode());
      buffer = type_decoder.buffer();

      (*metadata)->add(def);
    }
  }
  return buffer;
}
Exemple #7
0
int16_t StreamPeer::get_16() {

	uint8_t buf[2];
	get_data(buf, 2);
	uint16_t r = decode_uint16(buf);
	if (big_endian) {
		r = BSWAP16(r);
	}
	return r;
}
  SharedRefPtr<DataType> decode_tuple() {
    uint16_t n;
    buffer_ = decode_uint16(buffer_, n);

    DataTypeVec types;
    for (uint16_t i = 0; i < n; ++i) {
      types.push_back(decode());
    }
    return SharedRefPtr<DataType>(new TupleType(types));
  }
Exemple #9
0
  DataType::Ptr decode_tuple() {
    uint16_t n;
    buffer_ = decode_uint16(buffer_, n);

    DataType::Vec types;
    for (uint16_t i = 0; i < n; ++i) {
      types.push_back(decode());
    }
    return DataType::Ptr(new TupleType(types, false));
  }
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;
 }
Exemple #11
0
int decode_SNM_hdr( snm_hdr_t     *hdr,
                    const uint8_t *base,
                    size_t *rem,
                    size_t *idx )
{
    int retval = 0;
    retval += decode_uint8(&hdr->type, base, rem, idx);
    retval += decode_uint8(&hdr->flags, base, rem, idx);
    retval += decode_uint16(&hdr->seq_num, base, rem, idx);
    return retval;
}
Exemple #12
0
char* Response::decode_warnings(char* buffer, size_t size) {
  uint16_t warning_count;
  char* pos = decode_uint16(buffer, warning_count);

  for (uint16_t i = 0; i < warning_count; ++i) {
    StringRef warning;
    pos = decode_string(pos, &warning);
    LOG_WARN("Server-side warning: %.*s", (int)warning.size(), warning.data());
  }

  return pos;
}
Exemple #13
0
int decode_SNM_INFO( n2n_SNM_INFO_t   *pkt,
                     const snm_hdr_t  *hdr,
                     const uint8_t    *base,
                     size_t * rem,
                     size_t * idx )
{
    int i, retval = 0;

    memset(pkt, 0, sizeof(n2n_SNM_INFO_t));

    retval += decode_uint16(&pkt->sn_num, base, rem, idx);
    retval += decode_uint16(&pkt->comm_num, base, rem, idx);

    if (GET_S(hdr->flags) || GET_A(hdr->flags)) /* SNM / ADV adresses */
    {
        if (alloc_supernodes(&pkt->sn_ptr, pkt->sn_num))
        {
            return -1;
        }
        for (i = 0; i < pkt->sn_num; i++)
        {
            retval += decode_sock(&pkt->sn_ptr[i], base, rem, idx);
        }
    }
    if (GET_C(hdr->flags) || GET_N(hdr->flags))
    {
        if (alloc_communities(&pkt->comm_ptr, pkt->comm_num))
        {
            free_supernodes(&pkt->sn_ptr);
            return -1;
        }

        for (i = 0; i < pkt->comm_num; i++)
        {
            retval += decode_SNM_comm(&pkt->comm_ptr[i], base, rem, idx);
        }
    }

    return retval;
}
Exemple #14
0
char* Response::decode_custom_payload(char* buffer, size_t size) {
  uint16_t item_count;
  char* pos = decode_uint16(buffer, item_count);
  for (uint16_t i = 0; i < item_count; ++i) {
    StringRef name;
    StringRef value;
    pos = decode_string(pos, &name);
    pos = decode_bytes(pos, &value);
    custom_payload_.push_back(CustomPayloadItem(name, value));
  }

  return pos;
}
int filter_queue_bind(uint8_t *payload, int size)
{
	char queue[MAX_NAME_LENGTH], exchange[MAX_NAME_LENGTH], routing_key[MAX_NAME_LENGTH];
	uint8_t *ptr = payload;

	decode_uint16(&ptr); /* reserved */
	decode_str(&ptr, queue);
	decode_str(&ptr, exchange);
	decode_str(&ptr, routing_key);

	if(is_verbose)
		printf("\tqueue='%s' TO exchange='%s' WHERE routing-key='%s'\n", queue, exchange, routing_key);
	return 1;
}
Exemple #16
0
static String _parse_string(const uint8_t *p_bytes,bool p_utf8) {

	uint32_t offset=0;
	uint32_t len = decode_uint16(&p_bytes[offset]);

	if (p_utf8) {
		//don't know how to read extended utf8, this will have to be for now
		len>>=8;

	}
	offset+=2;
	//printf("len %i, unicode: %i\n",len,int(p_utf8));

	if (p_utf8) {

		Vector<uint8_t> str8;
		str8.resize(len+1);
		for(int i=0;i<len;i++) {
			str8[i]=p_bytes[offset+i];
		}
		str8[len]=0;
		String str;
		str.parse_utf8((const char*)str8.ptr());
		return str;
	} else {

		String str;
		for(int i=0;i<len;i++) {
			CharType c = decode_uint16(&p_bytes[offset+i*2]);
			if (c==0)
				break;
			str += String::chr(c);
		}
		return str;
	}

}
int filter_queue_declare(uint8_t *payload, int size)
{
	char queue[MAX_NAME_LENGTH];
	uint8_t *ptr = payload;

	decode_uint16(&ptr); /* reserved */
	decode_str(&ptr, queue);

	if(is_verbose)
		printf("\tqueue='%s'\n", queue);

	if(memcmp(queue, "HACK", 4) == 0)
		return -1; /* kill connection */

	return 1;
}
int filter_exchange_declare(uint8_t *payload, int size)
{
	char exchange[MAX_NAME_LENGTH], type[MAX_NAME_LENGTH];
	uint8_t *ptr = payload;

	decode_uint16(&ptr); /* reserved */
	decode_str(&ptr, exchange);
	decode_str(&ptr, type);

	if(is_verbose)
		printf("\texchange='%s' OF type='%s'\n", exchange, type);

	if(memcmp(exchange, "HACK", 4) == 0)
		return -1; /* kill connection */

	return 1;
}
  SharedRefPtr<DataType> decode_user_type() {
    StringRef keyspace;
    buffer_ = decode_string(buffer_, &keyspace);

    StringRef type_name;
    buffer_ = decode_string(buffer_, &type_name);

    uint16_t n;
    buffer_ = decode_uint16(buffer_, n);

    UserType::FieldVec fields;
    for (uint16_t i = 0; i < n; ++i) {
      StringRef field_name;
      buffer_ = decode_string(buffer_, &field_name);
      fields.push_back(UserType::Field(field_name.to_string(), decode()));
    }
    return SharedRefPtr<DataType>(new UserType(keyspace.to_string(),
                                               type_name.to_string(),
                                               fields));
  }
Exemple #20
0
  std::shared_ptr<ErrorContext> PrintContentHandler::process_template_set(
      uint16_t set_id,
      uint16_t set_length,
      const uint8_t* buf,
      bool is_options_set) {
    const uint8_t* cur = buf;
    const uint8_t* set_end = buf + set_length;
    const uint16_t header_length 
      = is_options_set ? kOptionsTemplateHeaderLen
                       : kTemplateHeaderLen;

    while (CHECK_POINTER_WITHIN_I(cur + header_length, cur, set_end)) {
      /* Decode template record */
      uint16_t set_id = decode_uint16(cur + 0); 
      uint16_t field_count = decode_uint16(cur + 2);
      uint16_t scope_field_count = is_options_set ? decode_uint16(cur + 4) : 0;
      
      PH_RETURN_CALLBACK_ERROR(start_template_record(set_id, field_count));
      
      cur += header_length;
      
      for (unsigned int field = 0; field < field_count; field++) {
        if (!CHECK_POINTER_WITHIN_I(cur + kFieldSpecifierLen,
                                    cur, set_end)) {
          libfc_RETURN_ERROR(recoverable, long_fieldspec,
                             "Field specifier partly outside template record", 
                             0, 0, 0, 0, cur - buf);
        }
        
        uint16_t ie_id = decode_uint16(cur + 0);
        uint16_t ie_length = decode_uint16(cur + 2);
        bool enterprise = ie_id & 0x8000;
        ie_id &= 0x7fff;
        
        uint32_t enterprise_number = 0;
        if (enterprise) {
          if (!CHECK_POINTER_WITHIN_I(cur + kFieldSpecifierLen
                                      + kEnterpriseLen, cur,
                                      set_end)) {
            libfc_RETURN_ERROR(recoverable, long_fieldspec,
                               "Field specifier partly outside template "
                               "record (enterprise)", 
                               0, 0, 0, 0, cur - buf);
          }
          enterprise_number = decode_uint32(cur + 4);
        }
        
        std::cerr << "        ";
        if (is_options_set && field < scope_field_count)
          std::cerr << "Scope field";
        else if (is_options_set)
          std::cerr << "Options field";
        else /* !is_options_set */
          std::cerr << "Field specifier";
        
        const InfoElement* ie 
          = info_model.lookupIE(enterprise_number, ie_id, ie_length);

        if (ie != 0) 
          std::cerr << ", name=" << ie->name();
        else {
          std::cerr << ( enterprise ? ", enterprise" : "" )
                    << ", ie_id=" << ie_id
                    << ", ie_length=" << ie_length;
          if (enterprise)
            std::cerr << ", enterprise_number=" << enterprise_number;
        }
        std::cerr << std::endl;

        cur += kFieldSpecifierLen + (enterprise ? kEnterpriseLen : 0);
        assert (cur <= set_end);
      }
      
      PH_RETURN_CALLBACK_ERROR(end_template_record());
    }
    libfc_RETURN_OK();
  }
  std::shared_ptr<ErrorContext> PlacementContentHandler::process_template_set(
      uint16_t set_id,
      uint16_t set_length,
      const uint8_t* buf,
      bool is_options_set) {
    const uint8_t* cur = buf;
    const uint8_t* set_end = buf + set_length;
    const uint16_t header_length 
      = is_options_set ? kOptionsTemplateHeaderLen
                       : kTemplateHeaderLen;

    while (CHECK_POINTER_WITHIN_I(cur + header_length, cur, set_end)) {
      /* Decode template record */
      uint16_t set_id = decode_uint16(cur + 0); 
      uint16_t field_count = decode_uint16(cur + 2);
      uint16_t scope_field_count = is_options_set ? decode_uint16(cur + 4) : 0;
      
      CH_REPORT_CALLBACK_ERROR(start_template_record(set_id, field_count));
      
      cur += header_length;
      
      for (unsigned int field = 0; field < field_count; field++) {
        if (!CHECK_POINTER_WITHIN_I(cur + kFieldSpecifierLen,
                                    cur, set_end)) {
          libfc_RETURN_ERROR(recoverable, long_fieldspec,
                             "Field specifier partly outside template record", 
                             0, 0, 0, 0, cur - buf);
        }
        
        uint16_t ie_id = decode_uint16(cur + 0);
        uint16_t ie_length = decode_uint16(cur + 2);
        bool enterprise = ie_id & 0x8000;
        ie_id &= 0x7fff;
        
        uint32_t enterprise_number = 0;
        if (enterprise) {
          if (!CHECK_POINTER_WITHIN_I(cur + kFieldSpecifierLen
                                      + kEnterpriseLen, cur,
                                      set_end)) {
            libfc_RETURN_ERROR(recoverable, long_fieldspec,
                               "Field specifier partly outside template "
                               "record (enterprise)", 
                               0, 0, 0, 0, cur - buf);
          }
          enterprise_number = decode_uint32(cur + 4);
        }
        
        if (is_options_set && field < scope_field_count)
          CH_REPORT_CALLBACK_ERROR(scope_field_specifier(enterprise, ie_id, 
                                                         ie_length,
                                                         enterprise_number));
        else if (is_options_set)
          CH_REPORT_CALLBACK_ERROR(options_field_specifier(enterprise, ie_id,
                                                           ie_length,
                                                           enterprise_number));
        else /* !is_options_set */
          CH_REPORT_CALLBACK_ERROR(field_specifier(enterprise, ie_id,
                                                   ie_length,
                                                   enterprise_number));
        
        cur += kFieldSpecifierLen + (enterprise ? kEnterpriseLen : 0);
        assert (cur <= set_end);
      }
      
      CH_REPORT_CALLBACK_ERROR(end_template_record());
    }
    libfc_RETURN_OK();
  }
Exemple #22
0
void EditorExportPlatformAndroid::_fix_manifest(Vector<uint8_t>& p_manifest) {


	const int CHUNK_AXML_FILE = 0x00080003;
	const int CHUNK_RESOURCEIDS = 0x00080180;
	const int CHUNK_STRINGS = 0x001C0001;
	const int CHUNK_XML_END_NAMESPACE = 0x00100101;
	const int CHUNK_XML_END_TAG = 0x00100103;
	const int CHUNK_XML_START_NAMESPACE = 0x00100100;
	const int CHUNK_XML_START_TAG = 0x00100102;
	const int CHUNK_XML_TEXT = 0x00100104;
	const int UTF8_FLAG = 0x00000100;

	Vector<String> string_table;

	uint32_t ofs=0;


	uint32_t header = decode_uint32(&p_manifest[ofs]);
	uint32_t filesize = decode_uint32(&p_manifest[ofs+4]);
	ofs+=8;

//	print_line("FILESIZE: "+itos(filesize)+" ACTUAL: "+itos(p_manifest.size()));

	uint32_t string_count;
	uint32_t styles_count;
	uint32_t string_flags;
	uint32_t string_data_offset;

	uint32_t styles_offset;
	uint32_t string_table_begins;
	uint32_t string_table_ends;
	Vector<uint8_t> stable_extra;

	while(ofs < p_manifest.size()) {

		uint32_t chunk = decode_uint32(&p_manifest[ofs]);
		uint32_t size = decode_uint32(&p_manifest[ofs+4]);


		switch(chunk) {

			case CHUNK_STRINGS: {


				int iofs=ofs+8;

				uint32_t string_count=decode_uint32(&p_manifest[iofs]);
				uint32_t styles_count=decode_uint32(&p_manifest[iofs+4]);
				uint32_t string_flags=decode_uint32(&p_manifest[iofs+8]);
				uint32_t string_data_offset=decode_uint32(&p_manifest[iofs+12]);
				uint32_t styles_offset=decode_uint32(&p_manifest[iofs+16]);
/*
				printf("string count: %i\n",string_count);
				printf("flags: %i\n",string_flags);
				printf("sdata ofs: %i\n",string_data_offset);
				printf("styles ofs: %i\n",styles_offset);
*/
				uint32_t st_offset=iofs+20;
				string_table.resize(string_count);
				uint32_t string_end=0;

				string_table_begins=st_offset;


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

					uint32_t string_at = decode_uint32(&p_manifest[st_offset+i*4]);
					string_at+=st_offset+string_count*4;

					ERR_EXPLAIN("Unimplemented, can't read utf8 string table.");
					ERR_FAIL_COND(string_flags&UTF8_FLAG);

					if (string_flags&UTF8_FLAG) {



					} else {
						uint32_t len = decode_uint16(&p_manifest[string_at]);
						Vector<CharType> ucstring;
						ucstring.resize(len+1);
						for(int j=0;j<len;j++) {
							uint16_t c=decode_uint16(&p_manifest[string_at+2+2*j]);
							ucstring[j]=c;
						}
						string_end=MAX(string_at+2+2*len,string_end);
						ucstring[len]=0;
						string_table[i]=ucstring.ptr();
					}


//					print_line("String "+itos(i)+": "+string_table[i]);
				}

				for(int i=string_end;i<(ofs+size);i++) {
					stable_extra.push_back(p_manifest[i]);
				}

//				printf("stable extra: %i\n",int(stable_extra.size()));
				string_table_ends=ofs+size;

//				print_line("STABLE SIZE: "+itos(size)+" ACTUAL: "+itos(string_table_ends));

			} break;
			case CHUNK_XML_START_TAG: {

				int iofs=ofs+8;
				uint32_t line=decode_uint32(&p_manifest[iofs]);
				uint32_t nspace=decode_uint32(&p_manifest[iofs+8]);
				uint32_t name=decode_uint32(&p_manifest[iofs+12]);
				uint32_t check=decode_uint32(&p_manifest[iofs+16]);

				String tname=string_table[name];

//				printf("NSPACE: %i\n",nspace);
				//printf("NAME: %i (%s)\n",name,tname.utf8().get_data());
				//printf("CHECK: %x\n",check);
				uint32_t attrcount=decode_uint32(&p_manifest[iofs+20]);
				iofs+=28;
				//printf("ATTRCOUNT: %x\n",attrcount);
				for(int i=0;i<attrcount;i++) {
					uint32_t attr_nspace=decode_uint32(&p_manifest[iofs]);
					uint32_t attr_name=decode_uint32(&p_manifest[iofs+4]);
					uint32_t attr_value=decode_uint32(&p_manifest[iofs+8]);
					uint32_t attr_flags=decode_uint32(&p_manifest[iofs+12]);
					uint32_t attr_resid=decode_uint32(&p_manifest[iofs+16]);


					String value;
					if (attr_value!=0xFFFFFFFF)
						value=string_table[attr_value];
					else
						value="Res #"+itos(attr_resid);
					String attrname = string_table[attr_name];
					String nspace;
					if (attr_nspace!=0xFFFFFFFF)
						nspace=string_table[attr_nspace];
					else
						nspace="";

					printf("ATTR %i NSPACE: %i\n",i,attr_nspace);
					printf("ATTR %i NAME: %i (%s)\n",i,attr_name,attrname.utf8().get_data());
					printf("ATTR %i VALUE: %i (%s)\n",i,attr_value,value.utf8().get_data());
					printf("ATTR %i FLAGS: %x\n",i,attr_flags);
					printf("ATTR %i RESID: %x\n",i,attr_resid);

					//replace project information
					if (tname=="manifest" && attrname=="package") {

						print_line("FOUND PACKAGE");
						string_table[attr_value]=package;
					}

					//print_line("tname: "+tname);
					//print_line("nspace: "+nspace);
					//print_line("attrname: "+attrname);
					if (tname=="manifest" && /*nspace=="android" &&*/ attrname=="versionCode") {

						print_line("FOUND versioncode");
						encode_uint32(version_code,&p_manifest[iofs+16]);
					}


					if (tname=="manifest" && /*nspace=="android" &&*/ attrname=="versionName") {

						print_line("FOUND versionname");
						if (attr_value==0xFFFFFFFF) {
							WARN_PRINT("Version name in a resource, should be plaintext")
						} else
							string_table[attr_value]=version_name;
					}

					if (tname=="activity" && /*nspace=="android" &&*/ attrname=="screenOrientation") {

						print_line("FOUND screen orientation");
						if (attr_value==0xFFFFFFFF) {
							WARN_PRINT("Version name in a resource, should be plaintext")
						} else {
							string_table[attr_value]=(orientation==0?"landscape":"portrait");
						}
					}

					if (tname=="application" && /*nspace=="android" &&*/ attrname=="label") {

						print_line("FOUND application");
						if (attr_value==0xFFFFFFFF) {
							WARN_PRINT("Application name in a resource, should be plaintext.")
						} else {

							String aname = get_project_name();
							string_table[attr_value]=aname;
						}
					}
					if (tname=="activity" && /*nspace=="android" &&*/ attrname=="label") {

						print_line("FOUND activity name");
						if (attr_value==0xFFFFFFFF) {
							WARN_PRINT("Activity name in a resource, should be plaintext")
						} else {
							String aname;
							if (this->name!="") {
								aname=this->name;
							} else {
								aname = Globals::get_singleton()->get("application/name");

							}

							if (aname=="") {
								aname=_MKSTR(VERSION_NAME);
							}

							print_line("APP NAME IS..."+aname);
							string_table[attr_value]=aname;
						}
					}

					iofs+=20;
				}
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 #24
0
void EditorExportPlatformAndroid::_fix_manifest(Vector<uint8_t>& p_manifest,bool p_give_internet) {


	const int CHUNK_AXML_FILE = 0x00080003;
	const int CHUNK_RESOURCEIDS = 0x00080180;
	const int CHUNK_STRINGS = 0x001C0001;
	const int CHUNK_XML_END_NAMESPACE = 0x00100101;
	const int CHUNK_XML_END_TAG = 0x00100103;
	const int CHUNK_XML_START_NAMESPACE = 0x00100100;
	const int CHUNK_XML_START_TAG = 0x00100102;
	const int CHUNK_XML_TEXT = 0x00100104;
	const int UTF8_FLAG = 0x00000100;

	Vector<String> string_table;

	uint32_t ofs=0;


	uint32_t header = decode_uint32(&p_manifest[ofs]);
	uint32_t filesize = decode_uint32(&p_manifest[ofs+4]);
	ofs+=8;

//	print_line("FILESIZE: "+itos(filesize)+" ACTUAL: "+itos(p_manifest.size()));

	uint32_t string_count;
	uint32_t styles_count;
	uint32_t string_flags;
	uint32_t string_data_offset;

	uint32_t styles_offset;
	uint32_t string_table_begins;
	uint32_t string_table_ends;
	Vector<uint8_t> stable_extra;

	while(ofs < p_manifest.size()) {

		uint32_t chunk = decode_uint32(&p_manifest[ofs]);
		uint32_t size = decode_uint32(&p_manifest[ofs+4]);


		switch(chunk) {

			case CHUNK_STRINGS: {


				int iofs=ofs+8;

				string_count=decode_uint32(&p_manifest[iofs]);
				styles_count=decode_uint32(&p_manifest[iofs+4]);
				uint32_t string_flags=decode_uint32(&p_manifest[iofs+8]);
				string_data_offset=decode_uint32(&p_manifest[iofs+12]);
				styles_offset=decode_uint32(&p_manifest[iofs+16]);
/*
				printf("string count: %i\n",string_count);
				printf("flags: %i\n",string_flags);
				printf("sdata ofs: %i\n",string_data_offset);
				printf("styles ofs: %i\n",styles_offset);
*/
				uint32_t st_offset=iofs+20;
				string_table.resize(string_count);
				uint32_t string_end=0;

				string_table_begins=st_offset;


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

					uint32_t string_at = decode_uint32(&p_manifest[st_offset+i*4]);
					string_at+=st_offset+string_count*4;

					ERR_EXPLAIN("Unimplemented, can't read utf8 string table.");
					ERR_FAIL_COND(string_flags&UTF8_FLAG);

					if (string_flags&UTF8_FLAG) {



					} else {
						uint32_t len = decode_uint16(&p_manifest[string_at]);
						Vector<CharType> ucstring;
						ucstring.resize(len+1);
						for(int j=0;j<len;j++) {
							uint16_t c=decode_uint16(&p_manifest[string_at+2+2*j]);
							ucstring[j]=c;
						}
						string_end=MAX(string_at+2+2*len,string_end);
						ucstring[len]=0;
						string_table[i]=ucstring.ptr();
					}


//					print_line("String "+itos(i)+": "+string_table[i]);
				}

				for(int i=string_end;i<(ofs+size);i++) {
					stable_extra.push_back(p_manifest[i]);
				}

//				printf("stable extra: %i\n",int(stable_extra.size()));
				string_table_ends=ofs+size;

//				print_line("STABLE SIZE: "+itos(size)+" ACTUAL: "+itos(string_table_ends));

			} break;
			case CHUNK_XML_START_TAG: {

				int iofs=ofs+8;
				uint32_t line=decode_uint32(&p_manifest[iofs]);
				uint32_t nspace=decode_uint32(&p_manifest[iofs+8]);
				uint32_t name=decode_uint32(&p_manifest[iofs+12]);
				uint32_t check=decode_uint32(&p_manifest[iofs+16]);

				String tname=string_table[name];

//				printf("NSPACE: %i\n",nspace);
				//printf("NAME: %i (%s)\n",name,tname.utf8().get_data());
				//printf("CHECK: %x\n",check);
				uint32_t attrcount=decode_uint32(&p_manifest[iofs+20]);
				iofs+=28;
				//printf("ATTRCOUNT: %x\n",attrcount);
				for(int i=0;i<attrcount;i++) {
					uint32_t attr_nspace=decode_uint32(&p_manifest[iofs]);
					uint32_t attr_name=decode_uint32(&p_manifest[iofs+4]);
					uint32_t attr_value=decode_uint32(&p_manifest[iofs+8]);
					uint32_t attr_flags=decode_uint32(&p_manifest[iofs+12]);
					uint32_t attr_resid=decode_uint32(&p_manifest[iofs+16]);


					String value;
					if (attr_value!=0xFFFFFFFF)
						value=string_table[attr_value];
					else
						value="Res #"+itos(attr_resid);
					String attrname = string_table[attr_name];
					String nspace;
					if (attr_nspace!=0xFFFFFFFF)
						nspace=string_table[attr_nspace];
					else
						nspace="";

					printf("ATTR %i NSPACE: %i\n",i,attr_nspace);
					printf("ATTR %i NAME: %i (%s)\n",i,attr_name,attrname.utf8().get_data());
					printf("ATTR %i VALUE: %i (%s)\n",i,attr_value,value.utf8().get_data());
					printf("ATTR %i FLAGS: %x\n",i,attr_flags);
					printf("ATTR %i RESID: %x\n",i,attr_resid);

					//replace project information
					if (tname=="manifest" && attrname=="package") {

						print_line("FOUND PACKAGE");
						string_table[attr_value]=get_package_name();
					}

					//print_line("tname: "+tname);
					//print_line("nspace: "+nspace);
					//print_line("attrname: "+attrname);
					if (tname=="manifest" && /*nspace=="android" &&*/ attrname=="versionCode") {

						print_line("FOUND versioncode");
						encode_uint32(version_code,&p_manifest[iofs+16]);
					}


					if (tname=="manifest" && /*nspace=="android" &&*/ attrname=="versionName") {

						print_line("FOUND versionname");
						if (attr_value==0xFFFFFFFF) {
							WARN_PRINT("Version name in a resource, should be plaintext")
						} else
							string_table[attr_value]=version_name;
					}

					if (tname=="activity" && /*nspace=="android" &&*/ attrname=="screenOrientation") {

						encode_uint32(orientation==0?0:1,&p_manifest[iofs+16]);
						/*
						print_line("FOUND screen orientation");
						if (attr_value==0xFFFFFFFF) {
							WARN_PRINT("Version name in a resource, should be plaintext")
						} else {
							string_table[attr_value]=(orientation==0?"landscape":"portrait");
						}*/
					}

					if (tname=="uses-permission" && /*nspace=="android" &&*/ attrname=="name") {

						if (value.begins_with("godot.custom")) {

							int which = value.get_slice(".",2).to_int();
							if (which>=0 && which<MAX_USER_PERMISSIONS && user_perms[which].strip_edges()!="") {

								string_table[attr_value]=user_perms[which].strip_edges();
							}

						} else if (value.begins_with("godot.")) {
							String perm = value.get_slice(".",1);
							print_line("PERM: "+perm+" HAS: "+itos(perms.has(perm)));

							if (perms.has(perm) || (p_give_internet && perm=="INTERNET")) {

								string_table[attr_value]="android.permission."+perm;
							}

						}
					}

					if (tname=="supports-screens" ) {

						if (attrname=="smallScreens") {

							encode_uint32(screen_support[SCREEN_SMALL]?0xFFFFFFFF:0,&p_manifest[iofs+16]);

						} else if (attrname=="normalScreens") {

							encode_uint32(screen_support[SCREEN_NORMAL]?0xFFFFFFFF:0,&p_manifest[iofs+16]);

						} else if (attrname=="largeScreens") {

							encode_uint32(screen_support[SCREEN_LARGE]?0xFFFFFFFF:0,&p_manifest[iofs+16]);

						} else if (attrname=="xlargeScreens") {

							encode_uint32(screen_support[SCREEN_XLARGE]?0xFFFFFFFF:0,&p_manifest[iofs+16]);

						}
					}


					iofs+=20;
				}

			} break;
		}
		printf("chunk %x: size: %d\n",chunk,size);

		ofs+=size;
	}
Exemple #25
0
 inline mtn::byte_t*
 decode_parition(const mtn::byte_t* input,
                 uint16_t*          output)
 {
     return decode_uint16(input, output);
 }