OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016 The Chromium 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 #include "nacl_io/hash.h" |
| 6 #include "nacl_io/osdirent.h" |
| 7 |
| 8 namespace nacl_io { |
| 9 |
| 10 ino_t HashPathSegment(ino_t hash, const char* str, size_t len) { |
| 11 // First add the path seperator |
| 12 hash = (hash * static_cast<ino_t>(33)) ^ '/'; |
| 13 while (len--) { |
| 14 hash = (hash * static_cast<ino_t>(33)) ^ *str++; |
| 15 } |
| 16 return hash; |
| 17 } |
| 18 |
| 19 ino_t HashPath(const Path& path) { |
| 20 // Prime the DJB2a hash |
| 21 ino_t hash = 5381; |
| 22 |
| 23 // Apply a running DJB2a to each part of the path |
| 24 for (size_t segment = 0; segment < path.Size(); segment++) { |
| 25 const std::string& part = path.Part(segment); |
| 26 hash = HashPathSegment(hash, part.c_str(), part.length()); |
| 27 } |
| 28 return hash; |
| 29 } |
| 30 |
| 31 } // namespace nacl_io |
OLD | NEW |