Advertisement
GodlyPacketz

Untitled

Aug 13th, 2022
1,124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.68 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4.  
  5. int util_len(void *ptr)
  6. {
  7.     int j = 0;
  8.     uint8_t *ptr_w = (uint8_t *)ptr;
  9.     while(ptr_w[j] != 0)
  10.     {
  11.         j++;
  12.     }
  13.     return j;
  14. }
  15.  
  16.  
  17. int util_zero(void *ptr, uint32_t size)
  18. {
  19.     uint8_t *ptr_w = (uint8_t *)ptr;
  20.     uint32_t j;
  21.     for(j = 0; j < size; j++)
  22.     {
  23.         ptr_w[j] = 0;
  24.     }
  25. }
  26.  
  27. int util_exists(void *ptr, void *ptr2, uint32_t ptr_size, uint32_t ptr2_size)
  28. {
  29.     uint8_t *ptr_w = (uint8_t *)ptr;
  30.     uint8_t *ptr2_w = (uint8_t *)ptr2;
  31.     uint32_t j;
  32.     int ptr2_pos = 0;
  33.  
  34.     if(ptr2_size > ptr_size) return -1;
  35.     for(j = 0; j < ptr_size; j++)
  36.     {
  37.         if(ptr_w[j] == ptr2_w[ptr2_pos])
  38.         {
  39.             #ifdef DEBUG_UTILS
  40.             printf("(util_exists) ptr_w[%d] == ptr2_w[%d]: success\r\n", j, ptr2_pos);
  41.             #endif
  42.             ptr2_pos++;
  43.             if(ptr2_pos == ptr2_size)
  44.             {
  45.                 #ifdef DEBUG_UTILS
  46.                 printf("(util_exists) success found exist match! starts at %d\r\n", j-ptr2_pos+1);
  47.                 #endif
  48.                 return j-ptr2_pos+1;
  49.             }
  50.             continue;
  51.         }
  52.         else if(ptr2_pos > 0)
  53.         {
  54.             #ifdef DEBUG_UTILS
  55.             printf("(util_exists) ptr_w[%d] != ptr2_w[%d]: failure reset continue\r\n", j, ptr2_pos);
  56.             #endif
  57.             ptr2_pos = 0;
  58.         }
  59.         else
  60.         {
  61.             #ifdef DEBUG_UTILS
  62.             printf("(util_exists) ptr_w[%d] != ptr2_w[%d]: failure continue\r\n", j, ptr2_pos);
  63.             #endif
  64.             continue;
  65.         }
  66.     }
  67.     if(ptr2_pos == ptr2_size)
  68.     {
  69.         return j-ptr2_pos+1;
  70.     }
  71.     return -1;
  72. }
  73.  
  74. static double get_bogos(void)
  75. {
  76.     uint8_t *recvbuffer = NULL;
  77.     uint16_t recvbuffer_len = 0;
  78.  
  79.     FILE *fp = fopen("/proc/cpuinfo", "r");
  80.     if(!fp)
  81.     {
  82.         #ifdef DEBUG
  83.         printf("(get_bogos) Failed to open /proc/cpuinfo");
  84.         #endif
  85.         return -1;
  86.     }
  87.  
  88.     recvbuffer = malloc(1024);
  89.     util_zero(recvbuffer, 1024);
  90.     while(fgets(recvbuffer, 1024, fp) != NULL)
  91.     {
  92.         int pos = 0;
  93.         int value_location = 0;
  94.         int ret = 0;
  95.         for(pos = 0; pos < 1024; )
  96.         {
  97.             if(recvbuffer[pos] == 0)
  98.             {
  99.                 recvbuffer_len = pos;
  100.                 break;
  101.             }
  102.  
  103.             if(recvbuffer[pos] == '\r' || recvbuffer[pos] == '\n')
  104.             {
  105.                 recvbuffer[pos] = 0;
  106.                 continue;
  107.             }
  108.             pos++;
  109.         }
  110.         #ifdef DEBUG
  111.         printf("(get_bogos) read line \"%s\" - with len %d/%d\r\n", recvbuffer, recvbuffer_len, util_len(recvbuffer));
  112.         #endif
  113.         //bogomips
  114.  
  115.         uint8_t *find_buf = malloc(8);
  116.         util_zero(find_buf, 8);
  117.         find_buf[0] = 'b';//b
  118.         find_buf[1] = 'o';//bo
  119.         find_buf[2] = 'g';//bog
  120.         find_buf[3] = 'o';//bogo
  121.  
  122.         find_buf[4] = 'm';
  123.         find_buf[5] = 'i';
  124.         find_buf[6] = 'p';
  125.         find_buf[7] = 's';
  126.  
  127.         ret = util_exists(recvbuffer, find_buf, recvbuffer_len, 8);
  128.         if(ret >= 0)// ret = begining of where bogomips is located
  129.         {
  130.             for(pos = ret+8; pos < 1024; pos++)
  131.             {
  132.                 if(recvbuffer[pos] == ':')
  133.                 {
  134.                     double bogos = 0;
  135.                     value_location = pos+2;
  136.                     bogos = strtod(recvbuffer+value_location, NULL);
  137.  
  138.                     util_zero(find_buf, 8);
  139.                     free(find_buf);
  140.                     find_buf = NULL;
  141.  
  142.                     util_zero(recvbuffer, 1024);
  143.                     free(recvbuffer);
  144.                     recvbuffer = NULL;
  145.                     return bogos;
  146.                 }
  147.             }
  148.         }
  149.  
  150.         util_zero(recvbuffer, 1024);
  151.  
  152.         util_zero(find_buf, 8);
  153.         free(find_buf);
  154.         find_buf = NULL;
  155.  
  156.     }
  157.     #ifdef DEBUG
  158.     printf("(get_bogos) Failed to find string \"bogomips\" in /proc/cpuinfo!\r\n");
  159.     #endif
  160.     util_zero(recvbuffer, 1024);
  161.     free(recvbuffer);
  162.     recvbuffer = NULL;
  163.     return -1;
  164. }
  165.  
  166. static uint8_t get_cores(void)
  167. {
  168.     uint8_t cores = 0;
  169.     uint8_t *recvbuffer = NULL;
  170.     uint16_t recvbuffer_len = 0;
  171.  
  172.     FILE *fp = fopen("/proc/cpuinfo", "r");
  173.     if(!fp)
  174.     {
  175.         #ifdef DEBUG
  176.         printf("(get_cores) Failed to open /proc/cpuinfo");
  177.         #endif
  178.         return 0;
  179.     }
  180.  
  181.     recvbuffer = malloc(1024);
  182.     util_zero(recvbuffer, 1024);
  183.     while(fgets(recvbuffer, 1024, fp) != NULL)
  184.     {
  185.         int pos = 0;
  186.         int value_location = 0;
  187.         int ret = 0;
  188.         for(pos = 0; pos < 1024; )
  189.         {
  190.             if(recvbuffer[pos] == 0)
  191.             {
  192.                 recvbuffer_len = pos;
  193.                 break;
  194.             }
  195.  
  196.             if(recvbuffer[pos] == '\r' || recvbuffer[pos] == '\n')
  197.             {
  198.                 recvbuffer[pos] = 0;
  199.                 continue;
  200.             }
  201.             pos++;
  202.         }
  203.         #ifdef DEBUG
  204.         printf("(get_cores) read line \"%s\" - with len %d/%d\r\n", recvbuffer, recvbuffer_len, util_len(recvbuffer));
  205.         #endif
  206.  
  207.         uint8_t *find_buf = malloc(9);
  208.         util_zero(find_buf, 9);
  209.         //processor
  210.         find_buf[0] = 'p';
  211.         find_buf[1] = 'r';
  212.         find_buf[2] = 'o';
  213.         find_buf[3] = 'c';
  214.         find_buf[4] = 'e';
  215.         find_buf[5] = 's';
  216.         find_buf[6] = 's';
  217.         find_buf[7] = 'o';
  218.         find_buf[8] = 'r';
  219.         ret = util_exists(recvbuffer, find_buf, recvbuffer_len, 9);
  220.         if(ret >= 0)// ret = begining of where bogomips is located
  221.         {
  222.             cores++;
  223.         }
  224.  
  225.         util_zero(recvbuffer, 1024);
  226.  
  227.         util_zero(find_buf, 8);
  228.         free(find_buf);
  229.         find_buf = NULL;
  230.  
  231.     }
  232.     util_zero(recvbuffer, 1024);
  233.     free(recvbuffer);
  234.     recvbuffer = NULL;
  235.     return cores;
  236. }
  237.  
  238. static uint32_t get_mem(void)
  239. {
  240.     uint8_t *recvbuffer = NULL;
  241.     uint16_t recvbuffer_len = 0;
  242.  
  243.     FILE *fp = fopen("/proc/meminfo", "r");
  244.     if(!fp)
  245.     {
  246.         #ifdef DEBUG
  247.         printf("(get_mem) Failed to open /proc/meminfo");
  248.         #endif
  249.         return -1;
  250.     }
  251.  
  252.     recvbuffer = malloc(1024);
  253.     util_zero(recvbuffer, 1024);
  254.     while(fgets(recvbuffer, 1024, fp) != NULL)
  255.     {
  256.         int pos = 0;
  257.         int value_location = 0;
  258.         int ret = 0;
  259.         for(pos = 0; pos < 1024; )
  260.         {
  261.             if(recvbuffer[pos] == 0)
  262.             {
  263.                 recvbuffer_len = pos;
  264.                 break;
  265.             }
  266.  
  267.             if(recvbuffer[pos] == '\r' || recvbuffer[pos] == '\n')
  268.             {
  269.                 recvbuffer[pos] = 0;
  270.                 continue;
  271.             }
  272.             pos++;
  273.         }
  274.         #ifdef DEBUG
  275.         printf("(get_mem) read line \"%s\" - with len %d/%d\r\n", recvbuffer, recvbuffer_len, util_len(recvbuffer));
  276.         #endif
  277.  
  278.         uint8_t *find_buf = malloc(8);
  279.         util_zero(find_buf, 8);
  280.         //MemTotal
  281.         find_buf[0] = 'M';
  282.         find_buf[1] = 'e';
  283.         find_buf[2] = 'm';
  284.         find_buf[3] = 'T';
  285.         find_buf[4] = 'o';
  286.         find_buf[5] = 't';
  287.         find_buf[6] = 'a';
  288.         find_buf[7] = 'l';
  289.  
  290.         ret = util_exists(recvbuffer, find_buf, recvbuffer_len, 8);
  291.         if(ret >= 0)// ret = begining of where bogomips is located
  292.         {
  293.             uint8_t found_colon = 0;
  294.             for(pos = ret+8; pos < 1024; pos++)
  295.             {
  296.                 if(recvbuffer[pos] != ' ' && found_colon == 1)
  297.                 {
  298.                     uint32_t mem_kb = 0;
  299.                     value_location = pos;
  300.                     mem_kb = atoi(recvbuffer+value_location);
  301.  
  302.                     util_zero(find_buf, 8);
  303.                     free(find_buf);
  304.                     find_buf = NULL;
  305.  
  306.                     util_zero(recvbuffer, 1024);
  307.                     free(recvbuffer);
  308.                     recvbuffer = NULL;
  309.                     return mem_kb/1000;
  310.                 }
  311.                 if(recvbuffer[pos] == ':')
  312.                 {
  313.                     found_colon = 1;
  314.                     continue;
  315.                 }
  316.             }
  317.         }
  318.  
  319.         util_zero(recvbuffer, 1024);
  320.  
  321.         util_zero(find_buf, 8);
  322.         free(find_buf);
  323.         find_buf = NULL;
  324.  
  325.     }
  326.     #ifdef DEBUG
  327.     printf("(get_bogos) Failed to find string \"bogomips\" in /proc/cpuinfo!\r\n");
  328.     #endif
  329.     util_zero(recvbuffer, 1024);
  330.     free(recvbuffer);
  331.     recvbuffer = NULL;
  332.     return -1;
  333. }
  334. int main()
  335. {
  336.         printf("Got %lf bogomips\r\n", get_bogos());
  337.     printf("Got %d cores\r\n", get_cores());
  338.     printf("Got %d mb ram\r\n", get_mem());
  339.         return 0;
  340. }
  341.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement