FITkit
Fakulta informačních technologií

enc28j60_string.c

Zobrazit dokumentaci tohoto souboru.
00001 /*******************************************************************************
00002    enc28j60_string.c: Knihovna funkcí pro práci s řetězci a pamětí.
00003    Copyright (C) 2010 Brno University of Technology,
00004                       Faculty of Information Technology
00005    Author(s): Martin Musil <xmusil34 AT fit.vutbr.cz>
00006 
00007    LICENSE TERMS
00008 
00009    Redistribution and use in source and binary forms, with or without
00010    modification, are permitted provided that the following conditions
00011    are met:
00012    1. Redistributions of source code must retain the above copyright
00013       notice, this list of conditions and the following disclaimer.
00014    2. Redistributions in binary form must reproduce the above copyright
00015       notice, this list of conditions and the following disclaimer in
00016       the documentation and/or other materials provided with the
00017       distribution.
00018    3. All advertising materials mentioning features or use of this software
00019       or firmware must display the following acknowledgement:
00020 
00021         This product includes software developed by the University of
00022         Technology, Faculty of Information Technology, Brno and its
00023         contributors.
00024 
00025    4. Neither the name of the Company nor the names of its contributors
00026       may be used to endorse or promote products derived from this
00027       software without specific prior written permission.
00028 
00029    This software or firmware is provided ``as is'', and any express or implied
00030    warranties, including, but not limited to, the implied warranties of
00031    merchantability and fitness for a particular purpose are disclaimed.
00032    In no event shall the company or contributors be liable for any
00033    direct, indirect, incidental, special, exemplary, or consequential
00034    damages (including, but not limited to, procurement of substitute
00035    goods or services; loss of use, data, or profits; or business
00036    interruption) however caused and on any theory of liability, whether
00037    in contract, strict liability, or tort (including negligence or
00038    otherwise) arising in any way out of the use of this software, even
00039    if advised of the possibility of such damage.
00040 
00041    $Id$
00042 
00043 *******************************************************************************/ 
00044 #include "enc28j60_string.h"
00045 
00049 inline void* enc_memcpy(void* dst, void* src, unsigned int count){
00050         char* dst_tmp = (char*) dst;
00051         char* src_tmp = (char*) src;
00052         
00053         while(count--)
00054                 *dst_tmp++ = *src_tmp++;
00055         
00056 return dst;
00057 }
00058 
00062 inline unsigned int enc_memcmp(void* str1, void* str2,unsigned int count){
00063         char* str1_tmp = (char*) str1;
00064         char* str2_tmp = (char*) str2;
00065 
00066         while(count--){
00067                 if(*str1_tmp++ != *str2_tmp++)
00068                         return 1;
00069         }
00070         
00071 return 0;
00072 }
00073 
00077 inline void* enc_memset(void* ptr, char value, unsigned int count){
00078         char* ptr_tmp = (char*) ptr;
00079         
00080         while(count--)
00081                 *ptr_tmp++ = value;
00082         
00083 return ptr;
00084 }
00085 
00089 inline unsigned int enc_strlen(void* ptr){
00090         unsigned int len = 0;
00091         char* ptr_tmp = (char*) ptr;
00092         
00093         while((*ptr_tmp) != 0){
00094                 len++;
00095                 ptr_tmp++;
00096         }
00097 return len;
00098 }
00099 
00104 unsigned char str_to_ip_port(char* ptr, char max_len, unsigned long* ip, unsigned int* port){
00105         
00106         
00107         while(( (! is_digit(*ptr))) & (max_len > 0)){
00108                 max_len--;
00109                 ptr++;
00110                 
00111         }
00112         
00113         char part = 1;
00114         unsigned int temp = 0;
00115         unsigned long temp_port = 0;
00116         
00117         while(max_len > 0){
00118         
00119                 switch(part){
00120                         case 1:
00121                         case 2:
00122                         case 3:
00123                                 if( is_digit(*ptr) & (temp <= 255) ){           //další číslice - přičtu do temp
00124                                         temp *= 10;
00125                                         temp += (*ptr) - 48;
00126                                         
00127                                 }
00128                                 else{
00129                                         if( ((*ptr) == '.') & ( temp <= 255 )){ // tečka - zápis čísla temp do parametru ip a přechod na 
00130                                                 ((char*)ip)[4-part] = (char)temp;       // další část IP adresy
00131                                                 part++;
00132                                                 temp = 0;
00133                                         }
00134                                         else{
00135                                                 return 0;       
00136                                         }       
00137                                 }
00138                                 break;
00139                                 
00140                         case 4:
00141                                 if( is_digit(*ptr) & (temp <= 255) ){           //další číslice - přičtu do temp
00142                                         temp *= 10;
00143                                         temp += (*ptr) - 48;
00144                                 }
00145                                 else{
00146                                         if( ((*ptr) == ':') & ( temp <= 255 ) ){        // dvojtečka - zápis čísla temp do parametru ip a přechod na 
00147                                                 ((char*)ip)[4-part] = (char)temp;               // čtení portu
00148                                                 part++;
00149                                                 temp = 0;
00150                                         }
00151                                         else{
00152                                                 if( temp <= 255){                                               // jiný znak než dvojtečka - IP přečteno, vracím úspěch a port = 0
00153                                                         *port = 0;
00154                                                         ((char*)ip)[4-part] = (char)temp;
00155                                                         return 1;
00156                                                 }
00157                                                 else    
00158                                                         return 0;
00159                                         }       
00160                                 }
00161                                 break;
00162                         //port
00163                         case 5:
00164                                 if( is_digit(*ptr) & (temp <= 255) ){           //další číslice - přičtu do temp_port
00165                                         temp_port *= 10;
00166                                         temp_port += (*ptr) - 48;
00167                                 }
00168                                 else{
00169                                         if( temp_port <= 65535){                                //jiný znak - konec portu, vracím úspěch,IP a port 
00170                                                 *port = temp_port;
00171                                                 return 1;
00172                                         }
00173                                         else{
00174                                                 return 0;
00175                                         }
00176                                 }
00177                                 
00178                 }
00179 
00181                 ptr++;
00182                 max_len--;      
00183 
00184         }
00185         
00186         //dosazen konec retezce
00187         switch(part){
00188                 case 1:
00189                 case 2:
00190                 case 3:
00191                         return 0;                                                                       // nejsou přečteny všechny části IP - neúspěch
00192                 case 4:
00193                         if(temp <= 255){
00194                                 ((char*)ip)[4-part] = (char)temp;               //přečtena 4. část IP - vracím úspěch a pouze IP adresu 
00195                                 *port = 0;
00196                                 return 1;
00197                         }
00198                         else
00199                                 return 0;
00200                         
00201                 case 5:
00202                         if( temp_port <= 65535){                                //přečten i port, vracím úspěch, IP a port
00203                                 *port = temp_port;
00204                                 return 1;
00205                         }       
00206                         else{
00207                                 *port = 0;
00208                                 return 1;
00209                         }
00210         }
00211         
00212 return 0;
00213 }
00214 
00218 unsigned char ip_to_str(char * ptr, char max_len, unsigned long ip){
00219         
00220         unsigned char tmp[3];
00221         char counter = max_len;
00222         unsigned char segment;  
00223         
00224         int i = 3;
00225         while( i >= 0){
00226         
00227                 segment = ((char*)&ip)[i];      
00228 
00229                 tmp[0] = (segment / 100) + 48;
00230                 segment %= 100;
00231                 tmp[1] = (segment / 10) + 48;
00232                 segment %= 10;
00233                 tmp[2] = segment + 48;
00234 
00235                 int k = 0;
00236                 char print = 0;
00237                 while(k < 3){
00238 
00239                         if(tmp[k] > '0'){                       //pocatecni nuly nezapisujem
00240                                 print = 1;
00241                                 if(counter > 0){
00242                                         counter--;
00243                                         *(ptr) = tmp[k];
00244                                         ptr++;
00245                                 }
00246                         }
00247                         else if(print == 1){            //nuly uprostred cisla samozrejme zapisujem
00248                                 if(counter > 0){
00249                                         counter--;
00250                                         *(ptr) = tmp[k];
00251                                         ptr++;
00252                                 }       
00253                         }
00254                         
00255                         k++;    
00256                 }
00257                 
00258                 if(print == 0){                         //byly same nuly, musime zapsat alespon jednu
00259                         if(counter > 0){
00260                                 counter--;
00261                                 *(ptr) = '0';
00262                                 ptr++;
00263                         }
00264                 }
00265                 
00266                 if((i != 0) & ( counter > 0)){          //prvni 3 segmenty IP konci teckou,ctvrty ne
00267                         counter--;
00268                         *(ptr) = '.';
00269                         ptr++;
00270                 }       
00271                         
00272                 i--;
00273                 print = 0;
00274                 
00275         } 
00276         //ukonceni retezce
00277         if(counter == 0){
00278                 ptr--;
00279                 *(ptr) = 0;
00280         }
00281         else
00282                 *(ptr) = 0;
00283         
00284 return max_len - counter;
00285 }