示例#1
1
文件: RSA.c 项目: adelapie/irma_card
/**
 * PKCS #1 OAEP decoding
 */
static int OAEP_decode(unsigned char *message,
        unsigned int encoded_bytes, const unsigned char *encoded,
        unsigned int label_bytes, const unsigned char *label) {
  unsigned int i, message_bytes;
  unsigned char DB[RSA_MOD_BYTES - RSA_SHA_BYTES - 1 /* Add MGF1 buffer space */ + 4];
  unsigned char seed[RSA_SHA_BYTES /* Add MGF1 buffer space */ + 4];

  debugValue("OAEP decode: encoded message", encoded, encoded_bytes);

  // First byte of encoded message must be 0x00
  if(encoded[0] != 0x00) {
    debugError("First byte of OAEP encoded message is not 0x00");
    return RSA_ERROR_OAEP_DECODE;
  }

  // Extract maskedDB and maskedSeed
  debugValue("OAEP decode: maskedSeed", encoded + 1, RSA_SHA_BYTES);
  Copy(RSA_SHA_BYTES, DB, encoded + 1 + RSA_SHA_BYTES);
  debugValue("OAEP decode: maskedDB", encoded + 1 + RSA_SHA_BYTES, RSA_MOD_BYTES - RSA_SHA_BYTES - 1);

  // Finding seed and DB
  MGF1(RSA_MOD_BYTES - RSA_SHA_BYTES - 1, DB, RSA_SHA_BYTES, seed);
  debugValue("OAEP decode: seedMask", seed, RSA_SHA_BYTES);

  XorAssign(RSA_SHA_BYTES, seed, encoded + 1);
  debugValue("OAEP decode: seed", seed, RSA_SHA_BYTES);

  MGF1(RSA_SHA_BYTES, seed, RSA_MOD_BYTES - RSA_SHA_BYTES - 1, DB);
  debugValue("OAEP decode: dbMask", DB, RSA_MOD_BYTES - RSA_SHA_BYTES - 1);

  XorAssign(RSA_MOD_BYTES - RSA_SHA_BYTES - 1, DB, encoded + 1 + RSA_SHA_BYTES);
  debugValue("OAEP decode: DB", DB, RSA_MOD_BYTES - RSA_SHA_BYTES - 1);

  // Compute the hash of l
  debugValue("OAEP decode: label", label, label_bytes);
  SHA(RSA_SHA_BYTES, seed, label_bytes, label);
  debugValue("OAEP decode: hash of label", seed, RSA_SHA_BYTES);

  // Check whether the first RSA_SHA_BYTES bytes of DB equal to lHash
  if (NotEqual(RSA_SHA_BYTES, seed, DB)) {
    debugError("First RSA_SHA_BYTES of DB do not match with hash of label");
    return RSA_ERROR_OAEP_DECODE;
  }

  // Try to locate the message in DB
  i = RSA_SHA_BYTES;
  while ((DB[i] == 0x00) && (DB[i] != 0x01) && (i < (RSA_MOD_BYTES - RSA_SHA_BYTES - 1 - 1))) i++;

  if ((i == (RSA_MOD_BYTES - RSA_SHA_BYTES - 1 - 1)) || (DB[i] != 0x01)) {
    debugError("Failed to locate the message in DB");
    return RSA_ERROR_OAEP_DECODE;
  }

  // Extract the message, starting after 0x01 byte to the end of DB
  message_bytes = RSA_MOD_BYTES - RSA_SHA_BYTES - 1 - 1 - (i + 1) + 1;
  CopyBytes(message_bytes, message, DB + i + 1);
  debugValue("OAEP decode: recovered message", message, message_bytes);

  return message_bytes;
}
void QgsMssqlGeometryParser::ReadMultiPoint(int iShape)
{
    int iFigure, iPoint, iNextPoint, iCount;
    iFigure = FigureOffset(iShape);
    iNextPoint = NextPointOffset(iFigure);
    iCount = iNextPoint - PointOffset(iFigure);
    if (iCount <= 0)
        return;
    // copy byte order
    CopyBytes( &chByteOrder, 1 );
    // copy type
    int wkbType;
    if ( chProps & SP_HASZVALUES )
        wkbType = QGis::WKBMultiPoint25D;
    else
        wkbType = QGis::WKBMultiPoint;
    CopyBytes( &wkbType, 4 );
    // copy point count
    CopyBytes( &iCount, 4 );
    // copy points
    for (iPoint = PointOffset(iFigure); iPoint < iNextPoint; iPoint++)
    {
        CopyPoint(iShape);
    }
}
示例#3
0
void QgsMssqlGeometryParser::ReadLineString( int iShape )
{
  int iFigure, iPoint, iNextPoint, i, iCount;
  iFigure = FigureOffset( iShape );

  iPoint = PointOffset( iFigure );
  iNextPoint = NextPointOffset( iFigure );
  iCount = iNextPoint - iPoint;
  if ( iCount <= 0 )
    return;
  // copy byte order
  CopyBytes( &chByteOrder, 1 );
  // copy type
  int wkbType;
  if ( chProps & SP_HASZVALUES )
    wkbType = QGis::WKBLineString25D;
  else
    wkbType = QGis::WKBLineString;
  CopyBytes( &wkbType, 4 );
  // copy length
  CopyBytes( &iCount, 4 );
  // copy coordinates
  i = 0;
  while ( iPoint < iNextPoint )
  {
    CopyCoordinates( iPoint );
    ++iPoint;
    ++i;
  }
}
示例#4
0
void QgsMssqlGeometryParser::ReadMultiLineString( int iShape )
{
  int i, iCount;
  iCount = nNumShapes - iShape - 1;
  if ( iCount <= 0 )
    return;
  // copy byte order
  CopyBytes( &chByteOrder, 1 );
  // copy type
  int wkbType;
  if ( chProps & SP_HASZVALUES )
    wkbType = QGis::WKBMultiLineString25D;
  else
    wkbType = QGis::WKBMultiLineString;
  CopyBytes( &wkbType, 4 );
  // copy length
  CopyBytes( &iCount, 4 );
  // copy linestrings
  for ( i = iShape + 1; i < nNumShapes; i++ )
  {
    if ( ParentOffset( i ) == ( unsigned int )iShape )
    {
      if ( ShapeType( i ) == ST_LINESTRING )
        ReadLineString( i );
    }
  }
}
示例#5
0
void QgsMssqlGeometryParser::ReadMultiPolygon( int iShape )
{
  int i;
  int iCount = nNumShapes - iShape - 1;;
  if ( iCount <= 0 )
    return;
  // copy byte order
  CopyBytes( &chByteOrder, 1 );
  // copy type
  int wkbType;
  if ( chProps & SP_HASZVALUES )
    wkbType = QGis::WKBMultiPolygon25D;
  else
    wkbType = QGis::WKBMultiPolygon;
  CopyBytes( &wkbType, 4 );
  // copy poly count
  CopyBytes( &iCount, 4 );
  // copy polygons
  for ( i = iShape + 1; i < nNumShapes; i++ )
  {
    if ( ParentOffset( i ) == ( unsigned int )iShape )
    {
      if ( ShapeType( i ) == ST_POLYGON )
        ReadPolygon( i );
    }
  }
}
示例#6
0
void QgsMssqlGeometryParser::ReadMultiPoint( int iShape )
{
  int i, iCount;
  iCount = nNumShapes - iShape - 1;
  if ( iCount <= 0 )
    return;
  // copy byte order
  CopyBytes( &chByteOrder, 1 );
  // copy type
  int wkbType;
  if ( chProps & SP_HASZVALUES )
    wkbType = QgsWkbTypes::MultiPoint25D;
  else
    wkbType = QgsWkbTypes::MultiPoint;
  CopyBytes( &wkbType, 4 );
  // copy point count
  CopyBytes( &iCount, 4 );
  // copy points
  for ( i = iShape + 1; i < nNumShapes; i++ )
  {
    if ( ParentOffset( i ) == ( unsigned int )iShape )
    {
      if ( ShapeType( i ) == ST_POINT )
        ReadPoint( i );
    }
  }
}
示例#7
0
void FAT32CreateFile(uint8 FileName[11])
{
	uint32 ClusID;
        DIR32 FileDir;
        uint16 FileWrDataTmp,FileWrTimeTmp;                         //双字节数据       
        uint8 FileWrData[2],FileWrTime[2];                          //单字节时间日期数据
        
        FileWrDataTmp=GetFileCrtDate(2012,5,2);                     //文件修改日期2010年7月2日
        FileWrData[0]=(uint8)FileWrDataTmp&0x00ff;                  //低字节提取
        FileWrData[1]=(uint8)(FileWrDataTmp>>8);                    //高字节提取
        
        FileWrTimeTmp=GetFileCrtTime(21,20,10);                     //文件修改时间21点20分10秒
        FileWrTime[0]=(uint8)FileWrTimeTmp&0x00ff;                  //低字节提取
        FileWrTime[1]=(uint8)(FileWrTimeTmp>>8);                    //高字节提取
                
	//ClusNum = Size / (BPB_SecPerClus * 512) + 1;
	EmptyBytes(&FileDir, sizeof(DIR));                          //先全部填充0
        CopyBytes(FileName, &FileDir.FileName, 11);                 //写入文件名
        
        FileDir.FileAttrib = 0x20;                                  //文件属性填充
        
        CopyBytes(FileWrData, &FileDir.DIR_WrtDate, 2);             //写入文件修改日期
        CopyBytes(FileWrTime, &FileDir.DIR_WrtTime, 2);             //写入文件修改时间
        
	//FileDir.FilePosit.Size = 0;
	ClusID = GetNext32FAT();                                    //得到一个空的FAT项,下面用来写对应文件的FAT表
        
	FileDir.DIR_FstClusHI=(uint16)(ClusID>>16);                 //文件开始簇高字
        FileDir.DIR_FstClusLo=(uint16)ClusID&0x0000ffff;            //文件簇开始簇号低字

	Write32FAT(ClusID, 0xffffffff);                             //写对应文件的FAT表1,即FAT1,0x0fffffff表示文件结束
        Write32FAT2(ClusID, 0xffffffff);                            //写对应文件的FAT表2,即FAT2,0x0fffffff表示文件结束
	WriteDIR32(GetEmptyDIR(), &FileDir);                        //写根目录指定项,首先搜索可以使用项,再写入
}
void QgsMssqlGeometryParser::CopyCoordinates( unsigned char* src )
{
    if ( IsGeography )
    {
        CopyBytes( src + 8, 8 ); // longitude
        CopyBytes( src, 8 ); // latitude
    }
    else
        // copy geometry coords
        CopyBytes( src, nPointSize );
}
示例#9
0
void QgsMssqlGeometryParser::CopyPoint( int iPoint )
{
  // copy byte order
  CopyBytes( &chByteOrder, 1 );
  // copy type
  int wkbType;
  if ( chProps & SP_HASZVALUES )
    wkbType = QGis::WKBPoint25D;
  else
    wkbType = QGis::WKBPoint;
  CopyBytes( &wkbType, 4 );
  // copy coordinates
  CopyCoordinates( iPoint );
}
示例#10
0
void QgsMssqlGeometryParser::CopyCoordinates( int iPoint )
{
  if ( IsGeography )
  {
    CopyBytes( pszData + nPointPos + 16 * iPoint + 8, 8 ); // longitude
    CopyBytes( pszData + nPointPos + 16 * iPoint, 8 ); // latitude
  }
  else
    // copy geometry coords
    CopyBytes( pszData + nPointPos + 16 * iPoint, 16 );

  if ( chProps & SP_HASZVALUES )
    CopyBytes( pszData + nPointPos + 16 * nNumPoints + 8 * iPoint, 8 ); // copy z value
}
示例#11
0
void CreateFile(uint8 FileName[11], uint8 FileAttrib)
{
	uint16 ClusID;
        DIR FileDir;
        uint8 DirectoryDIRFileName0[11]=".          ";
        uint8 DirectoryDIRFileName1[11]="..         ";
        
        uint16 FileCrtDateTmp,FileCrtTimeTmp;                 //双字节数据       
        uint8 FileCrtDate[2],FileCrtTime[2];                  //单字节时间日期数据
        
        FileCrtDateTmp=GetFileCrtDate(2012,5,2);              //文件修改日期2010年7月2日
        FileCrtDate[0]=(uint8)FileCrtDateTmp&0x00ff;          //低字节提取
        FileCrtDate[1]=(uint8)(FileCrtDateTmp>>8);            //高字节提取
        
        FileCrtTimeTmp=GetFileCrtTime(21,20,10);              //文件修改时间21点20分10秒
        FileCrtTime[0]=(uint8)FileCrtTimeTmp&0x00ff;          //低字节提取
        FileCrtTime[1]=(uint8)(FileCrtTimeTmp>>8);            //高字节提取
        
        
	//ClusNum = Size / (BPB_SecPerClus * 512) + 1;
	EmptyBytes(&FileDir, sizeof(DIR));                          //先全部填充0
        CopyBytes(FileName, &FileDir.FileName, 11);                 //写入文件名
        
        FileDir.FileAttrib = FileAttrib;                            //文件属性填充,0x10则为文件夹,0x20则为文件
        
        CopyBytes(FileCrtDate, &FileDir.FileCrtDate, 2);            //写入文件创建日期
        CopyBytes(FileCrtTime, &FileDir.FileCrtTime, 2);            //写入文件创建时间
        
	//FileDir.FilePosit.Size = 0;
	FileDir.FilePosit.Start = GetNextFAT();                     //得到一个空的FAT项,下面用来写对应文件的FAT表
        
	ClusID = FileDir.FilePosit.Start;                           //文件簇开始簇号

	WriteFAT(ClusID, 0xffff);                                   //写对应文件的FAT表1,即FAT1,0xffff表示文件结束
        WriteFAT2(ClusID, 0xffff);                                  //写对应文件的FAT表2,即FAT2,0xffff表示文件结束
	WriteDIR(GetEmptyDIR(), &FileDir);                          //写根目录指定项,首先搜索可以使用项,再写入
        
    
  if(FileDir.FileAttrib==0x10)                                      //如果是建立文件夹,则写“.”项和“..项”
     {
        CopyBytes(DirectoryDIRFileName0, &FileDir.FileName, 11);    //写入".项目录内容",该项为自身目录项内容
        WriteDirectoryDIR(ClusID,0,&FileDir);                       //写入,地址为新建目录的起始簇地址的前2个32Bytes,这项为第0个
        
        CopyBytes(DirectoryDIRFileName1, &FileDir.FileName, 11);    //写入"..项目录内容",该项为与父目录关系,起始簇地址为父目录的起始簇地址
        FileDir.FilePosit.Start=0x0000;                             //文件夹位于根目录下,可更改为文件夹的父文件夹起始簇地址
        WriteDirectoryDIR(ClusID,1,&FileDir);                       //写入,地址为新建目录的起始簇地址的前2个32Bytes,这项为第1个
     }
}
示例#12
0
文件: RSA.c 项目: adelapie/irma_card
/**
 * PKCS #1 Mask generation function 1.
 *
 * @param seed_bytes actual size of the seed.
 * @param seed buffer containing the seed and space for 4 additional bytes.
 * @param mask_bytes size of the mask to be generated.
 * @param mask buffer to store the generated mask.
 */
