common.scm 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. ;;; guile-gcrypt --- crypto tooling for guile
  2. ;;; Copyright © 2013, 2014, 2015 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 package-config)
  21. #:use-module (system foreign)
  22. #:use-module (ice-9 match)
  23. #:export (gcrypt-version
  24. libgcrypt->pointer
  25. libgcrypt->procedure
  26. error-source error-string))
  27. ;;; Commentary:
  28. ;;;
  29. ;;; Common code for the GNU Libgcrypt bindings. Loading this module
  30. ;;; initializes Libgcrypt as a side effect.
  31. ;;;
  32. ;;; Code:
  33. (define (libgcrypt->pointer name)
  34. "Return a pointer to symbol FUNC in libgcrypt."
  35. (catch #t
  36. (lambda ()
  37. (dynamic-func name (dynamic-link %libgcrypt)))
  38. (lambda args
  39. (lambda _
  40. (throw 'system-error name "~A" (list (strerror ENOSYS))
  41. (list ENOSYS))))))
  42. (define (libgcrypt->procedure return name params)
  43. "Return a pointer to symbol FUNC in libgcrypt."
  44. (catch #t
  45. (lambda ()
  46. (let ((ptr (dynamic-func name (dynamic-link %libgcrypt))))
  47. ;; The #:return-errno? facility was introduced in Guile 2.0.12.
  48. (pointer->procedure return ptr params
  49. #:return-errno? #t)))
  50. (lambda args
  51. (lambda _
  52. (throw 'system-error name "~A" (list (strerror ENOSYS))
  53. (list ENOSYS))))))
  54. (define gcrypt-version
  55. ;; According to the manual, this function must be called before any other,
  56. ;; and it's not clear whether it can be called more than once. So call it
  57. ;; right here from the top level.
  58. (let ((proc (libgcrypt->procedure '* "gcry_check_version" '(*))))
  59. (lambda ()
  60. "Return the version number of libgcrypt as a string."
  61. (pointer->string (proc %null-pointer)))))
  62. (define error-source
  63. (let ((proc (libgcrypt->procedure '* "gcry_strsource" (list int))))
  64. (lambda (err)
  65. "Return the error source (a string) for ERR, an error code as thrown
  66. along with 'gcry-error'."
  67. (pointer->string (proc err)))))
  68. (define error-string
  69. (let ((proc (libgcrypt->procedure '* "gcry_strerror" (list int))))
  70. (lambda (err)
  71. "Return the error description (a string) for ERR, an error code as
  72. thrown along with 'gcry-error'."
  73. (pointer->string (proc err)))))
  74. (define (gcrypt-error-printer port key args default-printer)
  75. "Print the gcrypt error specified by ARGS."
  76. (match args
  77. ((proc err)
  78. (format port "In procedure ~a: ~a: ~a"
  79. proc (error-source err) (error-string err)))))
  80. (set-exception-printer! 'gcry-error gcrypt-error-printer)
  81. ;;; gcrypt.scm ends here