Beispiel #1
0
int GWEN_TLV_DirectlyToBuffer(unsigned int tagType,
                              unsigned int tagMode,
                              const void *tagData,
                              int tagLength,
                              int isBerTlv,
                              GWEN_BUFFER *mbuf) {
  if (tagLength==-1)
    tagLength=strlen(tagData);

  if (isBerTlv) {
    unsigned char j;

    /* write tag type */
    j=tagMode;
    if (tagType>=0x1f) {
      j|=0x1f;
      GWEN_Buffer_AppendByte(mbuf, j);
      GWEN_Buffer_AppendByte(mbuf, (unsigned char)tagType);
    }
    else {
      j|=tagType;
      GWEN_Buffer_AppendByte(mbuf, j);
    }

    /* write tag length */
    if (tagLength>255) {
      /* two byte size */
      GWEN_Buffer_AppendByte(mbuf, 0x82);
      GWEN_Buffer_AppendByte(mbuf, ((tagLength>>8) & 0xff));
      GWEN_Buffer_AppendByte(mbuf, (tagLength & 0xff));
    }
    else if (tagLength>127) {
Beispiel #2
0
int AHB_DTAUS__AddDtaWord(GWEN_BUFFER *dst,
			  unsigned int size,
			  const char *s) {
  unsigned int i;
  unsigned int ssize;

  assert(dst);
  assert(size);
  assert(s);

  DBG_DEBUG(AQBANKING_LOGDOMAIN, "Adding DTA word: %s", s);

  ssize=strlen(s);
  if (ssize>size) {
    /* Error out here because e.g. truncated accountid will lead to failed jobs. */
    DBG_ERROR(AQBANKING_LOGDOMAIN, "Word \"%s\" too long: Has length %d but must not be longer than %d characters",
	      s, ssize, size);
    return -1;
  }

  for (i=0; i<size; i++) {
    char c;

    if (i>=ssize)
      c=0;
    else
      c=s[i];

    if (c)
      GWEN_Buffer_AppendByte(dst, c);
    else
      GWEN_Buffer_AppendByte(dst, ' ');
  } /* for */
  return 0;
}
Beispiel #3
0
int GWEN_Logger_Log(const char *logDomain,
                    GWEN_LOGGER_LEVEL priority, const char *s) {
  if (!GWEN_Gui_LogHook(logDomain, priority, s)) {
    const char *p;
    int rv;
    unsigned int i;
    GWEN_BUFFER *mbuf;
    GWEN_LOGGER *lg;

    lg=GWEN_LoggerDomain_GetLogger(logDomain);
    assert(lg);

    if (!lg->enabled)
      return 1;

    if (priority>lg->logLevel)
      /* priority too low, don't log */
      return 0;

    /* temporarily disable logging to avoid endless loops */
    lg->enabled=0;
    /* copy buffer, exchange all newlines by 0 */
    mbuf=GWEN_Buffer_new(0, strlen(s)+1, 0, 1);
    for (i=0; i<strlen(s)+1; i++) {
      if (s[i]=='\n') {
        GWEN_Buffer_AppendByte(mbuf, 0);
      }
      else
        GWEN_Buffer_AppendByte(mbuf, s[i]);
    }

    /* now log each line */
    rv=0;
    p=GWEN_Buffer_GetStart(mbuf);
    while (*p) {
      rv|=GWEN_Logger__Log(lg, priority, p);
      while(*p)
        p++;
      p++;
    }
    GWEN_Buffer_free(mbuf);
    /* reenable logging */
    lg->enabled=1;
    return rv;
  }
  else
    return 0;
}
Beispiel #4
0
int GWEN_Gui_CProgress_Log(GWEN_GUI_CPROGRESS *cp,
                           GWEN_UNUSED GWEN_LOGGER_LEVEL level,
                           const char *text)
{
  assert(cp);
  assert(text);

  if (!(GWEN_Gui_GetFlags(cp->gui) & GWEN_GUI_FLAGS_NONINTERACTIVE)) {
    GWEN_BUFFER *tbuf;
    const char *t;

    tbuf=GWEN_Buffer_new(0, 256, 0, 1);
    GWEN_Gui_GetRawText(cp->gui, text, tbuf);
    t=GWEN_Buffer_GetStart(tbuf);
    if (t[GWEN_Buffer_GetUsedBytes(tbuf)-1]!='\n') {
      GWEN_Buffer_AppendByte(tbuf, '\n');
      /* Just in case the buffer has been reallocated */
      t=GWEN_Buffer_GetStart(tbuf);
    }
    GWEN_Gui_StdPrintf(cp->gui, stderr, "%s", t);

    GWEN_Buffer_AppendString(cp->logBuf, t);
    GWEN_Buffer_free(tbuf);
    tbuf=0;
    if (cp->aborted)
      return GWEN_ERROR_USER_ABORTED;
  }
  return 0;
}
Beispiel #5
0
int AHB_DTAUS__AddNum(GWEN_BUFFER *dst,
                      unsigned int size,
                      const char *s) {
  unsigned int i, j;

  assert(dst);
  assert(s);

  DBG_DEBUG(AQBANKING_LOGDOMAIN, "Adding num : %s", s);

  i=strlen(s);
  if (i>size) {
    /* Error out here because e.g. truncated BLZ will lead to failed jobs. */
    DBG_ERROR(AQBANKING_LOGDOMAIN, "Number \"%s\" too long: Has length %d but must not be longer than %d characters",
	      s, i, size);
    return -1;
  }
  j=size-i;
  if (j) {
    unsigned int k;

    for (k=0; k<j; k++)
      GWEN_Buffer_AppendByte(dst, '0');
  }
  GWEN_Buffer_AppendString(dst, s);
  return 0;
}
Beispiel #6
0
int AH_User_AddTextWithoutTags(const char *s, GWEN_BUFFER *obuf) {
  while(*s) {
    if (*s=='<') {
      const char *s2;
      int l;

      s2=s;
      s2++;
      while(*s2 && *s2!='>')
	s2++;
      l=s2-s-2;
      if (l>0) {
	const char *s3;

	s3=s;
	s3++;
	if (l==2) {
	  if (strncasecmp(s3, "br", 2)==0)
	    GWEN_Buffer_AppendString(obuf, "\n");
	}
	else if (l==3) {
	  if (strncasecmp(s3, "br/", 3)==0)
	    GWEN_Buffer_AppendString(obuf, "\n");
	}
      }
      s=s2; /* set s to position of closing bracket */
    }
    else
      GWEN_Buffer_AppendByte(obuf, *s);
    /* next char */
    s++;
  }

  return 0;
}
Beispiel #7
0
int GWEN_Padd_PaddWithAnsiX9_23ToMultipleOf(GWEN_BUFFER *src, int y) {
  unsigned char paddLength;
  unsigned int i;

  paddLength=y-(GWEN_Buffer_GetUsedBytes(src) % y);
  for (i=0; i<paddLength; i++)
    GWEN_Buffer_AppendByte(src, paddLength);
  return 0;
}
Beispiel #8
0
void AB_ImExporterContext_AddLog(AB_IMEXPORTER_CONTEXT *iec,
				 const char *s) {
  assert(iec);
  if (s) {
    size_t l=strlen(s);
    if (s) {
      GWEN_Buffer_AppendString(iec->logs, s);
      if (s[l-1]!='\n')
        GWEN_Buffer_AppendByte(iec->logs, '\n');
    }
  }
}
Beispiel #9
0
int EBC_Provider_Generate_OrderId(AB_PROVIDER *pro, GWEN_BUFFER *buf) {
  uint32_t id;
  char rbuf[4];
  char c;
  uint32_t j;

  GWEN_Buffer_AllocRoom(buf, 4);
  id=AB_Banking_GetUniqueId(AB_Provider_GetBanking(pro));
  if (id==0)
    return GWEN_ERROR_IO;

  rbuf[3]=id%36;
  j=id/36;
  rbuf[2]=j%36;
  j/=36;
  rbuf[1]=j%36;
  j/=36;
  rbuf[0]=j%26;

  c=rbuf[0];
  c+='A';
  GWEN_Buffer_AppendByte(buf, c);

  c=rbuf[1];
  if (c<10) c+='0';
  else c+='A'-10;
  GWEN_Buffer_AppendByte(buf, c);

  c=rbuf[2];
  if (c<10) c+='0';
  else c+='A'-10;
  GWEN_Buffer_AppendByte(buf, c);

  c=rbuf[3];
  if (c<10) c+='0';
  else c+='A'-10;
  GWEN_Buffer_AppendByte(buf, c);

  return 0;
}
Beispiel #10
0
int GWEN_Date_toStringWithTemplate(const GWEN_DATE *t, const char *tmpl, GWEN_BUFFER *buf) {
  GWEN_DATE_TMPLCHAR_LIST *ll;
  const char *s;

  ll=GWEN_DateTmplChar_List_new();
  GWEN_Date__sampleTmplChars(t, tmpl, buf, ll);
  GWEN_Date__fillTmplChars(t, ll);

  s=tmpl;
  while(*s) {
    if (strchr("YMDWw", *s)) {
      GWEN_DATE_TMPLCHAR *e;
      char c;

      e=GWEN_Date__findTmplChar(ll, *s);
      assert(e);
      assert(e->content);
      if (s[1]=='*') {
        /* append full string */
        GWEN_Buffer_AppendString(buf, e->content);
        /* skip asterisk */
        s++;
      }
      else {
        c=e->content[e->nextChar];
        if (c!=0) {
          GWEN_Buffer_AppendByte(buf, c);
          e->nextChar++;
        }
      }
    }
    else
      GWEN_Buffer_AppendByte(buf, *s);
    s++;
  }
  GWEN_DateTmplChar_List_free(ll);
  return 0;
}
Beispiel #11
0
void AB_ImExporter_DtaToUtf8(const char *p,
                             int size,
                             GWEN_BUFFER *buf) {
  while(*p) {
    unsigned int c;

    if (!size)
      break;

    c=(unsigned char)(*(p++));
    switch(c) {
    case 0x5b: /* AE */
      GWEN_Buffer_AppendByte(buf, 0xc3);
      GWEN_Buffer_AppendByte(buf, 0x84);
      break;

    case 0x5c: /* OE */
      GWEN_Buffer_AppendByte(buf, 0xc3);
      GWEN_Buffer_AppendByte(buf, 0x96);
      break;

    case 0x5d: /* UE */
      GWEN_Buffer_AppendByte(buf, 0xc3);
      GWEN_Buffer_AppendByte(buf, 0x9c);
      break;

    case 0x7e: /* sharp s */
      GWEN_Buffer_AppendByte(buf, 0xc3);
      GWEN_Buffer_AppendByte(buf, 0x9f);
      break;

    default:
      if (c & 0x80) {
        /* produce sane UTF-8 even if something went wrong */
        DBG_ERROR(AQBANKING_LOGDOMAIN, "Invalid character in DTA string");
        c=' ';
      }
      GWEN_Buffer_AppendByte(buf, c);
    }
    if (size!=-1)
      size--;
  } /* while */
}
Beispiel #12
0
int GWEN_Directory_OsifyPath(const char *path, GWEN_BUFFER *pbuf,
                             int transformDriveElement) {
  const char *p;

  p=path;

  /* handle drive letters (only check for normal slashes here) */
#ifdef OS_WIN32
  if (transformDriveElement) {
    if (*p=='/')
      if (isalpha(p[1]))
        if (p[2]=='/' || p[2]==0) {
          GWEN_Buffer_AppendByte(pbuf, p[0]);
          GWEN_Buffer_AppendByte(pbuf, ':');
          p+=2;
        }
  }
#endif

  while(*p) {
    if (*p=='/' || *p=='\\') {
      while (*p=='/' || *p=='\\')
        p++;
#ifdef OS_WIN32
      GWEN_Buffer_AppendByte(pbuf, '\\');
#else
      GWEN_Buffer_AppendByte(pbuf, '/');
#endif
    }
    else {
      GWEN_Buffer_AppendByte(pbuf, *p);
      p++;
    }
  }

  return 0;
}
Beispiel #13
0
int GWEN_XML_AddNameSpace(GWEN_STRINGLIST2 *sl,
                          const char *prefix,
                          const char *name) {
  GWEN_BUFFER *nbuf;
  int rv;

  nbuf=GWEN_Buffer_new(0, 32, 0, 1);
  if (prefix)
    GWEN_Buffer_AppendString(nbuf, prefix);
  GWEN_Buffer_AppendByte(nbuf, ':');
  GWEN_Buffer_AppendString(nbuf, name);
  rv=GWEN_StringList2_AppendString(sl, GWEN_Buffer_GetStart(nbuf), 0,
                                   GWEN_StringList2_IntertMode_NoDouble);
  GWEN_Buffer_free(nbuf);
  return rv;
}
Beispiel #14
0
int GWEN_Padd_PaddWithIso9796_2(GWEN_BUFFER *buf, int dstSize) {
  unsigned int diff;
  char *p;
  int i;

  if ((unsigned int)dstSize<GWEN_Buffer_GetUsedBytes(buf)+12) {
    /*DBG_ERROR(GWEN_LOGDOMAIN, "Buffer contains too much data");*/
    return GWEN_ERROR_INVALID;
  }

  /* add trailer */
  GWEN_Buffer_AppendByte(buf, 0xbc);

  /* reset position to 0 */
  GWEN_Buffer_Rewind(buf);

  /* insert room for header */
  diff=dstSize-GWEN_Buffer_GetUsedBytes(buf)-11+1;
  if (GWEN_Buffer_InsertRoom(buf, 1+diff+1+8)) {
    DBG_ERROR(GWEN_LOGDOMAIN,
              "Could not insert room for %d bytes",
              1+diff+1+8);
    return GWEN_ERROR_GENERIC;
  }

  /* insert header and more-data-bit */
  p=GWEN_Buffer_GetStart(buf);
  *(p++)=0x60;

  /* insert padding field */
  for (i=0; i<diff; i++)
    *(p++)=0x0;
  *(p++)=0x01;

  /* insert 8 random bytes */
  GWEN_Crypt_Random(2, (uint8_t*)p, 8);
  for (i=0; i<8; i++) {
    if (*p==0)
      /* TODO: Need to find a better but yet fast way */
      *p=0xff;
    p++;
  }

  return 0;
}
Beispiel #15
0
int GWEN_XML__ReadData(GWEN_XML_CONTEXT *ctx,
                       GWEN_FAST_BUFFER *fb,
                       GWEN_UNUSED uint32_t flags) {
  int chr;
  unsigned char uc;
  GWEN_BUFFER *dbuf;

  dbuf=GWEN_Buffer_new(0, 256, 0, 1);

  for (;;) {
    GWEN_FASTBUFFER_PEEKBYTE(fb, chr);
    if (chr<0) {
      if (chr==GWEN_ERROR_EOF)
        break;
      else {
        GWEN_Buffer_free(dbuf);
        return chr;
      }
    }

    uc=(unsigned char) chr;
    if (uc=='<')
      break;
    fb->bufferReadPos++;
    GWEN_Buffer_AppendByte(dbuf, uc);
  }

  if (GWEN_Buffer_GetUsedBytes(dbuf)) {
    int rv;
    const char *s;

    s=GWEN_Buffer_GetStart(dbuf);
    if (*s) {
      rv=GWEN_XmlCtx_AddData(ctx, s);
      if (rv) {
        GWEN_Buffer_free(dbuf);
        return rv;
      }
    }
  }
  GWEN_Buffer_free(dbuf);

  return 0;
}
Beispiel #16
0
void AB_ImExporter_Iso8859_1ToUtf8(const char *p,
                                   int size,
                                   GWEN_BUFFER *buf) {
  while(*p) {
    unsigned int c;

    if (!size)
      break;

    c=(unsigned char)(*(p++));
    if (c<32 || c==127)
      c=32;
    if (c & 0x80) {
      GWEN_Buffer_AppendByte(buf, 0xc0 | c>>6);
      c &= ~0x40;
    }
    GWEN_Buffer_AppendByte(buf, c);
    if (size!=-1)
      size--;
  } /* while */
Beispiel #17
0
const char *GWEN_XML_FindNameSpace(GWEN_STRINGLIST2 *sl,
                                   const char *prefix,
                                   const char *name) {
  GWEN_BUFFER *nbuf;
  GWEN_STRINGLIST2_ITERATOR *it;

  nbuf=GWEN_Buffer_new(0, 32, 0, 1);
  if (prefix)
    GWEN_Buffer_AppendString(nbuf, prefix);
  GWEN_Buffer_AppendByte(nbuf, ':');
  GWEN_Buffer_AppendString(nbuf, name);

  it=GWEN_StringList2_First(sl);
  if (it) {
    const char *t;

    t=GWEN_StringList2Iterator_Data(it);
    assert(t);
    while(t) {
      const char *p;

      p=strchr(t, ':');
      assert(p);
      p++;
      if (strcasecmp(p, GWEN_Buffer_GetStart(nbuf))==0) {
        GWEN_StringList2Iterator_free(it);
        GWEN_Buffer_free(nbuf);
        return t;
      }
      t=GWEN_StringList2Iterator_Next(it);
    } /* while */
    GWEN_StringList2Iterator_free(it);
  }

  GWEN_Buffer_free(nbuf);
  return 0;
}
Beispiel #18
0
void GWEN_DBIO__XmlDb_ReadDataTags(GWEN_XMLNODE *node, GWEN_BUFFER *buf) {
  GWEN_XMLNODE *ndata;
  GWEN_BUFFER *tbuf;
  int rv;

  tbuf=GWEN_Buffer_new(0, 256, 0, 1);
  ndata=GWEN_XMLNode_GetFirstData(node);
  while(ndata) {
    const char *s;

    s=GWEN_XMLNode_GetData(ndata);
    if (s) {
      if (GWEN_Buffer_GetUsedBytes(tbuf))
	GWEN_Buffer_AppendByte(tbuf, ' ');
      GWEN_Buffer_AppendString(tbuf, s);
    }
    ndata=GWEN_XMLNode_GetNextData(node);
  }
  rv=GWEN_Text_UnescapeXmlToBuffer(GWEN_Buffer_GetStart(tbuf), buf);
  if (rv<0) {
    DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
  }
  GWEN_Buffer_free(tbuf);
}
Beispiel #19
0
int GWEN_LibLoader_OpenLibraryWithPath(GWEN_LIBLOADER *h,
                                       const char *path,
                                       const char *name)
{
  GWEN_BUFFER *buffer;
  unsigned int pos;
  unsigned int pos2;
  unsigned int i;
  int missingSoExt;
  int missingLibPrefix;
  int err;

  assert(h);
  assert(name);
  missingSoExt=0;
  missingLibPrefix=0;
  buffer=GWEN_Buffer_new(0, 256, 0, 1);

  if (path) {
    GWEN_Buffer_AppendString(buffer, path);
    GWEN_Buffer_AppendByte(buffer, '/');
  }
  /* remember current position */
  pos=GWEN_Buffer_GetPos(buffer);
  /* append name of the library to load */
  GWEN_Buffer_AppendString(buffer, name);
  i=strlen(name);

  /* check whether we have the ".so" extension */
  if ((i<=3) || (strcmp(name+i-3, ".so")!=0)) {
    /* no SO-extension, add it myself */
    missingSoExt=1;
  }

  /* check whether we have the "lib" prefix */
  if ((i<=3) || (strncmp(name, "lib", 3)!=0)) {
    /* no SO-extension, add it myself */
    missingLibPrefix=1;
  }

  /* try to load the library */
  err=GWEN_LibLoader_LoadLibrary(h, GWEN_Buffer_GetStart(buffer));
  if (!err) {
    DBG_INFO(GWEN_LOGDOMAIN, "Library \"%s\" loaded",
             GWEN_Buffer_GetStart(buffer));
    GWEN_Buffer_free(buffer);
    return 0;
  }

  /* could not load, check why */
  /*if (GWEN_Error_GetType(err)!=
      GWEN_Error_FindType(GWEN_LIBLOADER_ERROR_TYPE) ||
      GWEN_Error_GetCode(err)!=GWEN_LIBLOADER_ERROR_NOT_FOUND) {
    DBG_ERROR_ERR(GWEN_LOGDOMAIN, err);
    DBG_INFO(GWEN_LOGDOMAIN, "Could not load library \"%s\"",
             GWEN_Buffer_GetStart(buffer));
    GWEN_Buffer_free(buffer);
    return err;
  }
  */

  /* hmm, not found, try some variants */
  if (missingSoExt) {
    /* try again, this time with ".so" appended */
    pos2=GWEN_Buffer_GetPos(buffer);
    GWEN_Buffer_AppendString(buffer, ".so");
    err=GWEN_LibLoader_LoadLibrary(h, GWEN_Buffer_GetStart(buffer));
    if (!err) {
      DBG_INFO(GWEN_LOGDOMAIN, "Library \"%s\" loaded",
               GWEN_Buffer_GetStart(buffer));
      GWEN_Buffer_free(buffer);
      return 0;
    }
    GWEN_Buffer_Crop(buffer, 0, pos2);
    GWEN_Buffer_SetPos(buffer, pos2);

    /* could not load, check why */
    if (err!=GWEN_ERROR_NOT_FOUND) {
      DBG_INFO(GWEN_LOGDOMAIN, "Could not load library \"%s\"",
               GWEN_Buffer_GetStart(buffer));
      GWEN_Buffer_free(buffer);
      return err;
    }
  }

  if (missingLibPrefix) {
    GWEN_Buffer_SetPos(buffer, pos);
    /* insert "lib" */
    GWEN_Buffer_InsertString(buffer, "lib");
    /* try again */
    err=GWEN_LibLoader_LoadLibrary(h, GWEN_Buffer_GetStart(buffer));
    if (!err) {
      DBG_INFO(GWEN_LOGDOMAIN, "Library \"%s\" loaded",
               GWEN_Buffer_GetStart(buffer));
      GWEN_Buffer_free(buffer);
      return 0;
    }

    /* could not load, check why */
    if (err!=GWEN_ERROR_NOT_FOUND) {
      DBG_INFO(GWEN_LOGDOMAIN, "Could not load library \"%s\"",
               GWEN_Buffer_GetStart(buffer));
      GWEN_Buffer_free(buffer);
      return err;
    }

    /* try again, this time with ".so" AND "lib" */
    if (missingSoExt) {
      GWEN_Buffer_AppendString(buffer, ".so");
      err=GWEN_LibLoader_LoadLibrary(h, GWEN_Buffer_GetStart(buffer));
      if (!err) {
        DBG_INFO(GWEN_LOGDOMAIN, "Library \"%s\" loaded",
                 GWEN_Buffer_GetStart(buffer));
        GWEN_Buffer_free(buffer);
        return 0;
      }
    }
  }

  DBG_INFO(GWEN_LOGDOMAIN, "Library \"%s\" name (or variants) not found, giving up",
           name);
  GWEN_Buffer_free(buffer);
  return err;
}
Beispiel #20
0
int write_hp_file_c(ARGUMENTS *args, GWEN_XMLNODE *node)
{
  int rv;
  const char *f;
  GWEN_BUFFER *fname;
  GWEN_BUFFER *hbuf;
  const char *s;
  GWEN_SYNCIO *sio;

  int err;
  const char *nacc;
  const char *constAcc;
  const char *constName;
  const char *fromDbAcc;
  const char *fromDbName;
  const char *dupAcc;
  const char *dupName;
  const char *id;
  const char *prefix;

  id=get_struct_property(node, "id", 0);
  if (!id) {
    DBG_ERROR(0, "No id for struct");
    return -1;
  }

  prefix=get_struct_property(node, "prefix", 0);
  if (!prefix) {
    DBG_ERROR(0, "No prefix for struct");
    return -1;
  }

  f=get_struct_property(node, "filename", 0);
  if (!f) {
    DBG_ERROR(0, "No filename given");
    return -1;
  }

  nacc=get_struct_property(node, "access", "public");
  constAcc=get_struct_property(node, "constructor-access", nacc);
  constName=get_struct_property(node, "constructor-name", 0);
  fromDbAcc=get_struct_property(node, "fromdb-access", nacc);
  fromDbName=get_struct_property(node, "fromdb-name", 0);
  dupAcc=get_struct_property(node, "dup-access", nacc);
  dupName=get_struct_property(node, "fromdb-name", 0);

  fname=GWEN_Buffer_new(0, 256, 0, 1);
  GWEN_Buffer_AppendString(fname, f);
  GWEN_Buffer_AppendString(fname, "_p.h");

  sio=GWEN_SyncIo_File_new(GWEN_Buffer_GetStart(fname),
                           GWEN_SyncIo_File_CreationMode_CreateAlways);
  GWEN_SyncIo_AddFlags(sio,
                       GWEN_SYNCIO_FILE_FLAGS_READ |
                       GWEN_SYNCIO_FILE_FLAGS_WRITE |
                       GWEN_SYNCIO_FILE_FLAGS_UREAD |
                       GWEN_SYNCIO_FILE_FLAGS_UWRITE |
                       GWEN_SYNCIO_FILE_FLAGS_GREAD |
                       GWEN_SYNCIO_FILE_FLAGS_GWRITE);
  rv=GWEN_SyncIo_Connect(sio);
  if (rv<0) {
    DBG_ERROR(0, "open(%s): %s",
              GWEN_Buffer_GetStart(fname),
              strerror(errno));
    GWEN_Buffer_free(fname);
    GWEN_SyncIo_free(sio);
    return -1;
  }
  GWEN_Buffer_free(fname);

  /* Insert the auto-generation warning */
  GWEN_SyncIo_WriteString(sio, "/* This file is auto-generated from \"");
  GWEN_SyncIo_WriteString(sio, f);
  GWEN_SyncIo_WriteLine(sio, ".xml\" by the typemaker");
  GWEN_SyncIo_WriteLine(sio, "   tool of Gwenhywfar. ");
  GWEN_SyncIo_WriteLine(sio, "   Do not edit this file -- all changes will be lost! */");

  hbuf=GWEN_Buffer_new(0, 256, 0, 1);
  s=f;
  while (*s) {
    GWEN_Buffer_AppendByte(hbuf, toupper(*s));
    s++;
  }
  GWEN_Buffer_AppendString(hbuf, "_P_H");

  GWEN_SyncIo_WriteString(sio, "#ifndef ");
  GWEN_SyncIo_WriteLine(sio, GWEN_Buffer_GetStart(hbuf));
  GWEN_SyncIo_WriteString(sio, "#define ");
  GWEN_SyncIo_WriteLine(sio, GWEN_Buffer_GetStart(hbuf));
  GWEN_SyncIo_WriteLine(sio, "");

  if (strcasecmp(get_struct_property(node, "inherit", ""),
                 "private")==0) {
    GWEN_SyncIo_WriteLine(sio, "#include <gwenhywfar/inherit.h>");
  }

  if (strcasecmp(get_struct_property(node, "list", ""),
                 "private")==0) {
    GWEN_SyncIo_WriteLine(sio, "#include <gwenhywfar/misc.h>");
  }

  if (strcasecmp(get_struct_property(node, "list2", ""),
                 "private")==0) {
    GWEN_SyncIo_WriteLine(sio, "#include <gwenhywfar/list2.h>");
  }

  fname=GWEN_Buffer_new(0, 256, 0, 1);
  GWEN_Buffer_AppendString(fname, f);
  GWEN_Buffer_AppendString(fname, "_l.h");
  GWEN_SyncIo_WriteString(sio, "#include \"");
  GWEN_SyncIo_WriteString(sio, GWEN_Buffer_GetStart(fname));
  GWEN_SyncIo_WriteLine(sio, "\"");
  GWEN_Buffer_free(fname);

  GWEN_SyncIo_WriteLine(sio, "");

  rv=write_h_struct_c(args, node, sio);
  if (rv) {
    GWEN_Buffer_free(hbuf);
    GWEN_SyncIo_free(sio);
    return rv;
  }

  GWEN_SyncIo_WriteLine(sio, "");

  /* include private functions */
  if (strcasecmp(constAcc, "private")==0) {
    GWEN_SyncIo_WriteString(sio, id);
    GWEN_SyncIo_WriteString(sio, " *");
    GWEN_SyncIo_WriteString(sio, prefix);
    if (constName)
      GWEN_SyncIo_WriteString(sio, constName);
    else
      GWEN_SyncIo_WriteString(sio, "_new");
    GWEN_SyncIo_WriteLine(sio, "(void);");
  }

  /* FromDb */
  if (strcasecmp(fromDbAcc, "private")==0) {
    GWEN_SyncIo_WriteString(sio, id);
    GWEN_SyncIo_WriteString(sio, " *");
    GWEN_SyncIo_WriteString(sio, prefix);
    if (fromDbName)
      GWEN_SyncIo_WriteString(sio, fromDbName);
    else
      GWEN_SyncIo_WriteString(sio, "_fromDb");
    GWEN_SyncIo_WriteLine(sio, "(GWEN_DB_NODE *db);");
  }

  /* dup */
  if (strcasecmp(dupAcc, "private")==0) {
    GWEN_SyncIo_WriteString(sio, id);
    GWEN_SyncIo_WriteString(sio, " *");
    GWEN_SyncIo_WriteString(sio, prefix);
    if (dupName)
      GWEN_SyncIo_WriteString(sio, dupName);
    else
      GWEN_SyncIo_WriteString(sio, "_dup");
    GWEN_SyncIo_WriteString(sio, "(const ");
    GWEN_SyncIo_WriteString(sio, id);
    GWEN_SyncIo_WriteLine(sio, "*st);");
  }

  if (strcasecmp(nacc, "private")==0) {
    GWEN_SyncIo_WriteString(sio, "void ");
    GWEN_SyncIo_WriteString(sio, prefix);
    GWEN_SyncIo_WriteString(sio, "_free(");
    GWEN_SyncIo_WriteString(sio, id);
    GWEN_SyncIo_WriteLine(sio, " *st);");

    GWEN_SyncIo_WriteString(sio, "void ");
    GWEN_SyncIo_WriteString(sio, prefix);
    GWEN_SyncIo_WriteString(sio, "_Attach(");
    GWEN_SyncIo_WriteString(sio, id);
    GWEN_SyncIo_WriteLine(sio, " *st);");

    /* ReadDb */
    GWEN_SyncIo_WriteString(sio, "int ");
    GWEN_SyncIo_WriteString(sio, prefix);
    GWEN_SyncIo_WriteString(sio, "_ReadDb(");
    GWEN_SyncIo_WriteString(sio, id);
    GWEN_SyncIo_WriteLine(sio, " *st, GWEN_DB_NODE *db);");

    /* ToDb */
    GWEN_SyncIo_WriteString(sio, "int ");
    GWEN_SyncIo_WriteString(sio, prefix);
    GWEN_SyncIo_WriteString(sio, "_toDb(const ");
    GWEN_SyncIo_WriteString(sio, id);
    GWEN_SyncIo_WriteString(sio, "*st, GWEN_DB_NODE *db);");

    GWEN_SyncIo_WriteString(sio, "int ");
    GWEN_SyncIo_WriteString(sio, prefix);
    GWEN_SyncIo_WriteString(sio, "_IsModified(const ");
    GWEN_SyncIo_WriteString(sio, id);
    GWEN_SyncIo_WriteLine(sio, " *st);");

    GWEN_SyncIo_WriteString(sio, "void ");
    GWEN_SyncIo_WriteString(sio, prefix);
    GWEN_SyncIo_WriteString(sio, "_SetModified(");
    GWEN_SyncIo_WriteString(sio, id);
    GWEN_SyncIo_WriteLine(sio, " *st, int i);");
  }

  rv=write_h_setget_c(args, node, sio, "private");
  if (rv) {
    GWEN_Buffer_free(hbuf);
    GWEN_SyncIo_free(sio);
    return rv;
  }

  /* write trailing endif */
  err=GWEN_SyncIo_WriteLine(sio, "");
  if (err) {
    DBG_ERROR_ERR(0, err);
    GWEN_Buffer_free(hbuf);
    return -1;
  }

  err=GWEN_SyncIo_WriteString(sio, "#endif /* ");
  if (err) {
    DBG_ERROR_ERR(0, err);
    GWEN_Buffer_free(hbuf);
    return -1;
  }
  err=GWEN_SyncIo_WriteString(sio, GWEN_Buffer_GetStart(hbuf));
  if (err) {
    DBG_ERROR_ERR(0, err);
    GWEN_Buffer_free(hbuf);
    return -1;
  }
  err=GWEN_SyncIo_WriteLine(sio, " */");
  if (err) {
    DBG_ERROR_ERR(0, err);
    GWEN_Buffer_free(hbuf);
    return -1;
  }

  err=GWEN_SyncIo_Disconnect(sio);
  if (err) {
    DBG_ERROR_ERR(0, err);
    GWEN_SyncIo_free(sio);
    GWEN_Buffer_free(hbuf);
    return -1;
  }

  GWEN_Buffer_free(hbuf);
  return 0;
}
Beispiel #21
0
void *GWEN_Directory_HandlePathElement(const char *entry,
                                       void *data,
                                       unsigned int flags) {
  char *p;
  struct stat st;
  int exists;
  int withDrive;
  GWEN_BUFFER *buf;
  GWEN_BUFFER *ebuf = 0;

  withDrive=0;

#ifdef OS_WIN32
  if (entry && isalpha(*entry)) {
    int len;

    /* append backslash if entry only consists of a drive specification */
    len=strlen(entry);
    if ( (len==2) && (entry[1] == ':') ) {
      ebuf=GWEN_Buffer_new(0, len+2, 0, 1);
      GWEN_Buffer_AppendString(ebuf, entry);
      GWEN_Buffer_AppendByte(ebuf, '\\');
      withDrive=1;
      entry=GWEN_Buffer_GetStart(ebuf);
    }
  }
#endif /* OS_WIN32 */

  if (strcasecmp(entry, "..")==0) {
    DBG_ERROR(GWEN_LOGDOMAIN, "\"..\" detected");
    GWEN_Buffer_free(ebuf);
    return 0;
  }

  buf=(GWEN_BUFFER*)data;
  if (GWEN_Buffer_GetUsedBytes(buf) && !withDrive) {
    char c;

    c=GWEN_Buffer_GetStart(buf)[GWEN_Buffer_GetUsedBytes(buf)-1];
#ifdef OS_WIN32
    if (c!='\\')
      GWEN_Buffer_AppendByte(buf, '\\');
#else
    if (c!='/')
      GWEN_Buffer_AppendByte(buf, '/');
#endif /* OS_WIN32 */
  }
  GWEN_Buffer_AppendString(buf, entry);

  /* check for existence of the file/folder */
  p=GWEN_Buffer_GetStart(buf);
  DBG_VERBOUS(GWEN_LOGDOMAIN, "Checking path \"%s\"", p);
  if (stat(p, &st)) {
    exists=0;
    DBG_DEBUG(GWEN_LOGDOMAIN, "stat: %s (%s)", strerror(errno), p);
    if ((flags & GWEN_PATH_FLAGS_PATHMUSTEXIST) ||
        ((flags & GWEN_PATH_FLAGS_LAST) &&
         (flags & GWEN_PATH_FLAGS_NAMEMUSTEXIST))) {
      DBG_INFO(GWEN_LOGDOMAIN, "Path \"%s\" does not exist (it should)", p);
      GWEN_Buffer_free(ebuf);
      return 0;
    }
  }
  else {
    DBG_VERBOUS(GWEN_LOGDOMAIN, "Checking for type");
    exists=1;
    if (flags & GWEN_PATH_FLAGS_VARIABLE) {
      if (!S_ISREG(st.st_mode)) {
        DBG_INFO(GWEN_LOGDOMAIN, "%s not a regular file", p);
        GWEN_Buffer_free(ebuf);
        return 0;
      }
    }
    else {
      if (!S_ISDIR(st.st_mode)) {
        DBG_INFO(GWEN_LOGDOMAIN, "%s not a direcory", p);
        GWEN_Buffer_free(ebuf);
        return 0;
      }
    }
    if ((flags & GWEN_PATH_FLAGS_PATHMUSTNOTEXIST) ||
        ((flags & GWEN_PATH_FLAGS_LAST) &&
         (flags & GWEN_PATH_FLAGS_NAMEMUSTNOTEXIST))) {
      DBG_INFO(GWEN_LOGDOMAIN, "Path \"%s\" exists (it should not)", p);
      GWEN_Buffer_free(ebuf);
      return 0;
    }
  } /* if stat is ok */

  if (!exists) {
    int isPublic;

    DBG_DEBUG(GWEN_LOGDOMAIN, "Entry \"%s\" does not exist", p);

    isPublic=(
               ((flags & GWEN_PATH_FLAGS_LAST) &&
                (flags & GWEN_DIR_FLAGS_PUBLIC_NAME)) ||
               (!(flags & GWEN_PATH_FLAGS_LAST) &&
                (flags & GWEN_DIR_FLAGS_PUBLIC_PATH))
             );

    if (flags & GWEN_PATH_FLAGS_VARIABLE) {
      /* create file */
      int fd;

      DBG_DEBUG(GWEN_LOGDOMAIN, "Creating file \"%s\"", p);
      if (isPublic)
        fd=open(p, O_RDWR | O_CREAT | O_TRUNC,
                S_IRUSR | S_IWUSR
#ifdef S_IRGRP
                | S_IRGRP
#endif
#ifdef S_IROTH
                | S_IROTH
#endif
               );
      else
        fd=open(p, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
      if (fd==-1) {
        DBG_ERROR(GWEN_LOGDOMAIN, "open: %s (%s)", strerror(errno), p);
        GWEN_Buffer_free(ebuf);
        return 0;
      }
      close(fd);
      DBG_VERBOUS(GWEN_LOGDOMAIN, "Successfully created");
    }
    else {
      /* create dir */
      DBG_VERBOUS(GWEN_LOGDOMAIN, "Creating folder \"%s\"", p);

      if (isPublic) {
        if (GWEN_Directory_CreatePublic(p)) {
          DBG_ERROR(GWEN_LOGDOMAIN, "Could not create directory \"%s\"", p);
          GWEN_Buffer_free(ebuf);
          return 0;
        }
      }
      else {
        if (GWEN_Directory_Create(p)) {
          DBG_ERROR(GWEN_LOGDOMAIN, "Could not create directory \"%s\"", p);
          GWEN_Buffer_free(ebuf);
          return 0;
        }
      }
    }
  } /* if exists */
  else {
    DBG_VERBOUS(GWEN_LOGDOMAIN, "Entry \"%s\" exists", p);
  }
  DBG_VERBOUS(GWEN_LOGDOMAIN, "Returning this: %s", p);
  GWEN_Buffer_free(ebuf);
  return buf;
}
Beispiel #22
0
int GWEN_CryptMgr_Encrypt(GWEN_CRYPTMGR *cm, const uint8_t *pData, uint32_t lData, GWEN_BUFFER *dbuf) {
  GWEN_CRYPTHEAD *ch;
  uint32_t pos;
  uint8_t *p;
  uint32_t l;
  int rv;
  GWEN_BUFFER *cryptbuf;
  GWEN_BUFFER *tbuf;
  GWEN_CRYPT_KEY *mkey;

  assert(cm);

  /* generate a message key */
  mkey=GWEN_Crypt_KeyBlowFish_Generate(GWEN_Crypt_CryptMode_Cbc, 256/8, 2);
  if (mkey==NULL) {
    DBG_ERROR(GWEN_LOGDOMAIN, "Unable to generate BLOWFISH key");
    return GWEN_ERROR_GENERIC;
  }

  GWEN_Buffer_AppendByte(dbuf, GWEN_CRYPTMGR_TLV_ENCRYPTEDOBJECT);
  pos=GWEN_Buffer_GetPos(dbuf);
  GWEN_Buffer_AppendByte(dbuf, 0);
  GWEN_Buffer_AppendByte(dbuf, 0);

  /* prepare signature head */
  ch=GWEN_CryptHead_new();
  GWEN_CryptHead_SetKeyName(ch, cm->peerKeyName);
  GWEN_CryptHead_SetKeyNumber(ch, cm->peerKeyNumber);
  GWEN_CryptHead_SetKeyVersion(ch, cm->peerKeyVersion);
  GWEN_CryptHead_SetCryptProfile(ch, cm->signatureProfile);

  /* encrypt key */
  cryptbuf=GWEN_Buffer_new(0, lData+256, 0, 1);
  rv=GWEN_CryptMgr_EncryptKey(cm,
                              GWEN_Crypt_KeyBlowFish_GetKeyDataPtr(mkey),
                              GWEN_Crypt_KeyBlowFish_GetKeyDataLen(mkey),
                              cryptbuf);
  if (rv<0) {
    DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
    GWEN_Buffer_free(cryptbuf);
    GWEN_CryptHead_free(ch);
    GWEN_Crypt_Key_free(mkey);
    return rv;
  }
  GWEN_CryptHead_SetKey(ch,
                        (const uint8_t*)GWEN_Buffer_GetStart(cryptbuf),
                        GWEN_Buffer_GetUsedBytes(cryptbuf));
  GWEN_Buffer_free(cryptbuf);

  /* write crypt head to buffer */
  rv=GWEN_CryptHead_toBuffer(ch, dbuf, GWEN_CRYPTMGR_TLV_CRYPTHEAD);
  GWEN_CryptHead_free(ch);
  if (rv<0) {
    DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
    GWEN_Crypt_Key_free(mkey);
    return rv;
  }

  /* padd plain text data */
  tbuf=GWEN_Buffer_new(0, lData+256, 0, 1);
  GWEN_Buffer_AppendBytes(tbuf, (const char*)pData, lData);
  GWEN_Padd_PaddWithAnsiX9_23(tbuf);

  /* encrypt with message key */
  cryptbuf=GWEN_Buffer_new(0, lData+256, 0, 1);
  l=GWEN_Buffer_GetMaxUnsegmentedWrite(cryptbuf);
  rv=GWEN_Crypt_Key_Encipher(mkey,
                             (const uint8_t*)GWEN_Buffer_GetStart(tbuf),
                             GWEN_Buffer_GetUsedBytes(tbuf),
                             (uint8_t*)GWEN_Buffer_GetStart(cryptbuf),
                             &l);
  GWEN_Buffer_free(tbuf);
  if (rv<0) {
    DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
    GWEN_Buffer_free(cryptbuf);
    GWEN_Crypt_Key_free(mkey);
    return rv;
  }
  GWEN_Buffer_IncrementPos(cryptbuf, l);
  GWEN_Buffer_AdjustUsedBytes(cryptbuf);

  /* write encrypted data */
  GWEN_Tag16_DirectlyToBuffer(GWEN_CRYPTMGR_TLV_CRYPTDATA,
                              GWEN_Buffer_GetStart(cryptbuf),
                              GWEN_Buffer_GetUsedBytes(cryptbuf),
                              dbuf);
  GWEN_Buffer_free(cryptbuf);
  GWEN_Crypt_Key_free(mkey);

  /* write complete size */
  l=GWEN_Buffer_GetPos(dbuf)-pos-2;
  p=(uint8_t*)GWEN_Buffer_GetStart(dbuf)+pos;
  *(p++)=l & 0xff;
  *p=(l>>8) & 0xff;

  return 0;
}
Beispiel #23
0
int GWEN_CryptMgr_Sign(GWEN_CRYPTMGR *cm, const uint8_t *pData, uint32_t lData, GWEN_BUFFER *dbuf) {
  GWEN_SIGHEAD *sh;
  GWEN_SIGTAIL *st;
  GWEN_TIME *ti;
  uint32_t pos;
  uint32_t shPos;
  uint8_t *p;
  uint32_t l;
  int rv;
  GWEN_BUFFER *sigbuf;

  assert(cm);
  GWEN_Buffer_AppendByte(dbuf, GWEN_CRYPTMGR_TLV_SIGNEDOBJECT);
  pos=GWEN_Buffer_GetPos(dbuf);
  GWEN_Buffer_AppendByte(dbuf, 0);
  GWEN_Buffer_AppendByte(dbuf, 0);

  /* prepare signature head */
  sh=GWEN_SigHead_new();
  GWEN_SigHead_SetKeyName(sh, cm->localKeyName);
  GWEN_SigHead_SetKeyNumber(sh, cm->localKeyNumber);
  GWEN_SigHead_SetKeyVersion(sh, cm->localKeyVersion);
  ti=GWEN_CurrentTime();
  GWEN_SigHead_SetDateTime(sh, ti);
  GWEN_Time_free(ti);
  GWEN_SigHead_SetSignatureProfile(sh, cm->signatureProfile);
  GWEN_SigHead_SetSignatureNumber(sh, 1);

  /* write signature head to buffer */
  shPos=GWEN_Buffer_GetPos(dbuf);
  rv=GWEN_SigHead_toBuffer(sh, dbuf, GWEN_CRYPTMGR_TLV_SIGHEAD);
  GWEN_SigHead_free(sh);
  if (rv<0) {
    DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
    return rv;
  }

  /* write data to buffer */
  if (pData && lData)
    GWEN_Tag16_DirectlyToBuffer(GWEN_CRYPTMGR_TLV_SIGDATA,
                                (const char*)pData,
                                lData,
                                dbuf);

  /* sign data: signature head TLV + data TLV */
  sigbuf=GWEN_Buffer_new(0, 300, 0, 1);
  p=((uint8_t*)GWEN_Buffer_GetStart(dbuf))+shPos;
  l=GWEN_Buffer_GetPos(dbuf)-shPos;
  rv=GWEN_CryptMgr_SignData(cm, p, l, sigbuf);
  if (rv<0) {
    DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
    GWEN_Buffer_free(sigbuf);
    return rv;
  }

  /* create signature tail */
  st=GWEN_SigTail_new();
  GWEN_SigTail_SetSignature(st,
                            (const uint8_t*)GWEN_Buffer_GetStart(sigbuf),
                            GWEN_Buffer_GetUsedBytes(sigbuf));
  GWEN_Buffer_free(sigbuf);
  GWEN_SigTail_SetSignatureNumber(st, 1);

  /* write signature tail */
  rv=GWEN_SigTail_toBuffer(st, dbuf, GWEN_CRYPTMGR_TLV_SIGTAIL);
  GWEN_SigTail_free(st);
  if (rv<0) {
    DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
    return rv;
  }

  /* write complete size */
  l=GWEN_Buffer_GetPos(dbuf)-pos-2;
  p=(uint8_t*)GWEN_Buffer_GetStart(dbuf)+pos;
  *(p++)=l & 0xff;
  *p=(l>>8) & 0xff;

  return 0;
}
Beispiel #24
0
int AHB_DTAUS__CreateSetE(GWEN_BUFFER *dst,
                          GWEN_DB_NODE *cfg,
                          int csets,
			  AB_VALUE *sumEUR,
                          AB_VALUE *sumDEM,
                          AB_VALUE *sumBankCodes,
                          AB_VALUE *sumAccountIds){
  unsigned int i;
  char buffer[32];

  DBG_DEBUG(AQBANKING_LOGDOMAIN, "Creating E set");

  /* field 1, 2: record header */
  GWEN_Buffer_AppendString(dst, "0128E");

  /* field 3: reserved */
  for (i=0; i<5; i++) GWEN_Buffer_AppendByte(dst, ' ');

  /* field 4: number of C sets */
  snprintf(buffer, sizeof(buffer), "%07d", csets);
  if (AHB_DTAUS__AddNum(dst, 7, buffer)) {
    DBG_ERROR(AQBANKING_LOGDOMAIN, "Error writing to buffer");
    return -1;
  }

  /* field 5: sum of DEM values */
  snprintf(buffer, sizeof(buffer), "%013.0f",
	   AB_Value_GetValueAsDouble(sumDEM)*100.0);
  if (AHB_DTAUS__AddNum(dst, 13, buffer)) {
    DBG_ERROR(AQBANKING_LOGDOMAIN, "Error writing to buffer");
    return -1;
  }

  /* field 6: sum of peer account ids */
  snprintf(buffer, sizeof(buffer), "%017.0f",
	   AB_Value_GetValueAsDouble(sumAccountIds));
  if (AHB_DTAUS__AddNum(dst, 17, buffer)) {
    DBG_ERROR(AQBANKING_LOGDOMAIN, "Error writing to buffer");
    return -1;
  }

  /* field 7: sum of peer bank codes */
  snprintf(buffer, sizeof(buffer), "%017.0f",
	   AB_Value_GetValueAsDouble(sumBankCodes));
  if (AHB_DTAUS__AddNum(dst, 17, buffer)) {
    DBG_ERROR(AQBANKING_LOGDOMAIN, "Error writing to buffer");
    return -1;
  }

  /* field 8: sum of EUR values */
  snprintf(buffer, sizeof(buffer), "%013.0f",
	   AB_Value_GetValueAsDouble(sumEUR)*100.0);
  if (AHB_DTAUS__AddNum(dst, 13, buffer)) {
    DBG_ERROR(AQBANKING_LOGDOMAIN, "Error writing to buffer");
    return -1;
  }

  /* field 9: reserved */
  for (i=0; i<51; i++) GWEN_Buffer_AppendByte(dst, ' ');

  return 0;
}
Beispiel #25
0
int AHB_DTAUS__CreateSetC(GWEN_BUFFER *dst,
                          GWEN_DB_NODE *cfg,
                          GWEN_DB_NODE *xa,
			  AB_VALUE *sumEUR,
                          AB_VALUE *sumDEM,
                          AB_VALUE *sumBankCodes,
                          AB_VALUE *sumAccountIds){
  unsigned int i;
  const char *p;
  char buffer[32];
  int isDebitNote;
  int isEuro;
  unsigned int extSets;
  //unsigned int startPos;
  AB_VALUE *val;
  GWEN_STRINGLIST *purposeList;

  DBG_DEBUG(AQBANKING_LOGDOMAIN, "Creating C set");

  /* ______________________________________________________________________
   * preparations
   */
  purposeList=GWEN_StringList_new();
  /* cut purpose lines into manageable portions (max 27 chars) */
  for (i=0; ; i++) {
    int slen;
    GWEN_BUFFER *nbuf;

    p=GWEN_DB_GetCharValue(xa, "purpose", i, 0);
    if (p==NULL)
      break;
    if (i>14) {
      DBG_ERROR(AQBANKING_LOGDOMAIN, "Too many purpose lines (maxmimum is 14)");
      GWEN_StringList_free(purposeList);
      return -1;
    }

    slen=strlen(p);
    nbuf=GWEN_Buffer_new(0, slen+1, 0, 1);
    AB_ImExporter_Utf8ToDta(p, -1, nbuf);
    p=GWEN_Buffer_GetStart(nbuf);

    while(*p) {
      while(*p>0 && *p<33)
	p++;
      slen=strlen(p);
      if (slen==0)
        break;
      else if (slen>27) {
	char *ns;

	ns=(char*) malloc(28);
	assert(ns);
	memmove(ns, p, 27);
	ns[27]=0;
	/* let stringlist take over ownership of the the string */
	GWEN_StringList_AppendString(purposeList, ns, 1, 0);
	p+=27;
      }
      else {
	GWEN_StringList_AppendString(purposeList, p, 0, 0);
	break;
      }
    }
    GWEN_Buffer_free(nbuf);
  } /* for */

  //startPos=GWEN_Buffer_GetPos(dst);
  GWEN_Buffer_AllocRoom(dst, 256);

  isDebitNote=(strcasecmp(GWEN_DB_GetCharValue(cfg, "type", 0, "transfer"),
                          "debitnote")==0);
  isEuro=(strcasecmp(GWEN_DB_GetCharValue(cfg, "currency", 0, "EUR"),
                     "EUR")==0);

  /* compute number of extension sets */
  extSets=0;

  /* add purpose */
  if (GWEN_StringList_Count(purposeList))
    extSets+=GWEN_StringList_Count(purposeList)-1;

  /* add name */
  for (i=1; i<2; i++) { /* max 1 extset for local name */
    if (GWEN_DB_GetCharValue(xa, "localName", i, 0)==0)
      break;
    if (i>1) {
      DBG_ERROR(AQBANKING_LOGDOMAIN, "Too many name lines (maxmimum is 2)");
      GWEN_StringList_free(purposeList);
      return -1;
    }
    extSets++;
  } /* for */

  /* add other name */
  for (i=1; i<2; i++) { /* max 1 extset for remote name */
    if (GWEN_DB_GetCharValue(xa, "remoteName", i, 0)==0)
      break;
    if (i>1) {
      DBG_ERROR(AQBANKING_LOGDOMAIN, "Too many peer name lines (maxmimum is 2)");
      GWEN_StringList_free(purposeList);
      return -1;
    }
    extSets++;
  } /* for */

  /* check number of extension sets */
  if (extSets>15) {
    DBG_ERROR(AQBANKING_LOGDOMAIN, "Too many extension sets (%d)", extSets);
    GWEN_StringList_free(purposeList);
    return -1;
  }

  /* ______________________________________________________________________
   * actually write C set
   */

  /* field 1, 2: record header */
  snprintf(buffer, sizeof(buffer), "%04d", 187+(extSets*29));
  GWEN_Buffer_AppendString(dst, buffer);
  GWEN_Buffer_AppendByte(dst, 'C');

  /* field 3: acting bank code */
  if (AHB_DTAUS__AddNum(dst, 8, GWEN_DB_GetCharValue(cfg, "bankCode", 0, ""))) {
    DBG_ERROR(AQBANKING_LOGDOMAIN, "Error writing to buffer");
    GWEN_StringList_free(purposeList);
    return -1;
  }

  /* field 4: destination bank code */
  p=GWEN_DB_GetCharValue(xa, "remoteBankCode", 0, 0);
  if (p) {
    val=AB_Value_fromString(p);
    if (val==NULL) {
      DBG_ERROR(AQBANKING_LOGDOMAIN, "Bad bank code");
      GWEN_StringList_free(purposeList);
      return -1;
    }
    AB_Value_AddValue(sumBankCodes, val);
    AB_Value_free(val);
    if (AHB_DTAUS__AddNum(dst, 8, p)) {
      DBG_ERROR(AQBANKING_LOGDOMAIN, "Error writing to buffer");
      GWEN_StringList_free(purposeList);
      return -1;
    }
  }
  else {
    DBG_ERROR(AQBANKING_LOGDOMAIN, "Peer bank code missing");
    GWEN_StringList_free(purposeList);
    return -1;
  }

  /* field 5: destination account id */
  p=GWEN_DB_GetCharValue(xa, "remoteAccountNumber", 0, 0);
  if (p) {
    val=AB_Value_fromString(p);
    if (val==NULL) {
      DBG_ERROR(AQBANKING_LOGDOMAIN, "Bad account id");
      GWEN_StringList_free(purposeList);
      return -1;
    }
    AB_Value_AddValue(sumAccountIds, val);
    AB_Value_free(val);
    if (AHB_DTAUS__AddNum(dst, 10, p)) {
      DBG_ERROR(AQBANKING_LOGDOMAIN, "Error writing to buffer");
      GWEN_StringList_free(purposeList);
      return -1;
    }
  }
  else {
    DBG_ERROR(AQBANKING_LOGDOMAIN, "Peer account id missing");
    GWEN_StringList_free(purposeList);
    return -1;
  }

  /* field 6: internal customer number (0s for now) */
  for (i=0; i<13; i++) GWEN_Buffer_AppendByte(dst, '0');

  /* field 7a: text key */
  snprintf(buffer, sizeof(buffer), "%02d", GWEN_DB_GetIntValue(xa, "textkey", 0, isDebitNote?5:51));
  if (AHB_DTAUS__AddNum(dst, 2, buffer)) {
    DBG_ERROR(AQBANKING_LOGDOMAIN, "Error writing to buffer");
    GWEN_StringList_free(purposeList);
    return -1;
  }

  /* field 7b: text key extension */
  snprintf(buffer, sizeof(buffer), "%03d", GWEN_DB_GetIntValue(xa, "textkeyext", 0, 0));
  if (AHB_DTAUS__AddNum(dst, 3, buffer)) {
    DBG_ERROR(AQBANKING_LOGDOMAIN, "Error writing to buffer");
    GWEN_StringList_free(purposeList);
    return -1;
  }

  /* field 8: bank internal field */
  GWEN_Buffer_AppendByte(dst, ' ');

  /* field 9: value in DEM */
  if (!isEuro) {
    val=AB_Value_fromString(GWEN_DB_GetCharValue(xa, "value/value", 0, "0,0"));
    if (val==NULL || AB_Value_IsZero(val)) {
      AB_Value_free(val);
      DBG_ERROR(AQBANKING_LOGDOMAIN, "Bad DEM value:");
      GWEN_StringList_free(purposeList);
      return -1;
    }
    AB_Value_AddValue(sumDEM, val);
    snprintf(buffer, sizeof(buffer), "%011.0f", AB_Value_GetValueAsDouble(val)*100.0);
    AB_Value_free(val);
    if (AHB_DTAUS__AddNum(dst, 11, buffer)) {
      DBG_ERROR(AQBANKING_LOGDOMAIN, "Error writing to buffer");
      GWEN_StringList_free(purposeList);
      return -1;
    }
  }
  else {
    if (AHB_DTAUS__AddNum(dst, 11, "0")) {
      DBG_ERROR(AQBANKING_LOGDOMAIN, "Error writing to buffer");
      GWEN_StringList_free(purposeList);
      return -1;
    }
  }

  /* field 10: local bank code */
  p=GWEN_DB_GetCharValue(xa, "localbankCode", 0, 0);
  if (!p)
    p=GWEN_DB_GetCharValue(cfg, "bankCode", 0, 0);
  if (!p) {
    DBG_ERROR(AQBANKING_LOGDOMAIN, "No local bank code");
    GWEN_StringList_free(purposeList);
    return -1;
  }
  if (AHB_DTAUS__AddNum(dst, 8, p)) {
    DBG_ERROR(AQBANKING_LOGDOMAIN, "Error writing to buffer");
    GWEN_StringList_free(purposeList);
    return -1;
  }

  /* field 11: local account id */
  p=GWEN_DB_GetCharValue(xa, "localAccountNumber", 0, 0);
  if (!p)
    GWEN_DB_GetCharValue(cfg, "accountId", 0, 0);
  if (!p) {
    DBG_ERROR(AQBANKING_LOGDOMAIN, "No local account number");
    GWEN_StringList_free(purposeList);
    return -1;
  }
  if (AHB_DTAUS__AddNum(dst, 10, p)) {
    DBG_ERROR(AQBANKING_LOGDOMAIN, "Error writing to buffer");
    GWEN_StringList_free(purposeList);
    return -1;
  }

  /* field 12: value in EUR */
  if (isEuro) {
    val=AB_Value_fromString(GWEN_DB_GetCharValue(xa, "value/value", 0, "0,0"));
    if (val==NULL || AB_Value_IsZero(val)) {
      AB_Value_free(val);
      DBG_ERROR(AQBANKING_LOGDOMAIN, "Bad EUR value:");
      GWEN_StringList_free(purposeList);
      return -1;
    }
    AB_Value_AddValue(sumEUR, val);
    snprintf(buffer, sizeof(buffer), "%011.0f", AB_Value_GetValueAsDouble(val)*100.0);
    AB_Value_free(val);
    if (AHB_DTAUS__AddNum(dst, 11, buffer)) {
      DBG_ERROR(AQBANKING_LOGDOMAIN, "Error writing to buffer");
      GWEN_StringList_free(purposeList);
      return -1;
    }
  }
  else {
    if (AHB_DTAUS__AddNum(dst, 11, "0")) {
      DBG_ERROR(AQBANKING_LOGDOMAIN, "Error writing to buffer");
      GWEN_StringList_free(purposeList);
      return -1;
    }
  }

  /* field 13: blanks */
  for (i=0; i<3; i++) GWEN_Buffer_AppendByte(dst, ' ');

  /* field 14a: peer name */
  if (AHB_DTAUS__AddWord(dst, 27, GWEN_DB_GetCharValue(xa, "remoteName", 0, ""))) {
    DBG_ERROR(AQBANKING_LOGDOMAIN, "Error writing to buffer");
    GWEN_StringList_free(purposeList);
    return -1;
  }

  /* field 14b: blanks */
  for (i=0; i<8; i++) GWEN_Buffer_AppendByte(dst, ' ');

  /* field 15: name */
  if (AHB_DTAUS__AddWord(dst, 27, GWEN_DB_GetCharValue(xa, "localname", 0, ""))) {
    DBG_ERROR(AQBANKING_LOGDOMAIN, "Error writing to buffer");
    GWEN_StringList_free(purposeList);
    return -1;
  }

  /* field 16: purpose */
  p=GWEN_StringList_FirstString(purposeList);
  if (p==NULL)
    p="";
  if (AHB_DTAUS__AddWord(dst, 27, p)) {
    DBG_ERROR(AQBANKING_LOGDOMAIN, "Error writing to buffer");
    GWEN_StringList_free(purposeList);
    return -1;
  }

  /* field 17a: currency */
  if (isEuro)
    GWEN_Buffer_AppendByte(dst, '1');
  else
    GWEN_Buffer_AppendByte(dst, ' ');

  /* field 17b: blanks */
  for (i=0; i<2; i++) GWEN_Buffer_AppendByte(dst, ' ');

  /* field 18: number of extension sets */
  snprintf(buffer, sizeof(buffer), "%02d", extSets);
  if (AHB_DTAUS__AddNum(dst, 2, buffer)) {
    DBG_ERROR(AQBANKING_LOGDOMAIN, "Error writing to buffer");
    GWEN_StringList_free(purposeList);
    return -1;
  }

  if (extSets) {
    unsigned int writtenExtSets=0;

    /* now append extension sets */

    /* add peer name lines */
    for (i=1; i<2; i++) { /* max: 1 extset */
      unsigned int j;

      p=GWEN_DB_GetCharValue(xa, "remoteName", i, 0);
      if (!p)
	break;

      /* append extension set */
      GWEN_Buffer_AppendString(dst, "01");
      if (AHB_DTAUS__AddWord(dst, 27, p)) {
	DBG_ERROR(AQBANKING_LOGDOMAIN, "Error writing to buffer");
	GWEN_StringList_free(purposeList);
	return -1;
      }
      writtenExtSets++;

      if (writtenExtSets==2)
	/* 2 ext sets written, so we need to align "C 2.Satzabschnitt" to 128 now */
	for (j=0; j<11; j++) GWEN_Buffer_AppendByte(dst, ' ');
      else if (writtenExtSets>2 && ((writtenExtSets-2) % 4)==0)
	/* "C 3-5.Satzabschnitt" complete, align to 128 bytes */
	for (j=0; j<12; j++) GWEN_Buffer_AppendByte(dst, ' ');
    } /* for */

    /* add purpose lines */
    for (i=1; i<GWEN_StringList_Count(purposeList); i++) {
      unsigned int j;

      p=GWEN_StringList_StringAt(purposeList, i);
      if (!p)
	break;

      /* append extension set */
      GWEN_Buffer_AppendString(dst, "02");
      /* strings in the list are already in DTA charset */
      if (AHB_DTAUS__AddDtaWord(dst, 27, p)) {
	DBG_ERROR(AQBANKING_LOGDOMAIN, "Error writing to buffer");
	GWEN_StringList_free(purposeList);
	return -1;
      }
      writtenExtSets++;

      if (writtenExtSets==2)
	/* 2 ext sets written, so we need to align "C 2.Satzabschnitt" to 128 now */
	for (j=0; j<11; j++) GWEN_Buffer_AppendByte(dst, ' ');
      else if (writtenExtSets>2 && ((writtenExtSets-2) % 4)==0)
	/* "C 3-5.Satzabschnitt" complete, align to 128 bytes */
	for (j=0; j<12; j++) GWEN_Buffer_AppendByte(dst, ' ');
    } /* for */

    /* add name lines */
    for (i=1; i<2; i++) { /* max: 1 extset */
      unsigned int j;

      p=GWEN_DB_GetCharValue(xa, "localname", i, 0);
      if (!p)
	break;

      /* append extension set */
      GWEN_Buffer_AppendString(dst, "03");
      if (AHB_DTAUS__AddWord(dst, 27, p)) {
	DBG_ERROR(AQBANKING_LOGDOMAIN, "Error writing to buffer");
	GWEN_StringList_free(purposeList);
	return -1;
      }
      writtenExtSets++;

      if (writtenExtSets==2)
	/* 2 ext sets written, so we need to align "C 2.Satzabschnitt" to 128 now */
	for (j=0; j<11; j++) GWEN_Buffer_AppendByte(dst, ' ');
      else if (writtenExtSets>2 && ((writtenExtSets-2) % 4)==0)
	/* "C 3-5.Satzabschnitt" complete, align to 128 bytes */
	for (j=0; j<12; j++) GWEN_Buffer_AppendByte(dst, ' ');
    } /* for */
  }

  i=((GWEN_Buffer_GetUsedBytes(dst)+127) & ~127)-GWEN_Buffer_GetUsedBytes(dst);
  while(i--)
    GWEN_Buffer_AppendByte(dst, ' ');

  GWEN_StringList_free(purposeList);
  return 0;
}
Beispiel #26
0
int AHB_DTAUS__CreateSetA(GWEN_BUFFER *dst,
                          GWEN_DB_NODE *cfg) {
  unsigned int i;
  GWEN_TIME *gt;
  char buffer[16];
  int day, month, year;
  const char *p;
  GWEN_DB_NODE *dbT;

  DBG_DEBUG(AQBANKING_LOGDOMAIN, "Creating A set");
  /* field 1, 2: record header */
  GWEN_Buffer_AppendString(dst, "0128A");

  /* field 3: type */
  p=GWEN_DB_GetCharValue(cfg, "type", 0, "transfer");
  if (strcasecmp(p, "transfer")==0 ||
      strcasecmp(p, "transaction")==0)
    GWEN_Buffer_AppendString(dst, "GK");
  else if (strcasecmp(p, "debitnote")==0)
    GWEN_Buffer_AppendString(dst, "LK");
  else {
    DBG_ERROR(AQBANKING_LOGDOMAIN, "Unknown group \"%s\"", GWEN_DB_GroupName(cfg));
    return -1;
  }

  /* field 4: bank code */
  if (AHB_DTAUS__AddNum(dst, 8,
                          GWEN_DB_GetCharValue(cfg,
                                               "bankCode",
                                               0, "0"))) {
    DBG_ERROR(AQBANKING_LOGDOMAIN, "Error writing bankCode to buffer");
    return -1;
  }

  /* field 5: 0s */
  for (i=0; i<8; i++) GWEN_Buffer_AppendByte(dst, '0');

  /* field 6: sender name */
  if (AHB_DTAUS__AddWord(dst, 27,
                         GWEN_DB_GetCharValue(cfg,
                                              "name",
                                              0, "0"))) {
    DBG_ERROR(AQBANKING_LOGDOMAIN, "Error writing name to buffer");
    return -1;
  }

  /* field 7: date */
  gt=GWEN_CurrentTime();
  if (GWEN_Time_GetBrokenDownDate(gt, &day, &month, &year)) {
    DBG_ERROR(AQBANKING_LOGDOMAIN, "Unable to break down date");
    GWEN_Time_free(gt);
    return -1;
  }
  GWEN_Time_free(gt);
  snprintf(buffer, sizeof(buffer), "%02d%02d%02d", day, month+1, year%100);
  if (AHB_DTAUS__AddWord(dst, 6, buffer)) {
    DBG_ERROR(AQBANKING_LOGDOMAIN, "Error writing to buffer");
    return -1;
  }

  /* field 8: blanks */
  for (i=0; i<4; i++) GWEN_Buffer_AppendByte(dst, ' ');

  /* field 9: account id */
  if (AHB_DTAUS__AddNum(dst, 10,
                        GWEN_DB_GetCharValue(cfg,
                                             "accountid",
                                             0, ""))) {
    DBG_ERROR(AQBANKING_LOGDOMAIN, "Error writing accountid to buffer");
    return -1;
  }

  /* field 10: customer reference */
  if (AHB_DTAUS__AddNum(dst, 10,
                        GWEN_DB_GetCharValue(cfg,
                                             "custref",
                                             0, "0"))) {
    DBG_ERROR(AQBANKING_LOGDOMAIN, "Error writing custref to buffer");
    return -1;
  }

  /* field 11a: blanks */
  for (i=0; i<15; i++) GWEN_Buffer_AppendByte(dst, ' ');

  /* field 11b: date of execution */
  dbT=GWEN_DB_GetGroup(cfg, GWEN_PATH_FLAGS_NAMEMUSTEXIST, "execdate");
  if (dbT) {
    GWEN_TIME *ti;

    ti=GWEN_Time_fromDb(dbT);
    if (!ti) {
      DBG_ERROR(AQBANKING_LOGDOMAIN, "Bad execution date");
      return -1;
    }

    if (GWEN_Time_GetBrokenDownDate(ti, &day, &month, &year)) {
      DBG_ERROR(AQBANKING_LOGDOMAIN, "Bad execution date");
      GWEN_Time_free(ti);
      return -1;
    }
    snprintf(buffer, sizeof(buffer), "%02d%02d%04d", day, month+1, year);

    if (AHB_DTAUS__AddWord(dst, 8, buffer)) {
      DBG_ERROR(AQBANKING_LOGDOMAIN, "Error writing execdate to buffer");
      GWEN_Time_free(ti);
      return -1;
    }
    GWEN_Time_free(ti);
  }
  else {
    if (AHB_DTAUS__AddWord(dst, 8, "")) {
      DBG_ERROR(AQBANKING_LOGDOMAIN, "Error writing to buffer");
      return -1;
    }
  }

  /* field 11c: blanks */
  for (i=0; i<24; i++) GWEN_Buffer_AppendByte(dst, ' ');

  /* field 12: currency */
  p=GWEN_DB_GetCharValue(cfg, "currency", 0, "EUR");
  if (strcasecmp(p, "EUR")==0)
    GWEN_Buffer_AppendByte(dst, '1');
  else if (strcasecmp(p, "DEM")==0)
    GWEN_Buffer_AppendByte(dst, ' ');
  else {
    DBG_ERROR(AQBANKING_LOGDOMAIN, "Unknown currency \"%s\"", p);
    return -1;
  }

  return 0;
}
Beispiel #27
0
void AB_ImExporter_Utf8ToDta(const char *p,
                             int size,
                             GWEN_BUFFER *buf) {
  while(*p) {
    unsigned int c;

    if (!size)
      break;

    c=(unsigned char)(*(p++));
    if (size!=-1)
      size--;
    switch(c & 0xc0) {
    case 0xc0:
      if (!size) {
        DBG_ERROR(AQBANKING_LOGDOMAIN, "Incomplete UTF-8 sequence");
        c=' ';
        break;
      }
      c=(unsigned char)(*(p++));
      if (size!=-1)
        size--;
      if ((c & 0xc0) != 0x80) {
        DBG_ERROR(AQBANKING_LOGDOMAIN, "Invalid UTF-8 sequence");
        c=' ';
        break;
      }
      if (size && (*p & 0xc0) == 0x80)
        /* a sequence of 3 bytes and more cannot be translated to DTA */
        goto nextUtf8;
      switch(c) {
      case 0x84:
      case 0xa4: c=0x5b; break;
      case 0x96:
      case 0xb6: c=0x5c; break;
      case 0x9c:
      case 0xbc: c=0x5d; break;
      case 0x9f: c=0x7e; break;
      default:   c=' '; break;
      } /* switch */
      break;

    case 0x80:
      DBG_ERROR(AQBANKING_LOGDOMAIN, "Invalid UTF-8 sequence");
    nextUtf8:
      c=' ';
      while(size && (*p & 0xc0) == 0x80) {
        p++;
        if (size!=-1)
          size--;
      }
      break;

    default:
      c=toupper(c);
      if (!(isdigit(c) ||
	    (c>='A' && c<='Z') ||
	    (strchr(" .,&-+*%/$", c))))
        c=' ';
    } /* switch */
    GWEN_Buffer_AppendByte(buf, c);
  } /* while */
}
Beispiel #28
0
int GWEN_XMLNode__CheckAndSetNameSpace(GWEN_XMLNODE *n,
                                       const char *prefix,
                                       const char *nspace) {
  GWEN_XMLPROPERTY *pr;
  const char *p;
  int inUse;

  inUse=0;
  /* check current tag for prefix */
  if (prefix) {
    p=strchr(n->data, ':');
    if (p) {
      if (strncasecmp(n->data, prefix, p-n->data)==0) {
        DBG_DEBUG(GWEN_LOGDOMAIN, "Prefix \"%s\" used in tag \"%s\"",
                  prefix, n->data);
        inUse=1;
      }
    }

    if (!inUse) {
      /* check all attributes for prefixes */
      pr=n->properties;
      while(pr) {
        p=strchr(pr->name, ':');
        if (p) {
          if (strncasecmp(pr->name, prefix, p-pr->name)==0) {
            DBG_DEBUG(GWEN_LOGDOMAIN,
                      "Prefix \"%s\" used in attribute \"%s\" of tag \"%s\"",
                      prefix, pr->name, n->data);
            inUse=1;
            break;
          }
          else {
            DBG_DEBUG(GWEN_LOGDOMAIN,
                      "Prefix \"%s\" not used in attribute \"%s\" of tag \"%s\"",
                      prefix, pr->name, n->data);
          }
        }
        pr=pr->next;
      } /* while */
    }
  } /* if prefix */
  else {
    /* no prefix, check whether the current element hasn't any */
    p=strchr(n->data, ':');
    if (!p) {
      /* current tag has no prefix, check whether we have a namespace
       * declaration here */
      if (GWEN_XMLNode_GetProperty(n, "xmlns", 0)==0) {
        /* no, so the current namespace from above is used */
        DBG_DEBUG(GWEN_LOGDOMAIN,
                  "No prefix, current namespace is used");
        inUse=1;
      }
    }
  } /* if no prefix */

  if (inUse) {
    GWEN_BUFFER *nbuf;

    nbuf=GWEN_Buffer_new(0, 32, 0, 1);
    GWEN_Buffer_AppendString(nbuf, "xmlns");
    if (prefix) {
      GWEN_Buffer_AppendByte(nbuf, ':');
      GWEN_Buffer_AppendString(nbuf, prefix);
    }
    GWEN_XMLNode__SetProperty(n, GWEN_Buffer_GetStart(nbuf), nspace, 1);
    GWEN_Buffer_free(nbuf);
    return 1;
  }

  return 0;
}
Beispiel #29
0
int EBC_Provider_EuSign_A005(AB_PROVIDER *pro,
                             AB_USER *u,
                             const char *requestType,
                             const uint8_t *pMsg,
                             uint32_t lMsg,
                             GWEN_BUFFER *sbuf)
{
  EBC_PROVIDER *dp;
  GWEN_BUFFER *xbuf;
  GWEN_BUFFER *hbuf;
  GWEN_CRYPT_TOKEN *ct;
  const GWEN_CRYPT_TOKEN_CONTEXT *ctx;
  const GWEN_CRYPT_TOKEN_KEYINFO *ki;
  uint32_t keyId;
  int ksize;
  uint32_t l;
  GWEN_CRYPT_PADDALGO *algo;
  int rv;
  int numPaddBytes=0;
  const uint8_t digestInfo[]= {
    0x30, 0x31, 0x30, 0x0d,
    0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01,
    0x05, 0x00,
    0x04, 0x20
  };

  assert(pro);
  dp=GWEN_INHERIT_GETDATA(AB_PROVIDER, EBC_PROVIDER, pro);
  assert(dp);

  /* get crypt token and context */
  rv=EBC_Provider_MountToken(pro, u, &ct, &ctx);
  if (rv<0) {
    DBG_INFO(AQEBICS_LOGDOMAIN, "here (%d)", rv);
    return rv;
  }

  /* get key id */
  keyId=GWEN_Crypt_Token_Context_GetSignKeyId(ctx);
  ki=GWEN_Crypt_Token_GetKeyInfo(ct,
                                 keyId,
                                 0xffffffff,
                                 0);
  if (ki==NULL) {
    DBG_INFO(AQEBICS_LOGDOMAIN,
             "Keyinfo %04x not found on crypt token [%s:%s]",
             keyId,
             GWEN_Crypt_Token_GetTypeName(ct),
             GWEN_Crypt_Token_GetTokenName(ct));
    GWEN_Crypt_Token_Close(ct, 0, 0);
    return GWEN_ERROR_NOT_FOUND;
  }

  xbuf=GWEN_Buffer_new(0, 40, 0, 1);
  EBC_Provider_Sha256(pMsg, lMsg, xbuf);

  /* add digestInfo to hash of SignedInfo */
  hbuf=GWEN_Buffer_new(0, 256, 0, 1);
  ksize=GWEN_Crypt_Token_KeyInfo_GetKeySize(ki);

  GWEN_Buffer_AppendByte(hbuf, 0x01); /* block type */

  numPaddBytes=ksize-3-sizeof(digestInfo)-GWEN_Buffer_GetUsedBytes(xbuf);
  if (numPaddBytes<1) {
    DBG_ERROR(AQEBICS_LOGDOMAIN, "Invalid number of padd bytes, key too small (%d)", numPaddBytes);
    GWEN_Buffer_free(xbuf);
    GWEN_Buffer_free(hbuf);
    return GWEN_ERROR_INTERNAL;
  }
  GWEN_Buffer_FillWithBytes(hbuf, 0xff, numPaddBytes);

  GWEN_Buffer_AppendByte(hbuf, 0x01); /* separator */

  GWEN_Buffer_AppendBytes(hbuf, (const char *)digestInfo, sizeof(digestInfo)); /* digest info */
  GWEN_Buffer_AppendBytes(hbuf, GWEN_Buffer_GetStart(xbuf), GWEN_Buffer_GetUsedBytes(xbuf)); /* hash */
  GWEN_Buffer_free(xbuf);

  /* select padd algo */
  algo=GWEN_Crypt_PaddAlgo_new(GWEN_Crypt_PaddAlgoId_None);
  GWEN_Crypt_PaddAlgo_SetPaddSize(algo, ksize);

  /* actually sign */
  GWEN_Buffer_AllocRoom(sbuf, ksize+16);
  l=GWEN_Buffer_GetMaxUnsegmentedWrite(sbuf);
  rv=GWEN_Crypt_Token_Sign(ct, keyId,
                           algo,
                           (const uint8_t *)GWEN_Buffer_GetStart(hbuf),
                           GWEN_Buffer_GetUsedBytes(hbuf),
                           (uint8_t *)GWEN_Buffer_GetPosPointer(sbuf),
                           &l,
                           NULL, /* ignore seq counter */
                           0);
  GWEN_Crypt_PaddAlgo_free(algo);
  if (rv<0) {
    DBG_INFO(AQEBICS_LOGDOMAIN, "here (%d)", rv);
    GWEN_Buffer_free(hbuf);
    return rv;
  }
  GWEN_Buffer_IncrementPos(sbuf, l);
  GWEN_Buffer_AdjustUsedBytes(sbuf);

  GWEN_Buffer_free(hbuf);

  return 0;
}
Beispiel #30
0
int GWEN_Logger__CreateMessage(GWEN_LOGGER *lg,
                               GWEN_LOGGER_LEVEL priority, const char *s,
                               GWEN_BUFFER *mbuf) {
#ifdef HAVE_SNPRINTF
  unsigned int i;
#endif /* HAVE_SNPRINTF */
#ifdef HAVE_TIME_H
  struct tm *t;
  time_t tt;
#endif /* HAVE_TIME_H */
  char buffer[256];

  assert(lg);
  if (lg->logIdent) {
    if (strlen(lg->logIdent)+32>=sizeof(buffer)) {
      fprintf(stderr," LOGGER: Logbuffer too small (1).\n");
      return 1;
    }
  }

#ifdef HAVE_TIME_H
  tt=time(0);
  t=localtime(&tt);

# ifdef HAVE_SNPRINTF
#  ifdef HAVE_GETPID
  i=snprintf(buffer, sizeof(buffer)-1,
             "%d:%04d/%02d/%02d %02d-%02d-%02d:%s(%d):",priority,
             t->tm_year+1900, t->tm_mon+1, t->tm_mday,
             t->tm_hour, t->tm_min, t->tm_sec,
             lg->logIdent, (int)getpid());
#  else
  i=snprintf(buffer, sizeof(buffer)-1,
             "%d:%04d/%02d/%02d %02d-%02d-%02d:%s:",priority,
             t->tm_year+1900, t->tm_mon+1, t->tm_mday,
             t->tm_hour, t->tm_min, t->tm_sec,
             lg->logIdent);
#  endif /* HAVE_GETPID */
  if (i>=sizeof(buffer)) {
    fprintf(stderr," LOGGER: Logbuffer too small (2).\n");
    return 1;
  }
# else   /* HAVE_SNPRINTF */
#  ifdef HAVE_GETPID
  sprintf(buffer,"%d:%04d/%02d/%02d %02d-%02d-%02d:%s(%d):",priority,
          t->tm_year+1900, t->tm_mon+1, t->tm_mday,
          t->tm_hour, t->tm_min, t->tm_sec,
          lg->logIdent, (int)getpid());
#  else
  sprintf(buffer,"%d:%04d/%02d/%02d %02d-%02d-%02d:%s:",priority,
          t->tm_year+1900, t->tm_mon+1, t->tm_mday,
          t->tm_hour, t->tm_min, t->tm_sec,
          lg->logIdent);
#  endif /* HAVE_GETPID */
# endif  /* HAVE_SNPRINTF */
#else    /* HAVE_TIME_H */
# ifdef HAVE_SNPRINTF
  buffer[sizeof(buffer)-1]=0;
  i=snprintf(buffer, sizeof(buffer)-1,
             "%d:%s:",priority,
             lg->logIdent);
  if (i>=sizeof(buffer)) {
    fprintf(stderr," LOGGER: Logbuffer too small (3).\n");
    return 1;
  }
# else   /* HAVE_SNPRINTF */
  sprintf(buffer,"%d:%s:",priority,
          lg->logIdent);
# endif  /* HAVE_SNPRINTF */
#endif   /* HAVE_TIME_H */
  GWEN_Buffer_AppendString(mbuf, buffer);
  GWEN_Buffer_AppendString(mbuf, s);
  GWEN_Buffer_AppendByte(mbuf, '\n');
  return 0;
}