// File: /workspace/github_c_makefile_star2_O2/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, %r14
pushq $0x1
popq %rdi
movl $0x88, %esi
callq 0x2140
movq %rax, 0x10(%rsp)
pushq $-0x1
popq %r12
testq %rax, %rax
je 0x4165
movq %rax, %rbx
pushq $0x20
popq %rdi
callq 0x2280
testq %rax, %rax
je 0x415d
movq %rax, %r13
pushq $0x20
popq %rdi
callq 0x2280
testq %rax, %rax
je 0x4155
movq %rax, %r15
movq %r14, %rdi
movl %ebp, %esi
xorl %eax, %eax
callq 0x2150
cmpl $-0x1, %eax
je 0x414d
pushq $0x20
popq %rdx
movl %eax, %edi
movq %r13, %rsi
movl %eax, %r12d
callq 0x22d0
cmpq $-0x1, %rax
je 0x3e21
movl %r12d, (%rsp)
movb (%r13), %dl
xorl %eax, %eax
leaq 0x5046(%rip), %rcx      # 0x8d30
pushq $-0x1
popq %r12
cmpq $0x90, %rax
je 0x4128
cmpb %dl, (%rax,%rcx)
je 0x3d05
addq $0x10, %rax
jmp 0x3cee
movq %r14, 0x8(%rsp)
movq 0x8(%rax,%rcx), %rdx
movq %rdx, 0x48(%rbx)
movb 0x1(%rax,%rcx), %al
movb %al, 0x10(%rbx)
movl (%rsp), %eax
movl %eax, %r12d
movl %eax, (%rbx)
movb 0x1(%r13), %al
movb %al, 0x11(%rbx)
movb 0x2(%r13), %al
movb %al, 0x12(%rbx)
movb 0x3(%r13), %al
movb %al, 0x13(%rbx)
leaq 0x8(%r13), %rdi
callq 0x51e5
movswq %ax, %rax
movq %rax, 0x18(%rbx)
leaq 0x4(%r13), %rdi
callq 0x51c8
movq %rax, 0x20(%rbx)
andq $0x0, 0x28(%rbx)
movq %r13, %rdi
addq $0xa, %rdi
callq 0x51e5
movw %ax, 0x30(%rbx)
movl $0x1fe0, %r14d          # imm = 0x1FE0
addq 0x18(%rbx), %r14
shrq $0x5, %r14
movb %r14b, 0x32(%rbx)
movzwl %ax, %edi
callq 0x2280
movq %rax, 0x38(%rbx)
testq %rax, %rax
je 0x3e21
movzbl %r14b, %edi
pushq $0xf
popq %rsi
callq 0x2140
testq %rax, %rax
je 0x3e49
movq %r13, 0x28(%rsp)
movl %ebp, 0x1c(%rsp)
xorl %r13d, %r13d
movq %rax, 0x20(%rsp)
movq %rax, %rbp
xorl %r12d, %r12d
movzbl %r14b, %eax
cmpq %rax, %r13
jae 0x3e52
movl (%rsp), %edi
movq %r15, %rsi
pushq $0x20
popq %rdx
callq 0x22d0
movb 0xb(%r15), %r14b
testb %r14b, %r14b
je 0x3e4e
movq %rbp, %rdi
movq %r15, %rsi
pushq $0xb
popq %rdx
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
movb 0x32(%rbx), %r14b
addq $0xf, %rbp
jmp 0x3dbd
movl %r12d, %edi
callq 0x23b0
movq %r15, %rdi
callq 0x2230
movq %r13, %rdi
callq 0x2230
movq %rbx, %rdi
callq 0x2230
pushq $-0x1
popq %rbx
jmp 0x4168
movl (%rsp), %edi
jmp 0x3e24
movb %r13b, 0x32(%rbx)
movq 0x18(%rbx), %rax
movq %rax, 0x8(%rbx)
movq 0x20(%rsp), %rax
movq %rax, 0x40(%rbx)
movq %r15, %rdi
callq 0x2230
movq 0x28(%rsp), %rdi
callq 0x2230
testl %r12d, %r12d
je 0x3fec
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 0x41de
movq %rax, %r15
movq 0x8(%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 0x3ff5
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 0x3f7a
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 0x4264
movq %r14, %r12
movq 0x58(%rbx), %rsi
pushq $0x16
popq %rdx
movl %eax, %edi
callq 0x22d0
cmpq $0x16, %rax
pushq $-0x1
popq %r14
jne 0x41f9
movb 0x10(%r15), %al
testb %al, %al
jne 0x3fbe
leaq 0x14(%r15), %rdi
callq 0x51e5
testw %ax, %ax
jne 0x3fba
cmpb $0x0, 0x8(%r15)
je 0x4207
movb 0x10(%r15), %al
testb %al, %al
je 0x417d
movzbl %al, %edx
cmpl $0x3, %edx
jne 0x4232
movb $0x1, 0x10(%rbx)
movq %r15, %rdi
callq 0x51c8
movq %rax, 0x78(%rbx)
movl $0x200, %eax            # imm = 0x200
jmp 0x419d
movb $0x0, 0x10(%rbx)
jmp 0x4168
cmpl $0x4, %eax
jne 0x4211
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 0x40a5
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 0x4294
movq %r14, %r12
movq 0x58(%rbx), %rsi
pushq $0x16
popq %rdx
movl %eax, %edi
callq 0x22d0
cmpq $0x16, %rax
pushq $-0x1
popq %r14
jne 0x41f9
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 0x41d4
leal 0xfff(,%rax,2), %r15d
andl $0x3f000, %r15d         # imm = 0x3F000
orq $0xff0, %r15            # imm = 0xFF0
movq 0x58(%rbx), %rdi
jmp 0x41bf
movl (%rsp), %edi
callq 0x23b0
movq 0x4ea1(%rip), %rax      # 0x8fd8
movq (%rax), %rdi
movzbl (%r13), %edx
leaq 0x2962(%rip), %rsi      # 0x6aa8
xorl %eax, %eax
callq 0x22c0
movq %r15, %rdi
callq 0x2230
movq %r13, %rdi
callq 0x2230
movq %rbx, %rdi
callq 0x2230
movq %r12, %rbx
movq %rbx, %rax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb $0x2, 0x10(%rbx)
movq %r15, %rdi
callq 0x51c8
movq %rax, 0x78(%rbx)
addq $0x14, %r15
movq %r15, %rdi
callq 0x51e5
movswq %ax, %rax
movq %rax, 0x68(%rbx)
cmpq 0x60(%rbx), %rax
jbe 0x41d4
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 0x41d4
movq %rax, 0x58(%rbx)
movq %r15, 0x60(%rbx)
movq %r12, %rdi
callq 0x2230
jmp 0x4168
leaq 0x28e0(%rip), %rdi      # 0x6ac5
callq 0x2180
leaq 0x10(%rsp), %rdi
callq 0x4310
jmp 0x3e41
leaq 0x28fd(%rip), %rdi      # 0x6afd
callq 0x2180
jmp 0x424a
movb $0x3, 0x10(%r15)
jmp 0x3fd2
movq 0x4dc0(%rip), %rax      # 0x8fd8
movq (%rax), %rdi
movq 0x48(%rbx), %rdx
leaq 0x2910(%rip), %rsi      # 0x6b36
xorl %eax, %eax
callq 0x22c0
jmp 0x42fe
movq 0x4d9f(%rip), %rax      # 0x8fd8
movq (%rax), %rdi
leaq 0x28c9(%rip), %rsi      # 0x6b0c
xorl %eax, %eax
callq 0x22c0
leaq 0x10(%rsp), %rdi
callq 0x4310
movq %r12, %rdi
callq 0x2230
movq %r14, %rbx
jmp 0x4168
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 0x42c2
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 0x27fc(%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 0x4310
movq %r14, %rdi
jmp 0x3e3c