Blame source/uds/hashUtils.h

Packit Service 310c69
/*
Packit Service 310c69
 * Copyright (c) 2020 Red Hat, Inc.
Packit Service 310c69
 *
Packit Service 310c69
 * This program is free software; you can redistribute it and/or
Packit Service 310c69
 * modify it under the terms of the GNU General Public License
Packit Service 310c69
 * as published by the Free Software Foundation; either version 2
Packit Service 310c69
 * of the License, or (at your option) any later version.
Packit Service 310c69
 * 
Packit Service 310c69
 * This program is distributed in the hope that it will be useful,
Packit Service 310c69
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 310c69
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service 310c69
 * GNU General Public License for more details.
Packit Service 310c69
 * 
Packit Service 310c69
 * You should have received a copy of the GNU General Public License
Packit Service 310c69
 * along with this program; if not, write to the Free Software
Packit Service 310c69
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
Packit Service 310c69
 * 02110-1301, USA. 
Packit Service 310c69
 *
Packit Service 310c69
 * $Id: //eng/uds-releases/jasper/src/uds/hashUtils.h#1 $
Packit Service 310c69
 */
Packit Service 310c69
Packit Service 310c69
#ifndef HASH_UTILS_H
Packit Service 310c69
#define HASH_UTILS_H 1
Packit Service 310c69
Packit Service 310c69
#include "compiler.h"
Packit Service 310c69
#include "common.h"
Packit Service 310c69
#include "geometry.h"
Packit Service 310c69
#include "numeric.h"
Packit Service 310c69
#include "uds.h"
Packit Service 310c69
Packit Service 310c69
// How various portions of a hash are apportioned.  Size dependent.
Packit Service 310c69
enum {
Packit Service 310c69
  MASTER_INDEX_BYTES_OFFSET  = 0,  // size 8
Packit Service 310c69
  CHAPTER_INDEX_BYTES_OFFSET = 8,  // size 6
Packit Service 310c69
  SAMPLE_BYTES_OFFSET        = 14, // size 2
Packit Service 310c69
  MASTER_INDEX_BYTES_COUNT   = 8,
Packit Service 310c69
  CHAPTER_INDEX_BYTES_COUNT  = 6,
Packit Service 310c69
  SAMPLE_BYTES_COUNT         = 2,
Packit Service 310c69
};
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Extract the portion of a block name used by the chapter index.
Packit Service 310c69
 *
Packit Service 310c69
 * @param name The block name
Packit Service 310c69
 *
Packit Service 310c69
 * @return The chapter index bytes
Packit Service 310c69
 **/
Packit Service 310c69
static INLINE uint64_t extractChapterIndexBytes(const UdsChunkName *name)
Packit Service 310c69
{
Packit Service 310c69
  // Get the high order 16 bits, then the low order 32 bits
Packit Service 310c69
  uint64_t bytes
Packit Service 310c69
    = (uint64_t) getUInt16BE(&name->name[CHAPTER_INDEX_BYTES_OFFSET]) << 32;
Packit Service 310c69
  bytes |= getUInt32BE(&name->name[CHAPTER_INDEX_BYTES_OFFSET + 2]);
Packit Service 310c69
  return bytes;
Packit Service 310c69
}
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Extract the portion of a block name used by the master index.
Packit Service 310c69
 *
Packit Service 310c69
 * @param name The block name
Packit Service 310c69
 *
Packit Service 310c69
 * @return The master index portion of the block name
Packit Service 310c69
 **/
Packit Service 310c69
static INLINE uint64_t extractMasterIndexBytes(const UdsChunkName *name)
Packit Service 310c69
{
Packit Service 310c69
  return getUInt64BE(&name->name[MASTER_INDEX_BYTES_OFFSET]);
Packit Service 310c69
}
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Extract the portion of a block name used for sparse sampling.
Packit Service 310c69
 *
Packit Service 310c69
 * @param name The block name
Packit Service 310c69
 *
Packit Service 310c69
 * @return The sparse sample portion of the block name
