Back to index

im-sdk  12.3.91
comp-icattribute.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <string.h>
00004 #include <sys/types.h>
00005 
00006 #include <iiimp-data.h>
00007 
00008 #include "iiimp-dataP.h"
00009 
00010 
00011 static IIIMP_icattribute *
00012 iiimp_icattribute_new(
00013     IIIMP_data_s *   data_s,
00014     IIIMP_card16     id,
00015     void *           value,
00016     IIIMP_card16     input_method)
00017 {
00018     IIIMP_icattribute *     attr;
00019 
00020     attr = (IIIMP_icattribute *)malloc(sizeof (IIIMP_icattribute));
00021     if (NULL == attr) {
00022        data_s->status = IIIMP_DATA_MALLOC_ERROR;
00023        return NULL;
00024     }
00025 
00026     attr->nbyte = (2 + 2);
00027     attr->id = id;
00028     attr->next = NULL;
00029 
00030     switch(id) {
00031     case IIIMP_ICATTRIBUTE_INPUT_LANGUAGE: /* Input Language */
00032        attr->value.input_language = value;
00033        attr->value_nbyte = attr->value.input_language->nbyte;
00034        break;
00035     case IIIMP_ICATTRIBUTE_CHARACTER_SUBSETS: /* Character Subsets */
00036        attr->value.character_subsets = value;
00037        attr->value_nbyte = attr->value.character_subsets->nbyte;
00038        break;
00039     case IIIMP_ICATTRIBUTE_INPUT_METHOD_NAME: /* Input Mehtod Name */
00040        attr->value.input_method_name = value;
00041        attr->value_nbyte = attr->value.input_method_name->nbyte;
00042        break;
00043     case IIIMP_ICATTRIBUTE_INPUT_METHOD: /* Input Mehtod */
00044        attr->value.input_method = input_method;
00045        attr->value_nbyte = 2;
00046        break;
00047     default:
00048        break;
00049     }
00050 
00051     attr->nbyte += (attr->value_nbyte + PAD(attr->value_nbyte));
00052 
00053     return attr;
00054 }
00055 
00056 
00057 void
00058 iiimp_icattribute_delete(IIIMP_data_s * data_s, IIIMP_icattribute * icattr)
00059 {
00060     if (NULL == icattr) return;
00061 
00062     switch(icattr->id) {
00063     case IIIMP_ICATTRIBUTE_INPUT_LANGUAGE: /* Input Language */
00064        iiimp_string_delete(data_s, icattr->value.input_language);
00065        break;
00066     case IIIMP_ICATTRIBUTE_CHARACTER_SUBSETS: /* Character Subsets */
00067        iiimp_card32_list_delete(data_s, icattr->value.character_subsets);
00068        break;
00069     case IIIMP_ICATTRIBUTE_INPUT_METHOD_NAME: /* Input Mehtod Name */
00070        iiimp_string_delete(data_s, icattr->value.input_method_name);
00071        break;
00072     case IIIMP_ICATTRIBUTE_INPUT_METHOD: /* Input Mehtod */
00073        break;
00074     default:
00075        break;
00076     }
00077     free(icattr);
00078 
00079     return;
00080 }
00081 
00082 
00083 void
00084 iiimp_icattribute_list_delete(IIIMP_data_s * data_s, IIIMP_icattribute * icattr)
00085 {
00086     IIIMP_icattribute *     icattr_next;
00087     for (; NULL != icattr; icattr = icattr_next) {
00088        icattr_next = icattr->next;
00089 
00090        switch(icattr->id) {
00091        case IIIMP_ICATTRIBUTE_INPUT_LANGUAGE:
00092            iiimp_string_delete(data_s, icattr->value.input_language);
00093            break;
00094        case IIIMP_ICATTRIBUTE_CHARACTER_SUBSETS:
00095            iiimp_card32_list_delete(data_s, icattr->value.character_subsets);
00096            break;
00097        case IIIMP_ICATTRIBUTE_INPUT_METHOD_NAME:
00098            iiimp_string_delete(data_s, icattr->value.input_method_name);
00099            break;
00100        case IIIMP_ICATTRIBUTE_INPUT_METHOD:
00101            /* nothing to do */
00102            break;
00103        default:
00104            break;
00105        }
00106        free(icattr);
00107     }
00108     return;
00109 }
00110 
00111 
00112 IIIMP_icattribute *
00113 iiimp_icattribute_input_language_new(
00114     IIIMP_data_s *   data_s,
00115     IIIMP_string *   value)
00116 {
00117     return iiimp_icattribute_new(data_s, IIIMP_ICATTRIBUTE_INPUT_LANGUAGE,
00118                              (void *)value, 0);
00119 }
00120 
00121 
00122 IIIMP_icattribute *
00123 iiimp_icattribute_character_subsets_new(
00124     IIIMP_data_s *   data_s,
00125     IIIMP_card32_list *     value)
00126 {
00127     return iiimp_icattribute_new(data_s, IIIMP_ICATTRIBUTE_CHARACTER_SUBSETS,
00128                              (void *)value, 0);
00129 }
00130 
00131 
00132 IIIMP_icattribute *
00133 iiimp_icattribute_input_method_name_new(
00134     IIIMP_data_s *   data_s,
00135     IIIMP_string *   value)
00136 {
00137     return iiimp_icattribute_new(data_s, IIIMP_ICATTRIBUTE_INPUT_METHOD_NAME,
00138                              (void *)value, 0);
00139 }
00140 
00141 
00142 IIIMP_icattribute *
00143 iiimp_icattribute_input_method_new(
00144     IIIMP_data_s *   data_s,
00145     IIIMP_card16     value)
00146 {
00147     return iiimp_icattribute_new(data_s, IIIMP_ICATTRIBUTE_INPUT_METHOD,
00148                              NULL, value);
00149 }
00150 
00151 
00152 void
00153 iiimp_icattribute_pack(
00154     IIIMP_data_s *   data_s,
00155     IIIMP_icattribute *     m,
00156     size_t *         nbyte,
00157     uchar_t **              ptr)
00158 {
00159     size_t    rest;
00160     uchar_t * p;
00161 
00162     rest = *nbyte;
00163     p = *ptr;
00164 
00165     PUTU16(m->id, rest, p, data_s->byte_swap);
00166     PUTU16(m->value_nbyte, rest, p, data_s->byte_swap);
00167 
00168     switch(m->id) {
00169     case IIIMP_ICATTRIBUTE_INPUT_LANGUAGE: /* Input Language */
00170        iiimp_string_pack(data_s, m->value.input_language, &rest, &p);
00171        break;
00172     case IIIMP_ICATTRIBUTE_CHARACTER_SUBSETS: /* Character Subsets */
00173        iiimp_card32_list_pack(data_s, m->value.character_subsets, &rest, &p);
00174        break;
00175     case IIIMP_ICATTRIBUTE_INPUT_METHOD_NAME: /* Input Mehtod Name */
00176        iiimp_string_pack(data_s, m->value.input_method_name, &rest, &p);
00177        break;
00178     case IIIMP_ICATTRIBUTE_INPUT_METHOD: /* Input Mehtod */
00179        PUTU16(m->value.input_method, rest, p, data_s->byte_swap);
00180        PUTU16(0, rest, p, data_s->byte_swap);
00181        break;
00182     }
00183 
00184     *nbyte = rest;
00185     *ptr = p;
00186 
00187     return;
00188 }
00189 
00190 
00191 void
00192 iiimp_icattribute_list_pack(
00193     IIIMP_data_s *   data_s,
00194     IIIMP_icattribute *     m,
00195     size_t *         nbyte,
00196     uchar_t **              ptr)
00197 {
00198     size_t    rest;
00199     uchar_t * p;
00200 
00201     rest = *nbyte;
00202     p = *ptr;
00203 
00204     for (; NULL != m; m = m->next) {
00205        iiimp_icattribute_pack(data_s, m, &rest, &p);
00206     }
00207 
00208     *nbyte = rest;
00209     *ptr = p;
00210 
00211     return;
00212 }
00213 
00214 
00215 IIIMP_icattribute *
00216 iiimp_icattribute_unpack(
00217     IIIMP_data_s *   data_s,
00218     size_t *         nbyte,
00219     const uchar_t ** ptr,
00220     size_t           nbyte_max)
00221 {
00222     IIIMP_icattribute *     icattr;
00223     size_t           rest;
00224     const uchar_t *  p;
00225     IIIMP_card16     id;
00226     int                     len;
00227 
00228     rest = nbyte_max;
00229     p = *ptr;
00230 
00231     if ((*nbyte < rest) || (rest < 8)) {
00232        data_s->status = IIIMP_DATA_INVALID;
00233        return NULL;
00234     }
00235 
00236     GETU16(id, rest, p, data_s->byte_swap);
00237     GETU16(len, rest, p, data_s->byte_swap);
00238     if (0 == len) {
00239        data_s->status = IIIMP_DATA_NO_ERROR;
00240        return NULL;
00241     }
00242     if ((len < 0) || (rest < len)) {
00243        data_s->status = IIIMP_DATA_INVALID;
00244        return NULL;
00245     }
00246 
00247     icattr = (IIIMP_icattribute *)malloc(sizeof (IIIMP_icattribute));
00248     if (NULL == icattr) {
00249        data_s->status = IIIMP_DATA_MALLOC_ERROR;
00250        return NULL;
00251     }
00252 
00253     icattr->value_nbyte = len;
00254     icattr->next = NULL;
00255 
00256     switch(id) {
00257     case IIIMP_ICATTRIBUTE_INPUT_LANGUAGE: /* Input Language */
00258        icattr->value.input_language =
00259            iiimp_string_unpack(data_s, &rest, &p, len);
00260        if (NULL == icattr->value.input_language) {
00261            iiimp_icattribute_delete(data_s, icattr);
00262            return NULL;
00263        }
00264        break;
00265     case IIIMP_ICATTRIBUTE_CHARACTER_SUBSETS: /* Character Subsets */
00266        if (0 < len) {
00267            icattr->value.character_subsets =
00268               iiimp_card32_list_unpack(data_s, &rest, &p, len);
00269            if (NULL == icattr->value.character_subsets) {
00270               iiimp_icattribute_delete(data_s, icattr);
00271               return NULL;
00272            }
00273        } else {
00274            icattr->value.character_subsets = NULL;
00275        }
00276        break;
00277     case IIIMP_ICATTRIBUTE_INPUT_METHOD_NAME: /* Input Mehtod Name */
00278        icattr->value.input_method_name =
00279            iiimp_string_unpack(data_s, &rest, &p, len);
00280        if (NULL == icattr->value.input_method_name) {
00281            iiimp_icattribute_delete(data_s, icattr);
00282            return NULL;
00283        }
00284        break;
00285     case IIIMP_ICATTRIBUTE_INPUT_METHOD: /* Input Mehtod */
00286        GETU16(icattr->value.input_method, rest, p, data_s->byte_swap);
00287        SKIP16(rest, p);
00288        break;
00289     default:
00290        break;
00291     }
00292 
00293     icattr->id = id;
00294 
00295     *nbyte = rest;
00296     *ptr = p;
00297 
00298     return icattr;
00299 }
00300 
00301 
00302 IIIMP_icattribute *
00303 iiimp_icattribute_list_unpack(
00304     IIIMP_data_s *   data_s,
00305     size_t *         nbyte,
00306     const uchar_t ** ptr,
00307     size_t           nbyte_max)
00308 {
00309     IIIMP_icattribute *     attr;
00310     size_t           rest;
00311     const uchar_t *  p;
00312     IIIMP_icattribute *     attr_first;
00313     IIIMP_icattribute *     attr_last;
00314 
00315     rest = nbyte_max;
00316     p = *ptr;
00317     attr_first = NULL;
00318     attr_last = NULL;
00319 
00320     if (((*nbyte) < nbyte_max) || (0 != (rest & 0x01))) {
00321        data_s->status = IIIMP_DATA_INVALID;
00322        return NULL;
00323     }
00324 
00325     while (0 < rest) {
00326        attr = iiimp_icattribute_unpack(data_s, &rest, &p, rest);
00327        if (NULL == attr) {
00328            iiimp_icattribute_list_delete(data_s, attr_first);
00329            return NULL;
00330        } else {
00331            if (NULL == attr_first) {
00332               attr_first = attr;
00333            } else {
00334               attr_last->next = attr;
00335            }
00336            attr_last = attr;
00337        }
00338     }
00339 
00340     *nbyte -= (nbyte_max - rest);
00341     *ptr = p;
00342 
00343     return attr_first;
00344 }
00345 void
00346 iiimp_icattribute_print(
00347     IIIMP_data_s *   data_s,
00348     IIIMP_icattribute *     m)
00349 {
00350     if (NULL == m) return;
00351 
00352     switch (m->id) {
00353     case IIIMP_ICATTRIBUTE_INPUT_LANGUAGE:       /* Input Language */
00354        (void)fprintf(data_s->print_fp, "\tInput Language=");
00355        iiimp_string_print(data_s, m->value.input_language);
00356        (void)fputc('\n', data_s->print_fp);
00357        break;
00358     case IIIMP_ICATTRIBUTE_CHARACTER_SUBSETS:    /* Character Subsets */
00359        (void)fprintf(data_s->print_fp, "\tcharacter subsets=(");
00360        iiimp_character_subsets_list_print(data_s, m->value.character_subsets);
00361        (void)fprintf(data_s->print_fp, ")\n");
00362        break;
00363     case IIIMP_ICATTRIBUTE_INPUT_METHOD_NAME:    /* Input Method Name */
00364        (void)fprintf(data_s->print_fp, "\tInput Method=");
00365        iiimp_string_print(data_s, m->value.input_language);
00366        (void)fputc('\n', data_s->print_fp);
00367        break;
00368     case IIIMP_ICATTRIBUTE_INPUT_METHOD:  /* Input Method */
00369        (void)fprintf(data_s->print_fp, "\tInput Method=%d",
00370                     m->value.input_method);
00371        (void)fputc('\n', data_s->print_fp);
00372        break;
00373     }
00374 }
00375 
00376 
00377 void
00378 iiimp_icattribute_list_print(
00379     IIIMP_data_s *   data_s,
00380     IIIMP_icattribute *     m)
00381 {
00382     for (; NULL != m; m = m->next) {
00383        iiimp_icattribute_print(data_s, m);
00384     }
00385 }
00386 
00387 
00388 /* Local Variables: */
00389 /* c-file-style: "iiim-project" */
00390 /* End: */