Ignore:
Timestamp:
2010-11-25T13:42:50Z (15 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
8df8415
Parents:
a93d79a (diff), eb667613 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge mainline changes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/generic/adt/measured_strings.c

    ra93d79a r8fb1bf82  
    4141#include <unistd.h>
    4242#include <errno.h>
    43 #include <err.h>
    4443#include <async.h>
    4544
     
    5251 * @param[in] string    The initial character string to be stored.
    5352 * @param[in] length    The length of the given string without the terminating
    54  *                      zero ('/0') character. If the length is zero (0), the
    55  *                      actual length is computed. The given length is used and
    56  *                      appended with the terminating zero ('\\0') character
     53 *                      zero ('\0') character. If the length is zero, the actual
     54 *                      length is computed. The given length is used and
     55 *                      appended with the terminating zero ('\0') character
    5756 *                      otherwise.
    58  * @returns             The new bundled character string with measured length.
    59  * @returns             NULL if there is not enough memory left.
    60  */
    61 measured_string_ref
    62 measured_string_create_bulk(const char * string, size_t length)
    63 {
    64         measured_string_ref new;
     57 * @return              The new bundled character string with measured length.
     58 * @return              NULL if there is not enough memory left.
     59 */
     60measured_string_t *
     61measured_string_create_bulk(const char *string, size_t length)
     62{
     63        measured_string_t *new;
    6564
    6665        if (length == 0) {
    6766                while (string[length])
    68                         ++length;
    69         }
    70         new = (measured_string_ref) malloc(sizeof(measured_string_t) +
     67                        length++;
     68        }
     69        new = (measured_string_t *) malloc(sizeof(measured_string_t) +
    7170            (sizeof(char) * (length + 1)));
    7271        if (!new)
     
    8584 *
    8685 * @param[in] source    The source measured string to be copied.
    87  * @returns             The copy of the given measured string.
    88  * @returns             NULL if the source parameter is NULL.
    89  * @returns             NULL if there is not enough memory left.
    90  */
    91 measured_string_ref measured_string_copy(measured_string_ref source)
    92 {
    93         measured_string_ref new;
     86 * @return              The copy of the given measured string.
     87 * @return              NULL if the source parameter is NULL.
     88 * @return              NULL if there is not enough memory left.
     89 */
     90measured_string_t *measured_string_copy(measured_string_t *source)
     91{
     92        measured_string_t *new;
    9493
    9594        if (!source)
    9695                return NULL;
    9796
    98         new = (measured_string_ref) malloc(sizeof(measured_string_t));
     97        new = (measured_string_t *) malloc(sizeof(measured_string_t));
    9998        if (new) {
    10099                new->value = (char *) malloc(source->length + 1);
     
    104103                        new->value[new->length] = '\0';
    105104                        return new;
    106                 } else {
    107                         free(new);
    108105                }
     106                free(new);
    109107        }
    110108
     
    122120 *                      actual character strings.
    123121 *  @param[in] count    The size of the measured strings array.
    124  *  @returns            EOK on success.
    125  *  @returns            EINVAL if the strings or data parameter is NULL.
    126  *  @returns            EINVAL if the count parameter is zero (0).
    127  *  @returns            EINVAL if the sent array differs in size.
    128  *  @returns            EINVAL if there is inconsistency in sent measured
     122 *  @return             EOK on success.
     123 *  @return             EINVAL if the strings or data parameter is NULL.
     124 *  @return             EINVAL if the count parameter is zero (0).
     125 *  @return             EINVAL if the sent array differs in size.
     126 *  @return             EINVAL if there is inconsistency in sent measured
    129127 *                      strings' lengths (should not occur).
    130  *  @returns            ENOMEM if there is not enough memory left.
    131  *  @returns            Other error codes as defined for the
     128 *  @return             ENOMEM if there is not enough memory left.
     129 *  @return             Other error codes as defined for the
    132130 *                      async_data_write_finalize() function.
    133131 */
    134132int
    135 measured_strings_receive(measured_string_ref *strings, char **data,
     133measured_strings_receive(measured_string_t **strings, char **data,
    136134    size_t count)
    137135{
    138         ERROR_DECLARE;
    139 
    140136        size_t *lengths;
    141137        size_t index;
     
    143139        char *next;
    144140        ipc_callid_t callid;
     141        int rc;
    145142
    146143        if ((!strings) || (!data) || (count <= 0))
     
    156153                return EINVAL;
    157154        }
    158         if(ERROR_OCCURRED(async_data_write_finalize(callid, lengths,
    159             sizeof(size_t) * (count + 1)))) {
    160                 free(lengths);
    161                 return ERROR_CODE;
     155        rc = async_data_write_finalize(callid, lengths, length);
     156        if (rc != EOK) {
     157                free(lengths);
     158                return rc;
    162159        }
    163160
    164161        *data = malloc(lengths[count]);
    165         if (!(*data)) {
     162        if (!*data) {
    166163                free(lengths);
    167164                return ENOMEM;
     
    169166        (*data)[lengths[count] - 1] = '\0';
    170167
    171         *strings = (measured_string_ref) malloc(sizeof(measured_string_t) *
     168        *strings = (measured_string_t *) malloc(sizeof(measured_string_t) *
    172169            count);
    173         if (!(*strings)) {
     170        if (!*strings) {
    174171                free(lengths);
    175172                free(*data);
     
    178175
    179176        next = *data;
    180         for (index = 0; index < count; ++index) {
     177        for (index = 0; index < count; index++) {
    181178                (*strings)[index].length = lengths[index];
    182179                if (lengths[index] > 0) {
    183                         if ((!async_data_write_receive(&callid, &length)) ||
     180                        if (!async_data_write_receive(&callid, &length) ||
    184181                            (length != lengths[index])) {
    185182                                free(*data);
     
    188185                                return EINVAL;
    189186                        }
    190                         ERROR_PROPAGATE(async_data_write_finalize(callid, next,
    191                             lengths[index]));
     187                        rc = async_data_write_finalize(callid, next,
     188                            lengths[index]);
     189                        if (rc != EOK) {
     190                                free(*data);
     191                                free(*strings);
     192                                free(lengths);
     193                                return rc;
     194                        }
    192195                        (*strings)[index].value = next;
    193196                        next += lengths[index];
    194                         *next = '\0';
    195                         ++next;
     197                        *next++ = '\0';
    196198                } else {
    197199                        (*strings)[index].value = NULL;
     
    207209 * @param[in] strings   The measured strings array to be processed.
    208210 * @param[in] count     The measured strings array size.
    209  * @returns             The computed sizes array.
    210  * @returns             NULL if there is not enough memory left.
    211  */
    212 static size_t *prepare_lengths(const measured_string_ref strings, size_t count)
     211 * @return              The computed sizes array.
     212 * @return              NULL if there is not enough memory left.
     213 */
     214static size_t *prepare_lengths(const measured_string_t *strings, size_t count)
    213215{
    214216        size_t *lengths;
     
    221223
    222224        length = 0;
    223         for (index = 0; index < count; ++ index) {
     225        for (index = 0; index < count; index++) {
    224226                lengths[index] = strings[index].length;
    225227                length += lengths[index] + 1;
     
    236238 * @param[in] strings   The measured strings array to be transferred.
    237239 * @param[in] count     The measured strings array size.
    238  * @returns             EOK on success.
    239  * @returns             EINVAL if the strings parameter is NULL.
    240  * @returns             EINVAL if the count parameter is zero (0).
    241  * @returns             EINVAL if the calling module does not accept the given
     240 * @return              EOK on success.
     241 * @return              EINVAL if the strings parameter is NULL.
     242 * @return              EINVAL if the count parameter is zero (0).
     243 * @return              EINVAL if the calling module does not accept the given
    242244 *                      array size.
    243  * @returns             EINVAL if there is inconsistency in sent measured
     245 * @return              EINVAL if there is inconsistency in sent measured
    244246 *                      strings' lengths (should not occur).
    245  * @returns             Other error codes as defined for the
     247 * @return              Other error codes as defined for the
    246248 *                      async_data_read_finalize() function.
    247249 */
    248 int measured_strings_reply(const measured_string_ref strings, size_t count)
    249 {
    250         ERROR_DECLARE;
    251 
     250int measured_strings_reply(const measured_string_t *strings, size_t count)
     251{
    252252        size_t *lengths;
    253253        size_t index;
    254254        size_t length;
    255255        ipc_callid_t callid;
     256        int rc;
    256257
    257258        if ((!strings) || (count <= 0))
     
    262263                return ENOMEM;
    263264
    264         if ((!async_data_read_receive(&callid, &length)) ||
     265        if (!async_data_read_receive(&callid, &length) ||
    265266            (length != sizeof(size_t) * (count + 1))) {
    266267                free(lengths);
    267268                return EINVAL;
    268269        }
    269         if (ERROR_OCCURRED(async_data_read_finalize(callid, lengths,
    270             sizeof(size_t) * (count + 1)))) {
    271                 free(lengths);
    272                 return ERROR_CODE;
     270        rc = async_data_read_finalize(callid, lengths, length);
     271        if (rc != EOK) {
     272                free(lengths);
     273                return rc;
    273274        }
    274275        free(lengths);
    275276
    276         for (index = 0; index < count; ++ index) {
     277        for (index = 0; index < count; index++) {
    277278                if (strings[index].length > 0) {
    278                         if((!async_data_read_receive(&callid, &length)) ||
     279                        if (!async_data_read_receive(&callid, &length) ||
    279280                            (length != strings[index].length)) {
    280281                                return EINVAL;
    281282                        }
    282                         ERROR_PROPAGATE(async_data_read_finalize(callid,
    283                             strings[index].value, strings[index].length));
     283                        rc = async_data_read_finalize(callid,
     284                            strings[index].value, strings[index].length);
     285                        if (rc != EOK)
     286                                return rc;
    284287                }
    285288        }
     
    299302 *                      actual character strings.
    300303 * @param[in] count     The size of the measured strings array.
    301  * @returns             EOK on success.
    302  * @returns             EINVAL if the strings or data parameter is NULL.
    303  * @returns             EINVAL if the phone or count parameter is not positive.
    304  * @returns             EINVAL if the sent array differs in size.
    305  * @returns             ENOMEM if there is not enough memory left.
    306  * @returns             Other error codes as defined for the
     304 * @return              EOK on success.
     305 * @return              EINVAL if the strings or data parameter is NULL.
     306 * @return              EINVAL if the phone or count parameter is not positive.
     307 * @return              EINVAL if the sent array differs in size.
     308 * @return              ENOMEM if there is not enough memory left.
     309 * @return              Other error codes as defined for the
    307310 *                      async_data_read_start() function.
    308311 */
    309312int
    310 measured_strings_return(int phone, measured_string_ref *strings, char **data,
     313measured_strings_return(int phone, measured_string_t **strings, char **data,
    311314    size_t count)
    312315{
    313         ERROR_DECLARE;
    314 
    315316        size_t *lengths;
    316317        size_t index;
    317318        char *next;
    318 
    319         if ((phone <= 0) || (!strings) || (!data) || (count <= 0))
     319        int rc;
     320
     321        if ((phone < 0) || (!strings) || (!data) || (count <= 0))
    320322                return EINVAL;
    321323
     
    324326                return ENOMEM;
    325327
    326         if (ERROR_OCCURRED(async_data_read_start(phone, lengths,
    327             sizeof(size_t) * (count + 1)))) {
    328                 free(lengths);
    329                 return ERROR_CODE;
     328        rc = async_data_read_start(phone, lengths,
     329            sizeof(size_t) * (count + 1));
     330        if (rc != EOK) {
     331                free(lengths);
     332                return rc;
    330333        }
    331334
    332335        *data = malloc(lengths[count]);
    333         if (!(*data)) {
    334                 free(lengths);
    335                 return ENOMEM;
    336         }
    337 
    338         *strings = (measured_string_ref) malloc(sizeof(measured_string_t) *
     336        if (!*data) {
     337                free(lengths);
     338                return ENOMEM;
     339        }
     340
     341        *strings = (measured_string_t *) malloc(sizeof(measured_string_t) *
    339342            count);
    340         if (!(*strings)) {
     343        if (!*strings) {
    341344                free(lengths);
    342345                free(*data);
     
    345348
    346349        next = *data;
    347         for (index = 0; index < count; ++ index) {
     350        for (index = 0; index < count; index++) {
    348351                (*strings)[index].length = lengths[index];
    349352                if (lengths[index] > 0) {
    350                         ERROR_PROPAGATE(async_data_read_start(phone, next,
    351                             lengths[index]));
     353                        rc = async_data_read_start(phone, next, lengths[index]);
     354                        if (rc != EOK) {
     355                                free(lengths);
     356                                free(data);
     357                                free(strings);
     358                                return rc;
     359                        }
    352360                        (*strings)[index].value = next;
    353361                        next += lengths[index];
    354                         *next = '\0';
    355                         ++ next;
     362                        *next++ = '\0';
    356363                } else {
    357364                        (*strings)[index].value = NULL;
     
    371378 * @param[in] strings   The measured strings array to be transferred.
    372379 * @param[in] count     The measured strings array size.
    373  * @returns             EOK on success.
    374  * @returns             EINVAL if the strings parameter is NULL.
    375  * @returns             EINVAL if the phone or count parameter is not positive.
    376  * @returns             Other error codes as defined for the
     380 * @return              EOK on success.
     381 * @return              EINVAL if the strings parameter is NULL.
     382 * @return              EINVAL if the phone or count parameter is not positive.
     383 * @return              Other error codes as defined for the
    377384 *                      async_data_write_start() function.
    378385 */
    379386int
    380 measured_strings_send(int phone, const measured_string_ref strings,
     387measured_strings_send(int phone, const measured_string_t *strings,
    381388    size_t count)
    382389{
    383         ERROR_DECLARE;
    384 
    385390        size_t *lengths;
    386391        size_t index;
    387 
    388         if ((phone <= 0) || (!strings) || (count <= 0))
     392        int rc;
     393
     394        if ((phone < 0) || (!strings) || (count <= 0))
    389395                return EINVAL;
    390396
     
    393399                return ENOMEM;
    394400
    395         if (ERROR_OCCURRED(async_data_write_start(phone, lengths,
    396             sizeof(size_t) * (count + 1)))) {
    397                 free(lengths);
    398                 return ERROR_CODE;
     401        rc = async_data_write_start(phone, lengths,
     402            sizeof(size_t) * (count + 1));
     403        if (rc != EOK) {
     404                free(lengths);
     405                return rc;
    399406        }
    400407
    401408        free(lengths);
    402409
    403         for (index = 0; index < count; ++index) {
     410        for (index = 0; index < count; index++) {
    404411                if (strings[index].length > 0) {
    405                         ERROR_PROPAGATE(async_data_write_start(phone,
    406                             strings[index].value, strings[index].length));
     412                        rc = async_data_write_start(phone, strings[index].value,
     413                            strings[index].length);
     414                        if (rc != EOK)
     415                                return rc;
    407416                }
    408417        }
Note: See TracChangeset for help on using the changeset viewer.