summaryrefslogtreecommitdiff
path: root/firmware/lib/utility_string.c
diff options
context:
space:
mode:
authorRandall Spangler <rspangler@chromium.org>2011-07-07 10:30:25 -0700
committerRandall Spangler <rspangler@chromium.org>2011-07-08 09:17:07 -0700
commitbebe53c9193dbe875d793c7cb124ed66e610e1bd (patch)
tree8933d26b404b4a477d99ef97d2507431e8c1210b /firmware/lib/utility_string.c
parent93cf15e9a1c29962d6f08be6102d2ea7876d969f (diff)
downloadvboot-bebe53c9193dbe875d793c7cb124ed66e610e1bd.tar.gz
Add string utility functions.
These are used by the coming-soon vboot wrapper (vboot_api_kernel) to display debug information when Tab is pressed at a BIOS screen. BUG=chromium-os:17035 TEST=make && make runtests (runs new test!) Change-Id: I4893f31e9333f4e9d458a6e347213eef22f770cd Reviewed-on: http://gerrit.chromium.org/gerrit/3759 Reviewed-by: Bill Richardson <wfrichar@chromium.org> Tested-by: Randall Spangler <rspangler@chromium.org>
Diffstat (limited to 'firmware/lib/utility_string.c')
-rw-r--r--firmware/lib/utility_string.c71
1 files changed, 71 insertions, 0 deletions
diff --git a/firmware/lib/utility_string.c b/firmware/lib/utility_string.c
new file mode 100644
index 00000000..424c3e33
--- /dev/null
+++ b/firmware/lib/utility_string.c
@@ -0,0 +1,71 @@
+/* Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ *
+ * String utility functions that need to be built as part of the firmware.
+ */
+
+#include "sysincludes.h"
+#include "utility.h"
+
+
+uint32_t Uint64ToString(char *buf, uint32_t bufsize, uint64_t value,
+ uint32_t radix, uint32_t zero_pad_width) {
+ char ibuf[UINT64_TO_STRING_MAX];
+ char *s;
+ uint32_t usedsize = 1;
+
+ if (!buf)
+ return 0;
+
+ /* Clear output buffer in case of error */
+ *buf = '\0';
+
+ /* Sanity-check input args */
+ if (radix < 2 || radix > 36 || zero_pad_width >= UINT64_TO_STRING_MAX)
+ return 0;
+
+ /* Start at end of string and work backwards */
+ s = ibuf + UINT64_TO_STRING_MAX - 1;
+ *(s) = '\0';
+ do {
+ int v = value % radix;
+ value /= radix;
+
+ *(--s) = (char)(v < 10 ? v + '0' : v + 'a' - 10);
+ if (++usedsize > bufsize)
+ return 0; /* Result won't fit in buffer */
+ } while (value);
+
+ /* Zero-pad if necessary */
+ while (usedsize <= zero_pad_width) {
+ *(--s) = '0';
+ if (++usedsize > bufsize)
+ return 0; /* Result won't fit in buffer */
+ }
+
+ /* Now copy the string back to the input buffer. */
+ Memcpy(buf, s, usedsize);
+
+ /* Don't count the terminating null in the bytes used */
+ return usedsize - 1;
+}
+
+
+uint32_t Strncat(char *dest, const char *src, uint32_t destlen) {
+ uint32_t used = 0;
+
+ if (!dest || !src)
+ return 0;
+
+ /* Skip past existing string in destination.*/
+ while (dest[used] && used < destlen - 1)
+ used++;
+ /* Now copy source */
+ while (*src && used < destlen - 1)
+ dest[used++] = *src++;
+
+ /* Terminate destination and return count of non-null characters */
+ dest[used] = 0;
+ return used;
+}