1 // Copyright (c) 2009-2010 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.
5 #ifndef CHROMEOS_UTILITY_H_
6 #define CHROMEOS_UTILITY_H_
13 #include <base/values.h>
15 // Be VERY CAREFUL when touching this. It is include-order-sensitive.
16 // This library (libchromeos) is built with -fno-exceptions, but dbus-c++ uses
17 // exceptions heavily. In dbus-c++/types.h, there are overloaded operators for
18 // accessing DBus::MessageIters, which causes compilation to fail when they're
19 // included into libchromeos. Therefore, define away the throws in the header
24 // The comma keeps the resulting expression one statement (which makes unbraced
25 // if statements work properly).
27 // The extra twist is that dbus-c++/error.h has some incompatible uses of throw
28 // (like the throw annotation on method declarations), so we explicitly include
29 // dbus-c++/error.h first so its include guards will have been set, then include
31 #include <dbus-c++/error.h>
32 #define throw abort(),
33 #include <dbus-c++/types.h>
36 // For use in a switch statement to return the string from a label. Like:
37 // const char* CommandToName(CommandType command) {
39 // CHROMEOS_CASE_RETURN_LABEL(CMD_DELETE);
40 // CHROMEOS_CASE_RETURN_LABEL(CMD_OPEN);
42 // return "Unknown commmand";
44 #define CHROMEOS_CASE_RETURN_LABEL(label) \
45 case label: return #label
49 typedef std::vector<unsigned char> Blob;
51 // Returns a string that represents the hexadecimal encoded contents of blob.
52 // String will contain only the characters 0-9 and a-f.
55 // blob - The bytes to encode.
56 // string - ASCII representation of the blob in hex.
58 std::string AsciiEncode(const Blob &blob);
60 // Converts a string representing a sequence of bytes in hex into the actual
64 // str - The bytes to decode.
66 // The decoded bytes as a Blob.
68 Blob AsciiDecode(const std::string &str);
70 // Secure memset - volatile qualifier prevents a call to memset from being
73 // Based on memset_s in:
74 // https://buildsecurityin.us-cert.gov/daisy/bsi-rules/home/g1/771-BSI.html
75 void* SecureMemset(void *v, int c, size_t n);
77 // Compare [n] bytes starting at [s1] with [s2] and return 0 if they match,
78 // 1 if they don't. Time taken to perform the comparison is only dependent on
79 // [n] and not on the relationship of the match between [s1] and [s2].
80 int SafeMemcmp(const void* s1, const void* s2, size_t n);
82 // Convert a DBus message into a Value.
85 // message - Message to convert.
86 // value - Result pointer.
88 // True if conversion succeeded, false if it didn't.
89 bool DBusMessageToValue(DBus::Message& message, Value** v);
91 // Convert a DBus message iterator a Value.
94 // message - Message to convert.
95 // value - Result pointer.
97 // True if conversion succeeded, false if it didn't.
98 bool DBusMessageIterToValue(DBus::MessageIter& message, Value** v);
100 // Convert a DBus property map to a Value.
103 // message - Message to convert.
104 // value - Result pointer.
106 // True if conversion succeeded, false if it didn't.
107 bool DBusPropertyMapToValue(std::map<std::string, DBus::Variant>&
108 properties, Value** v);
110 } // namespace chromeos
113 #endif /* CHROMEOS_UTILITY_H_ */