[sxmp]: bug fixed #57;

v0.4.xx
Alexander Vdolainen 9 years ago
parent 097b29397b
commit d4180ed54b

@ -21,6 +21,7 @@
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sxmp/base64.h>
@ -35,16 +36,29 @@ static inline void encodeblock(unsigned char *in, unsigned char *out, int len)
out[3] = (unsigned char) (len > 2 ? b64[ (int)(in[2] & 0x3f) ] : '=');
}
static inline char __block(char b)
{
const char *j = strchr(b64, b);
if(j) return j - b64;
else return 0;
}
static inline void decodeblock(unsigned char *in, unsigned char *out)
{
out[0] = (unsigned char) (in[0] << 2 | in[1] >> 4);
out[1] = (unsigned char) (in[1] << 4 | in[2] >> 2);
out[2] = (unsigned char) (((in[2] << 6) & 0xc0) | in[3]);
char a = __block(in[0]);
char b = __block(in[1]);
char c = __block(in[2]);
char d = __block(in[3]);
out[0] = (unsigned char) (a << 2 | b >> 4);
out[1] = (unsigned char) (b << 4 | c >> 2);
out[2] = (unsigned char) (((c << 6) & 0xc0) | d);
}
size_t sx_rawlen2b64len(size_t raw_length)
{
return 4 * ((raw_length + 2) / 3);
return (size_t)(((4 * raw_length / 3) + 3) & ~3);
}
size_t sx_b64encode_in(const char *data, char *bdata, size_t data_len)
@ -72,6 +86,7 @@ size_t sx_b64encode_in(const char *data, char *bdata, size_t data_len)
} else ib[i] = (unsigned char)0;
}
encodeblock(ib, (unsigned char *)bdata + n, len);
break;
}
}

Loading…
Cancel
Save