amqp_table.c 18.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
/*
 * ***** BEGIN LICENSE BLOCK *****
 * Version: MIT
 *
 * Portions created by Alan Antonuk are Copyright (c) 2012-2013
 * Alan Antonuk. All Rights Reserved.
 *
 * Portions created by VMware are Copyright (c) 2007-2012 VMware, Inc.
 * All Rights Reserved.
 *
 * Portions created by Tony Garnock-Jones are Copyright (c) 2009-2010
 * VMware, Inc. and Tony Garnock-Jones. All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 * ***** END LICENSE BLOCK *****
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "amqp_private.h"
#include "amqp_table.h"
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define INITIAL_ARRAY_SIZE 16
#define INITIAL_TABLE_SIZE 16

static int amqp_decode_field_value(amqp_bytes_t encoded, amqp_pool_t *pool,
                                   amqp_field_value_t *entry, size_t *offset);

static int amqp_encode_field_value(amqp_bytes_t encoded,
                                   amqp_field_value_t *entry, size_t *offset);

/*---------------------------------------------------------------------------*/

static int amqp_decode_array(amqp_bytes_t encoded, amqp_pool_t *pool,
                             amqp_array_t *output, size_t *offset) {
  uint32_t arraysize;
  int num_entries = 0;
  int allocated_entries = INITIAL_ARRAY_SIZE;
  amqp_field_value_t *entries;
  size_t limit;
  int res;

  if (!amqp_decode_32(encoded, offset, &arraysize)) {
    return AMQP_STATUS_BAD_AMQP_DATA;
  }

  if (arraysize + *offset > encoded.len) {
    return AMQP_STATUS_BAD_AMQP_DATA;
  }

  entries = malloc(allocated_entries * sizeof(amqp_field_value_t));
  if (entries == NULL) {
    return AMQP_STATUS_NO_MEMORY;
  }

  limit = *offset + arraysize;
  while (*offset < limit) {
    if (num_entries >= allocated_entries) {
      void *newentries;
      allocated_entries = allocated_entries * 2;
      newentries =
          realloc(entries, allocated_entries * sizeof(amqp_field_value_t));
      res = AMQP_STATUS_NO_MEMORY;
      if (newentries == NULL) {
        goto out;
      }

      entries = newentries;
    }

    res = amqp_decode_field_value(encoded, pool, &entries[num_entries], offset);
    if (res < 0) {
      goto out;
    }

    num_entries++;
  }

  output->num_entries = num_entries;
  output->entries =
      amqp_pool_alloc(pool, num_entries * sizeof(amqp_field_value_t));
  /* NULL is legitimate if we requested a zero-length block. */
  if (output->entries == NULL) {
    if (num_entries == 0) {
      res = AMQP_STATUS_OK;
    } else {
      res = AMQP_STATUS_NO_MEMORY;
    }
    goto out;
  }

  memcpy(output->entries, entries, num_entries * sizeof(amqp_field_value_t));
  res = AMQP_STATUS_OK;

out:
  free(entries);
  return res;
}

int amqp_decode_table(amqp_bytes_t encoded, amqp_pool_t *pool,
                      amqp_table_t *output, size_t *offset) {
  uint32_t tablesize;
  int num_entries = 0;
  amqp_table_entry_t *entries;
  int allocated_entries = INITIAL_TABLE_SIZE;
  size_t limit;
  int res;

  if (!amqp_decode_32(encoded, offset, &tablesize)) {
    return AMQP_STATUS_BAD_AMQP_DATA;
  }

  if (tablesize + *offset > encoded.len) {
    return AMQP_STATUS_BAD_AMQP_DATA;
  }

  entries = malloc(allocated_entries * sizeof(amqp_table_entry_t));
  if (entries == NULL) {
    return AMQP_STATUS_NO_MEMORY;
  }

  limit = *offset + tablesize;
  while (*offset < limit) {
    uint8_t keylen;

    res = AMQP_STATUS_BAD_AMQP_DATA;
    if (!amqp_decode_8(encoded, offset, &keylen)) {
      goto out;
    }

    if (num_entries >= allocated_entries) {
      void *newentries;
      allocated_entries = allocated_entries * 2;
      newentries =
          realloc(entries, allocated_entries * sizeof(amqp_table_entry_t));
      res = AMQP_STATUS_NO_MEMORY;
      if (newentries == NULL) {
        goto out;
      }

      entries = newentries;
    }

    res = AMQP_STATUS_BAD_AMQP_DATA;
    if (!amqp_decode_bytes(encoded, offset, &entries[num_entries].key,
                           keylen)) {
      goto out;
    }

    res = amqp_decode_field_value(encoded, pool, &entries[num_entries].value,
                                  offset);
    if (res < 0) {
      goto out;
    }

    num_entries++;
  }

  output->num_entries = num_entries;
  output->entries =
      amqp_pool_alloc(pool, num_entries * sizeof(amqp_table_entry_t));
  /* NULL is legitimate if we requested a zero-length block. */
  if (output->entries == NULL) {
    if (num_entries == 0) {
      res = AMQP_STATUS_OK;
    } else {
      res = AMQP_STATUS_NO_MEMORY;
    }
    goto out;
  }

  memcpy(output->entries, entries, num_entries * sizeof(amqp_table_entry_t));
  res = AMQP_STATUS_OK;

out:
  free(entries);
  return res;
}

