// File: /workspace/github_c_makefile_star2_O3/sonicse[P]dbf2mysql/dbf.c
// Function: dbf_open
dbhead *dbf_open(char *file, int flags)
{
    int			file_no;
    dbhead		*dbh;
    f_descr		*fields;
    dbf_header		*head;
    dbf_field		*fieldc;
    int			t;
    int			i;
    char		*sp;
    int			memofound;
    dbf_memo_header	*dbmh;

    if ((dbh = (dbhead *)calloc(1, sizeof(dbhead))) == NULL) {
	return (dbhead *)DBF_ERROR;
    }

    if ((head = (dbf_header *)malloc(sizeof(dbf_header))) == NULL) {
	free(dbh);
	return (dbhead *)DBF_ERROR;
    }

    if ((fieldc = (dbf_field *)malloc(sizeof(dbf_field))) == NULL) {
	free(head);
	free(dbh);
	return (dbhead *)DBF_ERROR;
    }

    if ((file_no = open(file, flags)) == -1) {
	free(fieldc);
	free(head);
	free(dbh);
	return (dbhead *)DBF_ERROR;
    }

/* read in the disk-header */

    if (read(file_no, head, sizeof(dbf_header)) == -1) {
	close(file_no);
	free(fieldc);
	free(head);
	free(dbh);
	return (dbhead *)DBF_ERROR;
    }

    for (t = 0; t < sizeof(valid_dbf_types)/sizeof(valid_dbf_types[0]); t++)
	if (valid_dbf_types[t].sig == head->dbh_dbt)
	    break;

    if (t == sizeof(valid_dbf_types)/sizeof(valid_dbf_types[0])) {
	close(file_no);
	fprintf(stderr,"Header value %02x not valid\n", head->dbh_dbt);
	free(fieldc);
	free(head);
	free(dbh);
	return (dbhead *)DBF_ERROR;
    }

    dbh->db_description = valid_dbf_types[t].description;

    if (valid_dbf_types[t].hasMemo != DBF_MTYPE_NONE) {
	dbh->db_memo = valid_dbf_types[t].hasMemo;
    } else {
	dbh->db_memo = 0;
    }
    dbh->db_fd = file_no;
    dbh->db_year = head->dbh_year;
    dbh->db_month = head->dbh_month;
    dbh->db_day = head->dbh_day;
    dbh->db_hlen = get_short((u_char *)&head->dbh_hlen);
    dbh->db_records = get_long((u_char *)&head->dbh_records);
    dbh->db_currec = 0;
    dbh->db_rlen = get_short((u_char *)&head->dbh_rlen);
    dbh->db_nfields = (dbh->db_hlen - sizeof(dbf_header)) / sizeof(dbf_field);

    if ((dbh->db_buff = (u_char *)malloc(dbh->db_rlen)) == NULL) {
	close(file_no);
	free(fieldc);
	free(head);
	free(dbh);
	return (dbhead *)DBF_ERROR;
    }

    /* dbh->db_hlen - sizeof(dbf_header) isn't the
       correct size, cos dbh->hlen is in fact
       a little more cos of the 0x0D (and
       possibly another byte, 0x4E, I have
       seen this somewhere). Because of rounding
       everything turns out right :) */

    if ((fields = (f_descr *)calloc(dbh->db_nfields, sizeof(f_descr))) == NULL)
    {
	close(file_no);
	free(fieldc);
	free(head);
	free(dbh);
	return (dbhead *)DBF_ERROR;
    }

    memofound = 0;
    for (t = 0; t < dbh->db_nfields; t++)
    {
	read(file_no, fieldc, sizeof(dbf_field));
	if (fieldc->dbf_type == 0x00)
	{
	    dbh->db_nfields = t;
	    break;
	}
	strncpy(fields[t].db_name, fieldc->dbf_name, DBF_NAMELEN);
	fields[t].db_type = fieldc->dbf_type;	
	fields[t].db_flen = fieldc->dbf_flen;
	fields[t].db_dec  = fieldc->dbf_dec;
	fields[t].db_idx  = fieldc->dbf_isIndexed;
	if (fields[t].db_type == 'M')
	    memofound++;
    }

    dbh->db_offset = dbh->db_hlen;
    dbh->db_fields = fields;

    free(fieldc);
    free(head);

    if (memofound == 0)
    {
	dbh->db_memo = DBF_MTYPE_NONE;
    }
    else
    {
	dbh->mb_blksiz = DBF_DBT_BLOCK_SIZE;
	dbh->mb_bufsiz = dbh->mb_blksiz * 2;
	if ((dbh->mb_buffer = malloc(dbh->mb_bufsiz)) == NULL)
	{
	    perror("malloc");
	    dbf_close(&dbh);
	    return (dbhead *)DBF_ERROR;
	}
	dbmh = (dbf_memo_header *)dbh->mb_buffer;
	sp = strdup(file);
	i = strlen(sp)-1;
	switch (dbh->db_memo)
	{
	  case DBF_MTYPE_FPT:
	    if (isupper(sp[i]))
		strcpy(&sp[i-2],"FPT");
	    else
		strcpy(&sp[i-2],"fpt");
	    if ((dbh->db_memofd = open(sp, flags)) == -1)
	    {
		if (isupper(sp[i]))
		    strcpy(&sp[i-2],"fpt");
		else
		    strcpy(&sp[i-2],"FPT");
		if ((dbh->db_memofd = open(sp, flags)) == -1)
		{
		    char	msg[256];
		    int	saverrno = errno;

		    if (isupper(sp[i]))
			strcpy(&sp[i-2],"fpt");
		    else
			strcpy(&sp[i-2],"FPT");
		    snprintf(msg, sizeof(msg),
			     "couldn't find required memo data file: %s - (%d)",
			     sp,
			     saverrno);
		    msg[sizeof(msg)-1] = 0;
		    errno = saverrno;
		    perror(msg);
		    dbf_close(&dbh);
		    free(sp);
		    return (dbhead *)DBF_ERROR;
		}
	    }
	    if (read(dbh->db_memofd, dbh->mb_buffer, sizeof(dbf_memo_header)) != sizeof(dbf_memo_header))
	    {
		perror("memo file read");
		dbf_close(&dbh);
		free(sp);
		return (dbhead *)DBF_ERROR;
	    }
	    dbh->mb_next_blk = (dbmh->fpt.next_block[0]<<24) + (dbmh->fpt.next_block[1]<<16) +
		(dbmh->fpt.next_block[2]<<8) + (dbmh->fpt.next_block[3]<<0);
	    dbh->mb_blksiz = (dbmh->fpt.blocksize[0]<<8) + dbmh->fpt.blocksize[1];
#if 0
	    fprintf(stderr, "Visual FoxPro memo file block size of %lu, next %lu\n", dbh->mb_blksiz,dbh->mb_next_blk);
#endif
	    if (dbh->mb_blksiz > dbh->mb_bufsiz)
	    {
		int	newsiz = (((dbh->mb_blksiz * 2 + 4095)/4096)+1)*4096-16;
		void	*tmpptr = realloc(dbh->mb_buffer, newsiz);
		if (tmpptr != NULL)
		{
		    dbh->mb_buffer = tmpptr;
		    dbh->mb_bufsiz = newsiz;
		}
	    }
	    break;

	  case DBF_MTYPE_DBT3:
	  case DBF_MTYPE_DBT4:
	    if (isupper(sp[i]))
		strcpy(&sp[i-2],"DBT");
	    else
		strcpy(&sp[i-2],"dbt");
	    if ((dbh->db_memofd = open(sp, flags)) == -1)
	    {
		if (isupper(sp[i]))
		    strcpy(&sp[i-2],"dbt");
		else
		    strcpy(&sp[i-2],"DBT");
		if ((dbh->db_memofd = open(sp, flags)) == -1)
		{
		    char	msg[256];
		    int	saverrno = errno;

		    if (isupper(sp[i]))
			strcpy(&sp[i-2],"dbt");
		    else
			strcpy(&sp[i-2],"DBT");
		    snprintf(msg, sizeof(msg),
			     "couldn't find required memo data file: %s - (%d)",
			     sp,
			     saverrno);
		    msg[sizeof(msg)-1] = 0;
		    errno = saverrno;
		    perror(msg);
		    dbf_close(&dbh);
		    free(sp);
		    return (dbhead *)DBF_ERROR;
		}
	    }
	    if (read(dbh->db_memofd, dbh->mb_buffer, sizeof(dbf_memo_header)) != sizeof(dbf_memo_header))
	    {
		perror("memo file read");
		dbf_close(&dbh);
		free(sp);
		return (dbhead *)DBF_ERROR;
	    }

	    /* Visual FoxPro V6.0 when told to write a DB IV format actually
	       creates a DB III+ file however leaves the version field zero
	       like a DB IV one, try to account for this */
	    if (dbmh->dbiii.version == 0x00 &&
		get_short(dbmh->dbiv.blocklen) == 0 &&
		dbmh->dbiv.filename[0] == '\0')
		dbmh->dbiii.version = 0x03;

	    switch (dbmh->dbiii.version)
	    {
	      case 0x03:
		dbh->db_memo = DBF_MTYPE_DBT3; /* Set the real type */
		dbh->mb_next_blk = get_long(dbmh->dbiii.next_block);
		dbh->mb_blksiz = DBF_DBT_BLOCK_SIZE;
#if 0
		fprintf(stderr, "DB III memo file block size of %lu, next %lu\n", dbh->mb_blksiz, dbh->mb_next_blk);
#endif
		break;

	      case 0x00:
		dbh->db_memo = DBF_MTYPE_DBT4; /* Set the real type */
		dbh->mb_next_blk = get_long(dbmh->dbiv.next_block);
		dbh->mb_blksiz = get_short(dbmh->dbiv.blocklen);
#if 0
		fprintf(stderr, "DB IV memo file block size of %lu, next %lu\n", dbh->mb_blksiz, dbh->mb_next_blk);
#endif
	      break;

	      default:
		fprintf(stderr,"DB III+/DB IV bad memo file version %02x\n",
			dbmh->dbiii.version);
		dbf_close(&dbh);
		free(sp);
		return (dbhead *)DBF_ERROR;
	    }
	    
	    if (dbh->mb_blksiz > dbh->mb_bufsiz)
	    {
		int	newsiz = (((dbh->mb_blksiz * 2 + 4095)/4096)+1)*4096-16;
		void	*tmpptr = realloc(dbh->mb_buffer, newsiz);
		if (tmpptr != NULL)
		{
		    dbh->mb_buffer = tmpptr;
		    dbh->mb_bufsiz = newsiz;
		}
	    }
	    break;

	  case DBF_MTYPE_NONE:
	  default:
	    fprintf(stderr,"Found memo field in table of type (%s) which isn't supported yet\n",
		    dbh->db_description);
	    dbf_close(&dbh);
	    free(sp);
	    return (dbhead *)DBF_ERROR;
	    break;
	}
	free(sp);
    }
    
    
    return dbh;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movl %esi, %ebp
movq %rdi, %r13
movl $0x1, %edi
movl $0x88, %esi
callq 0x2140
movq %rax, 0x10(%rsp)
testq %rax, %rax
je 0x40ce
movq %rax, %rbx
movl $0x20, %edi
callq 0x2280
testq %rax, %rax
je 0x40c6
movq %rax, %r14
movl $0x20, %edi
callq 0x2280
testq %rax, %rax
je 0x40be
movq %rax, %r15
movq %r13, %rdi
movl %ebp, %esi
xorl %eax, %eax
callq 0x2150
cmpl $-0x1, %eax
je 0x40b6
movl %eax, %r12d
movl $0x20, %edx
movl %eax, %edi
movq %r14, %rsi
callq 0x22d0
cmpq $-0x1, %rax
je 0x40a9
movb (%r14), %dl
xorl %eax, %eax
leaq 0x4dd7(%rip), %rcx      # 0x8d30
cmpb %dl, (%rax,%rcx)
je 0x3f80
addq $0x10, %rax
cmpq $0x90, %rax
jne 0x3f59
movl %r12d, %edi
movq %r14, %rsi
movq %r15, %rdx
movq %rbx, %rcx
callq 0x25e0
jmp 0x40ce
movq 0x8(%rax,%rcx), %rdx
movq %rdx, 0x48(%rbx)
movb 0x1(%rax,%rcx), %al
movb %al, 0x10(%rbx)
movl %r12d, (%rbx)
movb 0x1(%r14), %al
movb %al, 0x11(%rbx)
movb 0x2(%r14), %al
movb %al, 0x12(%rbx)
movb 0x3(%r14), %al
movb %al, 0x13(%rbx)
leaq 0x8(%r14), %rdi
callq 0x5529
movswq %ax, %rax
movq %rax, 0x18(%rbx)
leaq 0x4(%r14), %rdi
callq 0x550c
movq %rax, 0x20(%rbx)
movq $0x0, 0x28(%rbx)
movq %r14, %rdi
addq $0xa, %rdi
callq 0x5529
movw %ax, 0x30(%rbx)
movl %r12d, (%rsp)
movl $0x1fe0, %r12d          # imm = 0x1FE0
addq 0x18(%rbx), %r12
shrq $0x5, %r12
movb %r12b, 0x32(%rbx)
movzwl %ax, %edi
callq 0x2280
movq %rax, 0x38(%rbx)
testq %rax, %rax
je 0x40ae
movzbl %r12b, %edi
movl $0xf, %esi
callq 0x2140
testq %rax, %rax
je 0x40ae
movq %r13, 0x20(%rsp)
movq %r14, 0x8(%rsp)
movl %ebp, 0x1c(%rsp)
testb %r12b, %r12b
movq %rax, 0x28(%rsp)
je 0x40ea
xorl %r13d, %r13d
movq %rax, %rbp
xorl %r12d, %r12d
movl $0x20, %edx
movl (%rsp), %edi
movq %r15, %rsi
callq 0x22d0
movb 0xb(%r15), %r14b
testb %r14b, %r14b
je 0x40ef
movl $0xb, %edx
movq %rbp, %rdi
movq %r15, %rsi
callq 0x2110
movb %r14b, 0xb(%rbp)
movb 0x10(%r15), %al
movb %al, 0xc(%rbp)
movb 0x11(%r15), %al
movb %al, 0xd(%rbp)
movb 0x1f(%r15), %al
movb %al, 0xe(%rbp)
xorl %eax, %eax
cmpb $0x4d, %r14b
sete %al
addl %eax, %r12d
incq %r13
movzbl 0x32(%rbx), %eax
addq $0xf, %rbp
cmpq %rax, %r13
jb 0x4045
jmp 0x40f3
movl %r12d, %edi
jmp 0x40b1
movl (%rsp), %edi
callq 0x23b0
movq %r15, %rdi
callq 0x2230
movq %r14, %rdi
callq 0x2230
movq %rbx, %rdi
callq 0x2230
movq $-0x1, %rbx
movq %rbx, %rax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r12d, %r12d
jmp 0x40f3
movb %r13b, 0x32(%rbx)
movq 0x18(%rbx), %rax
movq %rax, 0x8(%rbx)
movq 0x28(%rsp), %rax
movq %rax, 0x40(%rbx)
movq %r15, %rdi
callq 0x2230
movq 0x8(%rsp), %rdi
callq 0x2230
testl %r12d, %r12d
je 0x4288
movq $0x200, 0x68(%rbx)      # imm = 0x200
movq $0x400, 0x60(%rbx)      # imm = 0x400
movl $0x400, %edi            # imm = 0x400
callq 0x2280
movq %rax, 0x58(%rbx)
testq %rax, %rax
je 0x441c
movq %rax, %r15
movq 0x20(%rsp), %rdi
callq 0x2380
movq %rax, %r14
movq %rax, %rdi
callq 0x20d0
movq %rax, %r12
leal -0x1(%rax), %ebp
movzbl 0x10(%rbx), %eax
leal -0x1(%rax), %ecx
cmpl $0x2, %ecx
jae 0x4291
callq 0x2200
movq %rax, (%rsp)
movq (%rax), %rax
movslq %ebp, %rcx
movq %rcx, 0x8(%rsp)
movsbq (%r14,%rcx), %rcx
movzwl (%rax,%rcx,2), %eax
leal -0x3(%r12), %ecx
btl $0x8, %eax
movslq %ecx, %rax
movl $0x746264, %r13d        # imm = 0x746264
movl $0x544244, %ecx         # imm = 0x544244
cmovael %r13d, %ecx
movl %ecx, (%r14,%rax)
movq %r14, %rdi
movl 0x1c(%rsp), %ebp
movl %ebp, %esi
xorl %eax, %eax
callq 0x2150
movl %eax, 0x50(%rbx)
cmpl $-0x1, %eax
jne 0x421b
movabsq $-0x300000000, %rax     # imm = 0xFFFFFFFD00000000
movq (%rsp), %rcx
movq (%rcx), %rcx
movq 0x8(%rsp), %rdx
movsbq (%r14,%rdx), %rdx
movzwl (%rcx,%rdx,2), %ecx
shlq $0x20, %r12
addq %rax, %r12
sarq $0x20, %r12
btl $0x8, %ecx
movl $0x544244, %eax         # imm = 0x544244
cmovael %eax, %r13d
movl %r13d, (%r14,%r12)
movq %r14, %rdi
movl %ebp, %esi
xorl %eax, %eax
callq 0x2150
movl %eax, 0x50(%rbx)
cmpl $-0x1, %eax
je 0x4478
movq 0x58(%rbx), %rsi
movl $0x16, %edx
movl %eax, %edi
callq 0x22d0
cmpq $0x16, %rax
jne 0x4437
movb 0x10(%r15), %al
testb %al, %al
jne 0x425a
leaq 0x14(%r15), %rdi
callq 0x5529
testw %ax, %ax
jne 0x4256
cmpb $0x0, 0x8(%r15)
je 0x4443
movb 0x10(%r15), %al
testb %al, %al
je 0x43b8
movzbl %al, %edi
cmpl $0x3, %edi
jne 0x4466
movb $0x1, 0x10(%rbx)
movq %r15, %rdi
callq 0x550c
movq %rax, 0x78(%rbx)
movl $0x200, %eax            # imm = 0x200
jmp 0x43d8
movb $0x0, 0x10(%rbx)
jmp 0x40d5
cmpl $0x4, %eax
jne 0x444d
callq 0x2200
movq %rax, (%rsp)
movq (%rax), %rax
movslq %ebp, %rcx
movq %rcx, 0x8(%rsp)
movsbq (%r14,%rcx), %rcx
movzwl (%rax,%rcx,2), %eax
leal -0x3(%r12), %ecx
btl $0x8, %eax
movslq %ecx, %rax
movl $0x747066, %r13d        # imm = 0x747066
movl $0x545046, %ecx         # imm = 0x545046
cmovael %r13d, %ecx
movl %ecx, (%r14,%rax)
movq %r14, %rdi
movl 0x1c(%rsp), %ebp
movl %ebp, %esi
xorl %eax, %eax
callq 0x2150
movl %eax, 0x50(%rbx)
cmpl $-0x1, %eax
jne 0x4341
movabsq $-0x300000000, %rax     # imm = 0xFFFFFFFD00000000
movq (%rsp), %rcx
movq (%rcx), %rcx
movq 0x8(%rsp), %rdx
movsbq (%r14,%rdx), %rdx
movzwl (%rcx,%rdx,2), %ecx
shlq $0x20, %r12
addq %rax, %r12
sarq $0x20, %r12
btl $0x8, %ecx
movl $0x545046, %eax         # imm = 0x545046
cmovael %eax, %r13d
movl %r13d, (%r14,%r12)
movq %r14, %rdi
movl %ebp, %esi
xorl %eax, %eax
callq 0x2150
movl %eax, 0x50(%rbx)
cmpl $-0x1, %eax
je 0x44a8
movq 0x58(%rbx), %rsi
movl $0x16, %edx
movl %eax, %edi
callq 0x22d0
cmpq $0x16, %rax
jne 0x4437
movzbl (%r15), %eax
shll $0x18, %eax
movzbl 0x1(%r15), %ecx
shll $0x10, %ecx
cltq
orq %rcx, %rax
movzbl 0x2(%r15), %ecx
shll $0x8, %ecx
orq %rax, %rcx
movzbl 0x3(%r15), %eax
orq %rcx, %rax
movq %rax, 0x78(%rbx)
movzwl 0x6(%r15), %eax
rolw $0x8, %ax
movzwl %ax, %eax
movq %rax, 0x68(%rbx)
cmpq 0x60(%rbx), %rax
jbe 0x440f
leal 0xfff(,%rax,2), %r15d
andl $0x3f000, %r15d         # imm = 0x3F000
orq $0xff0, %r15            # imm = 0xFF0
movq 0x58(%rbx), %rdi
jmp 0x43fa
movb $0x2, 0x10(%rbx)
movq %r15, %rdi
callq 0x550c
movq %rax, 0x78(%rbx)
addq $0x14, %r15
movq %r15, %rdi
callq 0x5529
movswq %ax, %rax
movq %rax, 0x68(%rbx)
cmpq 0x60(%rbx), %rax
jbe 0x440f
leal 0xfff(,%rax,2), %eax
andl $0xfffff000, %eax       # imm = 0xFFFFF000
orl $0xff0, %eax            # imm = 0xFF0
movq 0x58(%rbx), %rdi
movslq %eax, %r15
movq %r15, %rsi
callq 0x2350
testq %rax, %rax
je 0x440f
movq %rax, 0x58(%rbx)
movq %r15, 0x60(%rbx)
movq %r14, %rdi
callq 0x2230
jmp 0x40d5
leaq 0x26a2(%rip), %rdi      # 0x6ac5
callq 0x2180
leaq 0x10(%rsp), %rdi
callq 0x4524
jmp 0x40ce
leaq 0x26bf(%rip), %rdi      # 0x6afd
jmp 0x450d
movb $0x3, 0x10(%r15)
jmp 0x426e
addq $0x48, %rbx
leaq 0x10(%rsp), %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0x2669
jmp 0x40ce
leaq 0x10(%rsp), %rsi
movq %r14, %rdx
callq 0x262c
jmp 0x40ce
callq 0x2040
movq %rax, %rbx
movl (%rax), %r15d
movq (%rsp), %rax
movq (%rax), %rax
movq 0x8(%rsp), %rcx
movsbq (%r14,%rcx), %rcx
movzwl (%rax,%rcx,2), %eax
btl $0x8, %eax
movl $0x544244, %eax         # imm = 0x544244
movl $0x746264, %ecx         # imm = 0x746264
jmp 0x44d6
callq 0x2040
movq %rax, %rbx
movl (%rax), %r15d
movq (%rsp), %rax
movq (%rax), %rax
movq 0x8(%rsp), %rcx
movsbq (%r14,%rcx), %rcx
movzwl (%rax,%rcx,2), %eax
btl $0x8, %eax
movl $0x545046, %eax         # imm = 0x545046
movl $0x747066, %ecx         # imm = 0x747066
cmovael %eax, %ecx
movl %ecx, (%r14,%r12)
leaq 0x25e8(%rip), %rdx      # 0x6acc
leaq 0x30(%rsp), %r12
movl $0x100, %esi            # imm = 0x100
movq %r12, %rdi
movq %r14, %rcx
movl %r15d, %r8d
xorl %eax, %eax
callq 0x2330
movb $0x0, 0xff(%r12)
movl %r15d, (%rbx)
movq %r12, %rdi
callq 0x2180
leaq 0x10(%rsp), %rdi
callq 0x4524
movq %r14, %rdi
jmp 0x40c9