Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.10 KB | None | 0 0
  1. #include <skier.h>
  2. #include "skibase16.h"
  3.  
  4. static unsigned char skibase16_decode_char(unsigned char c)
  5. {
  6.     if(c >= '0' && c <= '9') return c - '0';
  7.     if(c >= 'A' && c <= 'Z') return c - 'A' + 10;
  8.     if(c >= 'a' && c <= 'z') return c - 'a' + 10;
  9.     return 0xFF;
  10. }
  11.  
  12. int skibase16_decode(char *buff, unsigned long size, unsigned char *out)
  13. {
  14.     if(buff == NULL || size == 0) return 0;
  15.     if(size & 1) size--; if(out == NULL) out = (unsigned char *) buff;
  16.     unsigned char c, *base = out;
  17.     for (; size; size -= 2) {
  18.         if((c = skibase16_decode_char(*buff++)) == 0xFF) return 0;
  19.         *out = c << 4;
  20.         if((c = skibase16_decode_char(*buff++)) == 0xFF) return 0;
  21.         *out++ |= c;
  22.     }
  23.     *out = 0;
  24.     return out - base;
  25. }
  26.  
  27. int skibase16_encode(unsigned char *buff, unsigned long size, char *out)
  28. {
  29.     if(buff == NULL || size == 0 || out == NULL) return 0;
  30.     char *base = out;
  31.     while (size) {
  32.         *out++ = "0123456789ABCDEF"[*buff >> 4];
  33.         *out++ = "0123456789ABCDEF"[*buff & 15];
  34.         size--, buff++;
  35.     }
  36.     *out = 0;
  37.     return out - base;
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement