// File: /workspace/github_c_makefile_star2_O1/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 0x86a0
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 0x8692
movq %rdx, %r15
movl $0x20, %edi
callq 0x4600
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 0x41c0
movq %r12, %rdi
movl %eax, %esi
callq 0x4450
movq 0x8(%r14), %rdi
movl 0x10(%r14), %esi
callq 0x4170
cmpl $0x55555555, %eax       # imm = 0x55555555
jne 0x857a
xorl %r12d, %r12d
jmp 0x8594
movq 0x8(%r14), %rdi
movl 0x10(%r14), %esi
callq 0x4170
leal (%rax,%rax,2), %edi
incl %edi
callq 0x4600
movq %rax, %r12
movq %r12, 0x18(%r14)
movq %r15, %rdi
callq 0x41c0
movq %r12, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x4080
movq 0x18(%r14), %r12
movq %r15, %rdi
callq 0x41c0
movb $0x0, (%r12,%rax)
movl 0x10(%r14), %r15d
movq 0x8(%r14), %rdi
movl %r15d, %esi
callq 0x4170
addl %eax, %r15d
incl %r15d
movl 0x4(%r14), %esi
movq %rbx, %rdi
movl %r15d, %edx
callq 0x4580
movl 0x4(%r14), %eax
movq 0x10(%rbx), %rcx
movq (%rcx,%r13,8), %rcx
movl %eax, 0x8(%rcx)
movq 0xd0(%rbx), %rsi
cmpq %rsi, 0xc8(%rbx)
jb 0x861b
movq 0x40(%rbx), %rdi
shlq $0x4, %rsi
callq 0x45b0
movq %rax, 0x40(%rbx)
shlq 0xd0(%rbx)
movq 0xc8(%rbx), %rax
cmpl %ebp, %eax
jbe 0x8641
movl %eax, %ecx
movq 0x40(%rbx), %rdx
movq -0x8(%rdx,%rcx,8), %rsi
movq %rsi, (%rdx,%rcx,8)
leaq -0x1(%rcx), %rdx
movq %rdx, %rcx
cmpq %r13, %rdx
ja 0x8628
incq %rax
movq %rax, 0xc8(%rbx)
movq 0x40(%rbx), %rax
movq %r14, (%rax,%r13,8)
movq 0x8(%rbx), %rcx
movl 0x8(%rcx), %eax
testq %rax, %rax
je 0x867c
movq 0x10(%rcx), %rcx
xorl %edx, %edx
movq (%rcx,%rdx,8), %rsi
cmpw $0x2002, 0x8(%rsi)      # imm = 0x2002
jne 0x8674
incl 0xc(%rsi)
incq %rdx
cmpq %rdx, %rax
jne 0x8665
testl %ebp, %ebp
jne 0x8687
movq (%rbx), %rax
addl %r15d, 0x70(%rax)
movq (%rbx), %rax
addl %r15d, 0x28(%rax)
addl %r15d, 0x70(%rax)
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq