1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/security/sandbox/win/src/shared_handles.cc Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,67 @@ 1.4 +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1.5 +// Use of this source code is governed by a BSD-style license that can be 1.6 +// found in the LICENSE file. 1.7 + 1.8 +#include "sandbox/win/src/shared_handles.h" 1.9 + 1.10 +namespace sandbox { 1.11 + 1.12 +// Note once again the the assumption here is that the shared memory is 1.13 +// initialized with zeros in the process that calls SetHandle and that 1.14 +// the process that calls GetHandle 'sees' this memory. 1.15 + 1.16 +SharedHandles::SharedHandles() { 1.17 + shared_.items = NULL; 1.18 + shared_.max_items = 0; 1.19 +} 1.20 + 1.21 +bool SharedHandles::Init(void* raw_mem, size_t size_bytes) { 1.22 + if (size_bytes < sizeof(shared_.items[0])) { 1.23 + // The shared memory is too small! 1.24 + return false; 1.25 + } 1.26 + shared_.items = static_cast<SharedItem*>(raw_mem); 1.27 + shared_.max_items = size_bytes / sizeof(shared_.items[0]); 1.28 + return true; 1.29 +} 1.30 + 1.31 +// Note that an empty slot is marked with a tag == 0 that is why is 1.32 +// not a valid imput tag 1.33 +bool SharedHandles::SetHandle(uint32 tag, HANDLE handle) { 1.34 + if (0 == tag) { 1.35 + // Invalid tag 1.36 + return false; 1.37 + } 1.38 + // Find empty slot and put the tag and the handle there 1.39 + SharedItem* empty_slot = FindByTag(0); 1.40 + if (NULL == empty_slot) { 1.41 + return false; 1.42 + } 1.43 + empty_slot->tag = tag; 1.44 + empty_slot->item = handle; 1.45 + return true; 1.46 +} 1.47 + 1.48 +bool SharedHandles::GetHandle(uint32 tag, HANDLE* handle) { 1.49 + if (0 == tag) { 1.50 + // Invalid tag 1.51 + return false; 1.52 + } 1.53 + SharedItem* found = FindByTag(tag); 1.54 + if (NULL == found) { 1.55 + return false; 1.56 + } 1.57 + *handle = found->item; 1.58 + return true; 1.59 +} 1.60 + 1.61 +SharedHandles::SharedItem* SharedHandles::FindByTag(uint32 tag) { 1.62 + for (size_t ix = 0; ix != shared_.max_items; ++ix) { 1.63 + if (tag == shared_.items[ix].tag) { 1.64 + return &shared_.items[ix]; 1.65 + } 1.66 + } 1.67 + return NULL; 1.68 +} 1.69 + 1.70 +} // namespace sandbox