@@ -12,12 +12,12 @@ argument, which it then converts to a hexadecimal string and prints to
12
12
standard output. If you have the OpenSSL libraries installed, it
13
13
should compile and run without any extra effort.
14
14
15
- ~~~~
15
+ ~~~~ {.xfail-test}
16
16
extern mod std;
17
- use libc::size_t ;
17
+ use libc::c_uint ;
18
18
19
19
extern mod crypto {
20
- fn SHA1(src: *u8, sz: size_t , out: *u8) -> *u8;
20
+ fn SHA1(src: *u8, sz: c_uint , out: *u8) -> *u8;
21
21
}
22
22
23
23
fn as_hex(data: ~[u8]) -> ~str {
@@ -29,7 +29,7 @@ fn as_hex(data: ~[u8]) -> ~str {
29
29
fn sha1(data: ~str) -> ~str unsafe {
30
30
let bytes = str::to_bytes(data);
31
31
let hash = crypto::SHA1(vec::raw::to_ptr(bytes),
32
- vec::len(bytes) as size_t , ptr::null());
32
+ vec::len(bytes) as c_uint , ptr::null());
33
33
return as_hex(vec::from_buf(hash, 20));
34
34
}
35
35
@@ -43,11 +43,9 @@ fn main() {
43
43
Before we can call the ` SHA1 ` function defined in the OpenSSL library, we have
44
44
to declare it. That is what this part of the program does:
45
45
46
- ~~~~
47
- # use libc::size_t;
46
+ ~~~~ {.xfail-test}
48
47
extern mod crypto {
49
- fn SHA1(src: *u8, sz: size_t, out: *u8) -> *u8;
50
- }
48
+ fn SHA1(src: *u8, sz: uint, out: *u8) -> *u8; }
51
49
~~~~
52
50
53
51
An ` extern ` module declaration containing function signatures introduces the
@@ -64,11 +62,10 @@ searches for the shared library with that name, and links the library into the
64
62
program. If you want the module to have a different name from the actual
65
63
library, you can use the ` "link_name" ` attribute, like:
66
64
67
- ~~~~
68
- # use libc::size_t;
65
+ ~~~~ {.xfail-test}
69
66
#[link_name = "crypto"]
70
67
extern mod something {
71
- fn SHA1(src: *u8, sz: size_t , out: *u8) -> *u8;
68
+ fn SHA1(src: *u8, sz: uint , out: *u8) -> *u8;
72
69
}
73
70
~~~~
74
71
@@ -97,10 +94,9 @@ calling conventions.
97
94
98
95
The foreign ` SHA1 ` function takes three arguments, and returns a pointer.
99
96
100
- ~~~~
101
- # use libc::size_t;
97
+ ~~~~ {.xfail-test}
102
98
# extern mod crypto {
103
- fn SHA1(src: *u8, sz: size_t , out: *u8) -> *u8;
99
+ fn SHA1(src: *u8, sz: libc::c_uint , out: *u8) -> *u8;
104
100
# }
105
101
~~~~
106
102
@@ -112,8 +108,8 @@ probably even worse, your code will work on one platform, but break on
112
108
another.
113
109
114
110
In this case, we declare that ` SHA1 ` takes two ` unsigned char* `
115
- arguments and one ` size_t ` . The Rust equivalents are ` *u8 `
116
- unsafe pointers and an ` libc::size_t ` (which, like ` unsigned long ` , is a
111
+ arguments and one ` unsigned long ` . The Rust equivalents are ` *u8 `
112
+ unsafe pointers and an ` uint ` (which, like ` unsigned long ` , is a
117
113
machine-word-sized type).
118
114
119
115
The standard library provides various functions to create unsafe pointers,
@@ -128,16 +124,14 @@ The `sha1` function is the most obscure part of the program.
128
124
129
125
~~~~
130
126
# pub mod crypto {
131
- # use libc::size_t;
132
- # pub fn SHA1(src: *u8, sz: size_t, out: *u8) -> *u8 { out }
127
+ # pub fn SHA1(src: *u8, sz: uint, out: *u8) -> *u8 { out }
133
128
# }
134
- # use libc::size_t;
135
129
# fn as_hex(data: ~[u8]) -> ~str { ~"hi" }
136
130
fn sha1(data: ~str) -> ~str {
137
131
unsafe {
138
132
let bytes = str::to_bytes(data);
139
133
let hash = crypto::SHA1(vec::raw::to_ptr(bytes),
140
- vec::len(bytes) as size_t , ptr::null());
134
+ vec::len(bytes), ptr::null());
141
135
return as_hex(vec::from_buf(hash, 20));
142
136
}
143
137
}
@@ -175,16 +169,14 @@ Let's look at our `sha1` function again.
175
169
176
170
~~~~
177
171
# pub mod crypto {
178
- # use libc::size_t;
179
- # pub fn SHA1(src: *u8, sz: size_t, out: *u8) -> *u8 { out }
172
+ # pub fn SHA1(src: *u8, sz: uint, out: *u8) -> *u8 { out }
180
173
# }
181
- # use libc::size_t;
182
174
# fn as_hex(data: ~[u8]) -> ~str { ~"hi" }
183
175
# fn x(data: ~str) -> ~str {
184
176
# unsafe {
185
177
let bytes = str::to_bytes(data);
186
178
let hash = crypto::SHA1(vec::raw::to_ptr(bytes),
187
- vec::len(bytes) as size_t , ptr::null());
179
+ vec::len(bytes), ptr::null());
188
180
return as_hex(vec::from_buf(hash, 20));
189
181
# }
190
182
# }
0 commit comments