base64File.hpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /** 该文件目前未在信创设备上使用 [Gifur@2022614]*/
  2. #include <iostream>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. using namespace std;
  7. #define ERROR_INVALID_ARG -1
  8. #define ERROR_INVALID_OP -2
  9. #define ERROR_INVALID_SYN -3
  10. #define ERROR_OPEN_FILE -4
  11. #define OPERATION_SUCCESS 0
  12. const char base64_table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  13. int encode64(FILE* input_file, std::string &output_file) {
  14. //Variables Declaration
  15. int decode_buffer = 0;
  16. int byteCount = 1; int temp;
  17. char raw_byte = 0; char out_byte = 0;
  18. //Read and encode
  19. while (fread(&raw_byte, sizeof(char), 1, input_file)) {
  20. temp = raw_byte & ~(-1 << 8);
  21. decode_buffer = (decode_buffer << 8) + temp;
  22. if (byteCount % 3 == 0) { //Every 3 byte -> 4 base64 char
  23. for (temp = 0; temp < 4; temp++) {
  24. out_byte = base64_table[(decode_buffer >> (6 * (3 - temp)) & 0x3F)];
  25. output_file.push_back(out_byte);
  26. }
  27. decode_buffer = 0;
  28. }
  29. byteCount++;
  30. }
  31. if (byteCount % 3) { //If padding is needed
  32. for (temp = 0; temp < byteCount % 3; temp++)
  33. decode_buffer = (decode_buffer << 8); //Fill the missing 1 or 2 byte
  34. for (temp = 0; temp < 4 - (byteCount % 3); temp++) { //If fill 1 byte -> Get 3 char; if fill 2 byte -> get 2 char => 4-fill_no
  35. out_byte = base64_table[(decode_buffer >> (6 * (3 - temp)) & 0x3F)];
  36. output_file.push_back(out_byte);
  37. }
  38. for (temp = 0; temp < byteCount % 3; temp++)
  39. output_file.push_back('=');
  40. }
  41. return OPERATION_SUCCESS;
  42. }
  43. int decode64(std::string input_file, FILE* output_file) {
  44. int decode_buffer = 0;
  45. int byteCount = 1; int paddingCount = 0; int i;
  46. char raw_byte = 0; char resolved_byte = 0; unsigned char temp;
  47. //Read and decode
  48. while (fread(&raw_byte, sizeof(char), 1, output_file)) {
  49. if (raw_byte == '=') { //If a padding is encountered
  50. raw_byte = 'A'; //Turn it into 0
  51. paddingCount++;
  52. }
  53. //Decode the character
  54. const char* pt = strchr(base64_table, (unsigned char)raw_byte);
  55. if (!pt) {//Unable to resolve character
  56. return ERROR_INVALID_SYN;
  57. }
  58. resolved_byte = pt - base64_table;
  59. resolved_byte = resolved_byte & ~(-1 << 6); //Mask the byte to ensure all the bit except the last 6 is zero
  60. decode_buffer = (decode_buffer << 6) + resolved_byte;
  61. if (byteCount % 4 == 0) { //Every 4 base64 char -> 3 byte
  62. for (i = 0; i < 3 - paddingCount; i++) {
  63. temp = (decode_buffer >> (8 * (2 - i))) & ~(-1 << 8);
  64. fwrite(&temp, sizeof(char), 1, output_file);
  65. }
  66. decode_buffer = 0;
  67. }
  68. byteCount++;
  69. }
  70. return OPERATION_SUCCESS;
  71. }