Esempio n. 1
0
/*
 * nondata_nodes_cmp - compare 2 non-data nodes.
 * @priv: UBIFS file-system description object
 * @a: first node
 * @a: second node
 *
 * This function compares nodes @a and @b. It makes sure that inode nodes go
 * first and sorted by length in descending order. Directory entry nodes go
 * after inode nodes and are sorted in ascending hash valuer order.
 */
static int nondata_nodes_cmp(void *priv, struct list_head *a,
			     struct list_head *b)
{
	ino_t inuma, inumb;
	struct ubifs_info *c = priv;
	struct ubifs_scan_node *sa, *sb;

	cond_resched();
	if (a == b)
		return 0;

	sa = list_entry(a, struct ubifs_scan_node, list);
	sb = list_entry(b, struct ubifs_scan_node, list);

	ubifs_assert(key_type(c, &sa->key) != UBIFS_DATA_KEY &&
		     key_type(c, &sb->key) != UBIFS_DATA_KEY);
	ubifs_assert(sa->type != UBIFS_DATA_NODE &&
		     sb->type != UBIFS_DATA_NODE);

	/* Inodes go before directory entries */
	if (sa->type == UBIFS_INO_NODE) {
		if (sb->type == UBIFS_INO_NODE)
			return sb->len - sa->len;
		return -1;
	}
	if (sb->type == UBIFS_INO_NODE)
		return 1;

	ubifs_assert(key_type(c, &sa->key) == UBIFS_DENT_KEY ||
		     key_type(c, &sa->key) == UBIFS_XENT_KEY);
	ubifs_assert(key_type(c, &sb->key) == UBIFS_DENT_KEY ||
		     key_type(c, &sb->key) == UBIFS_XENT_KEY);
	ubifs_assert(sa->type == UBIFS_DENT_NODE ||
		     sa->type == UBIFS_XENT_NODE);
	ubifs_assert(sb->type == UBIFS_DENT_NODE ||
		     sb->type == UBIFS_XENT_NODE);

	inuma = key_inum(c, &sa->key);
	inumb = key_inum(c, &sb->key);

	if (inuma == inumb) {
		uint32_t hasha = key_hash(c, &sa->key);
		uint32_t hashb = key_hash(c, &sb->key);

		if (hasha <= hashb)
			return -1;
	} else if (inuma <= inumb)
		return -1;

	return 1;
}
Esempio n. 2
0
  Object* LookupTable::store(STATE, Object* key, Object* val) {
    unsigned int num_entries, num_bins, bin;
    LookupTableBucket* entry;
    LookupTableBucket* last = NULL;

    num_entries = entries_->to_native();
    num_bins = bins_->to_native();

    if(max_density_p(num_entries, num_bins)) {
      redistribute(state, num_bins <<= 1);
    }

    key_to_sym(key);
    bin = find_bin(key_hash(key), num_bins);
    entry = try_as<LookupTableBucket>(values_->at(state, bin));

    while(entry) {
      if(entry->key() == key) {
        entry->value(state, val);
        return val;
      }
      last = entry;
      entry = try_as<LookupTableBucket>(entry->next());
    }

    if(last) {
      last->next(state, LookupTableBucket::create(state, key, val));
    } else {
      values_->put(state, bin, LookupTableBucket::create(state, key, val));
    }

    entries(state, Fixnum::from(num_entries + 1));
    return val;
  }