static void MGF1(unsigned int seed_bytes, unsigned char *seed,
        unsigned int mask_bytes, unsigned char *mask) {
  unsigned int i = 0, n = (mask_bytes + RSA_SHA_BYTES - 1) / RSA_SHA_BYTES;
  unsigned char hash[RSA_SHA_BYTES];

  while (i < n - 1 /* exclude the last block */) {
    // Prepare hash data
    Copy(sizeof(unsigned int), seed + seed_bytes + (4 - sizeof(unsigned int)), (unsigned char *) &i);

    // Compute hash
    SHA(RSA_SHA_BYTES, hash, seed_bytes + 4, seed);

	  // Append hash to mask
	  Copy(RSA_SHA_BYTES, mask + i * RSA_SHA_BYTES, hash);

	  // Next block
	  i++;
  }

  // Prepare hash data (for the last block)
  Copy(sizeof(unsigned int), seed + seed_bytes + (4 - sizeof(unsigned int)), (unsigned char *) &i);

  // Compute hash
  SHA(RSA_SHA_BYTES, hash, seed_bytes + 4, seed);

  // Append hash to mask
  CopyBytes(mask_bytes - i * RSA_SHA_BYTES, mask + i * RSA_SHA_BYTES, hash);
}
示例#13
0
文件: ASN1.c 项目: adelapie/irma_card
/**
 * Encode the given number (of length bytes) into an ASN.1 DER object.
 *
 * DER encoding rules standard (ITU-T Rec. X.690 | ISO/IEC 8825-1):
 * http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf
 *
 * @param number the value to be encoded
 * @param length of the value stored in number
 * @param buffer to store the DER object
 * @param offset in front of which the object should be stored
 * @return the offset of the encoded object in the buffer
 */
unsigned int ASN1_encode_int(unsigned int number_bytes, const unsigned char *number,
                    unsigned char *buffer, unsigned int offset) {
  int skip = 0;

  // Determine the number of zero (0x00) bytes to skip
  while(number[skip] == 0x00 && skip < number_bytes - 1) {
    skip++;
  }

  // Store the value
  number_bytes -= skip;
  offset -= number_bytes;
  CopyBytes(number_bytes, buffer + offset, number + skip);

  // If needed, add a 0x00 byte for correct two-complements encoding
  if ((buffer[offset] & 0x80) != 0x00) {
    debugMessage("Correcting value for two-complements encoding");
    buffer[--offset] = 0x00;
    number_bytes++;
  }

  // Store the length
  offset = ASN1_encode_length(number_bytes, buffer, offset);

  // Store the tag
  buffer[--offset] = 0x02; // ASN.1 INTEGER

  return offset;
}
示例#14
0
void WriteDirectoryDIR(uint16 ClusID, uint16 Index, DIR* Value)
{
	uint32 LBA = ClusConvLBA(ClusID);
	ReadBlock(LBA);
	CopyBytes(Value, &BUFFER[(Index % 16) * 32], 32);
	WriteBlock(LBA);
}
示例#15
0
void WriteDirectoryDIR32(uint16 Index, DIR32* Value)
{
	uint32 LBA = DirStartSec() + Index / 16;
	ReadBlock(LBA);
	CopyBytes(Value, &BUFFER[(Index % 16) * 32], 32);
	WriteBlock(LBA);
}
示例#16
0
void QgsMssqlGeometryParser::ReadGeometryCollection( int iShape )
{
  int i;
  int iCount = nNumShapes - iShape - 1;;
  if ( iCount <= 0 )
    return;
  // copy byte order
  CopyBytes( &chByteOrder, 1 );
  // copy type
  int wkbType = QGis::WKBUnknown;;
  CopyBytes( &wkbType, 4 );
  // copy geom count
  CopyBytes( &iCount, 4 );
  for ( i = iShape + 1; i < nNumShapes; i++ )
  {
    if ( ParentOffset( i ) == ( unsigned int )iShape )
    {
      switch ( ShapeType( i ) )
      {
        case ST_POINT:
          ReadPoint( i );
          break;
        case ST_LINESTRING:
          ReadLineString( i );
          break;
        case ST_POLYGON:
          ReadPolygon( i );
          break;
        case ST_MULTIPOINT:
          ReadMultiPoint( i );
          break;
        case ST_MULTILINESTRING:
          ReadMultiLineString( i );
          break;
        case ST_MULTIPOLYGON:
          ReadMultiPolygon( i );
          break;
        case ST_GEOMETRYCOLLECTION:
          ReadGeometryCollection( i );
          break;
      }
    }
  }
}
示例#17
0
void acquire_DataFileMakePath (void)
{
   	char name[300], ext[10];

	Fmt (ext, "%s<%f[p3]", (double)dataFile.ext/1000);
    CopyBytes (ext, 0, ext, 1, 5);
	
    Fmt(name, "%s<%s%s", dataFile.name, ext);
    MakePathname(dataFile.dir, name, dataFile.path);
}
示例#18
0
void QgsMssqlGeometryParser::ReadPolygon( int iShape )
{
  int iFigure, iPoint, iNextPoint, iCount, i;
  int iNextFigure = NextFigureOffset( iShape );
  iCount = iNextFigure - FigureOffset( iShape );
  if ( iCount <= 0 )
    return;
  // copy byte order
  CopyBytes( &chByteOrder, 1 );
  // copy type
  int wkbType;
  if ( chProps & SP_HASZVALUES )
    wkbType = QGis::WKBPolygon25D;
  else
    wkbType = QGis::WKBPolygon;
  CopyBytes( &wkbType, 4 );
  // copy ring count
  CopyBytes( &iCount, 4 );
  // copy rings
  for ( iFigure = FigureOffset( iShape ); iFigure < iNextFigure; iFigure++ )
  {
    iPoint = PointOffset( iFigure );
    iNextPoint = NextPointOffset( iFigure );
    iCount = iNextPoint - iPoint;
    if ( iCount <= 0 )
      continue;
    // copy point count
    CopyBytes( &iCount, 4 );
    // copy coordinates
    i = 0;
    while ( iPoint < iNextPoint )
    {
      CopyCoordinates( iPoint );
      ++iPoint;
      ++i;
    }
  }
}
示例#19
0
/***********************************************************************
函数功能:创建一个子空文件
操作内容:1、文件名必须是数字或者大写字母,小于8位则用空格,扩展名为空格 
          2、文件大小事先不用设置,均为0;
          3、创建子文件的过程实际就是写子目录和FAT表,这里不写文件里的数据
          4、这里文件名为短名,8个字节,也就是8个字符
          5、子文件的目录写在其父目录的起始簇的空白目录项
***********************************************************************/
void CreateDirectoryFile(uint8 DirectoryName[11], uint8 FileName[11])  //父文件夹名和新建文件名,短文件名
{
	uint16 ClusID,ClusIDFile,Index;
        DIR FileDir;
        uint16 FileCrtDateTmp,FileCrtTimeTmp;                 //双字节数据       
        uint8 FileCrtDate[2],FileCrtTime[2];                  //单字节时间日期数据
 
	FileDir.FilePosit.Start = GetNextFAT();               //得到一个空的FAT项,下面用来写对应文件的FAT表       
	ClusID = FileDir.FilePosit.Start;                     //文件簇开始簇号,这是子文件的起始簇地址
        ClusIDFile = FileDir.FilePosit.Start;                 //暂存下来,以备后面写入

	WriteFAT(ClusID, 0xffff);                             //写对应文件的FAT表1,即FAT1,0xffff表示文件结束
        WriteFAT2(ClusID, 0xffff);                            //写对应文件的FAT表2,即FAT2,0xffff表示文件结束     
                             
        Index = GetFileID(DirectoryName,&FileDir);            //搜索得到文件夹的属性,主要是获得起始簇位置,方便写文件的目录项
        ReadDIR(Index, &FileDir);                             //读取父文件夹的目录项,只需要其起始簇
        
        FileCrtDateTmp=GetFileCrtDate(2010,7,2);              //文件修改日期2010年7月2日
        FileCrtDate[0]=(uint8)FileCrtDateTmp&0x00ff;          //低字节提取
        FileCrtDate[1]=(uint8)(FileCrtDateTmp>>8);            //高字节提取       
        FileCrtTimeTmp=GetFileCrtTime(21,20,10);              //文件修改时间21点20分10秒
        FileCrtTime[0]=(uint8)FileCrtTimeTmp&0x00ff;          //低字节提取
        FileCrtTime[1]=(uint8)(FileCrtTimeTmp>>8);            //高字节提取      
        
        CopyBytes(FileName, &FileDir.FileName, 11);           //写入文件名
        
        CopyBytes(FileCrtDate, &FileDir.FileCrtDate, 2);       //写入文件创建日期
        CopyBytes(FileCrtTime, &FileDir.FileCrtTime, 2);       //写入文件创建时间
        
        FileDir.FileAttrib = 0x20;                             //文件属性填充,0x10为子目录属性,即文件夹       
       
        ClusID = FileDir.FilePosit.Start;                      //父文件夹的开始簇号,子文件目录项写入这个簇中
        FileDir.FilePosit.Start=ClusIDFile;                    //新建文件的起始簇
        WriteDirectoryDIR(ClusID,GetDirectoryEmptyDIR(ClusID),&FileDir);//需要扫描父目录的起始簇中空白可以写入子目录的项
        
}
示例#20
0
文件: libmain.c 项目: mingpen/OpenNT
//*-----------------------------------------------------------------------
//| CpBlock
//|
//| PURPOSE:    Copy a block of information (possibly > 64K) from one
//|             file to another.  Copies blocks larger than 64K by copying
//|             64K chunks at a time.  (assuming MaxSize == 64K)
//|
//| ENTRY:      fd1     - Handle of source file
//|             pos1    - LFO of source block
//|             fd2     - Handle of destination file
//|             pos2    - LFO of destination block
//|             tLfo    - Number of bytes to copy
//|
//| EXIT:       0 if successful
//*-----------------------------------------------------------------------
INT PRIVATE CpBlock(FD fd1, LFO pos1, FD fd2, LFO pos2, LFO tLfo)
{
        WORD    cb;
        INT     i = 0, j = 0;

        // Determine number of MaxSize blocks we need to copy
        //-----------------------------------------------------------
        i = (INT)(tLfo / MaxSize);
        i++;

        // We can only copy <= 64K bytes at once.  See if block is
        // bigger than MaxSize divide the data up to smller blocks if
        // necessary move a block of bytes upward
        //
        // CONSIDER: It may be a miracle that this code works.  We
        // CONSIDER: should probably take a serious look at this and
        // CONSIDER: make sure it's doing what it is supposed to do,
        // CONSIDER: the way that it says it is doing it.
        //-----------------------------------------------------------
        do
            {
                cb = (WORD)(tLfo / i);
                if (cb != 0)
                        j = CopyBytes(fd1, pos1, fd2, pos2, cb);

                // Subtract bytes written from total
                //---------------------------------------------------
                tLfo -= (LFO)cb;

                // If we are reading and writing from the same file,
                // we need to update the two LFO's - otherwise, we
                // can set them both to 0, which will continue
                // reading from the current location of each file.
                //---------------------------------------------------
                if ( fd1 == fd2 )
                    {
                        pos1 += (LFO)cb;
                        pos2 += (LFO)cb;
                    }
                else
                        pos1 = pos2 = 0;

                i--;
            }
        while ( (i > 0) && (j == 0) );

        return (j);
}
示例#21
0
/*
   save a directory entry to disk, without a PWAD header
*/
void SaveEntryToRawFile( FILE *file, char *entryname)
{
   MDirPtr entry;

   for (entry = MasterDir; entry; entry = entry->next)
      if (! strncmp( entry->dir.name, entryname, 8))
	 break;
   if (entry)
   {
      BasicWadSeek( entry->wadfile, entry->dir.start);
      CopyBytes( file, entry->wadfile->fileinfo, entry->dir.size);
   }
   else
   {
      printf( "[Entry not in master directory.]\n");
      return;
   }
}
示例#22
0
void Trimmer::CopyLPCSubframe(BitIStream& bis, BitOStream& bos, FLACFrameHeader * fh, FLACMetaStreamInfo * msi, FLACSubframeHeader * sfh)
{
	uint8_t qlp = 0; // quantized linear predictor
	uint8_t qlpCoeffShift = 0;
	uint16_t unencQLPCoeffsBitSize = 0;
	if (GetWarmUpSamplesBitSize(fh, msi, sfh) % BITSINBYTE != 0) cerr << "GetWarmUpSamplesBitSize(fh, msi) % BITSINBYTE != 0" << endl;
	CopyBytes(bis, bos, GetWarmUpSamplesBitSize(fh, msi, sfh) / BITSINBYTE);
	bis.ReadInteger(&qlp, 4);
	bos.WriteInteger(qlp, 4);
	bis.ReadInteger(&qlpCoeffShift, 5);
	bos.WriteInteger(qlpCoeffShift, 5);

	unencQLPCoeffsBitSize = (qlp + 1) * GetPredictorOrder(sfh);

	if (unencQLPCoeffsBitSize != 0) CopyBits(bis, bos, unencQLPCoeffsBitSize);

	CopyResidual(bis, bos, fh, msi, sfh);
}
示例#23
0
/*----------------------------------------------------------------------------------------------
	Read a specified number of bytes from this stream into memory, starting at the current seek
	pointer.
----------------------------------------------------------------------------------------------*/
STDMETHODIMP ResourceStream::Read(void * pv, UCOMINT32 cb, UCOMINT32 * pcbRead)
{
	BEGIN_COM_METHOD;
	ChkComArrayArg((byte *)pv, cb);
	ChkComArgPtrN(pcbRead);
	UCOMINT32 cbRead;

	//Avoid reading past end of resource data.
	if (m_pbCur + cb > m_prgbData + m_cbData)
		cbRead = m_cbData - (m_pbCur - m_prgbData);
	else
		cbRead = cb;
	if (cbRead != 0)
		CopyBytes(m_pbCur, pv, cbRead);
	m_pbCur += cbRead;
	if (pcbRead)
		*pcbRead = cbRead;

	END_COM_METHOD(g_fact, IID_IStream);
}
示例#24
0
文件: RSA.c 项目: adelapie/irma_card
/**
 * PKCS #1 OAEP encoding
 */
static int OAEP_encode(unsigned char *encoded,
        unsigned int message_bytes, const unsigned char *message,
        unsigned int label_bytes, const unsigned char *label) {
  unsigned char DB[RSA_MOD_BYTES - RSA_SHA_BYTES - 1 /* Add MGF1 buffer space */ + 4];
  unsigned char seed[RSA_SHA_BYTES /* Add MGF1 buffer space */ + 4];

  // Construct DB
  debugValue("OAEP encode: label", label, label_bytes);
  SHA(RSA_SHA_BYTES, DB, label_bytes, label);
  debugValue("OAEP encode: hash of label", DB, RSA_SHA_BYTES);
  DB[RSA_MOD_BYTES - RSA_SHA_BYTES - message_bytes - 2] = 0x01;
  debugValue("OAEP encode: message", message, message_bytes);
  CopyBytes(message_bytes, DB + RSA_MOD_BYTES - RSA_SHA_BYTES - message_bytes - 1, message);
  debugValue("OAEP encode: DB", DB, RSA_MOD_BYTES - RSA_SHA_BYTES - 1);

  // Make a random seed
  RandomBytes(seed, RSA_SHA_BYTES);
  debugValue("OAEP encode: seed", seed, RSA_SHA_BYTES);

  // Construct maskedDB and maskedSeed
  MGF1(RSA_SHA_BYTES, seed, RSA_MOD_BYTES - RSA_SHA_BYTES - 1, encoded + 1 + RSA_SHA_BYTES);
  debugValue("OAEP encode: dbMask", encoded + 1 + RSA_SHA_BYTES, RSA_MOD_BYTES - RSA_SHA_BYTES - 1);

  XorAssign(RSA_MOD_BYTES - RSA_SHA_BYTES - 1, DB, encoded + 1 + RSA_SHA_BYTES);
  debugValue("OAEP encode: maskedDB", DB, RSA_MOD_BYTES - RSA_SHA_BYTES - 1);

  MGF1(RSA_MOD_BYTES - RSA_SHA_BYTES - 1, DB, RSA_SHA_BYTES, encoded + 1);
  debugValue("OAEP encode: seedMask", encoded + 1, RSA_SHA_BYTES);

  XorAssign(RSA_SHA_BYTES, seed, encoded + 1);
  debugValue("OAEP encode: maskedSeed", encoded + 1, RSA_SHA_BYTES);

  Copy(RSA_SHA_BYTES, encoded + 1, seed);
  Copy(RSA_MOD_BYTES - RSA_SHA_BYTES - 1, encoded + 1 + RSA_SHA_BYTES, DB);
  debugValue("OAEP encode: encoded message", encoded, RSA_MOD_BYTES);

  return RSA_MOD_BYTES;
}
示例#25
0
文件: CHV.c 项目: adelapie/irma_card
/**
 * Modify a PIN code
 *
 * @param buffer which contains the old and new code
 */
