Wrong char value in C - c

Need some help in the last part of this function:
char *lfsr(char *bin)
{
//bits significativos para fazer o xor 128 -> 128,126,101,99;
int bits[4];
int bit;
if(bin[0] == '0')
bits[0] = 0;
else if(bin[0] == '1')
bits[0] = 1;
if(bin[2] == '0')
bits[1] = 0;
else if(bin[2] == '1')
bits[1] = 1;
if(bin[21] == '0')
bits[2] = 0;
else if(bin[21] == '1')
bits[2] = 1;
if(bin[19] == '0')
bits[3] = 0;
else if(bin[19] == '1')
bits[3] = 1;
bit = bits[0] ^ bits[1] ^ bits[2] ^ bits[3] ^ 1;
//reconstruir o vector de char depois do lfsr
for(int i = 127; i >= 1; i--)
{
bin[i] = bin[i - 1];
}
bin[0] = (int)bit;
return bin;
}
Why the value of bin[0] is a strange character instead 0 or 1?
Anyone can help me please?
Thanks in advance. Cumps!

The problem is character '0' and integer 0 being different. character '0' is actually the ascii value 48.
try bin[0] = (char)(48 + bit);

I'm a C language psychic and my crystal ball tells me that somewhere in your program you did this:
printf("the value in bit[0] is %c\n", bit[0]);

Well, probably one of your if-elses didn't work.
Either bin[0], bin[2], bin[21] or bin[19] is neither '0', nor '1'.
Try running this code with your test data:
char *lfsr(char *bin)
{
//bits significativos para fazer o xor 128 -> 128,126,101,99;
int bits[4];
int bit;
if(bin[0] == '0')
bits[0] = 0;
else
{
if(bin[0] == '1')
bits[0] = 1;
else
printf("First if-else failed. bin[0] : %c\n", bin[0]);
}
if(bin[2] == '0')
bits[1] = 0;
else
{
if(bin[2] == '1')
bits[1] = 1;
else
printf("Second if-else failed. bin[2] : %c\n", bin[2]);
}
if(bin[21] == '0')
bits[2] = 0;
else
{
if(bin[21] == '1')
bits[2] = 1;
else
printf("Third if-else failed. bin[21] : %c\n", bin[21]);
}
if(bin[19] == '0')
bits[3] = 0;
else
{
if(bin[19] == '1')
bits[3] = 1;
else
printf("Fourth if-else failed. bin[19] : %c\n", bin[19]);
}
bit = bits[0] ^ bits[1] ^ bits[2] ^ bits[3] ^ 1;
//reconstruir o vector de char depois do lfsr
for(int i = 127; i >= 1; i--)
{
bin[i] = bin[i - 1];
}
bin[0] = (int)bit;
return bin;
}

Related

How to convert a signature from IDA style to code style in C?

Essentially I have a function which takes in a signature like "0F 2F 05 D8 20 ?? 03 72 41" and converts it to the signature like "\x0F\x2F\x05\xD8\x20\x90\x03\x72\x41" where wild card bytes ?? get converted to nop. Also it creates a mask where every non wild card byte gets represented as x. In this case it is xxxxx?xxx.
I tried to create such a function but I struggle to convert spaces to \x.
void idastyle_to_codestyle(const char* pattern, char* codestyle_pattern, char* mask) {
int patternLength = (int)strlen(pattern);
int j = 0, k = 0;
for (int i = 0; i < patternLength; i++) {
if (pattern[i] == ' ') {
codestyle_pattern[j] = '\x';
mask[k] = 'x';
j++; k++;
}
else if (pattern[i] == '?') {
codestyle_pattern[j] = '9';
codestyle_pattern[j + 1] = '0';
mask[k] = '?';
j += 2; k++;
}
else {
codestyle_pattern[j] = pattern[i];
j++;
}
}
}
int main() {
char* codestyle_pattern, char* mask;
idastyle_to_codestyle("0F 2F 05 D8 20 ?? 03 72 41", codestyle_pattern, mask);
return 0;
}
The algorithm is completely wrong but it doesnt matter as the compiler gives an error at codestyle_pattern[j] = '\x';. This function has char* codestyle_pattern and char* mask as outputs.
I found a solution. It doesn't validate signatures because it is a waste of time.
int hex2int(char ch)
{
if (ch >= '0' && ch <= '9')
return ch - '0';
if (ch >= 'A' && ch <= 'F')
return ch - 'A' + 10;
if (ch >= 'a' && ch <= 'f')
return ch - 'a' + 10;
return -1;
}
void idastyle_to_codestyle(const char* pattern, char* codestylePattern, char* mask)
{
int patternLength = (int)strlen(pattern);
int i = 0; int j = 0;
while (i < patternLength) {
if (pattern[i] == '?') {
if (pattern[i + 1] == '?') {
codestylePattern[j] = '\x90';
mask[j] = '?';
i += 3; j++;
}
}
else {
int firstDigit = hex2int(pattern[i]);
int secondDigit = hex2int(pattern[i + 1]);
codestylePattern[j] = (char)(firstDigit * 0x10 + secondDigit);
mask[j] = 'x';
i += 3; j++;
}
}
}

