Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /************************************************
- * Deprint is an FGL utility. It is comprised *
- * of two types of functions. Conversions *
- * read ptr-passed data and malloc()s a string *
- * that represents the data in hex or binary, *
- * and returns a ptr to it (null terminated). *
- * Print functions are inline and call the *
- * conversion functions, print the returned *
- * string and representation type (b/hex/etc) *
- * and then immediately frees the memory. *
- *************************************************/
- #include <limits.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdint.h>
- //#include "deprint.h"
- /// Prototypes
- // convert to (null terminated) string
- unsigned char *de_d2b_le(unsigned char const *restrict const data, unsigned int const tsize); // data to binary (litte endian)
- unsigned char *de_d2b_be(unsigned char const *restrict const data, unsigned int const tsize); // data to binary (big endian)
- unsigned char *de_d2h(unsigned char const *restrict const data, unsigned int const tsize, unsigned int const byte); // data to hex
- // print string ("\n type : converted representation\n")
- static inline void de_pble(unsigned char const *restrict const data, unsigned int const tsize); // print binary (little endian)
- static inline void de_pbbe(unsigned char const *restrict const data, unsigned int const tsize); // print binary (big endian)
- static inline void de_phex(unsigned char const *restrict const data, unsigned int const tsize, unsigned int const byte); // print hexidecimal
- /// Printing functions
- static inline void de_pble(unsigned char const *restrict const data, unsigned int const tsize)
- {// print binary little-endian
- char *ptr = (char *)de_d2b_le(data, tsize);
- fputs(" Binary (LE): ", stdout);
- puts(ptr);
- free(ptr);
- return;
- }
- static inline void de_pbbe(unsigned char const *restrict const data, unsigned int const tsize)
- {// print binary big-endian
- char *ptr = (char *)de_d2b_be(data, tsize);
- fputs(" Binary (BE): ", stdout);
- puts(ptr);
- free(ptr);
- return;
- }
- static inline void de_phex(unsigned char const *restrict const data, unsigned int const tsize, unsigned int const byte)
- {// print hex
- char *ptr = (char *)de_d2h(data, tsize, byte);
- if (byte)
- printf(" Hex Byte#%.2i: ",byte);
- else
- fputs(" Hexidecimal: ", stdout);
- puts(ptr);
- free(ptr);
- return;
- }
- /// DEPRINT C FILE
- /// Conversions
- unsigned char *de_d2b_le(unsigned char const *restrict const data, unsigned int const tsize)
- {// convert data to binary (little-endian)
- const int strspace = (tsize * CHAR_BIT); // (tsize << 3) used multiple calcs
- unsigned char *result = malloc(strspace + sizeof(char));
- // malloc required char space ((num of bits in tsize) + null term)
- if (result == NULL)
- return NULL;
- // bit mask and bitshift bit to most-significant bit.
- unsigned char bmask = 1u << (CHAR_BIT-1);
- int dataindex; // data byte being scaned (counts up representing bytes)
- int strindex = strspace; // result byte being stored (counts down representing bits)
- result[strindex] = '\0'; // add null term and decrement strindex by the char used.
- /* WTF IS THE WHILE & FOR LOOP DOING?!
- forgot to add this last time and i had to spend
- 10 minutes figuring out what the fuck i was doing
- one back , read fwd, one back, repeat for as many bytes
- ( (<<*8) , (>>&)*8 , (<<*8) ), repeat * tsize
- */
- while (strindex > 0)
- {
- strindex -= CHAR_BIT; // go to beginning of unread byte
- dataindex = (strindex / (-CHAR_BIT)) + (tsize - 1); // calc read byte
- for (; bmask > 0u; bmask >>= 1u, strindex++) // scan byte
- result[strindex] = (data[dataindex] & bmask) ? '1' : '0';
- bmask = 1u << (CHAR_BIT-1); // Reset mask
- strindex -= CHAR_BIT; // go to beginning of read byte
- }
- return result;
- }
- unsigned char *de_d2b_be(unsigned char const *restrict const data, unsigned int const tsize)
- {// convert data to binary (big-endian)
- const int strspace = (tsize * CHAR_BIT); // (tsize << 3) used multiple calcs
- unsigned char *result = malloc(strspace + sizeof(char));
- // malloc required char space ((num of bits in tsize) + null term)
- if (result == NULL)
- return NULL;
- // bit mask
- unsigned char bmask = 1u << (CHAR_BIT-1);
- unsigned int strindex = 0; // result byte being stored (counts up representing bits)
- result[strspace] = '\0'; // add null term to end of string
- while (strindex/CHAR_BIT < tsize)
- { // scan bytes forward until end of data
- for (; bmask > 0u; bmask >>= 1u, strindex++)
- result[strindex] = (data[strindex/CHAR_BIT] & bmask) ? '1' : '0';
- bmask = 1u << (CHAR_BIT-1); // reset mask
- }
- return result;
- }
- unsigned char *de_d2h(unsigned char const *restrict const data, unsigned int const tsize, unsigned int const byte)
- {// convert data to hexidecimal
- // Hexi-my-decimal const alphabet :^)
- char const hexi[16] = {'0','1','2','3',
- '4','5','6','7',
- '8','9','A','B',
- 'C','D','E','F'};
- // bit BYTE mask
- unsigned int bmask = 15u << (CHAR_BIT/2); // Most Significant Nibble Mask
- unsigned int index = 0;
- unsigned int const shift = CHAR_BIT / 2;
- // ptr to store the resulting string
- unsigned char *result = NULL;
- if (byte != 0 && byte <= tsize)
- { // scan user-specified byte
- result = malloc(sizeof(char) * 3);
- // malloc 2 characters + null ptr
- if (result == NULL)
- return NULL;
- result[2] = '\0'; // adding null term
- for (; index < 2; index++)
- { // mask and shift to index to proper hex character
- result[index] = (index & 1) ?
- hexi[(data[index/2] & bmask) >> shift] : // mask most significant byte
- hexi[data[index/2] & (bmask >> shift)]; // mask least significant byte
- }
- }
- else
- { // scan all bytes
- result = malloc(tsize + sizeof(char));
- // malloc all data + 1 character, and check error
- if (result == NULL)
- return NULL;
- // add null term
- result[tsize] = '\0';
- for (; index < tsize; index++)
- {// mask and shift to index to proper hex character
- result[index] = (index & 1) ?
- hexi[(data[index/2] & bmask) >> shift] : // mask most significant byte
- hexi[data[index/2] & (bmask >> shift)]; // mask least significant byte
- }
- }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement