Advertisement
Guest User

dr. Derek Denkins-Smith

a guest
Nov 5th, 2017
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.96 KB | None | 0 0
  1. #include <cstdint>
  2. #include <cstdio>
  3. #include <cinttypes>
  4.  
  5. namespace bitcompactor
  6. {
  7.     template <typename T>
  8.     T read_bits(const void *p, int first, int last)
  9.     {
  10.          int bits = last - first + 1;
  11.          int offset = first;
  12.          T dat = 0;
  13.          const std::uint8_t *src = (std::uint8_t *)p;
  14.  
  15.          src += (offset/8);
  16.          offset -= (offset/8) * 8;
  17.  
  18.          if (offset > 0) {
  19.                 dat |= *src >> offset;
  20.                 if (bits < 8) {
  21.                      dat &= ((1 << bits) - 1);
  22.                 }
  23.                 src++;
  24.                 offset = (8 - offset);
  25.                 bits -= offset;
  26.          }
  27.  
  28.          while (bits >= 8) {
  29.                 dat |= *src << offset;
  30.                 src++;
  31.                 offset += 8;
  32.                 bits -= 8;
  33.          }
  34.  
  35.          if (bits > 0) {
  36.                 dat |= (*src << offset) & (((1 << bits) - 1) << offset);
  37.          }
  38.  
  39.          return dat;
  40.     }
  41.  
  42.     // returns words written (characters)
  43.     int translate(void *data_ptr, const int data_bits, char *output, const char *alphabet, const int word_bits)
  44.     {
  45.         typedef std::uint32_t T;
  46.         int offset = 0;
  47.         for (int i = 0; i < data_bits; i += word_bits) {
  48.             T word = read_bits<T>(data_ptr, i, i + word_bits - 1);
  49.             output[offset++] = alphabet[word];
  50.         }
  51.         // add null
  52.         output[offset] = 0;
  53.         return offset;
  54.     }
  55. }
  56.  
  57. int main(int argc, char **argv)
  58. {
  59.     const char *alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ012345";
  60.     // Oooh fabulous arms in the air, wiggle your hands and fingers
  61.     std::uint64_t fabulous_data[2] =
  62.     {
  63.         0xDEADBEEFFEEDF00D,
  64.         // padding, translate() should/could if I wasn't lazy
  65.         0,
  66.     };
  67.     // DANGEROUS! ... my middle name is Danger ...
  68.     // that's right, dr. Derek D. Danger Denkins ... -Smithe
  69.     char output_string[128];
  70.     int output_words = bitcompactor::translate(fabulous_data, 64, output_string, alphabet, 5);
  71.  
  72.     std::printf("FABULOUS!!! ... no wait ...\n");
  73.     std::printf("fabulous_data = %" SCNxFAST64 "\n", fabulous_data[0]);
  74.     std::printf("Fabulalphabet = %s\n", alphabet);
  75.     std::printf("output_words = %i\n", output_words);
  76.     std::printf("output_string = \"%s\"\n", output_string);
  77.  
  78.     // twas all for naught
  79.     return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement