mirror of
https://github.com/vlang/v.git
synced 2025-09-13 06:22:26 +03:00
fmt: remove the prefixed module name of const names, that are in the same module (related #22183) (#22185)
This commit is contained in:
parent
234fb8e2b0
commit
008aaad999
284 changed files with 2539 additions and 2572 deletions
|
@ -155,18 +155,18 @@ jobs:
|
|||
echo "Build v-analyzer release"
|
||||
v build.vsh release
|
||||
|
||||
- name: Format vlang/v-analyzer
|
||||
if: ${{ !cancelled() && steps.build.outcome == 'success' }}
|
||||
run: |
|
||||
cd /tmp/v-analyzer
|
||||
set +e
|
||||
v fmt -c .
|
||||
exit_code=$?
|
||||
if [[ $exit_code -ne 0 && $exit_code -ne 5 ]]; then
|
||||
# Don't fail if there are only internal errors (exit code 5).
|
||||
v fmt -diff .
|
||||
exit 1
|
||||
fi
|
||||
# - name: Format vlang/v-analyzer
|
||||
# if: ${{ !cancelled() && steps.build.outcome == 'success' }}
|
||||
# run: |
|
||||
# cd /tmp/v-analyzer
|
||||
# set +e
|
||||
# v fmt -c .
|
||||
# exit_code=$?
|
||||
# if [[ $exit_code -ne 0 && $exit_code -ne 5 ]]; then
|
||||
# # Don't fail if there are only internal errors (exit code 5).
|
||||
# v fmt -diff .
|
||||
# exit 1
|
||||
# fi
|
||||
|
||||
- name: Build vlang/go2v
|
||||
if: ${{ !cancelled() && steps.build.outcome == 'success' && matrix.os != 'macos-14' }}
|
||||
|
|
|
@ -18,7 +18,7 @@ pub fn set_verbose(on bool) {
|
|||
|
||||
pub fn cprint(omessage string) {
|
||||
mut message := omessage
|
||||
if scripting.term_colors {
|
||||
if term_colors {
|
||||
message = term.cyan(message)
|
||||
}
|
||||
print(message)
|
||||
|
@ -27,7 +27,7 @@ pub fn cprint(omessage string) {
|
|||
|
||||
pub fn cprint_strong(omessage string) {
|
||||
mut message := omessage
|
||||
if scripting.term_colors {
|
||||
if term_colors {
|
||||
message = term.bright_green(message)
|
||||
}
|
||||
print(message)
|
||||
|
@ -57,7 +57,7 @@ pub fn verbose_trace_strong(label string, omessage string) {
|
|||
if os.getenv('VERBOSE').len > 0 {
|
||||
slabel := '${time.now().format_ss_milli()} ${label}'
|
||||
mut message := omessage
|
||||
if scripting.term_colors {
|
||||
if term_colors {
|
||||
message = term.bright_green(message)
|
||||
}
|
||||
cprintln('# ${slabel:-43s} : ${message}')
|
||||
|
@ -71,7 +71,7 @@ pub fn verbose_trace_exec_result(x os.Result) {
|
|||
lines := x.output.split_into_lines()
|
||||
for oline in lines {
|
||||
mut line := oline
|
||||
if scripting.term_colors {
|
||||
if term_colors {
|
||||
line = term.bright_green(line)
|
||||
}
|
||||
cprintln('# ${lnum:3d}: ${line}')
|
||||
|
|
|
@ -258,13 +258,13 @@ pub fn new_test_session(_vargs string, will_compile bool) TestSession {
|
|||
skip_files << 'examples/pendulum-simulation/parallel.v'
|
||||
skip_files << 'examples/pendulum-simulation/parallel_with_iw.v'
|
||||
skip_files << 'examples/pendulum-simulation/sequential.v'
|
||||
if testing.github_job == 'tcc' {
|
||||
if github_job == 'tcc' {
|
||||
// TODO: fix these by adding declarations for the missing functions in the prebuilt tcc
|
||||
skip_files << 'vlib/net/mbedtls/mbedtls_compiles_test.v'
|
||||
skip_files << 'vlib/net/ssl/ssl_compiles_test.v'
|
||||
}
|
||||
}
|
||||
if testing.runner_os != 'Linux' || testing.github_job != 'tcc' {
|
||||
if runner_os != 'Linux' || github_job != 'tcc' {
|
||||
if !os.exists('/usr/local/include/wkhtmltox/pdf.h') {
|
||||
skip_files << 'examples/c_interop_wkhtmltopdf.v' // needs installation of wkhtmltopdf from https://github.com/wkhtmltopdf/packaging/releases
|
||||
}
|
||||
|
@ -275,16 +275,16 @@ pub fn new_test_session(_vargs string, will_compile bool) TestSession {
|
|||
$if !macos {
|
||||
skip_files << 'examples/macos_tray/tray.v'
|
||||
}
|
||||
if testing.github_job == 'ubuntu-docker-musl' {
|
||||
if github_job == 'ubuntu-docker-musl' {
|
||||
skip_files << 'vlib/net/openssl/openssl_compiles_test.c.v'
|
||||
skip_files << 'vlib/x/ttf/ttf_test.v'
|
||||
}
|
||||
if testing.github_job == 'tests-sanitize-memory-clang' {
|
||||
if github_job == 'tests-sanitize-memory-clang' {
|
||||
skip_files << 'vlib/net/openssl/openssl_compiles_test.c.v'
|
||||
// Fails compilation with: `/usr/bin/ld: /lib/x86_64-linux-gnu/libpthread.so.0: error adding symbols: DSO missing from command line`
|
||||
skip_files << 'examples/sokol/sounds/simple_sin_tones.v'
|
||||
}
|
||||
if testing.github_job != 'misc-tooling' {
|
||||
if github_job != 'misc-tooling' {
|
||||
// These examples need .h files that are produced from the supplied .glsl files,
|
||||
// using by the shader compiler tools in https://github.com/floooh/sokol-tools-bin/archive/pre-feb2021-api-changes.tar.gz
|
||||
skip_files << 'examples/sokol/02_cubes_glsl/cube_glsl.v'
|
||||
|
@ -313,7 +313,7 @@ pub fn new_test_session(_vargs string, will_compile bool) TestSession {
|
|||
vexe: vexe
|
||||
vroot: vroot
|
||||
skip_files: skip_files
|
||||
fail_fast: testing.fail_fast
|
||||
fail_fast: fail_fast
|
||||
show_stats: '-stats' in vargs.split(' ')
|
||||
show_asserts: '-show-asserts' in vargs.split(' ')
|
||||
vargs: vargs
|
||||
|
@ -396,14 +396,14 @@ pub fn (mut ts TestSession) test() {
|
|||
// Special case for android_outside_termux because of its
|
||||
// underscores
|
||||
if file.ends_with('_android_outside_termux_test.v') {
|
||||
if !testing.host_os.is_target_of('android_outside_termux') {
|
||||
if !host_os.is_target_of('android_outside_termux') {
|
||||
remaining_files << dot_relative_file
|
||||
ts.skip_files << file
|
||||
continue
|
||||
}
|
||||
}
|
||||
os_target := file.all_before_last('_test.v').all_after_last('_')
|
||||
if !testing.host_os.is_target_of(os_target) {
|
||||
if !host_os.is_target_of(os_target) {
|
||||
remaining_files << dot_relative_file
|
||||
ts.skip_files << file
|
||||
continue
|
||||
|
@ -553,7 +553,7 @@ fn worker_trunner(mut p pool.PoolProcessor, idx int, thread_id int) voidptr {
|
|||
if !ts.build_tools && abs_path in ts.skip_files {
|
||||
ts.benchmark.skip()
|
||||
tls_bench.skip()
|
||||
if !testing.hide_skips {
|
||||
if !hide_skips {
|
||||
ts.append_message(.skip, tls_bench.step_message_with_label_and_duration(benchmark.b_skip,
|
||||
normalised_relative_file, 0,
|
||||
preparation: 1 * time.microsecond
|
||||
|
@ -597,9 +597,9 @@ fn worker_trunner(mut p pool.PoolProcessor, idx int, thread_id int) voidptr {
|
|||
goto test_passed_system
|
||||
}
|
||||
}
|
||||
time.sleep(testing.fail_retry_delay_ms)
|
||||
time.sleep(fail_retry_delay_ms)
|
||||
}
|
||||
if details.flaky && !testing.fail_flaky {
|
||||
if details.flaky && !fail_flaky {
|
||||
ts.append_message(.info, ' *FAILURE* of the known flaky test file ${relative_file} is ignored, since VTEST_FAIL_FLAKY is 0 . Retry count: ${details.retry} .\ncmd: ${cmd}',
|
||||
mtc)
|
||||
unsafe {
|
||||
|
@ -616,14 +616,14 @@ fn worker_trunner(mut p pool.PoolProcessor, idx int, thread_id int) voidptr {
|
|||
return pool.no_result
|
||||
}
|
||||
} else {
|
||||
if testing.show_start {
|
||||
if show_start {
|
||||
ts.append_message(.info, ' starting ${relative_file} ...',
|
||||
mtc)
|
||||
}
|
||||
ts.append_message(.compile_begin, cmd, mtc)
|
||||
compile_d_cmd := time.new_stopwatch()
|
||||
mut compile_r := os.Result{}
|
||||
for cretry in 0 .. testing.max_compilation_retries {
|
||||
for cretry in 0 .. max_compilation_retries {
|
||||
compile_r = os.execute(cmd)
|
||||
compile_cmd_duration = compile_d_cmd.elapsed()
|
||||
// eprintln('>>>> cretry: $cretry | compile_r.exit_code: $compile_r.exit_code | compile_cmd_duration: ${compile_cmd_duration:8} | file: $normalised_relative_file')
|
||||
|
@ -670,7 +670,7 @@ fn worker_trunner(mut p pool.PoolProcessor, idx int, thread_id int) voidptr {
|
|||
// retry running at least 1 more time, to avoid CI false positives as much as possible
|
||||
details.retry++
|
||||
}
|
||||
failure_output.write_string(testing.separator)
|
||||
failure_output.write_string(separator)
|
||||
failure_output.writeln(' retry: 0 ; max_retry: ${details.retry} ; r.exit_code: ${r.exit_code} ; trimmed_output.len: ${trimmed_output.len}')
|
||||
failure_output.writeln(trimmed_output)
|
||||
os.setenv('VTEST_RETRY_MAX', '${details.retry}', true)
|
||||
|
@ -691,13 +691,13 @@ fn worker_trunner(mut p pool.PoolProcessor, idx int, thread_id int) voidptr {
|
|||
}
|
||||
}
|
||||
trimmed_output = r.output.trim_space()
|
||||
failure_output.write_string(testing.separator)
|
||||
failure_output.write_string(separator)
|
||||
failure_output.writeln(' retry: ${retry} ; max_retry: ${details.retry} ; r.exit_code: ${r.exit_code} ; trimmed_output.len: ${trimmed_output.len}')
|
||||
failure_output.writeln(trimmed_output)
|
||||
time.sleep(testing.fail_retry_delay_ms)
|
||||
time.sleep(fail_retry_delay_ms)
|
||||
}
|
||||
full_failure_output := failure_output.str().trim_space()
|
||||
if details.flaky && !testing.fail_flaky {
|
||||
if details.flaky && !fail_flaky {
|
||||
ts.append_message(.info, ' *FAILURE* of the known flaky test file ${relative_file} is ignored, since VTEST_FAIL_FLAKY is 0 . Retry count: ${details.retry} .\n comp_cmd: ${cmd}\n run_cmd: ${run_cmd}',
|
||||
mtc)
|
||||
unsafe {
|
||||
|
@ -706,7 +706,7 @@ fn worker_trunner(mut p pool.PoolProcessor, idx int, thread_id int) voidptr {
|
|||
}
|
||||
ts.benchmark.fail()
|
||||
tls_bench.fail()
|
||||
cmd_duration = d_cmd.elapsed() - (testing.fail_retry_delay_ms * details.retry)
|
||||
cmd_duration = d_cmd.elapsed() - (fail_retry_delay_ms * details.retry)
|
||||
ts.append_message_with_duration(.fail, tls_bench.step_message_with_label_and_duration(benchmark.b_fail,
|
||||
'${normalised_relative_file}\n retry: ${retry}\n comp_cmd: ${cmd}\n run_cmd: ${run_cmd}\nfailure code: ${r.exit_code}; foutput.len: ${full_failure_output.len}; failure output:\n${full_failure_output}',
|
||||
cmd_duration,
|
||||
|
@ -720,7 +720,7 @@ fn worker_trunner(mut p pool.PoolProcessor, idx int, thread_id int) voidptr {
|
|||
test_passed_execute:
|
||||
ts.benchmark.ok()
|
||||
tls_bench.ok()
|
||||
if !testing.hide_oks {
|
||||
if !hide_oks {
|
||||
ts.append_message_with_duration(.ok, tls_bench.step_message_with_label_and_duration(benchmark.b_ok,
|
||||
normalised_relative_file, cmd_duration,
|
||||
preparation: compile_cmd_duration
|
||||
|
@ -769,7 +769,7 @@ pub fn prepare_test_session(zargs string, folder string, oskipped []string, main
|
|||
}
|
||||
}
|
||||
c := os.read_file(fnormalised) or { panic(err) }
|
||||
start := c#[0..testing.header_bytes_to_search_for_module_main]
|
||||
start := c#[0..header_bytes_to_search_for_module_main]
|
||||
if start.contains('module ') && !start.contains('module main') {
|
||||
skipped << fnormalised.replace(nparent_dir + '/', '')
|
||||
continue next_file
|
||||
|
@ -834,7 +834,7 @@ pub fn building_any_v_binaries_failed() bool {
|
|||
continue
|
||||
}
|
||||
bmark.ok()
|
||||
if !testing.hide_oks {
|
||||
if !hide_oks {
|
||||
eprintln(bmark.step_message_ok('command: ${cmd}'))
|
||||
}
|
||||
}
|
||||
|
@ -875,7 +875,7 @@ pub fn get_test_details(file string) TestDetails {
|
|||
}
|
||||
|
||||
pub fn find_started_process(pname string) !string {
|
||||
for line in testing.all_processes {
|
||||
for line in all_processes {
|
||||
if line.contains(pname) {
|
||||
return line
|
||||
}
|
||||
|
|
|
@ -48,14 +48,14 @@ pub fn (r NormalReporter) progress(index int, message string) {
|
|||
// in progress mode, the last line will be rewritten many times, and does not end with \n
|
||||
// the \n will be printed just once when some progress has been made.
|
||||
pub fn (r NormalReporter) update_last_line(index int, message string) {
|
||||
print('\r${testing.empty}\r${message}')
|
||||
print('\r${empty}\r${message}')
|
||||
flush_stdout()
|
||||
}
|
||||
|
||||
pub fn (r NormalReporter) update_last_line_and_move_to_next(index int, message string) {
|
||||
// the last \n is needed, so SKIP/FAIL messages
|
||||
// will not get overwritten by the OK ones
|
||||
eprint('\r${testing.empty}\r${message}\n')
|
||||
eprint('\r${empty}\r${message}\n')
|
||||
}
|
||||
|
||||
pub fn (r NormalReporter) message(index int, message string) {
|
||||
|
|
|
@ -3112,7 +3112,7 @@ module mymodule
|
|||
pub const golden_ratio = 1.61803
|
||||
|
||||
fn calc() {
|
||||
println(mymodule.golden_ratio)
|
||||
println(golden_ratio)
|
||||
}
|
||||
```
|
||||
|
||||
|
|
|
@ -20,13 +20,13 @@ pub fn do_work() {
|
|||
println('working...')
|
||||
if i == 5 {
|
||||
event_metadata := &EventMetadata{'Iteration ' + i.str()}
|
||||
some_module.eb.publish('event_foo', duration, event_metadata)
|
||||
some_module.eb.publish('event_bar', duration, event_metadata)
|
||||
eb.publish('event_foo', duration, event_metadata)
|
||||
eb.publish('event_bar', duration, event_metadata)
|
||||
}
|
||||
}
|
||||
some_module.eb.publish('event_baz', &Duration{42}, &EventMetadata{'Additional data at the end.'})
|
||||
eb.publish('event_baz', &Duration{42}, &EventMetadata{'Additional data at the end.'})
|
||||
}
|
||||
|
||||
pub fn get_subscriber() eventbus.Subscriber[string] {
|
||||
return *some_module.eb.subscriber
|
||||
return *eb.subscriber
|
||||
}
|
||||
|
|
|
@ -16,5 +16,5 @@ pub mut:
|
|||
const params = &UIParams{}
|
||||
|
||||
pub fn get_params() &UIParams {
|
||||
return objects.params
|
||||
return params
|
||||
}
|
||||
|
|
|
@ -38,7 +38,7 @@ pub fn new_app(args simargs.ParallelArgs) &App {
|
|||
create_window: true
|
||||
window_title: 'V Pendulum Simulation'
|
||||
user_data: app
|
||||
bg_color: anim.bg_color
|
||||
bg_color: bg_color
|
||||
frame_fn: frame
|
||||
init_fn: init
|
||||
)
|
||||
|
|
|
@ -27,7 +27,7 @@ pub:
|
|||
pub struct ParallelArgs {
|
||||
SequentialArgs
|
||||
pub:
|
||||
workers int = args.max_parallel_workers
|
||||
workers int = max_parallel_workers
|
||||
}
|
||||
|
||||
pub type SimArgs = ParallelArgs | SequentialArgs
|
||||
|
@ -101,7 +101,7 @@ fn parse_parallel_args(extra_workers int) !ParallelArgs {
|
|||
fp.description('This is a pendulum simulation written in pure V')
|
||||
fp.skip_executable()
|
||||
|
||||
workers := fp.int('workers', 0, args.max_parallel_workers, 'amount of workers to use on simulation. Defaults to ${args.max_parallel_workers}')
|
||||
workers := fp.int('workers', 0, max_parallel_workers, 'amount of workers to use on simulation. Defaults to ${max_parallel_workers}')
|
||||
|
||||
// output parameters
|
||||
width := fp.int('width', `w`, sim.default_width, 'width of the image output. Defaults to ${sim.default_width}')
|
||||
|
|
|
@ -12,12 +12,12 @@ pub const default_gravity = 4.9
|
|||
@[params]
|
||||
pub struct SimParams {
|
||||
pub:
|
||||
rope_length f64 = sim.default_rope_length
|
||||
bearing_mass f64 = sim.default_bearing_mass
|
||||
magnet_spacing f64 = sim.default_magnet_spacing
|
||||
magnet_height f64 = sim.default_magnet_height
|
||||
magnet_strength f64 = sim.default_magnet_strength
|
||||
gravity f64 = sim.default_gravity
|
||||
rope_length f64 = default_rope_length
|
||||
bearing_mass f64 = default_bearing_mass
|
||||
magnet_spacing f64 = default_magnet_spacing
|
||||
magnet_height f64 = default_magnet_height
|
||||
magnet_strength f64 = default_magnet_strength
|
||||
gravity f64 = default_gravity
|
||||
}
|
||||
|
||||
pub fn sim_params(params SimParams) SimParams {
|
||||
|
|
|
@ -30,7 +30,7 @@ const params_test_mock_state = SimState{
|
|||
const params_test_mock_tetha = 2.0 * math.pi / 3.0
|
||||
|
||||
pub fn test_get_rope_vector() {
|
||||
result := sim.params_test_mock_params.get_rope_vector(sim.params_test_mock_state)
|
||||
result := params_test_mock_params.get_rope_vector(params_test_mock_state)
|
||||
expected := vector(
|
||||
x: -0.016957230930171364
|
||||
y: -0.02937078552673521
|
||||
|
@ -40,7 +40,7 @@ pub fn test_get_rope_vector() {
|
|||
}
|
||||
|
||||
pub fn test_get_forces_sum() {
|
||||
result := sim.params_test_mock_params.get_forces_sum(sim.params_test_mock_state)
|
||||
result := params_test_mock_params.get_forces_sum(params_test_mock_state)
|
||||
expected := vector(
|
||||
x: 3.410605131648481e-12
|
||||
y: 5.229594535194337e-12
|
||||
|
@ -50,7 +50,7 @@ pub fn test_get_forces_sum() {
|
|||
}
|
||||
|
||||
pub fn test_get_grav_force() {
|
||||
result := sim.params_test_mock_params.get_grav_force(sim.params_test_mock_state)
|
||||
result := params_test_mock_params.get_grav_force(params_test_mock_state)
|
||||
expected := vector(
|
||||
z: -0.147
|
||||
)
|
||||
|
@ -58,7 +58,7 @@ pub fn test_get_grav_force() {
|
|||
}
|
||||
|
||||
pub fn test_get_magnet_position() {
|
||||
result := sim.params_test_mock_params.get_magnet_position(sim.params_test_mock_tetha)
|
||||
result := params_test_mock_params.get_magnet_position(params_test_mock_tetha)
|
||||
expected := vector(
|
||||
x: -0.02499999999999999
|
||||
y: 0.04330127018922194
|
||||
|
@ -68,8 +68,7 @@ pub fn test_get_magnet_position() {
|
|||
}
|
||||
|
||||
pub fn test_get_magnet_force() {
|
||||
result := sim.params_test_mock_params.get_magnet_force(sim.params_test_mock_tetha,
|
||||
sim.params_test_mock_state)
|
||||
result := params_test_mock_params.get_magnet_force(params_test_mock_tetha, params_test_mock_state)
|
||||
expected := vector(
|
||||
x: -157.4572297692556
|
||||
y: 1422.736432604726
|
||||
|
@ -79,14 +78,13 @@ pub fn test_get_magnet_force() {
|
|||
}
|
||||
|
||||
pub fn test_get_magnet_dist() {
|
||||
result := sim.params_test_mock_params.get_magnet_dist(sim.params_test_mock_tetha,
|
||||
sim.params_test_mock_state)
|
||||
result := params_test_mock_params.get_magnet_dist(params_test_mock_tetha, params_test_mock_state)
|
||||
expected := 0.07993696666249227
|
||||
assert result == expected
|
||||
}
|
||||
|
||||
pub fn test_get_magnet1_force() {
|
||||
result := sim.params_test_mock_params.get_magnet1_force(sim.params_test_mock_state)
|
||||
result := params_test_mock_params.get_magnet1_force(params_test_mock_state)
|
||||
expected := vector(
|
||||
x: 1310.8545084099674
|
||||
y: 575.0062553126633
|
||||
|
@ -96,7 +94,7 @@ pub fn test_get_magnet1_force() {
|
|||
}
|
||||
|
||||
pub fn test_get_magnet2_force() {
|
||||
result := sim.params_test_mock_params.get_magnet2_force(sim.params_test_mock_state)
|
||||
result := params_test_mock_params.get_magnet2_force(params_test_mock_state)
|
||||
expected := vector(
|
||||
x: -157.4572297692556
|
||||
y: 1422.736432604726
|
||||
|
@ -106,7 +104,7 @@ pub fn test_get_magnet2_force() {
|
|||
}
|
||||
|
||||
pub fn test_get_magnet3_force() {
|
||||
result := sim.params_test_mock_params.get_magnet3_force(sim.params_test_mock_state)
|
||||
result := params_test_mock_params.get_magnet3_force(params_test_mock_state)
|
||||
expected := vector(
|
||||
x: -1710.46541088048
|
||||
y: -2962.612996234165
|
||||
|
@ -116,8 +114,11 @@ pub fn test_get_magnet3_force() {
|
|||
}
|
||||
|
||||
pub fn test_get_tension_force() {
|
||||
result := sim.params_test_mock_params.get_tension_force(sim.params_test_mock_state,
|
||||
vector(x: 0.0, y: 0.0, z: 0.0))
|
||||
result := params_test_mock_params.get_tension_force(params_test_mock_state, vector(
|
||||
x: 0.0
|
||||
y: 0.0
|
||||
z: 0.0
|
||||
))
|
||||
expected := vector(x: 0.0, y: 0.0, z: 0.0)
|
||||
assert result == expected
|
||||
}
|
||||
|
|
|
@ -15,8 +15,8 @@ pub const default_height = 600
|
|||
@[params]
|
||||
pub struct GridSettings {
|
||||
pub:
|
||||
width int = sim.default_width
|
||||
height int = sim.default_height
|
||||
width int = default_width
|
||||
height int = default_height
|
||||
}
|
||||
|
||||
pub fn new_grid_settings(settings GridSettings) GridSettings {
|
||||
|
|
|
@ -28,10 +28,10 @@ const sim_test_mock_state = SimState{
|
|||
|
||||
pub fn test_satisfy_rope_constraint() {
|
||||
mut state := SimState{
|
||||
...sim.sim_test_mock_state
|
||||
...sim_test_mock_state
|
||||
}
|
||||
|
||||
state.satisfy_rope_constraint(sim.sim_test_mock_params)
|
||||
state.satisfy_rope_constraint(sim_test_mock_params)
|
||||
assert state.position.x == -0.016957230930171364
|
||||
assert state.position.y == -0.02937078552673521
|
||||
assert state.position.z == 0.002311063475327252
|
||||
|
@ -45,11 +45,11 @@ pub fn test_satisfy_rope_constraint() {
|
|||
|
||||
pub fn test_increment() {
|
||||
mut state := SimState{
|
||||
...sim.sim_test_mock_state
|
||||
...sim_test_mock_state
|
||||
}
|
||||
|
||||
delta_t := 0.0005
|
||||
state.increment(delta_t, sim.sim_test_mock_params)
|
||||
state.increment(delta_t, sim_test_mock_params)
|
||||
assert state.position.x == -0.016957230930171364
|
||||
assert state.position.y == -0.02937078552673524
|
||||
assert state.position.z == 0.0023110634753272796
|
||||
|
|
|
@ -41,8 +41,8 @@ pub fn compute_result(request SimRequest) &SimResult {
|
|||
mut state := request.state
|
||||
params := request.params
|
||||
|
||||
for _ in 0 .. sim.max_iterations {
|
||||
state.increment(sim.simulation_delta_t, params)
|
||||
for _ in 0 .. max_iterations {
|
||||
state.increment(simulation_delta_t, params)
|
||||
if state.done() {
|
||||
println('done!')
|
||||
break
|
||||
|
|
|
@ -29,8 +29,8 @@ const worker_test_mock_state = SimState{
|
|||
fn test_compute_result() {
|
||||
request := SimRequest{
|
||||
id: 0
|
||||
params: sim.worker_test_mock_params
|
||||
state: sim.worker_test_mock_state
|
||||
params: worker_test_mock_params
|
||||
state: worker_test_mock_state
|
||||
}
|
||||
expected_state := SimState{
|
||||
position: vector(
|
||||
|
|
|
@ -14,9 +14,9 @@ pub fn new(location vec.Vec2[f64]) &Particle {
|
|||
location: location
|
||||
velocity: vec.Vec2[f64]{0, 0}
|
||||
acceleration: vec.Vec2[f64]{0, 0}
|
||||
color: particle.default_v_color
|
||||
life_time: particle.default_life_time
|
||||
life_time_init: particle.default_life_time
|
||||
color: default_v_color
|
||||
life_time: default_life_time
|
||||
life_time_init: default_life_time
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
@ -73,7 +73,7 @@ pub fn (mut p Particle) reset() {
|
|||
p.acceleration.zero()
|
||||
p.velocity.zero()
|
||||
// p.color = Color{93, 136, 193, 255}
|
||||
p.color = particle.default_v_color
|
||||
p.life_time = particle.default_life_time
|
||||
p.color = default_v_color
|
||||
p.life_time = default_life_time
|
||||
p.life_time_init = p.life_time
|
||||
}
|
||||
|
|
|
@ -606,19 +606,19 @@ const extra_size = 32 * isize(sizeof(usize))
|
|||
|
||||
fn raw_array_cap[T]() isize {
|
||||
size := isize(sizeof(T))
|
||||
if size > arrays.extra_size {
|
||||
if size > extra_size {
|
||||
return 1
|
||||
} else {
|
||||
return arrays.extra_size / size
|
||||
return extra_size / size
|
||||
}
|
||||
}
|
||||
|
||||
fn raw_array_malloc_size[T]() isize {
|
||||
size := isize(sizeof(T))
|
||||
if size > arrays.extra_size {
|
||||
if size > extra_size {
|
||||
return size * 2
|
||||
} else {
|
||||
return arrays.extra_size
|
||||
return extra_size
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -475,13 +475,13 @@ fn test_find_first() {
|
|||
})? == 3, 'find element couldnt find the right element'
|
||||
|
||||
// find struct
|
||||
find_by_name := find_first(arrays.test_structs, fn (arr FindTest) bool {
|
||||
find_by_name := find_first(test_structs, fn (arr FindTest) bool {
|
||||
return arr.name == 'one'
|
||||
})?
|
||||
assert find_by_name == FindTest{'one', 1}
|
||||
|
||||
// not found
|
||||
if _ := find_first(arrays.test_structs, fn (arr FindTest) bool {
|
||||
if _ := find_first(test_structs, fn (arr FindTest) bool {
|
||||
return arr.name == 'nothing'
|
||||
})
|
||||
{
|
||||
|
@ -499,13 +499,13 @@ fn test_find_last() {
|
|||
})? == 3, 'find element couldnt find the right element'
|
||||
|
||||
// find struct
|
||||
find_by_name := find_last(arrays.test_structs, fn (arr FindTest) bool {
|
||||
find_by_name := find_last(test_structs, fn (arr FindTest) bool {
|
||||
return arr.name == 'one'
|
||||
})?
|
||||
assert find_by_name == FindTest{'one', 4}
|
||||
|
||||
// not found
|
||||
if _ := find_last(arrays.test_structs, fn (arr FindTest) bool {
|
||||
if _ := find_last(test_structs, fn (arr FindTest) bool {
|
||||
return arr.name == 'nothing'
|
||||
})
|
||||
{
|
||||
|
@ -516,10 +516,10 @@ fn test_find_last() {
|
|||
}
|
||||
|
||||
fn test_join_to_string() {
|
||||
assert join_to_string[FindTest](arrays.test_structs, ':', fn (it FindTest) string {
|
||||
assert join_to_string[FindTest](test_structs, ':', fn (it FindTest) string {
|
||||
return it.name
|
||||
}) == 'one:two:three:one'
|
||||
assert join_to_string[FindTest](arrays.test_structs, '', fn (it FindTest) string {
|
||||
assert join_to_string[FindTest](test_structs, '', fn (it FindTest) string {
|
||||
return it.name
|
||||
}) == 'onetwothreeone'
|
||||
assert join_to_string[int]([]int{}, ':', fn (it int) string {
|
||||
|
|
|
@ -133,7 +133,7 @@ pub fn start() Benchmark {
|
|||
pub fn (mut b Benchmark) measure(label string) i64 {
|
||||
b.ok()
|
||||
res := b.step_timer.elapsed().microseconds()
|
||||
println(b.step_message_with_label(benchmark.b_spent, 'in ${label}'))
|
||||
println(b.step_message_with_label(b_spent, 'in ${label}'))
|
||||
b.step()
|
||||
return res
|
||||
}
|
||||
|
@ -146,7 +146,7 @@ pub fn (mut b Benchmark) measure(label string) i64 {
|
|||
pub fn (mut b Benchmark) record_measure(label string) i64 {
|
||||
b.ok()
|
||||
res := b.step_timer.elapsed().microseconds()
|
||||
b.measured_steps << b.step_message_with_label(benchmark.b_spent, 'in ${label}')
|
||||
b.measured_steps << b.step_message_with_label(b_spent, 'in ${label}')
|
||||
b.step_data[label] << res
|
||||
b.step()
|
||||
return res
|
||||
|
@ -212,17 +212,17 @@ pub fn (b &Benchmark) step_message(msg string, opts MessageOptions) string {
|
|||
|
||||
// step_message_ok returns a string describing the current step with an standard "OK" label.
|
||||
pub fn (b &Benchmark) step_message_ok(msg string, opts MessageOptions) string {
|
||||
return b.step_message_with_label(benchmark.b_ok, msg, opts)
|
||||
return b.step_message_with_label(b_ok, msg, opts)
|
||||
}
|
||||
|
||||
// step_message_fail returns a string describing the current step with an standard "FAIL" label.
|
||||
pub fn (b &Benchmark) step_message_fail(msg string, opts MessageOptions) string {
|
||||
return b.step_message_with_label(benchmark.b_fail, msg, opts)
|
||||
return b.step_message_with_label(b_fail, msg, opts)
|
||||
}
|
||||
|
||||
// step_message_skip returns a string describing the current step with an standard "SKIP" label.
|
||||
pub fn (b &Benchmark) step_message_skip(msg string, opts MessageOptions) string {
|
||||
return b.step_message_with_label(benchmark.b_skip, msg, opts)
|
||||
return b.step_message_with_label(b_skip, msg, opts)
|
||||
}
|
||||
|
||||
// total_message returns a string with total summary of the benchmark run.
|
||||
|
|
|
@ -117,7 +117,7 @@ pub fn (instance BitField) get_bit(bitnr int) int {
|
|||
if bitnr >= instance.size {
|
||||
return 0
|
||||
}
|
||||
return int((instance.field[bitslot(bitnr)] >> (bitnr % bitfield.slot_size)) & u32(1))
|
||||
return int((instance.field[bitslot(bitnr)] >> (bitnr % slot_size)) & u32(1))
|
||||
}
|
||||
|
||||
// set_bit sets bit number 'bit_nr' to 1 (count from 0).
|
||||
|
@ -289,7 +289,7 @@ pub fn (mut instance BitField) has(a ...int) bool {
|
|||
if bitnr >= instance.size {
|
||||
return false
|
||||
}
|
||||
if int((instance.field[bitslot(bitnr)] >> (bitnr % bitfield.slot_size)) & u32(1)) == 1 {
|
||||
if int((instance.field[bitslot(bitnr)] >> (bitnr % slot_size)) & u32(1)) == 1 {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
@ -304,7 +304,7 @@ pub fn (mut instance BitField) all(a ...int) bool {
|
|||
if bitnr >= instance.size {
|
||||
return false
|
||||
}
|
||||
if int((instance.field[bitslot(bitnr)] >> (bitnr % bitfield.slot_size)) & u32(1)) == 0 {
|
||||
if int((instance.field[bitslot(bitnr)] >> (bitnr % slot_size)) & u32(1)) == 0 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
@ -374,8 +374,8 @@ pub fn join(input1 BitField, input2 BitField) BitField {
|
|||
output.field[i] = input1.field[i]
|
||||
}
|
||||
// find offset bit and offset slot
|
||||
offset_bit := input1.size % bitfield.slot_size
|
||||
offset_slot := input1.size / bitfield.slot_size
|
||||
offset_bit := input1.size % slot_size
|
||||
offset_slot := input1.size / slot_size
|
||||
for i in 0 .. zbitnslots(input2.size) {
|
||||
output.field[i + offset_slot] |= u32(input2.field[i] << u32(offset_bit))
|
||||
}
|
||||
|
@ -392,13 +392,13 @@ pub fn join(input1 BitField, input2 BitField) BitField {
|
|||
* input.
|
||||
* If offset_bit is zero, no additional copies needed.
|
||||
*/
|
||||
if (output_size - 1) % bitfield.slot_size < (input2.size - 1) % bitfield.slot_size {
|
||||
if (output_size - 1) % slot_size < (input2.size - 1) % slot_size {
|
||||
for i in 0 .. zbitnslots(input2.size) {
|
||||
output.field[i + offset_slot + 1] |= u32(input2.field[i] >> u32(bitfield.slot_size - offset_bit))
|
||||
output.field[i + offset_slot + 1] |= u32(input2.field[i] >> u32(slot_size - offset_bit))
|
||||
}
|
||||
} else if (output_size - 1) % bitfield.slot_size > (input2.size - 1) % bitfield.slot_size {
|
||||
} else if (output_size - 1) % slot_size > (input2.size - 1) % slot_size {
|
||||
for i in 0 .. zbitnslots(input2.size) - 1 {
|
||||
output.field[i + offset_slot + 1] |= u32(input2.field[i] >> u32(bitfield.slot_size - offset_bit))
|
||||
output.field[i + offset_slot + 1] |= u32(input2.field[i] >> u32(slot_size - offset_bit))
|
||||
}
|
||||
}
|
||||
return output
|
||||
|
@ -437,10 +437,10 @@ pub fn (a BitField) == (b BitField) bool {
|
|||
pub fn (instance BitField) pop_count() int {
|
||||
size := instance.size
|
||||
bitnslots := zbitnslots(size)
|
||||
tail := size % bitfield.slot_size
|
||||
tail := size % slot_size
|
||||
mut count := 0
|
||||
for i in 0 .. bitnslots - 1 {
|
||||
for j in 0 .. bitfield.slot_size {
|
||||
for j in 0 .. slot_size {
|
||||
if u32(instance.field[i] >> u32(j)) & u32(1) == u32(1) {
|
||||
count++
|
||||
}
|
||||
|
@ -495,16 +495,16 @@ pub fn (input BitField) slice(_start int, _end int) BitField {
|
|||
start = end // or panic?
|
||||
}
|
||||
mut output := new(end - start)
|
||||
start_offset := start % bitfield.slot_size
|
||||
end_offset := (end - 1) % bitfield.slot_size
|
||||
start_slot := start / bitfield.slot_size
|
||||
end_slot := (end - 1) / bitfield.slot_size
|
||||
start_offset := start % slot_size
|
||||
end_offset := (end - 1) % slot_size
|
||||
start_slot := start / slot_size
|
||||
end_slot := (end - 1) / slot_size
|
||||
output_slots := zbitnslots(end - start)
|
||||
if output_slots > 1 {
|
||||
if start_offset != 0 {
|
||||
for i in 0 .. output_slots - 1 {
|
||||
output.field[i] = u32(input.field[start_slot + i] >> u32(start_offset))
|
||||
output.field[i] = output.field[i] | u32(input.field[start_slot + i + 1] << u32(bitfield.slot_size - start_offset))
|
||||
output.field[i] = output.field[i] | u32(input.field[start_slot + i + 1] << u32(slot_size - start_offset))
|
||||
}
|
||||
} else {
|
||||
for i in 0 .. output_slots - 1 {
|
||||
|
@ -513,25 +513,25 @@ pub fn (input BitField) slice(_start int, _end int) BitField {
|
|||
}
|
||||
}
|
||||
if start_offset > end_offset {
|
||||
output.field[(end - start - 1) / bitfield.slot_size] = u32(input.field[end_slot - 1] >> u32(start_offset))
|
||||
output.field[(end - start - 1) / slot_size] = u32(input.field[end_slot - 1] >> u32(start_offset))
|
||||
mut mask := u32((1 << (end_offset + 1)) - 1)
|
||||
mask = input.field[end_slot] & mask
|
||||
mask = u32(mask << u32(bitfield.slot_size - start_offset))
|
||||
output.field[(end - start - 1) / bitfield.slot_size] |= mask
|
||||
mask = u32(mask << u32(slot_size - start_offset))
|
||||
output.field[(end - start - 1) / slot_size] |= mask
|
||||
} else if start_offset == 0 {
|
||||
mut mask := u32(0)
|
||||
if end_offset == bitfield.slot_size - 1 {
|
||||
if end_offset == slot_size - 1 {
|
||||
mask = u32(-1)
|
||||
} else {
|
||||
mask = u32(u32(1) << u32(end_offset + 1))
|
||||
mask = mask - u32(1)
|
||||
}
|
||||
output.field[(end - start - 1) / bitfield.slot_size] = (input.field[end_slot] & mask)
|
||||
output.field[(end - start - 1) / slot_size] = (input.field[end_slot] & mask)
|
||||
} else {
|
||||
mut mask := u32(((1 << (end_offset - start_offset + 1)) - 1) << start_offset)
|
||||
mask = input.field[end_slot] & mask
|
||||
mask = u32(mask >> u32(start_offset))
|
||||
output.field[(end - start - 1) / bitfield.slot_size] |= mask
|
||||
output.field[(end - start - 1) / slot_size] |= mask
|
||||
}
|
||||
return output
|
||||
}
|
||||
|
@ -543,13 +543,13 @@ pub fn (instance BitField) reverse() BitField {
|
|||
bitnslots := zbitnslots(size)
|
||||
mut output := new(size)
|
||||
for i := 0; i < (bitnslots - 1); i++ {
|
||||
for j in 0 .. bitfield.slot_size {
|
||||
for j in 0 .. slot_size {
|
||||
if u32(instance.field[i] >> u32(j)) & u32(1) == u32(1) {
|
||||
output.set_bit(size - i * bitfield.slot_size - j - 1)
|
||||
output.set_bit(size - i * slot_size - j - 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
bits_in_last_input_slot := (size - 1) % bitfield.slot_size + 1
|
||||
bits_in_last_input_slot := (size - 1) % slot_size + 1
|
||||
for j in 0 .. bits_in_last_input_slot {
|
||||
if u32(instance.field[bitnslots - 1] >> u32(j)) & u32(1) == u32(1) {
|
||||
output.set_bit(bits_in_last_input_slot - j - 1)
|
||||
|
@ -569,7 +569,7 @@ pub fn (mut instance BitField) resize(new_size int) {
|
|||
}
|
||||
instance.field = field.clone()
|
||||
instance.size = new_size
|
||||
if new_size < old_size && new_size % bitfield.slot_size != 0 {
|
||||
if new_size < old_size && new_size % slot_size != 0 {
|
||||
instance.clear_tail()
|
||||
}
|
||||
}
|
||||
|
@ -604,7 +604,7 @@ pub fn (instance BitField) rotate(offset int) BitField {
|
|||
// clear_tail clears the extra bits that are not part of the bitfield, but yet are allocated
|
||||
@[inline]
|
||||
fn (mut instance BitField) clear_tail() {
|
||||
tail := instance.size % bitfield.slot_size
|
||||
tail := instance.size % slot_size
|
||||
if tail != 0 {
|
||||
// create a mask for the tail
|
||||
mask := u32((1 << tail) - 1)
|
||||
|
@ -616,13 +616,13 @@ fn (mut instance BitField) clear_tail() {
|
|||
// bitmask is the bitmask needed to access a particular bit at offset bitnr
|
||||
@[inline]
|
||||
fn bitmask(bitnr int) u32 {
|
||||
return u32(u32(1) << u32(bitnr % bitfield.slot_size))
|
||||
return u32(u32(1) << u32(bitnr % slot_size))
|
||||
}
|
||||
|
||||
// bitslot is the slot index (i.e. the integer) where a particular bit is located
|
||||
@[inline]
|
||||
fn bitslot(size int) int {
|
||||
return size / bitfield.slot_size
|
||||
return size / slot_size
|
||||
}
|
||||
|
||||
// min returns the minimum of 2 integers; it is here to avoid importing math just for that
|
||||
|
@ -638,5 +638,5 @@ fn min(input1 int, input2 int) int {
|
|||
// zbitnslots returns the minimum number of whole integers, needed to represent a bitfield of size length
|
||||
@[inline]
|
||||
fn zbitnslots(length int) int {
|
||||
return (length - 1) / bitfield.slot_size + 1
|
||||
return (length - 1) / slot_size + 1
|
||||
}
|
||||
|
|
|
@ -50,7 +50,7 @@ pub fn from_rune(r rune) Character {
|
|||
pub fn length_in_characters(p voidptr) int {
|
||||
mut len := 0
|
||||
pc := &Character(p)
|
||||
for unsafe { pc[len] != wchar.zero } {
|
||||
for unsafe { pc[len] != zero } {
|
||||
len++
|
||||
}
|
||||
return len
|
||||
|
@ -114,7 +114,7 @@ pub fn from_string(s string) &Character {
|
|||
for i, r in srunes {
|
||||
result[i] = from_rune(r)
|
||||
}
|
||||
result[srunes.len] = wchar.zero
|
||||
result[srunes.len] = zero
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
|
|
@ -64,26 +64,26 @@ pub fn (cmd Command) help_message() string {
|
|||
help += '\n${cmd.description}\n'
|
||||
}
|
||||
mut abbrev_len := 0
|
||||
mut name_len := cli.min_description_indent_len
|
||||
mut name_len := min_description_indent_len
|
||||
if cmd.posix_mode {
|
||||
for flag in cmd.flags {
|
||||
if flag.abbrev != '' {
|
||||
abbrev_len = max(abbrev_len, flag.abbrev.len + cli.spacing + 1) // + 1 for '-' in front
|
||||
abbrev_len = max(abbrev_len, flag.abbrev.len + spacing + 1) // + 1 for '-' in front
|
||||
}
|
||||
name_len = max(name_len, abbrev_len + flag.name.len + cli.spacing + 2) // + 2 for '--' in front
|
||||
name_len = max(name_len, abbrev_len + flag.name.len + spacing + 2) // + 2 for '--' in front
|
||||
}
|
||||
for command in cmd.commands {
|
||||
name_len = max(name_len, command.name.len + cli.spacing)
|
||||
name_len = max(name_len, command.name.len + spacing)
|
||||
}
|
||||
} else {
|
||||
for flag in cmd.flags {
|
||||
if flag.abbrev != '' {
|
||||
abbrev_len = max(abbrev_len, flag.abbrev.len + cli.spacing + 1) // + 1 for '-' in front
|
||||
abbrev_len = max(abbrev_len, flag.abbrev.len + spacing + 1) // + 1 for '-' in front
|
||||
}
|
||||
name_len = max(name_len, abbrev_len + flag.name.len + cli.spacing + 1) // + 1 for '-' in front
|
||||
name_len = max(name_len, abbrev_len + flag.name.len + spacing + 1) // + 1 for '-' in front
|
||||
}
|
||||
for command in cmd.commands {
|
||||
name_len = max(name_len, command.name.len + cli.spacing)
|
||||
name_len = max(name_len, command.name.len + spacing)
|
||||
}
|
||||
}
|
||||
if cmd.flags.len > 0 {
|
||||
|
@ -102,17 +102,16 @@ pub fn (cmd Command) help_message() string {
|
|||
if flag.required {
|
||||
required = ' (required)'
|
||||
}
|
||||
base_indent := ' '.repeat(cli.base_indent_len)
|
||||
base_indent := ' '.repeat(base_indent_len)
|
||||
description_indent := ' '.repeat(name_len - flag_name.len)
|
||||
help += '${base_indent}${flag_name}${description_indent}' +
|
||||
pretty_description(flag.description + required, cli.base_indent_len + name_len) +
|
||||
'\n'
|
||||
pretty_description(flag.description + required, base_indent_len + name_len) + '\n'
|
||||
}
|
||||
}
|
||||
if cmd.commands.len > 0 {
|
||||
help += '\nCommands:\n'
|
||||
for command in cmd.commands {
|
||||
base_indent := ' '.repeat(cli.base_indent_len)
|
||||
base_indent := ' '.repeat(base_indent_len)
|
||||
description_indent := ' '.repeat(name_len - command.name.len)
|
||||
help += '${base_indent}${command.name}${description_indent}' +
|
||||
pretty_description(command.description, name_len) + '\n'
|
||||
|
|
|
@ -142,13 +142,13 @@ const cp_utf8 = 65001
|
|||
|
||||
// the string.to_wide doesn't work with SetClipboardData, don't know why
|
||||
fn to_wide(text string) C.HGLOBAL {
|
||||
len_required := C.MultiByteToWideChar(clipboard.cp_utf8, C.MB_ERR_INVALID_CHARS, voidptr(text.str),
|
||||
len_required := C.MultiByteToWideChar(cp_utf8, C.MB_ERR_INVALID_CHARS, voidptr(text.str),
|
||||
text.len + 1, C.NULL, 0)
|
||||
buf := C.GlobalAlloc(C.GMEM_MOVEABLE, i64(sizeof(u16)) * len_required)
|
||||
if buf != unsafe { nil } {
|
||||
mut locked := &u16(C.GlobalLock(buf))
|
||||
C.MultiByteToWideChar(clipboard.cp_utf8, C.MB_ERR_INVALID_CHARS, voidptr(text.str),
|
||||
text.len + 1, locked, len_required)
|
||||
C.MultiByteToWideChar(cp_utf8, C.MB_ERR_INVALID_CHARS, voidptr(text.str), text.len + 1,
|
||||
locked, len_required)
|
||||
unsafe {
|
||||
locked[len_required - 1] = u16(0)
|
||||
}
|
||||
|
|
|
@ -390,7 +390,7 @@ fn (mut cb Clipboard) start_listener() {
|
|||
fn (mut cb Clipboard) intern_atoms() {
|
||||
cb.atoms << Atom(4) // XA_ATOM
|
||||
cb.atoms << Atom(31) // XA_STRING
|
||||
for i, name in x11.atom_names {
|
||||
for i, name in atom_names {
|
||||
only_if_exists := if i == int(AtomType.utf8_string) { 1 } else { 0 }
|
||||
cb.atoms << C.XInternAtom(cb.display, &char(name.str), only_if_exists)
|
||||
if i == int(AtomType.utf8_string) && cb.atoms[i] == Atom(0) {
|
||||
|
|
|
@ -12,8 +12,8 @@ fn C.tinfl_decompress_mem_to_heap(source_buf voidptr, source_buf_len usize, out_
|
|||
// NB: this is a low level api, a high level implementation like zlib/gzip should be preferred
|
||||
@[manualfree]
|
||||
pub fn compress(data []u8, flags int) ![]u8 {
|
||||
if u64(data.len) > compress.max_size {
|
||||
return error('data too large (${data.len} > ${compress.max_size})')
|
||||
if u64(data.len) > max_size {
|
||||
return error('data too large (${data.len} > ${max_size})')
|
||||
}
|
||||
mut out_len := usize(0)
|
||||
|
||||
|
@ -21,8 +21,8 @@ pub fn compress(data []u8, flags int) ![]u8 {
|
|||
if address == 0 {
|
||||
return error('compression failed')
|
||||
}
|
||||
if u64(out_len) > compress.max_size {
|
||||
return error('compressed data is too large (${out_len} > ${compress.max_size})')
|
||||
if u64(out_len) > max_size {
|
||||
return error('compressed data is too large (${out_len} > ${max_size})')
|
||||
}
|
||||
return unsafe { address.vbytes(int(out_len)) }
|
||||
}
|
||||
|
@ -37,8 +37,8 @@ pub fn decompress(data []u8, flags int) ![]u8 {
|
|||
if address == 0 {
|
||||
return error('decompression failed')
|
||||
}
|
||||
if u64(out_len) > compress.max_size {
|
||||
return error('decompressed data is too large (${out_len} > ${compress.max_size})')
|
||||
if u64(out_len) > max_size {
|
||||
return error('decompressed data is too large (${out_len} > ${max_size})')
|
||||
}
|
||||
return unsafe { address.vbytes(int(out_len)) }
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@ fn test_gzip() {
|
|||
uncompressed := 'Hello world!'
|
||||
compressed := compress(uncompressed.bytes())!
|
||||
first2 := compressed[0..2]
|
||||
assert first2 != deflate.gzip_magic_numbers
|
||||
assert first2 != gzip_magic_numbers
|
||||
decompressed := decompress(compressed)!
|
||||
assert decompressed == uncompressed.bytes()
|
||||
}
|
||||
|
|
|
@ -70,7 +70,7 @@ pub mut:
|
|||
|
||||
// validate validates the header and returns its details if valid
|
||||
pub fn validate(data []u8, params DecompressParams) !GzipHeader {
|
||||
if data.len < gzip.min_header_length {
|
||||
if data.len < min_header_length {
|
||||
return error('data is too short, not gzip compressed?')
|
||||
} else if data[0] != 0x1f || data[1] != 0x8b {
|
||||
return error('wrong magic numbers, not gzip compressed?')
|
||||
|
@ -83,7 +83,7 @@ pub fn validate(data []u8, params DecompressParams) !GzipHeader {
|
|||
// correctly, so we dont accidently decompress something that belongs
|
||||
// to the header
|
||||
|
||||
if data[3] & gzip.reserved_bits > 0 {
|
||||
if data[3] & reserved_bits > 0 {
|
||||
// rfc 1952 2.3.1.2 Compliance
|
||||
// A compliant decompressor must give an error indication if any
|
||||
// reserved bit is non-zero, since such a bit could indicate the
|
||||
|
@ -92,12 +92,12 @@ pub fn validate(data []u8, params DecompressParams) !GzipHeader {
|
|||
return error('reserved flags are set, unsupported field detected')
|
||||
}
|
||||
|
||||
if data[3] & gzip.fextra > 0 {
|
||||
if data[3] & fextra > 0 {
|
||||
xlen := data[header.length]
|
||||
header.extra = data[header.length + 1..header.length + 1 + xlen]
|
||||
header.length += xlen + 1
|
||||
}
|
||||
if data[3] & gzip.fname > 0 {
|
||||
if data[3] & fname > 0 {
|
||||
// filename is zero-terminated, so skip until we hit a zero byte
|
||||
for header.length < data.len && data[header.length] != 0x00 {
|
||||
header.filename << data[header.length]
|
||||
|
@ -105,7 +105,7 @@ pub fn validate(data []u8, params DecompressParams) !GzipHeader {
|
|||
}
|
||||
header.length++
|
||||
}
|
||||
if data[3] & gzip.fcomment > 0 {
|
||||
if data[3] & fcomment > 0 {
|
||||
// comment is zero-terminated, so skip until we hit a zero byte
|
||||
for header.length < data.len && data[header.length] != 0x00 {
|
||||
header.comment << data[header.length]
|
||||
|
@ -113,7 +113,7 @@ pub fn validate(data []u8, params DecompressParams) !GzipHeader {
|
|||
}
|
||||
header.length++
|
||||
}
|
||||
if data[3] & gzip.fhcrc > 0 {
|
||||
if data[3] & fhcrc > 0 {
|
||||
if header.length + 12 > data.len {
|
||||
return error('data too short')
|
||||
}
|
||||
|
|
|
@ -431,10 +431,10 @@ pub:
|
|||
// extra decompression parameters can be set by `params`
|
||||
// Example: decompressed := zstd.decompress(b)!
|
||||
pub fn decompress(data []u8, params DecompressParams) ![]u8 {
|
||||
dst_capacity := C.ZSTD_getFrameContentSize(data.data, zstd.zstd_frame_header_size_max)
|
||||
if dst_capacity == zstd.zstd_content_size_unknown {
|
||||
dst_capacity := C.ZSTD_getFrameContentSize(data.data, zstd_frame_header_size_max)
|
||||
if dst_capacity == zstd_content_size_unknown {
|
||||
return error('The size cannot be determined, try use streaming mode to decompress data?')
|
||||
} else if dst_capacity == zstd.zstd_content_size_error {
|
||||
} else if dst_capacity == zstd_content_size_error {
|
||||
return error('An error occurred (e.g. invalid magic number, srcSize too small)')
|
||||
} else if dst_capacity == 0 {
|
||||
return error('The frame is valid but empty')
|
||||
|
@ -543,7 +543,7 @@ pub fn store_array[T](fname string, array []T, params CompressParams) ! {
|
|||
fout.close()
|
||||
}
|
||||
|
||||
mut buf_out := []u8{len: zstd.buf_out_size}
|
||||
mut buf_out := []u8{len: buf_out_size}
|
||||
mut input := &ZSTD_inBuffer{}
|
||||
mut output := &ZSTD_outBuffer{}
|
||||
mut remaining := usize(1)
|
||||
|
@ -554,7 +554,7 @@ pub fn store_array[T](fname string, array []T, params CompressParams) ! {
|
|||
input.size = 8
|
||||
input.pos = 0
|
||||
output.dst = buf_out.data
|
||||
output.size = zstd.buf_out_size
|
||||
output.size = buf_out_size
|
||||
output.pos = 0
|
||||
remaining = cctx.compress_stream2(output, input, .zstd_e_flush)
|
||||
check_zstd(remaining)!
|
||||
|
@ -564,12 +564,12 @@ pub fn store_array[T](fname string, array []T, params CompressParams) ! {
|
|||
input.size = usize(array.len * sizeof(T))
|
||||
input.pos = 0
|
||||
output.dst = buf_out.data
|
||||
output.size = zstd.buf_out_size
|
||||
output.size = buf_out_size
|
||||
output.pos = 0
|
||||
remaining = 1
|
||||
for remaining != 0 {
|
||||
output.dst = buf_out.data
|
||||
output.size = zstd.buf_out_size
|
||||
output.size = buf_out_size
|
||||
output.pos = 0
|
||||
remaining = cctx.compress_stream2(output, input, .zstd_e_end)
|
||||
check_zstd(remaining)!
|
||||
|
@ -587,7 +587,7 @@ pub fn load_array[T](fname string, params DecompressParams) ![]T {
|
|||
fin.close()
|
||||
}
|
||||
|
||||
mut buf_in := []u8{len: zstd.buf_in_size}
|
||||
mut buf_in := []u8{len: buf_in_size}
|
||||
mut len_buf := []u8{len: 8}
|
||||
mut input := &ZSTD_inBuffer{}
|
||||
mut output := &ZSTD_outBuffer{}
|
||||
|
|
|
@ -5,7 +5,7 @@ import os
|
|||
const samples_folder = os.join_path(os.dir(@FILE), 'samples')
|
||||
|
||||
fn s(fname string) string {
|
||||
return os.join_path(zstd.samples_folder, fname)
|
||||
return os.join_path(samples_folder, fname)
|
||||
}
|
||||
|
||||
fn test_zstd() {
|
||||
|
|
|
@ -118,7 +118,7 @@ pub fn (mut octx OneContext) run_two_contexts(mut ctx1 context.Context, mut ctx2
|
|||
c2done := ctx2.done()
|
||||
select {
|
||||
_ := <-octx_cancel_done {
|
||||
octx.cancel(onecontext.canceled)
|
||||
octx.cancel(canceled)
|
||||
}
|
||||
_ := <-c1done {
|
||||
octx.cancel(ctx1.err())
|
||||
|
@ -136,7 +136,7 @@ pub fn (mut octx OneContext) run_multiple_contexts(mut ctx context.Context) {
|
|||
cdone := ctx.done()
|
||||
select {
|
||||
_ := <-octx_cancel_done {
|
||||
octx.cancel(onecontext.canceled)
|
||||
octx.cancel(canceled)
|
||||
}
|
||||
_ := <-cdone {
|
||||
octx.cancel(ctx.err())
|
||||
|
|
|
@ -16,7 +16,7 @@ pub const block_size = 16
|
|||
// handle only one block of data at a time. In most cases, you
|
||||
// probably want to encrypt and decrypt using [[AesCbc](#AesCbc)]
|
||||
struct AesCipher {
|
||||
block_size int = aes.block_size
|
||||
block_size int = block_size
|
||||
mut:
|
||||
enc []u32
|
||||
dec []u32
|
||||
|
@ -55,7 +55,7 @@ pub fn new_cipher(key []u8) cipher.Block {
|
|||
|
||||
// block_size returns the block size of the checksum in bytes.
|
||||
pub fn (c &AesCipher) block_size() int {
|
||||
return aes.block_size
|
||||
return block_size
|
||||
}
|
||||
|
||||
// encrypt encrypts the first block of data in `src` to `dst`.
|
||||
|
@ -63,14 +63,14 @@ pub fn (c &AesCipher) block_size() int {
|
|||
// NOTE: `dst` and `src` must both be pre-allocated to the correct length.
|
||||
// NOTE: `dst` and `src` may be the same (overlapping entirely).
|
||||
pub fn (c &AesCipher) encrypt(mut dst []u8, src []u8) {
|
||||
if src.len < aes.block_size {
|
||||
if src.len < block_size {
|
||||
panic('crypto.aes: input not full block')
|
||||
}
|
||||
if dst.len < aes.block_size {
|
||||
if dst.len < block_size {
|
||||
panic('crypto.aes: output not full block')
|
||||
}
|
||||
// if subtle.inexact_overlap(dst[:block_size], src[:block_size]) {
|
||||
if subtle.inexact_overlap(dst[..aes.block_size], src[..aes.block_size]) {
|
||||
if subtle.inexact_overlap(dst[..block_size], src[..block_size]) {
|
||||
panic('crypto.aes: invalid buffer overlap')
|
||||
}
|
||||
// for now use generic version
|
||||
|
@ -82,13 +82,13 @@ pub fn (c &AesCipher) encrypt(mut dst []u8, src []u8) {
|
|||
// NOTE: `dst` and `src` must both be pre-allocated to the correct length.
|
||||
// NOTE: `dst` and `src` may be the same (overlapping entirely).
|
||||
pub fn (c &AesCipher) decrypt(mut dst []u8, src []u8) {
|
||||
if src.len < aes.block_size {
|
||||
if src.len < block_size {
|
||||
panic('crypto.aes: input not full block')
|
||||
}
|
||||
if dst.len < aes.block_size {
|
||||
if dst.len < block_size {
|
||||
panic('crypto.aes: output not full block')
|
||||
}
|
||||
if subtle.inexact_overlap(dst[..aes.block_size], src[..aes.block_size]) {
|
||||
if subtle.inexact_overlap(dst[..block_size], src[..block_size]) {
|
||||
panic('crypto.aes: invalid buffer overlap')
|
||||
}
|
||||
// for now use generic version
|
||||
|
|
|
@ -3,7 +3,7 @@ module bcrypt
|
|||
const alphabet = './ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
|
||||
|
||||
fn char64(c u8) u8 {
|
||||
for i, ch in bcrypt.alphabet {
|
||||
for i, ch in alphabet {
|
||||
if ch == c {
|
||||
return u8(i)
|
||||
}
|
||||
|
@ -69,28 +69,28 @@ fn base64_encode(data []u8) string {
|
|||
for src_index < data.len {
|
||||
mut c1 := data[src_index]
|
||||
src_index += 1
|
||||
result << bcrypt.alphabet[c1 >> 2]
|
||||
result << alphabet[c1 >> 2]
|
||||
c1 = (c1 & 0x03) << 4
|
||||
if src_index >= data.len {
|
||||
result << bcrypt.alphabet[c1]
|
||||
result << alphabet[c1]
|
||||
break
|
||||
}
|
||||
|
||||
mut c2 := data[src_index]
|
||||
src_index += 1
|
||||
c1 |= (c2 >> 4) & 0x0f
|
||||
result << bcrypt.alphabet[c1]
|
||||
result << alphabet[c1]
|
||||
c1 = (c2 & 0x0f) << 2
|
||||
if src_index >= data.len {
|
||||
result << bcrypt.alphabet[c1]
|
||||
result << alphabet[c1]
|
||||
break
|
||||
}
|
||||
|
||||
c2 = data[src_index]
|
||||
src_index += 1
|
||||
c1 |= (c2 >> 6) & 0x03
|
||||
result << bcrypt.alphabet[c1]
|
||||
result << bcrypt.alphabet[c2 & 0x3f]
|
||||
result << alphabet[c1]
|
||||
result << alphabet[c2 & 0x3f]
|
||||
}
|
||||
|
||||
return result.bytestr()
|
||||
|
|
|
@ -67,21 +67,21 @@ pub fn compare_hash_and_password(password []u8, hashed_password []u8) ! {
|
|||
|
||||
// generate_salt generate a string to be treated as a salt.
|
||||
pub fn generate_salt() string {
|
||||
randbytes := rand.bytes(bcrypt.salt_length) or { panic(err) }
|
||||
randbytes := rand.bytes(salt_length) or { panic(err) }
|
||||
return randbytes.bytestr()
|
||||
}
|
||||
|
||||
// new_from_password converting from password to a Hashed struct with bcrypt.
|
||||
fn new_from_password(password []u8, cost int) !&Hashed {
|
||||
mut cost_ := cost
|
||||
if cost < bcrypt.min_cost {
|
||||
cost_ = bcrypt.default_cost
|
||||
if cost < min_cost {
|
||||
cost_ = default_cost
|
||||
}
|
||||
mut p := &Hashed{}
|
||||
p.major = bcrypt.major_version
|
||||
p.minor = bcrypt.minor_version
|
||||
p.major = major_version
|
||||
p.minor = minor_version
|
||||
|
||||
if cost_ < bcrypt.min_cost || cost_ > bcrypt.max_cost {
|
||||
if cost_ < min_cost || cost_ > max_cost {
|
||||
return error('invalid cost')
|
||||
}
|
||||
p.cost = cost_
|
||||
|
@ -96,7 +96,7 @@ fn new_from_password(password []u8, cost int) !&Hashed {
|
|||
// new_from_hash converting from hashed data to a Hashed struct.
|
||||
fn new_from_hash(hashed_secret []u8) !&Hashed {
|
||||
mut tmp := hashed_secret.clone()
|
||||
if tmp.len < bcrypt.min_hash_size {
|
||||
if tmp.len < min_hash_size {
|
||||
return error('hash to short')
|
||||
}
|
||||
|
||||
|
@ -107,15 +107,15 @@ fn new_from_hash(hashed_secret []u8) !&Hashed {
|
|||
n = p.decode_cost(tmp) or { return err }
|
||||
tmp = tmp[n..].clone()
|
||||
|
||||
p.salt = tmp[..bcrypt.encoded_salt_size].clone()
|
||||
p.hash = tmp[bcrypt.encoded_salt_size..].clone()
|
||||
p.salt = tmp[..encoded_salt_size].clone()
|
||||
p.hash = tmp[encoded_salt_size..].clone()
|
||||
|
||||
return p
|
||||
}
|
||||
|
||||
// bcrypt hashing passwords.
|
||||
fn bcrypt(password []u8, cost int, salt []u8) ![]u8 {
|
||||
mut cipher_data := bcrypt.magic_cipher_data.clone()
|
||||
mut cipher_data := magic_cipher_data.clone()
|
||||
mut bf := expensive_blowfish_setup(password, u32(cost), salt) or { return err }
|
||||
|
||||
for i := 0; i < 24; i += 8 {
|
||||
|
@ -123,7 +123,7 @@ fn bcrypt(password []u8, cost int, salt []u8) ![]u8 {
|
|||
bf.encrypt(mut cipher_data[i..i + 8], cipher_data[i..i + 8])
|
||||
}
|
||||
}
|
||||
hash := base64_encode(cipher_data[..bcrypt.max_crypted_hash_size])
|
||||
hash := base64_encode(cipher_data[..max_crypted_hash_size])
|
||||
return hash.bytes()
|
||||
}
|
||||
|
||||
|
@ -165,9 +165,9 @@ fn (mut h Hashed) hash_u8() []u8 {
|
|||
arr[n] = `$`
|
||||
n++
|
||||
copy(mut arr[n..], h.salt)
|
||||
n += bcrypt.encoded_salt_size
|
||||
n += encoded_salt_size
|
||||
copy(mut arr[n..], h.hash)
|
||||
n += bcrypt.encoded_hash_size
|
||||
n += encoded_hash_size
|
||||
res := arr[..n].clone()
|
||||
return res
|
||||
}
|
||||
|
@ -177,8 +177,8 @@ fn (mut h Hashed) decode_version(sbytes []u8) !int {
|
|||
if sbytes[0] != `$` {
|
||||
return error("bcrypt hashes must start with '$'")
|
||||
}
|
||||
if sbytes[1] != bcrypt.major_version[0] {
|
||||
return error('bcrypt algorithm version ${bcrypt.major_version}')
|
||||
if sbytes[1] != major_version[0] {
|
||||
return error('bcrypt algorithm version ${major_version}')
|
||||
}
|
||||
h.major = sbytes[1].ascii_str()
|
||||
mut n := 3
|
||||
|
@ -199,7 +199,7 @@ fn (mut h Hashed) decode_cost(sbytes []u8) !int {
|
|||
|
||||
// check_cost check for reasonable quantities.
|
||||
fn check_cost(cost int) ! {
|
||||
if cost < bcrypt.min_cost || cost > bcrypt.max_cost {
|
||||
if cost < min_cost || cost > max_cost {
|
||||
return error('invalid cost')
|
||||
}
|
||||
}
|
||||
|
|
|
@ -77,42 +77,42 @@ pub fn (d Digest) str() string {
|
|||
|
||||
// new512 initializes the digest structure for a Blake2b 512 bit hash
|
||||
pub fn new512() !&Digest {
|
||||
return new_digest(blake2b.size512, []u8{})!
|
||||
return new_digest(size512, []u8{})!
|
||||
}
|
||||
|
||||
// new_pmac512 initializes the digest structure for a Blake2b 512 bit prefix MAC
|
||||
pub fn new_pmac512(key []u8) !&Digest {
|
||||
return new_digest(blake2b.size512, key)!
|
||||
return new_digest(size512, key)!
|
||||
}
|
||||
|
||||
// new384 initializes the digest structure for a Blake2b 384 bit hash
|
||||
pub fn new384() !&Digest {
|
||||
return new_digest(blake2b.size384, []u8{})!
|
||||
return new_digest(size384, []u8{})!
|
||||
}
|
||||
|
||||
// new_pmac384 initializes the digest structure for a Blake2b 384 bit prefix MAC
|
||||
pub fn new_pmac384(key []u8) !&Digest {
|
||||
return new_digest(blake2b.size384, key)!
|
||||
return new_digest(size384, key)!
|
||||
}
|
||||
|
||||
// new256 initializes the digest structure for a Blake2b 256 bit hash
|
||||
pub fn new256() !&Digest {
|
||||
return new_digest(blake2b.size256, []u8{})!
|
||||
return new_digest(size256, []u8{})!
|
||||
}
|
||||
|
||||
// new_pmac256 initializes the digest structure for a Blake2b 256 bit prefix MAC
|
||||
pub fn new_pmac256(key []u8) !&Digest {
|
||||
return new_digest(blake2b.size256, key)!
|
||||
return new_digest(size256, key)!
|
||||
}
|
||||
|
||||
// new160 initializes the digest structure for a Blake2b 160 bit hash
|
||||
pub fn new160() !&Digest {
|
||||
return new_digest(blake2b.size160, []u8{})!
|
||||
return new_digest(size160, []u8{})!
|
||||
}
|
||||
|
||||
// new_pmac160 initializes the digest structure for a Blake2b 160 bit prefix MAC
|
||||
pub fn new_pmac160(key []u8) !&Digest {
|
||||
return new_digest(blake2b.size160, key)!
|
||||
return new_digest(size160, key)!
|
||||
}
|
||||
|
||||
struct HashSizeError {
|
||||
|
@ -121,7 +121,7 @@ struct HashSizeError {
|
|||
}
|
||||
|
||||
fn (err HashSizeError) msg() string {
|
||||
return 'Hash size ${err.size} must be between 1 and ${blake2b.size512}'
|
||||
return 'Hash size ${err.size} must be between 1 and ${size512}'
|
||||
}
|
||||
|
||||
struct KeySizeError {
|
||||
|
@ -130,7 +130,7 @@ struct KeySizeError {
|
|||
}
|
||||
|
||||
fn (err KeySizeError) msg() string {
|
||||
return 'Key size ${err.size} must be between 0 and ${blake2b.size512}'
|
||||
return 'Key size ${err.size} must be between 0 and ${size512}'
|
||||
}
|
||||
|
||||
struct InputBufferSizeError {
|
||||
|
@ -139,7 +139,7 @@ struct InputBufferSizeError {
|
|||
}
|
||||
|
||||
fn (err InputBufferSizeError) msg() string {
|
||||
return 'The input buffer size ${err.size} .must be between 0 and ${blake2b.block_size}'
|
||||
return 'The input buffer size ${err.size} .must be between 0 and ${block_size}'
|
||||
}
|
||||
|
||||
// new_digest creates an initialized digest structure based on
|
||||
|
@ -152,20 +152,20 @@ fn (err InputBufferSizeError) msg() string {
|
|||
// key is used for just generating a hash. A key of 1 to
|
||||
// 64 bytes can be used for generating a prefix MAC.
|
||||
pub fn new_digest(hash_size u8, key []u8) !&Digest {
|
||||
if hash_size < 1 || hash_size > blake2b.size512 {
|
||||
if hash_size < 1 || hash_size > size512 {
|
||||
return HashSizeError{
|
||||
size: hash_size
|
||||
}
|
||||
}
|
||||
|
||||
if key.len < 0 || key.len > blake2b.size512 {
|
||||
if key.len < 0 || key.len > size512 {
|
||||
return KeySizeError{
|
||||
size: i32(key.len)
|
||||
}
|
||||
}
|
||||
|
||||
mut d := Digest{
|
||||
h: blake2b.iv.clone()
|
||||
h: iv.clone()
|
||||
t: unsigned.uint128_zero
|
||||
hash_size: hash_size
|
||||
}
|
||||
|
@ -178,7 +178,7 @@ pub fn new_digest(hash_size u8, key []u8) !&Digest {
|
|||
d.input_buffer.clear()
|
||||
d.input_buffer << key
|
||||
|
||||
pad_length := blake2b.block_size - key.len
|
||||
pad_length := block_size - key.len
|
||||
for _ in 0 .. pad_length {
|
||||
d.input_buffer << 0
|
||||
}
|
||||
|
@ -209,7 +209,7 @@ pub fn new_digest(hash_size u8, key []u8) !&Digest {
|
|||
fn (mut d Digest) move_input_to_message_blocks() ! {
|
||||
// the number of bytes in the input buffer
|
||||
// should never exceed the block size.
|
||||
if d.input_buffer.len < 0 || d.input_buffer.len > blake2b.block_size {
|
||||
if d.input_buffer.len < 0 || d.input_buffer.len > block_size {
|
||||
return InputBufferSizeError{
|
||||
size: i32(d.input_buffer.len)
|
||||
}
|
||||
|
@ -219,8 +219,8 @@ fn (mut d Digest) move_input_to_message_blocks() ! {
|
|||
d.t = d.t.add_64(u64(d.input_buffer.len))
|
||||
|
||||
// pad the input buffer if necessary
|
||||
if d.input_buffer.len < blake2b.block_size {
|
||||
pad_length := blake2b.block_size - d.input_buffer.len
|
||||
if d.input_buffer.len < block_size {
|
||||
pad_length := block_size - d.input_buffer.len
|
||||
|
||||
for _ in 0 .. pad_length {
|
||||
d.input_buffer << 0
|
||||
|
@ -249,7 +249,7 @@ pub fn (mut d Digest) write(data []u8) ! {
|
|||
// if the input buffer is already full,
|
||||
// process the existing input bytes first.
|
||||
// this is not the final input.
|
||||
if d.input_buffer.len >= blake2b.block_size {
|
||||
if d.input_buffer.len >= block_size {
|
||||
d.move_input_to_message_blocks()!
|
||||
d.f(false)
|
||||
}
|
||||
|
@ -257,7 +257,7 @@ pub fn (mut d Digest) write(data []u8) ! {
|
|||
// add data to the input buffer until you
|
||||
// run out of space in the input buffer or
|
||||
// run out of data, whichever comes first.
|
||||
empty_space := blake2b.block_size - d.input_buffer.len
|
||||
empty_space := block_size - d.input_buffer.len
|
||||
mut remaining_data := unsafe { data[..] }
|
||||
|
||||
if empty_space >= data.len {
|
||||
|
@ -278,7 +278,7 @@ pub fn (mut d Digest) write(data []u8) ! {
|
|||
// process the data in block size amounts until
|
||||
// all the data has been processed
|
||||
for remaining_data.len > 0 {
|
||||
if blake2b.block_size >= remaining_data.len {
|
||||
if block_size >= remaining_data.len {
|
||||
// running out of data
|
||||
// just add it to the input buffer and return
|
||||
d.input_buffer << remaining_data
|
||||
|
@ -287,8 +287,8 @@ pub fn (mut d Digest) write(data []u8) ! {
|
|||
|
||||
// add block size bytes to the input buffer
|
||||
// and process it
|
||||
d.input_buffer << remaining_data[..blake2b.block_size]
|
||||
remaining_data = unsafe { remaining_data[blake2b.block_size..] }
|
||||
d.input_buffer << remaining_data[..block_size]
|
||||
remaining_data = unsafe { remaining_data[block_size..] }
|
||||
|
||||
d.move_input_to_message_blocks()!
|
||||
d.f(false)
|
||||
|
|
|
@ -106,18 +106,18 @@ fn test_mixing_function_g() {
|
|||
}
|
||||
|
||||
for i in 0 .. 16 {
|
||||
assert v[i] == blake2b.expected_v_initial_results[i], 'expeccted expected_v_initial_results[${i}] ${blake2b.expected_v_initial_results[i]:016x} actual v[${i}] ${v[i]:016x}'
|
||||
assert v[i] == expected_v_initial_results[i], 'expeccted expected_v_initial_results[${i}] ${expected_v_initial_results[i]:016x} actual v[${i}] ${v[i]:016x}'
|
||||
}
|
||||
|
||||
for i in 0 .. 16 {
|
||||
assert d.m[i] == blake2b.expected_m_results[i], 'expeccted expected_m_results[${i}] ${blake2b.expected_m_results[i]:016x} actual d.m[${i}] ${d.m[i]:016x}'
|
||||
assert d.m[i] == expected_m_results[i], 'expeccted expected_m_results[${i}] ${expected_m_results[i]:016x} actual d.m[${i}] ${d.m[i]:016x}'
|
||||
}
|
||||
|
||||
for r in 0 .. blake2b.expected_v_results.len {
|
||||
for r in 0 .. expected_v_results.len {
|
||||
d.mixing_round(mut v, sigma[r % 10])
|
||||
|
||||
for i in 0 .. 16 {
|
||||
assert v[i] == blake2b.expected_v_results[r][i], 'expeccted expected_v_results[${r}][${i}] ${blake2b.expected_v_results[r][i]:016x} actual v[${i}] ${v[i]:016x}'
|
||||
assert v[i] == expected_v_results[r][i], 'expeccted expected_v_results[${r}][${i}] ${expected_v_results[r][i]:016x} actual v[${i}] ${v[i]:016x}'
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -131,6 +131,6 @@ fn test_mixing_function_g() {
|
|||
d.h[7] = d.h[7] ^ v[7] ^ v[15]
|
||||
|
||||
for i in 0 .. 8 {
|
||||
assert d.h[i] == blake2b.expected_h_results[i], 'expeccted expected_h_results[${i}] ${blake2b.expected_h_results[i]:016x} actual d.h[${i}] ${d.h[i]:016x}'
|
||||
assert d.h[i] == expected_h_results[i], 'expeccted expected_h_results[${i}] ${expected_h_results[i]:016x} actual d.h[${i}] ${d.h[i]:016x}'
|
||||
}
|
||||
}
|
||||
|
|
|
@ -76,42 +76,42 @@ pub fn (d Digest) str() string {
|
|||
|
||||
// new256 initializes the digest structure for a Blake2s 256 bit hash
|
||||
pub fn new256() !&Digest {
|
||||
return new_digest(blake2s.size256, []u8{})!
|
||||
return new_digest(size256, []u8{})!
|
||||
}
|
||||
|
||||
// new_pmac256 initializes the digest structure for a Blake2s 256 bit prefix MAC
|
||||
pub fn new_pmac256(key []u8) !&Digest {
|
||||
return new_digest(blake2s.size256, key)!
|
||||
return new_digest(size256, key)!
|
||||
}
|
||||
|
||||
// new224 initializes the digest structure for a Blake2s 224 bit hash
|
||||
pub fn new224() !&Digest {
|
||||
return new_digest(blake2s.size224, []u8{})!
|
||||
return new_digest(size224, []u8{})!
|
||||
}
|
||||
|
||||
// new_pmac224 initializes the digest structure for a Blake2s 224 bit prefix MAC
|
||||
pub fn new_pmac224(key []u8) !&Digest {
|
||||
return new_digest(blake2s.size224, key)!
|
||||
return new_digest(size224, key)!
|
||||
}
|
||||
|
||||
// new160 initializes the digest structure for a Blake2s 160 bit hash
|
||||
pub fn new160() !&Digest {
|
||||
return new_digest(blake2s.size160, []u8{})!
|
||||
return new_digest(size160, []u8{})!
|
||||
}
|
||||
|
||||
// new_pmac160 initializes the digest structure for a Blake2s 160 bit prefix MAC
|
||||
pub fn new_pmac160(key []u8) !&Digest {
|
||||
return new_digest(blake2s.size160, key)!
|
||||
return new_digest(size160, key)!
|
||||
}
|
||||
|
||||
// new126 initializes the digest structure for a Blake2s 128 bit hash
|
||||
pub fn new128() !&Digest {
|
||||
return new_digest(blake2s.size128, []u8{})!
|
||||
return new_digest(size128, []u8{})!
|
||||
}
|
||||
|
||||
// new_pmac128 initializes the digest structure for a Blake2s 128 bit prefix MAC
|
||||
pub fn new_pmac128(key []u8) !&Digest {
|
||||
return new_digest(blake2s.size128, key)!
|
||||
return new_digest(size128, key)!
|
||||
}
|
||||
|
||||
struct HashSizeError {
|
||||
|
@ -120,7 +120,7 @@ struct HashSizeError {
|
|||
}
|
||||
|
||||
fn (err HashSizeError) msg() string {
|
||||
return 'Hash size ${err.size} must be between 1 and ${blake2s.size256}'
|
||||
return 'Hash size ${err.size} must be between 1 and ${size256}'
|
||||
}
|
||||
|
||||
struct KeySizeError {
|
||||
|
@ -129,7 +129,7 @@ struct KeySizeError {
|
|||
}
|
||||
|
||||
fn (err KeySizeError) msg() string {
|
||||
return 'Key size ${err.size} must be between 0 and ${blake2s.size256}'
|
||||
return 'Key size ${err.size} must be between 0 and ${size256}'
|
||||
}
|
||||
|
||||
struct InputBufferSizeError {
|
||||
|
@ -138,7 +138,7 @@ struct InputBufferSizeError {
|
|||
}
|
||||
|
||||
fn (err InputBufferSizeError) msg() string {
|
||||
return 'The input buffer size ${err.size} .must be between 0 and ${blake2s.block_size}'
|
||||
return 'The input buffer size ${err.size} .must be between 0 and ${block_size}'
|
||||
}
|
||||
|
||||
// new_digest creates an initialized digest structure based on
|
||||
|
@ -151,20 +151,20 @@ fn (err InputBufferSizeError) msg() string {
|
|||
// key is used for just generating a hash. A key of 1 to
|
||||
// 32 bytes can be used for generating a prefix MAC.
|
||||
pub fn new_digest(hash_size u8, key []u8) !&Digest {
|
||||
if hash_size < 1 || hash_size > blake2s.size256 {
|
||||
if hash_size < 1 || hash_size > size256 {
|
||||
return HashSizeError{
|
||||
size: hash_size
|
||||
}
|
||||
}
|
||||
|
||||
if key.len < 0 || key.len > blake2s.size256 {
|
||||
if key.len < 0 || key.len > size256 {
|
||||
return KeySizeError{
|
||||
size: i32(key.len)
|
||||
}
|
||||
}
|
||||
|
||||
mut d := Digest{
|
||||
h: blake2s.iv.clone()
|
||||
h: iv.clone()
|
||||
t: 0
|
||||
hash_size: hash_size
|
||||
}
|
||||
|
@ -177,7 +177,7 @@ pub fn new_digest(hash_size u8, key []u8) !&Digest {
|
|||
d.input_buffer.clear()
|
||||
d.input_buffer << key
|
||||
|
||||
pad_length := blake2s.block_size - key.len
|
||||
pad_length := block_size - key.len
|
||||
for _ in 0 .. pad_length {
|
||||
d.input_buffer << 0
|
||||
}
|
||||
|
@ -208,7 +208,7 @@ pub fn new_digest(hash_size u8, key []u8) !&Digest {
|
|||
fn (mut d Digest) move_input_to_message_blocks() ! {
|
||||
// the number of bytes in the input buffer
|
||||
// should never exceed the block size.
|
||||
if d.input_buffer.len < 0 || d.input_buffer.len > blake2s.block_size {
|
||||
if d.input_buffer.len < 0 || d.input_buffer.len > block_size {
|
||||
return InputBufferSizeError{
|
||||
size: i32(d.input_buffer.len)
|
||||
}
|
||||
|
@ -218,8 +218,8 @@ fn (mut d Digest) move_input_to_message_blocks() ! {
|
|||
d.t += u64(d.input_buffer.len)
|
||||
|
||||
// pad the input buffer if necessary
|
||||
if d.input_buffer.len < blake2s.block_size {
|
||||
pad_length := blake2s.block_size - d.input_buffer.len
|
||||
if d.input_buffer.len < block_size {
|
||||
pad_length := block_size - d.input_buffer.len
|
||||
|
||||
for _ in 0 .. pad_length {
|
||||
d.input_buffer << 0
|
||||
|
@ -248,7 +248,7 @@ pub fn (mut d Digest) write(data []u8) ! {
|
|||
// if the input buffer is already full,
|
||||
// process the existing input bytes first.
|
||||
// this is not the final input.
|
||||
if d.input_buffer.len >= blake2s.block_size {
|
||||
if d.input_buffer.len >= block_size {
|
||||
d.move_input_to_message_blocks()!
|
||||
d.f(false)
|
||||
}
|
||||
|
@ -256,7 +256,7 @@ pub fn (mut d Digest) write(data []u8) ! {
|
|||
// add data to the input buffer until you
|
||||
// run out of space in the input buffer or
|
||||
// run out of data, whichever comes first.
|
||||
empty_space := blake2s.block_size - d.input_buffer.len
|
||||
empty_space := block_size - d.input_buffer.len
|
||||
mut remaining_data := unsafe { data[..] }
|
||||
|
||||
if empty_space >= data.len {
|
||||
|
@ -277,7 +277,7 @@ pub fn (mut d Digest) write(data []u8) ! {
|
|||
// process the data in block size amounts until
|
||||
// all the data has been processed
|
||||
for remaining_data.len > 0 {
|
||||
if blake2s.block_size >= remaining_data.len {
|
||||
if block_size >= remaining_data.len {
|
||||
// running out of data
|
||||
// just add it to the input buffer and return
|
||||
d.input_buffer << remaining_data
|
||||
|
@ -286,8 +286,8 @@ pub fn (mut d Digest) write(data []u8) ! {
|
|||
|
||||
// add block size bytes to the input buffer
|
||||
// and process it
|
||||
d.input_buffer << remaining_data[..blake2s.block_size]
|
||||
remaining_data = unsafe { remaining_data[blake2s.block_size..] }
|
||||
d.input_buffer << remaining_data[..block_size]
|
||||
remaining_data = unsafe { remaining_data[block_size..] }
|
||||
|
||||
d.move_input_to_message_blocks()!
|
||||
d.f(false)
|
||||
|
|
|
@ -83,18 +83,18 @@ fn test_mixing_function_g() {
|
|||
}
|
||||
|
||||
for i in 0 .. 16 {
|
||||
assert v[i] == blake2s.expected_v_initial_results[i], 'expeccted expected_v_initial_results[${i}] ${blake2s.expected_v_initial_results[i]:08x} actual v[${i}] ${v[i]:08x}'
|
||||
assert v[i] == expected_v_initial_results[i], 'expeccted expected_v_initial_results[${i}] ${expected_v_initial_results[i]:08x} actual v[${i}] ${v[i]:08x}'
|
||||
}
|
||||
|
||||
for i in 0 .. 16 {
|
||||
assert d.m[i] == blake2s.expected_m_results[i], 'expeccted expected_m_results[${i}] ${blake2s.expected_m_results[i]:08x} actual d.m[${i}] ${d.m[i]:08x}'
|
||||
assert d.m[i] == expected_m_results[i], 'expeccted expected_m_results[${i}] ${expected_m_results[i]:08x} actual d.m[${i}] ${d.m[i]:08x}'
|
||||
}
|
||||
|
||||
for r in 0 .. blake2s.expected_v_results.len {
|
||||
for r in 0 .. expected_v_results.len {
|
||||
d.mixing_round(mut v, sigma[r])
|
||||
|
||||
for i in 0 .. 16 {
|
||||
assert v[i] == blake2s.expected_v_results[r][i], 'expeccted expected_v_results[${r}][${i}] ${blake2s.expected_v_results[r][i]:08x} actual v[${i}] ${v[i]:08x}'
|
||||
assert v[i] == expected_v_results[r][i], 'expeccted expected_v_results[${r}][${i}] ${expected_v_results[r][i]:08x} actual v[${i}] ${v[i]:08x}'
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -108,6 +108,6 @@ fn test_mixing_function_g() {
|
|||
d.h[7] = d.h[7] ^ v[7] ^ v[15]
|
||||
|
||||
for i in 0 .. 8 {
|
||||
assert d.h[i] == blake2s.expected_h_results[i], 'expeccted expected_h_results[${i}] ${blake2s.expected_h_results[i]:08x} actual d.h[${i}] ${d.h[i]:08x}'
|
||||
assert d.h[i] == expected_h_results[i], 'expeccted expected_h_results[${i}] ${expected_h_results[i]:08x} actual d.h[${i}] ${d.h[i]:08x}'
|
||||
}
|
||||
}
|
||||
|
|
|
@ -131,7 +131,7 @@ mut:
|
|||
|
||||
// Digest.new_hash initializes a Digest structure for a Blake3 hash
|
||||
pub fn Digest.new_hash() !Digest {
|
||||
return Digest.new(blake3.iv, 0)
|
||||
return Digest.new(iv, 0)
|
||||
}
|
||||
|
||||
// Digest.new_keyed_hash initializes a Digest structure for a Blake3 keyed hash
|
||||
|
@ -147,10 +147,10 @@ pub fn Digest.new_keyed_hash(key []u8) !Digest {
|
|||
|
||||
// Digest.new_derive_key_hash initializes a Digest structure for deriving a Blake3 key
|
||||
pub fn Digest.new_derive_key_hash(context []u8) !Digest {
|
||||
mut context_digest := Digest.new(blake3.iv, u32(Flags.derive_key_context))!
|
||||
mut context_digest := Digest.new(iv, u32(Flags.derive_key_context))!
|
||||
|
||||
context_digest.write(context)!
|
||||
context_key := context_digest.checksum_internal(blake3.key_length)
|
||||
context_key := context_digest.checksum_internal(key_length)
|
||||
|
||||
// treat the context key bytes as little endian u32 values
|
||||
mut key_words := []u32{len: 8, cap: 8}
|
||||
|
@ -206,15 +206,15 @@ pub fn (mut d Digest) write(data []u8) ! {
|
|||
// if we have more than 1024 bytes in the input,
|
||||
// process it in chunks.
|
||||
|
||||
for d.input.len > blake3.chunk_size {
|
||||
for d.input.len > chunk_size {
|
||||
mut chunk := Chunk{}
|
||||
words := chunk.process_input(d.input[..blake3.chunk_size], d.key_words, d.chunk_counter,
|
||||
words := chunk.process_input(d.input[..chunk_size], d.key_words, d.chunk_counter,
|
||||
d.flags, false)
|
||||
|
||||
d.add_node(Node{ chaining_value: words[..8] }, 0)
|
||||
|
||||
d.chunk_counter += 1
|
||||
d.input = d.input[blake3.chunk_size..]
|
||||
d.input = d.input[chunk_size..]
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -261,12 +261,12 @@ fn (mut d Digest) checksum_internal(size u64) []u8 {
|
|||
mut flags := d.flags | u32(Flags.parent)
|
||||
flags |= if i == d.binary_edge.len - 1 { u32(Flags.root) } else { u32(0) }
|
||||
|
||||
words = f(d.key_words, block_words, u64(0), blake3.block_size, flags)
|
||||
words = f(d.key_words, block_words, u64(0), block_size, flags)
|
||||
|
||||
state.words = words
|
||||
state.chaining_value = d.key_words
|
||||
state.block_words = block_words
|
||||
state.block_len = blake3.block_size
|
||||
state.block_len = block_size
|
||||
state.flags = flags
|
||||
|
||||
right_node = Node{
|
||||
|
@ -327,7 +327,7 @@ fn (mut d Digest) add_node(node Node, level u8) {
|
|||
mut block_words := edge_node.chaining_value.clone()
|
||||
block_words << node.chaining_value
|
||||
|
||||
words := f(d.key_words, block_words, u64(0), blake3.block_size, d.flags | u32(Flags.parent))
|
||||
words := f(d.key_words, block_words, u64(0), block_size, d.flags | u32(Flags.parent))
|
||||
parent_node := Node{
|
||||
chaining_value: words[..8]
|
||||
}
|
||||
|
@ -345,19 +345,19 @@ fn (mut d Digest) add_node(node Node, level u8) {
|
|||
pub fn sum256(data []u8) []u8 {
|
||||
mut d := Digest.new_hash() or { panic(err) }
|
||||
d.write(data) or { panic(err) }
|
||||
return d.checksum_internal(blake3.size256)
|
||||
return d.checksum_internal(size256)
|
||||
}
|
||||
|
||||
// sum_keyed256 returns the Blake3 256 bit keyed hash of the data.
|
||||
pub fn sum_keyed256(data []u8, key []u8) []u8 {
|
||||
mut d := Digest.new_keyed_hash(key) or { panic(err) }
|
||||
d.write(data) or { panic(err) }
|
||||
return d.checksum_internal(blake3.size256)
|
||||
return d.checksum_internal(size256)
|
||||
}
|
||||
|
||||
// sum_derived_key256 returns the Blake3 256 bit derived key hash of the key material
|
||||
pub fn sum_derive_key256(context []u8, key_material []u8) []u8 {
|
||||
mut d := Digest.new_derive_key_hash(context) or { panic(err) }
|
||||
d.write(key_material) or { panic(err) }
|
||||
return d.checksum_internal(blake3.size256)
|
||||
return d.checksum_internal(size256)
|
||||
}
|
||||
|
|
|
@ -280,7 +280,7 @@ const test_cases = [
|
|||
]
|
||||
|
||||
fn test_various_test_cases() {
|
||||
for test_case in blake3.test_cases {
|
||||
for test_case in test_cases {
|
||||
mut chunk := Chunk{}
|
||||
input := test_case.input
|
||||
chunk.process_input(input.input_string.bytes(), input.key_words, input.chunk_number,
|
||||
|
|
|
@ -36,10 +36,10 @@ const data_segment = []u8{len: 251, cap: 251, init: u8(index)}
|
|||
fn test_run_test_vectors() {
|
||||
mut data := []u8{}
|
||||
for i in 0 .. 408 {
|
||||
data << blake3.data_segment
|
||||
data << data_segment
|
||||
}
|
||||
|
||||
for case in blake3.test_object.cases {
|
||||
for case in test_object.cases {
|
||||
extended_length := u64(case.hash.len)
|
||||
|
||||
// test Blake3 hash
|
||||
|
@ -72,7 +72,7 @@ fn test_run_test_vectors() {
|
|||
|
||||
// test Blake3 keyed hash
|
||||
|
||||
mut keyed_hash_d := Digest.new_keyed_hash(blake3.test_object.key.bytes()) or {
|
||||
mut keyed_hash_d := Digest.new_keyed_hash(test_object.key.bytes()) or {
|
||||
assert false, 'Digest.new_keyed_hash error: ${err}'
|
||||
return
|
||||
}
|
||||
|
@ -99,7 +99,7 @@ fn test_run_test_vectors() {
|
|||
assert keyed_hash_d.checksum(u64(keyed_hash_bytes.len)) == keyed_hash_bytes, 'keyed hash failed output length ${extended_length}'
|
||||
// test Blake3 derive key hash
|
||||
|
||||
mut derive_key_hash_d := Digest.new_derive_key_hash(blake3.test_object.context_string.bytes()) or {
|
||||
mut derive_key_hash_d := Digest.new_derive_key_hash(test_object.context_string.bytes()) or {
|
||||
assert false, 'Digest.new_derive_key_hash error: ${err}'
|
||||
return
|
||||
}
|
||||
|
|
|
@ -10,7 +10,7 @@ const block_size = 8
|
|||
|
||||
// A tripleDesCipher is an instance of TripleDES encryption.
|
||||
struct TripleDesCipher {
|
||||
block_size int = des.block_size
|
||||
block_size int = block_size
|
||||
mut:
|
||||
cipher1 DesCipher
|
||||
cipher2 DesCipher
|
||||
|
@ -19,7 +19,7 @@ mut:
|
|||
|
||||
// DesCipher is an instance of DES encryption.
|
||||
struct DesCipher {
|
||||
block_size int = des.block_size
|
||||
block_size int = block_size
|
||||
mut:
|
||||
subkeys [16]u64
|
||||
}
|
||||
|
@ -57,26 +57,26 @@ fn (mut c DesCipher) generate_subkeys(key_bytes []u8) {
|
|||
}
|
||||
|
||||
pub fn (c &DesCipher) encrypt(mut dst []u8, src []u8) {
|
||||
if src.len < des.block_size {
|
||||
if src.len < block_size {
|
||||
panic('crypto/des: input not full block')
|
||||
}
|
||||
if dst.len < des.block_size {
|
||||
if dst.len < block_size {
|
||||
panic('crypto/des: output not full block')
|
||||
}
|
||||
if subtle.inexact_overlap(dst[..des.block_size], src[..des.block_size]) {
|
||||
if subtle.inexact_overlap(dst[..block_size], src[..block_size]) {
|
||||
panic('crypto/des: invalid buffer overlap')
|
||||
}
|
||||
encrypt_block(c.subkeys[..], mut dst, src)
|
||||
}
|
||||
|
||||
pub fn (c &DesCipher) decrypt(mut dst []u8, src []u8) {
|
||||
if src.len < des.block_size {
|
||||
if src.len < block_size {
|
||||
panic('crypto/des: input not full block')
|
||||
}
|
||||
if dst.len < des.block_size {
|
||||
if dst.len < block_size {
|
||||
panic('crypto/des: output not full block')
|
||||
}
|
||||
if subtle.inexact_overlap(dst[..des.block_size], src[..des.block_size]) {
|
||||
if subtle.inexact_overlap(dst[..block_size], src[..block_size]) {
|
||||
panic('crypto/des: invalid buffer overlap')
|
||||
}
|
||||
decrypt_block(c.subkeys[..], mut dst, src)
|
||||
|
@ -95,13 +95,13 @@ pub fn new_triple_des_cipher(key []u8) cipher.Block {
|
|||
}
|
||||
|
||||
pub fn (c &TripleDesCipher) encrypt(mut dst []u8, src []u8) {
|
||||
if src.len < des.block_size {
|
||||
if src.len < block_size {
|
||||
panic('crypto/des: input not full block')
|
||||
}
|
||||
if dst.len < des.block_size {
|
||||
if dst.len < block_size {
|
||||
panic('crypto/des: output not full block')
|
||||
}
|
||||
if subtle.inexact_overlap(dst[..des.block_size], src[..des.block_size]) {
|
||||
if subtle.inexact_overlap(dst[..block_size], src[..block_size]) {
|
||||
panic('crypto/des: invalid buffer overlap')
|
||||
}
|
||||
|
||||
|
@ -131,13 +131,13 @@ pub fn (c &TripleDesCipher) encrypt(mut dst []u8, src []u8) {
|
|||
}
|
||||
|
||||
pub fn (c &TripleDesCipher) decrypt(mut dst []u8, src []u8) {
|
||||
if src.len < des.block_size {
|
||||
if src.len < block_size {
|
||||
panic('crypto/des: input not full block')
|
||||
}
|
||||
if dst.len < des.block_size {
|
||||
if dst.len < block_size {
|
||||
panic('crypto/des: output not full block')
|
||||
}
|
||||
if subtle.inexact_overlap(dst[..des.block_size], src[..des.block_size]) {
|
||||
if subtle.inexact_overlap(dst[..block_size], src[..block_size]) {
|
||||
panic('crypto/des: invalid buffer overlap')
|
||||
}
|
||||
|
||||
|
|
|
@ -31,15 +31,15 @@ pub type PrivateKey = []u8
|
|||
// seed returns the private key seed corresponding to priv.
|
||||
// RFC 8032's private keys correspond to seeds in this module.
|
||||
pub fn (priv PrivateKey) seed() []u8 {
|
||||
mut seed := []u8{len: ed25519.seed_size}
|
||||
mut seed := []u8{len: seed_size}
|
||||
copy(mut seed, priv[..32])
|
||||
return seed
|
||||
}
|
||||
|
||||
// public_key returns the []u8 corresponding to priv.
|
||||
pub fn (priv PrivateKey) public_key() PublicKey {
|
||||
assert priv.len == ed25519.private_key_size
|
||||
mut publickey := []u8{len: ed25519.public_key_size}
|
||||
assert priv.len == private_key_size
|
||||
mut publickey := []u8{len: public_key_size}
|
||||
copy(mut publickey, priv[32..])
|
||||
return PublicKey(publickey)
|
||||
}
|
||||
|
@ -61,16 +61,16 @@ pub fn (priv PrivateKey) sign(message []u8) ![]u8 {
|
|||
|
||||
// sign`signs the message with privatekey and returns a signature
|
||||
pub fn sign(privatekey PrivateKey, message []u8) ![]u8 {
|
||||
mut signature := []u8{len: ed25519.signature_size}
|
||||
mut signature := []u8{len: signature_size}
|
||||
sign_generic(mut signature, privatekey, message)!
|
||||
return signature
|
||||
}
|
||||
|
||||
fn sign_generic(mut signature []u8, privatekey []u8, message []u8) ! {
|
||||
if privatekey.len != ed25519.private_key_size {
|
||||
if privatekey.len != private_key_size {
|
||||
panic('ed25519: bad private key length: ${privatekey.len}')
|
||||
}
|
||||
seed, publickey := privatekey[..ed25519.seed_size], privatekey[ed25519.seed_size..]
|
||||
seed, publickey := privatekey[..seed_size], privatekey[seed_size..]
|
||||
|
||||
mut h := sha512.sum512(seed)
|
||||
mut s := edwards25519.new_scalar()
|
||||
|
@ -109,11 +109,11 @@ fn sign_generic(mut signature []u8, privatekey []u8, message []u8) ! {
|
|||
|
||||
// verify reports whether sig is a valid signature of message by publickey.
|
||||
pub fn verify(publickey PublicKey, message []u8, sig []u8) !bool {
|
||||
if publickey.len != ed25519.public_key_size {
|
||||
if publickey.len != public_key_size {
|
||||
return error('ed25519: bad public key length: ${publickey.len}')
|
||||
}
|
||||
|
||||
if sig.len != ed25519.signature_size || sig[63] & 224 != 0 {
|
||||
if sig.len != signature_size || sig[63] & 224 != 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
|
@ -145,10 +145,10 @@ pub fn verify(publickey PublicKey, message []u8, sig []u8) !bool {
|
|||
|
||||
// generate_key generates a public/private key pair entropy using `crypto.rand`.
|
||||
pub fn generate_key() !(PublicKey, PrivateKey) {
|
||||
mut seed := rand.bytes(ed25519.seed_size)!
|
||||
mut seed := rand.bytes(seed_size)!
|
||||
|
||||
privatekey := new_key_from_seed(seed)
|
||||
mut publickey := []u8{len: ed25519.public_key_size}
|
||||
mut publickey := []u8{len: public_key_size}
|
||||
copy(mut publickey, privatekey[32..])
|
||||
|
||||
return publickey, privatekey
|
||||
|
@ -158,13 +158,13 @@ pub fn generate_key() !(PublicKey, PrivateKey) {
|
|||
// correspond to seeds in this module
|
||||
pub fn new_key_from_seed(seed []u8) PrivateKey {
|
||||
// Outline the function body so that the returned key can be stack-allocated.
|
||||
mut privatekey := []u8{len: ed25519.private_key_size}
|
||||
mut privatekey := []u8{len: private_key_size}
|
||||
new_key_from_seed_generic(mut privatekey, seed)
|
||||
return PrivateKey(privatekey)
|
||||
}
|
||||
|
||||
fn new_key_from_seed_generic(mut privatekey []u8, seed []u8) {
|
||||
if seed.len != ed25519.seed_size {
|
||||
if seed.len != seed_size {
|
||||
panic('ed25519: bad seed length: ${seed.len}')
|
||||
}
|
||||
|
||||
|
|
|
@ -198,11 +198,11 @@ fn fe_mul_generic(a Element, b Element) Element {
|
|||
c3 := shift_right_by_51(r3)
|
||||
c4 := shift_right_by_51(r4)
|
||||
|
||||
rr0 := r0.lo & edwards25519.mask_low_51_bits + c4 * 19
|
||||
rr1 := r1.lo & edwards25519.mask_low_51_bits + c0
|
||||
rr2 := r2.lo & edwards25519.mask_low_51_bits + c1
|
||||
rr3 := r3.lo & edwards25519.mask_low_51_bits + c2
|
||||
rr4 := r4.lo & edwards25519.mask_low_51_bits + c3
|
||||
rr0 := r0.lo & mask_low_51_bits + c4 * 19
|
||||
rr1 := r1.lo & mask_low_51_bits + c0
|
||||
rr2 := r2.lo & mask_low_51_bits + c1
|
||||
rr3 := r3.lo & mask_low_51_bits + c2
|
||||
rr4 := r4.lo & mask_low_51_bits + c3
|
||||
|
||||
// Now all coefficients fit into 64-bit registers but are still too large to
|
||||
// be passed around as a Element. We therefore do one last carry chain,
|
||||
|
@ -229,11 +229,11 @@ fn (mut v Element) carry_propagate_generic() Element {
|
|||
c3 := v.l3 >> 51
|
||||
c4 := v.l4 >> 51
|
||||
|
||||
v.l0 = v.l0 & edwards25519.mask_low_51_bits + c4 * 19
|
||||
v.l1 = v.l1 & edwards25519.mask_low_51_bits + c0
|
||||
v.l2 = v.l2 & edwards25519.mask_low_51_bits + c1
|
||||
v.l3 = v.l3 & edwards25519.mask_low_51_bits + c2
|
||||
v.l4 = v.l4 & edwards25519.mask_low_51_bits + c3
|
||||
v.l0 = v.l0 & mask_low_51_bits + c4 * 19
|
||||
v.l1 = v.l1 & mask_low_51_bits + c0
|
||||
v.l2 = v.l2 & mask_low_51_bits + c1
|
||||
v.l3 = v.l3 & mask_low_51_bits + c2
|
||||
v.l4 = v.l4 & mask_low_51_bits + c3
|
||||
return v
|
||||
}
|
||||
|
||||
|
@ -310,11 +310,11 @@ fn fe_square_generic(a Element) Element {
|
|||
c3 := shift_right_by_51(r3)
|
||||
c4 := shift_right_by_51(r4)
|
||||
|
||||
rr0 := r0.lo & edwards25519.mask_low_51_bits + c4 * 19
|
||||
rr1 := r1.lo & edwards25519.mask_low_51_bits + c0
|
||||
rr2 := r2.lo & edwards25519.mask_low_51_bits + c1
|
||||
rr3 := r3.lo & edwards25519.mask_low_51_bits + c2
|
||||
rr4 := r4.lo & edwards25519.mask_low_51_bits + c3
|
||||
rr0 := r0.lo & mask_low_51_bits + c4 * 19
|
||||
rr1 := r1.lo & mask_low_51_bits + c0
|
||||
rr2 := r2.lo & mask_low_51_bits + c1
|
||||
rr3 := r3.lo & mask_low_51_bits + c2
|
||||
rr4 := r4.lo & mask_low_51_bits + c3
|
||||
|
||||
mut v := Element{
|
||||
l0: rr0
|
||||
|
@ -329,13 +329,13 @@ fn fe_square_generic(a Element) Element {
|
|||
|
||||
// zero sets v = 0, and returns v.
|
||||
pub fn (mut v Element) zero() Element {
|
||||
v = edwards25519.fe_zero
|
||||
v = fe_zero
|
||||
return v
|
||||
}
|
||||
|
||||
// one sets v = 1, and returns v.
|
||||
pub fn (mut v Element) one() Element {
|
||||
v = edwards25519.fe_one
|
||||
v = fe_one
|
||||
return v
|
||||
}
|
||||
|
||||
|
@ -359,15 +359,15 @@ pub fn (mut v Element) reduce() Element {
|
|||
v.l0 += 19 * c
|
||||
|
||||
v.l1 += v.l0 >> 51
|
||||
v.l0 = v.l0 & edwards25519.mask_low_51_bits
|
||||
v.l0 = v.l0 & mask_low_51_bits
|
||||
v.l2 += v.l1 >> 51
|
||||
v.l1 = v.l1 & edwards25519.mask_low_51_bits
|
||||
v.l1 = v.l1 & mask_low_51_bits
|
||||
v.l3 += v.l2 >> 51
|
||||
v.l2 = v.l2 & edwards25519.mask_low_51_bits
|
||||
v.l2 = v.l2 & mask_low_51_bits
|
||||
v.l4 += v.l3 >> 51
|
||||
v.l3 = v.l3 & edwards25519.mask_low_51_bits
|
||||
v.l3 = v.l3 & mask_low_51_bits
|
||||
// no additional carry
|
||||
v.l4 = v.l4 & edwards25519.mask_low_51_bits
|
||||
v.l4 = v.l4 & mask_low_51_bits
|
||||
|
||||
return v
|
||||
}
|
||||
|
@ -400,7 +400,7 @@ pub fn (mut v Element) subtract(a Element, b Element) Element {
|
|||
|
||||
// negate sets v = -a, and returns v.
|
||||
pub fn (mut v Element) negate(a Element) Element {
|
||||
return v.subtract(edwards25519.fe_zero, a)
|
||||
return v.subtract(fe_zero, a)
|
||||
}
|
||||
|
||||
// invert sets v = 1/z mod p, and returns v.
|
||||
|
@ -493,7 +493,7 @@ pub fn (mut v Element) multiply(x Element, y Element) Element {
|
|||
// mul_51 returns lo + hi * 2⁵¹ = a * b.
|
||||
fn mul_51(a u64, b u32) (u64, u64) {
|
||||
mh, ml := bits.mul_64(a, u64(b))
|
||||
lo := ml & edwards25519.mask_low_51_bits
|
||||
lo := ml & mask_low_51_bits
|
||||
hi := (mh << 13) | (ml >> 51)
|
||||
return lo, hi
|
||||
}
|
||||
|
@ -568,9 +568,9 @@ pub fn (mut r Element) sqrt_ratio(u Element, v Element) (Element, int) {
|
|||
mut uneg := b.negate(u)
|
||||
correct_sign_sqrt := check.equal(u)
|
||||
flipped_sign_sqrt := check.equal(uneg)
|
||||
flipped_sign_sqrt_i := check.equal(uneg.multiply(uneg, edwards25519.sqrt_m1))
|
||||
flipped_sign_sqrt_i := check.equal(uneg.multiply(uneg, sqrt_m1))
|
||||
|
||||
rprime := b.multiply(r, edwards25519.sqrt_m1) // r_prime = SQRT_M1 * r
|
||||
rprime := b.multiply(r, sqrt_m1) // r_prime = SQRT_M1 * r
|
||||
// r = CT_selected(r_prime IF flipped_sign_sqrt | flipped_sign_sqrt_i ELSE r)
|
||||
r.selected(rprime, r, flipped_sign_sqrt | flipped_sign_sqrt_i)
|
||||
|
||||
|
@ -629,20 +629,20 @@ pub fn (mut v Element) set_bytes(x []u8) !Element {
|
|||
|
||||
// Bits 0:51 (bytes 0:8, bits 0:64, shift 0, mask 51).
|
||||
v.l0 = binary.little_endian_u64(x[0..8])
|
||||
v.l0 &= edwards25519.mask_low_51_bits
|
||||
v.l0 &= mask_low_51_bits
|
||||
// Bits 51:102 (bytes 6:14, bits 48:112, shift 3, mask 51).
|
||||
v.l1 = binary.little_endian_u64(x[6..14]) >> 3
|
||||
v.l1 &= edwards25519.mask_low_51_bits
|
||||
v.l1 &= mask_low_51_bits
|
||||
// Bits 102:153 (bytes 12:20, bits 96:160, shift 6, mask 51).
|
||||
v.l2 = binary.little_endian_u64(x[12..20]) >> 6
|
||||
v.l2 &= edwards25519.mask_low_51_bits
|
||||
v.l2 &= mask_low_51_bits
|
||||
// Bits 153:204 (bytes 19:27, bits 152:216, shift 1, mask 51).
|
||||
v.l3 = binary.little_endian_u64(x[19..27]) >> 1
|
||||
v.l3 &= edwards25519.mask_low_51_bits
|
||||
v.l3 &= mask_low_51_bits
|
||||
// Bits 204:251 (bytes 24:32, bits 192:256, shift 12, mask 51).
|
||||
// Note: not bytes 25:33, shift 4, to avoid overread.
|
||||
v.l4 = binary.little_endian_u64(x[24..32]) >> 12
|
||||
v.l4 &= edwards25519.mask_low_51_bits
|
||||
v.l4 &= mask_low_51_bits
|
||||
|
||||
return v
|
||||
}
|
||||
|
|
|
@ -9,7 +9,7 @@ import encoding.hex
|
|||
const github_job = os.getenv('GITHUB_JOB')
|
||||
|
||||
fn testsuite_begin() {
|
||||
if edwards25519.github_job != '' {
|
||||
if github_job != '' {
|
||||
// ensure that the CI does not run flaky tests:
|
||||
rand.seed([u32(0xffff24), 0xabcd])
|
||||
}
|
||||
|
@ -23,11 +23,11 @@ const mask_low_52_bits = (u64(1) << 52) - 1
|
|||
|
||||
fn generate_field_element() Element {
|
||||
return Element{
|
||||
l0: rand.u64() & edwards25519.mask_low_52_bits
|
||||
l1: rand.u64() & edwards25519.mask_low_52_bits
|
||||
l2: rand.u64() & edwards25519.mask_low_52_bits
|
||||
l3: rand.u64() & edwards25519.mask_low_52_bits
|
||||
l4: rand.u64() & edwards25519.mask_low_52_bits
|
||||
l0: rand.u64() & mask_low_52_bits
|
||||
l1: rand.u64() & mask_low_52_bits
|
||||
l2: rand.u64() & mask_low_52_bits
|
||||
l3: rand.u64() & mask_low_52_bits
|
||||
l4: rand.u64() & mask_low_52_bits
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -80,11 +80,11 @@ const weird_limbs_52 = [
|
|||
|
||||
fn generate_weird_field_element() Element {
|
||||
return Element{
|
||||
l0: edwards25519.weird_limbs_52[rand.intn(edwards25519.weird_limbs_52.len) or { 0 }]
|
||||
l1: edwards25519.weird_limbs_51[rand.intn(edwards25519.weird_limbs_51.len) or { 0 }]
|
||||
l2: edwards25519.weird_limbs_51[rand.intn(edwards25519.weird_limbs_51.len) or { 0 }]
|
||||
l3: edwards25519.weird_limbs_51[rand.intn(edwards25519.weird_limbs_51.len) or { 0 }]
|
||||
l4: edwards25519.weird_limbs_51[rand.intn(edwards25519.weird_limbs_51.len) or { 0 }]
|
||||
l0: weird_limbs_52[rand.intn(weird_limbs_52.len) or { 0 }]
|
||||
l1: weird_limbs_51[rand.intn(weird_limbs_51.len) or { 0 }]
|
||||
l2: weird_limbs_51[rand.intn(weird_limbs_51.len) or { 0 }]
|
||||
l3: weird_limbs_51[rand.intn(weird_limbs_51.len) or { 0 }]
|
||||
l4: weird_limbs_51[rand.intn(weird_limbs_51.len) or { 0 }]
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@ import encoding.hex
|
|||
const github_job = os.getenv('GITHUB_JOB')
|
||||
|
||||
fn testsuite_begin() {
|
||||
if edwards25519.github_job != '' {
|
||||
if github_job != '' {
|
||||
// ensure that the CI does not run flaky tests:
|
||||
rand.seed([u32(0xffff24), 0xabcd])
|
||||
}
|
||||
|
@ -73,7 +73,7 @@ fn fn_cofactor(mut data []u8) bool {
|
|||
panic('data.len should be 64')
|
||||
}
|
||||
mut loworder := Point{}
|
||||
loworder.set_bytes(edwards25519.loworder_bytes) or { panic(err) }
|
||||
loworder.set_bytes(loworder_bytes) or { panic(err) }
|
||||
|
||||
mut s := new_scalar()
|
||||
mut p := Point{}
|
||||
|
|
|
@ -18,20 +18,20 @@ const gen_point = generator() or { panic(err) }
|
|||
|
||||
fn d_const_generate() !Element {
|
||||
mut v := Element{}
|
||||
v.set_bytes(edwards25519.d_bytes)!
|
||||
v.set_bytes(d_bytes)!
|
||||
return v
|
||||
}
|
||||
|
||||
fn d2_const_generate() !Element {
|
||||
mut v := Element{}
|
||||
v.add(edwards25519.d_const, edwards25519.d_const)
|
||||
v.add(d_const, d_const)
|
||||
return v
|
||||
}
|
||||
|
||||
// id_point_generate is the point at infinity.
|
||||
fn id_point_generate() !Point {
|
||||
mut p := Point{}
|
||||
p.set_bytes(edwards25519.id_bytes)!
|
||||
p.set_bytes(id_bytes)!
|
||||
return p
|
||||
}
|
||||
|
||||
|
@ -39,7 +39,7 @@ fn id_point_generate() !Point {
|
|||
// correspondence of this encoding with the values in RFC 8032.
|
||||
fn generator() !Point {
|
||||
mut p := Point{}
|
||||
p.set_bytes(edwards25519.gen_bytes)!
|
||||
p.set_bytes(gen_bytes)!
|
||||
return p
|
||||
}
|
||||
|
||||
|
@ -139,7 +139,7 @@ pub fn (mut v Point) set_bytes(x []u8) !Point {
|
|||
|
||||
// v = dy² + 1
|
||||
mut el3 := Element{}
|
||||
mut vv := el3.multiply(y2, edwards25519.d_const)
|
||||
mut vv := el3.multiply(y2, d_const)
|
||||
vv = vv.add(vv, fe_one)
|
||||
|
||||
// x = +√(u/v)
|
||||
|
@ -171,13 +171,13 @@ pub fn (mut v Point) set(u Point) Point {
|
|||
// new_identity_point returns a new Point set to the identity.
|
||||
pub fn new_identity_point() Point {
|
||||
mut p := Point{}
|
||||
return p.set(edwards25519.id_point)
|
||||
return p.set(id_point)
|
||||
}
|
||||
|
||||
// new_generator_point returns a new Point set to the canonical generator.
|
||||
pub fn new_generator_point() Point {
|
||||
mut p := Point{}
|
||||
return p.set(edwards25519.gen_point)
|
||||
return p.set(gen_point)
|
||||
}
|
||||
|
||||
fn (mut v ProjectiveCached) zero() ProjectiveCached {
|
||||
|
@ -276,14 +276,14 @@ fn (mut v ProjectiveCached) from_p3(p Point) ProjectiveCached {
|
|||
v.ypx.add(p.y, p.x)
|
||||
v.ymx.subtract(p.y, p.x)
|
||||
v.z.set(p.z)
|
||||
v.t2d.multiply(p.t, edwards25519.d2_const)
|
||||
v.t2d.multiply(p.t, d2_const)
|
||||
return v
|
||||
}
|
||||
|
||||
fn (mut v AffineCached) from_p3(p Point) AffineCached {
|
||||
v.ypx.add(p.y, p.x)
|
||||
v.ymx.subtract(p.y, p.x)
|
||||
v.t2d.multiply(p.t, edwards25519.d2_const)
|
||||
v.t2d.multiply(p.t, d2_const)
|
||||
|
||||
mut invz := Element{}
|
||||
invz.invert(p.z)
|
||||
|
@ -521,7 +521,7 @@ fn check_on_curve(points ...Point) bool {
|
|||
mut rhs := Element{}
|
||||
lhs.subtract(yy, xx)
|
||||
lhs.multiply(lhs, zz)
|
||||
rhs.multiply(edwards25519.d_const, xx)
|
||||
rhs.multiply(d_const, xx)
|
||||
rhs.multiply(rhs, yy)
|
||||
rhs.add(rhs, zzzz)
|
||||
|
||||
|
|
|
@ -10,8 +10,8 @@ fn test_invalid_encodings() {
|
|||
inv_bytes := hex.decode(invalid) or { panic(err) }
|
||||
mut p := new_generator_point()
|
||||
|
||||
out := p.set_bytes(inv_bytes) or { edwards25519.zero_point }
|
||||
assert out == edwards25519.zero_point
|
||||
out := p.set_bytes(inv_bytes) or { zero_point }
|
||||
assert out == zero_point
|
||||
// assert p.equal(bgp) == 1 //not makes sense when error
|
||||
|
||||
assert check_on_curve(p) == true
|
||||
|
|
|
@ -44,7 +44,7 @@ pub fn new_scalar() Scalar {
|
|||
// add sets s = x + y mod l, and returns s.
|
||||
pub fn (mut s Scalar) add(x Scalar, y Scalar) Scalar {
|
||||
// s = 1 * x + y mod l
|
||||
sc_mul_add(mut s.s, edwards25519.sc_one.s, x.s, y.s)
|
||||
sc_mul_add(mut s.s, sc_one.s, x.s, y.s)
|
||||
return s
|
||||
}
|
||||
|
||||
|
@ -57,21 +57,21 @@ pub fn (mut s Scalar) multiply_add(x Scalar, y Scalar, z Scalar) Scalar {
|
|||
// subtract sets s = x - y mod l, and returns s.
|
||||
pub fn (mut s Scalar) subtract(x Scalar, y Scalar) Scalar {
|
||||
// s = -1 * y + x mod l
|
||||
sc_mul_add(mut s.s, edwards25519.sc_minus_one.s, y.s, x.s)
|
||||
sc_mul_add(mut s.s, sc_minus_one.s, y.s, x.s)
|
||||
return s
|
||||
}
|
||||
|
||||
// negate sets s = -x mod l, and returns s.
|
||||
pub fn (mut s Scalar) negate(x Scalar) Scalar {
|
||||
// s = -1 * x + 0 mod l
|
||||
sc_mul_add(mut s.s, edwards25519.sc_minus_one.s, x.s, edwards25519.sc_zero.s)
|
||||
sc_mul_add(mut s.s, sc_minus_one.s, x.s, sc_zero.s)
|
||||
return s
|
||||
}
|
||||
|
||||
// multiply sets s = x * y mod l, and returns s.
|
||||
pub fn (mut s Scalar) multiply(x Scalar, y Scalar) Scalar {
|
||||
// s = x * y + 0 mod l
|
||||
sc_mul_add(mut s.s, x.s, y.s, edwards25519.sc_zero.s)
|
||||
sc_mul_add(mut s.s, x.s, y.s, sc_zero.s)
|
||||
return s
|
||||
}
|
||||
|
||||
|
@ -121,10 +121,10 @@ pub fn (mut s Scalar) set_canonical_bytes(x []u8) !Scalar {
|
|||
// is_reduced returns whether the given scalar is reduced modulo l.
|
||||
fn is_reduced(s Scalar) bool {
|
||||
for i := s.s.len - 1; i >= 0; i-- {
|
||||
if s.s[i] > edwards25519.sc_minus_one.s[i] {
|
||||
if s.s[i] > sc_minus_one.s[i] {
|
||||
return false
|
||||
}
|
||||
if s.s[i] < edwards25519.sc_minus_one.s[i] {
|
||||
if s.s[i] < sc_minus_one.s[i] {
|
||||
return true
|
||||
}
|
||||
/*
|
||||
|
@ -1096,7 +1096,7 @@ fn generate_scalar(size int) !Scalar {
|
|||
}
|
||||
return reflect.ValueOf(s)
|
||||
*/
|
||||
mut s := edwards25519.sc_zero
|
||||
mut s := sc_zero
|
||||
diceroll := rand.intn(100) or { 0 }
|
||||
match true {
|
||||
/*
|
||||
|
@ -1104,10 +1104,10 @@ fn generate_scalar(size int) !Scalar {
|
|||
case diceroll == 1:
|
||||
*/
|
||||
diceroll == 0 || diceroll == 1 {
|
||||
s = edwards25519.sc_one
|
||||
s = sc_one
|
||||
}
|
||||
diceroll == 2 {
|
||||
s = edwards25519.sc_minus_one
|
||||
s = sc_minus_one
|
||||
}
|
||||
diceroll < 5 {
|
||||
// rand.Read(s.s[:16]) // read random bytes and fill buf
|
||||
|
@ -1164,7 +1164,7 @@ type NotZeroScalar = Scalar
|
|||
|
||||
fn generate_notzero_scalar(size int) !NotZeroScalar {
|
||||
mut s := Scalar{}
|
||||
for s == edwards25519.sc_zero {
|
||||
for s == sc_zero {
|
||||
s = generate_scalar(size)!
|
||||
}
|
||||
return NotZeroScalar(s)
|
||||
|
|
|
@ -8,7 +8,7 @@ import math.big
|
|||
const github_job = os.getenv('GITHUB_JOB')
|
||||
|
||||
fn testsuite_begin() {
|
||||
if edwards25519.github_job != '' {
|
||||
if github_job != '' {
|
||||
// ensure that the CI does not run flaky tests:
|
||||
rand.seed([u32(0xffff24), 0xabcd])
|
||||
}
|
||||
|
@ -108,8 +108,8 @@ fn test_scalar_set_canonical_bytes_on_noncanonical_value() {
|
|||
b[31] += 1
|
||||
|
||||
mut s := sc_one
|
||||
out := s.set_canonical_bytes(b[..]) or { edwards25519.sc_error } // set_canonical_bytes shouldn't worked on a non-canonical value"
|
||||
assert out == edwards25519.sc_error
|
||||
out := s.set_canonical_bytes(b[..]) or { sc_error } // set_canonical_bytes shouldn't worked on a non-canonical value"
|
||||
assert out == sc_error
|
||||
assert s == sc_one
|
||||
}
|
||||
|
||||
|
|
|
@ -8,7 +8,7 @@ const dsc_basepoint = [u8(0xf4), 0xef, 0x7c, 0xa, 0x34, 0x55, 0x7b, 0x9f, 0x72,
|
|||
|
||||
fn dalek_scalar_basepoint() Point {
|
||||
mut p := Point{}
|
||||
p.set_bytes(edwards25519.dsc_basepoint) or { panic(err) }
|
||||
p.set_bytes(dsc_basepoint) or { panic(err) }
|
||||
return p
|
||||
}
|
||||
|
||||
|
@ -33,7 +33,7 @@ fn test_scalar_mult_small_scalars() {
|
|||
fn test_scalar_mult_vs_dalek() {
|
||||
mut p := Point{}
|
||||
mut b := new_generator_point()
|
||||
mut dsc := edwards25519.dalek_scalar
|
||||
mut dsc := dalek_scalar
|
||||
p.scalar_mult(mut dsc, b)
|
||||
mut ds := dalek_scalar_basepoint()
|
||||
assert ds.equal(p) == 1
|
||||
|
@ -43,7 +43,7 @@ fn test_scalar_mult_vs_dalek() {
|
|||
|
||||
fn test_scalar_base_mult_vs_dalek() {
|
||||
mut p := Point{}
|
||||
mut dsc := edwards25519.dalek_scalar
|
||||
mut dsc := dalek_scalar
|
||||
p.scalar_base_mult(mut dsc)
|
||||
mut ds := dalek_scalar_basepoint()
|
||||
assert ds.equal(p) == 1
|
||||
|
@ -55,13 +55,13 @@ fn test_vartime_double_basemult_vs_dalek() {
|
|||
mut p := Point{}
|
||||
mut z := Scalar{}
|
||||
b := new_generator_point()
|
||||
p.vartime_double_scalar_base_mult(edwards25519.dalek_scalar, b, z)
|
||||
p.vartime_double_scalar_base_mult(dalek_scalar, b, z)
|
||||
|
||||
mut ds := dalek_scalar_basepoint()
|
||||
assert ds.equal(p) == 1
|
||||
assert check_on_curve(p)
|
||||
|
||||
p.vartime_double_scalar_base_mult(z, b, edwards25519.dalek_scalar)
|
||||
p.vartime_double_scalar_base_mult(z, b, dalek_scalar)
|
||||
|
||||
assert ds.equal(p) == 1
|
||||
assert check_on_curve(p)
|
||||
|
|
|
@ -18,16 +18,16 @@ pub fn new(key []u8, data []u8, hash_func fn ([]u8) []u8, blocksize int) []u8 {
|
|||
b_key = hash_func(key)
|
||||
}
|
||||
if b_key.len < blocksize {
|
||||
b_key << hmac.npad[..blocksize - b_key.len]
|
||||
b_key << npad[..blocksize - b_key.len]
|
||||
}
|
||||
mut inner := []u8{}
|
||||
for i, b in hmac.ipad[..blocksize] {
|
||||
for i, b in ipad[..blocksize] {
|
||||
inner << b_key[i] ^ b
|
||||
}
|
||||
inner << data
|
||||
inner_hash := hash_func(inner)
|
||||
mut outer := []u8{cap: b_key.len}
|
||||
for i, b in hmac.opad[..blocksize] {
|
||||
for i, b in opad[..blocksize] {
|
||||
outer << b_key[i] ^ b
|
||||
}
|
||||
outer << inner_hash
|
||||
|
|
|
@ -60,8 +60,8 @@ fn test_hmac_md5() {
|
|||
'6f630fad67cda0ee1fb1f562db3aa53e',
|
||||
]
|
||||
mut result := ''
|
||||
for i, key in hmac.keys {
|
||||
result = new(key, hmac.data[i], md5.sum, md5.block_size).hex()
|
||||
for i, key in keys {
|
||||
result = new(key, data[i], md5.sum, md5.block_size).hex()
|
||||
assert result == md5_expected_results[i]
|
||||
}
|
||||
}
|
||||
|
@ -77,8 +77,8 @@ fn test_hmac_sha1() {
|
|||
'e8e99d0f45237d786d6bbaa7965c7808bbff1a91',
|
||||
]
|
||||
mut result := ''
|
||||
for i, key in hmac.keys {
|
||||
result = new(key, hmac.data[i], sha1.sum, sha1.block_size).hex()
|
||||
for i, key in keys {
|
||||
result = new(key, data[i], sha1.sum, sha1.block_size).hex()
|
||||
assert result == sha1_expected_results[i]
|
||||
}
|
||||
}
|
||||
|
@ -94,8 +94,8 @@ fn test_hmac_sha224() {
|
|||
'7358939e58683a448ac5065196d33191a1c1d33d4b8b0304dc60f5e0',
|
||||
]
|
||||
mut result := ''
|
||||
for i, key in hmac.keys {
|
||||
result = new(key, hmac.data[i], sha256.sum224, sha256.block_size).hex()
|
||||
for i, key in keys {
|
||||
result = new(key, data[i], sha256.sum224, sha256.block_size).hex()
|
||||
assert result == sha224_expected_results[i]
|
||||
}
|
||||
}
|
||||
|
@ -111,8 +111,8 @@ fn test_hmac_sha256() {
|
|||
'6355ac22e890d0a3c8481a5ca4825bc884d3e7a1ff98a2fc2ac7d8e064c3b2e6',
|
||||
]
|
||||
mut result := ''
|
||||
for i, key in hmac.keys {
|
||||
result = new(key, hmac.data[i], sha256.sum, sha256.block_size).hex()
|
||||
for i, key in keys {
|
||||
result = new(key, data[i], sha256.sum, sha256.block_size).hex()
|
||||
assert result == sha256_expected_results[i]
|
||||
}
|
||||
}
|
||||
|
@ -128,8 +128,8 @@ fn test_hmac_sha384() {
|
|||
'34f065bdedc2487c30a634d9a49cf42116f78bb386ea4d498aea05c0077f05373cfdaa9b59a7b0481bced9e3f55016a9',
|
||||
]
|
||||
mut result := ''
|
||||
for i, key in hmac.keys {
|
||||
result = new(key, hmac.data[i], sha512.sum384, sha512.block_size).hex()
|
||||
for i, key in keys {
|
||||
result = new(key, data[i], sha512.sum384, sha512.block_size).hex()
|
||||
assert result == sha384_expected_results[i]
|
||||
}
|
||||
}
|
||||
|
@ -145,8 +145,8 @@ fn test_hmac_sha512() {
|
|||
'09441cda584ed2f4d2f5b519c71baf3c79cce19dfc89a548e73b3bb382a9124d6e792b77bf57903ff5858e5d111d15f45d6fd118eea023f28d2eb234ebe62f85',
|
||||
]
|
||||
mut result := ''
|
||||
for i, key in hmac.keys {
|
||||
result = new(key, hmac.data[i], sha512.sum512, sha512.block_size).hex()
|
||||
for i, key in keys {
|
||||
result = new(key, data[i], sha512.sum512, sha512.block_size).hex()
|
||||
assert result == sha512_expected_results[i]
|
||||
}
|
||||
}
|
||||
|
@ -162,8 +162,8 @@ fn test_hmac_blake2s_256() {
|
|||
'467201ef5997a3442932b318083488cf9aa1d89bef2146154b4816d34863e33d',
|
||||
]
|
||||
mut result := ''
|
||||
for i, key in hmac.keys {
|
||||
result = new(key, hmac.data[i], blake2s.sum256, blake2s.block_size).hex()
|
||||
for i, key in keys {
|
||||
result = new(key, data[i], blake2s.sum256, blake2s.block_size).hex()
|
||||
assert result == blake2s_256_expected_results[i]
|
||||
}
|
||||
}
|
||||
|
@ -179,8 +179,8 @@ fn test_hmac_blake2s_224() {
|
|||
'17b9ebb1426a5a3dd6aa91567bd9cb9c19b3dc007adb726e55b98926',
|
||||
]
|
||||
mut result := ''
|
||||
for i, key in hmac.keys {
|
||||
result = new(key, hmac.data[i], blake2s.sum224, blake2s.block_size).hex()
|
||||
for i, key in keys {
|
||||
result = new(key, data[i], blake2s.sum224, blake2s.block_size).hex()
|
||||
assert result == blake2s_224_expected_results[i]
|
||||
}
|
||||
}
|
||||
|
@ -196,8 +196,8 @@ fn test_hmac_blake2s_160() {
|
|||
'6f3127fcba040fe6ea552b22c39b0fd83abca19a',
|
||||
]
|
||||
mut result := ''
|
||||
for i, key in hmac.keys {
|
||||
result = new(key, hmac.data[i], blake2s.sum160, blake2s.block_size).hex()
|
||||
for i, key in keys {
|
||||
result = new(key, data[i], blake2s.sum160, blake2s.block_size).hex()
|
||||
assert result == blake2s_160_expected_results[i]
|
||||
}
|
||||
}
|
||||
|
@ -213,8 +213,8 @@ fn test_hmac_blake2s_128() {
|
|||
'96a72e3adf5e0b02d4e6d4e8a7342a77',
|
||||
]
|
||||
mut result := ''
|
||||
for i, key in hmac.keys {
|
||||
result = new(key, hmac.data[i], blake2s.sum128, blake2s.block_size).hex()
|
||||
for i, key in keys {
|
||||
result = new(key, data[i], blake2s.sum128, blake2s.block_size).hex()
|
||||
assert result == blake2s_128_expected_results[i]
|
||||
}
|
||||
}
|
||||
|
@ -230,8 +230,8 @@ fn test_hmac_blake2b_512() {
|
|||
'f1c9b64e121330c512dc31e0d4a2fc84b7ca5be64e08934a7fc4640c4a1f5cc3c1f34d811c8079cc2df65a4e5d68baf833a1ec558546abeaa7d564840618db7b',
|
||||
]
|
||||
mut result := ''
|
||||
for i, key in hmac.keys {
|
||||
result = new(key, hmac.data[i], blake2b.sum512, blake2b.block_size).hex()
|
||||
for i, key in keys {
|
||||
result = new(key, data[i], blake2b.sum512, blake2b.block_size).hex()
|
||||
assert result == blake2b_512_expected_results[i]
|
||||
}
|
||||
}
|
||||
|
@ -247,8 +247,8 @@ fn test_hmac_blake2b_384() {
|
|||
'c9d0155de83454f0720b5310b4b891ddc9ab702b8260b15aa6f7291efec95b7e7a2c986019814b7c28c105c22f0ef961',
|
||||
]
|
||||
mut result := ''
|
||||
for i, key in hmac.keys {
|
||||
result = new(key, hmac.data[i], blake2b.sum384, blake2b.block_size).hex()
|
||||
for i, key in keys {
|
||||
result = new(key, data[i], blake2b.sum384, blake2b.block_size).hex()
|
||||
assert result == blake2b_384_expected_results[i]
|
||||
}
|
||||
}
|
||||
|
@ -264,8 +264,8 @@ fn test_hmac_blake2b_256() {
|
|||
'dce7f41e3db51656ffc97259ca0ef3358cbfb41ac3e74e2dd9cd8639ab4996a0',
|
||||
]
|
||||
mut result := ''
|
||||
for i, key in hmac.keys {
|
||||
result = new(key, hmac.data[i], blake2b.sum256, blake2b.block_size).hex()
|
||||
for i, key in keys {
|
||||
result = new(key, data[i], blake2b.sum256, blake2b.block_size).hex()
|
||||
assert result == blake2b_256_expected_results[i]
|
||||
}
|
||||
}
|
||||
|
@ -281,8 +281,8 @@ fn test_hmac_blake2b_160() {
|
|||
'fc5fb8ec933174d97c7712fa8f8802467ac42b1e',
|
||||
]
|
||||
mut result := ''
|
||||
for i, key in hmac.keys {
|
||||
result = new(key, hmac.data[i], blake2b.sum160, blake2b.block_size).hex()
|
||||
for i, key in keys {
|
||||
result = new(key, data[i], blake2b.sum160, blake2b.block_size).hex()
|
||||
assert result == blake2b_160_expected_results[i]
|
||||
}
|
||||
}
|
||||
|
@ -298,8 +298,8 @@ fn test_hmac_blake3_256() {
|
|||
'dac8165b07656b282c5b9f2f2cf22569560778cb6240b11a383f2bf466f1ba36',
|
||||
]
|
||||
mut result := ''
|
||||
for i, key in hmac.keys {
|
||||
result = new(key, hmac.data[i], blake3.sum256, blake3.block_size).hex()
|
||||
for i, key in keys {
|
||||
result = new(key, data[i], blake3.sum256, blake3.block_size).hex()
|
||||
assert result == blake3_256_expected_results[i]
|
||||
}
|
||||
}
|
||||
|
@ -316,8 +316,8 @@ fn test_hmac_sha3_512() {
|
|||
]
|
||||
|
||||
mut result := ''
|
||||
for i, key in hmac.keys {
|
||||
result = new(key, hmac.data[i], sha3.sum512, sha3.rate_512).hex()
|
||||
for i, key in keys {
|
||||
result = new(key, data[i], sha3.sum512, sha3.rate_512).hex()
|
||||
assert result == sha3_512_expected_results[i]
|
||||
}
|
||||
}
|
||||
|
@ -334,8 +334,8 @@ fn test_hmac_sha3_384() {
|
|||
]
|
||||
|
||||
mut result := ''
|
||||
for i, key in hmac.keys {
|
||||
result = new(key, hmac.data[i], sha3.sum384, sha3.rate_384).hex()
|
||||
for i, key in keys {
|
||||
result = new(key, data[i], sha3.sum384, sha3.rate_384).hex()
|
||||
assert result == sha3_384_expected_results[i]
|
||||
}
|
||||
}
|
||||
|
@ -352,8 +352,8 @@ fn test_hmac_sha3_256() {
|
|||
]
|
||||
|
||||
mut result := ''
|
||||
for i, key in hmac.keys {
|
||||
result = new(key, hmac.data[i], sha3.sum256, sha3.rate_256).hex()
|
||||
for i, key in keys {
|
||||
result = new(key, data[i], sha3.sum256, sha3.rate_256).hex()
|
||||
assert result == sha3_256_expected_results[i]
|
||||
}
|
||||
}
|
||||
|
@ -370,8 +370,8 @@ fn test_hmac_sha3_224() {
|
|||
]
|
||||
|
||||
mut result := ''
|
||||
for i, key in hmac.keys {
|
||||
result = new(key, hmac.data[i], sha3.sum224, sha3.rate_224).hex()
|
||||
for i, key in keys {
|
||||
result = new(key, data[i], sha3.sum224, sha3.rate_224).hex()
|
||||
assert result == sha3_224_expected_results[i]
|
||||
}
|
||||
}
|
||||
|
|
|
@ -40,16 +40,16 @@ pub fn (mut d Digest) free() {
|
|||
|
||||
fn (mut d Digest) init() {
|
||||
d.s = []u32{len: (4)}
|
||||
d.x = []u8{len: md5.block_size}
|
||||
d.x = []u8{len: block_size}
|
||||
d.reset()
|
||||
}
|
||||
|
||||
// reset the state of the Digest `d`
|
||||
pub fn (mut d Digest) reset() {
|
||||
d.s[0] = u32(md5.init0)
|
||||
d.s[1] = u32(md5.init1)
|
||||
d.s[2] = u32(md5.init2)
|
||||
d.s[3] = u32(md5.init3)
|
||||
d.s[0] = u32(init0)
|
||||
d.s[1] = u32(init1)
|
||||
d.s[2] = u32(init2)
|
||||
d.s[3] = u32(init3)
|
||||
d.nx = 0
|
||||
d.len = 0
|
||||
}
|
||||
|
@ -78,7 +78,7 @@ pub fn (mut d Digest) write(p_ []u8) !int {
|
|||
if d.nx > 0 {
|
||||
n := copy(mut d.x[d.nx..], p)
|
||||
d.nx += n
|
||||
if d.nx == md5.block_size {
|
||||
if d.nx == block_size {
|
||||
block(mut d, d.x)
|
||||
d.nx = 0
|
||||
}
|
||||
|
@ -88,8 +88,8 @@ pub fn (mut d Digest) write(p_ []u8) !int {
|
|||
p = p[n..]
|
||||
}
|
||||
}
|
||||
if p.len >= md5.block_size {
|
||||
n := p.len & ~(md5.block_size - 1)
|
||||
if p.len >= block_size {
|
||||
n := p.len & ~(block_size - 1)
|
||||
block(mut d, p[..n])
|
||||
if n >= p.len {
|
||||
p = []
|
||||
|
@ -135,7 +135,7 @@ fn (mut d Digest) checksum_internal() []u8 {
|
|||
if d.nx != 0 {
|
||||
panic('d.nx != 0')
|
||||
}
|
||||
mut digest := []u8{len: md5.size}
|
||||
mut digest := []u8{len: size}
|
||||
binary.little_endian_put_u32(mut digest, d.s[0])
|
||||
binary.little_endian_put_u32(mut digest[4..], d.s[1])
|
||||
binary.little_endian_put_u32(mut digest[8..], d.s[2])
|
||||
|
@ -166,12 +166,12 @@ fn block(mut dig Digest, p []u8) {
|
|||
|
||||
// size returns the size of the checksum in bytes.
|
||||
pub fn (d &Digest) size() int {
|
||||
return md5.size
|
||||
return size
|
||||
}
|
||||
|
||||
// block_size returns the block size of the checksum in bytes.
|
||||
pub fn (d &Digest) block_size() int {
|
||||
return md5.block_size
|
||||
return block_size
|
||||
}
|
||||
|
||||
// hexhash returns a hexadecimal MD5 hash sum `string` of `s`.
|
||||
|
|
|
@ -2,55 +2,55 @@ module pem
|
|||
|
||||
// example PEM structures from the RFC
|
||||
fn test_decode_rfc1421() {
|
||||
for i in 0 .. pem.test_data_rfc1421.len {
|
||||
decoded, rest := decode(pem.test_data_rfc1421[i]) or { Block{}, '' }
|
||||
assert decoded == pem.expected_results_rfc1421[i]
|
||||
assert decoded == decode_only(pem.test_data_rfc1421[i]) or { Block{} }
|
||||
for i in 0 .. test_data_rfc1421.len {
|
||||
decoded, rest := decode(test_data_rfc1421[i]) or { Block{}, '' }
|
||||
assert decoded == expected_results_rfc1421[i]
|
||||
assert decoded == decode_only(test_data_rfc1421[i]) or { Block{} }
|
||||
assert rest == ''
|
||||
}
|
||||
}
|
||||
|
||||
fn test_decode() {
|
||||
for i in 0 .. pem.test_data.len {
|
||||
decoded, rest := decode(pem.test_data[i]) or { Block{}, '' }
|
||||
assert decoded == pem.expected_results[i]
|
||||
assert decoded == decode_only(pem.test_data[i]) or { Block{} }
|
||||
assert rest == pem.expected_rest[i]
|
||||
for i in 0 .. test_data.len {
|
||||
decoded, rest := decode(test_data[i]) or { Block{}, '' }
|
||||
assert decoded == expected_results[i]
|
||||
assert decoded == decode_only(test_data[i]) or { Block{} }
|
||||
assert rest == expected_rest[i]
|
||||
}
|
||||
}
|
||||
|
||||
fn test_encode_rfc1421() {
|
||||
for i in 0 .. pem.test_data_rfc1421.len {
|
||||
encoded := pem.expected_results_rfc1421[i].encode() or { '' }
|
||||
for i in 0 .. test_data_rfc1421.len {
|
||||
encoded := expected_results_rfc1421[i].encode() or { '' }
|
||||
decoded, rest := decode(encoded) or { Block{}, '' }
|
||||
assert rest == ''
|
||||
assert decoded == pem.expected_results_rfc1421[i]
|
||||
assert decoded == expected_results_rfc1421[i]
|
||||
assert decoded == decode_only(encoded) or { Block{} }
|
||||
}
|
||||
}
|
||||
|
||||
fn test_encode() {
|
||||
for i in 0 .. pem.test_data.len {
|
||||
encoded := pem.expected_results[i].encode() or { '' }
|
||||
for i in 0 .. test_data.len {
|
||||
encoded := expected_results[i].encode() or { '' }
|
||||
decoded, rest := decode(encoded) or { Block{}, '' }
|
||||
assert rest == ''
|
||||
assert decoded == pem.expected_results[i]
|
||||
assert decoded == expected_results[i]
|
||||
assert decoded == decode_only(encoded) or { Block{} }
|
||||
}
|
||||
}
|
||||
|
||||
fn test_encode_config() {
|
||||
for i in 0 .. pem.test_data.len {
|
||||
encoded := pem.expected_results[i].encode(EncodeConfig{31, '\r\n'}) or { '' }
|
||||
for i in 0 .. test_data.len {
|
||||
encoded := expected_results[i].encode(EncodeConfig{31, '\r\n'}) or { '' }
|
||||
decoded, rest := decode(encoded) or { Block{}, '' }
|
||||
assert rest == ''
|
||||
assert decoded == pem.expected_results[i]
|
||||
assert decoded == expected_results[i]
|
||||
assert decoded == decode_only(encoded) or { Block{} }
|
||||
}
|
||||
}
|
||||
|
||||
fn test_decode_no_pem() {
|
||||
for test in pem.test_data_no_pem {
|
||||
for test in test_data_no_pem {
|
||||
if _, _ := decode(test) {
|
||||
assert false, 'decode should return `none` on input without PEM data'
|
||||
}
|
||||
|
|
|
@ -14,8 +14,8 @@ pub fn read(bytes_needed int) ![]u8 {
|
|||
mut remaining_bytes := bytes_needed
|
||||
// getrandom syscall wont block if requesting <= 256 bytes
|
||||
for bytes_read < bytes_needed {
|
||||
batch_size := if remaining_bytes > rand.read_batch_size {
|
||||
rand.read_batch_size
|
||||
batch_size := if remaining_bytes > read_batch_size {
|
||||
read_batch_size
|
||||
} else {
|
||||
remaining_bytes
|
||||
}
|
||||
|
@ -30,8 +30,8 @@ pub fn read(bytes_needed int) ![]u8 {
|
|||
}
|
||||
|
||||
fn getrandom(bytes_needed int, buffer voidptr) int {
|
||||
if bytes_needed > rand.read_batch_size {
|
||||
panic('getrandom() dont request more than ${rand.read_batch_size} bytes at once.')
|
||||
if bytes_needed > read_batch_size {
|
||||
panic('getrandom() dont request more than ${read_batch_size} bytes at once.')
|
||||
}
|
||||
return unsafe { C.syscall(C.SYS_getrandom, buffer, bytes_needed, 0) }
|
||||
}
|
||||
|
|
|
@ -17,8 +17,8 @@ pub fn read(bytes_needed int) ![]u8 {
|
|||
mut remaining_bytes := bytes_needed
|
||||
// getrandom syscall wont block if requesting <= 256 bytes
|
||||
for bytes_read < bytes_needed {
|
||||
batch_size := if remaining_bytes > rand.read_batch_size {
|
||||
rand.read_batch_size
|
||||
batch_size := if remaining_bytes > read_batch_size {
|
||||
read_batch_size
|
||||
} else {
|
||||
remaining_bytes
|
||||
}
|
||||
|
@ -33,8 +33,8 @@ pub fn read(bytes_needed int) ![]u8 {
|
|||
}
|
||||
|
||||
fn v_getrandom(bytes_needed int, buffer voidptr) int {
|
||||
if bytes_needed > rand.read_batch_size {
|
||||
panic('getrandom() dont request more than ${rand.read_batch_size} bytes at once.')
|
||||
if bytes_needed > read_batch_size {
|
||||
panic('getrandom() dont request more than ${read_batch_size} bytes at once.')
|
||||
}
|
||||
return C.getrandom(buffer, bytes_needed, 0)
|
||||
}
|
||||
|
|
|
@ -15,8 +15,8 @@ const bcrypt_use_system_preferred_rng = 0x00000002
|
|||
pub fn read(bytes_needed int) ![]u8 {
|
||||
mut buffer := []u8{len: bytes_needed}
|
||||
// use bcrypt_use_system_preferred_rng because we passed null as algo
|
||||
status := C.BCryptGenRandom(0, buffer.data, bytes_needed, rand.bcrypt_use_system_preferred_rng)
|
||||
if status != rand.status_success {
|
||||
status := C.BCryptGenRandom(0, buffer.data, bytes_needed, bcrypt_use_system_preferred_rng)
|
||||
if status != status_success {
|
||||
return &ReadError{}
|
||||
}
|
||||
return buffer
|
||||
|
|
|
@ -44,18 +44,18 @@ pub fn (mut d Digest) free() {
|
|||
}
|
||||
|
||||
fn (mut d Digest) init() {
|
||||
d.x = []u8{len: sha1.chunk}
|
||||
d.x = []u8{len: chunk}
|
||||
d.h = []u32{len: (5)}
|
||||
d.reset()
|
||||
}
|
||||
|
||||
// reset the state of the Digest `d`
|
||||
pub fn (mut d Digest) reset() {
|
||||
d.h[0] = u32(sha1.init0)
|
||||
d.h[1] = u32(sha1.init1)
|
||||
d.h[2] = u32(sha1.init2)
|
||||
d.h[3] = u32(sha1.init3)
|
||||
d.h[4] = u32(sha1.init4)
|
||||
d.h[0] = u32(init0)
|
||||
d.h[1] = u32(init1)
|
||||
d.h[2] = u32(init2)
|
||||
d.h[3] = u32(init3)
|
||||
d.h[4] = u32(init4)
|
||||
d.nx = 0
|
||||
d.len = 0
|
||||
}
|
||||
|
@ -85,7 +85,7 @@ pub fn (mut d Digest) write(p_ []u8) !int {
|
|||
if d.nx > 0 {
|
||||
n := copy(mut d.x[d.nx..], p)
|
||||
d.nx += n
|
||||
if d.nx == sha1.chunk {
|
||||
if d.nx == chunk {
|
||||
block(mut d, d.x)
|
||||
d.nx = 0
|
||||
}
|
||||
|
@ -95,8 +95,8 @@ pub fn (mut d Digest) write(p_ []u8) !int {
|
|||
p = p[n..]
|
||||
}
|
||||
}
|
||||
if p.len >= sha1.chunk {
|
||||
n := p.len & ~(sha1.chunk - 1)
|
||||
if p.len >= chunk {
|
||||
n := p.len & ~(chunk - 1)
|
||||
block(mut d, p[..n])
|
||||
if n >= p.len {
|
||||
p = []
|
||||
|
@ -139,7 +139,7 @@ fn (mut d Digest) checksum_internal() []u8 {
|
|||
len <<= 3
|
||||
binary.big_endian_put_u64(mut tmp, len)
|
||||
d.write(tmp[..8]) or { panic(err) }
|
||||
mut digest := []u8{len: sha1.size}
|
||||
mut digest := []u8{len: size}
|
||||
binary.big_endian_put_u32(mut digest, d.h[0])
|
||||
binary.big_endian_put_u32(mut digest[4..], d.h[1])
|
||||
binary.big_endian_put_u32(mut digest[8..], d.h[2])
|
||||
|
@ -171,12 +171,12 @@ fn block(mut dig Digest, p []u8) {
|
|||
|
||||
// size returns the size of the checksum in bytes.
|
||||
pub fn (d &Digest) size() int {
|
||||
return sha1.size
|
||||
return size
|
||||
}
|
||||
|
||||
// block_size returns the block size of the checksum in bytes.
|
||||
pub fn (d &Digest) block_size() int {
|
||||
return sha1.block_size
|
||||
return block_size
|
||||
}
|
||||
|
||||
// hexhash returns a hexadecimal SHA1 hash sum `string` of `s`.
|
||||
|
|
|
@ -40,7 +40,7 @@ fn block_generic(mut dig Digest, p_ []u8) {
|
|||
mut i := 0
|
||||
for i < 16 {
|
||||
f := b & c | (~b) & d
|
||||
t := bits.rotate_left_32(a, 5) + f + e + w[i & 0xf] + u32(sha1._k0)
|
||||
t := bits.rotate_left_32(a, 5) + f + e + w[i & 0xf] + u32(_k0)
|
||||
e = d
|
||||
d = c
|
||||
c = bits.rotate_left_32(b, 30)
|
||||
|
@ -52,7 +52,7 @@ fn block_generic(mut dig Digest, p_ []u8) {
|
|||
tmp := w[(i - 3) & 0xf] ^ w[(i - 8) & 0xf] ^ w[(i - 14) & 0xf] ^ w[i & 0xf]
|
||||
w[i & 0xf] = (tmp << 1) | (tmp >> (32 - 1))
|
||||
f := b & c | (~b) & d
|
||||
t := bits.rotate_left_32(a, 5) + f + e + w[i & 0xf] + u32(sha1._k0)
|
||||
t := bits.rotate_left_32(a, 5) + f + e + w[i & 0xf] + u32(_k0)
|
||||
e = d
|
||||
d = c
|
||||
c = bits.rotate_left_32(b, 30)
|
||||
|
@ -64,7 +64,7 @@ fn block_generic(mut dig Digest, p_ []u8) {
|
|||
tmp := w[(i - 3) & 0xf] ^ w[(i - 8) & 0xf] ^ w[(i - 14) & 0xf] ^ w[i & 0xf]
|
||||
w[i & 0xf] = (tmp << 1) | (tmp >> (32 - 1))
|
||||
f := b ^ c ^ d
|
||||
t := bits.rotate_left_32(a, 5) + f + e + w[i & 0xf] + u32(sha1._k1)
|
||||
t := bits.rotate_left_32(a, 5) + f + e + w[i & 0xf] + u32(_k1)
|
||||
e = d
|
||||
d = c
|
||||
c = bits.rotate_left_32(b, 30)
|
||||
|
@ -76,7 +76,7 @@ fn block_generic(mut dig Digest, p_ []u8) {
|
|||
tmp := w[(i - 3) & 0xf] ^ w[(i - 8) & 0xf] ^ w[(i - 14) & 0xf] ^ w[i & 0xf]
|
||||
w[i & 0xf] = (tmp << 1) | (tmp >> (32 - 1))
|
||||
f := ((b | c) & d) | (b & c)
|
||||
t := bits.rotate_left_32(a, 5) + f + e + w[i & 0xf] + u32(sha1._k2)
|
||||
t := bits.rotate_left_32(a, 5) + f + e + w[i & 0xf] + u32(_k2)
|
||||
e = d
|
||||
d = c
|
||||
c = bits.rotate_left_32(b, 30)
|
||||
|
@ -88,7 +88,7 @@ fn block_generic(mut dig Digest, p_ []u8) {
|
|||
tmp := w[(i - 3) & 0xf] ^ w[(i - 8) & 0xf] ^ w[(i - 14) & 0xf] ^ w[i & 0xf]
|
||||
w[i & 0xf] = (tmp << 1) | (tmp >> (32 - 1))
|
||||
f := b ^ c ^ d
|
||||
t := bits.rotate_left_32(a, 5) + f + e + w[i & 0xf] + u32(sha1._k3)
|
||||
t := bits.rotate_left_32(a, 5) + f + e + w[i & 0xf] + u32(_k3)
|
||||
e = d
|
||||
d = c
|
||||
c = bits.rotate_left_32(b, 30)
|
||||
|
|
|
@ -58,30 +58,30 @@ pub fn (mut d Digest) free() {
|
|||
|
||||
fn (mut d Digest) init() {
|
||||
d.h = []u32{len: (8)}
|
||||
d.x = []u8{len: sha256.chunk}
|
||||
d.x = []u8{len: chunk}
|
||||
d.reset()
|
||||
}
|
||||
|
||||
// reset the state of the Digest `d`
|
||||
pub fn (mut d Digest) reset() {
|
||||
if !d.is224 {
|
||||
d.h[0] = u32(sha256.init0)
|
||||
d.h[1] = u32(sha256.init1)
|
||||
d.h[2] = u32(sha256.init2)
|
||||
d.h[3] = u32(sha256.init3)
|
||||
d.h[4] = u32(sha256.init4)
|
||||
d.h[5] = u32(sha256.init5)
|
||||
d.h[6] = u32(sha256.init6)
|
||||
d.h[7] = u32(sha256.init7)
|
||||
d.h[0] = u32(init0)
|
||||
d.h[1] = u32(init1)
|
||||
d.h[2] = u32(init2)
|
||||
d.h[3] = u32(init3)
|
||||
d.h[4] = u32(init4)
|
||||
d.h[5] = u32(init5)
|
||||
d.h[6] = u32(init6)
|
||||
d.h[7] = u32(init7)
|
||||
} else {
|
||||
d.h[0] = u32(sha256.init0_224)
|
||||
d.h[1] = u32(sha256.init1_224)
|
||||
d.h[2] = u32(sha256.init2_224)
|
||||
d.h[3] = u32(sha256.init3_224)
|
||||
d.h[4] = u32(sha256.init4_224)
|
||||
d.h[5] = u32(sha256.init5_224)
|
||||
d.h[6] = u32(sha256.init6_224)
|
||||
d.h[7] = u32(sha256.init7_224)
|
||||
d.h[0] = u32(init0_224)
|
||||
d.h[1] = u32(init1_224)
|
||||
d.h[2] = u32(init2_224)
|
||||
d.h[3] = u32(init3_224)
|
||||
d.h[4] = u32(init4_224)
|
||||
d.h[5] = u32(init5_224)
|
||||
d.h[6] = u32(init6_224)
|
||||
d.h[7] = u32(init7_224)
|
||||
}
|
||||
d.nx = 0
|
||||
d.len = 0
|
||||
|
@ -119,7 +119,7 @@ pub fn (mut d Digest) write(p_ []u8) !int {
|
|||
if d.nx > 0 {
|
||||
n := copy(mut d.x[d.nx..], p)
|
||||
d.nx += n
|
||||
if d.nx == sha256.chunk {
|
||||
if d.nx == chunk {
|
||||
block(mut d, d.x)
|
||||
d.nx = 0
|
||||
}
|
||||
|
@ -129,8 +129,8 @@ pub fn (mut d Digest) write(p_ []u8) !int {
|
|||
p = p[n..]
|
||||
}
|
||||
}
|
||||
if p.len >= sha256.chunk {
|
||||
n := p.len & ~(sha256.chunk - 1)
|
||||
if p.len >= chunk {
|
||||
n := p.len & ~(chunk - 1)
|
||||
block(mut d, p[..n])
|
||||
if n >= p.len {
|
||||
p = []
|
||||
|
@ -152,7 +152,7 @@ pub fn (d &Digest) sum(b_in []u8) []u8 {
|
|||
hash := d0.checksum_internal()
|
||||
mut b_out := b_in.clone()
|
||||
if d0.is224 {
|
||||
for b in hash[..sha256.size224] {
|
||||
for b in hash[..size224] {
|
||||
b_out << b
|
||||
}
|
||||
} else {
|
||||
|
@ -182,7 +182,7 @@ fn (mut d Digest) checksum_internal() []u8 {
|
|||
if d.nx != 0 {
|
||||
panic('d.nx != 0')
|
||||
}
|
||||
mut digest := []u8{len: sha256.size}
|
||||
mut digest := []u8{len: size}
|
||||
binary.big_endian_put_u32(mut digest, d.h[0])
|
||||
binary.big_endian_put_u32(mut digest[4..], d.h[1])
|
||||
binary.big_endian_put_u32(mut digest[8..], d.h[2])
|
||||
|
@ -204,7 +204,7 @@ pub fn (mut d Digest) checksum() []u8 {
|
|||
out := d.checksum_internal()
|
||||
// if this digest has `size224` length, return the correct `size224` checksum
|
||||
if d.is224 {
|
||||
return out[0..sha256.size224]
|
||||
return out[0..size224]
|
||||
}
|
||||
// otherwise, returns a normal size
|
||||
return out
|
||||
|
@ -228,8 +228,8 @@ pub fn sum224(data []u8) []u8 {
|
|||
mut d := new224()
|
||||
d.write(data) or { panic(err) }
|
||||
sum := d.checksum_internal()
|
||||
mut sum224 := []u8{len: sha256.size224}
|
||||
copy(mut sum224, sum[..sha256.size224])
|
||||
mut sum224 := []u8{len: size224}
|
||||
copy(mut sum224, sum[..size224])
|
||||
return sum224
|
||||
}
|
||||
|
||||
|
@ -242,14 +242,14 @@ fn block(mut dig Digest, p []u8) {
|
|||
// size returns the size of the checksum in bytes.
|
||||
pub fn (d &Digest) size() int {
|
||||
if !d.is224 {
|
||||
return sha256.size
|
||||
return size
|
||||
}
|
||||
return sha256.size224
|
||||
return size224
|
||||
}
|
||||
|
||||
// block_size returns the block size of the checksum in bytes.
|
||||
pub fn (d &Digest) block_size() int {
|
||||
return sha256.block_size
|
||||
return block_size
|
||||
}
|
||||
|
||||
// hexhash returns a hexadecimal SHA256 hash sum `string` of `s`.
|
||||
|
|
|
@ -113,7 +113,7 @@ fn block_generic(mut dig Digest, p_ []u8) {
|
|||
for i in 0 .. 64 {
|
||||
t1 := h +
|
||||
((bits.rotate_left_32(e, -6)) ^ (bits.rotate_left_32(e, -11)) ^ (bits.rotate_left_32(e, -25))) +
|
||||
((e & f) ^ (~e & g)) + u32(sha256._k[i]) + w[i]
|
||||
((e & f) ^ (~e & g)) + u32(_k[i]) + w[i]
|
||||
t2 :=
|
||||
((bits.rotate_left_32(a, -2)) ^ (bits.rotate_left_32(a, -13)) ^ (bits.rotate_left_32(a, -22))) +
|
||||
((a & b) ^ (a & c) ^ (b & c))
|
||||
|
|
|
@ -41,32 +41,32 @@ const xof_pad = u8(0x1f)
|
|||
|
||||
// new512 initializes the digest structure for a sha3 512 bit hash
|
||||
pub fn new512() !&Digest {
|
||||
return new_digest(sha3.rate_512, sha3.size_512)!
|
||||
return new_digest(rate_512, size_512)!
|
||||
}
|
||||
|
||||
// new384 initializes the digest structure for a sha3 384 bit hash
|
||||
pub fn new384() !&Digest {
|
||||
return new_digest(sha3.rate_384, sha3.size_384)!
|
||||
return new_digest(rate_384, size_384)!
|
||||
}
|
||||
|
||||
// new256 initializes the digest structure for a sha3 256 bit hash
|
||||
pub fn new256() !&Digest {
|
||||
return new_digest(sha3.rate_256, sha3.size_256)!
|
||||
return new_digest(rate_256, size_256)!
|
||||
}
|
||||
|
||||
// new224 initializes the digest structure for a sha3 224 bit hash
|
||||
pub fn new224() !&Digest {
|
||||
return new_digest(sha3.rate_224, sha3.size_224)!
|
||||
return new_digest(rate_224, size_224)!
|
||||
}
|
||||
|
||||
// new256_xof initializes the digest structure for a sha3 256 bit extended output function
|
||||
pub fn new256xof(output_len int) !&Digest {
|
||||
return new_xof_digest(sha3.xof_rate_256, output_len)!
|
||||
return new_xof_digest(xof_rate_256, output_len)!
|
||||
}
|
||||
|
||||
// new128_xof initializes the digest structure for a sha3 128 bit extended output function
|
||||
pub fn new128xof(output_len int) !&Digest {
|
||||
return new_xof_digest(sha3.xof_rate_128, output_len)!
|
||||
return new_xof_digest(xof_rate_128, output_len)!
|
||||
}
|
||||
|
||||
struct HashSizeError {
|
||||
|
@ -75,7 +75,7 @@ struct HashSizeError {
|
|||
}
|
||||
|
||||
fn (err HashSizeError) msg() string {
|
||||
return 'Hash size ${err.size} must be ${sha3.size_224}, ${sha3.size_256}, ${sha3.size_384}, or ${sha3.size_512}'
|
||||
return 'Hash size ${err.size} must be ${size_224}, ${size_256}, ${size_384}, or ${size_512}'
|
||||
}
|
||||
|
||||
struct AbsorptionRateError {
|
||||
|
@ -94,7 +94,7 @@ struct XOFRateError {
|
|||
}
|
||||
|
||||
fn (err XOFRateError) msg() string {
|
||||
return 'Extended output rate ${err.rate} must be ${sha3.xof_rate_128} or ${sha3.xof_rate_256}'
|
||||
return 'Extended output rate ${err.rate} must be ${xof_rate_128} or ${xof_rate_256}'
|
||||
}
|
||||
|
||||
struct XOFSizeError {
|
||||
|
@ -125,32 +125,32 @@ mut:
|
|||
// Legal values are 224, 256, 384, and 512.
|
||||
pub fn new_digest(absorption_rate int, hash_size int) !&Digest {
|
||||
match hash_size {
|
||||
sha3.size_224 {
|
||||
if absorption_rate != sha3.rate_224 {
|
||||
size_224 {
|
||||
if absorption_rate != rate_224 {
|
||||
return AbsorptionRateError{
|
||||
rate: absorption_rate
|
||||
size: hash_size
|
||||
}
|
||||
}
|
||||
}
|
||||
sha3.size_256 {
|
||||
if absorption_rate != sha3.rate_256 {
|
||||
size_256 {
|
||||
if absorption_rate != rate_256 {
|
||||
return AbsorptionRateError{
|
||||
rate: absorption_rate
|
||||
size: hash_size
|
||||
}
|
||||
}
|
||||
}
|
||||
sha3.size_384 {
|
||||
if absorption_rate != sha3.rate_384 {
|
||||
size_384 {
|
||||
if absorption_rate != rate_384 {
|
||||
return AbsorptionRateError{
|
||||
rate: absorption_rate
|
||||
size: hash_size
|
||||
}
|
||||
}
|
||||
}
|
||||
sha3.size_512 {
|
||||
if absorption_rate != sha3.rate_512 {
|
||||
size_512 {
|
||||
if absorption_rate != rate_512 {
|
||||
return AbsorptionRateError{
|
||||
rate: absorption_rate
|
||||
size: hash_size
|
||||
|
@ -166,7 +166,7 @@ pub fn new_digest(absorption_rate int, hash_size int) !&Digest {
|
|||
|
||||
d := Digest{
|
||||
rate: absorption_rate
|
||||
suffix: sha3.hash_pad
|
||||
suffix: hash_pad
|
||||
output_len: hash_size
|
||||
s: State{}
|
||||
}
|
||||
|
@ -185,7 +185,7 @@ pub fn new_digest(absorption_rate int, hash_size int) !&Digest {
|
|||
// Legal values are positive integers.
|
||||
pub fn new_xof_digest(absorption_rate int, hash_size int) !&Digest {
|
||||
match absorption_rate {
|
||||
sha3.xof_rate_128, sha3.xof_rate_256 {
|
||||
xof_rate_128, xof_rate_256 {
|
||||
if hash_size < 1 {
|
||||
return XOFSizeError{
|
||||
size: hash_size
|
||||
|
@ -201,7 +201,7 @@ pub fn new_xof_digest(absorption_rate int, hash_size int) !&Digest {
|
|||
|
||||
d := Digest{
|
||||
rate: absorption_rate
|
||||
suffix: sha3.xof_pad
|
||||
suffix: xof_pad
|
||||
output_len: hash_size
|
||||
s: State{}
|
||||
}
|
||||
|
|
|
@ -212,7 +212,7 @@ const rho_offsets = [[int(0), 36, 3, 41, 18], [int(1), 44, 10, 45, 2],
|
|||
fn (mut s State) rho() {
|
||||
for x in 0 .. 5 {
|
||||
for y in 0 .. 5 {
|
||||
s.a[x][y] = bits.rotate_left_64(s.a[x][y], sha3.rho_offsets[x][y])
|
||||
s.a[x][y] = bits.rotate_left_64(s.a[x][y], rho_offsets[x][y])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -298,7 +298,7 @@ const iota_round_constants = [u64(0x0000000000000001), 0x0000000000008082, 0x800
|
|||
// to xor with lane 0, 0.
|
||||
@[inline]
|
||||
fn (mut s State) iota(round_index int) {
|
||||
s.a[0][0] ^= sha3.iota_round_constants[round_index]
|
||||
s.a[0][0] ^= iota_round_constants[round_index]
|
||||
}
|
||||
|
||||
fn (s State) str() string {
|
||||
|
|
|
@ -135,7 +135,7 @@ fn test_2_x_24_rounds_on_zero() {
|
|||
}
|
||||
|
||||
mut round := 0
|
||||
for rv in sha3.first_24 {
|
||||
for rv in first_24 {
|
||||
s.theta()
|
||||
|
||||
println('round ${round} verifying theta')
|
||||
|
@ -187,13 +187,13 @@ fn test_2_x_24_rounds_on_zero() {
|
|||
// check the actual bytes in the state
|
||||
first_state_bytes := s.to_bytes()
|
||||
for i in 0 .. 200 {
|
||||
assert first_state_bytes[i] == sha3.first_state_as_bytes[i], 'examining state byte ${i} ${first_state_bytes[i]:02x} != ${sha3.first_state_as_bytes[i]:02x}'
|
||||
assert first_state_bytes[i] == first_state_as_bytes[i], 'examining state byte ${i} ${first_state_bytes[i]:02x} != ${first_state_as_bytes[i]:02x}'
|
||||
}
|
||||
|
||||
println('verifying using previous state')
|
||||
|
||||
round = 0
|
||||
for rv in sha3.second_24 {
|
||||
for rv in second_24 {
|
||||
s.theta()
|
||||
|
||||
println('round ${round} verifying theta')
|
||||
|
@ -245,7 +245,7 @@ fn test_2_x_24_rounds_on_zero() {
|
|||
// check the actual bytes in the state
|
||||
second_state_bytes := s.to_bytes()
|
||||
for i in 0 .. 200 {
|
||||
assert second_state_bytes[i] == sha3.second_state_as_bytes[i], 'examining state byte ${i} ${second_state_bytes[i]:02x} != ${sha3.second_state_as_bytes[i]:02x}'
|
||||
assert second_state_bytes[i] == second_state_as_bytes[i], 'examining state byte ${i} ${second_state_bytes[i]:02x} != ${second_state_as_bytes[i]:02x}'
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -255,32 +255,32 @@ fn test_to_from_bytes() {
|
|||
// going from bytes to state as 5 x 5 u64 words to bytes
|
||||
// should give you the original byte array
|
||||
|
||||
s.from_bytes(sha3.first_state_as_bytes)
|
||||
s.from_bytes(first_state_as_bytes)
|
||||
|
||||
for x in 0 .. 5 {
|
||||
for y in 0 .. 5 {
|
||||
assert s.a[x][y] == sha3.first_state_as_words[x][y], 'x ${x} y ${y} ${s.a[x][y]:016x} != ${sha3.first_state_as_words[x][y]:016x}'
|
||||
assert s.a[x][y] == first_state_as_words[x][y], 'x ${x} y ${y} ${s.a[x][y]:016x} != ${first_state_as_words[x][y]:016x}'
|
||||
}
|
||||
}
|
||||
|
||||
mut result_bytes := s.to_bytes()
|
||||
|
||||
for i in 0 .. 200 {
|
||||
assert result_bytes[i] == sha3.first_state_as_bytes[i], 'examining state byte ${i} ${result_bytes[i]:02x} != ${sha3.first_state_as_bytes[i]:02x}'
|
||||
assert result_bytes[i] == first_state_as_bytes[i], 'examining state byte ${i} ${result_bytes[i]:02x} != ${first_state_as_bytes[i]:02x}'
|
||||
}
|
||||
|
||||
s.from_bytes(sha3.second_state_as_bytes)
|
||||
s.from_bytes(second_state_as_bytes)
|
||||
|
||||
for x in 0 .. 5 {
|
||||
for y in 0 .. 5 {
|
||||
assert s.a[x][y] == sha3.second_state_as_words[x][y], 'x ${x} y ${y} ${s.a[x][y]:016x} != ${sha3.second_state_as_words[x][y]:016x}'
|
||||
assert s.a[x][y] == second_state_as_words[x][y], 'x ${x} y ${y} ${s.a[x][y]:016x} != ${second_state_as_words[x][y]:016x}'
|
||||
}
|
||||
}
|
||||
|
||||
result_bytes = s.to_bytes()
|
||||
|
||||
for i in 0 .. 200 {
|
||||
assert result_bytes[i] == sha3.second_state_as_bytes[i], 'examining state byte ${i} ${result_bytes[i]:02x} != ${sha3.second_state_as_bytes[i]:02x}'
|
||||
assert result_bytes[i] == second_state_as_bytes[i], 'examining state byte ${i} ${result_bytes[i]:02x} != ${second_state_as_bytes[i]:02x}'
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -20,16 +20,16 @@ fn test_0_length_hash() {
|
|||
input := []u8{}
|
||||
|
||||
output_224 := sum224(input)
|
||||
assert output_224 == sha3.empty_message_sha3_224
|
||||
assert output_224 == empty_message_sha3_224
|
||||
|
||||
output_256 := sum256(input)
|
||||
assert output_256 == sha3.empty_message_sha3_256
|
||||
assert output_256 == empty_message_sha3_256
|
||||
|
||||
output_384 := sum384(input)
|
||||
assert output_384 == sha3.empty_message_sha3_384
|
||||
assert output_384 == empty_message_sha3_384
|
||||
|
||||
output_512 := sum512(input)
|
||||
assert output_512 == sha3.empty_message_sha3_512
|
||||
assert output_512 == empty_message_sha3_512
|
||||
}
|
||||
|
||||
const input_200 = [u8(0xa3), 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3, 0xa3,
|
||||
|
@ -66,17 +66,17 @@ const test_200_message_sha3_512 = [u8(0xE7), 0x6D, 0xFA, 0xD2, 0x20, 0x84, 0xA8,
|
|||
0x54, 0x5A, 0x1C, 0xE8, 0xBA, 0x00]
|
||||
|
||||
fn test_200_length_hash() {
|
||||
output_224 := sum224(sha3.input_200)
|
||||
assert output_224 == sha3.test_200_message_sha3_224
|
||||
output_224 := sum224(input_200)
|
||||
assert output_224 == test_200_message_sha3_224
|
||||
|
||||
output_256 := sum256(sha3.input_200)
|
||||
assert output_256 == sha3.test_200_message_sha3_256
|
||||
output_256 := sum256(input_200)
|
||||
assert output_256 == test_200_message_sha3_256
|
||||
|
||||
output_384 := sum384(sha3.input_200)
|
||||
assert output_384 == sha3.test_200_message_sha3_384
|
||||
output_384 := sum384(input_200)
|
||||
assert output_384 == test_200_message_sha3_384
|
||||
|
||||
output_512 := sum512(sha3.input_200)
|
||||
assert output_512 == sha3.test_200_message_sha3_512
|
||||
output_512 := sum512(input_200)
|
||||
assert output_512 == test_200_message_sha3_512
|
||||
}
|
||||
|
||||
const empty_message_shake128 = [u8(0x7F), 0x9C, 0x2B, 0xA4, 0xE8, 0x8F, 0x82, 0x7D, 0x61, 0x60,
|
||||
|
@ -151,10 +151,10 @@ fn test_0_length_xof() {
|
|||
input := []u8{}
|
||||
|
||||
output_128 := shake128(input, 512)
|
||||
assert output_128 == sha3.empty_message_shake128
|
||||
assert output_128 == empty_message_shake128
|
||||
|
||||
output_256 := shake256(input, 512)
|
||||
assert output_256 == sha3.empty_message_shake256
|
||||
assert output_256 == empty_message_shake256
|
||||
}
|
||||
|
||||
const test_200_message_shake128 = [u8(0x13), 0x1A, 0xB8, 0xD2, 0xB5, 0x94, 0x94, 0x6B, 0x9C, 0x81,
|
||||
|
@ -228,9 +228,9 @@ const test_200_message_shake256 = [u8(0xCD), 0x8A, 0x92, 0x0E, 0xD1, 0x41, 0xAA,
|
|||
fn test_200_length_xof() {
|
||||
input := []u8{}
|
||||
|
||||
output_128 := shake128(sha3.input_200, 512)
|
||||
assert output_128 == sha3.test_200_message_shake128
|
||||
output_128 := shake128(input_200, 512)
|
||||
assert output_128 == test_200_message_shake128
|
||||
|
||||
output_256 := shake256(sha3.input_200, 512)
|
||||
assert output_256 == sha3.test_200_message_shake256
|
||||
output_256 := shake256(input_200, 512)
|
||||
assert output_256 == test_200_message_shake256
|
||||
}
|
||||
|
|
|
@ -80,7 +80,7 @@ pub fn (mut d Digest) free() {
|
|||
|
||||
fn (mut d Digest) init() {
|
||||
d.h = []u64{len: (8)}
|
||||
d.x = []u8{len: sha512.chunk}
|
||||
d.x = []u8{len: chunk}
|
||||
d.reset()
|
||||
}
|
||||
|
||||
|
@ -88,44 +88,44 @@ fn (mut d Digest) init() {
|
|||
pub fn (mut d Digest) reset() {
|
||||
match d.function {
|
||||
.sha384 {
|
||||
d.h[0] = sha512.init0_384
|
||||
d.h[1] = sha512.init1_384
|
||||
d.h[2] = sha512.init2_384
|
||||
d.h[3] = sha512.init3_384
|
||||
d.h[4] = sha512.init4_384
|
||||
d.h[5] = sha512.init5_384
|
||||
d.h[6] = sha512.init6_384
|
||||
d.h[7] = sha512.init7_384
|
||||
d.h[0] = init0_384
|
||||
d.h[1] = init1_384
|
||||
d.h[2] = init2_384
|
||||
d.h[3] = init3_384
|
||||
d.h[4] = init4_384
|
||||
d.h[5] = init5_384
|
||||
d.h[6] = init6_384
|
||||
d.h[7] = init7_384
|
||||
}
|
||||
.sha512_224 {
|
||||
d.h[0] = sha512.init0_224
|
||||
d.h[1] = sha512.init1_224
|
||||
d.h[2] = sha512.init2_224
|
||||
d.h[3] = sha512.init3_224
|
||||
d.h[4] = sha512.init4_224
|
||||
d.h[5] = sha512.init5_224
|
||||
d.h[6] = sha512.init6_224
|
||||
d.h[7] = sha512.init7_224
|
||||
d.h[0] = init0_224
|
||||
d.h[1] = init1_224
|
||||
d.h[2] = init2_224
|
||||
d.h[3] = init3_224
|
||||
d.h[4] = init4_224
|
||||
d.h[5] = init5_224
|
||||
d.h[6] = init6_224
|
||||
d.h[7] = init7_224
|
||||
}
|
||||
.sha512_256 {
|
||||
d.h[0] = sha512.init0_256
|
||||
d.h[1] = sha512.init1_256
|
||||
d.h[2] = sha512.init2_256
|
||||
d.h[3] = sha512.init3_256
|
||||
d.h[4] = sha512.init4_256
|
||||
d.h[5] = sha512.init5_256
|
||||
d.h[6] = sha512.init6_256
|
||||
d.h[7] = sha512.init7_256
|
||||
d.h[0] = init0_256
|
||||
d.h[1] = init1_256
|
||||
d.h[2] = init2_256
|
||||
d.h[3] = init3_256
|
||||
d.h[4] = init4_256
|
||||
d.h[5] = init5_256
|
||||
d.h[6] = init6_256
|
||||
d.h[7] = init7_256
|
||||
}
|
||||
else {
|
||||
d.h[0] = sha512.init0
|
||||
d.h[1] = sha512.init1
|
||||
d.h[2] = sha512.init2
|
||||
d.h[3] = sha512.init3
|
||||
d.h[4] = sha512.init4
|
||||
d.h[5] = sha512.init5
|
||||
d.h[6] = sha512.init6
|
||||
d.h[7] = sha512.init7
|
||||
d.h[0] = init0
|
||||
d.h[1] = init1
|
||||
d.h[2] = init2
|
||||
d.h[3] = init3
|
||||
d.h[4] = init4
|
||||
d.h[5] = init5
|
||||
d.h[6] = init6
|
||||
d.h[7] = init7
|
||||
}
|
||||
}
|
||||
d.nx = 0
|
||||
|
@ -178,7 +178,7 @@ pub fn (mut d Digest) write(p_ []u8) !int {
|
|||
if d.nx > 0 {
|
||||
n := copy(mut d.x[d.nx..], p)
|
||||
d.nx += n
|
||||
if d.nx == sha512.chunk {
|
||||
if d.nx == chunk {
|
||||
block(mut d, d.x)
|
||||
d.nx = 0
|
||||
}
|
||||
|
@ -188,8 +188,8 @@ pub fn (mut d Digest) write(p_ []u8) !int {
|
|||
p = p[n..]
|
||||
}
|
||||
}
|
||||
if p.len >= sha512.chunk {
|
||||
n := p.len & ~(sha512.chunk - 1)
|
||||
if p.len >= chunk {
|
||||
n := p.len & ~(chunk - 1)
|
||||
block(mut d, p[..n])
|
||||
if n >= p.len {
|
||||
p = []
|
||||
|
@ -212,17 +212,17 @@ pub fn (d &Digest) sum(b_in []u8) []u8 {
|
|||
mut b_out := b_in.clone()
|
||||
match d0.function {
|
||||
.sha384 {
|
||||
for b in hash[..sha512.size384] {
|
||||
for b in hash[..size384] {
|
||||
b_out << b
|
||||
}
|
||||
}
|
||||
.sha512_224 {
|
||||
for b in hash[..sha512.size224] {
|
||||
for b in hash[..size224] {
|
||||
b_out << b
|
||||
}
|
||||
}
|
||||
.sha512_256 {
|
||||
for b in hash[..sha512.size256] {
|
||||
for b in hash[..size256] {
|
||||
b_out << b
|
||||
}
|
||||
}
|
||||
|
@ -255,7 +255,7 @@ fn (mut d Digest) checksum_internal() []u8 {
|
|||
if d.nx != 0 {
|
||||
panic('d.nx != 0')
|
||||
}
|
||||
mut digest := []u8{len: sha512.size}
|
||||
mut digest := []u8{len: size}
|
||||
binary.big_endian_put_u64(mut digest, d.h[0])
|
||||
binary.big_endian_put_u64(mut digest[8..], d.h[1])
|
||||
binary.big_endian_put_u64(mut digest[16..], d.h[2])
|
||||
|
@ -277,13 +277,13 @@ pub fn (mut d Digest) checksum() []u8 {
|
|||
out := d.checksum_internal()
|
||||
match d.function {
|
||||
.sha384 {
|
||||
return out[0..sha512.size384]
|
||||
return out[0..size384]
|
||||
}
|
||||
.sha512_224 {
|
||||
return out[0..sha512.size224]
|
||||
return out[0..size224]
|
||||
}
|
||||
.sha512_256 {
|
||||
return out[0..sha512.size256]
|
||||
return out[0..size256]
|
||||
}
|
||||
else {
|
||||
return out
|
||||
|
@ -303,8 +303,8 @@ pub fn sum384(data []u8) []u8 {
|
|||
mut d := new_digest(.sha384)
|
||||
d.write(data) or { panic(err) }
|
||||
sum := d.checksum_internal()
|
||||
mut sum384 := []u8{len: sha512.size384}
|
||||
copy(mut sum384, sum[..sha512.size384])
|
||||
mut sum384 := []u8{len: size384}
|
||||
copy(mut sum384, sum[..size384])
|
||||
return sum384
|
||||
}
|
||||
|
||||
|
@ -313,8 +313,8 @@ pub fn sum512_224(data []u8) []u8 {
|
|||
mut d := new_digest(.sha512_224)
|
||||
d.write(data) or { panic(err) }
|
||||
sum := d.checksum_internal()
|
||||
mut sum224 := []u8{len: sha512.size224}
|
||||
copy(mut sum224, sum[..sha512.size224])
|
||||
mut sum224 := []u8{len: size224}
|
||||
copy(mut sum224, sum[..size224])
|
||||
return sum224
|
||||
}
|
||||
|
||||
|
@ -323,8 +323,8 @@ pub fn sum512_256(data []u8) []u8 {
|
|||
mut d := new_digest(.sha512_256)
|
||||
d.write(data) or { panic(err) }
|
||||
sum := d.checksum_internal()
|
||||
mut sum256 := []u8{len: sha512.size256}
|
||||
copy(mut sum256, sum[..sha512.size256])
|
||||
mut sum256 := []u8{len: size256}
|
||||
copy(mut sum256, sum[..size256])
|
||||
return sum256
|
||||
}
|
||||
|
||||
|
@ -337,16 +337,16 @@ fn block(mut dig Digest, p []u8) {
|
|||
// size returns the size of the checksum in bytes.
|
||||
pub fn (d &Digest) size() int {
|
||||
match d.function {
|
||||
.sha512_224 { return sha512.size224 }
|
||||
.sha512_256 { return sha512.size256 }
|
||||
.sha384 { return sha512.size384 }
|
||||
else { return sha512.size }
|
||||
.sha512_224 { return size224 }
|
||||
.sha512_256 { return size256 }
|
||||
.sha384 { return size384 }
|
||||
else { return size }
|
||||
}
|
||||
}
|
||||
|
||||
// block_size returns the block size of the checksum in bytes.
|
||||
pub fn (d &Digest) block_size() int {
|
||||
return sha512.block_size
|
||||
return block_size
|
||||
}
|
||||
|
||||
// hexhash returns a hexadecimal SHA512 hash sum `string` of `s`.
|
||||
|
|
|
@ -51,8 +51,8 @@ pub fn new_bloom_filter[T](hash_func fn (T) u32, table_size int, num_functions i
|
|||
if table_size <= 0 {
|
||||
return error('table_size should great that 0')
|
||||
}
|
||||
if num_functions < 1 || num_functions > datatypes.salts.len {
|
||||
return error('num_functions should between 1~${datatypes.salts.len}')
|
||||
if num_functions < 1 || num_functions > salts.len {
|
||||
return error('num_functions should between 1~${salts.len}')
|
||||
}
|
||||
|
||||
return &BloomFilter[T]{
|
||||
|
@ -68,7 +68,7 @@ pub fn (mut b BloomFilter[T]) add(element T) {
|
|||
hash := b.hash_func(element)
|
||||
|
||||
for i in 0 .. b.num_functions {
|
||||
subhash := hash ^ datatypes.salts[i]
|
||||
subhash := hash ^ salts[i]
|
||||
index := int(subhash % u32(b.table_size))
|
||||
bb := u8((1 << (index % 8)))
|
||||
b.table[index / 8] |= bb
|
||||
|
@ -79,7 +79,7 @@ pub fn (mut b BloomFilter[T]) add(element T) {
|
|||
pub fn (b &BloomFilter[T]) exists(element T) bool {
|
||||
hash := b.hash_func(element)
|
||||
for i in 0 .. b.num_functions {
|
||||
subhash := hash ^ datatypes.salts[i]
|
||||
subhash := hash ^ salts[i]
|
||||
index := int(subhash % u32(b.table_size))
|
||||
bb := b.table[index / 8]
|
||||
bit := 1 << (index % 8)
|
||||
|
|
|
@ -182,73 +182,73 @@ fn (stmt Stmt) get_field_count() u16 {
|
|||
|
||||
// bind_bool binds a single boolean value to the statement `stmt`
|
||||
pub fn (mut stmt Stmt) bind_bool(b &bool) {
|
||||
stmt.bind(mysql.mysql_type_tiny, b, 0)
|
||||
stmt.bind(mysql_type_tiny, b, 0)
|
||||
}
|
||||
|
||||
// bind_byte binds a single byte value to the statement `stmt`
|
||||
pub fn (mut stmt Stmt) bind_byte(b &u8) {
|
||||
stmt.bind(mysql.mysql_type_tiny, b, 0)
|
||||
stmt.bind(mysql_type_tiny, b, 0)
|
||||
}
|
||||
|
||||
// bind_u8 binds a single u8 value to the statement `stmt`
|
||||
pub fn (mut stmt Stmt) bind_u8(b &u8) {
|
||||
stmt.bind(mysql.mysql_type_tiny, b, 0)
|
||||
stmt.bind(mysql_type_tiny, b, 0)
|
||||
}
|
||||
|
||||
// bind_i8 binds a single i8 value to the statement `stmt`
|
||||
pub fn (mut stmt Stmt) bind_i8(b &i8) {
|
||||
stmt.bind(mysql.mysql_type_tiny, b, 0)
|
||||
stmt.bind(mysql_type_tiny, b, 0)
|
||||
}
|
||||
|
||||
// bind_i16 binds a single i16 value to the statement `stmt`
|
||||
pub fn (mut stmt Stmt) bind_i16(b &i16) {
|
||||
stmt.bind(mysql.mysql_type_short, b, 0)
|
||||
stmt.bind(mysql_type_short, b, 0)
|
||||
}
|
||||
|
||||
// bind_u16 binds a single u16 value to the statement `stmt`
|
||||
pub fn (mut stmt Stmt) bind_u16(b &u16) {
|
||||
stmt.bind(mysql.mysql_type_short, b, 0)
|
||||
stmt.bind(mysql_type_short, b, 0)
|
||||
}
|
||||
|
||||
// bind_int binds a single int value to the statement `stmt`
|
||||
pub fn (mut stmt Stmt) bind_int(b &int) {
|
||||
stmt.bind(mysql.mysql_type_long, b, 0)
|
||||
stmt.bind(mysql_type_long, b, 0)
|
||||
}
|
||||
|
||||
// bind_u32 binds a single u32 value to the statement `stmt`
|
||||
pub fn (mut stmt Stmt) bind_u32(b &u32) {
|
||||
stmt.bind(mysql.mysql_type_long, b, 0)
|
||||
stmt.bind(mysql_type_long, b, 0)
|
||||
}
|
||||
|
||||
// bind_i64 binds a single i64 value to the statement `stmt`
|
||||
pub fn (mut stmt Stmt) bind_i64(b &i64) {
|
||||
stmt.bind(mysql.mysql_type_longlong, b, 0)
|
||||
stmt.bind(mysql_type_longlong, b, 0)
|
||||
}
|
||||
|
||||
// bind_u64 binds a single u64 value to the statement `stmt`
|
||||
pub fn (mut stmt Stmt) bind_u64(b &u64) {
|
||||
stmt.bind(mysql.mysql_type_longlong, b, 0)
|
||||
stmt.bind(mysql_type_longlong, b, 0)
|
||||
}
|
||||
|
||||
// bind_f32 binds a single f32 value to the statement `stmt`
|
||||
pub fn (mut stmt Stmt) bind_f32(b &f32) {
|
||||
stmt.bind(mysql.mysql_type_float, b, 0)
|
||||
stmt.bind(mysql_type_float, b, 0)
|
||||
}
|
||||
|
||||
// bind_f64 binds a single f64 value to the statement `stmt`
|
||||
pub fn (mut stmt Stmt) bind_f64(b &f64) {
|
||||
stmt.bind(mysql.mysql_type_double, b, 0)
|
||||
stmt.bind(mysql_type_double, b, 0)
|
||||
}
|
||||
|
||||
// bind_text binds a single string value to the statement `stmt`
|
||||
pub fn (mut stmt Stmt) bind_text(b string) {
|
||||
stmt.bind(mysql.mysql_type_string, b.str, u32(b.len))
|
||||
stmt.bind(mysql_type_string, b.str, u32(b.len))
|
||||
}
|
||||
|
||||
// bind_null binds a single NULL value to the statement `stmt`
|
||||
pub fn (mut stmt Stmt) bind_null() {
|
||||
stmt.binds << C.MYSQL_BIND{
|
||||
buffer_type: mysql.mysql_type_null
|
||||
buffer_type: mysql_type_null
|
||||
length: 0
|
||||
}
|
||||
}
|
||||
|
|
|
@ -188,7 +188,7 @@ pub fn (db &DB) q_int(query string) !int {
|
|||
}
|
||||
C.sqlite3_prepare_v2(db.conn, &char(query.str), query.len, &stmt, 0)
|
||||
code := C.sqlite3_step(stmt)
|
||||
if code != sqlite.sqlite_row {
|
||||
if code != sqlite_row {
|
||||
return db.error_message(code, query)
|
||||
}
|
||||
|
||||
|
@ -204,7 +204,7 @@ pub fn (db &DB) q_string(query string) !string {
|
|||
}
|
||||
C.sqlite3_prepare_v2(db.conn, &char(query.str), query.len, &stmt, 0)
|
||||
code := C.sqlite3_step(stmt)
|
||||
if code != sqlite.sqlite_row {
|
||||
if code != sqlite_row {
|
||||
return db.error_message(code, query)
|
||||
}
|
||||
|
||||
|
@ -220,7 +220,7 @@ pub fn (db &DB) exec(query string) ![]Row {
|
|||
C.sqlite3_finalize(stmt)
|
||||
}
|
||||
mut code := C.sqlite3_prepare_v2(db.conn, &char(query.str), query.len, &stmt, 0)
|
||||
if code != sqlite.sqlite_ok {
|
||||
if code != sqlite_ok {
|
||||
return db.error_message(code, query)
|
||||
}
|
||||
|
||||
|
@ -259,7 +259,7 @@ pub fn (db &DB) exec_one(query string) !Row {
|
|||
if rows.len == 0 {
|
||||
return &SQLError{
|
||||
msg: 'No rows'
|
||||
code: sqlite.sqlite_done
|
||||
code: sqlite_done
|
||||
}
|
||||
}
|
||||
res := rows[0]
|
||||
|
@ -314,7 +314,7 @@ pub fn (db &DB) exec_param_many(query string, params []string) ![]Row {
|
|||
mut rows := []Row{}
|
||||
for {
|
||||
res = C.sqlite3_step(stmt)
|
||||
if res != sqlite.sqlite_row {
|
||||
if res != sqlite_row {
|
||||
if rows.len == 0 && is_error(res) {
|
||||
return db.error_message(res, query)
|
||||
}
|
||||
|
|
|
@ -23,7 +23,7 @@ pub fn get_shared_library_extension() string {
|
|||
// shared libraries.
|
||||
@[inline]
|
||||
pub fn get_libname(libname string) string {
|
||||
return '${libname}${dl.dl_ext}'
|
||||
return '${libname}${dl_ext}'
|
||||
}
|
||||
|
||||
// open_opt tries to load a given dynamic shared object.
|
||||
|
|
|
@ -28,7 +28,7 @@ __global (
|
|||
|
||||
fn register_dl_loader(dl_loader &DynamicLibLoader) ! {
|
||||
if dl_loader.key in registered_dl_loaders {
|
||||
return loader.dl_register_issue_err
|
||||
return dl_register_issue_err
|
||||
}
|
||||
registered_dl_loaders[dl_loader.key] = dl_loader
|
||||
}
|
||||
|
@ -77,7 +77,7 @@ fn new_dynamic_lib_loader(conf DynamicLibLoaderConfig) !&DynamicLibLoader {
|
|||
paths << conf.paths
|
||||
|
||||
if paths.len == 0 {
|
||||
return loader.dl_no_path_issue_err
|
||||
return dl_no_path_issue_err
|
||||
}
|
||||
|
||||
mut dl_loader := &DynamicLibLoader{
|
||||
|
@ -112,7 +112,7 @@ pub fn (mut dl_loader DynamicLibLoader) open() !voidptr {
|
|||
}
|
||||
}
|
||||
|
||||
return loader.dl_open_issue_err
|
||||
return dl_open_issue_err
|
||||
}
|
||||
|
||||
// close closes the dynamic library.
|
||||
|
@ -124,7 +124,7 @@ pub fn (mut dl_loader DynamicLibLoader) close() ! {
|
|||
}
|
||||
}
|
||||
|
||||
return loader.dl_close_issue_err
|
||||
return dl_close_issue_err
|
||||
}
|
||||
|
||||
// get_sym gets a symbol from the dynamic library.
|
||||
|
@ -140,7 +140,7 @@ pub fn (mut dl_loader DynamicLibLoader) get_sym(name string) !voidptr {
|
|||
}
|
||||
|
||||
dl_loader.close()!
|
||||
return loader.dl_sym_issue_err
|
||||
return dl_sym_issue_err
|
||||
}
|
||||
|
||||
// unregister unregisters the DynamicLibLoader.
|
||||
|
|
|
@ -63,7 +63,7 @@ fn chunk_overhead() usize {
|
|||
|
||||
@[inline]
|
||||
fn min_large_size() usize {
|
||||
return 1 << dlmalloc.tree_bin_shift
|
||||
return 1 << tree_bin_shift
|
||||
}
|
||||
|
||||
@[inline]
|
||||
|
@ -140,15 +140,15 @@ fn is_aligned(a usize) bool {
|
|||
}
|
||||
|
||||
fn is_small(s usize) bool {
|
||||
return s >> dlmalloc.small_bin_shift < dlmalloc.n_small_bins
|
||||
return s >> small_bin_shift < n_small_bins
|
||||
}
|
||||
|
||||
fn small_index2size(idx u32) usize {
|
||||
return usize(idx) << dlmalloc.small_bin_shift
|
||||
return usize(idx) << small_bin_shift
|
||||
}
|
||||
|
||||
fn small_index(size usize) u32 {
|
||||
return u32(size >> dlmalloc.small_bin_shift)
|
||||
return u32(size >> small_bin_shift)
|
||||
}
|
||||
|
||||
fn align_up(a usize, alignment usize) usize {
|
||||
|
@ -169,10 +169,10 @@ fn least_bit(x u32) u32 {
|
|||
|
||||
fn leftshift_for_tree_index(x u32) u32 {
|
||||
y := usize(x)
|
||||
if y == dlmalloc.n_tree_bins - 1 {
|
||||
if y == n_tree_bins - 1 {
|
||||
return 0
|
||||
} else {
|
||||
return u32(sizeof(usize) * 8 - 1 - ((y >> 1) + dlmalloc.tree_bin_shift - 2))
|
||||
return u32(sizeof(usize) * 8 - 1 - ((y >> 1) + tree_bin_shift - 2))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -242,8 +242,8 @@ pub fn new(system_allocator Allocator) Dlmalloc {
|
|||
return Dlmalloc{
|
||||
smallmap: 0
|
||||
treemap: 0
|
||||
smallbins: unsafe { [(dlmalloc.n_small_bins + 1) * 2]&Chunk{} }
|
||||
treebins: unsafe { [dlmalloc.n_tree_bins]&TreeChunk{} }
|
||||
smallbins: unsafe { [(n_small_bins + 1) * 2]&Chunk{} }
|
||||
treebins: unsafe { [n_tree_bins]&TreeChunk{} }
|
||||
dvsize: 0
|
||||
topsize: 0
|
||||
dv: unsafe { nil }
|
||||
|
@ -293,15 +293,15 @@ const inuse = pinuse | cinuse
|
|||
const flag_bits = pinuse | cinuse | flag4
|
||||
|
||||
fn fencepost_head() usize {
|
||||
return dlmalloc.inuse | sizeof(usize)
|
||||
return inuse | sizeof(usize)
|
||||
}
|
||||
|
||||
fn (c &Chunk) size() usize {
|
||||
return c.head & ~dlmalloc.flag_bits
|
||||
return c.head & ~flag_bits
|
||||
}
|
||||
|
||||
fn (c &Chunk) mmapped() bool {
|
||||
return c.head & dlmalloc.inuse == 0
|
||||
return c.head & inuse == 0
|
||||
}
|
||||
|
||||
fn (c &Chunk) next() &Chunk {
|
||||
|
@ -317,39 +317,39 @@ fn (c &Chunk) prev() &Chunk {
|
|||
}
|
||||
|
||||
fn (c &Chunk) cinuse() bool {
|
||||
return c.head & dlmalloc.cinuse != 0
|
||||
return c.head & cinuse != 0
|
||||
}
|
||||
|
||||
fn (c &Chunk) pinuse() bool {
|
||||
return c.head & dlmalloc.pinuse != 0
|
||||
return c.head & pinuse != 0
|
||||
}
|
||||
|
||||
fn (mut c Chunk) clear_pinuse() {
|
||||
c.head &= ~dlmalloc.pinuse
|
||||
c.head &= ~pinuse
|
||||
}
|
||||
|
||||
fn (c &Chunk) inuse() bool {
|
||||
return c.head & dlmalloc.inuse != dlmalloc.pinuse
|
||||
return c.head & inuse != pinuse
|
||||
}
|
||||
|
||||
fn (mut c Chunk) set_inuse(size usize) {
|
||||
c.head = (c.head & dlmalloc.pinuse) | size | dlmalloc.cinuse
|
||||
c.head = (c.head & pinuse) | size | cinuse
|
||||
mut next := c.plus_offset(size)
|
||||
next.head |= dlmalloc.pinuse
|
||||
next.head |= pinuse
|
||||
}
|
||||
|
||||
fn (mut c Chunk) set_inuse_and_pinuse(size usize) {
|
||||
c.head = dlmalloc.pinuse | size | dlmalloc.cinuse
|
||||
c.head = pinuse | size | cinuse
|
||||
mut next := c.plus_offset(size)
|
||||
next.head |= dlmalloc.pinuse
|
||||
next.head |= pinuse
|
||||
}
|
||||
|
||||
fn (mut c Chunk) set_size_and_pinuse_of_inuse_chunk(size usize) {
|
||||
c.head = size | dlmalloc.pinuse | dlmalloc.cinuse
|
||||
c.head = size | pinuse | cinuse
|
||||
}
|
||||
|
||||
fn (mut c Chunk) set_size_and_pinuse_of_free_chunk(size usize) {
|
||||
c.head = size | dlmalloc.pinuse
|
||||
c.head = size | pinuse
|
||||
c.set_foot(size)
|
||||
}
|
||||
|
||||
|
@ -395,7 +395,7 @@ fn (tree &TreeChunk) chunk() &Chunk {
|
|||
}
|
||||
|
||||
fn (tree &TreeChunk) size(treemap u32) usize {
|
||||
return tree.chunk.head & ~dlmalloc.flag_bits
|
||||
return tree.chunk.head & ~flag_bits
|
||||
}
|
||||
|
||||
@[unsafe]
|
||||
|
@ -415,7 +415,7 @@ fn (tree &TreeChunk) prev() &TreeChunk {
|
|||
const extern = 1 << 0
|
||||
|
||||
fn (seg &Segment) is_extern() bool {
|
||||
return seg.flags & dlmalloc.extern != 0
|
||||
return seg.flags & extern != 0
|
||||
}
|
||||
|
||||
fn (seg &Segment) can_release_part(sys_alloc &Allocator) bool {
|
||||
|
@ -453,14 +453,14 @@ fn (mut dl Dlmalloc) treebin_at(idx u32) &&TreeChunk {
|
|||
}
|
||||
|
||||
fn (dl &Dlmalloc) compute_tree_index(size usize) u32 {
|
||||
x := size >> dlmalloc.tree_bin_shift
|
||||
x := size >> tree_bin_shift
|
||||
if x == 0 {
|
||||
return 0
|
||||
} else if x > 0xffff {
|
||||
return dlmalloc.n_tree_bins - 1
|
||||
return n_tree_bins - 1
|
||||
} else {
|
||||
k := sizeof(usize) * 8 - 1 - usize_leading_zeros(x)
|
||||
return u32((k << 1) + ((size >> (k + dlmalloc.tree_bin_shift - 1)) & 1))
|
||||
return u32((k << 1) + ((size >> (k + tree_bin_shift - 1)) & 1))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -615,7 +615,7 @@ pub fn (mut dl Dlmalloc) free_(mem voidptr) {
|
|||
p = prev
|
||||
if voidptr(p) != voidptr(dl.dv) {
|
||||
dl.unlink_chunk(p, prevsize)
|
||||
} else if (next.head & dlmalloc.inuse) == dlmalloc.inuse {
|
||||
} else if (next.head & inuse) == inuse {
|
||||
dl.dvsize = psize
|
||||
p.set_free_with_pinuse(psize, next)
|
||||
|
||||
|
@ -630,7 +630,7 @@ pub fn (mut dl Dlmalloc) free_(mem voidptr) {
|
|||
|
||||
tsize := dl.topsize
|
||||
dl.top = p
|
||||
p.head = tsize | dlmalloc.pinuse
|
||||
p.head = tsize | pinuse
|
||||
if voidptr(p) == voidptr(dl.dv) {
|
||||
dl.dv = nil
|
||||
dl.dvsize = 0
|
||||
|
@ -765,10 +765,10 @@ fn (mut dl Dlmalloc) release_unused_segments() usize {
|
|||
pred = sp
|
||||
sp = next
|
||||
}
|
||||
dl.release_checks = if nsegs > dlmalloc.max_release_check_rate {
|
||||
dl.release_checks = if nsegs > max_release_check_rate {
|
||||
nsegs
|
||||
} else {
|
||||
dlmalloc.max_release_check_rate
|
||||
max_release_check_rate
|
||||
}
|
||||
return released
|
||||
}
|
||||
|
@ -1024,7 +1024,7 @@ fn (mut dl Dlmalloc) malloc_real(size usize) voidptr {
|
|||
mut p := dl.top
|
||||
dl.top = p.plus_offset(nb)
|
||||
mut r := dl.top
|
||||
r.head = rsize | dlmalloc.pinuse
|
||||
r.head = rsize | pinuse
|
||||
p.set_size_and_pinuse_of_inuse_chunk(nb)
|
||||
ret := p.to_mem()
|
||||
|
||||
|
@ -1038,7 +1038,7 @@ fn (mut dl Dlmalloc) malloc_real(size usize) voidptr {
|
|||
@[unsafe]
|
||||
fn (mut dl Dlmalloc) init_bins() {
|
||||
unsafe {
|
||||
for i in 0 .. dlmalloc.n_small_bins {
|
||||
for i in 0 .. n_small_bins {
|
||||
mut bin := dl.smallbin_at(i)
|
||||
bin.prev = bin
|
||||
bin.next = bin
|
||||
|
@ -1056,7 +1056,7 @@ fn (mut dl Dlmalloc) init_top(ptr &Chunk, size_ usize) {
|
|||
dl.top = p
|
||||
dl.topsize = size
|
||||
// C.VALGRIND_MAKE_MEM_UNDEFINED(p.plus_offset(sizeof(usize)),sizeof(usize))
|
||||
p.head = size | dlmalloc.pinuse
|
||||
p.head = size | pinuse
|
||||
// C.VALGRIND_MAKE_MEM_UNDEFINED(p.plus_offset(size + sizeof(usize)),sizeof(usize))
|
||||
p.plus_offset(size).head = top_foot_size()
|
||||
dl.trim_check = u32(default_trim_threshold())
|
||||
|
@ -1088,7 +1088,7 @@ fn (mut dl Dlmalloc) sys_alloc(size usize) voidptr {
|
|||
dl.seg.base = tbase
|
||||
dl.seg.size = tsize
|
||||
dl.seg.flags = flags
|
||||
dl.release_checks = dlmalloc.max_release_check_rate
|
||||
dl.release_checks = max_release_check_rate
|
||||
dl.init_bins()
|
||||
tsize_ := tsize - top_foot_size()
|
||||
dl.init_top(&Chunk(tbase), tsize_)
|
||||
|
@ -1131,7 +1131,7 @@ fn (mut dl Dlmalloc) sys_alloc(size usize) voidptr {
|
|||
mut p := dl.top
|
||||
dl.top = p.plus_offset(size)
|
||||
mut r := dl.top
|
||||
r.head = rsize | dlmalloc.pinuse
|
||||
r.head = rsize | pinuse
|
||||
p.set_size_and_pinuse_of_inuse_chunk(size)
|
||||
ret := p.to_mem()
|
||||
|
||||
|
@ -1270,7 +1270,7 @@ fn (mut dl Dlmalloc) prepend_alloc(newbase voidptr, oldbase voidptr, size usize)
|
|||
dl.topsize += qsize
|
||||
tsize := dl.topsize
|
||||
dl.top = q
|
||||
q.head = tsize | dlmalloc.pinuse
|
||||
q.head = tsize | pinuse
|
||||
} else if voidptr(oldfirst) == voidptr(dl.dv) {
|
||||
dl.dvsize += qsize
|
||||
dsize := dl.dvsize
|
||||
|
@ -1475,7 +1475,7 @@ fn (mut dl Dlmalloc) try_realloc_chunk(p_ &Chunk, nb usize, can_move bool) &Chun
|
|||
newtopsize := newsize - nb
|
||||
mut newtop := p.plus_offset(nb)
|
||||
p.set_inuse(nb)
|
||||
newtop.head = newtopsize | dlmalloc.pinuse
|
||||
newtop.head = newtopsize | pinuse
|
||||
dl.top = newtop
|
||||
dl.topsize = newtopsize
|
||||
return p
|
||||
|
@ -1589,7 +1589,7 @@ fn (mut dl Dlmalloc) dispose_chunk(p_ &Chunk, psize_ usize) {
|
|||
p = prev
|
||||
if voidptr(p) != voidptr(dl.dv) {
|
||||
dl.unlink_chunk(p, prevsize)
|
||||
} else if next.head & dlmalloc.inuse == dlmalloc.inuse {
|
||||
} else if next.head & inuse == inuse {
|
||||
dl.dvsize = psize
|
||||
p.set_free_with_pinuse(psize, next)
|
||||
return
|
||||
|
@ -1601,7 +1601,7 @@ fn (mut dl Dlmalloc) dispose_chunk(p_ &Chunk, psize_ usize) {
|
|||
dl.topsize += psize
|
||||
tsize := dl.topsize
|
||||
dl.top = p
|
||||
p.head = tsize | dlmalloc.pinuse
|
||||
p.head = tsize | pinuse
|
||||
if voidptr(p) == voidptr(dl.dv) {
|
||||
dl.dv = nil
|
||||
dl.dvsize = 0
|
||||
|
|
|
@ -40,7 +40,7 @@ pub fn decode_to_string(src []u8) !string {
|
|||
// decode decodes a byte array `src` using Base32
|
||||
// and returns the decoded bytes or a `corrupt_input_error_msg` error.
|
||||
pub fn decode(src []u8) ![]u8 {
|
||||
mut e := new_encoding(base32.std_alphabet)
|
||||
mut e := new_encoding(std_alphabet)
|
||||
return e.decode(src)
|
||||
}
|
||||
|
||||
|
@ -59,7 +59,7 @@ pub fn encode_to_string(src []u8) string {
|
|||
// encode encodes a byte array `src` using Base32 and returns the
|
||||
// encoded bytes.
|
||||
pub fn encode(src []u8) []u8 {
|
||||
e := new_encoding(base32.std_alphabet)
|
||||
e := new_encoding(std_alphabet)
|
||||
return e.encode(src)
|
||||
}
|
||||
|
||||
|
@ -78,7 +78,7 @@ pub fn (enc &Encoding) encode_string_to_string(src string) string {
|
|||
// new_std_encoding creates a standard Base32 `Encoding` as defined in
|
||||
// RFC 4648.
|
||||
pub fn new_std_encoding() Encoding {
|
||||
return new_encoding_with_padding(base32.std_alphabet, base32.std_padding)
|
||||
return new_encoding_with_padding(std_alphabet, std_padding)
|
||||
}
|
||||
|
||||
// new_std_encoding creates a standard Base32 `Encoding` identical to
|
||||
|
@ -88,13 +88,13 @@ pub fn new_std_encoding() Encoding {
|
|||
// be contained in the `Encoding`'s alphabet and must be a rune equal or
|
||||
// below '\xff'.
|
||||
pub fn new_std_encoding_with_padding(padding u8) Encoding {
|
||||
return new_encoding_with_padding(base32.std_alphabet, padding)
|
||||
return new_encoding_with_padding(std_alphabet, padding)
|
||||
}
|
||||
|
||||
// new_encoding returns a Base32 `Encoding` with standard
|
||||
// `alphabet`s and standard padding.
|
||||
pub fn new_encoding(alphabet []u8) Encoding {
|
||||
return new_encoding_with_padding(alphabet, base32.std_padding)
|
||||
return new_encoding_with_padding(alphabet, std_padding)
|
||||
}
|
||||
|
||||
// new_encoding_with_padding returns a Base32 `Encoding` with specified
|
||||
|
@ -193,7 +193,7 @@ fn (enc &Encoding) encode_(src_ []u8, mut dst []u8) {
|
|||
|
||||
// Pad the final quantum
|
||||
if src.len < 5 {
|
||||
if enc.padding_char == base32.no_padding {
|
||||
if enc.padding_char == no_padding {
|
||||
break
|
||||
}
|
||||
|
||||
|
@ -218,7 +218,7 @@ fn (enc &Encoding) encode_(src_ []u8, mut dst []u8) {
|
|||
}
|
||||
|
||||
fn (enc &Encoding) encoded_len(n int) int {
|
||||
if enc.padding_char == base32.no_padding {
|
||||
if enc.padding_char == no_padding {
|
||||
return (n * 8 + 4) / 5
|
||||
}
|
||||
return (n + 4) / 5 * 8
|
||||
|
@ -276,7 +276,7 @@ fn (enc &Encoding) decode_(src_ []u8, mut dst []u8) !(int, bool) {
|
|||
|
||||
for j := 0; j < 8; {
|
||||
if src.len == 0 {
|
||||
if enc.padding_char != base32.no_padding {
|
||||
if enc.padding_char != no_padding {
|
||||
// We have reached the end and are missing padding
|
||||
// return n, false, corrupt_input_error(olen - src.len - j)
|
||||
return error(corrupt_input_error_msg(olen - src.len - j))
|
||||
|
|
|
@ -55,7 +55,7 @@ pub mut:
|
|||
end_index i64 = -1
|
||||
|
||||
end_line u8 = `\n`
|
||||
end_line_len int = csv.endline_cr_len // size of the endline rune \n = 1, \r\n = 2
|
||||
end_line_len int = endline_cr_len // size of the endline rune \n = 1, \r\n = 2
|
||||
separator u8 = `,` // comma is the default separator
|
||||
separator_len int = 1 // size of the separator rune
|
||||
quote u8 = `"` // double quote is the standard quote char
|
||||
|
@ -91,7 +91,7 @@ pub:
|
|||
comment u8 = `#` // every line that start with the quote char is ignored
|
||||
default_cell string = '*' // return this string if out of the csv boundaries
|
||||
empty_cell string // return this string if empty cell
|
||||
end_line_len int = csv.endline_cr_len // size of the endline rune
|
||||
end_line_len int = endline_cr_len // size of the endline rune
|
||||
quote u8 = `"` // double quote is the standard quote char
|
||||
quote_remove bool // if true clear the cell from the quotes
|
||||
}
|
||||
|
@ -115,7 +115,7 @@ pub fn csv_reader(cfg RandomAccessReaderConfig) !&RandomAccessReader {
|
|||
cr.end_index = cfg.end_index
|
||||
|
||||
if cfg.scr_buf != 0 && cfg.scr_buf_len > 0 {
|
||||
cr.mem_buf_type = csv.ram_csv // RAM buffer
|
||||
cr.mem_buf_type = ram_csv // RAM buffer
|
||||
cr.mem_buf = cfg.scr_buf
|
||||
cr.mem_buf_size = cfg.scr_buf_len
|
||||
if cfg.end_index == -1 {
|
||||
|
@ -136,7 +136,7 @@ pub fn csv_reader(cfg RandomAccessReaderConfig) !&RandomAccessReader {
|
|||
if !os.exists(cfg.file_path) {
|
||||
return error('ERROR: file ${cfg.file_path} not found!')
|
||||
}
|
||||
cr.mem_buf_type = csv.file_csv // File buffer
|
||||
cr.mem_buf_type = file_csv // File buffer
|
||||
// allocate the memory
|
||||
unsafe {
|
||||
cr.mem_buf = malloc(cfg.mem_buf_size)
|
||||
|
@ -184,9 +184,9 @@ pub fn csv_reader(cfg RandomAccessReaderConfig) !&RandomAccessReader {
|
|||
|
||||
// dispose_csv_reader release the resources used by the csv_reader
|
||||
pub fn (mut cr RandomAccessReader) dispose_csv_reader() {
|
||||
if cr.mem_buf_type == csv.ram_csv {
|
||||
if cr.mem_buf_type == ram_csv {
|
||||
// do nothing, ram buffer is static
|
||||
} else if cr.mem_buf_type == csv.file_csv {
|
||||
} else if cr.mem_buf_type == file_csv {
|
||||
// file close
|
||||
if cr.f.is_opened {
|
||||
cr.f.close()
|
||||
|
@ -205,7 +205,7 @@ pub fn (mut cr RandomAccessReader) dispose_csv_reader() {
|
|||
|
||||
fn (mut cr RandomAccessReader) fill_buffer(i i64) !i64 {
|
||||
// use ram
|
||||
if cr.mem_buf_type == csv.ram_csv {
|
||||
if cr.mem_buf_type == ram_csv {
|
||||
// do nothing, ram buffer are static for now
|
||||
cr.mem_buf_start = i
|
||||
cr.mem_buf_end = cr.mem_buf_size
|
||||
|
@ -213,7 +213,7 @@ fn (mut cr RandomAccessReader) fill_buffer(i i64) !i64 {
|
|||
// println("fill_buffer RAM: ${i} read_bytes_count: ${read_bytes_count} mem_buf_start: ${cr.mem_buf_start} mem_buf_end: ${cr.mem_buf_end}")
|
||||
return i64(read_bytes_count)
|
||||
// use file
|
||||
} else if cr.mem_buf_type == csv.file_csv {
|
||||
} else if cr.mem_buf_type == file_csv {
|
||||
cr.start_index = i
|
||||
cr.f.seek(cr.start_index, .start)!
|
||||
// IMPORTANT: add 64 bit support in vlib!!
|
||||
|
@ -240,7 +240,7 @@ pub fn (mut cr RandomAccessReader) map_csv() ! {
|
|||
mut quote_flag := false // true if we are parsing inside a quote
|
||||
|
||||
// if File return to the start of the file
|
||||
if cr.mem_buf_type == csv.file_csv {
|
||||
if cr.mem_buf_type == file_csv {
|
||||
cr.f.seek(cr.start_index, .start)!
|
||||
}
|
||||
|
||||
|
@ -325,7 +325,7 @@ pub fn (mut cr RandomAccessReader) map_csv() ! {
|
|||
}
|
||||
|
||||
// if File return to the start of the file
|
||||
if cr.mem_buf_type == csv.file_csv {
|
||||
if cr.mem_buf_type == file_csv {
|
||||
cr.f.seek(cr.start_index, .start)!
|
||||
}
|
||||
|
||||
|
@ -522,7 +522,7 @@ pub fn (mut cr RandomAccessReader) rows_count() !i64 {
|
|||
mut count := i64(0)
|
||||
mut i := i64(0)
|
||||
|
||||
if cr.mem_buf_type == csv.file_csv {
|
||||
if cr.mem_buf_type == file_csv {
|
||||
cr.f.seek(cr.start_index, .start)!
|
||||
}
|
||||
unsafe {
|
||||
|
@ -542,7 +542,7 @@ pub fn (mut cr RandomAccessReader) rows_count() !i64 {
|
|||
i += read_bytes_count
|
||||
}
|
||||
}
|
||||
if cr.mem_buf_type == csv.file_csv {
|
||||
if cr.mem_buf_type == file_csv {
|
||||
cr.f.seek(cr.start_index, .start)!
|
||||
}
|
||||
// println("rows_count Done!")
|
||||
|
|
|
@ -28,9 +28,9 @@ const unescape_quote_seq = ['"', '"', ''', "'"]
|
|||
// support through `string` is robust enough to deal with these cases.
|
||||
pub fn escape(input string, config EscapeConfig) string {
|
||||
return if config.quote {
|
||||
input.replace_each(html.escape_seq).replace_each(html.escape_quote_seq)
|
||||
input.replace_each(escape_seq).replace_each(escape_quote_seq)
|
||||
} else {
|
||||
input.replace_each(html.escape_seq)
|
||||
input.replace_each(escape_seq)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -41,9 +41,9 @@ pub fn unescape(input string, config UnescapeConfig) string {
|
|||
return if config.all {
|
||||
unescape_all(input)
|
||||
} else if config.quote {
|
||||
input.replace_each(html.unescape_seq).replace_each(html.unescape_quote_seq)
|
||||
input.replace_each(unescape_seq).replace_each(unescape_quote_seq)
|
||||
} else {
|
||||
input.replace_each(html.unescape_seq)
|
||||
input.replace_each(unescape_seq)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -58,7 +58,7 @@ fn find_file_marker(data string) (string, string, string) {
|
|||
if name != '' {
|
||||
return data[..i], name, after
|
||||
}
|
||||
j := data[i..].index(txtar.nlm) or { return fix_nl(data), '', '' }
|
||||
j := data[i..].index(nlm) or { return fix_nl(data), '', '' }
|
||||
i += j + 1 // positioned at start of new possible marker
|
||||
}
|
||||
return '', '', ''
|
||||
|
@ -68,7 +68,7 @@ fn find_file_marker(data string) (string, string, string) {
|
|||
// If so, it returns the name from the line, and the data after the line.
|
||||
// Otherwise it returns name == "".
|
||||
fn is_marker(data string) (string, string) {
|
||||
if !data.starts_with(txtar.mstart) {
|
||||
if !data.starts_with(mstart) {
|
||||
return '', ''
|
||||
}
|
||||
mut ndata := data
|
||||
|
@ -77,10 +77,10 @@ fn is_marker(data string) (string, string) {
|
|||
if i >= 0 {
|
||||
ndata, after = data[..i], data[i + 1..]
|
||||
}
|
||||
if !(ndata.ends_with(txtar.mend) && ndata.len >= txtar.mstart.len + txtar.mend.len) {
|
||||
if !(ndata.ends_with(mend) && ndata.len >= mstart.len + mend.len) {
|
||||
return '', ''
|
||||
}
|
||||
name := ndata[txtar.mstart.len..ndata.len - txtar.mend.len].trim_space()
|
||||
name := ndata[mstart.len..ndata.len - mend.len].trim_space()
|
||||
return name, after
|
||||
}
|
||||
|
||||
|
|
|
@ -34,10 +34,10 @@ pub fn display_width(s string, ambiguous_width int) int {
|
|||
// width_property_at returns the East Asian Width properties at string[index]
|
||||
pub fn east_asian_width_property_at(s string, index int) EastAsianWidthProperty {
|
||||
codepoint := utf8.get_uchar(s, index)
|
||||
mut left, mut right := 0, east_asian.east_asian_width_data.len - 1
|
||||
mut left, mut right := 0, east_asian_width_data.len - 1
|
||||
for left <= right {
|
||||
middle := left + ((right - left) / 2)
|
||||
entry := east_asian.east_asian_width_data[middle]
|
||||
entry := east_asian_width_data[middle]
|
||||
if codepoint < entry.point {
|
||||
right = middle - 1
|
||||
continue
|
||||
|
|
|
@ -1332,7 +1332,7 @@ fn is_excluding_latin(table &RangeTable, r rune) bool {
|
|||
const linear_max = 18
|
||||
|
||||
fn is_16(ranges []Range16, r u16) bool {
|
||||
if ranges.len <= utf8.linear_max && r <= utf8.max_latin_1 {
|
||||
if ranges.len <= linear_max && r <= max_latin_1 {
|
||||
for range in ranges {
|
||||
if r < range.lo {
|
||||
return false
|
||||
|
@ -1363,7 +1363,7 @@ fn is_16(ranges []Range16, r u16) bool {
|
|||
}
|
||||
|
||||
fn is_32(ranges []Range32, r u32) bool {
|
||||
if ranges.len <= utf8.linear_max && r <= utf8.max_latin_1 {
|
||||
if ranges.len <= linear_max && r <= max_latin_1 {
|
||||
for range in ranges {
|
||||
if r < range.lo {
|
||||
return false
|
||||
|
|
|
@ -176,7 +176,7 @@ pub fn is_number(r rune) bool {
|
|||
|
||||
// is_uchar_punct return true if the input unicode is a western unicode punctuation
|
||||
pub fn is_uchar_punct(uchar int) bool {
|
||||
return find_punct_in_table(uchar, utf8.unicode_punct_western) != 0
|
||||
return find_punct_in_table(uchar, unicode_punct_western) != 0
|
||||
}
|
||||
|
||||
// Global
|
||||
|
@ -188,7 +188,7 @@ pub fn is_global_punct(s string, index int) bool {
|
|||
|
||||
// is_uchar_global_punct return true if the input unicode is a global unicode punctuation
|
||||
pub fn is_uchar_global_punct(uchar int) bool {
|
||||
return find_punct_in_table(uchar, utf8.unicode_punct) != 0
|
||||
return find_punct_in_table(uchar, unicode_punct) != 0
|
||||
}
|
||||
|
||||
// Private functions
|
||||
|
|
|
@ -21,7 +21,7 @@ pub const default_entities_reverse = {
|
|||
@[params]
|
||||
pub struct EscapeConfig {
|
||||
pub:
|
||||
reverse_entities map[string]string = xml.default_entities_reverse
|
||||
reverse_entities map[string]string = default_entities_reverse
|
||||
}
|
||||
|
||||
// escape_text replaces all entities in the given string with their respective
|
||||
|
@ -40,7 +40,7 @@ pub fn escape_text(content string, config EscapeConfig) string {
|
|||
@[params]
|
||||
pub struct UnescapeConfig {
|
||||
pub:
|
||||
entities map[string]string = xml.default_entities
|
||||
entities map[string]string = default_entities
|
||||
}
|
||||
|
||||
// unescape_text replaces all entities in the given string with their respective
|
||||
|
|
|
@ -93,7 +93,7 @@ fn parse_attributes(attribute_contents string) !map[string]string {
|
|||
}
|
||||
|
||||
fn parse_comment(mut reader io.Reader) !XMLComment {
|
||||
mut comment_buffer := strings.new_builder(xml.default_string_builder_cap)
|
||||
mut comment_buffer := strings.new_builder(default_string_builder_cap)
|
||||
|
||||
mut local_buf := [u8(0)]
|
||||
for {
|
||||
|
@ -128,7 +128,7 @@ enum CDATAParserState {
|
|||
}
|
||||
|
||||
fn parse_cdata(mut reader io.Reader) !XMLCData {
|
||||
mut contents_buf := strings.new_builder(xml.default_string_builder_cap)
|
||||
mut contents_buf := strings.new_builder(default_string_builder_cap)
|
||||
|
||||
mut state := CDATAParserState.normal
|
||||
mut local_buf := [u8(0)]
|
||||
|
@ -176,7 +176,7 @@ fn parse_cdata(mut reader io.Reader) !XMLCData {
|
|||
fn parse_entity(contents string) !(DTDEntity, string) {
|
||||
// We find the nearest '>' to the start of the ENTITY
|
||||
entity_end := contents.index('>') or { return error('Entity declaration not closed.') }
|
||||
entity_contents := contents[xml.entity_len..entity_end]
|
||||
entity_contents := contents[entity_len..entity_end]
|
||||
|
||||
name := entity_contents.trim_left(' \t\n').all_before(' ')
|
||||
if name == '' {
|
||||
|
@ -195,7 +195,7 @@ fn parse_entity(contents string) !(DTDEntity, string) {
|
|||
fn parse_element(contents string) !(DTDElement, string) {
|
||||
// We find the nearest '>' to the start of the ELEMENT
|
||||
element_end := contents.index('>') or { return error('Element declaration not closed.') }
|
||||
element_contents := contents[xml.element_len..element_end].trim_left(' \t\n')
|
||||
element_contents := contents[element_len..element_end].trim_left(' \t\n')
|
||||
|
||||
mut name_span := TextSpan{}
|
||||
|
||||
|
@ -248,7 +248,7 @@ fn parse_element(contents string) !(DTDElement, string) {
|
|||
fn parse_doctype(mut reader io.Reader) !DocumentType {
|
||||
// We may have more < in the doctype so keep count
|
||||
mut depth := 1
|
||||
mut doctype_buffer := strings.new_builder(xml.default_string_builder_cap)
|
||||
mut doctype_buffer := strings.new_builder(default_string_builder_cap)
|
||||
mut local_buf := [u8(0)]
|
||||
for {
|
||||
ch := next_char(mut reader, mut local_buf)!
|
||||
|
@ -309,13 +309,13 @@ fn parse_prolog(mut reader io.Reader) !(Prolog, u8) {
|
|||
`<` {
|
||||
break
|
||||
}
|
||||
xml.byte_order_marking_first {
|
||||
byte_order_marking_first {
|
||||
// UTF-8 BOM
|
||||
mut bom_buf := [u8(0), 0]
|
||||
if reader.read(mut bom_buf)! != 2 {
|
||||
return error('Invalid UTF-8 BOM.')
|
||||
}
|
||||
if bom_buf != xml.byte_order_marking_bytes {
|
||||
if bom_buf != byte_order_marking_bytes {
|
||||
return error('Invalid UTF-8 BOM.')
|
||||
}
|
||||
ch = next_char(mut reader, mut local_buf)!
|
||||
|
@ -347,7 +347,7 @@ fn parse_prolog(mut reader io.Reader) !(Prolog, u8) {
|
|||
return error('Expecting a prolog starting with "<?xml".')
|
||||
}
|
||||
|
||||
mut prolog_buffer := strings.new_builder(xml.default_string_builder_cap)
|
||||
mut prolog_buffer := strings.new_builder(default_string_builder_cap)
|
||||
|
||||
// Keep reading character by character until we find the end of the prolog
|
||||
mut found_question_mark := false
|
||||
|
@ -380,7 +380,7 @@ fn parse_prolog(mut reader io.Reader) !(Prolog, u8) {
|
|||
prolog_attributes := prolog_buffer.str().trim_space()
|
||||
|
||||
attributes := if prolog_attributes.len == 0 {
|
||||
xml.default_prolog_attributes
|
||||
default_prolog_attributes
|
||||
} else {
|
||||
parse_attributes(prolog_attributes)!
|
||||
}
|
||||
|
@ -423,7 +423,7 @@ fn parse_prolog(mut reader io.Reader) !(Prolog, u8) {
|
|||
if reader.read(mut doc_buf)! != 6 {
|
||||
return error('Invalid DOCTYPE.')
|
||||
}
|
||||
if doc_buf != xml.doctype_chars {
|
||||
if doc_buf != doctype_chars {
|
||||
return error('Invalid DOCTYPE.')
|
||||
}
|
||||
found_doctype = true
|
||||
|
@ -453,7 +453,7 @@ fn parse_prolog(mut reader io.Reader) !(Prolog, u8) {
|
|||
}
|
||||
|
||||
fn parse_children(name string, attributes map[string]string, mut reader io.Reader) !XMLNode {
|
||||
mut inner_contents := strings.new_builder(xml.default_string_builder_cap)
|
||||
mut inner_contents := strings.new_builder(default_string_builder_cap)
|
||||
|
||||
mut children := []XMLNodeContents{}
|
||||
mut local_buf := [u8(0)]
|
||||
|
@ -470,17 +470,17 @@ fn parse_children(name string, attributes map[string]string, mut reader io.Reade
|
|||
if reader.read(mut next_two)! != 2 {
|
||||
return error('Invalid XML. Incomplete comment or CDATA declaration.')
|
||||
}
|
||||
if next_two == xml.double_dash {
|
||||
if next_two == double_dash {
|
||||
// Comment
|
||||
comment := parse_comment(mut reader)!
|
||||
children << comment
|
||||
} else if next_two == xml.c_tag {
|
||||
} else if next_two == c_tag {
|
||||
// <![CDATA -> DATA
|
||||
mut cdata_buf := []u8{len: 4}
|
||||
if reader.read(mut cdata_buf)! != 4 {
|
||||
return error('Invalid XML. Incomplete CDATA declaration.')
|
||||
}
|
||||
if cdata_buf != xml.data_chars {
|
||||
if cdata_buf != data_chars {
|
||||
return error('Invalid XML. Expected "CDATA" after "<![C".')
|
||||
}
|
||||
cdata := parse_cdata(mut reader)!
|
||||
|
@ -545,7 +545,7 @@ fn parse_children(name string, attributes map[string]string, mut reader io.Reade
|
|||
// opposed to the recommended static functions makes it easier to parse smaller nodes in extremely large
|
||||
// XML documents without running out of memory.
|
||||
pub fn parse_single_node(first_char u8, mut reader io.Reader) !XMLNode {
|
||||
mut contents := strings.new_builder(xml.default_string_builder_cap)
|
||||
mut contents := strings.new_builder(default_string_builder_cap)
|
||||
contents.write_u8(first_char)
|
||||
|
||||
mut local_buf := [u8(0)]
|
||||
|
|
|
@ -94,7 +94,7 @@ const xml_elements = [
|
|||
|
||||
fn test_single_element_parsing() ! {
|
||||
mut reader := FullBufferReader{
|
||||
contents: xml.sample_doc.bytes()
|
||||
contents: sample_doc.bytes()
|
||||
}
|
||||
// Skip the "<root>" tag
|
||||
mut skip := []u8{len: 6}
|
||||
|
@ -105,7 +105,7 @@ fn test_single_element_parsing() ! {
|
|||
|
||||
mut count := 0
|
||||
|
||||
for count < xml.xml_elements.len {
|
||||
for count < xml_elements.len {
|
||||
match ch {
|
||||
`<` {
|
||||
next_ch := next_char(mut reader, mut local_buf)!
|
||||
|
@ -113,7 +113,7 @@ fn test_single_element_parsing() ! {
|
|||
`/` {}
|
||||
else {
|
||||
parsed_element := parse_single_node(next_ch, mut reader)!
|
||||
assert xml.xml_elements[count] == parsed_element
|
||||
assert xml_elements[count] == parsed_element
|
||||
count++
|
||||
}
|
||||
}
|
||||
|
|
|
@ -69,7 +69,7 @@ const dedup_buffer_len = 20
|
|||
// publish publish an event with provided Params & name.
|
||||
fn (mut pb Publisher[T]) publish(name T, sender voidptr, args voidptr) {
|
||||
// println('Publisher.publish(name=${name} sender=${sender} args=${args})')
|
||||
mut handled_receivers := unsafe { [eventbus.dedup_buffer_len]voidptr{} } // handle duplicate bugs TODO fix properly + perf
|
||||
mut handled_receivers := unsafe { [dedup_buffer_len]voidptr{} } // handle duplicate bugs TODO fix properly + perf
|
||||
// is_key_down := name == 'on_key_down'
|
||||
mut j := 0
|
||||
for event in pb.registry.events {
|
||||
|
@ -83,7 +83,7 @@ fn (mut pb Publisher[T]) publish(name T, sender voidptr, args voidptr) {
|
|||
event.handler(event.receiver, args, sender)
|
||||
// handled_receivers << event.receiver
|
||||
handled_receivers[j] = event.receiver
|
||||
j = (j + 1) % eventbus.dedup_buffer_len
|
||||
j = (j + 1) % dedup_buffer_len
|
||||
}
|
||||
}
|
||||
pb.registry.events = pb.registry.events.filter(!(it.name == name && it.once))
|
||||
|
|
|
@ -144,7 +144,7 @@ pub fn new_flag_parser(args []string) &FlagParser {
|
|||
idx_dashdash: idx_dashdash
|
||||
all_after_dashdash: all_after_dashdash
|
||||
args: all_before_dashdash
|
||||
max_free_args: flag.max_args_number
|
||||
max_free_args: max_args_number
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -502,8 +502,8 @@ pub fn (mut fs FlagParser) string(name string, abbr u8, sdefault string, usage s
|
|||
// at least `n` in length. If the user gives less free arguments to the program,
|
||||
// the parser will return an error.
|
||||
pub fn (mut fs FlagParser) limit_free_args_to_at_least(n int) ! {
|
||||
if n > flag.max_args_number {
|
||||
return error('flag.limit_free_args_to_at_least expect n to be smaller than ${flag.max_args_number}')
|
||||
if n > max_args_number {
|
||||
return error('flag.limit_free_args_to_at_least expect n to be smaller than ${max_args_number}')
|
||||
}
|
||||
if n <= 0 {
|
||||
return error('flag.limit_free_args_to_at_least expect n to be a positive number')
|
||||
|
@ -515,8 +515,8 @@ pub fn (mut fs FlagParser) limit_free_args_to_at_least(n int) ! {
|
|||
// at exactly `n` in length. If the user gives more or less free arguments to the program,
|
||||
// the parser will return an error.
|
||||
pub fn (mut fs FlagParser) limit_free_args_to_exactly(n int) ! {
|
||||
if n > flag.max_args_number {
|
||||
return error('flag.limit_free_args_to_exactly expect n to be smaller than ${flag.max_args_number}')
|
||||
if n > max_args_number {
|
||||
return error('flag.limit_free_args_to_exactly expect n to be smaller than ${max_args_number}')
|
||||
}
|
||||
if n < 0 {
|
||||
return error('flag.limit_free_args_to_exactly expect n to be a non negative number')
|
||||
|
@ -547,7 +547,7 @@ pub fn (mut fs FlagParser) arguments_description(description string) {
|
|||
// That screen is usually shown when the `--help` option is given to the program.
|
||||
pub fn (fs FlagParser) usage() string {
|
||||
positive_min_arg := (fs.min_free_args > 0)
|
||||
positive_max_arg := (fs.max_free_args > 0 && fs.max_free_args != flag.max_args_number)
|
||||
positive_max_arg := (fs.max_free_args > 0 && fs.max_free_args != max_args_number)
|
||||
no_arguments := (fs.min_free_args == 0 && fs.max_free_args == 0)
|
||||
mut adesc := if fs.args_description.len > 0 { fs.args_description } else { '[ARGS]' }
|
||||
if no_arguments {
|
||||
|
@ -556,7 +556,7 @@ pub fn (fs FlagParser) usage() string {
|
|||
mut use := []string{}
|
||||
if fs.application_version != '' {
|
||||
use << '${fs.application_name} ${fs.application_version}'
|
||||
use << '${flag.underline}'
|
||||
use << '${underline}'
|
||||
}
|
||||
if fs.usage_examples.len == 0 {
|
||||
use << 'Usage: ${fs.application_name} [options] ${adesc}'
|
||||
|
@ -611,10 +611,10 @@ pub fn (fs FlagParser) usage() string {
|
|||
}
|
||||
option_names := ' ' + onames.join(', ')
|
||||
mut xspace := ''
|
||||
if option_names.len > flag.space.len - 2 {
|
||||
xspace = '\n${flag.space}'
|
||||
if option_names.len > space.len - 2 {
|
||||
xspace = '\n${space}'
|
||||
} else {
|
||||
xspace = flag.space[option_names.len..]
|
||||
xspace = space[option_names.len..]
|
||||
}
|
||||
fdesc := '${option_names}${xspace}${f.usage}'
|
||||
use << fdesc
|
||||
|
|
|
@ -533,8 +533,8 @@ const small_circle_segments = [0, 2, 4, 6, 6, 8, 8, 13, 10, 18, 12, 12, 10, 13,
|
|||
fn radius_to_segments(r f32) int {
|
||||
if r < 30 {
|
||||
ir := int(math.ceil(r))
|
||||
if ir > 0 && ir < gg.small_circle_segments.len {
|
||||
return gg.small_circle_segments[ir]
|
||||
if ir > 0 && ir < small_circle_segments.len {
|
||||
return small_circle_segments[ir]
|
||||
}
|
||||
return ir
|
||||
}
|
||||
|
|
|
@ -611,7 +611,7 @@ pub fn (ctx &Context) end(options EndOptions) {
|
|||
create_default_pass(ctx.clear_pass)
|
||||
}
|
||||
.passthru {
|
||||
create_default_pass(gg.dontcare_pass)
|
||||
create_default_pass(dontcare_pass)
|
||||
}
|
||||
}
|
||||
gfx.begin_pass(pass)
|
||||
|
|
|
@ -258,7 +258,7 @@ pub:
|
|||
const size = Size{0, 0}
|
||||
|
||||
pub fn window_size() Size {
|
||||
return gg.size
|
||||
return size
|
||||
}
|
||||
|
||||
pub struct Context {
|
||||
|
@ -324,7 +324,7 @@ pub fn new_context(cfg Config) &Context {
|
|||
g.width = cfg.width
|
||||
g.height = cfg.height
|
||||
g.ui_mode = cfg.ui_mode
|
||||
mut sz := gg.size
|
||||
mut sz := size
|
||||
sz.height = g.height
|
||||
sz.width = g.width
|
||||
g.config = cfg
|
||||
|
|
|
@ -46,7 +46,7 @@ pub fn (a Mat4) clean() Mat4 {
|
|||
unsafe {
|
||||
x := Mat4{}
|
||||
for c, value in a.e {
|
||||
if f32_abs(value) < m4.precision {
|
||||
if f32_abs(value) < precision {
|
||||
x.e[c] = 0
|
||||
} else {
|
||||
x.e[c] = value
|
||||
|
@ -70,7 +70,7 @@ pub fn (x Mat4) sum_all() f32 {
|
|||
pub fn (x Mat4) is_equal(y Mat4) bool {
|
||||
unsafe {
|
||||
for c, value in x.e {
|
||||
if f32_abs(value - y.e[c]) > m4.precision {
|
||||
if f32_abs(value - y.e[c]) > precision {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,15 +7,15 @@ import os
|
|||
// record_frame acts according to settings specified in `gg.recorder_settings`.
|
||||
@[if gg_record ?]
|
||||
pub fn (mut ctx Context) record_frame() {
|
||||
if ctx.frame in gg.recorder_settings.screenshot_frames {
|
||||
screenshot_file_path := '${gg.recorder_settings.screenshot_prefix}${ctx.frame}.png'
|
||||
if ctx.frame in recorder_settings.screenshot_frames {
|
||||
screenshot_file_path := '${recorder_settings.screenshot_prefix}${ctx.frame}.png'
|
||||
$if gg_record_trace ? {
|
||||
eprintln('>>> screenshoting ${screenshot_file_path}')
|
||||
}
|
||||
|
||||
sapp.screenshot_png(screenshot_file_path) or { panic(err) }
|
||||
}
|
||||
if ctx.frame == gg.recorder_settings.stop_at_frame {
|
||||
if ctx.frame == recorder_settings.stop_at_frame {
|
||||
$if gg_record_trace ? {
|
||||
eprintln('>>> exiting at frame ${ctx.frame}')
|
||||
}
|
||||
|
|
|
@ -309,7 +309,7 @@ pub fn color_from_string(s string) Color {
|
|||
mut hex_str := '0x' + s[1..]
|
||||
return hex(hex_str.int())
|
||||
} else {
|
||||
return gx.string_colors[s]
|
||||
return string_colors[s]
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -58,6 +58,6 @@ pub fn new(poly int) &Crc32 {
|
|||
|
||||
// sum calculates the CRC-32 checksum of `b` by using the IEEE polynomial.
|
||||
pub fn sum(b []u8) u32 {
|
||||
c := new(int(crc32.ieee))
|
||||
c := new(int(ieee))
|
||||
return c.sum32(b)
|
||||
}
|
||||
|
|
|
@ -11,9 +11,9 @@ const fnv32_prime = u32(16777619)
|
|||
// sum32 returns a fnv1a hash of the string, described by `data`
|
||||
@[direct_array_access; inline]
|
||||
pub fn sum32_string(data string) u32 {
|
||||
mut hash := fnv1a.fnv32_offset_basis
|
||||
mut hash := fnv32_offset_basis
|
||||
for i in 0 .. data.len {
|
||||
hash = (hash ^ u32(data[i])) * fnv1a.fnv32_prime
|
||||
hash = (hash ^ u32(data[i])) * fnv32_prime
|
||||
}
|
||||
return hash
|
||||
}
|
||||
|
@ -22,9 +22,9 @@ pub fn sum32_string(data string) u32 {
|
|||
// byte array `data`.
|
||||
@[direct_array_access; inline]
|
||||
pub fn sum32(data []u8) u32 {
|
||||
mut hash := fnv1a.fnv32_offset_basis
|
||||
mut hash := fnv32_offset_basis
|
||||
for i in 0 .. data.len {
|
||||
hash = (hash ^ u32(data[i])) * fnv1a.fnv32_prime
|
||||
hash = (hash ^ u32(data[i])) * fnv32_prime
|
||||
}
|
||||
return hash
|
||||
}
|
||||
|
@ -34,9 +34,9 @@ pub fn sum32(data []u8) u32 {
|
|||
pub fn sum32_struct[T](s &T) u32 {
|
||||
bp := unsafe { &u8(s) }
|
||||
sz := int(sizeof(T))
|
||||
mut hash := fnv1a.fnv32_offset_basis
|
||||
mut hash := fnv32_offset_basis
|
||||
for i in 0 .. sz {
|
||||
hash = unsafe { (hash ^ u32(bp[i])) * fnv1a.fnv32_prime }
|
||||
hash = unsafe { (hash ^ u32(bp[i])) * fnv32_prime }
|
||||
}
|
||||
return hash
|
||||
}
|
||||
|
@ -45,9 +45,9 @@ pub fn sum32_struct[T](s &T) u32 {
|
|||
// the address in the given &byte pointer `data`.
|
||||
@[direct_array_access; inline; unsafe]
|
||||
pub fn sum32_bytes(data &u8, data_len int) u32 {
|
||||
mut hash := fnv1a.fnv32_offset_basis
|
||||
mut hash := fnv32_offset_basis
|
||||
for i in 0 .. data_len {
|
||||
hash = unsafe { (hash ^ u32(data[i])) * fnv1a.fnv32_prime }
|
||||
hash = unsafe { (hash ^ u32(data[i])) * fnv32_prime }
|
||||
}
|
||||
return hash
|
||||
}
|
||||
|
@ -55,9 +55,9 @@ pub fn sum32_bytes(data &u8, data_len int) u32 {
|
|||
// sum64 returns a fnv1a hash of the string, described by `data`
|
||||
@[direct_array_access; inline]
|
||||
pub fn sum64_string(data string) u64 {
|
||||
mut hash := fnv1a.fnv64_offset_basis
|
||||
mut hash := fnv64_offset_basis
|
||||
for i in 0 .. data.len {
|
||||
hash = (hash ^ u64(data[i])) * fnv1a.fnv64_prime
|
||||
hash = (hash ^ u64(data[i])) * fnv64_prime
|
||||
}
|
||||
return hash
|
||||
}
|
||||
|
@ -66,9 +66,9 @@ pub fn sum64_string(data string) u64 {
|
|||
// byte array `data`.
|
||||
@[direct_array_access; inline]
|
||||
pub fn sum64(data []u8) u64 {
|
||||
mut hash := fnv1a.fnv64_offset_basis
|
||||
mut hash := fnv64_offset_basis
|
||||
for i in 0 .. data.len {
|
||||
hash = (hash ^ u64(data[i])) * fnv1a.fnv64_prime
|
||||
hash = (hash ^ u64(data[i])) * fnv64_prime
|
||||
}
|
||||
return hash
|
||||
}
|
||||
|
@ -77,9 +77,9 @@ pub fn sum64(data []u8) u64 {
|
|||
// the address in the given &byte pointer `data`.
|
||||
@[direct_array_access; inline; unsafe]
|
||||
pub fn sum64_bytes(data &u8, data_len int) u64 {
|
||||
mut hash := fnv1a.fnv64_offset_basis
|
||||
mut hash := fnv64_offset_basis
|
||||
for i in 0 .. data_len {
|
||||
hash = unsafe { (hash ^ u64(data[i])) * fnv1a.fnv64_prime }
|
||||
hash = unsafe { (hash ^ u64(data[i])) * fnv64_prime }
|
||||
}
|
||||
return hash
|
||||
}
|
||||
|
@ -89,9 +89,9 @@ pub fn sum64_bytes(data &u8, data_len int) u64 {
|
|||
pub fn sum64_struct[T](s &T) u64 {
|
||||
bp := unsafe { &u8(s) }
|
||||
sz := int(sizeof(T))
|
||||
mut hash := fnv1a.fnv64_offset_basis
|
||||
mut hash := fnv64_offset_basis
|
||||
for i in 0 .. sz {
|
||||
hash = unsafe { (hash ^ u64(bp[i])) * fnv1a.fnv64_prime }
|
||||
hash = unsafe { (hash ^ u64(bp[i])) * fnv64_prime }
|
||||
}
|
||||
return hash
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ const buf_max_len = 1024
|
|||
// until either EOF is reached on `src` or an error occurs.
|
||||
// An error is returned if an error is encountered during write.
|
||||
pub fn cp(mut src Reader, mut dst Writer) ! {
|
||||
mut buf := []u8{len: io.buf_max_len}
|
||||
mut buf := []u8{len: buf_max_len}
|
||||
for {
|
||||
len := src.read(mut buf) or { break }
|
||||
dst.write(buf[..len]) or { return err }
|
||||
|
|
|
@ -46,7 +46,7 @@ pub fn read_all(config ReadAllConfig) ![]u8 {
|
|||
mut r := config.reader
|
||||
read_till_eof := config.read_to_end_of_stream
|
||||
|
||||
mut b := []u8{len: io.read_all_len}
|
||||
mut b := []u8{len: read_all_len}
|
||||
mut read := 0
|
||||
for {
|
||||
new_read := r.read(mut b[read..]) or { break }
|
||||
|
@ -55,7 +55,7 @@ pub fn read_all(config ReadAllConfig) ![]u8 {
|
|||
break
|
||||
}
|
||||
if b.len == read {
|
||||
unsafe { b.grow_len(io.read_all_grow_len) }
|
||||
unsafe { b.grow_len(read_all_grow_len) }
|
||||
}
|
||||
}
|
||||
return b[..read]
|
||||
|
@ -64,7 +64,7 @@ pub fn read_all(config ReadAllConfig) ![]u8 {
|
|||
// read_any reads any available bytes from a reader
|
||||
// (until the reader returns a read of 0 length).
|
||||
pub fn read_any(mut r Reader) ![]u8 {
|
||||
mut b := []u8{len: io.read_all_len}
|
||||
mut b := []u8{len: read_all_len}
|
||||
mut read := 0
|
||||
for {
|
||||
new_read := r.read(mut b[read..]) or { return error('none') }
|
||||
|
@ -73,7 +73,7 @@ pub fn read_any(mut r Reader) ![]u8 {
|
|||
break
|
||||
}
|
||||
if b.len == read {
|
||||
unsafe { b.grow_len(io.read_all_grow_len) }
|
||||
unsafe { b.grow_len(read_all_grow_len) }
|
||||
}
|
||||
}
|
||||
return b[..read]
|
||||
|
|
|
@ -56,7 +56,7 @@ fn (mut s StringReaderTest) read(mut buf []u8) !int {
|
|||
const newline_count = 100000
|
||||
|
||||
fn test_stringreadertest() {
|
||||
text := '12345\n'.repeat(io.newline_count)
|
||||
text := '12345\n'.repeat(newline_count)
|
||||
mut s := StringReaderTest{
|
||||
text: text
|
||||
}
|
||||
|
@ -64,7 +64,7 @@ fn test_stringreadertest() {
|
|||
for i := 0; true; i++ {
|
||||
if _ := r.read_line() {
|
||||
} else {
|
||||
assert i == io.newline_count
|
||||
assert i == newline_count
|
||||
break
|
||||
}
|
||||
}
|
||||
|
@ -81,7 +81,7 @@ fn test_stringreadertest() {
|
|||
}
|
||||
|
||||
fn test_stringreadertest2() {
|
||||
text := '12345\r\n'.repeat(io.newline_count)
|
||||
text := '12345\r\n'.repeat(newline_count)
|
||||
mut s := StringReaderTest{
|
||||
text: text
|
||||
}
|
||||
|
@ -89,7 +89,7 @@ fn test_stringreadertest2() {
|
|||
for i := 0; true; i++ {
|
||||
if _ := r.read_line() {
|
||||
} else {
|
||||
assert i == io.newline_count
|
||||
assert i == newline_count
|
||||
break
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,7 +24,7 @@ pub fn temp_file(tfo TempFileOptions) !(os.File, string) {
|
|||
}
|
||||
d = d.trim_right(os.path_separator)
|
||||
prefix, suffix := prefix_and_suffix(tfo.pattern) or { return error(@FN + ' ${err.msg()}') }
|
||||
for retry := 0; retry < util.retries; retry++ {
|
||||
for retry := 0; retry < retries; retry++ {
|
||||
path := os.join_path(d, prefix + random_number() + suffix)
|
||||
mut mode := 'rw+'
|
||||
$if windows {
|
||||
|
@ -36,7 +36,7 @@ pub fn temp_file(tfo TempFileOptions) !(os.File, string) {
|
|||
}
|
||||
}
|
||||
return error(@FN +
|
||||
' could not create temporary file in "${d}". Retry limit (${util.retries}) exhausted. Please ensure write permissions.')
|
||||
' could not create temporary file in "${d}". Retry limit (${retries}) exhausted. Please ensure write permissions.')
|
||||
}
|
||||
|
||||
@[params]
|
||||
|
@ -59,7 +59,7 @@ pub fn temp_dir(tdo TempFileOptions) !string {
|
|||
os.ensure_folder_is_writable(d) or { return error_for_temporary_folder(@FN, d) }
|
||||
d = d.trim_right(os.path_separator)
|
||||
prefix, suffix := prefix_and_suffix(tdo.pattern) or { return error(@FN + ' ${err.msg()}') }
|
||||
for retry := 0; retry < util.retries; retry++ {
|
||||
for retry := 0; retry < retries; retry++ {
|
||||
path := os.join_path(d, prefix + random_number() + suffix)
|
||||
os.mkdir_all(path) or { continue }
|
||||
if os.is_dir(path) && os.exists(path) {
|
||||
|
@ -67,7 +67,7 @@ pub fn temp_dir(tdo TempFileOptions) !string {
|
|||
return path
|
||||
}
|
||||
}
|
||||
return error('${@FN} could not create temporary directory "${d}". Retry limit (${util.retries}) exhausted.')
|
||||
return error('${@FN} could not create temporary directory "${d}". Retry limit (${retries}) exhausted.')
|
||||
}
|
||||
|
||||
// * Utility functions
|
||||
|
|
|
@ -119,9 +119,9 @@ fn multiply_digit_array(operand_a []u32, operand_b []u32, mut storage []u32) {
|
|||
} else {
|
||||
operand_b.len
|
||||
}
|
||||
if max_len >= big.toom3_multiplication_limit {
|
||||
if max_len >= toom3_multiplication_limit {
|
||||
toom3_multiply_digit_array(operand_a, operand_b, mut storage)
|
||||
} else if max_len >= big.karatsuba_multiplication_limit {
|
||||
} else if max_len >= karatsuba_multiplication_limit {
|
||||
karatsuba_multiply_digit_array(operand_a, operand_b, mut storage)
|
||||
} else {
|
||||
simple_multiply_digit_array(operand_a, operand_b, mut storage)
|
||||
|
@ -249,7 +249,7 @@ const newton_division_limit = 10_000
|
|||
|
||||
@[inline]
|
||||
fn divide_array_by_array(operand_a []u32, operand_b []u32, mut quotient []u32, mut remainder []u32) {
|
||||
if operand_a.len >= big.newton_division_limit {
|
||||
if operand_a.len >= newton_division_limit {
|
||||
newton_divide_array_by_array(operand_a, operand_b, mut quotient, mut remainder)
|
||||
} else {
|
||||
binary_divide_array_by_array(operand_a, operand_b, mut quotient, mut remainder)
|
||||
|
|
|
@ -184,7 +184,7 @@ fn validate_string(characters string, radix u32) ! {
|
|||
|
||||
for index := start_index; index < characters.len; index++ {
|
||||
digit := characters[index]
|
||||
value := big.digit_array.index(digit)
|
||||
value := digit_array.index(digit)
|
||||
|
||||
if value == -1 {
|
||||
return error('math.big: Invalid character ${digit}')
|
||||
|
@ -212,7 +212,7 @@ fn integer_from_special_string(characters string, chunk_size int) Integer {
|
|||
mut offset := 0
|
||||
for index := characters.len - 1; index >= start_index; index-- {
|
||||
digit := characters[index]
|
||||
value := u32(big.digit_array.index(digit))
|
||||
value := u32(digit_array.index(digit))
|
||||
|
||||
current |= value << offset
|
||||
offset += chunk_size
|
||||
|
@ -254,7 +254,7 @@ fn integer_from_regular_string(characters string, radix u32) Integer {
|
|||
|
||||
for index := start_index; index < characters.len; index++ {
|
||||
digit := characters[index]
|
||||
value := big.digit_array.index(digit)
|
||||
value := digit_array.index(digit)
|
||||
|
||||
result *= radix_int
|
||||
result += integer_from_int(value)
|
||||
|
@ -876,7 +876,7 @@ fn (integer Integer) general_radix_str(radix u32) string {
|
|||
mut rune_array := []rune{cap: current.digits.len * 4}
|
||||
for current.signum > 0 {
|
||||
new_current, digit = current.div_mod_internal(divisor)
|
||||
rune_array << big.digit_array[digit.int()]
|
||||
rune_array << digit_array[digit.int()]
|
||||
unsafe { digit.free() }
|
||||
unsafe { current.free() }
|
||||
current = new_current
|
||||
|
|
|
@ -17,19 +17,19 @@ const frac_mask = u64((u64(1) << u64(shift)) - u64(1))
|
|||
|
||||
// inf returns positive infinity if sign >= 0, negative infinity if sign < 0.
|
||||
pub fn inf(sign int) f64 {
|
||||
v := if sign >= 0 { math.uvinf } else { math.uvneginf }
|
||||
v := if sign >= 0 { uvinf } else { uvneginf }
|
||||
return f64_from_bits(v)
|
||||
}
|
||||
|
||||
// nan returns an IEEE 754 ``not-a-number'' value.
|
||||
pub fn nan() f64 {
|
||||
return f64_from_bits(math.uvnan)
|
||||
return f64_from_bits(uvnan)
|
||||
}
|
||||
|
||||
// is_nan reports whether f is an IEEE 754 ``not-a-number'' value.
|
||||
pub fn is_nan(f f64) bool {
|
||||
$if fast_math {
|
||||
if f64_bits(f) == math.uvnan {
|
||||
if f64_bits(f) == uvnan {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
@ -62,7 +62,7 @@ pub fn is_finite(f f64) bool {
|
|||
pub fn normalize(x f64) (f64, int) {
|
||||
smallest_normal := 2.2250738585072014e-308 // 2**-1022
|
||||
if abs(x) < smallest_normal {
|
||||
return x * math.normalize_smallest_mask, -52
|
||||
return x * normalize_smallest_mask, -52
|
||||
}
|
||||
return x, 0
|
||||
}
|
||||
|
|
|
@ -57,7 +57,7 @@ pub fn trailing_zeros_16(x u16) int {
|
|||
return 16
|
||||
}
|
||||
// see comment in trailing_zeros_64
|
||||
return int(bits.de_bruijn32tab[u32(x & -x) * bits.de_bruijn32 >> (32 - 5)])
|
||||
return int(de_bruijn32tab[u32(x & -x) * de_bruijn32 >> (32 - 5)])
|
||||
}
|
||||
|
||||
// trailing_zeros_32 returns the number of trailing zero bits in x; the result is 32 for x == 0.
|
||||
|
@ -67,7 +67,7 @@ pub fn trailing_zeros_32(x u32) int {
|
|||
return 32
|
||||
}
|
||||
// see comment in trailing_zeros_64
|
||||
return int(bits.de_bruijn32tab[(x & -x) * bits.de_bruijn32 >> (32 - 5)])
|
||||
return int(de_bruijn32tab[(x & -x) * de_bruijn32 >> (32 - 5)])
|
||||
}
|
||||
|
||||
// trailing_zeros_64 returns the number of trailing zero bits in x; the result is 64 for x == 0.
|
||||
|
@ -87,7 +87,7 @@ pub fn trailing_zeros_64(x u64) int {
|
|||
// find by how many bits it was shifted by looking at which six bit
|
||||
// substring ended up at the top of the word.
|
||||
// (Knuth, volume 4, section 7.3.1)
|
||||
return int(bits.de_bruijn64tab[(x & -x) * bits.de_bruijn64 >> (64 - 6)])
|
||||
return int(de_bruijn64tab[(x & -x) * de_bruijn64 >> (64 - 6)])
|
||||
}
|
||||
|
||||
// --- OnesCount ---
|
||||
|
@ -131,9 +131,9 @@ pub fn ones_count_64(x u64) int {
|
|||
// Per "Hacker's Delight", the first line can be simplified
|
||||
// more, but it saves at best one instruction, so we leave
|
||||
// it alone for clarity.
|
||||
mut y := (x >> u64(1) & (bits.m0 & max_u64)) + (x & (bits.m0 & max_u64))
|
||||
y = (y >> u64(2) & (bits.m1 & max_u64)) + (y & (bits.m1 & max_u64))
|
||||
y = ((y >> 4) + y) & (bits.m2 & max_u64)
|
||||
mut y := (x >> u64(1) & (m0 & max_u64)) + (x & (m0 & max_u64))
|
||||
y = (y >> u64(2) & (m1 & max_u64)) + (y & (m1 & max_u64))
|
||||
y = ((y >> 4) + y) & (m2 & max_u64)
|
||||
y += y >> 8
|
||||
y += y >> 16
|
||||
y += y >> 32
|
||||
|
@ -152,8 +152,8 @@ const n64 = u64(64)
|
|||
// This function's execution time does not depend on the inputs.
|
||||
@[inline]
|
||||
pub fn rotate_left_8(x u8, k int) u8 {
|
||||
s := u8(k) & (bits.n8 - u8(1))
|
||||
return (x << s) | (x >> (bits.n8 - s))
|
||||
s := u8(k) & (n8 - u8(1))
|
||||
return (x << s) | (x >> (n8 - s))
|
||||
}
|
||||
|
||||
// rotate_left_16 returns the value of x rotated left by (k mod 16) bits.
|
||||
|
@ -162,8 +162,8 @@ pub fn rotate_left_8(x u8, k int) u8 {
|
|||
// This function's execution time does not depend on the inputs.
|
||||
@[inline]
|
||||
pub fn rotate_left_16(x u16, k int) u16 {
|
||||
s := u16(k) & (bits.n16 - u16(1))
|
||||
return (x << s) | (x >> (bits.n16 - s))
|
||||
s := u16(k) & (n16 - u16(1))
|
||||
return (x << s) | (x >> (n16 - s))
|
||||
}
|
||||
|
||||
// rotate_left_32 returns the value of x rotated left by (k mod 32) bits.
|
||||
|
@ -172,8 +172,8 @@ pub fn rotate_left_16(x u16, k int) u16 {
|
|||
// This function's execution time does not depend on the inputs.
|
||||
@[inline]
|
||||
pub fn rotate_left_32(x u32, k int) u32 {
|
||||
s := u32(k) & (bits.n32 - u32(1))
|
||||
return (x << s) | (x >> (bits.n32 - s))
|
||||
s := u32(k) & (n32 - u32(1))
|
||||
return (x << s) | (x >> (n32 - s))
|
||||
}
|
||||
|
||||
// rotate_left_64 returns the value of x rotated left by (k mod 64) bits.
|
||||
|
@ -182,8 +182,8 @@ pub fn rotate_left_32(x u32, k int) u32 {
|
|||
// This function's execution time does not depend on the inputs.
|
||||
@[inline]
|
||||
pub fn rotate_left_64(x u64, k int) u64 {
|
||||
s := u64(k) & (bits.n64 - u64(1))
|
||||
return (x << s) | (x >> (bits.n64 - s))
|
||||
s := u64(k) & (n64 - u64(1))
|
||||
return (x << s) | (x >> (n64 - s))
|
||||
}
|
||||
|
||||
// --- Reverse ---
|
||||
|
@ -202,18 +202,18 @@ pub fn reverse_16(x u16) u16 {
|
|||
// reverse_32 returns the value of x with its bits in reversed order.
|
||||
@[inline]
|
||||
pub fn reverse_32(x u32) u32 {
|
||||
mut y := ((x >> u32(1) & (bits.m0 & max_u32)) | ((x & (bits.m0 & max_u32)) << 1))
|
||||
y = ((y >> u32(2) & (bits.m1 & max_u32)) | ((y & (bits.m1 & max_u32)) << u32(2)))
|
||||
y = ((y >> u32(4) & (bits.m2 & max_u32)) | ((y & (bits.m2 & max_u32)) << u32(4)))
|
||||
mut y := ((x >> u32(1) & (m0 & max_u32)) | ((x & (m0 & max_u32)) << 1))
|
||||
y = ((y >> u32(2) & (m1 & max_u32)) | ((y & (m1 & max_u32)) << u32(2)))
|
||||
y = ((y >> u32(4) & (m2 & max_u32)) | ((y & (m2 & max_u32)) << u32(4)))
|
||||
return reverse_bytes_32(u32(y))
|
||||
}
|
||||
|
||||
// reverse_64 returns the value of x with its bits in reversed order.
|
||||
@[inline]
|
||||
pub fn reverse_64(x u64) u64 {
|
||||
mut y := ((x >> u64(1) & (bits.m0 & max_u64)) | ((x & (bits.m0 & max_u64)) << 1))
|
||||
y = ((y >> u64(2) & (bits.m1 & max_u64)) | ((y & (bits.m1 & max_u64)) << 2))
|
||||
y = ((y >> u64(4) & (bits.m2 & max_u64)) | ((y & (bits.m2 & max_u64)) << 4))
|
||||
mut y := ((x >> u64(1) & (m0 & max_u64)) | ((x & (m0 & max_u64)) << 1))
|
||||
y = ((y >> u64(2) & (m1 & max_u64)) | ((y & (m1 & max_u64)) << 2))
|
||||
y = ((y >> u64(4) & (m2 & max_u64)) | ((y & (m2 & max_u64)) << 4))
|
||||
return reverse_bytes_64(y)
|
||||
}
|
||||
|
||||
|
@ -231,7 +231,7 @@ pub fn reverse_bytes_16(x u16) u16 {
|
|||
// This function's execution time does not depend on the inputs.
|
||||
@[inline]
|
||||
pub fn reverse_bytes_32(x u32) u32 {
|
||||
y := ((x >> u32(8) & (bits.m3 & max_u32)) | ((x & (bits.m3 & max_u32)) << u32(8)))
|
||||
y := ((x >> u32(8) & (m3 & max_u32)) | ((x & (m3 & max_u32)) << u32(8)))
|
||||
return u32((y >> 16) | (y << 16))
|
||||
}
|
||||
|
||||
|
@ -240,8 +240,8 @@ pub fn reverse_bytes_32(x u32) u32 {
|
|||
// This function's execution time does not depend on the inputs.
|
||||
@[inline]
|
||||
pub fn reverse_bytes_64(x u64) u64 {
|
||||
mut y := ((x >> u64(8) & (bits.m3 & max_u64)) | ((x & (bits.m3 & max_u64)) << u64(8)))
|
||||
y = ((y >> u64(16) & (bits.m4 & max_u64)) | ((y & (bits.m4 & max_u64)) << u64(16)))
|
||||
mut y := ((x >> u64(8) & (m3 & max_u64)) | ((x & (m3 & max_u64)) << u64(8)))
|
||||
y = ((y >> u64(16) & (m4 & max_u64)) | ((y & (m4 & max_u64)) << u64(16)))
|
||||
return (y >> 32) | (y << 32)
|
||||
}
|
||||
|
||||
|
@ -387,13 +387,13 @@ pub fn mul_32(x u32, y u32) (u32, u32) {
|
|||
//
|
||||
// This function's execution time does not depend on the inputs.
|
||||
pub fn mul_64(x u64, y u64) (u64, u64) {
|
||||
x0 := x & bits.mask32
|
||||
x0 := x & mask32
|
||||
x1 := x >> 32
|
||||
y0 := y & bits.mask32
|
||||
y0 := y & mask32
|
||||
y1 := y >> 32
|
||||
w0 := x0 * y0
|
||||
t := x1 * y0 + (w0 >> 32)
|
||||
mut w1 := t & bits.mask32
|
||||
mut w1 := t & mask32
|
||||
w2 := t >> 32
|
||||
w1 += x0 * y1
|
||||
hi := x1 * y1 + w2 + (w1 >> 32)
|
||||
|
@ -408,7 +408,7 @@ pub fn mul_64(x u64, y u64) (u64, u64) {
|
|||
// div_32 panics for y == 0 (division by zero) or y <= hi (quotient overflow).
|
||||
pub fn div_32(hi u32, lo u32, y u32) (u32, u32) {
|
||||
if y != 0 && y <= hi {
|
||||
panic(bits.overflow_error)
|
||||
panic(overflow_error)
|
||||
}
|
||||
z := (u64(hi) << 32) | u64(lo)
|
||||
quo := u32(z / u64(y))
|
||||
|
@ -423,15 +423,15 @@ pub fn div_32(hi u32, lo u32, y u32) (u32, u32) {
|
|||
pub fn div_64(hi u64, lo u64, y1 u64) (u64, u64) {
|
||||
mut y := y1
|
||||
if y == 0 {
|
||||
panic(bits.overflow_error)
|
||||
panic(overflow_error)
|
||||
}
|
||||
if y <= hi {
|
||||
panic(bits.overflow_error)
|
||||
panic(overflow_error)
|
||||
}
|
||||
s := u32(leading_zeros_64(y))
|
||||
y <<= s
|
||||
yn1 := y >> 32
|
||||
yn0 := y & bits.mask32
|
||||
yn0 := y & mask32
|
||||
ss1 := (hi << s)
|
||||
xxx := 64 - s
|
||||
mut ss2 := lo >> xxx
|
||||
|
@ -454,28 +454,28 @@ pub fn div_64(hi u64, lo u64, y1 u64) (u64, u64) {
|
|||
un32 := ss1 | ss2
|
||||
un10 := lo << s
|
||||
un1 := un10 >> 32
|
||||
un0 := un10 & bits.mask32
|
||||
un0 := un10 & mask32
|
||||
mut q1 := un32 / yn1
|
||||
mut rhat := un32 - (q1 * yn1)
|
||||
for q1 >= bits.two32 || (q1 * yn0) > ((bits.two32 * rhat) + un1) {
|
||||
for q1 >= two32 || (q1 * yn0) > ((two32 * rhat) + un1) {
|
||||
q1--
|
||||
rhat += yn1
|
||||
if rhat >= bits.two32 {
|
||||
if rhat >= two32 {
|
||||
break
|
||||
}
|
||||
}
|
||||
un21 := (un32 * bits.two32) + (un1 - (q1 * y))
|
||||
un21 := (un32 * two32) + (un1 - (q1 * y))
|
||||
mut q0 := un21 / yn1
|
||||
rhat = un21 - q0 * yn1
|
||||
for q0 >= bits.two32 || (q0 * yn0) > ((bits.two32 * rhat) + un0) {
|
||||
for q0 >= two32 || (q0 * yn0) > ((two32 * rhat) + un0) {
|
||||
q0--
|
||||
rhat += yn1
|
||||
if rhat >= bits.two32 {
|
||||
if rhat >= two32 {
|
||||
break
|
||||
}
|
||||
}
|
||||
qq := ((q1 * bits.two32) + q0)
|
||||
rr := ((un21 * bits.two32) + un0 - (q0 * y)) >> s
|
||||
qq := ((q1 * two32) + q0)
|
||||
rr := ((un21 * two32) + un0 - (q0 * y)) >> s
|
||||
return qq, rr
|
||||
}
|
||||
|
||||
|
|
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue