Skip to content
Snippets Groups Projects
Commit d4d808fc authored by Sebastian Würl's avatar Sebastian Würl
Browse files

Add implementation for a64l

parent 8028c351
No related branches found
No related tags found
1 merge request!66Add implementation for a64l
...@@ -507,6 +507,7 @@ name = "string" ...@@ -507,6 +507,7 @@ name = "string"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"cbindgen 0.5.0", "cbindgen 0.5.0",
"compiler_builtins 0.1.0 (git+https://github.com/rust-lang-nursery/compiler-builtins.git)",
"errno 0.1.0", "errno 0.1.0",
"platform 0.1.0", "platform 0.1.0",
"stdlib 0.1.0", "stdlib 0.1.0",
......
...@@ -23,8 +23,29 @@ pub const EXIT_SUCCESS: c_int = 0; ...@@ -23,8 +23,29 @@ pub const EXIT_SUCCESS: c_int = 0;
static mut ATEXIT_FUNCS: [Option<extern "C" fn()>; 32] = [None; 32]; static mut ATEXIT_FUNCS: [Option<extern "C" fn()>; 32] = [None; 32];
#[no_mangle] #[no_mangle]
pub extern "C" fn a64l(s: *const c_char) -> c_long { pub unsafe extern "C" fn a64l(s: *const c_char) -> c_long {
unimplemented!(); if s as isize == 0 {
return 0;
}
let mut l: c_long = 0;
for x in 0..7 {
let c = *((s as isize + x) as *const c_char);
if c == 0 {
// string is null terminated
return l;
}
// ASCII to base64 conversion:
let mut bits: c_long = if c < 58 {
(c - 46) as c_long // ./0123456789
} else if c < 91 {
(c - 53) as c_long // A-Z
} else {
(c - 59) as c_long // a-z
};
bits <<= 6 * x;
l |= bits;
}
return l;
} }
#[no_mangle] #[no_mangle]
......
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
/setid /setid
/sprintf /sprintf
/stdlib/strtol /stdlib/strtol
/stdlib/a64l
/string/strncmp /string/strncmp
/string/strcspn /string/strcspn
/string/strchr /string/strchr
......
...@@ -24,6 +24,7 @@ BINS=\ ...@@ -24,6 +24,7 @@ BINS=\
sleep \ sleep \
sprintf \ sprintf \
stdlib/strtol \ stdlib/strtol \
stdlib/a64l \
string/strncmp \ string/strncmp \
string/strcspn \ string/strcspn \
string/strchr \ string/strchr \
......
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char* argv[]) {
char * s = "azAZ9."; // test boundaries
long l = a64l(s);
if (l != 194301926) {
printf("Invalid result: a64l(%s) = %ld\n", s, l);
return 1;
}
printf("Correct a64l: %s = %ld\n", s, l);
s = "azA"; // test null terminated string
l = a64l(s);
if (l != 53222) {
printf("Invalid result: a64l(%s) = %ld\n", s, l);
return 1;
}
printf("Correct a64l: %s = %ld\n", s, l);
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment