utility.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /* Copyright (c) 2013 The Chromium OS Authors. All rights reserved.
  2. * Use of this source code is governed by a BSD-style license that can be
  3. * found in the LICENSE file.
  4. */
  5. /*
  6. * Helper functions/wrappers for memory allocations, manipulation and
  7. * comparison.
  8. */
  9. #ifndef VBOOT_REFERENCE_UTILITY_H_
  10. #define VBOOT_REFERENCE_UTILITY_H_
  11. #include "sysincludes.h"
  12. #include "vboot_api.h"
  13. #ifdef VBOOT_DEBUG
  14. #define VbAssert(expr) do { if (!(expr)) { \
  15. VbExError("assert fail: %s at %s:%d\n", \
  16. #expr, __FILE__, __LINE__); }} while(0)
  17. #else
  18. #define VbAssert(expr)
  19. #endif
  20. /* Optional, up to the BIOS */
  21. #ifdef VBOOT_EASTER_EGG
  22. #define VBEASTEREGG VbExEasterEgg()
  23. #else
  24. #define VBEASTEREGG 0
  25. #endif
  26. /*
  27. * Combine [msw] and [lsw] uint16s to a uint32_t with its [msw] and [lsw]
  28. * forming the most and least signficant 16-bit words.
  29. */
  30. #define CombineUint16Pair(msw,lsw) (((uint32_t)(msw) << 16) | \
  31. (((lsw)) & 0xFFFF))
  32. /* Return the minimum of (a) or (b). */
  33. #define Min(a, b) (((a) < (b)) ? (a) : (b))
  34. /*
  35. * Buffer size required to hold the longest possible output of Uint64ToString()
  36. * - that is, Uint64ToString(~0, 2).
  37. */
  38. #define UINT64_TO_STRING_MAX 65
  39. /**
  40. * Convert a value to a string in the specified radix (2=binary, 10=decimal,
  41. * 16=hex) and store it in <buf>, which is <bufsize> chars long. If
  42. * <zero_pad_width>, left-pads the string to at least that width with '0'.
  43. * Returns the length of the stored string, not counting the terminating null.
  44. */
  45. uint32_t Uint64ToString(char *buf, uint32_t bufsize, uint64_t value,
  46. uint32_t radix, uint32_t zero_pad_width);
  47. /**
  48. * Concatenate <src> onto <dest>, which has space for <destlen> characters
  49. * including the terminating null. Note that <dest> will always be
  50. * null-terminated if <destlen> > 0. Returns the number of characters used in
  51. * <dest>, not counting the terminating null.
  52. */
  53. uint32_t StrnAppend(char *dest, const char *src, uint32_t destlen);
  54. #endif /* VBOOT_REFERENCE_UTILITY_H_ */