Packit Service 310c69
 **/
Packit Service 310c69
static INLINE uint32_t extractSamplingBytes(const UdsChunkName *name)
Packit Service 310c69
{
Packit Service 310c69
  return getUInt16BE(&name->name[SAMPLE_BYTES_OFFSET]);
Packit Service 310c69
}
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * For a given block, find the chapter delta list to use
Packit Service 310c69
 *
Packit Service 310c69
 * @param name     The block name to hash
Packit Service 310c69
 * @param geometry The geometry to use
Packit Service 310c69
 *
Packit Service 310c69
 * @return The chapter delta list where we expect to find the given blockname
Packit Service 310c69
 **/
Packit Service 310c69
static INLINE unsigned int hashToChapterDeltaList(const UdsChunkName *name,
Packit Service 310c69
                                                  const Geometry     *geometry)
Packit Service 310c69
{
Packit Service 310c69
  return (unsigned int) ((extractChapterIndexBytes(name)
Packit Service 310c69
                          >> geometry->chapterAddressBits)
Packit Service 310c69
                         & ((1 << geometry->chapterDeltaListBits) - 1));
Packit Service 310c69
}
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * For a given block, find the chapter delta address to use
Packit Service 310c69
 *
Packit Service 310c69
 * @param name     The block name to hash
Packit Service 310c69
 * @param geometry The geometry to use
Packit Service 310c69
 *
Packit Service 310c69
 * @return The chapter delta address to use
Packit Service 310c69
 **/
Packit Service 310c69
static INLINE unsigned int hashToChapterDeltaAddress(const UdsChunkName *name,
Packit Service 310c69
                                                     const Geometry *geometry)
Packit Service 310c69
{
Packit Service 310c69
  return (unsigned int) (extractChapterIndexBytes(name)
Packit Service 310c69
                         & ((1 << geometry->chapterAddressBits) - 1));
Packit Service 310c69
}
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * For a given block name, find the slot in the open chapter hash table
Packit Service 310c69
 * where it is expected to reside.
Packit Service 310c69
 *
Packit Service 310c69
 * @param name      The block name to hash
Packit Service 310c69
 * @param slotCount The size of the hash table
Packit Service 310c69
 *
Packit Service 310c69
 * @return the record number in the index page where we expect to find
Packit Service 310c69
 #         the given blockname
Packit Service 310c69
 **/
Packit Service 310c69
static INLINE unsigned int nameToHashSlot(const UdsChunkName *name,
Packit Service 310c69
                                          unsigned int slotCount)
Packit Service 310c69
{
Packit Service 310c69
  return (unsigned int) (extractChapterIndexBytes(name) % slotCount);
Packit Service 310c69
}
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Convert a chunk name to hex to make it more readable.
Packit Service 310c69
 *
Packit Service 310c69
 * @param chunkName  The chunk name
Packit Service 310c69
 * @param hexData    The resulting hexdata from the given chunk name
Packit Service 310c69
 * @param hexDataLen The capacity of hexData
Packit Service 310c69
 *
Packit Service 310c69
 * @return              UDS_SUCCESS,
Packit Service 310c69
 *                      or UDS_INVALID_ARGUMENT if hexDataLen
Packit Service 310c69
 *                      is too short.
Packit Service 310c69
 **/
Packit Service 310c69
int chunkNameToHex(const UdsChunkName *chunkName,
Packit Service 310c69
                   char               *hexData,
Packit Service 310c69
                   size_t              hexDataLen)
Packit Service 310c69
  __attribute__((warn_unused_result));
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Convert chunk data to hex to make it more readable.
Packit Service 310c69
 *
Packit Service 310c69
 * @param chunkData  The chunk data
Packit Service 310c69
 * @param hexData    The resulting hexdata from the given chunk data
Packit Service 310c69
 * @param hexDataLen The capacity of hexData
Packit Service 310c69
 *
Packit Service 310c69
 * @return              UDS_SUCCESS,
Packit Service 310c69
 *                      or UDS_INVALID_ARGUMENT if hexDataLen
