Advertisement
alaestor

[FGL Utility] deprint

Dec 17th, 2016
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.72 KB | None | 0 0
  1. /************************************************
  2. * Deprint is an FGL utility. It is comprised    *
  3. * of two types of functions. Conversions        *
  4. * read ptr-passed data and malloc()s a string   *
  5. * that represents the data in hex or binary,    *
  6. * and returns a ptr to it (null terminated).    *
  7. * Print functions are inline and call the       *
  8. * conversion functions, print the returned      *
  9. * string and representation type (b/hex/etc)    *
  10. * and then immediately frees the memory.        *
  11. *************************************************/
  12.  
  13. #include <limits.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <stdint.h>
  17. //#include "deprint.h"
  18.  
  19. /// Prototypes
  20. // convert to (null terminated) string
  21. unsigned char *de_d2b_le(unsigned char const *restrict const data, unsigned int const tsize); // data to binary (litte endian)
  22. unsigned char *de_d2b_be(unsigned char const *restrict const data, unsigned int const tsize); // data to binary (big endian)
  23. unsigned char *de_d2h(unsigned char const *restrict const data, unsigned int const tsize, unsigned int const byte); // data to hex
  24. // print string ("\n type : converted representation\n")
  25. static inline void de_pble(unsigned char const *restrict const data, unsigned int const tsize); // print binary (little endian)
  26. static inline void de_pbbe(unsigned char const *restrict const data, unsigned int const tsize); // print binary (big endian)
  27. static inline void de_phex(unsigned char const *restrict const data, unsigned int const tsize, unsigned int const byte); // print hexidecimal
  28.  
  29. /// Printing functions
  30. static inline void de_pble(unsigned char const *restrict const data, unsigned int const tsize)
  31. {// print binary little-endian
  32.     char *ptr = (char *)de_d2b_le(data, tsize);
  33.  
  34.     fputs(" Binary (LE): ", stdout);
  35.     puts(ptr);
  36.     free(ptr);
  37.  
  38.     return;
  39. }
  40.  
  41. static inline void de_pbbe(unsigned char const *restrict const data, unsigned int const tsize)
  42. {// print binary big-endian
  43.     char *ptr = (char *)de_d2b_be(data, tsize);
  44.  
  45.     fputs(" Binary (BE): ", stdout);
  46.     puts(ptr);
  47.     free(ptr);
  48.  
  49.     return;
  50. }
  51.  
  52. static inline void de_phex(unsigned char const *restrict const data, unsigned int const tsize, unsigned int const byte)
  53. {// print hex
  54.     char *ptr = (char *)de_d2h(data, tsize, byte);
  55.  
  56.     if (byte)
  57.         printf(" Hex Byte#%.2i: ",byte);
  58.     else
  59.         fputs(" Hexidecimal: ", stdout);
  60.  
  61.     puts(ptr);
  62.     free(ptr);
  63.  
  64.     return;
  65. }
  66.  
  67. /// DEPRINT C FILE
  68.  
  69. /// Conversions
  70. unsigned char *de_d2b_le(unsigned char const *restrict const data, unsigned int const tsize)
  71. {// convert data to binary (little-endian)
  72.     const int strspace = (tsize * CHAR_BIT); // (tsize << 3) used multiple calcs
  73.  
  74.     unsigned char *result = malloc(strspace + sizeof(char));
  75.     // malloc required char space ((num of bits in tsize) + null term)
  76.     if (result == NULL)
  77.         return NULL;
  78.  
  79.     // bit mask and bitshift bit to most-significant bit.
  80.     unsigned char bmask = 1u << (CHAR_BIT-1);
  81.  
  82.     int dataindex; // data byte being scaned (counts up representing bytes)
  83.     int strindex = strspace; // result byte being stored (counts down representing bits)
  84.     result[strindex] = '\0'; // add null term and decrement strindex by the char used.
  85.  
  86.     /* WTF IS THE WHILE & FOR LOOP DOING?!
  87.     forgot to add this last time and i had to spend
  88.     10 minutes figuring out what the fuck i was doing
  89.  
  90.         one back , read fwd, one back, repeat for as many bytes
  91.         ( (<<*8) , (>>&)*8 , (<<*8) ), repeat * tsize
  92.     */
  93.  
  94.     while (strindex > 0)
  95.     {
  96.         strindex -= CHAR_BIT; // go to beginning of unread byte
  97.         dataindex = (strindex / (-CHAR_BIT)) + (tsize - 1); // calc read byte
  98.  
  99.         for (; bmask > 0u; bmask >>= 1u, strindex++) // scan byte
  100.             result[strindex] = (data[dataindex] & bmask) ? '1' : '0';
  101.  
  102.         bmask = 1u << (CHAR_BIT-1); // Reset mask
  103.         strindex -= CHAR_BIT; // go to beginning of read byte
  104.     }
  105.  
  106.     return result;
  107. }
  108.  
  109. unsigned char *de_d2b_be(unsigned char const *restrict const data, unsigned int const tsize)
  110. {// convert data to binary (big-endian)
  111.     const int strspace = (tsize * CHAR_BIT); // (tsize << 3) used multiple calcs
  112.  
  113.     unsigned char *result = malloc(strspace + sizeof(char));
  114.     // malloc required char space ((num of bits in tsize) + null term)
  115.     if (result == NULL)
  116.         return NULL;
  117.  
  118.     // bit mask
  119.     unsigned char bmask = 1u << (CHAR_BIT-1);
  120.  
  121.     unsigned int strindex = 0; // result byte being stored (counts up representing bits)
  122.     result[strspace] = '\0'; // add null term to end of string
  123.  
  124.     while (strindex/CHAR_BIT < tsize)
  125.     { // scan bytes forward until end of data
  126.         for (; bmask > 0u; bmask >>= 1u, strindex++)
  127.             result[strindex] = (data[strindex/CHAR_BIT] & bmask) ? '1' : '0';
  128.  
  129.         bmask = 1u << (CHAR_BIT-1); // reset mask
  130.     }
  131.  
  132.     return result;
  133. }
  134.  
  135.  
  136. unsigned char *de_d2h(unsigned char const *restrict const data, unsigned int const tsize, unsigned int const byte)
  137. {// convert data to hexidecimal
  138.     // Hexi-my-decimal const alphabet            :^)
  139.     char const hexi[16] = {'0','1','2','3',
  140.                            '4','5','6','7',
  141.                            '8','9','A','B',
  142.                            'C','D','E','F'};
  143.  
  144.     // bit BYTE mask
  145.     unsigned int bmask = 15u << (CHAR_BIT/2); // Most Significant Nibble Mask
  146.     unsigned int index = 0;
  147.     unsigned int const shift = CHAR_BIT / 2;
  148.  
  149.     // ptr to store the resulting string
  150.     unsigned char *result = NULL;
  151.  
  152.     if (byte != 0 && byte <= tsize)
  153.     { // scan user-specified byte
  154.         result = malloc(sizeof(char) * 3);
  155.         // malloc 2 characters + null ptr
  156.         if (result == NULL)
  157.             return NULL;
  158.  
  159.         result[2] = '\0'; // adding null term
  160.  
  161.         for (; index < 2; index++)
  162.         { // mask and shift to index to proper hex character
  163.             result[index] = (index & 1) ?
  164.                 hexi[(data[index/2] & bmask) >> shift] : // mask most significant byte
  165.                 hexi[data[index/2] & (bmask >> shift)]; // mask least significant byte
  166.         }
  167.     }
  168.     else
  169.     { // scan all bytes
  170.         result = malloc(tsize + sizeof(char));
  171.         // malloc all data + 1 character, and check error
  172.         if (result == NULL)
  173.             return NULL;
  174.  
  175.         // add null term
  176.         result[tsize] = '\0';
  177.  
  178.         for (; index < tsize; index++)
  179.         {// mask and shift to index to proper hex character
  180.             result[index] = (index & 1) ?
  181.                 hexi[(data[index/2] & bmask) >> shift] : // mask most significant byte
  182.                 hexi[data[index/2] & (bmask >> shift)]; // mask least significant byte
  183.         }
  184.     }
  185.  
  186.     return result;
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement