Example #1
0
/**
 * \brief encoding curSymbol and save the results in encodeOutcome
 * \param curSymbol (byte) current encoding symbol
 * \param encodeOutcome (unsigned*) encoding results
 */
void encodeOneSymbol(byte curSymbol, unsigned *encodeOutcome)
{
    //computing current range
    unsigned range = high - low + 1;
    //computing high and low according to current symbol
    if(curSymbol)
        low = low + (unsigned)(range * (1 - pr) + 0.5);
    else
        high = low + (unsigned)(range * (1 - pr) + 0.5) - 1;
    //update range
    while(1)
    {
        if(!(FHIGH(high) ^ FHIGH(low)))    //condition 1 and 2
        {
            byte bit = FHIGH(high) >> (BIT - 1) & 1;
            PUTBIT(encodeOutcome, enCounter, bit);
            enCounter++;
            //condition 3 meets with condition 1
            while(neglectedSecondhighBit)
            {
                PUTBIT(encodeOutcome, enCounter, !bit);
                enCounter++;
                neglectedSecondhighBit--;
            }
        }
        else if((!SHIGH(high)) && SHIGH(low))  //condition 3
Example #2
0
int
main(int argc, char * argv[]) {

    FILE* ifp;
    bit* bitrow;
    int rows, cols, format;
    int padright;
    int row;
    const char * inputFilename;
    const char *name;
    int itemsperline;
    int bitsperitem;
    int item;
    int firstitem;
    const char hexchar[] = "0123456789abcdef";

    pbm_init(&argc, argv);

    if (argc-1 > 1)
        pm_error("Too many arguments (%d).  The only valid argument is an "
                 "input file name.", argc-1);
    else if (argc-1 == 1) 
        inputFilename = argv[1];
    else
        inputFilename = "-";

    generateName(inputFilename, &name);
    ifp = pm_openr(inputFilename);
    
    pbm_readpbminit(ifp, &cols, &rows, &format);
    bitrow = pbm_allocrow(cols);
    
    /* Compute padding to round cols up to the nearest multiple of 8. */
    overflow_add(cols, 8);
    padright = ((cols + 7)/8) * 8 - cols;

    printf("#define %s_width %d\n", name, cols);
    printf("#define %s_height %d\n", name, rows);
    printf("static char %s_bits[] = {\n", name);

    itemsperline = 0;
    bitsperitem = 0;
    item = 0;
    firstitem = 1;

#define PUTITEM \
    { \
    if ( firstitem ) \
        firstitem = 0; \
    else \
        putchar( ',' ); \
    if ( itemsperline == 15 ) \
        { \
        putchar( '\n' ); \
        itemsperline = 0; \
        } \
    if ( itemsperline == 0 ) \
        putchar( ' ' ); \
    ++itemsperline; \
    putchar('0'); \
    putchar('x'); \
    putchar(hexchar[item >> 4]); \
    putchar(hexchar[item & 15]); \
    bitsperitem = 0; \
    item = 0; \
    }

#define PUTBIT(b) \
    { \
    if ( bitsperitem == 8 ) \
        PUTITEM; \
    if ( (b) == PBM_BLACK ) \
        item += 1 << bitsperitem; \
    ++bitsperitem; \
    }

    for (row = 0; row < rows; ++row) {
        int col;
        pbm_readpbmrow(ifp, bitrow, cols, format);
        for (col = 0; col < cols; ++col)
            PUTBIT(bitrow[col]);
        for (col = 0; col < padright; ++col)
            PUTBIT(0);
    }

    pm_close(ifp);

    if (bitsperitem > 0)
        PUTITEM;
    printf("};\n");

    pbm_freerow(bitrow);

    strfree(name);

    exit(0);
}
Example #3
0
/*-
 ***********************************************************************
 *
 * MaskParseMask
 *
 ***********************************************************************
 */
MASK_USS_MASK *
MaskParseMask(char *pcMask, int iType, char *pcError)
{
  const char          acRoutine[] = "MaskParseMask()";
  char                acLocalError[MESSAGE_SIZE] = { 0 };
  char                cLastAction = 0;
  char                cNextAction = 0;
  char               *pcTemp = NULL;
  char               *pcTempLine = NULL;
  char               *pcToken = NULL;
  int                 i = 0;
  int                 j = 0;
  int                 iDone = 0;
  int                 iError = 0;
  int                 iLength = strlen(pcMask);
  int                 iMaskTableLength = MaskGetTableLength(iType);
  int                 iOffset = 0;
  MASK_B2S_TABLE     *pasMaskTable = MaskGetTableReference(iType);
  MASK_USS_MASK      *psMask = NULL;
  unsigned long       ulAllMask = 0;

  /*-
   *********************************************************************
   *
   * Sanity check the type and intialize variables.
   *
   *********************************************************************
   */
  switch (iType)
  {
  case MASK_RUNMODE_TYPE_CMP:
    ulAllMask = CMP_ALL_MASK;
    break;
  case MASK_RUNMODE_TYPE_DIG:
    ulAllMask = DIG_ALL_MASK;
    break;
  case MASK_RUNMODE_TYPE_MAP:
    ulAllMask = MAP_ALL_MASK;
    break;
  default:
    snprintf(pcError, MESSAGE_SIZE, "%s: Invalid type [%d]. That shouldn't happen.", acRoutine, iType);
    return NULL;
    break;
  }

  /*-
   *********************************************************************
   *
   * Allocate and initialize memory for the mask.
   *
   *********************************************************************
   */
  psMask = MaskNewMask(acLocalError);
  if (psMask == NULL)
  {
    snprintf(pcError, MESSAGE_SIZE, "%s: %s", acRoutine, acLocalError);
    return NULL;
  }

  /*-
   *********************************************************************
   *
   * Set the user-supplied string mask.
   *
   *********************************************************************
   */
  iError = MaskSetMask(psMask, pcMask, acLocalError);
  if (iError != ER_OK)
  {
    snprintf(pcError, MESSAGE_SIZE, "%s: %s", acRoutine, acLocalError);
    MaskFreeMask(psMask);
    return NULL;
  }

  /*-
   *********************************************************************
   *
   * Check that the mask begins with a valid anchor.
   *
   *********************************************************************
   */
  if (strncasecmp(pcMask, "all", 3) == 0)
  {
    psMask->ulMask = ~0;
    iLength = 3;
  }
  else if (strncasecmp(pcMask, "none", 4) == 0)
  {
    psMask->ulMask = 0;
    iLength = 4;
  }
  else
  {
    snprintf(pcError, MESSAGE_SIZE, "%s: Prefix = [%s] != [all|none]: Invalid prefix.", acRoutine, pcMask);
    MaskFreeMask(psMask);
    return NULL;
  }

  /*-
   *********************************************************************
   *
   * Continue parsing the mask. If there are no more fields, we're done.
   *
   *********************************************************************
   */
  switch (pcMask[iLength])
  {
  case '+':
  case '-':
    cLastAction = '?';
    cNextAction = pcMask[iLength++];
    break;
  case 0:
    psMask->ulMask &= ulAllMask;
    if (iType == MASK_RUNMODE_TYPE_CMP && psMask->ulMask == 0)
    {
      snprintf(pcError, MESSAGE_SIZE, "%s: CompareMask = [%s]: Mask must include at least one field (e.g., none+md5).", acRoutine, psMask->pcMask);
      MaskFreeMask(psMask);
      return NULL;
    }
    return psMask;
    break;
  default:
    snprintf(pcError, MESSAGE_SIZE, "%s: Operator = [%c] != [+|-]: Invalid operator.", acRoutine, pcMask[iLength]);
    MaskFreeMask(psMask);
    return NULL;
    break;
  }

  /*-
   *********************************************************************
   *
   * Copy the remainder of the input to a scratch pad.
   *
   *********************************************************************
   */
  pcTemp = &pcMask[iLength];
  iLength = strlen(pcTemp); /* Calculate new length. */
  pcTempLine = calloc(iLength + 1, 1);
  if (pcTempLine == NULL)
  {
    snprintf(pcError, MESSAGE_SIZE, "%s: calloc(): %s", acRoutine, strerror(errno));
    MaskFreeMask(psMask);
    return NULL;
  }
  strncpy(pcTempLine, pcTemp, iLength + 1);

  /*-
   *********************************************************************
   *
   * Remove EOL characters.
   *
   *********************************************************************
   */
  SupportChopEOLs(pcTempLine, 0, NULL);

  /*-
   *********************************************************************
   *
   * Scan through the string looking for tokens delimited by '+', '-',
   * or 0.
   *
   *********************************************************************
   */
  for (pcToken = pcTempLine, iOffset = 0, iDone = 0; !iDone;)
  {
    if (pcTempLine[iOffset] == '+' || pcTempLine[iOffset] == '-' || pcTempLine[iOffset] == 0)
    {
      if (pcTempLine[iOffset] == 0)
      {
        iDone = 1;
      }

      /*-
       *****************************************************************
       *
       * Update the action values.
       *
       *****************************************************************
       */
      cLastAction = cNextAction;
      cNextAction = pcTempLine[iOffset];

      /*-
       *****************************************************************
       *
       * Terminate the token.
       *
       *****************************************************************
       */
      pcTempLine[iOffset] = 0;

      /*-
       *****************************************************************
       *
       * Scan the table looking for this token. Add or subtract the
       * expanded token value (i.e., the time tokens count for more
       * than one mask bit each) from the mask depending on whether
       * '+' or '-' was given.
       *
       *****************************************************************
       */
      for (i = 0; i < iMaskTableLength; i++)
      {
        if (strcasecmp(pcToken, pasMaskTable[i].acName) == 0 && pasMaskTable[i].iBits2Set > 0)
        {
          for (j = 0; j < pasMaskTable[i].iBits2Set; j++)
          {
            PUTBIT(psMask->ulMask, ((cLastAction == '+') ? 1 : 0), (i + j));
          }
          break;
        }
      }
      if (i == iMaskTableLength)
      {
        /*-
         ***************************************************************
         *
         * Check to see if this is a group field before aborting.
         *
         ***************************************************************
         */
        if (strcasecmp(pcToken, "hashes") == 0 && (iType == MASK_RUNMODE_TYPE_CMP || iType == MASK_RUNMODE_TYPE_MAP))
        {
          if (cLastAction == '+')
          {
            psMask->ulMask |= (iType == MASK_RUNMODE_TYPE_CMP) ? CMP_HASHES_MASK : MAP_HASHES_MASK;
          }
          else
          {
            psMask->ulMask &= (iType == MASK_RUNMODE_TYPE_CMP) ? ~CMP_HASHES_MASK : ~MAP_HASHES_MASK;
          }
        }
        else if (strcasecmp(pcToken, "times") == 0 && (iType == MASK_RUNMODE_TYPE_CMP || iType == MASK_RUNMODE_TYPE_MAP))
        {
          if (cLastAction == '+')
          {
            psMask->ulMask |= (iType == MASK_RUNMODE_TYPE_CMP) ? CMP_TIMES_MASK : MAP_TIMES_MASK;
          }
          else
          {
            psMask->ulMask &= (iType == MASK_RUNMODE_TYPE_CMP) ? ~CMP_TIMES_MASK : ~MAP_TIMES_MASK;
          }
        }
        else
        {
          snprintf(pcError, MESSAGE_SIZE, "%s: Token = [%c%s]: Invalid value.", acRoutine, cLastAction, pcToken);
          MaskFreeMask(psMask);
          return NULL;
        }
      }
      if (!iDone)
      {
        iOffset++;
        pcToken = &pcTempLine[iOffset];
      }
    }
    else
    {
      iOffset++;
    }
  }

  /*-
   *********************************************************************
   *
   * Remove any extra bits, and send it back to the caller.
   *
   *********************************************************************
   */
  psMask->ulMask &= ulAllMask;

  return psMask;
}