a_mbstr.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/* a_mbstr.c */
/* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
* project 1999.
*/
/* ====================================================================
* Copyright (c) 1999 The OpenSSL Project. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* distribution.
*
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
*
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please contact
* licensing@OpenSSL.org.
*
* 5. Products derived from this software may not be called "OpenSSL"
* nor may "OpenSSL" appear in their names without prior written
* permission of the OpenSSL Project.
*
* 6. Redistributions of any form whatsoever must retain the following
* acknowledgment:
* "This product includes software developed by the OpenSSL Project
* for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
*
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
* ====================================================================
*
* This product includes cryptographic software written by Eric Young
* (eay@cryptsoft.com). This product includes software written by Tim
* Hudson (tjh@cryptsoft.com).
*
*/
#include <stdio.h>
#include <ctype.h>
#include "cryptlib.h"
static int is_printable(unsigned long value);
/* These functions take a string in UTF8, ASCII or multibyte form and
* a mask of permissible ASN1 string types. It then works out the minimal
* type (using the order Printable < IA5 < T61 < BMP < Universal < UTF8)
* and creates a string of the correct type with the supplied data.
* Yes this is horrible: it has to be :-(
* The 'ncopy' form checks minimum and maximum size limits too.
*/
{
}
{
int str_type;
int ret;
char free_out;
unsigned char *p;
int nchar;
char strbuf[32];
/* First do a string check and work out the number of characters */
switch(inform) {
case MBSTRING_BMP:
if(len & 1) {
return -1;
}
break;
case MBSTRING_UNIV:
if(len & 3) {
return -1;
}
break;
case MBSTRING_UTF8:
nchar = 0;
/* This counts the characters and does utf8 syntax checking */
if(ret < 0) {
return -1;
}
break;
case MBSTRING_ASC:
break;
default:
return -1;
}
return -1;
}
return -1;
}
/* Now work out minimal type (if any) */
return -1;
}
/* Now work out output format and string type */
else if(mask & B_ASN1_BMPSTRING) {
} else if(mask & B_ASN1_UNIVERSALSTRING) {
} else {
}
if(*out) {
free_out = 0;
}
} else {
free_out = 1;
if(!dest) {
return -1;
}
}
/* If both the same type just copy across */
return -1;
}
return str_type;
}
/* Work out how much space the destination will need */
switch(outform) {
case MBSTRING_ASC:
break;
case MBSTRING_BMP:
break;
case MBSTRING_UNIV:
break;
case MBSTRING_UTF8:
outlen = 0;
break;
}
return -1;
}
p[outlen] = 0;
return str_type;
}
/* This function traverses a string and passes the value of each character
* to an optional function along with a void * argument.
*/
{
unsigned long value;
int ret;
while(len) {
if(inform == MBSTRING_ASC) {
value = *p++;
len--;
} else if(inform == MBSTRING_BMP) {
value = *p++ << 8;
value |= *p++;
len -= 2;
} else if(inform == MBSTRING_UNIV) {
value = ((unsigned long)*p++) << 24;
value |= ((unsigned long)*p++) << 16;
value |= *p++ << 8;
value |= *p++;
len -= 4;
} else {
if(ret < 0) return -1;
p += ret;
}
if(rfunc) {
}
}
return 1;
}
/* Various utility functions for traverse_string */
/* Just count number of characters */
{
int *nchar;
(*nchar)++;
return 1;
}
/* Determine size of output as a UTF8 String */
{
int *outlen;
return 1;
}
/* Determine the "type" of a string: check each character against a
* supplied "mask".
*/
{
unsigned long types;
types &= ~B_ASN1_IA5STRING;
types &= ~B_ASN1_T61STRING;
types &= ~B_ASN1_BMPSTRING;
if(!types) return -1;
return 1;
}
/* Copy one byte per character ASCII like strings */
{
unsigned char **p, *q;
p = arg;
q = *p;
*q = (unsigned char) value;
(*p)++;
return 1;
}
/* Copy two byte per character BMPStrings */
{
unsigned char **p, *q;
p = arg;
q = *p;
*q = (unsigned char) (value & 0xff);
*p += 2;
return 1;
}
/* Copy four byte per character UniversalStrings */
{
unsigned char **p, *q;
p = arg;
q = *p;
*q = (unsigned char) (value & 0xff);
*p += 4;
return 1;
}
/* Copy to a UTF8String */
{
unsigned char **p;
int ret;
p = arg;
/* We already know there is enough room so pass 0xff as the length */
*p += ret;
return 1;
}
/* Return 1 if the character is permitted in a PrintableString */
static int is_printable(unsigned long value)
{
int ch;
if(value > 0x7f) return 0;
/* Note: we can't use 'isalnum' because certain accented
* characters may count as alphanumeric in some environments.
*/
#ifndef CHARSET_EBCDIC
#else /*CHARSET_EBCDIC*/
#endif /*CHARSET_EBCDIC*/
return 0;
}