You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

base64.c 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*-
  2. * Copyright 2016 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "config.h"
  17. #include "printf.h"
  18. #include "util.h"
  19. #include "cryptobox.h"
  20. #include "unix-std.h"
  21. static gdouble total_time = 0;
  22. static void
  23. rspamd_process_file (const gchar *fname, gint decode)
  24. {
  25. gint fd;
  26. gpointer map;
  27. struct stat st;
  28. guint8 *dest;
  29. gsize destlen;
  30. fd = open (fname, O_RDONLY);
  31. if (fd == -1) {
  32. rspamd_fprintf (stderr, "cannot open %s: %s", fname, strerror (errno));
  33. exit (EXIT_FAILURE);
  34. }
  35. if (fstat (fd, &st) == -1) {
  36. rspamd_fprintf (stderr, "cannot stat %s: %s", fname, strerror (errno));
  37. exit (EXIT_FAILURE);
  38. }
  39. map = mmap (NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
  40. close (fd);
  41. if (map == MAP_FAILED) {
  42. rspamd_fprintf (stderr, "cannot mmap %s: %s", fname, strerror (errno));
  43. exit (EXIT_FAILURE);
  44. }
  45. if (decode) {
  46. destlen = st.st_size / 4 * 3 + 10;
  47. dest = g_malloc (destlen);
  48. rspamd_cryptobox_base64_decode (map, st.st_size, dest, &destlen);
  49. }
  50. else {
  51. dest = rspamd_encode_base64 (map, st.st_size, 80, &destlen);
  52. }
  53. rspamd_printf ("%*s", (gint)destlen, dest);
  54. g_free (dest);
  55. munmap (map, st.st_size);
  56. }
  57. int
  58. main (int argc, char **argv)
  59. {
  60. gint i, start = 1, decode = 0;
  61. if (argc > 2 && *argv[1] == '-') {
  62. start = 2;
  63. if (argv[1][1] == 'd') {
  64. decode = 1;
  65. }
  66. }
  67. for (i = start; i < argc; i ++) {
  68. if (argv[i]) {
  69. rspamd_process_file (argv[i], decode);
  70. }
  71. }
  72. return 0;
  73. }