Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void deblock(char *in, char *base64, FILE * OutputFile, int len)
  5. {
  6. if (base64[(int)in[0]] < 64 && base64[(int)in[1]] < 64)
  7. fprintf(OutputFile, "%c",(base64[(int)in[0]] << 2 | base64[(int)in[1]] >> 4));
  8. if (base64[(int)in[1]] < 64 && base64[(int)in[2]] < 64)
  9. fprintf(OutputFile, "%c",(base64[(int)in[1]] << 4 | base64[(int)in[2]] >> 2));
  10. if (base64[(int)in[2]] < 64 && base64[(int)in[3]] < 64)
  11.  
  12. fprintf(OutputFile, "%c",((base64[(int)in[2]] << 6) & 192) | base64[(int)in[3]]);
  13. }
  14.  
  15. void decode (FILE *InputFile, FILE *OutputFile)
  16. {
  17. static char base64[] = {
  18. // ASCII table
  19. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  20. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  21. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 64, 63,
  22. 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64,
  23. 64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  24. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64,
  25. 64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
  26. 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64,
  27. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  28. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  29. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  30. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  31. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  32. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  33. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64,
  34. 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64
  35. };
  36. int len = 0;
  37. int i = 0;
  38. char buffer[4] = {0};
  39. while ((len = fread(buffer,sizeof(char),4,InputFile)) >= 1)
  40. {
  41. for (i = len; i < 4; i++)
  42. buffer[i] = 0;
  43. deblock(buffer,base64,OutputFile,len);
  44. }
  45. }
  46.  
  47.  
  48. int check_decode(FILE *file1, FILE *file2)
  49. {
  50. int k = 0;
  51. fseek (file1, 0, SEEK_END);
  52. if (((ftell(file1)) % 4) == 0)
  53. {
  54. fseek(file1,0,SEEK_SET);
  55. decode(file1,file2);
  56. }
  57. else
  58. {
  59. k = -1;
  60. }
  61. return k;
  62. }
  63.  
  64.  
  65. int main()
  66. {
  67. FILE *file1 = NULL;
  68. FILE *file2 = NULL;
  69. if (NULL == (file1 = fopen("INPUT1.txt", "r")))
  70. {
  71. printf("Can't open INPUT file!");
  72. return -1;
  73. }
  74. if (NULL == (file2 = fopen("OUTPUT.orig", "wb")))
  75. {
  76. printf("Can't create OUTPUT file");
  77. return -1;
  78. }
  79. if ( -1 == check_decode(file1, file2) )
  80. {
  81. printf("Eto ne base64");
  82. return -1;
  83. }
  84. fclose(file1);
  85. fclose(file2);
  86. return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement