list_t *_be_decode_to_list(const char *str, int *offset) { list_t *top, *p; if(*str == 'e') { return NULL; } top = p = (list_t *)malloc(sizeof(list_t)); do { int n; p->bencode = (bencode_t *)malloc(sizeof(bencode_t)); str += (n = be_decode(str, p->bencode)); if(offset != NULL) { *offset += n; } if(*str != 'e') { p->next = (list_t *)malloc(sizeof(list_t)); p = p->next; } else { p->next = NULL; } }while(*str != 'e'); if(offset != NULL) { *offset += 1; // 'e' } return top; }
list_t *_be_decode_to_dict(const char *str, int *offset) { list_t *top, *p; bencode_t *pair; if(*str == 'e') { return NULL; } top = p = (list_t *)malloc(sizeof(list_t)); do { int n; int m; pair = (bencode_t *)malloc(sizeof(bencode_t) * 2); str += (n = be_decode(str, pair)); str += (m = be_decode(str, pair + 1)); if(offset != NULL) { *offset += n + m; } p->bencode = pair; if(*str != 'e') { p->next = (list_t *)malloc(sizeof(list_t)); p = p->next; } else { p->next = NULL; } }while(*str != 'e'); if(offset != NULL) { *offset += 1; } return top; }
be_node *be_create_list() { be_node *n = be_decode("le"); return n; }
/* hackish way to create nodes! */ be_node *be_create_dict() { be_node *n = be_decode("de"); return n; }