int CHV_PIN_update(CHV_PIN *pin, unsigned int length, unsigned char *buffer) {
  int i;

  if (length == 2*CHV_PIN_SIZE) {
    // Verify the original PIN
    i = CHV_PIN_verify(pin, CHV_PIN_SIZE, buffer);
    if (i <= 0) {
      return i;
    }
    buffer += CHV_PIN_SIZE;
  }

  // Verify the new PIN size
  for (i = 0; i < pin->minSize; i++) {
    if (buffer[i] == 0x00) {
      return CHV_WRONG_LENGTH;
    }
  }

  // Store the new code
  CopyBytes(CHV_PIN_SIZE, pin->code, buffer);
  pin->count = CHV_PIN_COUNT;
  return CHV_VALID;
}
示例#26
0
unsigned char* QgsMssqlGeometryParser::ParseSqlGeometry( unsigned char* pszInput, int nLen )
{
  if ( nLen < 10 )
  {
    QgsDebugMsg( "ParseSqlGeometry not enough data" );
    DumpMemoryToLog( "Not enough data", pszInput, nLen );
    return NULL;
  }

  pszData = pszInput;
  nWkbMaxLen = nLen;

  /* store the SRS id for further use */
  nSRSId = ReadInt32( 0 );

  if ( ReadByte( 4 ) != 1 )
  {
    QgsDebugMsg( "ParseSqlGeometry corrupt data" );
    DumpMemoryToLog( "Corrupt data", pszInput, nLen );
    return NULL;
  }

  chProps = ReadByte( 5 );

  if ( chProps & SP_HASZVALUES && chProps & SP_HASMVALUES )
    nPointSize = 32;
  else if ( chProps & SP_HASZVALUES || chProps & SP_HASMVALUES )
    nPointSize = 24;
  else
    nPointSize = 16;

  /* store byte order */
  chByteOrder = QgsApplication::endian();

  pszWkb = new unsigned char[nLen]; // wkb should be less or equal in size
  nWkbLen = 0;

  if ( chProps & SP_ISSINGLEPOINT )
  {
    // single point geometry
    nNumPoints = 1;
    nPointPos = 6;

    if ( nLen < 6 + nPointSize )
    {
      delete [] pszWkb;
      QgsDebugMsg( "ParseSqlGeometry not enough data" );
      DumpMemoryToLog( "Not enough data", pszInput, nLen );
      return NULL;
    }

    CopyPoint( 0 );
  }
  else if ( chProps & SP_ISSINGLELINESEGMENT )
  {
    // single line segment with 2 points
    nNumPoints = 2;
    nPointPos = 6;

    if ( nLen < 6 + 2 * nPointSize )
    {
      delete [] pszWkb;
      QgsDebugMsg( "ParseSqlGeometry not enough data" );
      DumpMemoryToLog( "Not enough data", pszInput, nLen );
      return NULL;
    }

    // copy byte order
    CopyBytes( &chByteOrder, 1 );
    // copy type
    int wkbType;
    if ( chProps & SP_HASZVALUES )
      wkbType = QGis::WKBLineString25D;
    else
      wkbType = QGis::WKBLineString;
    CopyBytes( &wkbType, 4 );
    // copy point count
    int iCount = 2;
    CopyBytes( &iCount, 4 );
    // copy points
    CopyCoordinates( 0 );
    CopyCoordinates( 1 );
  }
  else
  {
    // complex geometries
    nNumPoints = ReadInt32( 6 );

    if ( nNumPoints <= 0 )
    {
      delete [] pszWkb;
      return NULL;
    }

    // position of the point array
    nPointPos = 10;

    // position of the figures
    nFigurePos = nPointPos + nPointSize * nNumPoints + 4;

    if ( nLen < nFigurePos )
    {
      delete [] pszWkb;
      QgsDebugMsg( "ParseSqlGeometry not enough data" );
      DumpMemoryToLog( "Not enough data", pszInput, nLen );
      return NULL;
    }

    nNumFigures = ReadInt32( nFigurePos - 4 );

    if ( nNumFigures <= 0 )
    {
      delete [] pszWkb;
      return NULL;
    }

    // position of the shapes
    nShapePos = nFigurePos + 5 * nNumFigures + 4;

    if ( nLen < nShapePos )
    {
      delete [] pszWkb;
      QgsDebugMsg( "ParseSqlGeometry not enough data" );
      DumpMemoryToLog( "Not enough data", pszInput, nLen );
      return NULL;
    }

    nNumShapes = ReadInt32( nShapePos - 4 );

    if ( nLen < nShapePos + 9 * nNumShapes )
    {
      delete [] pszWkb;
      QgsDebugMsg( "ParseSqlGeometry not enough data" );
      DumpMemoryToLog( "Not enough data", pszInput, nLen );
      return NULL;
    }

    if ( nNumShapes <= 0 )
    {
      delete [] pszWkb;
      return NULL;
    }

    // pick up the root shape
    if ( ParentOffset( 0 ) != 0xFFFFFFFF )
    {
      delete [] pszWkb;
      QgsDebugMsg( "ParseSqlGeometry corrupt data" );
      DumpMemoryToLog( "Not enough data", pszInput, nLen );
      return NULL;
    }

    // determine the shape type
    switch ( ShapeType( 0 ) )
    {
      case ST_POINT:
        ReadPoint( 0 );
        break;
      case ST_LINESTRING:
        ReadLineString( 0 );
        break;
      case ST_POLYGON:
        ReadPolygon( 0 );
        break;
      case ST_MULTIPOINT:
        ReadMultiPoint( 0 );
        break;
      case ST_MULTILINESTRING:
        ReadMultiLineString( 0 );
        break;
      case ST_MULTIPOLYGON:
        ReadMultiPolygon( 0 );
        break;
        //case ST_GEOMETRYCOLLECTION:
        //ReadGeometryCollection(0);
        //break;
      default:
        delete [] pszWkb;
        QgsDebugMsg( "ParseSqlGeometry unsupported geometry type" );
        DumpMemoryToLog( "Unsupported geometry type", pszInput, nLen );
        return NULL;
    }
  }

  return pszWkb;
}
示例#27
0
void Trimmer::CutTrack(string outputFLACFile, unsigned int leftSecond, unsigned int rightSecond)
{
   /* if (bs.GetString(3) == "ID3")
    {
    	bs.Skip(3);
    	uint32_t id3size = 0;
    	uint32_t b1 = 0, b2 = 0, b3 = 0, b4 = 0;
    	bs.GetInteger(&b1, 8);
    	bs.GetInteger(&b2, 8);
    	bs.GetInteger(&b3, 8);
    	bs.GetInteger(&b4, 8);
    	id3size = (b1 << 21) | (b2 << 14) | (b3 << 7) | b4;
    	bs.Skip(id3size);
    }*/

	BitIStream bis(fileName);
	BitOStream bos(outputFLACFile);
	FLACMetaStreamInfo msi;

	if (bis.ReadString(4) != "fLaC")
	{
		throw NoFLACFile();
	}
	bos.WriteString("fLaC");
			
	FLACMetaBlockHeader mbh;
	mbh.IsLastBlock = false;
			
	while (!mbh.IsLastBlock)
	{
		ReadMetaBlockHeader(bis, &mbh);
		WriteMetaBlockHeader(bos, &mbh);
		switch (mbh.BlockType)
		{
			case FLAC_META_STREAMINFO:
				ReadStreamInfo(bis, &msi);
				WriteStreamInfo(bos, &msi);
			break;

			case FLAC_META_SEEKTABLE:
			case FLAC_META_APPLICATION:
			case FLAC_META_VORBIS_COMMENT:
			case FLAC_META_CUESHEET:
			case FLAC_META_PADDING:
				CopyBytes(bis, bos, mbh.BlockSize);
			break;

			default:
				cerr << "Warning: unknown block type" << endl;
				CopyBytes(bis, bos, mbh.BlockSize);
			break;
		}
	}
	for (int i = 0; i < 10; i++)
	{

		//Frame start
		FLACFrameHeader fh;
		ReadFrameHeader(bis, &fh);
		WriteFrameHeader(bos, &fh);
		if (fh.SyncCode != 0b11111111111110) 
		{
			cout << "Invalid sync code" << endl;
			throw NoFLACFile();
		}
		for (int subframeN = 0; subframeN <= msi.ChannelsCount; subframeN++)
		{
			FLACSubframeHeader sfh;
			ReadSubframeHeader(bis, &sfh);
			WriteSubframeHeader(bos, &sfh);
			switch (sfh.Type)
			{
				case FLAC_SF_CONSTANT:	
					cout << "const" << endl;
					CopyConstantSubframe(bis, bos, &fh, &msi);
				break;
				case FLAC_SF_VERBATIM:
					cout << "verbatim" << endl;
					CopyVerbatimSubframe(bis, bos, &fh, &msi);
				break;
				case FLAC_SF_FIXED:
					cout << "fixed" << endl;
					CopyFixedSubframe(bis, bos, &fh, &msi, &sfh);
				break;
				case FLAC_SF_LPC:
					cout << "lpc" << endl;
					CopyLPCSubframe(bis, bos, &fh, &msi, &sfh);
				break;
				default:
				break;
			}
		}
		bos.AlignByte();
		uint16_t crc16;
		bis.ReadInteger(&crc16, 16);
		bos.WriteInteger(crc16, 16);

	/*ReadFrameHeader(bis, &fh);
	cout << fh.SyncCode << endl;*/
		//Frame end

	}
		
}
示例#28
0
void Trimmer::CopyVerbatimSubframe(BitIStream& bis, BitOStream& bos, FLACFrameHeader * fh, FLACMetaStreamInfo * msi)
{
	if (GetUnencSubblockBitSize(fh, msi) % BITSINBYTE != 0) cerr << "GetUnencSubblockBitSize(fh, msi) % BITSINBYTE != 0" << endl;
	CopyBytes(bis, bos, GetUnencSubblockBitSize(fh, msi) / BITSINBYTE);
}
示例#29
0
void ReadDIR(uint16 Index, DIR* Value)
{
	uint32 DirStart = DirStartSec();
	ReadBlock(DirStart + Index / 16);
	CopyBytes(&BUFFER[(Index % 16) * 32], Value, 32);
}
示例#30
0
文件: SM.c 项目: adelapie/irma_card
/**
 * Unwrap a command APDU from secure messaging
 */
int SM_APDU_unwrap(unsigned char *apdu, unsigned char *buffer, SM_parameters *params) {
  unsigned char mac[SM_MAC_BYTES];
  int i;
  unsigned int offset = 0;
  unsigned int do87DataBytes = 0;
  unsigned int do87Data = 0;

  IncrementBytes(SM_SSC_BYTES, params->ssc);

  if (apdu[offset] == 0x87) { // do87
    if (apdu[++offset] > 0x80) {
      do87Data = apdu[offset++] & 0x7f;
    } else {
      do87Data = 1;
    }

    for (i = 0; i < do87Data; i++) {
      do87DataBytes += apdu[offset + i] << (do87Data - 1 - i) * 8;
    }
    offset += do87Data;

    if (apdu[offset++] != 0x01) {
      return SM_ERROR_WRONG_DATA;
    }
    do87DataBytes--; // compensate for 0x01 marker

    // store pointer to data and defer decrypt to after mac check (do8e)
    do87Data = offset;
    offset += do87DataBytes;
  }

  if (apdu[offset] == 0x97) { // do97
    if (apdu[++offset] != 0x01) {
      return SM_ERROR_WRONG_DATA;
    }
    Le = apdu[++offset];
    offset++;
  }

  // do8e
  if (apdu[offset] != 0x8e || apdu[offset + 1] != 8) {
    return SM_ERROR_WRONG_DATA;
  }

  // verify mac
  i = 0;

  // SSC
  Copy(SM_SSC_BYTES, buffer, params->ssc);
  i += SM_SSC_BYTES;

  // Header
  buffer[i++] = CLA;
  buffer[i++] = INS;
  buffer[i++] = P1;
  buffer[i++] = P2;

  // Padding
  i = SM_ISO7816_4_pad(apdu, i);

  // Cryptogram (do87 and do97)
  CopyBytes(offset, buffer + i, apdu);
  do87Data += i;
  i += offset;

  // Padding
  i = SM_ISO7816_4_pad(buffer, i);

  // Verify the MAC
  SM_CBC_sign(i, buffer, mac, SM_KEY_BYTES, params->key_mac, SM_IV);
  if (NotEqual(SM_MAC_BYTES, mac, apdu + offset + 2)) {
    return SM_ERROR_MAC_INVALID;
  }

  // Decrypt data if available
  if (do87DataBytes != 0) {
    SM_CBC_decrypt(do87DataBytes, buffer + do87Data, apdu, SM_IV, SM_KEY_BYTES, params->key_enc);
    Lc = do87DataBytes;
    if (SM_ISO7816_4_unpad(apdu, &Lc) < 0) {
      return SM_ERROR_PADDING_INVALID;
    } else {
      return Lc;
    }
  }
}