/*-
 * Scram-rs - a SCRAM authentification authorization library
 * 
 * Copyright (C) 2021  Aleksandr Morozov
 * Copyright (C) 2025 Aleksandr Morozov
 * 
 * The syslog-rs crate can be redistributed and/or modified
 * under the terms of either of the following licenses:
 *
 *   1. the Mozilla Public License Version 2.0 (the “MPL”) OR
 *
 *   2. The MIT License (MIT)
 *                     
 *   3. EUROPEAN UNION PUBLIC LICENCE v. 1.2 EUPL © the European Union 2007, 2016
 */

 
use core::{mem, num::NonZeroU32};

use crate::{capi_scram_error_map_err, scram_ierror, scram_ierror_map, ScramErrorCode, ScramHashing, ScramResult, ScramSha256Ring, ScramSha256RustNative};

use super::{common, error::scram_result_err_ptr, CApiScramHasherProvider};

#[repr(C)]
pub struct CApiArray
{
    pub ptr: *const u8,
    pub capacity: usize,
    pub length: usize,
}

impl CApiArray
{
    pub 
    fn new(inp: Vec<u8>) -> Self
    {
        let r = 
            CApiArray
            {
                ptr: inp.as_ptr(),
                capacity: inp.capacity(),
                length: inp.len(),  
            };

        mem::forget(inp);

        return r;
    }
}
/*
#[no_mangle]
pub unsafe extern "C" 
fn capi_scram_sha256_derive(
    hash_prov: CApiScramHasherProvider, 
    password: *const u8, 
    password_len: usize,
    salt: *const u8, 
    salt_len: usize,
    iterations: u32
) -> *mut ScramResult<*mut u8>
{
    let pass_slice = 
        common::carr_to_slice_n(password, password_len, "password arg is not valid")?;
    let salt_slice = 
        common::carr_to_slice_n(salt, salt_len, "salt arg is not valid")?;
    let iters = 
        NonZeroU32::new(iterations)
            .ok_or(scram_ierror_map!(ScramErrorCode::InternalError, "iterations count is zero"))?;
        
    match hash_prov
    {
        CApiScramHasherProvider::RustNative => 
        {
            let res = 
                ScramSha256RustNative::derive(pass_slice, salt_slice, iters)
                    .map(|v| Box::into_raw(CApiArray::new(r)))
                match 
                    ScramSha256RustNative::derive(pass_slice, salt_slice, iters)
                {
                    Ok(r) => 
                        Ok(Box::into_raw(CApiArray::new(r))),
                    Err(e) =>
                        Err(e)
                };

            return Box::into_raw(Box::new(res));
        }
        
        #[cfg(not(feature = "exclude_sha1"))]
        CApiScramHasherProvider::RustRing => 
            return ScramSha256Ring::derive(pass_slice, salt_slice, iters),

        #[cfg(feature = "exclude_sha1")]
        CApiScramHasherProvider::RustRing =>
            scram_ierror!(ScramErrorCode::FeatureNotSupported, "compiled without ring crate"),
    }
}
*/