Back to index

im-sdk  12.3.91
comp-utf8string.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <string.h>
00004 #include <ctype.h>
00005 #include <sys/types.h>
00006 #if defined(lint) && defined(HAVE_NOTE_H)
00007 #include <note.h>
00008 #endif /* lint && HAVE_NOTE_H */
00009 
00010 #include <iiimp-data.h>
00011 
00012 #include "iiimp-dataP.h"
00013 
00014 IIIMP_utf8string *
00015 iiimp_utf8string_new(
00016     IIIMP_data_s *          data_s,
00017     size_t                  len,
00018     const IIIMP_card8 *             ptr)
00019 {
00020     IIIMP_utf8string *      str;
00021     size_t           nbyte;
00022 
00023     str = (IIIMP_utf8string *)malloc(sizeof (IIIMP_utf8string));
00024     if (NULL == str) {
00025        data_s->status = IIIMP_DATA_MALLOC_ERROR;
00026        return NULL;
00027     }
00028 
00029     nbyte = len;
00030 
00031     str->nbyte = (2 + nbyte + PAD(2 + nbyte));
00032     str->len = len;
00033     str->next = NULL;
00034 
00035     nbyte = ((sizeof (IIIMP_card8)) * len);
00036 
00037     if (0 == len) {
00038        str->ptr = NULL;
00039     } else {
00040        str->ptr = (IIIMP_card8 *)malloc(str->nbyte);
00041        if (NULL == str->ptr) {
00042            free(str);
00043            data_s->status = IIIMP_DATA_MALLOC_ERROR;
00044            return NULL;
00045        }
00046     }
00047 
00048     if (NULL != ptr) {
00049        (void)memcpy(str->ptr, ptr, len);
00050     }
00051 
00052     return str;
00053 }
00054 
00055 
00056 void
00057 iiimp_utf8string_delete(IIIMP_data_s * data_s, IIIMP_utf8string * str)
00058 {
00059 #if defined(lint) && defined(HAVE_NOTE_H)
00060     NOTE(ARGUNUSED(data_s))
00061 #endif /* lint && HAVE_NOTE_H */
00062        if (NULL == str) return;
00063     free(str->ptr);
00064     free(str);
00065     return;
00066 }
00067 
00068 
00069 void
00070 iiimp_utf8string_list_delete(IIIMP_data_s * data_s, IIIMP_utf8string * str)
00071 {
00072 #if defined(lint) && defined(HAVE_NOTE_H)
00073     NOTE(ARGUNUSED(data_s))
00074 #endif /* lint && HAVE_NOTE_H */
00075        IIIMP_utf8string *   str_next;
00076     for (; NULL != str; str = str_next) {
00077        str_next = str->next;
00078        free(str->ptr);
00079        free(str);
00080     }
00081     return;
00082 }
00083 
00084 
00085 void
00086 iiimp_utf8string_pack(
00087     IIIMP_data_s *   data_s,
00088     IIIMP_utf8string *      m,
00089     size_t *         nbyte,
00090     uchar_t **              ptr)
00091 {
00092     uchar_t * p;
00093     size_t    rest;
00094     int              i;
00095     int              pad;
00096 
00097     rest = *nbyte;
00098     p = *ptr;
00099 
00100     PUTU16((m->len), rest, p, data_s->byte_swap);
00101 
00102     pad = PAD(2 + (m->len));
00103 
00104     for (i = 0; i < m->len; i++) {
00105        PUTU8(*(m->ptr + i), rest, p, data_s->byte_swap);
00106     }
00107 
00108     for (i = 0; i < pad; i++)
00109        PUTU8(0, rest, p, data_s->byte_swap);
00110 
00111     *nbyte = rest;
00112     *ptr = p;
00113 
00114     return;
00115 }
00116 
00117 
00118 void
00119 iiimp_utf8string_list_pack(
00120     IIIMP_data_s *   data_s,
00121     IIIMP_utf8string *      m,
00122     size_t *         nbyte,
00123     uchar_t **              ptr)
00124 {
00125     size_t           rest;
00126     uchar_t *        p;
00127 
00128     rest = *nbyte;
00129     p = *ptr;
00130 
00131     for (; NULL != m; m = m->next) {
00132        iiimp_utf8string_pack(data_s, m, &rest, &p);
00133     }
00134 
00135     *nbyte = rest;
00136     *ptr = p;
00137 
00138     return;
00139 }
00140 
00141 
00142 IIIMP_utf8string *
00143 iiimp_utf8string_unpack(
00144     IIIMP_data_s *   data_s,
00145     size_t *         nbyte,
00146     const uchar_t ** ptr,
00147     size_t           nbyte_max)
00148 {
00149     IIIMP_utf8string *      str;
00150     const uchar_t *  p;
00151     size_t           rest;
00152     size_t           len;
00153     int                     data_size;
00154     int                     i, pad;
00155 
00156     rest = nbyte_max;
00157     p = *ptr;
00158 
00159     if ((*nbyte < rest) ||  (rest < 4)) {
00160        data_s->status = IIIMP_DATA_INVALID;
00161        return NULL;
00162     }
00163 
00164     GET16(len, rest, p, data_s->byte_swap);
00165     pad = PAD(2 + len);
00166     data_size = (len + pad);
00167     if (rest < data_size) {
00168        data_s->status = IIIMP_DATA_INVALID;
00169        return NULL;
00170     }
00171 
00172     str = (IIIMP_utf8string *)malloc(sizeof (IIIMP_utf8string));
00173     if (NULL == str) {
00174        data_s->status = IIIMP_DATA_MALLOC_ERROR;
00175        return NULL;
00176     }
00177 
00178     str->len = len;
00179     str->nbyte = (2 + len + PAD(2 + len));
00180     str->next = NULL;
00181     if (0 == len) {
00182        str->ptr = NULL;
00183     } else {
00184        str->ptr = (IIIMP_card8 *)malloc(len);
00185        if (NULL == str->ptr) {
00186            iiimp_utf8string_delete(data_s, str);
00187            data_s->status = IIIMP_DATA_MALLOC_ERROR;
00188            return NULL;
00189        }
00190 
00191        for (i = 0; i < str->len; i++) {
00192            GETU8(*(str->ptr + i), rest, p, data_s->byte_swap);
00193        }
00194 
00195        for (i = 0; i < pad; i++) {
00196            SKIP8(rest, p);
00197        }
00198     }
00199 
00200     *nbyte -= (2 + data_size);
00201     *ptr += (2 + data_size);
00202 
00203     return str;
00204 }
00205 
00206 
00207 IIIMP_utf8string *
00208 iiimp_utf8string_list_unpack(
00209     IIIMP_data_s *   data_s,
00210     size_t *         nbyte,
00211     const uchar_t ** ptr,
00212     size_t           nbyte_max)
00213 {
00214     IIIMP_utf8string *      str;
00215     size_t           rest;
00216     const uchar_t *  p;
00217     IIIMP_utf8string *      str_first;
00218     IIIMP_utf8string *      str_last;
00219 
00220     rest = nbyte_max;
00221     p = *ptr;
00222     str = NULL;
00223     str_first = NULL;
00224     str_last = NULL;
00225 
00226     if (((*nbyte) < nbyte_max) || (0 != (rest & 0x01)) || (0 == rest)) {
00227        data_s->status = IIIMP_DATA_INVALID;
00228        return NULL;
00229     }
00230 
00231     while (0 < rest) {
00232        str = iiimp_utf8string_unpack(data_s, &rest, &p, rest);
00233        if (NULL == str) {
00234            iiimp_utf8string_list_delete(data_s, str_first);
00235            return NULL;
00236        } else {
00237            if (NULL == str_first) {
00238               str_first = str;
00239            } else {
00240               str_last->next = str;
00241            }
00242            str_last = str;
00243        }
00244     }
00245 
00246     *nbyte -= (nbyte_max - rest);
00247     *ptr = p;
00248 
00249     return str_first;
00250 }
00251 
00252 
00253 void
00254 iiimp_utf8string_print(
00255     IIIMP_data_s *   data_s,
00256     IIIMP_utf8string *      m)
00257 {
00258     int                     i;
00259     const uchar_t *     p;
00260     int                 byte_len;
00261 
00262     if ((NULL == m) || (0 == m->len)) {
00263        return;
00264     }
00265 
00266     /* ASCII or UTF-8 */
00267     for (i = 0; i < m->len; i++) {
00268        if ((*(m->ptr + i) < 0x20) || (0x7f <= *(m->ptr + i))) {
00269            break;
00270        }
00271     }
00272 
00273     if (i == m->len) { /* ASCII */
00274        (void)fputc('"', data_s->print_fp);
00275        for (i = 0; i < m->len; i++) {
00276            (void)fprintf(data_s->print_fp, "%c", *(m->ptr + i));
00277        }
00278        (void)fputc('"', data_s->print_fp);
00279     } else {
00280         p = (uchar_t *)(m->ptr);
00281         byte_len = m->len; 
00282 
00283         (void)fputc('\n', data_s->print_fp);
00284         (void)fputc('\t', data_s->print_fp);
00285         for (i = 0; i < byte_len; i++) {
00286             (void)fprintf(data_s->print_fp, "%c", *p);
00287             p++;
00288         }
00289     }
00290 }
00291 
00292 
00293 void
00294 iiimp_utf8string_list_print(
00295     IIIMP_data_s *   data_s,
00296     IIIMP_utf8string *      m)
00297 {
00298     if (NULL == m) return;
00299     iiimp_utf8string_print(data_s, m);
00300     for (m = m->next; NULL != m; m = m->next) {
00301        (void)fputc(' ', data_s->print_fp);
00302        iiimp_utf8string_print(data_s, m);
00303     }
00304 }
00305 
00306 /* Local Variables: */
00307 /* c-file-style: "iiim-project" */
00308 /* End: */