Subversion Repositories HelenOS

Rev

Rev 3666 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /*
  2.  * Copyright (c) 2008 Lukas Mejdrech
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * - Redistributions of source code must retain the above copyright
  10.  *   notice, this list of conditions and the following disclaimer.
  11.  * - Redistributions in binary form must reproduce the above copyright
  12.  *   notice, this list of conditions and the following disclaimer in the
  13.  *   documentation and/or other materials provided with the distribution.
  14.  * - The name of the author may not be used to endorse or promote products
  15.  *   derived from this software without specific prior written permission.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  18.  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  19.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  20.  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  21.  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  23.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  24.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26.  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  */
  28.  
  29. /** @addtogroup net
  30.  * @{
  31.  */
  32.  
  33. /**
  34.  * @file
  35.  */
  36.  
  37. #include <errno.h>
  38. #include <malloc.h>
  39. #include <stdio.h>
  40. #include <string.h>
  41. #include <unistd.h>
  42. #include <ipc/ipc.h>
  43.  
  44. #include "err.h"
  45. #include "measured_strings.h"
  46. #include "modules.h"
  47.  
  48. size_t *    prepare_lengths( measured_string_ref strings, size_t count );
  49.  
  50. measured_string_ref measured_string_create_bulk( const char * string, size_t length ){
  51.     measured_string_ref new;
  52.  
  53.     if( length == 0 ){
  54.         while( string[ length ] ) ++ length;
  55.     }
  56.     new = ( measured_string_ref ) malloc( sizeof( measured_string_t ) + ( sizeof( char ) * ( length + 1 )));
  57.     if( ! new ) return NULL;
  58.     new->length = length;
  59.     new->value = (( char * ) new ) + sizeof( measured_string_t );
  60.     memcpy( new->value, string, new->length + 1 );
  61.     return new;
  62. }
  63.  
  64. int measured_strings_receive( measured_string_ref * strings, char ** data, size_t count ){
  65.     ERROR_DECLARE;
  66.  
  67.     size_t *    lengths;
  68.     int     index;
  69.     size_t      length;
  70.     char *      next;
  71.     ipc_callid_t    callid;
  72.  
  73.     if(( ! strings ) || ( ! data ) || ( count <= 0 )){
  74.         return EINVAL;
  75.     }
  76.     lengths = ( size_t * ) malloc( sizeof( size_t ) * ( count + 1 ));
  77.     if( ! lengths ) return ENOMEM;
  78.     if(( ! ipc_data_write_receive( & callid, & length ))
  79.     || ( length != sizeof( size_t ) * ( count + 1 ))){
  80.         free( lengths );
  81.         return EINVAL;
  82.     }
  83.     if( ERROR_OCCURED( ipc_data_write_finalize( callid, lengths, sizeof( size_t ) * ( count + 1 )))){
  84.         free( lengths );
  85.         return ERROR_CODE;
  86.     }
  87.     * data = malloc( lengths[ count ] );
  88.     if( !( * data )) return ENOMEM;
  89.     ( * data )[ lengths[ count ] - 1 ] = '\0';
  90.     * strings = ( measured_string_ref ) malloc( sizeof( measured_string_t ) * count );
  91.     if( !( * strings )){
  92.         free( lengths );
  93.         free( * data );
  94.         return ENOMEM;
  95.     }
  96.     next = * data;
  97.     for( index = 0; index < count; ++ index ){
  98.         ( * strings)[ index ].length = lengths[ index ];
  99.         if( lengths[ index ] > 0 ){
  100.             if(( ! ipc_data_write_receive( & callid, & length ))
  101.             || ( length != lengths[ index ] )){
  102.                 free( * data );
  103.                 free( * strings );
  104.                 free( lengths );
  105.                 return EINVAL;
  106.             }
  107.             ERROR_PROPAGATE( ipc_data_write_finalize( callid, next, lengths[ index ] ));
  108.             ( * strings)[ index ].value = next;
  109.             next += lengths[ index ];
  110.             * next = '\0';
  111.             ++ next;
  112.         }else{
  113.             ( * strings )[ index ].value = NULL;
  114.         }
  115.     }
  116.     free( lengths );
  117.     return EOK;
  118. }
  119.  
  120. int measured_strings_reply( measured_string_ref strings, size_t count ){
  121.     ERROR_DECLARE;
  122.  
  123.     size_t *    lengths;
  124.     int     index;
  125.     size_t      length;
  126.     ipc_callid_t    callid;
  127.  
  128.     if(( ! strings ) || ( count <= 0 )){
  129.         return EINVAL;
  130.     }
  131.     lengths = prepare_lengths( strings, count );
  132.     if( ! lengths ) return ENOMEM;
  133.     if(( ! ipc_data_read_receive( & callid, & length ))
  134.     || ( length != sizeof( size_t ) * ( count + 1 ))){
  135.         free( lengths );
  136.         return EINVAL;
  137.     }
  138.     if( ERROR_OCCURED( ipc_data_read_finalize( callid, lengths, sizeof( size_t ) * ( count + 1 )))){
  139.         free( lengths );
  140.         return ERROR_CODE;
  141.     }
  142.     free( lengths );
  143.     for( index = 0; index < count; ++ index ){
  144.         if( strings[ index ].length > 0 ){
  145.             if(( ! ipc_data_read_receive( & callid, & length ))
  146.             || ( length != strings[ index ].length )){
  147.                 return EINVAL;
  148.             }
  149.             ERROR_PROPAGATE( ipc_data_read_finalize( callid, strings[ index ].value, strings[ index ].length ));
  150.         }
  151.     }
  152.     return EOK;
  153. }
  154.  
  155. int measured_strings_return( int phone, measured_string_ref * strings, char ** data, size_t count ){
  156.     ERROR_DECLARE;
  157.  
  158.     size_t *    lengths;
  159.     int     index;
  160.     char *      next;
  161.  
  162.     if(( phone <= 0 ) || ( ! strings ) || ( ! data ) || ( count <= 0 )){
  163.         return EINVAL;
  164.     }
  165.     lengths = ( size_t * ) malloc( sizeof( size_t ) * ( count + 1 ));
  166.     if( ! lengths ) return ENOMEM;
  167.     if( ERROR_OCCURED( ipc_data_read_start( phone, lengths, sizeof( size_t ) * ( count + 1 )))){
  168.         free( lengths );
  169.         return ERROR_CODE;
  170.     }
  171.     * data = malloc( lengths[ count ] );
  172.     if( !( * data )) return ENOMEM;
  173.     * strings = ( measured_string_ref ) malloc( sizeof( measured_string_t ) * count );
  174.     if( !( * strings )){
  175.         free( lengths );
  176.         free( * data );
  177.         return ENOMEM;
  178.     }
  179.     next = * data;
  180.     for( index = 0; index < count; ++ index ){
  181.         ( * strings )[ index ].length = lengths[ index ];
  182.         if( lengths[ index ] > 0 ){
  183.             ERROR_PROPAGATE( ipc_data_read_start( phone, next, lengths[ index ] ));
  184.             ( * strings )[ index ].value = next;
  185.             next += lengths[ index ];
  186.             * next = '\0';
  187.             ++ next;
  188.         }else{
  189.             ( * strings )[ index ].value = NULL;
  190.         }
  191.     }
  192.     free( lengths );
  193.     return EOK;
  194. }
  195.  
  196. int measured_strings_send( int phone, measured_string_ref strings, size_t count ){
  197.     ERROR_DECLARE;
  198.  
  199.     size_t *    lengths;
  200.     int     index;
  201.  
  202.     if(( phone <= 0 ) || ( ! strings ) || ( count <= 0 )){
  203.         return EINVAL;
  204.     }
  205.     lengths = prepare_lengths( strings, count );
  206.     if( ! lengths ) return ENOMEM;
  207.     if( ERROR_OCCURED( ipc_data_write_start( phone, lengths, sizeof( size_t ) * ( count + 1 )))){
  208.         free( lengths );
  209.         return ERROR_CODE;
  210.     }
  211.     free( lengths );
  212.     for( index = 0; index < count; ++ index ){
  213.         if( strings[ index ].length > 0 ){
  214.             ERROR_PROPAGATE( ipc_data_write_start( phone, strings[ index ].value, strings[ index ].length ));
  215.         }
  216.     }
  217.     return EOK;
  218. }
  219.  
  220. size_t * prepare_lengths( measured_string_ref strings, size_t count ){
  221.     size_t *    lengths;
  222.     int     index;
  223.     size_t      length;
  224.  
  225.     lengths = ( size_t * ) malloc( sizeof( size_t ) * ( count + 1 ));
  226.     if( ! lengths ) return NULL;
  227.     length = 0;
  228.     for( index = 0; index < count; ++ index ){
  229.         lengths[ index ] = strings[ index ].length;
  230.         length += lengths[ index ] + 1;
  231.     }
  232.     lengths[ count ] = length;
  233.     return lengths;
  234. }
  235.  
  236. /** @}
  237.  */
  238.  
  239.