Home > Back-end >  Which convert the following C code to Delphi
Which convert the following C code to Delphi

Time:10-11

/* 
* unyaffs: extract files from yaffs2 file system image to the current directory
*
* Created by Kai Wei & lt; [email protected] & gt;
* This program is free software; You can redistribute it and/or modify the
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/

#include
#include
#include
#include
#include
#include
#include

# include "unyaffs. H"

# define CHUNK_SIZE 2048
# define SPARE_SIZE 64
# define MAX_OBJECTS 10000
# define YAFFS_OBJECTID_ROOT 1


Unsigned char data [CHUNK_SIZE + SPARE_SIZE];
Unsigned char * chunk_data=https://bbs.csdn.net/topics/data;
Unsigned char * spare_data=https://bbs.csdn.net/topics/data + CHUNK_SIZE;
Int img_file;

Char * obj_list [MAX_OBJECTS];
Int process_chunk ()
{
Int out_file, remain, s;
Char * full_path_name;

Spare_data yaffs_PackedTags2 * pt=(yaffs_PackedTags2 *);
If (pt - & gt; T.b yteCount==0 XFFFF) {//a new object

Yaffs_ObjectHeader * oh=(yaffs_ObjectHeader *) malloc (sizeof (yaffs_ObjectHeader));
Memcpy (oh, chunk_data, sizeof (yaffs_ObjectHeader));

Full_path_name=(char *) malloc (strlen (oh - & gt; Name) + strlen (obj_list [oh - & gt; parentObjectId]) + 2);
If (full_path_name==NULL) {
Perror (" malloc full path name \ n ");
}
Strcpy (full_path_name obj_list [oh - & gt; parentObjectId]);
Strcat (full_path_name, "/");
Strcat (full_path_name, oh - & gt; Name);
Obj_list [pt - & gt; t.o bjectId]=full_path_name;

The switch (oh - & gt; Type) {
Case YAFFS_OBJECT_TYPE_FILE:
Remain=oh - & gt; The fileSize;
Out_file=creat (full_path_name, oh - & gt; Yst_mode);
While (remain & gt; 0 {
If (read_chunk ())
return -1;
S=(remain & lt; Pt - & gt; T.b yteCount)? Remain: pt - & gt; T.b yteCount;
If (write (out_file chunk_data, s)==1)
return -1;
Remain -=s;
}
Close (out_file);
break;
Case YAFFS_OBJECT_TYPE_SYMLINK:
Symlink (oh - & gt; Alias, full_path_name);
break;
Case YAFFS_OBJECT_TYPE_DIRECTORY:
The mkdir (full_path_name, 0777);
break;
Case YAFFS_OBJECT_TYPE_HARDLINK:
Link (obj_list [oh - & gt; equivalentObjectId], full_path_name);
break;
}
}
}


Int read_chunk ()
{
Ssize_t s;
Int ret=1;
Memset (0 XFF chunk_data, sizeof (chunk_data));
S=read (img_file, data, CHUNK_SIZE + SPARE_SIZE);
If (s==1) {
Perror (" read image file \ n ");
{} else if (s==0)
Printf (" the end of the image \ n ");
} else if (s==(CHUNK_SIZE + SPARE_SIZE))) {
Ret=0;
} else {
Fprintf (stderr, "broken image file \ n");
}
return ret;
}

Int main (int arg c, char * * argv)
{
If (arg c!=2) {
Printf (" the Usage: unyaffs image_file_name \ n ");
The exit (1);
}
Img_file=open (argv [1], O_RDONLY);
If (img_file==1) {
Printf (" open image file failed \ n ");
The exit (1);
}

Obj_list [YAFFS_OBJECTID_ROOT]=". ";
While (1) {
If (read_chunk ()==1)
break;
Process_chunk ();
}
Close (img_file);
return 0;
}


/* 
* definition copied from yaffs project
*/

# # ifndef __UNYAFFS_H__
# define __UNYAFFS_H__


# define YAFFS_MAX_NAME_LENGTH 255
# define YAFFS_MAX_ALIAS_LENGTH 159
Definition of types *//*
Typedef unsigned char __u8;
Typedef unsigned short __u16;
Typedef unsigned __u32;

Typedef struct {
Unsigned sequenceNumber;
Unsigned objectId;
Unsigned chunkId;
Unsigned byteCount;
} yaffs_PackedTags2TagsPart;

Typedef struct {
Unsigned char colParity;
Unsigned lineParity;
Unsigned lineParityPrime;
} yaffs_ECCOther;

Typedef struct {
Yaffs_PackedTags2TagsPart t;
Yaffs_ECCOther ecc;
} yaffs_PackedTags2;

Typedef enum {
YAFFS_ECC_RESULT_UNKNOWN,
YAFFS_ECC_RESULT_NO_ERROR,
YAFFS_ECC_RESULT_FIXED,
YAFFS_ECC_RESULT_UNFIXED
} yaffs_ECCResult;

Typedef enum {
YAFFS_OBJECT_TYPE_UNKNOWN,
YAFFS_OBJECT_TYPE_FILE,
YAFFS_OBJECT_TYPE_SYMLINK,
YAFFS_OBJECT_TYPE_DIRECTORY,
YAFFS_OBJECT_TYPE_HARDLINK,
YAFFS_OBJECT_TYPE_SPECIAL
} yaffs_ObjectType;


Typedef struct {

Unsigned validMarker0;
Unsigned chunkUsed;/* the Status of the chunk, informs the or unused */
Unsigned objectId;/* If 0 then this is not part of an object (unused) */
Unsigned chunkId;/* If 0 then this is a header, else a data chunk */
Unsigned byteCount;/* Only valid for the data chunks */

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related