/**
 * Warranty & Liability
 * To the extent permitted by applicable law and unless explicitly
 * otherwise agreed upon, XLOG Technologies AG makes no warranties
 * regarding the provided information. XLOG Technologies AG assumes
 * no liability that any problems might be solved with the information
 * provided by XLOG Technologies AG.
 *
 * Rights & License
 * All industrial property rights regarding the information - copyright
 * and patent rights in particular - are the sole property of XLOG
 * Technologies AG. If the company was not the originator of some
 * excerpts, XLOG Technologies AG has at least obtained the right to
 * reproduce, change and translate the information.
 *
 * Reproduction is restricted to the whole unaltered document. Reproduction
 * of the information is only allowed for non-commercial uses. Selling,
 * giving away or letting of the execution of the library is prohibited.
 * The library can be distributed as part of your applications and libraries
 * for execution provided this comment remains unchanged.
 *
 * Restrictions
 * Only to be distributed with programs that add significant and primary
 * functionality to the library. Not to be distributed with additional
 * software intended to replace any components of the library.
 *
 * Trademarks
 * Jekejeke is a registered trademark of XLOG Technologies AG.
 */

:- ensure_loaded(library(text/charsio)).

/**
 * checksum_binary(A, B):
 * The predicate succeeds in B with the checksum of the file A.
 */
% checksum_binary(+Atom, -Atom)
checksum_binary(InName, Value) :-
   setup_once_cleanup(
      open(InName, read, InStream, [type(binary)]),
      sys_checksum_binary(InStream, Value),
      close(InStream)).

% sys_checksum_binary(+Stream, -Atom)
sys_checksum_binary(InStream, Value) :-
   digest_open(Digest),
   (enum_atoms(InStream, Line, [stop(-1), compress(false), max(8192)]),
      digest_update(Digest, Line),
      fail; true),
   digest_close(Digest, Value).

/**
 * hex_encode(A, B):
 * The predicate succeeds in B with hex encode of the A.
 */
% hex_encode(+Atom, -Atom)
hex_encode(A, B) :-
   atom_codes(A, L),
   sys_hex_encode_list(L, R, []),
   atom_codes(B, R).

% sys_hex_encode_list(+List, -List, +List)
sys_hex_encode_list([]) --> [].
sys_hex_encode_list([C|L]) -->
   sys_hex_encode_byte(C),
   sys_hex_encode_list(L).

% sys_hex_encode_byte(+Integer, -List, +List)
sys_hex_encode_byte(C) --> {C < 16}, !,
   "0", sys_hex_encode_nibble(C).
sys_hex_encode_byte(C) --> {A is C//16, B is C rem 16}, !,
   sys_hex_encode_nibble(A), sys_hex_encode_nibble(B).

% sys_hex_encode_nibble(+Integer, -List, +List)
sys_hex_encode_nibble(C) --> {C < 10}, !,
   {D is 0'0+C}, [D].
sys_hex_encode_nibble(C) -->
   {D is 0'a+C-10}, [D].

/****************************************************************/
/* Error Texts                                                  */
/****************************************************************/

% strings(+Atom, +Atom, -Atom)
:- multifile(strings/3).
strings('type_error.digest', de, 'Prüfsumme erwartet, gefunden ~q.').

strings('existence_error.algorithm', de, 'Algorithmus ~q undefiniert.').
strings('existence_error.encoding', de, 'Codierung ~q undefiniert.').

strings('type_error.digest', '', 'Digest expected, found ~q.').

strings('existence_error.algorithm', '', 'Undefined algorithm ~q.').
strings('existence_error.encoding', '', 'Undefined encoding ~q.').

/*******************************************************************/
/* Foreign Predicates                                              */
/*******************************************************************/

% digest_open(X):
% defined in foreign(util/seclib)

% digest_update(X, Y):
% defined in foreign(util/seclib)

% digest_close(X,Y):
% defined in foreign(util/seclib)

:- ensure_loaded(foreign(util/seclib)).