[an error occurred while processing this directive]
Вдогонку
(«Телесистемы»: Конференция «Микроконтроллеры и их применение»)

миниатюрный аудио-видеорекордер mAVR

Отправлено xlin 22 февраля 2003 г. 11:58
В ответ на: как вычислить контр. сумму отправлено ingemar 22 февраля 2003 г. 11:48

// ihex.h
// Ivan
// C 22.07.2002
// LC 22.07.2002

#ifndef __IHEX_H
#define __IHEX_H

#ifdef __WIN32__
#ifndef EXP
#define EXP __declspec(dllexport) __stdcall
#endif
#ifndef EXPNAME
#define EXPNAME extern "C"
#endif
#else
#ifndef EXP
#define EXP
#endif
#ifndef EXPNAME
#define EXPNAME
#endif
#endif

#define IHEX_OK 0
#define IHEX_ERROR (-1)

struct TIntelHexBlock{
unsigned long addr;
unsigned long size;
unsigned char *data;
TIntelHexBlock *next;
};

EXPNAME int EXP ReadIntelHex(const char *file_name,void *buffer,unsigned long buffer_size);


EXPNAME int EXP WriteIntelHex(const char *file_name,void *buffer);

#endif // __IHEX_H

------------------------------------------------------------

// ihex.cpp
// Ivan
// C 22.07.2002
// LC 22.07.2002

/*
#if defined(__WIN32__) || defined(WIN32)
// Win32
# define _W_
# ifdef __BORLANDC__
# define _B_
# include
# else
# include
# endif
#else
// Linux
#include
#include
#endif

#ifdef _B_
#pragma hdrstop
#endif
*/

#include
#pragma hdrstop

//#include

#define _W_


#include "ihex.h"

#ifdef _W_
#ifndef IHV
#define IHV INVALID_HANDLE_VALUE
#endif
#endif

#define BAD_HEX 0x100

#define tpData 0
#define tpEnd 1
#define tpSeg 2
#define tpLin 4
#define tpStart 5

static int HexChar2Int(char c){
if(c>='0' && c<='9')
return (int)(c-'0');
if(c>='A' && c<='F')
return (int)(c-'A'+10);
if(c>='a' && c<='f')
return (int)(c-'a'+10);
return BAD_HEX;
};

static int Hex2Int(char c0,char c1){
int x=HexChar2Int(c0);
if(x==BAD_HEX)
return BAD_HEX;
int x1=HexChar2Int(c1);
if(x1==BAD_HEX)
return BAD_HEX;
return (int)((x<<4)|x1);
};

static char Int2HexChar(int x){
return x>=10?x-10+'A':x+'0';
};

static void Int2HexByte(int x,char &c0,char &c1){
c0=Int2HexChar((x>>4)&0xff);
c1=Int2HexChar(x&0xff);
};

inline char* StrHexByte(char *p,int x){
char c0, c1;
Int2HexByte(x,c0,c1);
*p++=c0;
*p++=c1;
return p;
};

static char* ReadHexFile(const char *file_name){
char *p=0;
#ifdef _W_
HANDLE h=CreateFile(file_name,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,0,0);
if(h==IHV)
return 0;
DWORD fsz=GetFileSize(h,0);
if(fsz){
p=(char*)malloc(fsz+1);
DWORD rsz;
ReadFile(h,p,fsz,&rsz,0);
p[fsz]=0;
};
CloseHandle(h);
#else
int h=open(file_name,O_RDONLY);
if(h==-1)
return 0;
int sz=lseek(h,0,SEEK_END);
if(sz){
lseek(h,0,SEEK_SET);
p=(char*)malloc(sz+1);
read(h,p,sz);
p[sz]=0;
};
close(h);
#endif
return p;
};

static int SplitLine(char *p,int &cnt,unsigned long &addr,int &tp,unsigned char *data){
if(*p++!=':')
return IHEX_ERROR;
char c0, c1;
c0=*p++;
c1=*p++;
cnt=Hex2Int(c0,c1);
int csum=0;
if(cnt==BAD_HEX)
return IHEX_ERROR;
csum+=cnt;
c0=*p++;
c1=*p++;
int ahi=Hex2Int(c0,c1);
if(ahi==BAD_HEX)
return IHEX_ERROR;
csum+=ahi;
c0=*p++;
c1=*p++;
int alo=Hex2Int(c0,c1);
if(alo==BAD_HEX)
return IHEX_ERROR;
csum+=alo;
addr=(ahi<<8)+alo;
c0=*p++;
c1=*p++;
tp=Hex2Int(c0,c1);
if(tp==BAD_HEX)
return IHEX_ERROR;
csum+=tp;

for(int i=0;i c0=*p++;
c1=*p++;
int b=Hex2Int(c0,c1);
if(b==BAD_HEX)
return IHEX_ERROR;
csum+=b;
*data++=b;
};
c0=*p++;
c1=*p++;
int ncs=Hex2Int(c0,c1);
if(ncs==BAD_HEX)
return IHEX_ERROR;
csum&=0xFF;
if(((256-csum)&0xFF)!=ncs){
return IHEX_ERROR;
};
return IHEX_OK;
};

static char *GetNewLine(char *&p){
while(*p && (*p==10 || *p==' ' || *p==9))
p++;
if(!*p)
return 0;
char *ret=p;
while(*p && *p!=13)
p++;
*p++=0;
return ret;
};


EXPNAME int EXP ReadIntelHex(const char *file_name,void *buffer,unsigned long buffer_size){
unsigned char b[128];
char *pl=ReadHexFile(file_name);
if(!pl)
return IHEX_ERROR;
char *pbuf=(char*)buffer;
TIntelHexBlock *hb=(TIntelHexBlock*)pbuf;
hb->addr=0;
hb->size=0;
hb->data=pbuf+sizeof(*hb);
hb->next=0;
char *pline=pl;
char *nline=pl;
unsigned long seg=0;
int tp=tpData;
int ret=IHEX_ERROR;
while(tp!=tpEnd){
pline=GetNewLine(nline);
if(!*pline)
break;
unsigned long A=0;
int cnt=0;
if(SplitLine(pline,cnt,A,tp,b)==IHEX_ERROR)
break;
if(tp==tpEnd){
ret=IHEX_OK;
break;
};
if(tp==tpLin){
A=*((unsigned short*)&b[0]);
A<<=8;
seg=A;
TIntelHexBlock *nhb=(TIntelHexBlock*)hb->data+hb->size;
hb->next=nhb;
hb=nhb;
hb->addr=A;
hb->size=0;
hb->data=((char*)hb)+sizeof(*hb);
};
if(tp==tpData){
if((A+seg)!=(hb->addr+hb->size)){
TIntelHexBlock *nhb=(TIntelHexBlock*)hb->data+hb->size;
hb->next=nhb;
hb=nhb;
hb->addr=A+seg;
hb->size=0;
hb->data=((char*)hb)+sizeof(*hb);
};
for(int i=0;i hb->data[hb->size++]=b[i];
};
};
free(pl);
return ret;
};

EXPNAME int EXP WriteIntelHex(const char *file_name,void *buffer){
return IHEX_OK;
};


Составить ответ  |||  Конференция  |||  Архив

Ответы



Перейти к списку ответов  |||  Конференция  |||  Архив  |||  Главная страница  |||  Содержание  |||  Без кадра

E-mail: info@telesys.ru