📄 par.c
字号:
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- * * The contents of this file are subject to the Mozilla Public * License Version 1.1 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. * * The Original Code is par & libprc, released May 13, 1999. * * The Initial Developer of the Original Code is David Williams, * http://www.djw.org. Portions created by David Williams are * Copyright (C) 1999 David Williams. All Rights Reserved. * Contributor(s): * Hallvard Tr鎡teberg * Andrew Trevarrow *//* * par(1) see usage. * * created: djw@djw.org, January 2, 1999. *//* * Strictly speaking I guess prcsys.h is internal to the libprc library, * so it shouldn't be included by a user app (par). But it's so convenient * to deal with the (mac) porting issue that crop up here. */#include "prcsys.h"/* * prc.h is the public interface to libprc and should be all and user * program needs to use the library. */#include "prc.h"static char*prcstrtime(char* buf, prc_time_t ptime){ return prctimetostr(buf, ptime);}static intlist_header(prc_t* prc, FILE* fp){ char buf[256]; fprintf(fp, "name: %s\n", prc->name); fprintf(fp, "type: %s\n", prctypetostr(buf, prc->type)); fprintf(fp, "cid: %s\n", prctypetostr(buf, prc->cid)); prcstrfattr(buf, prc->flags); if (buf[0] == '\0') /* no flags */ strcpy(buf, "0x0000"); fprintf(fp, "attributes: %s\n" "version: %d\n", buf, prc->version); fprintf(fp, "ctime: %s\n", prcstrtime(buf, prc->ctime)); fprintf(fp, "mtime: %s\n", prcstrtime(buf, prc->mtime)); fprintf(fp, "btime: %s\n", prcstrtime(buf, prc->btime)); fprintf(fp, "modnum: %ld\n" "szappinfo: %ld\n" "szsortinfo: %ld\n" "nrecords: %d\n" "", prc->modnum, prc->appinfoSize, prc->sortinfoSize, prc->nrecords); return 0;}static intprcinfo(prc_t* prc){ list_header(prc, stdout); return 0;}static char*prcstrrexflags(char* buf, prc_byte_t flags){ if ((flags & PRC_REC_FLAGS_DELETE) != 0) buf[0] = 'D'; else buf[0] = '-'; if ((flags & PRC_REC_FLAGS_DIRTY) != 0) buf[1] = 'd'; else buf[1] = '-'; if ((flags & PRC_REC_FLAGS_BUSY) != 0) buf[2] = 'b'; else buf[2] = '-'; if ((flags & PRC_REC_FLAGS_SECRET) != 0) buf[3] = 's'; else buf[3] = '-'; buf[4] = '\0'; return buf;}static voiddata_dump(FILE* fp, void* datav, unsigned datalen, unsigned maxlen){ unsigned i; unsigned char* data = (unsigned char*)datav; if (datalen < maxlen) maxlen = datalen; for (i = 0; i < maxlen; i++) { int c = data[i]; if (isprint(c)) { fputc(c, fp); } else { fputc('.', fp); } }}static intlist_record_mappee(prc_record_t* record, void* arg){ FILE* fp = (FILE*)arg; char buf[256]; prcstrrexflags(buf, record->flags); fprintf(fp, "%s %2d %4ld ", buf, PRC_REC_FLAGS_GET_CATEGORY(record->flags), record->datalen); data_dump(fp, record->data, record->datalen, 48); fputc('\n', fp); return 0; /* keep going */}static intlist_resource_mappee(prc_resource_t* resource, void* arg){ FILE* fp = (FILE*)arg; char buf[256]; fprintf(fp, "%s %04x %4ld ", prctypetostr(buf, resource->type), resource->id, resource->datalen); data_dump(fp, resource->data, resource->datalen, 48); fputc('\n', fp); return 0; /* keep going */}static intprclist(prc_t* prc){ if (PRC_IS_RESOURCE(prc)) return prcmapresources(prc, list_resource_mappee, stdout); else return prcmaprecords(prc, list_record_mappee, stdout);}static intdata_to_file(void* data, prc_uint32_t datalen, const char* filename){ FILE* fp; int rv = 0; fp = fopen(filename, "wb"); if (!fp) { fprintf(stderr, "could not open %s\n", filename); return -1; } if (fwrite(data, 1, (size_t)datalen, fp) != (size_t)datalen) { fprintf(stderr, "write to %s failed\n", filename); rv = -1; } fclose(fp); return rv;}static prc_byte_t*file_to_data(const char* filename, prc_uint32_t* datalen_a){ FILE* fp; struct stat statbuf; prc_byte_t* data; prc_uint32_t datalen; fp = fopen(filename, "rb"); if (!fp) { fprintf(stderr, "cannot open %s\n", filename); return NULL; } if (fstat(fileno(fp), &statbuf) == -1) { fprintf(stderr, "could not stat!\n"); fclose(fp); return NULL; } datalen = (prc_uint32_t)statbuf.st_size; data = (prc_byte_t*)malloc((size_t)datalen); if (fread(data, 1, (size_t)datalen, fp) == -1) { fprintf(stderr, "could not stat!\n"); free(data); data = NULL; } fclose(fp); *datalen_a = datalen; return data;}static intextract_one_record(prc_record_t* record, unsigned index, const char* filename){ char buf[32]; if (!filename) { sprintf(buf, "%03d.%x.%ld.pdr", index, record->flags, record->id); filename = buf; } return data_to_file(record->data, record->datalen, filename);}static intextract_records(prc_t* prc, char** argv){ int i; prc_uint16_t index; prc_record_t* record; for (i = 0; argv[i] != NULL; i++) { char* a = argv[i]; char* f; f = NULL; if (strcmp(a, "-f") == 0) { f = argv[++i]; a = argv[++i]; } index = atoi(a); record = prcgetrecord(prc, index); if (!record) { fprintf(stderr, "index %d not found\n", index); return -1; } if (extract_one_record(record, index, f) == -1) return -1; } if (i == 0) { /* all */ record = NULL; index = 0; while ((record = prcgetnextrecord(prc, record)) != NULL) { extract_one_record(record, index, NULL); index++; } } return 0;}static prc_uint16_tprcstrtoid(const char* id_s){ prc_uint16_t id; if (id_s[0] == '0' && (id_s[1] == 'x' || id_s[1] == 'X')) { id_s += 2; id = (prc_uint16_t)strtol(id_s, NULL, 16); } else { id = (prc_uint16_t)strtol(id_s, NULL, 10); } return id;}static intextract_one_resource(prc_resource_t* resource, char* filename){ char buf[16]; if (!filename) { sprintf(buf, "%s%04x.bin", prctypetostr(buf, resource->type), resource->id); filename = buf; } return data_to_file(resource->data, resource->datalen, filename);}static intextract_resources(prc_t* prc, char** argv){ int i; prc_resource_t* resource; /* [-f filename] type id */ for (i = 0; argv[i] != NULL;) { char* f; prc_uint16_t id; prc_type_t type; char* type_s; char* id_s; type_s = argv[i]; f = NULL; if (strcmp(type_s, "-f") == 0) { i++; f = argv[i++]; type_s = argv[i]; } i++; id_s = argv[i++]; type = prcstrtotype(type_s); id = prcstrtoid(id_s); resource = prcgetresource(prc, type, id); if (!resource) { fprintf(stderr, "resource, type='%s', id=0x%x not found\n", type_s, id); return -1; } if (extract_one_resource(resource, f) == -1) return -1; } if (i == 0) { /* all */ resource = NULL; while ((resource = prcgetnextresource(prc, resource)) != NULL) { if (extract_one_resource(resource, NULL) == -1) return -1; } } return 0;}static unsignedparse_info_args(char** argv, char** appname, unsigned* doAppinfo, char** sortname, unsigned* doSortinfo){ unsigned nargs = 0; char* arg; if (appname != NULL) *appname = NULL; if (sortname != NULL) *sortname = NULL; if (doAppinfo != NULL) *doAppinfo = 0; if (doSortinfo != NULL) *doSortinfo = 0; while ((arg = argv[nargs]) != NULL) { if (strcmp(arg, "-A") == 0) { if (doAppinfo != NULL) *doAppinfo = 1; nargs++; if (appname != NULL) *appname = argv[nargs++]; } else if (strcmp(arg, "-S") == 0) { if (doSortinfo != NULL) *doSortinfo = 1; nargs++; if (sortname != NULL) *sortname = argv[nargs++]; } else { break; } } return nargs;}static intextract_info(prc_t* prc, char** argv){ int n = 0; char* appInfo = NULL; char* sortInfo = NULL; n += parse_info_args(argv, &appInfo, 0, &sortInfo, 0); if (appInfo != NULL) { /* dump app info to file */ data_to_file(prc->appinfoData, prc->appinfoSize, appInfo); } if (sortInfo != NULL) { /* dump sort info to file */ data_to_file(prc->sortinfoData, prc->sortinfoSize, appInfo); } return n;}static intextract_stream(prc_t* prc, char** argv){ char* filename = argv[0]; FILE* fp; int rv = 0; if (!filename) { fprintf(stderr, "no stream output file specified\n"); return -1; } fp = fopen(filename, "wb"); if (!fp) { fprintf(stderr, "could not open %s\n", filename); return -1; } for (;;) { prc_byte_t buf[4096]; prc_uint32_t nread; nread = prcstreamread(prc, buf, sizeof(buf)); if (fwrite(buf, 1, (size_t)nread, fp) != (size_t)nread) { fprintf(stderr, "write to %s failed\n", filename); rv = -1; } if (nread != sizeof(buf)) break; } fclose(fp); return rv;}static intprcextract(prc_t* prc, char** argv){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -