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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * Copyright 2024 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 double total_time = 0;
  22. static void
  23. rspamd_process_file(const char *fname, int decode)
  24. {
  25. int fd;
  26. gpointer map;
  27. struct stat st;
  28. uint8_t *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", (int) destlen, dest);
  54. g_free(dest);
  55. munmap(map, st.st_size);
  56. }
  57. int main(int argc, char **argv)
  58. {
  59. int i, start = 1, decode = 0;
  60. if (argc > 2 && *argv[1] == '-') {
  61. start = 2;
  62. if (argv[1][1] == 'd') {
  63. decode = 1;
  64. }
  65. }
  66. for (i = start; i < argc; i++) {
  67. if (argv[i]) {
  68. rspamd_process_file(argv[i], decode);
  69. }
  70. }
  71. return 0;
  72. }