forked from servo/core-foundation-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.rs
94 lines (79 loc) · 2.41 KB
/
data.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright 2013 The Servo Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use base::{
AbstractCFTypeRef,
CFAllocatorRef,
CFIndex,
CFRange,
CFTypeRef,
CFTypeID,
CFWrapper,
kCFAllocatorDefault,
};
use core::vec;
struct __CFData { private: () }
pub type CFDataRef = *__CFData;
impl AbstractCFTypeRef for CFDataRef {
fn as_type_ref(&self) -> CFTypeRef { *self as CFTypeRef }
fn type_id() -> CFTypeID {
unsafe {
CFDataGetTypeID()
}
}
}
type CFData = CFWrapper<CFDataRef, (), ()>;
pub impl CFData {
fn new_from_buf(buf: &[u8]) -> CFData {
let result;
unsafe {
result = CFDataCreate(kCFAllocatorDefault,
vec::raw::to_ptr(buf), buf.len() as CFIndex);
}
CFWrapper::wrap_owned(result)
}
// tread with caution; read-only
fn bytes(&self) -> *u8 {
unsafe {
CFDataGetBytePtr(self.obj)
}
}
fn len(&self) -> uint {
unsafe {
CFDataGetLength(self.obj) as uint
}
}
fn copy_to_buf(&self) -> ~[u8] {
unsafe {
vec::from_buf(self.bytes(), self.len())
}
}
fn with_buf<U>(&self, blk: &fn(v: &[u8]) -> U) -> U {
unsafe {
vec::raw::buf_as_slice(self.bytes(), self.len(), blk)
}
}
}
#[link_args="-framework CoreFoundation"]
#[nolink]
extern {
/*
* CFData.h
*/
fn CFDataCreate(allocator: CFAllocatorRef,
bytes: *u8, length: CFIndex) -> CFDataRef;
fn CFDataCreateCopy(allocator: CFAllocatorRef, theData: CFDataRef) -> CFDataRef;
fn CFDataCreateWithBytesNoCopy(allocator: CFAllocatorRef,
bytes: *u8, length: CFIndex,
bytesDeallocator: CFAllocatorRef) -> CFDataRef;
//fn CFDataFind
fn CFDataGetBytePtr(theData: CFDataRef) -> *u8;
fn CFDataGetBytes(theData: CFDataRef, range: CFRange, buffer: *u8);
fn CFDataGetLength(theData: CFDataRef) -> CFIndex;
fn CFDataGetTypeID() -> CFTypeID;
}