diff options
author | irungentoo <irungentoo@gmail.com> | 2013-07-26 17:48:12 -0700 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2013-07-26 17:48:12 -0700 |
commit | 85b2db2ad2349e86605993f52d199d2797aec2da (patch) | |
tree | 1d8a4af2533a2790cbd9d170c5c4164d2726e46a | |
parent | e6a33fede62f54b8d925a8d4b986581b3afddc0b (diff) | |
parent | a1b93c397c8776e1bb07e3e01110d0ee5b9d31b9 (diff) |
Merge pull request #112 from KostyaKow/master
took out several strlen() calls out of the loop; replaced int's with siz...
-rw-r--r-- | other/DHT_bootstrap.c | 8 | ||||
-rw-r--r-- | testing/DHT_cryptosendfiletest.c | 11 | ||||
-rw-r--r-- | testing/DHT_test.c | 11 | ||||
-rw-r--r-- | testing/Messenger_test.c | 8 | ||||
-rw-r--r-- | testing/nTox.c | 24 |
5 files changed, 36 insertions, 26 deletions
diff --git a/other/DHT_bootstrap.c b/other/DHT_bootstrap.c index 9d94fecc..8942c237 100644 --- a/other/DHT_bootstrap.c +++ b/other/DHT_bootstrap.c | |||
@@ -41,12 +41,14 @@ | |||
41 | 41 | ||
42 | #define PORT 33445 | 42 | #define PORT 33445 |
43 | 43 | ||
44 | //TODO: rewrite | ||
44 | unsigned char * hex_string_to_bin(char hex_string[]) | 45 | unsigned char * hex_string_to_bin(char hex_string[]) |
45 | { | 46 | { |
46 | unsigned char * val = malloc(strlen(hex_string)); | 47 | size_t len = strlen(hex_string); |
48 | unsigned char * val = malloc(len); | ||
47 | char * pos = hex_string; | 49 | char * pos = hex_string; |
48 | int i=0; | 50 | int i=0; |
49 | while(i < strlen(hex_string)) | 51 | while(i < len) |
50 | { | 52 | { |
51 | sscanf(pos,"%2hhx",&val[i]); | 53 | sscanf(pos,"%2hhx",&val[i]); |
52 | pos+=2; | 54 | pos+=2; |
@@ -139,4 +141,4 @@ int main(int argc, char *argv[]) | |||
139 | } | 141 | } |
140 | shutdown_networking(); | 142 | shutdown_networking(); |
141 | return 0; | 143 | return 0; |
142 | } \ No newline at end of file | 144 | } |
diff --git a/testing/DHT_cryptosendfiletest.c b/testing/DHT_cryptosendfiletest.c index b06ddea5..5c3a0256 100644 --- a/testing/DHT_cryptosendfiletest.c +++ b/testing/DHT_cryptosendfiletest.c | |||
@@ -60,14 +60,15 @@ void printip(IP_Port ip_port) | |||
60 | printf("\nIP: %u.%u.%u.%u Port: %u\n",ip_port.ip.c[0],ip_port.ip.c[1],ip_port.ip.c[2],ip_port.ip.c[3],ntohs(ip_port.port)); | 60 | printf("\nIP: %u.%u.%u.%u Port: %u\n",ip_port.ip.c[0],ip_port.ip.c[1],ip_port.ip.c[2],ip_port.ip.c[3],ntohs(ip_port.port)); |
61 | } | 61 | } |
62 | 62 | ||
63 | /* horrible function from one of my first C programs. | 63 | //TODO: rewrite |
64 | *only here because I was too lazy to write a proper one. */ | ||
65 | unsigned char * hex_string_to_bin(char hex_string[]) | 64 | unsigned char * hex_string_to_bin(char hex_string[]) |
66 | { | 65 | { |
67 | unsigned char * val = malloc(strlen(hex_string)); | 66 | size_t len = strlen(hex_string); |
67 | unsigned char * val = malloc(len); | ||
68 | char * pos = hex_string; | 68 | char * pos = hex_string; |
69 | int i=0; | 69 | int i=0; |
70 | while(i < strlen(hex_string)) { | 70 | while(i < len) |
71 | { | ||
71 | sscanf(pos,"%2hhx",&val[i]); | 72 | sscanf(pos,"%2hhx",&val[i]); |
72 | pos+=2; | 73 | pos+=2; |
73 | i++; | 74 | i++; |
@@ -239,4 +240,4 @@ int main(int argc, char *argv[]) | |||
239 | 240 | ||
240 | shutdown_networking(); | 241 | shutdown_networking(); |
241 | return 0; | 242 | return 0; |
242 | } \ No newline at end of file | 243 | } |
diff --git a/testing/DHT_test.c b/testing/DHT_test.c index bb482595..d14e1577 100644 --- a/testing/DHT_test.c +++ b/testing/DHT_test.c | |||
@@ -114,14 +114,15 @@ void printpacket(uint8_t * data, uint32_t length, IP_Port ip_port) | |||
114 | printf("\n--------------------END-----------------------------\n\n\n"); | 114 | printf("\n--------------------END-----------------------------\n\n\n"); |
115 | } | 115 | } |
116 | 116 | ||
117 | //horrible function from one of my first C programs. | 117 | //TODO: rewrite |
118 | //only here because I was too lazy to write a proper one. | ||
119 | unsigned char * hex_string_to_bin(char hex_string[]) | 118 | unsigned char * hex_string_to_bin(char hex_string[]) |
120 | { | 119 | { |
121 | unsigned char * val = malloc(strlen(hex_string)); | 120 | size_t len = strlen(hex_string); |
121 | unsigned char * val = malloc(len); | ||
122 | char * pos = hex_string; | 122 | char * pos = hex_string; |
123 | int i=0; | 123 | int i=0; |
124 | while(i < strlen(hex_string)) { | 124 | while(i < len) |
125 | { | ||
125 | sscanf(pos,"%2hhx",&val[i]); | 126 | sscanf(pos,"%2hhx",&val[i]); |
126 | pos+=2; | 127 | pos+=2; |
127 | i++; | 128 | i++; |
@@ -191,4 +192,4 @@ int main(int argc, char *argv[]) | |||
191 | 192 | ||
192 | shutdown_networking(); | 193 | shutdown_networking(); |
193 | return 0; | 194 | return 0; |
194 | } \ No newline at end of file | 195 | } |
diff --git a/testing/Messenger_test.c b/testing/Messenger_test.c index 19ab4cc3..f38eb962 100644 --- a/testing/Messenger_test.c +++ b/testing/Messenger_test.c | |||
@@ -50,14 +50,14 @@ | |||
50 | 50 | ||
51 | #endif | 51 | #endif |
52 | 52 | ||
53 | //horrible function from one of my first C programs. | 53 | //TODO: rewrite |
54 | //only here because I was too lazy to write a proper one. | ||
55 | unsigned char * hex_string_to_bin(char hex_string[]) | 54 | unsigned char * hex_string_to_bin(char hex_string[]) |
56 | { | 55 | { |
57 | unsigned char * val = malloc(strlen(hex_string)); | 56 | size_t len = strlen(hex_string); |
57 | unsigned char * val = malloc(len); | ||
58 | char * pos = hex_string; | 58 | char * pos = hex_string; |
59 | int i=0; | 59 | int i=0; |
60 | while(i < strlen(hex_string)) | 60 | while(i < len) |
61 | { | 61 | { |
62 | sscanf(pos,"%2hhx",&val[i]); | 62 | sscanf(pos,"%2hhx",&val[i]); |
63 | pos+=2; | 63 | pos+=2; |
diff --git a/testing/nTox.c b/testing/nTox.c index caa7d45c..3a5e9bb3 100644 --- a/testing/nTox.c +++ b/testing/nTox.c | |||
@@ -46,12 +46,15 @@ void new_lines(char *line) | |||
46 | do_refresh(); | 46 | do_refresh(); |
47 | } | 47 | } |
48 | 48 | ||
49 | //TODO: rewrite | ||
49 | unsigned char * hex_string_to_bin(char hex_string[]) | 50 | unsigned char * hex_string_to_bin(char hex_string[]) |
50 | { | 51 | { |
51 | unsigned char * val = malloc(strlen(hex_string)); | 52 | size_t len = strlen(hex_string); |
53 | unsigned char * val = malloc(len); | ||
52 | char * pos = hex_string; | 54 | char * pos = hex_string; |
53 | int i=0; | 55 | int i=0; |
54 | while(i < strlen(hex_string)) { | 56 | while(i < len) |
57 | { | ||
55 | sscanf(pos,"%2hhx",&val[i]); | 58 | sscanf(pos,"%2hhx",&val[i]); |
56 | pos+=2; | 59 | pos+=2; |
57 | i++; | 60 | i++; |
@@ -80,7 +83,7 @@ void line_eval(char lines[HISTORY][STRING_LENGTH], char *line) | |||
80 | doMessenger(); | 83 | doMessenger(); |
81 | } else if (line[1] == 'm') { //message command: /m friendnumber messsage | 84 | } else if (line[1] == 'm') { //message command: /m friendnumber messsage |
82 | int i; | 85 | int i; |
83 | int len = strlen(line); | 86 | size_t len = strlen(line); |
84 | char numstring[len-3]; | 87 | char numstring[len-3]; |
85 | char message[len-3]; | 88 | char message[len-3]; |
86 | for (i=0; i<len; i++) { | 89 | for (i=0; i<len; i++) { |
@@ -99,7 +102,8 @@ void line_eval(char lines[HISTORY][STRING_LENGTH], char *line) | |||
99 | } else if (line[1] == 'n') { | 102 | } else if (line[1] == 'n') { |
100 | uint8_t name[MAX_NAME_LENGTH]; | 103 | uint8_t name[MAX_NAME_LENGTH]; |
101 | int i = 0; | 104 | int i = 0; |
102 | for (i=3; i<strlen(line); i++) { | 105 | size_t len = strlen(line); |
106 | for (i=3; i<len; i++) { | ||
103 | if (line[i] == 0 || line[i] == '\n') break; | 107 | if (line[i] == 0 || line[i] == '\n') break; |
104 | name[i - 3] = line[i]; | 108 | name[i - 3] = line[i]; |
105 | } | 109 | } |
@@ -111,7 +115,8 @@ void line_eval(char lines[HISTORY][STRING_LENGTH], char *line) | |||
111 | } else if (line[1] == 's') { | 115 | } else if (line[1] == 's') { |
112 | uint8_t status[MAX_USERSTATUS_LENGTH]; | 116 | uint8_t status[MAX_USERSTATUS_LENGTH]; |
113 | int i = 0; | 117 | int i = 0; |
114 | for (i=3; i<strlen(line); i++) { | 118 | size_t len = strlen(line); |
119 | for (i=3; i<len; i++) { | ||
115 | if (line[i] == 0 || line[i] == '\n') break; | 120 | if (line[i] == 0 || line[i] == '\n') break; |
116 | status[i - 3] = line[i]; | 121 | status[i - 3] = line[i]; |
117 | } | 122 | } |
@@ -133,7 +138,8 @@ void wrap(char output[STRING_LENGTH], char input[STRING_LENGTH], int line_width) | |||
133 | { | 138 | { |
134 | int i = 0; | 139 | int i = 0; |
135 | strcpy(output,input); | 140 | strcpy(output,input); |
136 | for (i=line_width; i < strlen(output); i = i + line_width) { | 141 | size_t len = strlen(output); |
142 | for (i=line_width; i < len; i = i + line_width) { | ||
137 | while (output[i] != ' ' && i != 0) { | 143 | while (output[i] != ' ' && i != 0) { |
138 | i--; | 144 | i--; |
139 | } | 145 | } |
@@ -145,7 +151,7 @@ void wrap(char output[STRING_LENGTH], char input[STRING_LENGTH], int line_width) | |||
145 | 151 | ||
146 | int count_lines(char *string) | 152 | int count_lines(char *string) |
147 | { | 153 | { |
148 | int len = strlen(string); | 154 | size_t len = strlen(string); |
149 | int i; | 155 | int i; |
150 | int count = 1; | 156 | int count = 1; |
151 | for (i=0; i < len; i++) { | 157 | for (i=0; i < len; i++) { |
@@ -158,7 +164,7 @@ int count_lines(char *string) | |||
158 | 164 | ||
159 | char *appender(char *str, const char c) | 165 | char *appender(char *str, const char c) |
160 | { | 166 | { |
161 | int len = strlen(str); | 167 | size_t len = strlen(str); |
162 | if (len < STRING_LENGTH) { | 168 | if (len < STRING_LENGTH) { |
163 | str[len + 1] = str[len]; | 169 | str[len + 1] = str[len]; |
164 | str[len] = c; | 170 | str[len] = c; |
@@ -216,7 +222,7 @@ void print_message(int friendnumber, uint8_t * string, uint16_t length) | |||
216 | time ( &rawtime ); | 222 | time ( &rawtime ); |
217 | timeinfo = localtime ( &rawtime ); | 223 | timeinfo = localtime ( &rawtime ); |
218 | char* temp = asctime(timeinfo); | 224 | char* temp = asctime(timeinfo); |
219 | int len = strlen(temp); | 225 | size_t len = strlen(temp); |
220 | temp[len-1]='\0'; | 226 | temp[len-1]='\0'; |
221 | sprintf(msg, "[%d] %s <%s> %s", friendnumber, temp, name, string); // someone please fix this | 227 | sprintf(msg, "[%d] %s <%s> %s", friendnumber, temp, name, string); // someone please fix this |
222 | new_lines(msg); | 228 | new_lines(msg); |