int ASN1_mbstring_ncopy(ASN1_STRING **out, const unsigned char *in, int len, int inform, unsigned long mask, long minsize, long maxsize) { int str_type; int ret; char free_out; int outform, outlen = 0; ASN1_STRING *dest; unsigned char *p; int nchar; char strbuf[32]; int (*cpyfunc) (unsigned long, void *) = NULL; if (len == -1) len = sgx_strlen((const char *)in); if (!mask) mask = DIRSTRING_TYPE; /* First do a string check and work out the number of characters */ switch (inform) { case MBSTRING_BMP: if (len & 1) { ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ASN1_R_INVALID_BMPSTRING_LENGTH); return -1; } nchar = len >> 1; break; case MBSTRING_UNIV: if (len & 3) { ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ASN1_R_INVALID_UNIVERSALSTRING_LENGTH); return -1; } nchar = len >> 2; break; case MBSTRING_UTF8: nchar = 0; /* This counts the characters and does utf8 syntax checking */ ret = traverse_string(in, len, MBSTRING_UTF8, in_utf8, &nchar); if (ret < 0) { ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ASN1_R_INVALID_UTF8STRING); return -1; } break; case MBSTRING_ASC: nchar = len; break; default: ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ASN1_R_UNKNOWN_FORMAT); return -1; } if ((minsize > 0) && (nchar < minsize)) { ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ASN1_R_STRING_TOO_SHORT); BIO_snprintf(strbuf, sizeof strbuf, "%ld", minsize); ERR_add_error_data(2, "minsize=", strbuf); return -1; } if ((maxsize > 0) && (nchar > maxsize)) { ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ASN1_R_STRING_TOO_LONG); BIO_snprintf(strbuf, sizeof strbuf, "%ld", maxsize); ERR_add_error_data(2, "maxsize=", strbuf); return -1; } /* Now work out minimal type (if any) */ if (traverse_string(in, len, inform, type_str, &mask) < 0) { ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ASN1_R_ILLEGAL_CHARACTERS); return -1; } /* Now work out output format and string type */ outform = MBSTRING_ASC; if (mask & B_ASN1_PRINTABLESTRING) str_type = V_ASN1_PRINTABLESTRING; else if (mask & B_ASN1_IA5STRING) str_type = V_ASN1_IA5STRING; else if (mask & B_ASN1_T61STRING) str_type = V_ASN1_T61STRING; else if (mask & B_ASN1_BMPSTRING) { str_type = V_ASN1_BMPSTRING; outform = MBSTRING_BMP; } else if (mask & B_ASN1_UNIVERSALSTRING) { str_type = V_ASN1_UNIVERSALSTRING; outform = MBSTRING_UNIV; } else { str_type = V_ASN1_UTF8STRING; outform = MBSTRING_UTF8; } if (!out) return str_type; if (*out) { free_out = 0; dest = *out; if (dest->data) { dest->length = 0; OPENSSL_free(dest->data); dest->data = NULL; } dest->type = str_type; } else { free_out = 1; dest = ASN1_STRING_type_new(str_type); if (!dest) { ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ERR_R_MALLOC_FAILURE); return -1; } *out = dest; } /* If both the same type just copy across */ if (inform == outform) { if (!ASN1_STRING_set(dest, in, len)) { ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ERR_R_MALLOC_FAILURE); return -1; } return str_type; } /* Work out how much space the destination will need */ switch (outform) { case MBSTRING_ASC: outlen = nchar; cpyfunc = cpy_asc; break; case MBSTRING_BMP: outlen = nchar << 1; cpyfunc = cpy_bmp; break; case MBSTRING_UNIV: outlen = nchar << 2; cpyfunc = cpy_univ; break; case MBSTRING_UTF8: outlen = 0; traverse_string(in, len, inform, out_utf8, &outlen); cpyfunc = cpy_utf8; break; } if (!(p = OPENSSL_malloc(outlen + 1))) { if (free_out) ASN1_STRING_free(dest); ASN1err(ASN1_F_ASN1_MBSTRING_NCOPY, ERR_R_MALLOC_FAILURE); return -1; } dest->length = outlen; dest->data = p; p[outlen] = 0; traverse_string(in, len, inform, cpyfunc, &p); return str_type; }
int main() { t_node *head; t_node *n1; t_node *n2; int *i1; int *i2; int *i3; int i; int j; int k; char *c1; char *c2; char *c3; char a; char b; char c; head = (t_node*)xmalloc(10*sizeof(t_node)); /*new_node*/ n1 = new_node("one\n", NULL); my_str(n1->elem);/*prints one*/ n1->elem = NULL; free(n1); n1 = new_node(NULL, NULL); if(!n1->elem) my_str("create NULL node ok\n"); else my_str("create NULL node FAIL!\n"); free(n1); n1 = new_node("a", NULL); n2 = new_node("b", n1); my_str(n2->elem); my_str((n2->next)->elem);/*prints ba*/ my_char('\n'); my_str("---------------------------------------------------------------------\n"); /*add_node*/ add_node(n1, &head); my_str((*head).elem);/*prints a*/ my_char('\n'); add_node(n2, &head); my_str((*head).elem);/*prints b*/ my_char('\n'); add_node(NULL, &head); if(strcmp((*head).elem, n2->elem) == 0) my_str("add NULL ok\n"); else my_str("add NULL FAIL!\n"); add_node(new_node(NULL, NULL), &head); if(strcmp((*head).elem, n2->elem) == 0) my_str("add NULL node ok\n"); else my_str("add NULL node FAIL!\n"); add_node(new_node("something", NULL), NULL);/*if this line doesn't segfault then we're good!*/ my_str("---------------------------------------------------------------------\n"); /*traversals*/ empty_list(&head); i = 3; i1 = &i; add_node(new_node(i1, NULL), &head); j = 2; i2 = &j; add_node(new_node(i2, NULL), &head); k = 1; i3 = &k; add_node(new_node(i3, NULL), &head); traverse_int(head);/*prints 1 2 3 */ my_char('\n'); head->next->elem = NULL; traverse_int(head);/*prints 1 NULL 3*/ my_char('\n'); traverse_int(NULL);/*prints The list is empty!*/ empty_list(&head); c = 'c'; c1 = &c; add_node(new_node(c1, NULL), &head); b = 'b'; c2 = &b; add_node(new_node(c2, NULL), &head); a = 'a'; c3 = &a; add_node(new_node(c3, NULL), &head); traverse_char(head);/*prints a b c */ my_char('\n'); head->elem = NULL; traverse_char(head);/*prints NULL b c*/ my_char('\n'); traverse_char(NULL);/*prints The list is empty!*/ empty_list(&head); add_node(new_node("third", NULL), &head); add_node(new_node("second", NULL), &head); add_node(new_node("first", NULL), &head); traverse_string(head);/*prints first second third */ my_char('\n'); head->next->next->elem = NULL; traverse_string(head);/*prints first second NULL*/ my_char('\n'); traverse_string(NULL);/*prints The list is empty!*/ empty_list(&head); my_str("---------------------------------------------------------------------\n"); /*add_elem*/ add_elem("a", &head); add_elem("b", &head); add_elem("c", &head); my_str(head->elem);/*prints c*/ my_char('\n'); add_elem(NULL, &head); if(strcmp(head->elem, "c") == 0) my_str("add NULL elem ok\n"); else my_str("add NULL elem FAIL!\n"); my_str("---------------------------------------------------------------------\n"); /*append*/ append(new_node("z", NULL), &head); traverse_string(head);/*prints c b a z*/ my_char('\n'); append(NULL, &head); traverse_string(head);/*prints c b a z*/ my_char('\n'); append(new_node("stuff", NULL), NULL);/*if this line doesn't segfault then we're good!*/ my_str("---------------------------------------------------------------------\n"); /*add_node_at*/ add_node_at(new_node("d", NULL), &head, 0); traverse_string(head);/*prints d c b a z*/ my_char('\n'); add_node_at(new_node("y", NULL), &head, 42); traverse_string(head);/*prints d c b a z y*/ my_char('\n'); add_node_at(new_node("0", NULL), &head, 4); traverse_string(head);/*prints d c b a 0 z y*/ my_char('\n'); add_node_at(NULL, &head, 2); traverse_string(head);/*prints d c b a 0 z y*/ my_char('\n'); add_node_at(new_node(NULL, NULL), &head, 1); traverse_string(head);/*prints d c b a 0 z y*/ my_char('\n'); add_node_at(new_node("something", NULL), NULL, 7);/*if this line doesn't segfault then we're good!*/ my_str("---------------------------------------------------------------------\n"); /*remove_node*/ my_str(remove_node(&head));/*prints d*/ my_str(": "); traverse_string(head); my_char('\n'); if(!remove_node(NULL)) my_str("remove node from NULL ok\n"); else my_str("remove node from NULL FAIL!\n"); my_str("---------------------------------------------------------------------\n"); /*remove_node_at*/ my_str(remove_node_at(&head, 0));/*prints c*/ my_str(": "); traverse_string(head); my_char('\n'); my_str(remove_node_at(&head, 42));/*prints y*/ my_str(": "); traverse_string(head); my_char('\n'); my_str(remove_node_at(&head, 2));/*prints 0*/ my_str(": "); traverse_string(head); my_char('\n'); if(!remove_node_at(NULL, 100)) my_str("remove node from NULL ok\n"); else my_str("remove node from NULL FAIL!\n"); my_str("---------------------------------------------------------------------\n"); /*remove_last*/ my_str(remove_last(&head));/*prints z*/ my_str(": "); traverse_string(head); my_char('\n'); if(!remove_last(NULL)) my_str("remove last from NULL ok\n"); else my_str("remove last from NULL FAIL!\n"); my_str("---------------------------------------------------------------------\n"); /*count_nodes*/ my_int(count_nodes(head));/*prints 2*/ my_char('\n'); my_int(count_nodes(NULL));/*prints 0*/ my_char('\n'); my_str("---------------------------------------------------------------------\n"); /*node_at*/ my_str((node_at(head, 1))->elem);/*prints a*/ my_char('\n'); my_str((node_at(head, 0))->elem);/*prints b*/ my_char('\n'); my_str((node_at(head, 42))->elem);/*prints a*/ my_char('\n'); if(!node_at(NULL, 12)) my_str("node at with NULL ok\n"); else my_str("node at with NULL FAIL!\n"); my_str("---------------------------------------------------------------------\n"); /*elem_at*/ my_str(elem_at(head, 0));/*prints b*/ my_char('\n'); my_str(elem_at(head, 1));/*prints a*/ my_char('\n'); my_str(elem_at(head, 42));/*prints a*/ my_char('\n'); if(!elem_at(NULL, 3)) my_str("elem at with NULL ok\n"); else my_str("elem at with NULL FAIL!\n"); my_str("---------------------------------------------------------------------\n"); /*empty_list*/ my_int(count_nodes(head));/*prints 2*/ my_char('\n'); empty_list(&head); my_int(count_nodes(head));/*prints 0*/ my_char('\n'); empty_list(NULL);/*if this doesn't segfault then we're good!*/ my_str("---------------------------------------------------------------------\n"); free(head); return 0; }