common.scm 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. ;;; guile-gcrypt --- crypto tooling for guile
  2. ;;; Copyright © 2013, 2014, 2015, 2019 Ludovic Courtès <ludo@gnu.org>
  3. ;;; Copyright © 2019 Mathieu Othacehe <m.othacehe@gmail.com>
  4. ;;;
  5. ;;; This file is part of guile-gcrypt.
  6. ;;;
  7. ;;; guile-gcrypt is free software; you can redistribute it and/or modify it
  8. ;;; under the terms of the GNU General Public License as published by
  9. ;;; the Free Software Foundation; either version 3 of the License, or
  10. ;;; (at your option) any later version.
  11. ;;;
  12. ;;; guile-gcrypt is distributed in the hope that it will be useful, but
  13. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. ;;; General Public License for more details.
  16. ;;;
  17. ;;; You should have received a copy of the GNU General Public License
  18. ;;; along with guile-gcrypt. If not, see <http://www.gnu.org/licenses/>.
  19. (define-module (gcrypt common)
  20. #:use-module (gcrypt internal)
  21. #:use-module (system foreign)
  22. #:use-module (ice-9 match)
  23. #:re-export (gcrypt-version)
  24. #:export (error-source error-string))
  25. ;;; Commentary:
  26. ;;;
  27. ;;; Common code for the GNU Libgcrypt bindings.
  28. ;;;
  29. ;;; Code:
  30. (define error-source
  31. (let ((proc (libgcrypt->procedure '* "gcry_strsource" (list int))))
  32. (lambda (err)
  33. "Return the error source (a string) for ERR, an error code as thrown
  34. along with 'gcry-error'."
  35. (pointer->string (proc err)))))
  36. (define error-string
  37. (let ((proc (libgcrypt->procedure '* "gcry_strerror" (list int))))
  38. (lambda (err)
  39. "Return the error description (a string) for ERR, an error code as
  40. thrown along with 'gcry-error'."
  41. (pointer->string (proc err)))))
  42. (define (gcrypt-error-printer port key args default-printer)
  43. "Print the gcrypt error specified by ARGS."
  44. (match args
  45. ((proc err)
  46. (format port "In procedure ~a: ~a: ~a"
  47. proc (error-source err) (error-string err)))))
  48. (set-exception-printer! 'gcry-error gcrypt-error-printer)
  49. ;;; gcrypt.scm ends here