Esempio n. 3
0
void hashmap_remove(HashMap *hashmap, const char *key) {
  assert(hashmap != NULL);
  assert(key != NULL);

  int h = entry_key_hash(key);
  int hash = key_hash(h);
  int index = index_for_hash(hash, hashmap->capacity_);
  Entry *prev = NULL;
  for (Entry *entry = hashmap->entries_[index]; entry; entry = entry->next_) {
    if (hash == entry->hash_ || strcmp(entry->key_, key) == 0) {
      if (prev) {
        prev->next_ = entry->next_;
      } else {
        hashmap->entries_[index] = entry->next_;
      }
      if (hashmap->on_entry_removed_) {
        hashmap->on_entry_removed_(entry->key_, entry->data_);
      }
      free((char *)entry->key_);
      free(entry);
      --hashmap->size_;
    }

    prev = entry;
  }
}
Esempio n. 4
0
  void LookupTable::redistribute(STATE, size_t size) {
    size_t num = bins_->to_native();
    Tuple* new_values = Tuple::create(state, size);

    for(size_t i = 0; i < num; i++) {
      Tuple* entry = try_as<Tuple>(values_->at(state, i));

      while(entry) {
        Tuple* link = try_as<Tuple>(entry->at(state, 2));
        entry->put(state, 2, Qnil);

        size_t bin = find_bin(key_hash(entry->at(state, 0)), size);
        Tuple* slot = try_as<Tuple>(new_values->at(state, bin));

        if(!slot) {
          new_values->put(state, bin, entry);
        } else {
          entry_append(state, slot, entry);
        }

        entry = link;
      }
    }

    values(state, new_values);
    bins(state, Fixnum::from(size));
  }
// Explicit calculation which is used to test the wallet constant
// We get the same virtual size due to rounding(weight/4) for both use_max_sig values
static size_t CalculateNestedKeyhashInputSize(bool use_max_sig)
{
    // Generate ephemeral valid pubkey
    CKey key;
    key.MakeNewKey(true);
    CPubKey pubkey = key.GetPubKey();

    // Generate pubkey hash
    uint160 key_hash(Hash160(pubkey.begin(), pubkey.end()));

    // Create inner-script to enter into keystore. Key hash can't be 0...
    CScript inner_script = CScript() << OP_0 << std::vector<unsigned char>(key_hash.begin(), key_hash.end());

    // Create outer P2SH script for the output
    uint160 script_id(Hash160(inner_script.begin(), inner_script.end()));
    CScript script_pubkey = CScript() << OP_HASH160 << std::vector<unsigned char>(script_id.begin(), script_id.end()) << OP_EQUAL;

    // Add inner-script to key store and key to watchonly
    CBasicKeyStore keystore;
    keystore.AddCScript(inner_script);
    keystore.AddKeyPubKey(key, pubkey);

    // Fill in dummy signatures for fee calculation.
    SignatureData sig_data;

    if (!ProduceSignature(keystore, use_max_sig ? DUMMY_MAXIMUM_SIGNATURE_CREATOR : DUMMY_SIGNATURE_CREATOR, script_pubkey, sig_data)) {
        // We're hand-feeding it correct arguments; shouldn't happen
        assert(false);
    }

    CTxIn tx_in;
    UpdateInput(tx_in, sig_data);
    return (size_t)GetVirtualTransactionInputSize(tx_in);
}
Esempio n. 6
0
  void MethodTable::redistribute(STATE, size_t size) {
    size_t num = bins_->to_native();
    Tuple* new_values = Tuple::create(state, size);

    for(size_t i = 0; i < num; i++) {
      MethodTableBucket* entry = try_as<MethodTableBucket>(values_->at(state, i));

      while(entry) {
        MethodTableBucket* link = try_as<MethodTableBucket>(entry->next());
        entry->next(state, nil<MethodTableBucket>());

        size_t bin = find_bin(key_hash(entry->name()), size);
        MethodTableBucket* slot = try_as<MethodTableBucket>(new_values->at(state, bin));

        if(slot) {
          slot->append(state, entry);
        } else {
          new_values->put(state, bin, entry);
        }

        entry = link;
      }
    }

    values(state, new_values);
    bins(state, Fixnum::from(size));
  }
