* Copyright (C) 2004, 2005 by Dominic Rath *
* Dominic.Rath@gmx.de *
* *
+ * Copyright (C) 2007,2008 Øyvind Harboe *
+ * oyvind.harboe@zylin.com *
+ * *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
#include "binarybuffer.h"
-int buf_set_u32(u8* buffer, unsigned int first, unsigned int num, u32 value);
-u32 buf_get_u32(u8* buffer, unsigned int first, unsigned int num);
-u32 flip_u32(u32 value, unsigned int num);
-
const unsigned char bit_reverse_table256[] =
{
0x00, 0x80, 0x40, 0xC0, 0x20, 0xA0, 0x60, 0xE0, 0x10, 0x90, 0x50, 0xD0, 0x30, 0xB0, 0x70, 0xF0,
0x0F, 0x8F, 0x4F, 0xCF, 0x2F, 0xAF, 0x6F, 0xEF, 0x1F, 0x9F, 0x5F, 0xDF, 0x3F, 0xBF, 0x7F, 0xFF
};
-int buf_set_u32(u8* buffer, unsigned int first, unsigned int num, u32 value)
-{
- unsigned int i;
-
- if (!buffer)
- return ERROR_INVALID_ARGUMENTS;
-
- for (i=first; i<first+num; i++)
- {
- if (((value >> (i-first))&1) == 1)
- buffer[i/8] |= 1 << (i%8);
- else
- buffer[i/8] &= ~(1 << (i%8));
- }
-
- return ERROR_OK;
-}
-
-u32 buf_get_u32(u8* buffer, unsigned int first, unsigned int num)
-{
- u32 result = 0;
- unsigned int i;
-
- if (!buffer)
- {
- ERROR("buffer not initialized");
- return 0;
- }
-
- for (i=first; i<first+num; i++)
- {
- if (((buffer[i/8]>>(i%8))&1) == 1)
- result |= 1 << (i-first);
- }
-
- return result;
-}
u8* buf_cpy(u8 *from, u8 *to, int size)
{
return y;
}
-char* buf_to_str(u8 *buf, int buf_len, int radix)
+char* buf_to_str(const u8 *buf, int buf_len, int radix)
{
const char *DIGITS = "0123456789abcdef";
float factor;
else if (radix == 10)
{
factor = 2.40824; /* log(256) / log(10) = 2.40824 */
- }
+ }
else if (radix == 8)
{
factor = 2.66667; /* log(256) / log(8) = 2.66667 */
str = calloc(str_len + 1, 1);
for (i = b256_len - 1; i >= 0; i--)
- {
- tmp = buf[i];
- if ((i == (buf_len / 8)) && (buf_len % 8))
- tmp &= (0xff >> (8 - (buf_len % 8)));
-
- for (j = str_len; j > 0; j--)
- {
- tmp += (u32)str[j-1] * 256;
- str[j-1] = (u8)(tmp % radix);
- tmp /= radix;
- }
- }
-
- for (j = 0; j < str_len; j++)
+ {
+ tmp = buf[i];
+ if ((i == (buf_len / 8)) && (buf_len % 8))
+ tmp &= (0xff >> (8 - (buf_len % 8)));
+
+ for (j = str_len; j > 0; j--)
+ {
+ tmp += (u32)str[j-1] * 256;
+ str[j-1] = (u8)(tmp % radix);
+ tmp /= radix;
+ }
+ }
+
+ for (j = 0; j < str_len; j++)
str[j] = DIGITS[(int)str[j]];
return str;
}
-int str_to_buf(char* str, int str_len, u8 *buf, int buf_len, int radix)
+int str_to_buf(const char *str, int str_len, u8 *buf, int buf_len, int radix)
{
char *charbuf;
u32 tmp;
for (i = 0; charbuf[i]; i++)
{
tmp = charbuf[i];
- if ((tmp >= '0') && (tmp <= '9'))
- tmp = (tmp - '0');
- else if ((tmp >= 'a') && (tmp <= 'f'))
- tmp = (tmp - 'a' + 10);
- else if ((tmp >= 'A') && (tmp <= 'F'))
- tmp = (tmp - 'A' + 10);
- else continue; /* skip characters other than [0-9,a-f,A-F] */
+ if ((tmp >= '0') && (tmp <= '9'))
+ tmp = (tmp - '0');
+ else if ((tmp >= 'a') && (tmp <= 'f'))
+ tmp = (tmp - 'a' + 10);
+ else if ((tmp >= 'A') && (tmp <= 'F'))
+ tmp = (tmp - 'A' + 10);
+ else continue; /* skip characters other than [0-9,a-f,A-F] */
if (tmp >= radix)
continue; /* skip digits invalid for the current radix */
for (j = 0; j < b256_len; j++)
- {
- tmp += (u32)b256_buf[j] * radix;
- b256_buf[j] = (u8)(tmp & 0xFF);
- tmp >>= 8;
- }
+ {
+ tmp += (u32)b256_buf[j] * radix;
+ b256_buf[j] = (u8)(tmp & 0xFF);
+ tmp >>= 8;
+ }
}