home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 6 File / 06-File.zip / ramfs102.zip / src / ea.c < prev    next >
C/C++ Source or Header  |  1996-06-11  |  6KB  |  262 lines

  1. #include "includes.h"
  2.  
  3.  
  4.  
  5. int EaGetList (PFEALIST pFEAList, PEAOP pEAOP, PBLOCK pblkEA)
  6. {
  7.   PUCHAR  pCurGEA;
  8.   PUCHAR  pCurFEA;
  9.   USHORT  cbRestFEA;
  10.   USHORT  cbRestGEA;
  11.  
  12.   pCurFEA   = (PUCHAR) &pFEAList->list[0];
  13.   cbRestFEA = (USHORT) pFEAList->cbList - sizeof(ULONG);
  14.  
  15.   pCurGEA = (PUCHAR) &pEAOP->fpGEAList->list[0];
  16.   if (pEAOP->fpGEAList->cbList < sizeof(ULONG))
  17.   {
  18.     pEAOP->oError = 0;
  19.     return ERROR_EA_LIST_INCONSISTENT;
  20.   }
  21.   cbRestGEA = (USHORT) pEAOP->fpGEAList->cbList - sizeof(ULONG);
  22.  
  23.   /* loop through all the EAs that we are asked to find */
  24.   while (cbRestGEA)
  25.   {
  26.     USHORT cbName;
  27.     USHORT cbGEA;
  28.     USHORT cbFEA;
  29.     FLAT   flatCurFEA;
  30.     FLAT   flatEnd;
  31.  
  32.     cbName = pCurGEA[0];
  33.     cbGEA  = cbName + 2;
  34.  
  35.     if (cbRestGEA < cbGEA)
  36.     {
  37.       pEAOP->oError = 0;
  38.       return ERROR_EA_LIST_INCONSISTENT;
  39.     }
  40.  
  41.     if (FSH_CHECKEANAME (1, cbName, &pCurGEA[1]) != NO_ERROR)
  42.     {
  43.       pEAOP->oError = (USHORT) pCurGEA - (USHORT) pEAOP->fpGEAList;
  44.       return ERROR_INVALID_EA_NAME;
  45.     }
  46.  
  47.     /* let's try to find this name in blkEA */
  48.     /* tentatively build an FEA with zero-length value */
  49.     cbFEA = cbName + 5;
  50.     if (cbRestFEA < cbFEA)
  51.     {
  52.       pFEAList->cbList = 0;
  53.       if (pblkEA->cbSize)
  54.     pFEAList->cbList = pblkEA->cbSize + 4;
  55.       return ERROR_BUFFER_OVERFLOW;
  56.     }
  57.     pCurFEA[0] = 0;        /* flags    */
  58.     pCurFEA[1] = cbName;    /* cbName    */
  59.     pCurFEA[2] = 0;        /* cbValue LSB    */
  60.     pCurFEA[3] = 0;        /* cbValue MSB    */
  61.     FSH_UPPERCASE (&pCurGEA[1], cbName+1, &pCurFEA[4]);
  62.  
  63.     /* scan through all FEAs in blkEA for the EA */
  64.     flatCurFEA = pblkEA->flatAddr;
  65.     flatEnd = flatCurFEA + pblkEA->cbSize;
  66.     while (flatCurFEA < flatEnd)
  67.     {
  68.       USHORT cbCurName;
  69.       USHORT cbCurValue;
  70.       USHORT cbCurFEA;
  71.  
  72.       cbCurName  = VMReadUChar  (flatCurFEA+1);
  73.       cbCurValue = VMReadUShort (flatCurFEA+2);
  74.       cbCurFEA   = 5 + cbCurName + cbCurValue;
  75.       if (cbCurName == cbName)
  76.       {
  77.     char szCurName[256];
  78.  
  79.     VMRead (szCurName, flatCurFEA+4, cbCurName);
  80.     if (!memcmp (szCurName, &pCurFEA[4], cbCurName))
  81.     {
  82.       /* this is the EA we're looking for, overwrite the tentative FEA
  83.          with this one */
  84.       if (cbRestFEA < cbCurFEA)
  85.       {
  86.         pFEAList->cbList = 0;
  87.         if (pblkEA->cbSize)
  88.           pFEAList->cbList = pblkEA->cbSize + 4;
  89.         return ERROR_BUFFER_OVERFLOW;
  90.       }
  91.       VMRead (pCurFEA, flatCurFEA, cbCurFEA);
  92.       cbFEA = cbCurFEA;
  93.       break;
  94.     }
  95.       }
  96.  
  97.       flatCurFEA += cbCurFEA;
  98.     }
  99.  
  100. #ifdef DEBUG
  101.     if (flatCurFEA > flatEnd)
  102.     {
  103.       debugging = TRUE;
  104.       DEBUG_PRINTF3 ("\r\n!!! EaGetList  blkEA.flatAddr = 0x%08lX  flatCurFEA = 0x%08lX  flatEnd = 0x%08lX\r\n",
  105.              pblkEA->flatAddr, flatCurFEA, flatEnd);
  106.       INT3;
  107.     }
  108. #endif
  109.  
  110.     pCurFEA   += cbFEA;
  111.     cbRestFEA -= cbFEA;
  112.     pCurGEA   += cbGEA;
  113.     cbRestGEA -= cbGEA;
  114.   }
  115.  
  116.   pFEAList->cbList -= cbRestFEA;
  117.  
  118.   return NO_ERROR;
  119. }
  120.  
  121.  
  122.  
  123.  
  124. /* static */
  125. int add_ea (PBLOCK pblkEA, PFEA pFEA)
  126. {
  127.   FLAT   flatCurFEA;
  128.   FLAT   flatEnd;
  129.   USHORT cbFEA;
  130.   char   szUpName[256];
  131.   int    rc;
  132.  
  133.   FSH_UPPERCASE ((char *)pFEA + 4, sizeof(szUpName), szUpName);
  134.   flatCurFEA = pblkEA->flatAddr;
  135.   flatEnd    = flatCurFEA + pblkEA->cbSize;
  136.  
  137.   /* scan through all existing EAs for the file */
  138.   while (flatCurFEA < flatEnd)
  139.   {
  140.     USHORT cbCurName;
  141.     USHORT cbCurValue;
  142.     USHORT cbCurFEA;
  143.  
  144.     cbCurName  = VMReadUChar (flatCurFEA+1);
  145.     cbCurValue = VMReadUShort (flatCurFEA+2);
  146.     cbCurFEA   = 5 + cbCurName + cbCurValue;
  147.     if (cbCurName == pFEA->cbName)
  148.     {
  149.       char szCurName[256];
  150.  
  151.       VMRead (szCurName, flatCurFEA+4, cbCurName);
  152.       if (!memcmp (szCurName, szUpName, cbCurName))
  153.       {
  154.     /* the EA already exists, delete it so that it can be replaced */
  155.     VMCopy (flatCurFEA, flatCurFEA+cbCurFEA, flatEnd-flatCurFEA-cbCurFEA);
  156.     rc = BlockRealloc (pblkEA, pblkEA->cbSize - cbCurFEA);
  157.     if (rc)
  158.       return rc;
  159.     break;
  160.       }
  161.     }
  162.     flatCurFEA += cbCurFEA;
  163.   }
  164.  
  165.   if (pFEA->cbValue != 0)
  166.   {
  167.     /* the new EA has a value, so add it to the list */
  168.     cbFEA = 5 + pFEA->cbName + pFEA->cbValue;
  169.     rc = BlockRealloc (pblkEA, pblkEA->cbSize + cbFEA);
  170.     if (rc)
  171.     {
  172.       /* @@@ if the first BlockRealloc succeeded and this one failed, we are
  173.          returning an error code without having restored the state */
  174.       return rc;
  175.     }
  176.  
  177.     /* write the whole FEA with mixed-case name */
  178.     VMWrite (pblkEA->flatAddr + pblkEA->cbSize - cbFEA, pFEA, cbFEA);
  179.     /* replace name with the correct uppercased version */
  180.     VMWrite (pblkEA->flatAddr + pblkEA->cbSize - cbFEA + 4, szUpName, pFEA->cbName);
  181.   }
  182.   return NO_ERROR;
  183. }
  184.  
  185.  
  186.  
  187.  
  188. int EaAddList (PBLOCK pblkEA, PEAOP pEAOP)
  189. {
  190.   UCHAR *pCurFEA;
  191.   USHORT oError;
  192.   USHORT cbRest;
  193.   int    rc;
  194.  
  195.   if (FP_SEG(pEAOP) < 4)
  196.     return NO_ERROR;
  197.  
  198.   pCurFEA = (UCHAR *) pEAOP->fpFEAList;
  199.   cbRest = *(PUSHORT) pCurFEA;
  200.   if (cbRest < 4)
  201.   {
  202.     pEAOP->oError = 0;
  203.     return ERROR_EA_LIST_INCONSISTENT;
  204.   }
  205.  
  206.   pCurFEA += sizeof(ULONG);
  207.   cbRest  -= sizeof(ULONG);
  208.   oError   = sizeof(ULONG);
  209.   while (cbRest > 0)
  210.   {
  211.     USHORT cbName;
  212.     USHORT cbValue;
  213.     USHORT cbFEA;
  214.  
  215.     /* preliminary check of FEA size */
  216.     if (cbRest < 6)
  217.     {
  218.       pEAOP->oError = oError;
  219.       return ERROR_EA_LIST_INCONSISTENT;
  220.     }
  221.  
  222.     /* check for invalid EA flag bits */
  223.     if (pCurFEA[0] & 0x7F)
  224.     {
  225.       pEAOP->oError = oError;
  226.       return ERROR_EA_LIST_INCONSISTENT;
  227.     }
  228.  
  229.     /* check for invalid EA name */
  230.     cbName = pCurFEA[1];
  231.     rc = FSH_CHECKEANAME (1, cbName, &pCurFEA[4]);
  232.     if (rc)
  233.     {
  234.       pEAOP->oError = oError;
  235.       return ERROR_INVALID_EA_NAME;
  236.     }
  237.  
  238.     /* final check of FEA size */
  239.     cbValue = *(USHORT *) &pCurFEA[2];
  240.     cbFEA = 5 + cbName + cbValue;
  241.     if (cbRest < cbFEA)
  242.     {
  243.       pEAOP->oError = oError;
  244.       return ERROR_EA_LIST_INCONSISTENT;
  245.     }
  246.  
  247.     /* this FEA seems OK, try to add it */
  248.     rc = add_ea (pblkEA, (PFEA) pCurFEA);
  249.     if (rc)
  250.     {
  251.       pEAOP->oError = oError;
  252.       return rc;
  253.     }
  254.  
  255.     pCurFEA += cbFEA;
  256.     cbRest  -= cbFEA;
  257.     oError  += cbFEA;
  258.   }
  259.  
  260.   return NO_ERROR;
  261. }
  262.