Ok, this was embarrassing... Added core include file that was renamed
[tpg/acess2.git] / Kernel / lib.c
index 3d91c71..db01450 100644 (file)
@@ -2,7 +2,7 @@
  * Acess2
  * Common Library Functions
  */
-#include <common.h>
+#include <acess.h>
 
 // === CONSTANTS ===
 #define        RANDOM_SEED     0xACE55052
@@ -92,12 +92,12 @@ int strpos(const char *Str, char Ch)
 }
 
 /**
- * \fn int ByteSum(void *Ptr, int Size)
+ * \fn Uint8 ByteSum(void *Ptr, int Size)
  * \brief Adds the bytes in a memory region and returns the sum
  */
-int ByteSum(void *Ptr, int Size)
+Uint8 ByteSum(void *Ptr, int Size)
 {
-        int    sum = 0;
+       Uint8   sum = 0;
        while(Size--)   sum += *(Uint8*)Ptr++;
        return sum;
 }
@@ -341,5 +341,95 @@ Uint rand()
        return giRandomState;
 }
 
+/// \name Memory Validation
+/// \{
+/**
+ * \brief Checks if a string resides fully in valid memory
+ */
+int CheckString(char *String)
+{
+       // Check 1st page
+       if( MM_IsUser( (tVAddr)String ) )
+       {
+               // Traverse String
+               while( *String )
+               {
+                       if( !MM_IsUser( (tVAddr)String ) )
+                               return 0;
+                       // Increment string pointer
+                       String ++;
+               }
+               return 1;
+       }
+       else if( MM_GetPhysAddr( (tVAddr)String ) )
+       {
+               // Traverse String
+               while( *String )
+               {
+                       if( !MM_GetPhysAddr( (tVAddr)String ) )
+                               return 0;
+                       // Increment string pointer
+                       String ++;
+               }
+               return 1;
+       }
+       return 0;
+}
+
+/**
+ * \brief Check if a sized memory region is valid memory
+ */
+int CheckMem(void *Mem, int NumBytes)
+{
+       tVAddr  addr = (tVAddr)Mem;
+       
+       if( MM_IsUser( addr ) )
+       {
+               while( NumBytes-- )
+               {
+                       if( !MM_IsUser( addr ) )
+                               return 0;
+                       addr ++;
+               }
+               return 1;
+       }
+       else if( MM_GetPhysAddr( addr ) )
+       {
+               while( NumBytes-- )
+               {
+                       if( !MM_GetPhysAddr( addr ) )
+                               return 0;
+                       addr ++;
+               }
+               return 1;
+       }
+       return 0;
+}
+/// \}
+
+/**
+ * \brief Search a string array for \a Needle
+ * \note Helper function for eTplDrv_IOCtl::DRV_IOCTL_LOOKUP
+ */
+int LookupString(char **Array, char *Needle)
+{
+        int    i;
+       for( i = 0; Array[i]; i++ )
+       {
+               if(strcmp(Array[i], Needle) == 0)       return i;
+       }
+       return -1;
+}
+
+EXPORT(strlen);
+EXPORT(strdup);
+EXPORT(strcmp);
+EXPORT(strncmp);
+EXPORT(strcpy);
+//EXPORT(strncpy);
+
 EXPORT(timestamp);
 EXPORT(ReadUTF8);
+EXPORT(CheckMem);
+EXPORT(CheckString);
+EXPORT(LookupString);

UCC git Repository :: git.ucc.asn.au