/** * Decompresses single file if building stand-alone DeRNC tool. * @param pname Name of the program executable. * @param iname File name of the compressed input file. * @param oname File name of the decompressed output. * @return Returns 0 on success. On error prints a message * and returns nonzero value. */ int main_unpack (char *pname, char *iname, char *oname) { FILE *ifp, *ofp; long plen, ulen; void *packed, *unpacked; char buffer[4]; long leeway; ifp = fopen(iname, "rb"); if (!ifp) { ERRORF("%s: %s",iname,strerror(errno)); return 1; } //Checking if the file is RNC fseek (ifp, 0L, SEEK_END); plen = ftell (ifp); rewind (ifp); if (plen < 4) // Can't be an RNC file { if (strcmp (iname, oname)) return copy_file (iname, oname); return 0; } fread(buffer, 1, 4, ifp); if (strncmp(buffer, "RNC", 3) != 0) { fclose (ifp); if (strcmp(iname, oname) != 0) return copy_file(iname, oname); return 0; } rewind(ifp); //Reading compressed data, 8 bytes in buffer are for safety packed = malloc(plen+8); if (packed==NULL) { fclose(ifp); ERRORF("%s: %s",pname,strerror(errno)); return 1; } unsigned long rdlen; rdlen=fread(packed, 1, plen, ifp); fclose(ifp); //Getting unpacked file size & allocating space ulen = rnc_ulen(packed); if (ulen < 0) { free(packed); if (ulen == -1) // File wasn't RNC to start with return 0; printf("Error: %s\n", rnc_error (ulen)); return 1; } //Creating output buffer, 8 bytes are for safety unpacked = malloc(ulen+8); if (unpacked == NULL) { free(packed); ERRORF("%s: %s",pname,strerror(errno)); return 1; } //Do the decompression ulen = rnc_unpack (packed, unpacked, RNC_IGNORE_NONE, &leeway); if (ulen < 0) { printf("%s\n", rnc_error(ulen)); return 1; } //Write results to a file ofp = fopen(oname, "wb"); if (!ofp) { ERRORF("%s: %s",oname,strerror(errno)); return 1; } fwrite (unpacked, 1, ulen, ofp); fclose (ofp); free (unpacked); free (packed); return 0; }
int main_unpack (char *pname, char *iname, char *oname) { FILE *ifp, *ofp; long plen, ulen; void *packed, *unpacked; char buffer[4]; ifp = fopen(iname, "rb"); if (!ifp) { perror(iname); return 1; } fseek (ifp, 0L, SEEK_END); plen = ftell (ifp); rewind (ifp); if (plen < 4) /* Can't be an RNC file */ { if (strcmp (iname, oname)) return copy_file (iname, oname); return 0; } fread (buffer, 1, 4, ifp); if (strncmp (buffer, "RNC", 3)) { fclose (ifp); if (strcmp (iname, oname)) return copy_file (iname, oname); return 0; } rewind (ifp); packed = malloc(plen); if (!packed) { perror(pname); return 1; } fread (packed, 1, plen, ifp); fclose (ifp); ulen = rnc_ulen (packed); if (ulen < 0) { free (packed); if (ulen == -1) /* File wasn't RNC to start with */ return 0; fprintf(stderr, "%s\n", rnc_error (ulen)); return 1; } unpacked = malloc(ulen); if (!unpacked) { perror(pname); return 1; } ulen = rnc_unpack (packed, unpacked); if (ulen < 0) { fprintf(stderr, "%s\n", rnc_error (ulen)); return 1; } ofp = fopen(oname, "wb"); if (!ofp) { perror(oname); return 1; } fwrite (unpacked, 1, ulen, ofp); fclose (ofp); free (unpacked); free (packed); return 0; }