0

I have the following code below. It can successfully convert .txt files to LZW compressed files, however, when I run it on a .jpg (which should in theory work). The first char/int it reads is "255", then strcat() tries to concatenate k_str to wk on the second loop which should in theory be "216 or \330". Wk_ptr == "\377", incomplete sequence \330 . I'm not sure what the issue is.

Thanks

void encode(FILE *in, FILE *out) {
    unsigned char w[ENTRYSIZE] = "";
    unsigned char *w_ptr = w;
    unsigned char wk[ENTRYSIZE] = "";
    unsigned char *wk_ptr;
    unsigned char k;
    unsigned char k_str[2];
    unsigned int c;
    unsigned int i;

    for (i = 0; i < ASCII; i++) {
        dict[i][1] = i;
        dict[i][0] = 1;
    }

    while ((c = fgetc(in)) != EOF) {
        k_str[0] = c;
        k_str[1] = '\0';
        w_ptr = w;
        strcpy(wk, w_ptr);
        strcat(wk, k_str);
        wk_ptr = wk;


        if (check_dict(wk_ptr, 0) == 1) {
            strcpy(w, wk_ptr);
        } else {
            write12(out, check_dict(w_ptr, 1));
            add_dict(wk_ptr);
            k_str[0] = c;
            k_str[1] = '\0';
            strcpy(w, k_str);
        }
    }
    write12(out, check_dict(w_ptr, 1));
    flush12(out);
    write12(out, check_dict(w_ptr, 1));
}
Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
  • 7
    *when I run it on a .jpg (which should in theory work)* - no it should not work. `jpg` files are composed of arbitrary binary data and not from strings. Yet you are using string functions which cannot properly process binary data. As a side note - running LZW on JPG is pointless. – Eugene Sh. Oct 30 '19 at 19:20
  • Please don't use `strcpy` or `strcat` in new code. Btw; why is this tagged as C++? It looks more like C - you wouldn't write it like this in modern C++ *ever*. – Jesper Juhl Oct 30 '19 at 19:34

0 Answers0