// File: /workspace/github_c_makefile_star2_O2/rchiossi[P]dexterity/lib/dex_string.c
// Function: dx_string_add_stringdata
void dx_string_add_stringdata(Dex* dx, uint32_t index, char* str) {
  DexStringDataItem* sdata;
  int32_t delta;
  unsigned int i;

  if (dx == NULL || index >= dx->header->string_ids_size) return; 

  sdata = (DexStringDataItem*) malloc_s(sizeof(DexStringDataItem));

  sdata->meta.corrupted = false;
  sdata->meta.offset = dx->string_ids[index]->string_data_off;

  uitoul128(&(sdata->size),strlen(str));
  DX_ALLOC_LIST(uint8_t,sdata->data,ul128toui(sdata->size)*3+1);

  strncpy((char *)sdata->data,str,strlen(str));
  sdata->data[strlen(str)] = 0x0;

  //Fix references
  delta = sdata->size.size + ul128toui(sdata->size) + 1;
  dx_shift_offset(dx,sdata->meta.offset,delta);

  //Restore offset
  dx->string_ids[index]->string_data_off = sdata->meta.offset;

  //Add new stringdata to the string_data list
  if (dx->meta.string_data_list_size >= dx->meta.string_data_list_alloc)
    DX_REALLOC_LIST(DexStringDataItem*,dx->string_data_list,dx->meta.string_data_list_alloc);
  
  for (i=dx->meta.string_data_list_size; i>index; i--)
    dx->string_data_list[i] = dx->string_data_list[i-1];

  dx->meta.string_data_list_size++;  

  dx->string_data_list[index] = sdata;

  //Fix Maps
  for (i=0; i<dx->map_list->size; i++) {
    if (dx->map_list->list[i]->type == 0x2002) {
      dx->map_list->list[i]->size++;
    }    
  }

  //Fix header
  if (index == 0) 
    dx->header->data_size += delta;

  //Fix filesize
  dx->header->file_size += delta;  

  //Fix datasize
  dx->header->data_size += delta;  
}

// Assembly:
testq %rdi, %rdi
je 0x8321
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %esi, %ebp
movq %rdi, %rbx
movq (%rdi), %rax
cmpl %esi, 0x40(%rax)
jbe 0x8313
movq %rdx, %r15
pushq $0x20
popq %rdi
callq 0x46a0
movq %rax, %r14
movb $0x0, (%rax)
movq 0x10(%rbx), %rax
movl %ebp, %r13d
movq (%rax,%r13,8), %rax
movl 0x8(%rax), %eax
movl %eax, 0x4(%r14)
leaq 0x8(%r14), %r12
movq %r15, %rdi
callq 0x4200
movq %r12, %rdi
movl %eax, %esi
callq 0x44d0
movq 0x8(%r14), %rdi
movl 0x10(%r14), %esi
callq 0x4180
cmpl $0x55555555, %eax       # imm = 0x55555555
jne 0x81fa
xorl %r12d, %r12d
jmp 0x8214
movq 0x8(%r14), %rdi
movl 0x10(%r14), %esi
callq 0x4180
leal (%rax,%rax,2), %edi
incl %edi
callq 0x46a0
movq %rax, %r12
movq %r12, 0x18(%r14)
movq %r15, %rdi
callq 0x4200
movq %r12, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x4080
movq 0x18(%r14), %r12
movq %r15, %rdi
callq 0x4200
movb $0x0, (%r12,%rax)
movl 0x10(%r14), %r15d
movq 0x8(%r14), %rdi
movl %r15d, %esi
callq 0x4180
addl %eax, %r15d
incl %r15d
movl 0x4(%r14), %esi
movq %rbx, %rdi
movl %r15d, %edx
callq 0x4610
movl 0x4(%r14), %eax
movq 0x10(%rbx), %rcx
movq (%rcx,%r13,8), %rcx
movl %eax, 0x8(%rcx)
movq 0xc8(%rbx), %rax
movq 0xd0(%rbx), %rsi
cmpq %rsi, %rax
jb 0x82a5
movq 0x40(%rbx), %rdi
shlq $0x4, %rsi
callq 0x4640
movq %rax, 0x40(%rbx)
shlq 0xd0(%rbx)
movq 0xc8(%rbx), %rax
movl %eax, %ecx
cmpq %r13, %rcx
jbe 0x82c3
movq 0x40(%rbx), %rdx
leaq -0x1(%rcx), %rsi
movl %esi, %edi
movq (%rdx,%rdi,8), %rdi
movq %rdi, (%rdx,%rcx,8)
movq %rsi, %rcx
jmp 0x82a7
incq %rax
movq %rax, 0xc8(%rbx)
movq 0x40(%rbx), %rax
movq %r14, (%rax,%r13,8)
movq 0x8(%rbx), %rax
movl 0x8(%rax), %ecx
xorl %edx, %edx
cmpq %rcx, %rdx
jae 0x82fb
movq 0x10(%rax), %rsi
movq (%rsi,%rdx,8), %rsi
cmpw $0x2002, 0x8(%rsi)      # imm = 0x2002
jne 0x82f6
incl 0xc(%rsi)
incq %rdx
jmp 0x82de
xorl %eax, %eax
testl %ebp, %ebp
movq (%rbx), %rcx
cmovel %r15d, %eax
addl 0x70(%rcx), %eax
addl %r15d, 0x28(%rcx)
addl %r15d, %eax
movl %eax, 0x70(%rcx)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq