/PhpTool_src/phptool.c

https://gitlab.com/TSnake41/httpBat · C · 191 lines · 108 code · 31 blank · 52 comment · 18 complexity · 827b84305fb410b2234ee0c028c451f6 MD5 · raw file

  1. /*
  2. PHP_Tool - HTTPBAT
  3. This tool is used for multiples utilities
  4. like fix a bug in PHP 5, pass headers to clients,
  5. and help HTTPBAT to be faster for basics tasks which
  6. can't be realised in batch with enought performances !
  7. Made by Xenoxis - 2017
  8. Permission is hereby granted, free of charge, to any person obtaining
  9. a copy of this software and associated documentation files (the
  10. "Software"), to deal in the Software without restriction, including
  11. without limitation the rights to use, copy, modify, merge, publish,
  12. distribute, sublicense, and/or sell copies of the Software, and to
  13. permit persons to whom the Software is furnished to do so, subject to
  14. the following conditions:
  15. The above copyright notice and this permission notice shall be
  16. included in all copies or substantial portions of the Software.
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  21. CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  22. TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  23. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #define BUFFER_SIZE_STDIN 1024
  29. /* Mains Functions */
  30. void PHP(FILE*);
  31. void POST();
  32. void help();
  33. /* Functions used like "Libs" */
  34. void *CheckAlloc(void*);
  35. void FileWrite(char*, unsigned int, FILE* Output);
  36. FILE* OpenFile(char*);
  37. unsigned int ReadFile(char*);
  38. int main(unsigned int argv, unsigned char *argc[])
  39. {
  40. if (argv < 2) help();
  41. switch(argc[1][0])
  42. {
  43. /* 'h' like "Headers" */
  44. case 'h':
  45. PHP(OpenFile(argc[2]));
  46. break;
  47. /* 'l' like "Lenght" */
  48. case 'l':
  49. POST();
  50. break;
  51. /* Undefined command */
  52. default:
  53. help();
  54. break;
  55. }
  56. /* Never reached */
  57. return 0;
  58. }
  59. void PHP(FILE* Output)
  60. {
  61. unsigned char *pos, StdinBuffer[BUFFER_SIZE_STDIN + 1] = { '\0' }, lastChar[4];
  62. unsigned int readed = 0, reached = 0, pass=0;
  63. while ((readed = ReadFile(StdinBuffer)) > 0)
  64. {
  65. switch (reached)
  66. {
  67. case 1:
  68. FileWrite(StdinBuffer, readed, Output);
  69. break;
  70. default:
  71. if (pass == 0 && (StdinBuffer[0] == '\n' || StdinBuffer[0] == '\r'))
  72. {
  73. FileWrite(StdinBuffer + 1, readed - 1, Output);
  74. reached = 1;
  75. break;
  76. }
  77. pass++;
  78. pos = strstr(StdinBuffer, "\n\n");
  79. if (pos == NULL)
  80. {
  81. pos = strstr(StdinBuffer, "\r\n\r\n");
  82. if (pos == NULL) {FileWrite(StdinBuffer, readed, stdout); break;}
  83. }
  84. /*if (strcmpi("\n\n", lastChar+2) == 0)
  85. memcpy(StdinBuffer-4, &lastChar, 4);*/
  86. if (strlen(pos) > 2)
  87. {
  88. FileWrite(pos+2, strlen(pos+2), Output);
  89. FileWrite(StdinBuffer, readed - strlen(pos), stdout);
  90. reached = 1;
  91. }
  92. break;
  93. }
  94. }
  95. fclose(Output);
  96. exit(0);
  97. }
  98. void POST()
  99. {
  100. unsigned char StdinBuffer[BUFFER_SIZE_STDIN + 1] = { '\0' };
  101. unsigned long Totalreaded = 0;
  102. unsigned int readed = 0;
  103. while ((readed = ReadFile(StdinBuffer)) > 0)
  104. {
  105. Totalreaded+=readed;
  106. }
  107. printf("%ld", readed);
  108. free(StdinBuffer);
  109. exit(0);
  110. }
  111. void help()
  112. {
  113. printf(
  114. "PHP_Tool - HttpBat\n\n"
  115. "This tool is used for multiples utilities\n"
  116. "like fix a bug in PHP 5, pass headers to clients,\n"
  117. "and help HTTPBAT to be faster for basics tasks which\n"
  118. "can't be realised in batch with enought performances !\n\n"
  119. "Made by Xenoxis - 2017\n"
  120. );
  121. exit(0);
  122. }
  123. /*
  124. "Libs" for Php Tool
  125. -------------------
  126. Safer (upgraded) functions for use instantly
  127. with integred error-checking
  128. Best compromise between safely, speed and size
  129. */
  130. FILE* OpenFile(char* Output)
  131. {
  132. return CheckAlloc(fopen(Output, "wb"));
  133. }
  134. unsigned int ReadFile(char* Buffer)
  135. {
  136. return fread(Buffer, 1, BUFFER_SIZE_STDIN, stdin);
  137. }
  138. void FileWrite(char *ToOutput, unsigned int readed, FILE* Output)
  139. {
  140. fwrite(ToOutput, 1, readed, Output);
  141. return;
  142. }
  143. void *CheckAlloc(void* Ptr)
  144. {
  145. /* If an memory allocation fail, generaly, the
  146. pointer will be set to NULL, to provide a better source
  147. code and faster executable, it will be check in only one
  148. function */
  149. if (Ptr == NULL) {exit(-1);}
  150. return Ptr;
  151. }