Blame source/uds/hashUtils.c

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.c#2 $
Packit Service 310c69
 */
Packit Service 310c69
Packit Service 310c69
#include "hashUtils.h"
Packit Service 310c69
Packit Service 310c69
#include "errors.h"
Packit Service 310c69
#include "logger.h"
Packit Service 310c69
#include "permassert.h"
Packit Service 310c69
#include "stringUtils.h"
Packit Service 310c69
#include "uds.h"
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Convert a byte string to the hex representation.
Packit Service 310c69
 *
Packit Service 310c69
 * @param data          binary data to convert
Packit Service 310c69
 * @param dataLen       length of binary data
Packit Service 310c69
 * @param hex           target to write hex string into
Packit Service 310c69
 * @param hexLen        capacity of target string
Packit Service 310c69
 *
Packit Service 310c69
 * @return              UDS_SUCCESS,
Packit Service 310c69
 *                      or UDS_INVALID_ARGUMENT if hexLen
Packit Service 310c69
 *                      is too short.
Packit Service 310c69
 **/
Packit Service 310c69
static int dataToHex(const unsigned char *data, size_t dataLen,
Packit Service 310c69
                     char *hex, size_t hexLen)
Packit Service 310c69
{
Packit Service 310c69
  if (hexLen < 2 * dataLen + 1) {
Packit Service 310c69
    return logWarningWithStringError(UDS_INVALID_ARGUMENT,
Packit Service 310c69
                                     "hex data incorrect size");
Packit Service 310c69
  }
Packit Service 310c69
  size_t i;
Packit Service 310c69
  for (i = 0; i < dataLen; ++i) {
Packit Service 310c69
    int rc = fixedSprintf(__func__, &hex[2 * i], hexLen - (2 * i),
Packit Service 310c69
                          UDS_INVALID_ARGUMENT, "%02X", data[i]);
Packit Service 310c69
Packit Service 310c69
    if (rc != UDS_SUCCESS) {
Packit Service 310c69
      return rc;
Packit Service 310c69
    }
Packit Service 310c69
  }
Packit Service 310c69
  return UDS_SUCCESS;
Packit Service 310c69
}
Packit Service 310c69
Packit Service 310c69
/**********************************************************************/
Packit Service 310c69
int chunkNameToHex(const UdsChunkName *chunkName,
Packit Service 310c69
                   char *hexData, size_t hexDataLen)
Packit Service 310c69
{
Packit Service 310c69
  return dataToHex(chunkName->name, UDS_CHUNK_NAME_SIZE,
Packit Service 310c69
                   hexData, hexDataLen);
Packit Service 310c69
}
Packit Service 310c69
Packit Service 310c69
/**********************************************************************/
Packit Service 310c69
int chunkDataToHex(const UdsChunkData *chunkData,
Packit Service 310c69
                   char *hexData, size_t hexDataLen)
Packit Service 310c69
{
Packit Service 310c69
  return dataToHex(chunkData->data, UDS_MAX_BLOCK_DATA_SIZE,
Packit Service 310c69
                   hexData, hexDataLen);
Packit Service 310c69
}
Packit Service 310c69
Packit Service 310c69
/**********************************************************************/
Packit Service 310c69
unsigned int computeBits(unsigned int maxValue)
Packit Service 310c69
{
Packit Service 310c69
  // __builtin_clz() counts leading (high-order) zero bits, so if
Packit Service 310c69
  // we ever need this to be fast, under GCC we can do:
Packit Service 310c69
  // return ((maxValue == 0) ? 0 : (32 - __builtin_clz(maxValue)));
Packit Service 310c69
Packit Service 310c69
  unsigned int bits = 0;
Packit Service 310c69
  while (maxValue > 0) {
Packit Service 310c69
    maxValue >>= 1;
Packit Service 310c69
    bits++;
Packit Service 310c69
  }
Packit Service 310c69
  return bits;
Packit Service 310c69
}
Packit Service 310c69
Packit Service 310c69
/**********************************************************************/
Packit Service 310c69
void hashUtilsCompileTimeAssertions(void)
Packit Service 310c69
{
Packit Service 310c69
  STATIC_ASSERT((UDS_CHUNK_NAME_SIZE % sizeof(uint64_t)) == 0);
Packit Service 310c69
  STATIC_ASSERT(UDS_CHUNK_NAME_SIZE == 16);
Packit Service 310c69
}