resbin.c

来自「基于4个mips核的noc设计」· C语言 代码 · 共 2,388 行 · 第 1/4 页

C
2,388
字号
	      vv->next = NULL;	      *ppvv = vv;	      ppvv = &vv->next;	      data += 4;	      length -= 4;	      if (vallen < 4)		fatal (_("unexpected version value length %d"), vallen);	      vallen -= 4;	    }	}      else	fatal (_("unexpected version string"));      vi->next = NULL;      *pp = vi;      pp = &vi->next;          }  v = (struct versioninfo *) res_alloc (sizeof *v);  v->fixed = fi;  v->var = first;  r = (struct res_resource *) res_alloc (sizeof *r);  r->type = RES_TYPE_VERSIONINFO;  r->u.versioninfo = v;  return r;  }/* Convert an arbitrary user defined resource from binary.  */static struct res_resource *bin_to_res_userdata (data, length, big_endian)     const unsigned char *data;     unsigned long length;     int big_endian;{  struct rcdata_item *ri;  struct res_resource *r;  ri = (struct rcdata_item *) res_alloc (sizeof *ri);  ri->next = NULL;  ri->type = RCDATA_BUFFER;  ri->u.buffer.length = length;  ri->u.buffer.data = data;  r = (struct res_resource *) res_alloc (sizeof *r);  r->type = RES_TYPE_USERDATA;  r->u.rcdata = ri;  return r;}/* Macros to swap out values.  */#define put_16(be, v, s) ((be) ? bfd_putb16 ((v), (s)) : bfd_putl16 ((v), (s)))#define put_32(be, v, s) ((be) ? bfd_putb32 ((v), (s)) : bfd_putl32 ((v), (s)))/* Local functions used to convert resources to binary format.  */static void dword_align_bin PARAMS ((struct bindata ***, unsigned long *));static struct bindata *resid_to_bin PARAMS ((struct res_id, int));static struct bindata *unicode_to_bin PARAMS ((const unichar *, int));static struct bindata *res_to_bin_accelerator  PARAMS ((const struct accelerator *, int));static struct bindata *res_to_bin_cursor  PARAMS ((const struct cursor *, int));static struct bindata *res_to_bin_group_cursor  PARAMS ((const struct group_cursor *, int));static struct bindata *res_to_bin_dialog  PARAMS ((const struct dialog *, int));static struct bindata *res_to_bin_fontdir  PARAMS ((const struct fontdir *, int));static struct bindata *res_to_bin_group_icon  PARAMS ((const struct group_icon *, int));static struct bindata *res_to_bin_menu  PARAMS ((const struct menu *, int));static struct bindata *res_to_bin_menuitems  PARAMS ((const struct menuitem *, int));static struct bindata *res_to_bin_menuexitems  PARAMS ((const struct menuitem *, int));static struct bindata *res_to_bin_rcdata  PARAMS ((const struct rcdata_item *, int));static struct bindata *res_to_bin_stringtable  PARAMS ((const struct stringtable *, int));static struct bindata *string_to_unicode_bin PARAMS ((const char *, int));static struct bindata *res_to_bin_versioninfo  PARAMS ((const struct versioninfo *, int));static struct bindata *res_to_bin_generic  PARAMS ((unsigned long, const unsigned char *));/* Convert a resource to binary.  */struct bindata *res_to_bin (res, big_endian)     const struct res_resource *res;     int big_endian;{  switch (res->type)    {    default:      abort ();    case RES_TYPE_BITMAP:    case RES_TYPE_FONT:    case RES_TYPE_ICON:    case RES_TYPE_MESSAGETABLE:      return res_to_bin_generic (res->u.data.length, res->u.data.data);    case RES_TYPE_ACCELERATOR:      return res_to_bin_accelerator (res->u.acc, big_endian);    case RES_TYPE_CURSOR:      return res_to_bin_cursor (res->u.cursor, big_endian);    case RES_TYPE_GROUP_CURSOR:      return res_to_bin_group_cursor (res->u.group_cursor, big_endian);    case RES_TYPE_DIALOG:      return res_to_bin_dialog (res->u.dialog, big_endian);    case RES_TYPE_FONTDIR:      return res_to_bin_fontdir (res->u.fontdir, big_endian);    case RES_TYPE_GROUP_ICON:      return res_to_bin_group_icon (res->u.group_icon, big_endian);    case RES_TYPE_MENU:      return res_to_bin_menu (res->u.menu, big_endian);    case RES_TYPE_RCDATA:      return res_to_bin_rcdata (res->u.rcdata, big_endian);    case RES_TYPE_STRINGTABLE:      return res_to_bin_stringtable (res->u.stringtable, big_endian);    case RES_TYPE_USERDATA:      return res_to_bin_rcdata (res->u.rcdata, big_endian);    case RES_TYPE_VERSIONINFO:      return res_to_bin_versioninfo (res->u.versioninfo, big_endian);    }}/* Align to a 32 bit boundary.  PPP points to the of a list of bindata   structures.  LENGTH points to the length of the structures.  If   necessary, this adds a new bindata to bring length up to a 32 bit   boundary.  It updates *PPP and *LENGTH.  */static voiddword_align_bin (ppp, length)     struct bindata ***ppp;     unsigned long *length;{  int add;  struct bindata *d;  if ((*length & 3) == 0)    return;  add = 4 - (*length & 3);  d = (struct bindata *) reswr_alloc (sizeof *d);  d->length = add;  d->data = (unsigned char *) reswr_alloc (add);  memset (d->data, 0, add);  d->next = NULL;  **ppp = d;  *ppp = &(**ppp)->next;  *length += add;}/* Convert a resource ID to binary.  This always returns exactly one   bindata structure.  */static struct bindata *resid_to_bin (id, big_endian)     struct res_id id;     int big_endian;{  struct bindata *d;  d = (struct bindata *) reswr_alloc (sizeof *d);  if (! id.named)    {      d->length = 4;      d->data = (unsigned char *) reswr_alloc (4);      put_16 (big_endian, 0xffff, d->data);      put_16 (big_endian, id.u.id, d->data + 2);    }  else    {      int i;      d->length = id.u.n.length * 2 + 2;      d->data = (unsigned char *) reswr_alloc (d->length);      for (i = 0; i < id.u.n.length; i++)	put_16 (big_endian, id.u.n.name[i], d->data + i * 2);      put_16 (big_endian, 0, d->data + i * 2);    }  d->next = NULL;  return d;}/* Convert a null terminated unicode string to binary.  This always   returns exactly one bindata structure.  */static struct bindata *unicode_to_bin (str, big_endian)     const unichar *str;     int big_endian;{  int len;  struct bindata *d;  len = 0;  if (str != NULL)    {      const unichar *s;      for (s = str; *s != 0; s++)	++len;    }  d = (struct bindata *) reswr_alloc (sizeof *d);  d->length = len * 2 + 2;  d->data = (unsigned char *) reswr_alloc (d->length);  if (str == NULL)    put_16 (big_endian, 0, d->data);  else    {      const unichar *s;      int i;      for (s = str, i = 0; *s != 0; s++, i++)	put_16 (big_endian, *s, d->data + i * 2);      put_16 (big_endian, 0, d->data + i * 2);    }  d->next = NULL;  return d;}/* Convert an accelerator resource to binary.  */static struct bindata *res_to_bin_accelerator (accelerators, big_endian)     const struct accelerator *accelerators;     int big_endian;{  struct bindata *first, **pp;  const struct accelerator *a;  first = NULL;  pp = &first;  for (a = accelerators; a != NULL; a = a->next)    {      struct bindata *d;      d = (struct bindata *) reswr_alloc (sizeof *d);      d->length = 8;      d->data = (unsigned char *) reswr_alloc (8);      put_16 (big_endian,	      a->flags | (a->next != NULL ? 0 : ACC_LAST),	      d->data);      put_16 (big_endian, a->key, d->data + 2);      put_16 (big_endian, a->id, d->data + 4);      put_16 (big_endian, 0, d->data + 8);      d->next = NULL;      *pp = d;      pp = &d->next;    }  return first;}/* Convert a cursor resource to binary.  */static struct bindata *res_to_bin_cursor (c, big_endian)     const struct cursor *c;     int big_endian;{  struct bindata *d;  d = (struct bindata *) reswr_alloc (sizeof *d);  d->length = 4;  d->data = (unsigned char *) reswr_alloc (4);  put_16 (big_endian, c->xhotspot, d->data);  put_16 (big_endian, c->yhotspot, d->data + 2);  d->next = (struct bindata *) reswr_alloc (sizeof *d);  d->next->length = c->length;  d->next->data = (unsigned char *) c->data;  d->next->next = NULL;  return d;}/* Convert a group cursor resource to binary.  */static struct bindata *res_to_bin_group_cursor (group_cursors, big_endian)     const struct group_cursor *group_cursors;     int big_endian;{  struct bindata *first, **pp;  int c;  const struct group_cursor *gc;  first = (struct bindata *) reswr_alloc (sizeof *first);  first->length = 6;  first->data = (unsigned char *) reswr_alloc (6);  put_16 (big_endian, 0, first->data);  put_16 (big_endian, 2, first->data + 2);  first->next = NULL;  pp = &first->next;  c = 0;  for (gc = group_cursors; gc != NULL; gc = gc->next)    {      struct bindata *d;      ++c;      d = (struct bindata *) reswr_alloc (sizeof *d);      d->length = 14;      d->data = (unsigned char *) reswr_alloc (14);      put_16 (big_endian, gc->width, d->data);      put_16 (big_endian, gc->height, d->data + 2);      put_16 (big_endian, gc->planes, d->data + 4);      put_16 (big_endian, gc->bits, d->data + 6);      put_32 (big_endian, gc->bytes, d->data + 8);      put_16 (big_endian, gc->index, d->data + 12);      d->next = NULL;      *pp = d;      pp = &d->next;    }  put_16 (big_endian, c, first->data + 4);  return first;}/* Convert a dialog resource to binary.  */static struct bindata *res_to_bin_dialog (dialog, big_endian)     const struct dialog *dialog;     int big_endian;{  int dialogex;  struct bindata *first, **pp;  unsigned long length;  int off, c;  struct dialog_control *dc;  dialogex = extended_dialog (dialog);  first = (struct bindata *) reswr_alloc (sizeof *first);  first->length = dialogex ? 26 : 18;  first->data = (unsigned char *) reswr_alloc (first->length);  length = first->length;  if (! dialogex)    {      put_32 (big_endian, dialog->style, first->data);      put_32 (big_endian, dialog->exstyle, first->data + 4);      off = 8;    }  else    {      put_16 (big_endian, 1, first->data);      put_16 (big_endian, 0xffff, first->data + 2);      if (dialog->ex == NULL)	put_32 (big_endian, 0, first->data + 4);      else	put_32 (big_endian, dialog->ex->help, first->data + 4);      put_32 (big_endian, dialog->exstyle, first->data + 8);      put_32 (big_endian, dialog->style, first->data + 12);      off = 16;    }  put_16 (big_endian, dialog->x, first->data + off + 2);  put_16 (big_endian, dialog->y, first->data + off + 4);  put_16 (big_endian, dialog->width, first->data + off + 6);  put_16 (big_endian, dialog->height, first->data + off + 8);  pp = &first->next;  *pp = resid_to_bin (dialog->menu, big_endian);  length += (*pp)->length;  pp = &(*pp)->next;  *pp = resid_to_bin (dialog->class, big_endian);  length += (*pp)->length;  pp = &(*pp)->next;  *pp = unicode_to_bin (dialog->caption, big_endian);  length += (*pp)->length;  pp = &(*pp)->next;  if ((dialog->style & DS_SETFONT) != 0)    {      struct bindata *d;      d = (struct bindata *) reswr_alloc (sizeof *d);      d->length = dialogex ? 6 : 2;      d->data = (unsigned char *) reswr_alloc (d->length);      length += d->length;      put_16 (big_endian, dialog->pointsize, d->data);      if (dialogex)	{	  if (dialog->ex == NULL)	    {	      put_16 (big_endian, 0, d->data + 2);	      put_16 (big_endian, 0, d->data + 4);	    }	  else	    {	      put_16 (big_endian, dialog->ex->weight, d->data + 2);	      put_16 (big_endian, dialog->ex->italic, d->data + 4);	    }	}      *pp = d;      pp = &d->next;      *pp = unicode_to_bin (dialog->font, big_endian);      length += (*pp)->length;      pp = &(*pp)->next;    }  c = 0;  for (dc = dialog->controls; dc != NULL; dc = dc->next)    {      struct bindata *d;      int dcoff;      ++c;      dword_align_bin (&pp, &length);      d = (struct bindata *) reswr_alloc (sizeof *d);      d->length = dialogex ? 24 : 18;      d->data = (unsigned char *) reswr_alloc (d->length);      length += d->length;      if (! dialogex)	{	  put_32 (big_endian, dc->style, d->data);	  put_32 (big_endian, dc->exstyle, d->data + 4);	  dcoff = 8;	}      else	{	  put_32 (big_endian, dc->help, d->data);	  put_32 (big_endian, dc->exstyle, d->data + 4);	  put_32 (big_endian, dc->style, d->data + 8);	  dcoff = 12;	}      put_16 (big_endian, dc->x, d->data + dcoff);      put_16 (big_endian, dc->y, d->data + dcoff + 2);      put_16 (big_endian, dc->width, d->data + dcoff + 4);      put_16 (big_endian, dc->height, d->data + dcoff + 6);      if (dialogex)        put_32 (big_endian, dc->id, d->data + dcoff + 8);      else        put_16 (big_endian, dc->id, d->data + dcoff + 8);      *pp = d;      pp = &d->next;      *pp = resid_to_bin (dc->class, big_endian);      length += (*pp)->length;      pp = &(*pp)->next;      *pp = resid_to_bin (dc->text, big_endian);      length += (*pp)->length;      pp = &(*pp)->next;      d = (struct bindata *) reswr_alloc (sizeof *d);      d->length = 2;      d->data = (unsigned char *) reswr_alloc (2);      length += 2;      d->next = NULL;      *pp = d;      pp = &d->next;      if (dc->data == NULL)	put_16 (big_endian, 0, d->data);      else	{	  unsigned long sublen;	  dword_align_bin (&pp, &length);	  *pp = res_to_bin_rcdata (dc->data, big_endian);	  sublen = 0;	  while (*pp != NULL)	    {	      sublen += (*pp)->length;	      pp = &(*pp)->next;	    }	  put_16 (big_endian, sublen, d->data);	  length += sublen;	}    }  put_16 (big_endian, c, first->data + off);  return first;}/* Convert a fontdir resource to binary.  */static struct bindata *res_to_bin_fontdir (fontdirs, big_endian)     const struct fontdir *fontdirs;     int big_endian;{  struct bindata *first, **pp;  int c;  const struct fontdir *fd;  first = (struct bindata *) reswr_alloc (sizeof *first);  first->length = 2;  first->data = (unsigned char *) reswr_alloc (2);  first->next = NULL;  pp = &first->next;  c = 0;  for (fd = fontdirs; fd != NULL; fd = fd->next)    {      struct bindata *d;      ++c;      d = (struct bindata *) reswr_alloc (sizeof *d);      d->length = 2;      d->data = (unsigned char *) reswr_alloc (2);      put_16 (big_endian, fd->index, d->data);      *pp = d;      pp = &d->next;      d = (struct bindata *) reswr_alloc (sizeof *d);      d->length = fd->length;      d->data = (unsigned char *) fd->data;      d->next = NULL;      *pp = d;      pp = &d->next;          }  put_16 (big_endian, c, first->data);  return first;  }/* Convert a group icon resource to binary.  */static struct bindata *res_to_bin_group_icon (group_icons, big_endian)     const struct group_icon *group_icons;     int big_endian;{  struct bindata *first, **pp;  int c;  const struct group_icon *gi;  first = (struct bindata *) reswr_alloc (sizeof *first);  first->length = 6;  first->data = (unsigned char *) reswr_alloc (6);  put_16 (big_endian, 0, first->data);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?