Blame source/uds/hashUtils.c

Packit b55c50
/*
Packit b55c50
 * Copyright (c) 2020 Red Hat, Inc.
Packit b55c50
 *
Packit b55c50
 * This program is free software; you can redistribute it and/or
Packit b55c50
 * modify it under the terms of the GNU General Public License
Packit b55c50
 * as published by the Free Software Foundation; either version 2
Packit b55c50
 * of the License, or (at your option) any later version.
Packit b55c50
 * 
Packit b55c50
 * This program is distributed in the hope that it will be useful,
Packit b55c50
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit b55c50
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit b55c50
 * GNU General Public License for more details.
Packit b55c50
 * 
Packit b55c50
 * You should have received a copy of the GNU General Public License
Packit b55c50
 * along with this program; if not, write to the Free Software
Packit b55c50
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
Packit b55c50
 * 02110-1301, USA. 
Packit b55c50
 *
Packit b55c50
 * $Id: //eng/uds-releases/jasper/src/uds/hashUtils.c#2 $
Packit b55c50
 */
Packit b55c50
Packit b55c50
#include "hashUtils.h"
Packit b55c50
Packit b55c50
#include "errors.h"
Packit b55c50
#include "logger.h"
Packit b55c50
#include "permassert.h"
Packit b55c50
#include "stringUtils.h"
Packit b55c50
#include "uds.h"
Packit b55c50
Packit b55c50
/**
Packit b55c50
 * Convert a byte string to the hex representation.
Packit b55c50
 *
Packit b55c50
 * @param data          binary data to convert
Packit b55c50
 * @param dataLen       length of binary data
Packit b55c50
 * @param hex           target to write hex string into
Packit b55c50
 * @param hexLen        capacity of target string
Packit b55c50
 *
Packit b55c50
 * @return              UDS_SUCCESS,
Packit b55c50
 *                      or UDS_INVALID_ARGUMENT if hexLen
Packit b55c50
 *                      is too short.
Packit b55c50
 **/
Packit b55c50
static int dataToHex(const unsigned char *data, size_t dataLen,
Packit b55c50
                     char *hex, size_t hexLen)
Packit b55c50
{
Packit b55c50
  if (hexLen < 2 * dataLen + 1) {
Packit b55c50
    return logWarningWithStringError(UDS_INVALID_ARGUMENT,
Packit b55c50
                                     "hex data incorrect size");
Packit b55c50
  }
Packit b55c50
  size_t i;
Packit b55c50
  for (i = 0; i < dataLen; ++i) {
Packit b55c50
    int rc = fixedSprintf(__func__, &hex[2 * i], hexLen - (2 * i),
Packit b55c50
                          UDS_INVALID_ARGUMENT, "%02X", data[i]);
Packit b55c50
Packit b55c50
    if (rc != UDS_SUCCESS) {
Packit b55c50
      return rc;
Packit b55c50
    }
Packit b55c50
  }
Packit b55c50
  return UDS_SUCCESS;
Packit b55c50
}
Packit b55c50
Packit b55c50
/**********************************************************************/
Packit b55c50
int chunkNameToHex(const UdsChunkName *chunkName,
Packit b55c50
                   char *hexData, size_t hexDataLen)
Packit b55c50
{
Packit b55c50
  return dataToHex(chunkName->name, UDS_CHUNK_NAME_SIZE,
Packit b55c50
                   hexData, hexDataLen);
Packit b55c50
}
Packit b55c50
Packit b55c50
/**********************************************************************/
Packit b55c50
int chunkDataToHex(const UdsChunkData *chunkData,
Packit b55c50
                   char *hexData, size_t hexDataLen)
Packit b55c50
{
Packit b55c50
  return dataToHex(chunkData->data, UDS_MAX_BLOCK_DATA_SIZE,
Packit b55c50
                   hexData, hexDataLen);
Packit b55c50
}
Packit b55c50
Packit b55c50
/**********************************************************************/
Packit b55c50
unsigned int computeBits(unsigned int maxValue)
Packit b55c50
{
Packit b55c50
  // __builtin_clz() counts leading (high-order) zero bits, so if
Packit b55c50
  // we ever need this to be fast, under GCC we can do:
Packit b55c50
  // return ((maxValue == 0) ? 0 : (32 - __builtin_clz(maxValue)));
Packit b55c50
Packit b55c50
  unsigned int bits = 0;
Packit b55c50
  while (maxValue > 0) {
Packit b55c50
    maxValue >>= 1;
Packit b55c50
    bits++;
Packit b55c50
  }
Packit b55c50
  return bits;
Packit b55c50
}
Packit b55c50
Packit b55c50
/**********************************************************************/
Packit b55c50
void hashUtilsCompileTimeAssertions(void)
Packit b55c50
{
Packit b55c50
  STATIC_ASSERT((UDS_CHUNK_NAME_SIZE % sizeof(uint64_t)) == 0);
Packit b55c50
  STATIC_ASSERT(UDS_CHUNK_NAME_SIZE == 16);
Packit b55c50
}