⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 os2zip.c

📁 infozip2.2源码
💻 C
📖 第 1 页 / 共 2 页
字号:
#ifdef __32BIT__/* Perhaps due to bugs in the current OS/2 2.0 kernel, the success or   failure of the DosEnumAttribute() and DosQueryPathInfo() system calls   depends on the area where the return buffers are allocated. This   differs for the various compilers, for some alloca() works, for some   malloc() works, for some, both work. We'll have to live with that. *//* The use of malloc() is not very convenient, because it requires   backtracking (i.e. free()) at error returns. We do that for system   calls that may fail, but not for malloc() calls, because they are VERY   unlikely to fail. If ever, we just leave some memory allocated   over the usually short lifetime of a zip process ... */#ifdef __GNUC__#define alloc(x) alloca(x)#define unalloc(x)#else#define alloc(x) malloc(x)#define unalloc(x) free(x)#endifvoid GetEAs(char *path, char **bufptr, size_t *size,                        char **cbufptr, size_t *csize){  FILESTATUS4 fs;  PDENA2 pDENA, pFound;  EAOP2 eaop;  PGEA2 pGEA;  PGEA2LIST pGEAlist;  PFEA2LIST pFEAlist;  PEFHEADER pEAblock;  ULONG ulAttributes, ulMemoryBlock;  ULONG nLength;  ULONG nBlock;  char szName[CCHMAXPATH];  *size = *csize = 0;  strcpy(szName, path);  nLength = strlen(szName);  if (szName[nLength - 1] == '/')    szName[nLength - 1] = 0;  if (DosQueryPathInfo(szName, FIL_QUERYEASIZE, (PBYTE) &fs, sizeof(fs)))    return;  nBlock = max(fs.cbList, 65535);  if ((pDENA = alloc((size_t) nBlock)) == NULL)    return;  ulAttributes = -1;  if (DosEnumAttribute(ENUMEA_REFTYPE_PATH, szName, 1, pDENA, nBlock,                       &ulAttributes, ENUMEA_LEVEL_NO_VALUE)    || ulAttributes == 0    || (pGEAlist = alloc((size_t) nBlock)) == NULL)  {    unalloc(pDENA);    return;  }  pGEA = pGEAlist -> list;  memset(pGEAlist, 0, nBlock);  pFound = pDENA;  while (ulAttributes--)  {    if (!(strcmp(pFound -> szName, ".LONGNAME") == 0 && use_longname_ea))    {      pGEA -> cbName = pFound -> cbName;      strcpy(pGEA -> szName, pFound -> szName);      nLength = sizeof(GEA2) + strlen(pGEA -> szName);      nLength = ((nLength - 1) / sizeof(ULONG) + 1) * sizeof(ULONG);      pGEA -> oNextEntryOffset = ulAttributes ? nLength : 0;      pGEA   = (PGEA2)  ((PCH) pGEA + nLength);    }    pFound = (PDENA2) ((PCH) pFound + pFound -> oNextEntryOffset);  }  if (pGEA == pGEAlist -> list) /* no attributes to save */  {    unalloc(pDENA);    unalloc(pGEAlist);    return;  }  pGEAlist -> cbList = (PCH) pGEA - (PCH) pGEAlist;  pFEAlist = (PVOID) pDENA;  /* reuse buffer */  pFEAlist -> cbList = nBlock;  eaop.fpGEA2List = pGEAlist;  eaop.fpFEA2List = pFEAlist;  eaop.oError = 0;  if (DosQueryPathInfo(szName, FIL_QUERYEASFROMLIST,                       (PBYTE) &eaop, sizeof(eaop)))  {    unalloc(pDENA);    unalloc(pGEAlist);    return;  }  /* The maximum compressed size is (in case of STORE type) the     uncompressed size plus the size of the compression type field     plus the size of the CRC field. */  ulAttributes = pFEAlist -> cbList;  ulMemoryBlock = ulAttributes + sizeof(USHORT) + sizeof(ULONG);  pEAblock = (PEFHEADER) malloc(sizeof(EFHEADER) + ulMemoryBlock);  if (pEAblock == NULL)  {    unalloc(pDENA);    unalloc(pGEAlist);    return;  }  *bufptr = (char *) pEAblock;  *size = sizeof(EFHEADER);  pEAblock -> nID = EF_OS2EA;  pEAblock -> nSize = sizeof(pEAblock -> lSize);  pEAblock -> lSize = ulAttributes; /* uncompressed size */  nLength = memcompress((char *) (pEAblock + 1), ulMemoryBlock,                        (char *) pFEAlist, ulAttributes);  *size += nLength;  pEAblock -> nSize += nLength;  if ((pEAblock = (PEFHEADER) malloc(sizeof(EFHEADER))) == NULL)  {    unalloc(pDENA);    unalloc(pGEAlist);    return;  }  *cbufptr = (char *) pEAblock;  *csize = sizeof(EFHEADER);  pEAblock -> nID = EF_OS2EA;  pEAblock -> nSize = sizeof(pEAblock -> lSize);  pEAblock -> lSize = ulAttributes;  if (noisy)    printf(" (%ld bytes EA's)", ulAttributes);  unalloc(pDENA);  unalloc(pGEAlist);}#else /* !__32BIT__ */typedef struct{  ULONG oNextEntryOffset;  BYTE fEA;  BYTE cbName;  USHORT cbValue;  CHAR szName[1];}FEA2, *PFEA2;typedef struct{  ULONG cbList;  FEA2 list[1];}FEA2LIST, *PFEA2LIST;void GetEAs(char *path, char **bufptr, size_t *size,                        char **cbufptr, size_t *csize){  FILESTATUS2 fs;  PDENA1 pDENA, pFound;  EAOP eaop;  PGEALIST pGEAlist;  PGEA pGEA;  PFEALIST pFEAlist;  PFEA pFEA;  PFEA2LIST pFEA2list;  PFEA2 pFEA2;  EFHEADER *pEAblock;  ULONG ulAttributes;  USHORT nLength, nMaxSize;  char szName[CCHMAXPATH];  *size = *csize = 0;  strcpy(szName, path);  nLength = strlen(szName);  if (szName[nLength - 1] == '/')    szName[nLength - 1] = 0;  if (DosQueryPathInfo(szName, FIL_QUERYEASIZE, (PBYTE) &fs, sizeof(fs))      || fs.cbList <= 2 * sizeof(ULONG))    return;  ulAttributes = -1;  nMaxSize = (USHORT) min(fs.cbList * 2, 65520L);  if ((pDENA = malloc((size_t) nMaxSize)) == NULL)    return;  if (DosEnumAttribute(ENUMEA_REFTYPE_PATH, szName, 1, pDENA, fs.cbList,                       &ulAttributes, ENUMEA_LEVEL_NO_VALUE)    || ulAttributes == 0    || (pGEAlist = malloc(nMaxSize)) == NULL)  {    free(pDENA);    return;  }  pGEA = pGEAlist -> list;  pFound = pDENA;  while (ulAttributes--)  {    nLength = strlen(pFound -> szName);    if (!(strcmp(pFound -> szName, ".LONGNAME") == 0 && use_longname_ea))    {      pGEA -> cbName = pFound -> cbName;      strcpy(pGEA -> szName, pFound -> szName);      pGEA++;      pGEA = (PGEA) (((PCH) pGEA) + nLength);    }    pFound++;    pFound = (PDENA1) (((PCH) pFound) + nLength);  }  if (pGEA == pGEAlist -> list)  {    free(pDENA);    free(pGEAlist);    return;  }  pGEAlist -> cbList = (PCH) pGEA - (PCH) pGEAlist;  pFEAlist = (PFEALIST) pDENA; /* reuse buffer */  pFEAlist -> cbList = fs.cbList;  pFEA = pFEAlist -> list;  eaop.fpGEAList = pGEAlist;  eaop.fpFEAList = pFEAlist;  eaop.oError = 0;  if (DosQueryPathInfo(szName, FIL_QUERYEASFROMLIST,                       (PBYTE) &eaop, sizeof(eaop)))  {    free(pDENA);    free(pGEAlist);    return;  }  /* now convert into new OS/2 2.0 32-bit format */  pFEA2list = (PFEA2LIST) pGEAlist;  /* reuse buffer */  pFEA2 = pFEA2list -> list;  while ((PCH) pFEA - (PCH) pFEAlist < pFEAlist -> cbList)  {    nLength = sizeof(FEA) + pFEA -> cbName + 1 + pFEA -> cbValue;    memcpy((PCH) pFEA2 + sizeof(pFEA2 -> oNextEntryOffset), pFEA, nLength);    memset((PCH) pFEA2 + sizeof(pFEA2 -> oNextEntryOffset) + nLength, 0, 3);    pFEA = (PFEA) ((PCH) pFEA + nLength);    nLength = sizeof(FEA2) + pFEA2 -> cbName + 1 + pFEA2 -> cbValue;    nLength = ((nLength - 1) / sizeof(ULONG) + 1) * sizeof(ULONG);    /* rounded up to 4-byte boundary */    pFEA2 -> oNextEntryOffset =      ((PCH) pFEA - (PCH) pFEAlist < pFEAlist -> cbList) ? nLength : 0;    pFEA2 = (PFEA2) ((PCH) pFEA2 + nLength);  }  pFEA2list -> cbList = (PCH) pFEA2 - (PCH) pFEA2list;  ulAttributes = pFEA2list -> cbList;  pEAblock = (PEFHEADER) pDENA; /* reuse buffer */  *bufptr = (char *) pEAblock;  *size = sizeof(EFHEADER);  pEAblock -> nID = EF_OS2EA;  pEAblock -> nSize = sizeof(pEAblock -> lSize);  pEAblock -> lSize = ulAttributes; /* uncompressed size */  nLength = (USHORT) memcompress((char *) (pEAblock + 1),    nMaxSize - sizeof(EFHEADER), (char *) pFEA2list, ulAttributes);  *size += nLength;  pEAblock -> nSize += nLength;  pEAblock = (PEFHEADER) pGEAlist;  *cbufptr = (char *) pEAblock;  *csize = sizeof(EFHEADER);  pEAblock -> nID = EF_OS2EA;  pEAblock -> nSize = sizeof(pEAblock -> lSize);  pEAblock -> lSize = ulAttributes;  if (noisy)    printf(" (%ld bytes EA's)", ulAttributes);}#endif /* __32BIT__ */void GetACL(char *path, char **bufptr, size_t *size,                        char **cbufptr, size_t *csize){  static char *buffer;  char *cbuffer;  long bytes, cbytes;  PEFHEADER pACLblock;  if (buffer == NULL) /* avoid frequent allocation (for every file) */    if ((buffer = malloc(ACL_BUFFERSIZE)) == NULL)      return;  if (acl_get(NULL, path, buffer))    return; /* this will be the most likely case */  bytes = strlen(buffer);  cbytes = bytes + sizeof(USHORT) + sizeof(ULONG);  if ((*bufptr = realloc(*bufptr, *size + sizeof(EFHEADER) + cbytes)) == NULL)    return;  pACLblock = (PEFHEADER) (*bufptr + *size);  cbuffer = (char *) (pACLblock + 1);  cbytes = memcompress(cbuffer, cbytes, buffer, bytes);  *size += sizeof(EFHEADER) + cbytes;  pACLblock -> nID = EF_ACL;  pACLblock -> nSize = sizeof(pACLblock -> lSize) + cbytes;  pACLblock -> lSize = bytes; /* uncompressed size */  if ((*cbufptr = realloc(*cbufptr, *csize + sizeof(EFHEADER))) == NULL)    return;  pACLblock = (PEFHEADER) (*cbufptr + *csize);  *csize += sizeof(EFHEADER);  pACLblock -> nID = EF_ACL;  pACLblock -> nSize = sizeof(pACLblock -> lSize);  pACLblock -> lSize = bytes;  if (noisy)    printf(" (%ld bytes ACL)", bytes);}#ifdef USE_EF_UT_TIMEint GetExtraTime(struct zlist far *z, iztimes *z_utim){  int eb_c_size = EB_HEADSIZE + EB_UT_LEN(1);  int eb_l_size = eb_c_size;  char *eb_c_ptr;  char *eb_l_ptr;  eb_c_ptr = realloc(z->cextra, (z->cext + eb_c_size));  if (eb_c_ptr == NULL)    return ZE_MEM;  z->cextra = eb_c_ptr;  eb_c_ptr += z->cext;  z->cext += eb_c_size;  eb_c_ptr[0]  = 'U';  eb_c_ptr[1]  = 'T';  eb_c_ptr[2]  = EB_UT_LEN(1);          /* length of data part of e.f. */  eb_c_ptr[3]  = 0;  eb_c_ptr[4]  = EB_UT_FL_MTIME;  eb_c_ptr[5]  = (char)(z_utim->mtime);  eb_c_ptr[6]  = (char)(z_utim->mtime >> 8);  eb_c_ptr[7]  = (char)(z_utim->mtime >> 16);  eb_c_ptr[8]  = (char)(z_utim->mtime >> 24);  if (z_utim->mtime != z_utim->atime || z_utim->mtime != z_utim->ctime)  {    eb_c_ptr[4]  = EB_UT_FL_MTIME | EB_UT_FL_ATIME | EB_UT_FL_CTIME;    eb_l_size = EB_HEADSIZE + EB_UT_LEN(3); /* only on HPFS they can differ */  /* so only then it makes sense to store all three time stamps */  }  eb_l_ptr = realloc(z->extra, (z->ext + eb_l_size));  if (eb_l_ptr == NULL)    return ZE_MEM;  z->extra = eb_l_ptr;  eb_l_ptr += z->ext;  z->ext += eb_l_size;  memcpy(eb_l_ptr, eb_c_ptr, eb_c_size);  if (eb_l_size > eb_c_size)  {    eb_l_ptr[2]  = EB_UT_LEN(3);    eb_l_ptr[9]  = (char)(z_utim->atime);    eb_l_ptr[10] = (char)(z_utim->atime >> 8);    eb_l_ptr[11] = (char)(z_utim->atime >> 16);    eb_l_ptr[12] = (char)(z_utim->atime >> 24);    eb_l_ptr[13] = (char)(z_utim->ctime);    eb_l_ptr[14] = (char)(z_utim->ctime >> 8);    eb_l_ptr[15] = (char)(z_utim->ctime >> 16);    eb_l_ptr[16] = (char)(z_utim->ctime >> 24);  }  return ZE_OK;}#endif /* USE_EF_UT_TIME */int set_extra_field(struct zlist far *z, iztimes *z_utim){  /* store EA data in local header, and size only in central headers */  GetEAs(z->name, &z->extra, &z->ext, &z->cextra, &z->cext);  /* store ACL data in local header, and size only in central headers */  GetACL(z->name, &z->extra, &z->ext, &z->cextra, &z->cext);#ifdef USE_EF_UT_TIME  /* store extended time stamps in both headers */  return GetExtraTime(z, z_utim);#else /* !USE_EF_UT_TIME */  return ZE_OK;#endif /* ?USE_EF_UT_TIME */}#endif /* !UTIL *//* Initialize the table of uppercase characters including handling of   country dependent characters. */void init_upper(){  COUNTRYCODE cc;  unsigned nCnt, nU;  for (nCnt = 0; nCnt < sizeof(upper); nCnt++)    upper[nCnt] = lower[nCnt] = (unsigned char) nCnt;  cc.country = cc.codepage = 0;  DosMapCase(sizeof(upper), &cc, (PCHAR) upper);  for (nCnt = 0; nCnt < 256; nCnt++)  {    nU = upper[nCnt];    if (nU != nCnt && lower[nU] == (unsigned char) nU)      lower[nU] = (unsigned char) nCnt;  }  for (nCnt = 'A'; nCnt <= 'Z'; nCnt++)    lower[nCnt] = (unsigned char) (nCnt - 'A' + 'a');}char *StringLower(char *szArg){  unsigned char *szPtr;  for (szPtr = (unsigned char *) szArg; *szPtr; szPtr++)    *szPtr = lower[*szPtr];  return szArg;}#if defined(__IBMC__) && defined(__DEBUG_ALLOC__)void DebugMalloc(void){  _dump_allocated(0); /* print out debug malloc memory statistics */}#endif/******************************//*  Function version_local()  *//******************************/void version_local(){    static ZCONST char CompiledWith[] = "Compiled with %s%s for %s%s%s%s.\n\n";#if defined(__IBMC__) || defined(__WATCOMC__) || defined(_MSC_VER)    char buf[80];#endif    printf(CompiledWith,#ifdef __GNUC__#  ifdef __EMX__  /* __EMX__ is defined as "1" only (sigh) */      "emx+gcc ", __VERSION__,#  else      "gcc/2 ", __VERSION__,#  endif#elif defined(__IBMC__)      "IBM ",#  if (__IBMC__ < 200)      (sprintf(buf, "C Set/2 %d.%02d", __IBMC__/100,__IBMC__%100), buf),#  elif (__IBMC__ < 300)      (sprintf(buf, "C Set++ %d.%02d", __IBMC__/100,__IBMC__%100), buf),#  else      (sprintf(buf, "Visual Age C++ %d.%02d", __IBMC__/100,__IBMC__%100), buf),#  endif#elif defined(__WATCOMC__)      "Watcom C", (sprintf(buf, " (__WATCOMC__ = %d)", __WATCOMC__), buf),#elif defined(__TURBOC__)#  ifdef __BORLANDC__      "Borland C++",#    if (__BORLANDC__ < 0x0460)        " 1.0",#    elif (__BORLANDC__ == 0x0460)        " 1.5",#    else        " 2.0",#    endif#  else      "Turbo C",#    if (__TURBOC__ >= 661)       "++ 1.0 or later",#    elif (__TURBOC__ == 661)       " 3.0?",#    elif (__TURBOC__ == 397)       " 2.0",#    else       " 1.0 or 1.5?",#    endif#  endif#elif defined(MSC)      "Microsoft C ",#  ifdef _MSC_VER      (sprintf(buf, "%d.%02d", _MSC_VER/100, _MSC_VER%100), buf),#  else      "5.1 or earlier",#  endif#else      "unknown compiler", "",#endif /* __GNUC__ */      "OS/2",/* GRR:  does IBM C/2 identify itself as IBM rather than Microsoft? */#if (defined(MSC) || (defined(__WATCOMC__) && !defined(__386__)))#  if defined(M_I86HM) || defined(__HUGE__)      " (16-bit, huge)",#  elif defined(M_I86LM) || defined(__LARGE__)      " (16-bit, large)",#  elif defined(M_I86MM) || defined(__MEDIUM__)      " (16-bit, medium)",#  elif defined(M_I86CM) || defined(__COMPACT__)      " (16-bit, compact)",#  elif defined(M_I86SM) || defined(__SMALL__)      " (16-bit, small)",#  elif defined(M_I86TM) || defined(__TINY__)      " (16-bit, tiny)",#  else      " (16-bit)",#  endif#else      " 2.x/3.x (32-bit)",#endif#ifdef __DATE__      " on ", __DATE__#else      "", ""#endif    );    /* temporary debugging code for Borland compilers only */#ifdef __TURBOC__    printf("\t(__TURBOC__ = 0x%04x = %d)\n", __TURBOC__, __TURBOC__);#ifdef __BORLANDC__    printf("\t(__BORLANDC__ = 0x%04x)\n",__BORLANDC__);#else    printf("\tdebug(__BORLANDC__ not defined)\n");#endif#ifdef __TCPLUSPLUS__    printf("\t(__TCPLUSPLUS__ = 0x%04x)\n", __TCPLUSPLUS__);#else    printf("\tdebug(__TCPLUSPLUS__ not defined)\n");#endif#ifdef __BCPLUSPLUS__    printf("\t(__BCPLUSPLUS__ = 0x%04x)\n\n", __BCPLUSPLUS__);#else    printf("\tdebug(__BCPLUSPLUS__ not defined)\n\n");#endif#endif /* __TURBOC__ */} /* end function version_local() */#endif /* OS2 */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -