jadparser.c

来自「This is a resource based on j2me embedde」· C语言 代码 · 共 721 行 · 第 1/2 页

C
721
字号
            jadsmp.status = NULL_LEN;            pcsl_string_free(&jadkey_trimmed);            continue;        }        err = checkJadValueChars(&jadvalue_trimmed);        if (OUT_OF_MEMORY == err) {            jadsmp.status = OUT_OF_MEMORY;            midp_free_properties(&jadsmp);            pcsl_string_free(&jadkey_trimmed);            pcsl_string_free(&jadvalue_trimmed);            return jadsmp;        } else if (BAD_JAD_VALUE == err) {            jadsmp.status = BAD_JAD_VALUE;            pcsl_string_free(&jadkey_trimmed);            pcsl_string_free(&jadvalue_trimmed);            continue;        }        printPcslStringWithMessage("midpParseJad()", &jadkey_trimmed);        printPcslStringWithMessage("midpParseJad()", &jadvalue_trimmed);        /* Store key:value pair. */        jadsmp.pStringArr[count] = jadkey_trimmed;        jadsmp.pStringArr[count+1] = jadvalue_trimmed;        count += 2;    } /* end of for */    jadsmp = verifyJadMustProperties(jadsmp);    REPORT_INFO3(LC_AMS,                 "End jad parsing. Status=%d, count=%d, countLines=%d.",                 jadsmp.status, count, countLines);    return jadsmp;} /* end of midpParseJad */static MidpProperties verifyJadMustProperties(MidpProperties jadsmp) {    /* 5 MUST fields in JAD */    pcsl_string * name = NULL;    pcsl_string * version = NULL;    pcsl_string * vendor = NULL;    pcsl_string * jarUrl = NULL;    pcsl_string * jarSizeString = NULL;    int permittedJarSize = 0;    jint jarSizeByJad = 0;    jarUrl = midp_find_property(&jadsmp, &JAR_URL_PROP);    if (pcsl_string_is_null(jarUrl)) {        REPORT_INFO(LC_AMS,  "Missing Jar URL");        jadsmp.status = NO_JAR_URL_PROP;        return jadsmp;    }    jarSizeString = midp_find_property(&jadsmp, &JAR_SIZE_PROP);    if (pcsl_string_is_null(jarSizeString)) {        REPORT_INFO(LC_AMS,  "Missing Jar size");        jadsmp.status = NO_JAR_SIZE_PROP;        return jadsmp;    }    if (PCSL_STRING_OK !=        pcsl_string_convert_to_jint(jarSizeString, &jarSizeByJad)) {        /* NUMBER_ERROR */        REPORT_INFO1(LC_AMS, "JAD size ERROR %d", jarSizeByJad);        jadsmp.status = NUMBER_ERROR;        return jadsmp;    }    /* verify that requested jar size is not to big  */    permittedJarSize = midpGetMaxJarSizePermitted();    if (jarSizeByJad > permittedJarSize) {        REPORT_INFO2(LC_AMS, "Jar size requested by Jad is to big %d > %d",		     jarSizeByJad, permittedJarSize);        REPORT_INFO(LC_AMS,  "Out Of Storage");        jadsmp.status = OUT_OF_STORAGE;        return jadsmp;    }    name = midp_find_property(&jadsmp, &SUITE_NAME_PROP);    if (pcsl_string_is_null(name)) {        REPORT_INFO(LC_AMS, "Missing suite name");        jadsmp.status = NO_SUITE_NAME_PROP;        return jadsmp;    }    vendor = midp_find_property(&jadsmp, &SUITE_VENDOR_PROP);    if (pcsl_string_is_null(vendor)) {        REPORT_INFO(LC_AMS,  "Missing suite vendor");        jadsmp.status =  NO_SUITE_VENDOR_PROP;        return jadsmp;    }    version = midp_find_property(&jadsmp, &SUITE_VERSION_PROP);    if (pcsl_string_is_null(version)) {        REPORT_INFO(LC_AMS,  "Missing suite version");        jadsmp.status = NO_SUITE_VERSION_PROP;        return jadsmp;    }    if (!midpCheckVersion(version)) {        REPORT_INFO(LC_AMS,  "Corrupted suite version");        jadsmp.status = NO_SUITE_VERSION_PROP;        return jadsmp;    }    return jadsmp;} /* verifyJadMUSTProperties *//** * Check to see if all the chars in the key of a property are valid. * * @param jadkey key to check * @return BAD_JAD_KEY if a character is not valid for a key */static MIDPError checkJadKeyChars(const pcsl_string * jadkey) {    jchar current;    int i = 0;    MIDPError err = ALL_OK;    GET_PCSL_STRING_DATA_AND_LENGTH(jadkey)    if (PCSL_STRING_PARAMETER_ERROR(jadkey)) {        err = OUT_OF_MEMORY;    } else {        while (i < jadkey_len) {            current = jadkey_data[i];            if (current <= 0x1F             || current == 0x7F             || current == '('             || current == ')'             || current == '<'             || current == '>'             || current == '@'             || current == ','             || current == ';'             || current == '\''             || current == '"'             || current == '/'             || current == '['             || current == ']'             || current == '?'             || current == '='             || current == '{'             || current == '}'             || current == SP             || current == HT) {                printPcslStringWithMessage("checkJadKeyChars: BAD_JAD_KEY ",                                           jadkey);                err = BAD_JAD_KEY;                break;            }            i++;        } /* end of while */    }    RELEASE_PCSL_STRING_DATA_AND_LENGTH    return err;}/** * Check to see if all the chars in the value of a property are valid. * * @param value value to check * @return BAD_JAD_VALUE if a character is not valid for a value */static MIDPError checkJadValueChars(const pcsl_string * jadvalue) {    jchar current;    int i = 0;    MIDPError err = ALL_OK;    GET_PCSL_STRING_DATA_AND_LENGTH(jadvalue)    if (PCSL_STRING_PARAMETER_ERROR(jadvalue)) {        err = OUT_OF_MEMORY;    } else {        while (i < jadvalue_len) {            current = jadvalue_data[i];            /* if current is a CTL character, return an error */            if ((current <= 0x1F || current == 0x7F) && (current != HT)) {                printPcslStringWithMessage("checkJadValueChars: BAD_JAD_VALUE ",                                           jadvalue);                err = BAD_JAD_VALUE;                break;            }            i++;        } /* end of while */    }    RELEASE_PCSL_STRING_DATA_AND_LENGTH    return err;}/** * Counts a lines in jad file. Skips commented out and blank lines. * * @param buf jad file data * @return number of lines *//* IMPL NOTE: Looks like this function counts one line extra sometime. why ? */static int count_jad_lines(jchar* buf) {    int numberOfLines = 0;    if ((!buf) || (!*buf)) {        return -1;    }    while (*buf) {        /* skip commented out lines and lines that start with space */        if (COMMENTED_OUT(buf)) {            /* run to the end of line */            while (!NEW_LINE(buf)) {                buf++;            }            /* skip all the new line characters an the end of line */            while (NEW_LINE(buf)) {                buf++;            }        } else {            numberOfLines++;            while (!NEW_LINE(buf)) {                buf++;            }            while (NEW_LINE(buf)) {                buf++;            }        }    } /* end of while */    return numberOfLines;} /* end of count_jad_lines *//** * Reads a line from a jad file. * * @param jadbuf pointer to the jad jchar_buffer; this pointer will move to *               the next line * @param result pointer to pcsl_string where the new line from jad file *               will be stored, the caller is responsible for freeing this *               string * @return error code */static MIDPError readJadLine(jchar** jadbuf, pcsl_string * result) {    jchar* lineStart = NULL;    jchar* p = NULL;    int count = 0;    /* will use p to avoid all the *(*jadbuf) stuff and make it more readable */    p = (*jadbuf);    if (!(*p)) {        /* end of jchar_buffer */        *result = PCSL_STRING_NULL;        return END_OF_JAD;    }    /* skip commented out and blank lines */    while (COMMENTED_OUT(p) || NEW_LINE(p)) {        while (!NEW_LINE(p)) {            /* skip commented out line */            p++;        }        while (NEW_LINE(p)) {            /* skip new line */            p++;        }    }    lineStart = p;    for (;*p ; ) {        count++;        p++;        if (NEW_LINE(p)) {            *p = 0x00; /* cut the line */            if (*(p+1)) { /* if not end of jchar_buffer */                p++; /* point to the next line beginning */                break;            } /* end of if */        } /* end of if */    } /* end of for */    /* move jadbuf to point to the next line */    (*jadbuf) = p;    if (PCSL_STRING_OK !=        pcsl_string_convert_from_utf16(lineStart, count, result)) {        return OUT_OF_MEMORY;    }    return ALL_OK;} /* end of readJadLine *//** * Opens a file and fills the content of the file in the result_buf. <BR> * This function made memory allocation inside. * * @param filename   Path to the file. * @param result_buf Pointer to the buffer that will be filled by content of the file. * @return buffer file size in bytes */long readJadFile(const pcsl_string * filename, char** result_buf) {    int fd = 0;    char* err = NULL;    long bufsize = -1;    int numread = 0;    char* res = *result_buf;    if (pcsl_string_length(filename) <= 0) {        REPORT_INFO(LC_AMS, "readJadFile():No file name.");        return BAD_PARAMS;    }    fd = storage_open(&err, filename, OPEN_READ);    if(err != NULL) {        REPORT_INFO1(LC_AMS, "readJadFile():Can't open jad file '%s'",err);        storageFreeError(err);        return NO_JAD_FILE;    }    bufsize = storageSizeOf(&err, fd);    if((bufsize <= 0) || (err != NULL)) {        REPORT_INFO1(LC_AMS,  "readJadFile():Problem getting file size: %s",		     err );        storageFreeError(err);        return IO_ERROR;    }    res = (char*)midpMalloc(bufsize+1);    if (res == NULL || (err != NULL)) {        REPORT_INFO1(LC_AMS, "readJadFile():Can't allocate memory. %s", err);        storageFreeError(err);        return OUT_OF_MEMORY;    }    memset(res,0,(bufsize+1));    REPORT_INFO2(LC_AMS, "fd = %d, bufsize = %ld\n", fd, bufsize);    numread = storageRead(&err, fd, res, bufsize);    if((numread <= 0) || (numread != bufsize) || (err)) {        REPORT_INFO3(LC_AMS, "size = %ld, numread = %d, err = %s.",               bufsize, numread, err);        storageClose(&err, fd);        return IO_ERROR;    }    REPORT_INFO2(LC_AMS, "size = %ld, numread = %d", bufsize, numread);    storageClose(&err, fd);    if(err != NULL) {        REPORT_INFO1(LC_AMS, "Can't close jad file %s\n", err);    }    *result_buf = res;    return bufsize;} /* end of readJadFile */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?