From b848def42914e3e9a4e4efcd247d4fcc84c172a4 Mon Sep 17 00:00:00 2001 From: Jo-Philipp Wich Date: Tue, 7 Apr 2009 02:02:15 +0000 Subject: [PATCH] contrib/lar: - unify code style and data types - optimizations for string operations --- contrib/lar/lar.c | 45 +++++++++-------- .../lar/openwrt/050-lar-source-loader.patch | 49 +++++++++---------- 2 files changed, 46 insertions(+), 48 deletions(-) diff --git a/contrib/lar/lar.c b/contrib/lar/lar.c index 6bac9d6b8..f968634af 100644 --- a/contrib/lar/lar.c +++ b/contrib/lar/lar.c @@ -31,7 +31,8 @@ lar_index * lar_get_index( lar_archive *ar ) uint32_t i; uint32_t idx_offset; uint32_t idx_length; - lar_index *idx_map, *idx_ptr; + lar_index *idx_map; + lar_index *idx_ptr; if( lseek(ar->fd, -(sizeof(idx_offset)), SEEK_END) == -1 ) LAR_DIE("Unable to seek to end of archive"); @@ -45,9 +46,7 @@ lar_index * lar_get_index( lar_archive *ar ) idx_map = NULL; - for( i = 0; i < idx_length; \ - i += (sizeof(lar_index) - sizeof(char *)) - ) { + for( i = 0; i < idx_length; i += (sizeof(lar_index) - sizeof(char *)) ) { idx_ptr = (lar_index *)malloc(sizeof(lar_index)); lar_read32(ar->fd, &idx_ptr->noffset); @@ -64,9 +63,8 @@ lar_index * lar_get_index( lar_archive *ar ) return idx_map; } -uint32_t lar_get_filename( lar_archive *ar, - lar_index *idx_ptr, char *filename -) { +uint32_t lar_get_filename( lar_archive *ar, lar_index *idx_ptr, char *filename ) +{ if( idx_ptr->nlength >= LAR_FNAME_BUFFER ) LAR_DIE("Filename exceeds maximum allowed length"); @@ -76,7 +74,7 @@ uint32_t lar_get_filename( lar_archive *ar, if( read(ar->fd, filename, idx_ptr->nlength) < idx_ptr->nlength ) LAR_DIE("Unexpected EOF while reading filename"); - filename[idx_ptr->nlength] = 0; + filename[idx_ptr->nlength] = '\0'; return idx_ptr->nlength; } @@ -85,9 +83,10 @@ lar_member * lar_open_member( lar_archive *ar, const char *name ) { lar_index *idx_ptr = ar->index; lar_member *member; - char memberfile[LAR_FNAME_BUFFER]; char *memberdata; - size_t pgsz = getpagesize(); + size_t pgof; + size_t pgsz = getpagesize(); + LAR_FNAME(memberfile); while(idx_ptr) { @@ -95,10 +94,11 @@ lar_member * lar_open_member( lar_archive *ar, const char *name ) if( !strncmp(memberfile, name, idx_ptr->nlength) ) { + pgof = ( idx_ptr->foffset % pgsz ); + memberdata = mmap( - 0, idx_ptr->flength + ( idx_ptr->foffset % pgsz ), - PROT_READ, MAP_PRIVATE, ar->fd, - idx_ptr->foffset - ( idx_ptr->foffset % pgsz ) + 0, idx_ptr->flength + pgof, PROT_READ, MAP_PRIVATE, + ar->fd, idx_ptr->foffset - pgof ); if( memberdata == MAP_FAILED ) @@ -108,10 +108,10 @@ lar_member * lar_open_member( lar_archive *ar, const char *name ) member->type = idx_ptr->type; member->flags = idx_ptr->flags; member->length = idx_ptr->flength; - member->data = &memberdata[idx_ptr->foffset % pgsz]; + member->data = &memberdata[pgof]; member->mmap = memberdata; - member->mlen = idx_ptr->flength + ( idx_ptr->foffset % pgsz ); + member->mlen = idx_ptr->flength + pgof; return member; } @@ -176,10 +176,11 @@ int lar_close( lar_archive *ar ) lar_archive * lar_find_archive( const char *package, const char *path ) { - int seg = 1; - int len = 0; - int pln = 0; - int i, j; + uint32_t i; + uint32_t j; + uint32_t seg = 1; + uint32_t len = 0; + uint32_t pln = 0; struct stat s; LAR_FNAME(buffer); @@ -211,8 +212,7 @@ lar_archive * lar_find_archive( const char *package, const char *path ) buffer[pln+i] = ( package[i] == '.' ) ? LAR_DIRSEP : package[i]; } - buffer[pln+i+0] = '.'; buffer[pln+i+1] = 'l'; buffer[pln+i+2] = 'a'; - buffer[pln+i+3] = 'r'; buffer[pln+i+4] = '\0'; + strcpy(&buffer[pln+i], ".lar"); if( (stat(buffer, &s) > -1) && (s.st_mode & S_IFREG) ) return lar_open(buffer); @@ -236,8 +236,7 @@ lar_member * lar_find_member( lar_archive *ar, const char *package ) buffer[len] = ( package[len] == '.' ) ? '/' : package[len]; } - buffer[len+0] = '.'; buffer[len+1] = 'l'; buffer[len+2] = 'u'; - buffer[len+3] = 'a'; buffer[len+4] = '\0'; + strcpy(&buffer[len], ".lua"); return lar_open_member(ar, buffer); } diff --git a/contrib/lar/openwrt/050-lar-source-loader.patch b/contrib/lar/openwrt/050-lar-source-loader.patch index fbf21f7be..c17c2f5c8 100644 --- a/contrib/lar/openwrt/050-lar-source-loader.patch +++ b/contrib/lar/openwrt/050-lar-source-loader.patch @@ -12,8 +12,8 @@ diff -Nurb lua-5.1.4.orig/src/Makefile lua-5.1.4/src/Makefile LUA_O= lua.o diff -Nurb lua-5.1.4.orig/src/lar.c lua-5.1.4/src/lar.c --- lua-5.1.4.orig/src/lar.c 1970-01-01 01:00:00.000000000 +0100 -+++ lua-5.1.4/src/lar.c 2009-04-07 00:35:55.000000000 +0200 -@@ -0,0 +1,243 @@ ++++ lua-5.1.4/src/lar.c 2009-04-07 03:53:29.000000000 +0200 +@@ -0,0 +1,242 @@ +#include "lar.h" + +int lar_read32( int fd, uint32_t *val ) @@ -47,7 +47,8 @@ diff -Nurb lua-5.1.4.orig/src/lar.c lua-5.1.4/src/lar.c + uint32_t i; + uint32_t idx_offset; + uint32_t idx_length; -+ lar_index *idx_map, *idx_ptr; ++ lar_index *idx_map; ++ lar_index *idx_ptr; + + if( lseek(ar->fd, -(sizeof(idx_offset)), SEEK_END) == -1 ) + LAR_DIE("Unable to seek to end of archive"); @@ -61,9 +62,7 @@ diff -Nurb lua-5.1.4.orig/src/lar.c lua-5.1.4/src/lar.c + + idx_map = NULL; + -+ for( i = 0; i < idx_length; \ -+ i += (sizeof(lar_index) - sizeof(char *)) -+ ) { ++ for( i = 0; i < idx_length; i += (sizeof(lar_index) - sizeof(char *)) ) { + idx_ptr = (lar_index *)malloc(sizeof(lar_index)); + + lar_read32(ar->fd, &idx_ptr->noffset); @@ -80,9 +79,8 @@ diff -Nurb lua-5.1.4.orig/src/lar.c lua-5.1.4/src/lar.c + return idx_map; +} + -+uint32_t lar_get_filename( lar_archive *ar, -+ lar_index *idx_ptr, char *filename -+) { ++uint32_t lar_get_filename( lar_archive *ar, lar_index *idx_ptr, char *filename ) ++{ + if( idx_ptr->nlength >= LAR_FNAME_BUFFER ) + LAR_DIE("Filename exceeds maximum allowed length"); + @@ -92,7 +90,7 @@ diff -Nurb lua-5.1.4.orig/src/lar.c lua-5.1.4/src/lar.c + if( read(ar->fd, filename, idx_ptr->nlength) < idx_ptr->nlength ) + LAR_DIE("Unexpected EOF while reading filename"); + -+ filename[idx_ptr->nlength] = 0; ++ filename[idx_ptr->nlength] = '\0'; + + return idx_ptr->nlength; +} @@ -101,9 +99,10 @@ diff -Nurb lua-5.1.4.orig/src/lar.c lua-5.1.4/src/lar.c +{ + lar_index *idx_ptr = ar->index; + lar_member *member; -+ char memberfile[LAR_FNAME_BUFFER]; + char *memberdata; -+ size_t pgsz = getpagesize(); ++ size_t pgof; ++ size_t pgsz = getpagesize(); ++ LAR_FNAME(memberfile); + + while(idx_ptr) + { @@ -111,10 +110,11 @@ diff -Nurb lua-5.1.4.orig/src/lar.c lua-5.1.4/src/lar.c + + if( !strncmp(memberfile, name, idx_ptr->nlength) ) + { ++ pgof = ( idx_ptr->foffset % pgsz ); ++ + memberdata = mmap( -+ 0, idx_ptr->flength + ( idx_ptr->foffset % pgsz ), -+ PROT_READ, MAP_PRIVATE, ar->fd, -+ idx_ptr->foffset - ( idx_ptr->foffset % pgsz ) ++ 0, idx_ptr->flength + pgof, PROT_READ, MAP_PRIVATE, ++ ar->fd, idx_ptr->foffset - pgof + ); + + if( memberdata == MAP_FAILED ) @@ -124,10 +124,10 @@ diff -Nurb lua-5.1.4.orig/src/lar.c lua-5.1.4/src/lar.c + member->type = idx_ptr->type; + member->flags = idx_ptr->flags; + member->length = idx_ptr->flength; -+ member->data = &memberdata[idx_ptr->foffset % pgsz]; ++ member->data = &memberdata[pgof]; + + member->mmap = memberdata; -+ member->mlen = idx_ptr->flength + ( idx_ptr->foffset % pgsz ); ++ member->mlen = idx_ptr->flength + pgof; + + return member; + } @@ -192,10 +192,11 @@ diff -Nurb lua-5.1.4.orig/src/lar.c lua-5.1.4/src/lar.c + +lar_archive * lar_find_archive( const char *package, const char *path ) +{ -+ int seg = 1; -+ int len = 0; -+ int pln = 0; -+ int i, j; ++ uint32_t i; ++ uint32_t j; ++ uint32_t seg = 1; ++ uint32_t len = 0; ++ uint32_t pln = 0; + struct stat s; + LAR_FNAME(buffer); + @@ -227,8 +228,7 @@ diff -Nurb lua-5.1.4.orig/src/lar.c lua-5.1.4/src/lar.c + buffer[pln+i] = ( package[i] == '.' ) ? LAR_DIRSEP : package[i]; + } + -+ buffer[pln+i+0] = '.'; buffer[pln+i+1] = 'l'; buffer[pln+i+2] = 'a'; -+ buffer[pln+i+3] = 'r'; buffer[pln+i+4] = '\0'; ++ strcpy(&buffer[pln+i], ".lar"); + + if( (stat(buffer, &s) > -1) && (s.st_mode & S_IFREG) ) + return lar_open(buffer); @@ -252,8 +252,7 @@ diff -Nurb lua-5.1.4.orig/src/lar.c lua-5.1.4/src/lar.c + buffer[len] = ( package[len] == '.' ) ? '/' : package[len]; + } + -+ buffer[len+0] = '.'; buffer[len+1] = 'l'; buffer[len+2] = 'u'; -+ buffer[len+3] = 'a'; buffer[len+4] = '\0'; ++ strcpy(&buffer[len], ".lua"); + + return lar_open_member(ar, buffer); +} -- 2.25.1