Esempio n. 7
0
static void sprintf_key(const struct ubifs_info *c, const union ubifs_key *key,
			char *buffer)
{
	char *p = buffer;
	int type = key_type(c, key);

	if (c->key_fmt == UBIFS_SIMPLE_KEY_FMT) {
		switch (type) {
		case UBIFS_INO_KEY:
			sprintf(p, "(%lu, %s)", (unsigned long)key_inum(c, key),
			       get_key_type(type));
			break;
		case UBIFS_DENT_KEY:
		case UBIFS_XENT_KEY:
			sprintf(p, "(%lu, %s, %#08x)",
				(unsigned long)key_inum(c, key),
				get_key_type(type), key_hash(c, key));
			break;
		case UBIFS_DATA_KEY:
			sprintf(p, "(%lu, %s, %u)",
				(unsigned long)key_inum(c, key),
				get_key_type(type), key_block(c, key));
			break;
		case UBIFS_TRUN_KEY:
			sprintf(p, "(%lu, %s)",
				(unsigned long)key_inum(c, key),
				get_key_type(type));
			break;
		default:
			sprintf(p, "(bad key type: %#08x, %#08x)",
				key->u32[0], key->u32[1]);
		}
	} else
		sprintf(p, "bad key format %d", c->key_fmt);
}
Esempio n. 8
0
  Executable* MethodTable::remove(STATE, Symbol* name) {
    hashval bin;
    MethodTableBucket* entry;
    MethodTableBucket* last = NULL;

    size_t num_entries = entries_->to_native();
    size_t num_bins = bins_->to_native();

    if(min_density_p(num_entries, num_bins) && (num_bins >> 1) >= METHODTABLE_MIN_SIZE) {
      redistribute(state, num_bins >>= 1);
    }

    bin = find_bin(key_hash(name), num_bins);
    entry = try_as<MethodTableBucket>(values_->at(state, bin));

    while(entry) {
      if(entry->name() == name) {
        Executable* val = entry->method();
        if(last) {
          last->next(state, entry->next());
        } else {
          values_->put(state, bin, entry->next());
        }
        entries(state, Fixnum::from(entries_->to_native() - 1));
        return val;
      }

      last = entry;
      entry = try_as<MethodTableBucket>(entry->next());
    }

    return reinterpret_cast<Executable*>(Qnil);
  }
Esempio n. 9
0
  Object* LookupTable::remove(STATE, Object* key, bool* removed) {
    hashval bin;
    LookupTableBucket* entry;
    LookupTableBucket* last = NULL;

    size_t num_entries = entries_->to_native();
    size_t num_bins = bins_->to_native();

    if(min_density_p(num_entries, num_bins) && (num_bins >> 1) >= LOOKUPTABLE_MIN_SIZE) {
      redistribute(state, num_bins >>= 1);
    }

    key_to_sym(key);
    bin = find_bin(key_hash(key), num_bins);
    entry = try_as<LookupTableBucket>(values_->at(state, bin));

    while(entry) {
      if(entry->key() == key) {
        Object *val = entry->value();
        if(last) {
          last->next(state, entry->next());
        } else {
          values_->put(state, bin, entry->next());
        }
        entries(state, Fixnum::from(entries_->to_native() - 1));
        if(removed) *removed = true;
        return val;
      }

      last = entry;
      entry = try_as<LookupTableBucket>(entry->next());
    }

    return Qnil;
  }
Esempio n. 10
0
  Object* MethodTable::alias(STATE, Symbol* name, Symbol* vis,
                             Symbol* orig_name, Object* orig_method,
                             Module* orig_mod)
  {
    check_frozen(state);

    utilities::thread::SpinLock::LockGuard lg(lock_);

    Executable* orig_exec;

    if(Alias* alias = try_as<Alias>(orig_method)) {
      orig_exec = alias->original_exec();
      orig_mod = alias->original_module();
      orig_name = alias->original_name();
    } else if(orig_method->nil_p()) {
      orig_exec = nil<Executable>();
    } else {
      orig_exec = as<Executable>(orig_method);
    }

    Alias* method = Alias::create(state, orig_name, orig_mod, orig_exec);

    native_int num_entries = entries_->to_native();
    native_int num_bins = bins_->to_native();

    if(max_density_p(num_entries, num_bins)) {
      redistribute(state, num_bins <<= 1);
    }

    native_int bin = find_bin(key_hash(name), num_bins);

    MethodTableBucket* entry = try_as<MethodTableBucket>(values_->at(state, bin));
    MethodTableBucket* last = NULL;

    while(entry) {
      if(entry->name() == name) {
        entry->method_id(state, nil<String>());
        entry->method(state, method);
        entry->scope(state, cNil);
        entry->serial(state, Fixnum::from(0));
        entry->visibility(state, vis);
        return name;
      }

      last = entry;
      entry = try_as<MethodTableBucket>(entry->next());
    }

    if(last) {
      last->next(state, MethodTableBucket::create(
            state, name, nil<String>(), method, cNil, Fixnum::from(0), vis));
    } else {
      values_->put(state, bin, MethodTableBucket::create(
            state, name, nil<String>(), method, cNil, Fixnum::from(0), vis));
    }

    entries(state, Fixnum::from(num_entries + 1));
    return name;
  }
Esempio n. 11
0
  Object* MethodTable::store(STATE, Symbol* name, Object* exec, Symbol* vis)
  {
    check_frozen(state);

    utilities::thread::SpinLock::LockGuard lg(lock_);

    Executable* method;
    if(exec->nil_p()) {
      method = nil<Executable>();
    } else {
      if(Alias* stored_alias = try_as<Alias>(exec)) {
        lock_.unlock();
        Object* res = alias(state, name, vis,
                           stored_alias->original_name(),
                           stored_alias->original_exec(),
                           stored_alias->original_module());
        lock_.lock();
        return res;
      } else {
        method = as<Executable>(exec);
      }
    }

    native_int num_entries = entries_->to_native();
    native_int num_bins = bins_->to_native();

    if(max_density_p(num_entries, num_bins)) {
      redistribute(state, num_bins <<= 1);
    }

    native_int bin = find_bin(key_hash(name), num_bins);

    MethodTableBucket* entry = try_as<MethodTableBucket>(values_->at(state, bin));
    MethodTableBucket* last = NULL;

    while(entry) {
      if(entry->name() == name) {
        entry->method(state, method);
        entry->visibility(state, vis);
        return name;
      }

      last = entry;
      entry = try_as<MethodTableBucket>(entry->next());
    }

    if(last) {
      last->next(state, MethodTableBucket::create(state, name, method, vis));
    } else {
      values_->put(state, bin,
                         MethodTableBucket::create(state, name, method, vis));
    }

    entries(state, Fixnum::from(num_entries + 1));

    return name;
  }
Esempio n. 12
0
int goap_state_get(struct goap_state * state, const char * key) {
    unsigned int hash = key_hash(key);

    for(int i = 0; i < state->mask + 1; i++) {
        unsigned int index = (hash + i) & state->mask;
        if(state->pairs[index].hash == hash && strcmp(state->pairs[index].key, key) == 0) {
            return state->pairs[index].value;
        }
    }
    return 0;
}
Esempio n. 13
0
unsigned int goap_node_hash(void * node, void * ud) {
    struct goap_state * state = (struct goap_state *)node;

    unsigned int result = 0;
    for(int i = 0; i < state->mask + 1; i++) {
        if(state->pairs[i].key) {
            result ^= key_hash(state->pairs[i].key) ^ state->pairs[i].value;
        }
    }
    return result;
}
Esempio n. 14
0
void calculate_password_key_and_hash(const char *a_password,
				     char *a_key, char *a_hash)
{
	char key[256 / 8];
	char hash[256 / 8];

	key_from_password(a_password, key);
	htob64(key, a_key, 32);

	key_hash(key, hash);
	htob64(hash, a_hash, 32);
}
Esempio n. 15
0
  LookupTableBucket* LookupTable::find_entry(STATE, Object* key) {
    unsigned int bin;

    key_to_sym(key);
    bin = find_bin(key_hash(key), bins_->to_native());
    LookupTableBucket *entry = try_as<LookupTableBucket>(values_->at(state, bin));

    while(entry) {
      if(entry->key() == key) {
        return entry;
      }
      entry = try_as<LookupTableBucket>(entry->next());
    }
    return reinterpret_cast<LookupTableBucket *>(Qnil);
  }
Esempio n. 16
0
  MethodTableBucket* MethodTable::find_entry(Symbol* name) {
    unsigned int bin;

    bin = find_bin(key_hash(name), bins_->to_native());
    MethodTableBucket *entry = try_as<MethodTableBucket>(values_->at(bin));

    while(entry) {
      if(entry->name() == name) {
        return entry;
      }
      entry = try_as<MethodTableBucket>(entry->next());
    }

    return 0;
  }
Esempio n. 17
0
static key_t counter_get_key(const char *path) {
  size_t pathlen;
  unsigned int h;
  key_t key;

  /* ftok() uses stat(2) on the given path, which means that it needs to exist.
   * So we AVOID using ftok(3), and instead generate the key value ourselves.
   */

  pathlen = strlen(path);
  h = key_hash(path, pathlen);
  key = h & COUNTER_PROJ_ID;

  return key;
}
Esempio n. 18
0
void *hashmap_get(HashMap *hashmap, const char *key) {
  assert(hashmap != NULL);
  assert(key != NULL);

  int h = entry_key_hash(key);
  int hash = key_hash(h);
  int index = index_for_hash(hash, hashmap->capacity_);

  for (Entry *entry = hashmap->entries_[index]; entry; entry = entry->next_) {
    if (strcmp(entry->key_, key) == 0) {
      return entry->data_;
    }
  }

  return NULL;
}
Esempio n. 19
0
  MethodTableBucket* MethodTable::find_entry(Symbol* name) {
    unsigned int bin;
    utilities::thread::SpinLock::LockGuard lg(lock_);

    bin = find_bin(key_hash(name), bins_->to_native());
    MethodTableBucket *entry = try_as<MethodTableBucket>(values_->at(bin));

    while(entry) {
      if(entry->name() == name) {
        return entry;
      }
      entry = try_as<MethodTableBucket>(entry->next());
    }

    return 0;
  }
Esempio n. 20
0
  Object* MethodTable::store(STATE, Symbol* name, Object* exec, Symbol* vis) {
    unsigned int num_entries, num_bins, bin;
    MethodTableBucket* entry;
    MethodTableBucket* last = NULL;

    Executable* method;
    if(exec->nil_p()) {
      method = reinterpret_cast<Executable*>(Qnil);
    } else {
      if(Alias* alias = try_as<Alias>(exec)) {
        method = alias->original_exec();
      } else {
        method = as<Executable>(exec);
      }
    }

    num_entries = entries_->to_native();
    num_bins = bins_->to_native();

    if(max_density_p(num_entries, num_bins)) {
      redistribute(state, num_bins <<= 1);
    }

    bin = find_bin(key_hash(name), num_bins);
    entry = try_as<MethodTableBucket>(values_->at(state, bin));

    while(entry) {
      if(entry->name() == name) {
        entry->method(state, method);
        entry->visibility(state, vis);
        return name;
      }
      last = entry;
      entry = try_as<MethodTableBucket>(entry->next());
    }

    if(last) {
      last->next(state, MethodTableBucket::create(state, name, method, vis));
    } else {
      values_->put(state, bin, MethodTableBucket::create(state, name, method, vis));
    }

    entries(state, Fixnum::from(num_entries + 1));
    return name;
  }
Esempio n. 21
0
  Object* MethodTable::alias(STATE, Symbol* name, Symbol* vis,
                             Symbol* orig_name, Executable* orig_method,
                             Module* orig_mod)
  {
    unsigned int num_entries, num_bins, bin;
    MethodTableBucket* entry;
    MethodTableBucket* last = NULL;

    if(Alias* alias = try_as<Alias>(orig_method)) {
      orig_method = alias->original_exec();
      orig_mod = alias->original_module();
      orig_name = alias->original_name();
    }

    Alias* method = Alias::create(state, orig_name, orig_mod, orig_method);

    num_entries = entries_->to_native();
    num_bins = bins_->to_native();

    if(max_density_p(num_entries, num_bins)) {
      redistribute(state, num_bins <<= 1);
    }

    bin = find_bin(key_hash(name), num_bins);
    entry = try_as<MethodTableBucket>(values_->at(state, bin));

    while(entry) {
      if(entry->name() == name) {
        entry->method(state, method);
        entry->visibility(state, vis);
        return name;
      }
      last = entry;
      entry = try_as<MethodTableBucket>(entry->next());
    }

    if(last) {
      last->next(state, MethodTableBucket::create(state, name, method, vis));
    } else {
      values_->put(state, bin, MethodTableBucket::create(state, name, method, vis));
    }

    entries(state, Fixnum::from(num_entries + 1));
    return name;
  }
Esempio n. 22
0
  Object* LookupTable::remove(STATE, Object* key) {
    hashval bin;
    Object* val;
    Tuple* entry;
    Tuple* lst;

    key_to_sym(key);

    size_t num_entries = entries_->to_native();
    size_t num_bins = bins_->to_native();

    if(min_density_p(num_entries, num_bins) && (num_bins >> 1) >= LOOKUPTABLE_MIN_SIZE) {
      redistribute(state, num_bins >>= 1);
    }

    bin = find_bin(key_hash(key), num_bins);
    entry = try_as<Tuple>(values_->at(state, bin));

    lst = NULL;

    while(entry) {
      Object* link = entry->at(state, 2);

      if(entry->at(state, 0) == key) {
        val = entry->at(state, 1);
        if(lst) {
          lst->put(state, 2, link);
        } else {
          values_->put(state, bin, link);
        }
        entries(state, Fixnum::from(entries_->to_native() - 1));
        return val;
      }

      lst = entry;
      entry = try_as<Tuple>(link);
    }

    return Qnil;
  }
Esempio n. 23
0
  Tuple* LookupTable::find_entry(STATE, Object* key) {
    unsigned int bin;
    Tuple* entry;

    key_to_sym(key);
    bin = find_bin(key_hash(key), bins_->to_native());

    /* HACK: This should be fixed by not storing NULLs */
    Object* data = values_->at(state, bin);

    if (!data) return NULL;

    entry = try_as<Tuple>(data);

    while(entry) {
      if(entry->at(state, 0) == key) {
        return entry;
      }
      entry = try_as<Tuple>(entry->at(state, 2));
    }
    return NULL;
  }