How To Fix Segmentation Fault 11 in a C program?

I am new to C programming. I'm writing a function that converts an integer into hexadecimal.
For some reason, I am getting a segmentation fault 11. Please advise. Thank you!
Here is the code for my function:
it converts the integer to binary first
adds 0s where it is needed so binary length would be multiples of 4
reverses the order of the binary
converts every 4 numbers into hexadecimal
void printHexadecimalForm( int X )
//Purpose: Print parameter X in hexadecimal form
//Output: Hexadecimal representation of X directly printed
//Assumption: X is non-negative (i.e. >= 0)
{
//[TODO] CHANGE this to your solution.
int input = X;
int output[32];
int i = 0;
while(input != 0){
if(input%2 != 0){
input = input - 1;
input /= 2;
output[i] = 1;
i++;
}
else{
input /= 2;
output[i] = 0;
i++;
}
}
while(i % 4 != 0){
output[i + 1] = 0;
i++;
}
for (int j = 0; j < i/2; j++)
{
int temp = output[j];
output[j] = output[i - 1 - j];
output[i - 1 - j] = temp;
}
int c, k = 0;
for(int z = 0; z < i; z += 4; ){
for (c = z; c < c + 4; c++){
k = 10 * k + output[c];
}
if(k == 0000){
printf("%d",0);
}
if(k == 0001){
printf("%d",1);
}
if(k == 0010){
printf("%d",2);
}
if(k == 0011){
printf("%d",3);
}
if(k == 0100){
printf("%d",4);
}
if(k == 0101){
printf("%d",5);
}
if(k == 0110){
printf("%d",6);
}
if(k == 0111){
printf("%d",7);
}
if(k == 1000){
printf("%d",8);
}
if(k == 1001){
printf("%d",9);
}
if(k == 1010){
printf("%c", 'A');
}
if(k == 1011){
printf("%c", 'B');
}
if(k == 1100){
printf("%c", 'C');
}
if(k == 1101){
printf("%c", 'D');
}
if(k == 1110){
printf("%c", 'E');
}
if(k == 1111){
printf("%c", 'F');
}
}
}
I suggest you to take deep breath and start all over again. First remember, there's no need to convert anything to binary. Everything is binary already.
Maybe this little piece, which retrieves two hexadecimal characters could help you to get on to the track: (this is just one method)
int n = 165; // this is the number we want to display in hex (165 is 0xa5)
int i, hexChar;
i = n & 0xF; // bitwise AND with 00...001111
if(i < 10) // look up to ASCII table for more info
hexChar = i + 48; // character '0' is code 48, '1' is 49 etc.
else
hexChar = i + 55; // character 'A' is code 65, 'B' is 66 etc.
printf("Rigth most hex: %c\n", hexChar);
i = n >> 4; // shift all bits 4 steps to the right
i = i & 0xF; // bitwise AND with 00...001111
if(i < 10) // look up to ASCII table for more info
hexChar = i + 48; // character '0' is code 48, '1' is 49 etc.
else
hexChar = i + 55; // character 'A' is code 65, 'B' is 66 etc.
printf("Second hex: %c\n", hexChar);

Problem with converting the elements of the queue in C

The exercise is next:
"user enters some numbers (like 129109212). Then this number filters where are only '0' and '1' left (1101). Then if the last number length is more then 4, then every section from 4 numbers converts to a decimal (from binary), called "res" now. if one of the "res" is more then 8, the whole entered number deleted from the queue and the next number converts. Then print all the results".
EXAMPLE (with 2 numbers):
Enter the number: 10203040506071
The binary is: 10000001
(here are two numbers: 1000 and 0001. they convert (8 and 1))
THE RESULT IS: 81
Enter the number: 11023232030232
The binary is: 11000
(Here are 1100 and 0 - 1100 is more than 8 (12) and number deletes from the queue)
THE END RESULT:
1) 81
---END---
HERE IS THE CODE (LOGICALLY ALRIGHT BUT ERRORS OCCUR (reading error and so on( ))
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <math.h>
#define QMAX 100
typedef long long int li;
struct queue {
li qu[QMAX];
int rear, frnt;
};
//FUNCTIONS FOR THER QUEUE
void init(struct queue* q) {
q->frnt = 1;
q->rear = 0;
puts("--------THE QUEUE WAS CREATED SUCCESSFULLY!!!--------");
return;
}
void insert(struct queue* q, li x) {
if (q->rear < QMAX - 1) {
q->rear++;
//strcpy(q->qu[q->rear], str);
q->qu[q->rear] = x;
}
else
printf("---THE QUEUE IS FULL!---\n");
return;
}
int isempty(struct queue* q) {
if (q->rear == 0) return(1);
else return(0);
}
void print(struct queue* q) {
puts("----THE ELEMENTS OF THRE QUEUE ARE: ----");
int h;
if (isempty(q) == 1) {
printf("Очередь пуста!\n");
return;
}
for (h = q->frnt; h <= q->rear; h++)
printf("%lli\n", q->qu[h]);
return;
}
li removeEl(struct queue* q) {
int h; li x;
if (isempty(q) == 1) {
printf("Очередь пуста!\n");
return(0);
}
x = q->qu[q->frnt];
for (h = q->frnt; h < q->rear; h++) {
q->qu[h] = q->qu[h + 1];
}
q->rear--;
return(x);
}
int main()
{
char str[20], inputStr[100];
int i, num;
printf("Enter the amount of numbers: \n"); scanf("%d", &num);
struct queue* queue;
queue = (struct queue*)malloc(sizeof(struct queue));
init(queue);
for (i = 0; i < num; i++) {
li x;
printf("Enter your string : "); getchar();
scanf("%lli", &x); getchar();
insert(queue, x);
}
puts("THE QUEUE IS: ");
print(queue);
int h;
for (h = queue->frnt; h < queue->rear + 1; h++) {
char inputStr[127], res[20];
sprintf(inputStr, "%lli", queue->qu[h]);
//DLETING EVERYTHING IS NOT 0 OR 1
int j = 0;
for (i = 0; inputStr[i]; i++)
if (inputStr[i] >= '0' && inputStr[i] <= '1' || inputStr[i] == ' ' || inputStr[i] == '\0') {
inputStr[j] = inputStr[i];
j++;
}
inputStr[j] = '\0';
printf("String after modification : ");
printf("%s\n", inputStr); getchar();
int len = strlen(inputStr);
int temp[4];
int k;
char str[4] = "";
for (i = 0; i < len; i += 4) {
if (inputStr[i] != '\0' || inputStr[i] == '1' || inputStr[i] == '0')
temp[0] = inputStr[i] - '0';
if (inputStr[i + 1] != '\0' || inputStr[i + 1] == '1' || inputStr[i + 1] == '0')
temp[1] = inputStr[i + 1] - '0';
if (inputStr[i + 2] != '\0' || inputStr[i + 2] == '1' || inputStr[i + 2] == '0')
temp[2] = inputStr[i + 2] - '0';
if (inputStr[i + 3] != '\0' || inputStr[i + 3] == '1' || inputStr[i + 3] == '0')
temp[3] = inputStr[i + 3] - '0';
sprintf(str, "%d%d%d%d", temp[0], temp[1], temp[2], temp[3]);
k = str - '0';
//convertation into decimal system
int counter = 1, timeless, decimal = 0;
strcpy(res, "");
while (k != 0) {
timeless = k % 10 * counter;
decimal += timeless;
k /= 10;
counter *= 2;
}
if (decimal > 8) { removeEl(queue->qu[h]);}
else {
char dec; dec = decimal + '0';
strcat(res, dec);
}
strcat(res, '\0');
}
int num = res - '0';
queue->qu[h] = num;
printf("The decimal is: %d", queue->qu[h]);
}
/*
fgets(inputStr, 100, stdin); getchar();
int j = 0;
for (i = 0; inputStr[i]; i++) {
if (inputStr[i] >= '0' && inputStr[i] <= '1' || inputStr[i] == ' ' || inputStr[i] == '\0') {
inputStr[j] = inputStr[i];
j++;
}
}
inputStr[j] = '\0';
printf("String after modification : ");
printf("%s\n", inputStr); getchar();
int len = strlen(inputStr);
enqueue(queue, inputStr);
char num1[100] = "";
int temp[4];
int res = 0;
strcpy(str, "");
for (i = 0; i < len; i += 4) {
if (inputStr[i] != '\0' || inputStr[i] == '1' || inputStr[i] == '0')
temp[0] = inputStr[i] - '0';
if (inputStr[i + 1] != '\0' || inputStr[i + 1] == '1' || inputStr[i + 1] == '0')
temp[1] = inputStr[i + 1] - '0';
if (inputStr[i + 2] != '\0' || inputStr[i + 2] == '1' || inputStr[i + 2] == '0')
temp[2] = inputStr[i + 2] - '0';
if (inputStr[i + 3] != '\0' || inputStr[i + 3] == '1' || inputStr[i + 3] == '0')
temp[3] = inputStr[i + 3] - '0';
sprintf(str, "%d%d%d%d", temp[0], temp[1], temp[2], temp[3]);
strcat(num1, str);
strcat(num1, " ");
sscanf(str, "%d", &res);
printf("\nres is: %d", res);
//RES is converted to number char
//convertation into decimal system
int counter = 1, timeless, decimal = 0;
while (res != 0) {
timeless = res % 10 * counter;
decimal += timeless;
res /= 10;
counter *= 2;
}
printf("\ndecimal is: %d", decimal);
if (decimal > 9) dequeue(queue);
}
for (int i = queue->front; i < queue->size; i++) {
char str1[100] = "";
int len = strlen(&queue->array[i]);
int temp1[4];
int c = 0;
char decimals[32] = "";
char deca[32];
int res = 0;
for (i = 0; i < len; i += 4) {
strcpy(deca, "");
if (queue->array[i] != '\0' || queue->array[i] == '1' || queue->array[i] == '0')
temp1[0] = queue->array[i] - '0';
if (queue->array[i + 1] != '\0' || queue->array[i + 1] == '1' || queue->array[i + 1] == '0')
temp1[1] = queue->array[i + 1] - '0';
if (queue->array[i + 2] != '\0' || queue->array[i + 2] == '1' || queue->array[i + 2] == '0')
temp1[2] = queue->array[i + 2] - '0';
if (queue->array[i + 3] != '\0' || queue->array[i + 2] == '1' || queue->array[i + 2] == '0')
temp1[3] = queue->array[i + 2] - '0';
sprintf(str, "%d%d%d%d", temp1[0], temp1[1], temp1[2], temp1[3]);
strcat(str1, str);
strcat(str1, " ");
sscanf(str, "%d", &res);
printf("\nres is: %d", res);
//RES is converted to number char
//convertation into decimal system
int counter = 1, timeless, decimal = 0;
while (res != 0) {
timeless = res % 10 * counter;
decimal += timeless;
res /= 10;
counter *= 2;
}
char aka = decimal + '0';
strcat(decimals, &aka);
}
//printf("%s --- %s", str1, decimals);
}
*/
getchar();
return 0;
}
int convertBinaryToDecimal(long long n)
{
int decimalNumber = 0, i = 0, remainder;
while (n != 0)
{
remainder = n % 10;
n /= 10;
decimalNumber += remainder * pow(2, i);
++i;
}
return decimalNumber;
}

Need help regarding lzss compression in C

I had used this code from https://oku.edu.mie-u.ac.jp/~okumura/compression/lzss.c
This code was for file compression. I had modified it for a given string. For example :
d(2306):AuthorisationScheme:RADIUSserveratfd04:bd3:80e8:1::1usingPAPD/6LoWPANd(2306):WritingModule:SecurityConfigD/6LoWPANd(2306):WritingModule:RunCoordinatorD/6LoWPANd(2306):RequestingmoduleaddressD/6LoWPANd(2306):WritingModule:GetAddressD/smartcard-jni(2781):SmartCard_state_update_callback:status=6D/SmartCardNative(2781):status=6D/smartcard(2781):PN532Smartcard_loop_threadexitD/smartcard-jni(2781):SmartCard_loop_thread:SMARTCARD_STATUS_ABORTD/smartcard(2781):Smartcard_loop_uninitD/smartcard(2781):2D/serialcomm_pn532(2781):PN532:Readingfrom/dev/ttyUSB0-->D/smartcard(2781):Received(0x3)fromPN532:(dataBuf[0]:0x1)(0x1f5988)D/smartcard(2781):ReceivedStatusfromPN532:OK(cmd:0x2)D/smartcard-jni(2781):SmartCard_listener_update_callbackD/smartcard(2781):Received(0x1c2)fromPN532:(dataBuf[0]:0x32)(0x1f5988)D/smartcard(2781):vd(2306):AuthorisationScheme:RADIUSserveratfd04:bd3:80e8:1::1usingPAPD/6LoWPANd(2306):
The problem I am facing is, if I want to compress a file containing this , the code is able to do that. But the modified code that I had done having little bit of problem. The last 2 bits of compressed files are different. Though the content of file and the buffer both are exactly same.
The original code was reading from file. Here, I provided the string of same content.
The code goes here :
/* LZSS encoder-decoder (c) Haruhiko Okumura */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define EI 11 /* typically 10..13 */
#define EJ 4 /* typically 4..5 */
#define P 1 /* If match length <= P then output one character */
#define N (1 << EI) /* buffer size */
#define F ((1 << EJ) + P) /* lookahead buffer size */
int bit_buffer = 0, bit_mask = 128;
unsigned long codecount = 0, textcount = 0;
unsigned char buffer[N * 2];
FILE *infile, *outfile, *outfile2, *outfile3;
/*----*/
unsigned long payload_i = 0;
int tt = 0;
unsigned int buf_load[16000];
unsigned char *string_buf= "d(2306):AuthorisationScheme:RADIUSserveratfd04:bd3:80e8:1::1usingPAPD/6LoWPANd(2306):WritingModule:SecurityConfigD/6LoWPANd(2306):WritingModule:RunCoordinatorD/6LoWPANd(2306):RequestingmoduleaddressD/6LoWPANd(2306):WritingModule:GetAddressD/smartcard-jni(2781):SmartCard_state_update_callback:status=6D/SmartCardNative(2781):status=6D/smartcard(2781):PN532Smartcard_loop_threadexitD/smartcard-jni(2781):SmartCard_loop_thread:SMARTCARD_STATUS_ABORTD/smartcard(2781):Smartcard_loop_uninitD/smartcard(2781):2D/serialcomm_pn532(2781):PN532:Readingfrom/dev/ttyUSB0-->D/smartcard(2781):Received(0x3)fromPN532:(dataBuf[0]:0x1)(0x1f5988)D/smartcard(2781):ReceivedStatusfromPN532:OK(cmd:0x2)D/smartcard-jni(2781):SmartCard_listener_update_callbackD/smartcard(2781):Received(0x1c2)fromPN532:(dataBuf[0]:0x32)(0x1f5988)D/smartcard(2781):vd(2306):AuthorisationScheme:RADIUSserveratfd04:bd3:80e8:1::1usingPAPD/6LoWPANd(2306):";
/*----*/
void error(void)
{
printf("Output error\n"); exit(1);
}
void putbit1(void)
{
outfile2 = fopen("file2.lzss", "a");
bit_buffer |= bit_mask;
if ((bit_mask >>= 1) == 0) {
/*----*/
buf_load[payload_i] = bit_buffer;
if (fputc(buf_load[payload_i], outfile3) == EOF) error();
payload_i++;
/*----*/
if (fputc(bit_buffer, outfile2) == EOF) error();
if (fputc(bit_buffer, outfile) == EOF) error();
bit_buffer = 0; bit_mask = 128; codecount++;
}
fclose(outfile2);
}
void putbit0(void)
{
outfile2 = fopen("file2.lzss", "a");
if ((bit_mask >>= 1) == 0) {
/*----*/
buf_load[payload_i] = bit_buffer;
if (fputc(buf_load[payload_i], outfile3) == EOF) error();
payload_i++;
/*----*/
if (fputc(bit_buffer, outfile2) == EOF) error();
if (fputc(bit_buffer, outfile) == EOF) error();
bit_buffer = 0; bit_mask = 128; codecount++;
}
fclose(outfile2);
}
void flush_bit_buffer(void)
{
outfile2 = fopen("file2.lzss", "a");
if (bit_mask != 128) {
if (fputc(buf_load[payload_i], outfile3) == EOF) error();
if (fputc(bit_buffer, outfile2) == EOF) error();
if (fputc(bit_buffer, outfile) == EOF) error();
codecount++;
}
fclose(outfile2);
}
void output1(int c)
{
int mask;
putbit1();
mask = 256;
while (mask >>= 1) {
if (c & mask) putbit1();
else putbit0();
}
}
void output2(int x, int y)
{
int mask;
putbit0();
mask = N;
while (mask >>= 1) {
if (x & mask) putbit1();
else putbit0();
}
mask = (1 << EJ);
while (mask >>= 1) {
if (y & mask) putbit1();
else putbit0();
}
}
void encode(void)
{
int i, j, f1, x, y, r, s, bufferend, c;
for (i = 0; i < N - F; i++) buffer[i] = ' ';
for (i = N - F; i < N * 2; i++) {
if ((c = fgetc(infile)) == EOF) break;
if((c = string_buf[tt++]) == '\0') break;
buffer[i] = c; textcount++; //tt++;
}
bufferend = i; r = N - F; s = 0;
while (r < bufferend) {
f1 = (F <= bufferend - r) ? F : bufferend - r;
x = 0; y = 1; c = buffer[r];
for (i = r - 1; i >= s; i--)
if (buffer[i] == c) {
for (j = 1; j < f1; j++)
if (buffer[i + j] != buffer[r + j]) break;
if (j > y) {
x = i; y = j;
}
}
if (y <= P) output1(c);
else output2(x & (N - 1), y - 2);
r += y; s += y;
if (r >= N * 2 - F) {
for (i = 0; i < N; i++) buffer[i] = buffer[i + N];
bufferend -= N; r -= N; s -= N;
while (bufferend < N * 2) {
if ((c = fgetc(infile)) == EOF) break;
if((c = string_buf[tt++]) == '\0') break;
//tt++;
buffer[bufferend++] = c; textcount++;
}
}
}
flush_bit_buffer();
printf("text: %ld bytes\n", textcount);
printf("code: %ld bytes (%ld%%)\n",
codecount, (codecount * 100) / textcount);
}
int getbit(int n) /* get n bits */
{
int i, x;
static int buf, mask = 0;
x = 0;
for (i = 0; i < n; i++) {
if (mask == 0) {
if ((buf = fgetc(infile)) == EOF) return EOF;
mask = 128;
}
x <<= 1;
if (buf & mask) x++;
mask >>= 1;
}
return x;
}
void decode(void)
{
int i, j, k, r, c;
for (i = 0; i < N - F; i++) buffer[i] = ' ';
r = N - F;
while ((c = getbit(1)) != EOF) {
if (c) {
if ((c = getbit(8)) == EOF) break;
fputc(c, outfile);
buffer[r++] = c; r &= (N - 1);
} else {
if ((i = getbit(EI)) == EOF) break;
if ((j = getbit(EJ)) == EOF) break;
for (k = 0; k <= j + 1; k++) {
c = buffer[(i + k) & (N - 1)];
fputc(c, outfile);
buffer[r++] = c; r &= (N - 1);
}
}
}
}
int main(int argc, char *argv[])
{
int enc;
char *s;
memset(buf_load, '\0', sizeof(buf_load));
outfile3 = fopen ("file1.lzss", "wb");
if (argc != 4) {
printf("Usage: lzss e/d infile outfile\n\te = encode\td = decode\n");
return 1;
}
s = argv[1];
if (s[1] == 0 && (*s == 'd' || *s == 'D' || *s == 'e' || *s == 'E'))
enc = (*s == 'e' || *s == 'E');
else {
printf("? %s\n", s); return 1;
}
if ((infile = fopen(argv[2], "rb")) == NULL) {
printf("? %s\n", argv[2]); return 1;
}
if ((outfile = fopen(argv[3], "wb")) == NULL) {
printf("? %s\n", argv[3]); return 1;
}
if (enc) encode(); else decode();
fclose(infile); fclose(outfile);
fclose(outfile3);
return 0;
}
Looks like the issue lies in buffer reading-writing vs file read write. In file read-write the pointer is increased to next mem loc and reading and writing the same way. In string, each array is being read by incrementing index and then the compressed value is written in file in file write. In file, it is read as binary (wb), In string it is being read as array element. Can there be a problem for that ? Need expert's advice on that.
void flush_bit_buffer(void)
{
outfile2 = fopen("file2.lzss", "a");
if (bit_mask != 128) {
flush_bit[payload_i] = bit_buffer; /*This last bit should be put which I missed. It solved my issue*/
if (fputc(buf_load[payload_i], outfile3) == EOF) error();
if (fputc(bit_buffer, outfile2) == EOF) error();
if (fputc(bit_buffer, outfile) == EOF) error();
codecount++;
}
fclose(outfile2);
}
I solved it by adding the line in the code snippet. I needed to add last bit of bit buffer which solved the problem.

Send a variable to a function and modify this variable in C

I have the following vars:
char seed[NBITS + 1], x0[NBITS + 1], y0[NBITS + 1], z0[NBITS + 1], dT0[NBITS + 1];
And i want to change it values on this function:
void lfsr(char *bin, char *output)
{
//bits significativos para fazer o xor NBITS -> NBITS,126,101,99;
int bits[4];
int bit;
if(bin[0] == '0')
bits[0] = 0;
else if(bin[0] == '1')
bits[0] = 1;
if(bin[2] == '0')
bits[1] = 0;
else if(bin[2] == '1')
bits[1] = 1;
if(bin[21] == '0')
bits[2] = 0;
else if(bin[21] == '1')
bits[2] = 1;
if(bin[19] == '0')
bits[3] = 0;
else if(bin[19] == '1')
bits[3] = 1;
bit = bits[0] ^ bits[1] ^ bits[2] ^ bits[3] ^ 1;
//reconstruir o vector de char depois do lfsr
for(int i = 127; i >= 1; i--)
{
bin[i] = bin[i - 1];
}
bin[0] = (char)(48 + bit);
output = bin;
}
The way that I put the value in y0 from x is, for example, calling the lfsr functions like this:
lfsr(x0, y0);
What am I doing wrong?
I have to do 3 Fibonacci Linear Feedback Shift Register starting from x0.
x0 = 10101010101010
y0 = lfsr(101010101010)
z0 = lfsr(y0)
dT0 = lfsr(z0);
The results are good, but when I do the above code the value of x0 will be the same as dT0 if i use pointers.
Can anyone help me?
Thanks. Cumps!
Consider the following:
The numbers correspond to the taps. The bits are actually 15..0, left to right. The following is my implementation of the Fibonacci Linear Feedback Shift Register:
#include <stdio.h>
uint16_t fibLfsr(const uint16_t num)
{
uint16_t tempNum;
tempNum = (num) ^ (num >> 2) ^ (num >> 3) ^ (num >> 5);
tempNum = (tempNum & 0x1) << 15;
tempNum = (tempNum | (num >> 1));
return tempNum;
}
int main(void)
{
uint16_t testNum = 0xACE1;
printf("%#X\n", testNum);
testNum = fibLfsr(testNum);
printf("%#X\n", testNum);
return 0;
}
I'm not quite sure why you're using strings and converting them to binary. If this is necessary, you'll need some of the standard library APIs in stdlib and string to convert the string to an uint16_t before calling fibLfsr() and back to a string afterwards.

Resources