static int amqp_decode_field_value(amqp_bytes_t encoded, amqp_pool_t *pool,
                                   amqp_field_value_t *entry, size_t *offset) {
  int res = AMQP_STATUS_BAD_AMQP_DATA;

  if (!amqp_decode_8(encoded, offset, &entry->kind)) {
    goto out;
  }

#define TRIVIAL_FIELD_DECODER(bits)                                          \
  if (!amqp_decode_##bits(encoded, offset, &entry->value.u##bits)) goto out; \
  break
#define SIMPLE_FIELD_DECODER(bits, dest, how)                 \
  {                                                           \
    uint##bits##_t val;                                       \
    if (!amqp_decode_##bits(encoded, offset, &val)) goto out; \
    entry->value.dest = how;                                  \
  }                                                           \
  break

  switch (entry->kind) {
    case AMQP_FIELD_KIND_BOOLEAN:
      SIMPLE_FIELD_DECODER(8, boolean, val ? 1 : 0);

    case AMQP_FIELD_KIND_I8:
      SIMPLE_FIELD_DECODER(8, i8, (int8_t)val);
    case AMQP_FIELD_KIND_U8:
      TRIVIAL_FIELD_DECODER(8);

    case AMQP_FIELD_KIND_I16:
      SIMPLE_FIELD_DECODER(16, i16, (int16_t)val);
    case AMQP_FIELD_KIND_U16:
      TRIVIAL_FIELD_DECODER(16);

    case AMQP_FIELD_KIND_I32:
      SIMPLE_FIELD_DECODER(32, i32, (int32_t)val);
    case AMQP_FIELD_KIND_U32:
      TRIVIAL_FIELD_DECODER(32);

    case AMQP_FIELD_KIND_I64:
      SIMPLE_FIELD_DECODER(64, i64, (int64_t)val);
    case AMQP_FIELD_KIND_U64:
      TRIVIAL_FIELD_DECODER(64);

    case AMQP_FIELD_KIND_F32:
      TRIVIAL_FIELD_DECODER(32);
      /* and by punning, f32 magically gets the right value...! */

    case AMQP_FIELD_KIND_F64:
      TRIVIAL_FIELD_DECODER(64);
      /* and by punning, f64 magically gets the right value...! */

    case AMQP_FIELD_KIND_DECIMAL:
      if (!amqp_decode_8(encoded, offset, &entry->value.decimal.decimals) ||
          !amqp_decode_32(encoded, offset, &entry->value.decimal.value)) {
        goto out;
      }
      break;

    case AMQP_FIELD_KIND_UTF8:
    /* AMQP_FIELD_KIND_UTF8 and AMQP_FIELD_KIND_BYTES have the
       same implementation, but different interpretations. */
    /* fall through */
    case AMQP_FIELD_KIND_BYTES: {
      uint32_t len;
      if (!amqp_decode_32(encoded, offset, &len) ||
          !amqp_decode_bytes(encoded, offset, &entry->value.bytes, len)) {
        goto out;
      }
      break;
    }

    case AMQP_FIELD_KIND_ARRAY:
      res = amqp_decode_array(encoded, pool, &(entry->value.array), offset);
      goto out;

    case AMQP_FIELD_KIND_TIMESTAMP:
      TRIVIAL_FIELD_DECODER(64);

    case AMQP_FIELD_KIND_TABLE:
      res = amqp_decode_table(encoded, pool, &(entry->value.table), offset);
      goto out;

    case AMQP_FIELD_KIND_VOID:
      break;

    default:
      goto out;
  }

  res = AMQP_STATUS_OK;

out:
  return res;
}

/*---------------------------------------------------------------------------*/

static int amqp_encode_array(amqp_bytes_t encoded, amqp_array_t *input,
                             size_t *offset) {
  size_t start = *offset;
  int i, res;

  *offset += 4; /* size of the array gets filled in later on */

  for (i = 0; i < input->num_entries; i++) {
    res = amqp_encode_field_value(encoded, &input->entries[i], offset);
    if (res < 0) {
      goto out;
    }
  }

  if (!amqp_encode_32(encoded, &start, (uint32_t)(*offset - start - 4))) {
    res = AMQP_STATUS_TABLE_TOO_BIG;
    goto out;
  }

  res = AMQP_STATUS_OK;

out:
  return res;
}

int amqp_encode_table(amqp_bytes_t encoded, amqp_table_t *input,
                      size_t *offset) {
  size_t start = *offset;
  int i, res;

  *offset += 4; /* size of the table gets filled in later on */

  for (i = 0; i < input->num_entries; i++) {
    if (!amqp_encode_8(encoded, offset, (uint8_t)input->entries[i].key.len)) {
      res = AMQP_STATUS_TABLE_TOO_BIG;
      goto out;
    }

    if (!amqp_encode_bytes(encoded, offset, input->entries[i].key)) {
      res = AMQP_STATUS_TABLE_TOO_BIG;
      goto out;
    }

    res = amqp_encode_field_value(encoded, &input->entries[i].value, offset);
    if (res < 0) {
      goto out;
    }
  }

  if (!amqp_encode_32(encoded, &start, (uint32_t)(*offset - start - 4))) {
    res = AMQP_STATUS_TABLE_TOO_BIG;
    goto out;
  }

  res = AMQP_STATUS_OK;

out:
  return res;
}

static int amqp_encode_field_value(amqp_bytes_t encoded,
                                   amqp_field_value_t *entry, size_t *offset) {
  int res = AMQP_STATUS_BAD_AMQP_DATA;

  if (!amqp_encode_8(encoded, offset, entry->kind)) {
    goto out;
  }

#define FIELD_ENCODER(bits, val)                   \
  if (!amqp_encode_##bits(encoded, offset, val)) { \
    res = AMQP_STATUS_TABLE_TOO_BIG;               \
    goto out;                                      \
  }                                                \
  break

  switch (entry->kind) {
    case AMQP_FIELD_KIND_BOOLEAN:
      FIELD_ENCODER(8, entry->value.boolean ? 1 : 0);

    case AMQP_FIELD_KIND_I8:
      FIELD_ENCODER(8, entry->value.i8);
    case AMQP_FIELD_KIND_U8:
      FIELD_ENCODER(8, entry->value.u8);

    case AMQP_FIELD_KIND_I16:
      FIELD_ENCODER(16, entry->value.i16);
    case AMQP_FIELD_KIND_U16:
      FIELD_ENCODER(16, entry->value.u16);

    case AMQP_FIELD_KIND_I32:
      FIELD_ENCODER(32, entry->value.i32);
    case AMQP_FIELD_KIND_U32:
      FIELD_ENCODER(32, entry->value.u32);

    case AMQP_FIELD_KIND_I64:
      FIELD_ENCODER(64, entry->value.i64);
    case AMQP_FIELD_KIND_U64:
      FIELD_ENCODER(64, entry->value.u64);

    case AMQP_FIELD_KIND_F32:
      /* by punning, u32 magically gets the right value...! */
      FIELD_ENCODER(32, entry->value.u32);

    case AMQP_FIELD_KIND_F64:
      /* by punning, u64 magically gets the right value...! */
      FIELD_ENCODER(64, entry->value.u64);

    case AMQP_FIELD_KIND_DECIMAL:
      if (!amqp_encode_8(encoded, offset, entry->value.decimal.decimals) ||
          !amqp_encode_32(encoded, offset, entry->value.decimal.value)) {
        res = AMQP_STATUS_TABLE_TOO_BIG;
        goto out;
      }
      break;

    case AMQP_FIELD_KIND_UTF8:
    /* AMQP_FIELD_KIND_UTF8 and AMQP_FIELD_KIND_BYTES have the
       same implementation, but different interpretations. */
    /* fall through */
    case AMQP_FIELD_KIND_BYTES:
      if (!amqp_encode_32(encoded, offset, (uint32_t)entry->value.bytes.len) ||
          !amqp_encode_bytes(encoded, offset, entry->value.bytes)) {
        res = AMQP_STATUS_TABLE_TOO_BIG;
        goto out;
      }
      break;

    case AMQP_FIELD_KIND_ARRAY:
      res = amqp_encode_array(encoded, &entry->value.array, offset);
      goto out;

    case AMQP_FIELD_KIND_TIMESTAMP:
      FIELD_ENCODER(64, entry->value.u64);

    case AMQP_FIELD_KIND_TABLE:
      res = amqp_encode_table(encoded, &entry->value.table, offset);
      goto out;

    case AMQP_FIELD_KIND_VOID:
      break;

    default:
      res = AMQP_STATUS_INVALID_PARAMETER;
      goto out;
  }

  res = AMQP_STATUS_OK;

out:
  return res;
}

/*---------------------------------------------------------------------------*/

int amqp_table_entry_cmp(void const *entry1, void const *entry2) {
  amqp_table_entry_t const *p1 = (amqp_table_entry_t const *)entry1;
  amqp_table_entry_t const *p2 = (amqp_table_entry_t const *)entry2;

  int d;
  size_t minlen;

  minlen = p1->key.len;
  if (p2->key.len < minlen) {
    minlen = p2->key.len;
  }

  d = memcmp(p1->key.bytes, p2->key.bytes, minlen);
  if (d != 0) {
    return d;
  }

  return (int)p1->key.len - (int)p2->key.len;
}

static int amqp_field_value_clone(const amqp_field_value_t *original,
                                  amqp_field_value_t *clone,
                                  amqp_pool_t *pool) {
  int i;
  int res;
  clone->kind = original->kind;

  switch (clone->kind) {
    case AMQP_FIELD_KIND_BOOLEAN:
      clone->value.boolean = original->value.boolean;
      break;

    case AMQP_FIELD_KIND_I8:
      clone->value.i8 = original->value.i8;
      break;

    case AMQP_FIELD_KIND_U8:
      clone->value.u8 = original->value.u8;
      break;

    case AMQP_FIELD_KIND_I16:
      clone->value.i16 = original->value.i16;
      break;

    case AMQP_FIELD_KIND_U16:
      clone->value.u16 = original->value.u16;
      break;

    case AMQP_FIELD_KIND_I32:
      clone->value.i32 = original->value.i32;
      break;

    case AMQP_FIELD_KIND_U32:
      clone->value.u32 = original->value.u32;
      break;

    case AMQP_FIELD_KIND_I64:
      clone->value.i64 = original->value.i64;
      break;

    case AMQP_FIELD_KIND_U64:
    case AMQP_FIELD_KIND_TIMESTAMP:
      clone->value.u64 = original->value.u64;
      break;

    case AMQP_FIELD_KIND_F32:
      clone->value.f32 = original->value.f32;
      break;

    case AMQP_FIELD_KIND_F64:
      clone->value.f64 = original->value.f64;
      break;

    case AMQP_FIELD_KIND_DECIMAL:
      clone->value.decimal = original->value.decimal;
      break;

    case AMQP_FIELD_KIND_UTF8:
    case AMQP_FIELD_KIND_BYTES:
      if (0 == original->value.bytes.len) {
        clone->value.bytes = amqp_empty_bytes;
      } else {
        amqp_pool_alloc_bytes(pool, original->value.bytes.len,
                              &clone->value.bytes);
        if (NULL == clone->value.bytes.bytes) {
          return AMQP_STATUS_NO_MEMORY;
        }
        memcpy(clone->value.bytes.bytes, original->value.bytes.bytes,
               clone->value.bytes.len);
      }
      break;

    case AMQP_FIELD_KIND_ARRAY:
      if (0 == original->value.array.entries) {
        clone->value.array = amqp_empty_array;
      } else {
        clone->value.array.num_entries = original->value.array.num_entries;
        clone->value.array.entries = amqp_pool_alloc(
            pool, clone->value.array.num_entries * sizeof(amqp_field_value_t));
        if (NULL == clone->value.array.entries) {
          return AMQP_STATUS_NO_MEMORY;
        }

        for (i = 0; i < clone->value.array.num_entries; ++i) {
          res = amqp_field_value_clone(&original->value.array.entries[i],
                                       &clone->value.array.entries[i], pool);
          if (AMQP_STATUS_OK != res) {
            return res;
          }
        }
      }
      break;

    case AMQP_FIELD_KIND_TABLE:
      return amqp_table_clone(&original->value.table, &clone->value.table,
                              pool);

    case AMQP_FIELD_KIND_VOID:
      break;

    default:
      return AMQP_STATUS_INVALID_PARAMETER;
  }

  return AMQP_STATUS_OK;
}

static int amqp_table_entry_clone(const amqp_table_entry_t *original,
                                  amqp_table_entry_t *clone,
                                  amqp_pool_t *pool) {
  if (0 == original->key.len) {
    return AMQP_STATUS_INVALID_PARAMETER;
  }

  amqp_pool_alloc_bytes(pool, original->key.len, &clone->key);
  if (NULL == clone->key.bytes) {
    return AMQP_STATUS_NO_MEMORY;
  }

  memcpy(clone->key.bytes, original->key.bytes, clone->key.len);

  return amqp_field_value_clone(&original->value, &clone->value, pool);
}

int amqp_table_clone(const amqp_table_t *original, amqp_table_t *clone,
                     amqp_pool_t *pool) {
  int i;
  int res;
  clone->num_entries = original->num_entries;
  if (0 == clone->num_entries) {
    *clone = amqp_empty_table;
    return AMQP_STATUS_OK;
  }

  clone->entries =
      amqp_pool_alloc(pool, clone->num_entries * sizeof(amqp_table_entry_t));

  if (NULL == clone->entries) {
    return AMQP_STATUS_NO_MEMORY;
  }

  for (i = 0; i < clone->num_entries; ++i) {
    res =
        amqp_table_entry_clone(&original->entries[i], &clone->entries[i], pool);
    if (AMQP_STATUS_OK != res) {
      goto error_out1;
    }
  }

  return AMQP_STATUS_OK;

error_out1:
  return res;
}

amqp_table_entry_t amqp_table_construct_utf8_entry(const char *key,
                                                   const char *value) {
  amqp_table_entry_t ret;
  ret.key = amqp_cstring_bytes(key);
  ret.value.kind = AMQP_FIELD_KIND_UTF8;
  ret.value.value.bytes = amqp_cstring_bytes(value);
  return ret;
}

amqp_table_entry_t amqp_table_construct_table_entry(const char *key,
                                                    const amqp_table_t *value) {
  amqp_table_entry_t ret;
  ret.key = amqp_cstring_bytes(key);
  ret.value.kind = AMQP_FIELD_KIND_TABLE;
  ret.value.value.table = *value;
  return ret;
}

amqp_table_entry_t amqp_table_construct_bool_entry(const char *key,
                                                   const int value) {
  amqp_table_entry_t ret;
  ret.key = amqp_cstring_bytes(key);
  ret.value.kind = AMQP_FIELD_KIND_BOOLEAN;
  ret.value.value.boolean = value;
  return ret;
}

amqp_table_entry_t *amqp_table_get_entry_by_key(const amqp_table_t *table,
                                                const amqp_bytes_t key) {
  int i;
  assert(table != NULL);
  for (i = 0; i < table->num_entries; ++i) {
    if (amqp_bytes_equal(table->entries[i].key, key)) {
      return &table->entries[i];
    }
  }
  return NULL;
}