// File: /workspace/github_c_makefile_star2_O1/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, %rbx
movl $0x1, %edi
movl $0x88, %esi
callq 0x2140
movq %rax, 0x8(%rsp)
movq $-0x1, %r15
testq %rax, %rax
je 0x40e3
movq %rax, %r14
movl $0x20, %edi
callq 0x2280
testq %rax, %rax
je 0x40db
movq %rax, %r12
movl $0x20, %edi
callq 0x2280
testq %rax, %rax
je 0x40d3
movq %rax, %r13
movq %rbx, %rdi
movl %ebp, %esi
xorl %eax, %eax
callq 0x2150
cmpl $-0x1, %eax
je 0x40cb
movl $0x20, %edx
movl %eax, %r15d
movl %eax, %edi
movq %r12, %rsi
callq 0x22d0
cmpq $-0x1, %rax
je 0x3f4c
movb (%r12), %cl
leaq 0x4e0d(%rip), %rdx      # 0x8d30
xorl %eax, %eax
movl %r15d, %edi
movq $-0x1, %r15
cmpb %cl, (%rdx)
je 0x3f78
incq %rax
addq $0x10, %rdx
cmpq $0x9, %rax
jne 0x3f2f
movl $0x9, %eax
movl $0x9, %ecx
jmp 0x3f7a
movl %r15d, %edi
callq 0x23b0
movq %r13, %rdi
callq 0x2230
movq %r12, %rdi
callq 0x2230
movq %r14, %rdi
callq 0x2230
movq $-0x1, %r15
jmp 0x40e3
movl %eax, %ecx
cmpl $0x9, %ecx
je 0x443d
movq %rbx, 0x10(%rsp)
shlq $0x4, %rax
leaq 0x4d9d(%rip), %rcx      # 0x8d30
movq 0x8(%rax,%rcx), %rdx
movq %rdx, 0x48(%r14)
movb 0x1(%rax,%rcx), %al
movb %al, 0x10(%r14)
movl %edi, (%r14)
movb 0x1(%r12), %al
movb %al, 0x11(%r14)
movb 0x2(%r12), %al
movb %al, 0x12(%r14)
movb 0x3(%r12), %al
movb %al, 0x13(%r14)
movl %edi, (%rsp)
leaq 0x8(%r12), %rdi
callq 0x5599
movswq %ax, %rax
movq %rax, 0x18(%r14)
leaq 0x4(%r12), %rdi
callq 0x557c
movq %rax, 0x20(%r14)
movq $0x0, 0x28(%r14)
movq %r12, %rdi
addq $0xa, %rdi
callq 0x5599
movw %ax, 0x30(%r14)
movl $0x1fe0, %ebx           # imm = 0x1FE0
addq 0x18(%r14), %rbx
shrq $0x5, %rbx
movb %bl, 0x32(%r14)
movzwl %ax, %edi
callq 0x2280
movq %rax, 0x38(%r14)
testq %rax, %rax
je 0x40c3
movzbl %bl, %edi
movl $0xf, %esi
callq 0x2140
testq %rax, %rax
je 0x40c3
movq %r12, 0x28(%rsp)
movl %ebp, 0x1c(%rsp)
cmpb $0x0, 0x32(%r14)
movl (%rsp), %edi
movq %rax, 0x20(%rsp)
je 0x40f8
xorl %ebx, %ebx
movq %rax, %rbp
xorl %r12d, %r12d
movl $0x20, %edx
movq %r13, %rsi
callq 0x22d0
movb 0xb(%r13), %r15b
testb %r15b, %r15b
je 0x40fd
movl $0xb, %edx
movq %rbp, %rdi
movq %r13, %rsi
callq 0x2110
movb %r15b, 0xb(%rbp)
movb 0x10(%r13), %al
movb %al, 0xc(%rbp)
movb 0x11(%r13), %al
movb %al, 0xd(%rbp)
movb 0x1f(%r13), %al
movb %al, 0xe(%rbp)
xorl %eax, %eax
cmpb $0x4d, %r15b
sete %al
addl %eax, %r12d
incq %rbx
movzbl 0x32(%r14), %eax
addq $0xf, %rbp
cmpq %rax, %rbx
movl (%rsp), %edi
jb 0x405e
jmp 0x4101
movl (%rsp), %edi
callq 0x23b0
movq %r13, %rdi
callq 0x2230
movq %r12, %rdi
callq 0x2230
movq %r14, %rdi
callq 0x2230
movq %r15, %rax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %r12d, %r12d
jmp 0x4101
movb %bl, 0x32(%r14)
movq 0x18(%r14), %rax
movq %rax, 0x8(%r14)
movq 0x20(%rsp), %rax
movq %rax, 0x40(%r14)
movq %r13, %rdi
callq 0x2230
movq 0x28(%rsp), %rdi
callq 0x2230
testl %r12d, %r12d
je 0x429e
movq $0x200, 0x68(%r14)      # imm = 0x200
movq $0x400, 0x60(%r14)      # imm = 0x400
movl $0x400, %edi            # imm = 0x400
callq 0x2280
movq %rax, 0x58(%r14)
testq %rax, %rax
je 0x4450
movq %rax, %r13
movq 0x10(%rsp), %rdi
callq 0x2380
movq %rax, %r15
movq %rax, %rdi
callq 0x20d0
movq %rax, %rbx
leal -0x1(%rax), %ebp
movzbl 0x10(%r14), %eax
leal -0x1(%rax), %ecx
cmpl $0x2, %ecx
movq %r15, (%rsp)
jae 0x42a8
callq 0x2200
movq %rax, 0x10(%rsp)
movq (%rax), %rax
movslq %ebp, %rbp
movsbq (%r15,%rbp), %rcx
movzwl (%rax,%rcx,2), %eax
leal -0x3(%rbx), %ecx
btl $0x8, %eax
movslq %ecx, %rax
movq %r15, %rdi
movl $0x746264, %r15d        # imm = 0x746264
movl $0x544244, %ecx         # imm = 0x544244
cmovael %r15d, %ecx
movl %ecx, (%rdi,%rax)
movl 0x1c(%rsp), %r12d
movl %r12d, %esi
xorl %eax, %eax
callq 0x2150
movl %eax, 0x50(%r14)
cmpl $-0x1, %eax
jne 0x4229
movabsq $-0x300000000, %rax     # imm = 0xFFFFFFFD00000000
movq 0x10(%rsp), %rcx
movq (%rcx), %rcx
movq (%rsp), %rdi
movsbq (%rdi,%rbp), %rdx
movzwl (%rcx,%rdx,2), %ecx
shlq $0x20, %rbx
addq %rax, %rbx
sarq $0x20, %rbx
btl $0x8, %ecx
movl $0x544244, %eax         # imm = 0x544244
cmovael %eax, %r15d
movl %r15d, (%rdi,%rbx)
movl %r12d, %esi
xorl %eax, %eax
callq 0x2150
movl %eax, 0x50(%r14)
cmpl $-0x1, %eax
je 0x44ce
movl 0x50(%r14), %edi
movq 0x58(%r14), %rsi
movl $0x16, %edx
callq 0x22d0
cmpq $0x16, %rax
jne 0x446b
cmpb $0x0, 0x10(%r13)
movq $-0x1, %r15
jne 0x426d
leaq 0x14(%r13), %rdi
callq 0x5599
testw %ax, %ax
jne 0x426d
cmpb $0x0, 0x8(%r13)
jne 0x426d
movb $0x3, 0x10(%r13)
movzbl 0x10(%r13), %edx
testl %edx, %edx
je 0x43cf
cmpl $0x3, %edx
jne 0x44a3
movb $0x1, 0x10(%r14)
movq %r13, %rdi
callq 0x557c
movq %rax, 0x78(%r14)
movl $0x200, %eax            # imm = 0x200
jmp 0x43f0
movb $0x0, 0x10(%r14)
jmp 0x4433
cmpl $0x4, %eax
jne 0x448a
callq 0x2200
movq %rax, 0x10(%rsp)
movq (%rax), %rax
movslq %ebp, %rbp
movsbq (%r15,%rbp), %rcx
movzwl (%rax,%rcx,2), %eax
leal -0x3(%rbx), %ecx
btl $0x8, %eax
movslq %ecx, %rax
movq %r15, %rdi
movl $0x747066, %r15d        # imm = 0x747066
movl $0x545046, %ecx         # imm = 0x545046
cmovael %r15d, %ecx
movl %ecx, (%rdi,%rax)
movl 0x1c(%rsp), %r12d
movl %r12d, %esi
xorl %eax, %eax
callq 0x2150
movl %eax, 0x50(%r14)
cmpl $-0x1, %eax
jne 0x4353
movabsq $-0x300000000, %rax     # imm = 0xFFFFFFFD00000000
movq 0x10(%rsp), %rcx
movq (%rcx), %rcx
movq (%rsp), %rdi
movsbq (%rdi,%rbp), %rdx
movzwl (%rcx,%rdx,2), %ecx
shlq $0x20, %rbx
addq %rax, %rbx
sarq $0x20, %rbx
btl $0x8, %ecx
movl $0x545046, %eax         # imm = 0x545046
cmovael %eax, %r15d
movl %r15d, (%rdi,%rbx)
movl %r12d, %esi
xorl %eax, %eax
callq 0x2150
movl %eax, 0x50(%r14)
cmpl $-0x1, %eax
je 0x44fe
movl 0x50(%r14), %edi
movq 0x58(%r14), %rsi
movl $0x16, %edx
callq 0x22d0
cmpq $0x16, %rax
jne 0x446b
movzbl (%r13), %eax
shll $0x18, %eax
movzbl 0x1(%r13), %ecx
shll $0x10, %ecx
cltq
orq %rcx, %rax
movzbl 0x2(%r13), %ecx
shll $0x8, %ecx
orq %rax, %rcx
movzbl 0x3(%r13), %eax
orq %rcx, %rax
movq %rax, 0x78(%r14)
movzwl 0x6(%r13), %eax
rolw $0x8, %ax
movzwl %ax, %eax
movq %rax, 0x68(%r14)
cmpq 0x60(%r14), %rax
movq (%rsp), %r15
jbe 0x442b
leal 0xfff(,%rax,2), %ebx
andl $0x3f000, %ebx          # imm = 0x3F000
orq $0xff0, %rbx            # imm = 0xFF0
movq 0x58(%r14), %rdi
jmp 0x4416
movb $0x2, 0x10(%r14)
movq %r13, %rdi
callq 0x557c
movq %rax, 0x78(%r14)
addq $0x14, %r13
movq %r13, %rdi
callq 0x5599
movswq %ax, %rax
movq %rax, 0x68(%r14)
cmpq 0x60(%r14), %rax
movq (%rsp), %r15
jbe 0x442b
leal 0xfff(,%rax,2), %eax
andl $0xfffff000, %eax       # imm = 0xFFFFF000
orl $0xff0, %eax            # imm = 0xFF0
movq 0x58(%r14), %rdi
movslq %eax, %rbx
movq %rbx, %rsi
callq 0x2350
testq %rax, %rax
je 0x442b
movq %rax, 0x58(%r14)
movq %rbx, 0x60(%r14)
movq %r15, %rdi
callq 0x2230
movq 0x8(%rsp), %r15
jmp 0x40e3
movq %r12, %rsi
movq %r13, %rdx
movq %r14, %rcx
callq 0x261f
jmp 0x40e3
leaq 0x266e(%rip), %rdi      # 0x6ac5
callq 0x2180
leaq 0x8(%rsp), %rdi
callq 0x4578
jmp 0x3f6c
leaq 0x268b(%rip), %rdi      # 0x6afd
callq 0x2180
leaq 0x8(%rsp), %rdi
callq 0x4578
movq (%rsp), %rdi
jmp 0x3f67
addq $0x48, %r14
leaq 0x8(%rsp), %rsi
movq %r14, %rdi
movq %r15, %rdx
callq 0x25e0
jmp 0x3f6c
movq 0x4b2e(%rip), %rax      # 0x8fd8
movq (%rax), %rdi
leaq 0x2658(%rip), %rsi      # 0x6b0c
xorl %eax, %eax
callq 0x22c0
leaq 0x8(%rsp), %rdi
callq 0x4578
movq (%rsp), %rdi
jmp 0x40de
callq 0x2040
movq %rax, %r14
movl (%rax), %r13d
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq (%rsp), %r15
movsbq (%r15,%rbp), %rcx
movzwl (%rax,%rcx,2), %eax
btl $0x8, %eax
movl $0x544244, %eax         # imm = 0x544244
movl $0x746264, %ecx         # imm = 0x746264
jmp 0x452c
callq 0x2040
movq %rax, %r14
movl (%rax), %r13d
movq 0x10(%rsp), %rax
movq (%rax), %rax
movq (%rsp), %r15
movsbq (%r15,%rbp), %rcx
movzwl (%rax,%rcx,2), %eax
btl $0x8, %eax
movl $0x545046, %eax         # imm = 0x545046
movl $0x747066, %ecx         # imm = 0x747066
cmovael %eax, %ecx
movl %ecx, (%r15,%rbx)
leaq 0x2592(%rip), %rdx      # 0x6acc
leaq 0x30(%rsp), %rbx
movl $0x100, %esi            # imm = 0x100
movq %rbx, %rdi
movq %r15, %rcx
movl %r13d, %r8d
xorl %eax, %eax
callq 0x2330
movb $0x0, 0xff(%rbx)
movl %r13d, (%r14)
movq %rbx, %rdi
callq 0x2180
leaq 0x8(%rsp), %rdi
callq 0x4578
movq %r15, %rdi
jmp 0x3f67