// File: /workspace/github_c_makefile_star2_O2/sonicse[P]dbf2mysql/dbf.c
// Function: dbf_get_record
int dbf_get_record(dbhead *dbh, field *fields,  u_long rec)
{
    int     	t, i;
    u_char  	*dbffield;
    char	*end, *sp;
    double	dblval;
    long long	int8val;
    long	int4val;
    u_int	hh, mm, ss, month, day, year;
    u_int	daynum, leap_day, day_of_year, days_in_year;
    u_char 	*days_per_month;
    u_long	blknum;

/* calculate at which offset we have to read. *DON'T* forget the
   0x0D which seperates field-descriptions from records!

   Note (april 5 1996): This turns out to be included in db_hlen
*/
    i = dbh->db_hlen + (rec * dbh->db_rlen);

    if (lseek(dbh->db_fd, i, SEEK_SET) == -1) {
	lseek(dbh->db_fd, 0, SEEK_SET);
	dbh->db_offset = 0;
	return DBF_ERROR;
    }

    dbh->db_offset 	= i;
    dbh->db_currec	= rec;

    read(dbh->db_fd, dbh->db_buff, dbh->db_rlen);

    if (dbh->db_buff[0] == DBF_DELETED) {
	return DBF_DELETED;
    }

    dbffield = &dbh->db_buff[1];
    for (t = 0; t < dbh->db_nfields; t++)
    {
        switch(fields[t].db_type) {
          case 'C':		/* Character data */
	    end = (char *)&dbffield[fields[t].db_flen - 1];
	    i = fields[t].db_flen;

            //2011-03-23 AA: Skip only 0-21 code character
	    while (( i > 0) && ((*end < 0x21) && (*end >= 0))) {
		end--;
		i--;
	    }
            strncpy(fields[t].db_contents, (char *)dbffield, i);
	    fields[t].db_contents[i] = '\0';
	    break;

	  case 'B':		/* Binary data - 8 byte field - ???Endian Alert??? */
	    memcpy(&dblval, dbffield, sizeof(dblval));
	    snprintf(fields[t].db_contents, fields[t].db_blen,  "%.*f", fields[t].db_dec, dblval);
	    fields[t].db_contents[fields[t].db_blen-1] = '\0';
	    break;

          case 'M':		/* Memo field */
          case 'G':		/* OLE field */
	    switch(dbh->db_memo)
	    {
	      case DBF_MTYPE_FPT:
		blknum = get_long(dbffield);
		break;
	      case DBF_MTYPE_DBT3:
	      case DBF_MTYPE_DBT4:
		strncpy(fields[t].db_contents, (char *)dbffield, fields[t].db_flen);
		fields[t].db_contents[fields[t].db_flen] = '\0';
		blknum = atol(fields[t].db_contents);
		memset(fields[t].db_contents, 0, fields[t].db_flen);
		break;
	      default:
		blknum = 0;
	    }
	    if (blknum != 0)
		dbf_get_memo_data(dbh, &fields[t], blknum);
	    else
		memset(fields[t].db_contents, '\0', fields[t].db_blen);
	    break;

	  case 'Y':		/* Currency 8 byte field - ???Endian Alert??? */
	    memcpy(&int8val, dbffield, sizeof(int8val));
	    snprintf(fields[t].db_contents, fields[t].db_blen,  "%0*lld",
		     fields[t].db_dec+1, int8val);
	    fields[t].db_contents[fields[t].db_blen-1] = '\0';
	    end = fields[t].db_contents + strlen(fields[t].db_contents) + 1;
	    *end-- = '\0';
	    sp = end - 1;
	    for (i=0; i<fields[t].db_dec; i++)
		*end-- = *sp--;
	    *++sp = '.';
	    break;

	  case 'T':		/* Time 8 byte field - ???Endian Alert??? */
	    memcpy(&daynum, &dbffield[0], sizeof(daynum));
	    daynum -= 1721060;	/* Magic number for 0000-01-01 */
	    if (daynum <= 365 || daynum >= 3652500)
	    {			/* Only allow 0001-01-01 through 9999-12-31 */
	      year = month = day = 0;
	    }
	    else
	    {
	      year = daynum * 100 / 36525;
	      day_of_year = (daynum - year * 365) - (year-1) / 4 +
		  (((year - 1) / 100 + 1) * 3) / 4;
	      while (day_of_year >
		     (days_in_year =
		      ((year & 3) == 0 &&
		       (year % 100 ||
			(year % 400 == 0 && year)) ? 366 : 365)))
	      {
		day_of_year -= days_in_year;
		year++;
	      }
	      leap_day = 0;
	      if (days_in_year == 366)
	      {
		if (day_of_year > 31 + 28)
		{
		  day_of_year--;
		  if (day_of_year == 31 + 28)
		    leap_day = 1;		/* Handle leapyear's leapday */
		}
	      }
	      month = 1;
	      days_per_month = days_in_month;
	      while (day_of_year > *days_per_month)
	      {
		  month++;
		  day_of_year -= *days_per_month++;
	      }
	      day = day_of_year + leap_day;
	    }
	    memcpy(&int4val, &dbffield[4], sizeof(int4val));
	    int4val = int4val / 1000; /* Discard millseconds */
	    ss = int4val % 60;
	    mm = (int4val / 60) % 60;
	    hh = int4val / (60 * 60);
	    snprintf(fields[t].db_contents, fields[t].db_blen,
		     "%04d-%02d-%02d %02d:%02d:%02d",
		     year, month, day, hh, mm, ss);
	    fields[t].db_contents[fields[t].db_blen-1] = '\0';
	    break;

	  case 'I':		/* Integer 8 byte field - ???Endian Alert??? */
	    memcpy(&int4val, dbffield, sizeof(int4val));
	    snprintf(fields[t].db_contents, fields[t].db_blen,  "%ld", int4val);
	    fields[t].db_contents[fields[t].db_blen-1] = '\0';
	    break;

	  default:
	    end = (char *)dbffield;
	    i = fields[t].db_flen;
	    while (( i > 0) && ((*end < 0x21)/* || (*end > 0x7E)*/)) {
		end++;
		i--;
	    }
	    strncpy(fields[t].db_contents, end, i);
	    fields[t].db_contents[i] = '\0';
	    break;
	}
	dbffield += fields[t].db_flen;
    }

    dbh->db_offset += dbh->db_rlen;

    return DBF_VALID;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movq %rdx, %r15
movq %rsi, %r12
movq %rdi, %rbx
movzwl 0x30(%rdi), %eax
movl %r15d, %ecx
imull %eax, %ecx
addl 0x18(%rdi), %ecx
movl (%rdi), %edi
movslq %ecx, %r14
movq %r14, %rsi
xorl %edx, %edx
callq 0x2390
cmpq $-0x1, %rax
je 0x46b3
movq %r14, 0x8(%rbx)
movq %r15, 0x28(%rbx)
movl (%rbx), %edi
movq 0x38(%rbx), %rsi
movzwl 0x30(%rbx), %edx
callq 0x22d0
movq 0x38(%rbx), %r15
cmpb $0x2a, (%r15)
jne 0x46ca
pushq $0x2a
jmp 0x4e24
movl (%rbx), %edi
xorl %esi, %esi
xorl %edx, %edx
callq 0x2390
andq $0x0, 0x8(%rbx)
pushq $-0x1
jmp 0x4e24
incq %r15
xorl %edi, %edi
movq %r12, (%rsp)
movzbl 0x32(%rbx), %eax
cmpq %rax, %rdi
jae 0x4e1a
movq %rdi, %rax
shlq $0x5, %rax
leaq (%r12,%rax), %r13
movzbl 0xb(%r12,%rax), %eax
leal -0x42(%rax), %ecx
cmpl $0xb, %ecx
movq %rdi, 0x18(%rsp)
ja 0x475f
leaq 0x232b(%rip), %rdx      # 0x6a30
movslq (%rdx,%rcx,4), %rax
addq %rdx, %rax
jmpq *%rax
movzbl 0x10(%rbx), %eax
leal -0x1(%rax), %ecx
cmpl $0x2, %ecx
jae 0x47f0
movq 0x18(%r13), %rdi
movzbl 0xc(%r13), %edx
movq %r15, %rsi
callq 0x2110
movq 0x18(%r13), %rax
movzbl 0xc(%r13), %ecx
movb $0x0, (%rax,%rcx)
movq 0x18(%r13), %r14
movq %r14, %rdi
callq 0x2240
movq %rax, %rbp
movzbl 0xc(%r13), %edx
movq %r14, %rdi
xorl %esi, %esi
callq 0x2120
jmp 0x4800
cmpl $0x54, %eax
je 0x48d8
cmpl $0x59, %eax
jne 0x47ca
movq (%r15), %r8
movq 0x10(%r13), %rsi
movq 0x18(%r13), %rdi
movzbl 0xd(%r13), %ecx
incl %ecx
xorl %ebp, %ebp
leaq 0x23f5(%rip), %rdx      # 0x6b7d
xorl %eax, %eax
callq 0x2330
movq 0x10(%r13), %rax
movq 0x18(%r13), %rcx
movb %bpl, -0x1(%rcx,%rax)
movq 0x18(%r13), %r14
movq %r14, %rdi
callq 0x20d0
leaq (%r14,%rax), %rcx
movb %bpl, 0x1(%r14,%rax)
movzbl 0xd(%r13), %eax
cmpl %eax, %ebp
jae 0x49a3
movb -0x1(%rcx), %al
movb %al, (%rcx)
decq %rcx
incl %ebp
jmp 0x47b1
movzbl 0xc(%r13), %eax
movq %rax, %rsi
addq %r15, %rsi
movq %r15, %rcx
testl %eax, %eax
jle 0x49ab
cmpb $0x20, (%rcx)
jg 0x49af
incq %rcx
decl %eax
jmp 0x47d8
cmpl $0x4, %eax
jne 0x4863
movq %r15, %rdi
callq 0x51c8
movq %rax, %rbp
testq %rbp, %rbp
je 0x4863
movq 0x68(%rbx), %r12
imulq %rbp, %r12
movl 0x50(%rbx), %edi
movq %r12, %rsi
xorl %edx, %edx
callq 0x2390
cmpq $-0x1, %rax
je 0x4c07
movq %rax, %r14
movq %r15, 0x8(%rsp)
cmpq %r12, %rax
jne 0x4c13
movzbl 0x10(%rbx), %eax
cmpq $0x4, %rax
ja 0x4d54
leaq 0x2216(%rip), %rcx      # 0x6a60
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movq 0x18(%r13), %rax
movb $0x0, (%rax)
movq (%rsp), %r12
jmp 0x4c9f
movq 0x10(%r13), %rdx
movq 0x18(%r13), %rdi
xorl %esi, %esi
callq 0x2120
jmp 0x4a95
movzbl 0xc(%r13), %eax
testl %eax, %eax
jle 0x49bb
cmpb $0x20, -0x1(%r15,%rax)
ja 0x49bd
decq %rax
jmp 0x487c
movsd (%r15), %xmm0
movq 0x10(%r13), %rsi
movq 0x18(%r13), %rdi
movzbl 0xd(%r13), %ecx
leaq 0x22ca(%rip), %rdx      # 0x6b78
movb $0x1, %al
callq 0x2330
jmp 0x4a88
movq (%r15), %rcx
movq 0x10(%r13), %rsi
movq 0x18(%r13), %rdi
leaq 0x22d6(%rip), %rdx      # 0x6ba2
xorl %eax, %eax
callq 0x2330
jmp 0x4a88
movl (%r15), %esi
leal -0x51fe78(%rsi), %eax
xorl %r8d, %r8d
movl $0x0, %r9d
movl $0x0, %ecx
cmpl $0xffc845da, %eax       # imm = 0xFFC845DA
jb 0x4a22
leal -0x1a42e4(%rsi), %eax
imull $0x64, %eax, %eax
xorl %edx, %edx
movl $0x8ead, %ecx           # imm = 0x8EAD
divl %ecx
movl %eax, %ecx
imull $0xfffffe93, %eax, %eax # imm = 0xFFFFFE93
addl %eax, %esi
addl $0xffe5bd1c, %esi       # imm = 0xFFE5BD1C
movl %ecx, %eax
decl %eax
movl %eax, %edx
shrl $0x2, %edx
subl %edx, %esi
xorl %edx, %edx
movw $0x64, %di
divw %di
leal (%rax,%rax,2), %eax
addl $0x3, %eax
movzwl %ax, %r9d
shrl $0x2, %r9d
addl %esi, %r9d
xorl %edi, %edi
movl %ecx, %eax
xorl %edx, %edx
movl $0x190, %esi            # imm = 0x190
divl %esi
movl %eax, %esi
movl %ecx, %eax
xorl %edx, %edx
pushq $0x64
popq %r8
divl %r8d
testb $0x3, %cl
je 0x4967
movl $0x16d, %eax            # imm = 0x16D
jmp 0x4995
imull $0x64, %eax, %eax
cmpl %ecx, %eax
jne 0x498d
imull $0x190, %esi, %eax      # imm = 0x190
cmpl %ecx, %eax
sete %al
testl %ecx, %ecx
setne %dil
andb %al, %dil
movzbl %dil, %eax
addl $0x16d, %eax            # imm = 0x16D
jmp 0x4995
movl $0x16e, %eax            # imm = 0x16E
movb $0x1, %dil
movl %r9d, %edx
subl %eax, %edx
jbe 0x49dd
incl %ecx
movl %edx, %r9d
jmp 0x4941
movb $0x2e, (%rcx)
jmp 0x4a95
xorl %eax, %eax
jmp 0x49b2
movq %rcx, %rsi
movq 0x18(%r13), %rdi
movl %eax, %r14d
jmp 0x49c7
xorl %eax, %eax
movq 0x18(%r13), %rdi
movl %eax, %r14d
movq %r15, %rsi
movq %r14, %rdx
callq 0x2110
movq 0x18(%r13), %rax
movb $0x0, (%rax,%r14)
jmp 0x4a95
cmpl $0x3c, %r9d
setae %al
andb %al, %dil
leal -0x1(%r9), %edx
cmpl $0x3b, %edx
sete %al
andb %dil, %al
testb %dil, %dil
cmovnel %edx, %r9d
xorl %r8d, %r8d
movl %r9d, %edx
leaq 0x2094(%rip), %rsi      # 0x6a9c
movzbl (%r8,%rsi), %esi
subl %esi, %edx
jbe 0x4a19
incq %r8
movl %edx, %r9d
jmp 0x4a01
movzbl %al, %eax
addl %eax, %r9d
incl %r8d
movq 0x4(%r15), %rsi
movq %rsi, %rax
cqto
movl $0x3e8, %edi            # imm = 0x3E8
idivq %rdi
cqto
pushq $0x3c
popq %rdi
idivq %rdi
movq %rdx, %r10
movq %rsi, %rax
cqto
movl $0xea60, %r11d          # imm = 0xEA60
idivq %r11
cqto
idivq %rdi
movq %rdx, %r11
movq %rsi, %rax
cqto
movl $0x36ee80, %esi         # imm = 0x36EE80
idivq %rsi
movq %rax, %r14
movq 0x10(%r13), %rsi
movq 0x18(%r13), %rdi
subq $0x8, %rsp
leaq 0x210d(%rip), %rdx      # 0x6b84
xorl %eax, %eax
pushq %r10
pushq %r11
pushq %r14
callq 0x2330
addq $0x20, %rsp
movq 0x10(%r13), %rax
movq 0x18(%r13), %rcx
movb $0x0, -0x1(%rcx,%rax)
movzbl 0xc(%r13), %eax
addq %rax, %r15
movq 0x18(%rsp), %rdi
incq %rdi
jmp 0x46d3
movl 0x50(%rbx), %edi
movq 0x58(%rbx), %rsi
pushq $0x8
popq %rdx
callq 0x22d0
testq %rax, %rax
js 0x4d79
movq 0x58(%rbx), %rsi
movq 0x60(%rbx), %r12
movl (%rsi), %r14d
movzbl 0x4(%rsi), %eax
shll $0x18, %eax
movzbl 0x5(%rsi), %ecx
shll $0x10, %ecx
cltq
orq %rcx, %rax
movzbl 0x6(%rsi), %ecx
shll $0x8, %ecx
orq %rax, %rcx
movzbl 0x7(%rsi), %ebp
orq %rcx, %rbp
cmpq %rbp, %r12
movq 0x8(%rsp), %r15
jae 0x4cb1
leal 0xfff(%rbp), %eax
andl $0xfffff000, %eax       # imm = 0xFFFFF000
orl $0xff0, %eax            # imm = 0xFF0
movslq %eax, %r12
movq %rsi, %rdi
movq %r12, %rsi
callq 0x2350
testq %rax, %rax
je 0x4ca9
movq %rax, %rsi
movq %rax, 0x58(%rbx)
movq %r12, 0x60(%rbx)
jmp 0x4cb1
movq 0x58(%rbx), %rbp
movb $0x1, %al
xorl %r15d, %r15d
testb $0x1, %al
je 0x4bd3
leaq 0x200(%r15), %r12
cmpq %r12, 0x60(%rbx)
jae 0x4b81
movq 0x58(%rbx), %rdi
leal 0x800(%r15), %eax
movslq %eax, %r14
movq %r14, %rsi
callq 0x2350
testq %rax, %rax
je 0x4e37
movq %rax, %rbp
movq %rax, 0x58(%rbx)
movq %r14, 0x60(%rbx)
addq %r15, %rbp
movl 0x50(%rbx), %edi
movl $0x200, %edx            # imm = 0x200
movq %rbp, %rsi
callq 0x22d0
cmpq $0x200, %rax            # imm = 0x200
jl 0x4d8a
leaq 0x200(%rbp), %rcx
xorl %edx, %edx
cmpl $0x200, %edx            # imm = 0x200
sete %al
je 0x4bbd
cmpb $0x1a, (%rbp,%rdx)
je 0x4bc8
incq %rdx
jmp 0x4ba6
movq %rcx, %rbp
movq %r12, %r15
jmp 0x4b3f
addq %rdx, %rbp
addq %rdx, %r15
jmp 0x4b3f
movb $0x0, (%rbp)
movq 0x10(%r13), %rbp
cmpq %r15, %rbp
movq (%rsp), %r12
ja 0x4c7b
movq 0x18(%r13), %rdi
leaq 0x1(%r15), %rbp
movq %rbp, %rsi
callq 0x2350
testq %rax, %rax
je 0x4c77
movq %rax, 0x18(%r13)
movq %rbp, 0x10(%r13)
jmp 0x4c7b
leaq 0x2067(%rip), %rdi      # 0x6c75
jmp 0x4da2
callq 0x2040
movq %rax, 0x10(%rsp)
movl (%rax), %r15d
subq $0x8, %rsp
movl $0x100, %esi            # imm = 0x100
leaq 0x28(%rsp), %rdi
leaq 0x204f(%rip), %rdx      # 0x6c84
movq %rbp, %rcx
movq %r12, %r8
movq %r14, %r9
xorl %eax, %eax
pushq %r15
callq 0x2330
popq %rcx
popq %rdx
movb $0x0, 0x11f(%rsp)
movq 0x10(%rsp), %rax
movl %r15d, (%rax)
leaq 0x20(%rsp), %rdi
callq 0x2180
movq 0x10(%r13), %rdx
movq 0x18(%r13), %rdi
xorl %esi, %esi
callq 0x2120
jmp 0x485a
movq 0x10(%r13), %rbp
leaq -0x1(%rbp), %r14
cmpq %rbp, %r15
cmovbq %r15, %r14
movq 0x18(%r13), %rdi
movq 0x58(%rbx), %rsi
movq %r14, %rdx
callq 0x2110
movq 0x18(%r13), %rax
movb $0x0, (%rax,%r14)
movq 0x8(%rsp), %r15
jmp 0x4a95
movq 0x58(%rbx), %rsi
movq 0x60(%rbx), %r12
cmpq %rbp, %r12
cmovbq %r12, %rbp
movl 0x50(%rbx), %edi
movq %rbp, %rdx
callq 0x22d0
testq %rax, %rax
js 0x4d9b
bswapl %r14d
cmpl $0x2, %r14d
movq (%rsp), %r12
je 0x4ce8
cmpl $0x1, %r14d
je 0x4cf4
testl %r14d, %r14d
jne 0x4a95
movq 0x18(%r13), %rax
movb $0x0, (%rax)
jmp 0x4a95
movq 0x10(%r13), %r12
cmpq %rbp, %r12
ja 0x4dad
movq 0x18(%r13), %rdi
leaq 0x1(%rbp), %r12
movq %r12, %rsi
callq 0x2350
testq %rax, %rax
je 0x4da9
movq %rax, 0x18(%r13)
movq %r12, 0x10(%r13)
jmp 0x4dad
movl 0x50(%rbx), %edi
movq 0x58(%rbx), %rsi
pushq $0x8
popq %rdx
callq 0x22d0
testq %rax, %rax
movq (%rsp), %r12
jns 0x4ddb
leaq 0x1f7b(%rip), %rdi      # 0x6cc5
callq 0x2180
jmp 0x4c9f
movq 0x427d(%rip), %rax      # 0x8fd8
movq (%rax), %rcx
leaq 0x1fbe(%rip), %rdi      # 0x6d23
pushq $0x3a
popq %rsi
pushq $0x1
popq %rdx
callq 0x23a0
movb $0x0, 0x10(%rbx)
jmp 0x485a
leaq 0x1f45(%rip), %rdi      # 0x6cc5
callq 0x2180
jmp 0x485a
leaq 0x1f34(%rip), %rdi      # 0x6cc5
callq 0x2180
jmp 0x4853
leaq 0x1f23(%rip), %rdi      # 0x6cc5
callq 0x2180
jmp 0x4dd2
movq 0x10(%r13), %r12
leaq -0x1(%r12), %r14
cmpq %r12, %rbp
cmovbq %rbp, %r14
movq 0x18(%r13), %rdi
movq 0x58(%rbx), %rsi
movq %r14, %rdx
callq 0x2110
movq 0x18(%r13), %rax
movb $0x0, (%rax,%r14)
movq (%rsp), %r12
jmp 0x4a95
movq 0x58(%rbx), %rdi
addq $0x4, %rdi
callq 0x51c8
movq 0x41e9(%rip), %rax      # 0x8fd8
movq (%rax), %rdi
leaq 0x1efa(%rip), %rsi      # 0x6cf3
movq %r13, %rdx
movq %rbp, %rcx
xorl %eax, %eax
callq 0x22c0
movq 0x10(%r13), %rdx
movq 0x18(%r13), %rdi
xorl %esi, %esi
callq 0x2120
jmp 0x4c9f
movzwl 0x30(%rbx), %eax
addq %rax, 0x8(%rbx)
pushq $0x20
popq %rax
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1e95(%rip), %rdi      # 0x6cd3
callq 0x2180
pushq $0x1
popq %rdi
callq 0x2250