Packit Service 310c69
 *                      is too short.
Packit Service 310c69
 **/
Packit Service 310c69
int chunkDataToHex(const UdsChunkData *chunkData,
Packit Service 310c69
                   char               *hexData,
Packit Service 310c69
                   size_t              hexDataLen)
Packit Service 310c69
  __attribute__((warn_unused_result));
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Compute the number of bits required to store a field with the given
Packit Service 310c69
 * maximum value.
Packit Service 310c69
 *
Packit Service 310c69
 * @param maxValue   The maximum value of the field
Packit Service 310c69
 *
Packit Service 310c69
 * @return           the number of bits required
Packit Service 310c69
 **/
Packit Service 310c69
unsigned int computeBits(unsigned int maxValue)
Packit Service 310c69
  __attribute__((warn_unused_result));
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * FOR TESTING. Set the portion of a block name used by the chapter index.
Packit Service 310c69
 *
Packit Service 310c69
 * @param name   The block name
Packit Service 310c69
 * @param value  The value to store
Packit Service 310c69
 **/
Packit Service 310c69
static INLINE void setChapterIndexBytes(UdsChunkName *name, uint64_t value)
Packit Service 310c69
{
Packit Service 310c69
  // Store the high order bytes, then the low-order bytes
Packit Service 310c69
  storeUInt16BE(&name->name[CHAPTER_INDEX_BYTES_OFFSET],
Packit Service 310c69
                (uint16_t)(value >> 32));
Packit Service 310c69
  storeUInt32BE(&name->name[CHAPTER_INDEX_BYTES_OFFSET + 2],
Packit Service 310c69
                (uint32_t)value);
Packit Service 310c69
}
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * FOR TESTING. Set the bits used to find a chapter delta list
Packit Service 310c69
 *
Packit Service 310c69
 * @param name     The block name
Packit Service 310c69
 * @param geometry The geometry to use
Packit Service 310c69
 * @param value    The value to store
Packit Service 310c69
 **/
Packit Service 310c69
static INLINE void setChapterDeltaListBits(UdsChunkName   *name,
Packit Service 310c69
                                           const Geometry *geometry,
Packit Service 310c69
                                           uint64_t        value)
Packit Service 310c69
{
Packit Service 310c69
  uint64_t deltaAddress = hashToChapterDeltaAddress(name, geometry);
Packit Service 310c69
  deltaAddress |= value << geometry->chapterAddressBits;
Packit Service 310c69
  setChapterIndexBytes(name, deltaAddress);
Packit Service 310c69
}
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * FOR TESTING. Set the portion of a block name used by the master index.
Packit Service 310c69
 *
Packit Service 310c69
 * @param name  The block name
Packit Service 310c69
 * @param val   The value to store
Packit Service 310c69
 **/
Packit Service 310c69
static INLINE void setMasterIndexBytes(UdsChunkName *name, uint64_t val)
Packit Service 310c69
{
Packit Service 310c69
  storeUInt64BE(&name->name[MASTER_INDEX_BYTES_OFFSET], val);
Packit Service 310c69
}
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Set the portion of a block name used for sparse sampling.
Packit Service 310c69
 *
Packit Service 310c69
 * @param name   The block name
Packit Service 310c69
 * @param value  The value to store
Packit Service 310c69
 **/
Packit Service 310c69
static INLINE void setSamplingBytes(UdsChunkName *name, uint32_t value)
Packit Service 310c69
{
Packit Service 310c69
  storeUInt16BE(&name->name[SAMPLE_BYTES_OFFSET], (uint16_t)value);
Packit Service 310c69
}
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Special function wrapper required for compile-time assertions. This
Packit Service 310c69
 * function will fail to compile if UDS_CHUNK_NAME_SIZE is not an integer
Packit Service 310c69
 * multiple of 8.
Packit Service 310c69
 **/
Packit Service 310c69
void hashUtilsCompileTimeAssertions(void);
Packit Service 310c69
Packit Service 310c69
#endif /* HASH_UTILS_H */