From 808fdc2c3745074a941fc73c9e6233d8e41cfdc0 Mon Sep 17 00:00:00 2001
From: guowenxue <guowenxue@gmail.com>
Date: Mon, 13 Apr 2020 23:42:28 +0800
Subject: [PATCH] Add library and makefile sample code

---
 /dev/null                        |   18 ----
 lib_makefile/encrypto/makefile   |   22 +++++
 lib_makefile/main.c              |   32 ++++++++
 lib_makefile/makefile            |   34 ++++++++
 lib_makefile/encrypto/encrypto.h |   23 +++++
 lib_makefile/encrypto/encrypto.c |   66 ++++++++++++++++
 6 files changed, 177 insertions(+), 18 deletions(-)

diff --git a/ch7_library/lib/crypto.h b/ch7_library/lib/crypto.h
deleted file mode 100644
index 0aea94f..0000000
--- a/ch7_library/lib/crypto.h
+++ /dev/null
@@ -1,28 +0,0 @@
-/********************************************************************************
- *      Copyright:  (C) 2018 LingYun IoT System Studio
- *                  All rights reserved.
- *
- *       Filename:  crypto.h
- *    Description:  This head file is LingYun crypto library head file
- *
- *        Version:  1.0.0(2018年12月20日)
- *         Author:  Guo Wenxue <guowenxue@gmail.com>
- *      ChangeLog:  1, Release initial version on "2018年12月20日 12时32分09秒"
- *                 
- ********************************************************************************/
-
-#ifndef  _CRYPTO_H_
-#define  _CRYPTO_H_
-
-/* Description: This function used to encrypto message in $plaintext, and put ciphertext into $ciphertext 
- * Return value:  0: Success <0: Failure
- */
-extern int encrypt(char *plaintext, char *ciphertext, int size);
-
-/* Description: This function used to decrypto message in $ciphertext, and put plaintext into $plaintext 
- * Return value:  0: Success <0: Failure
- */
-extern int decrypt(char *ciphertext, char *plaintext, int size);
-
-#endif   /* ----- #ifndef _CRYPTO_H_  ----- */
-
diff --git a/ch7_library/makefile b/ch7_library/makefile
deleted file mode 100644
index 6b7f9c6..0000000
--- a/ch7_library/makefile
+++ /dev/null
@@ -1,18 +0,0 @@
-
-all:
-	@echo "Start compile library..."
-	make -C src
-	@echo "Start compile test program..."
-	make -C test
-
-run:
-	@echo "Start run test program..."
-	make run -C test
-
-clean:
-	make clean -C src
-	make clean -C test
-
-distclean: clean
-	rm -f lib/*.a
-	rm -f lib/*.so
diff --git a/ch7_library/src/crypto.c b/ch7_library/src/crypto.c
deleted file mode 100644
index 1bcc9bc..0000000
--- a/ch7_library/src/crypto.c
+++ /dev/null
@@ -1,48 +0,0 @@
-/*********************************************************************************
- *      Copyright:  (C) 2018 LingYun IoT System Studio
- *                  All rights reserved.
- *
- *       Filename:  crypto.c
- *    Description:  This file is LingYun crypto library 
- *                 
- *        Version:  1.0.0(2018年12月20日)
- *         Author:  Guo Wenxue <guowenxue@gmail.com>
- *      ChangeLog:  1, Release initial version on "2018年12月20日 11时56分04秒"
- *                 
- ********************************************************************************/
-#include <string.h>
-
-int encrypt(char *plaintext, char *ciphertext, int size)
-{
-    int       i;
-
-    if( size < strlen(plaintext) )
-    {
-        return -1;
-    }
-
-    for(i=0; i<strlen(plaintext); i++)
-    {
-        ciphertext[i] = plaintext[i]+3;
-    }
-
-    return 0;
-}
-
-int decrypt(char *ciphertext, char *plaintext, int size)
-{
-    int      i;
-
-    if( size < strlen(ciphertext) )
-    {
-        return -1;
-    }
-
-    for(i=0; i<strlen(ciphertext); i++)
-    {
-        plaintext[i] = ciphertext[i]-3;
-    }
-
-    return 0;
-}
-
diff --git a/ch7_library/src/crypto.h b/ch7_library/src/crypto.h
deleted file mode 100644
index 0aea94f..0000000
--- a/ch7_library/src/crypto.h
+++ /dev/null
@@ -1,28 +0,0 @@
-/********************************************************************************
- *      Copyright:  (C) 2018 LingYun IoT System Studio
- *                  All rights reserved.
- *
- *       Filename:  crypto.h
- *    Description:  This head file is LingYun crypto library head file
- *
- *        Version:  1.0.0(2018年12月20日)
- *         Author:  Guo Wenxue <guowenxue@gmail.com>
- *      ChangeLog:  1, Release initial version on "2018年12月20日 12时32分09秒"
- *                 
- ********************************************************************************/
-
-#ifndef  _CRYPTO_H_
-#define  _CRYPTO_H_
-
-/* Description: This function used to encrypto message in $plaintext, and put ciphertext into $ciphertext 
- * Return value:  0: Success <0: Failure
- */
-extern int encrypt(char *plaintext, char *ciphertext, int size);
-
-/* Description: This function used to decrypto message in $ciphertext, and put plaintext into $plaintext 
- * Return value:  0: Success <0: Failure
- */
-extern int decrypt(char *ciphertext, char *plaintext, int size);
-
-#endif   /* ----- #ifndef _CRYPTO_H_  ----- */
-
diff --git a/ch7_library/src/makefile b/ch7_library/src/makefile
deleted file mode 100644
index f716fd4..0000000
--- a/ch7_library/src/makefile
+++ /dev/null
@@ -1,32 +0,0 @@
-
-LIBNAME=mycrypto
-INSTPATH=`pwd`/../lib/
-
-CC=gcc
-AR=ar
-
-all: dynamic_lib static_lib
-	@make clear
-	@make install
-
-dynamic_lib:
-	${CC} -shared -fPIC *.c -o lib${LIBNAME}.so
-
-static_lib:
-	${CC} -c *.c
-	${AR} -rcs lib${LIBNAME}.a *.o
-
-install: 
-	cp -rf lib${LIBNAME}.* ${INSTPATH}
-	cp -rf *.h ${INSTPATH}
-
-uninstall:
-	rm -f ${INSTPATH}/lib${LIBNAME}.*
-	rm -f ${INSTPATH}/*.h
-
-clear:
-	rm -f *.o
-
-clean: clear
-	rm -f lib${LIBNAME}.* 
-
diff --git a/ch7_library/test/main.c b/ch7_library/test/main.c
deleted file mode 100644
index 4ddc4f1..0000000
--- a/ch7_library/test/main.c
+++ /dev/null
@@ -1,43 +0,0 @@
-/*********************************************************************************
- *      Copyright:  (C) 2018 LingYun IoT System Studio
- *                  All rights reserved.
- *
- *       Filename:  main.c
- *    Description:  This file is LingYun Crypto library test program
- *                 
- *        Version:  1.0.0(2018年12月20日)
- *         Author:  Guo Wenxue <guowenxue@gmail.com>
- *      ChangeLog:  1, Release initial version on "2018年12月20日 12时33分32秒"
- *                 
- ********************************************************************************/
-
-#include <stdio.h>
-#include <string.h>
-#include "crypto.h"
-
-int main(int argc, char *argv[])
-{
-    char            *message="abcdefghijklmnopq";
-    char             ciphertext[64];
-    char             plaintext[64];
-
-    memset(ciphertext, 0, sizeof(ciphertext));
-    if( encrypt(message, ciphertext, sizeof(ciphertext))< 0 )
-    {
-        printf("encrypt plaintext failure\n");
-        return -1;
-    }
-    printf("encrypt ciphertext: %s\n", ciphertext);
-
-
-    memset(plaintext, 0, sizeof(plaintext));
-    if( decrypt(ciphertext, plaintext, sizeof(plaintext))< 0 )
-    {
-        printf("decrypt ciphertext failure\n");
-        return -1;
-    }
-    printf("decrypt  plaintext: %s\n", plaintext);
-
-    
-    return 0;
-}
diff --git a/ch7_library/test/makefile b/ch7_library/test/makefile
deleted file mode 100644
index efa87a1..0000000
--- a/ch7_library/test/makefile
+++ /dev/null
@@ -1,18 +0,0 @@
-APPNAME=test
-LIBPATH=`pwd`/../lib/
-
-CFLAGS+=-I${LIBPATH}
-LDFLAGS+=-L${LIBPATH}
-LDFLAGS+=-static
-
-CC=gcc
-
-all:
-	${CC} ${CFLAGS} main.c -o ${APPNAME} ${LDFLAGS} -lmycrypto
-
-clean:
-	rm -f ${APPNAME}
-
-run:
-	export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:LIBPATH
-	./${APPNAME}
diff --git a/lib_makefile/encrypto/encrypto.c b/lib_makefile/encrypto/encrypto.c
new file mode 100644
index 0000000..bed4ee1
--- /dev/null
+++ b/lib_makefile/encrypto/encrypto.c
@@ -0,0 +1,66 @@
+/*********************************************************************************
+ *      Copyright:  (C) 2020 LingYun IoT System Studio
+ *                  All rights reserved.
+ *
+ *       Filename:  encrypto.c
+ *    Description:  This file 
+ *                 
+ *        Version:  1.0.0(2020年04月04日)
+ *         Author:  Guo Wenxue <guowenxue@gmail.com>
+ *      ChangeLog:  1, Release initial version on "2020年04月04日 21时02分36秒"
+ *                 
+ ********************************************************************************/
+
+#include <stdio.h>
+#include <string.h>
+
+#include "encrypto.h"
+
+int       g_var = 0;
+
+static int test(void)
+{
+    return 0;
+}
+
+int encrypto(char *str, char *ciphertxt, int size) 
+{
+    int i;
+
+    test();
+
+    if(!str || !ciphertxt || size<strlen(str))
+    {
+        printf("%s:%d %s(): Invalid input arguments\n", __FILE__, __LINE__, __FUNCTION__);
+        return -1;
+    }
+
+    for(i=0; i<strlen(str); i++)
+    {
+        ciphertxt[i]=str[i]+1;
+    }
+
+
+    g_var ++;
+
+    return 0;
+}
+
+
+int decrypto(char *ciphertxt, char *text, int size)
+{
+    int i;
+
+    if( !ciphertxt || !text || size<strlen(ciphertxt) )
+    {
+        printf("%s:%d %s(): Invalid input arguments\n", __FILE__, __LINE__, __FUNCTION__);
+        return -1;
+    }
+
+    for(i=0; i<strlen(ciphertxt); i++)
+    {
+        text[i]=ciphertxt[i]-1;
+    }
+
+    return 0;
+}
diff --git a/lib_makefile/encrypto/encrypto.h b/lib_makefile/encrypto/encrypto.h
new file mode 100644
index 0000000..9003173
--- /dev/null
+++ b/lib_makefile/encrypto/encrypto.h
@@ -0,0 +1,23 @@
+/********************************************************************************
+ *      Copyright:  (C) 2020 LingYun IoT System Studio
+ *                  All rights reserved.
+ *
+ *       Filename:  encrypto.h
+ *    Description:  This head file 
+ *
+ *        Version:  1.0.0(2020年04月04日)
+ *         Author:  Guo Wenxue <guowenxue@gmail.com>
+ *      ChangeLog:  1, Release initial version on "2020年04月04日 21时13分05秒"
+ *                 
+ ********************************************************************************/
+#ifndef  _ENCRYPTO_H_
+#define  _ENCRYPTO_H_
+
+extern int       g_var ;
+
+int encrypto(char *str, char *ciphertxt, int size);
+       
+int decrypto(char *ciphertxt, char *text, int size);
+
+
+#endif   /* ----- #ifndef _ENCRYPTO_H_  ----- */
diff --git a/lib_makefile/encrypto/makefile b/lib_makefile/encrypto/makefile
new file mode 100644
index 0000000..bd8cc4c
--- /dev/null
+++ b/lib_makefile/encrypto/makefile
@@ -0,0 +1,22 @@
+LIBNAME=libencrypto
+
+CC=${CROSSTOOL}gcc
+LD=${CROSSTOOL}ld
+AS=${CROSSTOOL}as
+AR=${CROSSTOOL}ar
+
+all: liba
+	@rm -f *.o
+
+libso:
+	${CC} -fPIC -shared *.c -o ${LIBNAME}.so
+
+liba:
+	${CC} -c *.c
+	${AR} -rcs ${LIBNAME}.a *.o
+
+clean:
+	rm -f ${LIBNAME}.*
+
+distclean: clean
+	rm -f *.o
diff --git a/lib_makefile/main.c b/lib_makefile/main.c
new file mode 100644
index 0000000..76f4646
--- /dev/null
+++ b/lib_makefile/main.c
@@ -0,0 +1,32 @@
+/*********************************************************************************
+ *      Copyright:  (C) 2020 LingYun IoT System Studio
+ *                  All rights reserved.
+ *
+ *       Filename:  main.c
+ *    Description:  This file 
+ *                 
+ *        Version:  1.0.0(2020年04月04日)
+ *         Author:  Guo Wenxue <guowenxue@gmail.com>
+ *      ChangeLog:  1, Release initial version on "2020年04月04日 20时57分10秒"
+ *                 
+ ********************************************************************************/
+
+#include <stdio.h>
+#include "encrypto.h"
+
+int main (int argc, char *argv[])
+{
+    char    *str="hello world"; 
+    char     ciphertxt[128];
+    char     text[128];
+
+    encrypto(str, ciphertxt, 128);
+    printf("cipher text: %s\r\n", ciphertxt);
+
+    decrypto(ciphertxt, text, 128);
+    printf("text: %s\r\n", text);
+
+
+    return 0;
+} 
+
diff --git a/lib_makefile/makefile b/lib_makefile/makefile
new file mode 100644
index 0000000..c4f1f13
--- /dev/null
+++ b/lib_makefile/makefile
@@ -0,0 +1,34 @@
+
+APP_NAME=test
+
+CROSSTOOL?=/opt/rpi/arm-bcm2708/arm-linux-gnueabihf/bin/arm-linux-
+CC=${CROSSTOOL}gcc
+LD=${CROSSTOOL}ld
+AS=${CROSSTOOL}as
+AR=${CROSSTOOL}ar
+
+# Compile flags
+CFLAGS=-Wall -Werror
+CFLAGS+=-Iencrypto
+
+# Linker flags
+LDFLAGS=-Lencrypto -lencrypto
+
+
+all: banner libencrypto
+	${CC} ${CFLAGS} main.c -o ${APP_NAME} ${LDFLAGS}
+
+banner:
+	@echo "Start to compile ${SRC} by ${CC}"
+
+libencrypto:
+	@make CROSSTOOL=${CROSSTOOL} -C encrypto
+
+clean: 
+	@make clean -C encrypto
+	rm -f ${APP_NAME}
+
+distclean: clean
+	@make distclean -C encrypto
+	rm -f *.o
+

--
Gitblit v1.9.1