Esempio n. 24
0
void hashmap_put(HashMap *hashmap, const char *key, void *value) {
  assert(hashmap != NULL);
  assert(key != NULL);

  int h = entry_key_hash(key);
  int hash = key_hash(h);
  int index = index_for_hash(hash, hashmap->capacity_);
  for (Entry *entry = hashmap->entries_[index]; entry; entry = entry->next_) {
    if (hash == entry->hash_ || strcmp(entry->key_, key) == 0) {
      void *old_data = entry->data_;
      if (value == old_data) {
        return;
      }
      if (hashmap->on_entry_removed_) {
        hashmap->on_entry_removed_(entry->key_, old_data);
      }
      entry->data_ = value;
      return;
    }
  }

  add_entry(hashmap, hash, key, value, index);
}
Esempio n. 25
0
void goap_state_set(struct goap_state * state, const char * key, int value) {
    unsigned int hash = key_hash(key);

again:
    for(int i = 0; i < state->mask + 1; i++) {
        unsigned int index = (hash + i) & state->mask;
        if(state->pairs[index].hash == hash && strcmp(state->pairs[index].key, key) == 0) {
            state->pairs[index].value = value;
            return;
        }
        if(state->pairs[index].hash == 0) {
            state->pairs[index].hash = hash;
            state->pairs[index].key = strdup(key);
            state->pairs[index].value = value;
            return;
        }
    }

    struct goap_state * new_state = _new_goap_state(((state->mask + 1) << 1) - 1);

    for(int j = 0; j < state->mask + 1; j++) {
        for(int i = 0; i < new_state->mask + 1; i++) {
            unsigned int index = (state->pairs[j].hash + j) & new_state->mask;
            if(new_state->pairs[index].hash == 0) {
                new_state->pairs[index].hash = state->pairs[j].hash;
                new_state->pairs[index].key = state->pairs[j].key;
                new_state->pairs[index].value = state->pairs[j].value;
            }
        }
    }

    free(state->pairs);
    *state = *new_state;
    free(new_state);

    goto again;
}
Esempio n. 26
0
  Executable* MethodTable::remove(STATE, Symbol* name) {
    check_frozen(state);

    utilities::thread::SpinLock::LockGuard lg(lock_);

    native_int num_entries = entries_->to_native();
    native_int num_bins = bins_->to_native();

    if(min_density_p(num_entries, num_bins) &&
         (num_bins >> 1) >= METHODTABLE_MIN_SIZE) {
      redistribute(state, num_bins >>= 1);
    }

    native_int bin = find_bin(key_hash(name), num_bins);
    MethodTableBucket* entry = try_as<MethodTableBucket>(values_->at(state, bin));
    MethodTableBucket* last = NULL;

    while(entry) {
      if(entry->name() == name) {
        Executable* val = entry->method();
        if(last) {
          last->next(state, entry->next());
        } else {
          values_->put(state, bin, entry->next());
        }

        entries(state, Fixnum::from(entries_->to_native() - 1));
        return val;
      }

      last = entry;
      entry = try_as<MethodTableBucket>(entry->next());
    }

    return nil<Executable>();
  }
Esempio n. 27
0
  Object* LookupTable::store(STATE, Object* key, Object* val) {
    unsigned int num_entries, num_bins, bin;
    Object* new_ent;
    Tuple* cur;
    Tuple* entry;

    key_to_sym(key);
    num_entries = entries_->to_native();
    num_bins = bins_->to_native();

    if(max_density_p(num_entries, num_bins)) {
      redistribute(state, num_bins <<= 1);
    }

    bin = find_bin(key_hash(key), num_bins);
    cur = entry = try_as<Tuple>(values_->at(state, bin));

    while(entry) {
      if(entry->at(state, 0) == key) {
        entry->put(state, 1, val);
        return val;
      }
      cur = entry;
      entry = try_as<Tuple>(entry->at(state, 2));
    }

    new_ent = entry_new(state, key, val);
    if(cur) {
      cur->put(state, 2, new_ent);
    } else {
      values_->put(state, bin, new_ent);
    }

    entries(state, Fixnum::from(num_entries + 1));
    return val;
  }
Esempio n. 28
0
void cmd_setinipw(const char *iniPW, SERVER_REC * server, WI_ITEM_REC * item)
{
	int pw_len, re_enc = 0;
	char B64digest[50] = { '\0' };
	char key[32] = { '\0' };
	char hash[32] = { '\0' };

	char new_iniKey[KEYBUF_SIZE], old_iniKey[KEYBUF_SIZE], iniPath_new[300];

	strcpy(old_iniKey, iniKey);

	if (iniPW != NULL) {
		pw_len = strlen(iniPW);
		if (pw_len < 1 || (size_t) pw_len > sizeof(new_iniKey)) {
			printtext(server,
				  item !=
				  NULL ? window_item_get_target(item) : NULL,
				  MSGLEVEL_CRAP,
				  "\002FiSH:\002 No parameters. Usage: /setinipw <sekure_blow.ini_password>");
			return;
		}

		if (strfcpy(new_iniKey, (char *)iniPW, sizeof(new_iniKey)) ==
		    NULL)
			return;
		ZeroMemory(iniPW, pw_len);
		pw_len = strlen(new_iniKey);

		if (pw_len < 8) {
			printtext(server,
				  item !=
				  NULL ? window_item_get_target(item) : NULL,
				  MSGLEVEL_CRAP,
				  "\002FiSH:\002 Password too short, at least 8 characters needed! Usage: /setinipw <sekure_blow.ini_password>");
			return;
		}

		key_from_password(new_iniKey, key);
		htob64(key, B64digest, 32);
		ZeroMemory(new_iniKey, sizeof(new_iniKey));
		strcpy(iniKey, B64digest);	// this is used for encrypting blow.ini
	} else {
		strcpy(iniKey, default_iniKey);	// use default blow.ini key
	}

	key_hash(key, hash);
	htob64(hash, B64digest, 32);	// this is used to verify the entered password
	ZeroMemory(hash, sizeof(hash));
	ZeroMemory(key, sizeof(key));

	// re-encrypt blow.ini with new password
	strcpy(iniPath_new, iniPath);
	strcat(iniPath_new, "_new");

	re_enc = recrypt_ini_file(iniPath, iniPath_new, old_iniKey);
	if (re_enc < 0) {
		ZeroMemory(B64digest, sizeof(B64digest));
		ZeroMemory(old_iniKey, sizeof(old_iniKey));

		printtext(server,
			  item != NULL ? window_item_get_target(item) : NULL,
			  MSGLEVEL_CRAP,
			  "\002FiSH ERROR:\002 Unable to write new blow.ini, probably out of disc space.");
		return;
	} else {
		ZeroMemory(old_iniKey, sizeof(old_iniKey));
	}

	if (setIniValue("FiSH", "ini_password_Hash", B64digest, iniPath) == -1) {
		ZeroMemory(B64digest, sizeof(B64digest));
		printtext(server,
			  item != NULL ? window_item_get_target(item) : NULL,
			  MSGLEVEL_CRAP,
			  "\002FiSH ERROR:\002 Unable to write to blow.ini, probably out of space or permission denied.");
		return;
	}

	ZeroMemory(B64digest, sizeof(B64digest));

	if (re_enc) {
		printtext(server,
			  item != NULL ? window_item_get_target(item) : NULL,
			  MSGLEVEL_CRAP,
			  "\002FiSH: Re-encrypted blow.ini\002 with new password.");
	}

	if (iniPW != NULL) {
		printtext(server,
			  item != NULL ? window_item_get_target(item) : NULL,
			  MSGLEVEL_CRAP,
			  "\002FiSH:\002 blow.ini password hash saved.");
	}
}
Esempio n. 29
0
int translate_event(struct input_event *ev)
{
	if (ev->type != EV_KEY)
		return 0;

	if (verbose) {
		fputs("Input: ", stdout);
		pp_event(ev);
	}

	uint16_t code = ev->code;

	switch (code) {
	case FN_KEY_CODE:
		if (ev->value == 2)
			return 0;
		fn_pressed = ev->value;
		struct input_event ev2;
		for (int i=0;interesting_list[i];i++) {
			uint16_t k = interesting_list[i];
			if (get_bit(interesting_on, k)) {
				if (verbose) {
					fputs("Interesting key: ", stdout);
					print_key_name(k);
					putchar('\n');
				}
				set_bit(interesting_off, k, 1);
				ev2 = *ev;
				ev2.code = k;
				if (!fn_pressed)
					ev2.code = fn_keys_map[key_hash(k)];
				ev2.value = 0;
				write_event(&ev2);
			}
		}
		memset(interesting_on, 0, sizeof(interesting_on));
		return 0;
	}

	int is_interesting = get_bit(interesting_keys, code);

	if (is_interesting) {
		if (verbose) {
			puts("This is interesting key");
		}
		if (!ev->value) {
			set_bit(interesting_off, code, 0);
			set_bit(interesting_on, code, 0);
		} else {
			if (get_bit(interesting_off, code)) {
				if (verbose) {
					fputs("Suppressing force-off key: ", stdout);
					print_key_name(code);
					putchar('\n');
				}
				return 0;
			}
			set_bit(interesting_on, code, 1);
		}
		if (fn_pressed)
			ev->code = code = fn_keys_map[key_hash(code)];
	}

	return 1;
}